|
| 1 | +import { resolveProviderRequestPolicyConfig } from "./provider-request-config.js"; |
| 2 | + |
| 3 | +type OpenAIResponsesPayloadModel = { |
| 4 | + api?: unknown; |
| 5 | + baseUrl?: unknown; |
| 6 | + provider?: unknown; |
| 7 | + contextWindow?: unknown; |
| 8 | + compat?: { supportsStore?: boolean }; |
| 9 | +}; |
| 10 | + |
| 11 | +type OpenAIResponsesPayloadPolicyOptions = { |
| 12 | + extraParams?: Record<string, unknown>; |
| 13 | + storeMode?: "provider-policy" | "disable" | "preserve"; |
| 14 | + enablePromptCacheStripping?: boolean; |
| 15 | + enableServerCompaction?: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +export type OpenAIResponsesPayloadPolicy = { |
| 19 | + allowsServiceTier: boolean; |
| 20 | + compactThreshold: number; |
| 21 | + explicitStore: boolean | undefined; |
| 22 | + shouldStripDisabledReasoningPayload: boolean; |
| 23 | + shouldStripPromptCache: boolean; |
| 24 | + shouldStripStore: boolean; |
| 25 | + useServerCompaction: boolean; |
| 26 | +}; |
| 27 | + |
| 28 | +const OPENAI_RESPONSES_APIS = new Set([ |
| 29 | + "openai-responses", |
| 30 | + "azure-openai-responses", |
| 31 | + "openai-codex-responses", |
| 32 | +]); |
| 33 | + |
| 34 | +function parsePositiveInteger(value: unknown): number | undefined { |
| 35 | + if (typeof value === "number" && Number.isFinite(value) && value > 0) { |
| 36 | + return Math.floor(value); |
| 37 | + } |
| 38 | + if (typeof value === "string") { |
| 39 | + const parsed = Number.parseInt(value, 10); |
| 40 | + if (Number.isFinite(parsed) && parsed > 0) { |
| 41 | + return parsed; |
| 42 | + } |
| 43 | + } |
| 44 | + return undefined; |
| 45 | +} |
| 46 | + |
| 47 | +function resolveOpenAIResponsesCompactThreshold(model: { contextWindow?: unknown }): number { |
| 48 | + const contextWindow = parsePositiveInteger(model.contextWindow); |
| 49 | + if (contextWindow) { |
| 50 | + return Math.max(1_000, Math.floor(contextWindow * 0.7)); |
| 51 | + } |
| 52 | + return 80_000; |
| 53 | +} |
| 54 | + |
| 55 | +function shouldEnableOpenAIResponsesServerCompaction( |
| 56 | + explicitStore: boolean | undefined, |
| 57 | + provider: unknown, |
| 58 | + extraParams: Record<string, unknown> | undefined, |
| 59 | +): boolean { |
| 60 | + const configured = extraParams?.responsesServerCompaction; |
| 61 | + if (configured === false) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + if (explicitStore !== true) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + if (configured === true) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + return provider === "openai"; |
| 71 | +} |
| 72 | + |
| 73 | +function stripDisabledOpenAIReasoningPayload(payloadObj: Record<string, unknown>): void { |
| 74 | + const reasoning = payloadObj.reasoning; |
| 75 | + if (reasoning === "none") { |
| 76 | + delete payloadObj.reasoning; |
| 77 | + return; |
| 78 | + } |
| 79 | + if (!reasoning || typeof reasoning !== "object" || Array.isArray(reasoning)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // GPT-5 models reject `reasoning.effort: "none"`. Treat the disabled effort |
| 84 | + // as "reasoning omitted" instead of forwarding an unsupported value. |
| 85 | + const reasoningObj = reasoning as Record<string, unknown>; |
| 86 | + if (reasoningObj.effort === "none") { |
| 87 | + delete payloadObj.reasoning; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function resolveOpenAIResponsesPayloadPolicy( |
| 92 | + model: OpenAIResponsesPayloadModel, |
| 93 | + options: OpenAIResponsesPayloadPolicyOptions = {}, |
| 94 | +): OpenAIResponsesPayloadPolicy { |
| 95 | + const capabilities = resolveProviderRequestPolicyConfig({ |
| 96 | + provider: typeof model.provider === "string" ? model.provider : undefined, |
| 97 | + api: typeof model.api === "string" ? model.api : undefined, |
| 98 | + baseUrl: typeof model.baseUrl === "string" ? model.baseUrl : undefined, |
| 99 | + compat: model.compat, |
| 100 | + capability: "llm", |
| 101 | + transport: "stream", |
| 102 | + }).capabilities; |
| 103 | + const storeMode = options.storeMode ?? "provider-policy"; |
| 104 | + const explicitStore = |
| 105 | + storeMode === "preserve" |
| 106 | + ? undefined |
| 107 | + : storeMode === "disable" |
| 108 | + ? capabilities.supportsResponsesStoreField |
| 109 | + ? false |
| 110 | + : undefined |
| 111 | + : capabilities.allowsResponsesStore |
| 112 | + ? true |
| 113 | + : undefined; |
| 114 | + const isResponsesApi = typeof model.api === "string" && OPENAI_RESPONSES_APIS.has(model.api); |
| 115 | + |
| 116 | + return { |
| 117 | + allowsServiceTier: capabilities.allowsOpenAIServiceTier, |
| 118 | + compactThreshold: |
| 119 | + parsePositiveInteger(options.extraParams?.responsesCompactThreshold) ?? |
| 120 | + resolveOpenAIResponsesCompactThreshold(model), |
| 121 | + explicitStore, |
| 122 | + shouldStripDisabledReasoningPayload: capabilities.supportsOpenAIReasoningCompatPayload, |
| 123 | + shouldStripPromptCache: |
| 124 | + options.enablePromptCacheStripping === true && capabilities.shouldStripResponsesPromptCache, |
| 125 | + shouldStripStore: |
| 126 | + explicitStore !== true && model.compat?.supportsStore === false && isResponsesApi, |
| 127 | + useServerCompaction: |
| 128 | + options.enableServerCompaction === true && |
| 129 | + shouldEnableOpenAIResponsesServerCompaction( |
| 130 | + explicitStore, |
| 131 | + model.provider, |
| 132 | + options.extraParams, |
| 133 | + ), |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +export function applyOpenAIResponsesPayloadPolicy( |
| 138 | + payloadObj: Record<string, unknown>, |
| 139 | + policy: OpenAIResponsesPayloadPolicy, |
| 140 | +): void { |
| 141 | + if (policy.explicitStore !== undefined) { |
| 142 | + payloadObj.store = policy.explicitStore; |
| 143 | + } |
| 144 | + if (policy.shouldStripStore) { |
| 145 | + delete payloadObj.store; |
| 146 | + } |
| 147 | + if (policy.shouldStripPromptCache) { |
| 148 | + delete payloadObj.prompt_cache_key; |
| 149 | + delete payloadObj.prompt_cache_retention; |
| 150 | + } |
| 151 | + if (policy.useServerCompaction && payloadObj.context_management === undefined) { |
| 152 | + payloadObj.context_management = [ |
| 153 | + { |
| 154 | + type: "compaction", |
| 155 | + compact_threshold: policy.compactThreshold, |
| 156 | + }, |
| 157 | + ]; |
| 158 | + } |
| 159 | + if (policy.shouldStripDisabledReasoningPayload) { |
| 160 | + stripDisabledOpenAIReasoningPayload(payloadObj); |
| 161 | + } |
| 162 | +} |
0 commit comments