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