@openrouter/ai-sdk-provider 0.7.1 → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +86 -59
- package/dist/index.d.ts +86 -59
- package/dist/index.js +1427 -613
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1387 -601
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +46 -19
- package/dist/internal/index.d.ts +46 -19
- package/dist/internal/index.js +1375 -602
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1336 -591
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +11 -16
package/dist/internal/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __defProps = Object.defineProperties;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
7
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
8
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
10
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
11
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
12
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
@@ -20,21 +22,9 @@ var __spreadValues = (a, b) => {
|
|
|
20
22
|
return a;
|
|
21
23
|
};
|
|
22
24
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
-
var __objRest = (source, exclude) => {
|
|
24
|
-
var target = {};
|
|
25
|
-
for (var prop in source)
|
|
26
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
-
target[prop] = source[prop];
|
|
28
|
-
if (source != null && __getOwnPropSymbols)
|
|
29
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
-
target[prop] = source[prop];
|
|
32
|
-
}
|
|
33
|
-
return target;
|
|
34
|
-
};
|
|
35
25
|
var __export = (target, all) => {
|
|
36
|
-
for (var
|
|
37
|
-
__defProp(target,
|
|
26
|
+
for (var name14 in all)
|
|
27
|
+
__defProp(target, name14, { get: all[name14], enumerable: true });
|
|
38
28
|
};
|
|
39
29
|
var __copyProps = (to, from, except, desc) => {
|
|
40
30
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -44,6 +34,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
44
34
|
}
|
|
45
35
|
return to;
|
|
46
36
|
};
|
|
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
|
+
));
|
|
47
45
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
48
46
|
|
|
49
47
|
// src/internal/index.ts
|
|
@@ -54,72 +52,929 @@ __export(index_exports, {
|
|
|
54
52
|
});
|
|
55
53
|
module.exports = __toCommonJS(index_exports);
|
|
56
54
|
|
|
57
|
-
//
|
|
55
|
+
// node_modules/.pnpm/@ai-sdk+provider@2.0.0-beta.1/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
56
|
+
var marker = "vercel.ai.error";
|
|
57
|
+
var symbol = Symbol.for(marker);
|
|
58
|
+
var _a;
|
|
59
|
+
var _AISDKError = class _AISDKError2 extends Error {
|
|
60
|
+
/**
|
|
61
|
+
* Creates an AI SDK Error.
|
|
62
|
+
*
|
|
63
|
+
* @param {Object} params - The parameters for creating the error.
|
|
64
|
+
* @param {string} params.name - The name of the error.
|
|
65
|
+
* @param {string} params.message - The error message.
|
|
66
|
+
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
67
|
+
*/
|
|
68
|
+
constructor({
|
|
69
|
+
name: name14,
|
|
70
|
+
message,
|
|
71
|
+
cause
|
|
72
|
+
}) {
|
|
73
|
+
super(message);
|
|
74
|
+
this[_a] = true;
|
|
75
|
+
this.name = name14;
|
|
76
|
+
this.cause = cause;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Checks if the given error is an AI SDK Error.
|
|
80
|
+
* @param {unknown} error - The error to check.
|
|
81
|
+
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
82
|
+
*/
|
|
83
|
+
static isInstance(error) {
|
|
84
|
+
return _AISDKError2.hasMarker(error, marker);
|
|
85
|
+
}
|
|
86
|
+
static hasMarker(error, marker15) {
|
|
87
|
+
const markerSymbol = Symbol.for(marker15);
|
|
88
|
+
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
_a = symbol;
|
|
92
|
+
var AISDKError = _AISDKError;
|
|
93
|
+
var name = "AI_APICallError";
|
|
94
|
+
var marker2 = `vercel.ai.error.${name}`;
|
|
95
|
+
var symbol2 = Symbol.for(marker2);
|
|
96
|
+
var _a2;
|
|
97
|
+
var APICallError = class extends AISDKError {
|
|
98
|
+
constructor({
|
|
99
|
+
message,
|
|
100
|
+
url,
|
|
101
|
+
requestBodyValues,
|
|
102
|
+
statusCode,
|
|
103
|
+
responseHeaders,
|
|
104
|
+
responseBody,
|
|
105
|
+
cause,
|
|
106
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
|
107
|
+
statusCode === 409 || // conflict
|
|
108
|
+
statusCode === 429 || // too many requests
|
|
109
|
+
statusCode >= 500),
|
|
110
|
+
// server error
|
|
111
|
+
data
|
|
112
|
+
}) {
|
|
113
|
+
super({ name, message, cause });
|
|
114
|
+
this[_a2] = true;
|
|
115
|
+
this.url = url;
|
|
116
|
+
this.requestBodyValues = requestBodyValues;
|
|
117
|
+
this.statusCode = statusCode;
|
|
118
|
+
this.responseHeaders = responseHeaders;
|
|
119
|
+
this.responseBody = responseBody;
|
|
120
|
+
this.isRetryable = isRetryable;
|
|
121
|
+
this.data = data;
|
|
122
|
+
}
|
|
123
|
+
static isInstance(error) {
|
|
124
|
+
return AISDKError.hasMarker(error, marker2);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
_a2 = symbol2;
|
|
128
|
+
var name2 = "AI_EmptyResponseBodyError";
|
|
129
|
+
var marker3 = `vercel.ai.error.${name2}`;
|
|
130
|
+
var symbol3 = Symbol.for(marker3);
|
|
131
|
+
var _a3;
|
|
132
|
+
var EmptyResponseBodyError = class extends AISDKError {
|
|
133
|
+
// used in isInstance
|
|
134
|
+
constructor({ message = "Empty response body" } = {}) {
|
|
135
|
+
super({ name: name2, message });
|
|
136
|
+
this[_a3] = true;
|
|
137
|
+
}
|
|
138
|
+
static isInstance(error) {
|
|
139
|
+
return AISDKError.hasMarker(error, marker3);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
_a3 = symbol3;
|
|
143
|
+
function getErrorMessage(error) {
|
|
144
|
+
if (error == null) {
|
|
145
|
+
return "unknown error";
|
|
146
|
+
}
|
|
147
|
+
if (typeof error === "string") {
|
|
148
|
+
return error;
|
|
149
|
+
}
|
|
150
|
+
if (error instanceof Error) {
|
|
151
|
+
return error.message;
|
|
152
|
+
}
|
|
153
|
+
return JSON.stringify(error);
|
|
154
|
+
}
|
|
155
|
+
var name3 = "AI_InvalidArgumentError";
|
|
156
|
+
var marker4 = `vercel.ai.error.${name3}`;
|
|
157
|
+
var symbol4 = Symbol.for(marker4);
|
|
158
|
+
var _a4;
|
|
159
|
+
var InvalidArgumentError = class extends AISDKError {
|
|
160
|
+
constructor({
|
|
161
|
+
message,
|
|
162
|
+
cause,
|
|
163
|
+
argument
|
|
164
|
+
}) {
|
|
165
|
+
super({ name: name3, message, cause });
|
|
166
|
+
this[_a4] = true;
|
|
167
|
+
this.argument = argument;
|
|
168
|
+
}
|
|
169
|
+
static isInstance(error) {
|
|
170
|
+
return AISDKError.hasMarker(error, marker4);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
_a4 = symbol4;
|
|
174
|
+
var name4 = "AI_InvalidPromptError";
|
|
175
|
+
var marker5 = `vercel.ai.error.${name4}`;
|
|
176
|
+
var symbol5 = Symbol.for(marker5);
|
|
177
|
+
var _a5;
|
|
178
|
+
var InvalidPromptError = class extends AISDKError {
|
|
179
|
+
constructor({
|
|
180
|
+
prompt,
|
|
181
|
+
message,
|
|
182
|
+
cause
|
|
183
|
+
}) {
|
|
184
|
+
super({ name: name4, message: `Invalid prompt: ${message}`, cause });
|
|
185
|
+
this[_a5] = true;
|
|
186
|
+
this.prompt = prompt;
|
|
187
|
+
}
|
|
188
|
+
static isInstance(error) {
|
|
189
|
+
return AISDKError.hasMarker(error, marker5);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
_a5 = symbol5;
|
|
193
|
+
var name5 = "AI_InvalidResponseDataError";
|
|
194
|
+
var marker6 = `vercel.ai.error.${name5}`;
|
|
195
|
+
var symbol6 = Symbol.for(marker6);
|
|
196
|
+
var _a6;
|
|
197
|
+
var InvalidResponseDataError = class extends AISDKError {
|
|
198
|
+
constructor({
|
|
199
|
+
data,
|
|
200
|
+
message = `Invalid response data: ${JSON.stringify(data)}.`
|
|
201
|
+
}) {
|
|
202
|
+
super({ name: name5, message });
|
|
203
|
+
this[_a6] = true;
|
|
204
|
+
this.data = data;
|
|
205
|
+
}
|
|
206
|
+
static isInstance(error) {
|
|
207
|
+
return AISDKError.hasMarker(error, marker6);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
_a6 = symbol6;
|
|
211
|
+
var name6 = "AI_JSONParseError";
|
|
212
|
+
var marker7 = `vercel.ai.error.${name6}`;
|
|
213
|
+
var symbol7 = Symbol.for(marker7);
|
|
214
|
+
var _a7;
|
|
215
|
+
var JSONParseError = class extends AISDKError {
|
|
216
|
+
constructor({ text, cause }) {
|
|
217
|
+
super({
|
|
218
|
+
name: name6,
|
|
219
|
+
message: `JSON parsing failed: Text: ${text}.
|
|
220
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
221
|
+
cause
|
|
222
|
+
});
|
|
223
|
+
this[_a7] = true;
|
|
224
|
+
this.text = text;
|
|
225
|
+
}
|
|
226
|
+
static isInstance(error) {
|
|
227
|
+
return AISDKError.hasMarker(error, marker7);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
_a7 = symbol7;
|
|
231
|
+
var name7 = "AI_LoadAPIKeyError";
|
|
232
|
+
var marker8 = `vercel.ai.error.${name7}`;
|
|
233
|
+
var symbol8 = Symbol.for(marker8);
|
|
234
|
+
var _a8;
|
|
235
|
+
_a8 = symbol8;
|
|
236
|
+
var name8 = "AI_LoadSettingError";
|
|
237
|
+
var marker9 = `vercel.ai.error.${name8}`;
|
|
238
|
+
var symbol9 = Symbol.for(marker9);
|
|
239
|
+
var _a9;
|
|
240
|
+
_a9 = symbol9;
|
|
241
|
+
var name9 = "AI_NoContentGeneratedError";
|
|
242
|
+
var marker10 = `vercel.ai.error.${name9}`;
|
|
243
|
+
var symbol10 = Symbol.for(marker10);
|
|
244
|
+
var _a10;
|
|
245
|
+
_a10 = symbol10;
|
|
246
|
+
var name10 = "AI_NoSuchModelError";
|
|
247
|
+
var marker11 = `vercel.ai.error.${name10}`;
|
|
248
|
+
var symbol11 = Symbol.for(marker11);
|
|
249
|
+
var _a11;
|
|
250
|
+
_a11 = symbol11;
|
|
251
|
+
var name11 = "AI_TooManyEmbeddingValuesForCallError";
|
|
252
|
+
var marker12 = `vercel.ai.error.${name11}`;
|
|
253
|
+
var symbol12 = Symbol.for(marker12);
|
|
254
|
+
var _a12;
|
|
255
|
+
_a12 = symbol12;
|
|
256
|
+
var name12 = "AI_TypeValidationError";
|
|
257
|
+
var marker13 = `vercel.ai.error.${name12}`;
|
|
258
|
+
var symbol13 = Symbol.for(marker13);
|
|
259
|
+
var _a13;
|
|
260
|
+
var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
|
|
261
|
+
constructor({ value, cause }) {
|
|
262
|
+
super({
|
|
263
|
+
name: name12,
|
|
264
|
+
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
265
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
266
|
+
cause
|
|
267
|
+
});
|
|
268
|
+
this[_a13] = true;
|
|
269
|
+
this.value = value;
|
|
270
|
+
}
|
|
271
|
+
static isInstance(error) {
|
|
272
|
+
return AISDKError.hasMarker(error, marker13);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Wraps an error into a TypeValidationError.
|
|
276
|
+
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
277
|
+
* Otherwise, it creates a new TypeValidationError.
|
|
278
|
+
*
|
|
279
|
+
* @param {Object} params - The parameters for wrapping the error.
|
|
280
|
+
* @param {unknown} params.value - The value that failed validation.
|
|
281
|
+
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
282
|
+
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
283
|
+
*/
|
|
284
|
+
static wrap({
|
|
285
|
+
value,
|
|
286
|
+
cause
|
|
287
|
+
}) {
|
|
288
|
+
return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
_a13 = symbol13;
|
|
292
|
+
var TypeValidationError = _TypeValidationError;
|
|
293
|
+
var name13 = "AI_UnsupportedFunctionalityError";
|
|
294
|
+
var marker14 = `vercel.ai.error.${name13}`;
|
|
295
|
+
var symbol14 = Symbol.for(marker14);
|
|
296
|
+
var _a14;
|
|
297
|
+
var UnsupportedFunctionalityError = class extends AISDKError {
|
|
298
|
+
constructor({
|
|
299
|
+
functionality,
|
|
300
|
+
message = `'${functionality}' functionality not supported.`
|
|
301
|
+
}) {
|
|
302
|
+
super({ name: name13, message });
|
|
303
|
+
this[_a14] = true;
|
|
304
|
+
this.functionality = functionality;
|
|
305
|
+
}
|
|
306
|
+
static isInstance(error) {
|
|
307
|
+
return AISDKError.hasMarker(error, marker14);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
_a14 = symbol14;
|
|
311
|
+
|
|
312
|
+
// node_modules/.pnpm/eventsource-parser@3.0.3/node_modules/eventsource-parser/dist/index.js
|
|
313
|
+
var ParseError = class extends Error {
|
|
314
|
+
constructor(message, options) {
|
|
315
|
+
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
function noop(_arg) {
|
|
319
|
+
}
|
|
320
|
+
function createParser(callbacks) {
|
|
321
|
+
if (typeof callbacks == "function")
|
|
322
|
+
throw new TypeError(
|
|
323
|
+
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
324
|
+
);
|
|
325
|
+
const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
|
|
326
|
+
let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
|
|
327
|
+
function feed(newChunk) {
|
|
328
|
+
const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
|
|
329
|
+
for (const line of complete)
|
|
330
|
+
parseLine(line);
|
|
331
|
+
incompleteLine = incomplete, isFirstChunk = false;
|
|
332
|
+
}
|
|
333
|
+
function parseLine(line) {
|
|
334
|
+
if (line === "") {
|
|
335
|
+
dispatchEvent();
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (line.startsWith(":")) {
|
|
339
|
+
onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const fieldSeparatorIndex = line.indexOf(":");
|
|
343
|
+
if (fieldSeparatorIndex !== -1) {
|
|
344
|
+
const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
|
|
345
|
+
processField(field, value, line);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
processField(line, "", line);
|
|
349
|
+
}
|
|
350
|
+
function processField(field, value, line) {
|
|
351
|
+
switch (field) {
|
|
352
|
+
case "event":
|
|
353
|
+
eventType = value;
|
|
354
|
+
break;
|
|
355
|
+
case "data":
|
|
356
|
+
data = `${data}${value}
|
|
357
|
+
`;
|
|
358
|
+
break;
|
|
359
|
+
case "id":
|
|
360
|
+
id = value.includes("\0") ? void 0 : value;
|
|
361
|
+
break;
|
|
362
|
+
case "retry":
|
|
363
|
+
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
|
|
364
|
+
new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
365
|
+
type: "invalid-retry",
|
|
366
|
+
value,
|
|
367
|
+
line
|
|
368
|
+
})
|
|
369
|
+
);
|
|
370
|
+
break;
|
|
371
|
+
default:
|
|
372
|
+
onError(
|
|
373
|
+
new ParseError(
|
|
374
|
+
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
|
|
375
|
+
{ type: "unknown-field", field, value, line }
|
|
376
|
+
)
|
|
377
|
+
);
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function dispatchEvent() {
|
|
382
|
+
data.length > 0 && onEvent({
|
|
383
|
+
id,
|
|
384
|
+
event: eventType || void 0,
|
|
385
|
+
// If the data buffer's last character is a U+000A LINE FEED (LF) character,
|
|
386
|
+
// then remove the last character from the data buffer.
|
|
387
|
+
data: data.endsWith(`
|
|
388
|
+
`) ? data.slice(0, -1) : data
|
|
389
|
+
}), id = void 0, data = "", eventType = "";
|
|
390
|
+
}
|
|
391
|
+
function reset(options = {}) {
|
|
392
|
+
incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = true, id = void 0, data = "", eventType = "", incompleteLine = "";
|
|
393
|
+
}
|
|
394
|
+
return { feed, reset };
|
|
395
|
+
}
|
|
396
|
+
function splitLines(chunk) {
|
|
397
|
+
const lines = [];
|
|
398
|
+
let incompleteLine = "", searchIndex = 0;
|
|
399
|
+
for (; searchIndex < chunk.length; ) {
|
|
400
|
+
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
401
|
+
`, searchIndex);
|
|
402
|
+
let lineEnd = -1;
|
|
403
|
+
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
|
|
404
|
+
incompleteLine = chunk.slice(searchIndex);
|
|
405
|
+
break;
|
|
406
|
+
} else {
|
|
407
|
+
const line = chunk.slice(searchIndex, lineEnd);
|
|
408
|
+
lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
|
|
409
|
+
` && searchIndex++;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return [lines, incompleteLine];
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// node_modules/.pnpm/eventsource-parser@3.0.3/node_modules/eventsource-parser/dist/stream.js
|
|
416
|
+
var EventSourceParserStream = class extends TransformStream {
|
|
417
|
+
constructor({ onError, onRetry, onComment } = {}) {
|
|
418
|
+
let parser;
|
|
419
|
+
super({
|
|
420
|
+
start(controller) {
|
|
421
|
+
parser = createParser({
|
|
422
|
+
onEvent: (event) => {
|
|
423
|
+
controller.enqueue(event);
|
|
424
|
+
},
|
|
425
|
+
onError(error) {
|
|
426
|
+
onError === "terminate" ? controller.error(error) : typeof onError == "function" && onError(error);
|
|
427
|
+
},
|
|
428
|
+
onRetry,
|
|
429
|
+
onComment
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
transform(chunk) {
|
|
433
|
+
parser.feed(chunk);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@3.0.0-beta.2_zod@3.25.74/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
440
|
+
var z4 = __toESM(require("zod/v4"), 1);
|
|
441
|
+
|
|
442
|
+
// node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.25.74/node_modules/zod-to-json-schema/dist/esm/Options.js
|
|
443
|
+
var ignoreOverride = Symbol("Let zodToJsonSchema decide on which parser to use");
|
|
444
|
+
|
|
445
|
+
// node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.25.74/node_modules/zod-to-json-schema/dist/esm/selectParser.js
|
|
446
|
+
var import_zod4 = require("zod");
|
|
447
|
+
|
|
448
|
+
// node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.25.74/node_modules/zod-to-json-schema/dist/esm/parsers/array.js
|
|
58
449
|
var import_zod = require("zod");
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
450
|
+
|
|
451
|
+
// node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.25.74/node_modules/zod-to-json-schema/dist/esm/parsers/record.js
|
|
452
|
+
var import_zod2 = require("zod");
|
|
453
|
+
|
|
454
|
+
// node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.25.74/node_modules/zod-to-json-schema/dist/esm/parsers/string.js
|
|
455
|
+
var ALPHA_NUMERIC = new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
456
|
+
|
|
457
|
+
// node_modules/.pnpm/zod-to-json-schema@3.24.5_zod@3.25.74/node_modules/zod-to-json-schema/dist/esm/parsers/object.js
|
|
458
|
+
var import_zod3 = require("zod");
|
|
459
|
+
|
|
460
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@3.0.0-beta.2_zod@3.25.74/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
461
|
+
function combineHeaders(...headers) {
|
|
462
|
+
return headers.reduce(
|
|
463
|
+
(combinedHeaders, currentHeaders) => __spreadValues(__spreadValues({}, combinedHeaders), currentHeaders != null ? currentHeaders : {}),
|
|
464
|
+
{}
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
function extractResponseHeaders(response) {
|
|
468
|
+
return Object.fromEntries([...response.headers]);
|
|
469
|
+
}
|
|
470
|
+
var createIdGenerator = ({
|
|
471
|
+
prefix,
|
|
472
|
+
size = 16,
|
|
473
|
+
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
474
|
+
separator = "-"
|
|
475
|
+
} = {}) => {
|
|
476
|
+
const generator = () => {
|
|
477
|
+
const alphabetLength = alphabet.length;
|
|
478
|
+
const chars = new Array(size);
|
|
479
|
+
for (let i = 0; i < size; i++) {
|
|
480
|
+
chars[i] = alphabet[Math.random() * alphabetLength | 0];
|
|
481
|
+
}
|
|
482
|
+
return chars.join("");
|
|
483
|
+
};
|
|
484
|
+
if (prefix == null) {
|
|
485
|
+
return generator;
|
|
486
|
+
}
|
|
487
|
+
if (alphabet.includes(separator)) {
|
|
488
|
+
throw new InvalidArgumentError({
|
|
489
|
+
argument: "separator",
|
|
490
|
+
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
return () => `${prefix}${separator}${generator()}`;
|
|
494
|
+
};
|
|
495
|
+
var generateId = createIdGenerator();
|
|
496
|
+
function isAbortError(error) {
|
|
497
|
+
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
|
498
|
+
}
|
|
499
|
+
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
|
500
|
+
function handleFetchError({
|
|
501
|
+
error,
|
|
502
|
+
url,
|
|
503
|
+
requestBodyValues
|
|
504
|
+
}) {
|
|
505
|
+
if (isAbortError(error)) {
|
|
506
|
+
return error;
|
|
507
|
+
}
|
|
508
|
+
if (error instanceof TypeError && FETCH_FAILED_ERROR_MESSAGES.includes(error.message.toLowerCase())) {
|
|
509
|
+
const cause = error.cause;
|
|
510
|
+
if (cause != null) {
|
|
511
|
+
return new APICallError({
|
|
512
|
+
message: `Cannot connect to API: ${cause.message}`,
|
|
513
|
+
cause,
|
|
514
|
+
url,
|
|
515
|
+
requestBodyValues,
|
|
516
|
+
isRetryable: true
|
|
517
|
+
// retry when network error
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return error;
|
|
522
|
+
}
|
|
523
|
+
function removeUndefinedEntries(record) {
|
|
524
|
+
return Object.fromEntries(
|
|
525
|
+
Object.entries(record).filter(([_key, value]) => value != null)
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
var suspectProtoRx = /"__proto__"\s*:/;
|
|
529
|
+
var suspectConstructorRx = /"constructor"\s*:/;
|
|
530
|
+
function _parse(text) {
|
|
531
|
+
const obj = JSON.parse(text);
|
|
532
|
+
if (obj === null || typeof obj !== "object") {
|
|
533
|
+
return obj;
|
|
534
|
+
}
|
|
535
|
+
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
|
536
|
+
return obj;
|
|
537
|
+
}
|
|
538
|
+
return filter(obj);
|
|
539
|
+
}
|
|
540
|
+
function filter(obj) {
|
|
541
|
+
let next = [obj];
|
|
542
|
+
while (next.length) {
|
|
543
|
+
const nodes = next;
|
|
544
|
+
next = [];
|
|
545
|
+
for (const node of nodes) {
|
|
546
|
+
if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
547
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
548
|
+
}
|
|
549
|
+
if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
550
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
551
|
+
}
|
|
552
|
+
for (const key in node) {
|
|
553
|
+
const value = node[key];
|
|
554
|
+
if (value && typeof value === "object") {
|
|
555
|
+
next.push(value);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return obj;
|
|
561
|
+
}
|
|
562
|
+
function secureJsonParse(text) {
|
|
563
|
+
const { stackTraceLimit } = Error;
|
|
564
|
+
Error.stackTraceLimit = 0;
|
|
565
|
+
try {
|
|
566
|
+
return _parse(text);
|
|
567
|
+
} finally {
|
|
568
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
|
572
|
+
function validator(validate) {
|
|
573
|
+
return { [validatorSymbol]: true, validate };
|
|
574
|
+
}
|
|
575
|
+
function isValidator(value) {
|
|
576
|
+
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
577
|
+
}
|
|
578
|
+
function asValidator(value) {
|
|
579
|
+
return isValidator(value) ? value : standardSchemaValidator(value);
|
|
580
|
+
}
|
|
581
|
+
function standardSchemaValidator(standardSchema) {
|
|
582
|
+
return validator(async (value) => {
|
|
583
|
+
const result = await standardSchema["~standard"].validate(value);
|
|
584
|
+
return result.issues == null ? { success: true, value: result.value } : {
|
|
585
|
+
success: false,
|
|
586
|
+
error: new TypeValidationError({
|
|
587
|
+
value,
|
|
588
|
+
cause: result.issues
|
|
589
|
+
})
|
|
590
|
+
};
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
async function validateTypes({
|
|
594
|
+
value,
|
|
595
|
+
schema
|
|
596
|
+
}) {
|
|
597
|
+
const result = await safeValidateTypes({ value, schema });
|
|
598
|
+
if (!result.success) {
|
|
599
|
+
throw TypeValidationError.wrap({ value, cause: result.error });
|
|
600
|
+
}
|
|
601
|
+
return result.value;
|
|
602
|
+
}
|
|
603
|
+
async function safeValidateTypes({
|
|
604
|
+
value,
|
|
605
|
+
schema
|
|
606
|
+
}) {
|
|
607
|
+
const validator2 = asValidator(schema);
|
|
608
|
+
try {
|
|
609
|
+
if (validator2.validate == null) {
|
|
610
|
+
return { success: true, value, rawValue: value };
|
|
611
|
+
}
|
|
612
|
+
const result = await validator2.validate(value);
|
|
613
|
+
if (result.success) {
|
|
614
|
+
return { success: true, value: result.value, rawValue: value };
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
success: false,
|
|
618
|
+
error: TypeValidationError.wrap({ value, cause: result.error }),
|
|
619
|
+
rawValue: value
|
|
620
|
+
};
|
|
621
|
+
} catch (error) {
|
|
622
|
+
return {
|
|
623
|
+
success: false,
|
|
624
|
+
error: TypeValidationError.wrap({ value, cause: error }),
|
|
625
|
+
rawValue: value
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
async function parseJSON({
|
|
630
|
+
text,
|
|
631
|
+
schema
|
|
632
|
+
}) {
|
|
633
|
+
try {
|
|
634
|
+
const value = secureJsonParse(text);
|
|
635
|
+
if (schema == null) {
|
|
636
|
+
return value;
|
|
637
|
+
}
|
|
638
|
+
return validateTypes({ value, schema });
|
|
639
|
+
} catch (error) {
|
|
640
|
+
if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
|
|
641
|
+
throw error;
|
|
642
|
+
}
|
|
643
|
+
throw new JSONParseError({ text, cause: error });
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
async function safeParseJSON({
|
|
647
|
+
text,
|
|
648
|
+
schema
|
|
649
|
+
}) {
|
|
650
|
+
try {
|
|
651
|
+
const value = secureJsonParse(text);
|
|
652
|
+
if (schema == null) {
|
|
653
|
+
return { success: true, value, rawValue: value };
|
|
654
|
+
}
|
|
655
|
+
return await safeValidateTypes({ value, schema });
|
|
656
|
+
} catch (error) {
|
|
657
|
+
return {
|
|
658
|
+
success: false,
|
|
659
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error }),
|
|
660
|
+
rawValue: void 0
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
function isParsableJson(input) {
|
|
665
|
+
try {
|
|
666
|
+
secureJsonParse(input);
|
|
667
|
+
return true;
|
|
668
|
+
} catch (e) {
|
|
669
|
+
return false;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
function parseJsonEventStream({
|
|
673
|
+
stream,
|
|
674
|
+
schema
|
|
675
|
+
}) {
|
|
676
|
+
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
|
|
677
|
+
new TransformStream({
|
|
678
|
+
async transform({ data }, controller) {
|
|
679
|
+
if (data === "[DONE]") {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
controller.enqueue(await safeParseJSON({ text: data, schema }));
|
|
683
|
+
}
|
|
684
|
+
})
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
var getOriginalFetch2 = () => globalThis.fetch;
|
|
688
|
+
var postJsonToApi = async ({
|
|
689
|
+
url,
|
|
690
|
+
headers,
|
|
691
|
+
body,
|
|
692
|
+
failedResponseHandler,
|
|
693
|
+
successfulResponseHandler,
|
|
694
|
+
abortSignal,
|
|
695
|
+
fetch
|
|
696
|
+
}) => postToApi({
|
|
697
|
+
url,
|
|
698
|
+
headers: __spreadValues({
|
|
699
|
+
"Content-Type": "application/json"
|
|
700
|
+
}, headers),
|
|
701
|
+
body: {
|
|
702
|
+
content: JSON.stringify(body),
|
|
703
|
+
values: body
|
|
704
|
+
},
|
|
705
|
+
failedResponseHandler,
|
|
706
|
+
successfulResponseHandler,
|
|
707
|
+
abortSignal,
|
|
708
|
+
fetch
|
|
709
|
+
});
|
|
710
|
+
var postToApi = async ({
|
|
711
|
+
url,
|
|
712
|
+
headers = {},
|
|
713
|
+
body,
|
|
714
|
+
successfulResponseHandler,
|
|
715
|
+
failedResponseHandler,
|
|
716
|
+
abortSignal,
|
|
717
|
+
fetch = getOriginalFetch2()
|
|
718
|
+
}) => {
|
|
719
|
+
try {
|
|
720
|
+
const response = await fetch(url, {
|
|
721
|
+
method: "POST",
|
|
722
|
+
headers: removeUndefinedEntries(headers),
|
|
723
|
+
body: body.content,
|
|
724
|
+
signal: abortSignal
|
|
725
|
+
});
|
|
726
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
727
|
+
if (!response.ok) {
|
|
728
|
+
let errorInformation;
|
|
729
|
+
try {
|
|
730
|
+
errorInformation = await failedResponseHandler({
|
|
731
|
+
response,
|
|
732
|
+
url,
|
|
733
|
+
requestBodyValues: body.values
|
|
734
|
+
});
|
|
735
|
+
} catch (error) {
|
|
736
|
+
if (isAbortError(error) || APICallError.isInstance(error)) {
|
|
737
|
+
throw error;
|
|
738
|
+
}
|
|
739
|
+
throw new APICallError({
|
|
740
|
+
message: "Failed to process error response",
|
|
741
|
+
cause: error,
|
|
742
|
+
statusCode: response.status,
|
|
743
|
+
url,
|
|
744
|
+
responseHeaders,
|
|
745
|
+
requestBodyValues: body.values
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
throw errorInformation.value;
|
|
749
|
+
}
|
|
750
|
+
try {
|
|
751
|
+
return await successfulResponseHandler({
|
|
752
|
+
response,
|
|
753
|
+
url,
|
|
754
|
+
requestBodyValues: body.values
|
|
755
|
+
});
|
|
756
|
+
} catch (error) {
|
|
757
|
+
if (error instanceof Error) {
|
|
758
|
+
if (isAbortError(error) || APICallError.isInstance(error)) {
|
|
759
|
+
throw error;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
throw new APICallError({
|
|
763
|
+
message: "Failed to process successful response",
|
|
764
|
+
cause: error,
|
|
765
|
+
statusCode: response.status,
|
|
766
|
+
url,
|
|
767
|
+
responseHeaders,
|
|
768
|
+
requestBodyValues: body.values
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
} catch (error) {
|
|
772
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
var createJsonErrorResponseHandler = ({
|
|
776
|
+
errorSchema,
|
|
777
|
+
errorToMessage,
|
|
778
|
+
isRetryable
|
|
779
|
+
}) => async ({ response, url, requestBodyValues }) => {
|
|
780
|
+
const responseBody = await response.text();
|
|
781
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
782
|
+
if (responseBody.trim() === "") {
|
|
783
|
+
return {
|
|
784
|
+
responseHeaders,
|
|
785
|
+
value: new APICallError({
|
|
786
|
+
message: response.statusText,
|
|
787
|
+
url,
|
|
788
|
+
requestBodyValues,
|
|
789
|
+
statusCode: response.status,
|
|
790
|
+
responseHeaders,
|
|
791
|
+
responseBody,
|
|
792
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
|
793
|
+
})
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
try {
|
|
797
|
+
const parsedError = await parseJSON({
|
|
798
|
+
text: responseBody,
|
|
799
|
+
schema: errorSchema
|
|
800
|
+
});
|
|
801
|
+
return {
|
|
802
|
+
responseHeaders,
|
|
803
|
+
value: new APICallError({
|
|
804
|
+
message: errorToMessage(parsedError),
|
|
805
|
+
url,
|
|
806
|
+
requestBodyValues,
|
|
807
|
+
statusCode: response.status,
|
|
808
|
+
responseHeaders,
|
|
809
|
+
responseBody,
|
|
810
|
+
data: parsedError,
|
|
811
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
|
812
|
+
})
|
|
813
|
+
};
|
|
814
|
+
} catch (parseError) {
|
|
815
|
+
return {
|
|
816
|
+
responseHeaders,
|
|
817
|
+
value: new APICallError({
|
|
818
|
+
message: response.statusText,
|
|
819
|
+
url,
|
|
820
|
+
requestBodyValues,
|
|
821
|
+
statusCode: response.status,
|
|
822
|
+
responseHeaders,
|
|
823
|
+
responseBody,
|
|
824
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
|
825
|
+
})
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
830
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
831
|
+
if (response.body == null) {
|
|
832
|
+
throw new EmptyResponseBodyError({});
|
|
833
|
+
}
|
|
834
|
+
return {
|
|
835
|
+
responseHeaders,
|
|
836
|
+
value: parseJsonEventStream({
|
|
837
|
+
stream: response.body,
|
|
838
|
+
schema: chunkSchema
|
|
839
|
+
})
|
|
840
|
+
};
|
|
841
|
+
};
|
|
842
|
+
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
|
843
|
+
const responseBody = await response.text();
|
|
844
|
+
const parsedResult = await safeParseJSON({
|
|
845
|
+
text: responseBody,
|
|
846
|
+
schema: responseSchema
|
|
847
|
+
});
|
|
848
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
849
|
+
if (!parsedResult.success) {
|
|
850
|
+
throw new APICallError({
|
|
851
|
+
message: "Invalid JSON response",
|
|
852
|
+
cause: parsedResult.error,
|
|
853
|
+
statusCode: response.status,
|
|
854
|
+
responseHeaders,
|
|
855
|
+
responseBody,
|
|
856
|
+
url,
|
|
857
|
+
requestBodyValues
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
return {
|
|
861
|
+
responseHeaders,
|
|
862
|
+
value: parsedResult.value,
|
|
863
|
+
rawValue: parsedResult.rawValue
|
|
864
|
+
};
|
|
865
|
+
};
|
|
866
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
|
867
|
+
var { btoa, atob } = globalThis;
|
|
868
|
+
function convertUint8ArrayToBase64(array) {
|
|
869
|
+
let latin1string = "";
|
|
870
|
+
for (let i = 0; i < array.length; i++) {
|
|
871
|
+
latin1string += String.fromCodePoint(array[i]);
|
|
872
|
+
}
|
|
873
|
+
return btoa(latin1string);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// src/chat/index.ts
|
|
877
|
+
var import_v44 = require("zod/v4");
|
|
878
|
+
|
|
879
|
+
// src/schemas/reasoning-details.ts
|
|
880
|
+
var import_v4 = require("zod/v4");
|
|
881
|
+
var ReasoningDetailSummarySchema = import_v4.z.object({
|
|
882
|
+
type: import_v4.z.literal("reasoning.summary" /* Summary */),
|
|
883
|
+
summary: import_v4.z.string()
|
|
62
884
|
});
|
|
63
|
-
var ReasoningDetailEncryptedSchema =
|
|
64
|
-
type:
|
|
65
|
-
data:
|
|
885
|
+
var ReasoningDetailEncryptedSchema = import_v4.z.object({
|
|
886
|
+
type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
|
|
887
|
+
data: import_v4.z.string()
|
|
66
888
|
});
|
|
67
|
-
var ReasoningDetailTextSchema =
|
|
68
|
-
type:
|
|
69
|
-
text:
|
|
70
|
-
signature:
|
|
889
|
+
var ReasoningDetailTextSchema = import_v4.z.object({
|
|
890
|
+
type: import_v4.z.literal("reasoning.text" /* Text */),
|
|
891
|
+
text: import_v4.z.string().nullish(),
|
|
892
|
+
signature: import_v4.z.string().nullish()
|
|
71
893
|
});
|
|
72
|
-
var ReasoningDetailUnionSchema =
|
|
894
|
+
var ReasoningDetailUnionSchema = import_v4.z.union([
|
|
73
895
|
ReasoningDetailSummarySchema,
|
|
74
896
|
ReasoningDetailEncryptedSchema,
|
|
75
897
|
ReasoningDetailTextSchema
|
|
76
898
|
]);
|
|
77
|
-
var ReasoningDetailsWithUnknownSchema =
|
|
899
|
+
var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
|
|
78
900
|
ReasoningDetailUnionSchema,
|
|
79
|
-
|
|
901
|
+
import_v4.z.unknown().transform(() => null)
|
|
80
902
|
]);
|
|
81
|
-
var ReasoningDetailArraySchema =
|
|
903
|
+
var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
82
904
|
|
|
83
|
-
// src/
|
|
84
|
-
var
|
|
85
|
-
var
|
|
86
|
-
|
|
905
|
+
// src/schemas/error-response.ts
|
|
906
|
+
var import_v42 = require("zod/v4");
|
|
907
|
+
var OpenRouterErrorResponseSchema = import_v42.z.object({
|
|
908
|
+
error: import_v42.z.object({
|
|
909
|
+
code: import_v42.z.union([import_v42.z.string(), import_v42.z.number()]).nullable(),
|
|
910
|
+
message: import_v42.z.string(),
|
|
911
|
+
type: import_v42.z.string().nullable(),
|
|
912
|
+
param: import_v42.z.any().nullable()
|
|
913
|
+
})
|
|
914
|
+
});
|
|
915
|
+
var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
916
|
+
errorSchema: OpenRouterErrorResponseSchema,
|
|
917
|
+
errorToMessage: (data) => data.error.message
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
// src/utils/map-finish-reason.ts
|
|
921
|
+
function mapOpenRouterFinishReason(finishReason) {
|
|
922
|
+
switch (finishReason) {
|
|
923
|
+
case "stop":
|
|
924
|
+
return "stop";
|
|
925
|
+
case "length":
|
|
926
|
+
return "length";
|
|
927
|
+
case "content_filter":
|
|
928
|
+
return "content-filter";
|
|
929
|
+
case "function_call":
|
|
930
|
+
case "tool_calls":
|
|
931
|
+
return "tool-calls";
|
|
932
|
+
default:
|
|
933
|
+
return "unknown";
|
|
934
|
+
}
|
|
935
|
+
}
|
|
87
936
|
|
|
88
|
-
// src/convert-to-openrouter-chat-messages.ts
|
|
89
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
937
|
+
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
90
938
|
function getCacheControl(providerMetadata) {
|
|
91
|
-
var
|
|
939
|
+
var _a15, _b, _c;
|
|
92
940
|
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
93
941
|
const openrouter = providerMetadata == null ? void 0 : providerMetadata.openrouter;
|
|
94
|
-
return (_c = (_b = (
|
|
942
|
+
return (_c = (_b = (_a15 = openrouter == null ? void 0 : openrouter.cacheControl) != null ? _a15 : openrouter == null ? void 0 : openrouter.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
95
943
|
}
|
|
96
944
|
function convertToOpenRouterChatMessages(prompt) {
|
|
97
|
-
var
|
|
945
|
+
var _a15, _b, _c;
|
|
98
946
|
const messages = [];
|
|
99
|
-
for (const { role, content,
|
|
947
|
+
for (const { role, content, providerOptions } of prompt) {
|
|
100
948
|
switch (role) {
|
|
101
949
|
case "system": {
|
|
102
950
|
messages.push({
|
|
103
951
|
role: "system",
|
|
104
952
|
content,
|
|
105
|
-
cache_control: getCacheControl(
|
|
953
|
+
cache_control: getCacheControl(providerOptions)
|
|
106
954
|
});
|
|
107
955
|
break;
|
|
108
956
|
}
|
|
109
957
|
case "user": {
|
|
110
|
-
if (content.length === 1 && ((
|
|
958
|
+
if (content.length === 1 && ((_a15 = content[0]) == null ? void 0 : _a15.type) === "text") {
|
|
959
|
+
const cacheControl = (_b = getCacheControl(providerOptions)) != null ? _b : getCacheControl(content[0].providerOptions);
|
|
960
|
+
const contentWithCacheControl = cacheControl ? [
|
|
961
|
+
{
|
|
962
|
+
type: "text",
|
|
963
|
+
text: content[0].text,
|
|
964
|
+
cache_control: cacheControl
|
|
965
|
+
}
|
|
966
|
+
] : content[0].text;
|
|
111
967
|
messages.push({
|
|
112
968
|
role: "user",
|
|
113
|
-
content:
|
|
114
|
-
cache_control: (_b = getCacheControl(providerMetadata)) != null ? _b : getCacheControl(content[0].providerMetadata)
|
|
969
|
+
content: contentWithCacheControl
|
|
115
970
|
});
|
|
116
971
|
break;
|
|
117
972
|
}
|
|
118
|
-
const messageCacheControl = getCacheControl(
|
|
973
|
+
const messageCacheControl = getCacheControl(providerOptions);
|
|
119
974
|
const contentParts = content.map(
|
|
120
975
|
(part) => {
|
|
121
|
-
var
|
|
122
|
-
const cacheControl = (
|
|
976
|
+
var _a16, _b2, _c2, _d, _e, _f, _g;
|
|
977
|
+
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
123
978
|
switch (part.type) {
|
|
124
979
|
case "text":
|
|
125
980
|
return {
|
|
@@ -128,33 +983,35 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
128
983
|
// For text parts, only use part-specific cache control
|
|
129
984
|
cache_control: cacheControl
|
|
130
985
|
};
|
|
131
|
-
case "image":
|
|
132
|
-
return {
|
|
133
|
-
type: "image_url",
|
|
134
|
-
image_url: {
|
|
135
|
-
url: part.image instanceof URL ? part.image.toString() : `data:${(_b2 = part.mimeType) != null ? _b2 : "image/jpeg"};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(
|
|
136
|
-
part.image
|
|
137
|
-
)}`
|
|
138
|
-
},
|
|
139
|
-
// For image parts, use part-specific or message-level cache control
|
|
140
|
-
cache_control: cacheControl
|
|
141
|
-
};
|
|
142
986
|
case "file":
|
|
987
|
+
if ((_b2 = part.mediaType) == null ? void 0 : _b2.startsWith("image/")) {
|
|
988
|
+
return {
|
|
989
|
+
type: "image_url",
|
|
990
|
+
image_url: {
|
|
991
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${(_c2 = part.mediaType) != null ? _c2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
|
|
992
|
+
part.data instanceof Uint8Array ? part.data : new Uint8Array()
|
|
993
|
+
)}`
|
|
994
|
+
},
|
|
995
|
+
// For image parts, use part-specific or message-level cache control
|
|
996
|
+
cache_control: cacheControl
|
|
997
|
+
};
|
|
998
|
+
}
|
|
143
999
|
return {
|
|
144
1000
|
type: "file",
|
|
145
1001
|
file: {
|
|
146
1002
|
filename: String(
|
|
147
|
-
(
|
|
1003
|
+
(_g = (_f = (_e = (_d = part.providerOptions) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename) != null ? _f : part.filename) != null ? _g : ""
|
|
148
1004
|
),
|
|
149
|
-
file_data: part.data instanceof Uint8Array ? `data:${part.
|
|
1005
|
+
file_data: part.data instanceof Uint8Array ? `data:${part.mediaType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mediaType};base64,${part.data}`
|
|
150
1006
|
},
|
|
151
1007
|
cache_control: cacheControl
|
|
152
1008
|
};
|
|
153
1009
|
default: {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
1010
|
+
return {
|
|
1011
|
+
type: "text",
|
|
1012
|
+
text: "",
|
|
1013
|
+
cache_control: cacheControl
|
|
1014
|
+
};
|
|
158
1015
|
}
|
|
159
1016
|
}
|
|
160
1017
|
}
|
|
@@ -182,7 +1039,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
182
1039
|
type: "function",
|
|
183
1040
|
function: {
|
|
184
1041
|
name: part.toolName,
|
|
185
|
-
arguments: JSON.stringify(part.
|
|
1042
|
+
arguments: JSON.stringify(part.input)
|
|
186
1043
|
}
|
|
187
1044
|
});
|
|
188
1045
|
break;
|
|
@@ -191,23 +1048,14 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
191
1048
|
reasoning += part.text;
|
|
192
1049
|
reasoningDetails.push({
|
|
193
1050
|
type: "reasoning.text" /* Text */,
|
|
194
|
-
text: part.text
|
|
195
|
-
signature: part.signature
|
|
196
|
-
});
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
case "redacted-reasoning": {
|
|
200
|
-
reasoningDetails.push({
|
|
201
|
-
type: "reasoning.encrypted" /* Encrypted */,
|
|
202
|
-
data: part.data
|
|
1051
|
+
text: part.text
|
|
203
1052
|
});
|
|
204
1053
|
break;
|
|
205
1054
|
}
|
|
206
1055
|
case "file":
|
|
207
1056
|
break;
|
|
208
1057
|
default: {
|
|
209
|
-
|
|
210
|
-
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
|
1058
|
+
break;
|
|
211
1059
|
}
|
|
212
1060
|
}
|
|
213
1061
|
}
|
|
@@ -217,95 +1065,86 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
217
1065
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
218
1066
|
reasoning: reasoning || void 0,
|
|
219
1067
|
reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
|
|
220
|
-
cache_control: getCacheControl(
|
|
1068
|
+
cache_control: getCacheControl(providerOptions)
|
|
221
1069
|
});
|
|
222
1070
|
break;
|
|
223
1071
|
}
|
|
224
1072
|
case "tool": {
|
|
225
1073
|
for (const toolResponse of content) {
|
|
1074
|
+
const content2 = getToolResultContent(toolResponse);
|
|
226
1075
|
messages.push({
|
|
227
1076
|
role: "tool",
|
|
228
1077
|
tool_call_id: toolResponse.toolCallId,
|
|
229
|
-
content:
|
|
230
|
-
cache_control: (_c = getCacheControl(
|
|
1078
|
+
content: content2,
|
|
1079
|
+
cache_control: (_c = getCacheControl(providerOptions)) != null ? _c : getCacheControl(toolResponse.providerOptions)
|
|
231
1080
|
});
|
|
232
1081
|
}
|
|
233
1082
|
break;
|
|
234
1083
|
}
|
|
235
1084
|
default: {
|
|
236
|
-
|
|
237
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
1085
|
+
break;
|
|
238
1086
|
}
|
|
239
1087
|
}
|
|
240
1088
|
}
|
|
241
1089
|
return messages;
|
|
242
1090
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
function mapOpenRouterChatLogProbsOutput(logprobs) {
|
|
246
|
-
var _a, _b;
|
|
247
|
-
return (_b = (_a = logprobs == null ? void 0 : logprobs.content) == null ? void 0 : _a.map(({ token, logprob, top_logprobs }) => ({
|
|
248
|
-
token,
|
|
249
|
-
logprob,
|
|
250
|
-
topLogprobs: top_logprobs ? top_logprobs.map(({ token: token2, logprob: logprob2 }) => ({
|
|
251
|
-
token: token2,
|
|
252
|
-
logprob: logprob2
|
|
253
|
-
})) : []
|
|
254
|
-
}))) != null ? _b : void 0;
|
|
1091
|
+
function getToolResultContent(input) {
|
|
1092
|
+
return input.output.type === "text" ? input.output.value : JSON.stringify(input.output.value);
|
|
255
1093
|
}
|
|
256
1094
|
|
|
257
|
-
// src/
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
1095
|
+
// src/chat/get-tool-choice.ts
|
|
1096
|
+
var import_v43 = require("zod/v4");
|
|
1097
|
+
var ChatCompletionToolChoiceSchema = import_v43.z.union([
|
|
1098
|
+
import_v43.z.literal("auto"),
|
|
1099
|
+
import_v43.z.literal("none"),
|
|
1100
|
+
import_v43.z.literal("required"),
|
|
1101
|
+
import_v43.z.object({
|
|
1102
|
+
type: import_v43.z.literal("function"),
|
|
1103
|
+
function: import_v43.z.object({
|
|
1104
|
+
name: import_v43.z.string()
|
|
1105
|
+
})
|
|
1106
|
+
})
|
|
1107
|
+
]);
|
|
1108
|
+
function getChatCompletionToolChoice(toolChoice) {
|
|
1109
|
+
switch (toolChoice.type) {
|
|
1110
|
+
case "auto":
|
|
1111
|
+
case "none":
|
|
1112
|
+
case "required":
|
|
1113
|
+
return toolChoice.type;
|
|
1114
|
+
case "tool": {
|
|
1115
|
+
return {
|
|
1116
|
+
type: "function",
|
|
1117
|
+
function: { name: toolChoice.toolName }
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
default: {
|
|
1121
|
+
toolChoice;
|
|
1122
|
+
throw new Error(`Invalid tool choice type: ${toolChoice}`);
|
|
1123
|
+
}
|
|
271
1124
|
}
|
|
272
1125
|
}
|
|
273
1126
|
|
|
274
|
-
// src/
|
|
275
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
276
|
-
var import_zod2 = require("zod");
|
|
277
|
-
var OpenRouterErrorResponseSchema = import_zod2.z.object({
|
|
278
|
-
error: import_zod2.z.object({
|
|
279
|
-
message: import_zod2.z.string(),
|
|
280
|
-
type: import_zod2.z.string(),
|
|
281
|
-
param: import_zod2.z.any().nullable(),
|
|
282
|
-
code: import_zod2.z.string().nullable()
|
|
283
|
-
})
|
|
284
|
-
});
|
|
285
|
-
var openrouterFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
|
286
|
-
errorSchema: OpenRouterErrorResponseSchema,
|
|
287
|
-
errorToMessage: (data) => data.error.message
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
// src/openrouter-chat-language-model.ts
|
|
291
|
-
function isFunctionTool(tool) {
|
|
292
|
-
return "parameters" in tool;
|
|
293
|
-
}
|
|
1127
|
+
// src/chat/index.ts
|
|
294
1128
|
var OpenRouterChatLanguageModel = class {
|
|
295
1129
|
constructor(modelId, settings, config) {
|
|
296
|
-
this.specificationVersion = "
|
|
1130
|
+
this.specificationVersion = "v2";
|
|
1131
|
+
this.provider = "openrouter";
|
|
297
1132
|
this.defaultObjectGenerationMode = "tool";
|
|
1133
|
+
this.supportedUrls = {
|
|
1134
|
+
"image/*": [
|
|
1135
|
+
/^data:image\/[a-zA-Z]+;base64,/,
|
|
1136
|
+
/^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
|
|
1137
|
+
],
|
|
1138
|
+
// 'text/*': [/^data:text\//, /^https?:\/\/.+$/],
|
|
1139
|
+
"application/*": [/^data:application\//, /^https?:\/\/.+$/]
|
|
1140
|
+
};
|
|
298
1141
|
this.modelId = modelId;
|
|
299
1142
|
this.settings = settings;
|
|
300
1143
|
this.config = config;
|
|
301
1144
|
}
|
|
302
|
-
get provider() {
|
|
303
|
-
return this.config.provider;
|
|
304
|
-
}
|
|
305
1145
|
getArgs({
|
|
306
|
-
mode,
|
|
307
1146
|
prompt,
|
|
308
|
-
|
|
1147
|
+
maxOutputTokens,
|
|
309
1148
|
temperature,
|
|
310
1149
|
topP,
|
|
311
1150
|
frequencyPenalty,
|
|
@@ -314,12 +1153,10 @@ var OpenRouterChatLanguageModel = class {
|
|
|
314
1153
|
stopSequences,
|
|
315
1154
|
responseFormat,
|
|
316
1155
|
topK,
|
|
317
|
-
|
|
1156
|
+
tools,
|
|
1157
|
+
toolChoice
|
|
318
1158
|
}) {
|
|
319
|
-
|
|
320
|
-
const type = mode.type;
|
|
321
|
-
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata.openrouter) != null ? _a : {};
|
|
322
|
-
const baseArgs = __spreadValues(__spreadValues(__spreadValues({
|
|
1159
|
+
const baseArgs = __spreadValues(__spreadValues({
|
|
323
1160
|
// model id:
|
|
324
1161
|
model: this.modelId,
|
|
325
1162
|
models: this.settings.models,
|
|
@@ -330,7 +1167,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
330
1167
|
user: this.settings.user,
|
|
331
1168
|
parallel_tool_calls: this.settings.parallelToolCalls,
|
|
332
1169
|
// standardized settings:
|
|
333
|
-
max_tokens:
|
|
1170
|
+
max_tokens: maxOutputTokens,
|
|
334
1171
|
temperature,
|
|
335
1172
|
top_p: topP,
|
|
336
1173
|
frequency_penalty: frequencyPenalty,
|
|
@@ -345,97 +1182,74 @@ var OpenRouterChatLanguageModel = class {
|
|
|
345
1182
|
include_reasoning: this.settings.includeReasoning,
|
|
346
1183
|
reasoning: this.settings.reasoning,
|
|
347
1184
|
usage: this.settings.usage
|
|
348
|
-
}, this.config.extraBody), this.settings.extraBody)
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
parameters: mode.tool.parameters
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
]
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
// Handle all non-text types with a single default case
|
|
374
|
-
default: {
|
|
375
|
-
const _exhaustiveCheck = type;
|
|
376
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
377
|
-
functionality: `${_exhaustiveCheck} mode`
|
|
378
|
-
});
|
|
379
|
-
}
|
|
1185
|
+
}, this.config.extraBody), this.settings.extraBody);
|
|
1186
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
1187
|
+
return __spreadProps(__spreadValues({}, baseArgs), {
|
|
1188
|
+
response_format: { type: "json_object" }
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
if (tools && tools.length > 0) {
|
|
1192
|
+
const mappedTools = tools.filter((tool) => tool.type === "function").map((tool) => ({
|
|
1193
|
+
type: "function",
|
|
1194
|
+
function: {
|
|
1195
|
+
name: tool.name,
|
|
1196
|
+
description: tool.type,
|
|
1197
|
+
parameters: tool.inputSchema
|
|
1198
|
+
}
|
|
1199
|
+
}));
|
|
1200
|
+
return __spreadProps(__spreadValues({}, baseArgs), {
|
|
1201
|
+
tools: mappedTools,
|
|
1202
|
+
tool_choice: toolChoice ? getChatCompletionToolChoice(toolChoice) : void 0
|
|
1203
|
+
});
|
|
380
1204
|
}
|
|
1205
|
+
return baseArgs;
|
|
381
1206
|
}
|
|
382
1207
|
async doGenerate(options) {
|
|
383
|
-
var _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
384
|
-
const
|
|
385
|
-
const
|
|
1208
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
|
|
1209
|
+
const providerOptions = options.providerOptions || {};
|
|
1210
|
+
const openrouterOptions = providerOptions.openrouter || {};
|
|
1211
|
+
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1212
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
386
1213
|
url: this.config.url({
|
|
387
1214
|
path: "/chat/completions",
|
|
388
1215
|
modelId: this.modelId
|
|
389
1216
|
}),
|
|
390
|
-
headers:
|
|
1217
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
391
1218
|
body: args,
|
|
392
1219
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
393
|
-
successfulResponseHandler:
|
|
1220
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
394
1221
|
OpenRouterNonStreamChatCompletionResponseSchema
|
|
395
1222
|
),
|
|
396
1223
|
abortSignal: options.abortSignal,
|
|
397
1224
|
fetch: this.config.fetch
|
|
398
1225
|
});
|
|
399
|
-
const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
|
|
400
1226
|
const choice = response.choices[0];
|
|
401
1227
|
if (!choice) {
|
|
402
1228
|
throw new Error("No choice in response");
|
|
403
1229
|
}
|
|
404
1230
|
const usageInfo = response.usage ? {
|
|
405
|
-
|
|
406
|
-
|
|
1231
|
+
inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
|
|
1232
|
+
outputTokens: (_b = response.usage.completion_tokens) != null ? _b : 0,
|
|
1233
|
+
totalTokens: ((_c = response.usage.prompt_tokens) != null ? _c : 0) + ((_d = response.usage.completion_tokens) != null ? _d : 0),
|
|
1234
|
+
reasoningTokens: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0,
|
|
1235
|
+
cachedInputTokens: (_h = (_g = response.usage.prompt_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : 0
|
|
407
1236
|
} : {
|
|
408
|
-
|
|
409
|
-
|
|
1237
|
+
inputTokens: 0,
|
|
1238
|
+
outputTokens: 0,
|
|
1239
|
+
totalTokens: 0,
|
|
1240
|
+
reasoningTokens: 0,
|
|
1241
|
+
cachedInputTokens: 0
|
|
410
1242
|
};
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
usage: {
|
|
415
|
-
promptTokens: response.usage.prompt_tokens,
|
|
416
|
-
promptTokensDetails: response.usage.prompt_tokens_details ? {
|
|
417
|
-
cachedTokens: (_e = response.usage.prompt_tokens_details.cached_tokens) != null ? _e : 0
|
|
418
|
-
} : void 0,
|
|
419
|
-
completionTokens: response.usage.completion_tokens,
|
|
420
|
-
completionTokensDetails: response.usage.completion_tokens_details ? {
|
|
421
|
-
reasoningTokens: (_f = response.usage.completion_tokens_details.reasoning_tokens) != null ? _f : 0
|
|
422
|
-
} : void 0,
|
|
423
|
-
cost: response.usage.cost,
|
|
424
|
-
totalTokens: (_g = response.usage.total_tokens) != null ? _g : 0
|
|
425
|
-
}
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
|
|
429
|
-
const reasoningDetails = (_h = choice.message.reasoning_details) != null ? _h : [];
|
|
430
|
-
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
431
|
-
var _a2;
|
|
1243
|
+
const reasoningDetails = (_i = choice.message.reasoning_details) != null ? _i : [];
|
|
1244
|
+
reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
1245
|
+
var _a16;
|
|
432
1246
|
switch (detail.type) {
|
|
433
1247
|
case "reasoning.text" /* Text */: {
|
|
434
1248
|
if (detail.text) {
|
|
435
1249
|
return {
|
|
436
1250
|
type: "text",
|
|
437
1251
|
text: detail.text,
|
|
438
|
-
signature: (
|
|
1252
|
+
signature: (_a16 = detail.signature) != null ? _a16 : void 0
|
|
439
1253
|
};
|
|
440
1254
|
}
|
|
441
1255
|
break;
|
|
@@ -469,68 +1283,92 @@ var OpenRouterChatLanguageModel = class {
|
|
|
469
1283
|
text: choice.message.reasoning
|
|
470
1284
|
}
|
|
471
1285
|
] : [];
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
toolCallId: (
|
|
1286
|
+
const content = [];
|
|
1287
|
+
if (choice.message.content) {
|
|
1288
|
+
content.push({
|
|
1289
|
+
type: "text",
|
|
1290
|
+
text: choice.message.content
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
if (choice.message.tool_calls) {
|
|
1294
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
1295
|
+
content.push({
|
|
1296
|
+
type: "tool-call",
|
|
1297
|
+
toolCallId: (_j = toolCall.id) != null ? _j : generateId(),
|
|
484
1298
|
toolName: toolCall.function.name,
|
|
485
|
-
|
|
486
|
-
};
|
|
487
|
-
}
|
|
1299
|
+
input: toolCall.function.arguments
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
return {
|
|
1304
|
+
content,
|
|
488
1305
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
489
1306
|
usage: usageInfo,
|
|
490
|
-
rawCall: { rawPrompt, rawSettings },
|
|
491
|
-
rawResponse: { headers: responseHeaders },
|
|
492
1307
|
warnings: [],
|
|
493
|
-
|
|
494
|
-
|
|
1308
|
+
providerMetadata: {
|
|
1309
|
+
openrouter: {
|
|
1310
|
+
usage: {
|
|
1311
|
+
promptTokens: (_k = usageInfo.inputTokens) != null ? _k : 0,
|
|
1312
|
+
completionTokens: (_l = usageInfo.outputTokens) != null ? _l : 0,
|
|
1313
|
+
totalTokens: (_m = usageInfo.totalTokens) != null ? _m : 0,
|
|
1314
|
+
cost: (_n = response.usage) == null ? void 0 : _n.cost,
|
|
1315
|
+
promptTokensDetails: {
|
|
1316
|
+
cachedTokens: (_q = (_p = (_o = response.usage) == null ? void 0 : _o.prompt_tokens_details) == null ? void 0 : _p.cached_tokens) != null ? _q : 0
|
|
1317
|
+
},
|
|
1318
|
+
completionTokensDetails: {
|
|
1319
|
+
reasoningTokens: (_t = (_s = (_r = response.usage) == null ? void 0 : _r.completion_tokens_details) == null ? void 0 : _s.reasoning_tokens) != null ? _t : 0
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
},
|
|
1324
|
+
request: { body: args },
|
|
1325
|
+
response: {
|
|
1326
|
+
id: response.id,
|
|
1327
|
+
modelId: response.model,
|
|
1328
|
+
headers: responseHeaders
|
|
1329
|
+
}
|
|
1330
|
+
};
|
|
495
1331
|
}
|
|
496
1332
|
async doStream(options) {
|
|
497
|
-
var
|
|
498
|
-
const
|
|
499
|
-
const
|
|
1333
|
+
var _a15;
|
|
1334
|
+
const providerOptions = options.providerOptions || {};
|
|
1335
|
+
const openrouterOptions = providerOptions.openrouter || {};
|
|
1336
|
+
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1337
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
500
1338
|
url: this.config.url({
|
|
501
1339
|
path: "/chat/completions",
|
|
502
1340
|
modelId: this.modelId
|
|
503
1341
|
}),
|
|
504
|
-
headers:
|
|
1342
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
505
1343
|
body: __spreadProps(__spreadValues({}, args), {
|
|
506
1344
|
stream: true,
|
|
507
1345
|
// only include stream_options when in strict compatibility mode:
|
|
508
1346
|
stream_options: this.config.compatibility === "strict" ? __spreadValues({
|
|
509
1347
|
include_usage: true
|
|
510
|
-
}, ((
|
|
1348
|
+
}, ((_a15 = this.settings.usage) == null ? void 0 : _a15.include) ? { include_usage: true } : {}) : void 0
|
|
511
1349
|
}),
|
|
512
1350
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
513
|
-
successfulResponseHandler:
|
|
1351
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
514
1352
|
OpenRouterStreamChatCompletionChunkSchema
|
|
515
1353
|
),
|
|
516
1354
|
abortSignal: options.abortSignal,
|
|
517
1355
|
fetch: this.config.fetch
|
|
518
1356
|
});
|
|
519
|
-
const _b = args, { messages: rawPrompt } = _b, rawSettings = __objRest(_b, ["messages"]);
|
|
520
1357
|
const toolCalls = [];
|
|
521
1358
|
let finishReason = "other";
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
1359
|
+
const usage = {
|
|
1360
|
+
inputTokens: Number.NaN,
|
|
1361
|
+
outputTokens: Number.NaN,
|
|
1362
|
+
totalTokens: Number.NaN,
|
|
1363
|
+
reasoningTokens: Number.NaN,
|
|
1364
|
+
cachedInputTokens: Number.NaN
|
|
525
1365
|
};
|
|
526
|
-
let logprobs;
|
|
527
1366
|
const openrouterUsage = {};
|
|
528
|
-
const shouldIncludeUsageAccounting = !!((_c = this.settings.usage) == null ? void 0 : _c.include);
|
|
529
1367
|
return {
|
|
530
1368
|
stream: response.pipeThrough(
|
|
531
1369
|
new TransformStream({
|
|
532
1370
|
transform(chunk, controller) {
|
|
533
|
-
var
|
|
1371
|
+
var _a16, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
534
1372
|
if (!chunk.success) {
|
|
535
1373
|
finishReason = "error";
|
|
536
1374
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -555,20 +1393,23 @@ var OpenRouterChatLanguageModel = class {
|
|
|
555
1393
|
});
|
|
556
1394
|
}
|
|
557
1395
|
if (value.usage != null) {
|
|
558
|
-
usage =
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
};
|
|
1396
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
1397
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
1398
|
+
usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
|
|
562
1399
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
563
1400
|
if (value.usage.prompt_tokens_details) {
|
|
1401
|
+
const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
|
|
1402
|
+
usage.cachedInputTokens = cachedInputTokens;
|
|
564
1403
|
openrouterUsage.promptTokensDetails = {
|
|
565
|
-
cachedTokens:
|
|
1404
|
+
cachedTokens: cachedInputTokens
|
|
566
1405
|
};
|
|
567
1406
|
}
|
|
568
1407
|
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
569
1408
|
if (value.usage.completion_tokens_details) {
|
|
1409
|
+
const reasoningTokens = (_b = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b : 0;
|
|
1410
|
+
usage.reasoningTokens = reasoningTokens;
|
|
570
1411
|
openrouterUsage.completionTokensDetails = {
|
|
571
|
-
reasoningTokens
|
|
1412
|
+
reasoningTokens
|
|
572
1413
|
};
|
|
573
1414
|
}
|
|
574
1415
|
openrouterUsage.cost = value.usage.cost;
|
|
@@ -585,13 +1426,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
585
1426
|
if (delta.content != null) {
|
|
586
1427
|
controller.enqueue({
|
|
587
1428
|
type: "text-delta",
|
|
588
|
-
|
|
1429
|
+
delta: delta.content,
|
|
1430
|
+
id: generateId()
|
|
589
1431
|
});
|
|
590
1432
|
}
|
|
591
1433
|
if (delta.reasoning != null) {
|
|
592
1434
|
controller.enqueue({
|
|
593
|
-
type: "reasoning",
|
|
594
|
-
|
|
1435
|
+
type: "reasoning-delta",
|
|
1436
|
+
delta: delta.reasoning,
|
|
1437
|
+
id: generateId()
|
|
595
1438
|
});
|
|
596
1439
|
}
|
|
597
1440
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
@@ -600,14 +1443,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
600
1443
|
case "reasoning.text" /* Text */: {
|
|
601
1444
|
if (detail.text) {
|
|
602
1445
|
controller.enqueue({
|
|
603
|
-
type: "reasoning",
|
|
604
|
-
|
|
1446
|
+
type: "reasoning-delta",
|
|
1447
|
+
delta: detail.text,
|
|
1448
|
+
id: generateId()
|
|
605
1449
|
});
|
|
606
1450
|
}
|
|
607
1451
|
if (detail.signature) {
|
|
608
1452
|
controller.enqueue({
|
|
609
|
-
type: "reasoning-
|
|
610
|
-
|
|
1453
|
+
type: "reasoning-end",
|
|
1454
|
+
id: generateId()
|
|
611
1455
|
});
|
|
612
1456
|
}
|
|
613
1457
|
break;
|
|
@@ -615,8 +1459,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
615
1459
|
case "reasoning.encrypted" /* Encrypted */: {
|
|
616
1460
|
if (detail.data) {
|
|
617
1461
|
controller.enqueue({
|
|
618
|
-
type: "
|
|
619
|
-
|
|
1462
|
+
type: "reasoning-delta",
|
|
1463
|
+
delta: "[REDACTED]",
|
|
1464
|
+
id: generateId()
|
|
620
1465
|
});
|
|
621
1466
|
}
|
|
622
1467
|
break;
|
|
@@ -624,8 +1469,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
624
1469
|
case "reasoning.summary" /* Summary */: {
|
|
625
1470
|
if (detail.summary) {
|
|
626
1471
|
controller.enqueue({
|
|
627
|
-
type: "reasoning",
|
|
628
|
-
|
|
1472
|
+
type: "reasoning-delta",
|
|
1473
|
+
delta: detail.summary,
|
|
1474
|
+
id: generateId()
|
|
629
1475
|
});
|
|
630
1476
|
}
|
|
631
1477
|
break;
|
|
@@ -637,33 +1483,24 @@ var OpenRouterChatLanguageModel = class {
|
|
|
637
1483
|
}
|
|
638
1484
|
}
|
|
639
1485
|
}
|
|
640
|
-
const mappedLogprobs = mapOpenRouterChatLogProbsOutput(
|
|
641
|
-
choice == null ? void 0 : choice.logprobs
|
|
642
|
-
);
|
|
643
|
-
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
644
|
-
if (logprobs === void 0) {
|
|
645
|
-
logprobs = [];
|
|
646
|
-
}
|
|
647
|
-
logprobs.push(...mappedLogprobs);
|
|
648
|
-
}
|
|
649
1486
|
if (delta.tool_calls != null) {
|
|
650
1487
|
for (const toolCallDelta of delta.tool_calls) {
|
|
651
1488
|
const index = toolCallDelta.index;
|
|
652
1489
|
if (toolCalls[index] == null) {
|
|
653
1490
|
if (toolCallDelta.type !== "function") {
|
|
654
|
-
throw new
|
|
1491
|
+
throw new InvalidResponseDataError({
|
|
655
1492
|
data: toolCallDelta,
|
|
656
1493
|
message: `Expected 'function' type.`
|
|
657
1494
|
});
|
|
658
1495
|
}
|
|
659
1496
|
if (toolCallDelta.id == null) {
|
|
660
|
-
throw new
|
|
1497
|
+
throw new InvalidResponseDataError({
|
|
661
1498
|
data: toolCallDelta,
|
|
662
1499
|
message: `Expected 'id' to be a string.`
|
|
663
1500
|
});
|
|
664
1501
|
}
|
|
665
|
-
if (((
|
|
666
|
-
throw new
|
|
1502
|
+
if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
|
|
1503
|
+
throw new InvalidResponseDataError({
|
|
667
1504
|
data: toolCallDelta,
|
|
668
1505
|
message: `Expected 'function.name' to be a string.`
|
|
669
1506
|
});
|
|
@@ -681,20 +1518,26 @@ var OpenRouterChatLanguageModel = class {
|
|
|
681
1518
|
if (toolCall2 == null) {
|
|
682
1519
|
throw new Error("Tool call is missing");
|
|
683
1520
|
}
|
|
684
|
-
if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null &&
|
|
1521
|
+
if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
685
1522
|
controller.enqueue({
|
|
686
|
-
type: "tool-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
1523
|
+
type: "tool-input-start",
|
|
1524
|
+
id: toolCall2.id,
|
|
1525
|
+
toolName: toolCall2.function.name
|
|
1526
|
+
});
|
|
1527
|
+
controller.enqueue({
|
|
1528
|
+
type: "tool-input-delta",
|
|
1529
|
+
id: toolCall2.id,
|
|
1530
|
+
delta: toolCall2.function.arguments
|
|
1531
|
+
});
|
|
1532
|
+
controller.enqueue({
|
|
1533
|
+
type: "tool-input-end",
|
|
1534
|
+
id: toolCall2.id
|
|
691
1535
|
});
|
|
692
1536
|
controller.enqueue({
|
|
693
1537
|
type: "tool-call",
|
|
694
|
-
|
|
695
|
-
toolCallId: (_g = toolCall2.id) != null ? _g : (0, import_provider_utils3.generateId)(),
|
|
1538
|
+
toolCallId: toolCall2.id,
|
|
696
1539
|
toolName: toolCall2.function.name,
|
|
697
|
-
|
|
1540
|
+
input: toolCall2.function.arguments
|
|
698
1541
|
});
|
|
699
1542
|
toolCall2.sent = true;
|
|
700
1543
|
}
|
|
@@ -704,23 +1547,27 @@ var OpenRouterChatLanguageModel = class {
|
|
|
704
1547
|
if (toolCall == null) {
|
|
705
1548
|
throw new Error("Tool call is missing");
|
|
706
1549
|
}
|
|
1550
|
+
if (((_g = toolCallDelta.function) == null ? void 0 : _g.name) != null) {
|
|
1551
|
+
controller.enqueue({
|
|
1552
|
+
type: "tool-input-start",
|
|
1553
|
+
id: toolCall.id,
|
|
1554
|
+
toolName: toolCall.function.name
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
707
1557
|
if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
|
|
708
1558
|
toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
|
|
709
1559
|
}
|
|
710
1560
|
controller.enqueue({
|
|
711
|
-
type: "tool-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
toolName: toolCall.function.name,
|
|
715
|
-
argsTextDelta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
|
|
1561
|
+
type: "tool-input-delta",
|
|
1562
|
+
id: toolCall.id,
|
|
1563
|
+
delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
|
|
716
1564
|
});
|
|
717
|
-
if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null &&
|
|
1565
|
+
if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
718
1566
|
controller.enqueue({
|
|
719
1567
|
type: "tool-call",
|
|
720
|
-
|
|
721
|
-
toolCallId: (_n = toolCall.id) != null ? _n : (0, import_provider_utils3.generateId)(),
|
|
1568
|
+
toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
|
|
722
1569
|
toolName: toolCall.function.name,
|
|
723
|
-
|
|
1570
|
+
input: toolCall.function.arguments
|
|
724
1571
|
});
|
|
725
1572
|
toolCall.sent = true;
|
|
726
1573
|
}
|
|
@@ -728,198 +1575,141 @@ var OpenRouterChatLanguageModel = class {
|
|
|
728
1575
|
}
|
|
729
1576
|
},
|
|
730
1577
|
flush(controller) {
|
|
731
|
-
var
|
|
1578
|
+
var _a16;
|
|
732
1579
|
if (finishReason === "tool-calls") {
|
|
733
1580
|
for (const toolCall of toolCalls) {
|
|
734
1581
|
if (!toolCall.sent) {
|
|
735
1582
|
controller.enqueue({
|
|
736
1583
|
type: "tool-call",
|
|
737
|
-
|
|
738
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils3.generateId)(),
|
|
1584
|
+
toolCallId: (_a16 = toolCall.id) != null ? _a16 : generateId(),
|
|
739
1585
|
toolName: toolCall.function.name,
|
|
740
1586
|
// Coerce invalid arguments to an empty JSON object
|
|
741
|
-
|
|
1587
|
+
input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}"
|
|
742
1588
|
});
|
|
743
1589
|
toolCall.sent = true;
|
|
744
1590
|
}
|
|
745
1591
|
}
|
|
746
1592
|
}
|
|
747
|
-
|
|
748
|
-
if (shouldIncludeUsageAccounting && (openrouterUsage.totalTokens !== void 0 || openrouterUsage.cost !== void 0 || openrouterUsage.promptTokensDetails !== void 0 || openrouterUsage.completionTokensDetails !== void 0)) {
|
|
749
|
-
providerMetadata.openrouter = {
|
|
750
|
-
usage: openrouterUsage
|
|
751
|
-
};
|
|
752
|
-
}
|
|
753
|
-
const hasProviderMetadata = Object.keys(providerMetadata).length > 0 && shouldIncludeUsageAccounting;
|
|
754
|
-
controller.enqueue(__spreadValues({
|
|
1593
|
+
controller.enqueue({
|
|
755
1594
|
type: "finish",
|
|
756
1595
|
finishReason,
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
1596
|
+
usage,
|
|
1597
|
+
providerMetadata: {
|
|
1598
|
+
openrouter: {
|
|
1599
|
+
usage: openrouterUsage
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
760
1603
|
}
|
|
761
1604
|
})
|
|
762
1605
|
),
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1606
|
+
warnings: [],
|
|
1607
|
+
request: { body: args },
|
|
1608
|
+
response: { headers: responseHeaders }
|
|
766
1609
|
};
|
|
767
1610
|
}
|
|
768
1611
|
};
|
|
769
|
-
var OpenRouterChatCompletionBaseResponseSchema =
|
|
770
|
-
id:
|
|
771
|
-
model:
|
|
772
|
-
usage:
|
|
773
|
-
prompt_tokens:
|
|
774
|
-
prompt_tokens_details:
|
|
775
|
-
cached_tokens:
|
|
1612
|
+
var OpenRouterChatCompletionBaseResponseSchema = import_v44.z.object({
|
|
1613
|
+
id: import_v44.z.string().optional(),
|
|
1614
|
+
model: import_v44.z.string().optional(),
|
|
1615
|
+
usage: import_v44.z.object({
|
|
1616
|
+
prompt_tokens: import_v44.z.number(),
|
|
1617
|
+
prompt_tokens_details: import_v44.z.object({
|
|
1618
|
+
cached_tokens: import_v44.z.number()
|
|
776
1619
|
}).nullish(),
|
|
777
|
-
completion_tokens:
|
|
778
|
-
completion_tokens_details:
|
|
779
|
-
reasoning_tokens:
|
|
1620
|
+
completion_tokens: import_v44.z.number(),
|
|
1621
|
+
completion_tokens_details: import_v44.z.object({
|
|
1622
|
+
reasoning_tokens: import_v44.z.number()
|
|
780
1623
|
}).nullish(),
|
|
781
|
-
total_tokens:
|
|
782
|
-
cost:
|
|
1624
|
+
total_tokens: import_v44.z.number(),
|
|
1625
|
+
cost: import_v44.z.number().optional()
|
|
783
1626
|
}).nullish()
|
|
784
1627
|
});
|
|
785
1628
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
786
|
-
choices:
|
|
787
|
-
|
|
788
|
-
message:
|
|
789
|
-
role:
|
|
790
|
-
content:
|
|
791
|
-
reasoning:
|
|
1629
|
+
choices: import_v44.z.array(
|
|
1630
|
+
import_v44.z.object({
|
|
1631
|
+
message: import_v44.z.object({
|
|
1632
|
+
role: import_v44.z.literal("assistant"),
|
|
1633
|
+
content: import_v44.z.string().nullable().optional(),
|
|
1634
|
+
reasoning: import_v44.z.string().nullable().optional(),
|
|
792
1635
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
793
|
-
tool_calls:
|
|
794
|
-
|
|
795
|
-
id:
|
|
796
|
-
type:
|
|
797
|
-
function:
|
|
798
|
-
name:
|
|
799
|
-
arguments:
|
|
1636
|
+
tool_calls: import_v44.z.array(
|
|
1637
|
+
import_v44.z.object({
|
|
1638
|
+
id: import_v44.z.string().optional().nullable(),
|
|
1639
|
+
type: import_v44.z.literal("function"),
|
|
1640
|
+
function: import_v44.z.object({
|
|
1641
|
+
name: import_v44.z.string(),
|
|
1642
|
+
arguments: import_v44.z.string()
|
|
800
1643
|
})
|
|
801
1644
|
})
|
|
802
1645
|
).optional()
|
|
803
1646
|
}),
|
|
804
|
-
index:
|
|
805
|
-
logprobs:
|
|
806
|
-
content:
|
|
807
|
-
|
|
808
|
-
token:
|
|
809
|
-
logprob:
|
|
810
|
-
top_logprobs:
|
|
811
|
-
|
|
812
|
-
token:
|
|
813
|
-
logprob:
|
|
1647
|
+
index: import_v44.z.number(),
|
|
1648
|
+
logprobs: import_v44.z.object({
|
|
1649
|
+
content: import_v44.z.array(
|
|
1650
|
+
import_v44.z.object({
|
|
1651
|
+
token: import_v44.z.string(),
|
|
1652
|
+
logprob: import_v44.z.number(),
|
|
1653
|
+
top_logprobs: import_v44.z.array(
|
|
1654
|
+
import_v44.z.object({
|
|
1655
|
+
token: import_v44.z.string(),
|
|
1656
|
+
logprob: import_v44.z.number()
|
|
814
1657
|
})
|
|
815
1658
|
)
|
|
816
1659
|
})
|
|
817
1660
|
).nullable()
|
|
818
1661
|
}).nullable().optional(),
|
|
819
|
-
finish_reason:
|
|
1662
|
+
finish_reason: import_v44.z.string().optional().nullable()
|
|
820
1663
|
})
|
|
821
1664
|
)
|
|
822
1665
|
});
|
|
823
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1666
|
+
var OpenRouterStreamChatCompletionChunkSchema = import_v44.z.union([
|
|
824
1667
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
825
|
-
choices:
|
|
826
|
-
|
|
827
|
-
delta:
|
|
828
|
-
role:
|
|
829
|
-
content:
|
|
830
|
-
reasoning:
|
|
1668
|
+
choices: import_v44.z.array(
|
|
1669
|
+
import_v44.z.object({
|
|
1670
|
+
delta: import_v44.z.object({
|
|
1671
|
+
role: import_v44.z.enum(["assistant"]).optional(),
|
|
1672
|
+
content: import_v44.z.string().nullish(),
|
|
1673
|
+
reasoning: import_v44.z.string().nullish().optional(),
|
|
831
1674
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
832
|
-
tool_calls:
|
|
833
|
-
|
|
834
|
-
index:
|
|
835
|
-
id:
|
|
836
|
-
type:
|
|
837
|
-
function:
|
|
838
|
-
name:
|
|
839
|
-
arguments:
|
|
1675
|
+
tool_calls: import_v44.z.array(
|
|
1676
|
+
import_v44.z.object({
|
|
1677
|
+
index: import_v44.z.number(),
|
|
1678
|
+
id: import_v44.z.string().nullish(),
|
|
1679
|
+
type: import_v44.z.literal("function").optional(),
|
|
1680
|
+
function: import_v44.z.object({
|
|
1681
|
+
name: import_v44.z.string().nullish(),
|
|
1682
|
+
arguments: import_v44.z.string().nullish()
|
|
840
1683
|
})
|
|
841
1684
|
})
|
|
842
1685
|
).nullish()
|
|
843
1686
|
}).nullish(),
|
|
844
|
-
logprobs:
|
|
845
|
-
content:
|
|
846
|
-
|
|
847
|
-
token:
|
|
848
|
-
logprob:
|
|
849
|
-
top_logprobs:
|
|
850
|
-
|
|
851
|
-
token:
|
|
852
|
-
logprob:
|
|
1687
|
+
logprobs: import_v44.z.object({
|
|
1688
|
+
content: import_v44.z.array(
|
|
1689
|
+
import_v44.z.object({
|
|
1690
|
+
token: import_v44.z.string(),
|
|
1691
|
+
logprob: import_v44.z.number(),
|
|
1692
|
+
top_logprobs: import_v44.z.array(
|
|
1693
|
+
import_v44.z.object({
|
|
1694
|
+
token: import_v44.z.string(),
|
|
1695
|
+
logprob: import_v44.z.number()
|
|
853
1696
|
})
|
|
854
1697
|
)
|
|
855
1698
|
})
|
|
856
1699
|
).nullable()
|
|
857
1700
|
}).nullish(),
|
|
858
|
-
finish_reason:
|
|
859
|
-
index:
|
|
1701
|
+
finish_reason: import_v44.z.string().nullable().optional(),
|
|
1702
|
+
index: import_v44.z.number()
|
|
860
1703
|
})
|
|
861
1704
|
)
|
|
862
1705
|
}),
|
|
863
1706
|
OpenRouterErrorResponseSchema
|
|
864
1707
|
]);
|
|
865
|
-
function prepareToolsAndToolChoice(mode) {
|
|
866
|
-
var _a;
|
|
867
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
868
|
-
if (tools == null) {
|
|
869
|
-
return { tools: void 0, tool_choice: void 0 };
|
|
870
|
-
}
|
|
871
|
-
const mappedTools = tools.map((tool) => {
|
|
872
|
-
if (isFunctionTool(tool)) {
|
|
873
|
-
return {
|
|
874
|
-
type: "function",
|
|
875
|
-
function: {
|
|
876
|
-
name: tool.name,
|
|
877
|
-
description: tool.description,
|
|
878
|
-
parameters: tool.parameters
|
|
879
|
-
}
|
|
880
|
-
};
|
|
881
|
-
}
|
|
882
|
-
return {
|
|
883
|
-
type: "function",
|
|
884
|
-
function: {
|
|
885
|
-
name: tool.name
|
|
886
|
-
}
|
|
887
|
-
};
|
|
888
|
-
});
|
|
889
|
-
const toolChoice = mode.toolChoice;
|
|
890
|
-
if (toolChoice == null) {
|
|
891
|
-
return { tools: mappedTools, tool_choice: void 0 };
|
|
892
|
-
}
|
|
893
|
-
const type = toolChoice.type;
|
|
894
|
-
switch (type) {
|
|
895
|
-
case "auto":
|
|
896
|
-
case "none":
|
|
897
|
-
case "required":
|
|
898
|
-
return { tools: mappedTools, tool_choice: type };
|
|
899
|
-
case "tool":
|
|
900
|
-
return {
|
|
901
|
-
tools: mappedTools,
|
|
902
|
-
tool_choice: {
|
|
903
|
-
type: "function",
|
|
904
|
-
function: {
|
|
905
|
-
name: toolChoice.toolName
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
};
|
|
909
|
-
default: {
|
|
910
|
-
const _exhaustiveCheck = type;
|
|
911
|
-
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
1708
|
|
|
916
|
-
// src/
|
|
917
|
-
var
|
|
918
|
-
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
919
|
-
var import_zod4 = require("zod");
|
|
1709
|
+
// src/completion/index.ts
|
|
1710
|
+
var import_v45 = require("zod/v4");
|
|
920
1711
|
|
|
921
|
-
// src/convert-to-openrouter-completion-prompt.ts
|
|
922
|
-
var import_provider2 = require("@ai-sdk/provider");
|
|
1712
|
+
// src/completion/convert-to-openrouter-completion-prompt.ts
|
|
923
1713
|
function convertToOpenRouterCompletionPrompt({
|
|
924
1714
|
prompt,
|
|
925
1715
|
inputFormat,
|
|
@@ -939,8 +1729,8 @@ function convertToOpenRouterCompletionPrompt({
|
|
|
939
1729
|
for (const { role, content } of prompt) {
|
|
940
1730
|
switch (role) {
|
|
941
1731
|
case "system": {
|
|
942
|
-
throw new
|
|
943
|
-
message:
|
|
1732
|
+
throw new InvalidPromptError({
|
|
1733
|
+
message: `Unexpected system message in prompt: ${content}`,
|
|
944
1734
|
prompt
|
|
945
1735
|
});
|
|
946
1736
|
}
|
|
@@ -950,21 +1740,13 @@ function convertToOpenRouterCompletionPrompt({
|
|
|
950
1740
|
case "text": {
|
|
951
1741
|
return part.text;
|
|
952
1742
|
}
|
|
953
|
-
case "image": {
|
|
954
|
-
throw new import_provider2.UnsupportedFunctionalityError({
|
|
955
|
-
functionality: "images"
|
|
956
|
-
});
|
|
957
|
-
}
|
|
958
1743
|
case "file": {
|
|
959
|
-
throw new
|
|
1744
|
+
throw new UnsupportedFunctionalityError({
|
|
960
1745
|
functionality: "file attachments"
|
|
961
1746
|
});
|
|
962
1747
|
}
|
|
963
1748
|
default: {
|
|
964
|
-
|
|
965
|
-
throw new Error(
|
|
966
|
-
`Unsupported content type: ${_exhaustiveCheck}`
|
|
967
|
-
);
|
|
1749
|
+
return "";
|
|
968
1750
|
}
|
|
969
1751
|
}
|
|
970
1752
|
}).join("");
|
|
@@ -975,39 +1757,38 @@ ${userMessage}
|
|
|
975
1757
|
break;
|
|
976
1758
|
}
|
|
977
1759
|
case "assistant": {
|
|
978
|
-
const assistantMessage = content.map(
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
);
|
|
1760
|
+
const assistantMessage = content.map(
|
|
1761
|
+
(part) => {
|
|
1762
|
+
switch (part.type) {
|
|
1763
|
+
case "text": {
|
|
1764
|
+
return part.text;
|
|
1765
|
+
}
|
|
1766
|
+
case "tool-call": {
|
|
1767
|
+
throw new UnsupportedFunctionalityError({
|
|
1768
|
+
functionality: "tool-call messages"
|
|
1769
|
+
});
|
|
1770
|
+
}
|
|
1771
|
+
case "tool-result": {
|
|
1772
|
+
throw new UnsupportedFunctionalityError({
|
|
1773
|
+
functionality: "tool-result messages"
|
|
1774
|
+
});
|
|
1775
|
+
}
|
|
1776
|
+
case "reasoning": {
|
|
1777
|
+
throw new UnsupportedFunctionalityError({
|
|
1778
|
+
functionality: "reasoning messages"
|
|
1779
|
+
});
|
|
1780
|
+
}
|
|
1781
|
+
case "file": {
|
|
1782
|
+
throw new UnsupportedFunctionalityError({
|
|
1783
|
+
functionality: "file attachments"
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
default: {
|
|
1787
|
+
return "";
|
|
1788
|
+
}
|
|
1008
1789
|
}
|
|
1009
1790
|
}
|
|
1010
|
-
|
|
1791
|
+
).join("");
|
|
1011
1792
|
text += `${assistant}:
|
|
1012
1793
|
${assistantMessage}
|
|
1013
1794
|
|
|
@@ -1015,13 +1796,12 @@ ${assistantMessage}
|
|
|
1015
1796
|
break;
|
|
1016
1797
|
}
|
|
1017
1798
|
case "tool": {
|
|
1018
|
-
throw new
|
|
1799
|
+
throw new UnsupportedFunctionalityError({
|
|
1019
1800
|
functionality: "tool messages"
|
|
1020
1801
|
});
|
|
1021
1802
|
}
|
|
1022
1803
|
default: {
|
|
1023
|
-
|
|
1024
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
1804
|
+
break;
|
|
1025
1805
|
}
|
|
1026
1806
|
}
|
|
1027
1807
|
}
|
|
@@ -1032,40 +1812,60 @@ ${assistantMessage}
|
|
|
1032
1812
|
};
|
|
1033
1813
|
}
|
|
1034
1814
|
|
|
1035
|
-
// src/
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
(
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1815
|
+
// src/completion/index.ts
|
|
1816
|
+
var OpenRouterCompletionChunkSchema = import_v45.z.union([
|
|
1817
|
+
import_v45.z.object({
|
|
1818
|
+
id: import_v45.z.string().optional(),
|
|
1819
|
+
model: import_v45.z.string().optional(),
|
|
1820
|
+
choices: import_v45.z.array(
|
|
1821
|
+
import_v45.z.object({
|
|
1822
|
+
text: import_v45.z.string(),
|
|
1823
|
+
reasoning: import_v45.z.string().nullish().optional(),
|
|
1824
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1825
|
+
finish_reason: import_v45.z.string().nullish(),
|
|
1826
|
+
index: import_v45.z.number(),
|
|
1827
|
+
logprobs: import_v45.z.object({
|
|
1828
|
+
tokens: import_v45.z.array(import_v45.z.string()),
|
|
1829
|
+
token_logprobs: import_v45.z.array(import_v45.z.number()),
|
|
1830
|
+
top_logprobs: import_v45.z.array(import_v45.z.record(import_v45.z.string(), import_v45.z.number())).nullable()
|
|
1831
|
+
}).nullable().optional()
|
|
1832
|
+
})
|
|
1833
|
+
),
|
|
1834
|
+
usage: import_v45.z.object({
|
|
1835
|
+
prompt_tokens: import_v45.z.number(),
|
|
1836
|
+
prompt_tokens_details: import_v45.z.object({
|
|
1837
|
+
cached_tokens: import_v45.z.number()
|
|
1838
|
+
}).nullish(),
|
|
1839
|
+
completion_tokens: import_v45.z.number(),
|
|
1840
|
+
completion_tokens_details: import_v45.z.object({
|
|
1841
|
+
reasoning_tokens: import_v45.z.number()
|
|
1842
|
+
}).nullish(),
|
|
1843
|
+
total_tokens: import_v45.z.number(),
|
|
1844
|
+
cost: import_v45.z.number().optional()
|
|
1845
|
+
}).nullish()
|
|
1846
|
+
}),
|
|
1847
|
+
OpenRouterErrorResponseSchema
|
|
1848
|
+
]);
|
|
1053
1849
|
var OpenRouterCompletionLanguageModel = class {
|
|
1054
1850
|
constructor(modelId, settings, config) {
|
|
1055
|
-
this.specificationVersion = "
|
|
1851
|
+
this.specificationVersion = "v2";
|
|
1852
|
+
this.provider = "openrouter";
|
|
1853
|
+
this.supportedUrls = {
|
|
1854
|
+
"image/*": [
|
|
1855
|
+
/^data:image\/[a-zA-Z]+;base64,/,
|
|
1856
|
+
/^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
|
|
1857
|
+
],
|
|
1858
|
+
"text/*": [/^data:text\//, /^https?:\/\/.+$/],
|
|
1859
|
+
"application/*": [/^data:application\//, /^https?:\/\/.+$/]
|
|
1860
|
+
};
|
|
1056
1861
|
this.defaultObjectGenerationMode = void 0;
|
|
1057
1862
|
this.modelId = modelId;
|
|
1058
1863
|
this.settings = settings;
|
|
1059
1864
|
this.config = config;
|
|
1060
1865
|
}
|
|
1061
|
-
get provider() {
|
|
1062
|
-
return this.config.provider;
|
|
1063
|
-
}
|
|
1064
1866
|
getArgs({
|
|
1065
|
-
mode,
|
|
1066
|
-
inputFormat,
|
|
1067
1867
|
prompt,
|
|
1068
|
-
|
|
1868
|
+
maxOutputTokens,
|
|
1069
1869
|
temperature,
|
|
1070
1870
|
topP,
|
|
1071
1871
|
frequencyPenalty,
|
|
@@ -1074,16 +1874,24 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1074
1874
|
responseFormat,
|
|
1075
1875
|
topK,
|
|
1076
1876
|
stopSequences,
|
|
1077
|
-
|
|
1877
|
+
tools,
|
|
1878
|
+
toolChoice
|
|
1078
1879
|
}) {
|
|
1079
|
-
var _a, _b;
|
|
1080
|
-
const type = mode.type;
|
|
1081
|
-
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata.openrouter) != null ? _a : {};
|
|
1082
1880
|
const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
|
|
1083
1881
|
prompt,
|
|
1084
|
-
inputFormat
|
|
1882
|
+
inputFormat: "prompt"
|
|
1085
1883
|
});
|
|
1086
|
-
|
|
1884
|
+
if (tools == null ? void 0 : tools.length) {
|
|
1885
|
+
throw new UnsupportedFunctionalityError({
|
|
1886
|
+
functionality: "tools"
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
if (toolChoice) {
|
|
1890
|
+
throw new UnsupportedFunctionalityError({
|
|
1891
|
+
functionality: "toolChoice"
|
|
1892
|
+
});
|
|
1893
|
+
}
|
|
1894
|
+
return __spreadValues(__spreadValues({
|
|
1087
1895
|
// model id:
|
|
1088
1896
|
model: this.modelId,
|
|
1089
1897
|
models: this.settings.models,
|
|
@@ -1093,7 +1901,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1093
1901
|
suffix: this.settings.suffix,
|
|
1094
1902
|
user: this.settings.user,
|
|
1095
1903
|
// standardized settings:
|
|
1096
|
-
max_tokens:
|
|
1904
|
+
max_tokens: maxOutputTokens,
|
|
1097
1905
|
temperature,
|
|
1098
1906
|
top_p: topP,
|
|
1099
1907
|
frequency_penalty: frequencyPenalty,
|
|
@@ -1107,58 +1915,27 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1107
1915
|
// OpenRouter specific settings:
|
|
1108
1916
|
include_reasoning: this.settings.includeReasoning,
|
|
1109
1917
|
reasoning: this.settings.reasoning
|
|
1110
|
-
}, this.config.extraBody), this.settings.extraBody)
|
|
1111
|
-
switch (type) {
|
|
1112
|
-
case "regular": {
|
|
1113
|
-
if ((_b = mode.tools) == null ? void 0 : _b.length) {
|
|
1114
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1115
|
-
functionality: "tools"
|
|
1116
|
-
});
|
|
1117
|
-
}
|
|
1118
|
-
if (mode.toolChoice) {
|
|
1119
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1120
|
-
functionality: "toolChoice"
|
|
1121
|
-
});
|
|
1122
|
-
}
|
|
1123
|
-
return baseArgs;
|
|
1124
|
-
}
|
|
1125
|
-
case "object-json": {
|
|
1126
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1127
|
-
functionality: "object-json mode"
|
|
1128
|
-
});
|
|
1129
|
-
}
|
|
1130
|
-
case "object-tool": {
|
|
1131
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1132
|
-
functionality: "object-tool mode"
|
|
1133
|
-
});
|
|
1134
|
-
}
|
|
1135
|
-
// Handle all non-text types with a single default case
|
|
1136
|
-
default: {
|
|
1137
|
-
const _exhaustiveCheck = type;
|
|
1138
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1139
|
-
functionality: `${_exhaustiveCheck} mode`
|
|
1140
|
-
});
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1918
|
+
}, this.config.extraBody), this.settings.extraBody);
|
|
1143
1919
|
}
|
|
1144
1920
|
async doGenerate(options) {
|
|
1145
|
-
var _b, _c, _d, _e, _f;
|
|
1146
|
-
const
|
|
1147
|
-
const
|
|
1921
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
1922
|
+
const providerOptions = options.providerOptions || {};
|
|
1923
|
+
const openrouterOptions = providerOptions.openrouter || {};
|
|
1924
|
+
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1925
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
1148
1926
|
url: this.config.url({
|
|
1149
1927
|
path: "/completions",
|
|
1150
1928
|
modelId: this.modelId
|
|
1151
1929
|
}),
|
|
1152
|
-
headers:
|
|
1930
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
1153
1931
|
body: args,
|
|
1154
1932
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
1155
|
-
successfulResponseHandler:
|
|
1933
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
1156
1934
|
OpenRouterCompletionChunkSchema
|
|
1157
1935
|
),
|
|
1158
1936
|
abortSignal: options.abortSignal,
|
|
1159
1937
|
fetch: this.config.fetch
|
|
1160
1938
|
});
|
|
1161
|
-
const _a = args, { prompt: rawPrompt } = _a, rawSettings = __objRest(_a, ["prompt"]);
|
|
1162
1939
|
if ("error" in response) {
|
|
1163
1940
|
throw new Error(`${response.error.message}`);
|
|
1164
1941
|
}
|
|
@@ -1167,54 +1944,62 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1167
1944
|
throw new Error("No choice in OpenRouter completion response");
|
|
1168
1945
|
}
|
|
1169
1946
|
return {
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1947
|
+
content: [
|
|
1948
|
+
{
|
|
1949
|
+
type: "text",
|
|
1950
|
+
text: (_a15 = choice.text) != null ? _a15 : ""
|
|
1951
|
+
}
|
|
1952
|
+
],
|
|
1953
|
+
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
1176
1954
|
usage: {
|
|
1177
|
-
|
|
1178
|
-
|
|
1955
|
+
inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : 0,
|
|
1956
|
+
outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : 0,
|
|
1957
|
+
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),
|
|
1958
|
+
reasoningTokens: (_l = (_k = (_j = response.usage) == null ? void 0 : _j.completion_tokens_details) == null ? void 0 : _k.reasoning_tokens) != null ? _l : 0,
|
|
1959
|
+
cachedInputTokens: (_o = (_n = (_m = response.usage) == null ? void 0 : _m.prompt_tokens_details) == null ? void 0 : _n.cached_tokens) != null ? _o : 0
|
|
1179
1960
|
},
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
warnings: []
|
|
1961
|
+
warnings: [],
|
|
1962
|
+
response: {
|
|
1963
|
+
headers: responseHeaders
|
|
1964
|
+
}
|
|
1185
1965
|
};
|
|
1186
1966
|
}
|
|
1187
1967
|
async doStream(options) {
|
|
1188
|
-
const
|
|
1189
|
-
const
|
|
1968
|
+
const providerOptions = options.providerOptions || {};
|
|
1969
|
+
const openrouterOptions = providerOptions.openrouter || {};
|
|
1970
|
+
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1971
|
+
const { value: response, responseHeaders } = await postJsonToApi({
|
|
1190
1972
|
url: this.config.url({
|
|
1191
1973
|
path: "/completions",
|
|
1192
1974
|
modelId: this.modelId
|
|
1193
1975
|
}),
|
|
1194
|
-
headers:
|
|
1195
|
-
body: __spreadProps(__spreadValues({},
|
|
1976
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
1977
|
+
body: __spreadProps(__spreadValues({}, args), {
|
|
1196
1978
|
stream: true,
|
|
1197
1979
|
// only include stream_options when in strict compatibility mode:
|
|
1198
1980
|
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
|
|
1199
1981
|
}),
|
|
1200
1982
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
1201
|
-
successfulResponseHandler:
|
|
1983
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
1202
1984
|
OpenRouterCompletionChunkSchema
|
|
1203
1985
|
),
|
|
1204
1986
|
abortSignal: options.abortSignal,
|
|
1205
1987
|
fetch: this.config.fetch
|
|
1206
1988
|
});
|
|
1207
|
-
const _a = args, { prompt: rawPrompt } = _a, rawSettings = __objRest(_a, ["prompt"]);
|
|
1208
1989
|
let finishReason = "other";
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1990
|
+
const usage = {
|
|
1991
|
+
inputTokens: Number.NaN,
|
|
1992
|
+
outputTokens: Number.NaN,
|
|
1993
|
+
totalTokens: Number.NaN,
|
|
1994
|
+
reasoningTokens: Number.NaN,
|
|
1995
|
+
cachedInputTokens: Number.NaN
|
|
1212
1996
|
};
|
|
1213
|
-
|
|
1997
|
+
const openrouterUsage = {};
|
|
1214
1998
|
return {
|
|
1215
1999
|
stream: response.pipeThrough(
|
|
1216
2000
|
new TransformStream({
|
|
1217
2001
|
transform(chunk, controller) {
|
|
2002
|
+
var _a15, _b;
|
|
1218
2003
|
if (!chunk.success) {
|
|
1219
2004
|
finishReason = "error";
|
|
1220
2005
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -1227,10 +2012,27 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1227
2012
|
return;
|
|
1228
2013
|
}
|
|
1229
2014
|
if (value.usage != null) {
|
|
1230
|
-
usage =
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
2015
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
2016
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
2017
|
+
usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
|
|
2018
|
+
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
2019
|
+
if (value.usage.prompt_tokens_details) {
|
|
2020
|
+
const cachedInputTokens = (_a15 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a15 : 0;
|
|
2021
|
+
usage.cachedInputTokens = cachedInputTokens;
|
|
2022
|
+
openrouterUsage.promptTokensDetails = {
|
|
2023
|
+
cachedTokens: cachedInputTokens
|
|
2024
|
+
};
|
|
2025
|
+
}
|
|
2026
|
+
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
2027
|
+
if (value.usage.completion_tokens_details) {
|
|
2028
|
+
const reasoningTokens = (_b = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b : 0;
|
|
2029
|
+
usage.reasoningTokens = reasoningTokens;
|
|
2030
|
+
openrouterUsage.completionTokensDetails = {
|
|
2031
|
+
reasoningTokens
|
|
2032
|
+
};
|
|
2033
|
+
}
|
|
2034
|
+
openrouterUsage.cost = value.usage.cost;
|
|
2035
|
+
openrouterUsage.totalTokens = value.usage.total_tokens;
|
|
1234
2036
|
}
|
|
1235
2037
|
const choice = value.choices[0];
|
|
1236
2038
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -1239,60 +2041,31 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
1239
2041
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
1240
2042
|
controller.enqueue({
|
|
1241
2043
|
type: "text-delta",
|
|
1242
|
-
|
|
2044
|
+
delta: choice.text,
|
|
2045
|
+
id: generateId()
|
|
1243
2046
|
});
|
|
1244
2047
|
}
|
|
1245
|
-
const mappedLogprobs = mapOpenRouterCompletionLogProbs(
|
|
1246
|
-
choice == null ? void 0 : choice.logprobs
|
|
1247
|
-
);
|
|
1248
|
-
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
1249
|
-
if (logprobs === void 0) {
|
|
1250
|
-
logprobs = [];
|
|
1251
|
-
}
|
|
1252
|
-
logprobs.push(...mappedLogprobs);
|
|
1253
|
-
}
|
|
1254
2048
|
},
|
|
1255
2049
|
flush(controller) {
|
|
1256
2050
|
controller.enqueue({
|
|
1257
2051
|
type: "finish",
|
|
1258
2052
|
finishReason,
|
|
1259
|
-
|
|
1260
|
-
|
|
2053
|
+
usage,
|
|
2054
|
+
providerMetadata: {
|
|
2055
|
+
openrouter: {
|
|
2056
|
+
usage: openrouterUsage
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
1261
2059
|
});
|
|
1262
2060
|
}
|
|
1263
2061
|
})
|
|
1264
2062
|
),
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
2063
|
+
response: {
|
|
2064
|
+
headers: responseHeaders
|
|
2065
|
+
}
|
|
1268
2066
|
};
|
|
1269
2067
|
}
|
|
1270
2068
|
};
|
|
1271
|
-
var OpenRouterCompletionChunkSchema = import_zod4.z.union([
|
|
1272
|
-
import_zod4.z.object({
|
|
1273
|
-
id: import_zod4.z.string().optional(),
|
|
1274
|
-
model: import_zod4.z.string().optional(),
|
|
1275
|
-
choices: import_zod4.z.array(
|
|
1276
|
-
import_zod4.z.object({
|
|
1277
|
-
text: import_zod4.z.string(),
|
|
1278
|
-
reasoning: import_zod4.z.string().nullish().optional(),
|
|
1279
|
-
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1280
|
-
finish_reason: import_zod4.z.string().nullish(),
|
|
1281
|
-
index: import_zod4.z.number(),
|
|
1282
|
-
logprobs: import_zod4.z.object({
|
|
1283
|
-
tokens: import_zod4.z.array(import_zod4.z.string()),
|
|
1284
|
-
token_logprobs: import_zod4.z.array(import_zod4.z.number()),
|
|
1285
|
-
top_logprobs: import_zod4.z.array(import_zod4.z.record(import_zod4.z.string(), import_zod4.z.number())).nullable()
|
|
1286
|
-
}).nullable().optional()
|
|
1287
|
-
})
|
|
1288
|
-
),
|
|
1289
|
-
usage: import_zod4.z.object({
|
|
1290
|
-
prompt_tokens: import_zod4.z.number(),
|
|
1291
|
-
completion_tokens: import_zod4.z.number()
|
|
1292
|
-
}).optional().nullable()
|
|
1293
|
-
}),
|
|
1294
|
-
OpenRouterErrorResponseSchema
|
|
1295
|
-
]);
|
|
1296
2069
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1297
2070
|
0 && (module.exports = {
|
|
1298
2071
|
OpenRouterChatLanguageModel,
|