Skip to content
Draft
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
87 changes: 85 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,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;
Expand Down Expand Up @@ -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<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;
}

/**
* 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;
Expand Down
82 changes: 70 additions & 12 deletions src/canvas/AssistantPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,37 @@ 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,
})}
<button
type="button"
className="cw-assistant-message-action"
title={t("assistant.panel.retractHint")}
onClick={() => void chat.retract(message.id)}
>
{t("assistant.panel.retract")}
</button>
</small>
)}
{message.status === "cancelled" && (
<small className="cw-assistant-message-status">
{t("assistant.panel.cancelled")}
<button
type="button"
className="cw-assistant-message-action"
title={t("assistant.panel.refillHint")}
onClick={() => chat.refill(message)}
>
{t("assistant.panel.refill")}
</button>
</small>
)}
{(message.status === "stopped" ||
message.status === "interrupted") && (
<small className="cw-assistant-message-status">
Expand Down Expand Up @@ -524,6 +555,25 @@ export function AssistantPanel({
</button>
</div>
)}
{!chat.sending &&
value.messages.some((message) => message.status === "queued") && (
<div className="cw-assistant-queue-banner" role="status">
<span>
{t("assistant.panel.waiting", {
count: value.messages.filter(
(message) => message.status === "queued",
).length,
})}
</span>
<button
type="button"
onClick={() => void chat.proceed()}
disabled={!chat.canSend}
>
{t("assistant.panel.proceed")}
</button>
</div>
)}
<form
className="cw-assistant-composer"
aria-label={t("assistant.panel.composer")}
Expand Down Expand Up @@ -729,28 +779,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 +827,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
46 changes: 46 additions & 0 deletions src/canvas/assistant-experience.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading
Loading