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