pi2dsh 0.3.0 → 0.3.2

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.
Files changed (104) hide show
  1. package/README.md +31 -11
  2. package/README.zh.md +31 -11
  3. package/dist/all-Rt4K68QA.mjs +28512 -0
  4. package/dist/all-Rt4K68QA.mjs.map +1 -0
  5. package/dist/anthropic-messages-Beym0_aC.mjs +6438 -0
  6. package/dist/anthropic-messages-Beym0_aC.mjs.map +1 -0
  7. package/dist/azure-openai-responses-cHqZTgLD.mjs +209 -0
  8. package/dist/azure-openai-responses-cHqZTgLD.mjs.map +1 -0
  9. package/dist/{build-DsGYAgiT.mjs → build-BG7p2Lbr.mjs} +2 -2
  10. package/dist/{build-DsGYAgiT.mjs.map → build-BG7p2Lbr.mjs.map} +1 -1
  11. package/dist/cli.mjs +1 -1
  12. package/dist/compat/pi-ai.d.mts +25 -3
  13. package/dist/compat/pi-ai.d.mts.map +1 -1
  14. package/dist/compat/pi-ai.mjs +3 -2919
  15. package/dist/compat/pi-coding-agent.d.mts +12 -1
  16. package/dist/compat/pi-coding-agent.d.mts.map +1 -1
  17. package/dist/compat/pi-coding-agent.mjs +2 -2
  18. package/dist/credentials-oauth.d.mts +27 -0
  19. package/dist/credentials-oauth.d.mts.map +1 -0
  20. package/dist/credentials-oauth.mjs +91 -0
  21. package/dist/credentials-oauth.mjs.map +1 -0
  22. package/dist/deferred-tools-BD4g5QUx.mjs +32 -0
  23. package/dist/deferred-tools-BD4g5QUx.mjs.map +1 -0
  24. package/dist/diagnostics-CJye1UFh.mjs +99 -0
  25. package/dist/diagnostics-CJye1UFh.mjs.map +1 -0
  26. package/dist/dist-IQKOgQyt.mjs +10035 -0
  27. package/dist/dist-IQKOgQyt.mjs.map +1 -0
  28. package/dist/dist-QBBA-dL_.mjs +1246 -0
  29. package/dist/dist-QBBA-dL_.mjs.map +1 -0
  30. package/dist/error-body-DKA3lknR.mjs +104 -0
  31. package/dist/error-body-DKA3lknR.mjs.map +1 -0
  32. package/dist/github-copilot-headers-CZo2QHrt.mjs +25 -0
  33. package/dist/github-copilot-headers-CZo2QHrt.mjs.map +1 -0
  34. package/dist/google-generative-ai-B7OZs5lu.mjs +358 -0
  35. package/dist/google-generative-ai-B7OZs5lu.mjs.map +1 -0
  36. package/dist/google-shared-CRsAUItF.mjs +29042 -0
  37. package/dist/google-shared-CRsAUItF.mjs.map +1 -0
  38. package/dist/google-vertex-DmdaHQtB.mjs +402 -0
  39. package/dist/google-vertex-DmdaHQtB.mjs.map +1 -0
  40. package/dist/hash-Dbggamac.mjs +19 -0
  41. package/dist/hash-Dbggamac.mjs.map +1 -0
  42. package/dist/headers-CPzZhJQn.mjs +17 -0
  43. package/dist/headers-CPzZhJQn.mjs.map +1 -0
  44. package/dist/host.d.mts.map +1 -1
  45. package/dist/host.mjs +15 -3
  46. package/dist/host.mjs.map +1 -1
  47. package/dist/images-models-DJtHo9Rp.mjs +1068 -0
  48. package/dist/images-models-DJtHo9Rp.mjs.map +1 -0
  49. package/dist/index.d.mts.map +1 -1
  50. package/dist/index.mjs +1 -1
  51. package/dist/json-parse-CFyFZSLJ.mjs +384 -0
  52. package/dist/json-parse-CFyFZSLJ.mjs.map +1 -0
  53. package/dist/{mcp-config-gqg7GJEE.mjs → mcp-config-D2B3kv-Z.mjs} +34 -16
  54. package/dist/mcp-config-D2B3kv-Z.mjs.map +1 -0
  55. package/dist/mistral-conversations-HHZCcFv_.mjs +42007 -0
  56. package/dist/mistral-conversations-HHZCcFv_.mjs.map +1 -0
  57. package/dist/multipart-parser-CIa5BbwO.mjs +301 -0
  58. package/dist/multipart-parser-CIa5BbwO.mjs.map +1 -0
  59. package/dist/oauth-bridge-C6fL1qSv.mjs +375 -0
  60. package/dist/oauth-bridge-C6fL1qSv.mjs.map +1 -0
  61. package/dist/openai-DkzRh5_G.mjs +6732 -0
  62. package/dist/openai-DkzRh5_G.mjs.map +1 -0
  63. package/dist/openai-codex-responses-vmovNu-q.mjs +1169 -0
  64. package/dist/openai-codex-responses-vmovNu-q.mjs.map +1 -0
  65. package/dist/openai-completions-fXHhRolF.mjs +947 -0
  66. package/dist/openai-completions-fXHhRolF.mjs.map +1 -0
  67. package/dist/openai-prompt-cache-BDWtZRKh.mjs +11 -0
  68. package/dist/openai-prompt-cache-BDWtZRKh.mjs.map +1 -0
  69. package/dist/openai-responses-DKdCYf3h.mjs +247 -0
  70. package/dist/openai-responses-DKdCYf3h.mjs.map +1 -0
  71. package/dist/openai-responses-shared-D0Jq7yVP.mjs +572 -0
  72. package/dist/openai-responses-shared-D0Jq7yVP.mjs.map +1 -0
  73. package/dist/openrouter-images-fG3Aj6M4.mjs +127 -0
  74. package/dist/openrouter-images-fG3Aj6M4.mjs.map +1 -0
  75. package/dist/otel-BQSQRBLF.mjs +24935 -0
  76. package/dist/otel-BQSQRBLF.mjs.map +1 -0
  77. package/dist/pi-ai-DhYGgq4Y.mjs +2984 -0
  78. package/dist/pi-ai-DhYGgq4Y.mjs.map +1 -0
  79. package/dist/{pi-coding-agent-Z1hTs61i.mjs → pi-coding-agent-fPHwKP4d.mjs} +27 -3
  80. package/dist/{pi-coding-agent-Z1hTs61i.mjs.map → pi-coding-agent-fPHwKP4d.mjs.map} +1 -1
  81. package/dist/pi-messages-CV-Ts8An.mjs +306 -0
  82. package/dist/pi-messages-CV-Ts8An.mjs.map +1 -0
  83. package/dist/provider-env-C72pF4vP.mjs +40 -0
  84. package/dist/provider-env-C72pF4vP.mjs.map +1 -0
  85. package/dist/provider-retry-Cw6YJ4cr.mjs +80 -0
  86. package/dist/provider-retry-Cw6YJ4cr.mjs.map +1 -0
  87. package/dist/rolldown-runtime-CTfmNlz1.mjs +44 -0
  88. package/dist/{runtime-oLd2EInK.mjs → runtime-B9R7bDWT.mjs} +838 -345
  89. package/dist/runtime-B9R7bDWT.mjs.map +1 -0
  90. package/dist/runtime.d.mts.map +1 -1
  91. package/dist/runtime.mjs +1 -1
  92. package/dist/sanitize-unicode-wLA11KvZ.mjs +29 -0
  93. package/dist/sanitize-unicode-wLA11KvZ.mjs.map +1 -0
  94. package/dist/src-C3WBfOks.mjs +5031 -0
  95. package/dist/src-C3WBfOks.mjs.map +1 -0
  96. package/dist/transform-messages-DGq9mvhA.mjs +379 -0
  97. package/dist/transform-messages-DGq9mvhA.mjs.map +1 -0
  98. package/dist/uuid-DtFT7tOE.mjs +65 -0
  99. package/dist/uuid-DtFT7tOE.mjs.map +1 -0
  100. package/package.json +11 -1
  101. package/dist/compat/pi-ai.mjs.map +0 -1
  102. package/dist/mcp-config-gqg7GJEE.mjs.map +0 -1
  103. package/dist/rolldown-runtime-D-uZhY3_.mjs +0 -16
  104. package/dist/runtime-oLd2EInK.mjs.map +0 -1
@@ -0,0 +1,947 @@
1
+
2
+ import { a as AssistantMessageEventStream } from "./diagnostics-CJye1UFh.mjs";
3
+ import { a as calculateCost, o as clampThinkingLevel } from "./images-models-DJtHo9Rp.mjs";
4
+ import { t as headersToRecord } from "./headers-CPzZhJQn.mjs";
5
+ import { n as parseStreamingJson } from "./json-parse-CFyFZSLJ.mjs";
6
+ import { t as getProviderEnvValue } from "./provider-env-C72pF4vP.mjs";
7
+ import { t as retryProviderRequest } from "./provider-retry-Cw6YJ4cr.mjs";
8
+ import { t as sanitizeSurrogates } from "./sanitize-unicode-wLA11KvZ.mjs";
9
+ import { c as createGrammarToolInputProperties, d as resolveJsonSchemaStrictSampling, i as buildBaseOptions, l as getGrammarToolInput, n as MIN_ANSWER_TOKENS, o as clampReasoning, s as appendGrammarToolInputJsonDelta, t as transformMessages, u as resolveGrammarConstrainedSampling } from "./transform-messages-DGq9mvhA.mjs";
10
+ import { n as hasCopilotVisionInput, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-CZo2QHrt.mjs";
11
+ import { n as OpenAI } from "./openai-DkzRh5_G.mjs";
12
+ import { n as normalizeProviderError, t as formatProviderError } from "./error-body-DKA3lknR.mjs";
13
+ import { t as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-BDWtZRKh.mjs";
14
+ import { t as shortHash } from "./hash-Dbggamac.mjs";
15
+ //#region node_modules/.pnpm/@earendil-works+pi-ai@0.84.1_ws@8.21.3_zod@4.4.3/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js
16
+ /**
17
+ * Check if conversation messages contain tool calls or tool results.
18
+ * This is needed because Anthropic (via proxy) requires the tools param
19
+ * to be present when messages include tool_calls or tool role messages.
20
+ */
21
+ function hasHeader(headers, name) {
22
+ if (!headers) return false;
23
+ const expected = name.toLowerCase();
24
+ for (const [key, value] of Object.entries(headers)) if (key.toLowerCase() === expected && value !== null && value.trim().length > 0) return true;
25
+ return false;
26
+ }
27
+ function getClientApiKey(provider, apiKey, headers) {
28
+ if (apiKey) return apiKey;
29
+ if (hasHeader(headers, "authorization") || hasHeader(headers, "cf-aig-authorization")) return "unused";
30
+ throw new Error(`No API key for provider: ${provider}`);
31
+ }
32
+ function hasToolHistory(messages) {
33
+ for (const msg of messages) {
34
+ if (msg.role === "toolResult") return true;
35
+ if (msg.role === "assistant") {
36
+ if (msg.content.some((block) => block.type === "toolCall")) return true;
37
+ }
38
+ }
39
+ return false;
40
+ }
41
+ function getDeferredToolNames(messages) {
42
+ const names = /* @__PURE__ */ new Set();
43
+ for (const message of messages) if (message.role === "toolResult") for (const name of message.addedToolNames ?? []) names.add(name);
44
+ return names;
45
+ }
46
+ function getToolsByName(tools, names) {
47
+ if (!tools) return [];
48
+ const toolsByName = new Map(tools.map((tool) => [tool.name, tool]));
49
+ return Array.from(names).map((name) => toolsByName.get(name)).filter((tool) => tool !== void 0);
50
+ }
51
+ function isTextContentBlock(block) {
52
+ return block.type === "text";
53
+ }
54
+ function isThinkingContentBlock(block) {
55
+ return block.type === "thinking";
56
+ }
57
+ function isToolCallBlock(block) {
58
+ return block.type === "toolCall";
59
+ }
60
+ function isImageContentBlock(block) {
61
+ return block.type === "image";
62
+ }
63
+ function isEncryptedReasoningDetail(detail) {
64
+ if (typeof detail !== "object" || detail === null) return false;
65
+ const candidate = detail;
66
+ return candidate.type === "reasoning.encrypted" && typeof candidate.id === "string" && candidate.id.length > 0 && typeof candidate.data === "string" && candidate.data.length > 0;
67
+ }
68
+ function resolveCacheRetention(cacheRetention, env) {
69
+ if (cacheRetention) return cacheRetention;
70
+ if (getProviderEnvValue("PI_CACHE_RETENTION", env) === "long") return "long";
71
+ return "short";
72
+ }
73
+ const stream = (model, context, options) => {
74
+ const stream = new AssistantMessageEventStream();
75
+ (async () => {
76
+ const output = {
77
+ role: "assistant",
78
+ content: [],
79
+ api: model.api,
80
+ provider: model.provider,
81
+ model: model.id,
82
+ usage: {
83
+ input: 0,
84
+ output: 0,
85
+ cacheRead: 0,
86
+ cacheWrite: 0,
87
+ totalTokens: 0,
88
+ cost: {
89
+ input: 0,
90
+ output: 0,
91
+ cacheRead: 0,
92
+ cacheWrite: 0,
93
+ total: 0
94
+ }
95
+ },
96
+ stopReason: "pending",
97
+ timestamp: Date.now()
98
+ };
99
+ try {
100
+ const apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);
101
+ const compat = getCompat(model);
102
+ const grammarToolInputProperties = createGrammarToolInputProperties(context.tools, compat.supportsOpenAIGrammarTools);
103
+ const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
104
+ const cacheSessionId = cacheRetention === "none" ? void 0 : options?.sessionId;
105
+ const client = createClient(model, context, apiKey, options?.headers, options?.fetch, cacheSessionId, compat);
106
+ let params = buildParams(model, context, options, compat, cacheRetention, grammarToolInputProperties);
107
+ const nextParams = await options?.onPayload?.(params, model);
108
+ if (nextParams !== void 0) params = nextParams;
109
+ const requestOptions = {
110
+ ...options?.signal ? { signal: options.signal } : {},
111
+ ...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
112
+ maxRetries: 0
113
+ };
114
+ const { data: openaiStream, response } = await retryProviderRequest(() => client.chat.completions.create(params, requestOptions).withResponse(), {
115
+ maxRetries: options?.maxRetries,
116
+ maxRetryDelayMs: options?.maxRetryDelayMs,
117
+ signal: options?.signal
118
+ });
119
+ await options?.onResponse?.({
120
+ status: response.status,
121
+ headers: headersToRecord(response.headers)
122
+ }, model);
123
+ stream.push({
124
+ type: "start",
125
+ partial: output
126
+ });
127
+ let textBlock = null;
128
+ let thinkingBlock = null;
129
+ let hasFinishReason = false;
130
+ const toolCallBlocksByIndex = /* @__PURE__ */ new Map();
131
+ const toolCallBlocksById = /* @__PURE__ */ new Map();
132
+ const pendingReasoningDetailsByToolCallId = /* @__PURE__ */ new Map();
133
+ const blocks = output.content;
134
+ const getContentIndex = (block) => blocks.indexOf(block);
135
+ const getCustomToolCallInput = (block) => {
136
+ const property = block.customInput?.property;
137
+ if (property === void 0) return "";
138
+ const value = block.arguments[property];
139
+ return typeof value === "string" ? value : "";
140
+ };
141
+ const appendCustomToolCallInput = (block, nextInput, close) => {
142
+ const customInput = block.customInput;
143
+ if (!customInput) return void 0;
144
+ const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
145
+ block.arguments = { [customInput.property]: nextInput };
146
+ return delta;
147
+ };
148
+ const finishBlock = (block) => {
149
+ const contentIndex = getContentIndex(block);
150
+ if (contentIndex === -1) return;
151
+ if (block.type === "text") stream.push({
152
+ type: "text_end",
153
+ contentIndex,
154
+ content: block.text,
155
+ partial: output
156
+ });
157
+ else if (block.type === "thinking") stream.push({
158
+ type: "thinking_end",
159
+ contentIndex,
160
+ content: block.thinking,
161
+ partial: output
162
+ });
163
+ else if (block.type === "toolCall") {
164
+ if (block.customInput) {
165
+ const delta = appendCustomToolCallInput(block, getCustomToolCallInput(block), true);
166
+ if (delta !== void 0) stream.push({
167
+ type: "toolcall_delta",
168
+ contentIndex,
169
+ delta,
170
+ partial: output
171
+ });
172
+ } else block.arguments = parseStreamingJson(block.partialArgs);
173
+ delete block.partialArgs;
174
+ delete block.customInput;
175
+ delete block.streamIndex;
176
+ stream.push({
177
+ type: "toolcall_end",
178
+ contentIndex,
179
+ toolCall: block,
180
+ partial: output
181
+ });
182
+ }
183
+ };
184
+ const ensureTextBlock = () => {
185
+ if (!textBlock) {
186
+ textBlock = {
187
+ type: "text",
188
+ text: ""
189
+ };
190
+ blocks.push(textBlock);
191
+ stream.push({
192
+ type: "text_start",
193
+ contentIndex: getContentIndex(textBlock),
194
+ partial: output
195
+ });
196
+ }
197
+ return textBlock;
198
+ };
199
+ const ensureThinkingBlock = (thinkingSignature) => {
200
+ if (!thinkingBlock) {
201
+ thinkingBlock = {
202
+ type: "thinking",
203
+ thinking: "",
204
+ thinkingSignature
205
+ };
206
+ blocks.push(thinkingBlock);
207
+ stream.push({
208
+ type: "thinking_start",
209
+ contentIndex: getContentIndex(thinkingBlock),
210
+ partial: output
211
+ });
212
+ }
213
+ return thinkingBlock;
214
+ };
215
+ const applyPendingReasoningDetail = (block) => {
216
+ if (!block.id) return;
217
+ const pendingReasoningDetail = pendingReasoningDetailsByToolCallId.get(block.id);
218
+ if (pendingReasoningDetail) {
219
+ block.thoughtSignature = pendingReasoningDetail;
220
+ pendingReasoningDetailsByToolCallId.delete(block.id);
221
+ }
222
+ };
223
+ const ensureToolCallBlock = (toolCall) => {
224
+ const streamIndex = typeof toolCall.index === "number" ? toolCall.index : void 0;
225
+ const name = toolCall.function?.name ?? toolCall.custom?.name ?? "";
226
+ let block = streamIndex !== void 0 ? toolCallBlocksByIndex.get(streamIndex) : void 0;
227
+ if (!block && toolCall.id) block = toolCallBlocksById.get(toolCall.id);
228
+ if (!block) {
229
+ const customInputProperty = toolCall.custom && !toolCall.function ? grammarToolInputProperties.get(name) ?? "input" : void 0;
230
+ const hasCustomInput = customInputProperty !== void 0;
231
+ block = {
232
+ type: "toolCall",
233
+ id: toolCall.id || "",
234
+ name,
235
+ arguments: hasCustomInput ? { [customInputProperty]: "" } : {},
236
+ partialArgs: hasCustomInput ? void 0 : "",
237
+ customInput: hasCustomInput ? {
238
+ property: customInputProperty,
239
+ jsonBuffer: {
240
+ input: "",
241
+ started: false,
242
+ closed: false
243
+ }
244
+ } : void 0,
245
+ streamIndex
246
+ };
247
+ if (streamIndex !== void 0) toolCallBlocksByIndex.set(streamIndex, block);
248
+ if (toolCall.id) toolCallBlocksById.set(toolCall.id, block);
249
+ blocks.push(block);
250
+ stream.push({
251
+ type: "toolcall_start",
252
+ contentIndex: getContentIndex(block),
253
+ partial: output
254
+ });
255
+ }
256
+ if (streamIndex !== void 0 && block.streamIndex === void 0) {
257
+ block.streamIndex = streamIndex;
258
+ toolCallBlocksByIndex.set(streamIndex, block);
259
+ }
260
+ if (toolCall.id) toolCallBlocksById.set(toolCall.id, block);
261
+ if (!block.name && name) block.name = name;
262
+ if (toolCall.custom && !toolCall.function && !block.customInput) {
263
+ const customInputProperty = grammarToolInputProperties.get(block.name) ?? "input";
264
+ block.arguments = { [customInputProperty]: "" };
265
+ block.customInput = {
266
+ property: customInputProperty,
267
+ jsonBuffer: {
268
+ input: "",
269
+ started: false,
270
+ closed: false
271
+ }
272
+ };
273
+ delete block.partialArgs;
274
+ }
275
+ applyPendingReasoningDetail(block);
276
+ return block;
277
+ };
278
+ for await (const chunk of openaiStream) {
279
+ if (!chunk || typeof chunk !== "object") continue;
280
+ output.responseId ||= chunk.id;
281
+ if (typeof chunk.model === "string" && chunk.model.length > 0 && chunk.model !== model.id) output.responseModel ||= chunk.model;
282
+ if (chunk.usage) output.usage = parseChunkUsage(chunk.usage, model);
283
+ const choice = Array.isArray(chunk.choices) ? chunk.choices[0] : void 0;
284
+ if (!choice) continue;
285
+ if (!chunk.usage && choice.usage) output.usage = parseChunkUsage(choice.usage, model);
286
+ if (choice.finish_reason) {
287
+ output.rawStopReason = choice.finish_reason;
288
+ const finishReasonResult = mapStopReason(choice.finish_reason);
289
+ output.stopReason = finishReasonResult.stopReason;
290
+ if (finishReasonResult.errorMessage) output.errorMessage = finishReasonResult.errorMessage;
291
+ hasFinishReason = true;
292
+ }
293
+ if (choice.delta) {
294
+ if (choice.delta.content !== null && choice.delta.content !== void 0 && choice.delta.content.length > 0) {
295
+ const block = ensureTextBlock();
296
+ block.text += choice.delta.content;
297
+ stream.push({
298
+ type: "text_delta",
299
+ contentIndex: getContentIndex(block),
300
+ delta: choice.delta.content,
301
+ partial: output
302
+ });
303
+ }
304
+ const reasoningFields = [
305
+ "reasoning_content",
306
+ "reasoning",
307
+ "reasoning_text"
308
+ ];
309
+ const deltaFields = choice.delta;
310
+ let foundReasoningField = null;
311
+ for (const field of reasoningFields) {
312
+ const value = deltaFields[field];
313
+ if (typeof value === "string" && value.length > 0) {
314
+ foundReasoningField = field;
315
+ break;
316
+ }
317
+ }
318
+ if (foundReasoningField) {
319
+ const delta = deltaFields[foundReasoningField];
320
+ if (typeof delta === "string" && delta.length > 0) {
321
+ const block = ensureThinkingBlock(model.provider === "opencode-go" && foundReasoningField === "reasoning" ? "reasoning_content" : foundReasoningField);
322
+ block.thinking += delta;
323
+ stream.push({
324
+ type: "thinking_delta",
325
+ contentIndex: getContentIndex(block),
326
+ delta,
327
+ partial: output
328
+ });
329
+ }
330
+ }
331
+ if (choice?.delta?.tool_calls) for (const toolCall of choice.delta.tool_calls) {
332
+ const block = ensureToolCallBlock(toolCall);
333
+ if (!block.id && toolCall.id) {
334
+ block.id = toolCall.id;
335
+ toolCallBlocksById.set(toolCall.id, block);
336
+ }
337
+ const name = toolCall.function?.name ?? toolCall.custom?.name;
338
+ if (!block.name && name) block.name = name;
339
+ let delta = "";
340
+ if (toolCall.function?.arguments) {
341
+ delta = toolCall.function.arguments;
342
+ block.partialArgs = (block.partialArgs ?? "") + toolCall.function.arguments;
343
+ block.arguments = parseStreamingJson(block.partialArgs);
344
+ } else if (toolCall.custom?.input) delta = appendCustomToolCallInput(block, getCustomToolCallInput(block) + toolCall.custom.input, false) ?? "";
345
+ stream.push({
346
+ type: "toolcall_delta",
347
+ contentIndex: getContentIndex(block),
348
+ delta,
349
+ partial: output
350
+ });
351
+ }
352
+ const reasoningDetails = choice.delta.reasoning_details;
353
+ if (Array.isArray(reasoningDetails)) {
354
+ for (const detail of reasoningDetails) if (isEncryptedReasoningDetail(detail)) {
355
+ const serializedDetail = JSON.stringify(detail);
356
+ const matchingToolCall = toolCallBlocksById.get(detail.id);
357
+ if (matchingToolCall) matchingToolCall.thoughtSignature = serializedDetail;
358
+ else pendingReasoningDetailsByToolCallId.set(detail.id, serializedDetail);
359
+ }
360
+ }
361
+ }
362
+ }
363
+ for (const block of blocks) finishBlock(block);
364
+ if (options?.signal?.aborted) throw new Error("Request was aborted");
365
+ if (output.stopReason === "aborted") throw new Error("Request was aborted");
366
+ if (!hasFinishReason && !compat.supportsFinishReason) output.stopReason = output.content.some((block) => block.type === "toolCall") ? "toolUse" : "stop";
367
+ if (output.stopReason === "error") throw new Error(output.errorMessage || "Provider returned an error stop reason");
368
+ if (compat.supportsFinishReason && !hasFinishReason || output.stopReason === "pending") throw new Error("Stream ended without finish_reason");
369
+ stream.push({
370
+ type: "done",
371
+ reason: output.stopReason,
372
+ message: output
373
+ });
374
+ stream.end();
375
+ } catch (error) {
376
+ for (const block of output.content) {
377
+ delete block.index;
378
+ delete block.partialArgs;
379
+ delete block.customInput;
380
+ delete block.streamIndex;
381
+ }
382
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
383
+ output.errorMessage = formatProviderError(normalizeProviderError(error));
384
+ const rawMetadata = error?.error?.metadata?.raw;
385
+ if (rawMetadata && !output.errorMessage.includes(String(rawMetadata))) output.errorMessage += `\n${rawMetadata}`;
386
+ stream.push({
387
+ type: "error",
388
+ reason: output.stopReason,
389
+ error: output
390
+ });
391
+ stream.end();
392
+ }
393
+ })();
394
+ return stream;
395
+ };
396
+ const streamSimple = (model, context, options) => {
397
+ getClientApiKey(model.provider, options?.apiKey, options?.headers);
398
+ const base = buildBaseOptions(model, context, options, options?.apiKey);
399
+ const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
400
+ const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning;
401
+ const toolChoice = options?.toolChoice;
402
+ return stream(model, context, {
403
+ ...base,
404
+ reasoningEffort,
405
+ toolChoice,
406
+ thinkingBudgets: options?.thinkingBudgets
407
+ });
408
+ };
409
+ function createClient(model, context, apiKey, optionsHeaders, fetch, sessionId, compat = getCompat(model)) {
410
+ const headers = { ...model.headers };
411
+ if (model.provider === "github-copilot") {
412
+ const hasImages = hasCopilotVisionInput(context.messages);
413
+ const copilotHeaders = buildCopilotDynamicHeaders({
414
+ messages: context.messages,
415
+ hasImages
416
+ });
417
+ Object.assign(headers, copilotHeaders);
418
+ }
419
+ if (sessionId && compat.sendSessionAffinityHeaders) {
420
+ if (compat.sessionAffinityFormat === "openrouter") headers["x-session-id"] = sessionId;
421
+ else {
422
+ if (compat.sessionAffinityFormat === "openai") headers.session_id = sessionId;
423
+ headers["x-client-request-id"] = sessionId;
424
+ headers["x-session-affinity"] = sessionId;
425
+ }
426
+ }
427
+ if (optionsHeaders) Object.assign(headers, optionsHeaders);
428
+ return new OpenAI({
429
+ apiKey,
430
+ baseURL: model.baseUrl,
431
+ dangerouslyAllowBrowser: true,
432
+ fetch,
433
+ defaultHeaders: headers
434
+ });
435
+ }
436
+ function buildParams(model, context, options, compat = getCompat(model), cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env), grammarToolInputProperties = createGrammarToolInputProperties(context.tools, compat.supportsOpenAIGrammarTools)) {
437
+ const messages = convertMessages(model, context, compat, { grammarToolInputProperties });
438
+ const cacheControl = getCompatCacheControl(compat, cacheRetention);
439
+ const params = {
440
+ model: model.id,
441
+ messages,
442
+ stream: true,
443
+ prompt_cache_key: model.baseUrl.includes("api.openai.com") && cacheRetention !== "none" || cacheRetention === "long" && compat.supportsLongCacheRetention ? clampOpenAIPromptCacheKey(options?.sessionId) : void 0,
444
+ prompt_cache_retention: cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : void 0
445
+ };
446
+ if (compat.supportsUsageInStreaming !== false) params.stream_options = { include_usage: true };
447
+ if (compat.supportsStore) params.store = false;
448
+ if (options?.maxTokens) {
449
+ if (compat.maxTokensField === "max_tokens") params.max_tokens = options.maxTokens;
450
+ else params.max_completion_tokens = options.maxTokens;
451
+ }
452
+ if (options?.temperature !== void 0) params.temperature = options.temperature;
453
+ const deferredToolNames = compat.deferredToolsMode === "kimi" ? getDeferredToolNames(context.messages) : /* @__PURE__ */ new Set();
454
+ const activeTools = context.tools?.filter((tool) => !deferredToolNames.has(tool.name));
455
+ if (activeTools && activeTools.length > 0) {
456
+ params.tools = convertTools(activeTools, compat);
457
+ if (compat.zaiToolStream) params.tool_stream = true;
458
+ } else if (hasToolHistory(context.messages)) params.tools = [];
459
+ if (cacheControl) applyAnthropicCacheControl(messages, params.tools, cacheControl);
460
+ if (options?.toolChoice) params.tool_choice = options.toolChoice;
461
+ if (compat.thinkingFormat === "zai" && model.reasoning) {
462
+ const zaiParams = params;
463
+ zaiParams.thinking = options?.reasoningEffort ? {
464
+ type: "enabled",
465
+ clear_thinking: false
466
+ } : { type: "disabled" };
467
+ if (options?.reasoningEffort && compat.supportsReasoningEffort) {
468
+ const mappedEffort = model.thinkingLevelMap?.[options.reasoningEffort];
469
+ const effort = mappedEffort === void 0 ? options.reasoningEffort : mappedEffort;
470
+ if (typeof effort === "string") zaiParams.reasoning_effort = effort;
471
+ }
472
+ } else if (compat.thinkingFormat === "qwen" && model.reasoning) {
473
+ params.enable_thinking = !!options?.reasoningEffort;
474
+ if (options?.reasoningEffort && compat.supportsReasoningEffort) {
475
+ const effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
476
+ if (typeof effort === "string") params.reasoning_effort = effort;
477
+ }
478
+ } else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) params.chat_template_kwargs = {
479
+ enable_thinking: !!options?.reasoningEffort,
480
+ preserve_thinking: true
481
+ };
482
+ else if (compat.thinkingFormat === "chat-template" && model.reasoning) {
483
+ const chatTemplateKwargs = buildChatTemplateValues(model, options, compat.chatTemplateKwargs);
484
+ if (chatTemplateKwargs) params.chat_template_kwargs = chatTemplateKwargs;
485
+ } else if (compat.thinkingFormat === "baseten" && model.reasoning) {
486
+ const basetenParams = params;
487
+ const chatTemplateArgs = buildChatTemplateValues(model, options, compat.chatTemplateArgs);
488
+ if (chatTemplateArgs) basetenParams.chat_template_args = chatTemplateArgs;
489
+ if (compat.supportsReasoningEffort) {
490
+ const requestedEffort = options?.reasoningEffort;
491
+ const mappedEffort = requestedEffort ? model.thinkingLevelMap?.[requestedEffort] : model.thinkingLevelMap?.off;
492
+ const effort = mappedEffort === void 0 ? requestedEffort : mappedEffort;
493
+ if (typeof effort === "string") basetenParams.reasoning_effort = effort;
494
+ }
495
+ } else if (compat.thinkingFormat === "deepseek" && model.reasoning) {
496
+ if (options?.reasoningEffort) params.thinking = { type: "enabled" };
497
+ else if (model.thinkingLevelMap?.off !== null) params.thinking = { type: "disabled" };
498
+ if (options?.reasoningEffort && compat.supportsReasoningEffort) params.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
499
+ } else if (compat.thinkingFormat === "openrouter" && model.reasoning) {
500
+ const openRouterParams = params;
501
+ if (options?.reasoningEffort) openRouterParams.reasoning = { effort: model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort };
502
+ else if (model.thinkingLevelMap?.off !== null) openRouterParams.reasoning = { effort: model.thinkingLevelMap?.off ?? "none" };
503
+ } else if (compat.thinkingFormat === "ant-ling" && model.reasoning && options?.reasoningEffort) {
504
+ const effort = model.thinkingLevelMap?.[options.reasoningEffort];
505
+ if (typeof effort === "string") params.reasoning = { effort };
506
+ } else if (compat.thinkingFormat === "together" && model.reasoning) {
507
+ const togetherParams = params;
508
+ togetherParams.reasoning = { enabled: !!options?.reasoningEffort };
509
+ if (options?.reasoningEffort && compat.supportsReasoningEffort) togetherParams.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
510
+ } else if (compat.thinkingFormat === "string-thinking" && model.reasoning) {
511
+ const stringThinkingParams = params;
512
+ if (options?.reasoningEffort) stringThinkingParams.thinking = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
513
+ else if (model.thinkingLevelMap?.off !== null) stringThinkingParams.thinking = model.thinkingLevelMap?.off ?? "none";
514
+ } else if (options?.reasoningEffort && model.reasoning && compat.supportsReasoningEffort) params.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
515
+ else if (!options?.reasoningEffort && model.reasoning && compat.supportsReasoningEffort) {
516
+ const offValue = model.thinkingLevelMap?.off;
517
+ if (typeof offValue === "string") params.reasoning_effort = offValue;
518
+ }
519
+ if (compat.supportsThinkingTokenBudget && options?.reasoningEffort && model.reasoning) {
520
+ const level = clampReasoning(options.reasoningEffort);
521
+ const budgets = {
522
+ minimal: 1024,
523
+ low: 2048,
524
+ medium: 8192,
525
+ high: 16384,
526
+ ...options.thinkingBudgets
527
+ };
528
+ const ceiling = params.max_tokens ?? params.max_completion_tokens ?? model.maxTokens;
529
+ const budget = Math.min(budgets[level], Math.max(0, ceiling - MIN_ANSWER_TOKENS));
530
+ if (budget > 0) params.thinking_token_budget = budget;
531
+ }
532
+ if (model.compat?.openRouterRouting) params.provider = model.compat.openRouterRouting;
533
+ if (model.compat?.vercelGatewayRouting) {
534
+ const routing = model.compat.vercelGatewayRouting;
535
+ if (routing.only || routing.order) {
536
+ const gatewayOptions = {};
537
+ if (routing.only) gatewayOptions.only = routing.only;
538
+ if (routing.order) gatewayOptions.order = routing.order;
539
+ params.providerOptions = { gateway: gatewayOptions };
540
+ }
541
+ }
542
+ if (options?.samplingParams) Object.assign(params, options.samplingParams);
543
+ return params;
544
+ }
545
+ function buildChatTemplateValues(model, options, values) {
546
+ const resolvedValues = {};
547
+ for (const [key, value] of Object.entries(values)) {
548
+ const resolved = resolveChatTemplateKwargValue(model, options, value);
549
+ if (resolved !== void 0) resolvedValues[key] = resolved;
550
+ }
551
+ return Object.keys(resolvedValues).length > 0 ? resolvedValues : void 0;
552
+ }
553
+ function resolveChatTemplateKwargValue(model, options, value) {
554
+ if (typeof value !== "object" || value === null) return value;
555
+ const reasoningEffort = options?.reasoningEffort;
556
+ if (!reasoningEffort && value.omitWhenOff) return;
557
+ if (value.$var === "thinking.enabled") return !!reasoningEffort;
558
+ const mappedValue = reasoningEffort ? model.thinkingLevelMap?.[reasoningEffort] : model.thinkingLevelMap?.off;
559
+ return mappedValue === void 0 ? reasoningEffort : typeof mappedValue === "string" ? mappedValue : void 0;
560
+ }
561
+ function getCompatCacheControl(compat, cacheRetention) {
562
+ if (compat.cacheControlFormat !== "anthropic" || cacheRetention === "none") return;
563
+ const ttl = cacheRetention === "long" && compat.supportsLongCacheRetention ? "1h" : void 0;
564
+ return {
565
+ type: "ephemeral",
566
+ ...ttl ? { ttl } : {}
567
+ };
568
+ }
569
+ function applyAnthropicCacheControl(messages, tools, cacheControl) {
570
+ addCacheControlToSystemPrompt(messages, cacheControl);
571
+ addCacheControlToLastTool(tools, cacheControl);
572
+ addCacheControlToLastConversationMessage(messages, cacheControl);
573
+ }
574
+ function addCacheControlToSystemPrompt(messages, cacheControl) {
575
+ for (const message of messages) if (message.role === "system" || message.role === "developer") {
576
+ addCacheControlToInstructionMessage(message, cacheControl);
577
+ return;
578
+ }
579
+ }
580
+ function addCacheControlToLastConversationMessage(messages, cacheControl) {
581
+ for (let i = messages.length - 1; i >= 0; i--) {
582
+ const message = messages[i];
583
+ if (message.role === "user" || message.role === "assistant" || message.role === "tool") {
584
+ if (addCacheControlToMessage(message, cacheControl)) return;
585
+ }
586
+ }
587
+ }
588
+ function addCacheControlToLastTool(tools, cacheControl) {
589
+ if (!tools || tools.length === 0) return;
590
+ const lastTool = tools[tools.length - 1];
591
+ lastTool.cache_control = cacheControl;
592
+ }
593
+ function addCacheControlToInstructionMessage(message, cacheControl) {
594
+ return addCacheControlToTextContent(message, cacheControl);
595
+ }
596
+ function addCacheControlToMessage(message, cacheControl) {
597
+ if (message.role === "user" || message.role === "assistant" || message.role === "tool") return addCacheControlToTextContent(message, cacheControl);
598
+ return false;
599
+ }
600
+ function addCacheControlToTextContent(message, cacheControl) {
601
+ const content = message.content;
602
+ if (typeof content === "string") {
603
+ if (content.length === 0) return false;
604
+ message.content = [{
605
+ type: "text",
606
+ text: content,
607
+ cache_control: cacheControl
608
+ }];
609
+ return true;
610
+ }
611
+ if (!Array.isArray(content)) return false;
612
+ for (let i = content.length - 1; i >= 0; i--) {
613
+ const part = content[i];
614
+ if (part?.type === "text") {
615
+ const textPart = part;
616
+ textPart.cache_control = cacheControl;
617
+ return true;
618
+ }
619
+ }
620
+ return false;
621
+ }
622
+ function convertMessages(model, context, compat, options) {
623
+ const params = [];
624
+ const normalizeToolCallId = (id) => {
625
+ if (id.includes("|")) {
626
+ const separatorIndex = id.indexOf("|");
627
+ const callId = id.slice(0, separatorIndex).replace(/[^a-zA-Z0-9_-]/g, "_");
628
+ const itemId = id.slice(separatorIndex + 1).replace(/[^a-zA-Z0-9_-]/g, "_");
629
+ const combinedId = itemId.length > 0 ? `${callId}_${itemId}` : callId;
630
+ if (combinedId.length <= 40) return combinedId;
631
+ const hash = shortHash(id).slice(0, 8);
632
+ return `${callId.slice(0, Math.max(1, 40 - hash.length - 1))}_${hash}`;
633
+ }
634
+ if (model.provider === "openai") return id.length > 40 ? id.slice(0, 40) : id;
635
+ return id;
636
+ };
637
+ const transformedMessages = transformMessages(context.messages, model, (id) => normalizeToolCallId(id));
638
+ if (context.systemPrompt) {
639
+ const role = model.reasoning && compat.supportsDeveloperRole ? "developer" : "system";
640
+ params.push({
641
+ role,
642
+ content: sanitizeSurrogates(context.systemPrompt)
643
+ });
644
+ }
645
+ let lastRole = null;
646
+ for (let i = 0; i < transformedMessages.length; i++) {
647
+ const msg = transformedMessages[i];
648
+ if (compat.requiresAssistantAfterToolResult && lastRole === "toolResult" && msg.role === "user") params.push({
649
+ role: "assistant",
650
+ content: "I have processed the tool results."
651
+ });
652
+ if (msg.role === "user") {
653
+ if (typeof msg.content === "string") params.push({
654
+ role: "user",
655
+ content: sanitizeSurrogates(msg.content)
656
+ });
657
+ else {
658
+ const content = msg.content.map((item) => {
659
+ if (item.type === "text") return {
660
+ type: "text",
661
+ text: sanitizeSurrogates(item.text)
662
+ };
663
+ else return {
664
+ type: "image_url",
665
+ image_url: { url: `data:${item.mimeType};base64,${item.data}` }
666
+ };
667
+ });
668
+ if (content.length === 0) continue;
669
+ params.push({
670
+ role: "user",
671
+ content
672
+ });
673
+ }
674
+ } else if (msg.role === "assistant") {
675
+ const assistantMsg = {
676
+ role: "assistant",
677
+ content: compat.requiresAssistantAfterToolResult ? "" : null
678
+ };
679
+ const assistantTextParts = msg.content.filter(isTextContentBlock).filter((block) => block.text.trim().length > 0).map((block) => ({
680
+ type: "text",
681
+ text: sanitizeSurrogates(block.text)
682
+ }));
683
+ const assistantText = assistantTextParts.map((part) => part.text).join("");
684
+ const nonEmptyThinkingBlocks = msg.content.filter(isThinkingContentBlock).filter((block) => block.thinking.trim().length > 0);
685
+ if (nonEmptyThinkingBlocks.length > 0) {
686
+ if (compat.requiresThinkingAsText) assistantMsg.content = [{
687
+ type: "text",
688
+ text: nonEmptyThinkingBlocks.map((block) => sanitizeSurrogates(block.thinking)).join("\n\n")
689
+ }, ...assistantTextParts];
690
+ else {
691
+ if (assistantText.length > 0) assistantMsg.content = assistantText;
692
+ let signature = nonEmptyThinkingBlocks[0].thinkingSignature;
693
+ if (model.provider === "opencode-go" && signature === "reasoning") signature = "reasoning_content";
694
+ if (signature && signature.length > 0) assistantMsg[signature] = nonEmptyThinkingBlocks.map((block) => block.thinking).join("\n");
695
+ }
696
+ } else if (assistantText.length > 0) assistantMsg.content = assistantText;
697
+ const toolCalls = msg.content.filter(isToolCallBlock);
698
+ if (toolCalls.length > 0) {
699
+ assistantMsg.tool_calls = toolCalls.map((tc) => {
700
+ const customInputProperty = options?.grammarToolInputProperties?.get(tc.name);
701
+ if (customInputProperty !== void 0) return {
702
+ id: tc.id,
703
+ type: "custom",
704
+ custom: {
705
+ name: tc.name,
706
+ input: sanitizeSurrogates(getGrammarToolInput(tc.name, tc.arguments, customInputProperty))
707
+ }
708
+ };
709
+ return {
710
+ id: tc.id,
711
+ type: "function",
712
+ function: {
713
+ name: tc.name,
714
+ arguments: JSON.stringify(tc.arguments)
715
+ }
716
+ };
717
+ });
718
+ const reasoningDetails = toolCalls.filter((tc) => tc.thoughtSignature).map((tc) => {
719
+ try {
720
+ return JSON.parse(tc.thoughtSignature);
721
+ } catch {
722
+ return null;
723
+ }
724
+ }).filter(Boolean);
725
+ if (reasoningDetails.length > 0) assistantMsg.reasoning_details = reasoningDetails;
726
+ }
727
+ if (compat.requiresReasoningContentOnAssistantMessages && model.reasoning && assistantMsg.reasoning_content === void 0) assistantMsg.reasoning_content = "";
728
+ const content = assistantMsg.content;
729
+ if (!(content !== null && content !== void 0 && (typeof content === "string" ? content.length > 0 : content.length > 0)) && !assistantMsg.tool_calls) continue;
730
+ params.push(assistantMsg);
731
+ } else if (msg.role === "toolResult") {
732
+ const imageBlocks = [];
733
+ const deferredToolNames = /* @__PURE__ */ new Set();
734
+ let j = i;
735
+ for (; j < transformedMessages.length && transformedMessages[j].role === "toolResult"; j++) {
736
+ const toolMsg = transformedMessages[j];
737
+ const textResult = toolMsg.content.filter(isTextContentBlock).map((block) => block.text).join("\n");
738
+ const hasImages = toolMsg.content.some((c) => c.type === "image");
739
+ const toolResultText = textResult.length > 0 ? textResult : hasImages ? "(see attached image)" : "(no tool output)";
740
+ const toolResultMsg = {
741
+ role: "tool",
742
+ content: sanitizeSurrogates(toolResultText),
743
+ tool_call_id: toolMsg.toolCallId
744
+ };
745
+ if (compat.requiresToolResultName && toolMsg.toolName) toolResultMsg.name = toolMsg.toolName;
746
+ params.push(toolResultMsg);
747
+ if (compat.deferredToolsMode === "kimi") for (const name of toolMsg.addedToolNames ?? []) deferredToolNames.add(name);
748
+ if (hasImages && model.input.includes("image")) {
749
+ for (const block of toolMsg.content) if (isImageContentBlock(block)) imageBlocks.push({
750
+ type: "image_url",
751
+ image_url: { url: `data:${block.mimeType};base64,${block.data}` }
752
+ });
753
+ }
754
+ }
755
+ i = j - 1;
756
+ if (imageBlocks.length > 0) {
757
+ if (compat.requiresAssistantAfterToolResult) params.push({
758
+ role: "assistant",
759
+ content: "I have processed the tool results."
760
+ });
761
+ params.push({
762
+ role: "user",
763
+ content: [{
764
+ type: "text",
765
+ text: "Attached image(s) from tool result:"
766
+ }, ...imageBlocks]
767
+ });
768
+ lastRole = "user";
769
+ } else lastRole = "toolResult";
770
+ if (deferredToolNames.size > 0) {
771
+ const deferredTools = getToolsByName(context.tools, deferredToolNames);
772
+ if (deferredTools.length > 0) {
773
+ const kimiToolMessage = {
774
+ role: "system",
775
+ tools: convertTools(deferredTools, compat)
776
+ };
777
+ params.push(kimiToolMessage);
778
+ }
779
+ }
780
+ continue;
781
+ }
782
+ lastRole = msg.role;
783
+ }
784
+ return params;
785
+ }
786
+ function convertTools(tools, compat) {
787
+ return tools.map((tool) => {
788
+ const grammar = resolveGrammarConstrainedSampling(tool, compat.supportsOpenAIGrammarTools);
789
+ if (grammar) return {
790
+ type: "custom",
791
+ custom: {
792
+ name: tool.name,
793
+ description: tool.description,
794
+ format: {
795
+ type: "grammar",
796
+ grammar: {
797
+ syntax: grammar.format,
798
+ definition: grammar.definition
799
+ }
800
+ }
801
+ }
802
+ };
803
+ const strict = resolveJsonSchemaStrictSampling(tool, compat.supportsStrictMode !== false);
804
+ return {
805
+ type: "function",
806
+ function: {
807
+ name: tool.name,
808
+ description: tool.description,
809
+ parameters: tool.parameters,
810
+ ...compat.supportsStrictMode !== false && { strict: strict ?? false }
811
+ }
812
+ };
813
+ });
814
+ }
815
+ function parseChunkUsage(rawUsage, model) {
816
+ const promptTokens = rawUsage.prompt_tokens || 0;
817
+ const cacheReadTokens = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.prompt_cache_hit_tokens ?? 0;
818
+ const cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
819
+ const input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);
820
+ const outputTokens = rawUsage.completion_tokens || 0;
821
+ const usage = {
822
+ input,
823
+ output: outputTokens,
824
+ cacheRead: cacheReadTokens,
825
+ cacheWrite: cacheWriteTokens,
826
+ reasoning: rawUsage.completion_tokens_details?.reasoning_tokens || 0,
827
+ totalTokens: input + outputTokens + cacheReadTokens + cacheWriteTokens,
828
+ cost: {
829
+ input: 0,
830
+ output: 0,
831
+ cacheRead: 0,
832
+ cacheWrite: 0,
833
+ total: 0
834
+ }
835
+ };
836
+ calculateCost(model, usage);
837
+ return usage;
838
+ }
839
+ function mapStopReason(reason) {
840
+ if (reason === null) return { stopReason: "stop" };
841
+ switch (reason) {
842
+ case "stop":
843
+ case "end": return { stopReason: "stop" };
844
+ case "length": return { stopReason: "length" };
845
+ case "function_call":
846
+ case "tool_calls": return { stopReason: "toolUse" };
847
+ case "content_filter": return {
848
+ stopReason: "error",
849
+ errorMessage: "Provider finish_reason: content_filter"
850
+ };
851
+ case "network_error": return {
852
+ stopReason: "error",
853
+ errorMessage: "Provider finish_reason: network_error"
854
+ };
855
+ default: return {
856
+ stopReason: "error",
857
+ errorMessage: `Provider finish_reason: ${reason}`
858
+ };
859
+ }
860
+ }
861
+ /**
862
+ * Auto-detect compatibility settings from provider name and baseUrl.
863
+ * Used as the base when model.compat is not set; explicit model.compat
864
+ * entries override these detected values.
865
+ */
866
+ function detectCompat(model) {
867
+ const provider = model.provider;
868
+ const baseUrl = model.baseUrl;
869
+ const isZai = provider === "zai" || provider === "zai-coding-cn" || baseUrl.includes("api.z.ai") || baseUrl.includes("open.bigmodel.cn");
870
+ const isTogether = provider === "together" || baseUrl.includes("api.together.ai") || baseUrl.includes("api.together.xyz");
871
+ const isMoonshot = provider === "moonshotai" || provider === "moonshotai-cn" || baseUrl.includes("api.moonshot.");
872
+ const isOpenRouter = provider === "openrouter" || baseUrl.includes("openrouter.ai");
873
+ const isCloudflareWorkersAI = provider === "cloudflare-workers-ai" || baseUrl.includes("api.cloudflare.com");
874
+ const isCloudflareAiGateway = provider === "cloudflare-ai-gateway" || baseUrl.includes("gateway.ai.cloudflare.com");
875
+ const isNvidia = provider === "nvidia" || baseUrl.includes("integrate.api.nvidia.com");
876
+ const isAntLing = provider === "ant-ling" || baseUrl.includes("api.ant-ling.com");
877
+ const isNonStandard = isNvidia || provider === "cerebras" || baseUrl.includes("cerebras.ai") || provider === "xai" || baseUrl.includes("api.x.ai") || isTogether || baseUrl.includes("chutes.ai") || baseUrl.includes("deepseek.com") || isZai || isMoonshot || provider === "opencode" || baseUrl.includes("opencode.ai") || isCloudflareWorkersAI || isCloudflareAiGateway || isAntLing;
878
+ const useMaxTokens = baseUrl.includes("chutes.ai") || isMoonshot || isCloudflareAiGateway || isTogether || isNvidia || isAntLing || isZai;
879
+ const isGrok = provider === "xai" || baseUrl.includes("api.x.ai");
880
+ const isDeepSeek = provider === "deepseek" || baseUrl.includes("deepseek.com");
881
+ const isOpenRouterDeveloperRoleModel = isOpenRouter && (model.id.startsWith("anthropic/") || model.id.startsWith("openai/"));
882
+ const cacheControlFormat = provider === "openrouter" && model.id.startsWith("anthropic/") ? "anthropic" : void 0;
883
+ return {
884
+ supportsStore: !isNonStandard,
885
+ supportsDeveloperRole: isOpenRouterDeveloperRoleModel || !isNonStandard && !isOpenRouter,
886
+ supportsReasoningEffort: !isGrok && !isZai && !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia && !isAntLing,
887
+ supportsUsageInStreaming: true,
888
+ supportsFinishReason: true,
889
+ maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
890
+ requiresToolResultName: false,
891
+ requiresAssistantAfterToolResult: false,
892
+ requiresThinkingAsText: false,
893
+ requiresReasoningContentOnAssistantMessages: isDeepSeek,
894
+ thinkingFormat: isDeepSeek ? "deepseek" : isZai ? "zai" : isTogether ? "together" : isAntLing ? "ant-ling" : isOpenRouter ? "openrouter" : "openai",
895
+ openRouterRouting: {},
896
+ vercelGatewayRouting: {},
897
+ chatTemplateKwargs: {},
898
+ chatTemplateArgs: {},
899
+ zaiToolStream: false,
900
+ supportsThinkingTokenBudget: false,
901
+ supportsStrictMode: !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia,
902
+ supportsOpenAIGrammarTools: false,
903
+ cacheControlFormat,
904
+ sendSessionAffinityHeaders: false,
905
+ deferredToolsMode: void 0,
906
+ sessionAffinityFormat: isOpenRouter ? "openrouter" : "openai",
907
+ supportsLongCacheRetention: !(isTogether || isCloudflareWorkersAI || isCloudflareAiGateway || isNvidia || isAntLing)
908
+ };
909
+ }
910
+ /**
911
+ * Get resolved compatibility settings for a model.
912
+ * Auto-detects from provider/URL then overrides with explicit model.compat.
913
+ */
914
+ function getCompat(model) {
915
+ const detected = detectCompat(model);
916
+ if (!model.compat) return detected;
917
+ return {
918
+ supportsStore: model.compat.supportsStore ?? detected.supportsStore,
919
+ supportsDeveloperRole: model.compat.supportsDeveloperRole ?? detected.supportsDeveloperRole,
920
+ supportsReasoningEffort: model.compat.supportsReasoningEffort ?? detected.supportsReasoningEffort,
921
+ supportsUsageInStreaming: model.compat.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,
922
+ supportsFinishReason: model.compat.supportsFinishReason ?? detected.supportsFinishReason,
923
+ maxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,
924
+ requiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,
925
+ requiresAssistantAfterToolResult: model.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,
926
+ requiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,
927
+ requiresReasoningContentOnAssistantMessages: model.compat.requiresReasoningContentOnAssistantMessages ?? detected.requiresReasoningContentOnAssistantMessages,
928
+ thinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,
929
+ openRouterRouting: model.compat.openRouterRouting ?? {},
930
+ vercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,
931
+ chatTemplateKwargs: model.compat.chatTemplateKwargs ?? detected.chatTemplateKwargs,
932
+ chatTemplateArgs: model.compat.chatTemplateArgs ?? detected.chatTemplateArgs,
933
+ zaiToolStream: model.compat.zaiToolStream ?? detected.zaiToolStream,
934
+ supportsThinkingTokenBudget: model.compat.supportsThinkingTokenBudget ?? detected.supportsThinkingTokenBudget,
935
+ supportsStrictMode: model.compat.supportsStrictMode ?? detected.supportsStrictMode,
936
+ supportsOpenAIGrammarTools: model.compat.supportsOpenAIGrammarTools ?? detected.supportsOpenAIGrammarTools,
937
+ cacheControlFormat: model.compat.cacheControlFormat ?? detected.cacheControlFormat,
938
+ sendSessionAffinityHeaders: model.compat.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,
939
+ deferredToolsMode: model.compat.deferredToolsMode ?? detected.deferredToolsMode,
940
+ sessionAffinityFormat: model.compat.sessionAffinityFormat ?? detected.sessionAffinityFormat,
941
+ supportsLongCacheRetention: model.compat.supportsLongCacheRetention ?? detected.supportsLongCacheRetention
942
+ };
943
+ }
944
+ //#endregion
945
+ export { convertMessages, stream, streamSimple };
946
+
947
+ //# sourceMappingURL=openai-completions-fXHhRolF.mjs.map