diff --git a/web/features/chat/ChatStateAdapter.tsx b/web/features/chat/ChatStateAdapter.tsx index a05f2ef04..226a31aef 100644 --- a/web/features/chat/ChatStateAdapter.tsx +++ b/web/features/chat/ChatStateAdapter.tsx @@ -184,6 +184,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 { @@ -439,6 +442,7 @@ function createSessionEntry( lastSeq: 0, updatedAt: Date.now(), selectedBranches: {}, + lastTurnFailed: false, }; } @@ -1314,6 +1318,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 @@ -2662,8 +2670,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 { sessionKey: current.key, sessionId: current.sessionId, @@ -2685,6 +2723,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]); @@ -2921,6 +2963,7 @@ export function ChatStateAdapterProvider({ cancelStreamingTurn, submitUserReply, regenerateLastMessage, + resendLastMessage, deleteTurn, editMessage, switchBranch, @@ -2951,6 +2994,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 e0bc7769c..6864506b0 100644 --- a/web/features/chat/components/ChatWorkspace.tsx +++ b/web/features/chat/components/ChatWorkspace.tsx @@ -282,6 +282,7 @@ export default function ChatWorkspace({ cancelStreamingTurn, submitUserReply, regenerateLastMessage, + resendLastMessage, deleteTurn, editMessage, switchBranch, @@ -2149,6 +2150,10 @@ export default function ChatWorkspace({ regenerateLastMessage(); }, [regenerateLastMessage]); + const handleResendMessage = useCallback(() => { + resendLastMessage(); + }, [resendLastMessage]); + const handleToggleKB = useCallback( (name: string) => { const current = state.knowledgeBases; @@ -2579,6 +2584,8 @@ export default function ChatWorkspace({ language={state.language} onCopyAssistantMessage={copyAssistantMessage} onRegenerateMessage={handleRegenerateMessage} + canResendLastTurn={state.lastTurnFailed} + onResendLastTurn={handleResendMessage} onConfirmOutline={handleConfirmOutline} onPreviewAttachment={handlePreviewMessageAttachment} onOpenConsultation={(events) => { diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx index 19de731d7..b34ecb1e2 100644 --- a/web/features/chat/messages/ChatMessageList.tsx +++ b/web/features/chat/messages/ChatMessageList.tsx @@ -1815,6 +1815,8 @@ export const ChatMessageList = memo(function ChatMessageList({ language, onCopyAssistantMessage, onRegenerateMessage, + canResendLastTurn = false, + onResendLastTurn, onConfirmOutline, onPreviewAttachment, onOpenConsultation, @@ -1836,6 +1838,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, @@ -2146,6 +2152,12 @@ export const ChatMessageList = memo(function ChatMessageList({ (!pairedUserMessage?.capability || pairedUserMessage?.capability === "chat") && (showActions || terminalErrorRetryable); + const showResend = + !isStreaming && + isLastAssistant && + canResendLastTurn && + Boolean(pairedUserMessage?.requestSnapshot) && + Boolean(onResendLastTurn); const deletableTurnUserId = msgDone && pairedUserMessage?.id != null && onDeleteTurn ? pairedUserMessage.id @@ -2218,6 +2230,15 @@ export const ChatMessageList = memo(function ChatMessageList({ {t("Retry")} ) : null} + {showResend && !showRegenerate ? ( + + ) : null} ); })()} @@ -2247,6 +2268,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 370acc323..1e10a97d1 100644 --- a/web/locales/en/app.json +++ b/web/locales/en/app.json @@ -864,6 +864,7 @@ "Delete permanently": "Delete permanently", "No deleted chats": "No deleted chats", "Permanently delete this session?": "Permanently delete this session?", + "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 c5560af59..cb658b4bd 100644 --- a/web/locales/zh/app.json +++ b/web/locales/zh/app.json @@ -893,6 +893,7 @@ "Delete permanently": "彻底删除", "No deleted chats": "没有已删除的对话", "Permanently delete this session?": "确定要彻底删除此对话吗?", + "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, "重新发送"); +});