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
126 changes: 124 additions & 2 deletions src/backend/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -69,7 +71,17 @@ export interface ChatMessage {
role: "user" | "assistant";
text: string;
content?: ReferenceContent | null;
status: "complete" | "streaming" | "stopped" | "interrupted";
status:
| "complete"
| "streaming"
| "queued"
| "cancelled"
| "stopped"
| "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[];
model?: string;
thinking?: Thinking;
Expand Down Expand Up @@ -135,7 +147,20 @@ 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 }
/** Text that reached the reply while it was still running. */
| { type: "steered"; 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 All @@ -149,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`;
Expand Down Expand Up @@ -271,6 +298,101 @@ 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 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<InsertedReply | null> {
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.
*
* 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
101 changes: 92 additions & 9 deletions src/canvas/AssistantPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,42 @@ 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 === "inserted" && (
<small className="cw-assistant-message-status">
{t("assistant.panel.inserted")}
</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 +560,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 @@ -670,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();
}}
/>
<AttachmentStrip projectId={documentId} assets={value.assets.map(resolveAsset)} />
Expand Down Expand Up @@ -729,28 +787,46 @@ 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>
) : (
)}
{chat.sending && (
<button
type="submit"
className="cw-assistant-send"
aria-label={t("assistant.panel.send")}
title={t("assistant.panel.send")}
type="button"
className="cw-assistant-queue-button"
title={t("assistant.panel.queueHint")}
disabled={!chat.canSend || locked || !value.draft.trim()}
onClick={() => void chat.send()}
>
<CanvasIcon name="arrow" size={19} />
{t("assistant.panel.queueShort")}
</button>
)}
<button
type="submit"
className="cw-assistant-send"
aria-label={chat.sending
? chat.steerable
? t("assistant.panel.insert")
: t("assistant.panel.queue")
: t("assistant.panel.send")}
title={chat.sending
? chat.steerable
? t("assistant.panel.insertHint")
: 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 +850,13 @@ 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">
{chat.steerable
? t("assistant.panel.insertWhileRunning")
: 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
62 changes: 62 additions & 0 deletions src/canvas/assistant-experience.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,68 @@
.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-toolbar .cw-assistant-queue-button {
height: 32px;
padding: 0 10px;
border: 1px solid var(--chat-border);
border-radius: 8px;
background: transparent;
color: var(--chat-text);
font-size: 12px;
cursor: pointer;
}

.cw-assistant-toolbar .cw-assistant-queue-button:disabled {
opacity: 0.45;
cursor: default;
}

.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