@earendil-works/pi-ai 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -4
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +224 -17
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/azure-openai-responses.js +1 -1
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +4 -2
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/bedrock-converse-stream.lazy.d.ts +7 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.lazy.js +18 -5
- package/dist/api/bedrock-converse-stream.lazy.js.map +1 -1
- package/dist/api/constrained-sampling.d.ts +0 -2
- package/dist/api/constrained-sampling.d.ts.map +1 -1
- package/dist/api/constrained-sampling.js +0 -13
- package/dist/api/constrained-sampling.js.map +1 -1
- package/dist/api/lazy.d.ts +1 -1
- package/dist/api/lazy.d.ts.map +1 -1
- package/dist/api/lazy.js +9 -8
- package/dist/api/lazy.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +7 -3
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts +2 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +13 -5
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +4 -2
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/simple-options.d.ts +1 -0
- package/dist/api/simple-options.d.ts.map +1 -1
- package/dist/api/simple-options.js +16 -2
- package/dist/api/simple-options.js.map +1 -1
- package/dist/api/strict-tool-limits.d.ts +13 -0
- package/dist/api/strict-tool-limits.d.ts.map +1 -0
- package/dist/api/strict-tool-limits.js +50 -0
- package/dist/api/strict-tool-limits.js.map +1 -0
- package/dist/auth/oauth/load.d.ts.map +1 -1
- package/dist/auth/oauth/load.js +5 -2
- package/dist/auth/oauth/load.js.map +1 -1
- package/dist/cache-warming.d.ts +53 -0
- package/dist/cache-warming.d.ts.map +1 -0
- package/dist/cache-warming.js +82 -0
- package/dist/cache-warming.js.map +1 -0
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +1 -0
- package/dist/env-api-keys.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lazy-bedrock-provider.d.ts +10 -0
- package/dist/lazy-bedrock-provider.d.ts.map +1 -0
- package/dist/lazy-bedrock-provider.js +13 -0
- package/dist/lazy-bedrock-provider.js.map +1 -0
- package/dist/lazy-oauth.d.ts +10 -0
- package/dist/lazy-oauth.d.ts.map +1 -0
- package/dist/lazy-oauth.js +23 -0
- package/dist/lazy-oauth.js.map +1 -0
- package/dist/models.d.ts +7 -0
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +4 -0
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +4 -0
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +19 -11
- package/dist/models.js.map +1 -1
- package/dist/providers/all.d.ts.map +1 -1
- package/dist/providers/all.js +2 -0
- package/dist/providers/all.js.map +1 -1
- package/dist/providers/compat-schema.d.ts +16 -0
- package/dist/providers/compat-schema.d.ts.map +1 -1
- package/dist/providers/compat-schema.js +22 -1
- package/dist/providers/compat-schema.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/anthropic.json +1 -1
- package/dist/providers/data/azure.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/google.json +1 -1
- package/dist/providers/data/nvidia.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/qwen-token-plan-individual-cn.json +1 -0
- package/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/dist/providers/data/radius.json +1 -1
- package/dist/providers/data/together.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/github-copilot.d.ts +3 -1
- package/dist/providers/github-copilot.d.ts.map +1 -1
- package/dist/providers/github-copilot.js +13 -2
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/providers/openai-codex.js +1 -1
- package/dist/providers/openai-codex.js.map +1 -1
- package/dist/providers/qwen-token-plan-individual-cn.d.ts +3 -0
- package/dist/providers/qwen-token-plan-individual-cn.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.js +15 -0
- package/dist/providers/qwen-token-plan-individual-cn.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts +6 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js +8 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js.map +1 -0
- package/dist/types.d.ts +8 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/error-body.d.ts +8 -0
- package/dist/utils/error-body.d.ts.map +1 -1
- package/dist/utils/error-body.js +28 -1
- package/dist/utils/error-body.js.map +1 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +3 -0
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +11 -3
|
@@ -67,6 +67,7 @@ function getCompat(model) {
|
|
|
67
67
|
supportsDeveloperRole: model.compat?.supportsDeveloperRole ?? true,
|
|
68
68
|
supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
|
|
69
69
|
sessionAffinityFormat: model.compat?.sessionAffinityFormat ?? detectSessionAffinityFormat(model),
|
|
70
|
+
promptCacheKeyMode: model.compat?.promptCacheKeyMode ?? "auto",
|
|
70
71
|
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true,
|
|
71
72
|
supportsStrictMode: model.compat?.supportsStrictMode ?? false,
|
|
72
73
|
supportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,
|
|
@@ -221,7 +222,7 @@ function createClient(model, context, apiKey, optionsHeaders, identity, fetch, s
|
|
|
221
222
|
}
|
|
222
223
|
else {
|
|
223
224
|
if (compat.sessionAffinityFormat === "openai") {
|
|
224
|
-
headers
|
|
225
|
+
headers["session-id"] = sessionId;
|
|
225
226
|
}
|
|
226
227
|
headers["x-client-request-id"] = sessionId;
|
|
227
228
|
}
|
|
@@ -252,13 +253,14 @@ function buildParams(model, context, options, compat, grammarToolInputProperties
|
|
|
252
253
|
configurationUpdateEffort,
|
|
253
254
|
});
|
|
254
255
|
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
256
|
+
const sendPromptCacheKey = cacheRetention !== "none" && compat.promptCacheKeyMode !== "disabled";
|
|
255
257
|
// Sign in with ChatGPT rejects these request fields.
|
|
256
258
|
const omitUnsupportedFields = isChatGPTSignIn(model, options?.apiKey);
|
|
257
259
|
const params = {
|
|
258
260
|
model: model.id,
|
|
259
261
|
input: messages,
|
|
260
262
|
stream: true,
|
|
261
|
-
prompt_cache_key:
|
|
263
|
+
prompt_cache_key: sendPromptCacheKey ? clampOpenAIPromptCacheKey(options?.sessionId) : undefined,
|
|
262
264
|
prompt_cache_retention: omitUnsupportedFields ? undefined : getPromptCacheRetention(compat, cacheRetention),
|
|
263
265
|
prompt_cache_options: omitUnsupportedFields ? undefined : getPromptCacheOptions(compat, cacheRetention),
|
|
264
266
|
store: false,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-responses.js","sourceRoot":"","sources":["../../src/api/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAelD,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAuB,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAAE,gCAAgC,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAChG,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EACN,wBAAwB,EACxB,qBAAqB,EACrB,uBAAuB,EACvB,sBAAsB,GACtB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAE9E,MAAM,0BAA0B,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC,CAAC;AACnF,wGAAwG;AACxG,MAAM,kCAAkC,GAAG,EAAE,CAAC;AAC9C,MAAM,iBAAiB,GAAG,oCAAoC,CAAC;AAC/D,MAAM,4BAA4B,GACjC,8IAA8I,CAAC;AAChJ,oFAAoF;AACpF,M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YACC,OAAO,CAAC,CAAC;IACX,CAAC;AACF,CAAC;AAED,SAAS,uBAAuB,CAC/B,KAAY,EACZ,WAAsE,EACtE,KAA4C;IAE5C,MAAM,UAAU,GAAG,4BAA4B,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;IACpE,IAAI,UAAU,KAAK,CAAC;QAAE,OAAO;IAE7B,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,UAAU,CAAC;IAC/B,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,UAAU,CAAC;IAChC,KAAK,CAAC,IAAI,CAAC,SAAS,IAAI,UAAU,CAAC;IACnC,KAAK,CAAC,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC;IACpC,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC;AACxG,CAAC","sourcesContent":["import OpenAI from \"openai\";\nimport type { ResponseCreateParamsStreaming } from \"openai/resources/responses/responses.js\";\nimport { clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tCacheRetention,\n\tModel,\n\tOpenAIResponsesCompat,\n\tProviderEnv,\n\tProviderHeaders,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tTranscriptContext,\n\tUsage,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord } from \"../utils/headers.ts\";\nimport { type ClientIdentity, getIdentityHeaders } from \"../utils/identity.ts\";\nimport { getProviderEnvValue } from \"../utils/provider-env.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { getDeclaredTools, resolveTranscript, resolveTranscriptTools } from \"../utils/transcript.ts\";\nimport { createGrammarToolInputProperties } from \"./constrained-sampling.ts\";\nimport { buildCopilotDynamicHeaders, hasCopilotVisionInput } from \"./github-copilot-headers.ts\";\nimport { clampOpenAIPromptCacheKey } from \"./openai-prompt-cache.ts\";\nimport {\n\tconvertResponsesMessages,\n\tconvertResponsesTools,\n\tgetResponsesToolSupport,\n\tprocessResponsesStream,\n} from \"./openai-responses-shared.ts\";\nimport { buildBaseOptions, resolveSamplingParams } from \"./simple-options.ts\";\n\nconst OPENAI_TOOL_CALL_PROVIDERS = new Set([\"openai\", \"openai-codex\", \"opencode\"]);\n// OpenAI Responses rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265\nconst OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;\nconst CHATGPT_USAGE_URL = \"https://chatgpt.com/settings/usage\";\nconst CHATGPT_WORKSPACE_LIMIT_HINT =\n\t\"You've reached your ChatGPT workspace's usage limit. Ask your workspace admin to increase it at admin.openai.com → Workspace → Usage limits.\";\n// Fixed reasoning.effort while configuration_update items carry the thinking level.\nconst CONFIGURATION_UPDATE_REQUEST_EFFORT = \"medium\";\n\n/**\n * OpenAI API keys start with `sk-`; a different credential sent directly to OpenAI\n * is a Sign in with ChatGPT access token.\n */\nfunction isChatGPTSignIn(model: Model<\"openai-responses\">, apiKey: string | undefined): boolean {\n\treturn (\n\t\tmodel.provider === \"openai\" &&\n\t\tmodel.baseUrl === \"https://api.openai.com/v1\" &&\n\t\tapiKey !== undefined &&\n\t\t!apiKey.startsWith(\"sk-\")\n\t);\n}\n\nfunction hasHeader(headers: ProviderHeaders | undefined, name: string): boolean {\n\tif (!headers) return false;\n\tconst expected = name.toLowerCase();\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (key.toLowerCase() === expected && value !== null && value.trim().length > 0) return true;\n\t}\n\treturn false;\n}\n\nfunction getClientApiKey(provider: string, apiKey: string | undefined, headers: ProviderHeaders | undefined): string {\n\tif (apiKey) return apiKey;\n\tif (hasHeader(headers, \"authorization\") || hasHeader(headers, \"cf-aig-authorization\")) return \"unused\";\n\tthrow new Error(`No API key for provider: ${provider}`);\n}\n\nfunction detectSessionAffinityFormat(model: Pick<Model<\"openai-responses\">, \"provider\" | \"baseUrl\">) {\n\treturn model.provider === \"openrouter\" || model.baseUrl.includes(\"openrouter.ai\") ? \"openrouter\" : \"openai\";\n}\n\n/**\n * Resolve cache retention preference.\n * Defaults to \"short\" and uses PI_CACHE_RETENTION for backward compatibility.\n */\nfunction resolveCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nfunction getCompat(model: Model<\"openai-responses\">): Required<OpenAIResponsesCompat> {\n\treturn {\n\t\tsupportsDeveloperRole: model.compat?.supportsDeveloperRole ?? true,\n\t\tsupportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,\n\t\tsessionAffinityFormat: model.compat?.sessionAffinityFormat ?? detectSessionAffinityFormat(model),\n\t\tsupportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true,\n\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? false,\n\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\tsupportsAdditionalTools: model.compat?.supportsAdditionalTools ?? false,\n\t\tsupportsToolSearch: model.compat?.supportsToolSearch ?? false,\n\t\tsupportsExplicitPromptCacheMode: model.compat?.supportsExplicitPromptCacheMode ?? false,\n\t\tsupportsMaxOutputTokens: model.compat?.supportsMaxOutputTokens ?? true,\n\t\tsupportsMidConvoEffort: model.compat?.supportsMidConvoEffort ?? false,\n\t};\n}\n\n/** Effort to send in configuration_update items, or undefined to send it as reasoning.effort. */\nfunction getConfigurationUpdateEffort(\n\tmodel: Model<\"openai-responses\">,\n\toptions: OpenAIResponsesOptions | undefined,\n\tcompat: Required<OpenAIResponsesCompat>,\n): string | undefined {\n\tif (\n\t\t!compat.supportsMidConvoEffort ||\n\t\t!options?.reasoningEffort ||\n\t\t// Sign in with ChatGPT rejects configuration_update items.\n\t\tisChatGPTSignIn(model, options.apiKey)\n\t) {\n\t\treturn undefined;\n\t}\n\treturn model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;\n}\n\nfunction getPromptCacheRetention(\n\tcompat: Required<OpenAIResponsesCompat>,\n\tcacheRetention: CacheRetention,\n): \"24h\" | undefined {\n\treturn cacheRetention === \"long\" && compat.supportsLongCacheRetention && !compat.supportsExplicitPromptCacheMode\n\t\t? \"24h\"\n\t\t: undefined;\n}\n\nfunction getPromptCacheOptions(\n\tcompat: Required<OpenAIResponsesCompat>,\n\tcacheRetention: CacheRetention,\n): ResponseCreateParamsStreaming[\"prompt_cache_options\"] {\n\tif (!compat.supportsExplicitPromptCacheMode) return undefined;\n\tif (cacheRetention === \"none\") return { mode: \"explicit\" };\n\tif (cacheRetention === \"long\" && compat.supportsLongCacheRetention) return { ttl: \"30m\" };\n\treturn undefined;\n}\n\n// OpenAI Responses-specific options\nexport interface OpenAIResponsesOptions extends StreamOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\";\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n\tserviceTier?: ResponseCreateParamsStreaming[\"service_tier\"];\n\ttoolChoice?: ResponseCreateParamsStreaming[\"tool_choice\"];\n}\n\n/**\n * Generate function for OpenAI Responses API\n */\nexport const stream: StreamFunction<\"openai-responses\", OpenAIResponsesOptions> = (\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: OpenAIResponsesOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\tconst normalizedContext = resolveTranscript(context, getCompat(model).supportsMidConvoSystemMessages);\n\n\t// Start async processing\n\t(async () => {\n\t\tconst compat = getCompat(model);\n\t\tconst configurationUpdateEffort = getConfigurationUpdateEffort(model, options, compat);\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: model.api as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\t...(configurationUpdateEffort === undefined ? {} : { providerThinkingLevel: configurationUpdateEffort }),\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"pending\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\t// Create OpenAI client\n\t\t\tconst apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);\n\t\t\tconst cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);\n\t\t\tconst cacheSessionId = cacheRetention === \"none\" ? undefined : options?.sessionId;\n\t\t\tconst grammarToolInputProperties = createGrammarToolInputProperties(\n\t\t\t\tgetDeclaredTools(normalizedContext.messages),\n\t\t\t\tcompat.supportsOpenAIGrammarTools,\n\t\t\t);\n\t\t\tconst client = createClient(\n\t\t\t\tmodel,\n\t\t\t\tnormalizedContext,\n\t\t\t\tapiKey,\n\t\t\t\toptions?.headers,\n\t\t\t\toptions?.identity,\n\t\t\t\toptions?.fetch,\n\t\t\t\tcacheSessionId,\n\t\t\t);\n\t\t\tlet params = buildParams(\n\t\t\t\tmodel,\n\t\t\t\tnormalizedContext,\n\t\t\t\toptions,\n\t\t\t\tcompat,\n\t\t\t\tgrammarToolInputProperties,\n\t\t\t\tconfigurationUpdateEffort,\n\t\t\t);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as ResponseCreateParamsStreaming;\n\t\t\t}\n\t\t\tconst requestOptions = {\n\t\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\t\tmaxRetries: 0,\n\t\t\t};\n\t\t\tconst { data: openaiStream, response } = await retryProviderRequest(\n\t\t\t\t() => client.responses.create(params, requestOptions).withResponse(),\n\t\t\t\t{\n\t\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tawait processResponsesStream(openaiStream, output, stream, model, {\n\t\t\t\tonProviderStreamEvent: options?.onProviderStreamEvent,\n\t\t\t\tserviceTier: options?.serviceTier,\n\t\t\t\tgrammarToolInputProperties,\n\t\t\t\tapplyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),\n\t\t\t});\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"pending\") {\n\t\t\t\tthrow new Error(\"OpenAI Responses stream ended without a stop reason\");\n\t\t\t}\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(output.errorMessage || \"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\tconst errorMessage = formatProviderError(\n\t\t\t\tnormalizeProviderError(error),\n\t\t\t\t`${model.provider === \"openai\" ? \"OpenAI\" : model.provider} API error`,\n\t\t\t);\n\t\t\t// Sign in with ChatGPT shares the subscription's usage limit with other apps.\n\t\t\t// On ChatGPT Business plans, the workspace admin also sets a per-user limit.\n\t\t\tif (errorMessage.includes(\"subscription_sharing_usage_limit_exceeded\")) {\n\t\t\t\toutput.errorMessage = `${errorMessage}\\nCheck your ChatGPT usage: ${CHATGPT_USAGE_URL}`;\n\t\t\t} else if (errorMessage.includes(\"subscription_sharing_workplace_individual_usage_limit_exceeded\")) {\n\t\t\t\toutput.errorMessage = `${errorMessage}\\n${CHATGPT_WORKSPACE_LIMIT_HINT}`;\n\t\t\t} else {\n\t\t\t\toutput.errorMessage = errorMessage;\n\t\t\t}\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"openai-responses\", SimpleStreamOptions> = (\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tgetClientApiKey(model.provider, options?.apiKey, options?.headers);\n\n\tconst base = {\n\t\t...buildBaseOptions(model, context, options, options?.apiKey),\n\t\ttoolChoice: options?.toolChoice,\n\t} satisfies OpenAIResponsesOptions;\n\tconst clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;\n\tconst reasoningEffort = clampedReasoning === \"off\" ? undefined : clampedReasoning;\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\treasoningEffort,\n\t} satisfies OpenAIResponsesOptions);\n};\n\nfunction createClient(\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\tapiKey: string,\n\toptionsHeaders?: ProviderHeaders,\n\tidentity?: ClientIdentity,\n\tfetch?: typeof globalThis.fetch,\n\tsessionId?: string,\n) {\n\tconst compat = getCompat(model);\n\tconst headers: ProviderHeaders = { ...getIdentityHeaders(model, identity), ...model.headers };\n\tif (model.provider === \"github-copilot\") {\n\t\tconst hasImages = hasCopilotVisionInput(context.messages);\n\t\tconst copilotHeaders = buildCopilotDynamicHeaders({\n\t\t\tmessages: context.messages,\n\t\t\thasImages,\n\t\t});\n\t\tObject.assign(headers, copilotHeaders);\n\t}\n\n\tif (sessionId) {\n\t\tif (compat.sessionAffinityFormat === \"openrouter\") {\n\t\t\theaders[\"x-session-id\"] = sessionId;\n\t\t} else {\n\t\t\tif (compat.sessionAffinityFormat === \"openai\") {\n\t\t\t\theaders.session_id = sessionId;\n\t\t\t}\n\t\t\theaders[\"x-client-request-id\"] = sessionId;\n\t\t}\n\t}\n\n\t// Merge options headers last so they can override defaults\n\tif (optionsHeaders) {\n\t\tObject.assign(headers, optionsHeaders);\n\t}\n\n\treturn new OpenAI({\n\t\tapiKey,\n\t\tbaseURL: model.baseUrl,\n\t\tdangerouslyAllowBrowser: true,\n\t\tfetch,\n\t\tdefaultHeaders: headers,\n\t});\n}\n\nfunction buildParams(\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions: OpenAIResponsesOptions | undefined,\n\tcompat: Required<OpenAIResponsesCompat>,\n\tgrammarToolInputProperties: ReadonlyMap<string, string>,\n\tconfigurationUpdateEffort: string | undefined,\n) {\n\tconst transcriptTools = resolveTranscriptTools(context.messages, getResponsesToolSupport(compat));\n\tconst messages = convertResponsesMessages(model, context, OPENAI_TOOL_CALL_PROVIDERS, {\n\t\tgrammarToolInputProperties,\n\t\tsupportsMidConvoSystemMessages: compat.supportsMidConvoSystemMessages,\n\t\tsupportsAdditionalTools: compat.supportsAdditionalTools,\n\t\tsupportsToolSearch: compat.supportsToolSearch,\n\t\ttoolOptions: {\n\t\t\tsupportsStrictMode: compat.supportsStrictMode,\n\t\t\tsupportsOpenAIGrammarTools: compat.supportsOpenAIGrammarTools,\n\t\t},\n\t\tconfigurationUpdateEffort,\n\t});\n\n\tconst cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);\n\t// Sign in with ChatGPT rejects these request fields.\n\tconst omitUnsupportedFields = isChatGPTSignIn(model, options?.apiKey);\n\tconst params: ResponseCreateParamsStreaming = {\n\t\tmodel: model.id,\n\t\tinput: messages,\n\t\tstream: true,\n\t\tprompt_cache_key: cacheRetention === \"none\" ? undefined : clampOpenAIPromptCacheKey(options?.sessionId),\n\t\tprompt_cache_retention: omitUnsupportedFields ? undefined : getPromptCacheRetention(compat, cacheRetention),\n\t\tprompt_cache_options: omitUnsupportedFields ? undefined : getPromptCacheOptions(compat, cacheRetention),\n\t\tstore: false,\n\t};\n\n\tif (options?.maxTokens && compat.supportsMaxOutputTokens && !omitUnsupportedFields) {\n\t\tparams.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);\n\t}\n\n\tif (options?.temperature !== undefined && !omitUnsupportedFields) {\n\t\tparams.temperature = options?.temperature;\n\t}\n\n\tif (options?.serviceTier !== undefined) {\n\t\tparams.service_tier = options.serviceTier;\n\t}\n\n\tif (transcriptTools.requestTools.length > 0) {\n\t\tparams.tools = convertResponsesTools(transcriptTools.requestTools, {\n\t\t\tsupportsStrictMode: compat.supportsStrictMode,\n\t\t\tsupportsOpenAIGrammarTools: compat.supportsOpenAIGrammarTools,\n\t\t});\n\t}\n\n\tif (options?.toolChoice !== undefined) {\n\t\tparams.tool_choice = options.toolChoice;\n\t}\n\n\tconst reasoningEffort = options?.reasoningEffort ?? (options?.reasoningSummary ? \"medium\" : undefined);\n\tif (model.reasoning) {\n\t\tif (reasoningEffort) {\n\t\t\tconst requestedEffort = options?.reasoningEffort\n\t\t\t\t? (model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort)\n\t\t\t\t: reasoningEffort;\n\t\t\tconst effort = configurationUpdateEffort === undefined ? requestedEffort : CONFIGURATION_UPDATE_REQUEST_EFFORT;\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: effort as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t\tsummary: options?.reasoningSummary || \"auto\",\n\t\t\t};\n\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t} else if (model.provider !== \"github-copilot\" && model.thinkingLevelMap?.off !== null) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: (model.thinkingLevelMap?.off ?? \"none\") as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t};\n\t\t}\n\t\tif (model.provider === \"xai\") params.include = [\"reasoning.encrypted_content\"];\n\t}\n\n\t// Last so model and request sampling parameters override named request fields.\n\tconst samplingParams = resolveSamplingParams(model, reasoningEffort ?? \"off\", options?.samplingParams);\n\tif (samplingParams) {\n\t\tObject.assign(params, samplingParams);\n\t}\n\n\treturn params;\n}\n\nfunction getServiceTierCostMultiplier(\n\tmodel: Pick<Model<\"openai-responses\">, \"id\">,\n\tserviceTier: ResponseCreateParamsStreaming[\"service_tier\"] | undefined,\n): number {\n\tswitch (serviceTier) {\n\t\tcase \"flex\":\n\t\t\treturn 0.5;\n\t\tcase \"priority\":\n\t\tcase \"fast\":\n\t\t\treturn model.id === \"gpt-5.5\" ? 2.5 : 2;\n\t\tdefault:\n\t\t\treturn 1;\n\t}\n}\n\nfunction applyServiceTierPricing(\n\tusage: Usage,\n\tserviceTier: ResponseCreateParamsStreaming[\"service_tier\"] | undefined,\n\tmodel: Pick<Model<\"openai-responses\">, \"id\">,\n) {\n\tconst multiplier = getServiceTierCostMultiplier(model, serviceTier);\n\tif (multiplier === 1) return;\n\n\tusage.cost.input *= multiplier;\n\tusage.cost.output *= multiplier;\n\tusage.cost.cacheRead *= multiplier;\n\tusage.cost.cacheWrite *= multiplier;\n\tusage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"openai-responses.js","sourceRoot":"","sources":["../../src/api/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAelD,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,M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AAC;IACpE,IAAI,UAAU,KAAK,CAAC;QAAE,OAAO;IAE7B,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,UAAU,CAAC;IAC/B,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,UAAU,CAAC;IAChC,KAAK,CAAC,IAAI,CAAC,SAAS,IAAI,UAAU,CAAC;IACnC,KAAK,CAAC,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC;IACpC,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC;AACxG,CAAC","sourcesContent":["import OpenAI from \"openai\";\nimport type { ResponseCreateParamsStreaming } from \"openai/resources/responses/responses.js\";\nimport { clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tCacheRetention,\n\tModel,\n\tOpenAIResponsesCompat,\n\tProviderEnv,\n\tProviderHeaders,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tTranscriptContext,\n\tUsage,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord } from \"../utils/headers.ts\";\nimport { type ClientIdentity, getIdentityHeaders } from \"../utils/identity.ts\";\nimport { getProviderEnvValue } from \"../utils/provider-env.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { getDeclaredTools, resolveTranscript, resolveTranscriptTools } from \"../utils/transcript.ts\";\nimport { createGrammarToolInputProperties } from \"./constrained-sampling.ts\";\nimport { buildCopilotDynamicHeaders, hasCopilotVisionInput } from \"./github-copilot-headers.ts\";\nimport { clampOpenAIPromptCacheKey } from \"./openai-prompt-cache.ts\";\nimport {\n\tconvertResponsesMessages,\n\tconvertResponsesTools,\n\tgetResponsesToolSupport,\n\tprocessResponsesStream,\n} from \"./openai-responses-shared.ts\";\nimport { buildBaseOptions, resolveSamplingParams } from \"./simple-options.ts\";\n\nconst OPENAI_TOOL_CALL_PROVIDERS = new Set([\"openai\", \"openai-codex\", \"opencode\"]);\n// OpenAI Responses rejects max_output_tokens below 16: https://github.com/earendil-works/pi/issues/6265\nconst OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;\nconst CHATGPT_USAGE_URL = \"https://chatgpt.com/settings/usage\";\nconst CHATGPT_WORKSPACE_LIMIT_HINT =\n\t\"You've reached your ChatGPT workspace's usage limit. Ask your workspace admin to increase it at admin.openai.com → Workspace → Usage limits.\";\n// Fixed reasoning.effort while configuration_update items carry the thinking level.\nconst CONFIGURATION_UPDATE_REQUEST_EFFORT = \"medium\";\n\n/**\n * OpenAI API keys start with `sk-`; a different credential sent directly to OpenAI\n * is a Sign in with ChatGPT access token.\n */\nfunction isChatGPTSignIn(model: Model<\"openai-responses\">, apiKey: string | undefined): boolean {\n\treturn (\n\t\tmodel.provider === \"openai\" &&\n\t\tmodel.baseUrl === \"https://api.openai.com/v1\" &&\n\t\tapiKey !== undefined &&\n\t\t!apiKey.startsWith(\"sk-\")\n\t);\n}\n\nfunction hasHeader(headers: ProviderHeaders | undefined, name: string): boolean {\n\tif (!headers) return false;\n\tconst expected = name.toLowerCase();\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (key.toLowerCase() === expected && value !== null && value.trim().length > 0) return true;\n\t}\n\treturn false;\n}\n\nfunction getClientApiKey(provider: string, apiKey: string | undefined, headers: ProviderHeaders | undefined): string {\n\tif (apiKey) return apiKey;\n\tif (hasHeader(headers, \"authorization\") || hasHeader(headers, \"cf-aig-authorization\")) return \"unused\";\n\tthrow new Error(`No API key for provider: ${provider}`);\n}\n\nfunction detectSessionAffinityFormat(model: Pick<Model<\"openai-responses\">, \"provider\" | \"baseUrl\">) {\n\treturn model.provider === \"openrouter\" || model.baseUrl.includes(\"openrouter.ai\") ? \"openrouter\" : \"openai\";\n}\n\n/**\n * Resolve cache retention preference.\n * Defaults to \"short\" and uses PI_CACHE_RETENTION for backward compatibility.\n */\nfunction resolveCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nfunction getCompat(model: Model<\"openai-responses\">): Required<OpenAIResponsesCompat> {\n\treturn {\n\t\tsupportsDeveloperRole: model.compat?.supportsDeveloperRole ?? true,\n\t\tsupportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,\n\t\tsessionAffinityFormat: model.compat?.sessionAffinityFormat ?? detectSessionAffinityFormat(model),\n\t\tpromptCacheKeyMode: model.compat?.promptCacheKeyMode ?? \"auto\",\n\t\tsupportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true,\n\t\tsupportsStrictMode: model.compat?.supportsStrictMode ?? false,\n\t\tsupportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,\n\t\tsupportsAdditionalTools: model.compat?.supportsAdditionalTools ?? false,\n\t\tsupportsToolSearch: model.compat?.supportsToolSearch ?? false,\n\t\tsupportsExplicitPromptCacheMode: model.compat?.supportsExplicitPromptCacheMode ?? false,\n\t\tsupportsMaxOutputTokens: model.compat?.supportsMaxOutputTokens ?? true,\n\t\tsupportsMidConvoEffort: model.compat?.supportsMidConvoEffort ?? false,\n\t};\n}\n\n/** Effort to send in configuration_update items, or undefined to send it as reasoning.effort. */\nfunction getConfigurationUpdateEffort(\n\tmodel: Model<\"openai-responses\">,\n\toptions: OpenAIResponsesOptions | undefined,\n\tcompat: Required<OpenAIResponsesCompat>,\n): string | undefined {\n\tif (\n\t\t!compat.supportsMidConvoEffort ||\n\t\t!options?.reasoningEffort ||\n\t\t// Sign in with ChatGPT rejects configuration_update items.\n\t\tisChatGPTSignIn(model, options.apiKey)\n\t) {\n\t\treturn undefined;\n\t}\n\treturn model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;\n}\n\nfunction getPromptCacheRetention(\n\tcompat: Required<OpenAIResponsesCompat>,\n\tcacheRetention: CacheRetention,\n): \"24h\" | undefined {\n\treturn cacheRetention === \"long\" && compat.supportsLongCacheRetention && !compat.supportsExplicitPromptCacheMode\n\t\t? \"24h\"\n\t\t: undefined;\n}\n\nfunction getPromptCacheOptions(\n\tcompat: Required<OpenAIResponsesCompat>,\n\tcacheRetention: CacheRetention,\n): ResponseCreateParamsStreaming[\"prompt_cache_options\"] {\n\tif (!compat.supportsExplicitPromptCacheMode) return undefined;\n\tif (cacheRetention === \"none\") return { mode: \"explicit\" };\n\tif (cacheRetention === \"long\" && compat.supportsLongCacheRetention) return { ttl: \"30m\" };\n\treturn undefined;\n}\n\n// OpenAI Responses-specific options\nexport interface OpenAIResponsesOptions extends StreamOptions {\n\treasoningEffort?: \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\";\n\treasoningSummary?: \"auto\" | \"detailed\" | \"concise\" | null;\n\tserviceTier?: ResponseCreateParamsStreaming[\"service_tier\"];\n\ttoolChoice?: ResponseCreateParamsStreaming[\"tool_choice\"];\n}\n\n/**\n * Generate function for OpenAI Responses API\n */\nexport const stream: StreamFunction<\"openai-responses\", OpenAIResponsesOptions> = (\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: OpenAIResponsesOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\tconst normalizedContext = resolveTranscript(context, getCompat(model).supportsMidConvoSystemMessages);\n\n\t// Start async processing\n\t(async () => {\n\t\tconst compat = getCompat(model);\n\t\tconst configurationUpdateEffort = getConfigurationUpdateEffort(model, options, compat);\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: model.api as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\t...(configurationUpdateEffort === undefined ? {} : { providerThinkingLevel: configurationUpdateEffort }),\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"pending\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\t// Create OpenAI client\n\t\t\tconst apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);\n\t\t\tconst cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);\n\t\t\tconst cacheSessionId = cacheRetention === \"none\" ? undefined : options?.sessionId;\n\t\t\tconst grammarToolInputProperties = createGrammarToolInputProperties(\n\t\t\t\tgetDeclaredTools(normalizedContext.messages),\n\t\t\t\tcompat.supportsOpenAIGrammarTools,\n\t\t\t);\n\t\t\tconst client = createClient(\n\t\t\t\tmodel,\n\t\t\t\tnormalizedContext,\n\t\t\t\tapiKey,\n\t\t\t\toptions?.headers,\n\t\t\t\toptions?.identity,\n\t\t\t\toptions?.fetch,\n\t\t\t\tcacheSessionId,\n\t\t\t);\n\t\t\tlet params = buildParams(\n\t\t\t\tmodel,\n\t\t\t\tnormalizedContext,\n\t\t\t\toptions,\n\t\t\t\tcompat,\n\t\t\t\tgrammarToolInputProperties,\n\t\t\t\tconfigurationUpdateEffort,\n\t\t\t);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as ResponseCreateParamsStreaming;\n\t\t\t}\n\t\t\tconst requestOptions = {\n\t\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\t\tmaxRetries: 0,\n\t\t\t};\n\t\t\tconst { data: openaiStream, response } = await retryProviderRequest(\n\t\t\t\t() => client.responses.create(params, requestOptions).withResponse(),\n\t\t\t\t{\n\t\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t},\n\t\t\t);\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\t\t\tstream.push({ type: \"start\", partial: output });\n\n\t\t\tawait processResponsesStream(openaiStream, output, stream, model, {\n\t\t\t\tonProviderStreamEvent: options?.onProviderStreamEvent,\n\t\t\t\tserviceTier: options?.serviceTier,\n\t\t\t\tgrammarToolInputProperties,\n\t\t\t\tapplyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),\n\t\t\t});\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"pending\") {\n\t\t\t\tthrow new Error(\"OpenAI Responses stream ended without a stop reason\");\n\t\t\t}\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(output.errorMessage || \"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\tconst errorMessage = formatProviderError(\n\t\t\t\tnormalizeProviderError(error),\n\t\t\t\t`${model.provider === \"openai\" ? \"OpenAI\" : model.provider} API error`,\n\t\t\t);\n\t\t\t// Sign in with ChatGPT shares the subscription's usage limit with other apps.\n\t\t\t// On ChatGPT Business plans, the workspace admin also sets a per-user limit.\n\t\t\tif (errorMessage.includes(\"subscription_sharing_usage_limit_exceeded\")) {\n\t\t\t\toutput.errorMessage = `${errorMessage}\\nCheck your ChatGPT usage: ${CHATGPT_USAGE_URL}`;\n\t\t\t} else if (errorMessage.includes(\"subscription_sharing_workplace_individual_usage_limit_exceeded\")) {\n\t\t\t\toutput.errorMessage = `${errorMessage}\\n${CHATGPT_WORKSPACE_LIMIT_HINT}`;\n\t\t\t} else {\n\t\t\t\toutput.errorMessage = errorMessage;\n\t\t\t}\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"openai-responses\", SimpleStreamOptions> = (\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tgetClientApiKey(model.provider, options?.apiKey, options?.headers);\n\n\tconst base = {\n\t\t...buildBaseOptions(model, context, options, options?.apiKey),\n\t\ttoolChoice: options?.toolChoice,\n\t} satisfies OpenAIResponsesOptions;\n\tconst clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined;\n\tconst reasoningEffort = clampedReasoning === \"off\" ? undefined : clampedReasoning;\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\treasoningEffort,\n\t} satisfies OpenAIResponsesOptions);\n};\n\nfunction createClient(\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\tapiKey: string,\n\toptionsHeaders?: ProviderHeaders,\n\tidentity?: ClientIdentity,\n\tfetch?: typeof globalThis.fetch,\n\tsessionId?: string,\n) {\n\tconst compat = getCompat(model);\n\tconst headers: ProviderHeaders = { ...getIdentityHeaders(model, identity), ...model.headers };\n\tif (model.provider === \"github-copilot\") {\n\t\tconst hasImages = hasCopilotVisionInput(context.messages);\n\t\tconst copilotHeaders = buildCopilotDynamicHeaders({\n\t\t\tmessages: context.messages,\n\t\t\thasImages,\n\t\t});\n\t\tObject.assign(headers, copilotHeaders);\n\t}\n\n\tif (sessionId) {\n\t\tif (compat.sessionAffinityFormat === \"openrouter\") {\n\t\t\theaders[\"x-session-id\"] = sessionId;\n\t\t} else {\n\t\t\tif (compat.sessionAffinityFormat === \"openai\") {\n\t\t\t\theaders[\"session-id\"] = sessionId;\n\t\t\t}\n\t\t\theaders[\"x-client-request-id\"] = sessionId;\n\t\t}\n\t}\n\n\t// Merge options headers last so they can override defaults\n\tif (optionsHeaders) {\n\t\tObject.assign(headers, optionsHeaders);\n\t}\n\n\treturn new OpenAI({\n\t\tapiKey,\n\t\tbaseURL: model.baseUrl,\n\t\tdangerouslyAllowBrowser: true,\n\t\tfetch,\n\t\tdefaultHeaders: headers,\n\t});\n}\n\nfunction buildParams(\n\tmodel: Model<\"openai-responses\">,\n\tcontext: TranscriptContext,\n\toptions: OpenAIResponsesOptions | undefined,\n\tcompat: Required<OpenAIResponsesCompat>,\n\tgrammarToolInputProperties: ReadonlyMap<string, string>,\n\tconfigurationUpdateEffort: string | undefined,\n) {\n\tconst transcriptTools = resolveTranscriptTools(context.messages, getResponsesToolSupport(compat));\n\tconst messages = convertResponsesMessages(model, context, OPENAI_TOOL_CALL_PROVIDERS, {\n\t\tgrammarToolInputProperties,\n\t\tsupportsMidConvoSystemMessages: compat.supportsMidConvoSystemMessages,\n\t\tsupportsAdditionalTools: compat.supportsAdditionalTools,\n\t\tsupportsToolSearch: compat.supportsToolSearch,\n\t\ttoolOptions: {\n\t\t\tsupportsStrictMode: compat.supportsStrictMode,\n\t\t\tsupportsOpenAIGrammarTools: compat.supportsOpenAIGrammarTools,\n\t\t},\n\t\tconfigurationUpdateEffort,\n\t});\n\n\tconst cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);\n\tconst sendPromptCacheKey = cacheRetention !== \"none\" && compat.promptCacheKeyMode !== \"disabled\";\n\t// Sign in with ChatGPT rejects these request fields.\n\tconst omitUnsupportedFields = isChatGPTSignIn(model, options?.apiKey);\n\tconst params: ResponseCreateParamsStreaming = {\n\t\tmodel: model.id,\n\t\tinput: messages,\n\t\tstream: true,\n\t\tprompt_cache_key: sendPromptCacheKey ? clampOpenAIPromptCacheKey(options?.sessionId) : undefined,\n\t\tprompt_cache_retention: omitUnsupportedFields ? undefined : getPromptCacheRetention(compat, cacheRetention),\n\t\tprompt_cache_options: omitUnsupportedFields ? undefined : getPromptCacheOptions(compat, cacheRetention),\n\t\tstore: false,\n\t};\n\n\tif (options?.maxTokens && compat.supportsMaxOutputTokens && !omitUnsupportedFields) {\n\t\tparams.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);\n\t}\n\n\tif (options?.temperature !== undefined && !omitUnsupportedFields) {\n\t\tparams.temperature = options?.temperature;\n\t}\n\n\tif (options?.serviceTier !== undefined) {\n\t\tparams.service_tier = options.serviceTier;\n\t}\n\n\tif (transcriptTools.requestTools.length > 0) {\n\t\tparams.tools = convertResponsesTools(transcriptTools.requestTools, {\n\t\t\tsupportsStrictMode: compat.supportsStrictMode,\n\t\t\tsupportsOpenAIGrammarTools: compat.supportsOpenAIGrammarTools,\n\t\t});\n\t}\n\n\tif (options?.toolChoice !== undefined) {\n\t\tparams.tool_choice = options.toolChoice;\n\t}\n\n\tconst reasoningEffort = options?.reasoningEffort ?? (options?.reasoningSummary ? \"medium\" : undefined);\n\tif (model.reasoning) {\n\t\tif (reasoningEffort) {\n\t\t\tconst requestedEffort = options?.reasoningEffort\n\t\t\t\t? (model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort)\n\t\t\t\t: reasoningEffort;\n\t\t\tconst effort = configurationUpdateEffort === undefined ? requestedEffort : CONFIGURATION_UPDATE_REQUEST_EFFORT;\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: effort as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t\tsummary: options?.reasoningSummary || \"auto\",\n\t\t\t};\n\t\t\tparams.include = [\"reasoning.encrypted_content\"];\n\t\t} else if (model.provider !== \"github-copilot\" && model.thinkingLevelMap?.off !== null) {\n\t\t\tparams.reasoning = {\n\t\t\t\teffort: (model.thinkingLevelMap?.off ?? \"none\") as NonNullable<typeof params.reasoning>[\"effort\"],\n\t\t\t};\n\t\t}\n\t\tif (model.provider === \"xai\") params.include = [\"reasoning.encrypted_content\"];\n\t}\n\n\t// Last so model and request sampling parameters override named request fields.\n\tconst samplingParams = resolveSamplingParams(model, reasoningEffort ?? \"off\", options?.samplingParams);\n\tif (samplingParams) {\n\t\tObject.assign(params, samplingParams);\n\t}\n\n\treturn params;\n}\n\nfunction getServiceTierCostMultiplier(\n\tmodel: Pick<Model<\"openai-responses\">, \"id\">,\n\tserviceTier: ResponseCreateParamsStreaming[\"service_tier\"] | undefined,\n): number {\n\tswitch (serviceTier) {\n\t\tcase \"flex\":\n\t\t\treturn 0.5;\n\t\tcase \"priority\":\n\t\tcase \"fast\":\n\t\t\treturn model.id === \"gpt-5.5\" ? 2.5 : 2;\n\t\tdefault:\n\t\t\treturn 1;\n\t}\n}\n\nfunction applyServiceTierPricing(\n\tusage: Usage,\n\tserviceTier: ResponseCreateParamsStreaming[\"service_tier\"] | undefined,\n\tmodel: Pick<Model<\"openai-responses\">, \"id\">,\n) {\n\tconst multiplier = getServiceTierCostMultiplier(model, serviceTier);\n\tif (multiplier === 1) return;\n\n\tusage.cost.input *= multiplier;\n\tusage.cost.output *= multiplier;\n\tusage.cost.cacheRead *= multiplier;\n\tusage.cost.cacheWrite *= multiplier;\n\tusage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Api, Model, ModelThinkingLevel, SamplingParams, SimpleStreamOptions, StreamOptions, ThinkingBudgets, ThinkingLevel, TranscriptContext } from "../types.ts";
|
|
2
|
+
/** Fit maxTokens into the context window, leaving room for the input being counted higher than estimated. */
|
|
2
3
|
export declare function clampMaxTokensToContext(model: Model<Api>, context: TranscriptContext, maxTokens: number): number;
|
|
3
4
|
export declare function resolveSamplingParams(model: Model<Api>, thinkingLevel: ModelThinkingLevel, requestParams?: SamplingParams): SamplingParams | undefined;
|
|
4
5
|
export declare function buildBaseOptions(model: Model<Api>, context: TranscriptContext, options?: SimpleStreamOptions, apiKey?: string): StreamOptions;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simple-options.d.ts","sourceRoot":"","sources":["../../src/api/simple-options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,GAAG,EACH,KAAK,EACL,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"simple-options.d.ts","sourceRoot":"","sources":["../../src/api/simple-options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,GAAG,EACH,KAAK,EACL,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,MAAM,aAAa,CAAC;AAarB,6GAA6G;AAC7G,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAShH;AAED,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,aAAa,EAAE,kBAAkB,EACjC,aAAa,CAAC,EAAE,cAAc,GAC5B,cAAc,GAAG,SAAS,CAM5B;AAED,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,OAAO,EAAE,iBAAiB,EAC1B,OAAO,CAAC,EAAE,mBAAmB,EAC7B,MAAM,CAAC,EAAE,MAAM,GACb,aAAa,CA0Bf;AAED,4FAA4F;AAC5F,eAAO,MAAM,iBAAiB,OAAO,CAAC;AAEtC,eAAO,MAAM,wBAAwB,EAAE,eAKtC,CAAC;AAEF,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,GAAG,SAAS,GAAG,OAAO,CAAC,aAAa,EAAE,OAAO,GAAG,KAAK,CAAC,GAAG,SAAS,CAErH;AAED,wBAAgB,sBAAsB,CAAC,cAAc,EAAE,aAAa,EAAE,aAAa,CAAC,EAAE,eAAe,GAAG,MAAM,CAI7G;AAED,kGAAkG;AAClG,wBAAgB,+BAA+B,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/F;AAED,wBAAgB,0BAA0B,CAEzC,aAAa,EAAE,MAAM,GAAG,SAAS,EACjC,cAAc,EAAE,MAAM,EACtB,cAAc,EAAE,aAAa,EAC7B,aAAa,CAAC,EAAE,eAAe,GAC7B;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,CAU/C"}
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import { clampThinkingLevel } from "../models.js";
|
|
2
2
|
import { estimateContextTokens } from "../utils/estimate.js";
|
|
3
3
|
const CONTEXT_SAFETY_TOKENS = 4096;
|
|
4
|
+
// OpenRouter counts the input with its own tokenizer and rejects input + max_tokens above the
|
|
5
|
+
// window before routing. It counted prose up to 12% above the provider's count (#10497).
|
|
6
|
+
const CONTEXT_SAFETY_RATIO = 0.15;
|
|
7
|
+
// The 15% margin is kept only while it leaves at least this much output. Otherwise (a nearly full
|
|
8
|
+
// large window, or any small window such as a 32k local model) only the fixed margin is kept, and
|
|
9
|
+
// output is capped at this size so an OpenRouter overcount still fits for a while before compaction.
|
|
10
|
+
const MIN_OUTPUT_WITH_RATIO_MARGIN = 32768;
|
|
4
11
|
const MIN_MAX_TOKENS = 1;
|
|
12
|
+
/** Fit maxTokens into the context window, leaving room for the input being counted higher than estimated. */
|
|
5
13
|
export function clampMaxTokensToContext(model, context, maxTokens) {
|
|
6
14
|
if (model.contextWindow <= 0)
|
|
7
15
|
return Math.max(MIN_MAX_TOKENS, maxTokens);
|
|
8
|
-
const
|
|
9
|
-
|
|
16
|
+
const input = estimateContextTokens(context).tokens;
|
|
17
|
+
const margin = Math.max(CONTEXT_SAFETY_TOKENS, Math.ceil(input * CONTEXT_SAFETY_RATIO));
|
|
18
|
+
let room = model.contextWindow - input - margin;
|
|
19
|
+
if (room < MIN_OUTPUT_WITH_RATIO_MARGIN) {
|
|
20
|
+
room = Math.min(MIN_OUTPUT_WITH_RATIO_MARGIN, model.contextWindow - input - CONTEXT_SAFETY_TOKENS);
|
|
21
|
+
}
|
|
22
|
+
return Math.min(maxTokens, Math.max(MIN_MAX_TOKENS, room));
|
|
10
23
|
}
|
|
11
24
|
export function resolveSamplingParams(model, thinkingLevel, requestParams) {
|
|
12
25
|
const effectiveThinkingLevel = clampThinkingLevel(model, thinkingLevel);
|
|
@@ -28,6 +41,7 @@ export function buildBaseOptions(model, context, options, apiKey) {
|
|
|
28
41
|
transport: options?.transport,
|
|
29
42
|
cacheRetention: options?.cacheRetention,
|
|
30
43
|
sessionId: options?.sessionId,
|
|
44
|
+
disableContinuation: options?.disableContinuation,
|
|
31
45
|
headers: options?.headers,
|
|
32
46
|
identity: options?.identity,
|
|
33
47
|
onPayload: options?.onPayload,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simple-options.js","sourceRoot":"","sources":["../../src/api/simple-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAYlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAE7D,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB,MAAM,UAAU,uBAAuB,CAAC,KAAiB,EAAE,OAA0B,EAAE,SAAiB;IACvG,IAAI,KAAK,CAAC,aAAa,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IACzE,MAAM,
|
|
1
|
+
{"version":3,"file":"simple-options.js","sourceRoot":"","sources":["../../src/api/simple-options.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aAAa,GAAG,cAAc,EAAE,cAAc,CAAC,CAAC;IAEzG,IAAI,SAAS,IAAI,cAAc,EAAE,CAAC;QACjC,cAAc,GAAG,+BAA+B,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;AACtC,CAAC","sourcesContent":["import { clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tModel,\n\tModelThinkingLevel,\n\tSamplingParams,\n\tSimpleStreamOptions,\n\tStreamOptions,\n\tThinkingBudgets,\n\tThinkingLevel,\n\tTranscriptContext,\n} from \"../types.ts\";\nimport { estimateContextTokens } from \"../utils/estimate.ts\";\n\nconst CONTEXT_SAFETY_TOKENS = 4096;\n// OpenRouter counts the input with its own tokenizer and rejects input + max_tokens above the\n// window before routing. It counted prose up to 12% above the provider's count (#10497).\nconst CONTEXT_SAFETY_RATIO = 0.15;\n// The 15% margin is kept only while it leaves at least this much output. Otherwise (a nearly full\n// large window, or any small window such as a 32k local model) only the fixed margin is kept, and\n// output is capped at this size so an OpenRouter overcount still fits for a while before compaction.\nconst MIN_OUTPUT_WITH_RATIO_MARGIN = 32768;\nconst MIN_MAX_TOKENS = 1;\n\n/** Fit maxTokens into the context window, leaving room for the input being counted higher than estimated. */\nexport function clampMaxTokensToContext(model: Model<Api>, context: TranscriptContext, maxTokens: number): number {\n\tif (model.contextWindow <= 0) return Math.max(MIN_MAX_TOKENS, maxTokens);\n\tconst input = estimateContextTokens(context).tokens;\n\tconst margin = Math.max(CONTEXT_SAFETY_TOKENS, Math.ceil(input * CONTEXT_SAFETY_RATIO));\n\tlet room = model.contextWindow - input - margin;\n\tif (room < MIN_OUTPUT_WITH_RATIO_MARGIN) {\n\t\troom = Math.min(MIN_OUTPUT_WITH_RATIO_MARGIN, model.contextWindow - input - CONTEXT_SAFETY_TOKENS);\n\t}\n\treturn Math.min(maxTokens, Math.max(MIN_MAX_TOKENS, room));\n}\n\nexport function resolveSamplingParams(\n\tmodel: Model<Api>,\n\tthinkingLevel: ModelThinkingLevel,\n\trequestParams?: SamplingParams,\n): SamplingParams | undefined {\n\tconst effectiveThinkingLevel = clampThinkingLevel(model, thinkingLevel);\n\tconst thinkingLevelParams = model.samplingParamsByThinkingLevel?.[effectiveThinkingLevel];\n\treturn model.samplingParams || thinkingLevelParams || requestParams\n\t\t? { ...model.samplingParams, ...thinkingLevelParams, ...requestParams }\n\t\t: undefined;\n}\n\nexport function buildBaseOptions(\n\tmodel: Model<Api>,\n\tcontext: TranscriptContext,\n\toptions?: SimpleStreamOptions,\n\tapiKey?: string,\n): StreamOptions {\n\tconst samplingParams = resolveSamplingParams(model, options?.reasoning ?? \"off\", options?.samplingParams);\n\treturn {\n\t\ttemperature: options?.temperature,\n\t\tsamplingParams,\n\t\tmaxTokens: clampMaxTokensToContext(model, context, options?.maxTokens ?? model.maxTokens),\n\t\tsignal: options?.signal,\n\t\ttelemetry: options?.telemetry,\n\t\tapiKey: apiKey || options?.apiKey,\n\t\tfetch: options?.fetch,\n\t\ttransport: options?.transport,\n\t\tcacheRetention: options?.cacheRetention,\n\t\tsessionId: options?.sessionId,\n\t\tdisableContinuation: options?.disableContinuation,\n\t\theaders: options?.headers,\n\t\tidentity: options?.identity,\n\t\tonPayload: options?.onPayload,\n\t\tonResponse: options?.onResponse,\n\t\tonProviderStreamEvent: options?.onProviderStreamEvent,\n\t\ttimeoutMs: options?.timeoutMs,\n\t\twebsocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,\n\t\tmaxRetries: options?.maxRetries,\n\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\tmetadata: options?.metadata,\n\t\tenv: options?.env,\n\t};\n}\n\n/** Tokens always left for the answer when a thinking budget shares the response ceiling. */\nexport const MIN_ANSWER_TOKENS = 1024;\n\nexport const DEFAULT_THINKING_BUDGETS: ThinkingBudgets = {\n\tminimal: 1024,\n\tlow: 2048,\n\tmedium: 8192,\n\thigh: 16384,\n};\n\nexport function clampReasoning(effort: ThinkingLevel | undefined): Exclude<ThinkingLevel, \"xhigh\" | \"max\"> | undefined {\n\treturn effort === \"xhigh\" || effort === \"max\" ? \"high\" : effort;\n}\n\nexport function thinkingBudgetForLevel(reasoningLevel: ThinkingLevel, customBudgets?: ThinkingBudgets): number {\n\tconst budgets = { ...DEFAULT_THINKING_BUDGETS, ...customBudgets };\n\tconst level = clampReasoning(reasoningLevel)!;\n\treturn budgets[level]!;\n}\n\n/** Cap a thinking budget so at least MIN_ANSWER_TOKENS remain under a shared response ceiling. */\nexport function clampThinkingBudgetToAnswerRoom(thinkingBudget: number, ceiling: number): number {\n\treturn Math.min(thinkingBudget, Math.max(0, ceiling - MIN_ANSWER_TOKENS));\n}\n\nexport function adjustMaxTokensForThinking(\n\t// Undefined means no explicit caller cap. Use the model cap and fit thinking inside it.\n\tbaseMaxTokens: number | undefined,\n\tmodelMaxTokens: number,\n\treasoningLevel: ThinkingLevel,\n\tcustomBudgets?: ThinkingBudgets,\n): { maxTokens: number; thinkingBudget: number } {\n\tlet thinkingBudget = thinkingBudgetForLevel(reasoningLevel, customBudgets);\n\tconst maxTokens =\n\t\tbaseMaxTokens === undefined ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);\n\n\tif (maxTokens <= thinkingBudget) {\n\t\tthinkingBudget = clampThinkingBudgetToAnswerRoom(thinkingBudget, maxTokens);\n\t}\n\n\treturn { maxTokens, thinkingBudget };\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Returns true when a provider error message says the request's strict tools exceed the provider's limits. */
|
|
2
|
+
export declare function isStrictToolLimitErrorMessage(message: string): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Records that a session's strict tools exceeded Claude's limits; does nothing without a session. Keeps at most
|
|
5
|
+
* `MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS` sessions.
|
|
6
|
+
*
|
|
7
|
+
* Entries are normally freed by `cleanupSessionResources(sessionId)` when a session ends. The cap bounds memory for
|
|
8
|
+
* library users that never call it; a dropped session just pays one more rejection.
|
|
9
|
+
*/
|
|
10
|
+
export declare function markStrictToolLimitsExceeded(sessionId: string | undefined): void;
|
|
11
|
+
/** Returns true when an earlier request of this session had its strict tools rejected for exceeding Claude's limits. */
|
|
12
|
+
export declare function hasExceededStrictToolLimits(sessionId: string | undefined): boolean;
|
|
13
|
+
//# sourceMappingURL=strict-tool-limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict-tool-limits.d.ts","sourceRoot":"","sources":["../../src/api/strict-tool-limits.ts"],"names":[],"mappings":"AAYA,+GAA+G;AAC/G,wBAAgB,6BAA6B,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEtE;AAWD;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAOhF;AAED,wHAAwH;AACxH,wBAAgB,2BAA2B,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAElF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { registerSessionResourceCleanup } from "../session-resources.js";
|
|
2
|
+
/**
|
|
3
|
+
* Claude rejects (on the Anthropic API and on Bedrock) a request whose strict tools exceed its limits with one of these
|
|
4
|
+
* messages. Observed limits: 20 strict tools, and 16 optional or union-typed parameters across all strict tools.
|
|
5
|
+
*/
|
|
6
|
+
const STRICT_TOOL_LIMIT_ERROR_MESSAGES = [
|
|
7
|
+
"The compiled grammar is too large",
|
|
8
|
+
"Too many strict tools (",
|
|
9
|
+
"Schemas contains too many parameters with union types (",
|
|
10
|
+
];
|
|
11
|
+
/** Returns true when a provider error message says the request's strict tools exceed the provider's limits. */
|
|
12
|
+
export function isStrictToolLimitErrorMessage(message) {
|
|
13
|
+
return STRICT_TOOL_LIMIT_ERROR_MESSAGES.some((limitMessage) => message.includes(limitMessage));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Sessions whose strict tools Claude rejected for exceeding its limits. Their later requests send `strict: "prefer"`
|
|
17
|
+
* tools non-strict right away: the tool set rarely changes between turns, so trying strict again would only repeat
|
|
18
|
+
* the rejection, and switching back to strict later would rewrite the tool definitions and miss the prompt cache.
|
|
19
|
+
* Shared by the Anthropic and Bedrock providers, which run the same Claude limits.
|
|
20
|
+
*/
|
|
21
|
+
const sessionsOverStrictToolLimits = new Set();
|
|
22
|
+
const MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS = 10_000;
|
|
23
|
+
/**
|
|
24
|
+
* Records that a session's strict tools exceeded Claude's limits; does nothing without a session. Keeps at most
|
|
25
|
+
* `MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS` sessions.
|
|
26
|
+
*
|
|
27
|
+
* Entries are normally freed by `cleanupSessionResources(sessionId)` when a session ends. The cap bounds memory for
|
|
28
|
+
* library users that never call it; a dropped session just pays one more rejection.
|
|
29
|
+
*/
|
|
30
|
+
export function markStrictToolLimitsExceeded(sessionId) {
|
|
31
|
+
if (sessionId === undefined)
|
|
32
|
+
return;
|
|
33
|
+
sessionsOverStrictToolLimits.add(sessionId);
|
|
34
|
+
if (sessionsOverStrictToolLimits.size > MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS) {
|
|
35
|
+
const oldest = sessionsOverStrictToolLimits.values().next().value;
|
|
36
|
+
if (oldest !== undefined)
|
|
37
|
+
sessionsOverStrictToolLimits.delete(oldest);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Returns true when an earlier request of this session had its strict tools rejected for exceeding Claude's limits. */
|
|
41
|
+
export function hasExceededStrictToolLimits(sessionId) {
|
|
42
|
+
return sessionId !== undefined && sessionsOverStrictToolLimits.has(sessionId);
|
|
43
|
+
}
|
|
44
|
+
registerSessionResourceCleanup((sessionId) => {
|
|
45
|
+
if (sessionId)
|
|
46
|
+
sessionsOverStrictToolLimits.delete(sessionId);
|
|
47
|
+
else
|
|
48
|
+
sessionsOverStrictToolLimits.clear();
|
|
49
|
+
});
|
|
50
|
+
//# sourceMappingURL=strict-tool-limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict-tool-limits.js","sourceRoot":"","sources":["../../src/api/strict-tool-limits.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AAEzE;;;GAGG;AACH,MAAM,gCAAgC,GAAG;IACxC,mCAAmC;IACnC,yBAAyB;IACzB,yDAAyD;CACzD,CAAC;AAEF,+GAA+G;AAC/G,MAAM,UAAU,6BAA6B,CAAC,OAAe;IAC5D,OAAO,gCAAgC,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC;AAChG,CAAC;AAED;;;;;GAKG;AACH,MAAM,4BAA4B,GAAG,IAAI,GAAG,EAAU,CAAC;AACvD,MAAM,oCAAoC,GAAG,MAAM,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B,CAAC,SAA6B;IACzE,IAAI,SAAS,KAAK,SAAS;QAAE,OAAO;IACpC,4BAA4B,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC5C,IAAI,4BAA4B,CAAC,IAAI,GAAG,oCAAoC,EAAE,CAAC;QAC9E,MAAM,MAAM,GAAG,4BAA4B,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;QAClE,IAAI,MAAM,KAAK,SAAS;YAAE,4BAA4B,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvE,CAAC;AACF,CAAC;AAED,wHAAwH;AACxH,MAAM,UAAU,2BAA2B,CAAC,SAA6B;IACxE,OAAO,SAAS,KAAK,SAAS,IAAI,4BAA4B,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AAC/E,CAAC;AAED,8BAA8B,CAAC,CAAC,SAAS,EAAE,EAAE;IAC5C,IAAI,SAAS;QAAE,4BAA4B,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;;QACzD,4BAA4B,CAAC,KAAK,EAAE,CAAC;AAC3C,CAAC,CAAC,CAAC","sourcesContent":["import { registerSessionResourceCleanup } from \"../session-resources.ts\";\n\n/**\n * Claude rejects (on the Anthropic API and on Bedrock) a request whose strict tools exceed its limits with one of these\n * messages. Observed limits: 20 strict tools, and 16 optional or union-typed parameters across all strict tools.\n */\nconst STRICT_TOOL_LIMIT_ERROR_MESSAGES = [\n\t\"The compiled grammar is too large\",\n\t\"Too many strict tools (\",\n\t\"Schemas contains too many parameters with union types (\",\n];\n\n/** Returns true when a provider error message says the request's strict tools exceed the provider's limits. */\nexport function isStrictToolLimitErrorMessage(message: string): boolean {\n\treturn STRICT_TOOL_LIMIT_ERROR_MESSAGES.some((limitMessage) => message.includes(limitMessage));\n}\n\n/**\n * Sessions whose strict tools Claude rejected for exceeding its limits. Their later requests send `strict: \"prefer\"`\n * tools non-strict right away: the tool set rarely changes between turns, so trying strict again would only repeat\n * the rejection, and switching back to strict later would rewrite the tool definitions and miss the prompt cache.\n * Shared by the Anthropic and Bedrock providers, which run the same Claude limits.\n */\nconst sessionsOverStrictToolLimits = new Set<string>();\nconst MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS = 10_000;\n\n/**\n * Records that a session's strict tools exceeded Claude's limits; does nothing without a session. Keeps at most\n * `MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS` sessions.\n *\n * Entries are normally freed by `cleanupSessionResources(sessionId)` when a session ends. The cap bounds memory for\n * library users that never call it; a dropped session just pays one more rejection.\n */\nexport function markStrictToolLimitsExceeded(sessionId: string | undefined): void {\n\tif (sessionId === undefined) return;\n\tsessionsOverStrictToolLimits.add(sessionId);\n\tif (sessionsOverStrictToolLimits.size > MAX_SESSIONS_OVER_STRICT_TOOL_LIMITS) {\n\t\tconst oldest = sessionsOverStrictToolLimits.values().next().value;\n\t\tif (oldest !== undefined) sessionsOverStrictToolLimits.delete(oldest);\n\t}\n}\n\n/** Returns true when an earlier request of this session had its strict tools rejected for exceeding Claude's limits. */\nexport function hasExceededStrictToolLimits(sessionId: string | undefined): boolean {\n\treturn sessionId !== undefined && sessionsOverStrictToolLimits.has(sessionId);\n}\n\nregisterSessionResourceCleanup((sessionId) => {\n\tif (sessionId) sessionsOverStrictToolLimits.delete(sessionId);\n\telse sessionsOverStrictToolLimits.clear();\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../../src/auth/oauth/load.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../../src/auth/oauth/load.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAgB7C,KAAK,gBAAgB,GAAG;IACvB,SAAS,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAChD,WAAW,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAClD,aAAa,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACpD,aAAa,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACpD,UAAU,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACjD,UAAU,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACjD,IAAI,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAC3C,GAAG,EAAE,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAC1C,MAAM,EAAE,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,KAAK,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;CACvF,CAAC;AAIF,4EAA4E;AAC5E,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAE/E;AAED,eAAO,MAAM,kBAAkB,QAAa,OAAO,CAAC,SAAS,CAG5D,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAa,OAAO,CAAC,SAAS,CAG9D,CAAC;AAEF,eAAO,MAAM,sBAAsB,QAAa,OAAO,CAAC,SAAS,CAGhE,CAAC;AAEF,eAAO,MAAM,sBAAsB,QAAa,OAAO,CAAC,SAAS,CAGhE,CAAC;AAEF,eAAO,MAAM,mBAAmB,QAAa,OAAO,CAAC,SAAS,CAG7D,CAAC;AAEF,eAAO,MAAM,mBAAmB,QAAa,OAAO,CAAC,SAAS,CAG7D,CAAC;AAEF,eAAO,MAAM,aAAa,QAAa,OAAO,CAAC,SAAS,CAGvD,CAAC;AAEF,eAAO,MAAM,YAAY,QAAa,OAAO,CAAC,SAAS,CAGtD,CAAC;AAEF,eAAO,MAAM,eAAe,YAAmB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,KAAG,OAAO,CAAC,SAAS,CAOnG,CAAC"}
|
package/dist/auth/oauth/load.js
CHANGED
|
@@ -10,11 +10,14 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
10
10
|
* Loads an OAuth flow module through a variable specifier so bundlers cannot
|
|
11
11
|
* follow the import into Node-only flow code (`node:http` callback servers,
|
|
12
12
|
* `node:crypto` PKCE). The `.ts`/`.js` rewrite keeps the trick working from
|
|
13
|
-
* both source and built output.
|
|
13
|
+
* both source and built output. The specifier is resolved against this
|
|
14
|
+
* module's URL, not left relative: a bundler inlining this file (Chord's
|
|
15
|
+
* worker base) keeps the variable import, which would otherwise resolve
|
|
16
|
+
* against the bundle's location.
|
|
14
17
|
*/
|
|
15
18
|
const importOAuthModule = (specifier) => {
|
|
16
19
|
const runtimeSpecifier = import.meta.url.endsWith(".js") ? specifier.replace(/\.ts$/, ".js") : specifier;
|
|
17
|
-
return import(__rewriteRelativeImportExtension(runtimeSpecifier));
|
|
20
|
+
return import(__rewriteRelativeImportExtension(new URL(runtimeSpecifier, import.meta.url).href));
|
|
18
21
|
};
|
|
19
22
|
let bundledLoaders;
|
|
20
23
|
/** Registers statically bundled OAuth flows for standalone Bun binaries. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.js","sourceRoot":"","sources":["../../../src/auth/oauth/load.ts"],"names":[],"mappings":";;;;;;;;AAEA
|
|
1
|
+
{"version":3,"file":"load.js","sourceRoot":"","sources":["../../../src/auth/oauth/load.ts"],"names":[],"mappings":";;;;;;;;AAEA;;;;;;;;GAQG;AACH,MAAM,iBAAiB,GAAG,CAAC,SAAiB,EAAoB,EAAE;IACjE,MAAM,gBAAgB,GAAG,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACzG,OAAO,MAAM,kCAAC,IAAI,GAAG,CAAC,gBAAgB,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAC,CAAC;AAChE,CAAC,CAAC;AAcF,IAAI,cAA4C,CAAC;AAEjD,4EAA4E;AAC5E,MAAM,UAAU,+BAA+B,CAAC,OAAyB;IACxE,cAAc,GAAG,OAAO,CAAC;AAC1B,CAAC;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,IAAwB,EAAE;IAChE,IAAI,cAAc;QAAE,OAAO,cAAc,CAAC,SAAS,EAAE,CAAC;IACtD,OAAQ,CAAC,MAAM,iBAAiB,CAAC,gBAAgB,CAAC,CAAmC,CAAC,cAAc,CAAC;AACtG,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,IAA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C;AACvF,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,IAAwB,EAAE;IAC1D,IAAI,cAAc;QAAE,OAAO,cAAc,CAAC,GAAG,EAAE,CAAC;IAChD,OAAQ,CAAC,MAAM,iBAAiB,CAAC,UAAU,CAAC,CAA6B,CAAC,QAAQ,CAAC;AACpF,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,OAA0C,EAAsB,EAAE;IACvG,IAAI,cAAc;QAAE,OAAO,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC1D,OACC,CAAC,MAAM,iBAAiB,CAAC,aAAa,CAAC,CAGvC,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;AAC9B,CAAC,CAAC","sourcesContent":["import type { OAuthAuth } from \"../types.ts\";\n\n/**\n * Loads an OAuth flow module through a variable specifier so bundlers cannot\n * follow the import into Node-only flow code (`node:http` callback servers,\n * `node:crypto` PKCE). The `.ts`/`.js` rewrite keeps the trick working from\n * both source and built output. The specifier is resolved against this\n * module's URL, not left relative: a bundler inlining this file (Chord's\n * worker base) keeps the variable import, which would otherwise resolve\n * against the bundle's location.\n */\nconst importOAuthModule = (specifier: string): Promise<unknown> => {\n\tconst runtimeSpecifier = import.meta.url.endsWith(\".js\") ? specifier.replace(/\\.ts$/, \".js\") : specifier;\n\treturn import(new URL(runtimeSpecifier, import.meta.url).href);\n};\n\ntype OAuthFlowLoaders = {\n\tanthropic: () => OAuthAuth | Promise<OAuthAuth>;\n\topenaiCodex: () => OAuthAuth | Promise<OAuthAuth>;\n\topenaiChatGPT: () => OAuthAuth | Promise<OAuthAuth>;\n\tgithubCopilot: () => OAuthAuth | Promise<OAuthAuth>;\n\topenrouter: () => OAuthAuth | Promise<OAuthAuth>;\n\tkimiCoding: () => OAuthAuth | Promise<OAuthAuth>;\n\tmeta: () => OAuthAuth | Promise<OAuthAuth>;\n\txai: () => OAuthAuth | Promise<OAuthAuth>;\n\tradius: (options: { name: string; gateway: string }) => OAuthAuth | Promise<OAuthAuth>;\n};\n\nlet bundledLoaders: OAuthFlowLoaders | undefined;\n\n/** Registers statically bundled OAuth flows for standalone Bun binaries. */\nexport function registerBundledOAuthFlowLoaders(loaders: OAuthFlowLoaders): void {\n\tbundledLoaders = loaders;\n}\n\nexport const loadAnthropicOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.anthropic();\n\treturn ((await importOAuthModule(\"./anthropic.ts\")) as { anthropicOAuth: OAuthAuth }).anthropicOAuth;\n};\n\nexport const loadOpenAICodexOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.openaiCodex();\n\treturn ((await importOAuthModule(\"./openai-codex.ts\")) as { openaiCodexOAuth: OAuthAuth }).openaiCodexOAuth;\n};\n\nexport const loadOpenAIChatGPTOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.openaiChatGPT();\n\treturn ((await importOAuthModule(\"./openai-chatgpt.ts\")) as { openaiChatGPTOAuth: OAuthAuth }).openaiChatGPTOAuth;\n};\n\nexport const loadGitHubCopilotOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.githubCopilot();\n\treturn ((await importOAuthModule(\"./github-copilot.ts\")) as { githubCopilotOAuth: OAuthAuth }).githubCopilotOAuth;\n};\n\nexport const loadOpenRouterOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.openrouter();\n\treturn ((await importOAuthModule(\"./openrouter.ts\")) as { openRouterOAuth: OAuthAuth }).openRouterOAuth;\n};\n\nexport const loadKimiCodingOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.kimiCoding();\n\treturn ((await importOAuthModule(\"./kimi-coding.ts\")) as { kimiCodingOAuth: OAuthAuth }).kimiCodingOAuth;\n};\n\nexport const loadMetaOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.meta();\n\treturn ((await importOAuthModule(\"./meta.ts\")) as { metaOAuth: OAuthAuth }).metaOAuth;\n};\n\nexport const loadXaiOAuth = async (): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.xai();\n\treturn ((await importOAuthModule(\"./xai.ts\")) as { xaiOAuth: OAuthAuth }).xaiOAuth;\n};\n\nexport const loadRadiusOAuth = async (options: { name: string; gateway: string }): Promise<OAuthAuth> => {\n\tif (bundledLoaders) return bundledLoaders.radius(options);\n\treturn (\n\t\t(await importOAuthModule(\"./radius.ts\")) as {\n\t\t\tcreateRadiusOAuth: (input: { name: string; gateway: string }) => OAuthAuth;\n\t\t}\n\t).createRadiusOAuth(options);\n};\n"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Api, Model, SimpleStreamOptions } from "./types.ts";
|
|
2
|
+
/** Whether a refresh is sent ("warm") or warming ends until the next real request ("stop"). */
|
|
3
|
+
export type CacheWarmingAction = "warm" | "stop";
|
|
4
|
+
/** "streaming" while the agent run that sent the request is still active; "idle" after it settled. */
|
|
5
|
+
export type CacheWarmingPhase = "streaming" | "idle";
|
|
6
|
+
/** Inputs and outcome of one warm-or-stop decision. */
|
|
7
|
+
export interface CacheWarmingDecision {
|
|
8
|
+
phase: CacheWarmingPhase;
|
|
9
|
+
/** Price of this refresh: a cache read of the prompt plus one output token. */
|
|
10
|
+
warmCost: number;
|
|
11
|
+
/** Extra price of the next real request if the cache entry is lost. */
|
|
12
|
+
missCost: number;
|
|
13
|
+
/** Estimated chance that a real request arrives before the entry expires. */
|
|
14
|
+
continuationProbability: number;
|
|
15
|
+
/** `continuationProbability * missCost - warmCost`. */
|
|
16
|
+
expectedSavings: number;
|
|
17
|
+
/** False when the prompt size or the model's prices are unknown. */
|
|
18
|
+
economicsAvailable: boolean;
|
|
19
|
+
/** "warm" when `expectedSavings` is at least `CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS`. */
|
|
20
|
+
action: CacheWarmingAction;
|
|
21
|
+
}
|
|
22
|
+
/** Streaming warming never continues past this long after the real request that started it. */
|
|
23
|
+
export declare const CACHE_WARMING_MAX_AGE_MS: number;
|
|
24
|
+
/** Idle warming uses a shorter horizon because continuation estimates become less reliable with age. */
|
|
25
|
+
export declare const CACHE_WARMING_MAX_IDLE_AGE_MS: number;
|
|
26
|
+
/** A refresh is sent only when it is expected to save at least this many dollars. */
|
|
27
|
+
export declare const CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS = 0.05;
|
|
28
|
+
/**
|
|
29
|
+
* Chance that a real request arrives before the cache entry expires while the agent sits idle. Measured from our own
|
|
30
|
+
* usage; per-session estimates were not better than this constant.
|
|
31
|
+
*/
|
|
32
|
+
export declare const CACHE_WARMING_IDLE_CONTINUATION_PROBABILITY = 0.15;
|
|
33
|
+
/** Refresh at 90% of the TTL while preserving at least ten seconds of margin. */
|
|
34
|
+
export declare function getCacheWarmingDelayMs(ttlMs: number): number | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Lifetime of the prompt cache entry a request writes, from the model's `promptCache` tier for the retention the
|
|
37
|
+
* request used. Undefined when the model has no lifetime for that tier or caching is off.
|
|
38
|
+
*/
|
|
39
|
+
export declare function getPromptCacheTtlMs(model: Model<Api>, options: SimpleStreamOptions | undefined): number | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Whether replaying the request with a one-token output cap leaves its cache entry untouched. Anthropic's budget-based
|
|
42
|
+
* thinking (Claude models without adaptive thinking) derives `budget_tokens` from `max_tokens`; the replay would get a
|
|
43
|
+
* different budget, which Anthropic keys the message cache on, and the model could still think for thousands of tokens.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isCacheWarmingReplayable(model: Model<Api>, options: SimpleStreamOptions | undefined): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The options of a refresh: the request's own, capped at one output token, without retries, and without replacing the
|
|
48
|
+
* conversation's provider thread state (the refresh repeats the last request).
|
|
49
|
+
*/
|
|
50
|
+
export declare function getCacheWarmingRequestOptions<T extends SimpleStreamOptions>(options: T, signal: AbortSignal): T;
|
|
51
|
+
/** Decide whether refreshing the cache entry of a `promptTokens`-token request pays for itself in `phase`. */
|
|
52
|
+
export declare function evaluateCacheWarming(model: Model<Api>, promptTokens: number, phase: CacheWarmingPhase): CacheWarmingDecision;
|
|
53
|
+
//# sourceMappingURL=cache-warming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-warming.d.ts","sourceRoot":"","sources":["../src/cache-warming.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,mBAAmB,EAAS,MAAM,YAAY,CAAC;AAGzE,+FAA+F;AAC/F,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,MAAM,CAAC;AAEjD,sGAAsG;AACtG,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,MAAM,CAAC;AAErD,uDAAuD;AACvD,MAAM,WAAW,oBAAoB;IACpC,KAAK,EAAE,iBAAiB,CAAC;IACzB,+EAA+E;IAC/E,QAAQ,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,QAAQ,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,uBAAuB,EAAE,MAAM,CAAC;IAChC,uDAAuD;IACvD,eAAe,EAAE,MAAM,CAAC;IACxB,oEAAoE;IACpE,kBAAkB,EAAE,OAAO,CAAC;IAC5B,0FAA0F;IAC1F,MAAM,EAAE,kBAAkB,CAAC;CAC3B;AAED,+FAA+F;AAC/F,eAAO,MAAM,wBAAwB,QAAc,CAAC;AAEpD,wGAAwG;AACxG,eAAO,MAAM,6BAA6B,QAAc,CAAC;AAEzD,qFAAqF;AACrF,eAAO,MAAM,sCAAsC,OAAO,CAAC;AAE3D;;;GAGG;AACH,eAAO,MAAM,2CAA2C,OAAO,CAAC;AAEhE,iFAAiF;AACjF,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGxE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,mBAAmB,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAOnH;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,mBAAmB,GAAG,SAAS,GAAG,OAAO,CAG7G;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAAC,CAAC,SAAS,mBAAmB,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,GAAG,CAAC,CAE/G;AAED,8GAA8G;AAC9G,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,iBAAiB,GACtB,oBAAoB,CAoBtB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Prompt cache warming policy shared by pi's hosts: when a refresh is due, whether a request can be replayed, and
|
|
2
|
+
// whether a refresh pays for itself. Hosts own the scheduling; this module owns the numbers.
|
|
3
|
+
import { calculateCost } from "./models.js";
|
|
4
|
+
import { getProviderEnvValue } from "./utils/provider-env.js";
|
|
5
|
+
/** Streaming warming never continues past this long after the real request that started it. */
|
|
6
|
+
export const CACHE_WARMING_MAX_AGE_MS = 60 * 60_000;
|
|
7
|
+
/** Idle warming uses a shorter horizon because continuation estimates become less reliable with age. */
|
|
8
|
+
export const CACHE_WARMING_MAX_IDLE_AGE_MS = 30 * 60_000;
|
|
9
|
+
/** A refresh is sent only when it is expected to save at least this many dollars. */
|
|
10
|
+
export const CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS = 0.05;
|
|
11
|
+
/**
|
|
12
|
+
* Chance that a real request arrives before the cache entry expires while the agent sits idle. Measured from our own
|
|
13
|
+
* usage; per-session estimates were not better than this constant.
|
|
14
|
+
*/
|
|
15
|
+
export const CACHE_WARMING_IDLE_CONTINUATION_PROBABILITY = 0.15;
|
|
16
|
+
/** Refresh at 90% of the TTL while preserving at least ten seconds of margin. */
|
|
17
|
+
export function getCacheWarmingDelayMs(ttlMs) {
|
|
18
|
+
if (ttlMs <= 10_000)
|
|
19
|
+
return undefined;
|
|
20
|
+
return Math.max(1, Math.floor(Math.min(ttlMs * 0.9, ttlMs - 10_000)));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Lifetime of the prompt cache entry a request writes, from the model's `promptCache` tier for the retention the
|
|
24
|
+
* request used. Undefined when the model has no lifetime for that tier or caching is off.
|
|
25
|
+
*/
|
|
26
|
+
export function getPromptCacheTtlMs(model, options) {
|
|
27
|
+
const retention = options?.cacheRetention ??
|
|
28
|
+
(getProviderEnvValue("PI_CACHE_RETENTION", options?.env) === "long" ? "long" : "short");
|
|
29
|
+
if (retention === "none")
|
|
30
|
+
return undefined;
|
|
31
|
+
const seconds = model.promptCache?.[retention];
|
|
32
|
+
return seconds === undefined ? undefined : seconds * 1000;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Whether replaying the request with a one-token output cap leaves its cache entry untouched. Anthropic's budget-based
|
|
36
|
+
* thinking (Claude models without adaptive thinking) derives `budget_tokens` from `max_tokens`; the replay would get a
|
|
37
|
+
* different budget, which Anthropic keys the message cache on, and the model could still think for thousands of tokens.
|
|
38
|
+
*/
|
|
39
|
+
export function isCacheWarmingReplayable(model, options) {
|
|
40
|
+
if (!options?.reasoning || model.api !== "anthropic-messages")
|
|
41
|
+
return true;
|
|
42
|
+
return model.compat?.forceAdaptiveThinking === true;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The options of a refresh: the request's own, capped at one output token, without retries, and without replacing the
|
|
46
|
+
* conversation's provider thread state (the refresh repeats the last request).
|
|
47
|
+
*/
|
|
48
|
+
export function getCacheWarmingRequestOptions(options, signal) {
|
|
49
|
+
return { ...options, maxTokens: 1, disableContinuation: true, maxRetries: 0, signal };
|
|
50
|
+
}
|
|
51
|
+
/** Decide whether refreshing the cache entry of a `promptTokens`-token request pays for itself in `phase`. */
|
|
52
|
+
export function evaluateCacheWarming(model, promptTokens, phase) {
|
|
53
|
+
const cacheHitCost = price(model, { cacheRead: promptTokens });
|
|
54
|
+
const cacheMissCost = price(model, model.cost.cacheWrite > 0 ? { cacheWrite: promptTokens } : { input: promptTokens });
|
|
55
|
+
const warmCost = price(model, { cacheRead: promptTokens, output: 1 });
|
|
56
|
+
const missCost = Math.max(0, cacheMissCost - cacheHitCost);
|
|
57
|
+
const continuationProbability = phase === "idle" ? CACHE_WARMING_IDLE_CONTINUATION_PROBABILITY : 1;
|
|
58
|
+
const economicsAvailable = promptTokens > 0 && (cacheHitCost > 0 || cacheMissCost > 0);
|
|
59
|
+
const expectedSavings = continuationProbability * missCost - warmCost;
|
|
60
|
+
return {
|
|
61
|
+
phase,
|
|
62
|
+
warmCost,
|
|
63
|
+
missCost,
|
|
64
|
+
continuationProbability,
|
|
65
|
+
expectedSavings,
|
|
66
|
+
economicsAvailable,
|
|
67
|
+
action: expectedSavings >= CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS ? "warm" : "stop",
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function price(model, tokens) {
|
|
71
|
+
const usage = {
|
|
72
|
+
input: 0,
|
|
73
|
+
output: 0,
|
|
74
|
+
cacheRead: 0,
|
|
75
|
+
cacheWrite: 0,
|
|
76
|
+
totalTokens: 0,
|
|
77
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
78
|
+
...tokens,
|
|
79
|
+
};
|
|
80
|
+
return calculateCost(model, usage).total;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=cache-warming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-warming.js","sourceRoot":"","sources":["../src/cache-warming.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,6FAA6F;AAC7F,OAAO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ACN,KAAK;QACL,QAAQ;QACR,QAAQ;QACR,uBAAuB;QACvB,eAAe;QACf,kBAAkB;QAClB,MAAM,EAAE,eAAe,IAAI,sCAAsC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM;KACnF,CAAC;AACH,CAAC;AAED,SAAS,KAAK,CACb,KAAiB,EACjB,MAA6E;IAE7E,MAAM,KAAK,GAAU;QACpB,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,WAAW,EAAE,CAAC;QACd,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE;QACpE,GAAG,MAAM;KACT,CAAC;IACF,OAAO,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,KAAK,CAAC;AAC1C,CAAC","sourcesContent":["// Prompt cache warming policy shared by pi's hosts: when a refresh is due, whether a request can be replayed, and\n// whether a refresh pays for itself. Hosts own the scheduling; this module owns the numbers.\nimport { calculateCost } from \"./models.ts\";\nimport type { Api, Model, SimpleStreamOptions, Usage } from \"./types.ts\";\nimport { getProviderEnvValue } from \"./utils/provider-env.ts\";\n\n/** Whether a refresh is sent (\"warm\") or warming ends until the next real request (\"stop\"). */\nexport type CacheWarmingAction = \"warm\" | \"stop\";\n\n/** \"streaming\" while the agent run that sent the request is still active; \"idle\" after it settled. */\nexport type CacheWarmingPhase = \"streaming\" | \"idle\";\n\n/** Inputs and outcome of one warm-or-stop decision. */\nexport interface CacheWarmingDecision {\n\tphase: CacheWarmingPhase;\n\t/** Price of this refresh: a cache read of the prompt plus one output token. */\n\twarmCost: number;\n\t/** Extra price of the next real request if the cache entry is lost. */\n\tmissCost: number;\n\t/** Estimated chance that a real request arrives before the entry expires. */\n\tcontinuationProbability: number;\n\t/** `continuationProbability * missCost - warmCost`. */\n\texpectedSavings: number;\n\t/** False when the prompt size or the model's prices are unknown. */\n\teconomicsAvailable: boolean;\n\t/** \"warm\" when `expectedSavings` is at least `CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS`. */\n\taction: CacheWarmingAction;\n}\n\n/** Streaming warming never continues past this long after the real request that started it. */\nexport const CACHE_WARMING_MAX_AGE_MS = 60 * 60_000;\n\n/** Idle warming uses a shorter horizon because continuation estimates become less reliable with age. */\nexport const CACHE_WARMING_MAX_IDLE_AGE_MS = 30 * 60_000;\n\n/** A refresh is sent only when it is expected to save at least this many dollars. */\nexport const CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS = 0.05;\n\n/**\n * Chance that a real request arrives before the cache entry expires while the agent sits idle. Measured from our own\n * usage; per-session estimates were not better than this constant.\n */\nexport const CACHE_WARMING_IDLE_CONTINUATION_PROBABILITY = 0.15;\n\n/** Refresh at 90% of the TTL while preserving at least ten seconds of margin. */\nexport function getCacheWarmingDelayMs(ttlMs: number): number | undefined {\n\tif (ttlMs <= 10_000) return undefined;\n\treturn Math.max(1, Math.floor(Math.min(ttlMs * 0.9, ttlMs - 10_000)));\n}\n\n/**\n * Lifetime of the prompt cache entry a request writes, from the model's `promptCache` tier for the retention the\n * request used. Undefined when the model has no lifetime for that tier or caching is off.\n */\nexport function getPromptCacheTtlMs(model: Model<Api>, options: SimpleStreamOptions | undefined): number | undefined {\n\tconst retention =\n\t\toptions?.cacheRetention ??\n\t\t(getProviderEnvValue(\"PI_CACHE_RETENTION\", options?.env) === \"long\" ? \"long\" : \"short\");\n\tif (retention === \"none\") return undefined;\n\tconst seconds = model.promptCache?.[retention];\n\treturn seconds === undefined ? undefined : seconds * 1000;\n}\n\n/**\n * Whether replaying the request with a one-token output cap leaves its cache entry untouched. Anthropic's budget-based\n * thinking (Claude models without adaptive thinking) derives `budget_tokens` from `max_tokens`; the replay would get a\n * different budget, which Anthropic keys the message cache on, and the model could still think for thousands of tokens.\n */\nexport function isCacheWarmingReplayable(model: Model<Api>, options: SimpleStreamOptions | undefined): boolean {\n\tif (!options?.reasoning || model.api !== \"anthropic-messages\") return true;\n\treturn (model as Model<\"anthropic-messages\">).compat?.forceAdaptiveThinking === true;\n}\n\n/**\n * The options of a refresh: the request's own, capped at one output token, without retries, and without replacing the\n * conversation's provider thread state (the refresh repeats the last request).\n */\nexport function getCacheWarmingRequestOptions<T extends SimpleStreamOptions>(options: T, signal: AbortSignal): T {\n\treturn { ...options, maxTokens: 1, disableContinuation: true, maxRetries: 0, signal };\n}\n\n/** Decide whether refreshing the cache entry of a `promptTokens`-token request pays for itself in `phase`. */\nexport function evaluateCacheWarming(\n\tmodel: Model<Api>,\n\tpromptTokens: number,\n\tphase: CacheWarmingPhase,\n): CacheWarmingDecision {\n\tconst cacheHitCost = price(model, { cacheRead: promptTokens });\n\tconst cacheMissCost = price(\n\t\tmodel,\n\t\tmodel.cost.cacheWrite > 0 ? { cacheWrite: promptTokens } : { input: promptTokens },\n\t);\n\tconst warmCost = price(model, { cacheRead: promptTokens, output: 1 });\n\tconst missCost = Math.max(0, cacheMissCost - cacheHitCost);\n\tconst continuationProbability = phase === \"idle\" ? CACHE_WARMING_IDLE_CONTINUATION_PROBABILITY : 1;\n\tconst economicsAvailable = promptTokens > 0 && (cacheHitCost > 0 || cacheMissCost > 0);\n\tconst expectedSavings = continuationProbability * missCost - warmCost;\n\treturn {\n\t\tphase,\n\t\twarmCost,\n\t\tmissCost,\n\t\tcontinuationProbability,\n\t\texpectedSavings,\n\t\teconomicsAvailable,\n\t\taction: expectedSavings >= CACHE_WARMING_MINIMUM_EXPECTED_SAVINGS ? \"warm\" : \"stop\",\n\t};\n}\n\nfunction price(\n\tmodel: Model<Api>,\n\ttokens: Partial<Pick<Usage, \"input\" | \"output\" | \"cacheRead\" | \"cacheWrite\">>,\n): number {\n\tconst usage: Usage = {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\ttotalTokens: 0,\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t...tokens,\n\t};\n\treturn calculateCost(model, usage).total;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env-api-keys.d.ts","sourceRoot":"","sources":["../src/env-api-keys.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG7D,eAAO,MAAM,wBAAwB,yBAAyB,CAAC;AAC/D,eAAO,MAAM,yBAAyB,0BAA0B,CAAC;AACjE,eAAO,MAAM,qBAAqB,sBAAsB,CAAC;AACzD,eAAO,MAAM,gCAAgC,iCAAiC,CAAC;AAC/E,eAAO,MAAM,6BAA6B,8BAA8B,CAAC;AACzE,eAAO,MAAM,gCAAgC,iCAAiC,CAAC;AAC/E,eAAO,MAAM,iCAAiC,kCAAkC,CAAC;AACjF,eAAO,MAAM,0BAA0B,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"env-api-keys.d.ts","sourceRoot":"","sources":["../src/env-api-keys.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG7D,eAAO,MAAM,wBAAwB,yBAAyB,CAAC;AAC/D,eAAO,MAAM,yBAAyB,0BAA0B,CAAC;AACjE,eAAO,MAAM,qBAAqB,sBAAsB,CAAC;AACzD,eAAO,MAAM,gCAAgC,iCAAiC,CAAC;AAC/E,eAAO,MAAM,6BAA6B,8BAA8B,CAAC;AACzE,eAAO,MAAM,gCAAgC,iCAAiC,CAAC;AAC/E,eAAO,MAAM,iCAAiC,kCAAkC,CAAC;AACjF,eAAO,MAAM,0BAA0B,2BAA2B,CAAC;AA8FnE;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,aAAa,EAAE,GAAG,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;AAC9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