web: chat reasoning block and tool calls for the whole lifecycle (C6)

This commit is contained in:
2026-10-19 06:14:48 +01:00
parent f48e53d5c8
commit 7df4891f14
6 changed files with 1200 additions and 17 deletions
+221 -1
View File
@@ -7,6 +7,9 @@ import assert from "node:assert/strict";
import {
CHAT_FOLLOW_THRESHOLD_PX,
ChatTurn,
MAX_RENDERED_DIFF_LINES,
TOOL_DETAIL_MAX_LENGTH,
TOOL_LONG_RUNNING_MILLIS,
chatBottomDistancePx,
chatFirstScroll,
chatFollowsAt,
@@ -17,10 +20,14 @@ import {
closeChatConfirmation,
directoryLabel,
filterSessions,
formatToolDuration,
groupSessionsByDirectory,
lineDiff,
mergeHistoryPage,
partRendering,
prettyToolInput,
reasoningLabel,
runningToolElapsedMillis,
sessionActivity,
sessionActivityBadge,
sessionChipLabel,
@@ -29,9 +36,23 @@ import {
sessionStatusLabel,
sessionTitle,
sessionWorkLabel,
toolCallCount,
toolCallRow,
toolCallsAnnouncement,
toolCallsLabel,
toolDetail,
toolDurationMillis,
toolEditDiff,
toolErrorText,
toolFailed,
toolLongRunning,
toolOutputText,
toolRunning,
toolsRunning,
transcriptRows,
turnElapsedMillis,
} from "../src/chat.js";
import { ChangeLineKind } from "../src/changes.js";
import { PipelinesClient, ProtocolError } from "../src/client.js";
import { PipelinesRepository } from "../src/repository.js";
@@ -97,11 +118,210 @@ test("toolCallsLabel never pluralises a count of one", () => {
assert.equal(toolCallsLabel(4), "4 tools called");
});
test("reasoningLabel keeps the one-block wording and counts multiple steps", () => {
test("reasoningLabel is Thinking/Reasoning with the step count and streaming word", () => {
assert.equal(reasoningLabel(1, false), "Reasoning");
assert.equal(reasoningLabel(1, true), "Thinking…");
assert.equal(reasoningLabel(3, false), "Reasoning · 3 steps");
assert.equal(reasoningLabel(3, true), "Thinking · 3 steps");
assert.equal(reasoningLabel(1), "Reasoning");
assert.equal(reasoningLabel(3), "Reasoning · 3 steps");
});
// --- Tool-call lifecycle, detail and the edit diff (C6) ---------------------
test("toolRunning treats streaming and running as in flight, completed/error as settled", () => {
assert.equal(toolRunning({ type: "tool", state: { status: "streaming" } }), true);
assert.equal(toolRunning({ type: "tool", state: { status: "running" } }), true);
assert.equal(toolRunning({ type: "tool", state: { status: "completed" } }), false);
assert.equal(toolRunning({ type: "tool", state: { status: "error" } }), false);
assert.equal(toolRunning({ type: "tool" }), false);
assert.equal(toolRunning({ type: "text", state: { status: "running" } }), false);
});
test("toolsRunning and toolFailed summarise the lifecycle", () => {
const call = (status) => ({ type: "tool", state: { status } });
assert.equal(toolsRunning([call("completed"), call("running")]), true);
assert.equal(toolsRunning([call("completed"), call("error")]), false);
assert.equal(toolsRunning([]), false);
assert.equal(toolFailed({ status: "error" }), true);
assert.equal(toolFailed({ status: "completed" }), false);
assert.equal(toolFailed(undefined), false);
});
test("toolCallCount counts only tool parts and toolCallsAnnouncement matches the plural", () => {
const parts = [{ type: "tool" }, { type: "text" }, { type: "reasoning" }, { type: "tool" }];
assert.equal(toolCallCount(parts), 2);
assert.equal(toolCallCount([]), 0);
assert.equal(toolCallsAnnouncement(1), "1 tool call, expand for details");
assert.equal(toolCallsAnnouncement(3), "3 tool calls, expand for details");
});
test("toolDetail takes the first present detail key, collapsed and trimmed to 72", () => {
const part = (input) => ({ type: "tool", state: { input } });
assert.equal(toolDetail(part({ command: "ls -la" })), "ls -la");
assert.equal(toolDetail(part({ description: "do a thing", command: "x" })), "do a thing");
assert.equal(toolDetail(part({ filePath: "/a/b.ts" })), "/a/b.ts");
assert.equal(toolDetail(part({ path: "p", command: "c" })), "c");
assert.equal(toolDetail(part({ command: " a b\tc\n d " })), "a b c d");
const detail = toolDetail(part({ command: "x".repeat(100) }));
assert.equal(detail.length, TOOL_DETAIL_MAX_LENGTH);
assert.ok(detail.endsWith("…"));
assert.equal(toolDetail(part({ other: "nope" })), null);
assert.equal(toolDetail(part("partial json")), null);
assert.equal(toolDetail(part({ command: 42 })), null);
assert.equal(toolDetail({ type: "tool" }), null);
});
test("formatToolDuration renders ms, one-decimal seconds and minutes", () => {
assert.equal(formatToolDuration(340), "340ms");
assert.equal(formatToolDuration(999), "999ms");
assert.equal(formatToolDuration(1000), "1.0s");
assert.equal(formatToolDuration(1200), "1.2s");
assert.equal(formatToolDuration(59600), "59.6s");
assert.equal(formatToolDuration(60000), "1m 0s");
assert.equal(formatToolDuration(123000), "2m 3s");
});
test("toolDurationMillis reads ran-else-created to completed, null when unusable", () => {
assert.equal(toolDurationMillis({ time: { created: 10, ran: 20, completed: 120 } }), 100);
assert.equal(toolDurationMillis({ time: { created: 10, ran: 0, completed: 110 } }), 100);
assert.equal(toolDurationMillis({ time: { created: 10, ran: 20 } }), null);
assert.equal(toolDurationMillis({ time: { created: 10, ran: 20, completed: 5 } }), null);
assert.equal(toolDurationMillis({}), null);
});
test("runningToolElapsedMillis prefers the protocol start and falls back to the local stamp", () => {
const part = { type: "tool", time: { created: 100, ran: 500 } };
assert.equal(runningToolElapsedMillis(part, 999, 1500), 1000);
assert.equal(runningToolElapsedMillis({ type: "tool" }, 1000, 4000), 3000);
assert.equal(runningToolElapsedMillis({ type: "tool" }, 0, 4000), 0);
assert.equal(toolLongRunning(TOOL_LONG_RUNNING_MILLIS), true);
assert.equal(toolLongRunning(TOOL_LONG_RUNNING_MILLIS - 1), false);
});
test("toolOutputText joins text content, (no output) when settled-empty and '' in flight", () => {
const state = (status, content) => ({ status, content });
assert.equal(
toolOutputText(state("completed", [{ type: "text", text: "a" }, { type: "text", text: "b" }])),
"a\nb",
);
assert.equal(toolOutputText(state("completed", [{ type: "text", text: " " }])), "(no output)");
assert.equal(toolOutputText(state("running", [])), "");
assert.equal(toolOutputText(state("streaming", [])), "");
assert.equal(toolOutputText(state("error", [])), "(no output)");
assert.equal(toolOutputText(null), "");
});
test("toolErrorText prefers the server's error message over the output", () => {
assert.equal(
toolErrorText({ status: "error", error: { message: "boom" }, content: [{ type: "text", text: "partial" }] }),
"boom",
);
assert.equal(toolErrorText({ status: "error", content: [{ type: "text", text: "partial" }] }), "partial");
assert.equal(toolErrorText({ status: "error", error: { message: " " }, content: [] }), "(no output)");
});
test("prettyToolInput pretty-prints objects, keeps a partial string and blanks nothing", () => {
assert.equal(prettyToolInput(null), "");
assert.equal(prettyToolInput("{\"a\":"), "{\"a\":");
assert.equal(prettyToolInput({ a: 1 }), '{\n "a": 1\n}');
assert.equal(prettyToolInput([1, 2]), "[\n 1,\n 2\n]");
});
test("lineDiff LCS marks only changed lines and carries ChangeLineKind values", () => {
const lines = lineDiff(["one", "two", "three"], ["one", "TWO", "three"]);
assert.deepEqual(lines, [
{ kind: ChangeLineKind.Context, text: "one" },
{ kind: ChangeLineKind.Remove, text: "two" },
{ kind: ChangeLineKind.Add, text: "TWO" },
{ kind: ChangeLineKind.Context, text: "three" },
]);
});
test("toolEditDiff derives the file, true counts and the LCS line kinds", () => {
const diff = toolEditDiff({ filePath: "a.ts", oldString: "one\ntwo\nthree", newString: "one\nTWO\nthree" });
assert.equal(diff.file, "a.ts");
assert.equal(diff.added, 1);
assert.equal(diff.removed, 1);
assert.equal(diff.omitted, 0);
assert.deepEqual(diff.lines, [
{ kind: ChangeLineKind.Context, text: "one" },
{ kind: ChangeLineKind.Remove, text: "two" },
{ kind: ChangeLineKind.Add, text: "TWO" },
{ kind: ChangeLineKind.Context, text: "three" },
]);
assert.equal(toolEditDiff({ oldString: "", newString: "" }), null);
assert.equal(toolEditDiff({ oldString: "x" }), null);
assert.equal(toolEditDiff({ command: "ls" }), null);
assert.equal(toolEditDiff("nope"), null);
});
test("toolEditDiff caps rendered lines at MAX_RENDERED_DIFF_LINES and counts the rest", () => {
const oldLines = Array.from({ length: 300 }, (_, i) => `old-${i}`);
const newLines = Array.from({ length: 300 }, (_, i) => `new-${i}`);
const diff = toolEditDiff({ oldString: oldLines.join("\n"), newString: newLines.join("\n") });
assert.equal(diff.lines.length, MAX_RENDERED_DIFF_LINES);
assert.equal(diff.added, 300);
assert.equal(diff.removed, 300);
assert.equal(diff.omitted, 600 - MAX_RENDERED_DIFF_LINES);
});
test("lineDiff falls back to remove-then-add past the algorithm guard", () => {
const lines = lineDiff(Array.from({ length: 401 }, () => "a"), ["b"]);
assert.equal(lines.length, 402);
assert.equal(lines[0].kind, ChangeLineKind.Remove);
assert.equal(lines.at(-1).kind, ChangeLineKind.Add);
});
test("toolCallRow composes the name, detail, duration, input, output, error and diff", () => {
const part = {
type: "tool",
id: "t1",
name: "edit",
state: {
status: "completed",
input: { filePath: "a.ts", oldString: "one", newString: "two" },
content: [{ type: "text", text: "done" }],
},
time: { created: 10, ran: 20, completed: 120 },
};
const row = toolCallRow(part);
assert.equal(row.name, "edit");
assert.equal(row.detail, "a.ts");
assert.equal(row.duration, "100ms");
assert.equal(row.input, '{\n "filePath": "a.ts",\n "oldString": "one",\n "newString": "two"\n}');
assert.equal(row.output, "done");
assert.equal(row.error, null);
assert.equal(row.diff.file, "a.ts");
assert.equal(row.diff.added, 1);
assert.equal(row.diff.removed, 1);
});
test("toolCallRow marks a failed call with its error text and no diff", () => {
const row = toolCallRow({
type: "tool",
id: "t2",
name: "shell",
state: { status: "error", input: { command: "false" }, error: { message: "exit 1" } },
});
assert.equal(row.name, "shell");
assert.equal(row.detail, "false");
assert.equal(row.duration, null);
assert.equal(row.error, "exit 1");
assert.equal(row.diff, null);
});
test("turnElapsedMillis spans earliest start to latest completion and is null when unusable", () => {
const parts = [
{ time: { created: 1000, ran: 1200, completed: 5000 } },
{ time: { created: 800, ran: 0, completed: 4000 } },
];
assert.equal(turnElapsedMillis(parts), 4200);
assert.equal(turnElapsedMillis([{ time: { created: 1000, ran: 1200 } }]), null);
assert.equal(turnElapsedMillis([{}]), null);
assert.equal(turnElapsedMillis([{ time: { created: 1000, ran: 1000, completed: 1000 } }]), null);
});
// --- Session identity -------------------------------------------------------
test("sessionTitle falls back from the title to the first 18 characters of the id", () => {