Files
archie/ui/test/chat-tools.test.js
T
sam 0d73f2fa5a 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
2026-08-14 02:36:12 +10:00

81 lines
3.0 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import "./shim.js";
import { assistantBubble } from "../src/chat/chat-render.js";
import { appendToolCall } from "../src/chat/chat-tools.js";
// childIndex reports where a node sits among its parent's children, which is
// how the tests assert that the work is listed above the answer.
function childIndex(parent, selector) {
const node = parent.querySelector(selector);
return parent.childNodes.indexOf(node);
}
test("a tool call is listed above the answer it produced", () => {
const row = assistantBubble();
const host = row.querySelector(".chat-bubble");
appendToolCall(host, { tool: "shell", text: "exit 0" });
const tools = host.querySelector(".chat-tools");
assert.equal(tools.childNodes.length, 1);
assert.equal(host.querySelector(".chat-tool-name").textContent, "shell");
assert.equal(host.querySelector(".chat-tool-summary").textContent, "exit 0");
assert.ok(
childIndex(host, ".chat-tools") < childIndex(host, ".chat-bubble-text"),
"tool activity must render above the reply text",
);
});
test("further tool calls join the same list in order", () => {
const row = assistantBubble();
const host = row.querySelector(".chat-bubble");
appendToolCall(host, { tool: "memory_edit", text: "added entry" });
appendToolCall(host, { tool: "shell", text: "exit 0" });
const list = host.querySelector(".chat-tools");
const names = list.childNodes.map((line) => line.querySelector(".chat-tool-name").textContent);
assert.deepEqual(names, ["memory_edit", "shell"]);
});
test("a failed tool call is marked so it does not read as success", () => {
const row = assistantBubble();
const host = row.querySelector(".chat-bubble");
appendToolCall(host, { tool: "read", text: "failed: no such file" });
const line = host.querySelector(".chat-tool");
assert.match(line.className, /failed/);
});
test("a successful tool call is not marked as failed", () => {
const row = assistantBubble();
const host = row.querySelector(".chat-bubble");
appendToolCall(host, { tool: "read", text: "42 lines" });
assert.doesNotMatch(host.querySelector(".chat-tool").className, /failed/);
});
test("a tool event with no name is ignored rather than rendered blank", () => {
const row = assistantBubble();
const host = row.querySelector(".chat-bubble");
appendToolCall(host, { tool: "", text: "exit 0" });
assert.equal(host.querySelector(".chat-tools").childNodes.length, 0);
});
// A bubble that predates the tool list -- a retried turn reusing an older
// one -- must still show tool activity rather than silently dropping it.
test("a bubble without a tool list still records tool activity", () => {
const row = assistantBubble();
const host = row.querySelector(".chat-bubble");
host.childNodes = host.childNodes.filter((node) => node.className !== "chat-tools");
appendToolCall(host, { tool: "shell", text: "exit 0" });
assert.equal(host.querySelector(".chat-tool-name").textContent, "shell");
});