Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/backend/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<QueuedReply | null> {
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;
Expand Down
47 changes: 35 additions & 12 deletions src/canvas/AssistantPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,21 @@ export function AssistantPanel({
</a>
)}
{message.role === "user" && !!message.assets?.length && <AttachmentStrip projectId={documentId} assets={message.assets.map(resolveAsset)} />}
{message.status === "queued" && (
<small className="cw-assistant-message-status">
{t("assistant.panel.queued", {
position:
value.messages
.filter((item) => item.status === "queued")
.findIndex((item) => item.id === message.id) + 1,
})}
</small>
)}
{message.status === "cancelled" && (
<small className="cw-assistant-message-status">
{t("assistant.panel.cancelled")}
</small>
)}
{(message.status === "stopped" ||
message.status === "interrupted") && (
<small className="cw-assistant-message-status">
Expand Down Expand Up @@ -729,28 +744,31 @@ export function AssistantPanel({
onConfigure={onOpenSettings}
/>
<div className="cw-assistant-toolbar-spacer" />
{chat.sending ? (
{chat.sending && (
<button
type="button"
className="cw-assistant-send"
className="cw-assistant-send is-stop"
aria-label={t("assistant.panel.stop")}
title={t("assistant.panel.stop")}
disabled={!chat.started || chat.stopping}
onClick={() => void chat.stop()}
>
<CanvasIcon name="stop" size={16} />
</button>
) : (
<button
type="submit"
className="cw-assistant-send"
aria-label={t("assistant.panel.send")}
title={t("assistant.panel.send")}
disabled={!chat.canSend || locked || !value.draft.trim()}
>
<CanvasIcon name="arrow" size={19} />
</button>
)}
<button
type="submit"
className="cw-assistant-send"
aria-label={chat.sending
? t("assistant.panel.queue")
: t("assistant.panel.send")}
title={chat.sending
? t("assistant.panel.queueHint")
: t("assistant.panel.send")}
disabled={!chat.canSend || locked || !value.draft.trim()}
>
<CanvasIcon name="arrow" size={19} />
</button>
</div>
</form>
<div className="cw-assistant-footnote">
Expand All @@ -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() && (
<span className="cw-assistant-queue-hint">
{t("assistant.panel.queueWhileRunning")}
</span>
)}
{latestTrace && (
<a className="cw-assistant-trace-link cw-assistant-live-trace"
href={`/?${latestTrace}`} target="_blank" rel="noopener noreferrer">
Expand Down
13 changes: 13 additions & 0 deletions src/canvas/assistant-experience.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
109 changes: 83 additions & 26 deletions src/canvas/useAssistantChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -506,22 +551,26 @@ 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,
abort.signal,
(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,
messages: event.conversation.messages.map((message) =>
message.status === "streaming"
? {
...message,
id: assistantId,
id: reply,
}
: message,
),
Expand All @@ -531,15 +580,15 @@ export function useAssistantChat(
setValue((previous) => ({
...previous,
messages: previous.messages.map((message) =>
message.id === assistantId
message.id === reply
? { ...message, text: message.text + event.text }
: message,
),
}));
} 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),
Expand All @@ -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") {
Expand All @@ -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);
Expand All @@ -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));
}
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/messages/en-US/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/messages/zh-CN/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": "上传图片、视频、音频或文本",
Expand Down
Loading
Loading