@@ -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