fix: print the running tool once, not twice (#17)

This commit was merged in pull request #17.
This commit is contained in:
2026-10-05 09:49:39 +00:00
parent 26e05dea5b
commit ff99778e5d
7 changed files with 198 additions and 34 deletions
+46 -1
View File
@@ -1,9 +1,11 @@
import { describe, expect, it } from "vitest";
import { buildThread, type LiveState } from "../lib/thread";
import { buildThread, describeActivity, type LiveState } from "../lib/thread";
import type { ChatTurn } from "../lib/types";
const idle: LiveState = { isStreaming: false, response: "", thinking: "" };
const activity = { label: "Reading a page", count: 1 };
function turn(id: string, prompt: string, response: string, overrides: Partial<ChatTurn> = {}): ChatTurn {
return {
id,
@@ -133,3 +135,46 @@ describe("buildThread", () => {
expect(thread[0].error).toBe("Request failed");
});
});
describe("carrying the running tool", () => {
const live: LiveState = { isStreaming: true, response: "", thinking: "", activity };
it("puts it on the message being streamed", () => {
const thread = buildThread([turn("t1", "q", "a")], live);
const assistant = thread.find((message) => message.role === "assistant");
expect(assistant?.activity).toEqual(activity);
});
it("keeps it off every earlier message", () => {
// Only the newest turn can be mid-flight, and an older answer carrying a
// "Searching the web" line would be a claim about a finished turn.
const thread = buildThread([turn("t1", "q", "a"), turn("t2", "q2", "a2")], live);
expect(thread.slice(2).every((message) => message.activity === undefined)).toBe(true);
});
it("drops it once the turn is over", () => {
const thread = buildThread([turn("t1", "q", "a")], { ...idle, activity });
expect(thread.every((message) => message.activity === undefined)).toBe(true);
});
it("never puts one on a question", () => {
const thread = buildThread([turn("t1", "q", "a")], live);
expect(thread.find((message) => message.role === "user")?.activity).toBeUndefined();
});
});
describe("describeActivity", () => {
it("is just the label for a first call", () => {
expect(describeActivity({ label: "Reading a page", count: 1 })).toBe("Reading a page");
});
it("says a repeat is a repeat, because a bare number reads as progress", () => {
expect(describeActivity({ label: "Reading a page", count: 3 })).toBe(
"Reading a page · the same call again (3)",
);
});
});