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