From 391fba0dfeb348853dc90c5d5eb008276a73b23f Mon Sep 17 00:00:00 2001 From: wtz2333 Date: Tue, 17 Nov 2026 11:23:17 +0800 Subject: [PATCH 1/3] [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); +}); From 9ce729bc3bd4bbbe9ce81dd4a5ca309644b689ed Mon Sep 17 00:00:00 2001 From: wtz2333 Date: Tue, 17 Nov 2026 11:37:09 +0800 Subject: [PATCH 2/3] [feat] canvas: withdraw a queued message and continue a stalled queue A message waiting behind the running reply showed its position but offered no way to take it back, and a queue left behind by a failure or a restart had no way to start again without retyping it. A queued bubble now carries Withdraw, which removes it from the thread under the same lock the chain takes when it picks the next message up. A queue with nothing draining it shows a banner with the count and Continue, which starts the oldest message and reads its reply from the usual stream. A cancelled message offers Edit again, which puts it back in the composer so it is sent deliberately rather than silently. Refs #379 --- src/backend/chat.ts | 36 +++++++++++++++++++ src/canvas/AssistantPanel.tsx | 35 +++++++++++++++++++ src/canvas/assistant-experience.css | 33 ++++++++++++++++++ src/canvas/useAssistantChat.ts | 49 ++++++++++++++++++++++++++ src/i18n/messages/en-US/assistant.ts | 6 ++++ src/i18n/messages/zh-CN/assistant.ts | 6 ++++ tests/assistantQueue.test.ts | 52 ++++++++++++++++++++++++++++ 7 files changed, 217 insertions(+) diff --git a/src/backend/chat.ts b/src/backend/chat.ts index b9905634a..b66403b0c 100644 --- a/src/backend/chat.ts +++ b/src/backend/chat.ts @@ -145,6 +145,8 @@ export type ChatEvent = | { type: "sources"; sources: ChatSource[] } /** A message accepted while a reply runs; it starts when that reply ends. */ | { type: "queued"; message: ChatMessage } + /** A queued message was withdrawn before it started. */ + | { type: "retracted"; messageId: string } | { type: "end"; turnId?: string; @@ -318,6 +320,40 @@ export async function sendChat( return null; } +/** + * Take back a message that is still waiting behind the running reply. + * + * A message that has already started is part of the record: the server + * refuses it with 409 and the caller still has to stop the reply instead. + */ +export async function retractChatMessage( + id: string, + message: string, +): Promise<{ retracted: string; conversation: Conversation }> { + return request<{ retracted: string; conversation: Conversation }>( + path(id) + `/messages/${message}`, + { method: "DELETE", headers }, + ); +} + +/** + * Continue a queue that nothing is draining, for example after a restart + * or after a reply failed with messages still waiting behind it. + */ +export async function continueQueue( + id: string, + signal: AbortSignal, + onEvent: (event: ChatEvent) => void, + t: Translate = defaultTranslate, +) { + const response = await fetch("/api" + path(id) + "/queue/dispatch", { + ...json("POST", {}), + headers: { ...headers, "Content-Type": "application/json" }, + signal, + }); + await readChatEvents(response, onEvent, t); +} + export interface RuntimeModel { isDefault?: boolean; id: string; diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index e654443d0..6ea5f6c58 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -494,11 +494,27 @@ export function AssistantPanel({ .filter((item) => item.status === "queued") .findIndex((item) => item.id === message.id) + 1, })} + )} {message.status === "cancelled" && ( {t("assistant.panel.cancelled")} + )} {(message.status === "stopped" || @@ -539,6 +555,25 @@ export function AssistantPanel({
)} + {!chat.sending && + value.messages.some((message) => message.status === "queued") && ( +
+ + {t("assistant.panel.waiting", { + count: value.messages.filter( + (message) => message.status === "queued", + ).length, + })} + + +
+ )}
({ + ...previous, + messages: previous.messages.filter( + (message) => message.id !== event.messageId, + ), + })); } 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; @@ -689,6 +699,42 @@ export function useAssistantChat( } } }; + /** Withdraw a message that has not started yet. */ + const retract = async (messageId: string) => { + const before = current.current; + setValue((previous) => ({ + ...previous, + messages: previous.messages.filter((message) => message.id !== messageId), + })); + try { + const result = await retractChatMessage(projectId, messageId); + if (!alive.current) return; + setValue((previous) => ({ ...previous, messages: result.conversation.messages })); + } catch (reason) { + if (!alive.current) return; + setValue(before); + setError(displayError(errorMessage(reason))); + } + }; + /** Put a cancelled message back in the composer so it can be sent again. */ + const refill = (message: ChatMessage) => { + setValue((previous) => ({ + ...previous, + draft: message.text, + draftContent: message.content ?? null, + assets: message.assets ?? [], + })); + }; + /** Start the oldest waiting message when nothing is running. */ + const proceed = async () => { + if (!loaded || switching.current || submitting.current) return; + const next = current.current.messages.find( + (message) => message.status === "queued", + ); + await follow(next?.id ?? crypto.randomUUID(), async (_id, signal, onEvent) => { + await continueQueue(projectId, signal, onEvent, t); + }); + }; const stop = async () => { if (!turnId.current || stopping) return; setStopping(true); @@ -739,6 +785,9 @@ export function useAssistantChat( addNodes, addAssets, send, + retract, + refill, + proceed, stop, reload: () => setLoadAttempt((n) => n + 1), }; diff --git a/src/i18n/messages/en-US/assistant.ts b/src/i18n/messages/en-US/assistant.ts index 23a7180c6..6ff921625 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -66,6 +66,12 @@ export const assistant = { "assistant.panel.queueWhileRunning": "Press Enter to queue this behind the running reply", "assistant.panel.cancelled": "Queued message cancelled", + "assistant.panel.retract": "Withdraw", + "assistant.panel.retractHint": "Withdraw this queued message before it starts", + "assistant.panel.refill": "Edit again", + "assistant.panel.refillHint": "Put this message back in the composer to send it again", + "assistant.panel.waiting": "{count} message(s) waiting to run", + "assistant.panel.proceed": "Continue", "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 0eb755e87..a7c8b7d06 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -64,6 +64,12 @@ export const assistant = { "assistant.panel.queued": "排队中 · 第 {position} 位", "assistant.panel.queueWhileRunning": "按 Enter 排队,当前回复结束后自动发送", "assistant.panel.cancelled": "排队消息已取消", + "assistant.panel.retract": "撤回", + "assistant.panel.retractHint": "撤回这条还没开始执行的排队消息", + "assistant.panel.refill": "重新编辑", + "assistant.panel.refillHint": "把这条消息放回输入框,确认后再发送", + "assistant.panel.waiting": "{count} 条消息在排队等待执行", + "assistant.panel.proceed": "继续执行", "assistant.panel.stop": "停止回复", "assistant.panel.upload": "上传参考", "assistant.panel.uploadHint": "上传图片、视频、音频或文本", diff --git a/tests/assistantQueue.test.ts b/tests/assistantQueue.test.ts index 097312ed5..8c498539f 100644 --- a/tests/assistantQueue.test.ts +++ b/tests/assistantQueue.test.ts @@ -1,6 +1,8 @@ import assert from "node:assert/strict"; import test from "node:test"; import { + continueQueue, + retractChatMessage, sendChat, type ChatEvent, type ChatRequest, @@ -100,3 +102,53 @@ test("an idle project answers on the same endpoint with the running reply", asyn assert.equal(ends[0]!.type === "end" && ends[0]!.nextTurnId, "turn-2"); assert.equal(ends[1]!.type === "end" && ends[1]!.nextTurnId, null); }); + +test("a queued message is withdrawn through the message itself", async (t) => { + const fetch = t.mock.method( + globalThis, + "fetch", + async (url: string, init?: RequestInit) => { + assert.ok(url.endsWith("/assistant/messages/message-2")); + assert.equal(init?.method, "DELETE"); + return new Response( + JSON.stringify({ + retracted: "message-2", + conversation: { messages: [], draft: "", assets: [], thinking: "auto", modelSelections: {} }, + }), + { status: 200, headers: { "Content-Type": "application/json" } }, + ); + }, + ); + const result = await retractChatMessage("project", "message-2"); + assert.equal(result.retracted, "message-2"); + assert.equal(fetch.mock.calls.length, 1); +}); + +test("a stalled queue is continued and its reply read from the same stream", async (t) => { + const saved = { + messages: [], + draft: "", + assets: [], + thinking: "auto", + modelSelections: {}, + }; + const fetch = t.mock.method( + globalThis, + "fetch", + async (url: string) => { + assert.ok(url.endsWith("/assistant/queue/dispatch")); + return sse([ + { type: "start", conversation: saved }, + { type: "delta", text: "继续" }, + { type: "end", turnId: "turn-2", nextTurnId: null, conversation: saved, error: null }, + ]); + }, + ); + const events: ChatEvent[] = []; + await continueQueue("project", new AbortController().signal, (event) => events.push(event)); + assert.deepEqual( + events.map((event) => event.type), + ["start", "delta", "end"], + ); + assert.equal(fetch.mock.calls.length, 1); +}); From 8756a97105f23c293046423cc99f172c4a7803aa Mon Sep 17 00:00:00 2001 From: wtz2333 Date: Thu, 19 Nov 2026 10:09:07 +0800 Subject: [PATCH 3/3] [feat] canvas: insert a message into the reply that is already running While a reply runs the composer now offers both deliveries: Enter (or the send button) hands the text to that reply through the runtime's own channel, and a Queue button waits for it to end. The inserted message is shown where it was sent, before the reply it joined. --- src/backend/chat.ts | 41 ++++++++++++++++++++- src/canvas/AssistantPanel.tsx | 33 ++++++++++++++--- src/canvas/assistant-experience.css | 16 +++++++++ src/canvas/useAssistantChat.ts | 54 ++++++++++++++++++++++++++++ src/i18n/messages/en-US/assistant.ts | 6 ++++ src/i18n/messages/zh-CN/assistant.ts | 6 ++++ tests/assistantQueue.test.ts | 52 +++++++++++++++++++++++++++ 7 files changed, 203 insertions(+), 5 deletions(-) diff --git a/src/backend/chat.ts b/src/backend/chat.ts index b66403b0c..21e271faf 100644 --- a/src/backend/chat.ts +++ b/src/backend/chat.ts @@ -26,6 +26,8 @@ export interface RuntimeInfo { | "unsupported_version" | "unknown"; supportsSkills?: boolean; + /** The runtime can take extra text inside a reply that is already running. */ + supportsSteering?: boolean; } export interface ChatSkill { source?: "builtin" | "workspace" | "project"; @@ -75,7 +77,9 @@ export interface ChatMessage { | "queued" | "cancelled" | "stopped" - | "interrupted"; + | "interrupted" + /** Text handed to the reply that was running when it was sent. */ + | "inserted"; /** Position of a queued message in the delivery order the server assigned. */ queueSeq?: number; assets?: ChatAsset[]; @@ -147,6 +151,8 @@ export type ChatEvent = | { type: "queued"; message: ChatMessage } /** A queued message was withdrawn before it started. */ | { type: "retracted"; messageId: string } + /** Text that reached the reply while it was still running. */ + | { type: "steered"; message: ChatMessage } | { type: "end"; turnId?: string; @@ -168,6 +174,8 @@ export interface ChatRequest { runtime?: AgentRuntime; runtimeModel?: string | null; skills?: string[]; + /** Insert reaches the running reply; queue waits for it to end. */ + delivery?: "queue" | "insert"; } const path = (id: string) => `/creation/projects/${id}/assistant`; @@ -320,6 +328,37 @@ export async function sendChat( return null; } +export interface InsertedReply { + inserted: ChatMessage; + chainId: string; + turnId: string; +} + +/** + * Hand the text to the reply that is already running. + * + * `delivery: "insert"` goes through the runtime's own channel, so the answer + * keeps the work it has already done. The server refuses the insert when no + * reply is running, when the runtime has no such channel, or when the message + * carries assets; the caller shows that refusal as it is. + */ +export async function insertChat( + 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, delivery: "insert" }), + headers: { ...headers, "Content-Type": "application/json" }, + signal, + }); + if (response.status === 202) return (await response.json()) as InsertedReply; + await readChatEvents(response, onEvent, t); + return null; +} + /** * Take back a message that is still waiting behind the running reply. * diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 6ea5f6c58..8c55c4889 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -504,6 +504,11 @@ export function AssistantPanel({ )} + {message.status === "inserted" && ( + + {t("assistant.panel.inserted")} + + )} {message.status === "cancelled" && ( {t("assistant.panel.cancelled")} @@ -720,7 +725,10 @@ export function AssistantPanel({ onSubmit={() => { nearBottom.current = true; pendingReferences.cancel(); - void chat.send(); + // Enter reaches the running reply when the runtime can take it; + // otherwise it queues behind that reply. + if (chat.sending && chat.steerable) void chat.insert(); + else void chat.send(); }} /> @@ -791,14 +799,29 @@ export function AssistantPanel({ )} + {chat.sending && ( + + )}