xonecas.io
64875
Mc

mini-coder / commit aa37ba5

refactor(turn): isolate provider options assembly

xonecas2026-03-14 aa37ba511a2b2e63bef037a1b10116b996e56e6c parent f9b99f1

2 files changed

src/llm-api/turn-provider-options.ts+100−0
@@ -0,0 +1,100 @@
1+import { isOpenAIGPT } from "./history-transforms.ts";
2+import {
3+ getCacheFamily,
4+ getCachingProviderOptions,
5+ getThinkingProviderOptions,
6+ type ThinkingEffort,
7+} from "./provider-options.ts";
8+
9+function isRecord(value: unknown): value is Record<string, unknown> {
10+ return value !== null && typeof value === "object";
11+}
12+
13+function mergeDeep(
14+ target: Record<string, unknown>,
15+ source: Record<string, unknown>,
16+): Record<string, unknown> {
17+ const output: Record<string, unknown> = { ...target };
18+ for (const key in source) {
19+ const sVal = source[key];
20+ const tVal = target[key];
21+ output[key] =
22+ isRecord(sVal) && isRecord(tVal) ? { ...tVal, ...sVal } : sVal;
23+ }
24+ return output;
25+}
26+
27+interface BuildTurnProviderOptionsInput {
28+ modelString: string;
29+ thinkingEffort: ThinkingEffort | undefined;
30+ promptCachingEnabled: boolean;
31+ openaiPromptCacheRetention: "in_memory" | "24h";
32+ googleCachedContent: string | null;
33+ toolCount: number;
34+ hasSystemPrompt: boolean;
35+}
36+
37+interface TurnProviderOptionsResult {
38+ cacheFamily: ReturnType<typeof getCacheFamily>;
39+ thinkingOpts: Record<string, unknown> | null;
40+ cacheOpts: Record<string, unknown> | null;
41+ providerOptions: Record<string, unknown>;
42+ reasoningSummaryRequested: boolean;
43+}
44+
45+export function buildTurnProviderOptions(
46+ input: BuildTurnProviderOptionsInput,
47+): TurnProviderOptionsResult {
48+ const {
49+ modelString,
50+ thinkingEffort,
51+ promptCachingEnabled,
52+ openaiPromptCacheRetention,
53+ googleCachedContent,
54+ toolCount,
55+ hasSystemPrompt,
56+ } = input;
57+
58+ const thinkingOpts = thinkingEffort
59+ ? getThinkingProviderOptions(modelString, thinkingEffort)
60+ : null;
61+
62+ const reasoningSummaryRequested =
63+ isRecord(thinkingOpts) &&
64+ isRecord(thinkingOpts.openai) &&
65+ typeof thinkingOpts.openai.reasoningSummary === "string";
66+
67+ const cacheFamily = getCacheFamily(modelString);
68+ const cacheOpts = getCachingProviderOptions(modelString, {
69+ enabled: promptCachingEnabled,
70+ openaiRetention: openaiPromptCacheRetention,
71+ googleCachedContent,
72+ googleExplicitCachingCompatible: toolCount === 0 && !hasSystemPrompt,
73+ });
74+
75+ const baseProviderOpts = {
76+ ...(thinkingOpts ?? {}),
77+ ...(isOpenAIGPT(modelString)
78+ ? {
79+ openai: {
80+ store: false,
81+ ...(isRecord(thinkingOpts?.openai)
82+ ? (thinkingOpts.openai as object)
83+ : {}),
84+ },
85+ }
86+ : {}),
87+ };
88+
89+ const providerOptions = cacheOpts
90+ ? mergeDeep(baseProviderOpts, cacheOpts)
91+ : baseProviderOpts;
92+
93+ return {
94+ cacheFamily,
95+ thinkingOpts,
96+ cacheOpts,
97+ providerOptions,
98+ reasoningSummaryRequested,
99+ };
100+}
src/llm-api/turn.ts+16−61
@@ -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 },