Merge PR #1394: chat message resend

# Conflicts:
#	web/features/chat/messages/ChatMessageList.tsx
#	web/locales/en/app.json
#	web/locales/zh/app.json
This commit is contained in:
Bingxi Zhao (Frank)
2026-09-23 10:27:04 +08:00
6 changed files with 125 additions and 0 deletions

View File

@@ -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<string, number>;
/** 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<boolean>;
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<void>;
/** 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<ChatState>(() => {
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,

View File

@@ -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) => {

View File

@@ -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")}
</button>
) : null}
{showResend && !showRegenerate ? (
<button
type="button"
onClick={() => onResendLastTurn?.()}
className="shrink-0 rounded-md px-2 py-1 text-[11.5px] font-medium text-[var(--foreground)] hover:bg-[var(--muted)]"
>
{t("Resend")}
</button>
) : null}
</div>
);
})()}
@@ -2247,6 +2268,13 @@ export const ChatMessageList = memo(function ChatMessageList({
onClick={() => onRegenerateMessage()}
/>
)}
{showResend && (
<RoughActionButton
icon={RefreshCcw}
label={t("Resend")}
onClick={() => onResendLastTurn?.()}
/>
)}
{showDelete && (
<DeleteTurnButton
onDelete={() => onDeleteTurn?.(deletableTurnUserId)}

View File

@@ -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",

View File

@@ -893,6 +893,7 @@
"Delete permanently": "彻底删除",
"No deleted chats": "没有已删除的对话",
"Permanently delete this session?": "确定要彻底删除此对话吗?",
"Resend": "重新发送",
"Up to date": "已最新",
"Update available": "有新版本",
"Run Tour": "运行引导",

View File

@@ -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, "重新发送");
});