Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(ai): handle stream errors (#2171)

Eric Hayes8e670eee4f1a
diff --git a/js/app/packages/core/component/AI/component/message/ChatMessages.tsx b/js/app/packages/core/component/AI/component/message/ChatMessages.tsx
index 3a629c02a1..cbe7ebc9a9 100644
--- a/js/app/packages/core/component/AI/component/message/ChatMessages.tsx
+++ b/js/app/packages/core/component/AI/component/message/ChatMessages.tsx
@@ -1,6 +1,7 @@
import { useChatContext } from '@core/component/AI/context';
import type { ChatMessageWithAttachments } from '@core/component/AI/types';
import { asChatMessage } from '@core/component/AI/util/message';
+import { toast } from '@core/component/Toast/Toast';
import { StaticMarkdownContext } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
import { aiChatTheme } from '@core/component/LexicalMarkdown/theme';
import { PulsingStar } from '@entity/components/PulsingStar';
@@ -162,6 +163,26 @@ export function ChatMessages(props: ChatMessagesProps) {
if (!stream) return [];
return stream.data();
};
+ // handle stream error
+ createEffect(
+ on(streamData, (data) => {
+ const latest = data.at(-1);
+ if (!latest) return;
+ if (latest.type !== 'error') return;
+
+ const error = 'stream_error' in latest ? latest.stream_error : undefined;
+ if (error === 'model_context_overflow') {
+ toast.failure(
+ 'Too much context. Remove attachments or start a new chat'
+ );
+ } else {
+ toast.failure('Failed to respond to message');
+ }
+
+ // Clear stream so the isGenerating effect in Chat.tsx resets
+ chat.setStream(undefined);
+ })
+ );
// when a user message arrives via stream, update optimistic ID or append
createEffect(
on(streamData, (data) => {
diff --git a/js/app/packages/core/component/AI/util/handleError.ts b/js/app/packages/core/component/AI/util/handleError.ts
deleted file mode 100644
index 04a8b385f4..0000000000
--- a/js/app/packages/core/component/AI/util/handleError.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { toast } from '@core/component/Toast/Toast';
-import type { ChatMessageStream } from '@service-connection/stream';
-
-type Item = ReturnType<ChatMessageStream['data']>[number];
-type StreamError = Extract<Item, { type: 'error' }>;
-
-export function handleError(error: StreamError) {
- console.error(error);
- toast.failure('Failed to respond to message');
- // const { showPaywall } = usePaywallState();
- // if (error.error_type === "payment_required") {
- // showPaywall();
- // } else if (error.error_type === "model_context_overflow") {
- // toast.failure("Too much context. Remove attachments or start a new chat");
- // } else {
- // toast.failure("Failed to respond to message");
- // }
-}