@@ -1,974 +1,903 @@
11 import { describe, expect, test } from "bun:test";
22 import type { ToolSet } from "ai";
33 import {
4- getReasoningDeltaFromStreamChunk,
5- isOpenAIGPT,
6- normalizeOpenAICompatibleToolCallInputs,
7- sanitizeGeminiToolMessages,
8- stripGPTCommentaryFromHistory,
9- stripOpenAIItemIdsFromHistory,
10- stripToolRuntimeInputFields,
4+ getReasoningDeltaFromStreamChunk,
5+ isOpenAIGPT,
6+ normalizeOpenAICompatibleToolCallInputs,
7+ sanitizeGeminiToolMessages,
8+ stripGPTCommentaryFromHistory,
9+ stripToolRuntimeInputFields,
1110 } from "./history-transforms.ts";
1211 import type { CoreMessage } from "./turn.ts";
1312 import {
14- annotateAnthropicCacheBreakpoints,
15- applyContextPruning,
16- compactToolResultPayloads,
17- getMessageDiagnostics,
13+ annotateAnthropicCacheBreakpoints,
14+ applyContextPruning,
15+ compactToolResultPayloads,
16+ getMessageDiagnostics,
1817 } from "./turn-context.ts";
1918 import { annotateToolCaching } from "./turn-execution.ts";
2019
2120 describe("isOpenAIGPT", () => {
22- test("matches openai/gpt-* models", () => {
23- expect(isOpenAIGPT("openai/gpt-4o")).toBe(true);
24- expect(isOpenAIGPT("openai/gpt-4o-mini")).toBe(true);
25- expect(isOpenAIGPT("openai/gpt-5.3-codex")).toBe(true);
26- });
27-
28- test("matches zen/gpt-* models", () => {
29- expect(isOpenAIGPT("zen/gpt-5.3-codex")).toBe(true);
30- expect(isOpenAIGPT("zen/gpt-4o")).toBe(true);
31- });
32-
33- test("does not match non-gpt openai models", () => {
34- expect(isOpenAIGPT("openai/o3")).toBe(false);
35- expect(isOpenAIGPT("openai/o1-mini")).toBe(false);
36- });
37-
38- test("does not match other providers", () => {
39- expect(isOpenAIGPT("anthropic/claude-sonnet-4-5")).toBe(false);
40- expect(isOpenAIGPT("google/gemini-2.0-flash")).toBe(false);
41- expect(isOpenAIGPT("zen/claude-sonnet-4-6")).toBe(false);
42- });
21+ test("matches openai/gpt-* models", () => {
22+ expect(isOpenAIGPT("openai/gpt-4o")).toBe(true);
23+ expect(isOpenAIGPT("openai/gpt-4o-mini")).toBe(true);
24+ expect(isOpenAIGPT("openai/gpt-5.3-codex")).toBe(true);
25+ });
26+
27+ test("matches zen/gpt-* models", () => {
28+ expect(isOpenAIGPT("zen/gpt-5.3-codex")).toBe(true);
29+ expect(isOpenAIGPT("zen/gpt-4o")).toBe(true);
30+ });
31+
32+ test("does not match non-gpt openai models", () => {
33+ expect(isOpenAIGPT("openai/o3")).toBe(false);
34+ expect(isOpenAIGPT("openai/o1-mini")).toBe(false);
35+ });
36+
37+ test("does not match other providers", () => {
38+ expect(isOpenAIGPT("anthropic/claude-sonnet-4-5")).toBe(false);
39+ expect(isOpenAIGPT("google/gemini-2.0-flash")).toBe(false);
40+ expect(isOpenAIGPT("zen/claude-sonnet-4-6")).toBe(false);
41+ });
4342 });
4443
4544 describe("normalizeOpenAICompatibleToolCallInputs", () => {
46- const makeAssistantToolCallMessages = (input: string): CoreMessage[] => [
47- {
48- role: "assistant",
49- content: [
50- {
51- type: "tool-call",
52- toolCallId: "call_1",
53- toolName: "read",
54- input,
55- },
56- ],
57- } as unknown as CoreMessage,
58- ];
59-
60- test("parses stringified object tool-call input for zen openai-compatible chat models", () => {
61- const messages = makeAssistantToolCallMessages('{"path":"TODO.md"}');
62-
63- const normalized = normalizeOpenAICompatibleToolCallInputs(
64- messages,
65- "zen/glm-5",
66- );
67- const part = (normalized[0] as { content: Array<Record<string, unknown>> })
68- .content[0];
69- expect(part?.input).toEqual({ path: "TODO.md" });
70- });
71-
72- test("leaves non-object parsed inputs unchanged", () => {
73- const messages = makeAssistantToolCallMessages('"raw-string"');
74-
75- const normalized = normalizeOpenAICompatibleToolCallInputs(
76- messages,
77- "zen/glm-5",
78- );
79- expect(normalized).toBe(messages);
80- });
81-
82- test("does not run for non zen-openai-compatible models", () => {
83- const messages = makeAssistantToolCallMessages('{"path":"TODO.md"}');
84-
85- expect(
86- normalizeOpenAICompatibleToolCallInputs(messages, "zen/gpt-5.3-codex"),
87- ).toBe(messages);
88- expect(
89- normalizeOpenAICompatibleToolCallInputs(messages, "openai/gpt-4o"),
90- ).toBe(messages);
91- });
45+ const makeAssistantToolCallMessages = (input: string): CoreMessage[] => [
46+ {
47+ role: "assistant",
48+ content: [
49+ {
50+ type: "tool-call",
51+ toolCallId: "call_1",
52+ toolName: "read",
53+ input,
54+ },
55+ ],
56+ } as unknown as CoreMessage,
57+ ];
58+
59+ test("parses stringified object tool-call input for zen openai-compatible chat models", () => {
60+ const messages = makeAssistantToolCallMessages('{"path":"TODO.md"}');
61+
62+ const normalized = normalizeOpenAICompatibleToolCallInputs(
63+ messages,
64+ "zen/glm-5",
65+ );
66+ const part = (normalized[0] as { content: Array<Record<string, unknown>> })
67+ .content[0];
68+ expect(part?.input).toEqual({ path: "TODO.md" });
69+ });
70+
71+ test("leaves non-object parsed inputs unchanged", () => {
72+ const messages = makeAssistantToolCallMessages('"raw-string"');
73+
74+ const normalized = normalizeOpenAICompatibleToolCallInputs(
75+ messages,
76+ "zen/glm-5",
77+ );
78+ expect(normalized).toBe(messages);
79+ });
80+
81+ test("does not run for non zen-openai-compatible models", () => {
82+ const messages = makeAssistantToolCallMessages('{"path":"TODO.md"}');
83+
84+ expect(
85+ normalizeOpenAICompatibleToolCallInputs(messages, "zen/gpt-5.3-codex"),
86+ ).toBe(messages);
87+ expect(
88+ normalizeOpenAICompatibleToolCallInputs(messages, "openai/gpt-4o"),
89+ ).toBe(messages);
90+ });
9291 });
9392
9493 describe("stripToolRuntimeInputFields", () => {
95- test("strips runtime-only tool fields from assistant tool-call inputs", () => {
96- const messages: CoreMessage[] = [
97- {
98- role: "assistant",
99- content: [
100- {
101- type: "tool-call",
102- toolCallId: "call_1",
103- toolName: "read",
104- input: {
105- path: "TODO.md",
106- line: 1,
107- cwd: "/tmp/project",
108- },
109- },
110- ],
111- } as unknown as CoreMessage,
112- ];
113-
114- const stripped = stripToolRuntimeInputFields(messages);
115- const part = (stripped[0] as { content: Array<Record<string, unknown>> })
116- .content[0];
117- expect(part?.input).toEqual({ path: "TODO.md", line: 1 });
118- });
119-
120- test("leaves non-assistant messages and clean tool calls untouched", () => {
121- const messages: CoreMessage[] = [
122- { role: "user", content: "hi" },
123- {
124- role: "assistant",
125- content: [
126- {
127- type: "tool-call",
128- toolCallId: "call_2",
129- toolName: "read",
130- input: { path: "TODO.md" },
131- },
132- ],
133- } as unknown as CoreMessage,
134- ];
135-
136- expect(stripToolRuntimeInputFields(messages)).toBe(messages);
137- });
94+ test("strips runtime-only tool fields from assistant tool-call inputs", () => {
95+ const messages: CoreMessage[] = [
96+ {
97+ role: "assistant",
98+ content: [
99+ {
100+ type: "tool-call",
101+ toolCallId: "call_1",
102+ toolName: "read",
103+ input: {
104+ path: "TODO.md",
105+ line: 1,
106+ cwd: "/tmp/project",
107+ },
108+ },
109+ ],
110+ } as unknown as CoreMessage,
111+ ];
112+
113+ const stripped = stripToolRuntimeInputFields(messages);
114+ const part = (stripped[0] as { content: Array<Record<string, unknown>> })
115+ .content[0];
116+ expect(part?.input).toEqual({ path: "TODO.md", line: 1 });
117+ });
118+
119+ test("leaves non-assistant messages and clean tool calls untouched", () => {
120+ const messages: CoreMessage[] = [
121+ { role: "user", content: "hi" },
122+ {
123+ role: "assistant",
124+ content: [
125+ {
126+ type: "tool-call",
127+ toolCallId: "call_2",
128+ toolName: "read",
129+ input: { path: "TODO.md" },
130+ },
131+ ],
132+ } as unknown as CoreMessage,
133+ ];
134+
135+ expect(stripToolRuntimeInputFields(messages)).toBe(messages);
136+ });
138137 });
139138
140139 describe("getReasoningDeltaFromStreamChunk", () => {
141- test("reads SDK reasoning-delta text field", () => {
142- expect(
143- getReasoningDeltaFromStreamChunk({
144- type: "reasoning-delta",
145- text: "step by step",
146- }),
147- ).toBe("step by step");
148- });
149-
150- test("keeps compatibility with legacy reasoning textDelta field", () => {
151- expect(
152- getReasoningDeltaFromStreamChunk({
153- type: "reasoning",
154- textDelta: "legacy reasoning",
155- }),
156- ).toBe("legacy reasoning");
157- });
158-
159- test("reads SDK reasoning-delta delta field", () => {
160- expect(
161- getReasoningDeltaFromStreamChunk({
162- type: "reasoning",
163- delta: "new delta",
164- }),
165- ).toBe("new delta");
166- });
167-
168- test("returns null for non-reasoning chunks", () => {
169- expect(
170- getReasoningDeltaFromStreamChunk({ type: "text-delta", text: "hi" }),
171- ).toBe(null);
172- });
140+ test("reads SDK reasoning-delta text field", () => {
141+ expect(
142+ getReasoningDeltaFromStreamChunk({
143+ type: "reasoning-delta",
144+ text: "step by step",
145+ }),
146+ ).toBe("step by step");
147+ });
148+
149+ test("keeps compatibility with legacy reasoning textDelta field", () => {
150+ expect(
151+ getReasoningDeltaFromStreamChunk({
152+ type: "reasoning",
153+ textDelta: "legacy reasoning",
154+ }),
155+ ).toBe("legacy reasoning");
156+ });
157+
158+ test("reads SDK reasoning-delta delta field", () => {
159+ expect(
160+ getReasoningDeltaFromStreamChunk({
161+ type: "reasoning",
162+ delta: "new delta",
163+ }),
164+ ).toBe("new delta");
165+ });
166+
167+ test("returns null for non-reasoning chunks", () => {
168+ expect(
169+ getReasoningDeltaFromStreamChunk({ type: "text-delta", text: "hi" }),
170+ ).toBe(null);
171+ });
173172 });
174173
175174 describe("sanitizeGeminiToolMessages", () => {
176- const healthyGeminiToolTurn = (userContent: string): CoreMessage[] =>
177- [
178- { role: "user", content: userContent },
179- {
180- role: "assistant",
181- content: [
182- {
183- type: "tool-call",
184- toolCallId: "healthy-call",
185- toolName: "read",
186- input: {},
187- providerOptions: { google: { thoughtSignature: "sig-ok" } },
188- },
189- ],
190- },
191- {
192- role: "tool",
193- content: [
194- {
195- type: "tool-result",
196- toolCallId: "healthy-call",
197- toolName: "read",
198- output: { type: "json", value: { ok: true } },
199- },
200- ],
201- },
202- ] as unknown as CoreMessage[];
203-
204- const brokenGeminiToolTurn = (toolCallId = "broken-call"): CoreMessage[] =>
205- [
206- {
207- role: "assistant",
208- content: [
209- {
210- type: "tool-call",
211- toolCallId,
212- toolName: "shell",
213- input: {},
214- },
215- ],
216- },
217- {
218- role: "tool",
219- content: [
220- {
221- type: "tool-result",
222- toolCallId,
223- toolName: "shell",
224- output: { type: "json", value: { ok: false } },
225- },
226- ],
227- },
228- ] as unknown as CoreMessage[];
229-
230- test("valid signed single call preserved", () => {
231- const messages: CoreMessage[] = [
232- { role: "user", content: "current turn" },
233- {
234- role: "assistant",
235- content: [
236- {
237- type: "tool-call",
238- toolCallId: "call-1",
239- toolName: "read",
240- input: {},
241- providerOptions: { google: { thoughtSignature: "sig-1" } },
242- },
243- ],
244- } as unknown as CoreMessage,
245- ];
246-
247- expect(
248- sanitizeGeminiToolMessages(messages, "google/gemini-2.5-pro", true),
249- ).toEqual(messages);
250- });
251-
252- test("valid parallel calls with only first signed preserved", () => {
253- const messages: CoreMessage[] = [
254- { role: "user", content: "parallel call turn" },
255- {
256- role: "assistant",
257- content: [
258- {
259- type: "tool-call",
260- toolCallId: "signed-anchor",
261- toolName: "read",
262- input: {},
263- providerOptions: { google: { thoughtSignature: "sig-anchor" } },
264- },
265- {
266- type: "tool-call",
267- toolCallId: "unsigned-followup",
Cut short: this commit's diff is over 1000 lines.