From 391fba0dfeb348853dc90c5d5eb008276a73b23f Mon Sep 17 00:00:00 2001 From: wtz2333 Date: Tue, 17 Nov 2026 11:23:17 +0800 Subject: [PATCH] [feat] canvas: queue a message sent while a reply runs While a reply streams, the composer kept the draft but replaced Send with Stop and Enter returned without a word, so a supplement could only be typed in and lost. It is sent now: the server answers 202 with the queued message, the log shows it as queued, and the same stream keeps feeding the replies that follow until the chain ends. Refs #379 --- src/backend/chat.ts | 51 ++++++++++++- src/canvas/AssistantPanel.tsx | 47 +++++++++--- src/canvas/assistant-experience.css | 13 ++++ src/canvas/useAssistantChat.ts | 109 ++++++++++++++++++++------- src/i18n/messages/en-US/assistant.ts | 6 ++ src/i18n/messages/zh-CN/assistant.ts | 5 ++ tests/assistantQueue.test.ts | 102 +++++++++++++++++++++++++ 7 files changed, 293 insertions(+), 40 deletions(-) create mode 100644 tests/assistantQueue.test.ts diff --git a/src/backend/chat.ts b/src/backend/chat.ts index f935df4c8..b9905634a 100644 --- a/src/backend/chat.ts +++ b/src/backend/chat.ts @@ -69,7 +69,15 @@ export interface ChatMessage { role: "user" | "assistant"; text: string; content?: ReferenceContent | null; - status: "complete" | "streaming" | "stopped" | "interrupted"; + status: + | "complete" + | "streaming" + | "queued" + | "cancelled" + | "stopped" + | "interrupted"; + /** Position of a queued message in the delivery order the server assigned. */ + queueSeq?: number; assets?: ChatAsset[]; model?: string; thinking?: Thinking; @@ -135,7 +143,16 @@ export type ChatEvent = | ({ type: "tool" } & ChatTool) | { type: "delta"; text: string } | { type: "sources"; sources: ChatSource[] } - | { type: "end"; conversation: Conversation | null; error: string | null }; + /** A message accepted while a reply runs; it starts when that reply ends. */ + | { type: "queued"; message: ChatMessage } + | { + type: "end"; + turnId?: string; + /** Set when the server has already started the next queued message. */ + nextTurnId?: string | null; + conversation: Conversation | null; + error: string | null; + }; export interface ChatRequest { conversationId?: string; @@ -271,6 +288,36 @@ async function readChatEvents( } } +export interface QueuedReply { + queued: ChatMessage; + chainId: string; + turnId: string; +} + +/** + * Hand a message to the assistant without waiting for the running reply. + * + * The server answers with a stream when the project is idle and with + * `202 { queued }` when a reply is running, so a client submits here without + * having to know which of the two it is. + */ +export async function sendChat( + id: string, + value: ChatRequest, + signal: AbortSignal, + onEvent: (event: ChatEvent) => void, + t: Translate = defaultTranslate, +): Promise { + const response = await fetch("/api" + path(id) + "/messages", { + ...json("POST", value), + headers: { ...headers, "Content-Type": "application/json" }, + signal, + }); + if (response.status === 202) return (await response.json()) as QueuedReply; + await readChatEvents(response, onEvent, t); + return null; +} + export interface RuntimeModel { isDefault?: boolean; id: string; diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 1da3e8e12..e654443d0 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -486,6 +486,21 @@ export function AssistantPanel({ )} {message.role === "user" && !!message.assets?.length && } + {message.status === "queued" && ( + + {t("assistant.panel.queued", { + position: + value.messages + .filter((item) => item.status === "queued") + .findIndex((item) => item.id === message.id) + 1, + })} + + )} + {message.status === "cancelled" && ( + + {t("assistant.panel.cancelled")} + + )} {(message.status === "stopped" || message.status === "interrupted") && ( @@ -729,10 +744,10 @@ export function AssistantPanel({ onConfigure={onOpenSettings} />
- {chat.sending ? ( + {chat.sending && ( - ) : ( - )} +
@@ -774,6 +792,11 @@ export function AssistantPanel({ ) : ( `${chat.runtimes.find((item) => item.id === (value.runtime ?? "default"))?.label ?? t("assistant.panel.chooseAgent")}` )} + {chat.sending && !!value.draft.trim() && ( + + {t("assistant.panel.queueWhileRunning")} + + )} {latestTrace && ( diff --git a/src/canvas/assistant-experience.css b/src/canvas/assistant-experience.css index b4f713889..9ca091850 100644 --- a/src/canvas/assistant-experience.css +++ b/src/canvas/assistant-experience.css @@ -180,6 +180,19 @@ .cw-assistant-toolbar .cw-assistant-send { margin-left: auto; } +.cw-assistant-toolbar .cw-assistant-send.is-stop { + width: 32px; + height: 32px; + border: 1px solid var(--chat-border); + background: transparent; + color: var(--chat-text); +} +.cw-assistant-toolbar .cw-assistant-send.is-stop + .cw-assistant-send { + margin-left: 6px; +} +.cw-assistant-queue-hint { + display: block; +} .cw-chat-markdown strong { font-weight: 600; } diff --git a/src/canvas/useAssistantChat.ts b/src/canvas/useAssistantChat.ts index 63875a7d6..d080c5c3f 100644 --- a/src/canvas/useAssistantChat.ts +++ b/src/canvas/useAssistantChat.ts @@ -17,7 +17,9 @@ import { stopChat, type ChatAsset, type Conversation, + type ChatRequest, conversationIsEmpty, + sendChat, } from "../backend/chat"; import { flushChatDraft, rememberChat } from "../backend/chatDrafts"; import { creationClient } from "../backend/creationClient.ts"; @@ -441,38 +443,81 @@ export function useAssistantChat( commit, ); + const requestFor = (input: Conversation, id: string): ChatRequest => ({ + id, + conversationId: input.conversationId, + modelId: agent?.id, + runtime: input.runtime ?? "default", + runtimeModel: input.runtimeModel, + text: input.draft, + content: input.draftContent, + assets: input.assets.map((asset) => asset.id), + thinking: builtinThinking(input, agent?.id), + modelSelections: input.modelSelections, + skills: input.skills ?? [], + }); + /** + * Send what the composer holds without waiting for the running reply. + * + * The server queues the message and starts it when that reply ends, so the + * composer stays usable instead of dropping the keystroke on the floor. + */ + const queue = async () => { + const input = current.current; + setError(""); + try { + await flushChatDraft(projectId); + const accepted = await sendChat( + projectId, + requestFor(input, crypto.randomUUID()), + new AbortController().signal, + () => {}, + t, + ); + if (!alive.current) return; + if (!accepted) { + // The reply ended in between, so this message opened a turn of its own. + // Read that run instead of leaving its stream unread. + const { conversation: saved, activeTurnId } = await conversation(projectId); + if (!alive.current) return; + setValue(restoreConversation(projectId, saved, Boolean(activeTurnId))); + if (activeTurnId) void resume.current(activeTurnId); + return; + } + setValue((previous) => + previous.messages.some((message) => message.id === accepted.queued.id) + ? previous + : { + ...previous, + messages: [...previous.messages, accepted.queued], + draft: "", + draftContent: null, + assets: [], + skills: [], + }, + ); + } catch (reason) { + if (alive.current) setError(displayError(errorMessage(reason))); + } + }; const send = async () => { if ( !loaded || switching.current || - submitting.current || uploading.current || !canSend || !current.current.draft.trim() ) return; + // A reply is already arriving: queue behind it instead of failing silently. + if (submitting.current) { + await queue(); + return; + } const input = current.current; await follow(crypto.randomUUID(), async (id, signal, onEvent) => { await flushChatDraft(projectId); - await streamChat( - projectId, - { - id, - conversationId: input.conversationId, - modelId: agent?.id, - runtime: input.runtime ?? "default", - runtimeModel: input.runtimeModel, - text: input.draft, - content: input.draftContent, - assets: input.assets.map((asset) => asset.id), - thinking: builtinThinking(input, agent?.id), - modelSelections: input.modelSelections, - skills: input.skills ?? [], - }, - signal, - onEvent, - t, - ); + await sendChat(projectId, requestFor(input, id), signal, onEvent, t); }); }; /** Re-attach to a turn that kept running while this project was not shown. */ @@ -506,7 +551,9 @@ export function useAssistantChat( const abort = new AbortController(); controller.current = abort; turnId.current = id; - const assistantId = crypto.randomUUID(); + // One stream covers every reply in the chain, so the reply being written + // is replaced each time a `start` frame opens a new one. + let reply = crypto.randomUUID(); try { await run( id, @@ -514,6 +561,8 @@ export function useAssistantChat( (event) => { if (!alive.current) return; if (event.type === "start") { + // A new reply begins; the previous one is saved in this conversation. + reply = crypto.randomUUID(); setStarted(true); setValue({ ...event.conversation, @@ -521,7 +570,7 @@ export function useAssistantChat( message.status === "streaming" ? { ...message, - id: assistantId, + id: reply, } : message, ), @@ -531,7 +580,7 @@ export function useAssistantChat( setValue((previous) => ({ ...previous, messages: previous.messages.map((message) => - message.id === assistantId + message.id === reply ? { ...message, text: message.text + event.text } : message, ), @@ -539,7 +588,7 @@ export function useAssistantChat( } else if (event.type === "sources") { setValue((previous) => ({ ...previous, - messages: previous.messages.map((message) => message.id === assistantId + messages: previous.messages.map((message) => message.id === reply ? { ...message, sources: Array.from(new Map([ ...event.sources.map((source) => [source.id, source] as const), ...(message.sources ?? []).map((source) => [source.id, source] as const), @@ -550,7 +599,7 @@ export function useAssistantChat( else if (event.type === "plan") { setValue((previous) => ({ ...previous, - messages: previous.messages.map((message) => message.id === assistantId + messages: previous.messages.map((message) => message.id === reply ? { ...message, plan: event.entries } : message), })); } else if (event.type === "tool") { @@ -559,7 +608,7 @@ export function useAssistantChat( setValue((previous) => ({ ...previous, messages: previous.messages.map((message) => { - if (message.id !== assistantId) return message; + if (message.id !== reply) return message; const tools = [...(message.tools ?? [])]; const index = tools.findIndex((item) => item.id === tool.id); if (index < 0) tools.push(tool); @@ -568,7 +617,15 @@ export function useAssistantChat( }), })); if (event.status !== "running") onEnd.current(); + } else if (event.type === "queued") { + setValue((previous) => + previous.messages.some((message) => message.id === event.message.id) + ? previous + : { ...previous, messages: [...previous.messages, event.message] }, + ); } else if (event.type === "end") { + // The chain moves on to the next queued message; stop must follow it. + if (event.nextTurnId) turnId.current = event.nextTurnId; if (event.conversation) setValue(event.conversation); if (event.error) setError(displayError(event.error)); } diff --git a/src/i18n/messages/en-US/assistant.ts b/src/i18n/messages/en-US/assistant.ts index 3e19abda4..23a7180c6 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -60,6 +60,12 @@ export const assistant = { "assistant.panel.stopped": "Reply stopped", "assistant.panel.interrupted": "Reply interrupted", "assistant.panel.send": "Send message", + "assistant.panel.queue": "Queue message", + "assistant.panel.queueHint": "Send it when the current reply finishes", + "assistant.panel.queued": "Queued · position {position}", + "assistant.panel.queueWhileRunning": + "Press Enter to queue this behind the running reply", + "assistant.panel.cancelled": "Queued message cancelled", "assistant.panel.stop": "Stop reply", "assistant.panel.upload": "Upload reference", "assistant.panel.uploadHint": "Upload images, video, audio or text", diff --git a/src/i18n/messages/zh-CN/assistant.ts b/src/i18n/messages/zh-CN/assistant.ts index 4c95240b8..0eb755e87 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -59,6 +59,11 @@ export const assistant = { "assistant.panel.stopped": "已停止回复", "assistant.panel.interrupted": "回复已中断", "assistant.panel.send": "发送消息", + "assistant.panel.queue": "排队发送", + "assistant.panel.queueHint": "当前回复结束后发送", + "assistant.panel.queued": "排队中 · 第 {position} 位", + "assistant.panel.queueWhileRunning": "按 Enter 排队,当前回复结束后自动发送", + "assistant.panel.cancelled": "排队消息已取消", "assistant.panel.stop": "停止回复", "assistant.panel.upload": "上传参考", "assistant.panel.uploadHint": "上传图片、视频、音频或文本", diff --git a/tests/assistantQueue.test.ts b/tests/assistantQueue.test.ts new file mode 100644 index 000000000..097312ed5 --- /dev/null +++ b/tests/assistantQueue.test.ts @@ -0,0 +1,102 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + sendChat, + type ChatEvent, + type ChatRequest, +} from "../src/backend/chat.ts"; + +const request: ChatRequest = { + id: "message-1", + text: "再补一点", + assets: [], + thinking: "auto", + modelSelections: {}, +}; + +const sse = (events: unknown[]) => + new Response( + events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""), + { headers: { "Content-Type": "text/event-stream" } }, + ); + +test("a message sent while a reply runs is queued instead of started", async (t) => { + const queued = { + id: "message-1", + role: "user", + text: "再补一点", + status: "queued", + queueSeq: 2, + }; + const fetch = t.mock.method( + globalThis, + "fetch", + async (url: string, init?: RequestInit) => { + assert.ok(url.endsWith("/assistant/messages")); + assert.equal(init?.method, "POST"); + return new Response( + JSON.stringify({ queued, chainId: "chain-1", turnId: "turn-1" }), + { status: 202, headers: { "Content-Type": "application/json" } }, + ); + }, + ); + const events: ChatEvent[] = []; + const accepted = await sendChat( + "project", + request, + new AbortController().signal, + (event) => events.push(event), + ); + assert.equal(accepted?.queued.status, "queued"); + assert.equal(accepted?.chainId, "chain-1"); + assert.deepEqual(events, []); + assert.equal(fetch.mock.calls.length, 1); +}); + +test("an idle project answers on the same endpoint with the running reply", async (t) => { + const saved = { + messages: [], + draft: "", + assets: [], + thinking: "auto", + modelSelections: {}, + }; + t.mock.method(globalThis, "fetch", async () => + sse([ + { type: "start", conversation: saved }, + { type: "delta", text: "先做这个" }, + // The chain moves on to a queued message on the same stream. + { + type: "end", + turnId: "turn-1", + nextTurnId: "turn-2", + conversation: saved, + error: null, + }, + { type: "start", conversation: saved }, + { type: "delta", text: "|完" }, + { + type: "end", + turnId: "turn-2", + nextTurnId: null, + conversation: saved, + error: null, + }, + ]), + ); + const events: ChatEvent[] = []; + const accepted = await sendChat( + "project", + request, + new AbortController().signal, + (event) => events.push(event), + ); + assert.equal(accepted, null); + assert.deepEqual( + events.map((event) => event.type), + ["start", "delta", "end", "start", "delta", "end"], + ); + const ends = events.filter((event) => event.type === "end"); + assert.equal(ends[0]!.type === "end" && ends[0]!.nextTurnId, "turn-2"); + assert.equal(ends[1]!.type === "end" && ends[1]!.nextTurnId, null); +});