From f9ac27e9a40cd6bcabbefde79a18f9d49321b1a3 Mon Sep 17 00:00:00 2001
From: Timothy Carambat <rambat1010@gmail.com>
Date: Tue, 16 Apr 2024 16:25:32 -0700
Subject: [PATCH] Handle Anthropic streamable errors (#1113)

---
 server/utils/AiProviders/anthropic/index.js | 24 ++++++++++++++++++-
 server/utils/chats/stream.js                | 26 ++++++++++++++-------
 2 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/server/utils/AiProviders/anthropic/index.js b/server/utils/AiProviders/anthropic/index.js
index 5e8e40a30..6a8ad3c42 100644
--- a/server/utils/AiProviders/anthropic/index.js
+++ b/server/utils/AiProviders/anthropic/index.js
@@ -138,7 +138,7 @@ class AnthropicLLM {
   async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
     if (!this.isValidChatCompletionModel(this.model))
       throw new Error(
-        `OpenAI chat: ${this.model} is not valid for chat completion!`
+        `Anthropic chat: ${this.model} is not valid for chat completion!`
       );
 
     const streamRequest = await this.anthropic.messages.stream({
@@ -163,6 +163,28 @@ class AnthropicLLM {
       const handleAbort = () => clientAbortedHandler(resolve, fullText);
       response.on("close", handleAbort);
 
+      stream.on("error", (event) => {
+        const parseErrorMsg = (event) => {
+          const error = event?.error?.error;
+          if (!!error)
+            return `Anthropic Error:${error?.type || "unknown"} ${
+              error?.message || "unknown error."
+            }`;
+          return event.message;
+        };
+
+        writeResponseChunk(response, {
+          uuid,
+          sources: [],
+          type: "abort",
+          textResponse: null,
+          close: true,
+          error: parseErrorMsg(event),
+        });
+        response.removeListener("close", handleAbort);
+        resolve(fullText);
+      });
+
       stream.on("streamEvent", (message) => {
         const data = message;
         if (
diff --git a/server/utils/chats/stream.js b/server/utils/chats/stream.js
index b5128c193..57025da30 100644
--- a/server/utils/chats/stream.js
+++ b/server/utils/chats/stream.js
@@ -205,20 +205,30 @@ async function streamChatWithWorkspace(
     });
   }
 
-  const { chat } = await WorkspaceChats.new({
-    workspaceId: workspace.id,
-    prompt: message,
-    response: { text: completeText, sources, type: chatMode },
-    threadId: thread?.id || null,
-    user,
-  });
+  if (completeText?.length > 0) {
+    const { chat } = await WorkspaceChats.new({
+      workspaceId: workspace.id,
+      prompt: message,
+      response: { text: completeText, sources, type: chatMode },
+      threadId: thread?.id || null,
+      user,
+    });
+
+    writeResponseChunk(response, {
+      uuid,
+      type: "finalizeResponseStream",
+      close: true,
+      error: false,
+      chatId: chat.id,
+    });
+    return;
+  }
 
   writeResponseChunk(response, {
     uuid,
     type: "finalizeResponseStream",
     close: true,
     error: false,
-    chatId: chat.id,
   });
   return;
 }
-- 
GitLab