feat(chat): show tool calls inline and stream replies into one message
Chat returned only the final answer, so a wrong tool choice, a hallucinated action, or a footgun like `systemctl stop` against `restart` was invisible from the conversation. Only text deltas were threaded through the turn, and tool calls executed inside Generate with nowhere to report. Replace the delta callback with gateway.TurnStream, which carries text fragments and completed tool calls on one ordered path from the model loop to the channel. The sequence is the point: a tool line that overtakes the sentence it interrupted describes a turn that never happened. Text-only callers use gateway.DeltaFunc and are unaffected. A tool is reported on its result, never on its call -- the call part carries no outcome yet, so reporting both would list every tool twice, once with nothing to say. Telegram renders it behind [chat.telegram] show_tool_calls, off by default: this is debugging visibility, and it narrates every internal step to provide it. The dashboard always shows it, being the operator's own observability surface; its tool lines are live-only and are not persisted with the conversation. The Telegram live reply changes mechanism, and with it what a frame MEANS. It used to stream through sendMessageDraft: drafts are ephemeral 30-second previews, so a tool call longer than that outlived the preview, and successive frames were observed concatenated with their cursors intact. It now sends one real message and edits it. An update used to be "the next preview of the answer"; it is now "the whole answer as it currently stands, replacing what is there". The cursor is appended at render time and never stored, so exactly one exists and always at the end, and a frame too long for one message is cut to its tail rather than rejected outright. A stopped or failed turn now edits the cursor away instead of leaving it blinking on a message nothing will finish. That cleanup runs on a live context because turnCtx is already dead, and it runs on both failure branches: ai-sdk raises an aborted stream as ErrAborted with ctx.Err() formatted in rather than wrapped, so errors.Is cannot be trusted to tell a /stop from a fault. Closes #467
This commit is contained in:
1 parent
9ef6f41910
commit
0d73f2fa5a
27 files changed
+1492
-179
No files matched your search
@@ -5,6 +5,22 @@ export function sessionTitle(session) {
|
||||
return session.title || session.branch_name || `Conversation ${session.session_id.slice(0, 8)}`;
|
||||
}
|
||||
|
||||
// assistantBubble builds the row a live reply streams into. The tool list is
|
||||
// created empty and up front so tool activity has somewhere to land that the
|
||||
// streaming text replacement cannot destroy, and so its position above the
|
||||
// reply is fixed by the bubble rather than by insertion order.
|
||||
export function assistantBubble() {
|
||||
return el(
|
||||
"div.chat-bubble-row.assistant",
|
||||
el(
|
||||
"div.chat-bubble",
|
||||
el("div.chat-bubble-meta", "Archie"),
|
||||
el("div.chat-tools"),
|
||||
el("div.chat-bubble-text"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function chatBubble(message) {
|
||||
const from = message.from ?? message.From ?? "";
|
||||
const text = message.text ?? message.Text ?? "";
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { el } from "../base/dom.js";
|
||||
|
||||
// Tool activity is rendered into its own list inside the assistant bubble
|
||||
// rather than folded into the reply text: the text node is replaced wholesale
|
||||
// on every streamed frame, and again on the final reply, so anything written
|
||||
// into it is lost the next time the answer grows.
|
||||
//
|
||||
// The list is part of the bubble from the start (see assistantBubble) and
|
||||
// sits above the reply, matching the shape the same turn takes in Telegram:
|
||||
// the work first, then the answer built from it.
|
||||
export function appendToolCall(host, event) {
|
||||
const name = event?.tool ?? "";
|
||||
if (!host || !name) return null;
|
||||
|
||||
const summary = event.text ?? "";
|
||||
const line = el(
|
||||
"div.chat-tool",
|
||||
el("span.chat-tool-icon", "🔧"),
|
||||
el("span.chat-tool-name", name),
|
||||
el("span.chat-tool-summary", summary),
|
||||
);
|
||||
// A failure reads as a completed step unless it is marked as one.
|
||||
if (summary.startsWith("failed:")) line.className = "chat-tool failed";
|
||||
|
||||
toolList(host).append(line);
|
||||
return line;
|
||||
}
|
||||
|
||||
// toolList returns the bubble's tool list. A bubble built elsewhere may not
|
||||
// have one, in which case the list is appended rather than dropped -- tool
|
||||
// activity out of position still beats tool activity nowhere.
|
||||
function toolList(host) {
|
||||
const existing = host.querySelector(".chat-tools");
|
||||
if (existing) return existing;
|
||||
const list = el("div.chat-tools");
|
||||
host.append(list);
|
||||
return list;
|
||||
}
|
||||
@@ -59,6 +59,15 @@
|
||||
.chat-bubble-text table { width: 100%; margin: .7rem 0; border-collapse: collapse; font-size: .92em; }
|
||||
.chat-bubble-text th, .chat-bubble-text td { padding: .45rem .6rem; text-align: left; border: 1px solid var(--border); }
|
||||
.chat-bubble-text th { color: var(--fg); background: color-mix(in srgb, var(--surface) 70%, transparent); font-weight: 700; }
|
||||
/* Tool activity: a quiet log above the answer it produced. It is deliberately
|
||||
smaller and dimmer than the reply -- it is evidence, not the point. An empty
|
||||
list renders nothing, so a turn that called no tools looks untouched. */
|
||||
.chat-tools { display: grid; gap: .2rem; margin-bottom: .5rem; font-size: .78rem; }
|
||||
.chat-tools:empty { display: none; }
|
||||
.chat-tool { display: flex; gap: .4rem; align-items: baseline; color: var(--fg-muted); overflow-wrap: anywhere; }
|
||||
.chat-tool-name { color: var(--fg); font-weight: 600; }
|
||||
.chat-tool.failed .chat-tool-summary { color: var(--danger); }
|
||||
|
||||
.chat-compose { padding: .85rem 1rem 1rem; border-top: 1px solid var(--border); }
|
||||
.chat-composer-shell { position: relative; padding: .7rem .8rem .55rem; background: var(--surface-hover); border: 1px solid var(--border-strong); border-radius: .9rem; }
|
||||
.chat-composer { width: 100%; min-height: 3.3rem; resize: vertical; box-sizing: border-box; background: transparent; color: var(--fg); border: 0; padding: .25rem 0; font: inherit; }
|
||||
|
||||
+10
-2
@@ -1,8 +1,9 @@
|
||||
import { el } from "../base/dom.js";
|
||||
import { api } from "../base/api.js";
|
||||
import { createChatView } from "./chat-view.js";
|
||||
import { chatBubble } from "./chat-render.js";
|
||||
import { assistantBubble, chatBubble } from "./chat-render.js";
|
||||
import { updateStreamingReply } from "./chat-stream.js";
|
||||
import { appendToolCall } from "./chat-tools.js";
|
||||
import { channelID } from "./chat-state.js";
|
||||
import { retryChatTurn, resolveTurn } from "./chat-retry.js";
|
||||
import { renderCommands, renderModels, renderSelectors, renderSessions } from "./chat-catalog.js";
|
||||
@@ -207,9 +208,13 @@ export function chatPage() {
|
||||
appendMessage({ from: "web", text });
|
||||
composer.value = "";
|
||||
}
|
||||
const replyBubble = isRetry ? retry.replyBubble : el("div.chat-bubble-row.assistant", el("div.chat-bubble", el("div.chat-bubble-meta", "Archie"), el("div.chat-bubble-text")));
|
||||
const replyBubble = isRetry ? retry.replyBubble : assistantBubble();
|
||||
if (!isRetry) transcript.append(replyBubble);
|
||||
replyBubble.querySelector(".chat-retry")?.remove();
|
||||
// A retry reuses the failed turn's bubble, so its tool list still holds
|
||||
// the attempt that did not finish. Clear it: appending to it would
|
||||
// present another turn's work as this one's evidence.
|
||||
replyBubble.querySelector(".chat-tools")?.replaceChildren();
|
||||
let replyText = replyBubble.querySelector(".chat-bubble-text");
|
||||
let streamedText = "";
|
||||
let finished = false;
|
||||
@@ -245,6 +250,9 @@ export function chatPage() {
|
||||
streamedText += event.text;
|
||||
replyText = updateStreamingReply(replyText, streamedText);
|
||||
}
|
||||
if (event.type === "tool") {
|
||||
appendToolCall(replyBubble.querySelector(".chat-bubble") ?? replyBubble, event);
|
||||
}
|
||||
if (event.type === "done") {
|
||||
finished = true;
|
||||
if (!streamedText) {
|
||||
|
||||
Reference in new issue
Block a user