diff --git a/src/backend/chat.ts b/src/backend/chat.ts index f935df4c8..b66403b0c 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,18 @@ 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 } + /** A queued message was withdrawn before it started. */ + | { type: "retracted"; messageId: string } + | { + 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 +290,70 @@ 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; +} + +/** + * 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 1da3e8e12..6ea5f6c58 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -486,6 +486,37 @@ 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") && ( @@ -524,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, + })} + + +
+ )}
- {chat.sending ? ( + {chat.sending && ( - ) : ( - )} +
@@ -774,6 +827,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..23acd3382 100644 --- a/src/canvas/assistant-experience.css +++ b/src/canvas/assistant-experience.css @@ -180,6 +180,52 @@ .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-assistant-message-action { + margin-left: 8px; + padding: 0; + border: 0; + background: transparent; + color: inherit; + font: inherit; + text-decoration: underline; + cursor: pointer; +} + +.cw-assistant-queue-banner { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + margin: 0 0 8px; + padding: 6px 10px; + border: 1px solid var(--chat-border); + border-radius: 8px; + font-size: 12px; +} + +.cw-assistant-queue-banner button { + padding: 2px 8px; + border: 1px solid var(--chat-border); + border-radius: 6px; + background: transparent; + color: inherit; + font: inherit; + cursor: pointer; +} .cw-chat-markdown strong { font-weight: 600; } diff --git a/src/canvas/useAssistantChat.ts b/src/canvas/useAssistantChat.ts index 63875a7d6..80d4d3b7a 100644 --- a/src/canvas/useAssistantChat.ts +++ b/src/canvas/useAssistantChat.ts @@ -16,8 +16,13 @@ import { streamChat, stopChat, type ChatAsset, + type ChatMessage, type Conversation, + type ChatRequest, conversationIsEmpty, + sendChat, + retractChatMessage, + continueQueue, } from "../backend/chat"; import { flushChatDraft, rememberChat } from "../backend/chatDrafts"; import { creationClient } from "../backend/creationClient.ts"; @@ -441,38 +446,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 +554,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 +564,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 +573,7 @@ export function useAssistantChat( message.status === "streaming" ? { ...message, - id: assistantId, + id: reply, } : message, ), @@ -531,7 +583,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 +591,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 +602,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 +611,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 +620,22 @@ 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 === "retracted") { + setValue((previous) => ({ + ...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; if (event.conversation) setValue(event.conversation); if (event.error) setError(displayError(event.error)); } @@ -632,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); @@ -682,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 3e19abda4..6ff921625 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -60,6 +60,18 @@ 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.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 4c95240b8..a7c8b7d06 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -59,6 +59,17 @@ 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.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 new file mode 100644 index 000000000..8c498539f --- /dev/null +++ b/tests/assistantQueue.test.ts @@ -0,0 +1,154 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + continueQueue, + retractChatMessage, + 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); +}); + +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); +});