@openrouter/ai-sdk-provider 1.5.3 → 6.0.0-alpha.0

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/dist/index.js CHANGED
@@ -1,30 +1,11 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
6
  var __export = (target, all) => {
26
- for (var name14 in all)
27
- __defProp(target, name14, { get: all[name14], enumerable: true });
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
28
9
  };
29
10
  var __copyProps = (to, from, except, desc) => {
30
11
  if (from && typeof from === "object" || typeof from === "function") {
@@ -34,2946 +15,1207 @@ var __copyProps = (to, from, except, desc) => {
34
15
  }
35
16
  return to;
36
17
  };
37
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
- // If the importer is in node compatibility mode or this is not an ESM
39
- // file that has been converted to a CommonJS file using a Babel-
40
- // compatible transform (i.e. "__esModule" has not been set), then set
41
- // "default" to the CommonJS "module.exports" for node compatibility.
42
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
- mod
44
- ));
45
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
19
 
47
20
  // src/index.ts
48
21
  var index_exports = {};
49
22
  __export(index_exports, {
50
- OpenRouter: () => OpenRouter,
23
+ VERSION: () => VERSION,
51
24
  createOpenRouter: () => createOpenRouter,
52
- decodeToon: () => decodeToon,
53
- encodeToon: () => encodeToon,
54
25
  openrouter: () => openrouter
55
26
  });
56
27
  module.exports = __toCommonJS(index_exports);
57
28
 
58
- // node_modules/.pnpm/@ai-sdk+provider@2.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
59
- var marker = "vercel.ai.error";
60
- var symbol = Symbol.for(marker);
61
- var _a;
62
- var _AISDKError = class _AISDKError2 extends Error {
63
- /**
64
- * Creates an AI SDK Error.
65
- *
66
- * @param {Object} params - The parameters for creating the error.
67
- * @param {string} params.name - The name of the error.
68
- * @param {string} params.message - The error message.
69
- * @param {unknown} [params.cause] - The underlying cause of the error.
70
- */
71
- constructor({
72
- name: name14,
73
- message,
74
- cause
75
- }) {
76
- super(message);
77
- this[_a] = true;
78
- this.name = name14;
79
- this.cause = cause;
80
- }
81
- /**
82
- * Checks if the given error is an AI SDK Error.
83
- * @param {unknown} error - The error to check.
84
- * @returns {boolean} True if the error is an AI SDK Error, false otherwise.
85
- */
86
- static isInstance(error) {
87
- return _AISDKError2.hasMarker(error, marker);
88
- }
89
- static hasMarker(error, marker15) {
90
- const markerSymbol = Symbol.for(marker15);
91
- return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
92
- }
93
- };
94
- _a = symbol;
95
- var AISDKError = _AISDKError;
96
- var name = "AI_APICallError";
97
- var marker2 = `vercel.ai.error.${name}`;
98
- var symbol2 = Symbol.for(marker2);
99
- var _a2;
100
- var APICallError = class extends AISDKError {
101
- constructor({
102
- message,
103
- url,
104
- requestBodyValues,
105
- statusCode,
106
- responseHeaders,
107
- responseBody,
108
- cause,
109
- isRetryable = statusCode != null && (statusCode === 408 || // request timeout
110
- statusCode === 409 || // conflict
111
- statusCode === 429 || // too many requests
112
- statusCode >= 500),
113
- // server error
114
- data
115
- }) {
116
- super({ name, message, cause });
117
- this[_a2] = true;
118
- this.url = url;
119
- this.requestBodyValues = requestBodyValues;
120
- this.statusCode = statusCode;
121
- this.responseHeaders = responseHeaders;
122
- this.responseBody = responseBody;
123
- this.isRetryable = isRetryable;
124
- this.data = data;
125
- }
126
- static isInstance(error) {
127
- return AISDKError.hasMarker(error, marker2);
128
- }
129
- };
130
- _a2 = symbol2;
131
- var name2 = "AI_EmptyResponseBodyError";
132
- var marker3 = `vercel.ai.error.${name2}`;
133
- var symbol3 = Symbol.for(marker3);
134
- var _a3;
135
- var EmptyResponseBodyError = class extends AISDKError {
136
- // used in isInstance
137
- constructor({ message = "Empty response body" } = {}) {
138
- super({ name: name2, message });
139
- this[_a3] = true;
140
- }
141
- static isInstance(error) {
142
- return AISDKError.hasMarker(error, marker3);
143
- }
144
- };
145
- _a3 = symbol3;
146
- function getErrorMessage(error) {
147
- if (error == null) {
148
- return "unknown error";
149
- }
150
- if (typeof error === "string") {
151
- return error;
152
- }
153
- if (error instanceof Error) {
154
- return error.message;
155
- }
156
- return JSON.stringify(error);
157
- }
158
- var name3 = "AI_InvalidArgumentError";
159
- var marker4 = `vercel.ai.error.${name3}`;
160
- var symbol4 = Symbol.for(marker4);
161
- var _a4;
162
- var InvalidArgumentError = class extends AISDKError {
163
- constructor({
164
- message,
165
- cause,
166
- argument
167
- }) {
168
- super({ name: name3, message, cause });
169
- this[_a4] = true;
170
- this.argument = argument;
171
- }
172
- static isInstance(error) {
173
- return AISDKError.hasMarker(error, marker4);
174
- }
175
- };
176
- _a4 = symbol4;
177
- var name4 = "AI_InvalidPromptError";
178
- var marker5 = `vercel.ai.error.${name4}`;
179
- var symbol5 = Symbol.for(marker5);
180
- var _a5;
181
- var InvalidPromptError = class extends AISDKError {
182
- constructor({
183
- prompt,
184
- message,
185
- cause
186
- }) {
187
- super({ name: name4, message: `Invalid prompt: ${message}`, cause });
188
- this[_a5] = true;
189
- this.prompt = prompt;
190
- }
191
- static isInstance(error) {
192
- return AISDKError.hasMarker(error, marker5);
193
- }
194
- };
195
- _a5 = symbol5;
196
- var name5 = "AI_InvalidResponseDataError";
197
- var marker6 = `vercel.ai.error.${name5}`;
198
- var symbol6 = Symbol.for(marker6);
199
- var _a6;
200
- var InvalidResponseDataError = class extends AISDKError {
201
- constructor({
202
- data,
203
- message = `Invalid response data: ${JSON.stringify(data)}.`
204
- }) {
205
- super({ name: name5, message });
206
- this[_a6] = true;
207
- this.data = data;
208
- }
209
- static isInstance(error) {
210
- return AISDKError.hasMarker(error, marker6);
211
- }
212
- };
213
- _a6 = symbol6;
214
- var name6 = "AI_JSONParseError";
215
- var marker7 = `vercel.ai.error.${name6}`;
216
- var symbol7 = Symbol.for(marker7);
217
- var _a7;
218
- var JSONParseError = class extends AISDKError {
219
- constructor({ text, cause }) {
220
- super({
221
- name: name6,
222
- message: `JSON parsing failed: Text: ${text}.
223
- Error message: ${getErrorMessage(cause)}`,
224
- cause
225
- });
226
- this[_a7] = true;
227
- this.text = text;
228
- }
229
- static isInstance(error) {
230
- return AISDKError.hasMarker(error, marker7);
231
- }
232
- };
233
- _a7 = symbol7;
234
- var name7 = "AI_LoadAPIKeyError";
235
- var marker8 = `vercel.ai.error.${name7}`;
236
- var symbol8 = Symbol.for(marker8);
237
- var _a8;
238
- var LoadAPIKeyError = class extends AISDKError {
239
- // used in isInstance
240
- constructor({ message }) {
241
- super({ name: name7, message });
242
- this[_a8] = true;
243
- }
244
- static isInstance(error) {
245
- return AISDKError.hasMarker(error, marker8);
246
- }
247
- };
248
- _a8 = symbol8;
249
- var name8 = "AI_LoadSettingError";
250
- var marker9 = `vercel.ai.error.${name8}`;
251
- var symbol9 = Symbol.for(marker9);
252
- var _a9;
253
- _a9 = symbol9;
254
- var name9 = "AI_NoContentGeneratedError";
255
- var marker10 = `vercel.ai.error.${name9}`;
256
- var symbol10 = Symbol.for(marker10);
257
- var _a10;
258
- var NoContentGeneratedError = class extends AISDKError {
259
- // used in isInstance
260
- constructor({
261
- message = "No content generated."
262
- } = {}) {
263
- super({ name: name9, message });
264
- this[_a10] = true;
265
- }
266
- static isInstance(error) {
267
- return AISDKError.hasMarker(error, marker10);
268
- }
269
- };
270
- _a10 = symbol10;
271
- var name10 = "AI_NoSuchModelError";
272
- var marker11 = `vercel.ai.error.${name10}`;
273
- var symbol11 = Symbol.for(marker11);
274
- var _a11;
275
- _a11 = symbol11;
276
- var name11 = "AI_TooManyEmbeddingValuesForCallError";
277
- var marker12 = `vercel.ai.error.${name11}`;
278
- var symbol12 = Symbol.for(marker12);
279
- var _a12;
280
- _a12 = symbol12;
281
- var name12 = "AI_TypeValidationError";
282
- var marker13 = `vercel.ai.error.${name12}`;
283
- var symbol13 = Symbol.for(marker13);
284
- var _a13;
285
- var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
286
- constructor({ value, cause }) {
287
- super({
288
- name: name12,
289
- message: `Type validation failed: Value: ${JSON.stringify(value)}.
290
- Error message: ${getErrorMessage(cause)}`,
291
- cause
292
- });
293
- this[_a13] = true;
294
- this.value = value;
295
- }
296
- static isInstance(error) {
297
- return AISDKError.hasMarker(error, marker13);
298
- }
299
- /**
300
- * Wraps an error into a TypeValidationError.
301
- * If the cause is already a TypeValidationError with the same value, it returns the cause.
302
- * Otherwise, it creates a new TypeValidationError.
303
- *
304
- * @param {Object} params - The parameters for wrapping the error.
305
- * @param {unknown} params.value - The value that failed validation.
306
- * @param {unknown} params.cause - The original error or cause of the validation failure.
307
- * @returns {TypeValidationError} A TypeValidationError instance.
308
- */
309
- static wrap({
310
- value,
311
- cause
312
- }) {
313
- return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
314
- }
315
- };
316
- _a13 = symbol13;
317
- var TypeValidationError = _TypeValidationError;
318
- var name13 = "AI_UnsupportedFunctionalityError";
319
- var marker14 = `vercel.ai.error.${name13}`;
320
- var symbol14 = Symbol.for(marker14);
321
- var _a14;
322
- var UnsupportedFunctionalityError = class extends AISDKError {
323
- constructor({
324
- functionality,
325
- message = `'${functionality}' functionality not supported.`
326
- }) {
327
- super({ name: name13, message });
328
- this[_a14] = true;
329
- this.functionality = functionality;
330
- }
331
- static isInstance(error) {
332
- return AISDKError.hasMarker(error, marker14);
333
- }
334
- };
335
- _a14 = symbol14;
29
+ // src/openrouter-provider.ts
30
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
336
31
 
337
- // node_modules/.pnpm/eventsource-parser@3.0.3/node_modules/eventsource-parser/dist/index.js
338
- var ParseError = class extends Error {
339
- constructor(message, options) {
340
- super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
341
- }
342
- };
343
- function noop(_arg) {
32
+ // src/chat/openrouter-chat-language-model.ts
33
+ var import_provider_utils = require("@ai-sdk/provider-utils");
34
+ var import_sdk = require("@openrouter/sdk");
35
+
36
+ // src/utils/build-provider-metadata.ts
37
+ function filterUndefined(obj) {
38
+ return Object.fromEntries(
39
+ Object.entries(obj).filter(([_, v]) => v !== void 0)
40
+ );
344
41
  }
345
- function createParser(callbacks) {
346
- if (typeof callbacks == "function")
347
- throw new TypeError(
348
- "`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
349
- );
350
- const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
351
- let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
352
- function feed(newChunk) {
353
- const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
354
- for (const line of complete)
355
- parseLine(line);
356
- incompleteLine = incomplete, isFirstChunk = false;
42
+ function buildProviderMetadata(response) {
43
+ if (!response) {
44
+ return void 0;
45
+ }
46
+ const usage = response.usage;
47
+ const usageMetadata = usage ? filterUndefined({
48
+ promptTokens: usage.promptTokens,
49
+ completionTokens: usage.completionTokens,
50
+ totalTokens: usage.totalTokens,
51
+ ...usage.promptTokensDetails && {
52
+ promptTokensDetails: filterUndefined({
53
+ cachedTokens: usage.promptTokensDetails.cachedTokens ?? void 0,
54
+ cacheWriteTokens: usage.promptTokensDetails.cacheWriteTokens ?? void 0,
55
+ audioTokens: usage.promptTokensDetails.audioTokens ?? void 0,
56
+ videoTokens: usage.promptTokensDetails.videoTokens ?? void 0
57
+ })
58
+ },
59
+ ...usage.completionTokensDetails && {
60
+ completionTokensDetails: filterUndefined({
61
+ reasoningTokens: usage.completionTokensDetails.reasoningTokens ?? void 0,
62
+ imageTokens: usage.completionTokensDetails.imageTokens ?? void 0
63
+ })
64
+ },
65
+ cost: usage.cost,
66
+ isByok: usage.isByok,
67
+ costDetails: usage.costDetails
68
+ }) : void 0;
69
+ const metadata = filterUndefined({
70
+ responseId: response.id,
71
+ provider: response.provider,
72
+ usage: usageMetadata
73
+ });
74
+ return {
75
+ openrouter: metadata
76
+ };
77
+ }
78
+
79
+ // src/utils/build-usage.ts
80
+ function buildUsage(usage) {
81
+ if (!usage) {
82
+ return {
83
+ inputTokens: {
84
+ total: 0,
85
+ noCache: void 0,
86
+ cacheRead: void 0,
87
+ cacheWrite: void 0
88
+ },
89
+ outputTokens: {
90
+ total: 0,
91
+ text: void 0,
92
+ reasoning: void 0
93
+ },
94
+ raw: void 0
95
+ };
357
96
  }
358
- function parseLine(line) {
359
- if (line === "") {
360
- dispatchEvent();
361
- return;
362
- }
363
- if (line.startsWith(":")) {
364
- onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
365
- return;
97
+ const rawUsage = {
98
+ inputTokens: usage.inputTokens ?? null,
99
+ outputTokens: usage.outputTokens ?? null,
100
+ ...usage.inputTokensDetails && {
101
+ inputTokensDetails: {
102
+ cachedTokens: usage.inputTokensDetails.cachedTokens ?? null
103
+ }
104
+ },
105
+ ...usage.outputTokensDetails && {
106
+ outputTokensDetails: {
107
+ reasoningTokens: usage.outputTokensDetails.reasoningTokens ?? null
108
+ }
366
109
  }
367
- const fieldSeparatorIndex = line.indexOf(":");
368
- if (fieldSeparatorIndex !== -1) {
369
- const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
370
- processField(field, value, line);
371
- return;
110
+ };
111
+ return {
112
+ inputTokens: {
113
+ total: usage.inputTokens ?? 0,
114
+ noCache: void 0,
115
+ cacheRead: usage.inputTokensDetails?.cachedTokens,
116
+ cacheWrite: void 0
117
+ },
118
+ outputTokens: {
119
+ total: usage.outputTokens ?? 0,
120
+ text: void 0,
121
+ reasoning: usage.outputTokensDetails?.reasoningTokens
122
+ },
123
+ raw: rawUsage
124
+ };
125
+ }
126
+
127
+ // src/chat/convert-to-openrouter-messages.ts
128
+ function convertToOpenRouterMessages(prompt) {
129
+ const result = [];
130
+ for (const message of prompt) {
131
+ const converted = convertMessage(message);
132
+ result.push(...converted);
133
+ }
134
+ return result;
135
+ }
136
+ function convertMessage(message) {
137
+ const messageWithOptions = message;
138
+ const providerOptions = messageWithOptions.providerOptions;
139
+ const providerMetadata = messageWithOptions.providerMetadata;
140
+ switch (message.role) {
141
+ case "system":
142
+ return [{ role: "system", content: message.content }];
143
+ case "user":
144
+ return [convertUserMessage(message.content)];
145
+ case "assistant":
146
+ return convertAssistantMessage(message.content, providerMetadata, providerOptions);
147
+ case "tool":
148
+ return convertToolMessage(message.content);
149
+ default: {
150
+ const _exhaustive = message;
151
+ throw new Error(`Unknown message role: ${_exhaustive.role}`);
372
152
  }
373
- processField(line, "", line);
374
153
  }
375
- function processField(field, value, line) {
376
- switch (field) {
377
- case "event":
378
- eventType = value;
379
- break;
380
- case "data":
381
- data = `${data}${value}
382
- `;
383
- break;
384
- case "id":
385
- id = value.includes("\0") ? void 0 : value;
386
- break;
387
- case "retry":
388
- /^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
389
- new ParseError(`Invalid \`retry\` value: "${value}"`, {
390
- type: "invalid-retry",
391
- value,
392
- line
393
- })
394
- );
154
+ }
155
+ function convertUserMessage(content) {
156
+ const convertedContent = [];
157
+ for (const part of content) {
158
+ switch (part.type) {
159
+ case "text": {
160
+ convertedContent.push({ type: "input_text", text: part.text });
395
161
  break;
396
- default:
397
- onError(
398
- new ParseError(
399
- `Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
400
- { type: "unknown-field", field, value, line }
401
- )
402
- );
162
+ }
163
+ case "file":
164
+ convertedContent.push(convertFilePart(part));
403
165
  break;
166
+ default: {
167
+ const _exhaustive = part;
168
+ throw new Error(`Unknown user content type: ${_exhaustive.type}`);
169
+ }
404
170
  }
405
171
  }
406
- function dispatchEvent() {
407
- data.length > 0 && onEvent({
408
- id,
409
- event: eventType || void 0,
410
- // If the data buffer's last character is a U+000A LINE FEED (LF) character,
411
- // then remove the last character from the data buffer.
412
- data: data.endsWith(`
413
- `) ? data.slice(0, -1) : data
414
- }), id = void 0, data = "", eventType = "";
415
- }
416
- function reset(options = {}) {
417
- incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = true, id = void 0, data = "", eventType = "", incompleteLine = "";
418
- }
419
- return { feed, reset };
172
+ return { role: "user", content: convertedContent };
420
173
  }
421
- function splitLines(chunk) {
422
- const lines = [];
423
- let incompleteLine = "", searchIndex = 0;
424
- for (; searchIndex < chunk.length; ) {
425
- const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
426
- `, searchIndex);
427
- let lineEnd = -1;
428
- if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
429
- incompleteLine = chunk.slice(searchIndex);
430
- break;
431
- } else {
432
- const line = chunk.slice(searchIndex, lineEnd);
433
- lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
434
- ` && searchIndex++;
435
- }
174
+ function convertFilePart(part) {
175
+ const url = convertDataContent(part.data, part.mediaType);
176
+ if (part.mediaType.startsWith("image/")) {
177
+ return {
178
+ type: "input_image",
179
+ imageUrl: url,
180
+ detail: "auto"
181
+ };
436
182
  }
437
- return [lines, incompleteLine];
183
+ return {
184
+ type: "input_file",
185
+ fileUrl: url
186
+ };
438
187
  }
439
-
440
- // node_modules/.pnpm/eventsource-parser@3.0.3/node_modules/eventsource-parser/dist/stream.js
441
- var EventSourceParserStream = class extends TransformStream {
442
- constructor({ onError, onRetry, onComment } = {}) {
443
- let parser;
444
- super({
445
- start(controller) {
446
- parser = createParser({
447
- onEvent: (event) => {
448
- controller.enqueue(event);
449
- },
450
- onError(error) {
451
- onError === "terminate" ? controller.error(error) : typeof onError == "function" && onError(error);
452
- },
453
- onRetry,
454
- onComment
455
- });
456
- },
457
- transform(chunk) {
458
- parser.feed(chunk);
459
- }
460
- });
188
+ function convertDataContent(data, mediaType) {
189
+ if (data instanceof URL) {
190
+ return data.toString();
461
191
  }
462
- };
463
-
464
- // node_modules/.pnpm/@ai-sdk+provider-utils@3.0.1_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
465
- var z4 = __toESM(require("zod/v4"), 1);
466
-
467
- // node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/Options.js
468
- var ignoreOverride = Symbol("Let zodToJsonSchema decide on which parser to use");
469
-
470
- // node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/selectParser.js
471
- var import_zod3 = require("zod");
472
-
473
- // node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/array.js
474
- var import_zod = require("zod");
475
-
476
- // node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/record.js
477
- var import_zod2 = require("zod");
478
-
479
- // node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/string.js
480
- var ALPHA_NUMERIC = new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
481
-
482
- // node_modules/.pnpm/@ai-sdk+provider-utils@3.0.1_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
483
- function combineHeaders(...headers) {
484
- return headers.reduce(
485
- (combinedHeaders, currentHeaders) => __spreadValues(__spreadValues({}, combinedHeaders), currentHeaders != null ? currentHeaders : {}),
486
- {}
487
- );
192
+ if (data instanceof Uint8Array) {
193
+ const base64 = uint8ArrayToBase64(data);
194
+ return `data:${mediaType};base64,${base64}`;
195
+ }
196
+ if (data.startsWith("http://") || data.startsWith("https://") || data.startsWith("data:")) {
197
+ return data;
198
+ }
199
+ return `data:${mediaType};base64,${data}`;
488
200
  }
489
- function extractResponseHeaders(response) {
490
- return Object.fromEntries([...response.headers]);
201
+ function uint8ArrayToBase64(bytes) {
202
+ let binary = "";
203
+ for (const byte of bytes) {
204
+ binary += String.fromCharCode(byte);
205
+ }
206
+ return btoa(binary);
491
207
  }
492
- var createIdGenerator = ({
493
- prefix,
494
- size = 16,
495
- alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
496
- separator = "-"
497
- } = {}) => {
498
- const generator = () => {
499
- const alphabetLength = alphabet.length;
500
- const chars = new Array(size);
501
- for (let i = 0; i < size; i++) {
502
- chars[i] = alphabet[Math.random() * alphabetLength | 0];
208
+ function extractReasoningDetails(content, providerMetadata, providerOptions) {
209
+ const messageLevel = providerOptions?.openrouter?.reasoning_details ?? providerMetadata?.openrouter?.reasoning_details;
210
+ if (messageLevel && Array.isArray(messageLevel) && messageLevel.length > 0) {
211
+ return messageLevel;
212
+ }
213
+ for (const part of content) {
214
+ if (part.type === "reasoning") {
215
+ const partWithMeta = part;
216
+ const partLevel = partWithMeta.providerOptions?.openrouter?.reasoning_details ?? partWithMeta.providerMetadata?.openrouter?.reasoning_details;
217
+ if (partLevel && Array.isArray(partLevel) && partLevel.length > 0) {
218
+ return partLevel;
219
+ }
503
220
  }
504
- return chars.join("");
505
- };
506
- if (prefix == null) {
507
- return generator;
508
221
  }
509
- if (alphabet.includes(separator)) {
510
- throw new InvalidArgumentError({
511
- argument: "separator",
512
- message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
513
- });
514
- }
515
- return () => `${prefix}${separator}${generator()}`;
516
- };
517
- var generateId = createIdGenerator();
518
- function isAbortError(error) {
519
- return (error instanceof Error || error instanceof DOMException) && (error.name === "AbortError" || error.name === "ResponseAborted" || // Next.js
520
- error.name === "TimeoutError");
222
+ return void 0;
521
223
  }
522
- var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
523
- function handleFetchError({
524
- error,
525
- url,
526
- requestBodyValues
527
- }) {
528
- if (isAbortError(error)) {
529
- return error;
530
- }
531
- if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
532
- const cause = error.cause;
533
- if (cause != null) {
534
- return new APICallError({
535
- message: `Cannot connect to API: ${cause.message}`,
536
- cause,
537
- url,
538
- requestBodyValues,
539
- isRetryable: true
540
- // retry when network error
224
+ function transformReasoningToApiFormat(sdkItems) {
225
+ const apiItems = [];
226
+ for (const rawItem of sdkItems) {
227
+ if (typeof rawItem !== "object" || rawItem === null) {
228
+ continue;
229
+ }
230
+ const item = rawItem;
231
+ const baseProps = {
232
+ id: item.id,
233
+ format: item.format ?? null
234
+ };
235
+ let index = item.index ?? 0;
236
+ if (item.type === "reasoning.text" && item.text !== void 0) {
237
+ apiItems.push({
238
+ type: "reasoning.text",
239
+ text: item.text,
240
+ signature: item.signature ?? null,
241
+ index,
242
+ ...baseProps
243
+ });
244
+ continue;
245
+ }
246
+ if (item.type === "reasoning.summary" && item.summary !== void 0) {
247
+ apiItems.push({
248
+ type: "reasoning.summary",
249
+ summary: typeof item.summary === "string" ? item.summary : "",
250
+ index,
251
+ ...baseProps
541
252
  });
253
+ continue;
254
+ }
255
+ if (item.type === "reasoning.encrypted" && item.data !== void 0) {
256
+ apiItems.push({
257
+ type: "reasoning.encrypted",
258
+ data: item.data,
259
+ index,
260
+ ...baseProps
261
+ });
262
+ continue;
263
+ }
264
+ if (item.type === "reasoning" || item.content || item.summary || item.encryptedContent) {
265
+ if (item.content && Array.isArray(item.content)) {
266
+ for (const contentItem of item.content) {
267
+ if (contentItem.type === "reasoning_text" && contentItem.text) {
268
+ apiItems.push({
269
+ type: "reasoning.text",
270
+ text: contentItem.text,
271
+ signature: item.signature ?? null,
272
+ index: index++,
273
+ ...baseProps
274
+ });
275
+ }
276
+ }
277
+ }
278
+ if (item.summary && Array.isArray(item.summary)) {
279
+ for (const summaryItem of item.summary) {
280
+ if (summaryItem.type === "summary_text" && summaryItem.text) {
281
+ apiItems.push({
282
+ type: "reasoning.summary",
283
+ summary: summaryItem.text,
284
+ index: index++,
285
+ ...baseProps
286
+ });
287
+ }
288
+ }
289
+ }
290
+ if (item.encryptedContent) {
291
+ apiItems.push({
292
+ type: "reasoning.encrypted",
293
+ data: item.encryptedContent,
294
+ index: index++,
295
+ ...baseProps
296
+ });
297
+ }
542
298
  }
543
299
  }
544
- return error;
545
- }
546
- function removeUndefinedEntries(record) {
547
- return Object.fromEntries(
548
- Object.entries(record).filter(([_key, value]) => value != null)
549
- );
300
+ return apiItems;
550
301
  }
551
- function loadApiKey({
552
- apiKey,
553
- environmentVariableName,
554
- apiKeyParameterName = "apiKey",
555
- description
556
- }) {
557
- if (typeof apiKey === "string") {
558
- return apiKey;
302
+ function buildReasoningFromDetails(items) {
303
+ if (items.length === 0) {
304
+ return void 0;
559
305
  }
560
- if (apiKey != null) {
561
- throw new LoadAPIKeyError({
562
- message: `${description} API key must be a string.`
563
- });
564
- }
565
- if (typeof process === "undefined") {
566
- throw new LoadAPIKeyError({
567
- message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
568
- });
569
- }
570
- apiKey = process.env[environmentVariableName];
571
- if (apiKey == null) {
572
- throw new LoadAPIKeyError({
573
- message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
574
- });
306
+ const reasoning = {};
307
+ const textItems = items.filter((i) => i.type === "reasoning.text" && i.text);
308
+ if (textItems.length > 0) {
309
+ reasoning.text = textItems.map((i) => i.text).join("");
575
310
  }
576
- if (typeof apiKey !== "string") {
577
- throw new LoadAPIKeyError({
578
- message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
579
- });
311
+ const summaryItems = items.filter(
312
+ (i) => i.type === "reasoning.summary" && i.summary
313
+ );
314
+ if (summaryItems.length > 0) {
315
+ reasoning.summary = summaryItems.map((i) => i.summary).join("");
580
316
  }
581
- return apiKey;
582
- }
583
- var suspectProtoRx = /"__proto__"\s*:/;
584
- var suspectConstructorRx = /"constructor"\s*:/;
585
- function _parse(text) {
586
- const obj = JSON.parse(text);
587
- if (obj === null || typeof obj !== "object") {
588
- return obj;
317
+ const encryptedItem = items.find(
318
+ (i) => i.type === "reasoning.encrypted" && i.data
319
+ );
320
+ if (encryptedItem?.data) {
321
+ reasoning.encrypted = encryptedItem.data;
589
322
  }
590
- if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
591
- return obj;
323
+ if (!reasoning.text && !reasoning.summary && !reasoning.encrypted) {
324
+ return void 0;
592
325
  }
593
- return filter(obj);
326
+ return reasoning;
594
327
  }
595
- function filter(obj) {
596
- let next = [obj];
597
- while (next.length) {
598
- const nodes = next;
599
- next = [];
600
- for (const node of nodes) {
601
- if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
602
- throw new SyntaxError("Object contains forbidden prototype property");
603
- }
604
- if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
605
- throw new SyntaxError("Object contains forbidden prototype property");
606
- }
607
- for (const key in node) {
608
- const value = node[key];
609
- if (value && typeof value === "object") {
610
- next.push(value);
611
- }
328
+ function convertAssistantMessage(content, providerMetadata, providerOptions) {
329
+ const result = [];
330
+ let textContent = "";
331
+ const sdkReasoningDetails = extractReasoningDetails(
332
+ content,
333
+ providerMetadata,
334
+ providerOptions
335
+ );
336
+ const reasoningItems = sdkReasoningDetails ? transformReasoningToApiFormat(sdkReasoningDetails) : [];
337
+ const reasoning = buildReasoningFromDetails(reasoningItems);
338
+ for (const part of content) {
339
+ switch (part.type) {
340
+ case "text":
341
+ textContent += part.text;
342
+ break;
343
+ case "reasoning":
344
+ textContent += part.text;
345
+ break;
346
+ case "tool-call":
347
+ result.push({
348
+ type: "function_call",
349
+ callId: part.toolCallId,
350
+ name: part.toolName,
351
+ arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input)
352
+ });
353
+ break;
354
+ case "file":
355
+ break;
356
+ case "tool-result":
357
+ result.push(convertToolResult(part));
358
+ break;
359
+ default: {
360
+ const _exhaustive = part;
361
+ throw new Error(
362
+ `Unknown assistant content type: ${_exhaustive.type}`
363
+ );
612
364
  }
613
365
  }
614
366
  }
615
- return obj;
616
- }
617
- function secureJsonParse(text) {
618
- const { stackTraceLimit } = Error;
619
- Error.stackTraceLimit = 0;
620
- try {
621
- return _parse(text);
622
- } finally {
623
- Error.stackTraceLimit = stackTraceLimit;
624
- }
625
- }
626
- var validatorSymbol = Symbol.for("vercel.ai.validator");
627
- function validator(validate) {
628
- return { [validatorSymbol]: true, validate };
629
- }
630
- function isValidator(value) {
631
- return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
632
- }
633
- function asValidator(value) {
634
- return isValidator(value) ? value : standardSchemaValidator(value);
635
- }
636
- function standardSchemaValidator(standardSchema) {
637
- return validator(async (value) => {
638
- const result = await standardSchema["~standard"].validate(value);
639
- return result.issues == null ? { success: true, value: result.value } : {
640
- success: false,
641
- error: new TypeValidationError({
642
- value,
643
- cause: result.issues
644
- })
367
+ if (textContent) {
368
+ const assistantMessage = {
369
+ role: "assistant",
370
+ content: textContent
645
371
  };
646
- });
647
- }
648
- async function validateTypes({
649
- value,
650
- schema
651
- }) {
652
- const result = await safeValidateTypes({ value, schema });
653
- if (!result.success) {
654
- throw TypeValidationError.wrap({ value, cause: result.error });
372
+ if (reasoning) {
373
+ assistantMessage.reasoning = reasoning;
374
+ }
375
+ result.unshift(assistantMessage);
376
+ } else if (reasoning) {
377
+ const assistantMessage = {
378
+ role: "assistant",
379
+ content: ""
380
+ };
381
+ assistantMessage.reasoning = reasoning;
382
+ result.unshift(assistantMessage);
655
383
  }
656
- return result.value;
384
+ return result;
657
385
  }
658
- async function safeValidateTypes({
659
- value,
660
- schema
661
- }) {
662
- const validator2 = asValidator(schema);
663
- try {
664
- if (validator2.validate == null) {
665
- return { success: true, value, rawValue: value };
386
+ function convertToolMessage(content) {
387
+ const result = [];
388
+ for (const part of content) {
389
+ if (part.type === "tool-result") {
390
+ result.push(convertToolResult(part));
666
391
  }
667
- const result = await validator2.validate(value);
668
- if (result.success) {
669
- return { success: true, value: result.value, rawValue: value };
670
- }
671
- return {
672
- success: false,
673
- error: TypeValidationError.wrap({ value, cause: result.error }),
674
- rawValue: value
675
- };
676
- } catch (error) {
677
- return {
678
- success: false,
679
- error: TypeValidationError.wrap({ value, cause: error }),
680
- rawValue: value
681
- };
682
392
  }
393
+ return result;
394
+ }
395
+ function convertToolResult(part) {
396
+ const output = convertToolResultOutput(part.output);
397
+ return {
398
+ type: "function_call_output",
399
+ callId: part.toolCallId,
400
+ output: output.value,
401
+ status: output.isError ? "incomplete" : "completed"
402
+ };
683
403
  }
684
- async function parseJSON({
685
- text,
686
- schema
687
- }) {
688
- try {
689
- const value = secureJsonParse(text);
690
- if (schema == null) {
691
- return value;
404
+ function convertToolResultOutput(output) {
405
+ switch (output.type) {
406
+ case "text":
407
+ return { value: output.value, isError: false };
408
+ case "json":
409
+ return { value: JSON.stringify(output.value), isError: false };
410
+ case "execution-denied":
411
+ return {
412
+ value: `Execution denied: ${output.reason ?? "No reason provided"}`,
413
+ isError: true
414
+ };
415
+ case "error-text":
416
+ return { value: output.value, isError: true };
417
+ case "error-json":
418
+ return { value: JSON.stringify(output.value), isError: true };
419
+ case "content": {
420
+ const textParts = output.value.filter((item) => item.type === "text").map((item) => item.text);
421
+ return { value: textParts.join("\n"), isError: false };
692
422
  }
693
- return validateTypes({ value, schema });
694
- } catch (error) {
695
- if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
696
- throw error;
423
+ default: {
424
+ const _exhaustive = output;
425
+ throw new Error(`Unknown tool result output type: ${_exhaustive.type}`);
697
426
  }
698
- throw new JSONParseError({ text, cause: error });
699
427
  }
700
428
  }
701
- async function safeParseJSON({
702
- text,
703
- schema
704
- }) {
705
- try {
706
- const value = secureJsonParse(text);
707
- if (schema == null) {
708
- return { success: true, value, rawValue: value };
429
+
430
+ // src/chat/extract-reasoning-details.ts
431
+ function extractReasoningDetails2(response) {
432
+ const extractedDetails = [];
433
+ for (const outputItem of response.output) {
434
+ if ("type" in outputItem && outputItem.type === "reasoning") {
435
+ const reasoningItem = outputItem;
436
+ extractedDetails.push({
437
+ type: "reasoning",
438
+ id: reasoningItem.id,
439
+ content: reasoningItem.content,
440
+ summary: reasoningItem.summary,
441
+ encryptedContent: reasoningItem.encryptedContent,
442
+ signature: reasoningItem.signature,
443
+ format: reasoningItem.format
444
+ });
709
445
  }
710
- return await safeValidateTypes({ value, schema });
711
- } catch (error) {
712
- return {
713
- success: false,
714
- error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error }),
715
- rawValue: void 0
716
- };
717
446
  }
447
+ return extractedDetails.length > 0 ? extractedDetails : void 0;
718
448
  }
719
- function isParsableJson(input) {
720
- try {
721
- secureJsonParse(input);
722
- return true;
723
- } catch (e) {
449
+ function hasEncryptedReasoning(reasoningDetails) {
450
+ if (!reasoningDetails) {
724
451
  return false;
725
452
  }
726
- }
727
- function parseJsonEventStream({
728
- stream,
729
- schema
730
- }) {
731
- return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
732
- new TransformStream({
733
- async transform({ data }, controller) {
734
- if (data === "[DONE]") {
735
- return;
736
- }
737
- controller.enqueue(await safeParseJSON({ text: data, schema }));
738
- }
739
- })
740
- );
741
- }
742
- var getOriginalFetch2 = () => globalThis.fetch;
743
- var postJsonToApi = async ({
744
- url,
745
- headers,
746
- body,
747
- failedResponseHandler,
748
- successfulResponseHandler,
749
- abortSignal,
750
- fetch
751
- }) => postToApi({
752
- url,
753
- headers: __spreadValues({
754
- "Content-Type": "application/json"
755
- }, headers),
756
- body: {
757
- content: JSON.stringify(body),
758
- values: body
759
- },
760
- failedResponseHandler,
761
- successfulResponseHandler,
762
- abortSignal,
763
- fetch
764
- });
765
- var postToApi = async ({
766
- url,
767
- headers = {},
768
- body,
769
- successfulResponseHandler,
770
- failedResponseHandler,
771
- abortSignal,
772
- fetch = getOriginalFetch2()
773
- }) => {
774
- try {
775
- const response = await fetch(url, {
776
- method: "POST",
777
- headers: removeUndefinedEntries(headers),
778
- body: body.content,
779
- signal: abortSignal
780
- });
781
- const responseHeaders = extractResponseHeaders(response);
782
- if (!response.ok) {
783
- let errorInformation;
784
- try {
785
- errorInformation = await failedResponseHandler({
786
- response,
787
- url,
788
- requestBodyValues: body.values
789
- });
790
- } catch (error) {
791
- if (isAbortError(error) || APICallError.isInstance(error)) {
792
- throw error;
793
- }
794
- throw new APICallError({
795
- message: "Failed to process error response",
796
- cause: error,
797
- statusCode: response.status,
798
- url,
799
- responseHeaders,
800
- requestBodyValues: body.values
801
- });
802
- }
803
- throw errorInformation.value;
804
- }
805
- try {
806
- return await successfulResponseHandler({
807
- response,
808
- url,
809
- requestBodyValues: body.values
810
- });
811
- } catch (error) {
812
- if (error instanceof Error) {
813
- if (isAbortError(error) || APICallError.isInstance(error)) {
814
- throw error;
815
- }
816
- }
817
- throw new APICallError({
818
- message: "Failed to process successful response",
819
- cause: error,
820
- statusCode: response.status,
821
- url,
822
- responseHeaders,
823
- requestBodyValues: body.values
824
- });
453
+ return reasoningDetails.some((d) => {
454
+ if (typeof d === "object" && d !== null) {
455
+ const obj = d;
456
+ return obj.encryptedContent != null || obj.type === "reasoning.encrypted";
825
457
  }
826
- } catch (error) {
827
- throw handleFetchError({ error, url, requestBodyValues: body.values });
828
- }
829
- };
830
- var createJsonErrorResponseHandler = ({
831
- errorSchema,
832
- errorToMessage,
833
- isRetryable
834
- }) => async ({ response, url, requestBodyValues }) => {
835
- const responseBody = await response.text();
836
- const responseHeaders = extractResponseHeaders(response);
837
- if (responseBody.trim() === "") {
838
- return {
839
- responseHeaders,
840
- value: new APICallError({
841
- message: response.statusText,
842
- url,
843
- requestBodyValues,
844
- statusCode: response.status,
845
- responseHeaders,
846
- responseBody,
847
- isRetryable: isRetryable == null ? void 0 : isRetryable(response)
848
- })
849
- };
850
- }
851
- try {
852
- const parsedError = await parseJSON({
853
- text: responseBody,
854
- schema: errorSchema
855
- });
856
- return {
857
- responseHeaders,
858
- value: new APICallError({
859
- message: errorToMessage(parsedError),
860
- url,
861
- requestBodyValues,
862
- statusCode: response.status,
863
- responseHeaders,
864
- responseBody,
865
- data: parsedError,
866
- isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
867
- })
868
- };
869
- } catch (parseError) {
870
- return {
871
- responseHeaders,
872
- value: new APICallError({
873
- message: response.statusText,
874
- url,
875
- requestBodyValues,
876
- statusCode: response.status,
877
- responseHeaders,
878
- responseBody,
879
- isRetryable: isRetryable == null ? void 0 : isRetryable(response)
880
- })
881
- };
882
- }
883
- };
884
- var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
885
- const responseHeaders = extractResponseHeaders(response);
886
- if (response.body == null) {
887
- throw new EmptyResponseBodyError({});
888
- }
889
- return {
890
- responseHeaders,
891
- value: parseJsonEventStream({
892
- stream: response.body,
893
- schema: chunkSchema
894
- })
895
- };
896
- };
897
- var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
898
- const responseBody = await response.text();
899
- const parsedResult = await safeParseJSON({
900
- text: responseBody,
901
- schema: responseSchema
458
+ return false;
902
459
  });
903
- const responseHeaders = extractResponseHeaders(response);
904
- if (!parsedResult.success) {
905
- throw new APICallError({
906
- message: "Invalid JSON response",
907
- cause: parsedResult.error,
908
- statusCode: response.status,
909
- responseHeaders,
910
- responseBody,
911
- url,
912
- requestBodyValues
913
- });
460
+ }
461
+ function buildReasoningProviderMetadata(reasoningDetails) {
462
+ if (!reasoningDetails || reasoningDetails.length === 0) {
463
+ return void 0;
914
464
  }
915
465
  return {
916
- responseHeaders,
917
- value: parsedResult.value,
918
- rawValue: parsedResult.rawValue
466
+ openrouter: {
467
+ reasoning_details: reasoningDetails
468
+ }
919
469
  };
920
- };
921
- var schemaSymbol = Symbol.for("vercel.ai.schema");
922
- var { btoa, atob } = globalThis;
923
- function convertUint8ArrayToBase64(array) {
924
- let latin1string = "";
925
- for (let i = 0; i < array.length; i++) {
926
- latin1string += String.fromCodePoint(array[i]);
927
- }
928
- return btoa(latin1string);
929
- }
930
- function withoutTrailingSlash(url) {
931
- return url == null ? void 0 : url.replace(/\/$/, "");
932
- }
933
-
934
- // src/schemas/reasoning-details.ts
935
- var import_v4 = require("zod/v4");
936
-
937
- // src/utils/type-guards.ts
938
- function isDefinedOrNotNull(value) {
939
- return value !== null && value !== void 0;
940
470
  }
941
471
 
942
- // src/schemas/format.ts
943
- var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
944
- ReasoningFormat2["Unknown"] = "unknown";
945
- ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
946
- ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
947
- ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
948
- ReasoningFormat2["GoogleGeminiV1"] = "google-gemini-v1";
949
- return ReasoningFormat2;
950
- })(ReasoningFormat || {});
951
-
952
- // src/schemas/reasoning-details.ts
953
- var CommonReasoningDetailSchema = import_v4.z.object({
954
- id: import_v4.z.string().nullish(),
955
- format: import_v4.z.enum(ReasoningFormat).nullish(),
956
- index: import_v4.z.number().optional()
957
- }).loose();
958
- var ReasoningDetailSummarySchema = import_v4.z.object({
959
- type: import_v4.z.literal("reasoning.summary" /* Summary */),
960
- summary: import_v4.z.string()
961
- }).extend(CommonReasoningDetailSchema.shape);
962
- var ReasoningDetailEncryptedSchema = import_v4.z.object({
963
- type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
964
- data: import_v4.z.string()
965
- }).extend(CommonReasoningDetailSchema.shape);
966
- var ReasoningDetailTextSchema = import_v4.z.object({
967
- type: import_v4.z.literal("reasoning.text" /* Text */),
968
- text: import_v4.z.string().nullish(),
969
- signature: import_v4.z.string().nullish()
970
- }).extend(CommonReasoningDetailSchema.shape);
971
- var ReasoningDetailUnionSchema = import_v4.z.union([
972
- ReasoningDetailSummarySchema,
973
- ReasoningDetailEncryptedSchema,
974
- ReasoningDetailTextSchema
975
- ]);
976
- var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
977
- ReasoningDetailUnionSchema,
978
- import_v4.z.unknown().transform(() => null)
979
- ]);
980
- var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
981
- var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
982
- import_v4.z.object({
983
- delta: import_v4.z.object({
984
- reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
985
- })
986
- }).transform(
987
- (data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
988
- ),
989
- import_v4.z.object({
990
- message: import_v4.z.object({
991
- reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
992
- })
993
- }).transform(
994
- (data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
995
- ),
996
- import_v4.z.object({
997
- text: import_v4.z.string(),
998
- reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
999
- }).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
1000
- ]);
1001
-
1002
- // src/schemas/error-response.ts
1003
- var import_v42 = require("zod/v4");
1004
- var OpenRouterErrorResponseSchema = import_v42.z.object({
1005
- error: import_v42.z.object({
1006
- code: import_v42.z.union([import_v42.z.string(), import_v42.z.number()]).nullable().optional().default(null),
1007
- message: import_v42.z.string(),
1008
- type: import_v42.z.string().nullable().optional().default(null),
1009
- param: import_v42.z.any().nullable().optional().default(null)
1010
- }).passthrough()
1011
- }).passthrough();
1012
- var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
1013
- errorSchema: OpenRouterErrorResponseSchema,
1014
- errorToMessage: (data) => data.error.message
1015
- });
1016
-
1017
- // src/schemas/provider-metadata.ts
1018
- var import_v43 = require("zod/v4");
1019
- var FileAnnotationSchema = import_v43.z.object({
1020
- type: import_v43.z.literal("file"),
1021
- file: import_v43.z.object({
1022
- hash: import_v43.z.string(),
1023
- name: import_v43.z.string(),
1024
- content: import_v43.z.array(
1025
- import_v43.z.object({
1026
- type: import_v43.z.string(),
1027
- text: import_v43.z.string().optional()
1028
- }).catchall(import_v43.z.any())
1029
- ).optional()
1030
- }).catchall(import_v43.z.any())
1031
- }).catchall(import_v43.z.any());
1032
- var OpenRouterProviderMetadataSchema = import_v43.z.object({
1033
- provider: import_v43.z.string(),
1034
- reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
1035
- annotations: import_v43.z.array(FileAnnotationSchema).optional(),
1036
- usage: import_v43.z.object({
1037
- promptTokens: import_v43.z.number(),
1038
- promptTokensDetails: import_v43.z.object({
1039
- cachedTokens: import_v43.z.number()
1040
- }).catchall(import_v43.z.any()).optional(),
1041
- completionTokens: import_v43.z.number(),
1042
- completionTokensDetails: import_v43.z.object({
1043
- reasoningTokens: import_v43.z.number()
1044
- }).catchall(import_v43.z.any()).optional(),
1045
- totalTokens: import_v43.z.number(),
1046
- cost: import_v43.z.number().optional(),
1047
- costDetails: import_v43.z.object({
1048
- upstreamInferenceCost: import_v43.z.number()
1049
- }).catchall(import_v43.z.any()).optional()
1050
- }).catchall(import_v43.z.any())
1051
- }).catchall(import_v43.z.any());
1052
- var OpenRouterProviderOptionsSchema = import_v43.z.object({
1053
- openrouter: import_v43.z.object({
1054
- reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
1055
- annotations: import_v43.z.array(FileAnnotationSchema).optional()
1056
- }).optional()
1057
- }).optional();
1058
-
1059
- // src/utils/map-finish-reason.ts
472
+ // src/chat/map-openrouter-finish-reason.ts
1060
473
  function mapOpenRouterFinishReason(finishReason) {
474
+ if (finishReason == null) {
475
+ return { unified: "other", raw: void 0 };
476
+ }
1061
477
  switch (finishReason) {
478
+ case "end_turn":
1062
479
  case "stop":
1063
- return "stop";
480
+ case "stop_sequence":
481
+ return { unified: "stop", raw: finishReason };
482
+ case "max_tokens":
1064
483
  case "length":
1065
- return "length";
1066
- case "content_filter":
1067
- return "content-filter";
1068
- case "function_call":
484
+ return { unified: "length", raw: finishReason };
485
+ case "tool_use":
1069
486
  case "tool_calls":
1070
- return "tool-calls";
487
+ return { unified: "tool-calls", raw: finishReason };
488
+ case "content_filter":
489
+ return { unified: "content-filter", raw: finishReason };
490
+ case "error":
491
+ return { unified: "error", raw: finishReason };
1071
492
  default:
1072
- return "unknown";
1073
- }
1074
- }
1075
-
1076
- // src/types/openrouter-chat-completions-input.ts
1077
- var OPENROUTER_AUDIO_FORMATS = [
1078
- "wav",
1079
- "mp3",
1080
- "aiff",
1081
- "aac",
1082
- "ogg",
1083
- "flac",
1084
- "m4a",
1085
- "pcm16",
1086
- "pcm24"
1087
- ];
1088
-
1089
- // src/chat/is-url.ts
1090
- function isUrl({
1091
- url,
1092
- protocols
1093
- }) {
1094
- try {
1095
- const urlObj = new URL(url);
1096
- return protocols.has(urlObj.protocol);
1097
- } catch (_) {
1098
- return false;
493
+ return { unified: "other", raw: finishReason };
1099
494
  }
1100
495
  }
1101
496
 
1102
- // src/chat/file-url-utils.ts
1103
- function getFileUrl({
1104
- part,
1105
- defaultMediaType
1106
- }) {
1107
- var _a15, _b;
1108
- if (part.data instanceof Uint8Array) {
1109
- const base64 = convertUint8ArrayToBase64(part.data);
1110
- return `data:${(_a15 = part.mediaType) != null ? _a15 : defaultMediaType};base64,${base64}`;
1111
- }
1112
- const stringUrl = part.data.toString();
1113
- if (isUrl({
1114
- url: stringUrl,
1115
- protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1116
- })) {
1117
- return stringUrl;
497
+ // src/chat/openrouter-chat-language-model.ts
498
+ var OpenRouterChatLanguageModel = class {
499
+ specificationVersion = "v3";
500
+ provider = "openrouter";
501
+ modelId;
502
+ settings;
503
+ /**
504
+ * Supported URL patterns by media type.
505
+ * OpenRouter Chat API only supports image URLs natively.
506
+ * PDF URLs are not supported - use PDF data URIs or the Responses API instead.
507
+ */
508
+ supportedUrls = {
509
+ "image/*": [/^https?:\/\/.*$/]
510
+ };
511
+ constructor(modelId, settings) {
512
+ this.modelId = modelId;
513
+ this.settings = settings;
1118
514
  }
1119
- return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
1120
- }
1121
- function getMediaType(dataUrl, defaultMediaType) {
1122
- var _a15;
1123
- const match = dataUrl.match(/^data:([^;]+)/);
1124
- return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
1125
- }
1126
- function getBase64FromDataUrl(dataUrl) {
1127
- const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
1128
- return match ? match[1] : dataUrl;
1129
- }
1130
- var MIME_TO_FORMAT = {
1131
- // MP3 variants
1132
- mpeg: "mp3",
1133
- mp3: "mp3",
1134
- // WAV variants
1135
- "x-wav": "wav",
1136
- wave: "wav",
1137
- wav: "wav",
1138
- // OGG variants
1139
- ogg: "ogg",
1140
- vorbis: "ogg",
1141
- // AAC variants
1142
- aac: "aac",
1143
- "x-aac": "aac",
1144
- // M4A variants
1145
- m4a: "m4a",
1146
- "x-m4a": "m4a",
1147
- mp4: "m4a",
1148
- // AIFF variants
1149
- aiff: "aiff",
1150
- "x-aiff": "aiff",
1151
- // FLAC
1152
- flac: "flac",
1153
- "x-flac": "flac",
1154
- // PCM variants
1155
- pcm16: "pcm16",
1156
- pcm24: "pcm24"
1157
- };
1158
- function getInputAudioData(part) {
1159
- const fileData = getFileUrl({
1160
- part,
1161
- defaultMediaType: "audio/mpeg"
1162
- });
1163
- if (isUrl({
1164
- url: fileData,
1165
- protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1166
- })) {
1167
- throw new Error(
1168
- `Audio files cannot be provided as URLs.
1169
-
1170
- OpenRouter requires audio to be base64-encoded. Please:
1171
- 1. Download the audio file locally
1172
- 2. Read it as a Buffer or Uint8Array
1173
- 3. Pass it as the data parameter
1174
-
1175
- The AI SDK will automatically handle base64 encoding.
1176
-
1177
- Learn more: https://openrouter.ai/docs/features/multimodal/audio`
1178
- );
1179
- }
1180
- const data = getBase64FromDataUrl(fileData);
1181
- const mediaType = part.mediaType || "audio/mpeg";
1182
- const rawFormat = mediaType.replace("audio/", "");
1183
- const format = MIME_TO_FORMAT[rawFormat];
1184
- if (format === void 0) {
1185
- const supportedList = OPENROUTER_AUDIO_FORMATS.join(", ");
1186
- throw new Error(
1187
- `Unsupported audio format: "${mediaType}"
1188
-
1189
- OpenRouter supports the following audio formats: ${supportedList}
1190
-
1191
- Learn more: https://openrouter.ai/docs/features/multimodal/audio`
515
+ async doGenerate(options) {
516
+ const warnings = [];
517
+ const client = new import_sdk.OpenRouter({
518
+ apiKey: this.settings.apiKey,
519
+ serverURL: this.settings.baseURL
520
+ });
521
+ const openRouterInput = convertToOpenRouterMessages(options.prompt);
522
+ const tools = convertToolsToResponsesFormat(options.tools, warnings);
523
+ const toolChoice = convertToolChoiceToResponsesFormat(options.toolChoice);
524
+ const text = convertResponseFormatToText(options.responseFormat);
525
+ const requestParams = {
526
+ model: this.modelId,
527
+ input: openRouterInput,
528
+ stream: false,
529
+ ...options.maxOutputTokens !== void 0 && {
530
+ maxOutputTokens: options.maxOutputTokens
531
+ },
532
+ ...options.temperature !== void 0 && {
533
+ temperature: options.temperature
534
+ },
535
+ ...options.topP !== void 0 && { topP: options.topP },
536
+ ...tools.length > 0 && { tools },
537
+ ...toolChoice !== void 0 && { toolChoice },
538
+ ...text !== void 0 && { text }
539
+ };
540
+ const combinedHeaders = (0, import_provider_utils.normalizeHeaders)(
541
+ (0, import_provider_utils.combineHeaders)(this.settings.headers, options.headers)
1192
542
  );
1193
- }
1194
- return { data, format };
1195
- }
1196
-
1197
- // src/chat/convert-to-openrouter-chat-messages.ts
1198
- function getCacheControl(providerMetadata) {
1199
- var _a15, _b, _c;
1200
- const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
1201
- const openrouter2 = providerMetadata == null ? void 0 : providerMetadata.openrouter;
1202
- return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
1203
- }
1204
- function convertToOpenRouterChatMessages(prompt) {
1205
- var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1206
- const messages = [];
1207
- for (const { role, content, providerOptions } of prompt) {
1208
- switch (role) {
1209
- case "system": {
1210
- messages.push({
1211
- role: "system",
1212
- content,
1213
- cache_control: getCacheControl(providerOptions)
1214
- });
1215
- break;
543
+ const response = await client.beta.responses.send(requestParams, {
544
+ fetchOptions: {
545
+ signal: options.abortSignal,
546
+ headers: combinedHeaders
1216
547
  }
1217
- case "user": {
1218
- if (content.length === 1 && ((_a15 = content[0]) == null ? void 0 : _a15.type) === "text") {
1219
- const cacheControl = (_b = getCacheControl(providerOptions)) != null ? _b : getCacheControl(content[0].providerOptions);
1220
- const contentWithCacheControl = cacheControl ? [
1221
- {
1222
- type: "text",
1223
- text: content[0].text,
1224
- cache_control: cacheControl
1225
- }
1226
- ] : content[0].text;
1227
- messages.push({
1228
- role: "user",
1229
- content: contentWithCacheControl
548
+ });
549
+ const content = [];
550
+ const reasoningDetails = extractReasoningDetails2(response);
551
+ const reasoningMetadata = buildReasoningProviderMetadata(reasoningDetails);
552
+ for (const outputItem of response.output) {
553
+ if (outputItem.type === "reasoning") {
554
+ const reasoningItem = outputItem;
555
+ const reasoningText = reasoningItem.content?.filter((c) => c.type === "reasoning_text").map((c) => c.text).join("") || reasoningItem.summary?.filter((c) => c.type === "summary_text").map((c) => c.text).join("") || "";
556
+ if (reasoningText) {
557
+ content.push({
558
+ type: "reasoning",
559
+ text: reasoningText,
560
+ ...reasoningMetadata && { providerMetadata: reasoningMetadata }
1230
561
  });
1231
- break;
1232
562
  }
1233
- const messageCacheControl = getCacheControl(providerOptions);
1234
- const contentParts = content.map(
1235
- (part) => {
1236
- var _a16, _b2, _c2, _d2, _e2, _f2, _g2;
1237
- const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1238
- switch (part.type) {
1239
- case "text":
1240
- return {
1241
- type: "text",
1242
- text: part.text,
1243
- // For text parts, only use part-specific cache control
1244
- cache_control: cacheControl
1245
- };
1246
- case "file": {
1247
- if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
1248
- const url = getFileUrl({
1249
- part,
1250
- defaultMediaType: "image/jpeg"
1251
- });
1252
- return {
1253
- type: "image_url",
1254
- image_url: {
1255
- url
1256
- },
1257
- // For image parts, use part-specific or message-level cache control
1258
- cache_control: cacheControl
1259
- };
1260
- }
1261
- if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
1262
- return {
1263
- type: "input_audio",
1264
- input_audio: getInputAudioData(part),
1265
- cache_control: cacheControl
1266
- };
1267
- }
1268
- const fileName = String(
1269
- (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
1270
- );
1271
- const fileData = getFileUrl({
1272
- part,
1273
- defaultMediaType: "application/pdf"
1274
- });
1275
- if (isUrl({
1276
- url: fileData,
1277
- protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1278
- })) {
1279
- return {
1280
- type: "file",
1281
- file: {
1282
- filename: fileName,
1283
- file_data: fileData
1284
- }
1285
- };
1286
- }
1287
- return {
1288
- type: "file",
1289
- file: {
1290
- filename: fileName,
1291
- file_data: fileData
1292
- },
1293
- cache_control: cacheControl
1294
- };
1295
- }
1296
- default: {
1297
- return {
1298
- type: "text",
1299
- text: "",
1300
- cache_control: cacheControl
1301
- };
1302
- }
1303
- }
1304
- }
1305
- );
1306
- messages.push({
1307
- role: "user",
1308
- content: contentParts
1309
- });
1310
- break;
1311
- }
1312
- case "assistant": {
1313
- let text = "";
1314
- let reasoning = "";
1315
- const toolCalls = [];
1316
- const accumulatedReasoningDetails = [];
1317
- for (const part of content) {
1318
- switch (part.type) {
1319
- case "text": {
1320
- text += part.text;
1321
- break;
1322
- }
1323
- case "tool-call": {
1324
- const partReasoningDetails = (_c = part.providerOptions) == null ? void 0 : _c.openrouter;
1325
- if ((partReasoningDetails == null ? void 0 : partReasoningDetails.reasoning_details) && Array.isArray(partReasoningDetails.reasoning_details)) {
1326
- accumulatedReasoningDetails.push(
1327
- ...partReasoningDetails.reasoning_details
1328
- );
1329
- }
1330
- toolCalls.push({
1331
- id: part.toolCallId,
1332
- type: "function",
1333
- function: {
1334
- name: part.toolName,
1335
- arguments: JSON.stringify(part.input)
1336
- }
1337
- });
1338
- break;
1339
- }
1340
- case "reasoning": {
1341
- reasoning += part.text;
1342
- const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
1343
- if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
1344
- accumulatedReasoningDetails.push(
1345
- ...parsedPartProviderOptions.data.openrouter.reasoning_details
1346
- );
1347
- }
1348
- break;
1349
- }
1350
- case "file":
1351
- break;
1352
- default: {
1353
- break;
1354
- }
563
+ } else if (outputItem.type === "message") {
564
+ const messageItem = outputItem;
565
+ for (const contentItem of messageItem.content) {
566
+ if (contentItem.type === "output_text" && contentItem.text) {
567
+ content.push({
568
+ type: "text",
569
+ text: contentItem.text
570
+ });
1355
571
  }
1356
572
  }
1357
- const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1358
- const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
1359
- const messageAnnotations = parsedProviderOptions.success ? (_i = (_h = parsedProviderOptions.data) == null ? void 0 : _h.openrouter) == null ? void 0 : _i.annotations : void 0;
1360
- const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
1361
- messages.push({
1362
- role: "assistant",
1363
- content: text,
1364
- tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
1365
- reasoning: reasoning || void 0,
1366
- reasoning_details: finalReasoningDetails,
1367
- annotations: messageAnnotations,
1368
- cache_control: getCacheControl(providerOptions)
573
+ } else if (outputItem.type === "function_call") {
574
+ const functionCallItem = outputItem;
575
+ content.push({
576
+ type: "tool-call",
577
+ toolCallId: functionCallItem.callId,
578
+ toolName: functionCallItem.name,
579
+ // Default to empty object when arguments is undefined/empty
580
+ // (some providers omit arguments for tools with no parameters)
581
+ input: functionCallItem.arguments || "{}",
582
+ ...reasoningMetadata && { providerMetadata: reasoningMetadata }
1369
583
  });
1370
- break;
1371
584
  }
1372
- case "tool": {
1373
- for (const toolResponse of content) {
1374
- const content2 = getToolResultContent(toolResponse);
1375
- messages.push({
1376
- role: "tool",
1377
- tool_call_id: toolResponse.toolCallId,
1378
- content: content2,
1379
- cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
1380
- });
1381
- }
1382
- break;
1383
- }
1384
- default: {
1385
- break;
1386
- }
1387
- }
1388
- }
1389
- return messages;
1390
- }
1391
- function getToolResultContent(input) {
1392
- return input.output.type === "text" ? input.output.value : JSON.stringify(input.output.value);
1393
- }
1394
-
1395
- // src/chat/get-tool-choice.ts
1396
- var import_v44 = require("zod/v4");
1397
- var ChatCompletionToolChoiceSchema = import_v44.z.union([
1398
- import_v44.z.literal("auto"),
1399
- import_v44.z.literal("none"),
1400
- import_v44.z.literal("required"),
1401
- import_v44.z.object({
1402
- type: import_v44.z.literal("function"),
1403
- function: import_v44.z.object({
1404
- name: import_v44.z.string()
1405
- })
1406
- })
1407
- ]);
1408
- function getChatCompletionToolChoice(toolChoice) {
1409
- switch (toolChoice.type) {
1410
- case "auto":
1411
- case "none":
1412
- case "required":
1413
- return toolChoice.type;
1414
- case "tool": {
1415
- return {
1416
- type: "function",
1417
- function: { name: toolChoice.toolName }
1418
- };
1419
- }
1420
- default: {
1421
- toolChoice;
1422
- throw new InvalidArgumentError({
1423
- argument: "toolChoice",
1424
- message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
1425
- });
1426
585
  }
1427
- }
1428
- }
1429
-
1430
- // src/chat/schemas.ts
1431
- var import_v46 = require("zod/v4");
1432
-
1433
- // src/schemas/image.ts
1434
- var import_v45 = require("zod/v4");
1435
- var ImageResponseSchema = import_v45.z.object({
1436
- type: import_v45.z.literal("image_url"),
1437
- image_url: import_v45.z.object({
1438
- url: import_v45.z.string()
1439
- }).passthrough()
1440
- }).passthrough();
1441
- var ImageResponseWithUnknownSchema = import_v45.z.union([
1442
- ImageResponseSchema,
1443
- import_v45.z.unknown().transform(() => null)
1444
- ]);
1445
- var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1446
-
1447
- // src/chat/schemas.ts
1448
- var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
1449
- id: import_v46.z.string().optional(),
1450
- model: import_v46.z.string().optional(),
1451
- provider: import_v46.z.string().optional(),
1452
- usage: import_v46.z.object({
1453
- prompt_tokens: import_v46.z.number(),
1454
- prompt_tokens_details: import_v46.z.object({
1455
- cached_tokens: import_v46.z.number()
1456
- }).passthrough().nullish(),
1457
- completion_tokens: import_v46.z.number(),
1458
- completion_tokens_details: import_v46.z.object({
1459
- reasoning_tokens: import_v46.z.number()
1460
- }).passthrough().nullish(),
1461
- total_tokens: import_v46.z.number(),
1462
- cost: import_v46.z.number().optional(),
1463
- cost_details: import_v46.z.object({
1464
- upstream_inference_cost: import_v46.z.number().nullish()
1465
- }).passthrough().nullish()
1466
- }).passthrough().nullish()
1467
- }).passthrough();
1468
- var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
1469
- // Success response with choices
1470
- OpenRouterChatCompletionBaseResponseSchema.extend({
1471
- choices: import_v46.z.array(
1472
- import_v46.z.object({
1473
- message: import_v46.z.object({
1474
- role: import_v46.z.literal("assistant"),
1475
- content: import_v46.z.string().nullable().optional(),
1476
- reasoning: import_v46.z.string().nullable().optional(),
1477
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1478
- images: ImageResponseArraySchema.nullish(),
1479
- tool_calls: import_v46.z.array(
1480
- import_v46.z.object({
1481
- id: import_v46.z.string().optional().nullable(),
1482
- type: import_v46.z.literal("function"),
1483
- function: import_v46.z.object({
1484
- name: import_v46.z.string(),
1485
- arguments: import_v46.z.string()
1486
- }).passthrough()
1487
- }).passthrough()
1488
- ).optional(),
1489
- annotations: import_v46.z.array(
1490
- import_v46.z.union([
1491
- // URL citation from web search
1492
- import_v46.z.object({
1493
- type: import_v46.z.literal("url_citation"),
1494
- url_citation: import_v46.z.object({
1495
- end_index: import_v46.z.number(),
1496
- start_index: import_v46.z.number(),
1497
- title: import_v46.z.string(),
1498
- url: import_v46.z.string(),
1499
- content: import_v46.z.string().optional()
1500
- }).passthrough()
1501
- }).passthrough(),
1502
- // File annotation from FileParserPlugin (old format)
1503
- import_v46.z.object({
1504
- type: import_v46.z.literal("file_annotation"),
1505
- file_annotation: import_v46.z.object({
1506
- file_id: import_v46.z.string(),
1507
- quote: import_v46.z.string().optional()
1508
- }).passthrough()
1509
- }).passthrough(),
1510
- // File annotation from FileParserPlugin (new format)
1511
- import_v46.z.object({
1512
- type: import_v46.z.literal("file"),
1513
- file: import_v46.z.object({
1514
- hash: import_v46.z.string(),
1515
- name: import_v46.z.string(),
1516
- content: import_v46.z.array(
1517
- import_v46.z.object({
1518
- type: import_v46.z.string(),
1519
- text: import_v46.z.string().optional()
1520
- }).passthrough()
1521
- ).optional()
1522
- }).passthrough()
1523
- }).passthrough()
1524
- ])
1525
- ).nullish()
1526
- }).passthrough(),
1527
- index: import_v46.z.number().nullish(),
1528
- logprobs: import_v46.z.object({
1529
- content: import_v46.z.array(
1530
- import_v46.z.object({
1531
- token: import_v46.z.string(),
1532
- logprob: import_v46.z.number(),
1533
- top_logprobs: import_v46.z.array(
1534
- import_v46.z.object({
1535
- token: import_v46.z.string(),
1536
- logprob: import_v46.z.number()
1537
- }).passthrough()
1538
- )
1539
- }).passthrough()
1540
- ).nullable()
1541
- }).passthrough().nullable().optional(),
1542
- finish_reason: import_v46.z.string().optional().nullable()
1543
- }).passthrough()
1544
- )
1545
- }),
1546
- // Error response (HTTP 200 with error payload)
1547
- OpenRouterErrorResponseSchema.extend({
1548
- user_id: import_v46.z.string().optional()
1549
- })
1550
- ]);
1551
- var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1552
- OpenRouterChatCompletionBaseResponseSchema.extend({
1553
- choices: import_v46.z.array(
1554
- import_v46.z.object({
1555
- delta: import_v46.z.object({
1556
- role: import_v46.z.enum(["assistant"]).optional(),
1557
- content: import_v46.z.string().nullish(),
1558
- reasoning: import_v46.z.string().nullish().optional(),
1559
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1560
- images: ImageResponseArraySchema.nullish(),
1561
- tool_calls: import_v46.z.array(
1562
- import_v46.z.object({
1563
- index: import_v46.z.number().nullish(),
1564
- id: import_v46.z.string().nullish(),
1565
- type: import_v46.z.literal("function").optional(),
1566
- function: import_v46.z.object({
1567
- name: import_v46.z.string().nullish(),
1568
- arguments: import_v46.z.string().nullish()
1569
- }).passthrough()
1570
- }).passthrough()
1571
- ).nullish(),
1572
- annotations: import_v46.z.array(
1573
- import_v46.z.union([
1574
- // URL citation from web search
1575
- import_v46.z.object({
1576
- type: import_v46.z.literal("url_citation"),
1577
- url_citation: import_v46.z.object({
1578
- end_index: import_v46.z.number(),
1579
- start_index: import_v46.z.number(),
1580
- title: import_v46.z.string(),
1581
- url: import_v46.z.string(),
1582
- content: import_v46.z.string().optional()
1583
- }).passthrough()
1584
- }).passthrough(),
1585
- // File annotation from FileParserPlugin (old format)
1586
- import_v46.z.object({
1587
- type: import_v46.z.literal("file_annotation"),
1588
- file_annotation: import_v46.z.object({
1589
- file_id: import_v46.z.string(),
1590
- quote: import_v46.z.string().optional()
1591
- }).passthrough()
1592
- }).passthrough(),
1593
- // File annotation from FileParserPlugin (new format)
1594
- import_v46.z.object({
1595
- type: import_v46.z.literal("file"),
1596
- file: import_v46.z.object({
1597
- hash: import_v46.z.string(),
1598
- name: import_v46.z.string(),
1599
- content: import_v46.z.array(
1600
- import_v46.z.object({
1601
- type: import_v46.z.string(),
1602
- text: import_v46.z.string().optional()
1603
- }).passthrough()
1604
- ).optional()
1605
- }).passthrough()
1606
- }).passthrough()
1607
- ])
1608
- ).nullish()
1609
- }).passthrough().nullish(),
1610
- logprobs: import_v46.z.object({
1611
- content: import_v46.z.array(
1612
- import_v46.z.object({
1613
- token: import_v46.z.string(),
1614
- logprob: import_v46.z.number(),
1615
- top_logprobs: import_v46.z.array(
1616
- import_v46.z.object({
1617
- token: import_v46.z.string(),
1618
- logprob: import_v46.z.number()
1619
- }).passthrough()
1620
- )
1621
- }).passthrough()
1622
- ).nullable()
1623
- }).passthrough().nullish(),
1624
- finish_reason: import_v46.z.string().nullable().optional(),
1625
- index: import_v46.z.number().nullish()
1626
- }).passthrough()
1627
- )
1628
- }),
1629
- OpenRouterErrorResponseSchema
1630
- ]);
1631
-
1632
- // src/chat/index.ts
1633
- var OpenRouterChatLanguageModel = class {
1634
- constructor(modelId, settings, config) {
1635
- this.specificationVersion = "v2";
1636
- this.provider = "openrouter";
1637
- this.defaultObjectGenerationMode = "tool";
1638
- this.supportsImageUrls = true;
1639
- this.supportedUrls = {
1640
- "image/*": [
1641
- /^data:image\/[a-zA-Z]+;base64,/,
1642
- /^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
1643
- ],
1644
- // 'text/*': [/^data:text\//, /^https?:\/\/.+$/],
1645
- "application/*": [/^data:application\//, /^https?:\/\/.+$/]
1646
- };
1647
- this.modelId = modelId;
1648
- this.settings = settings;
1649
- this.config = config;
1650
- }
1651
- getArgs({
1652
- prompt,
1653
- maxOutputTokens,
1654
- temperature,
1655
- topP,
1656
- frequencyPenalty,
1657
- presencePenalty,
1658
- seed,
1659
- stopSequences,
1660
- responseFormat,
1661
- topK,
1662
- tools,
1663
- toolChoice
1664
- }) {
1665
- var _a15;
1666
- const baseArgs = __spreadValues(__spreadValues({
1667
- // model id:
1668
- model: this.modelId,
1669
- models: this.settings.models,
1670
- // model specific settings:
1671
- logit_bias: this.settings.logitBias,
1672
- logprobs: this.settings.logprobs === true || typeof this.settings.logprobs === "number" ? true : void 0,
1673
- top_logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
1674
- user: this.settings.user,
1675
- parallel_tool_calls: this.settings.parallelToolCalls,
1676
- // standardized settings:
1677
- max_tokens: maxOutputTokens,
1678
- temperature,
1679
- top_p: topP,
1680
- frequency_penalty: frequencyPenalty,
1681
- presence_penalty: presencePenalty,
1682
- seed,
1683
- stop: stopSequences,
1684
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
1685
- type: "json_schema",
1686
- json_schema: __spreadValues({
1687
- schema: responseFormat.schema,
1688
- strict: true,
1689
- name: (_a15 = responseFormat.name) != null ? _a15 : "response"
1690
- }, responseFormat.description && {
1691
- description: responseFormat.description
1692
- })
1693
- } : { type: "json_object" } : void 0,
1694
- top_k: topK,
1695
- // messages:
1696
- messages: convertToOpenRouterChatMessages(prompt),
1697
- // OpenRouter specific settings:
1698
- include_reasoning: this.settings.includeReasoning,
1699
- reasoning: this.settings.reasoning,
1700
- usage: this.settings.usage,
1701
- // Web search settings:
1702
- plugins: this.settings.plugins,
1703
- web_search_options: this.settings.web_search_options,
1704
- // Provider routing settings:
1705
- provider: this.settings.provider,
1706
- // Debug settings:
1707
- debug: this.settings.debug
1708
- }, this.config.extraBody), this.settings.extraBody);
1709
- if (tools && tools.length > 0) {
1710
- const mappedTools = tools.filter(
1711
- (tool) => tool.type === "function"
1712
- ).map((tool) => ({
1713
- type: "function",
1714
- function: {
1715
- name: tool.name,
1716
- description: tool.description,
1717
- parameters: tool.inputSchema
1718
- }
1719
- }));
1720
- return __spreadProps(__spreadValues({}, baseArgs), {
1721
- tools: mappedTools,
1722
- tool_choice: toolChoice ? getChatCompletionToolChoice(toolChoice) : void 0
1723
- });
1724
- }
1725
- return baseArgs;
1726
- }
1727
- async doGenerate(options) {
1728
- var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
1729
- const providerOptions = options.providerOptions || {};
1730
- const openrouterOptions = providerOptions.openrouter || {};
1731
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
1732
- const { value: responseValue, responseHeaders } = await postJsonToApi({
1733
- url: this.config.url({
1734
- path: "/chat/completions",
1735
- modelId: this.modelId
1736
- }),
1737
- headers: combineHeaders(this.config.headers(), options.headers),
1738
- body: args,
1739
- failedResponseHandler: openrouterFailedResponseHandler,
1740
- successfulResponseHandler: createJsonResponseHandler(
1741
- OpenRouterNonStreamChatCompletionResponseSchema
1742
- ),
1743
- abortSignal: options.abortSignal,
1744
- fetch: this.config.fetch
1745
- });
1746
- if ("error" in responseValue) {
1747
- const errorData = responseValue.error;
1748
- throw new APICallError({
1749
- message: errorData.message,
1750
- url: this.config.url({
1751
- path: "/chat/completions",
1752
- modelId: this.modelId
1753
- }),
1754
- requestBodyValues: args,
1755
- statusCode: 200,
1756
- responseHeaders,
1757
- data: errorData
1758
- });
1759
- }
1760
- const response = responseValue;
1761
- const choice = response.choices[0];
1762
- if (!choice) {
1763
- throw new NoContentGeneratedError({
1764
- message: "No choice in response"
1765
- });
1766
- }
1767
- const usageInfo = response.usage ? {
1768
- inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
1769
- outputTokens: (_b = response.usage.completion_tokens) != null ? _b : 0,
1770
- totalTokens: ((_c = response.usage.prompt_tokens) != null ? _c : 0) + ((_d = response.usage.completion_tokens) != null ? _d : 0),
1771
- reasoningTokens: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0,
1772
- cachedInputTokens: (_h = (_g = response.usage.prompt_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : 0
1773
- } : {
1774
- inputTokens: 0,
1775
- outputTokens: 0,
1776
- totalTokens: 0,
1777
- reasoningTokens: 0,
1778
- cachedInputTokens: 0
1779
- };
1780
- const reasoningDetails = (_i = choice.message.reasoning_details) != null ? _i : [];
1781
- const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
1782
- switch (detail.type) {
1783
- case "reasoning.text" /* Text */: {
1784
- if (detail.text) {
1785
- return {
1786
- type: "reasoning",
1787
- text: detail.text,
1788
- providerMetadata: {
1789
- openrouter: {
1790
- reasoning_details: [detail]
1791
- }
1792
- }
1793
- };
1794
- }
1795
- break;
1796
- }
1797
- case "reasoning.summary" /* Summary */: {
1798
- if (detail.summary) {
1799
- return {
1800
- type: "reasoning",
1801
- text: detail.summary,
1802
- providerMetadata: {
1803
- openrouter: {
1804
- reasoning_details: [detail]
1805
- }
1806
- }
1807
- };
1808
- }
1809
- break;
1810
- }
1811
- case "reasoning.encrypted" /* Encrypted */: {
1812
- if (detail.data) {
1813
- return {
1814
- type: "reasoning",
1815
- text: "[REDACTED]",
1816
- providerMetadata: {
1817
- openrouter: {
1818
- reasoning_details: [detail]
1819
- }
1820
- }
1821
- };
1822
- }
1823
- break;
1824
- }
1825
- default: {
1826
- detail;
1827
- }
1828
- }
1829
- return null;
1830
- }).filter((p) => p !== null) : choice.message.reasoning ? [
1831
- {
1832
- type: "reasoning",
1833
- text: choice.message.reasoning
1834
- }
1835
- ] : [];
1836
- const content = [];
1837
- content.push(...reasoning);
1838
- if (choice.message.content) {
586
+ if (response.outputText && !content.some((c) => c.type === "text")) {
1839
587
  content.push({
1840
588
  type: "text",
1841
- text: choice.message.content
589
+ text: response.outputText
1842
590
  });
1843
591
  }
1844
- if (choice.message.tool_calls) {
1845
- for (const toolCall of choice.message.tool_calls) {
1846
- content.push({
1847
- type: "tool-call",
1848
- toolCallId: (_j = toolCall.id) != null ? _j : generateId(),
1849
- toolName: toolCall.function.name,
1850
- input: toolCall.function.arguments,
1851
- providerMetadata: {
1852
- openrouter: {
1853
- reasoning_details: reasoningDetails
1854
- }
1855
- }
1856
- });
1857
- }
1858
- }
1859
- if (choice.message.images) {
1860
- for (const image of choice.message.images) {
1861
- content.push({
1862
- type: "file",
1863
- mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1864
- data: getBase64FromDataUrl(image.image_url.url)
1865
- });
1866
- }
1867
- }
1868
- if (choice.message.annotations) {
1869
- for (const annotation of choice.message.annotations) {
1870
- if (annotation.type === "url_citation") {
1871
- content.push({
1872
- type: "source",
1873
- sourceType: "url",
1874
- id: annotation.url_citation.url,
1875
- url: annotation.url_citation.url,
1876
- title: annotation.url_citation.title,
1877
- providerMetadata: {
1878
- openrouter: {
1879
- content: annotation.url_citation.content || ""
1880
- }
1881
- }
1882
- });
1883
- }
1884
- }
1885
- }
1886
- const fileAnnotations = (_k = choice.message.annotations) == null ? void 0 : _k.filter(
1887
- (a) => a.type === "file"
592
+ let finishReason = mapOpenRouterFinishReason(
593
+ response.status === "completed" ? "stop" : response.status ?? "stop"
1888
594
  );
1889
- const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
1890
- const hasEncryptedReasoning = reasoningDetails.some(
1891
- (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
595
+ const hasToolCalls = content.some((c) => c.type === "tool-call");
596
+ if (hasToolCalls && hasEncryptedReasoning(reasoningDetails) && finishReason.unified === "stop") {
597
+ finishReason = { unified: "tool-calls", raw: finishReason.raw };
598
+ }
599
+ const usage = buildUsage(
600
+ response.usage ? {
601
+ inputTokens: response.usage.inputTokens,
602
+ outputTokens: response.usage.outputTokens
603
+ } : void 0
1892
604
  );
1893
- const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
1894
- const effectiveFinishReason = shouldOverrideFinishReason ? "tool-calls" : mapOpenRouterFinishReason(choice.finish_reason);
605
+ const providerMetadata = buildProviderMetadata({
606
+ id: response.id,
607
+ provider: void 0,
608
+ // Responses API doesn't expose provider in response
609
+ usage: response.usage ? {
610
+ promptTokens: response.usage.inputTokens,
611
+ completionTokens: response.usage.outputTokens,
612
+ totalTokens: response.usage.totalTokens,
613
+ cost: response.usage.cost ?? void 0,
614
+ // Map inputTokensDetails -> promptTokensDetails
615
+ promptTokensDetails: response.usage.inputTokensDetails ? {
616
+ cachedTokens: response.usage.inputTokensDetails.cachedTokens
617
+ } : void 0,
618
+ // Map outputTokensDetails -> completionTokensDetails
619
+ completionTokensDetails: response.usage.outputTokensDetails ? {
620
+ reasoningTokens: response.usage.outputTokensDetails.reasoningTokens
621
+ } : void 0
622
+ } : void 0
623
+ });
1895
624
  return {
1896
625
  content,
1897
- finishReason: effectiveFinishReason,
1898
- usage: usageInfo,
1899
- warnings: [],
1900
- providerMetadata: {
1901
- openrouter: OpenRouterProviderMetadataSchema.parse({
1902
- provider: (_l = response.provider) != null ? _l : "",
1903
- reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
1904
- annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
1905
- usage: __spreadValues(__spreadValues(__spreadValues({
1906
- promptTokens: (_n = usageInfo.inputTokens) != null ? _n : 0,
1907
- completionTokens: (_o = usageInfo.outputTokens) != null ? _o : 0,
1908
- totalTokens: (_p = usageInfo.totalTokens) != null ? _p : 0,
1909
- cost: (_q = response.usage) == null ? void 0 : _q.cost
1910
- }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
1911
- promptTokensDetails: {
1912
- cachedTokens: response.usage.prompt_tokens_details.cached_tokens
1913
- }
1914
- } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
1915
- completionTokensDetails: {
1916
- reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
1917
- }
1918
- } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
1919
- costDetails: {
1920
- upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
1921
- }
1922
- } : {})
1923
- })
626
+ finishReason,
627
+ usage,
628
+ warnings,
629
+ providerMetadata,
630
+ request: {
631
+ body: requestParams
1924
632
  },
1925
- request: { body: args },
1926
633
  response: {
1927
634
  id: response.id,
1928
- modelId: response.model,
1929
- headers: responseHeaders
635
+ timestamp: new Date(response.createdAt * 1e3),
636
+ modelId: response.model
1930
637
  }
1931
638
  };
1932
639
  }
1933
640
  async doStream(options) {
1934
- var _a15;
1935
- const providerOptions = options.providerOptions || {};
1936
- const openrouterOptions = providerOptions.openrouter || {};
1937
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
1938
- const { value: response, responseHeaders } = await postJsonToApi({
1939
- url: this.config.url({
1940
- path: "/chat/completions",
1941
- modelId: this.modelId
1942
- }),
1943
- headers: combineHeaders(this.config.headers(), options.headers),
1944
- body: __spreadProps(__spreadValues({}, args), {
1945
- stream: true,
1946
- // only include stream_options when in strict compatibility mode:
1947
- stream_options: this.config.compatibility === "strict" ? __spreadValues({
1948
- include_usage: true
1949
- }, ((_a15 = this.settings.usage) == null ? void 0 : _a15.include) ? { include_usage: true } : {}) : void 0
1950
- }),
1951
- failedResponseHandler: openrouterFailedResponseHandler,
1952
- successfulResponseHandler: createEventSourceResponseHandler(
1953
- OpenRouterStreamChatCompletionChunkSchema
1954
- ),
1955
- abortSignal: options.abortSignal,
1956
- fetch: this.config.fetch
641
+ const warnings = [];
642
+ const client = new import_sdk.OpenRouter({
643
+ apiKey: this.settings.apiKey,
644
+ serverURL: this.settings.baseURL
1957
645
  });
1958
- const toolCalls = [];
1959
- let finishReason = "other";
1960
- const usage = {
1961
- inputTokens: Number.NaN,
1962
- outputTokens: Number.NaN,
1963
- totalTokens: Number.NaN,
1964
- reasoningTokens: Number.NaN,
1965
- cachedInputTokens: Number.NaN
646
+ const openRouterInput = convertToOpenRouterMessages(options.prompt);
647
+ const tools = convertToolsToResponsesFormat(options.tools, warnings);
648
+ const toolChoice = convertToolChoiceToResponsesFormat(options.toolChoice);
649
+ const text = convertResponseFormatToText(options.responseFormat);
650
+ const requestParams = {
651
+ model: this.modelId,
652
+ input: openRouterInput,
653
+ stream: true,
654
+ ...options.maxOutputTokens !== void 0 && {
655
+ maxOutputTokens: options.maxOutputTokens
656
+ },
657
+ ...options.temperature !== void 0 && {
658
+ temperature: options.temperature
659
+ },
660
+ ...options.topP !== void 0 && { topP: options.topP },
661
+ ...tools.length > 0 && { tools },
662
+ ...toolChoice !== void 0 && { toolChoice },
663
+ ...text !== void 0 && { text }
1966
664
  };
1967
- const openrouterUsage = {};
1968
- const accumulatedReasoningDetails = [];
1969
- const accumulatedFileAnnotations = [];
1970
- let textStarted = false;
1971
- let reasoningStarted = false;
1972
- let textId;
1973
- let reasoningId;
1974
- let openrouterResponseId;
1975
- let provider;
1976
- return {
1977
- stream: response.pipeThrough(
1978
- new TransformStream({
1979
- transform(chunk, controller) {
1980
- var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1981
- if (!chunk.success) {
1982
- finishReason = "error";
1983
- controller.enqueue({ type: "error", error: chunk.error });
1984
- return;
1985
- }
1986
- const value = chunk.value;
1987
- if ("error" in value) {
1988
- finishReason = "error";
1989
- controller.enqueue({ type: "error", error: value.error });
1990
- return;
1991
- }
1992
- if (value.provider) {
1993
- provider = value.provider;
1994
- }
1995
- if (value.id) {
1996
- openrouterResponseId = value.id;
1997
- controller.enqueue({
1998
- type: "response-metadata",
1999
- id: value.id
2000
- });
2001
- }
2002
- if (value.model) {
2003
- controller.enqueue({
2004
- type: "response-metadata",
2005
- modelId: value.model
2006
- });
2007
- }
2008
- if (value.usage != null) {
2009
- usage.inputTokens = value.usage.prompt_tokens;
2010
- usage.outputTokens = value.usage.completion_tokens;
2011
- usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
2012
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
2013
- if (value.usage.prompt_tokens_details) {
2014
- const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
2015
- usage.cachedInputTokens = cachedInputTokens;
2016
- openrouterUsage.promptTokensDetails = {
2017
- cachedTokens: cachedInputTokens
2018
- };
2019
- }
2020
- openrouterUsage.completionTokens = value.usage.completion_tokens;
2021
- if (value.usage.completion_tokens_details) {
2022
- const reasoningTokens = (_b = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b : 0;
2023
- usage.reasoningTokens = reasoningTokens;
2024
- openrouterUsage.completionTokensDetails = {
2025
- reasoningTokens
2026
- };
2027
- }
2028
- openrouterUsage.cost = value.usage.cost;
2029
- openrouterUsage.totalTokens = value.usage.total_tokens;
2030
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
2031
- if (upstreamInferenceCost != null) {
2032
- openrouterUsage.costDetails = {
2033
- upstreamInferenceCost
2034
- };
2035
- }
2036
- }
2037
- const choice = value.choices[0];
2038
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
2039
- finishReason = mapOpenRouterFinishReason(choice.finish_reason);
2040
- }
2041
- if ((choice == null ? void 0 : choice.delta) == null) {
2042
- return;
2043
- }
2044
- const delta = choice.delta;
2045
- const emitReasoningChunk = (chunkText, providerMetadata) => {
2046
- if (!reasoningStarted) {
2047
- reasoningId = openrouterResponseId || generateId();
2048
- controller.enqueue({
2049
- providerMetadata,
2050
- type: "reasoning-start",
2051
- id: reasoningId
2052
- });
2053
- reasoningStarted = true;
2054
- }
2055
- controller.enqueue({
2056
- providerMetadata,
2057
- type: "reasoning-delta",
2058
- delta: chunkText,
2059
- id: reasoningId || generateId()
2060
- });
2061
- };
2062
- if (delta.reasoning_details && delta.reasoning_details.length > 0) {
2063
- for (const detail of delta.reasoning_details) {
2064
- if (detail.type === "reasoning.text" /* Text */) {
2065
- const lastDetail = accumulatedReasoningDetails[accumulatedReasoningDetails.length - 1];
2066
- if ((lastDetail == null ? void 0 : lastDetail.type) === "reasoning.text" /* Text */) {
2067
- lastDetail.text = (lastDetail.text || "") + (detail.text || "");
2068
- lastDetail.signature = lastDetail.signature || detail.signature;
2069
- lastDetail.format = lastDetail.format || detail.format;
2070
- } else {
2071
- accumulatedReasoningDetails.push(__spreadValues({}, detail));
2072
- }
2073
- } else {
2074
- accumulatedReasoningDetails.push(detail);
2075
- }
2076
- }
2077
- const reasoningMetadata = {
2078
- openrouter: {
2079
- reasoning_details: delta.reasoning_details
2080
- }
2081
- };
2082
- for (const detail of delta.reasoning_details) {
2083
- switch (detail.type) {
2084
- case "reasoning.text" /* Text */: {
2085
- if (detail.text) {
2086
- emitReasoningChunk(detail.text, reasoningMetadata);
2087
- }
2088
- break;
2089
- }
2090
- case "reasoning.encrypted" /* Encrypted */: {
2091
- if (detail.data) {
2092
- emitReasoningChunk("[REDACTED]", reasoningMetadata);
2093
- }
2094
- break;
2095
- }
2096
- case "reasoning.summary" /* Summary */: {
2097
- if (detail.summary) {
2098
- emitReasoningChunk(detail.summary, reasoningMetadata);
2099
- }
2100
- break;
2101
- }
2102
- default: {
2103
- detail;
2104
- break;
2105
- }
2106
- }
2107
- }
2108
- } else if (delta.reasoning) {
2109
- emitReasoningChunk(delta.reasoning);
2110
- }
2111
- if (delta.content) {
2112
- if (reasoningStarted && !textStarted) {
2113
- controller.enqueue({
2114
- type: "reasoning-end",
2115
- id: reasoningId || generateId()
2116
- });
2117
- reasoningStarted = false;
2118
- }
2119
- if (!textStarted) {
2120
- textId = openrouterResponseId || generateId();
2121
- controller.enqueue({
2122
- type: "text-start",
2123
- id: textId
2124
- });
2125
- textStarted = true;
2126
- }
2127
- controller.enqueue({
2128
- type: "text-delta",
2129
- delta: delta.content,
2130
- id: textId || generateId()
2131
- });
2132
- }
2133
- if (delta.annotations) {
2134
- for (const annotation of delta.annotations) {
2135
- if (annotation.type === "url_citation") {
2136
- controller.enqueue({
2137
- type: "source",
2138
- sourceType: "url",
2139
- id: annotation.url_citation.url,
2140
- url: annotation.url_citation.url,
2141
- title: annotation.url_citation.title,
2142
- providerMetadata: {
2143
- openrouter: {
2144
- content: annotation.url_citation.content || ""
2145
- }
2146
- }
2147
- });
2148
- } else if (annotation.type === "file") {
2149
- const file = annotation.file;
2150
- if (file && typeof file === "object" && "hash" in file && "name" in file) {
2151
- accumulatedFileAnnotations.push(
2152
- annotation
2153
- );
2154
- }
2155
- }
2156
- }
2157
- }
2158
- if (delta.tool_calls != null) {
2159
- for (const toolCallDelta of delta.tool_calls) {
2160
- const index = (_d = toolCallDelta.index) != null ? _d : toolCalls.length - 1;
2161
- if (toolCalls[index] == null) {
2162
- if (toolCallDelta.type !== "function") {
2163
- throw new InvalidResponseDataError({
2164
- data: toolCallDelta,
2165
- message: `Expected 'function' type.`
2166
- });
2167
- }
2168
- if (toolCallDelta.id == null) {
2169
- throw new InvalidResponseDataError({
2170
- data: toolCallDelta,
2171
- message: `Expected 'id' to be a string.`
2172
- });
2173
- }
2174
- if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
2175
- throw new InvalidResponseDataError({
2176
- data: toolCallDelta,
2177
- message: `Expected 'function.name' to be a string.`
2178
- });
2179
- }
2180
- toolCalls[index] = {
2181
- id: toolCallDelta.id,
2182
- type: "function",
2183
- function: {
2184
- name: toolCallDelta.function.name,
2185
- arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
2186
- },
2187
- inputStarted: false,
2188
- sent: false
2189
- };
2190
- const toolCall2 = toolCalls[index];
2191
- if (toolCall2 == null) {
2192
- throw new InvalidResponseDataError({
2193
- data: { index, toolCallsLength: toolCalls.length },
2194
- message: `Tool call at index ${index} is missing after creation.`
2195
- });
2196
- }
2197
- if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
2198
- toolCall2.inputStarted = true;
2199
- controller.enqueue({
2200
- type: "tool-input-start",
2201
- id: toolCall2.id,
2202
- toolName: toolCall2.function.name
2203
- });
2204
- controller.enqueue({
2205
- type: "tool-input-delta",
2206
- id: toolCall2.id,
2207
- delta: toolCall2.function.arguments
2208
- });
2209
- controller.enqueue({
2210
- type: "tool-input-end",
2211
- id: toolCall2.id
2212
- });
2213
- controller.enqueue({
2214
- type: "tool-call",
2215
- toolCallId: toolCall2.id,
2216
- toolName: toolCall2.function.name,
2217
- input: toolCall2.function.arguments,
2218
- providerMetadata: {
2219
- openrouter: {
2220
- reasoning_details: accumulatedReasoningDetails
2221
- }
2222
- }
2223
- });
2224
- toolCall2.sent = true;
2225
- }
2226
- continue;
2227
- }
2228
- const toolCall = toolCalls[index];
2229
- if (toolCall == null) {
2230
- throw new InvalidResponseDataError({
2231
- data: {
2232
- index,
2233
- toolCallsLength: toolCalls.length,
2234
- toolCallDelta
2235
- },
2236
- message: `Tool call at index ${index} is missing during merge.`
2237
- });
2238
- }
2239
- if (!toolCall.inputStarted) {
2240
- toolCall.inputStarted = true;
2241
- controller.enqueue({
2242
- type: "tool-input-start",
2243
- id: toolCall.id,
2244
- toolName: toolCall.function.name
2245
- });
2246
- }
2247
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
2248
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
2249
- }
2250
- controller.enqueue({
2251
- type: "tool-input-delta",
2252
- id: toolCall.id,
2253
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
2254
- });
2255
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
2256
- controller.enqueue({
2257
- type: "tool-call",
2258
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
2259
- toolName: toolCall.function.name,
2260
- input: toolCall.function.arguments,
2261
- providerMetadata: {
2262
- openrouter: {
2263
- reasoning_details: accumulatedReasoningDetails
2264
- }
2265
- }
2266
- });
2267
- toolCall.sent = true;
2268
- }
2269
- }
2270
- }
2271
- if (delta.images != null) {
2272
- for (const image of delta.images) {
2273
- controller.enqueue({
2274
- type: "file",
2275
- mediaType: getMediaType(image.image_url.url, "image/jpeg"),
2276
- data: getBase64FromDataUrl(image.image_url.url)
2277
- });
2278
- }
2279
- }
2280
- },
2281
- flush(controller) {
2282
- var _a16;
2283
- const hasToolCalls = toolCalls.length > 0;
2284
- const hasEncryptedReasoning = accumulatedReasoningDetails.some(
2285
- (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
2286
- );
2287
- if (hasToolCalls && hasEncryptedReasoning && finishReason === "stop") {
2288
- finishReason = "tool-calls";
2289
- }
2290
- if (finishReason === "tool-calls") {
2291
- for (const toolCall of toolCalls) {
2292
- if (toolCall && !toolCall.sent) {
2293
- controller.enqueue({
2294
- type: "tool-call",
2295
- toolCallId: (_a16 = toolCall.id) != null ? _a16 : generateId(),
2296
- toolName: toolCall.function.name,
2297
- // Coerce invalid arguments to an empty JSON object
2298
- input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
2299
- providerMetadata: {
2300
- openrouter: {
2301
- reasoning_details: accumulatedReasoningDetails
2302
- }
2303
- }
2304
- });
2305
- toolCall.sent = true;
2306
- }
2307
- }
2308
- }
2309
- if (reasoningStarted) {
2310
- controller.enqueue({
2311
- type: "reasoning-end",
2312
- id: reasoningId || generateId()
2313
- });
2314
- }
2315
- if (textStarted) {
2316
- controller.enqueue({
2317
- type: "text-end",
2318
- id: textId || generateId()
2319
- });
2320
- }
2321
- const openrouterMetadata = {
2322
- usage: openrouterUsage
2323
- };
2324
- if (provider !== void 0) {
2325
- openrouterMetadata.provider = provider;
2326
- }
2327
- if (accumulatedReasoningDetails.length > 0) {
2328
- openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
2329
- }
2330
- if (accumulatedFileAnnotations.length > 0) {
2331
- openrouterMetadata.annotations = accumulatedFileAnnotations;
665
+ const combinedHeaders = (0, import_provider_utils.normalizeHeaders)(
666
+ (0, import_provider_utils.combineHeaders)(this.settings.headers, options.headers)
667
+ );
668
+ const eventStream = await client.beta.responses.send(requestParams, {
669
+ fetchOptions: {
670
+ signal: options.abortSignal,
671
+ headers: combinedHeaders
672
+ }
673
+ });
674
+ const state = createStreamState();
675
+ const transformedStream = new ReadableStream({
676
+ async start(controller) {
677
+ controller.enqueue({
678
+ type: "stream-start",
679
+ warnings
680
+ });
681
+ try {
682
+ for await (const event of eventStream) {
683
+ const parts = transformResponsesEvent(event, state);
684
+ for (const part of parts) {
685
+ controller.enqueue(part);
2332
686
  }
2333
- controller.enqueue({
2334
- type: "finish",
2335
- finishReason,
2336
- usage,
2337
- providerMetadata: {
2338
- openrouter: openrouterMetadata
2339
- }
2340
- });
2341
687
  }
2342
- })
2343
- ),
2344
- warnings: [],
2345
- request: { body: args },
2346
- response: { headers: responseHeaders }
688
+ } catch (error) {
689
+ controller.enqueue({
690
+ type: "error",
691
+ error
692
+ });
693
+ } finally {
694
+ controller.close();
695
+ }
696
+ }
697
+ });
698
+ return {
699
+ stream: transformedStream,
700
+ request: {
701
+ body: requestParams
702
+ }
2347
703
  };
2348
704
  }
2349
705
  };
2350
-
2351
- // src/completion/convert-to-openrouter-completion-prompt.ts
2352
- function convertToOpenRouterCompletionPrompt({
2353
- prompt,
2354
- inputFormat,
2355
- user = "user",
2356
- assistant = "assistant"
2357
- }) {
2358
- if (inputFormat === "prompt" && prompt.length === 1 && prompt[0] && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0] && prompt[0].content[0].type === "text") {
2359
- return { prompt: prompt[0].content[0].text };
2360
- }
2361
- let text = "";
2362
- if (prompt[0] && prompt[0].role === "system") {
2363
- text += `${prompt[0].content}
2364
-
2365
- `;
2366
- prompt = prompt.slice(1);
2367
- }
2368
- for (const { role, content } of prompt) {
2369
- switch (role) {
2370
- case "system": {
2371
- throw new InvalidPromptError({
2372
- message: `Unexpected system message in prompt: ${content}`,
2373
- prompt
706
+ function createStreamState() {
707
+ return {
708
+ responseId: void 0,
709
+ responseModel: void 0,
710
+ responseCreated: void 0,
711
+ textStarted: false,
712
+ textId: "text-0",
713
+ reasoningStarted: false,
714
+ reasoningId: "reasoning-0",
715
+ textEnded: false,
716
+ reasoningEnded: false,
717
+ sourceIds: [],
718
+ toolCalls: /* @__PURE__ */ new Map()
719
+ };
720
+ }
721
+ function transformResponsesEvent(event, state) {
722
+ const parts = [];
723
+ switch (event.type) {
724
+ // Response lifecycle events
725
+ case "response.created":
726
+ case "response.in_progress": {
727
+ if (event.response) {
728
+ state.responseId = event.response.id;
729
+ state.responseModel = event.response.model;
730
+ state.responseCreated = event.response.createdAt;
731
+ }
732
+ break;
733
+ }
734
+ // Text streaming
735
+ case "response.output_text.delta": {
736
+ if (!state.textStarted) {
737
+ state.textStarted = true;
738
+ parts.push({
739
+ type: "text-start",
740
+ id: state.textId
2374
741
  });
2375
742
  }
2376
- case "user": {
2377
- const userMessage = content.map((part) => {
2378
- switch (part.type) {
2379
- case "text": {
2380
- return part.text;
2381
- }
2382
- case "file": {
2383
- throw new UnsupportedFunctionalityError({
2384
- functionality: "file attachments"
2385
- });
2386
- }
2387
- default: {
2388
- return "";
2389
- }
2390
- }
2391
- }).join("");
2392
- text += `${user}:
2393
- ${userMessage}
2394
-
2395
- `;
2396
- break;
743
+ if (event.delta && event.delta.length > 0) {
744
+ parts.push({
745
+ type: "text-delta",
746
+ id: state.textId,
747
+ delta: event.delta
748
+ });
2397
749
  }
2398
- case "assistant": {
2399
- const assistantMessage = content.map(
2400
- (part) => {
2401
- switch (part.type) {
2402
- case "text": {
2403
- return part.text;
2404
- }
2405
- case "tool-call": {
2406
- throw new UnsupportedFunctionalityError({
2407
- functionality: "tool-call messages"
2408
- });
2409
- }
2410
- case "tool-result": {
2411
- throw new UnsupportedFunctionalityError({
2412
- functionality: "tool-result messages"
2413
- });
2414
- }
2415
- case "reasoning": {
2416
- throw new UnsupportedFunctionalityError({
2417
- functionality: "reasoning messages"
2418
- });
2419
- }
2420
- case "file": {
2421
- throw new UnsupportedFunctionalityError({
2422
- functionality: "file attachments"
2423
- });
2424
- }
2425
- default: {
2426
- return "";
2427
- }
2428
- }
2429
- }
2430
- ).join("");
2431
- text += `${assistant}:
2432
- ${assistantMessage}
2433
-
2434
- `;
2435
- break;
750
+ break;
751
+ }
752
+ case "response.output_text.done": {
753
+ if (state.textStarted && !state.textEnded) {
754
+ state.textEnded = true;
755
+ parts.push({
756
+ type: "text-end",
757
+ id: state.textId
758
+ });
2436
759
  }
2437
- case "tool": {
2438
- throw new UnsupportedFunctionalityError({
2439
- functionality: "tool messages"
760
+ break;
761
+ }
762
+ // Reasoning streaming
763
+ case "response.reasoning_text.delta": {
764
+ if (!state.reasoningStarted) {
765
+ state.reasoningStarted = true;
766
+ parts.push({
767
+ type: "reasoning-start",
768
+ id: state.reasoningId
2440
769
  });
2441
770
  }
2442
- default: {
2443
- break;
771
+ if (event.delta && event.delta.length > 0) {
772
+ parts.push({
773
+ type: "reasoning-delta",
774
+ id: state.reasoningId,
775
+ delta: event.delta
776
+ });
2444
777
  }
778
+ break;
2445
779
  }
2446
- }
2447
- text += `${assistant}:
2448
- `;
2449
- return {
2450
- prompt: text
2451
- };
2452
- }
2453
-
2454
- // src/completion/schemas.ts
2455
- var import_v47 = require("zod/v4");
2456
- var OpenRouterCompletionChunkSchema = import_v47.z.union([
2457
- import_v47.z.object({
2458
- id: import_v47.z.string().optional(),
2459
- model: import_v47.z.string().optional(),
2460
- choices: import_v47.z.array(
2461
- import_v47.z.object({
2462
- text: import_v47.z.string(),
2463
- reasoning: import_v47.z.string().nullish().optional(),
2464
- reasoning_details: ReasoningDetailArraySchema.nullish(),
2465
- finish_reason: import_v47.z.string().nullish(),
2466
- index: import_v47.z.number().nullish(),
2467
- logprobs: import_v47.z.object({
2468
- tokens: import_v47.z.array(import_v47.z.string()),
2469
- token_logprobs: import_v47.z.array(import_v47.z.number()),
2470
- top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
2471
- }).passthrough().nullable().optional()
2472
- }).passthrough()
2473
- ),
2474
- usage: import_v47.z.object({
2475
- prompt_tokens: import_v47.z.number(),
2476
- prompt_tokens_details: import_v47.z.object({
2477
- cached_tokens: import_v47.z.number()
2478
- }).passthrough().nullish(),
2479
- completion_tokens: import_v47.z.number(),
2480
- completion_tokens_details: import_v47.z.object({
2481
- reasoning_tokens: import_v47.z.number()
2482
- }).passthrough().nullish(),
2483
- total_tokens: import_v47.z.number(),
2484
- cost: import_v47.z.number().optional(),
2485
- cost_details: import_v47.z.object({
2486
- upstream_inference_cost: import_v47.z.number().nullish()
2487
- }).passthrough().nullish()
2488
- }).passthrough().nullish()
2489
- }).passthrough(),
2490
- OpenRouterErrorResponseSchema
2491
- ]);
2492
-
2493
- // src/completion/index.ts
2494
- var OpenRouterCompletionLanguageModel = class {
2495
- constructor(modelId, settings, config) {
2496
- this.specificationVersion = "v2";
2497
- this.provider = "openrouter";
2498
- this.supportsImageUrls = true;
2499
- this.supportedUrls = {
2500
- "image/*": [
2501
- /^data:image\/[a-zA-Z]+;base64,/,
2502
- /^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
2503
- ],
2504
- "text/*": [/^data:text\//, /^https?:\/\/.+$/],
2505
- "application/*": [/^data:application\//, /^https?:\/\/.+$/]
2506
- };
2507
- this.defaultObjectGenerationMode = void 0;
2508
- this.modelId = modelId;
2509
- this.settings = settings;
2510
- this.config = config;
2511
- }
2512
- getArgs({
2513
- prompt,
2514
- maxOutputTokens,
2515
- temperature,
2516
- topP,
2517
- frequencyPenalty,
2518
- presencePenalty,
2519
- seed,
2520
- responseFormat,
2521
- topK,
2522
- stopSequences,
2523
- tools,
2524
- toolChoice
2525
- }) {
2526
- const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
2527
- prompt,
2528
- inputFormat: "prompt"
2529
- });
2530
- if (tools == null ? void 0 : tools.length) {
2531
- throw new UnsupportedFunctionalityError({
2532
- functionality: "tools"
780
+ // Function call arguments streaming
781
+ case "response.function_call_arguments.delta": {
782
+ const toolCallId = event.itemId;
783
+ let toolState = state.toolCalls.get(toolCallId);
784
+ if (!toolState) {
785
+ toolState = { argumentsStarted: false };
786
+ state.toolCalls.set(toolCallId, toolState);
787
+ }
788
+ if (!toolState.argumentsStarted) {
789
+ toolState.argumentsStarted = true;
790
+ parts.push({
791
+ type: "tool-input-start",
792
+ id: toolCallId,
793
+ toolName: toolState.name ?? ""
794
+ // Will be filled in by output_item.added
795
+ });
796
+ }
797
+ if (event.delta && event.delta.length > 0) {
798
+ parts.push({
799
+ type: "tool-input-delta",
800
+ id: toolCallId,
801
+ delta: event.delta
802
+ });
803
+ }
804
+ break;
805
+ }
806
+ case "response.function_call_arguments.done": {
807
+ const toolCallId = event.itemId;
808
+ const toolState = state.toolCalls.get(toolCallId);
809
+ if (!toolState?.argumentsStarted) {
810
+ parts.push({
811
+ type: "tool-input-start",
812
+ id: toolCallId,
813
+ toolName: event.name
814
+ });
815
+ const args = event.arguments || "{}";
816
+ parts.push({
817
+ type: "tool-input-delta",
818
+ id: toolCallId,
819
+ delta: args
820
+ });
821
+ }
822
+ parts.push({
823
+ type: "tool-input-end",
824
+ id: toolCallId
825
+ });
826
+ parts.push({
827
+ type: "tool-call",
828
+ toolCallId,
829
+ toolName: event.name,
830
+ input: event.arguments || "{}"
2533
831
  });
832
+ break;
833
+ }
834
+ // Output item events (for function call metadata)
835
+ case "response.output_item.added": {
836
+ if (event.item.type === "function_call") {
837
+ const funcItem = event.item;
838
+ const toolCallId = funcItem.callId ?? `tool-${event.outputIndex}`;
839
+ const toolState = state.toolCalls.get(toolCallId) ?? {
840
+ argumentsStarted: false
841
+ };
842
+ toolState.name = funcItem.name;
843
+ state.toolCalls.set(toolCallId, toolState);
844
+ }
845
+ break;
846
+ }
847
+ // Annotation events (web search sources)
848
+ case "response.output_text.annotation.added": {
849
+ const annotation = event.annotation;
850
+ if (annotation.type === "url_citation") {
851
+ const urlAnnotation = annotation;
852
+ const sourceId = `source-${state.sourceIds.length}`;
853
+ state.sourceIds.push(sourceId);
854
+ parts.push({
855
+ type: "source",
856
+ sourceType: "url",
857
+ id: sourceId,
858
+ url: urlAnnotation.url,
859
+ title: urlAnnotation.title
860
+ });
861
+ }
862
+ break;
2534
863
  }
2535
- if (toolChoice) {
2536
- throw new UnsupportedFunctionalityError({
2537
- functionality: "toolChoice"
864
+ // Response completed - extract final usage data
865
+ case "response.completed": {
866
+ if (state.textStarted && !state.textEnded) {
867
+ state.textEnded = true;
868
+ parts.push({
869
+ type: "text-end",
870
+ id: state.textId
871
+ });
872
+ }
873
+ if (state.reasoningStarted && !state.reasoningEnded) {
874
+ state.reasoningEnded = true;
875
+ parts.push({
876
+ type: "reasoning-end",
877
+ id: state.reasoningId
878
+ });
879
+ }
880
+ const response = event.response;
881
+ parts.push({
882
+ type: "response-metadata",
883
+ id: response.id,
884
+ timestamp: response.createdAt ? new Date(response.createdAt * 1e3) : void 0,
885
+ modelId: response.model
2538
886
  });
887
+ const finishReason = mapOpenRouterFinishReason(
888
+ response.status === "completed" ? "stop" : response.status ?? "stop"
889
+ );
890
+ const usage = buildUsage(
891
+ response.usage ? {
892
+ inputTokens: response.usage.inputTokens,
893
+ outputTokens: response.usage.outputTokens
894
+ } : void 0
895
+ );
896
+ const providerMetadata = buildProviderMetadata({
897
+ id: response.id,
898
+ provider: void 0,
899
+ // Responses API doesn't expose provider
900
+ usage: response.usage ? {
901
+ promptTokens: response.usage.inputTokens,
902
+ completionTokens: response.usage.outputTokens,
903
+ totalTokens: response.usage.totalTokens,
904
+ cost: response.usage.cost ?? void 0,
905
+ // Map inputTokensDetails -> promptTokensDetails
906
+ promptTokensDetails: response.usage.inputTokensDetails ? {
907
+ cachedTokens: response.usage.inputTokensDetails.cachedTokens
908
+ } : void 0,
909
+ // Map outputTokensDetails -> completionTokensDetails
910
+ completionTokensDetails: response.usage.outputTokensDetails ? {
911
+ reasoningTokens: response.usage.outputTokensDetails.reasoningTokens
912
+ } : void 0
913
+ } : void 0
914
+ });
915
+ parts.push({
916
+ type: "finish",
917
+ finishReason,
918
+ usage,
919
+ providerMetadata
920
+ });
921
+ break;
2539
922
  }
2540
- return __spreadValues(__spreadValues({
2541
- // model id:
2542
- model: this.modelId,
2543
- models: this.settings.models,
2544
- // model specific settings:
2545
- logit_bias: this.settings.logitBias,
2546
- logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
2547
- suffix: this.settings.suffix,
2548
- user: this.settings.user,
2549
- // standardized settings:
2550
- max_tokens: maxOutputTokens,
2551
- temperature,
2552
- top_p: topP,
2553
- frequency_penalty: frequencyPenalty,
2554
- presence_penalty: presencePenalty,
2555
- seed,
2556
- stop: stopSequences,
2557
- response_format: responseFormat,
2558
- top_k: topK,
2559
- // prompt:
2560
- prompt: completionPrompt,
2561
- // OpenRouter specific settings:
2562
- include_reasoning: this.settings.includeReasoning,
2563
- reasoning: this.settings.reasoning
2564
- }, this.config.extraBody), this.settings.extraBody);
2565
- }
2566
- async doGenerate(options) {
2567
- var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
2568
- const providerOptions = options.providerOptions || {};
2569
- const openrouterOptions = providerOptions.openrouter || {};
2570
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
2571
- const { value: response, responseHeaders } = await postJsonToApi({
2572
- url: this.config.url({
2573
- path: "/completions",
2574
- modelId: this.modelId
2575
- }),
2576
- headers: combineHeaders(this.config.headers(), options.headers),
2577
- body: args,
2578
- failedResponseHandler: openrouterFailedResponseHandler,
2579
- successfulResponseHandler: createJsonResponseHandler(
2580
- OpenRouterCompletionChunkSchema
2581
- ),
2582
- abortSignal: options.abortSignal,
2583
- fetch: this.config.fetch
2584
- });
2585
- if ("error" in response) {
2586
- const errorData = response.error;
2587
- throw new APICallError({
2588
- message: errorData.message,
2589
- url: this.config.url({
2590
- path: "/completions",
2591
- modelId: this.modelId
2592
- }),
2593
- requestBodyValues: args,
2594
- statusCode: 200,
2595
- responseHeaders,
2596
- data: errorData
923
+ // Response incomplete or failed
924
+ case "response.incomplete":
925
+ case "response.failed": {
926
+ if (state.textStarted && !state.textEnded) {
927
+ state.textEnded = true;
928
+ parts.push({
929
+ type: "text-end",
930
+ id: state.textId
931
+ });
932
+ }
933
+ if (state.reasoningStarted && !state.reasoningEnded) {
934
+ state.reasoningEnded = true;
935
+ parts.push({
936
+ type: "reasoning-end",
937
+ id: state.reasoningId
938
+ });
939
+ }
940
+ const response = event.response;
941
+ parts.push({
942
+ type: "response-metadata",
943
+ id: response.id,
944
+ timestamp: response.createdAt ? new Date(response.createdAt * 1e3) : void 0,
945
+ modelId: response.model
2597
946
  });
947
+ const finishReason = event.type === "response.failed" ? mapOpenRouterFinishReason("error") : mapOpenRouterFinishReason(response.status ?? "incomplete");
948
+ const usage = buildUsage(
949
+ response.usage ? {
950
+ inputTokens: response.usage.inputTokens,
951
+ outputTokens: response.usage.outputTokens
952
+ } : void 0
953
+ );
954
+ const providerMetadata = buildProviderMetadata({
955
+ id: response.id,
956
+ provider: void 0,
957
+ usage: response.usage ? {
958
+ promptTokens: response.usage.inputTokens,
959
+ completionTokens: response.usage.outputTokens,
960
+ totalTokens: response.usage.totalTokens,
961
+ cost: response.usage.cost ?? void 0,
962
+ // Map inputTokensDetails -> promptTokensDetails
963
+ promptTokensDetails: response.usage.inputTokensDetails ? {
964
+ cachedTokens: response.usage.inputTokensDetails.cachedTokens
965
+ } : void 0,
966
+ // Map outputTokensDetails -> completionTokensDetails
967
+ completionTokensDetails: response.usage.outputTokensDetails ? {
968
+ reasoningTokens: response.usage.outputTokensDetails.reasoningTokens
969
+ } : void 0
970
+ } : void 0
971
+ });
972
+ parts.push({
973
+ type: "finish",
974
+ finishReason,
975
+ usage,
976
+ providerMetadata
977
+ });
978
+ break;
2598
979
  }
2599
- const choice = response.choices[0];
2600
- if (!choice) {
2601
- throw new NoContentGeneratedError({
2602
- message: "No choice in OpenRouter completion response"
980
+ // Error event
981
+ case "error": {
982
+ const errorEvent = event;
983
+ parts.push({
984
+ type: "error",
985
+ error: new Error(
986
+ errorEvent.error?.message ?? "Unknown streaming error"
987
+ )
2603
988
  });
989
+ break;
2604
990
  }
991
+ // Ignored events (handled implicitly or not needed)
992
+ case "response.output_item.done":
993
+ case "response.content_part.added":
994
+ case "response.content_part.done":
995
+ case "response.refusal.delta":
996
+ case "response.refusal.done":
997
+ case "response.reasoning_text.done":
998
+ case "response.reasoning_summary_part.added":
999
+ case "response.reasoning_summary_part.done":
1000
+ case "response.reasoning_summary_text.delta":
1001
+ case "response.reasoning_summary_text.done":
1002
+ case "response.image_generation_call.in_progress":
1003
+ case "response.image_generation_call.generating":
1004
+ case "response.image_generation_call.partial_image":
1005
+ case "response.image_generation_call.completed":
1006
+ break;
1007
+ }
1008
+ return parts;
1009
+ }
1010
+ function convertToolsToResponsesFormat(tools, warnings) {
1011
+ if (!tools || tools.length === 0) {
1012
+ return [];
1013
+ }
1014
+ return tools.map((tool) => {
1015
+ if (tool.type !== "function") {
1016
+ warnings.push({
1017
+ type: "unsupported",
1018
+ feature: `tool type '${tool.type}'`,
1019
+ details: `Only 'function' type tools are supported. Tool '${tool.name ?? "unknown"}' has type '${tool.type}'.`
1020
+ });
1021
+ return null;
1022
+ }
1023
+ const functionTool = tool;
2605
1024
  return {
2606
- content: [
2607
- {
2608
- type: "text",
2609
- text: (_a15 = choice.text) != null ? _a15 : ""
2610
- }
2611
- ],
2612
- finishReason: mapOpenRouterFinishReason(choice.finish_reason),
2613
- usage: {
2614
- inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : 0,
2615
- outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : 0,
2616
- totalTokens: ((_g = (_f = response.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : 0) + ((_i = (_h = response.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : 0),
2617
- reasoningTokens: (_l = (_k = (_j = response.usage) == null ? void 0 : _j.completion_tokens_details) == null ? void 0 : _k.reasoning_tokens) != null ? _l : 0,
2618
- cachedInputTokens: (_o = (_n = (_m = response.usage) == null ? void 0 : _m.prompt_tokens_details) == null ? void 0 : _n.cached_tokens) != null ? _o : 0
2619
- },
2620
- warnings: [],
2621
- response: {
2622
- headers: responseHeaders
2623
- }
1025
+ type: "function",
1026
+ name: functionTool.name,
1027
+ description: functionTool.description ?? null,
1028
+ parameters: functionTool.inputSchema
2624
1029
  };
1030
+ }).filter((tool) => tool !== null);
1031
+ }
1032
+ function convertToolChoiceToResponsesFormat(toolChoice) {
1033
+ if (!toolChoice) {
1034
+ return void 0;
2625
1035
  }
2626
- async doStream(options) {
2627
- const providerOptions = options.providerOptions || {};
2628
- const openrouterOptions = providerOptions.openrouter || {};
2629
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
2630
- const { value: response, responseHeaders } = await postJsonToApi({
2631
- url: this.config.url({
2632
- path: "/completions",
2633
- modelId: this.modelId
2634
- }),
2635
- headers: combineHeaders(this.config.headers(), options.headers),
2636
- body: __spreadProps(__spreadValues({}, args), {
2637
- stream: true,
2638
- // only include stream_options when in strict compatibility mode:
2639
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
2640
- }),
2641
- failedResponseHandler: openrouterFailedResponseHandler,
2642
- successfulResponseHandler: createEventSourceResponseHandler(
2643
- OpenRouterCompletionChunkSchema
2644
- ),
2645
- abortSignal: options.abortSignal,
2646
- fetch: this.config.fetch
2647
- });
2648
- let finishReason = "other";
2649
- const usage = {
2650
- inputTokens: Number.NaN,
2651
- outputTokens: Number.NaN,
2652
- totalTokens: Number.NaN,
2653
- reasoningTokens: Number.NaN,
2654
- cachedInputTokens: Number.NaN
1036
+ switch (toolChoice.type) {
1037
+ case "auto":
1038
+ return "auto";
1039
+ case "none":
1040
+ return "none";
1041
+ case "required":
1042
+ return "required";
1043
+ case "tool":
1044
+ return {
1045
+ type: "function",
1046
+ name: toolChoice.toolName
1047
+ };
1048
+ default:
1049
+ return void 0;
1050
+ }
1051
+ }
1052
+ function convertResponseFormatToText(responseFormat) {
1053
+ if (!responseFormat) {
1054
+ return void 0;
1055
+ }
1056
+ if (responseFormat.type === "text") {
1057
+ return {
1058
+ format: { type: "text" }
2655
1059
  };
2656
- const openrouterUsage = {};
1060
+ }
1061
+ if (responseFormat.type === "json") {
1062
+ if (responseFormat.schema) {
1063
+ return {
1064
+ format: {
1065
+ type: "json_schema",
1066
+ name: responseFormat.name ?? "response",
1067
+ description: responseFormat.description,
1068
+ schema: responseFormat.schema
1069
+ }
1070
+ };
1071
+ }
2657
1072
  return {
2658
- stream: response.pipeThrough(
2659
- new TransformStream({
2660
- transform(chunk, controller) {
2661
- var _a15, _b, _c;
2662
- if (!chunk.success) {
2663
- finishReason = "error";
2664
- controller.enqueue({ type: "error", error: chunk.error });
2665
- return;
2666
- }
2667
- const value = chunk.value;
2668
- if ("error" in value) {
2669
- finishReason = "error";
2670
- controller.enqueue({ type: "error", error: value.error });
2671
- return;
2672
- }
2673
- if (value.usage != null) {
2674
- usage.inputTokens = value.usage.prompt_tokens;
2675
- usage.outputTokens = value.usage.completion_tokens;
2676
- usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
2677
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
2678
- if (value.usage.prompt_tokens_details) {
2679
- const cachedInputTokens = (_a15 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a15 : 0;
2680
- usage.cachedInputTokens = cachedInputTokens;
2681
- openrouterUsage.promptTokensDetails = {
2682
- cachedTokens: cachedInputTokens
2683
- };
2684
- }
2685
- openrouterUsage.completionTokens = value.usage.completion_tokens;
2686
- if (value.usage.completion_tokens_details) {
2687
- const reasoningTokens = (_b = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b : 0;
2688
- usage.reasoningTokens = reasoningTokens;
2689
- openrouterUsage.completionTokensDetails = {
2690
- reasoningTokens
2691
- };
2692
- }
2693
- openrouterUsage.cost = value.usage.cost;
2694
- openrouterUsage.totalTokens = value.usage.total_tokens;
2695
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
2696
- if (upstreamInferenceCost != null) {
2697
- openrouterUsage.costDetails = {
2698
- upstreamInferenceCost
2699
- };
2700
- }
2701
- }
2702
- const choice = value.choices[0];
2703
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
2704
- finishReason = mapOpenRouterFinishReason(choice.finish_reason);
2705
- }
2706
- if ((choice == null ? void 0 : choice.text) != null) {
2707
- controller.enqueue({
2708
- type: "text-delta",
2709
- delta: choice.text,
2710
- id: generateId()
2711
- });
2712
- }
2713
- },
2714
- flush(controller) {
2715
- controller.enqueue({
2716
- type: "finish",
2717
- finishReason,
2718
- usage,
2719
- providerMetadata: {
2720
- openrouter: {
2721
- usage: openrouterUsage
2722
- }
2723
- }
2724
- });
2725
- }
2726
- })
2727
- ),
2728
- response: {
2729
- headers: responseHeaders
2730
- }
1073
+ format: { type: "json_object" }
2731
1074
  };
2732
1075
  }
2733
- };
2734
-
2735
- // src/embedding/schemas.ts
2736
- var import_v48 = require("zod/v4");
2737
- var openrouterEmbeddingUsageSchema = import_v48.z.object({
2738
- prompt_tokens: import_v48.z.number(),
2739
- total_tokens: import_v48.z.number(),
2740
- cost: import_v48.z.number().optional()
2741
- });
2742
- var openrouterEmbeddingDataSchema = import_v48.z.object({
2743
- object: import_v48.z.literal("embedding"),
2744
- embedding: import_v48.z.array(import_v48.z.number()),
2745
- index: import_v48.z.number().optional()
2746
- });
2747
- var OpenRouterEmbeddingResponseSchema = import_v48.z.object({
2748
- id: import_v48.z.string().optional(),
2749
- object: import_v48.z.literal("list"),
2750
- data: import_v48.z.array(openrouterEmbeddingDataSchema),
2751
- model: import_v48.z.string(),
2752
- usage: openrouterEmbeddingUsageSchema.optional()
2753
- });
1076
+ return void 0;
1077
+ }
2754
1078
 
2755
- // src/embedding/index.ts
1079
+ // src/embedding/openrouter-embedding-model.ts
1080
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
1081
+ var import_sdk2 = require("@openrouter/sdk");
2756
1082
  var OpenRouterEmbeddingModel = class {
2757
- constructor(modelId, settings, config) {
2758
- this.specificationVersion = "v2";
2759
- this.provider = "openrouter";
2760
- this.maxEmbeddingsPerCall = void 0;
2761
- this.supportsParallelCalls = true;
1083
+ specificationVersion = "v3";
1084
+ provider = "openrouter";
1085
+ modelId;
1086
+ settings;
1087
+ /**
1088
+ * Maximum number of embeddings that can be generated in a single API call.
1089
+ * Set to 2048 as a reasonable default for most embedding models.
1090
+ */
1091
+ maxEmbeddingsPerCall = 2048;
1092
+ /**
1093
+ * Whether the model supports parallel calls.
1094
+ */
1095
+ supportsParallelCalls = true;
1096
+ constructor(modelId, settings) {
2762
1097
  this.modelId = modelId;
2763
1098
  this.settings = settings;
2764
- this.config = config;
2765
1099
  }
2766
1100
  async doEmbed(options) {
2767
- var _a15;
2768
- const { values, abortSignal, headers } = options;
2769
- const args = __spreadValues(__spreadValues({
1101
+ const warnings = [];
1102
+ const client = new import_sdk2.OpenRouter({
1103
+ apiKey: this.settings.apiKey,
1104
+ serverURL: this.settings.baseURL
1105
+ });
1106
+ const requestParams = {
2770
1107
  model: this.modelId,
2771
- input: values,
2772
- user: this.settings.user,
2773
- provider: this.settings.provider
2774
- }, this.config.extraBody), this.settings.extraBody);
2775
- const { value: responseValue, responseHeaders } = await postJsonToApi({
2776
- url: this.config.url({
2777
- path: "/embeddings",
2778
- modelId: this.modelId
2779
- }),
2780
- headers: combineHeaders(this.config.headers(), headers),
2781
- body: args,
2782
- failedResponseHandler: openrouterFailedResponseHandler,
2783
- successfulResponseHandler: createJsonResponseHandler(
2784
- OpenRouterEmbeddingResponseSchema
2785
- ),
2786
- abortSignal,
2787
- fetch: this.config.fetch
1108
+ input: options.values
1109
+ };
1110
+ const modelOptions = this.settings.modelOptions;
1111
+ if (modelOptions?.user) {
1112
+ requestParams.user = modelOptions.user;
1113
+ }
1114
+ if (modelOptions?.provider) {
1115
+ requestParams.provider = modelOptions.provider;
1116
+ }
1117
+ const combinedHeaders = (0, import_provider_utils2.normalizeHeaders)(
1118
+ (0, import_provider_utils2.combineHeaders)(this.settings.headers, options.headers)
1119
+ );
1120
+ const response = await client.embeddings.generate(requestParams, {
1121
+ fetchOptions: {
1122
+ signal: options.abortSignal,
1123
+ headers: combinedHeaders
1124
+ }
2788
1125
  });
2789
- return {
2790
- embeddings: responseValue.data.map((item) => item.embedding),
2791
- usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
2792
- providerMetadata: ((_a15 = responseValue.usage) == null ? void 0 : _a15.cost) ? {
2793
- openrouter: {
2794
- usage: {
2795
- cost: responseValue.usage.cost
2796
- }
2797
- }
2798
- } : void 0,
2799
- response: {
2800
- headers: responseHeaders,
2801
- body: responseValue
1126
+ if (typeof response === "string") {
1127
+ throw new Error(`Unexpected string response from embeddings API: ${response}`);
1128
+ }
1129
+ const responseBody = response;
1130
+ const sortedData = [...responseBody.data].sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
1131
+ const embeddings = sortedData.map((item) => {
1132
+ if (typeof item.embedding === "string") {
1133
+ throw new Error("Base64 encoded embeddings are not supported");
2802
1134
  }
1135
+ return item.embedding;
1136
+ });
1137
+ return {
1138
+ embeddings,
1139
+ usage: responseBody.usage ? { tokens: responseBody.usage.promptTokens } : void 0,
1140
+ warnings
2803
1141
  };
2804
1142
  }
2805
1143
  };
2806
1144
 
2807
- // src/facade.ts
2808
- var OpenRouter = class {
1145
+ // src/image/openrouter-image-model.ts
1146
+ var OpenRouterImageModel = class {
1147
+ specificationVersion = "v3";
1148
+ provider = "openrouter";
1149
+ modelId;
2809
1150
  /**
2810
- * Creates a new OpenRouter provider instance.
1151
+ * Maximum number of images that can be generated in a single API call.
2811
1152
  */
2812
- constructor(options = {}) {
2813
- var _a15, _b;
2814
- this.baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
2815
- this.apiKey = options.apiKey;
2816
- this.headers = options.headers;
2817
- this.api_keys = options.api_keys;
2818
- }
2819
- get baseConfig() {
2820
- return {
2821
- baseURL: this.baseURL,
2822
- headers: () => __spreadValues(__spreadValues({
2823
- Authorization: `Bearer ${loadApiKey({
2824
- apiKey: this.apiKey,
2825
- environmentVariableName: "OPENROUTER_API_KEY",
2826
- description: "OpenRouter"
2827
- })}`
2828
- }, this.headers), this.api_keys && Object.keys(this.api_keys).length > 0 && {
2829
- "X-Provider-API-Keys": JSON.stringify(this.api_keys)
2830
- })
2831
- };
2832
- }
2833
- chat(modelId, settings = {}) {
2834
- return new OpenRouterChatLanguageModel(modelId, settings, __spreadProps(__spreadValues({
2835
- provider: "openrouter.chat"
2836
- }, this.baseConfig), {
2837
- compatibility: "strict",
2838
- url: ({ path }) => `${this.baseURL}${path}`
2839
- }));
2840
- }
2841
- completion(modelId, settings = {}) {
2842
- return new OpenRouterCompletionLanguageModel(modelId, settings, __spreadProps(__spreadValues({
2843
- provider: "openrouter.completion"
2844
- }, this.baseConfig), {
2845
- compatibility: "strict",
2846
- url: ({ path }) => `${this.baseURL}${path}`
2847
- }));
2848
- }
2849
- textEmbeddingModel(modelId, settings = {}) {
2850
- return new OpenRouterEmbeddingModel(modelId, settings, __spreadProps(__spreadValues({
2851
- provider: "openrouter.embedding"
2852
- }, this.baseConfig), {
2853
- url: ({ path }) => `${this.baseURL}${path}`
2854
- }));
1153
+ maxImagesPerCall = 1;
1154
+ constructor(modelId, _settings) {
1155
+ this.modelId = modelId;
2855
1156
  }
2856
- /**
2857
- * @deprecated Use textEmbeddingModel instead
2858
- */
2859
- embedding(modelId, settings = {}) {
2860
- return this.textEmbeddingModel(modelId, settings);
1157
+ async doGenerate(_options) {
1158
+ throw new Error(
1159
+ "Image generation not yet supported. See: https://github.com/OpenRouterTeam/ai-sdk-provider/issues/new?title=Image+generation+support"
1160
+ );
2861
1161
  }
2862
1162
  };
2863
1163
 
2864
- // src/utils/remove-undefined.ts
2865
- function removeUndefinedEntries2(record) {
2866
- return Object.fromEntries(
2867
- Object.entries(record).filter(([, value]) => value !== null)
2868
- );
2869
- }
2870
-
2871
- // src/utils/with-user-agent-suffix.ts
2872
- function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2873
- const cleanedHeaders = removeUndefinedEntries2(
2874
- headers != null ? headers : {}
2875
- );
2876
- const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
2877
- const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ");
2878
- return __spreadProps(__spreadValues({}, cleanedHeaders), {
2879
- "user-agent": newUserAgent
2880
- });
2881
- }
2882
-
2883
- // src/version.ts
2884
- var VERSION = false ? "0.0.0-test" : "1.5.3";
2885
-
2886
- // src/provider.ts
1164
+ // src/openrouter-provider.ts
2887
1165
  function createOpenRouter(options = {}) {
2888
- var _a15, _b, _c;
2889
- const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
2890
- const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
2891
- const getHeaders = () => withUserAgentSuffix(
2892
- __spreadValues(__spreadValues({
2893
- Authorization: `Bearer ${loadApiKey({
2894
- apiKey: options.apiKey,
2895
- environmentVariableName: "OPENROUTER_API_KEY",
2896
- description: "OpenRouter"
2897
- })}`
2898
- }, options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
2899
- "X-Provider-API-Keys": JSON.stringify(options.api_keys)
2900
- }),
2901
- `ai-sdk/openrouter/${VERSION}`
1166
+ const baseURL = (0, import_provider_utils3.withoutTrailingSlash)(
1167
+ options.baseURL ?? options.baseUrl ?? "https://openrouter.ai/api/v1"
2902
1168
  );
2903
- const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
2904
- provider: "openrouter.chat",
2905
- url: ({ path }) => `${baseURL}${path}`,
2906
- headers: getHeaders,
2907
- compatibility,
2908
- fetch: options.fetch,
2909
- extraBody: options.extraBody
2910
- });
2911
- const createCompletionModel = (modelId, settings = {}) => new OpenRouterCompletionLanguageModel(modelId, settings, {
2912
- provider: "openrouter.completion",
2913
- url: ({ path }) => `${baseURL}${path}`,
2914
- headers: getHeaders,
2915
- compatibility,
2916
- fetch: options.fetch,
2917
- extraBody: options.extraBody
2918
- });
2919
- const createEmbeddingModel = (modelId, settings = {}) => new OpenRouterEmbeddingModel(modelId, settings, {
2920
- provider: "openrouter.embedding",
2921
- url: ({ path }) => `${baseURL}${path}`,
2922
- headers: getHeaders,
2923
- fetch: options.fetch,
2924
- extraBody: options.extraBody
2925
- });
2926
- const createLanguageModel = (modelId, settings) => {
2927
- if (new.target) {
2928
- throw new Error(
2929
- "The OpenRouter model function cannot be called with the new keyword."
2930
- );
2931
- }
2932
- if (modelId === "openai/gpt-3.5-turbo-instruct") {
2933
- return createCompletionModel(
2934
- modelId,
2935
- settings
2936
- );
2937
- }
2938
- return createChatModel(modelId, settings);
1169
+ const getModelSettings = (modelOptions) => {
1170
+ const apiKey = (0, import_provider_utils3.loadApiKey)({
1171
+ apiKey: options.apiKey,
1172
+ environmentVariableName: "OPENROUTER_API_KEY",
1173
+ description: "OpenRouter"
1174
+ });
1175
+ return {
1176
+ apiKey,
1177
+ baseURL,
1178
+ headers: options.headers,
1179
+ fetch: options.fetch,
1180
+ extraBody: options.extraBody,
1181
+ modelOptions
1182
+ };
2939
1183
  };
2940
- const provider = (modelId, settings) => createLanguageModel(modelId, settings);
2941
- provider.languageModel = createLanguageModel;
2942
- provider.chat = createChatModel;
2943
- provider.completion = createCompletionModel;
2944
- provider.textEmbeddingModel = createEmbeddingModel;
2945
- provider.embedding = createEmbeddingModel;
1184
+ const languageModel = (modelId, modelOptions) => {
1185
+ return new OpenRouterChatLanguageModel(modelId, getModelSettings(modelOptions));
1186
+ };
1187
+ const embeddingModel = (modelId, modelOptions) => {
1188
+ return new OpenRouterEmbeddingModel(modelId, getModelSettings(modelOptions));
1189
+ };
1190
+ const imageModel = (modelId, modelOptions) => {
1191
+ return new OpenRouterImageModel(modelId, getModelSettings(modelOptions));
1192
+ };
1193
+ const provider = Object.assign(
1194
+ // Make provider callable - calling it directly creates a language model
1195
+ (modelId, modelOptions) => languageModel(modelId, modelOptions),
1196
+ {
1197
+ specificationVersion: "v3",
1198
+ languageModel,
1199
+ chat: languageModel,
1200
+ embeddingModel,
1201
+ textEmbeddingModel: embeddingModel,
1202
+ imageModel,
1203
+ image: imageModel,
1204
+ embedding: embeddingModel
1205
+ }
1206
+ );
2946
1207
  return provider;
2947
1208
  }
2948
- var openrouter = createOpenRouter({
2949
- compatibility: "strict"
2950
- // strict for OpenRouter API
2951
- });
2952
1209
 
2953
- // src/toon/index.ts
2954
- async function getToonModule() {
2955
- try {
2956
- return await import("@toon-format/toon");
2957
- } catch (e) {
2958
- throw new Error(
2959
- "The @toon-format/toon package is required for TOON encoding/decoding. Install it with: npm install @toon-format/toon"
2960
- );
2961
- }
2962
- }
2963
- async function encodeToon(value, options) {
2964
- const toon = await getToonModule();
2965
- return toon.encode(value, options);
2966
- }
2967
- async function decodeToon(input, options) {
2968
- const toon = await getToonModule();
2969
- return toon.decode(input, options);
2970
- }
1210
+ // src/version.ts
1211
+ var VERSION = "6.0.0-alpha.0";
1212
+
1213
+ // src/index.ts
1214
+ var openrouter = createOpenRouter();
2971
1215
  // Annotate the CommonJS export names for ESM import in node:
2972
1216
  0 && (module.exports = {
2973
- OpenRouter,
1217
+ VERSION,
2974
1218
  createOpenRouter,
2975
- decodeToon,
2976
- encodeToon,
2977
1219
  openrouter
2978
1220
  });
2979
1221
  //# sourceMappingURL=index.js.map