From 36cadb4c4dd091c0a1f63706b12646a4af105efd Mon Sep 17 00:00:00 2001 From: XZH Date: Thu, 10 Sep 2026 21:54:49 -0700 Subject: [PATCH] feat(chat): add resend for failed turns When a chat turn ends with a failure (not a user cancel), preserve the user message and expose a Resend affordance on the trailing assistant bubble. Resend removes the failed assistant placeholder and re-submits the original message with its full request snapshot (attachments, capability, tools, KB, language, etc.) so the new turn runs with the same context. The user message is not duplicated (displayUserMessage: false). --- web/features/chat/ChatStateAdapter.tsx | 44 +++++++++++++++++++ .../chat/components/ChatWorkspace.tsx | 7 +++ .../chat/messages/ChatMessageList.tsx | 28 ++++++++++++ web/locales/en/app.json | 1 + web/locales/zh/app.json | 1 + web/tests/chat-resend-failed-turn.test.ts | 44 +++++++++++++++++++ 6 files changed, 125 insertions(+) create mode 100644 web/tests/chat-resend-failed-turn.test.ts diff --git a/web/features/chat/ChatStateAdapter.tsx b/web/features/chat/ChatStateAdapter.tsx index 9ac681978..7308854fb 100644 --- a/web/features/chat/ChatStateAdapter.tsx +++ b/web/features/chat/ChatStateAdapter.tsx @@ -162,6 +162,9 @@ export interface ChatState { /** Edit-branching: keyed by stringified parent_message_id (or "null" * for the root). Empty means "default to latest sibling everywhere". */ selectedBranches: Record; + /** True when the last turn ended in a failure (not a user cancel) and + * the session is no longer streaming. Drives the Resend affordance. */ + lastTurnFailed: boolean; } export interface SessionConfiguration { @@ -409,6 +412,7 @@ function createSessionEntry( lastSeq: 0, updatedAt: Date.now(), selectedBranches: {}, + lastTurnFailed: false, }; } @@ -1206,6 +1210,10 @@ interface ChatContextValue { }, ) => Promise; regenerateLastMessage: () => void; + /** Re-send the last user message after a failed turn, preserving the + * original request snapshot (attachments, capability, tools, KB, etc.) + * so the new turn runs with the same context as the failed one. */ + resendLastMessage: () => void; deleteTurn: (messageId: number) => Promise; /** Re-send a user message under a new branch (sibling of the original). * Uses the composer's current capability / refs — only the text is @@ -2504,8 +2512,38 @@ export function ChatStateAdapterProvider({ }); }, [sendThroughRunner]); + const resendLastMessage = useCallback(() => { + const currentState = stateRef.current; + const key = currentState.selectedKey; + if (!key) return; + const session = currentState.sessions[key]; + if (!session || !session.sessionId) return; + if (session.isStreaming) return; + if (session.status !== "failed" && session.status !== "rejected") return; + const lastUser = [...session.messages] + .reverse() + .find((m) => m.role === "user" && m.requestSnapshot); + if (!lastUser?.requestSnapshot) return; + // Remove the trailing failed assistant bubble so the new turn's + // STREAM_START placeholder replaces it rather than stacking below. + dispatch({ type: "POP_LAST_ASSISTANT", key }); + const snapshot = lastUser.requestSnapshot; + sendMessage( + snapshot.content, + undefined, + undefined, + undefined, + undefined, + { + displayUserMessage: false, + requestSnapshotOverride: snapshot, + }, + ); + }, [sendMessage]); + const derivedState = useMemo(() => { const current = ensureSelectedSession(state); + const lastMsg = current.messages[current.messages.length - 1]; return { sessionId: current.sessionId, sessionTitle: current.sessionTitle, @@ -2524,6 +2562,10 @@ export function ChatStateAdapterProvider({ currentStage: current.currentStage, language: current.language, selectedBranches: current.selectedBranches, + lastTurnFailed: + !current.isStreaming && + (current.status === "failed" || current.status === "rejected") && + lastMsg?.role === "assistant", }; }, [state]); @@ -2751,6 +2793,7 @@ export function ChatStateAdapterProvider({ cancelStreamingTurn, submitUserReply, regenerateLastMessage, + resendLastMessage, deleteTurn, editMessage, switchBranch, @@ -2780,6 +2823,7 @@ export function ChatStateAdapterProvider({ cancelStreamingTurn, submitUserReply, regenerateLastMessage, + resendLastMessage, deleteTurn, editMessage, switchBranch, diff --git a/web/features/chat/components/ChatWorkspace.tsx b/web/features/chat/components/ChatWorkspace.tsx index 53fefade5..410046247 100644 --- a/web/features/chat/components/ChatWorkspace.tsx +++ b/web/features/chat/components/ChatWorkspace.tsx @@ -269,6 +269,7 @@ export default function ChatWorkspace({ cancelStreamingTurn, submitUserReply, regenerateLastMessage, + resendLastMessage, deleteTurn, editMessage, switchBranch, @@ -2087,6 +2088,10 @@ export default function ChatWorkspace({ regenerateLastMessage(); }, [regenerateLastMessage]); + const handleResendMessage = useCallback(() => { + resendLastMessage(); + }, [resendLastMessage]); + const handleToggleKB = useCallback( (name: string) => { const current = state.knowledgeBases; @@ -2479,6 +2484,8 @@ export default function ChatWorkspace({ language={state.language} onCopyAssistantMessage={copyAssistantMessage} onRegenerateMessage={handleRegenerateMessage} + canResendLastTurn={state.lastTurnFailed} + onResendLastTurn={handleResendMessage} onConfirmOutline={handleConfirmOutline} onPreviewAttachment={handlePreviewMessageAttachment} onDeleteTurn={deleteTurn} diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx index 850cb7cf4..6edee035a 100644 --- a/web/features/chat/messages/ChatMessageList.tsx +++ b/web/features/chat/messages/ChatMessageList.tsx @@ -1451,6 +1451,8 @@ export const ChatMessageList = memo(function ChatMessageList({ language, onCopyAssistantMessage, onRegenerateMessage, + canResendLastTurn = false, + onResendLastTurn, onConfirmOutline, onPreviewAttachment, onDeleteTurn, @@ -1471,6 +1473,10 @@ export const ChatMessageList = memo(function ChatMessageList({ language?: string; onCopyAssistantMessage: CopyHandler; onRegenerateMessage: () => void; + /** True when the last turn failed (not cancelled) and streaming has + * stopped. Drives the Resend affordance on the trailing assistant. */ + canResendLastTurn?: boolean; + onResendLastTurn?: () => void; onConfirmOutline?: ( outline: Array<{ title: string; overview: string }>, topic: string, @@ -1738,6 +1744,12 @@ export const ChatMessageList = memo(function ChatMessageList({ Boolean(pairedUserMessage) && (!pairedUserMessage?.capability || pairedUserMessage?.capability === "chat"); + const showResend = + !isStreaming && + isLastAssistant && + canResendLastTurn && + Boolean(pairedUserMessage?.requestSnapshot) && + Boolean(onResendLastTurn); const deletableTurnUserId = msgDone && pairedUserMessage?.id != null && onDeleteTurn ? pairedUserMessage.id @@ -1832,6 +1844,15 @@ export const ChatMessageList = memo(function ChatMessageList({ {t("Retry")} ) : null} + {showResend && !showRegenerate ? ( + + ) : null} ); })()} @@ -1861,6 +1882,13 @@ export const ChatMessageList = memo(function ChatMessageList({ onClick={() => onRegenerateMessage()} /> )} + {showResend && ( + onResendLastTurn?.()} + /> + )} {showDelete && ( onDeleteTurn?.(deletableTurnUserId)} diff --git a/web/locales/en/app.json b/web/locales/en/app.json index ea72027d8..ca2fc2695 100644 --- a/web/locales/en/app.json +++ b/web/locales/en/app.json @@ -846,6 +846,7 @@ "llm-only": "llm-only", "Copy": "Copy", "Regenerate": "Regenerate", + "Resend": "Resend", "Up to date": "Up to date", "Update available": "Update available", "Run Tour": "Run Tour", diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json index 702df3f39..5944126e4 100644 --- a/web/locales/zh/app.json +++ b/web/locales/zh/app.json @@ -875,6 +875,7 @@ "llm-only": "仅 LLM", "Copy": "复制", "Regenerate": "重新生成", + "Resend": "重新发送", "Up to date": "已最新", "Update available": "有新版本", "Run Tour": "运行引导", diff --git a/web/tests/chat-resend-failed-turn.test.ts b/web/tests/chat-resend-failed-turn.test.ts new file mode 100644 index 000000000..803ca596a --- /dev/null +++ b/web/tests/chat-resend-failed-turn.test.ts @@ -0,0 +1,44 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import path from "node:path"; +import test from "node:test"; + +const source = (relative: string) => + fs.readFileSync(path.resolve(process.cwd(), relative), "utf8"); + +test("ChatStateAdapter exposes lastTurnFailed derived state", () => { + const adapter = source("features/chat/ChatStateAdapter.tsx"); + assert.match(adapter, /lastTurnFailed/); + assert.match(adapter, /"failed".*"rejected"/); + assert.match(adapter, /lastMsg\?\.role === "assistant"/); +}); + +test("ChatStateAdapter implements resendLastMessage preserving snapshot", () => { + const adapter = source("features/chat/ChatStateAdapter.tsx"); + assert.match(adapter, /resendLastMessage/); + assert.match(adapter, /requestSnapshotOverride/); + assert.match(adapter, /displayUserMessage: false/); + assert.match(adapter, /POP_LAST_ASSISTANT/); +}); + +test("ChatMessageList renders Resend only for failed last assistant turn", () => { + const list = source("features/chat/messages/ChatMessageList.tsx"); + assert.match(list, /canResendLastTurn/); + assert.match(list, /onResendLastTurn/); + assert.match(list, /showResend/); + assert.match(list, /Resend/); +}); + +test("ChatWorkspace wires resend props to ChatMessageList", () => { + const workspace = source("features/chat/components/ChatWorkspace.tsx"); + assert.match(workspace, /resendLastMessage/); + assert.match(workspace, /canResendLastTurn=\{state\.lastTurnFailed\}/); + assert.match(workspace, /onResendLastTurn=\{handleResendMessage\}/); +}); + +test("Resend translations exist in en and zh", () => { + const en = JSON.parse(source("locales/en/app.json")); + const zh = JSON.parse(source("locales/zh/app.json")); + assert.equal(en.Resend, "Resend"); + assert.equal(zh.Resend, "重新发送"); +});