@hyperspaceng/neural-ai 0.61.2 → 0.62.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.
Files changed (169) hide show
  1. package/dist/api-registry.d.ts +20 -0
  2. package/dist/api-registry.d.ts.map +1 -0
  3. package/dist/api-registry.js +44 -0
  4. package/dist/api-registry.js.map +1 -0
  5. package/dist/bedrock-provider.d.ts +5 -0
  6. package/dist/bedrock-provider.d.ts.map +1 -0
  7. package/dist/bedrock-provider.js +6 -0
  8. package/dist/bedrock-provider.js.map +1 -0
  9. package/dist/cli.d.ts +3 -0
  10. package/dist/cli.d.ts.map +1 -0
  11. package/dist/cli.js +116 -0
  12. package/dist/cli.js.map +1 -0
  13. package/dist/env-api-keys.d.ts +9 -0
  14. package/dist/env-api-keys.d.ts.map +1 -0
  15. package/dist/env-api-keys.js +107 -0
  16. package/dist/env-api-keys.js.map +1 -0
  17. package/dist/index.d.ts +24 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +13 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/models.d.ts +24 -0
  22. package/dist/models.d.ts.map +1 -0
  23. package/dist/models.generated.d.ts +14109 -0
  24. package/dist/models.generated.d.ts.map +1 -0
  25. package/dist/models.generated.js +13897 -0
  26. package/dist/models.generated.js.map +1 -0
  27. package/dist/models.js +55 -0
  28. package/dist/models.js.map +1 -0
  29. package/dist/oauth.d.ts +2 -0
  30. package/dist/oauth.d.ts.map +1 -0
  31. package/dist/oauth.js +2 -0
  32. package/dist/oauth.js.map +1 -0
  33. package/dist/providers/amazon-bedrock.d.ts +15 -0
  34. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  35. package/dist/providers/amazon-bedrock.js +622 -0
  36. package/dist/providers/amazon-bedrock.js.map +1 -0
  37. package/dist/providers/anthropic.d.ts +40 -0
  38. package/dist/providers/anthropic.d.ts.map +1 -0
  39. package/dist/providers/anthropic.js +743 -0
  40. package/dist/providers/anthropic.js.map +1 -0
  41. package/dist/providers/azure-openai-responses.d.ts +15 -0
  42. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  43. package/dist/providers/azure-openai-responses.js +187 -0
  44. package/dist/providers/azure-openai-responses.js.map +1 -0
  45. package/dist/providers/github-copilot-headers.d.ts +8 -0
  46. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  47. package/dist/providers/github-copilot-headers.js +29 -0
  48. package/dist/providers/github-copilot-headers.js.map +1 -0
  49. package/dist/providers/google-gemini-cli.d.ts +74 -0
  50. package/dist/providers/google-gemini-cli.d.ts.map +1 -0
  51. package/dist/providers/google-gemini-cli.js +760 -0
  52. package/dist/providers/google-gemini-cli.js.map +1 -0
  53. package/dist/providers/google-shared.d.ts +65 -0
  54. package/dist/providers/google-shared.d.ts.map +1 -0
  55. package/dist/providers/google-shared.js +312 -0
  56. package/dist/providers/google-shared.js.map +1 -0
  57. package/dist/providers/google-vertex.d.ts +15 -0
  58. package/dist/providers/google-vertex.d.ts.map +1 -0
  59. package/dist/providers/google-vertex.js +402 -0
  60. package/dist/providers/google-vertex.js.map +1 -0
  61. package/dist/providers/google.d.ts +13 -0
  62. package/dist/providers/google.d.ts.map +1 -0
  63. package/dist/providers/google.js +358 -0
  64. package/dist/providers/google.js.map +1 -0
  65. package/dist/providers/mistral.d.ts +22 -0
  66. package/dist/providers/mistral.d.ts.map +1 -0
  67. package/dist/providers/mistral.js +501 -0
  68. package/dist/providers/mistral.js.map +1 -0
  69. package/dist/providers/openai-codex-responses.d.ts +9 -0
  70. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  71. package/dist/providers/openai-codex-responses.js +741 -0
  72. package/dist/providers/openai-codex-responses.js.map +1 -0
  73. package/dist/providers/openai-completions.d.ts +15 -0
  74. package/dist/providers/openai-completions.d.ts.map +1 -0
  75. package/dist/providers/openai-completions.js +734 -0
  76. package/dist/providers/openai-completions.js.map +1 -0
  77. package/dist/providers/openai-responses-shared.d.ts +17 -0
  78. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  79. package/dist/providers/openai-responses-shared.js +453 -0
  80. package/dist/providers/openai-responses-shared.js.map +1 -0
  81. package/dist/providers/openai-responses.d.ts +13 -0
  82. package/dist/providers/openai-responses.d.ts.map +1 -0
  83. package/dist/providers/openai-responses.js +201 -0
  84. package/dist/providers/openai-responses.js.map +1 -0
  85. package/dist/providers/register-builtins.d.ts +38 -0
  86. package/dist/providers/register-builtins.d.ts.map +1 -0
  87. package/dist/providers/register-builtins.js +261 -0
  88. package/dist/providers/register-builtins.js.map +1 -0
  89. package/dist/providers/simple-options.d.ts +8 -0
  90. package/dist/providers/simple-options.d.ts.map +1 -0
  91. package/dist/providers/simple-options.js +35 -0
  92. package/dist/providers/simple-options.js.map +1 -0
  93. package/dist/providers/transform-messages.d.ts +8 -0
  94. package/dist/providers/transform-messages.d.ts.map +1 -0
  95. package/dist/providers/transform-messages.js +155 -0
  96. package/dist/providers/transform-messages.js.map +1 -0
  97. package/dist/stream.d.ts +8 -0
  98. package/dist/stream.d.ts.map +1 -0
  99. package/dist/stream.js +27 -0
  100. package/dist/stream.js.map +1 -0
  101. package/dist/types.d.ts +294 -0
  102. package/dist/types.d.ts.map +1 -0
  103. package/dist/types.js +2 -0
  104. package/dist/types.js.map +1 -0
  105. package/dist/utils/event-stream.d.ts +21 -0
  106. package/dist/utils/event-stream.d.ts.map +1 -0
  107. package/dist/utils/event-stream.js +81 -0
  108. package/dist/utils/event-stream.js.map +1 -0
  109. package/dist/utils/hash.d.ts +3 -0
  110. package/dist/utils/hash.d.ts.map +1 -0
  111. package/dist/utils/hash.js +14 -0
  112. package/dist/utils/hash.js.map +1 -0
  113. package/dist/utils/json-parse.d.ts +9 -0
  114. package/dist/utils/json-parse.d.ts.map +1 -0
  115. package/dist/utils/json-parse.js +29 -0
  116. package/dist/utils/json-parse.js.map +1 -0
  117. package/dist/utils/oauth/anthropic.d.ts +25 -0
  118. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  119. package/dist/utils/oauth/anthropic.js +335 -0
  120. package/dist/utils/oauth/anthropic.js.map +1 -0
  121. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  122. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  123. package/dist/utils/oauth/github-copilot.js +292 -0
  124. package/dist/utils/oauth/github-copilot.js.map +1 -0
  125. package/dist/utils/oauth/google-antigravity.d.ts +26 -0
  126. package/dist/utils/oauth/google-antigravity.d.ts.map +1 -0
  127. package/dist/utils/oauth/google-antigravity.js +376 -0
  128. package/dist/utils/oauth/google-antigravity.js.map +1 -0
  129. package/dist/utils/oauth/google-gemini-cli.d.ts +26 -0
  130. package/dist/utils/oauth/google-gemini-cli.d.ts.map +1 -0
  131. package/dist/utils/oauth/google-gemini-cli.js +481 -0
  132. package/dist/utils/oauth/google-gemini-cli.js.map +1 -0
  133. package/dist/utils/oauth/index.d.ts +61 -0
  134. package/dist/utils/oauth/index.d.ts.map +1 -0
  135. package/dist/utils/oauth/index.js +131 -0
  136. package/dist/utils/oauth/index.js.map +1 -0
  137. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  138. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  139. package/dist/utils/oauth/oauth-page.js +105 -0
  140. package/dist/utils/oauth/oauth-page.js.map +1 -0
  141. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  142. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  143. package/dist/utils/oauth/openai-codex.js +373 -0
  144. package/dist/utils/oauth/openai-codex.js.map +1 -0
  145. package/dist/utils/oauth/pkce.d.ts +13 -0
  146. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  147. package/dist/utils/oauth/pkce.js +31 -0
  148. package/dist/utils/oauth/pkce.js.map +1 -0
  149. package/dist/utils/oauth/types.d.ts +47 -0
  150. package/dist/utils/oauth/types.d.ts.map +1 -0
  151. package/dist/utils/oauth/types.js +2 -0
  152. package/dist/utils/oauth/types.js.map +1 -0
  153. package/dist/utils/overflow.d.ts +52 -0
  154. package/dist/utils/overflow.d.ts.map +1 -0
  155. package/dist/utils/overflow.js +117 -0
  156. package/dist/utils/overflow.js.map +1 -0
  157. package/dist/utils/sanitize-unicode.d.ts +22 -0
  158. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  159. package/dist/utils/sanitize-unicode.js +26 -0
  160. package/dist/utils/sanitize-unicode.js.map +1 -0
  161. package/dist/utils/typebox-helpers.d.ts +17 -0
  162. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  163. package/dist/utils/typebox-helpers.js +21 -0
  164. package/dist/utils/typebox-helpers.js.map +1 -0
  165. package/dist/utils/validation.d.ts +18 -0
  166. package/dist/utils/validation.d.ts.map +1 -0
  167. package/dist/utils/validation.js +80 -0
  168. package/dist/utils/validation.js.map +1 -0
  169. package/package.json +1 -1
@@ -0,0 +1,734 @@
1
+ import OpenAI from "openai";
2
+ import { getEnvApiKey } from "../env-api-keys.js";
3
+ import { calculateCost, supportsXhigh } from "../models.js";
4
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
5
+ import { parseStreamingJson } from "../utils/json-parse.js";
6
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
7
+ import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
8
+ import { buildBaseOptions, clampReasoning } from "./simple-options.js";
9
+ import { transformMessages } from "./transform-messages.js";
10
+ /**
11
+ * Check if conversation messages contain tool calls or tool results.
12
+ * This is needed because Anthropic (via proxy) requires the tools param
13
+ * to be present when messages include tool_calls or tool role messages.
14
+ */
15
+ function hasToolHistory(messages) {
16
+ for (const msg of messages) {
17
+ if (msg.role === "toolResult") {
18
+ return true;
19
+ }
20
+ if (msg.role === "assistant") {
21
+ if (msg.content.some((block) => block.type === "toolCall")) {
22
+ return true;
23
+ }
24
+ }
25
+ }
26
+ return false;
27
+ }
28
+ export const streamOpenAICompletions = (model, context, options) => {
29
+ const stream = new AssistantMessageEventStream();
30
+ (async () => {
31
+ const output = {
32
+ role: "assistant",
33
+ content: [],
34
+ api: model.api,
35
+ provider: model.provider,
36
+ model: model.id,
37
+ usage: {
38
+ input: 0,
39
+ output: 0,
40
+ cacheRead: 0,
41
+ cacheWrite: 0,
42
+ totalTokens: 0,
43
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
44
+ },
45
+ stopReason: "stop",
46
+ timestamp: Date.now(),
47
+ };
48
+ try {
49
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
50
+ const client = createClient(model, context, apiKey, options?.headers);
51
+ let params = buildParams(model, context, options);
52
+ const nextParams = await options?.onPayload?.(params, model);
53
+ if (nextParams !== undefined) {
54
+ params = nextParams;
55
+ }
56
+ const openaiStream = await client.chat.completions.create(params, { signal: options?.signal });
57
+ stream.push({ type: "start", partial: output });
58
+ let currentBlock = null;
59
+ const blocks = output.content;
60
+ const blockIndex = () => blocks.length - 1;
61
+ const finishCurrentBlock = (block) => {
62
+ if (block) {
63
+ if (block.type === "text") {
64
+ stream.push({
65
+ type: "text_end",
66
+ contentIndex: blockIndex(),
67
+ content: block.text,
68
+ partial: output,
69
+ });
70
+ }
71
+ else if (block.type === "thinking") {
72
+ stream.push({
73
+ type: "thinking_end",
74
+ contentIndex: blockIndex(),
75
+ content: block.thinking,
76
+ partial: output,
77
+ });
78
+ }
79
+ else if (block.type === "toolCall") {
80
+ block.arguments = parseStreamingJson(block.partialArgs);
81
+ delete block.partialArgs;
82
+ stream.push({
83
+ type: "toolcall_end",
84
+ contentIndex: blockIndex(),
85
+ toolCall: block,
86
+ partial: output,
87
+ });
88
+ }
89
+ }
90
+ };
91
+ for await (const chunk of openaiStream) {
92
+ // OpenAI documents ChatCompletionChunk.id as the unique chat completion identifier,
93
+ // and each chunk in a streamed completion carries the same id.
94
+ output.responseId ||= chunk.id;
95
+ if (chunk.usage) {
96
+ output.usage = parseChunkUsage(chunk.usage, model);
97
+ }
98
+ const choice = chunk.choices?.[0];
99
+ if (!choice)
100
+ continue;
101
+ // Fallback: some providers (e.g., Moonshot) return usage
102
+ // in choice.usage instead of the standard chunk.usage
103
+ if (!chunk.usage && choice.usage) {
104
+ output.usage = parseChunkUsage(choice.usage, model);
105
+ }
106
+ if (choice.finish_reason) {
107
+ const finishReasonResult = mapStopReason(choice.finish_reason);
108
+ output.stopReason = finishReasonResult.stopReason;
109
+ if (finishReasonResult.errorMessage) {
110
+ output.errorMessage = finishReasonResult.errorMessage;
111
+ }
112
+ }
113
+ if (choice.delta) {
114
+ if (choice.delta.content !== null &&
115
+ choice.delta.content !== undefined &&
116
+ choice.delta.content.length > 0) {
117
+ if (!currentBlock || currentBlock.type !== "text") {
118
+ finishCurrentBlock(currentBlock);
119
+ currentBlock = { type: "text", text: "" };
120
+ output.content.push(currentBlock);
121
+ stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
122
+ }
123
+ if (currentBlock.type === "text") {
124
+ currentBlock.text += choice.delta.content;
125
+ stream.push({
126
+ type: "text_delta",
127
+ contentIndex: blockIndex(),
128
+ delta: choice.delta.content,
129
+ partial: output,
130
+ });
131
+ }
132
+ }
133
+ // Some endpoints return reasoning in reasoning_content (llama.cpp),
134
+ // or reasoning (other openai compatible endpoints)
135
+ // Use the first non-empty reasoning field to avoid duplication
136
+ // (e.g., chutes.ai returns both reasoning_content and reasoning with same content)
137
+ const reasoningFields = ["reasoning_content", "reasoning", "reasoning_text"];
138
+ let foundReasoningField = null;
139
+ for (const field of reasoningFields) {
140
+ if (choice.delta[field] !== null &&
141
+ choice.delta[field] !== undefined &&
142
+ choice.delta[field].length > 0) {
143
+ if (!foundReasoningField) {
144
+ foundReasoningField = field;
145
+ break;
146
+ }
147
+ }
148
+ }
149
+ if (foundReasoningField) {
150
+ if (!currentBlock || currentBlock.type !== "thinking") {
151
+ finishCurrentBlock(currentBlock);
152
+ currentBlock = {
153
+ type: "thinking",
154
+ thinking: "",
155
+ thinkingSignature: foundReasoningField,
156
+ };
157
+ output.content.push(currentBlock);
158
+ stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
159
+ }
160
+ if (currentBlock.type === "thinking") {
161
+ const delta = choice.delta[foundReasoningField];
162
+ currentBlock.thinking += delta;
163
+ stream.push({
164
+ type: "thinking_delta",
165
+ contentIndex: blockIndex(),
166
+ delta,
167
+ partial: output,
168
+ });
169
+ }
170
+ }
171
+ if (choice?.delta?.tool_calls) {
172
+ for (const toolCall of choice.delta.tool_calls) {
173
+ if (!currentBlock ||
174
+ currentBlock.type !== "toolCall" ||
175
+ (toolCall.id && currentBlock.id !== toolCall.id)) {
176
+ finishCurrentBlock(currentBlock);
177
+ currentBlock = {
178
+ type: "toolCall",
179
+ id: toolCall.id || "",
180
+ name: toolCall.function?.name || "",
181
+ arguments: {},
182
+ partialArgs: "",
183
+ };
184
+ output.content.push(currentBlock);
185
+ stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
186
+ }
187
+ if (currentBlock.type === "toolCall") {
188
+ if (toolCall.id)
189
+ currentBlock.id = toolCall.id;
190
+ if (toolCall.function?.name)
191
+ currentBlock.name = toolCall.function.name;
192
+ let delta = "";
193
+ if (toolCall.function?.arguments) {
194
+ delta = toolCall.function.arguments;
195
+ currentBlock.partialArgs += toolCall.function.arguments;
196
+ currentBlock.arguments = parseStreamingJson(currentBlock.partialArgs);
197
+ }
198
+ stream.push({
199
+ type: "toolcall_delta",
200
+ contentIndex: blockIndex(),
201
+ delta,
202
+ partial: output,
203
+ });
204
+ }
205
+ }
206
+ }
207
+ const reasoningDetails = choice.delta.reasoning_details;
208
+ if (reasoningDetails && Array.isArray(reasoningDetails)) {
209
+ for (const detail of reasoningDetails) {
210
+ if (detail.type === "reasoning.encrypted" && detail.id && detail.data) {
211
+ const matchingToolCall = output.content.find((b) => b.type === "toolCall" && b.id === detail.id);
212
+ if (matchingToolCall) {
213
+ matchingToolCall.thoughtSignature = JSON.stringify(detail);
214
+ }
215
+ }
216
+ }
217
+ }
218
+ }
219
+ }
220
+ finishCurrentBlock(currentBlock);
221
+ if (options?.signal?.aborted) {
222
+ throw new Error("Request was aborted");
223
+ }
224
+ if (output.stopReason === "aborted") {
225
+ throw new Error("Request was aborted");
226
+ }
227
+ if (output.stopReason === "error") {
228
+ throw new Error(output.errorMessage || "Provider returned an error stop reason");
229
+ }
230
+ stream.push({ type: "done", reason: output.stopReason, message: output });
231
+ stream.end();
232
+ }
233
+ catch (error) {
234
+ for (const block of output.content)
235
+ delete block.index;
236
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
237
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
238
+ // Some providers via OpenRouter give additional information in this field.
239
+ const rawMetadata = error?.error?.metadata?.raw;
240
+ if (rawMetadata)
241
+ output.errorMessage += `\n${rawMetadata}`;
242
+ stream.push({ type: "error", reason: output.stopReason, error: output });
243
+ stream.end();
244
+ }
245
+ })();
246
+ return stream;
247
+ };
248
+ export const streamSimpleOpenAICompletions = (model, context, options) => {
249
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
250
+ if (!apiKey) {
251
+ throw new Error(`No API key for provider: ${model.provider}`);
252
+ }
253
+ const base = buildBaseOptions(model, options, apiKey);
254
+ const reasoningEffort = supportsXhigh(model) ? options?.reasoning : clampReasoning(options?.reasoning);
255
+ const toolChoice = options?.toolChoice;
256
+ return streamOpenAICompletions(model, context, {
257
+ ...base,
258
+ reasoningEffort,
259
+ toolChoice,
260
+ });
261
+ };
262
+ function createClient(model, context, apiKey, optionsHeaders) {
263
+ if (!apiKey) {
264
+ if (!process.env.OPENAI_API_KEY) {
265
+ throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");
266
+ }
267
+ apiKey = process.env.OPENAI_API_KEY;
268
+ }
269
+ const headers = { ...model.headers };
270
+ if (model.provider === "github-copilot") {
271
+ const hasImages = hasCopilotVisionInput(context.messages);
272
+ const copilotHeaders = buildCopilotDynamicHeaders({
273
+ messages: context.messages,
274
+ hasImages,
275
+ });
276
+ Object.assign(headers, copilotHeaders);
277
+ }
278
+ // Merge options headers last so they can override defaults
279
+ if (optionsHeaders) {
280
+ Object.assign(headers, optionsHeaders);
281
+ }
282
+ return new OpenAI({
283
+ apiKey,
284
+ baseURL: model.baseUrl,
285
+ dangerouslyAllowBrowser: true,
286
+ defaultHeaders: headers,
287
+ });
288
+ }
289
+ function buildParams(model, context, options) {
290
+ const compat = getCompat(model);
291
+ const messages = convertMessages(model, context, compat);
292
+ maybeAddOpenRouterAnthropicCacheControl(model, messages);
293
+ const params = {
294
+ model: model.id,
295
+ messages,
296
+ stream: true,
297
+ };
298
+ if (compat.supportsUsageInStreaming !== false) {
299
+ params.stream_options = { include_usage: true };
300
+ }
301
+ if (compat.supportsStore) {
302
+ params.store = false;
303
+ }
304
+ if (options?.maxTokens) {
305
+ if (compat.maxTokensField === "max_tokens") {
306
+ params.max_tokens = options.maxTokens;
307
+ }
308
+ else {
309
+ params.max_completion_tokens = options.maxTokens;
310
+ }
311
+ }
312
+ if (options?.temperature !== undefined) {
313
+ params.temperature = options.temperature;
314
+ }
315
+ if (context.tools) {
316
+ params.tools = convertTools(context.tools, compat);
317
+ }
318
+ else if (hasToolHistory(context.messages)) {
319
+ // Anthropic (via LiteLLM/proxy) requires tools param when conversation has tool_calls/tool_results
320
+ params.tools = [];
321
+ }
322
+ if (options?.toolChoice) {
323
+ params.tool_choice = options.toolChoice;
324
+ }
325
+ if (compat.thinkingFormat === "zai" && model.reasoning) {
326
+ params.enable_thinking = !!options?.reasoningEffort;
327
+ }
328
+ else if (compat.thinkingFormat === "qwen" && model.reasoning) {
329
+ params.enable_thinking = !!options?.reasoningEffort;
330
+ }
331
+ else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) {
332
+ params.chat_template_kwargs = { enable_thinking: !!options?.reasoningEffort };
333
+ }
334
+ else if (compat.thinkingFormat === "openrouter" && options?.reasoningEffort && model.reasoning) {
335
+ // OpenRouter normalizes reasoning across providers via a nested reasoning object.
336
+ const openRouterParams = params;
337
+ openRouterParams.reasoning = {
338
+ effort: mapReasoningEffort(options.reasoningEffort, compat.reasoningEffortMap),
339
+ };
340
+ }
341
+ else if (options?.reasoningEffort && model.reasoning && compat.supportsReasoningEffort) {
342
+ // OpenAI-style reasoning_effort
343
+ params.reasoning_effort = mapReasoningEffort(options.reasoningEffort, compat.reasoningEffortMap);
344
+ }
345
+ // OpenRouter provider routing preferences
346
+ if (model.baseUrl.includes("openrouter.ai") && model.compat?.openRouterRouting) {
347
+ params.provider = model.compat.openRouterRouting;
348
+ }
349
+ // Vercel AI Gateway provider routing preferences
350
+ if (model.baseUrl.includes("ai-gateway.vercel.sh") && model.compat?.vercelGatewayRouting) {
351
+ const routing = model.compat.vercelGatewayRouting;
352
+ if (routing.only || routing.order) {
353
+ const gatewayOptions = {};
354
+ if (routing.only)
355
+ gatewayOptions.only = routing.only;
356
+ if (routing.order)
357
+ gatewayOptions.order = routing.order;
358
+ params.providerOptions = { gateway: gatewayOptions };
359
+ }
360
+ }
361
+ return params;
362
+ }
363
+ function mapReasoningEffort(effort, reasoningEffortMap) {
364
+ return reasoningEffortMap[effort] ?? effort;
365
+ }
366
+ function maybeAddOpenRouterAnthropicCacheControl(model, messages) {
367
+ if (model.provider !== "openrouter" || !model.id.startsWith("anthropic/"))
368
+ return;
369
+ // Anthropic-style caching requires cache_control on a text part. Add a breakpoint
370
+ // on the last user/assistant message (walking backwards until we find text content).
371
+ for (let i = messages.length - 1; i >= 0; i--) {
372
+ const msg = messages[i];
373
+ if (msg.role !== "user" && msg.role !== "assistant")
374
+ continue;
375
+ const content = msg.content;
376
+ if (typeof content === "string") {
377
+ msg.content = [
378
+ Object.assign({ type: "text", text: content }, { cache_control: { type: "ephemeral" } }),
379
+ ];
380
+ return;
381
+ }
382
+ if (!Array.isArray(content))
383
+ continue;
384
+ // Find last text part and add cache_control
385
+ for (let j = content.length - 1; j >= 0; j--) {
386
+ const part = content[j];
387
+ if (part?.type === "text") {
388
+ Object.assign(part, { cache_control: { type: "ephemeral" } });
389
+ return;
390
+ }
391
+ }
392
+ }
393
+ }
394
+ export function convertMessages(model, context, compat) {
395
+ const params = [];
396
+ const normalizeToolCallId = (id) => {
397
+ // Handle pipe-separated IDs from OpenAI Responses API
398
+ // Format: {call_id}|{id} where {id} can be 400+ chars with special chars (+, /, =)
399
+ // These come from providers like github-copilot, openai-codex, opencode
400
+ // Extract just the call_id part and normalize it
401
+ if (id.includes("|")) {
402
+ const [callId] = id.split("|");
403
+ // Sanitize to allowed chars and truncate to 40 chars (OpenAI limit)
404
+ return callId.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 40);
405
+ }
406
+ if (model.provider === "openai")
407
+ return id.length > 40 ? id.slice(0, 40) : id;
408
+ return id;
409
+ };
410
+ const transformedMessages = transformMessages(context.messages, model, (id) => normalizeToolCallId(id));
411
+ if (context.systemPrompt) {
412
+ const useDeveloperRole = model.reasoning && compat.supportsDeveloperRole;
413
+ const role = useDeveloperRole ? "developer" : "system";
414
+ params.push({ role: role, content: sanitizeSurrogates(context.systemPrompt) });
415
+ }
416
+ let lastRole = null;
417
+ for (let i = 0; i < transformedMessages.length; i++) {
418
+ const msg = transformedMessages[i];
419
+ // Some providers don't allow user messages directly after tool results
420
+ // Insert a synthetic assistant message to bridge the gap
421
+ if (compat.requiresAssistantAfterToolResult && lastRole === "toolResult" && msg.role === "user") {
422
+ params.push({
423
+ role: "assistant",
424
+ content: "I have processed the tool results.",
425
+ });
426
+ }
427
+ if (msg.role === "user") {
428
+ if (typeof msg.content === "string") {
429
+ params.push({
430
+ role: "user",
431
+ content: sanitizeSurrogates(msg.content),
432
+ });
433
+ }
434
+ else {
435
+ const content = msg.content.map((item) => {
436
+ if (item.type === "text") {
437
+ return {
438
+ type: "text",
439
+ text: sanitizeSurrogates(item.text),
440
+ };
441
+ }
442
+ else {
443
+ return {
444
+ type: "image_url",
445
+ image_url: {
446
+ url: `data:${item.mimeType};base64,${item.data}`,
447
+ },
448
+ };
449
+ }
450
+ });
451
+ const filteredContent = !model.input.includes("image")
452
+ ? content.filter((c) => c.type !== "image_url")
453
+ : content;
454
+ if (filteredContent.length === 0)
455
+ continue;
456
+ params.push({
457
+ role: "user",
458
+ content: filteredContent,
459
+ });
460
+ }
461
+ }
462
+ else if (msg.role === "assistant") {
463
+ // Some providers don't accept null content, use empty string instead
464
+ const assistantMsg = {
465
+ role: "assistant",
466
+ content: compat.requiresAssistantAfterToolResult ? "" : null,
467
+ };
468
+ const textBlocks = msg.content.filter((b) => b.type === "text");
469
+ // Filter out empty text blocks to avoid API validation errors
470
+ const nonEmptyTextBlocks = textBlocks.filter((b) => b.text && b.text.trim().length > 0);
471
+ if (nonEmptyTextBlocks.length > 0) {
472
+ // Always send assistant content as a plain string (OpenAI Chat Completions
473
+ // API standard format). Sending as an array of {type:"text", text:"..."}
474
+ // objects is non-standard and causes some models (e.g. DeepSeek V3.2 via
475
+ // NVIDIA NIM) to mirror the content-block structure literally in their
476
+ // output, producing recursive nesting like [{'type':'text','text':'[{...}]'}].
477
+ assistantMsg.content = nonEmptyTextBlocks.map((b) => sanitizeSurrogates(b.text)).join("");
478
+ }
479
+ // Handle thinking blocks
480
+ const thinkingBlocks = msg.content.filter((b) => b.type === "thinking");
481
+ // Filter out empty thinking blocks to avoid API validation errors
482
+ const nonEmptyThinkingBlocks = thinkingBlocks.filter((b) => b.thinking && b.thinking.trim().length > 0);
483
+ if (nonEmptyThinkingBlocks.length > 0) {
484
+ if (compat.requiresThinkingAsText) {
485
+ // Convert thinking blocks to plain text (no tags to avoid model mimicking them)
486
+ const thinkingText = nonEmptyThinkingBlocks.map((b) => b.thinking).join("\n\n");
487
+ const textContent = assistantMsg.content;
488
+ if (textContent) {
489
+ textContent.unshift({ type: "text", text: thinkingText });
490
+ }
491
+ else {
492
+ assistantMsg.content = [{ type: "text", text: thinkingText }];
493
+ }
494
+ }
495
+ else {
496
+ // Use the signature from the first thinking block if available (for llama.cpp server + gpt-oss)
497
+ const signature = nonEmptyThinkingBlocks[0].thinkingSignature;
498
+ if (signature && signature.length > 0) {
499
+ assistantMsg[signature] = nonEmptyThinkingBlocks.map((b) => b.thinking).join("\n");
500
+ }
501
+ }
502
+ }
503
+ const toolCalls = msg.content.filter((b) => b.type === "toolCall");
504
+ if (toolCalls.length > 0) {
505
+ assistantMsg.tool_calls = toolCalls.map((tc) => ({
506
+ id: tc.id,
507
+ type: "function",
508
+ function: {
509
+ name: tc.name,
510
+ arguments: JSON.stringify(tc.arguments),
511
+ },
512
+ }));
513
+ const reasoningDetails = toolCalls
514
+ .filter((tc) => tc.thoughtSignature)
515
+ .map((tc) => {
516
+ try {
517
+ return JSON.parse(tc.thoughtSignature);
518
+ }
519
+ catch {
520
+ return null;
521
+ }
522
+ })
523
+ .filter(Boolean);
524
+ if (reasoningDetails.length > 0) {
525
+ assistantMsg.reasoning_details = reasoningDetails;
526
+ }
527
+ }
528
+ // Skip assistant messages that have no content and no tool calls.
529
+ // Some providers require "either content or tool_calls, but not none".
530
+ // Other providers also don't accept empty assistant messages.
531
+ // This handles aborted assistant responses that got no content.
532
+ const content = assistantMsg.content;
533
+ const hasContent = content !== null &&
534
+ content !== undefined &&
535
+ (typeof content === "string" ? content.length > 0 : content.length > 0);
536
+ if (!hasContent && !assistantMsg.tool_calls) {
537
+ continue;
538
+ }
539
+ params.push(assistantMsg);
540
+ }
541
+ else if (msg.role === "toolResult") {
542
+ const imageBlocks = [];
543
+ let j = i;
544
+ for (; j < transformedMessages.length && transformedMessages[j].role === "toolResult"; j++) {
545
+ const toolMsg = transformedMessages[j];
546
+ // Extract text and image content
547
+ const textResult = toolMsg.content
548
+ .filter((c) => c.type === "text")
549
+ .map((c) => c.text)
550
+ .join("\n");
551
+ const hasImages = toolMsg.content.some((c) => c.type === "image");
552
+ // Always send tool result with text (or placeholder if only images)
553
+ const hasText = textResult.length > 0;
554
+ // Some providers require the 'name' field in tool results
555
+ const toolResultMsg = {
556
+ role: "tool",
557
+ content: sanitizeSurrogates(hasText ? textResult : "(see attached image)"),
558
+ tool_call_id: toolMsg.toolCallId,
559
+ };
560
+ if (compat.requiresToolResultName && toolMsg.toolName) {
561
+ toolResultMsg.name = toolMsg.toolName;
562
+ }
563
+ params.push(toolResultMsg);
564
+ if (hasImages && model.input.includes("image")) {
565
+ for (const block of toolMsg.content) {
566
+ if (block.type === "image") {
567
+ imageBlocks.push({
568
+ type: "image_url",
569
+ image_url: {
570
+ url: `data:${block.mimeType};base64,${block.data}`,
571
+ },
572
+ });
573
+ }
574
+ }
575
+ }
576
+ }
577
+ i = j - 1;
578
+ if (imageBlocks.length > 0) {
579
+ if (compat.requiresAssistantAfterToolResult) {
580
+ params.push({
581
+ role: "assistant",
582
+ content: "I have processed the tool results.",
583
+ });
584
+ }
585
+ params.push({
586
+ role: "user",
587
+ content: [
588
+ {
589
+ type: "text",
590
+ text: "Attached image(s) from tool result:",
591
+ },
592
+ ...imageBlocks,
593
+ ],
594
+ });
595
+ lastRole = "user";
596
+ }
597
+ else {
598
+ lastRole = "toolResult";
599
+ }
600
+ continue;
601
+ }
602
+ lastRole = msg.role;
603
+ }
604
+ return params;
605
+ }
606
+ function convertTools(tools, compat) {
607
+ return tools.map((tool) => ({
608
+ type: "function",
609
+ function: {
610
+ name: tool.name,
611
+ description: tool.description,
612
+ parameters: tool.parameters, // TypeBox already generates JSON Schema
613
+ // Only include strict if provider supports it. Some reject unknown fields.
614
+ ...(compat.supportsStrictMode !== false && { strict: false }),
615
+ },
616
+ }));
617
+ }
618
+ function parseChunkUsage(rawUsage, model) {
619
+ const cachedTokens = rawUsage.prompt_tokens_details?.cached_tokens || 0;
620
+ const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens || 0;
621
+ // OpenAI includes cached tokens in prompt_tokens, so subtract to get non-cached input
622
+ const input = (rawUsage.prompt_tokens || 0) - cachedTokens;
623
+ // Compute totalTokens ourselves since we add reasoning_tokens to output
624
+ // and some providers (e.g., Groq) don't include them in total_tokens
625
+ const outputTokens = (rawUsage.completion_tokens || 0) + reasoningTokens;
626
+ const usage = {
627
+ input,
628
+ output: outputTokens,
629
+ cacheRead: cachedTokens,
630
+ cacheWrite: 0,
631
+ totalTokens: input + outputTokens + cachedTokens,
632
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
633
+ };
634
+ calculateCost(model, usage);
635
+ return usage;
636
+ }
637
+ function mapStopReason(reason) {
638
+ if (reason === null)
639
+ return { stopReason: "stop" };
640
+ switch (reason) {
641
+ case "stop":
642
+ case "end":
643
+ return { stopReason: "stop" };
644
+ case "length":
645
+ return { stopReason: "length" };
646
+ case "function_call":
647
+ case "tool_calls":
648
+ return { stopReason: "toolUse" };
649
+ case "content_filter":
650
+ return { stopReason: "error", errorMessage: "Provider finish_reason: content_filter" };
651
+ case "network_error":
652
+ return { stopReason: "error", errorMessage: "Provider finish_reason: network_error" };
653
+ default:
654
+ return {
655
+ stopReason: "error",
656
+ errorMessage: `Provider finish_reason: ${reason}`,
657
+ };
658
+ }
659
+ }
660
+ /**
661
+ * Detect compatibility settings from provider and baseUrl for known providers.
662
+ * Provider takes precedence over URL-based detection since it's explicitly configured.
663
+ * Returns a fully resolved OpenAICompletionsCompat object with all fields set.
664
+ */
665
+ function detectCompat(model) {
666
+ const provider = model.provider;
667
+ const baseUrl = model.baseUrl;
668
+ const isZai = provider === "zai" || baseUrl.includes("api.z.ai");
669
+ const isNonStandard = provider === "cerebras" ||
670
+ baseUrl.includes("cerebras.ai") ||
671
+ provider === "xai" ||
672
+ baseUrl.includes("api.x.ai") ||
673
+ baseUrl.includes("chutes.ai") ||
674
+ baseUrl.includes("deepseek.com") ||
675
+ isZai ||
676
+ provider === "opencode" ||
677
+ baseUrl.includes("opencode.ai");
678
+ const useMaxTokens = baseUrl.includes("chutes.ai");
679
+ const isGrok = provider === "xai" || baseUrl.includes("api.x.ai");
680
+ const isGroq = provider === "groq" || baseUrl.includes("groq.com");
681
+ const reasoningEffortMap = isGroq && model.id === "qwen/qwen3-32b"
682
+ ? {
683
+ minimal: "default",
684
+ low: "default",
685
+ medium: "default",
686
+ high: "default",
687
+ xhigh: "default",
688
+ }
689
+ : {};
690
+ return {
691
+ supportsStore: !isNonStandard,
692
+ supportsDeveloperRole: !isNonStandard,
693
+ supportsReasoningEffort: !isGrok && !isZai,
694
+ reasoningEffortMap,
695
+ supportsUsageInStreaming: true,
696
+ maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
697
+ requiresToolResultName: false,
698
+ requiresAssistantAfterToolResult: false,
699
+ requiresThinkingAsText: false,
700
+ thinkingFormat: isZai
701
+ ? "zai"
702
+ : provider === "openrouter" || baseUrl.includes("openrouter.ai")
703
+ ? "openrouter"
704
+ : "openai",
705
+ openRouterRouting: {},
706
+ vercelGatewayRouting: {},
707
+ supportsStrictMode: true,
708
+ };
709
+ }
710
+ /**
711
+ * Get resolved compatibility settings for a model.
712
+ * Uses explicit model.compat if provided, otherwise auto-detects from provider/URL.
713
+ */
714
+ function getCompat(model) {
715
+ const detected = detectCompat(model);
716
+ if (!model.compat)
717
+ return detected;
718
+ return {
719
+ supportsStore: model.compat.supportsStore ?? detected.supportsStore,
720
+ supportsDeveloperRole: model.compat.supportsDeveloperRole ?? detected.supportsDeveloperRole,
721
+ supportsReasoningEffort: model.compat.supportsReasoningEffort ?? detected.supportsReasoningEffort,
722
+ reasoningEffortMap: model.compat.reasoningEffortMap ?? detected.reasoningEffortMap,
723
+ supportsUsageInStreaming: model.compat.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,
724
+ maxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,
725
+ requiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,
726
+ requiresAssistantAfterToolResult: model.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,
727
+ requiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,
728
+ thinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,
729
+ openRouterRouting: model.compat.openRouterRouting ?? {},
730
+ vercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,
731
+ supportsStrictMode: model.compat.supportsStrictMode ?? detected.supportsStrictMode,
732
+ };
733
+ }
734
+ //# sourceMappingURL=openai-completions.js.map