@@ -1,180 +1,180 @@
11 import { describe, expect, test } from "bun:test";
22 import type { CoreMessage } from "../llm-api/turn.ts";
33 import {
4- extractAssistantText,
5- makeInterruptMessage,
6- sanitizeModelAuthoredMessages,
4+ extractAssistantText,
5+ makeInterruptMessage,
6+ sanitizeModelAuthoredMessages,
77 } from "./agent-helpers.ts";
88
99 // ─── extractAssistantText ─────────────────────────────────────────────────────
1010
1111 describe("extractAssistantText", () => {
12- test("returns empty string for empty message list", () => {
13- expect(extractAssistantText([])).toBe("");
14- });
15-
16- test("returns empty string when no assistant messages present", () => {
17- const msgs: CoreMessage[] = [{ role: "user", content: "hello" }];
18- expect(extractAssistantText(msgs)).toBe("");
19- });
20-
21- test("extracts string content from an assistant message", () => {
22- const msgs: CoreMessage[] = [{ role: "assistant", content: "All done." }];
23- expect(extractAssistantText(msgs)).toBe("All done.");
24- });
25-
26- test("extracts text parts from array content", () => {
27- const msgs: CoreMessage[] = [
28- {
29- role: "assistant",
30- content: [
31- { type: "text", text: "I will run the tests." },
32- { type: "tool-call", toolCallId: "1", toolName: "shell", input: {} },
33- ],
34- },
35- ];
36- expect(extractAssistantText(msgs)).toBe("I will run the tests.");
37- });
38-
39- test("concatenates text across multiple assistant messages", () => {
40- const msgs: CoreMessage[] = [
41- { role: "user", content: "go" },
42- { role: "assistant", content: "Fixing the bug." },
43- { role: "assistant", content: "All done." },
44- ];
45- expect(extractAssistantText(msgs)).toBe("Fixing the bug.\nAll done.");
46- });
47-
48- test("returns empty string when assistant message has no text parts", () => {
49- const msgs: CoreMessage[] = [
50- {
51- role: "assistant",
52- content: [
53- { type: "tool-call", toolCallId: "1", toolName: "shell", input: {} },
54- ],
55- },
56- ];
57- expect(extractAssistantText(msgs)).toBe("");
58- });
59-
60- test("extracts text from array content when last part is a tool call", () => {
61- const msgs: CoreMessage[] = [
62- {
63- role: "assistant",
64- content: [
65- { type: "text", text: "Tests pass." },
66- { type: "tool-call", toolCallId: "1", toolName: "shell", input: {} },
67- ],
68- },
69- ];
70- expect(extractAssistantText(msgs)).toBe("Tests pass.");
71- });
12+ test("returns empty string for empty message list", () => {
13+ expect(extractAssistantText([])).toBe("");
14+ });
15+
16+ test("returns empty string when no assistant messages present", () => {
17+ const msgs: CoreMessage[] = [{ role: "user", content: "hello" }];
18+ expect(extractAssistantText(msgs)).toBe("");
19+ });
20+
21+ test("extracts string content from an assistant message", () => {
22+ const msgs: CoreMessage[] = [{ role: "assistant", content: "All done." }];
23+ expect(extractAssistantText(msgs)).toBe("All done.");
24+ });
25+
26+ test("extracts text parts from array content", () => {
27+ const msgs: CoreMessage[] = [
28+ {
29+ role: "assistant",
30+ content: [
31+ { type: "text", text: "I will run the tests." },
32+ { type: "tool-call", toolCallId: "1", toolName: "shell", input: {} },
33+ ],
34+ },
35+ ];
36+ expect(extractAssistantText(msgs)).toBe("I will run the tests.");
37+ });
38+
39+ test("concatenates text across multiple assistant messages", () => {
40+ const msgs: CoreMessage[] = [
41+ { role: "user", content: "go" },
42+ { role: "assistant", content: "Fixing the bug." },
43+ { role: "assistant", content: "All done." },
44+ ];
45+ expect(extractAssistantText(msgs)).toBe("Fixing the bug.\nAll done.");
46+ });
47+
48+ test("returns empty string when assistant message has no text parts", () => {
49+ const msgs: CoreMessage[] = [
50+ {
51+ role: "assistant",
52+ content: [
53+ { type: "tool-call", toolCallId: "1", toolName: "shell", input: {} },
54+ ],
55+ },
56+ ];
57+ expect(extractAssistantText(msgs)).toBe("");
58+ });
59+
60+ test("extracts text from array content when last part is a tool call", () => {
61+ const msgs: CoreMessage[] = [
62+ {
63+ role: "assistant",
64+ content: [
65+ { type: "text", text: "Tests pass." },
66+ { type: "tool-call", toolCallId: "1", toolName: "shell", input: {} },
67+ ],
68+ },
69+ ];
70+ expect(extractAssistantText(msgs)).toBe("Tests pass.");
71+ });
7272 });
7373
7474 describe("sanitizeModelAuthoredMessages", () => {
75- test("strips GPT commentary parts before messages are reused or persisted", () => {
76- const msgs: CoreMessage[] = [
77- {
78- role: "assistant",
79- content: [
80- { type: "reasoning", text: "plan" },
81- {
82- type: "text",
83- text: "hidden commentary",
84- providerOptions: { openai: { phase: "commentary" } },
85- },
86- {
87- type: "text",
88- text: "final answer",
89- providerOptions: { openai: { phase: "final_answer" } },
90- },
91- ],
92- } as unknown as CoreMessage,
93- ];
94-
95- expect(sanitizeModelAuthoredMessages(msgs, "openai/gpt-5.3-codex")).toEqual(
96- [
97- {
98- role: "assistant",
99- content: [
100- { type: "reasoning", text: "plan" },
101- {
102- type: "text",
103- text: "final answer",
104- providerOptions: { openai: { phase: "final_answer" } },
105- },
106- ],
107- },
108- ],
109- );
110- });
111-
112- test("strips runtime-only tool fields from persisted tool calls", () => {
113- const msgs: CoreMessage[] = [
114- {
115- role: "assistant",
116- content: [
117- {
118- type: "tool-call",
119- toolCallId: "1",
120- toolName: "read",
121- input: {
122- path: "TODO.md",
123- cwd: "/tmp/project",
124- },
125- },
126- ],
127- } as unknown as CoreMessage,
128- ];
129-
130- expect(
131- sanitizeModelAuthoredMessages(msgs, "anthropic/claude-sonnet-4-5"),
132- ).toEqual([
133- {
134- role: "assistant",
135- content: [
136- {
137- type: "tool-call",
138- toolCallId: "1",
139- toolName: "read",
140- input: { path: "TODO.md" },
141- },
142- ],
143- },
144- ]);
145- });
75+ test("strips GPT commentary parts before messages are reused or persisted", () => {
76+ const msgs: CoreMessage[] = [
77+ {
78+ role: "assistant",
79+ content: [
80+ { type: "reasoning", text: "plan" },
81+ {
82+ type: "text",
83+ text: "hidden commentary",
84+ providerOptions: { openai: { phase: "commentary" } },
85+ },
86+ {
87+ type: "text",
88+ text: "final answer",
89+ providerOptions: { openai: { phase: "final_answer" } },
90+ },
91+ ],
92+ } as unknown as CoreMessage,
93+ ];
94+
95+ expect(sanitizeModelAuthoredMessages(msgs, "openai/gpt-5.3-codex")).toEqual(
96+ [
97+ {
98+ role: "assistant",
99+ content: [
100+ { type: "reasoning", text: "plan" },
101+ {
102+ type: "text",
103+ text: "final answer",
104+ providerOptions: { openai: { phase: "final_answer" } },
105+ },
106+ ],
107+ },
108+ ],
109+ );
110+ });
111+
112+ test("strips runtime-only tool fields from persisted tool calls", () => {
113+ const msgs: CoreMessage[] = [
114+ {
115+ role: "assistant",
116+ content: [
117+ {
118+ type: "tool-call",
119+ toolCallId: "1",
120+ toolName: "read",
121+ input: {
122+ path: "TODO.md",
123+ cwd: "/tmp/project",
124+ },
125+ },
126+ ],
127+ } as unknown as CoreMessage,
128+ ];
129+
130+ expect(
131+ sanitizeModelAuthoredMessages(msgs, "anthropic/claude-sonnet-4-5"),
132+ ).toEqual([
133+ {
134+ role: "assistant",
135+ content: [
136+ {
137+ type: "tool-call",
138+ toolCallId: "1",
139+ toolName: "read",
140+ input: { path: "TODO.md" },
141+ },
142+ ],
143+ },
144+ ]);
145+ });
146146 });
147147
148148 // ─── makeInterruptMessage ─────────────────────────────────────────────────────
149149
150150 describe("makeInterruptMessage", () => {
151- test("user reason produces an assistant role message", () => {
152- const msg = makeInterruptMessage("user");
153- expect(msg.role).toBe("assistant");
154- });
155-
156- test("error reason produces an assistant role message", () => {
157- const msg = makeInterruptMessage("error");
158- expect(msg.role).toBe("assistant");
159- });
160-
161- test("user reason content contains system-message tag", () => {
162- const msg = makeInterruptMessage("user");
163- expect(msg.content).toContain("<system-message>");
164- expect(msg.content).toContain("</system-message>");
165- expect(msg.content).toContain("interrupted by the user");
166- });
167-
168- test("error reason content contains system-message tag", () => {
169- const msg = makeInterruptMessage("error");
170- expect(msg.content).toContain("<system-message>");
171- expect(msg.content).toContain("</system-message>");
172- expect(msg.content).toContain("interrupted due to an error");
173- });
174-
175- test("user and error produce different messages", () => {
176- expect(makeInterruptMessage("user").content).not.toBe(
177- makeInterruptMessage("error").content,
178- );
179- });
151+ test("user reason produces an assistant role message", () => {
152+ const msg = makeInterruptMessage("user");
153+ expect(msg.role).toBe("assistant");
154+ });
155+
156+ test("error reason produces an assistant role message", () => {
157+ const msg = makeInterruptMessage("error");
158+ expect(msg.role).toBe("assistant");
159+ });
160+
161+ test("user reason content contains system-message tag", () => {
162+ const msg = makeInterruptMessage("user");
163+ expect(msg.content).toContain("<system-message>");
164+ expect(msg.content).toContain("</system-message>");
165+ expect(msg.content).toContain("interrupted by the user");
166+ });
167+
168+ test("error reason content contains system-message tag", () => {
169+ const msg = makeInterruptMessage("error");
170+ expect(msg.content).toContain("<system-message>");
171+ expect(msg.content).toContain("</system-message>");
172+ expect(msg.content).toContain("interrupted due to an error");
173+ });
174+
175+ test("user and error produce different messages", () => {
176+ expect(makeInterruptMessage("user").content).not.toBe(
177+ makeInterruptMessage("error").content,
178+ );
179+ });
180180 });