Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(conversation): surface terminal errors on assistant turns that already streamed content (#16086)

Arvin Xu330694fe4991
diff --git a/src/features/Conversation/ChatItem/components/ErrorContent.test.tsx b/src/features/Conversation/ChatItem/components/ErrorContent.test.tsx
new file mode 100644
index 00000000000..f4ef95313c0
--- /dev/null
+++ b/src/features/Conversation/ChatItem/components/ErrorContent.test.tsx
@@ -0,0 +1,72 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { fireEvent, render, screen } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import ErrorContent from './ErrorContent';
+
+const deleteMessageMock = vi.fn();
+const updateMessageErrorMock = vi.fn();
+let messageContent: string | undefined = '';
+
+// Drive the Alert's `afterClose` directly via a click, so we exercise
+// ErrorContent's dismiss branching without the real close animation.
+vi.mock('@lobehub/ui', () => ({
+ Alert: ({ afterClose }: { afterClose?: () => void }) => (
+ <button type="button" onClick={() => afterClose?.()}>
+ close
+ </button>
+ ),
+ Skeleton: { Button: () => <div>loading</div> },
+}));
+
+vi.mock('antd', () => ({
+ Button: ({ children }: { children?: ReactNode }) => <span>{children}</span>,
+}));
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock('@/features/Conversation', () => ({
+ useConversationStore: (selector: (s: unknown) => unknown) =>
+ selector({
+ deleteMessage: deleteMessageMock,
+ updateMessageError: updateMessageErrorMock,
+ }),
+}));
+
+vi.mock('@/features/Conversation/store', () => ({
+ dataSelectors: {
+ getDisplayMessageById: (id: string) => () => ({ content: messageContent, id }),
+ },
+}));
+
+describe('ErrorContent dismiss behavior', () => {
+ beforeEach(() => {
+ deleteMessageMock.mockClear();
+ updateMessageErrorMock.mockClear();
+ });
+
+ it('clears only the error (keeps the message) when the turn already streamed content', () => {
+ messageContent = 'already streamed text';
+ render(<ErrorContent error={{ message: 'boom' } as any} id="msg-1" />);
+
+ fireEvent.click(screen.getByText('close'));
+
+ expect(updateMessageErrorMock).toHaveBeenCalledWith('msg-1', null);
+ expect(deleteMessageMock).not.toHaveBeenCalled();
+ });
+
+ it('deletes the message when it is just an empty error', () => {
+ messageContent = '';
+ render(<ErrorContent error={{ message: 'boom' } as any} id="msg-1" />);
+
+ fireEvent.click(screen.getByText('close'));
+
+ expect(deleteMessageMock).toHaveBeenCalledWith('msg-1');
+ expect(updateMessageErrorMock).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/features/Conversation/ChatItem/components/ErrorContent.tsx b/src/features/Conversation/ChatItem/components/ErrorContent.tsx
index 38c935623a6..92e7027a8d9 100644
--- a/src/features/Conversation/ChatItem/components/ErrorContent.tsx
+++ b/src/features/Conversation/ChatItem/components/ErrorContent.tsx
@@ -5,6 +5,7 @@ import { memo, Suspense } from 'react';
import { useTranslation } from 'react-i18next';
import { useConversationStore } from '@/features/Conversation';
+import { dataSelectors } from '@/features/Conversation/store';
import { type ChatItemProps } from '../type';
@@ -17,7 +18,13 @@ export interface ErrorContentProps {
const ErrorContent = memo<ErrorContentProps>(({ customErrorRender, error, id, onRegenerate }) => {
const { t } = useTranslation('common');
- const [deleteMessage] = useConversationStore((s) => [s.deleteMessage]);
+ const [deleteMessage, updateMessageError] = useConversationStore((s) => [
+ s.deleteMessage,
+ s.updateMessageError,
+ ]);
+ const messageContent = useConversationStore((s) =>
+ id ? dataSelectors.getDisplayMessageById(id)(s)?.content : undefined,
+ );
if (!error) return;
@@ -51,7 +58,13 @@ const ErrorContent = memo<ErrorContentProps>(({ customErrorRender, error, id, on
title={error.message}
afterClose={() => {
error?.afterClose?.();
- if (id) {
+ if (!id) return;
+ // A turn can carry a terminal error on top of content it already
+ // streamed. Dismissing the error must not delete that content — just
+ // clear the error and keep the message.
+ if (messageContent && messageContent.trim() !== '') {
+ updateMessageError(id, null);
+ } else {
deleteMessage(id);
}
}}
diff --git a/src/features/Conversation/Messages/Assistant/index.tsx b/src/features/Conversation/Messages/Assistant/index.tsx
index 0e10626a23f..dea13ee3afa 100644
--- a/src/features/Conversation/Messages/Assistant/index.tsx
+++ b/src/features/Conversation/Messages/Assistant/index.tsx
@@ -66,14 +66,6 @@ const AssistantMessage = memo<AssistantMessageProps>(
const errorContent = useErrorContent(error);
- const shouldForceShowError =
- error?.type === 'ProviderBizError' &&
- (error?.body as any)?.provider === 'google' &&
- !!(
- (error?.body as any)?.context?.promptFeedback?.blockReason ||
- (error?.body as any)?.context?.finishReason
- );
-
// remove line breaks in artifact tag to make the ast transform easier
const message = !editing ? normalizeThinkTags(processWithArtifact(content)) : content;
@@ -106,6 +98,10 @@ const AssistantMessage = memo<AssistantMessageProps>(
belowMessage={hasEmptyErrorMessage ? footerRender : undefined}
customErrorRender={(error) => <ErrorMessageExtra data={item} error={error} />}
editing={editing}
+ // ChatItem renders this as the primary block when the message is empty,
+ // or inside messageExtra (below the content) when the turn streamed
+ // content before erroring — so don't gate it on empty content.
+ error={errorContent && error ? errorContent : undefined}
id={id}
loading={generating || isCreating}
message={message}
@@ -123,11 +119,6 @@ const AssistantMessage = memo<AssistantMessageProps>(
{!disableEditing && actionBarHolder}
</>
}
- error={
- errorContent && error && (message === LOADING_FLAT || !message || shouldForceShowError)
- ? errorContent
- : undefined
- }
messageExtra={
<>
{interrupted && <InterruptedHint />}
diff --git a/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.test.tsx b/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.test.tsx
index a418b682782..8be635ddb99 100644
--- a/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.test.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.test.tsx
@@ -144,4 +144,35 @@ describe('AssistantGroup ContentBlock', () => {
expect(screen.getByText('guide:claude-code:rate_limit')).toBeInTheDocument();
});
+
+ it('renders the error below the content when a turn errors after streaming content', () => {
+ render(
+ <ContentBlock
+ assistantId="assistant-1"
+ content="The assistant already wrote this before the turn died."
+ id="block-1"
+ error={
+ {
+ body: {
+ agentType: 'claude-code',
+ code: HeterogeneousAgentSessionErrorCode.RateLimit,
+ message: "You've hit your limit · resets 2:50pm (Asia/Shanghai)",
+ rateLimitInfo: {
+ rateLimitType: 'five_hour',
+ resetsAt: 1_778_741_400,
+ status: 'rejected',
+ },
+ stderr: "You've hit your limit · resets 2:50pm (Asia/Shanghai)",
+ },
+ message: "You've hit your limit · resets 2:50pm (Asia/Shanghai)",
+ type: 'AgentRuntimeError',
+ } as any
+ }
+ />,
+ );
+
+ // Content is preserved AND the error is surfaced, instead of being dropped.
+ expect(screen.getByText('message content')).toBeInTheDocument();
+ expect(screen.getByText('guide:claude-code:rate_limit')).toBeInTheDocument();
+ });
});
diff --git a/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.tsx b/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.tsx
index 31a0cdadab2..4c9611e7634 100644
--- a/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/components/ContentBlock.tsx
@@ -50,25 +50,25 @@ const ContentBlock = memo<ContentBlockProps>(
continueGeneration(assistantId);
}, [assistantId, continueGeneration, deleteMessage, id]);
+ const errorBlock = error ? (
+ <ErrorContent
+ error={errorContent && error ? errorContent : undefined}
+ id={id}
+ customErrorRender={(alertError) => (
+ <ErrorMessageExtra
+ data={{ error, id }}
+ error={alertError}
+ onRegenerate={handleRegenerate}
+ />
+ )}
+ onRegenerate={handleRegenerate}
+ />
+ ) : null;
+
+ // Nothing was streamed before the turn died: the error stands in for the
+ // whole block.
if (error && (content === LOADING_FLAT || !content)) {
- return (
- <ErrorContent
- id={id}
- customErrorRender={(alertError) => (
- <ErrorMessageExtra
- data={{ error, id }}
- error={alertError}
- onRegenerate={handleRegenerate}
- />
- )}
- error={
- errorContent && error && (content === LOADING_FLAT || !content)
- ? errorContent
- : undefined
- }
- onRegenerate={handleRegenerate}
- />
- );
+ return errorBlock;
}
return (
@@ -101,6 +101,11 @@ const ContentBlock = memo<ContentBlockProps>(
<Tools disableEditing={disableEditing} messageId={id} />
</SafeBoundary>
)}
+
+ {/* A terminal error (e.g. upstream overload) can land on a turn that
+ already streamed content + a successful tool call. Surface it below
+ the content instead of silently dropping it. */}
+ {errorBlock && <SafeBoundary>{errorBlock}</SafeBoundary>}
</Flexbox>
);
},