@@ -2,17 +2,11 @@ import type { FlexibleSchema, StepResult } from "ai";
22 import { dynamicTool, jsonSchema, stepCountIs, streamText } from "ai";
33 import { isApiLogEnabled, logApiEvent } from "./api-log.ts";
44 import {
5- isOpenAIGPT,
65 normalizeOpenAICompatibleToolCallInputs,
76 sanitizeGeminiToolMessagesWithMetadata,
87 stripOpenAIHistoryTransforms,
98 } from "./history-transforms.ts";
10-import {
11- getCacheFamily,
12- getCachingProviderOptions,
13- getThinkingProviderOptions,
14- type ThinkingEffort,
15-} from "./provider-options.ts";
9+import { getCacheFamily, type ThinkingEffort } from "./provider-options.ts";
1610 import {
1711 annotateAnthropicCacheBreakpoints,
1812 applyContextPruning,
@@ -22,6 +16,7 @@ import {
2216 getMessageDiagnostics,
2317 getMessageStats,
2418 } from "./turn-context.ts";
19+import { buildTurnProviderOptions } from "./turn-provider-options.ts";
2520
2621 export type { ContextPruningMode } from "./turn-context.ts";
2722
@@ -46,24 +41,6 @@ const MAX_STEPS = 50;
4641
4742 // ─── Local helpers ────────────────────────────────────────────────────────────
4843
49-function isRecord(value: unknown): value is Record<string, unknown> {
50- return value !== null && typeof value === "object";
51-}
52-
53-function mergeDeep(
54- target: Record<string, unknown>,
55- source: Record<string, unknown>,
56-): Record<string, unknown> {
57- const output: Record<string, unknown> = { ...target };
58- for (const key in source) {
59- const sVal = source[key];
60- const tVal = target[key];
61- output[key] =
62- isRecord(sVal) && isRecord(tVal) ? { ...tVal, ...sVal } : sVal;
63- }
64- return output;
65-}
66-
6744 function isZodSchema(s: unknown): boolean {
6845 return s !== null && typeof s === "object" && "_def" in (s as object);
6946 }
@@ -252,17 +229,21 @@ export async function* runTurn(options: {
252229
253230 try {
254231 const toolCount = Object.keys(toolSet).length;
255- const thinkingOpts = thinkingEffort
256- ? getThinkingProviderOptions(modelString, thinkingEffort)
257- : null;
232+ const providerOptionsResult = buildTurnProviderOptions({
233+ modelString,
234+ thinkingEffort,
235+ promptCachingEnabled,
236+ openaiPromptCacheRetention,
237+ googleCachedContent,
238+ toolCount,
239+ hasSystemPrompt: Boolean(systemPrompt),
240+ });
258241
259242 logApiEvent("turn start", {
260243 modelString,
261244 messageCount: messages.length,
262245 reasoningSummaryRequested:
263- isRecord(thinkingOpts) &&
264- isRecord(thinkingOpts.openai) &&
265- typeof thinkingOpts.openai.reasoningSummary === "string",
246+ providerOptionsResult.reasoningSummaryRequested,
266247 pruningMode,
267248 toolResultPayloadCapBytes,
268249 });
@@ -293,40 +274,14 @@ export async function* runTurn(options: {
293274 };
294275 }
295276
296- // Build provider options (thinking + cache)
297- const cacheFamily = getCacheFamily(modelString);
298- const cacheOpts = getCachingProviderOptions(modelString, {
299- enabled: promptCachingEnabled,
300- openaiRetention: openaiPromptCacheRetention,
301- googleCachedContent,
302- googleExplicitCachingCompatible: toolCount === 0 && !systemPrompt,
303- });
304277 if (isApiLogEnabled()) {
305278 logApiEvent("prompt caching configured", {
306279 enabled: promptCachingEnabled,
307- cacheFamily,
308- cacheOpts,
280+ cacheFamily: providerOptionsResult.cacheFamily,
281+ cacheOpts: providerOptionsResult.cacheOpts,
309282 });
310283 }
311284
312- const baseProviderOpts = {
313- ...(thinkingOpts ?? {}),
314- // GPT models via responses API: opt out of storage, merge thinking opts
315- ...(isOpenAIGPT(modelString)
316- ? {
317- openai: {
318- store: false,
319- ...(isRecord(thinkingOpts?.openai)
320- ? (thinkingOpts.openai as object)
321- : {}),
322- },
323- }
324- : {}),
325- };
326- const mergedProviderOpts = cacheOpts
327- ? mergeDeep(baseProviderOpts, cacheOpts)
328- : baseProviderOpts;
329-
330285 const result = streamText({
331286 model,
332287 messages: prepared.messages,
@@ -357,8 +312,8 @@ export async function* runTurn(options: {
357312 return undefined;
358313 },
359314 ...(prepared.systemPrompt ? { system: prepared.systemPrompt } : {}),
360- ...(Object.keys(mergedProviderOpts).length > 0
361- ? { providerOptions: mergedProviderOpts }
315+ ...(Object.keys(providerOptionsResult.providerOptions).length > 0
316+ ? { providerOptions: providerOptionsResult.providerOptions }
362317 : {}),
363318 ...(signal ? { abortSignal: signal } : {}),
364319 timeout: { chunkMs: 120_000 },