@openrouter/ai-sdk-provider 1.5.4 → 6.0.0-alpha.1

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