xonecas.io
64875
Mc

mini-coder / commit 670d7e4

refactor: remove dead Anthropic thinking code and unused stripOpenAIItemIdsFromHistory

- Remove unreachable non-adaptive Anthropic thinking path (budgetTokens + dead
  'betas' field that used wrong key name and referenced graduated beta)
- Remove ANTHROPIC_BUDGET map (no longer used)
- Simplify getAnthropicThinkingOptions to always use adaptive thinking with
  effort mapping, which works for all current Anthropic thinking models
- Remove standalone stripOpenAIItemIdsFromHistory export (dead code, itemId
  stripping still happens internally in stripOpenAIHistoryTransforms)
- Remove corresponding test block for stripOpenAIItemIdsFromHistory

Verified against AI SDK v6.0.127 source:
- SDK uses 'anthropicBeta' not 'betas' for provider options
- 'interleaved-thinking-2025-05-14' beta no longer exists in SDK
- All Anthropic thinking models match isAdaptive, making else branch unreachable
- itemId stripping with store:false is harmless but unnecessary for correctness

xonecas2026-03-20 670d7e4997963c1ff9548e4bdddd64c3bf4457f4 parent 524dc76

4 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

src/llm-api/history-transforms.ts+6−7
@@ -1,14 +1,13 @@
11 export {
2- sanitizeGeminiToolMessages,
3- sanitizeGeminiToolMessagesWithMetadata,
2+ sanitizeGeminiToolMessages,
3+ sanitizeGeminiToolMessagesWithMetadata,
44 } from "./history/gemini.ts";
55
66 export {
7- isOpenAIGPT,
8- normalizeOpenAICompatibleToolCallInputs,
9- stripGPTCommentaryFromHistory,
10- stripOpenAIHistoryTransforms,
11- stripOpenAIItemIdsFromHistory,
7+ isOpenAIGPT,
8+ normalizeOpenAICompatibleToolCallInputs,
9+ stripGPTCommentaryFromHistory,
10+ stripOpenAIHistoryTransforms,
1211 } from "./history/openai.ts";
1312 export { getReasoningDeltaFromStreamChunk } from "./history/reasoning.ts";
1413 export { stripToolRuntimeInputFields } from "./history/shared.ts";
src/llm-api/history/openai.ts+137−165
@@ -1,203 +1,175 @@
11 import {
2- isOpenAIGPTModelFamily,
3- isZenOpenAICompatibleChatModel,
2+ isOpenAIGPTModelFamily,
3+ isZenOpenAICompatibleChatModel,
44 } from "../model-routing.ts";
55 import type { CoreMessage } from "../turn.ts";
66 import { getPartProviderOptions, isRecord, isToolCallPart } from "./shared.ts";
77
88 function stripOpenAIItemIdFromPart(part: unknown): {
9- part: unknown;
10- changed: boolean;
9+ part: unknown;
10+ changed: boolean;
1111 } {
12- if (!isRecord(part)) return { part, changed: false };
12+ if (!isRecord(part)) return { part, changed: false };
1313
14- let changed = false;
15- const nextPart = { ...part };
14+ let changed = false;
15+ const nextPart = { ...part };
1616
17- const dropItemId = (field: "providerOptions" | "providerMetadata"): void => {
18- const source = nextPart[field];
19- if (!isRecord(source)) return;
20- const openai = source.openai;
21- if (!isRecord(openai) || !("itemId" in openai)) return;
17+ const dropItemId = (field: "providerOptions" | "providerMetadata"): void => {
18+ const source = nextPart[field];
19+ if (!isRecord(source)) return;
20+ const openai = source.openai;
21+ if (!isRecord(openai) || !("itemId" in openai)) return;
2222
23- const nextOpenAI = { ...openai };
24- delete nextOpenAI.itemId;
25- nextPart[field] = { ...source, openai: nextOpenAI };
26- changed = true;
27- };
23+ const nextOpenAI = { ...openai };
24+ delete nextOpenAI.itemId;
25+ nextPart[field] = { ...source, openai: nextOpenAI };
26+ changed = true;
27+ };
2828
29- dropItemId("providerOptions");
30- dropItemId("providerMetadata");
29+ dropItemId("providerOptions");
30+ dropItemId("providerMetadata");
3131
32- return { part: changed ? nextPart : part, changed };
32+ return { part: changed ? nextPart : part, changed };
3333 }
3434
3535 function getOpenAITextPhase(part: unknown): string | null {
36- const providerOptions = getPartProviderOptions(part);
37- if (!providerOptions) return null;
38- const openai = providerOptions.openai;
39- if (!isRecord(openai)) return null;
40- return typeof openai.phase === "string" ? openai.phase : null;
36+ const providerOptions = getPartProviderOptions(part);
37+ if (!providerOptions) return null;
38+ const openai = providerOptions.openai;
39+ if (!isRecord(openai)) return null;
40+ return typeof openai.phase === "string" ? openai.phase : null;
4141 }
4242
4343 function isCommentaryTextPart(part: unknown): boolean {
44- if (!isRecord(part) || part.type !== "text") return false;
45- return getOpenAITextPhase(part) === "commentary";
44+ if (!isRecord(part) || part.type !== "text") return false;
45+ return getOpenAITextPhase(part) === "commentary";
4646 }
4747
4848 export function isOpenAIGPT(modelString: string): boolean {
49- return isOpenAIGPTModelFamily(modelString);
50-}
51-
52-export function stripOpenAIItemIdsFromHistory(
53- messages: CoreMessage[],
54- modelString: string,
55-): CoreMessage[] {
56- if (!isOpenAIGPT(modelString)) return messages;
57-
58- let mutated = false;
59- const result = messages.map((message) => {
60- if (!Array.isArray(message.content)) return message;
61-
62- let contentMutated = false;
63- const content = message.content.map((part) => {
64- const cleaned = stripOpenAIItemIdFromPart(part);
65- if (cleaned.changed) contentMutated = true;
66- return cleaned.part;
67- });
68-
69- if (!contentMutated) return message;
70- mutated = true;
71- return {
72- ...message,
73- content: content as CoreMessage["content"],
74- } as CoreMessage;
75- });
76-
77- return mutated ? result : messages;
49+ return isOpenAIGPTModelFamily(modelString);
7850 }
7951
8052 export function normalizeOpenAICompatibleToolCallInputs(
81- messages: CoreMessage[],
82- modelString: string,
53+ messages: CoreMessage[],
54+ modelString: string,
8355 ): CoreMessage[] {
84- if (!isZenOpenAICompatibleChatModel(modelString)) return messages;
85-
86- let mutated = false;
87- const result = messages.map((message) => {
88- if (message.role !== "assistant" || !Array.isArray(message.content)) {
89- return message;
90- }
91-
92- let contentMutated = false;
93- const nextContent = message.content.map((part) => {
94- if (
95- !isToolCallPart(part) ||
96- !("input" in part) ||
97- typeof part.input !== "string"
98- ) {
99- return part;
100- }
101-
102- try {
103- const parsed = JSON.parse(part.input);
104- if (!isRecord(parsed) || Array.isArray(parsed)) return part;
105- contentMutated = true;
106- return {
107- ...part,
108- input: parsed,
109- };
110- } catch {
111- return part;
112- }
113- });
114-
115- if (!contentMutated) return message;
116- mutated = true;
117- return {
118- ...message,
119- content: nextContent as CoreMessage["content"],
120- } as CoreMessage;
121- });
122-
123- return mutated ? result : messages;
56+ if (!isZenOpenAICompatibleChatModel(modelString)) return messages;
57+
58+ let mutated = false;
59+ const result = messages.map((message) => {
60+ if (message.role !== "assistant" || !Array.isArray(message.content)) {
61+ return message;
62+ }
63+
64+ let contentMutated = false;
65+ const nextContent = message.content.map((part) => {
66+ if (
67+ !isToolCallPart(part) ||
68+ !("input" in part) ||
69+ typeof part.input !== "string"
70+ ) {
71+ return part;
72+ }
73+
74+ try {
75+ const parsed = JSON.parse(part.input);
76+ if (!isRecord(parsed) || Array.isArray(parsed)) return part;
77+ contentMutated = true;
78+ return {
79+ ...part,
80+ input: parsed,
81+ };
82+ } catch {
83+ return part;
84+ }
85+ });
86+
87+ if (!contentMutated) return message;
88+ mutated = true;
89+ return {
90+ ...message,
91+ content: nextContent as CoreMessage["content"],
92+ } as CoreMessage;
93+ });
94+
95+ return mutated ? result : messages;
12496 }
12597
12698 function stripOpenAIHistory(
127- messages: CoreMessage[],
128- modelString: string,
129- options: { stripItemIds: boolean },
99+ messages: CoreMessage[],
100+ modelString: string,
101+ options: { stripItemIds: boolean },
130102 ): CoreMessage[] {
131- if (!isOpenAIGPT(modelString)) return messages;
132-
133- let mutated = false;
134- const result: CoreMessage[] = [];
135- let skipToolResults = false;
136-
137- for (const message of messages) {
138- if (skipToolResults) {
139- if (message.role === "tool") {
140- mutated = true;
141- continue;
142- }
143- skipToolResults = false;
144- }
145-
146- let messageForCommentary = message;
147- if (options.stripItemIds && Array.isArray(message.content)) {
148- let contentMutated = false;
149- const strippedContent = message.content.map((part) => {
150- const cleaned = stripOpenAIItemIdFromPart(part);
151- if (cleaned.changed) contentMutated = true;
152- return cleaned.part;
153- });
154- if (contentMutated) {
155- mutated = true;
156- messageForCommentary = {
157- ...message,
158- content: strippedContent as CoreMessage["content"],
159- } as CoreMessage;
160- }
161- }
162-
163- if (
164- messageForCommentary.role !== "assistant" ||
165- !Array.isArray(messageForCommentary.content)
166- ) {
167- result.push(messageForCommentary);
168- continue;
169- }
170-
171- const filtered = messageForCommentary.content.filter(
172- (part) => !isCommentaryTextPart(part),
173- );
174- if (filtered.length === messageForCommentary.content.length) {
175- result.push(messageForCommentary);
176- } else if (filtered.length === 0) {
177- mutated = true;
178- skipToolResults = true;
179- } else {
180- mutated = true;
181- result.push({
182- ...messageForCommentary,
183- content: filtered,
184- } as CoreMessage);
185- }
186- }
187-
188- return mutated ? result : messages;
103+ if (!isOpenAIGPT(modelString)) return messages;
104+
105+ let mutated = false;
106+ const result: CoreMessage[] = [];
107+ let skipToolResults = false;
108+
109+ for (const message of messages) {
110+ if (skipToolResults) {
111+ if (message.role === "tool") {
112+ mutated = true;
113+ continue;
114+ }
115+ skipToolResults = false;
116+ }
117+
118+ let messageForCommentary = message;
119+ if (options.stripItemIds && Array.isArray(message.content)) {
120+ let contentMutated = false;
121+ const strippedContent = message.content.map((part) => {
122+ const cleaned = stripOpenAIItemIdFromPart(part);
123+ if (cleaned.changed) contentMutated = true;
124+ return cleaned.part;
125+ });
126+ if (contentMutated) {
127+ mutated = true;
128+ messageForCommentary = {
129+ ...message,
130+ content: strippedContent as CoreMessage["content"],
131+ } as CoreMessage;
132+ }
133+ }
134+
135+ if (
136+ messageForCommentary.role !== "assistant" ||
137+ !Array.isArray(messageForCommentary.content)
138+ ) {
139+ result.push(messageForCommentary);
140+ continue;
141+ }
142+
143+ const filtered = messageForCommentary.content.filter(
144+ (part) => !isCommentaryTextPart(part),
145+ );
146+ if (filtered.length === messageForCommentary.content.length) {
147+ result.push(messageForCommentary);
148+ } else if (filtered.length === 0) {
149+ mutated = true;
150+ skipToolResults = true;
151+ } else {
152+ mutated = true;
153+ result.push({
154+ ...messageForCommentary,
155+ content: filtered,
156+ } as CoreMessage);
157+ }
158+ }
159+
160+ return mutated ? result : messages;
189161 }
190162
191163 export function stripGPTCommentaryFromHistory(
192- messages: CoreMessage[],
193- modelString: string,
164+ messages: CoreMessage[],
165+ modelString: string,
194166 ): CoreMessage[] {
195- return stripOpenAIHistory(messages, modelString, { stripItemIds: false });
167+ return stripOpenAIHistory(messages, modelString, { stripItemIds: false });
196168 }
197169
198170 export function stripOpenAIHistoryTransforms(
199- messages: CoreMessage[],
200- modelString: string,
171+ messages: CoreMessage[],
172+ modelString: string,
201173 ): CoreMessage[] {
202- return stripOpenAIHistory(messages, modelString, { stripItemIds: true });
174+ return stripOpenAIHistory(messages, modelString, { stripItemIds: true });
203175 }
src/llm-api/provider-options.ts+76−96
@@ -1,141 +1,121 @@
11 import { supportsThinking } from "./model-info.ts";
22 import {
3- isAnthropicModelFamily,
4- isGeminiModelFamily,
5- isOpenAIReasoningModelFamily,
6- parseModelString,
3+ isAnthropicModelFamily,
4+ isGeminiModelFamily,
5+ isOpenAIReasoningModelFamily,
6+ parseModelString,
77 } from "./model-routing.ts";
88
99 export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
1010
1111 type CacheFamily = "google" | "anthropic" | "none";
1212
13-const ANTHROPIC_BUDGET: Record<ThinkingEffort, number> = {
14- low: 4_096,
15- medium: 8_192,
16- high: 16_384,
17- xhigh: 32_768,
18-};
19-
2013 const GEMINI_BUDGET: Record<ThinkingEffort, number> = {
21- low: 4_096,
22- medium: 8_192,
23- high: 16_384,
24- xhigh: 24_575,
14+ low: 4_096,
15+ medium: 8_192,
16+ high: 16_384,
17+ xhigh: 24_575,
2518 };
2619
2720 function clampEffort(
28- effort: ThinkingEffort,
29- max: ThinkingEffort,
21+ effort: ThinkingEffort,
22+ max: ThinkingEffort,
3023 ): ThinkingEffort {
31- const ORDER: ThinkingEffort[] = ["low", "medium", "high", "xhigh"];
32- const effortIdx = ORDER.indexOf(effort);
33- const maxIdx = ORDER.indexOf(max);
34- return ORDER[Math.min(effortIdx, maxIdx)] as ThinkingEffort;
24+ const ORDER: ThinkingEffort[] = ["low", "medium", "high", "xhigh"];
25+ const effortIdx = ORDER.indexOf(effort);
26+ const maxIdx = ORDER.indexOf(max);
27+ return ORDER[Math.min(effortIdx, maxIdx)] as ThinkingEffort;
3528 }
3629
3730 function getAnthropicThinkingOptions(
38- modelId: string,
39- effort: ThinkingEffort,
31+ modelId: string,
32+ effort: ThinkingEffort,
4033 ): Record<string, unknown> {
41- const isAdaptive =
42- /^claude-3-7/.test(modelId) ||
43- /^claude-sonnet-4/.test(modelId) ||
44- /^claude-opus-4/.test(modelId);
45- if (isAdaptive) {
46- const isOpus = /^claude-opus-4/.test(modelId);
47- const xhighMapping = isOpus ? "max" : "high";
48- const mapped = effort === "xhigh" ? xhighMapping : effort;
49- return { anthropic: { thinking: { type: "adaptive" }, effort: mapped } };
50- }
51-
52- return {
53- anthropic: {
54- thinking: { type: "enabled", budgetTokens: ANTHROPIC_BUDGET[effort] },
55- betas: ["interleaved-thinking-2025-05-14"],
56- },
57- };
34+ const isOpus = /^claude-opus-4/.test(modelId);
35+ const xhighMapping = isOpus ? "max" : "high";
36+ const mapped = effort === "xhigh" ? xhighMapping : effort;
37+ return { anthropic: { thinking: { type: "adaptive" }, effort: mapped } };
5838 }
5939
6040 function getOpenAIThinkingOptions(
61- modelId: string,
62- effort: ThinkingEffort,
41+ modelId: string,
42+ effort: ThinkingEffort,
6343 ): Record<string, unknown> {
64- const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
65- const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
66- return { openai: { reasoningEffort: clamped, reasoningSummary: "auto" } };
44+ const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
45+ const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
46+ return { openai: { reasoningEffort: clamped, reasoningSummary: "auto" } };
6747 }
6848
6949 function getGeminiThinkingOptions(
70- modelId: string,
71- effort: ThinkingEffort,
50+ modelId: string,
51+ effort: ThinkingEffort,
7252 ): Record<string, unknown> {
73- if (/^gemini-3/.test(modelId)) {
74- return {
75- google: {
76- thinkingConfig: {
77- includeThoughts: true,
78- thinkingLevel: clampEffort(effort, "high"),
79- },
80- },
81- };
82- }
83-
84- return {
85- google: {
86- thinkingConfig: {
87- includeThoughts: true,
88- thinkingBudget: GEMINI_BUDGET[effort],
89- },
90- },
91- };
53+ if (/^gemini-3/.test(modelId)) {
54+ return {
55+ google: {
56+ thinkingConfig: {
57+ includeThoughts: true,
58+ thinkingLevel: clampEffort(effort, "high"),
59+ },
60+ },
61+ };
62+ }
63+
64+ return {
65+ google: {
66+ thinkingConfig: {
67+ includeThoughts: true,
68+ thinkingBudget: GEMINI_BUDGET[effort],
69+ },
70+ },
71+ };
9272 }
9373
9474 interface ThinkingStrategy {
95- supports: (modelString: string) => boolean;
96- build: (modelId: string, effort: ThinkingEffort) => Record<string, unknown>;
75+ supports: (modelString: string) => boolean;
76+ build: (modelId: string, effort: ThinkingEffort) => Record<string, unknown>;
9777 }
9878
9979 const THINKING_STRATEGIES: readonly ThinkingStrategy[] = [
100- {
101- supports: isAnthropicModelFamily,
102- build: getAnthropicThinkingOptions,
103- },
104- {
105- supports: isOpenAIReasoningModelFamily,
106- build: getOpenAIThinkingOptions,
107- },
108- {
109- supports: isGeminiModelFamily,
110- build: getGeminiThinkingOptions,
111- },
80+ {
81+ supports: isAnthropicModelFamily,
82+ build: getAnthropicThinkingOptions,
83+ },
84+ {
85+ supports: isOpenAIReasoningModelFamily,
86+ build: getOpenAIThinkingOptions,
87+ },
88+ {
89+ supports: isGeminiModelFamily,
90+ build: getGeminiThinkingOptions,
91+ },
11292 ];
11393
11494 export function getThinkingProviderOptions(
115- modelString: string,
116- effort: ThinkingEffort,
95+ modelString: string,
96+ effort: ThinkingEffort,
11797 ): Record<string, unknown> | null {
118- if (!supportsThinking(modelString)) return null;
98+ if (!supportsThinking(modelString)) return null;
11999
120- const { modelId } = parseModelString(modelString);
121- for (const strategy of THINKING_STRATEGIES) {
122- if (!strategy.supports(modelString)) continue;
123- return strategy.build(modelId, effort);
124- }
100+ const { modelId } = parseModelString(modelString);
101+ for (const strategy of THINKING_STRATEGIES) {
102+ if (!strategy.supports(modelString)) continue;
103+ return strategy.build(modelId, effort);
104+ }
125105
126- return null;
106+ return null;
127107 }
128108
129109 const CACHE_FAMILY_RULES: ReadonlyArray<
130- readonly [match: (modelString: string) => boolean, family: CacheFamily]
110+ readonly [match: (modelString: string) => boolean, family: CacheFamily]
131111 > = [
132- [isAnthropicModelFamily, "anthropic"],
133- [isGeminiModelFamily, "google"],
112+ [isAnthropicModelFamily, "anthropic"],
113+ [isGeminiModelFamily, "google"],
134114 ];
135115
136116 export function getCacheFamily(modelString: string): CacheFamily {
137- for (const [match, family] of CACHE_FAMILY_RULES) {
138- if (match(modelString)) return family;
139- }
140- return "none";
117+ for (const [match, family] of CACHE_FAMILY_RULES) {
118+ if (match(modelString)) return family;
119+ }
120+ return "none";
141121 }
src/llm-api/turn.test.ts+862−933
@@ -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.