xonecas.io
64875
Mc

mini-coder / commit 9c150f4

fix(llm-api): clean up provider-options extraction

- Fix providers.test.ts duplicate imports (now imports from provider-options.ts only)
- Remove unused re-exports from providers.ts (getCacheFamily, getCachingProviderOptions,
  getThinkingProviderOptions) since turn.ts imports directly from provider-options.ts
- Make CacheFamily type internal to provider-options.ts (no external consumers)

xonecas2026-03-14 9c150f47c4f38464d9ed5ffd3ff124c7f492ff16 parent 0b5a3db

4 files changed

src/llm-api/provider-options.ts+145−0
@@ -0,0 +1,145 @@
1+import { supportsThinking } from "./model-info.ts";
2+import {
3+ isAnthropicModelFamily,
4+ isGeminiModelFamily,
5+ isOpenAIReasoningModelFamily,
6+ parseModelString,
7+} from "./model-routing.ts";
8+
9+export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
10+
11+interface CachingOptions {
12+ enabled: boolean;
13+ openaiRetention?: "in_memory" | "24h";
14+ googleCachedContent?: string | null;
15+ googleExplicitCachingCompatible?: boolean;
16+}
17+
18+type CacheFamily = "google" | "anthropic" | "none";
19+
20+const ANTHROPIC_BUDGET: Record<ThinkingEffort, number> = {
21+ low: 4_096,
22+ medium: 8_192,
23+ high: 16_384,
24+ xhigh: 32_768,
25+};
26+
27+const GEMINI_BUDGET: Record<ThinkingEffort, number> = {
28+ low: 4_096,
29+ medium: 8_192,
30+ high: 16_384,
31+ xhigh: 24_575,
32+};
33+
34+function clampEffort(
35+ effort: ThinkingEffort,
36+ max: ThinkingEffort,
37+): ThinkingEffort {
38+ const ORDER: ThinkingEffort[] = ["low", "medium", "high", "xhigh"];
39+ const effortIdx = ORDER.indexOf(effort);
40+ const maxIdx = ORDER.indexOf(max);
41+ return ORDER[Math.min(effortIdx, maxIdx)] as ThinkingEffort;
42+}
43+
44+function getAnthropicThinkingOptions(
45+ modelId: string,
46+ effort: ThinkingEffort,
47+): Record<string, unknown> {
48+ const isAdaptive =
49+ /^claude-3-7/.test(modelId) ||
50+ /^claude-sonnet-4/.test(modelId) ||
51+ /^claude-opus-4/.test(modelId);
52+ if (isAdaptive) {
53+ const isOpus = /^claude-opus-4/.test(modelId);
54+ const mapped = effort === "xhigh" ? (isOpus ? "max" : "high") : effort;
55+ return { anthropic: { thinking: { type: "adaptive" }, effort: mapped } };
56+ }
57+
58+ return {
59+ anthropic: {
60+ thinking: { type: "enabled", budgetTokens: ANTHROPIC_BUDGET[effort] },
61+ betas: ["interleaved-thinking-2025-05-14"],
62+ },
63+ };
64+}
65+
66+function getOpenAIThinkingOptions(
67+ modelId: string,
68+ effort: ThinkingEffort,
69+): Record<string, unknown> {
70+ const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
71+ const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
72+ return { openai: { reasoningEffort: clamped, reasoningSummary: "auto" } };
73+}
74+
75+function getGeminiThinkingOptions(
76+ modelId: string,
77+ effort: ThinkingEffort,
78+): Record<string, unknown> {
79+ if (/^gemini-3/.test(modelId)) {
80+ return {
81+ google: {
82+ thinkingConfig: {
83+ includeThoughts: true,
84+ thinkingLevel: clampEffort(effort, "high"),
85+ },
86+ },
87+ };
88+ }
89+
90+ return {
91+ google: {
92+ thinkingConfig: {
93+ includeThoughts: true,
94+ thinkingBudget: GEMINI_BUDGET[effort],
95+ },
96+ },
97+ };
98+}
99+
100+export function getThinkingProviderOptions(
101+ modelString: string,
102+ effort: ThinkingEffort,
103+): Record<string, unknown> | null {
104+ if (!supportsThinking(modelString)) return null;
105+
106+ const { modelId } = parseModelString(modelString);
107+
108+ if (isAnthropicModelFamily(modelString)) {
109+ return getAnthropicThinkingOptions(modelId, effort);
110+ }
111+
112+ if (isOpenAIReasoningModelFamily(modelString)) {
113+ return getOpenAIThinkingOptions(modelId, effort);
114+ }
115+
116+ if (isGeminiModelFamily(modelString)) {
117+ return getGeminiThinkingOptions(modelId, effort);
118+ }
119+
120+ return null;
121+}
122+
123+export function getCacheFamily(modelString: string): CacheFamily {
124+ if (isAnthropicModelFamily(modelString)) return "anthropic";
125+ if (isGeminiModelFamily(modelString)) return "google";
126+ return "none";
127+}
128+
129+export function getCachingProviderOptions(
130+ modelString: string,
131+ opts: CachingOptions,
132+): Record<string, unknown> | null {
133+ if (!opts.enabled) return null;
134+
135+ const family = getCacheFamily(modelString);
136+ if (
137+ family === "google" &&
138+ opts.googleCachedContent &&
139+ opts.googleExplicitCachingCompatible !== false
140+ ) {
141+ return { google: { cachedContent: opts.googleCachedContent } };
142+ }
143+
144+ return null;
145+}
src/llm-api/providers.test.ts+5−3
@@ -1,6 +1,10 @@
11 import { describe, expect, test } from "bun:test";
22
3-import { getThinkingProviderOptions } from "./providers.ts";
3+import {
4+ getCacheFamily,
5+ getCachingProviderOptions,
6+ getThinkingProviderOptions,
7+} from "./provider-options.ts";
48
59 describe("getThinkingProviderOptions", () => {
610 test("requests OpenAI reasoning summary for GPT reasoning models", () => {
@@ -39,8 +43,6 @@ describe("getThinkingProviderOptions", () => {
3943 });
4044 });
4145
42-import { getCacheFamily, getCachingProviderOptions } from "./providers.ts";
43-
4446 describe("getCacheFamily", () => {
4547 test("identifies anthropic family", () => {
4648 expect(getCacheFamily("anthropic/claude-3-opus")).toBe("anthropic");
src/llm-api/providers.ts+4−156
@@ -8,16 +8,11 @@ import { logApiEvent } from "./api-log.ts";
88 import {
99 type AvailableModelsSnapshot,
1010 fetchAvailableModelsSnapshot,
11- getContextWindow as getContextWindowFromCache,
12- supportsThinking as supportsThinkingFromCache,
1311 } from "./model-info.ts";
14-import {
15- getZenBackend,
16- isAnthropicModelFamily,
17- isGeminiModelFamily,
18- isOpenAIReasoningModelFamily,
19- parseModelString,
20-} from "./model-routing.ts";
12+import { getZenBackend } from "./model-routing.ts";
13+
14+export { getContextWindow } from "./model-info.ts";
15+export { type ThinkingEffort } from "./provider-options.ts";
2116
2217 // ─── Zen endpoint constants ────────────────────────────────────────────────────
2318
@@ -168,154 +163,7 @@ function directGoogle() {
168163 return _directGoogle;
169164 }
170165
171-// ─── Model string parsing ─────────────────────────────────────────────────────
172-
173-export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
174-
175-function supportsThinking(modelString: string): boolean {
176- return supportsThinkingFromCache(modelString);
177-}
178-
179-// Token budgets for Anthropic extended thinking (budget_tokens mode).
180-// Adaptive-mode models (claude-sonnet-4-6+, claude-opus-4+) use effort
181-// strings instead; detected by model ID below.
182-const ANTHROPIC_BUDGET: Record<ThinkingEffort, number> = {
183- low: 4_096,
184- medium: 8_192,
185- high: 16_384,
186- xhigh: 32_768,
187-};
188-
189-// Clamp xhigh → high for providers that don't support it.
190-function clampEffort(
191- effort: ThinkingEffort,
192- max: ThinkingEffort,
193-): ThinkingEffort {
194- const ORDER: ThinkingEffort[] = ["low", "medium", "high", "xhigh"];
195- const i = ORDER.indexOf(effort);
196- const m = ORDER.indexOf(max);
197- return ORDER[Math.min(i, m)] as ThinkingEffort;
198-}
199-
200-export function getThinkingProviderOptions(
201- modelString: string,
202- effort: ThinkingEffort,
203-): Record<string, unknown> | null {
204- if (!supportsThinking(modelString)) return null;
205-
206- const { modelId } = parseModelString(modelString);
207-
208- // Anthropic (direct or via Zen routing through @ai-sdk/anthropic)
209- if (isAnthropicModelFamily(modelString)) {
210- // Claude 3.7+, Sonnet 4.x, Opus 4.x support adaptive effort strings.
211- const isAdaptive =
212- /^claude-3-7/.test(modelId) ||
213- /^claude-sonnet-4/.test(modelId) ||
214- /^claude-opus-4/.test(modelId);
215- if (isAdaptive) {
216- // Adaptive: effort string ("low"|"medium"|"high"|"max")
217- // xhigh maps to "max" for Opus; clamp to "high" for others.
218- const isOpus = /^claude-opus-4/.test(modelId);
219- const mapped = effort === "xhigh" ? (isOpus ? "max" : "high") : effort;
220- return { anthropic: { thinking: { type: "adaptive" }, effort: mapped } };
221- }
222-
223- // Extended thinking: budget_tokens integer
224- const budget = ANTHROPIC_BUDGET[effort];
225- return {
226- anthropic: {
227- thinking: { type: "enabled", budgetTokens: budget },
228- betas: ["interleaved-thinking-2025-05-14"],
229- },
230- };
231- }
232-
233- // OpenAI o-series and GPT-5 (direct or via Zen routing through @ai-sdk/openai)
234- if (isOpenAIReasoningModelFamily(modelString)) {
235- // xhigh only valid on gpt-5.2+, o4+; clamp for others.
236- const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
237- const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
238- return { openai: { reasoningEffort: clamped, reasoningSummary: "auto" } };
239- }
240-
241- // Google Gemini (direct or via Zen routing through @ai-sdk/google)
242- if (isGeminiModelFamily(modelString)) {
243- // Gemini 3.x: thinkingLevel enum. 2.5.x: budgetTokens.
244- if (/^gemini-3/.test(modelId)) {
245- // No xhigh for Gemini 3.
246- const level = clampEffort(effort, "high");
247- return {
248- google: {
249- thinkingConfig: {
250- includeThoughts: true,
251- thinkingLevel: level,
252- },
253- },
254- };
255- }
256- // Gemini 2.5: budget tokens. Capped at 24575 per API limit.
257- const GEMINI_BUDGET: Record<ThinkingEffort, number> = {
258- low: 4_096,
259- medium: 8_192,
260- high: 16_384,
261- xhigh: 24_575,
262- };
263- return {
264- google: {
265- thinkingConfig: {
266- includeThoughts: true,
267- thinkingBudget: GEMINI_BUDGET[effort],
268- },
269- },
270- };
271- }
272166
273- // Unrecognised provider with a reasoning model (e.g. future providers) —
274- // return null and let the call proceed without thinking options.
275- return null;
276-}
277-interface CachingOptions {
278- enabled: boolean;
279- openaiRetention?: "in_memory" | "24h";
280- googleCachedContent?: string | null;
281- googleExplicitCachingCompatible?: boolean;
282-}
283-
284-type CacheFamily = "google" | "anthropic" | "none";
285-
286-export function getCacheFamily(modelString: string): CacheFamily {
287- if (isAnthropicModelFamily(modelString)) return "anthropic";
288- if (isGeminiModelFamily(modelString)) return "google";
289- return "none";
290-}
291-
292-export function getCachingProviderOptions(
293- modelString: string,
294- opts: CachingOptions,
295-): Record<string, unknown> | null {
296- if (!opts.enabled) return null;
297-
298- const family = getCacheFamily(modelString);
299-
300- if (
301- family === "google" &&
302- opts.googleCachedContent &&
303- opts.googleExplicitCachingCompatible !== false
304- ) {
305- return { google: { cachedContent: opts.googleCachedContent } };
306- }
307-
308- return null;
309-}
310-
311-/**
312- * Return the known context window size (in tokens) for a model string.
313- * Accepts either a bare model ID or a "provider/model-id" string.
314- * Returns null when the model is unknown.
315- */
316-export function getContextWindow(modelString: string): number | null {
317- return getContextWindowFromCache(modelString);
318-}
319167
320168 // ─── Public API ───────────────────────────────────────────────────────────────
321169
src/llm-api/turn.ts+1−1
@@ -16,7 +16,7 @@ import {
1616 getCachingProviderOptions,
1717 getThinkingProviderOptions,
1818 type ThinkingEffort,
19-} from "./providers.ts";
19+} from "./provider-options.ts";
2020 import {
2121 annotateAnthropicCacheBreakpoints,
2222 applyContextPruning,