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(chat): delete only the errored step of a heterogeneous run, not the whole turn (#16851)

Arvin Xuf52c52bd0a69
diff --git a/src/features/Conversation/Error/heterogeneous.ts b/src/features/Conversation/Error/heterogeneous.ts
new file mode 100644
index 00000000000..259d09dcb06
--- /dev/null
+++ b/src/features/Conversation/Error/heterogeneous.ts
@@ -0,0 +1,32 @@
+import {
+ type HeterogeneousAgentSessionError,
+ HeterogeneousAgentSessionErrorCode,
+} from '@lobechat/electron-client-ipc';
+
+/**
+ * Heterogeneous-agent (Claude Code / Codex) session errors that render the
+ * dedicated status-guide card (auth required, CLI missing, overload, rate
+ * limit). Kept in this dependency-free module so non-React callers (e.g. the
+ * message action bar) can branch on them without pulling the whole Error UI
+ * barrel.
+ */
+export const HETEROGENEOUS_AGENT_STATUS_GUIDE_ERROR_CODES = new Set<string>([
+ HeterogeneousAgentSessionErrorCode.AuthRequired,
+ HeterogeneousAgentSessionErrorCode.CliNotFound,
+ HeterogeneousAgentSessionErrorCode.Overloaded,
+ HeterogeneousAgentSessionErrorCode.RateLimit,
+]);
+
+export const isHeterogeneousAgentStatusGuideError = (
+ value: unknown,
+): value is HeterogeneousAgentSessionError => {
+ if (!value || typeof value !== 'object') return false;
+
+ const { agentType, code } = value as Partial<HeterogeneousAgentSessionError>;
+
+ return (
+ (agentType === 'claude-code' || agentType === 'codex') &&
+ typeof code === 'string' &&
+ HETEROGENEOUS_AGENT_STATUS_GUIDE_ERROR_CODES.has(code)
+ );
+};
diff --git a/src/features/Conversation/Error/index.tsx b/src/features/Conversation/Error/index.tsx
index e1e69893632..f5e14e9e947 100644
--- a/src/features/Conversation/Error/index.tsx
+++ b/src/features/Conversation/Error/index.tsx
@@ -1,4 +1,3 @@
-import type { HeterogeneousAgentSessionError } from '@lobechat/electron-client-ipc';
import { HeterogeneousAgentSessionErrorCode } from '@lobechat/electron-client-ipc';
import { type ILobeAgentRuntimeErrorType } from '@lobechat/model-runtime';
import { AgentRuntimeErrorType, getErrorCodeSpec } from '@lobechat/model-runtime';
@@ -23,8 +22,13 @@ import { serverConfigSelectors, useServerConfigStore } from '@/store/serverConfi
import { getRuntimeErrorMessage } from '@/utils/locale/runtimeErrorMessage';
import ChatInvalidAPIKey from './ChatInvalidApiKey';
+import { isHeterogeneousAgentStatusGuideError } from './heterogeneous';
import { useHeterogeneousAutoRetry } from './useHeterogeneousAutoRetry';
+// Re-export so existing barrel consumers (ContentBlock, message action bar) can
+// keep importing the guard from '@/features/Conversation/Error'.
+export { isHeterogeneousAgentStatusGuideError } from './heterogeneous';
+
interface ErrorMessageData {
error?: ChatMessageError | null;
id: string;
@@ -88,13 +92,6 @@ const QuotaLimitError = dynamic(() => import('./QuotaLimitError'), { loading, ss
const TraceIdError = dynamic(() => import('./TraceIdError'), { loading, ssr: false });
-const HETEROGENEOUS_AGENT_STATUS_GUIDE_ERROR_CODES = new Set<string>([
- HeterogeneousAgentSessionErrorCode.AuthRequired,
- HeterogeneousAgentSessionErrorCode.CliNotFound,
- HeterogeneousAgentSessionErrorCode.Overloaded,
- HeterogeneousAgentSessionErrorCode.RateLimit,
-]);
-
// `UnknownChatFetchError` is excluded: its localized copy is a generic
// "unknown error" message, so the trace-id report UI is strictly more useful.
const LEGACY_LOCALIZED_ERROR_TYPES = new Set<string>(
@@ -156,20 +153,6 @@ const shouldShowTraceIdError = (
return !hasLocalizedErrorMessage(errorType);
};
-export const isHeterogeneousAgentStatusGuideError = (
- value: unknown,
-): value is HeterogeneousAgentSessionError => {
- if (!value || typeof value !== 'object') return false;
-
- const { agentType, code } = value as Partial<HeterogeneousAgentSessionError>;
-
- return (
- (agentType === 'claude-code' || agentType === 'codex') &&
- typeof code === 'string' &&
- HETEROGENEOUS_AGENT_STATUS_GUIDE_ERROR_CODES.has(code)
- );
-};
-
// Config for the errorMessage display
const getErrorAlertConfig = (
errorType?: IToolErrorType | ILobeAgentRuntimeErrorType | ErrorType,
diff --git a/src/features/Conversation/Messages/components/MessageActionBar/actions/del.test.ts b/src/features/Conversation/Messages/components/MessageActionBar/actions/del.test.ts
new file mode 100644
index 00000000000..a10b01bb439
--- /dev/null
+++ b/src/features/Conversation/Messages/components/MessageActionBar/actions/del.test.ts
@@ -0,0 +1,95 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import type { UIChatMessage } from '@lobechat/types';
+import { renderHook } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import type { MessageActionContext } from '../types';
+import { delAction } from './del';
+
+const deleteMessage = vi.fn();
+const deleteDBMessage = vi.fn();
+
+vi.mock('../../../../store', () => ({
+ useConversationStore: (selector: (s: any) => any) => selector({ deleteMessage, deleteDBMessage }),
+}));
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+const build = (
+ data: Partial<UIChatMessage>,
+ role: MessageActionContext['role'] = 'group',
+ id = 'group-1',
+) =>
+ renderHook(() => delAction.useBuild({ data: data as UIChatMessage, id, role })).result.current!;
+
+describe('delAction', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('deletes only the errored tail step of a heterogeneous (CC/Codex) run', () => {
+ const data = {
+ id: 'group-1',
+ role: 'assistantGroup',
+ children: [
+ { id: 'step-23', content: 'done' },
+ {
+ id: 'step-24',
+ error: { body: { agentType: 'claude-code', code: 'overloaded' } },
+ },
+ ],
+ } as unknown as UIChatMessage;
+
+ build(data).handleClick!();
+
+ // Must target the failed step's DB id (a child block), not the group id.
+ expect(deleteDBMessage).toHaveBeenCalledWith('step-24');
+ expect(deleteMessage).not.toHaveBeenCalled();
+ });
+
+ it('deletes the whole group when the tail error is NOT a heterogeneous-agent error', () => {
+ // A normal grouped reply that merely ends in a generic tool/provider error
+ // keeps the whole-group delete.
+ const data = {
+ id: 'group-1',
+ role: 'assistantGroup',
+ children: [
+ { id: 'step-1', content: 'done' },
+ { id: 'step-2', error: { type: 'PluginError', body: { message: 'boom' } } },
+ ],
+ } as unknown as UIChatMessage;
+
+ build(data).handleClick!();
+
+ expect(deleteMessage).toHaveBeenCalledWith('group-1');
+ expect(deleteDBMessage).not.toHaveBeenCalled();
+ });
+
+ it('deletes the whole group when no step errored', () => {
+ const data = {
+ id: 'group-1',
+ role: 'assistantGroup',
+ children: [{ id: 'step-23', content: 'done' }],
+ } as unknown as UIChatMessage;
+
+ build(data).handleClick!();
+
+ expect(deleteMessage).toHaveBeenCalledWith('group-1');
+ expect(deleteDBMessage).not.toHaveBeenCalled();
+ });
+
+ it('deletes by message id for a non-group message', () => {
+ build(
+ { id: 'msg-1', role: 'assistant', content: 'hi' } as unknown as UIChatMessage,
+ 'assistant',
+ 'msg-1',
+ ).handleClick!();
+
+ expect(deleteMessage).toHaveBeenCalledWith('msg-1');
+ expect(deleteDBMessage).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/features/Conversation/Messages/components/MessageActionBar/actions/del.ts b/src/features/Conversation/Messages/components/MessageActionBar/actions/del.ts
index 79b641b9ac4..472ddec7072 100644
--- a/src/features/Conversation/Messages/components/MessageActionBar/actions/del.ts
+++ b/src/features/Conversation/Messages/components/MessageActionBar/actions/del.ts
@@ -2,6 +2,8 @@ import { Trash } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
+import { isHeterogeneousAgentStatusGuideError } from '@/features/Conversation/Error/heterogeneous';
+
import { useConversationStore } from '../../../../store';
import { defineAction } from '../defineAction';
@@ -10,16 +12,43 @@ export const delAction = defineAction({
useBuild: (ctx) => {
const { t } = useTranslation('common');
const deleteMessage = useConversationStore((s) => s.deleteMessage);
+ const deleteDBMessage = useConversationStore((s) => s.deleteDBMessage);
+
+ // A heterogeneous (CC/Codex) turn renders as ONE assistantGroup bubble whose
+ // `children` are every real step in the run. Deleting the group id removes
+ // the ENTIRE run β€” including all the steps that succeeded before the tail
+ // failed. When the run's TAIL step is a heterogeneous-agent status error
+ // (upstream overload, rate limit, …), delete ONLY that step so the user
+ // keeps the work already done and can retry from there.
+ //
+ // Scope matters: a normal grouped reply that merely ends in a generic
+ // tool/provider error must keep the whole-group delete, so this is gated on
+ // the LAST child being a heterogeneous-agent status error β€” not any error.
+ // The errored step's id is a real DB message (a child block), not a
+ // top-level bubble, so it must go through `deleteDBMessage`;
+ // `deleteMessage`/`getDisplayMessageById` only resolve top-level display
+ // messages and would no-op on a child id.
+ const lastChild = ctx.role === 'group' ? ctx.data?.children?.at(-1) : undefined;
+ const erroredBlockId =
+ lastChild?.error && isHeterogeneousAgentStatusGuideError(lastChild.error.body)
+ ? lastChild.id
+ : undefined;
return useMemo(
() => ({
danger: true,
- handleClick: () => deleteMessage(ctx.id),
+ handleClick: () => {
+ if (erroredBlockId) {
+ void deleteDBMessage(erroredBlockId);
+ return;
+ }
+ deleteMessage(ctx.id);
+ },
icon: Trash,
key: 'del',
label: t('delete'),
}),
- [t, ctx.id, deleteMessage],
+ [t, ctx.id, deleteMessage, deleteDBMessage, erroredBlockId],
);
},
});