@eko-ai/eko 2.0.3-alpha.2 → 2.0.3-alpha.3
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.cjs.js +2385 -343
- package/dist/index.esm.js +2385 -343
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/types/llm.types.d.ts +1 -1
- package/dist/types/llm.types.d.ts.map +1 -1
- package/package.json +2 -1
package/dist/index.cjs.js
CHANGED
|
@@ -165,10 +165,10 @@ class AgentContext {
|
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
// src/errors/ai-sdk-error.ts
|
|
168
|
-
var marker = "vercel.ai.error";
|
|
169
|
-
var symbol = Symbol.for(marker);
|
|
170
|
-
var _a;
|
|
171
|
-
var _AISDKError = class _AISDKError extends Error {
|
|
168
|
+
var marker$1 = "vercel.ai.error";
|
|
169
|
+
var symbol$1 = Symbol.for(marker$1);
|
|
170
|
+
var _a$1;
|
|
171
|
+
var _AISDKError$1 = class _AISDKError extends Error {
|
|
172
172
|
/**
|
|
173
173
|
* Creates an AI SDK Error.
|
|
174
174
|
*
|
|
@@ -183,7 +183,7 @@ var _AISDKError = class _AISDKError extends Error {
|
|
|
183
183
|
cause
|
|
184
184
|
}) {
|
|
185
185
|
super(message);
|
|
186
|
-
this[_a] = true;
|
|
186
|
+
this[_a$1] = true;
|
|
187
187
|
this.name = name14;
|
|
188
188
|
this.cause = cause;
|
|
189
189
|
}
|
|
@@ -193,22 +193,22 @@ var _AISDKError = class _AISDKError extends Error {
|
|
|
193
193
|
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
194
194
|
*/
|
|
195
195
|
static isInstance(error) {
|
|
196
|
-
return _AISDKError.hasMarker(error, marker);
|
|
196
|
+
return _AISDKError.hasMarker(error, marker$1);
|
|
197
197
|
}
|
|
198
198
|
static hasMarker(error, marker15) {
|
|
199
199
|
const markerSymbol = Symbol.for(marker15);
|
|
200
200
|
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
201
201
|
}
|
|
202
202
|
};
|
|
203
|
-
_a = symbol;
|
|
204
|
-
var AISDKError = _AISDKError;
|
|
203
|
+
_a$1 = symbol$1;
|
|
204
|
+
var AISDKError$1 = _AISDKError$1;
|
|
205
205
|
|
|
206
206
|
// src/errors/api-call-error.ts
|
|
207
|
-
var name = "AI_APICallError";
|
|
208
|
-
var marker2 = `vercel.ai.error.${name}`;
|
|
209
|
-
var symbol2 = Symbol.for(marker2);
|
|
210
|
-
var _a2;
|
|
211
|
-
var APICallError = class extends AISDKError {
|
|
207
|
+
var name$1 = "AI_APICallError";
|
|
208
|
+
var marker2$1 = `vercel.ai.error.${name$1}`;
|
|
209
|
+
var symbol2$1 = Symbol.for(marker2$1);
|
|
210
|
+
var _a2$1;
|
|
211
|
+
var APICallError$1 = class APICallError extends AISDKError$1 {
|
|
212
212
|
constructor({
|
|
213
213
|
message,
|
|
214
214
|
url,
|
|
@@ -224,8 +224,8 @@ var APICallError = class extends AISDKError {
|
|
|
224
224
|
// server error
|
|
225
225
|
data
|
|
226
226
|
}) {
|
|
227
|
-
super({ name, message, cause });
|
|
228
|
-
this[_a2] = true;
|
|
227
|
+
super({ name: name$1, message, cause });
|
|
228
|
+
this[_a2$1] = true;
|
|
229
229
|
this.url = url;
|
|
230
230
|
this.requestBodyValues = requestBodyValues;
|
|
231
231
|
this.statusCode = statusCode;
|
|
@@ -235,30 +235,30 @@ var APICallError = class extends AISDKError {
|
|
|
235
235
|
this.data = data;
|
|
236
236
|
}
|
|
237
237
|
static isInstance(error) {
|
|
238
|
-
return AISDKError.hasMarker(error, marker2);
|
|
238
|
+
return AISDKError$1.hasMarker(error, marker2$1);
|
|
239
239
|
}
|
|
240
240
|
};
|
|
241
|
-
_a2 = symbol2;
|
|
241
|
+
_a2$1 = symbol2$1;
|
|
242
242
|
|
|
243
243
|
// src/errors/empty-response-body-error.ts
|
|
244
|
-
var name2 = "AI_EmptyResponseBodyError";
|
|
245
|
-
var marker3 = `vercel.ai.error.${name2}`;
|
|
246
|
-
var symbol3 = Symbol.for(marker3);
|
|
247
|
-
var _a3;
|
|
248
|
-
var EmptyResponseBodyError = class extends AISDKError {
|
|
244
|
+
var name2$1 = "AI_EmptyResponseBodyError";
|
|
245
|
+
var marker3$1 = `vercel.ai.error.${name2$1}`;
|
|
246
|
+
var symbol3$1 = Symbol.for(marker3$1);
|
|
247
|
+
var _a3$1;
|
|
248
|
+
var EmptyResponseBodyError$1 = class EmptyResponseBodyError extends AISDKError$1 {
|
|
249
249
|
// used in isInstance
|
|
250
250
|
constructor({ message = "Empty response body" } = {}) {
|
|
251
|
-
super({ name: name2, message });
|
|
252
|
-
this[_a3] = true;
|
|
251
|
+
super({ name: name2$1, message });
|
|
252
|
+
this[_a3$1] = true;
|
|
253
253
|
}
|
|
254
254
|
static isInstance(error) {
|
|
255
|
-
return AISDKError.hasMarker(error, marker3);
|
|
255
|
+
return AISDKError$1.hasMarker(error, marker3$1);
|
|
256
256
|
}
|
|
257
257
|
};
|
|
258
|
-
_a3 = symbol3;
|
|
258
|
+
_a3$1 = symbol3$1;
|
|
259
259
|
|
|
260
260
|
// src/errors/get-error-message.ts
|
|
261
|
-
function getErrorMessage(error) {
|
|
261
|
+
function getErrorMessage$1(error) {
|
|
262
262
|
if (error == null) {
|
|
263
263
|
return "unknown error";
|
|
264
264
|
}
|
|
@@ -272,119 +272,119 @@ function getErrorMessage(error) {
|
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
// src/errors/invalid-argument-error.ts
|
|
275
|
-
var name3 = "AI_InvalidArgumentError";
|
|
276
|
-
var marker4 = `vercel.ai.error.${name3}`;
|
|
277
|
-
var symbol4 = Symbol.for(marker4);
|
|
278
|
-
var _a4;
|
|
279
|
-
var InvalidArgumentError = class extends AISDKError {
|
|
275
|
+
var name3$1 = "AI_InvalidArgumentError";
|
|
276
|
+
var marker4$1 = `vercel.ai.error.${name3$1}`;
|
|
277
|
+
var symbol4$1 = Symbol.for(marker4$1);
|
|
278
|
+
var _a4$1;
|
|
279
|
+
var InvalidArgumentError$1 = class InvalidArgumentError extends AISDKError$1 {
|
|
280
280
|
constructor({
|
|
281
281
|
message,
|
|
282
282
|
cause,
|
|
283
283
|
argument
|
|
284
284
|
}) {
|
|
285
|
-
super({ name: name3, message, cause });
|
|
286
|
-
this[_a4] = true;
|
|
285
|
+
super({ name: name3$1, message, cause });
|
|
286
|
+
this[_a4$1] = true;
|
|
287
287
|
this.argument = argument;
|
|
288
288
|
}
|
|
289
289
|
static isInstance(error) {
|
|
290
|
-
return AISDKError.hasMarker(error, marker4);
|
|
290
|
+
return AISDKError$1.hasMarker(error, marker4$1);
|
|
291
291
|
}
|
|
292
292
|
};
|
|
293
|
-
_a4 = symbol4;
|
|
293
|
+
_a4$1 = symbol4$1;
|
|
294
294
|
|
|
295
295
|
// src/errors/invalid-prompt-error.ts
|
|
296
|
-
var name4 = "AI_InvalidPromptError";
|
|
297
|
-
var marker5 = `vercel.ai.error.${name4}`;
|
|
298
|
-
var symbol5 = Symbol.for(marker5);
|
|
299
|
-
var _a5;
|
|
300
|
-
var InvalidPromptError = class extends AISDKError {
|
|
296
|
+
var name4$1 = "AI_InvalidPromptError";
|
|
297
|
+
var marker5$1 = `vercel.ai.error.${name4$1}`;
|
|
298
|
+
var symbol5$1 = Symbol.for(marker5$1);
|
|
299
|
+
var _a5$1;
|
|
300
|
+
var InvalidPromptError$1 = class InvalidPromptError extends AISDKError$1 {
|
|
301
301
|
constructor({
|
|
302
302
|
prompt,
|
|
303
303
|
message,
|
|
304
304
|
cause
|
|
305
305
|
}) {
|
|
306
|
-
super({ name: name4, message: `Invalid prompt: ${message}`, cause });
|
|
307
|
-
this[_a5] = true;
|
|
306
|
+
super({ name: name4$1, message: `Invalid prompt: ${message}`, cause });
|
|
307
|
+
this[_a5$1] = true;
|
|
308
308
|
this.prompt = prompt;
|
|
309
309
|
}
|
|
310
310
|
static isInstance(error) {
|
|
311
|
-
return AISDKError.hasMarker(error, marker5);
|
|
311
|
+
return AISDKError$1.hasMarker(error, marker5$1);
|
|
312
312
|
}
|
|
313
313
|
};
|
|
314
|
-
_a5 = symbol5;
|
|
314
|
+
_a5$1 = symbol5$1;
|
|
315
315
|
|
|
316
316
|
// src/errors/invalid-response-data-error.ts
|
|
317
|
-
var name5 = "AI_InvalidResponseDataError";
|
|
318
|
-
var marker6 = `vercel.ai.error.${name5}`;
|
|
319
|
-
var symbol6 = Symbol.for(marker6);
|
|
320
|
-
var _a6;
|
|
321
|
-
var InvalidResponseDataError = class extends AISDKError {
|
|
317
|
+
var name5$1 = "AI_InvalidResponseDataError";
|
|
318
|
+
var marker6$1 = `vercel.ai.error.${name5$1}`;
|
|
319
|
+
var symbol6$1 = Symbol.for(marker6$1);
|
|
320
|
+
var _a6$1;
|
|
321
|
+
var InvalidResponseDataError$1 = class InvalidResponseDataError extends AISDKError$1 {
|
|
322
322
|
constructor({
|
|
323
323
|
data,
|
|
324
324
|
message = `Invalid response data: ${JSON.stringify(data)}.`
|
|
325
325
|
}) {
|
|
326
|
-
super({ name: name5, message });
|
|
327
|
-
this[_a6] = true;
|
|
326
|
+
super({ name: name5$1, message });
|
|
327
|
+
this[_a6$1] = true;
|
|
328
328
|
this.data = data;
|
|
329
329
|
}
|
|
330
330
|
static isInstance(error) {
|
|
331
|
-
return AISDKError.hasMarker(error, marker6);
|
|
331
|
+
return AISDKError$1.hasMarker(error, marker6$1);
|
|
332
332
|
}
|
|
333
333
|
};
|
|
334
|
-
_a6 = symbol6;
|
|
334
|
+
_a6$1 = symbol6$1;
|
|
335
335
|
|
|
336
336
|
// src/errors/json-parse-error.ts
|
|
337
|
-
var name6 = "AI_JSONParseError";
|
|
338
|
-
var marker7 = `vercel.ai.error.${name6}`;
|
|
339
|
-
var symbol7 = Symbol.for(marker7);
|
|
340
|
-
var _a7;
|
|
341
|
-
var JSONParseError = class extends AISDKError {
|
|
337
|
+
var name6$1 = "AI_JSONParseError";
|
|
338
|
+
var marker7$1 = `vercel.ai.error.${name6$1}`;
|
|
339
|
+
var symbol7$1 = Symbol.for(marker7$1);
|
|
340
|
+
var _a7$1;
|
|
341
|
+
var JSONParseError$1 = class JSONParseError extends AISDKError$1 {
|
|
342
342
|
constructor({ text, cause }) {
|
|
343
343
|
super({
|
|
344
|
-
name: name6,
|
|
344
|
+
name: name6$1,
|
|
345
345
|
message: `JSON parsing failed: Text: ${text}.
|
|
346
|
-
Error message: ${getErrorMessage(cause)}`,
|
|
346
|
+
Error message: ${getErrorMessage$1(cause)}`,
|
|
347
347
|
cause
|
|
348
348
|
});
|
|
349
|
-
this[_a7] = true;
|
|
349
|
+
this[_a7$1] = true;
|
|
350
350
|
this.text = text;
|
|
351
351
|
}
|
|
352
352
|
static isInstance(error) {
|
|
353
|
-
return AISDKError.hasMarker(error, marker7);
|
|
353
|
+
return AISDKError$1.hasMarker(error, marker7$1);
|
|
354
354
|
}
|
|
355
355
|
};
|
|
356
|
-
_a7 = symbol7;
|
|
356
|
+
_a7$1 = symbol7$1;
|
|
357
357
|
|
|
358
358
|
// src/errors/load-api-key-error.ts
|
|
359
|
-
var name7 = "AI_LoadAPIKeyError";
|
|
360
|
-
var marker8 = `vercel.ai.error.${name7}`;
|
|
361
|
-
var symbol8 = Symbol.for(marker8);
|
|
362
|
-
var _a8;
|
|
363
|
-
var LoadAPIKeyError = class extends AISDKError {
|
|
359
|
+
var name7$1 = "AI_LoadAPIKeyError";
|
|
360
|
+
var marker8$1 = `vercel.ai.error.${name7$1}`;
|
|
361
|
+
var symbol8$1 = Symbol.for(marker8$1);
|
|
362
|
+
var _a8$1;
|
|
363
|
+
var LoadAPIKeyError$1 = class LoadAPIKeyError extends AISDKError$1 {
|
|
364
364
|
// used in isInstance
|
|
365
365
|
constructor({ message }) {
|
|
366
|
-
super({ name: name7, message });
|
|
367
|
-
this[_a8] = true;
|
|
366
|
+
super({ name: name7$1, message });
|
|
367
|
+
this[_a8$1] = true;
|
|
368
368
|
}
|
|
369
369
|
static isInstance(error) {
|
|
370
|
-
return AISDKError.hasMarker(error, marker8);
|
|
370
|
+
return AISDKError$1.hasMarker(error, marker8$1);
|
|
371
371
|
}
|
|
372
372
|
};
|
|
373
|
-
_a8 = symbol8;
|
|
373
|
+
_a8$1 = symbol8$1;
|
|
374
374
|
|
|
375
375
|
// src/errors/load-setting-error.ts
|
|
376
376
|
var name8 = "AI_LoadSettingError";
|
|
377
377
|
var marker9 = `vercel.ai.error.${name8}`;
|
|
378
378
|
var symbol9 = Symbol.for(marker9);
|
|
379
379
|
var _a9;
|
|
380
|
-
var LoadSettingError = class extends AISDKError {
|
|
380
|
+
var LoadSettingError = class extends AISDKError$1 {
|
|
381
381
|
// used in isInstance
|
|
382
382
|
constructor({ message }) {
|
|
383
383
|
super({ name: name8, message });
|
|
384
384
|
this[_a9] = true;
|
|
385
385
|
}
|
|
386
386
|
static isInstance(error) {
|
|
387
|
-
return AISDKError.hasMarker(error, marker9);
|
|
387
|
+
return AISDKError$1.hasMarker(error, marker9);
|
|
388
388
|
}
|
|
389
389
|
};
|
|
390
390
|
_a9 = symbol9;
|
|
@@ -394,7 +394,7 @@ var name10 = "AI_NoSuchModelError";
|
|
|
394
394
|
var marker11 = `vercel.ai.error.${name10}`;
|
|
395
395
|
var symbol11 = Symbol.for(marker11);
|
|
396
396
|
var _a11;
|
|
397
|
-
var NoSuchModelError = class extends AISDKError {
|
|
397
|
+
var NoSuchModelError = class extends AISDKError$1 {
|
|
398
398
|
constructor({
|
|
399
399
|
errorName = name10,
|
|
400
400
|
modelId,
|
|
@@ -407,7 +407,7 @@ var NoSuchModelError = class extends AISDKError {
|
|
|
407
407
|
this.modelType = modelType;
|
|
408
408
|
}
|
|
409
409
|
static isInstance(error) {
|
|
410
|
-
return AISDKError.hasMarker(error, marker11);
|
|
410
|
+
return AISDKError$1.hasMarker(error, marker11);
|
|
411
411
|
}
|
|
412
412
|
};
|
|
413
413
|
_a11 = symbol11;
|
|
@@ -417,7 +417,7 @@ var name11 = "AI_TooManyEmbeddingValuesForCallError";
|
|
|
417
417
|
var marker12 = `vercel.ai.error.${name11}`;
|
|
418
418
|
var symbol12 = Symbol.for(marker12);
|
|
419
419
|
var _a12;
|
|
420
|
-
var TooManyEmbeddingValuesForCallError = class extends AISDKError {
|
|
420
|
+
var TooManyEmbeddingValuesForCallError = class extends AISDKError$1 {
|
|
421
421
|
constructor(options) {
|
|
422
422
|
super({
|
|
423
423
|
name: name11,
|
|
@@ -430,29 +430,29 @@ var TooManyEmbeddingValuesForCallError = class extends AISDKError {
|
|
|
430
430
|
this.values = options.values;
|
|
431
431
|
}
|
|
432
432
|
static isInstance(error) {
|
|
433
|
-
return AISDKError.hasMarker(error, marker12);
|
|
433
|
+
return AISDKError$1.hasMarker(error, marker12);
|
|
434
434
|
}
|
|
435
435
|
};
|
|
436
436
|
_a12 = symbol12;
|
|
437
437
|
|
|
438
438
|
// src/errors/type-validation-error.ts
|
|
439
|
-
var name12 = "AI_TypeValidationError";
|
|
440
|
-
var marker13 = `vercel.ai.error.${name12}`;
|
|
441
|
-
var symbol13 = Symbol.for(marker13);
|
|
442
|
-
var _a13;
|
|
443
|
-
var _TypeValidationError = class _TypeValidationError extends AISDKError {
|
|
439
|
+
var name12$1 = "AI_TypeValidationError";
|
|
440
|
+
var marker13$1 = `vercel.ai.error.${name12$1}`;
|
|
441
|
+
var symbol13$1 = Symbol.for(marker13$1);
|
|
442
|
+
var _a13$1;
|
|
443
|
+
var _TypeValidationError$1 = class _TypeValidationError extends AISDKError$1 {
|
|
444
444
|
constructor({ value, cause }) {
|
|
445
445
|
super({
|
|
446
|
-
name: name12,
|
|
446
|
+
name: name12$1,
|
|
447
447
|
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
448
|
-
Error message: ${getErrorMessage(cause)}`,
|
|
448
|
+
Error message: ${getErrorMessage$1(cause)}`,
|
|
449
449
|
cause
|
|
450
450
|
});
|
|
451
|
-
this[_a13] = true;
|
|
451
|
+
this[_a13$1] = true;
|
|
452
452
|
this.value = value;
|
|
453
453
|
}
|
|
454
454
|
static isInstance(error) {
|
|
455
|
-
return AISDKError.hasMarker(error, marker13);
|
|
455
|
+
return AISDKError$1.hasMarker(error, marker13$1);
|
|
456
456
|
}
|
|
457
457
|
/**
|
|
458
458
|
* Wraps an error into a TypeValidationError.
|
|
@@ -471,28 +471,28 @@ Error message: ${getErrorMessage(cause)}`,
|
|
|
471
471
|
return _TypeValidationError.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError({ value, cause });
|
|
472
472
|
}
|
|
473
473
|
};
|
|
474
|
-
_a13 = symbol13;
|
|
475
|
-
var TypeValidationError = _TypeValidationError;
|
|
474
|
+
_a13$1 = symbol13$1;
|
|
475
|
+
var TypeValidationError$1 = _TypeValidationError$1;
|
|
476
476
|
|
|
477
477
|
// src/errors/unsupported-functionality-error.ts
|
|
478
|
-
var name13 = "AI_UnsupportedFunctionalityError";
|
|
479
|
-
var marker14 = `vercel.ai.error.${name13}`;
|
|
480
|
-
var symbol14 = Symbol.for(marker14);
|
|
481
|
-
var _a14;
|
|
482
|
-
var UnsupportedFunctionalityError = class extends AISDKError {
|
|
478
|
+
var name13$1 = "AI_UnsupportedFunctionalityError";
|
|
479
|
+
var marker14$1 = `vercel.ai.error.${name13$1}`;
|
|
480
|
+
var symbol14$1 = Symbol.for(marker14$1);
|
|
481
|
+
var _a14$1;
|
|
482
|
+
var UnsupportedFunctionalityError$1 = class UnsupportedFunctionalityError extends AISDKError$1 {
|
|
483
483
|
constructor({
|
|
484
484
|
functionality,
|
|
485
485
|
message = `'${functionality}' functionality not supported.`
|
|
486
486
|
}) {
|
|
487
|
-
super({ name: name13, message });
|
|
488
|
-
this[_a14] = true;
|
|
487
|
+
super({ name: name13$1, message });
|
|
488
|
+
this[_a14$1] = true;
|
|
489
489
|
this.functionality = functionality;
|
|
490
490
|
}
|
|
491
491
|
static isInstance(error) {
|
|
492
|
-
return AISDKError.hasMarker(error, marker14);
|
|
492
|
+
return AISDKError$1.hasMarker(error, marker14$1);
|
|
493
493
|
}
|
|
494
494
|
};
|
|
495
|
-
_a14 = symbol14;
|
|
495
|
+
_a14$1 = symbol14$1;
|
|
496
496
|
|
|
497
497
|
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
498
498
|
return (size = defaultSize) => {
|
|
@@ -509,13 +509,13 @@ function getDefaultExportFromCjs (x) {
|
|
|
509
509
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
510
510
|
}
|
|
511
511
|
|
|
512
|
-
var secureJsonParse = {exports: {}};
|
|
512
|
+
var secureJsonParse$1 = {exports: {}};
|
|
513
513
|
|
|
514
|
-
var hasRequiredSecureJsonParse;
|
|
514
|
+
var hasRequiredSecureJsonParse$1;
|
|
515
515
|
|
|
516
|
-
function requireSecureJsonParse () {
|
|
517
|
-
if (hasRequiredSecureJsonParse) return secureJsonParse.exports;
|
|
518
|
-
hasRequiredSecureJsonParse = 1;
|
|
516
|
+
function requireSecureJsonParse$1 () {
|
|
517
|
+
if (hasRequiredSecureJsonParse$1) return secureJsonParse$1.exports;
|
|
518
|
+
hasRequiredSecureJsonParse$1 = 1;
|
|
519
519
|
|
|
520
520
|
const hasBuffer = typeof Buffer !== 'undefined';
|
|
521
521
|
const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
@@ -636,19 +636,19 @@ function requireSecureJsonParse () {
|
|
|
636
636
|
}
|
|
637
637
|
}
|
|
638
638
|
|
|
639
|
-
secureJsonParse.exports = parse;
|
|
640
|
-
secureJsonParse.exports.default = parse;
|
|
641
|
-
secureJsonParse.exports.parse = parse;
|
|
642
|
-
secureJsonParse.exports.safeParse = safeParse;
|
|
643
|
-
secureJsonParse.exports.scan = filter;
|
|
644
|
-
return secureJsonParse.exports;
|
|
639
|
+
secureJsonParse$1.exports = parse;
|
|
640
|
+
secureJsonParse$1.exports.default = parse;
|
|
641
|
+
secureJsonParse$1.exports.parse = parse;
|
|
642
|
+
secureJsonParse$1.exports.safeParse = safeParse;
|
|
643
|
+
secureJsonParse$1.exports.scan = filter;
|
|
644
|
+
return secureJsonParse$1.exports;
|
|
645
645
|
}
|
|
646
646
|
|
|
647
|
-
var secureJsonParseExports = requireSecureJsonParse();
|
|
648
|
-
var SecureJSON = /*@__PURE__*/getDefaultExportFromCjs(secureJsonParseExports);
|
|
647
|
+
var secureJsonParseExports$1 = requireSecureJsonParse$1();
|
|
648
|
+
var SecureJSON$1 = /*@__PURE__*/getDefaultExportFromCjs(secureJsonParseExports$1);
|
|
649
649
|
|
|
650
650
|
// src/combine-headers.ts
|
|
651
|
-
function combineHeaders(...headers) {
|
|
651
|
+
function combineHeaders$1(...headers) {
|
|
652
652
|
return headers.reduce(
|
|
653
653
|
(combinedHeaders, currentHeaders) => ({
|
|
654
654
|
...combinedHeaders,
|
|
@@ -717,7 +717,7 @@ function createEventSourceParserStream() {
|
|
|
717
717
|
}
|
|
718
718
|
return new TransformStream({
|
|
719
719
|
transform(chunk, controller) {
|
|
720
|
-
const { lines, incompleteLine } = splitLines(buffer, chunk);
|
|
720
|
+
const { lines, incompleteLine } = splitLines$1(buffer, chunk);
|
|
721
721
|
buffer = incompleteLine;
|
|
722
722
|
for (let i = 0; i < lines.length; i++) {
|
|
723
723
|
parseLine(lines[i], controller);
|
|
@@ -729,7 +729,7 @@ function createEventSourceParserStream() {
|
|
|
729
729
|
}
|
|
730
730
|
});
|
|
731
731
|
}
|
|
732
|
-
function splitLines(buffer, chunk) {
|
|
732
|
+
function splitLines$1(buffer, chunk) {
|
|
733
733
|
const lines = [];
|
|
734
734
|
let currentLine = buffer;
|
|
735
735
|
for (let i = 0; i < chunk.length; ) {
|
|
@@ -751,14 +751,14 @@ function splitLines(buffer, chunk) {
|
|
|
751
751
|
}
|
|
752
752
|
|
|
753
753
|
// src/extract-response-headers.ts
|
|
754
|
-
function extractResponseHeaders(response) {
|
|
754
|
+
function extractResponseHeaders$1(response) {
|
|
755
755
|
const headers = {};
|
|
756
756
|
response.headers.forEach((value, key) => {
|
|
757
757
|
headers[key] = value;
|
|
758
758
|
});
|
|
759
759
|
return headers;
|
|
760
760
|
}
|
|
761
|
-
var createIdGenerator = ({
|
|
761
|
+
var createIdGenerator$1 = ({
|
|
762
762
|
prefix,
|
|
763
763
|
size: defaultSize = 16,
|
|
764
764
|
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
@@ -769,27 +769,27 @@ var createIdGenerator = ({
|
|
|
769
769
|
return generator;
|
|
770
770
|
}
|
|
771
771
|
if (alphabet.includes(separator)) {
|
|
772
|
-
throw new InvalidArgumentError({
|
|
772
|
+
throw new InvalidArgumentError$1({
|
|
773
773
|
argument: "separator",
|
|
774
774
|
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
775
775
|
});
|
|
776
776
|
}
|
|
777
777
|
return (size) => `${prefix}${separator}${generator(size)}`;
|
|
778
778
|
};
|
|
779
|
-
var generateId = createIdGenerator();
|
|
779
|
+
var generateId$1 = createIdGenerator$1();
|
|
780
780
|
|
|
781
781
|
// src/remove-undefined-entries.ts
|
|
782
|
-
function removeUndefinedEntries(record) {
|
|
782
|
+
function removeUndefinedEntries$1(record) {
|
|
783
783
|
return Object.fromEntries(
|
|
784
784
|
Object.entries(record).filter(([_key, value]) => value != null)
|
|
785
785
|
);
|
|
786
786
|
}
|
|
787
787
|
|
|
788
788
|
// src/is-abort-error.ts
|
|
789
|
-
function isAbortError(error) {
|
|
789
|
+
function isAbortError$1(error) {
|
|
790
790
|
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
|
791
791
|
}
|
|
792
|
-
function loadApiKey({
|
|
792
|
+
function loadApiKey$1({
|
|
793
793
|
apiKey,
|
|
794
794
|
environmentVariableName,
|
|
795
795
|
apiKeyParameterName = "apiKey",
|
|
@@ -799,23 +799,23 @@ function loadApiKey({
|
|
|
799
799
|
return apiKey;
|
|
800
800
|
}
|
|
801
801
|
if (apiKey != null) {
|
|
802
|
-
throw new LoadAPIKeyError({
|
|
802
|
+
throw new LoadAPIKeyError$1({
|
|
803
803
|
message: `${description} API key must be a string.`
|
|
804
804
|
});
|
|
805
805
|
}
|
|
806
806
|
if (typeof process === "undefined") {
|
|
807
|
-
throw new LoadAPIKeyError({
|
|
807
|
+
throw new LoadAPIKeyError$1({
|
|
808
808
|
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
|
809
809
|
});
|
|
810
810
|
}
|
|
811
811
|
apiKey = process.env[environmentVariableName];
|
|
812
812
|
if (apiKey == null) {
|
|
813
|
-
throw new LoadAPIKeyError({
|
|
813
|
+
throw new LoadAPIKeyError$1({
|
|
814
814
|
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
|
815
815
|
});
|
|
816
816
|
}
|
|
817
817
|
if (typeof apiKey !== "string") {
|
|
818
|
-
throw new LoadAPIKeyError({
|
|
818
|
+
throw new LoadAPIKeyError$1({
|
|
819
819
|
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
|
820
820
|
});
|
|
821
821
|
}
|
|
@@ -873,39 +873,39 @@ function loadSetting({
|
|
|
873
873
|
}
|
|
874
874
|
|
|
875
875
|
// src/validator.ts
|
|
876
|
-
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
|
877
|
-
function validator(validate) {
|
|
878
|
-
return { [validatorSymbol]: true, validate };
|
|
876
|
+
var validatorSymbol$1 = Symbol.for("vercel.ai.validator");
|
|
877
|
+
function validator$1(validate) {
|
|
878
|
+
return { [validatorSymbol$1]: true, validate };
|
|
879
879
|
}
|
|
880
|
-
function isValidator(value) {
|
|
881
|
-
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
880
|
+
function isValidator$1(value) {
|
|
881
|
+
return typeof value === "object" && value !== null && validatorSymbol$1 in value && value[validatorSymbol$1] === true && "validate" in value;
|
|
882
882
|
}
|
|
883
|
-
function asValidator(value) {
|
|
884
|
-
return isValidator(value) ? value : zodValidator(value);
|
|
883
|
+
function asValidator$1(value) {
|
|
884
|
+
return isValidator$1(value) ? value : zodValidator$1(value);
|
|
885
885
|
}
|
|
886
|
-
function zodValidator(zodSchema) {
|
|
887
|
-
return validator((value) => {
|
|
886
|
+
function zodValidator$1(zodSchema) {
|
|
887
|
+
return validator$1((value) => {
|
|
888
888
|
const result = zodSchema.safeParse(value);
|
|
889
889
|
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
890
890
|
});
|
|
891
891
|
}
|
|
892
892
|
|
|
893
893
|
// src/validate-types.ts
|
|
894
|
-
function validateTypes({
|
|
894
|
+
function validateTypes$1({
|
|
895
895
|
value,
|
|
896
896
|
schema: inputSchema
|
|
897
897
|
}) {
|
|
898
|
-
const result = safeValidateTypes({ value, schema: inputSchema });
|
|
898
|
+
const result = safeValidateTypes$1({ value, schema: inputSchema });
|
|
899
899
|
if (!result.success) {
|
|
900
|
-
throw TypeValidationError.wrap({ value, cause: result.error });
|
|
900
|
+
throw TypeValidationError$1.wrap({ value, cause: result.error });
|
|
901
901
|
}
|
|
902
902
|
return result.value;
|
|
903
903
|
}
|
|
904
|
-
function safeValidateTypes({
|
|
904
|
+
function safeValidateTypes$1({
|
|
905
905
|
value,
|
|
906
906
|
schema
|
|
907
907
|
}) {
|
|
908
|
-
const validator2 = asValidator(schema);
|
|
908
|
+
const validator2 = asValidator$1(schema);
|
|
909
909
|
try {
|
|
910
910
|
if (validator2.validate == null) {
|
|
911
911
|
return { success: true, value };
|
|
@@ -916,55 +916,55 @@ function safeValidateTypes({
|
|
|
916
916
|
}
|
|
917
917
|
return {
|
|
918
918
|
success: false,
|
|
919
|
-
error: TypeValidationError.wrap({ value, cause: result.error })
|
|
919
|
+
error: TypeValidationError$1.wrap({ value, cause: result.error })
|
|
920
920
|
};
|
|
921
921
|
} catch (error) {
|
|
922
922
|
return {
|
|
923
923
|
success: false,
|
|
924
|
-
error: TypeValidationError.wrap({ value, cause: error })
|
|
924
|
+
error: TypeValidationError$1.wrap({ value, cause: error })
|
|
925
925
|
};
|
|
926
926
|
}
|
|
927
927
|
}
|
|
928
928
|
|
|
929
929
|
// src/parse-json.ts
|
|
930
|
-
function parseJSON({
|
|
930
|
+
function parseJSON$1({
|
|
931
931
|
text,
|
|
932
932
|
schema
|
|
933
933
|
}) {
|
|
934
934
|
try {
|
|
935
|
-
const value = SecureJSON.parse(text);
|
|
935
|
+
const value = SecureJSON$1.parse(text);
|
|
936
936
|
if (schema == null) {
|
|
937
937
|
return value;
|
|
938
938
|
}
|
|
939
|
-
return validateTypes({ value, schema });
|
|
939
|
+
return validateTypes$1({ value, schema });
|
|
940
940
|
} catch (error) {
|
|
941
|
-
if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
|
|
941
|
+
if (JSONParseError$1.isInstance(error) || TypeValidationError$1.isInstance(error)) {
|
|
942
942
|
throw error;
|
|
943
943
|
}
|
|
944
|
-
throw new JSONParseError({ text, cause: error });
|
|
944
|
+
throw new JSONParseError$1({ text, cause: error });
|
|
945
945
|
}
|
|
946
946
|
}
|
|
947
|
-
function safeParseJSON({
|
|
947
|
+
function safeParseJSON$1({
|
|
948
948
|
text,
|
|
949
949
|
schema
|
|
950
950
|
}) {
|
|
951
951
|
try {
|
|
952
|
-
const value = SecureJSON.parse(text);
|
|
952
|
+
const value = SecureJSON$1.parse(text);
|
|
953
953
|
if (schema == null) {
|
|
954
954
|
return { success: true, value, rawValue: value };
|
|
955
955
|
}
|
|
956
|
-
const validationResult = safeValidateTypes({ value, schema });
|
|
956
|
+
const validationResult = safeValidateTypes$1({ value, schema });
|
|
957
957
|
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
|
958
958
|
} catch (error) {
|
|
959
959
|
return {
|
|
960
960
|
success: false,
|
|
961
|
-
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
|
|
961
|
+
error: JSONParseError$1.isInstance(error) ? error : new JSONParseError$1({ text, cause: error })
|
|
962
962
|
};
|
|
963
963
|
}
|
|
964
964
|
}
|
|
965
|
-
function isParsableJson(input) {
|
|
965
|
+
function isParsableJson$1(input) {
|
|
966
966
|
try {
|
|
967
|
-
SecureJSON.parse(input);
|
|
967
|
+
SecureJSON$1.parse(input);
|
|
968
968
|
return true;
|
|
969
969
|
} catch (e) {
|
|
970
970
|
return false;
|
|
@@ -978,12 +978,12 @@ function parseProviderOptions({
|
|
|
978
978
|
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
|
979
979
|
return void 0;
|
|
980
980
|
}
|
|
981
|
-
const parsedProviderOptions = safeValidateTypes({
|
|
981
|
+
const parsedProviderOptions = safeValidateTypes$1({
|
|
982
982
|
value: providerOptions[provider],
|
|
983
983
|
schema
|
|
984
984
|
});
|
|
985
985
|
if (!parsedProviderOptions.success) {
|
|
986
|
-
throw new InvalidArgumentError({
|
|
986
|
+
throw new InvalidArgumentError$1({
|
|
987
987
|
argument: "providerOptions",
|
|
988
988
|
message: `invalid ${provider} provider options`,
|
|
989
989
|
cause: parsedProviderOptions.error
|
|
@@ -991,8 +991,8 @@ function parseProviderOptions({
|
|
|
991
991
|
}
|
|
992
992
|
return parsedProviderOptions.value;
|
|
993
993
|
}
|
|
994
|
-
var getOriginalFetch2 = () => globalThis.fetch;
|
|
995
|
-
var postJsonToApi = async ({
|
|
994
|
+
var getOriginalFetch2$1 = () => globalThis.fetch;
|
|
995
|
+
var postJsonToApi$1 = async ({
|
|
996
996
|
url,
|
|
997
997
|
headers,
|
|
998
998
|
body,
|
|
@@ -1000,7 +1000,7 @@ var postJsonToApi = async ({
|
|
|
1000
1000
|
successfulResponseHandler,
|
|
1001
1001
|
abortSignal,
|
|
1002
1002
|
fetch
|
|
1003
|
-
}) => postToApi({
|
|
1003
|
+
}) => postToApi$1({
|
|
1004
1004
|
url,
|
|
1005
1005
|
headers: {
|
|
1006
1006
|
"Content-Type": "application/json",
|
|
@@ -1023,7 +1023,7 @@ var postFormDataToApi = async ({
|
|
|
1023
1023
|
successfulResponseHandler,
|
|
1024
1024
|
abortSignal,
|
|
1025
1025
|
fetch
|
|
1026
|
-
}) => postToApi({
|
|
1026
|
+
}) => postToApi$1({
|
|
1027
1027
|
url,
|
|
1028
1028
|
headers,
|
|
1029
1029
|
body: {
|
|
@@ -1035,23 +1035,23 @@ var postFormDataToApi = async ({
|
|
|
1035
1035
|
abortSignal,
|
|
1036
1036
|
fetch
|
|
1037
1037
|
});
|
|
1038
|
-
var postToApi = async ({
|
|
1038
|
+
var postToApi$1 = async ({
|
|
1039
1039
|
url,
|
|
1040
1040
|
headers = {},
|
|
1041
1041
|
body,
|
|
1042
1042
|
successfulResponseHandler,
|
|
1043
1043
|
failedResponseHandler,
|
|
1044
1044
|
abortSignal,
|
|
1045
|
-
fetch = getOriginalFetch2()
|
|
1045
|
+
fetch = getOriginalFetch2$1()
|
|
1046
1046
|
}) => {
|
|
1047
1047
|
try {
|
|
1048
1048
|
const response = await fetch(url, {
|
|
1049
1049
|
method: "POST",
|
|
1050
|
-
headers: removeUndefinedEntries(headers),
|
|
1050
|
+
headers: removeUndefinedEntries$1(headers),
|
|
1051
1051
|
body: body.content,
|
|
1052
1052
|
signal: abortSignal
|
|
1053
1053
|
});
|
|
1054
|
-
const responseHeaders = extractResponseHeaders(response);
|
|
1054
|
+
const responseHeaders = extractResponseHeaders$1(response);
|
|
1055
1055
|
if (!response.ok) {
|
|
1056
1056
|
let errorInformation;
|
|
1057
1057
|
try {
|
|
@@ -1061,10 +1061,10 @@ var postToApi = async ({
|
|
|
1061
1061
|
requestBodyValues: body.values
|
|
1062
1062
|
});
|
|
1063
1063
|
} catch (error) {
|
|
1064
|
-
if (isAbortError(error) || APICallError.isInstance(error)) {
|
|
1064
|
+
if (isAbortError$1(error) || APICallError$1.isInstance(error)) {
|
|
1065
1065
|
throw error;
|
|
1066
1066
|
}
|
|
1067
|
-
throw new APICallError({
|
|
1067
|
+
throw new APICallError$1({
|
|
1068
1068
|
message: "Failed to process error response",
|
|
1069
1069
|
cause: error,
|
|
1070
1070
|
statusCode: response.status,
|
|
@@ -1083,11 +1083,11 @@ var postToApi = async ({
|
|
|
1083
1083
|
});
|
|
1084
1084
|
} catch (error) {
|
|
1085
1085
|
if (error instanceof Error) {
|
|
1086
|
-
if (isAbortError(error) || APICallError.isInstance(error)) {
|
|
1086
|
+
if (isAbortError$1(error) || APICallError$1.isInstance(error)) {
|
|
1087
1087
|
throw error;
|
|
1088
1088
|
}
|
|
1089
1089
|
}
|
|
1090
|
-
throw new APICallError({
|
|
1090
|
+
throw new APICallError$1({
|
|
1091
1091
|
message: "Failed to process successful response",
|
|
1092
1092
|
cause: error,
|
|
1093
1093
|
statusCode: response.status,
|
|
@@ -1097,13 +1097,13 @@ var postToApi = async ({
|
|
|
1097
1097
|
});
|
|
1098
1098
|
}
|
|
1099
1099
|
} catch (error) {
|
|
1100
|
-
if (isAbortError(error)) {
|
|
1100
|
+
if (isAbortError$1(error)) {
|
|
1101
1101
|
throw error;
|
|
1102
1102
|
}
|
|
1103
1103
|
if (error instanceof TypeError && error.message === "fetch failed") {
|
|
1104
1104
|
const cause = error.cause;
|
|
1105
1105
|
if (cause != null) {
|
|
1106
|
-
throw new APICallError({
|
|
1106
|
+
throw new APICallError$1({
|
|
1107
1107
|
message: `Cannot connect to API: ${cause.message}`,
|
|
1108
1108
|
cause,
|
|
1109
1109
|
url,
|
|
@@ -1124,17 +1124,17 @@ async function resolve(value) {
|
|
|
1124
1124
|
}
|
|
1125
1125
|
return Promise.resolve(value);
|
|
1126
1126
|
}
|
|
1127
|
-
var createJsonErrorResponseHandler = ({
|
|
1127
|
+
var createJsonErrorResponseHandler$1 = ({
|
|
1128
1128
|
errorSchema,
|
|
1129
1129
|
errorToMessage,
|
|
1130
1130
|
isRetryable
|
|
1131
1131
|
}) => async ({ response, url, requestBodyValues }) => {
|
|
1132
1132
|
const responseBody = await response.text();
|
|
1133
|
-
const responseHeaders = extractResponseHeaders(response);
|
|
1133
|
+
const responseHeaders = extractResponseHeaders$1(response);
|
|
1134
1134
|
if (responseBody.trim() === "") {
|
|
1135
1135
|
return {
|
|
1136
1136
|
responseHeaders,
|
|
1137
|
-
value: new APICallError({
|
|
1137
|
+
value: new APICallError$1({
|
|
1138
1138
|
message: response.statusText,
|
|
1139
1139
|
url,
|
|
1140
1140
|
requestBodyValues,
|
|
@@ -1146,13 +1146,13 @@ var createJsonErrorResponseHandler = ({
|
|
|
1146
1146
|
};
|
|
1147
1147
|
}
|
|
1148
1148
|
try {
|
|
1149
|
-
const parsedError = parseJSON({
|
|
1149
|
+
const parsedError = parseJSON$1({
|
|
1150
1150
|
text: responseBody,
|
|
1151
1151
|
schema: errorSchema
|
|
1152
1152
|
});
|
|
1153
1153
|
return {
|
|
1154
1154
|
responseHeaders,
|
|
1155
|
-
value: new APICallError({
|
|
1155
|
+
value: new APICallError$1({
|
|
1156
1156
|
message: errorToMessage(parsedError),
|
|
1157
1157
|
url,
|
|
1158
1158
|
requestBodyValues,
|
|
@@ -1166,7 +1166,7 @@ var createJsonErrorResponseHandler = ({
|
|
|
1166
1166
|
} catch (parseError) {
|
|
1167
1167
|
return {
|
|
1168
1168
|
responseHeaders,
|
|
1169
|
-
value: new APICallError({
|
|
1169
|
+
value: new APICallError$1({
|
|
1170
1170
|
message: response.statusText,
|
|
1171
1171
|
url,
|
|
1172
1172
|
requestBodyValues,
|
|
@@ -1178,10 +1178,10 @@ var createJsonErrorResponseHandler = ({
|
|
|
1178
1178
|
};
|
|
1179
1179
|
}
|
|
1180
1180
|
};
|
|
1181
|
-
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
1182
|
-
const responseHeaders = extractResponseHeaders(response);
|
|
1181
|
+
var createEventSourceResponseHandler$1 = (chunkSchema) => async ({ response }) => {
|
|
1182
|
+
const responseHeaders = extractResponseHeaders$1(response);
|
|
1183
1183
|
if (response.body == null) {
|
|
1184
|
-
throw new EmptyResponseBodyError({});
|
|
1184
|
+
throw new EmptyResponseBodyError$1({});
|
|
1185
1185
|
}
|
|
1186
1186
|
return {
|
|
1187
1187
|
responseHeaders,
|
|
@@ -1192,7 +1192,7 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
|
|
|
1192
1192
|
return;
|
|
1193
1193
|
}
|
|
1194
1194
|
controller.enqueue(
|
|
1195
|
-
safeParseJSON({
|
|
1195
|
+
safeParseJSON$1({
|
|
1196
1196
|
text: data,
|
|
1197
1197
|
schema: chunkSchema
|
|
1198
1198
|
})
|
|
@@ -1202,15 +1202,15 @@ var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) =>
|
|
|
1202
1202
|
)
|
|
1203
1203
|
};
|
|
1204
1204
|
};
|
|
1205
|
-
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
|
1205
|
+
var createJsonResponseHandler$1 = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
|
1206
1206
|
const responseBody = await response.text();
|
|
1207
|
-
const parsedResult = safeParseJSON({
|
|
1207
|
+
const parsedResult = safeParseJSON$1({
|
|
1208
1208
|
text: responseBody,
|
|
1209
1209
|
schema: responseSchema
|
|
1210
1210
|
});
|
|
1211
|
-
const responseHeaders = extractResponseHeaders(response);
|
|
1211
|
+
const responseHeaders = extractResponseHeaders$1(response);
|
|
1212
1212
|
if (!parsedResult.success) {
|
|
1213
|
-
throw new APICallError({
|
|
1213
|
+
throw new APICallError$1({
|
|
1214
1214
|
message: "Invalid JSON response",
|
|
1215
1215
|
cause: parsedResult.error,
|
|
1216
1216
|
statusCode: response.status,
|
|
@@ -1227,9 +1227,9 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
|
1227
1227
|
};
|
|
1228
1228
|
};
|
|
1229
1229
|
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
|
1230
|
-
const responseHeaders = extractResponseHeaders(response);
|
|
1230
|
+
const responseHeaders = extractResponseHeaders$1(response);
|
|
1231
1231
|
if (!response.body) {
|
|
1232
|
-
throw new APICallError({
|
|
1232
|
+
throw new APICallError$1({
|
|
1233
1233
|
message: "Response body is empty",
|
|
1234
1234
|
url,
|
|
1235
1235
|
requestBodyValues,
|
|
@@ -1245,7 +1245,7 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
|
1245
1245
|
value: new Uint8Array(buffer)
|
|
1246
1246
|
};
|
|
1247
1247
|
} catch (error) {
|
|
1248
|
-
throw new APICallError({
|
|
1248
|
+
throw new APICallError$1({
|
|
1249
1249
|
message: "Failed to read response as array buffer",
|
|
1250
1250
|
url,
|
|
1251
1251
|
requestBodyValues,
|
|
@@ -1258,22 +1258,22 @@ var createBinaryResponseHandler = () => async ({ response, url, requestBodyValue
|
|
|
1258
1258
|
};
|
|
1259
1259
|
|
|
1260
1260
|
// src/uint8-utils.ts
|
|
1261
|
-
var { btoa, atob: atob$1 } = globalThis;
|
|
1261
|
+
var { btoa: btoa$1, atob: atob$1 } = globalThis;
|
|
1262
1262
|
function convertBase64ToUint8Array(base64String) {
|
|
1263
1263
|
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
|
|
1264
1264
|
const latin1string = atob$1(base64Url);
|
|
1265
1265
|
return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
|
|
1266
1266
|
}
|
|
1267
|
-
function convertUint8ArrayToBase64(array) {
|
|
1267
|
+
function convertUint8ArrayToBase64$1(array) {
|
|
1268
1268
|
let latin1string = "";
|
|
1269
1269
|
for (let i = 0; i < array.length; i++) {
|
|
1270
1270
|
latin1string += String.fromCodePoint(array[i]);
|
|
1271
1271
|
}
|
|
1272
|
-
return btoa(latin1string);
|
|
1272
|
+
return btoa$1(latin1string);
|
|
1273
1273
|
}
|
|
1274
1274
|
|
|
1275
1275
|
// src/without-trailing-slash.ts
|
|
1276
|
-
function withoutTrailingSlash(url) {
|
|
1276
|
+
function withoutTrailingSlash$1(url) {
|
|
1277
1277
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
1278
1278
|
}
|
|
1279
1279
|
|
|
@@ -5731,7 +5731,7 @@ function convertToOpenAIChatMessages({
|
|
|
5731
5731
|
return {
|
|
5732
5732
|
type: "image_url",
|
|
5733
5733
|
image_url: {
|
|
5734
|
-
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
|
|
5734
|
+
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64$1(part.image)}`,
|
|
5735
5735
|
// OpenAI specific extension: image detail
|
|
5736
5736
|
detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
5737
5737
|
}
|
|
@@ -5739,7 +5739,7 @@ function convertToOpenAIChatMessages({
|
|
|
5739
5739
|
}
|
|
5740
5740
|
case "file": {
|
|
5741
5741
|
if (part.data instanceof URL) {
|
|
5742
|
-
throw new UnsupportedFunctionalityError({
|
|
5742
|
+
throw new UnsupportedFunctionalityError$1({
|
|
5743
5743
|
functionality: "'File content parts with URL data' functionality not supported."
|
|
5744
5744
|
});
|
|
5745
5745
|
}
|
|
@@ -5767,7 +5767,7 @@ function convertToOpenAIChatMessages({
|
|
|
5767
5767
|
};
|
|
5768
5768
|
}
|
|
5769
5769
|
default: {
|
|
5770
|
-
throw new UnsupportedFunctionalityError({
|
|
5770
|
+
throw new UnsupportedFunctionalityError$1({
|
|
5771
5771
|
functionality: `File content part type ${part.mimeType} in user messages`
|
|
5772
5772
|
});
|
|
5773
5773
|
}
|
|
@@ -5802,7 +5802,7 @@ function convertToOpenAIChatMessages({
|
|
|
5802
5802
|
}
|
|
5803
5803
|
if (useLegacyFunctionCalling) {
|
|
5804
5804
|
if (toolCalls.length > 1) {
|
|
5805
|
-
throw new UnsupportedFunctionalityError({
|
|
5805
|
+
throw new UnsupportedFunctionalityError$1({
|
|
5806
5806
|
functionality: "useLegacyFunctionCalling with multiple tool calls in one message"
|
|
5807
5807
|
});
|
|
5808
5808
|
}
|
|
@@ -5887,7 +5887,7 @@ var openaiErrorDataSchema = z.object({
|
|
|
5887
5887
|
code: z.union([z.string(), z.number()]).nullish()
|
|
5888
5888
|
})
|
|
5889
5889
|
});
|
|
5890
|
-
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
5890
|
+
var openaiFailedResponseHandler = createJsonErrorResponseHandler$1({
|
|
5891
5891
|
errorSchema: openaiErrorDataSchema,
|
|
5892
5892
|
errorToMessage: (data) => data.error.message
|
|
5893
5893
|
});
|
|
@@ -5947,7 +5947,7 @@ function prepareTools$3({
|
|
|
5947
5947
|
toolWarnings
|
|
5948
5948
|
};
|
|
5949
5949
|
case "required":
|
|
5950
|
-
throw new UnsupportedFunctionalityError({
|
|
5950
|
+
throw new UnsupportedFunctionalityError$1({
|
|
5951
5951
|
functionality: "useLegacyFunctionCalling and toolChoice: required"
|
|
5952
5952
|
});
|
|
5953
5953
|
default:
|
|
@@ -5996,7 +5996,7 @@ function prepareTools$3({
|
|
|
5996
5996
|
};
|
|
5997
5997
|
default: {
|
|
5998
5998
|
const _exhaustiveCheck = type;
|
|
5999
|
-
throw new UnsupportedFunctionalityError({
|
|
5999
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6000
6000
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
6001
6001
|
});
|
|
6002
6002
|
}
|
|
@@ -6059,12 +6059,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
6059
6059
|
}
|
|
6060
6060
|
const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
|
|
6061
6061
|
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
|
|
6062
|
-
throw new UnsupportedFunctionalityError({
|
|
6062
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6063
6063
|
functionality: "useLegacyFunctionCalling with parallelToolCalls"
|
|
6064
6064
|
});
|
|
6065
6065
|
}
|
|
6066
6066
|
if (useLegacyFunctionCalling && this.supportsStructuredOutputs) {
|
|
6067
|
-
throw new UnsupportedFunctionalityError({
|
|
6067
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6068
6068
|
functionality: "structuredOutputs with useLegacyFunctionCalling"
|
|
6069
6069
|
});
|
|
6070
6070
|
}
|
|
@@ -6265,15 +6265,15 @@ var OpenAIChatLanguageModel = class {
|
|
|
6265
6265
|
responseHeaders,
|
|
6266
6266
|
value: response,
|
|
6267
6267
|
rawValue: rawResponse
|
|
6268
|
-
} = await postJsonToApi({
|
|
6268
|
+
} = await postJsonToApi$1({
|
|
6269
6269
|
url: this.config.url({
|
|
6270
6270
|
path: "/chat/completions",
|
|
6271
6271
|
modelId: this.modelId
|
|
6272
6272
|
}),
|
|
6273
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
6273
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
6274
6274
|
body,
|
|
6275
6275
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
6276
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
6276
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
6277
6277
|
openaiChatResponseSchema
|
|
6278
6278
|
),
|
|
6279
6279
|
abortSignal: options.abortSignal,
|
|
@@ -6301,7 +6301,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
6301
6301
|
toolCalls: this.settings.useLegacyFunctionCalling && choice.message.function_call ? [
|
|
6302
6302
|
{
|
|
6303
6303
|
toolCallType: "function",
|
|
6304
|
-
toolCallId: generateId(),
|
|
6304
|
+
toolCallId: generateId$1(),
|
|
6305
6305
|
toolName: choice.message.function_call.name,
|
|
6306
6306
|
args: choice.message.function_call.arguments
|
|
6307
6307
|
}
|
|
@@ -6309,7 +6309,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
6309
6309
|
var _a2;
|
|
6310
6310
|
return {
|
|
6311
6311
|
toolCallType: "function",
|
|
6312
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
6312
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId$1(),
|
|
6313
6313
|
toolName: toolCall.function.name,
|
|
6314
6314
|
args: toolCall.function.arguments
|
|
6315
6315
|
};
|
|
@@ -6379,15 +6379,15 @@ var OpenAIChatLanguageModel = class {
|
|
|
6379
6379
|
// only include stream_options when in strict compatibility mode:
|
|
6380
6380
|
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
|
|
6381
6381
|
};
|
|
6382
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
6382
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
6383
6383
|
url: this.config.url({
|
|
6384
6384
|
path: "/chat/completions",
|
|
6385
6385
|
modelId: this.modelId
|
|
6386
6386
|
}),
|
|
6387
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
6387
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
6388
6388
|
body,
|
|
6389
6389
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
6390
|
-
successfulResponseHandler: createEventSourceResponseHandler(
|
|
6390
|
+
successfulResponseHandler: createEventSourceResponseHandler$1(
|
|
6391
6391
|
openaiChatChunkSchema
|
|
6392
6392
|
),
|
|
6393
6393
|
abortSignal: options.abortSignal,
|
|
@@ -6475,7 +6475,7 @@ var OpenAIChatLanguageModel = class {
|
|
|
6475
6475
|
const mappedToolCalls = useLegacyFunctionCalling && delta.function_call != null ? [
|
|
6476
6476
|
{
|
|
6477
6477
|
type: "function",
|
|
6478
|
-
id: generateId(),
|
|
6478
|
+
id: generateId$1(),
|
|
6479
6479
|
function: delta.function_call,
|
|
6480
6480
|
index: 0
|
|
6481
6481
|
}
|
|
@@ -6485,19 +6485,19 @@ var OpenAIChatLanguageModel = class {
|
|
|
6485
6485
|
const index = toolCallDelta.index;
|
|
6486
6486
|
if (toolCalls[index] == null) {
|
|
6487
6487
|
if (toolCallDelta.type !== "function") {
|
|
6488
|
-
throw new InvalidResponseDataError({
|
|
6488
|
+
throw new InvalidResponseDataError$1({
|
|
6489
6489
|
data: toolCallDelta,
|
|
6490
6490
|
message: `Expected 'function' type.`
|
|
6491
6491
|
});
|
|
6492
6492
|
}
|
|
6493
6493
|
if (toolCallDelta.id == null) {
|
|
6494
|
-
throw new InvalidResponseDataError({
|
|
6494
|
+
throw new InvalidResponseDataError$1({
|
|
6495
6495
|
data: toolCallDelta,
|
|
6496
6496
|
message: `Expected 'id' to be a string.`
|
|
6497
6497
|
});
|
|
6498
6498
|
}
|
|
6499
6499
|
if (((_a = toolCallDelta.function) == null ? void 0 : _a.name) == null) {
|
|
6500
|
-
throw new InvalidResponseDataError({
|
|
6500
|
+
throw new InvalidResponseDataError$1({
|
|
6501
6501
|
data: toolCallDelta,
|
|
6502
6502
|
message: `Expected 'function.name' to be a string.`
|
|
6503
6503
|
});
|
|
@@ -6522,11 +6522,11 @@ var OpenAIChatLanguageModel = class {
|
|
|
6522
6522
|
argsTextDelta: toolCall2.function.arguments
|
|
6523
6523
|
});
|
|
6524
6524
|
}
|
|
6525
|
-
if (isParsableJson(toolCall2.function.arguments)) {
|
|
6525
|
+
if (isParsableJson$1(toolCall2.function.arguments)) {
|
|
6526
6526
|
controller.enqueue({
|
|
6527
6527
|
type: "tool-call",
|
|
6528
6528
|
toolCallType: "function",
|
|
6529
|
-
toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
|
|
6529
|
+
toolCallId: (_e = toolCall2.id) != null ? _e : generateId$1(),
|
|
6530
6530
|
toolName: toolCall2.function.name,
|
|
6531
6531
|
args: toolCall2.function.arguments
|
|
6532
6532
|
});
|
|
@@ -6549,11 +6549,11 @@ var OpenAIChatLanguageModel = class {
|
|
|
6549
6549
|
toolName: toolCall.function.name,
|
|
6550
6550
|
argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
|
|
6551
6551
|
});
|
|
6552
|
-
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
6552
|
+
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson$1(toolCall.function.arguments)) {
|
|
6553
6553
|
controller.enqueue({
|
|
6554
6554
|
type: "tool-call",
|
|
6555
6555
|
toolCallType: "function",
|
|
6556
|
-
toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
|
|
6556
|
+
toolCallId: (_l = toolCall.id) != null ? _l : generateId$1(),
|
|
6557
6557
|
toolName: toolCall.function.name,
|
|
6558
6558
|
args: toolCall.function.arguments
|
|
6559
6559
|
});
|
|
@@ -6740,7 +6740,7 @@ function convertToOpenAICompletionPrompt({
|
|
|
6740
6740
|
for (const { role, content } of prompt) {
|
|
6741
6741
|
switch (role) {
|
|
6742
6742
|
case "system": {
|
|
6743
|
-
throw new InvalidPromptError({
|
|
6743
|
+
throw new InvalidPromptError$1({
|
|
6744
6744
|
message: "Unexpected system message in prompt: ${content}",
|
|
6745
6745
|
prompt
|
|
6746
6746
|
});
|
|
@@ -6752,7 +6752,7 @@ function convertToOpenAICompletionPrompt({
|
|
|
6752
6752
|
return part.text;
|
|
6753
6753
|
}
|
|
6754
6754
|
case "image": {
|
|
6755
|
-
throw new UnsupportedFunctionalityError({
|
|
6755
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6756
6756
|
functionality: "images"
|
|
6757
6757
|
});
|
|
6758
6758
|
}
|
|
@@ -6771,7 +6771,7 @@ ${userMessage}
|
|
|
6771
6771
|
return part.text;
|
|
6772
6772
|
}
|
|
6773
6773
|
case "tool-call": {
|
|
6774
|
-
throw new UnsupportedFunctionalityError({
|
|
6774
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6775
6775
|
functionality: "tool-call messages"
|
|
6776
6776
|
});
|
|
6777
6777
|
}
|
|
@@ -6784,7 +6784,7 @@ ${assistantMessage}
|
|
|
6784
6784
|
break;
|
|
6785
6785
|
}
|
|
6786
6786
|
case "tool": {
|
|
6787
|
-
throw new UnsupportedFunctionalityError({
|
|
6787
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6788
6788
|
functionality: "tool messages"
|
|
6789
6789
|
});
|
|
6790
6790
|
}
|
|
@@ -6885,24 +6885,24 @@ var OpenAICompletionLanguageModel = class {
|
|
|
6885
6885
|
switch (type) {
|
|
6886
6886
|
case "regular": {
|
|
6887
6887
|
if ((_a = mode.tools) == null ? void 0 : _a.length) {
|
|
6888
|
-
throw new UnsupportedFunctionalityError({
|
|
6888
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6889
6889
|
functionality: "tools"
|
|
6890
6890
|
});
|
|
6891
6891
|
}
|
|
6892
6892
|
if (mode.toolChoice) {
|
|
6893
|
-
throw new UnsupportedFunctionalityError({
|
|
6893
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6894
6894
|
functionality: "toolChoice"
|
|
6895
6895
|
});
|
|
6896
6896
|
}
|
|
6897
6897
|
return { args: baseArgs, warnings };
|
|
6898
6898
|
}
|
|
6899
6899
|
case "object-json": {
|
|
6900
|
-
throw new UnsupportedFunctionalityError({
|
|
6900
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6901
6901
|
functionality: "object-json mode"
|
|
6902
6902
|
});
|
|
6903
6903
|
}
|
|
6904
6904
|
case "object-tool": {
|
|
6905
|
-
throw new UnsupportedFunctionalityError({
|
|
6905
|
+
throw new UnsupportedFunctionalityError$1({
|
|
6906
6906
|
functionality: "object-tool mode"
|
|
6907
6907
|
});
|
|
6908
6908
|
}
|
|
@@ -6918,15 +6918,15 @@ var OpenAICompletionLanguageModel = class {
|
|
|
6918
6918
|
responseHeaders,
|
|
6919
6919
|
value: response,
|
|
6920
6920
|
rawValue: rawResponse
|
|
6921
|
-
} = await postJsonToApi({
|
|
6921
|
+
} = await postJsonToApi$1({
|
|
6922
6922
|
url: this.config.url({
|
|
6923
6923
|
path: "/completions",
|
|
6924
6924
|
modelId: this.modelId
|
|
6925
6925
|
}),
|
|
6926
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
6926
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
6927
6927
|
body: args,
|
|
6928
6928
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
6929
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
6929
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
6930
6930
|
openaiCompletionResponseSchema
|
|
6931
6931
|
),
|
|
6932
6932
|
abortSignal: options.abortSignal,
|
|
@@ -6957,15 +6957,15 @@ var OpenAICompletionLanguageModel = class {
|
|
|
6957
6957
|
// only include stream_options when in strict compatibility mode:
|
|
6958
6958
|
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
|
|
6959
6959
|
};
|
|
6960
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
6960
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
6961
6961
|
url: this.config.url({
|
|
6962
6962
|
path: "/completions",
|
|
6963
6963
|
modelId: this.modelId
|
|
6964
6964
|
}),
|
|
6965
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
6965
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
6966
6966
|
body,
|
|
6967
6967
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
6968
|
-
successfulResponseHandler: createEventSourceResponseHandler(
|
|
6968
|
+
successfulResponseHandler: createEventSourceResponseHandler$1(
|
|
6969
6969
|
openaiCompletionChunkSchema
|
|
6970
6970
|
),
|
|
6971
6971
|
abortSignal: options.abortSignal,
|
|
@@ -7117,12 +7117,12 @@ var OpenAIEmbeddingModel = class {
|
|
|
7117
7117
|
values
|
|
7118
7118
|
});
|
|
7119
7119
|
}
|
|
7120
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
7120
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
7121
7121
|
url: this.config.url({
|
|
7122
7122
|
path: "/embeddings",
|
|
7123
7123
|
modelId: this.modelId
|
|
7124
7124
|
}),
|
|
7125
|
-
headers: combineHeaders(this.config.headers(), headers),
|
|
7125
|
+
headers: combineHeaders$1(this.config.headers(), headers),
|
|
7126
7126
|
body: {
|
|
7127
7127
|
model: this.modelId,
|
|
7128
7128
|
input: values,
|
|
@@ -7131,7 +7131,7 @@ var OpenAIEmbeddingModel = class {
|
|
|
7131
7131
|
user: this.settings.user
|
|
7132
7132
|
},
|
|
7133
7133
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
7134
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
7134
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
7135
7135
|
openaiTextEmbeddingResponseSchema
|
|
7136
7136
|
),
|
|
7137
7137
|
abortSignal,
|
|
@@ -7195,12 +7195,12 @@ var OpenAIImageModel = class {
|
|
|
7195
7195
|
warnings.push({ type: "unsupported-setting", setting: "seed" });
|
|
7196
7196
|
}
|
|
7197
7197
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
7198
|
-
const { value: response, responseHeaders } = await postJsonToApi({
|
|
7198
|
+
const { value: response, responseHeaders } = await postJsonToApi$1({
|
|
7199
7199
|
url: this.config.url({
|
|
7200
7200
|
path: "/images/generations",
|
|
7201
7201
|
modelId: this.modelId
|
|
7202
7202
|
}),
|
|
7203
|
-
headers: combineHeaders(this.config.headers(), headers),
|
|
7203
|
+
headers: combineHeaders$1(this.config.headers(), headers),
|
|
7204
7204
|
body: {
|
|
7205
7205
|
model: this.modelId,
|
|
7206
7206
|
prompt,
|
|
@@ -7210,7 +7210,7 @@ var OpenAIImageModel = class {
|
|
|
7210
7210
|
...!hasDefaultResponseFormat.has(this.modelId) ? { response_format: "b64_json" } : {}
|
|
7211
7211
|
},
|
|
7212
7212
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
7213
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
7213
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
7214
7214
|
openaiImageResponseSchema
|
|
7215
7215
|
),
|
|
7216
7216
|
abortSignal,
|
|
@@ -7354,10 +7354,10 @@ var OpenAITranscriptionModel = class {
|
|
|
7354
7354
|
path: "/audio/transcriptions",
|
|
7355
7355
|
modelId: this.modelId
|
|
7356
7356
|
}),
|
|
7357
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
7357
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
7358
7358
|
formData,
|
|
7359
7359
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
7360
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
7360
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
7361
7361
|
openaiTranscriptionResponseSchema
|
|
7362
7362
|
),
|
|
7363
7363
|
abortSignal: options.abortSignal,
|
|
@@ -7441,14 +7441,14 @@ function convertToOpenAIResponsesMessages({
|
|
|
7441
7441
|
case "image": {
|
|
7442
7442
|
return {
|
|
7443
7443
|
type: "input_image",
|
|
7444
|
-
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
|
|
7444
|
+
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64$1(part.image)}`,
|
|
7445
7445
|
// OpenAI specific extension: image detail
|
|
7446
7446
|
detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
7447
7447
|
};
|
|
7448
7448
|
}
|
|
7449
7449
|
case "file": {
|
|
7450
7450
|
if (part.data instanceof URL) {
|
|
7451
|
-
throw new UnsupportedFunctionalityError({
|
|
7451
|
+
throw new UnsupportedFunctionalityError$1({
|
|
7452
7452
|
functionality: "File URLs in user messages"
|
|
7453
7453
|
});
|
|
7454
7454
|
}
|
|
@@ -7461,7 +7461,7 @@ function convertToOpenAIResponsesMessages({
|
|
|
7461
7461
|
};
|
|
7462
7462
|
}
|
|
7463
7463
|
default: {
|
|
7464
|
-
throw new UnsupportedFunctionalityError({
|
|
7464
|
+
throw new UnsupportedFunctionalityError$1({
|
|
7465
7465
|
functionality: "Only PDF files are supported in user messages"
|
|
7466
7466
|
});
|
|
7467
7467
|
}
|
|
@@ -7603,7 +7603,7 @@ function prepareResponsesTools({
|
|
|
7603
7603
|
}
|
|
7604
7604
|
default: {
|
|
7605
7605
|
const _exhaustiveCheck = type;
|
|
7606
|
-
throw new UnsupportedFunctionalityError({
|
|
7606
|
+
throw new UnsupportedFunctionalityError$1({
|
|
7607
7607
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
7608
7608
|
});
|
|
7609
7609
|
}
|
|
@@ -7802,15 +7802,15 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
7802
7802
|
responseHeaders,
|
|
7803
7803
|
value: response,
|
|
7804
7804
|
rawValue: rawResponse
|
|
7805
|
-
} = await postJsonToApi({
|
|
7805
|
+
} = await postJsonToApi$1({
|
|
7806
7806
|
url: this.config.url({
|
|
7807
7807
|
path: "/responses",
|
|
7808
7808
|
modelId: this.modelId
|
|
7809
7809
|
}),
|
|
7810
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
7810
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
7811
7811
|
body,
|
|
7812
7812
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
7813
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
7813
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
7814
7814
|
z.object({
|
|
7815
7815
|
id: z.string(),
|
|
7816
7816
|
created_at: z.number(),
|
|
@@ -7881,7 +7881,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
7881
7881
|
var _a2, _b2, _c2;
|
|
7882
7882
|
return {
|
|
7883
7883
|
sourceType: "url",
|
|
7884
|
-
id: (_c2 = (_b2 = (_a2 = this.config).generateId) == null ? void 0 : _b2.call(_a2)) != null ? _c2 : generateId(),
|
|
7884
|
+
id: (_c2 = (_b2 = (_a2 = this.config).generateId) == null ? void 0 : _b2.call(_a2)) != null ? _c2 : generateId$1(),
|
|
7885
7885
|
url: annotation.url,
|
|
7886
7886
|
title: annotation.title
|
|
7887
7887
|
};
|
|
@@ -7928,18 +7928,18 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
7928
7928
|
}
|
|
7929
7929
|
async doStream(options) {
|
|
7930
7930
|
const { args: body, warnings } = this.getArgs(options);
|
|
7931
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
7931
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
7932
7932
|
url: this.config.url({
|
|
7933
7933
|
path: "/responses",
|
|
7934
7934
|
modelId: this.modelId
|
|
7935
7935
|
}),
|
|
7936
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
7936
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
7937
7937
|
body: {
|
|
7938
7938
|
...body,
|
|
7939
7939
|
stream: true
|
|
7940
7940
|
},
|
|
7941
7941
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
7942
|
-
successfulResponseHandler: createEventSourceResponseHandler(
|
|
7942
|
+
successfulResponseHandler: createEventSourceResponseHandler$1(
|
|
7943
7943
|
openaiResponsesChunkSchema
|
|
7944
7944
|
),
|
|
7945
7945
|
abortSignal: options.abortSignal,
|
|
@@ -8032,7 +8032,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
8032
8032
|
type: "source",
|
|
8033
8033
|
source: {
|
|
8034
8034
|
sourceType: "url",
|
|
8035
|
-
id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId(),
|
|
8035
|
+
id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId$1(),
|
|
8036
8036
|
url: value.annotation.url,
|
|
8037
8037
|
title: value.annotation.title
|
|
8038
8038
|
}
|
|
@@ -8300,12 +8300,12 @@ var OpenAISpeechModel = class {
|
|
|
8300
8300
|
value: audio,
|
|
8301
8301
|
responseHeaders,
|
|
8302
8302
|
rawValue: rawResponse
|
|
8303
|
-
} = await postJsonToApi({
|
|
8303
|
+
} = await postJsonToApi$1({
|
|
8304
8304
|
url: this.config.url({
|
|
8305
8305
|
path: "/audio/speech",
|
|
8306
8306
|
modelId: this.modelId
|
|
8307
8307
|
}),
|
|
8308
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
8308
|
+
headers: combineHeaders$1(this.config.headers(), options.headers),
|
|
8309
8309
|
body: requestBody,
|
|
8310
8310
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
8311
8311
|
successfulResponseHandler: createBinaryResponseHandler(),
|
|
@@ -8331,11 +8331,11 @@ var OpenAISpeechModel = class {
|
|
|
8331
8331
|
// src/openai-provider.ts
|
|
8332
8332
|
function createOpenAI(options = {}) {
|
|
8333
8333
|
var _a, _b, _c;
|
|
8334
|
-
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.openai.com/v1";
|
|
8334
|
+
const baseURL = (_a = withoutTrailingSlash$1(options.baseURL)) != null ? _a : "https://api.openai.com/v1";
|
|
8335
8335
|
const compatibility = (_b = options.compatibility) != null ? _b : "compatible";
|
|
8336
8336
|
const providerName = (_c = options.name) != null ? _c : "openai";
|
|
8337
8337
|
const getHeaders = () => ({
|
|
8338
|
-
Authorization: `Bearer ${loadApiKey({
|
|
8338
|
+
Authorization: `Bearer ${loadApiKey$1({
|
|
8339
8339
|
apiKey: options.apiKey,
|
|
8340
8340
|
environmentVariableName: "OPENAI_API_KEY",
|
|
8341
8341
|
description: "OpenAI"
|
|
@@ -8663,7 +8663,7 @@ var anthropicErrorDataSchema = z.object({
|
|
|
8663
8663
|
message: z.string()
|
|
8664
8664
|
})
|
|
8665
8665
|
});
|
|
8666
|
-
var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
8666
|
+
var anthropicFailedResponseHandler = createJsonErrorResponseHandler$1({
|
|
8667
8667
|
errorSchema: anthropicErrorDataSchema,
|
|
8668
8668
|
errorToMessage: (data) => data.error.message
|
|
8669
8669
|
});
|
|
@@ -8781,7 +8781,7 @@ function prepareTools$2(mode) {
|
|
|
8781
8781
|
};
|
|
8782
8782
|
default: {
|
|
8783
8783
|
const _exhaustiveCheck = type;
|
|
8784
|
-
throw new UnsupportedFunctionalityError({
|
|
8784
|
+
throw new UnsupportedFunctionalityError$1({
|
|
8785
8785
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
8786
8786
|
});
|
|
8787
8787
|
}
|
|
@@ -8810,7 +8810,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
8810
8810
|
switch (type) {
|
|
8811
8811
|
case "system": {
|
|
8812
8812
|
if (system != null) {
|
|
8813
|
-
throw new UnsupportedFunctionalityError({
|
|
8813
|
+
throw new UnsupportedFunctionalityError$1({
|
|
8814
8814
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
8815
8815
|
});
|
|
8816
8816
|
}
|
|
@@ -8849,7 +8849,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
8849
8849
|
} : {
|
|
8850
8850
|
type: "base64",
|
|
8851
8851
|
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
8852
|
-
data: convertUint8ArrayToBase64(part.image)
|
|
8852
|
+
data: convertUint8ArrayToBase64$1(part.image)
|
|
8853
8853
|
},
|
|
8854
8854
|
cache_control: cacheControl
|
|
8855
8855
|
});
|
|
@@ -8857,7 +8857,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
8857
8857
|
}
|
|
8858
8858
|
case "file": {
|
|
8859
8859
|
if (part.mimeType !== "application/pdf") {
|
|
8860
|
-
throw new UnsupportedFunctionalityError({
|
|
8860
|
+
throw new UnsupportedFunctionalityError$1({
|
|
8861
8861
|
functionality: "Non-PDF files in user messages"
|
|
8862
8862
|
});
|
|
8863
8863
|
}
|
|
@@ -9154,7 +9154,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
9154
9154
|
};
|
|
9155
9155
|
if (isThinking) {
|
|
9156
9156
|
if (thinkingBudget == null) {
|
|
9157
|
-
throw new UnsupportedFunctionalityError({
|
|
9157
|
+
throw new UnsupportedFunctionalityError$1({
|
|
9158
9158
|
functionality: "thinking requires a budget"
|
|
9159
9159
|
});
|
|
9160
9160
|
}
|
|
@@ -9199,7 +9199,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
9199
9199
|
};
|
|
9200
9200
|
}
|
|
9201
9201
|
case "object-json": {
|
|
9202
|
-
throw new UnsupportedFunctionalityError({
|
|
9202
|
+
throw new UnsupportedFunctionalityError$1({
|
|
9203
9203
|
functionality: "json-mode object generation"
|
|
9204
9204
|
});
|
|
9205
9205
|
}
|
|
@@ -9225,7 +9225,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
9225
9225
|
betas,
|
|
9226
9226
|
headers
|
|
9227
9227
|
}) {
|
|
9228
|
-
return combineHeaders(
|
|
9228
|
+
return combineHeaders$1(
|
|
9229
9229
|
await resolve(this.config.headers),
|
|
9230
9230
|
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
9231
9231
|
headers
|
|
@@ -9246,12 +9246,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
9246
9246
|
responseHeaders,
|
|
9247
9247
|
value: response,
|
|
9248
9248
|
rawValue: rawResponse
|
|
9249
|
-
} = await postJsonToApi({
|
|
9249
|
+
} = await postJsonToApi$1({
|
|
9250
9250
|
url: this.buildRequestUrl(false),
|
|
9251
9251
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
9252
9252
|
body: this.transformRequestBody(args),
|
|
9253
9253
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
9254
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
9254
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
9255
9255
|
anthropicMessagesResponseSchema
|
|
9256
9256
|
),
|
|
9257
9257
|
abortSignal: options.abortSignal,
|
|
@@ -9321,12 +9321,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
9321
9321
|
async doStream(options) {
|
|
9322
9322
|
const { args, warnings, betas } = await this.getArgs(options);
|
|
9323
9323
|
const body = { ...args, stream: true };
|
|
9324
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
9324
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
9325
9325
|
url: this.buildRequestUrl(true),
|
|
9326
9326
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
9327
9327
|
body: this.transformRequestBody(body),
|
|
9328
9328
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
9329
|
-
successfulResponseHandler: createEventSourceResponseHandler(
|
|
9329
|
+
successfulResponseHandler: createEventSourceResponseHandler$1(
|
|
9330
9330
|
anthropicMessagesChunkSchema
|
|
9331
9331
|
),
|
|
9332
9332
|
abortSignal: options.abortSignal,
|
|
@@ -9767,10 +9767,10 @@ var anthropicTools = {
|
|
|
9767
9767
|
// src/anthropic-provider.ts
|
|
9768
9768
|
function createAnthropic(options = {}) {
|
|
9769
9769
|
var _a;
|
|
9770
|
-
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.anthropic.com/v1";
|
|
9770
|
+
const baseURL = (_a = withoutTrailingSlash$1(options.baseURL)) != null ? _a : "https://api.anthropic.com/v1";
|
|
9771
9771
|
const getHeaders = () => ({
|
|
9772
9772
|
"anthropic-version": "2023-06-01",
|
|
9773
|
-
"x-api-key": loadApiKey({
|
|
9773
|
+
"x-api-key": loadApiKey$1({
|
|
9774
9774
|
apiKey: options.apiKey,
|
|
9775
9775
|
environmentVariableName: "ANTHROPIC_API_KEY",
|
|
9776
9776
|
description: "Anthropic"
|
|
@@ -9910,7 +9910,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
|
9910
9910
|
switch (role) {
|
|
9911
9911
|
case "system": {
|
|
9912
9912
|
if (!systemMessagesAllowed) {
|
|
9913
|
-
throw new UnsupportedFunctionalityError({
|
|
9913
|
+
throw new UnsupportedFunctionalityError$1({
|
|
9914
9914
|
functionality: "system messages are only supported at the beginning of the conversation"
|
|
9915
9915
|
});
|
|
9916
9916
|
}
|
|
@@ -9936,7 +9936,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
|
9936
9936
|
} : {
|
|
9937
9937
|
inlineData: {
|
|
9938
9938
|
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
9939
|
-
data: convertUint8ArrayToBase64(part.image)
|
|
9939
|
+
data: convertUint8ArrayToBase64$1(part.image)
|
|
9940
9940
|
}
|
|
9941
9941
|
}
|
|
9942
9942
|
);
|
|
@@ -9974,12 +9974,12 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
|
9974
9974
|
}
|
|
9975
9975
|
case "file": {
|
|
9976
9976
|
if (part.mimeType !== "image/png") {
|
|
9977
|
-
throw new UnsupportedFunctionalityError({
|
|
9977
|
+
throw new UnsupportedFunctionalityError$1({
|
|
9978
9978
|
functionality: "Only PNG images are supported in assistant messages"
|
|
9979
9979
|
});
|
|
9980
9980
|
}
|
|
9981
9981
|
if (part.data instanceof URL) {
|
|
9982
|
-
throw new UnsupportedFunctionalityError({
|
|
9982
|
+
throw new UnsupportedFunctionalityError$1({
|
|
9983
9983
|
functionality: "File data URLs in assistant messages are not supported"
|
|
9984
9984
|
});
|
|
9985
9985
|
}
|
|
@@ -10038,7 +10038,7 @@ var googleErrorDataSchema = z.object({
|
|
|
10038
10038
|
status: z.string()
|
|
10039
10039
|
})
|
|
10040
10040
|
});
|
|
10041
|
-
var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
10041
|
+
var googleFailedResponseHandler = createJsonErrorResponseHandler$1({
|
|
10042
10042
|
errorSchema: googleErrorDataSchema,
|
|
10043
10043
|
errorToMessage: (data) => data.error.message
|
|
10044
10044
|
});
|
|
@@ -10113,7 +10113,7 @@ function prepareTools$1(mode, useSearchGrounding, dynamicRetrievalConfig, modelI
|
|
|
10113
10113
|
};
|
|
10114
10114
|
default: {
|
|
10115
10115
|
const _exhaustiveCheck = type;
|
|
10116
|
-
throw new UnsupportedFunctionalityError({
|
|
10116
|
+
throw new UnsupportedFunctionalityError$1({
|
|
10117
10117
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
10118
10118
|
});
|
|
10119
10119
|
}
|
|
@@ -10284,7 +10284,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
10284
10284
|
var _a, _b, _c, _d, _e;
|
|
10285
10285
|
const { args, warnings } = await this.getArgs(options);
|
|
10286
10286
|
const body = JSON.stringify(args);
|
|
10287
|
-
const mergedHeaders = combineHeaders(
|
|
10287
|
+
const mergedHeaders = combineHeaders$1(
|
|
10288
10288
|
await resolve(this.config.headers),
|
|
10289
10289
|
options.headers
|
|
10290
10290
|
);
|
|
@@ -10292,14 +10292,14 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
10292
10292
|
responseHeaders,
|
|
10293
10293
|
value: response,
|
|
10294
10294
|
rawValue: rawResponse
|
|
10295
|
-
} = await postJsonToApi({
|
|
10295
|
+
} = await postJsonToApi$1({
|
|
10296
10296
|
url: `${this.config.baseURL}/${getModelPath(
|
|
10297
10297
|
this.modelId
|
|
10298
10298
|
)}:generateContent`,
|
|
10299
10299
|
headers: mergedHeaders,
|
|
10300
10300
|
body: args,
|
|
10301
10301
|
failedResponseHandler: googleFailedResponseHandler,
|
|
10302
|
-
successfulResponseHandler: createJsonResponseHandler(responseSchema),
|
|
10302
|
+
successfulResponseHandler: createJsonResponseHandler$1(responseSchema),
|
|
10303
10303
|
abortSignal: options.abortSignal,
|
|
10304
10304
|
fetch: this.config.fetch
|
|
10305
10305
|
});
|
|
@@ -10345,18 +10345,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
10345
10345
|
async doStream(options) {
|
|
10346
10346
|
const { args, warnings } = await this.getArgs(options);
|
|
10347
10347
|
const body = JSON.stringify(args);
|
|
10348
|
-
const headers = combineHeaders(
|
|
10348
|
+
const headers = combineHeaders$1(
|
|
10349
10349
|
await resolve(this.config.headers),
|
|
10350
10350
|
options.headers
|
|
10351
10351
|
);
|
|
10352
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
10352
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
10353
10353
|
url: `${this.config.baseURL}/${getModelPath(
|
|
10354
10354
|
this.modelId
|
|
10355
10355
|
)}:streamGenerateContent?alt=sse`,
|
|
10356
10356
|
headers,
|
|
10357
10357
|
body: args,
|
|
10358
10358
|
failedResponseHandler: googleFailedResponseHandler,
|
|
10359
|
-
successfulResponseHandler: createEventSourceResponseHandler(chunkSchema),
|
|
10359
|
+
successfulResponseHandler: createEventSourceResponseHandler$1(chunkSchema),
|
|
10360
10360
|
abortSignal: options.abortSignal,
|
|
10361
10361
|
fetch: this.config.fetch
|
|
10362
10362
|
});
|
|
@@ -10632,11 +10632,11 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
|
10632
10632
|
values
|
|
10633
10633
|
});
|
|
10634
10634
|
}
|
|
10635
|
-
const mergedHeaders = combineHeaders(
|
|
10635
|
+
const mergedHeaders = combineHeaders$1(
|
|
10636
10636
|
await resolve(this.config.headers),
|
|
10637
10637
|
headers
|
|
10638
10638
|
);
|
|
10639
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
10639
|
+
const { responseHeaders, value: response } = await postJsonToApi$1({
|
|
10640
10640
|
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
|
10641
10641
|
headers: mergedHeaders,
|
|
10642
10642
|
body: {
|
|
@@ -10648,7 +10648,7 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
|
10648
10648
|
}))
|
|
10649
10649
|
},
|
|
10650
10650
|
failedResponseHandler: googleFailedResponseHandler,
|
|
10651
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
10651
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
10652
10652
|
googleGenerativeAITextEmbeddingResponseSchema
|
|
10653
10653
|
),
|
|
10654
10654
|
abortSignal,
|
|
@@ -10673,9 +10673,9 @@ function isSupportedFileUrl(url) {
|
|
|
10673
10673
|
// src/google-provider.ts
|
|
10674
10674
|
function createGoogleGenerativeAI(options = {}) {
|
|
10675
10675
|
var _a;
|
|
10676
|
-
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://generativelanguage.googleapis.com/v1beta";
|
|
10676
|
+
const baseURL = (_a = withoutTrailingSlash$1(options.baseURL)) != null ? _a : "https://generativelanguage.googleapis.com/v1beta";
|
|
10677
10677
|
const getHeaders = () => ({
|
|
10678
|
-
"x-goog-api-key": loadApiKey({
|
|
10678
|
+
"x-goog-api-key": loadApiKey$1({
|
|
10679
10679
|
apiKey: options.apiKey,
|
|
10680
10680
|
environmentVariableName: "GOOGLE_GENERATIVE_AI_API_KEY",
|
|
10681
10681
|
description: "Google Generative AI"
|
|
@@ -10688,7 +10688,7 @@ function createGoogleGenerativeAI(options = {}) {
|
|
|
10688
10688
|
provider: "google.generative-ai",
|
|
10689
10689
|
baseURL,
|
|
10690
10690
|
headers: getHeaders,
|
|
10691
|
-
generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
|
|
10691
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId$1,
|
|
10692
10692
|
isSupportedUrl: isSupportedFileUrl,
|
|
10693
10693
|
fetch: options.fetch
|
|
10694
10694
|
});
|
|
@@ -11526,9 +11526,9 @@ var BedrockErrorSchema = z.object({
|
|
|
11526
11526
|
type: z.string().nullish()
|
|
11527
11527
|
});
|
|
11528
11528
|
var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
11529
|
-
const responseHeaders = extractResponseHeaders(response);
|
|
11529
|
+
const responseHeaders = extractResponseHeaders$1(response);
|
|
11530
11530
|
if (response.body == null) {
|
|
11531
|
-
throw new EmptyResponseBodyError({});
|
|
11531
|
+
throw new EmptyResponseBodyError$1({});
|
|
11532
11532
|
}
|
|
11533
11533
|
const codec = new EventStreamCodec(toUtf8, fromUtf8);
|
|
11534
11534
|
let buffer = new Uint8Array(0);
|
|
@@ -11558,7 +11558,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
11558
11558
|
buffer = buffer.slice(totalLength);
|
|
11559
11559
|
if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
|
|
11560
11560
|
const data = textDecoder.decode(decoded.body);
|
|
11561
|
-
const parsedDataResult = safeParseJSON({ text: data });
|
|
11561
|
+
const parsedDataResult = safeParseJSON$1({ text: data });
|
|
11562
11562
|
if (!parsedDataResult.success) {
|
|
11563
11563
|
controller.enqueue(parsedDataResult);
|
|
11564
11564
|
break;
|
|
@@ -11567,7 +11567,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
11567
11567
|
let wrappedData = {
|
|
11568
11568
|
[(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
|
|
11569
11569
|
};
|
|
11570
|
-
const validatedWrappedData = safeValidateTypes({
|
|
11570
|
+
const validatedWrappedData = safeValidateTypes$1({
|
|
11571
11571
|
value: wrappedData,
|
|
11572
11572
|
schema: chunkSchema
|
|
11573
11573
|
});
|
|
@@ -11650,13 +11650,13 @@ function prepareTools(mode) {
|
|
|
11650
11650
|
};
|
|
11651
11651
|
default: {
|
|
11652
11652
|
const _exhaustiveCheck = type;
|
|
11653
|
-
throw new UnsupportedFunctionalityError({
|
|
11653
|
+
throw new UnsupportedFunctionalityError$1({
|
|
11654
11654
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
11655
11655
|
});
|
|
11656
11656
|
}
|
|
11657
11657
|
}
|
|
11658
11658
|
}
|
|
11659
|
-
var generateFileId = createIdGenerator({ prefix: "file", size: 16 });
|
|
11659
|
+
var generateFileId = createIdGenerator$1({ prefix: "file", size: 16 });
|
|
11660
11660
|
function getCachePoint(providerMetadata) {
|
|
11661
11661
|
var _a;
|
|
11662
11662
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
@@ -11673,7 +11673,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
11673
11673
|
switch (type) {
|
|
11674
11674
|
case "system": {
|
|
11675
11675
|
if (messages.length > 0) {
|
|
11676
|
-
throw new UnsupportedFunctionalityError({
|
|
11676
|
+
throw new UnsupportedFunctionalityError$1({
|
|
11677
11677
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
11678
11678
|
});
|
|
11679
11679
|
}
|
|
@@ -11702,7 +11702,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
11702
11702
|
}
|
|
11703
11703
|
case "image": {
|
|
11704
11704
|
if (part.image instanceof URL) {
|
|
11705
|
-
throw new UnsupportedFunctionalityError({
|
|
11705
|
+
throw new UnsupportedFunctionalityError$1({
|
|
11706
11706
|
functionality: "Image URLs in user messages"
|
|
11707
11707
|
});
|
|
11708
11708
|
}
|
|
@@ -11712,7 +11712,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
11712
11712
|
"/"
|
|
11713
11713
|
)) == null ? void 0 : _b[1],
|
|
11714
11714
|
source: {
|
|
11715
|
-
bytes: convertUint8ArrayToBase64(
|
|
11715
|
+
bytes: convertUint8ArrayToBase64$1(
|
|
11716
11716
|
(_c = part.image) != null ? _c : part.image
|
|
11717
11717
|
)
|
|
11718
11718
|
}
|
|
@@ -11722,7 +11722,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
11722
11722
|
}
|
|
11723
11723
|
case "file": {
|
|
11724
11724
|
if (part.data instanceof URL) {
|
|
11725
|
-
throw new UnsupportedFunctionalityError({
|
|
11725
|
+
throw new UnsupportedFunctionalityError$1({
|
|
11726
11726
|
functionality: "File URLs in user messages"
|
|
11727
11727
|
});
|
|
11728
11728
|
}
|
|
@@ -12012,7 +12012,7 @@ var BedrockChatLanguageModel = class {
|
|
|
12012
12012
|
(_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.reasoning_config
|
|
12013
12013
|
);
|
|
12014
12014
|
if (!reasoningConfigOptions.success) {
|
|
12015
|
-
throw new InvalidArgumentError({
|
|
12015
|
+
throw new InvalidArgumentError$1({
|
|
12016
12016
|
argument: "providerOptions.bedrock.reasoning_config",
|
|
12017
12017
|
message: "invalid reasoning configuration options",
|
|
12018
12018
|
cause: reasoningConfigOptions.error
|
|
@@ -12077,7 +12077,7 @@ var BedrockChatLanguageModel = class {
|
|
|
12077
12077
|
};
|
|
12078
12078
|
}
|
|
12079
12079
|
case "object-json": {
|
|
12080
|
-
throw new UnsupportedFunctionalityError({
|
|
12080
|
+
throw new UnsupportedFunctionalityError$1({
|
|
12081
12081
|
functionality: "json-mode object generation"
|
|
12082
12082
|
});
|
|
12083
12083
|
}
|
|
@@ -12113,21 +12113,21 @@ var BedrockChatLanguageModel = class {
|
|
|
12113
12113
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
12114
12114
|
const { command: args, warnings } = this.getArgs(options);
|
|
12115
12115
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
12116
|
-
const { value: response, responseHeaders } = await postJsonToApi({
|
|
12116
|
+
const { value: response, responseHeaders } = await postJsonToApi$1({
|
|
12117
12117
|
url,
|
|
12118
|
-
headers: combineHeaders(
|
|
12118
|
+
headers: combineHeaders$1(
|
|
12119
12119
|
await resolve(this.config.headers),
|
|
12120
12120
|
options.headers
|
|
12121
12121
|
),
|
|
12122
12122
|
body: args,
|
|
12123
|
-
failedResponseHandler: createJsonErrorResponseHandler({
|
|
12123
|
+
failedResponseHandler: createJsonErrorResponseHandler$1({
|
|
12124
12124
|
errorSchema: BedrockErrorSchema,
|
|
12125
12125
|
errorToMessage: (error) => {
|
|
12126
12126
|
var _a2;
|
|
12127
12127
|
return `${(_a2 = error.message) != null ? _a2 : "Unknown error"}`;
|
|
12128
12128
|
}
|
|
12129
12129
|
}),
|
|
12130
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
12130
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
12131
12131
|
BedrockResponseSchema
|
|
12132
12132
|
),
|
|
12133
12133
|
abortSignal: options.abortSignal,
|
|
@@ -12195,14 +12195,14 @@ var BedrockChatLanguageModel = class {
|
|
|
12195
12195
|
async doStream(options) {
|
|
12196
12196
|
const { command: args, warnings } = this.getArgs(options);
|
|
12197
12197
|
const url = `${this.getUrl(this.modelId)}/converse-stream`;
|
|
12198
|
-
const { value: response, responseHeaders } = await postJsonToApi({
|
|
12198
|
+
const { value: response, responseHeaders } = await postJsonToApi$1({
|
|
12199
12199
|
url,
|
|
12200
|
-
headers: combineHeaders(
|
|
12200
|
+
headers: combineHeaders$1(
|
|
12201
12201
|
await resolve(this.config.headers),
|
|
12202
12202
|
options.headers
|
|
12203
12203
|
),
|
|
12204
12204
|
body: args,
|
|
12205
|
-
failedResponseHandler: createJsonErrorResponseHandler({
|
|
12205
|
+
failedResponseHandler: createJsonErrorResponseHandler$1({
|
|
12206
12206
|
errorSchema: BedrockErrorSchema,
|
|
12207
12207
|
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
12208
12208
|
}),
|
|
@@ -12485,17 +12485,17 @@ var BedrockEmbeddingModel = class {
|
|
|
12485
12485
|
normalize: this.settings.normalize
|
|
12486
12486
|
};
|
|
12487
12487
|
const url = this.getUrl(this.modelId);
|
|
12488
|
-
const { value: response } = await postJsonToApi({
|
|
12488
|
+
const { value: response } = await postJsonToApi$1({
|
|
12489
12489
|
url,
|
|
12490
12490
|
headers: await resolve(
|
|
12491
|
-
combineHeaders(await resolve(this.config.headers), headers)
|
|
12491
|
+
combineHeaders$1(await resolve(this.config.headers), headers)
|
|
12492
12492
|
),
|
|
12493
12493
|
body: args,
|
|
12494
|
-
failedResponseHandler: createJsonErrorResponseHandler({
|
|
12494
|
+
failedResponseHandler: createJsonErrorResponseHandler$1({
|
|
12495
12495
|
errorSchema: BedrockErrorSchema,
|
|
12496
12496
|
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
12497
12497
|
}),
|
|
12498
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
12498
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
12499
12499
|
BedrockEmbeddingResponseSchema
|
|
12500
12500
|
),
|
|
12501
12501
|
fetch: this.config.fetch,
|
|
@@ -12580,17 +12580,17 @@ var BedrockImageModel = class {
|
|
|
12580
12580
|
});
|
|
12581
12581
|
}
|
|
12582
12582
|
const currentDate = (_f = (_e = (_d = this.config._internal) == null ? void 0 : _d.currentDate) == null ? void 0 : _e.call(_d)) != null ? _f : /* @__PURE__ */ new Date();
|
|
12583
|
-
const { value: response, responseHeaders } = await postJsonToApi({
|
|
12583
|
+
const { value: response, responseHeaders } = await postJsonToApi$1({
|
|
12584
12584
|
url: this.getUrl(this.modelId),
|
|
12585
12585
|
headers: await resolve(
|
|
12586
|
-
combineHeaders(await resolve(this.config.headers), headers)
|
|
12586
|
+
combineHeaders$1(await resolve(this.config.headers), headers)
|
|
12587
12587
|
),
|
|
12588
12588
|
body: args,
|
|
12589
|
-
failedResponseHandler: createJsonErrorResponseHandler({
|
|
12589
|
+
failedResponseHandler: createJsonErrorResponseHandler$1({
|
|
12590
12590
|
errorSchema: BedrockErrorSchema,
|
|
12591
12591
|
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
12592
12592
|
}),
|
|
12593
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
12593
|
+
successfulResponseHandler: createJsonResponseHandler$1(
|
|
12594
12594
|
bedrockImageResponseSchema
|
|
12595
12595
|
),
|
|
12596
12596
|
abortSignal,
|
|
@@ -12649,7 +12649,7 @@ function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
|
12649
12649
|
const signer = new AwsV4Signer({
|
|
12650
12650
|
url,
|
|
12651
12651
|
method: "POST",
|
|
12652
|
-
headers: Object.entries(removeUndefinedEntries(originalHeaders)),
|
|
12652
|
+
headers: Object.entries(removeUndefinedEntries$1(originalHeaders)),
|
|
12653
12653
|
body,
|
|
12654
12654
|
region: credentials.region,
|
|
12655
12655
|
accessKeyId: credentials.accessKeyId,
|
|
@@ -12662,8 +12662,8 @@ function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
|
12662
12662
|
return fetch(input, {
|
|
12663
12663
|
...init,
|
|
12664
12664
|
body,
|
|
12665
|
-
headers: removeUndefinedEntries(
|
|
12666
|
-
combineHeaders(originalHeaders, signedHeaders)
|
|
12665
|
+
headers: removeUndefinedEntries$1(
|
|
12666
|
+
combineHeaders$1(originalHeaders, signedHeaders)
|
|
12667
12667
|
)
|
|
12668
12668
|
});
|
|
12669
12669
|
};
|
|
@@ -12717,7 +12717,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
12717
12717
|
}, options.fetch);
|
|
12718
12718
|
const getBaseUrl = () => {
|
|
12719
12719
|
var _a, _b;
|
|
12720
|
-
return (_b = withoutTrailingSlash(
|
|
12720
|
+
return (_b = withoutTrailingSlash$1(
|
|
12721
12721
|
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${loadSetting({
|
|
12722
12722
|
settingValue: options.region,
|
|
12723
12723
|
settingName: "region",
|
|
@@ -12732,7 +12732,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
12732
12732
|
baseUrl: getBaseUrl,
|
|
12733
12733
|
headers: (_a = options.headers) != null ? _a : {},
|
|
12734
12734
|
fetch: sigv4Fetch,
|
|
12735
|
-
generateId
|
|
12735
|
+
generateId: generateId$1
|
|
12736
12736
|
});
|
|
12737
12737
|
};
|
|
12738
12738
|
const provider = function(modelId, settings) {
|
|
@@ -12769,29 +12769,2065 @@ function createAmazonBedrock(options = {}) {
|
|
|
12769
12769
|
}
|
|
12770
12770
|
createAmazonBedrock();
|
|
12771
12771
|
|
|
12772
|
-
|
|
12773
|
-
|
|
12774
|
-
|
|
12775
|
-
|
|
12776
|
-
|
|
12777
|
-
|
|
12778
|
-
|
|
12779
|
-
|
|
12780
|
-
|
|
12781
|
-
|
|
12782
|
-
|
|
12783
|
-
|
|
12784
|
-
|
|
12785
|
-
|
|
12786
|
-
|
|
12787
|
-
|
|
12788
|
-
|
|
12789
|
-
|
|
12790
|
-
|
|
12791
|
-
|
|
12792
|
-
|
|
12793
|
-
|
|
12794
|
-
|
|
12772
|
+
// src/errors/ai-sdk-error.ts
|
|
12773
|
+
var marker = "vercel.ai.error";
|
|
12774
|
+
var symbol = Symbol.for(marker);
|
|
12775
|
+
var _a;
|
|
12776
|
+
var _AISDKError = class _AISDKError extends Error {
|
|
12777
|
+
/**
|
|
12778
|
+
* Creates an AI SDK Error.
|
|
12779
|
+
*
|
|
12780
|
+
* @param {Object} params - The parameters for creating the error.
|
|
12781
|
+
* @param {string} params.name - The name of the error.
|
|
12782
|
+
* @param {string} params.message - The error message.
|
|
12783
|
+
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
12784
|
+
*/
|
|
12785
|
+
constructor({
|
|
12786
|
+
name: name14,
|
|
12787
|
+
message,
|
|
12788
|
+
cause
|
|
12789
|
+
}) {
|
|
12790
|
+
super(message);
|
|
12791
|
+
this[_a] = true;
|
|
12792
|
+
this.name = name14;
|
|
12793
|
+
this.cause = cause;
|
|
12794
|
+
}
|
|
12795
|
+
/**
|
|
12796
|
+
* Checks if the given error is an AI SDK Error.
|
|
12797
|
+
* @param {unknown} error - The error to check.
|
|
12798
|
+
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
12799
|
+
*/
|
|
12800
|
+
static isInstance(error) {
|
|
12801
|
+
return _AISDKError.hasMarker(error, marker);
|
|
12802
|
+
}
|
|
12803
|
+
static hasMarker(error, marker15) {
|
|
12804
|
+
const markerSymbol = Symbol.for(marker15);
|
|
12805
|
+
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
12806
|
+
}
|
|
12807
|
+
};
|
|
12808
|
+
_a = symbol;
|
|
12809
|
+
var AISDKError = _AISDKError;
|
|
12810
|
+
|
|
12811
|
+
// src/errors/api-call-error.ts
|
|
12812
|
+
var name = "AI_APICallError";
|
|
12813
|
+
var marker2 = `vercel.ai.error.${name}`;
|
|
12814
|
+
var symbol2 = Symbol.for(marker2);
|
|
12815
|
+
var _a2;
|
|
12816
|
+
var APICallError = class extends AISDKError {
|
|
12817
|
+
constructor({
|
|
12818
|
+
message,
|
|
12819
|
+
url,
|
|
12820
|
+
requestBodyValues,
|
|
12821
|
+
statusCode,
|
|
12822
|
+
responseHeaders,
|
|
12823
|
+
responseBody,
|
|
12824
|
+
cause,
|
|
12825
|
+
isRetryable = statusCode != null && (statusCode === 408 || // request timeout
|
|
12826
|
+
statusCode === 409 || // conflict
|
|
12827
|
+
statusCode === 429 || // too many requests
|
|
12828
|
+
statusCode >= 500),
|
|
12829
|
+
// server error
|
|
12830
|
+
data
|
|
12831
|
+
}) {
|
|
12832
|
+
super({ name, message, cause });
|
|
12833
|
+
this[_a2] = true;
|
|
12834
|
+
this.url = url;
|
|
12835
|
+
this.requestBodyValues = requestBodyValues;
|
|
12836
|
+
this.statusCode = statusCode;
|
|
12837
|
+
this.responseHeaders = responseHeaders;
|
|
12838
|
+
this.responseBody = responseBody;
|
|
12839
|
+
this.isRetryable = isRetryable;
|
|
12840
|
+
this.data = data;
|
|
12841
|
+
}
|
|
12842
|
+
static isInstance(error) {
|
|
12843
|
+
return AISDKError.hasMarker(error, marker2);
|
|
12844
|
+
}
|
|
12845
|
+
};
|
|
12846
|
+
_a2 = symbol2;
|
|
12847
|
+
|
|
12848
|
+
// src/errors/empty-response-body-error.ts
|
|
12849
|
+
var name2 = "AI_EmptyResponseBodyError";
|
|
12850
|
+
var marker3 = `vercel.ai.error.${name2}`;
|
|
12851
|
+
var symbol3 = Symbol.for(marker3);
|
|
12852
|
+
var _a3;
|
|
12853
|
+
var EmptyResponseBodyError = class extends AISDKError {
|
|
12854
|
+
// used in isInstance
|
|
12855
|
+
constructor({ message = "Empty response body" } = {}) {
|
|
12856
|
+
super({ name: name2, message });
|
|
12857
|
+
this[_a3] = true;
|
|
12858
|
+
}
|
|
12859
|
+
static isInstance(error) {
|
|
12860
|
+
return AISDKError.hasMarker(error, marker3);
|
|
12861
|
+
}
|
|
12862
|
+
};
|
|
12863
|
+
_a3 = symbol3;
|
|
12864
|
+
|
|
12865
|
+
// src/errors/get-error-message.ts
|
|
12866
|
+
function getErrorMessage(error) {
|
|
12867
|
+
if (error == null) {
|
|
12868
|
+
return "unknown error";
|
|
12869
|
+
}
|
|
12870
|
+
if (typeof error === "string") {
|
|
12871
|
+
return error;
|
|
12872
|
+
}
|
|
12873
|
+
if (error instanceof Error) {
|
|
12874
|
+
return error.message;
|
|
12875
|
+
}
|
|
12876
|
+
return JSON.stringify(error);
|
|
12877
|
+
}
|
|
12878
|
+
|
|
12879
|
+
// src/errors/invalid-argument-error.ts
|
|
12880
|
+
var name3 = "AI_InvalidArgumentError";
|
|
12881
|
+
var marker4 = `vercel.ai.error.${name3}`;
|
|
12882
|
+
var symbol4 = Symbol.for(marker4);
|
|
12883
|
+
var _a4;
|
|
12884
|
+
var InvalidArgumentError = class extends AISDKError {
|
|
12885
|
+
constructor({
|
|
12886
|
+
message,
|
|
12887
|
+
cause,
|
|
12888
|
+
argument
|
|
12889
|
+
}) {
|
|
12890
|
+
super({ name: name3, message, cause });
|
|
12891
|
+
this[_a4] = true;
|
|
12892
|
+
this.argument = argument;
|
|
12893
|
+
}
|
|
12894
|
+
static isInstance(error) {
|
|
12895
|
+
return AISDKError.hasMarker(error, marker4);
|
|
12896
|
+
}
|
|
12897
|
+
};
|
|
12898
|
+
_a4 = symbol4;
|
|
12899
|
+
|
|
12900
|
+
// src/errors/invalid-prompt-error.ts
|
|
12901
|
+
var name4 = "AI_InvalidPromptError";
|
|
12902
|
+
var marker5 = `vercel.ai.error.${name4}`;
|
|
12903
|
+
var symbol5 = Symbol.for(marker5);
|
|
12904
|
+
var _a5;
|
|
12905
|
+
var InvalidPromptError = class extends AISDKError {
|
|
12906
|
+
constructor({
|
|
12907
|
+
prompt,
|
|
12908
|
+
message,
|
|
12909
|
+
cause
|
|
12910
|
+
}) {
|
|
12911
|
+
super({ name: name4, message: `Invalid prompt: ${message}`, cause });
|
|
12912
|
+
this[_a5] = true;
|
|
12913
|
+
this.prompt = prompt;
|
|
12914
|
+
}
|
|
12915
|
+
static isInstance(error) {
|
|
12916
|
+
return AISDKError.hasMarker(error, marker5);
|
|
12917
|
+
}
|
|
12918
|
+
};
|
|
12919
|
+
_a5 = symbol5;
|
|
12920
|
+
|
|
12921
|
+
// src/errors/invalid-response-data-error.ts
|
|
12922
|
+
var name5 = "AI_InvalidResponseDataError";
|
|
12923
|
+
var marker6 = `vercel.ai.error.${name5}`;
|
|
12924
|
+
var symbol6 = Symbol.for(marker6);
|
|
12925
|
+
var _a6;
|
|
12926
|
+
var InvalidResponseDataError = class extends AISDKError {
|
|
12927
|
+
constructor({
|
|
12928
|
+
data,
|
|
12929
|
+
message = `Invalid response data: ${JSON.stringify(data)}.`
|
|
12930
|
+
}) {
|
|
12931
|
+
super({ name: name5, message });
|
|
12932
|
+
this[_a6] = true;
|
|
12933
|
+
this.data = data;
|
|
12934
|
+
}
|
|
12935
|
+
static isInstance(error) {
|
|
12936
|
+
return AISDKError.hasMarker(error, marker6);
|
|
12937
|
+
}
|
|
12938
|
+
};
|
|
12939
|
+
_a6 = symbol6;
|
|
12940
|
+
|
|
12941
|
+
// src/errors/json-parse-error.ts
|
|
12942
|
+
var name6 = "AI_JSONParseError";
|
|
12943
|
+
var marker7 = `vercel.ai.error.${name6}`;
|
|
12944
|
+
var symbol7 = Symbol.for(marker7);
|
|
12945
|
+
var _a7;
|
|
12946
|
+
var JSONParseError = class extends AISDKError {
|
|
12947
|
+
constructor({ text, cause }) {
|
|
12948
|
+
super({
|
|
12949
|
+
name: name6,
|
|
12950
|
+
message: `JSON parsing failed: Text: ${text}.
|
|
12951
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
12952
|
+
cause
|
|
12953
|
+
});
|
|
12954
|
+
this[_a7] = true;
|
|
12955
|
+
this.text = text;
|
|
12956
|
+
}
|
|
12957
|
+
static isInstance(error) {
|
|
12958
|
+
return AISDKError.hasMarker(error, marker7);
|
|
12959
|
+
}
|
|
12960
|
+
};
|
|
12961
|
+
_a7 = symbol7;
|
|
12962
|
+
|
|
12963
|
+
// src/errors/load-api-key-error.ts
|
|
12964
|
+
var name7 = "AI_LoadAPIKeyError";
|
|
12965
|
+
var marker8 = `vercel.ai.error.${name7}`;
|
|
12966
|
+
var symbol8 = Symbol.for(marker8);
|
|
12967
|
+
var _a8;
|
|
12968
|
+
var LoadAPIKeyError = class extends AISDKError {
|
|
12969
|
+
// used in isInstance
|
|
12970
|
+
constructor({ message }) {
|
|
12971
|
+
super({ name: name7, message });
|
|
12972
|
+
this[_a8] = true;
|
|
12973
|
+
}
|
|
12974
|
+
static isInstance(error) {
|
|
12975
|
+
return AISDKError.hasMarker(error, marker8);
|
|
12976
|
+
}
|
|
12977
|
+
};
|
|
12978
|
+
_a8 = symbol8;
|
|
12979
|
+
|
|
12980
|
+
// src/errors/type-validation-error.ts
|
|
12981
|
+
var name12 = "AI_TypeValidationError";
|
|
12982
|
+
var marker13 = `vercel.ai.error.${name12}`;
|
|
12983
|
+
var symbol13 = Symbol.for(marker13);
|
|
12984
|
+
var _a13;
|
|
12985
|
+
var _TypeValidationError = class _TypeValidationError extends AISDKError {
|
|
12986
|
+
constructor({ value, cause }) {
|
|
12987
|
+
super({
|
|
12988
|
+
name: name12,
|
|
12989
|
+
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
12990
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
12991
|
+
cause
|
|
12992
|
+
});
|
|
12993
|
+
this[_a13] = true;
|
|
12994
|
+
this.value = value;
|
|
12995
|
+
}
|
|
12996
|
+
static isInstance(error) {
|
|
12997
|
+
return AISDKError.hasMarker(error, marker13);
|
|
12998
|
+
}
|
|
12999
|
+
/**
|
|
13000
|
+
* Wraps an error into a TypeValidationError.
|
|
13001
|
+
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
13002
|
+
* Otherwise, it creates a new TypeValidationError.
|
|
13003
|
+
*
|
|
13004
|
+
* @param {Object} params - The parameters for wrapping the error.
|
|
13005
|
+
* @param {unknown} params.value - The value that failed validation.
|
|
13006
|
+
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
13007
|
+
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
13008
|
+
*/
|
|
13009
|
+
static wrap({
|
|
13010
|
+
value,
|
|
13011
|
+
cause
|
|
13012
|
+
}) {
|
|
13013
|
+
return _TypeValidationError.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError({ value, cause });
|
|
13014
|
+
}
|
|
13015
|
+
};
|
|
13016
|
+
_a13 = symbol13;
|
|
13017
|
+
var TypeValidationError = _TypeValidationError;
|
|
13018
|
+
|
|
13019
|
+
// src/errors/unsupported-functionality-error.ts
|
|
13020
|
+
var name13 = "AI_UnsupportedFunctionalityError";
|
|
13021
|
+
var marker14 = `vercel.ai.error.${name13}`;
|
|
13022
|
+
var symbol14 = Symbol.for(marker14);
|
|
13023
|
+
var _a14;
|
|
13024
|
+
var UnsupportedFunctionalityError = class extends AISDKError {
|
|
13025
|
+
constructor({
|
|
13026
|
+
functionality,
|
|
13027
|
+
message = `'${functionality}' functionality not supported.`
|
|
13028
|
+
}) {
|
|
13029
|
+
super({ name: name13, message });
|
|
13030
|
+
this[_a14] = true;
|
|
13031
|
+
this.functionality = functionality;
|
|
13032
|
+
}
|
|
13033
|
+
static isInstance(error) {
|
|
13034
|
+
return AISDKError.hasMarker(error, marker14);
|
|
13035
|
+
}
|
|
13036
|
+
};
|
|
13037
|
+
_a14 = symbol14;
|
|
13038
|
+
|
|
13039
|
+
var secureJsonParse = {exports: {}};
|
|
13040
|
+
|
|
13041
|
+
var hasRequiredSecureJsonParse;
|
|
13042
|
+
|
|
13043
|
+
function requireSecureJsonParse () {
|
|
13044
|
+
if (hasRequiredSecureJsonParse) return secureJsonParse.exports;
|
|
13045
|
+
hasRequiredSecureJsonParse = 1;
|
|
13046
|
+
|
|
13047
|
+
const hasBuffer = typeof Buffer !== 'undefined';
|
|
13048
|
+
const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
13049
|
+
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
13050
|
+
|
|
13051
|
+
function _parse (text, reviver, options) {
|
|
13052
|
+
// Normalize arguments
|
|
13053
|
+
if (options == null) {
|
|
13054
|
+
if (reviver !== null && typeof reviver === 'object') {
|
|
13055
|
+
options = reviver;
|
|
13056
|
+
reviver = undefined;
|
|
13057
|
+
}
|
|
13058
|
+
}
|
|
13059
|
+
|
|
13060
|
+
if (hasBuffer && Buffer.isBuffer(text)) {
|
|
13061
|
+
text = text.toString();
|
|
13062
|
+
}
|
|
13063
|
+
|
|
13064
|
+
// BOM checker
|
|
13065
|
+
if (text && text.charCodeAt(0) === 0xFEFF) {
|
|
13066
|
+
text = text.slice(1);
|
|
13067
|
+
}
|
|
13068
|
+
|
|
13069
|
+
// Parse normally, allowing exceptions
|
|
13070
|
+
const obj = JSON.parse(text, reviver);
|
|
13071
|
+
|
|
13072
|
+
// Ignore null and non-objects
|
|
13073
|
+
if (obj === null || typeof obj !== 'object') {
|
|
13074
|
+
return obj
|
|
13075
|
+
}
|
|
13076
|
+
|
|
13077
|
+
const protoAction = (options && options.protoAction) || 'error';
|
|
13078
|
+
const constructorAction = (options && options.constructorAction) || 'error';
|
|
13079
|
+
|
|
13080
|
+
// options: 'error' (default) / 'remove' / 'ignore'
|
|
13081
|
+
if (protoAction === 'ignore' && constructorAction === 'ignore') {
|
|
13082
|
+
return obj
|
|
13083
|
+
}
|
|
13084
|
+
|
|
13085
|
+
if (protoAction !== 'ignore' && constructorAction !== 'ignore') {
|
|
13086
|
+
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
|
13087
|
+
return obj
|
|
13088
|
+
}
|
|
13089
|
+
} else if (protoAction !== 'ignore' && constructorAction === 'ignore') {
|
|
13090
|
+
if (suspectProtoRx.test(text) === false) {
|
|
13091
|
+
return obj
|
|
13092
|
+
}
|
|
13093
|
+
} else {
|
|
13094
|
+
if (suspectConstructorRx.test(text) === false) {
|
|
13095
|
+
return obj
|
|
13096
|
+
}
|
|
13097
|
+
}
|
|
13098
|
+
|
|
13099
|
+
// Scan result for proto keys
|
|
13100
|
+
return filter(obj, { protoAction, constructorAction, safe: options && options.safe })
|
|
13101
|
+
}
|
|
13102
|
+
|
|
13103
|
+
function filter (obj, { protoAction = 'error', constructorAction = 'error', safe } = {}) {
|
|
13104
|
+
let next = [obj];
|
|
13105
|
+
|
|
13106
|
+
while (next.length) {
|
|
13107
|
+
const nodes = next;
|
|
13108
|
+
next = [];
|
|
13109
|
+
|
|
13110
|
+
for (const node of nodes) {
|
|
13111
|
+
if (protoAction !== 'ignore' && Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly
|
|
13112
|
+
if (safe === true) {
|
|
13113
|
+
return null
|
|
13114
|
+
} else if (protoAction === 'error') {
|
|
13115
|
+
throw new SyntaxError('Object contains forbidden prototype property')
|
|
13116
|
+
}
|
|
13117
|
+
|
|
13118
|
+
delete node.__proto__; // eslint-disable-line no-proto
|
|
13119
|
+
}
|
|
13120
|
+
|
|
13121
|
+
if (constructorAction !== 'ignore' &&
|
|
13122
|
+
Object.prototype.hasOwnProperty.call(node, 'constructor') &&
|
|
13123
|
+
Object.prototype.hasOwnProperty.call(node.constructor, 'prototype')) { // Avoid calling node.hasOwnProperty directly
|
|
13124
|
+
if (safe === true) {
|
|
13125
|
+
return null
|
|
13126
|
+
} else if (constructorAction === 'error') {
|
|
13127
|
+
throw new SyntaxError('Object contains forbidden prototype property')
|
|
13128
|
+
}
|
|
13129
|
+
|
|
13130
|
+
delete node.constructor;
|
|
13131
|
+
}
|
|
13132
|
+
|
|
13133
|
+
for (const key in node) {
|
|
13134
|
+
const value = node[key];
|
|
13135
|
+
if (value && typeof value === 'object') {
|
|
13136
|
+
next.push(value);
|
|
13137
|
+
}
|
|
13138
|
+
}
|
|
13139
|
+
}
|
|
13140
|
+
}
|
|
13141
|
+
return obj
|
|
13142
|
+
}
|
|
13143
|
+
|
|
13144
|
+
function parse (text, reviver, options) {
|
|
13145
|
+
const stackTraceLimit = Error.stackTraceLimit;
|
|
13146
|
+
Error.stackTraceLimit = 0;
|
|
13147
|
+
try {
|
|
13148
|
+
return _parse(text, reviver, options)
|
|
13149
|
+
} finally {
|
|
13150
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
13151
|
+
}
|
|
13152
|
+
}
|
|
13153
|
+
|
|
13154
|
+
function safeParse (text, reviver) {
|
|
13155
|
+
const stackTraceLimit = Error.stackTraceLimit;
|
|
13156
|
+
Error.stackTraceLimit = 0;
|
|
13157
|
+
try {
|
|
13158
|
+
return _parse(text, reviver, { safe: true })
|
|
13159
|
+
} catch (_e) {
|
|
13160
|
+
return null
|
|
13161
|
+
} finally {
|
|
13162
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
13163
|
+
}
|
|
13164
|
+
}
|
|
13165
|
+
|
|
13166
|
+
secureJsonParse.exports = parse;
|
|
13167
|
+
secureJsonParse.exports.default = parse;
|
|
13168
|
+
secureJsonParse.exports.parse = parse;
|
|
13169
|
+
secureJsonParse.exports.safeParse = safeParse;
|
|
13170
|
+
secureJsonParse.exports.scan = filter;
|
|
13171
|
+
return secureJsonParse.exports;
|
|
13172
|
+
}
|
|
13173
|
+
|
|
13174
|
+
var secureJsonParseExports = requireSecureJsonParse();
|
|
13175
|
+
var SecureJSON = /*@__PURE__*/getDefaultExportFromCjs(secureJsonParseExports);
|
|
13176
|
+
|
|
13177
|
+
class ParseError extends Error {
|
|
13178
|
+
constructor(message, options) {
|
|
13179
|
+
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
13180
|
+
}
|
|
13181
|
+
}
|
|
13182
|
+
function noop(_arg) {
|
|
13183
|
+
}
|
|
13184
|
+
function createParser(callbacks) {
|
|
13185
|
+
if (typeof callbacks == "function")
|
|
13186
|
+
throw new TypeError(
|
|
13187
|
+
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
13188
|
+
);
|
|
13189
|
+
const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
|
|
13190
|
+
let incompleteLine = "", isFirstChunk = true, id, data = "", eventType = "";
|
|
13191
|
+
function feed(newChunk) {
|
|
13192
|
+
const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
|
|
13193
|
+
for (const line of complete)
|
|
13194
|
+
parseLine(line);
|
|
13195
|
+
incompleteLine = incomplete, isFirstChunk = false;
|
|
13196
|
+
}
|
|
13197
|
+
function parseLine(line) {
|
|
13198
|
+
if (line === "") {
|
|
13199
|
+
dispatchEvent();
|
|
13200
|
+
return;
|
|
13201
|
+
}
|
|
13202
|
+
if (line.startsWith(":")) {
|
|
13203
|
+
onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
|
|
13204
|
+
return;
|
|
13205
|
+
}
|
|
13206
|
+
const fieldSeparatorIndex = line.indexOf(":");
|
|
13207
|
+
if (fieldSeparatorIndex !== -1) {
|
|
13208
|
+
const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
|
|
13209
|
+
processField(field, value, line);
|
|
13210
|
+
return;
|
|
13211
|
+
}
|
|
13212
|
+
processField(line, "", line);
|
|
13213
|
+
}
|
|
13214
|
+
function processField(field, value, line) {
|
|
13215
|
+
switch (field) {
|
|
13216
|
+
case "event":
|
|
13217
|
+
eventType = value;
|
|
13218
|
+
break;
|
|
13219
|
+
case "data":
|
|
13220
|
+
data = `${data}${value}
|
|
13221
|
+
`;
|
|
13222
|
+
break;
|
|
13223
|
+
case "id":
|
|
13224
|
+
id = value.includes("\0") ? void 0 : value;
|
|
13225
|
+
break;
|
|
13226
|
+
case "retry":
|
|
13227
|
+
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
|
|
13228
|
+
new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
13229
|
+
type: "invalid-retry",
|
|
13230
|
+
value,
|
|
13231
|
+
line
|
|
13232
|
+
})
|
|
13233
|
+
);
|
|
13234
|
+
break;
|
|
13235
|
+
default:
|
|
13236
|
+
onError(
|
|
13237
|
+
new ParseError(
|
|
13238
|
+
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
|
|
13239
|
+
{ type: "unknown-field", field, value, line }
|
|
13240
|
+
)
|
|
13241
|
+
);
|
|
13242
|
+
break;
|
|
13243
|
+
}
|
|
13244
|
+
}
|
|
13245
|
+
function dispatchEvent() {
|
|
13246
|
+
data.length > 0 && onEvent({
|
|
13247
|
+
id,
|
|
13248
|
+
event: eventType || void 0,
|
|
13249
|
+
// If the data buffer's last character is a U+000A LINE FEED (LF) character,
|
|
13250
|
+
// then remove the last character from the data buffer.
|
|
13251
|
+
data: data.endsWith(`
|
|
13252
|
+
`) ? data.slice(0, -1) : data
|
|
13253
|
+
}), id = void 0, data = "", eventType = "";
|
|
13254
|
+
}
|
|
13255
|
+
function reset(options = {}) {
|
|
13256
|
+
incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = true, id = void 0, data = "", eventType = "", incompleteLine = "";
|
|
13257
|
+
}
|
|
13258
|
+
return { feed, reset };
|
|
13259
|
+
}
|
|
13260
|
+
function splitLines(chunk) {
|
|
13261
|
+
const lines = [];
|
|
13262
|
+
let incompleteLine = "", searchIndex = 0;
|
|
13263
|
+
for (; searchIndex < chunk.length; ) {
|
|
13264
|
+
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
13265
|
+
`, searchIndex);
|
|
13266
|
+
let lineEnd = -1;
|
|
13267
|
+
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
|
|
13268
|
+
incompleteLine = chunk.slice(searchIndex);
|
|
13269
|
+
break;
|
|
13270
|
+
} else {
|
|
13271
|
+
const line = chunk.slice(searchIndex, lineEnd);
|
|
13272
|
+
lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
|
|
13273
|
+
` && searchIndex++;
|
|
13274
|
+
}
|
|
13275
|
+
}
|
|
13276
|
+
return [lines, incompleteLine];
|
|
13277
|
+
}
|
|
13278
|
+
|
|
13279
|
+
class EventSourceParserStream extends TransformStream {
|
|
13280
|
+
constructor({ onError, onRetry, onComment } = {}) {
|
|
13281
|
+
let parser;
|
|
13282
|
+
super({
|
|
13283
|
+
start(controller) {
|
|
13284
|
+
parser = createParser({
|
|
13285
|
+
onEvent: (event) => {
|
|
13286
|
+
controller.enqueue(event);
|
|
13287
|
+
},
|
|
13288
|
+
onError(error) {
|
|
13289
|
+
onError === "terminate" ? controller.error(error) : typeof onError == "function" && onError(error);
|
|
13290
|
+
},
|
|
13291
|
+
onRetry,
|
|
13292
|
+
onComment
|
|
13293
|
+
});
|
|
13294
|
+
},
|
|
13295
|
+
transform(chunk) {
|
|
13296
|
+
parser.feed(chunk);
|
|
13297
|
+
}
|
|
13298
|
+
});
|
|
13299
|
+
}
|
|
13300
|
+
}
|
|
13301
|
+
|
|
13302
|
+
// src/combine-headers.ts
|
|
13303
|
+
function combineHeaders(...headers) {
|
|
13304
|
+
return headers.reduce(
|
|
13305
|
+
(combinedHeaders, currentHeaders) => ({
|
|
13306
|
+
...combinedHeaders,
|
|
13307
|
+
...currentHeaders != null ? currentHeaders : {}
|
|
13308
|
+
}),
|
|
13309
|
+
{}
|
|
13310
|
+
);
|
|
13311
|
+
}
|
|
13312
|
+
|
|
13313
|
+
// src/extract-response-headers.ts
|
|
13314
|
+
function extractResponseHeaders(response) {
|
|
13315
|
+
const headers = {};
|
|
13316
|
+
response.headers.forEach((value, key) => {
|
|
13317
|
+
headers[key] = value;
|
|
13318
|
+
});
|
|
13319
|
+
return headers;
|
|
13320
|
+
}
|
|
13321
|
+
var createIdGenerator = ({
|
|
13322
|
+
prefix,
|
|
13323
|
+
size: defaultSize = 16,
|
|
13324
|
+
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
13325
|
+
separator = "-"
|
|
13326
|
+
} = {}) => {
|
|
13327
|
+
const generator = customAlphabet(alphabet, defaultSize);
|
|
13328
|
+
if (prefix == null) {
|
|
13329
|
+
return generator;
|
|
13330
|
+
}
|
|
13331
|
+
if (alphabet.includes(separator)) {
|
|
13332
|
+
throw new InvalidArgumentError({
|
|
13333
|
+
argument: "separator",
|
|
13334
|
+
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
13335
|
+
});
|
|
13336
|
+
}
|
|
13337
|
+
return (size) => `${prefix}${separator}${generator(size)}`;
|
|
13338
|
+
};
|
|
13339
|
+
var generateId = createIdGenerator();
|
|
13340
|
+
|
|
13341
|
+
// src/remove-undefined-entries.ts
|
|
13342
|
+
function removeUndefinedEntries(record) {
|
|
13343
|
+
return Object.fromEntries(
|
|
13344
|
+
Object.entries(record).filter(([_key, value]) => value != null)
|
|
13345
|
+
);
|
|
13346
|
+
}
|
|
13347
|
+
|
|
13348
|
+
// src/is-abort-error.ts
|
|
13349
|
+
function isAbortError(error) {
|
|
13350
|
+
return error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
|
13351
|
+
}
|
|
13352
|
+
function loadApiKey({
|
|
13353
|
+
apiKey,
|
|
13354
|
+
environmentVariableName,
|
|
13355
|
+
apiKeyParameterName = "apiKey",
|
|
13356
|
+
description
|
|
13357
|
+
}) {
|
|
13358
|
+
if (typeof apiKey === "string") {
|
|
13359
|
+
return apiKey;
|
|
13360
|
+
}
|
|
13361
|
+
if (apiKey != null) {
|
|
13362
|
+
throw new LoadAPIKeyError({
|
|
13363
|
+
message: `${description} API key must be a string.`
|
|
13364
|
+
});
|
|
13365
|
+
}
|
|
13366
|
+
if (typeof process === "undefined") {
|
|
13367
|
+
throw new LoadAPIKeyError({
|
|
13368
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`
|
|
13369
|
+
});
|
|
13370
|
+
}
|
|
13371
|
+
apiKey = process.env[environmentVariableName];
|
|
13372
|
+
if (apiKey == null) {
|
|
13373
|
+
throw new LoadAPIKeyError({
|
|
13374
|
+
message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`
|
|
13375
|
+
});
|
|
13376
|
+
}
|
|
13377
|
+
if (typeof apiKey !== "string") {
|
|
13378
|
+
throw new LoadAPIKeyError({
|
|
13379
|
+
message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`
|
|
13380
|
+
});
|
|
13381
|
+
}
|
|
13382
|
+
return apiKey;
|
|
13383
|
+
}
|
|
13384
|
+
|
|
13385
|
+
// src/validator.ts
|
|
13386
|
+
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
|
13387
|
+
function validator(validate) {
|
|
13388
|
+
return { [validatorSymbol]: true, validate };
|
|
13389
|
+
}
|
|
13390
|
+
function isValidator(value) {
|
|
13391
|
+
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
13392
|
+
}
|
|
13393
|
+
function asValidator(value) {
|
|
13394
|
+
return isValidator(value) ? value : zodValidator(value);
|
|
13395
|
+
}
|
|
13396
|
+
function zodValidator(zodSchema) {
|
|
13397
|
+
return validator((value) => {
|
|
13398
|
+
const result = zodSchema.safeParse(value);
|
|
13399
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
13400
|
+
});
|
|
13401
|
+
}
|
|
13402
|
+
|
|
13403
|
+
// src/validate-types.ts
|
|
13404
|
+
function validateTypes({
|
|
13405
|
+
value,
|
|
13406
|
+
schema: inputSchema
|
|
13407
|
+
}) {
|
|
13408
|
+
const result = safeValidateTypes({ value, schema: inputSchema });
|
|
13409
|
+
if (!result.success) {
|
|
13410
|
+
throw TypeValidationError.wrap({ value, cause: result.error });
|
|
13411
|
+
}
|
|
13412
|
+
return result.value;
|
|
13413
|
+
}
|
|
13414
|
+
function safeValidateTypes({
|
|
13415
|
+
value,
|
|
13416
|
+
schema
|
|
13417
|
+
}) {
|
|
13418
|
+
const validator2 = asValidator(schema);
|
|
13419
|
+
try {
|
|
13420
|
+
if (validator2.validate == null) {
|
|
13421
|
+
return { success: true, value };
|
|
13422
|
+
}
|
|
13423
|
+
const result = validator2.validate(value);
|
|
13424
|
+
if (result.success) {
|
|
13425
|
+
return result;
|
|
13426
|
+
}
|
|
13427
|
+
return {
|
|
13428
|
+
success: false,
|
|
13429
|
+
error: TypeValidationError.wrap({ value, cause: result.error })
|
|
13430
|
+
};
|
|
13431
|
+
} catch (error) {
|
|
13432
|
+
return {
|
|
13433
|
+
success: false,
|
|
13434
|
+
error: TypeValidationError.wrap({ value, cause: error })
|
|
13435
|
+
};
|
|
13436
|
+
}
|
|
13437
|
+
}
|
|
13438
|
+
|
|
13439
|
+
// src/parse-json.ts
|
|
13440
|
+
function parseJSON({
|
|
13441
|
+
text,
|
|
13442
|
+
schema
|
|
13443
|
+
}) {
|
|
13444
|
+
try {
|
|
13445
|
+
const value = SecureJSON.parse(text);
|
|
13446
|
+
if (schema == null) {
|
|
13447
|
+
return value;
|
|
13448
|
+
}
|
|
13449
|
+
return validateTypes({ value, schema });
|
|
13450
|
+
} catch (error) {
|
|
13451
|
+
if (JSONParseError.isInstance(error) || TypeValidationError.isInstance(error)) {
|
|
13452
|
+
throw error;
|
|
13453
|
+
}
|
|
13454
|
+
throw new JSONParseError({ text, cause: error });
|
|
13455
|
+
}
|
|
13456
|
+
}
|
|
13457
|
+
function safeParseJSON({
|
|
13458
|
+
text,
|
|
13459
|
+
schema
|
|
13460
|
+
}) {
|
|
13461
|
+
try {
|
|
13462
|
+
const value = SecureJSON.parse(text);
|
|
13463
|
+
if (schema == null) {
|
|
13464
|
+
return { success: true, value, rawValue: value };
|
|
13465
|
+
}
|
|
13466
|
+
const validationResult = safeValidateTypes({ value, schema });
|
|
13467
|
+
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
|
13468
|
+
} catch (error) {
|
|
13469
|
+
return {
|
|
13470
|
+
success: false,
|
|
13471
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text, cause: error })
|
|
13472
|
+
};
|
|
13473
|
+
}
|
|
13474
|
+
}
|
|
13475
|
+
function isParsableJson(input) {
|
|
13476
|
+
try {
|
|
13477
|
+
SecureJSON.parse(input);
|
|
13478
|
+
return true;
|
|
13479
|
+
} catch (e) {
|
|
13480
|
+
return false;
|
|
13481
|
+
}
|
|
13482
|
+
}
|
|
13483
|
+
var getOriginalFetch2 = () => globalThis.fetch;
|
|
13484
|
+
var postJsonToApi = async ({
|
|
13485
|
+
url,
|
|
13486
|
+
headers,
|
|
13487
|
+
body,
|
|
13488
|
+
failedResponseHandler,
|
|
13489
|
+
successfulResponseHandler,
|
|
13490
|
+
abortSignal,
|
|
13491
|
+
fetch
|
|
13492
|
+
}) => postToApi({
|
|
13493
|
+
url,
|
|
13494
|
+
headers: {
|
|
13495
|
+
"Content-Type": "application/json",
|
|
13496
|
+
...headers
|
|
13497
|
+
},
|
|
13498
|
+
body: {
|
|
13499
|
+
content: JSON.stringify(body),
|
|
13500
|
+
values: body
|
|
13501
|
+
},
|
|
13502
|
+
failedResponseHandler,
|
|
13503
|
+
successfulResponseHandler,
|
|
13504
|
+
abortSignal,
|
|
13505
|
+
fetch
|
|
13506
|
+
});
|
|
13507
|
+
var postToApi = async ({
|
|
13508
|
+
url,
|
|
13509
|
+
headers = {},
|
|
13510
|
+
body,
|
|
13511
|
+
successfulResponseHandler,
|
|
13512
|
+
failedResponseHandler,
|
|
13513
|
+
abortSignal,
|
|
13514
|
+
fetch = getOriginalFetch2()
|
|
13515
|
+
}) => {
|
|
13516
|
+
try {
|
|
13517
|
+
const response = await fetch(url, {
|
|
13518
|
+
method: "POST",
|
|
13519
|
+
headers: removeUndefinedEntries(headers),
|
|
13520
|
+
body: body.content,
|
|
13521
|
+
signal: abortSignal
|
|
13522
|
+
});
|
|
13523
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
13524
|
+
if (!response.ok) {
|
|
13525
|
+
let errorInformation;
|
|
13526
|
+
try {
|
|
13527
|
+
errorInformation = await failedResponseHandler({
|
|
13528
|
+
response,
|
|
13529
|
+
url,
|
|
13530
|
+
requestBodyValues: body.values
|
|
13531
|
+
});
|
|
13532
|
+
} catch (error) {
|
|
13533
|
+
if (isAbortError(error) || APICallError.isInstance(error)) {
|
|
13534
|
+
throw error;
|
|
13535
|
+
}
|
|
13536
|
+
throw new APICallError({
|
|
13537
|
+
message: "Failed to process error response",
|
|
13538
|
+
cause: error,
|
|
13539
|
+
statusCode: response.status,
|
|
13540
|
+
url,
|
|
13541
|
+
responseHeaders,
|
|
13542
|
+
requestBodyValues: body.values
|
|
13543
|
+
});
|
|
13544
|
+
}
|
|
13545
|
+
throw errorInformation.value;
|
|
13546
|
+
}
|
|
13547
|
+
try {
|
|
13548
|
+
return await successfulResponseHandler({
|
|
13549
|
+
response,
|
|
13550
|
+
url,
|
|
13551
|
+
requestBodyValues: body.values
|
|
13552
|
+
});
|
|
13553
|
+
} catch (error) {
|
|
13554
|
+
if (error instanceof Error) {
|
|
13555
|
+
if (isAbortError(error) || APICallError.isInstance(error)) {
|
|
13556
|
+
throw error;
|
|
13557
|
+
}
|
|
13558
|
+
}
|
|
13559
|
+
throw new APICallError({
|
|
13560
|
+
message: "Failed to process successful response",
|
|
13561
|
+
cause: error,
|
|
13562
|
+
statusCode: response.status,
|
|
13563
|
+
url,
|
|
13564
|
+
responseHeaders,
|
|
13565
|
+
requestBodyValues: body.values
|
|
13566
|
+
});
|
|
13567
|
+
}
|
|
13568
|
+
} catch (error) {
|
|
13569
|
+
if (isAbortError(error)) {
|
|
13570
|
+
throw error;
|
|
13571
|
+
}
|
|
13572
|
+
if (error instanceof TypeError && error.message === "fetch failed") {
|
|
13573
|
+
const cause = error.cause;
|
|
13574
|
+
if (cause != null) {
|
|
13575
|
+
throw new APICallError({
|
|
13576
|
+
message: `Cannot connect to API: ${cause.message}`,
|
|
13577
|
+
cause,
|
|
13578
|
+
url,
|
|
13579
|
+
requestBodyValues: body.values,
|
|
13580
|
+
isRetryable: true
|
|
13581
|
+
// retry when network error
|
|
13582
|
+
});
|
|
13583
|
+
}
|
|
13584
|
+
}
|
|
13585
|
+
throw error;
|
|
13586
|
+
}
|
|
13587
|
+
};
|
|
13588
|
+
var createJsonErrorResponseHandler = ({
|
|
13589
|
+
errorSchema,
|
|
13590
|
+
errorToMessage,
|
|
13591
|
+
isRetryable
|
|
13592
|
+
}) => async ({ response, url, requestBodyValues }) => {
|
|
13593
|
+
const responseBody = await response.text();
|
|
13594
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
13595
|
+
if (responseBody.trim() === "") {
|
|
13596
|
+
return {
|
|
13597
|
+
responseHeaders,
|
|
13598
|
+
value: new APICallError({
|
|
13599
|
+
message: response.statusText,
|
|
13600
|
+
url,
|
|
13601
|
+
requestBodyValues,
|
|
13602
|
+
statusCode: response.status,
|
|
13603
|
+
responseHeaders,
|
|
13604
|
+
responseBody,
|
|
13605
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
|
13606
|
+
})
|
|
13607
|
+
};
|
|
13608
|
+
}
|
|
13609
|
+
try {
|
|
13610
|
+
const parsedError = parseJSON({
|
|
13611
|
+
text: responseBody,
|
|
13612
|
+
schema: errorSchema
|
|
13613
|
+
});
|
|
13614
|
+
return {
|
|
13615
|
+
responseHeaders,
|
|
13616
|
+
value: new APICallError({
|
|
13617
|
+
message: errorToMessage(parsedError),
|
|
13618
|
+
url,
|
|
13619
|
+
requestBodyValues,
|
|
13620
|
+
statusCode: response.status,
|
|
13621
|
+
responseHeaders,
|
|
13622
|
+
responseBody,
|
|
13623
|
+
data: parsedError,
|
|
13624
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
|
13625
|
+
})
|
|
13626
|
+
};
|
|
13627
|
+
} catch (parseError) {
|
|
13628
|
+
return {
|
|
13629
|
+
responseHeaders,
|
|
13630
|
+
value: new APICallError({
|
|
13631
|
+
message: response.statusText,
|
|
13632
|
+
url,
|
|
13633
|
+
requestBodyValues,
|
|
13634
|
+
statusCode: response.status,
|
|
13635
|
+
responseHeaders,
|
|
13636
|
+
responseBody,
|
|
13637
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
|
13638
|
+
})
|
|
13639
|
+
};
|
|
13640
|
+
}
|
|
13641
|
+
};
|
|
13642
|
+
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
|
13643
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
13644
|
+
if (response.body == null) {
|
|
13645
|
+
throw new EmptyResponseBodyError({});
|
|
13646
|
+
}
|
|
13647
|
+
return {
|
|
13648
|
+
responseHeaders,
|
|
13649
|
+
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(
|
|
13650
|
+
new TransformStream({
|
|
13651
|
+
transform({ data }, controller) {
|
|
13652
|
+
if (data === "[DONE]") {
|
|
13653
|
+
return;
|
|
13654
|
+
}
|
|
13655
|
+
controller.enqueue(
|
|
13656
|
+
safeParseJSON({
|
|
13657
|
+
text: data,
|
|
13658
|
+
schema: chunkSchema
|
|
13659
|
+
})
|
|
13660
|
+
);
|
|
13661
|
+
}
|
|
13662
|
+
})
|
|
13663
|
+
)
|
|
13664
|
+
};
|
|
13665
|
+
};
|
|
13666
|
+
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
|
13667
|
+
const responseBody = await response.text();
|
|
13668
|
+
const parsedResult = safeParseJSON({
|
|
13669
|
+
text: responseBody,
|
|
13670
|
+
schema: responseSchema
|
|
13671
|
+
});
|
|
13672
|
+
const responseHeaders = extractResponseHeaders(response);
|
|
13673
|
+
if (!parsedResult.success) {
|
|
13674
|
+
throw new APICallError({
|
|
13675
|
+
message: "Invalid JSON response",
|
|
13676
|
+
cause: parsedResult.error,
|
|
13677
|
+
statusCode: response.status,
|
|
13678
|
+
responseHeaders,
|
|
13679
|
+
responseBody,
|
|
13680
|
+
url,
|
|
13681
|
+
requestBodyValues
|
|
13682
|
+
});
|
|
13683
|
+
}
|
|
13684
|
+
return {
|
|
13685
|
+
responseHeaders,
|
|
13686
|
+
value: parsedResult.value,
|
|
13687
|
+
rawValue: parsedResult.rawValue
|
|
13688
|
+
};
|
|
13689
|
+
};
|
|
13690
|
+
|
|
13691
|
+
// src/uint8-utils.ts
|
|
13692
|
+
var { btoa} = globalThis;
|
|
13693
|
+
function convertUint8ArrayToBase64(array) {
|
|
13694
|
+
let latin1string = "";
|
|
13695
|
+
for (let i = 0; i < array.length; i++) {
|
|
13696
|
+
latin1string += String.fromCodePoint(array[i]);
|
|
13697
|
+
}
|
|
13698
|
+
return btoa(latin1string);
|
|
13699
|
+
}
|
|
13700
|
+
|
|
13701
|
+
// src/without-trailing-slash.ts
|
|
13702
|
+
function withoutTrailingSlash(url) {
|
|
13703
|
+
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
13704
|
+
}
|
|
13705
|
+
|
|
13706
|
+
var __defProp = Object.defineProperty;
|
|
13707
|
+
var __defProps = Object.defineProperties;
|
|
13708
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
13709
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
13710
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
13711
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
13712
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13713
|
+
var __spreadValues = (a, b) => {
|
|
13714
|
+
for (var prop in b || (b = {}))
|
|
13715
|
+
if (__hasOwnProp.call(b, prop))
|
|
13716
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13717
|
+
if (__getOwnPropSymbols)
|
|
13718
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
13719
|
+
if (__propIsEnum.call(b, prop))
|
|
13720
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13721
|
+
}
|
|
13722
|
+
return a;
|
|
13723
|
+
};
|
|
13724
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
13725
|
+
var __objRest = (source, exclude) => {
|
|
13726
|
+
var target = {};
|
|
13727
|
+
for (var prop in source)
|
|
13728
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
13729
|
+
target[prop] = source[prop];
|
|
13730
|
+
if (source != null && __getOwnPropSymbols)
|
|
13731
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
13732
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
13733
|
+
target[prop] = source[prop];
|
|
13734
|
+
}
|
|
13735
|
+
return target;
|
|
13736
|
+
};
|
|
13737
|
+
function getCacheControl(providerMetadata) {
|
|
13738
|
+
var _a, _b, _c;
|
|
13739
|
+
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
13740
|
+
const openrouter2 = providerMetadata == null ? void 0 : providerMetadata.openrouter;
|
|
13741
|
+
return (_c = (_b = (_a = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
13742
|
+
}
|
|
13743
|
+
function convertToOpenRouterChatMessages(prompt) {
|
|
13744
|
+
var _a, _b, _c;
|
|
13745
|
+
const messages = [];
|
|
13746
|
+
for (const { role, content, providerMetadata } of prompt) {
|
|
13747
|
+
switch (role) {
|
|
13748
|
+
case "system": {
|
|
13749
|
+
messages.push({
|
|
13750
|
+
role: "system",
|
|
13751
|
+
content,
|
|
13752
|
+
cache_control: getCacheControl(providerMetadata)
|
|
13753
|
+
});
|
|
13754
|
+
break;
|
|
13755
|
+
}
|
|
13756
|
+
case "user": {
|
|
13757
|
+
if (content.length === 1 && ((_a = content[0]) == null ? void 0 : _a.type) === "text") {
|
|
13758
|
+
messages.push({
|
|
13759
|
+
role: "user",
|
|
13760
|
+
content: content[0].text,
|
|
13761
|
+
cache_control: (_b = getCacheControl(providerMetadata)) != null ? _b : getCacheControl(content[0].providerMetadata)
|
|
13762
|
+
});
|
|
13763
|
+
break;
|
|
13764
|
+
}
|
|
13765
|
+
const messageCacheControl = getCacheControl(providerMetadata);
|
|
13766
|
+
const contentParts = content.map(
|
|
13767
|
+
(part) => {
|
|
13768
|
+
var _a2, _b2, _c2, _d;
|
|
13769
|
+
switch (part.type) {
|
|
13770
|
+
case "text":
|
|
13771
|
+
return {
|
|
13772
|
+
type: "text",
|
|
13773
|
+
text: part.text,
|
|
13774
|
+
// For text parts, only use part-specific cache control
|
|
13775
|
+
cache_control: (_a2 = getCacheControl(part.providerMetadata)) != null ? _a2 : messageCacheControl
|
|
13776
|
+
};
|
|
13777
|
+
case "image":
|
|
13778
|
+
return {
|
|
13779
|
+
type: "image_url",
|
|
13780
|
+
image_url: {
|
|
13781
|
+
url: part.image instanceof URL ? part.image.toString() : `data:${(_b2 = part.mimeType) != null ? _b2 : "image/jpeg"};base64,${convertUint8ArrayToBase64(
|
|
13782
|
+
part.image
|
|
13783
|
+
)}`
|
|
13784
|
+
},
|
|
13785
|
+
// For image parts, use part-specific or message-level cache control
|
|
13786
|
+
cache_control: (_c2 = getCacheControl(part.providerMetadata)) != null ? _c2 : messageCacheControl
|
|
13787
|
+
};
|
|
13788
|
+
case "file":
|
|
13789
|
+
return {
|
|
13790
|
+
type: "text",
|
|
13791
|
+
text: part.data instanceof URL ? part.data.toString() : part.data,
|
|
13792
|
+
cache_control: (_d = getCacheControl(part.providerMetadata)) != null ? _d : messageCacheControl
|
|
13793
|
+
};
|
|
13794
|
+
default: {
|
|
13795
|
+
const _exhaustiveCheck = part;
|
|
13796
|
+
throw new Error(
|
|
13797
|
+
`Unsupported content part type: ${_exhaustiveCheck}`
|
|
13798
|
+
);
|
|
13799
|
+
}
|
|
13800
|
+
}
|
|
13801
|
+
}
|
|
13802
|
+
);
|
|
13803
|
+
messages.push({
|
|
13804
|
+
role: "user",
|
|
13805
|
+
content: contentParts
|
|
13806
|
+
});
|
|
13807
|
+
break;
|
|
13808
|
+
}
|
|
13809
|
+
case "assistant": {
|
|
13810
|
+
let text = "";
|
|
13811
|
+
const toolCalls = [];
|
|
13812
|
+
for (const part of content) {
|
|
13813
|
+
switch (part.type) {
|
|
13814
|
+
case "text": {
|
|
13815
|
+
text += part.text;
|
|
13816
|
+
break;
|
|
13817
|
+
}
|
|
13818
|
+
case "tool-call": {
|
|
13819
|
+
toolCalls.push({
|
|
13820
|
+
id: part.toolCallId,
|
|
13821
|
+
type: "function",
|
|
13822
|
+
function: {
|
|
13823
|
+
name: part.toolName,
|
|
13824
|
+
arguments: JSON.stringify(part.args)
|
|
13825
|
+
}
|
|
13826
|
+
});
|
|
13827
|
+
break;
|
|
13828
|
+
}
|
|
13829
|
+
// TODO: Handle reasoning and redacted-reasoning
|
|
13830
|
+
case "reasoning":
|
|
13831
|
+
case "redacted-reasoning":
|
|
13832
|
+
break;
|
|
13833
|
+
default: {
|
|
13834
|
+
const _exhaustiveCheck = part;
|
|
13835
|
+
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
|
13836
|
+
}
|
|
13837
|
+
}
|
|
13838
|
+
}
|
|
13839
|
+
messages.push({
|
|
13840
|
+
role: "assistant",
|
|
13841
|
+
content: text,
|
|
13842
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
13843
|
+
cache_control: getCacheControl(providerMetadata)
|
|
13844
|
+
});
|
|
13845
|
+
break;
|
|
13846
|
+
}
|
|
13847
|
+
case "tool": {
|
|
13848
|
+
for (const toolResponse of content) {
|
|
13849
|
+
messages.push({
|
|
13850
|
+
role: "tool",
|
|
13851
|
+
tool_call_id: toolResponse.toolCallId,
|
|
13852
|
+
content: JSON.stringify(toolResponse.result),
|
|
13853
|
+
cache_control: (_c = getCacheControl(providerMetadata)) != null ? _c : getCacheControl(toolResponse.providerMetadata)
|
|
13854
|
+
});
|
|
13855
|
+
}
|
|
13856
|
+
break;
|
|
13857
|
+
}
|
|
13858
|
+
default: {
|
|
13859
|
+
const _exhaustiveCheck = role;
|
|
13860
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
13861
|
+
}
|
|
13862
|
+
}
|
|
13863
|
+
}
|
|
13864
|
+
return messages;
|
|
13865
|
+
}
|
|
13866
|
+
|
|
13867
|
+
// src/map-openrouter-chat-logprobs.ts
|
|
13868
|
+
function mapOpenRouterChatLogProbsOutput(logprobs) {
|
|
13869
|
+
var _a, _b;
|
|
13870
|
+
return (_b = (_a = logprobs == null ? void 0 : logprobs.content) == null ? void 0 : _a.map(({ token, logprob, top_logprobs }) => ({
|
|
13871
|
+
token,
|
|
13872
|
+
logprob,
|
|
13873
|
+
topLogprobs: top_logprobs ? top_logprobs.map(({ token: token2, logprob: logprob2 }) => ({
|
|
13874
|
+
token: token2,
|
|
13875
|
+
logprob: logprob2
|
|
13876
|
+
})) : []
|
|
13877
|
+
}))) != null ? _b : void 0;
|
|
13878
|
+
}
|
|
13879
|
+
|
|
13880
|
+
// src/map-openrouter-finish-reason.ts
|
|
13881
|
+
function mapOpenRouterFinishReason(finishReason) {
|
|
13882
|
+
switch (finishReason) {
|
|
13883
|
+
case "stop":
|
|
13884
|
+
return "stop";
|
|
13885
|
+
case "length":
|
|
13886
|
+
return "length";
|
|
13887
|
+
case "content_filter":
|
|
13888
|
+
return "content-filter";
|
|
13889
|
+
case "function_call":
|
|
13890
|
+
case "tool_calls":
|
|
13891
|
+
return "tool-calls";
|
|
13892
|
+
default:
|
|
13893
|
+
return "unknown";
|
|
13894
|
+
}
|
|
13895
|
+
}
|
|
13896
|
+
var OpenRouterErrorResponseSchema = z.object({
|
|
13897
|
+
error: z.object({
|
|
13898
|
+
message: z.string(),
|
|
13899
|
+
type: z.string(),
|
|
13900
|
+
param: z.any().nullable(),
|
|
13901
|
+
code: z.string().nullable()
|
|
13902
|
+
})
|
|
13903
|
+
});
|
|
13904
|
+
var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
13905
|
+
errorSchema: OpenRouterErrorResponseSchema,
|
|
13906
|
+
errorToMessage: (data) => data.error.message
|
|
13907
|
+
});
|
|
13908
|
+
|
|
13909
|
+
// src/openrouter-chat-language-model.ts
|
|
13910
|
+
function isFunctionTool(tool) {
|
|
13911
|
+
return "parameters" in tool;
|
|
13912
|
+
}
|
|
13913
|
+
var OpenRouterChatLanguageModel = class {
|
|
13914
|
+
constructor(modelId, settings, config) {
|
|
13915
|
+
this.specificationVersion = "v1";
|
|
13916
|
+
this.defaultObjectGenerationMode = "tool";
|
|
13917
|
+
this.modelId = modelId;
|
|
13918
|
+
this.settings = settings;
|
|
13919
|
+
this.config = config;
|
|
13920
|
+
}
|
|
13921
|
+
get provider() {
|
|
13922
|
+
return this.config.provider;
|
|
13923
|
+
}
|
|
13924
|
+
getArgs({
|
|
13925
|
+
mode,
|
|
13926
|
+
prompt,
|
|
13927
|
+
maxTokens,
|
|
13928
|
+
temperature,
|
|
13929
|
+
topP,
|
|
13930
|
+
frequencyPenalty,
|
|
13931
|
+
presencePenalty,
|
|
13932
|
+
seed,
|
|
13933
|
+
stopSequences,
|
|
13934
|
+
responseFormat,
|
|
13935
|
+
topK,
|
|
13936
|
+
providerMetadata
|
|
13937
|
+
}) {
|
|
13938
|
+
var _a;
|
|
13939
|
+
const type = mode.type;
|
|
13940
|
+
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata["openrouter"]) != null ? _a : {};
|
|
13941
|
+
const baseArgs = __spreadValues(__spreadValues(__spreadValues({
|
|
13942
|
+
// model id:
|
|
13943
|
+
model: this.modelId,
|
|
13944
|
+
models: this.settings.models,
|
|
13945
|
+
// model specific settings:
|
|
13946
|
+
logit_bias: this.settings.logitBias,
|
|
13947
|
+
logprobs: this.settings.logprobs === true || typeof this.settings.logprobs === "number" ? true : void 0,
|
|
13948
|
+
top_logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
|
|
13949
|
+
user: this.settings.user,
|
|
13950
|
+
parallel_tool_calls: this.settings.parallelToolCalls,
|
|
13951
|
+
// standardized settings:
|
|
13952
|
+
max_tokens: maxTokens,
|
|
13953
|
+
temperature,
|
|
13954
|
+
top_p: topP,
|
|
13955
|
+
frequency_penalty: frequencyPenalty,
|
|
13956
|
+
presence_penalty: presencePenalty,
|
|
13957
|
+
seed,
|
|
13958
|
+
stop: stopSequences,
|
|
13959
|
+
response_format: responseFormat,
|
|
13960
|
+
top_k: topK,
|
|
13961
|
+
// messages:
|
|
13962
|
+
messages: convertToOpenRouterChatMessages(prompt),
|
|
13963
|
+
// OpenRouter specific settings:
|
|
13964
|
+
include_reasoning: this.settings.includeReasoning,
|
|
13965
|
+
reasoning: this.settings.reasoning
|
|
13966
|
+
}, this.config.extraBody), this.settings.extraBody), extraCallingBody);
|
|
13967
|
+
switch (type) {
|
|
13968
|
+
case "regular": {
|
|
13969
|
+
return __spreadValues(__spreadValues({}, baseArgs), prepareToolsAndToolChoice(mode));
|
|
13970
|
+
}
|
|
13971
|
+
case "object-json": {
|
|
13972
|
+
return __spreadProps(__spreadValues({}, baseArgs), {
|
|
13973
|
+
response_format: { type: "json_object" }
|
|
13974
|
+
});
|
|
13975
|
+
}
|
|
13976
|
+
case "object-tool": {
|
|
13977
|
+
return __spreadProps(__spreadValues({}, baseArgs), {
|
|
13978
|
+
tool_choice: { type: "function", function: { name: mode.tool.name } },
|
|
13979
|
+
tools: [
|
|
13980
|
+
{
|
|
13981
|
+
type: "function",
|
|
13982
|
+
function: {
|
|
13983
|
+
name: mode.tool.name,
|
|
13984
|
+
description: mode.tool.description,
|
|
13985
|
+
parameters: mode.tool.parameters
|
|
13986
|
+
}
|
|
13987
|
+
}
|
|
13988
|
+
]
|
|
13989
|
+
});
|
|
13990
|
+
}
|
|
13991
|
+
// Handle all non-text types with a single default case
|
|
13992
|
+
default: {
|
|
13993
|
+
const _exhaustiveCheck = type;
|
|
13994
|
+
throw new UnsupportedFunctionalityError({
|
|
13995
|
+
functionality: `${_exhaustiveCheck} mode`
|
|
13996
|
+
});
|
|
13997
|
+
}
|
|
13998
|
+
}
|
|
13999
|
+
}
|
|
14000
|
+
async doGenerate(options) {
|
|
14001
|
+
var _b, _c, _d, _e, _f, _g, _h;
|
|
14002
|
+
const args = this.getArgs(options);
|
|
14003
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
14004
|
+
url: this.config.url({
|
|
14005
|
+
path: "/chat/completions",
|
|
14006
|
+
modelId: this.modelId
|
|
14007
|
+
}),
|
|
14008
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
14009
|
+
body: args,
|
|
14010
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
14011
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
14012
|
+
OpenRouterNonStreamChatCompletionResponseSchema
|
|
14013
|
+
),
|
|
14014
|
+
abortSignal: options.abortSignal,
|
|
14015
|
+
fetch: this.config.fetch
|
|
14016
|
+
});
|
|
14017
|
+
const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
|
|
14018
|
+
const choice = response.choices[0];
|
|
14019
|
+
if (!choice) {
|
|
14020
|
+
throw new Error("No choice in response");
|
|
14021
|
+
}
|
|
14022
|
+
return {
|
|
14023
|
+
response: {
|
|
14024
|
+
id: response.id,
|
|
14025
|
+
modelId: response.model
|
|
14026
|
+
},
|
|
14027
|
+
text: (_b = choice.message.content) != null ? _b : void 0,
|
|
14028
|
+
reasoning: (_c = choice.message.reasoning) != null ? _c : void 0,
|
|
14029
|
+
toolCalls: (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
14030
|
+
var _a2;
|
|
14031
|
+
return {
|
|
14032
|
+
toolCallType: "function",
|
|
14033
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
14034
|
+
toolName: toolCall.function.name,
|
|
14035
|
+
args: toolCall.function.arguments
|
|
14036
|
+
};
|
|
14037
|
+
}),
|
|
14038
|
+
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
14039
|
+
usage: {
|
|
14040
|
+
promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : 0,
|
|
14041
|
+
completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0
|
|
14042
|
+
},
|
|
14043
|
+
rawCall: { rawPrompt, rawSettings },
|
|
14044
|
+
rawResponse: { headers: responseHeaders },
|
|
14045
|
+
warnings: [],
|
|
14046
|
+
logprobs: mapOpenRouterChatLogProbsOutput(choice.logprobs)
|
|
14047
|
+
};
|
|
14048
|
+
}
|
|
14049
|
+
async doStream(options) {
|
|
14050
|
+
const args = this.getArgs(options);
|
|
14051
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
14052
|
+
url: this.config.url({
|
|
14053
|
+
path: "/chat/completions",
|
|
14054
|
+
modelId: this.modelId
|
|
14055
|
+
}),
|
|
14056
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
14057
|
+
body: __spreadProps(__spreadValues({}, args), {
|
|
14058
|
+
stream: true,
|
|
14059
|
+
// only include stream_options when in strict compatibility mode:
|
|
14060
|
+
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
|
|
14061
|
+
}),
|
|
14062
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
14063
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
14064
|
+
OpenRouterStreamChatCompletionChunkSchema
|
|
14065
|
+
),
|
|
14066
|
+
abortSignal: options.abortSignal,
|
|
14067
|
+
fetch: this.config.fetch
|
|
14068
|
+
});
|
|
14069
|
+
const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
|
|
14070
|
+
const toolCalls = [];
|
|
14071
|
+
let finishReason = "other";
|
|
14072
|
+
let usage = {
|
|
14073
|
+
promptTokens: Number.NaN,
|
|
14074
|
+
completionTokens: Number.NaN
|
|
14075
|
+
};
|
|
14076
|
+
let logprobs;
|
|
14077
|
+
return {
|
|
14078
|
+
stream: response.pipeThrough(
|
|
14079
|
+
new TransformStream({
|
|
14080
|
+
transform(chunk, controller) {
|
|
14081
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
14082
|
+
if (!chunk.success) {
|
|
14083
|
+
finishReason = "error";
|
|
14084
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
14085
|
+
return;
|
|
14086
|
+
}
|
|
14087
|
+
const value = chunk.value;
|
|
14088
|
+
if ("error" in value) {
|
|
14089
|
+
finishReason = "error";
|
|
14090
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
14091
|
+
return;
|
|
14092
|
+
}
|
|
14093
|
+
if (value.id) {
|
|
14094
|
+
controller.enqueue({
|
|
14095
|
+
type: "response-metadata",
|
|
14096
|
+
id: value.id
|
|
14097
|
+
});
|
|
14098
|
+
}
|
|
14099
|
+
if (value.model) {
|
|
14100
|
+
controller.enqueue({
|
|
14101
|
+
type: "response-metadata",
|
|
14102
|
+
modelId: value.model
|
|
14103
|
+
});
|
|
14104
|
+
}
|
|
14105
|
+
if (value.usage != null) {
|
|
14106
|
+
usage = {
|
|
14107
|
+
promptTokens: value.usage.prompt_tokens,
|
|
14108
|
+
completionTokens: value.usage.completion_tokens
|
|
14109
|
+
};
|
|
14110
|
+
}
|
|
14111
|
+
const choice = value.choices[0];
|
|
14112
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
14113
|
+
finishReason = mapOpenRouterFinishReason(choice.finish_reason);
|
|
14114
|
+
}
|
|
14115
|
+
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
14116
|
+
return;
|
|
14117
|
+
}
|
|
14118
|
+
const delta = choice.delta;
|
|
14119
|
+
if (delta.content != null) {
|
|
14120
|
+
controller.enqueue({
|
|
14121
|
+
type: "text-delta",
|
|
14122
|
+
textDelta: delta.content
|
|
14123
|
+
});
|
|
14124
|
+
}
|
|
14125
|
+
if (delta.reasoning != null) {
|
|
14126
|
+
controller.enqueue({
|
|
14127
|
+
type: "reasoning",
|
|
14128
|
+
textDelta: delta.reasoning
|
|
14129
|
+
});
|
|
14130
|
+
}
|
|
14131
|
+
const mappedLogprobs = mapOpenRouterChatLogProbsOutput(
|
|
14132
|
+
choice == null ? void 0 : choice.logprobs
|
|
14133
|
+
);
|
|
14134
|
+
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
14135
|
+
if (logprobs === void 0) logprobs = [];
|
|
14136
|
+
logprobs.push(...mappedLogprobs);
|
|
14137
|
+
}
|
|
14138
|
+
if (delta.tool_calls != null) {
|
|
14139
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
14140
|
+
const index = toolCallDelta.index;
|
|
14141
|
+
if (toolCalls[index] == null) {
|
|
14142
|
+
if (toolCallDelta.type !== "function") {
|
|
14143
|
+
throw new InvalidResponseDataError({
|
|
14144
|
+
data: toolCallDelta,
|
|
14145
|
+
message: `Expected 'function' type.`
|
|
14146
|
+
});
|
|
14147
|
+
}
|
|
14148
|
+
if (toolCallDelta.id == null) {
|
|
14149
|
+
throw new InvalidResponseDataError({
|
|
14150
|
+
data: toolCallDelta,
|
|
14151
|
+
message: `Expected 'id' to be a string.`
|
|
14152
|
+
});
|
|
14153
|
+
}
|
|
14154
|
+
if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
|
|
14155
|
+
throw new InvalidResponseDataError({
|
|
14156
|
+
data: toolCallDelta,
|
|
14157
|
+
message: `Expected 'function.name' to be a string.`
|
|
14158
|
+
});
|
|
14159
|
+
}
|
|
14160
|
+
toolCalls[index] = {
|
|
14161
|
+
id: toolCallDelta.id,
|
|
14162
|
+
type: "function",
|
|
14163
|
+
function: {
|
|
14164
|
+
name: toolCallDelta.function.name,
|
|
14165
|
+
arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
|
|
14166
|
+
},
|
|
14167
|
+
sent: false
|
|
14168
|
+
};
|
|
14169
|
+
const toolCall2 = toolCalls[index];
|
|
14170
|
+
if (toolCall2 == null) {
|
|
14171
|
+
throw new Error("Tool call is missing");
|
|
14172
|
+
}
|
|
14173
|
+
if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
14174
|
+
controller.enqueue({
|
|
14175
|
+
type: "tool-call-delta",
|
|
14176
|
+
toolCallType: "function",
|
|
14177
|
+
toolCallId: toolCall2.id,
|
|
14178
|
+
toolName: toolCall2.function.name,
|
|
14179
|
+
argsTextDelta: toolCall2.function.arguments
|
|
14180
|
+
});
|
|
14181
|
+
controller.enqueue({
|
|
14182
|
+
type: "tool-call",
|
|
14183
|
+
toolCallType: "function",
|
|
14184
|
+
toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
|
|
14185
|
+
toolName: toolCall2.function.name,
|
|
14186
|
+
args: toolCall2.function.arguments
|
|
14187
|
+
});
|
|
14188
|
+
toolCall2.sent = true;
|
|
14189
|
+
}
|
|
14190
|
+
continue;
|
|
14191
|
+
}
|
|
14192
|
+
const toolCall = toolCalls[index];
|
|
14193
|
+
if (toolCall == null) {
|
|
14194
|
+
throw new Error("Tool call is missing");
|
|
14195
|
+
}
|
|
14196
|
+
if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
|
|
14197
|
+
toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
|
|
14198
|
+
}
|
|
14199
|
+
controller.enqueue({
|
|
14200
|
+
type: "tool-call-delta",
|
|
14201
|
+
toolCallType: "function",
|
|
14202
|
+
toolCallId: toolCall.id,
|
|
14203
|
+
toolName: toolCall.function.name,
|
|
14204
|
+
argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
|
|
14205
|
+
});
|
|
14206
|
+
if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
14207
|
+
controller.enqueue({
|
|
14208
|
+
type: "tool-call",
|
|
14209
|
+
toolCallType: "function",
|
|
14210
|
+
toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
|
|
14211
|
+
toolName: toolCall.function.name,
|
|
14212
|
+
args: toolCall.function.arguments
|
|
14213
|
+
});
|
|
14214
|
+
toolCall.sent = true;
|
|
14215
|
+
}
|
|
14216
|
+
}
|
|
14217
|
+
}
|
|
14218
|
+
},
|
|
14219
|
+
flush(controller) {
|
|
14220
|
+
var _a2;
|
|
14221
|
+
if (finishReason === "tool-calls") {
|
|
14222
|
+
for (const toolCall of toolCalls) {
|
|
14223
|
+
if (!toolCall.sent) {
|
|
14224
|
+
controller.enqueue({
|
|
14225
|
+
type: "tool-call",
|
|
14226
|
+
toolCallType: "function",
|
|
14227
|
+
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
14228
|
+
toolName: toolCall.function.name,
|
|
14229
|
+
// Coerce invalid arguments to an empty JSON object
|
|
14230
|
+
args: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}"
|
|
14231
|
+
});
|
|
14232
|
+
toolCall.sent = true;
|
|
14233
|
+
}
|
|
14234
|
+
}
|
|
14235
|
+
}
|
|
14236
|
+
controller.enqueue({
|
|
14237
|
+
type: "finish",
|
|
14238
|
+
finishReason,
|
|
14239
|
+
logprobs,
|
|
14240
|
+
usage
|
|
14241
|
+
});
|
|
14242
|
+
}
|
|
14243
|
+
})
|
|
14244
|
+
),
|
|
14245
|
+
rawCall: { rawPrompt, rawSettings },
|
|
14246
|
+
rawResponse: { headers: responseHeaders },
|
|
14247
|
+
warnings: []
|
|
14248
|
+
};
|
|
14249
|
+
}
|
|
14250
|
+
};
|
|
14251
|
+
var OpenRouterChatCompletionBaseResponseSchema = z.object({
|
|
14252
|
+
id: z.string().optional(),
|
|
14253
|
+
model: z.string().optional(),
|
|
14254
|
+
usage: z.object({
|
|
14255
|
+
prompt_tokens: z.number(),
|
|
14256
|
+
completion_tokens: z.number(),
|
|
14257
|
+
total_tokens: z.number()
|
|
14258
|
+
}).nullish()
|
|
14259
|
+
});
|
|
14260
|
+
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
14261
|
+
choices: z.array(
|
|
14262
|
+
z.object({
|
|
14263
|
+
message: z.object({
|
|
14264
|
+
role: z.literal("assistant"),
|
|
14265
|
+
content: z.string().nullable().optional(),
|
|
14266
|
+
reasoning: z.string().nullable().optional(),
|
|
14267
|
+
tool_calls: z.array(
|
|
14268
|
+
z.object({
|
|
14269
|
+
id: z.string().optional().nullable(),
|
|
14270
|
+
type: z.literal("function"),
|
|
14271
|
+
function: z.object({
|
|
14272
|
+
name: z.string(),
|
|
14273
|
+
arguments: z.string()
|
|
14274
|
+
})
|
|
14275
|
+
})
|
|
14276
|
+
).optional()
|
|
14277
|
+
}),
|
|
14278
|
+
index: z.number(),
|
|
14279
|
+
logprobs: z.object({
|
|
14280
|
+
content: z.array(
|
|
14281
|
+
z.object({
|
|
14282
|
+
token: z.string(),
|
|
14283
|
+
logprob: z.number(),
|
|
14284
|
+
top_logprobs: z.array(
|
|
14285
|
+
z.object({
|
|
14286
|
+
token: z.string(),
|
|
14287
|
+
logprob: z.number()
|
|
14288
|
+
})
|
|
14289
|
+
)
|
|
14290
|
+
})
|
|
14291
|
+
).nullable()
|
|
14292
|
+
}).nullable().optional(),
|
|
14293
|
+
finish_reason: z.string().optional().nullable()
|
|
14294
|
+
})
|
|
14295
|
+
)
|
|
14296
|
+
});
|
|
14297
|
+
var OpenRouterStreamChatCompletionChunkSchema = z.union([
|
|
14298
|
+
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
14299
|
+
choices: z.array(
|
|
14300
|
+
z.object({
|
|
14301
|
+
delta: z.object({
|
|
14302
|
+
role: z.enum(["assistant"]).optional(),
|
|
14303
|
+
content: z.string().nullish(),
|
|
14304
|
+
reasoning: z.string().nullish().optional(),
|
|
14305
|
+
tool_calls: z.array(
|
|
14306
|
+
z.object({
|
|
14307
|
+
index: z.number(),
|
|
14308
|
+
id: z.string().nullish(),
|
|
14309
|
+
type: z.literal("function").optional(),
|
|
14310
|
+
function: z.object({
|
|
14311
|
+
name: z.string().nullish(),
|
|
14312
|
+
arguments: z.string().nullish()
|
|
14313
|
+
})
|
|
14314
|
+
})
|
|
14315
|
+
).nullish()
|
|
14316
|
+
}).nullish(),
|
|
14317
|
+
logprobs: z.object({
|
|
14318
|
+
content: z.array(
|
|
14319
|
+
z.object({
|
|
14320
|
+
token: z.string(),
|
|
14321
|
+
logprob: z.number(),
|
|
14322
|
+
top_logprobs: z.array(
|
|
14323
|
+
z.object({
|
|
14324
|
+
token: z.string(),
|
|
14325
|
+
logprob: z.number()
|
|
14326
|
+
})
|
|
14327
|
+
)
|
|
14328
|
+
})
|
|
14329
|
+
).nullable()
|
|
14330
|
+
}).nullish(),
|
|
14331
|
+
finish_reason: z.string().nullable().optional(),
|
|
14332
|
+
index: z.number()
|
|
14333
|
+
})
|
|
14334
|
+
)
|
|
14335
|
+
}),
|
|
14336
|
+
OpenRouterErrorResponseSchema
|
|
14337
|
+
]);
|
|
14338
|
+
function prepareToolsAndToolChoice(mode) {
|
|
14339
|
+
var _a;
|
|
14340
|
+
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
14341
|
+
if (tools == null) {
|
|
14342
|
+
return { tools: void 0, tool_choice: void 0 };
|
|
14343
|
+
}
|
|
14344
|
+
const mappedTools = tools.map((tool) => {
|
|
14345
|
+
if (isFunctionTool(tool)) {
|
|
14346
|
+
return {
|
|
14347
|
+
type: "function",
|
|
14348
|
+
function: {
|
|
14349
|
+
name: tool.name,
|
|
14350
|
+
description: tool.description,
|
|
14351
|
+
parameters: tool.parameters
|
|
14352
|
+
}
|
|
14353
|
+
};
|
|
14354
|
+
} else {
|
|
14355
|
+
return {
|
|
14356
|
+
type: "function",
|
|
14357
|
+
function: {
|
|
14358
|
+
name: tool.name
|
|
14359
|
+
}
|
|
14360
|
+
};
|
|
14361
|
+
}
|
|
14362
|
+
});
|
|
14363
|
+
const toolChoice = mode.toolChoice;
|
|
14364
|
+
if (toolChoice == null) {
|
|
14365
|
+
return { tools: mappedTools, tool_choice: void 0 };
|
|
14366
|
+
}
|
|
14367
|
+
const type = toolChoice.type;
|
|
14368
|
+
switch (type) {
|
|
14369
|
+
case "auto":
|
|
14370
|
+
case "none":
|
|
14371
|
+
case "required":
|
|
14372
|
+
return { tools: mappedTools, tool_choice: type };
|
|
14373
|
+
case "tool":
|
|
14374
|
+
return {
|
|
14375
|
+
tools: mappedTools,
|
|
14376
|
+
tool_choice: {
|
|
14377
|
+
type: "function",
|
|
14378
|
+
function: {
|
|
14379
|
+
name: toolChoice.toolName
|
|
14380
|
+
}
|
|
14381
|
+
}
|
|
14382
|
+
};
|
|
14383
|
+
default: {
|
|
14384
|
+
const _exhaustiveCheck = type;
|
|
14385
|
+
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
|
|
14386
|
+
}
|
|
14387
|
+
}
|
|
14388
|
+
}
|
|
14389
|
+
function convertToOpenRouterCompletionPrompt({
|
|
14390
|
+
prompt,
|
|
14391
|
+
inputFormat,
|
|
14392
|
+
user = "user",
|
|
14393
|
+
assistant = "assistant"
|
|
14394
|
+
}) {
|
|
14395
|
+
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") {
|
|
14396
|
+
return { prompt: prompt[0].content[0].text };
|
|
14397
|
+
}
|
|
14398
|
+
let text = "";
|
|
14399
|
+
if (prompt[0] && prompt[0].role === "system") {
|
|
14400
|
+
text += `${prompt[0].content}
|
|
14401
|
+
|
|
14402
|
+
`;
|
|
14403
|
+
prompt = prompt.slice(1);
|
|
14404
|
+
}
|
|
14405
|
+
for (const { role, content } of prompt) {
|
|
14406
|
+
switch (role) {
|
|
14407
|
+
case "system": {
|
|
14408
|
+
throw new InvalidPromptError({
|
|
14409
|
+
message: "Unexpected system message in prompt: ${content}",
|
|
14410
|
+
prompt
|
|
14411
|
+
});
|
|
14412
|
+
}
|
|
14413
|
+
case "user": {
|
|
14414
|
+
const userMessage = content.map((part) => {
|
|
14415
|
+
switch (part.type) {
|
|
14416
|
+
case "text": {
|
|
14417
|
+
return part.text;
|
|
14418
|
+
}
|
|
14419
|
+
case "image": {
|
|
14420
|
+
throw new UnsupportedFunctionalityError({
|
|
14421
|
+
functionality: "images"
|
|
14422
|
+
});
|
|
14423
|
+
}
|
|
14424
|
+
case "file": {
|
|
14425
|
+
throw new UnsupportedFunctionalityError({
|
|
14426
|
+
functionality: "file attachments"
|
|
14427
|
+
});
|
|
14428
|
+
}
|
|
14429
|
+
default: {
|
|
14430
|
+
const _exhaustiveCheck = part;
|
|
14431
|
+
throw new Error(
|
|
14432
|
+
`Unsupported content type: ${_exhaustiveCheck}`
|
|
14433
|
+
);
|
|
14434
|
+
}
|
|
14435
|
+
}
|
|
14436
|
+
}).join("");
|
|
14437
|
+
text += `${user}:
|
|
14438
|
+
${userMessage}
|
|
14439
|
+
|
|
14440
|
+
`;
|
|
14441
|
+
break;
|
|
14442
|
+
}
|
|
14443
|
+
case "assistant": {
|
|
14444
|
+
const assistantMessage = content.map((part) => {
|
|
14445
|
+
switch (part.type) {
|
|
14446
|
+
case "text": {
|
|
14447
|
+
return part.text;
|
|
14448
|
+
}
|
|
14449
|
+
case "tool-call": {
|
|
14450
|
+
throw new UnsupportedFunctionalityError({
|
|
14451
|
+
functionality: "tool-call messages"
|
|
14452
|
+
});
|
|
14453
|
+
}
|
|
14454
|
+
case "reasoning": {
|
|
14455
|
+
throw new UnsupportedFunctionalityError({
|
|
14456
|
+
functionality: "reasoning messages"
|
|
14457
|
+
});
|
|
14458
|
+
}
|
|
14459
|
+
case "redacted-reasoning": {
|
|
14460
|
+
throw new UnsupportedFunctionalityError({
|
|
14461
|
+
functionality: "redacted reasoning messages"
|
|
14462
|
+
});
|
|
14463
|
+
}
|
|
14464
|
+
default: {
|
|
14465
|
+
const _exhaustiveCheck = part;
|
|
14466
|
+
throw new Error(
|
|
14467
|
+
`Unsupported content type: ${_exhaustiveCheck}`
|
|
14468
|
+
);
|
|
14469
|
+
}
|
|
14470
|
+
}
|
|
14471
|
+
}).join("");
|
|
14472
|
+
text += `${assistant}:
|
|
14473
|
+
${assistantMessage}
|
|
14474
|
+
|
|
14475
|
+
`;
|
|
14476
|
+
break;
|
|
14477
|
+
}
|
|
14478
|
+
case "tool": {
|
|
14479
|
+
throw new UnsupportedFunctionalityError({
|
|
14480
|
+
functionality: "tool messages"
|
|
14481
|
+
});
|
|
14482
|
+
}
|
|
14483
|
+
default: {
|
|
14484
|
+
const _exhaustiveCheck = role;
|
|
14485
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
14486
|
+
}
|
|
14487
|
+
}
|
|
14488
|
+
}
|
|
14489
|
+
text += `${assistant}:
|
|
14490
|
+
`;
|
|
14491
|
+
return {
|
|
14492
|
+
prompt: text
|
|
14493
|
+
};
|
|
14494
|
+
}
|
|
14495
|
+
|
|
14496
|
+
// src/map-openrouter-completion-logprobs.ts
|
|
14497
|
+
function mapOpenRouterCompletionLogProbs(logprobs) {
|
|
14498
|
+
return logprobs == null ? void 0 : logprobs.tokens.map((token, index) => {
|
|
14499
|
+
var _a, _b;
|
|
14500
|
+
return {
|
|
14501
|
+
token,
|
|
14502
|
+
logprob: (_a = logprobs.token_logprobs[index]) != null ? _a : 0,
|
|
14503
|
+
topLogprobs: logprobs.top_logprobs ? Object.entries((_b = logprobs.top_logprobs[index]) != null ? _b : {}).map(
|
|
14504
|
+
([token2, logprob]) => ({
|
|
14505
|
+
token: token2,
|
|
14506
|
+
logprob
|
|
14507
|
+
})
|
|
14508
|
+
) : []
|
|
14509
|
+
};
|
|
14510
|
+
});
|
|
14511
|
+
}
|
|
14512
|
+
|
|
14513
|
+
// src/openrouter-completion-language-model.ts
|
|
14514
|
+
var OpenRouterCompletionLanguageModel = class {
|
|
14515
|
+
constructor(modelId, settings, config) {
|
|
14516
|
+
this.specificationVersion = "v1";
|
|
14517
|
+
this.defaultObjectGenerationMode = void 0;
|
|
14518
|
+
this.modelId = modelId;
|
|
14519
|
+
this.settings = settings;
|
|
14520
|
+
this.config = config;
|
|
14521
|
+
}
|
|
14522
|
+
get provider() {
|
|
14523
|
+
return this.config.provider;
|
|
14524
|
+
}
|
|
14525
|
+
getArgs({
|
|
14526
|
+
mode,
|
|
14527
|
+
inputFormat,
|
|
14528
|
+
prompt,
|
|
14529
|
+
maxTokens,
|
|
14530
|
+
temperature,
|
|
14531
|
+
topP,
|
|
14532
|
+
frequencyPenalty,
|
|
14533
|
+
presencePenalty,
|
|
14534
|
+
seed,
|
|
14535
|
+
responseFormat,
|
|
14536
|
+
topK,
|
|
14537
|
+
stopSequences,
|
|
14538
|
+
providerMetadata
|
|
14539
|
+
}) {
|
|
14540
|
+
var _a, _b;
|
|
14541
|
+
const type = mode.type;
|
|
14542
|
+
const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata["openrouter"]) != null ? _a : {};
|
|
14543
|
+
const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
|
|
14544
|
+
prompt,
|
|
14545
|
+
inputFormat
|
|
14546
|
+
});
|
|
14547
|
+
const baseArgs = __spreadValues(__spreadValues(__spreadValues({
|
|
14548
|
+
// model id:
|
|
14549
|
+
model: this.modelId,
|
|
14550
|
+
models: this.settings.models,
|
|
14551
|
+
// model specific settings:
|
|
14552
|
+
logit_bias: this.settings.logitBias,
|
|
14553
|
+
logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
|
|
14554
|
+
suffix: this.settings.suffix,
|
|
14555
|
+
user: this.settings.user,
|
|
14556
|
+
// standardized settings:
|
|
14557
|
+
max_tokens: maxTokens,
|
|
14558
|
+
temperature,
|
|
14559
|
+
top_p: topP,
|
|
14560
|
+
frequency_penalty: frequencyPenalty,
|
|
14561
|
+
presence_penalty: presencePenalty,
|
|
14562
|
+
seed,
|
|
14563
|
+
stop: stopSequences,
|
|
14564
|
+
response_format: responseFormat,
|
|
14565
|
+
top_k: topK,
|
|
14566
|
+
// prompt:
|
|
14567
|
+
prompt: completionPrompt,
|
|
14568
|
+
// OpenRouter specific settings:
|
|
14569
|
+
include_reasoning: this.settings.includeReasoning,
|
|
14570
|
+
reasoning: this.settings.reasoning
|
|
14571
|
+
}, this.config.extraBody), this.settings.extraBody), extraCallingBody);
|
|
14572
|
+
switch (type) {
|
|
14573
|
+
case "regular": {
|
|
14574
|
+
if ((_b = mode.tools) == null ? void 0 : _b.length) {
|
|
14575
|
+
throw new UnsupportedFunctionalityError({
|
|
14576
|
+
functionality: "tools"
|
|
14577
|
+
});
|
|
14578
|
+
}
|
|
14579
|
+
if (mode.toolChoice) {
|
|
14580
|
+
throw new UnsupportedFunctionalityError({
|
|
14581
|
+
functionality: "toolChoice"
|
|
14582
|
+
});
|
|
14583
|
+
}
|
|
14584
|
+
return baseArgs;
|
|
14585
|
+
}
|
|
14586
|
+
case "object-json": {
|
|
14587
|
+
throw new UnsupportedFunctionalityError({
|
|
14588
|
+
functionality: "object-json mode"
|
|
14589
|
+
});
|
|
14590
|
+
}
|
|
14591
|
+
case "object-tool": {
|
|
14592
|
+
throw new UnsupportedFunctionalityError({
|
|
14593
|
+
functionality: "object-tool mode"
|
|
14594
|
+
});
|
|
14595
|
+
}
|
|
14596
|
+
// Handle all non-text types with a single default case
|
|
14597
|
+
default: {
|
|
14598
|
+
const _exhaustiveCheck = type;
|
|
14599
|
+
throw new UnsupportedFunctionalityError({
|
|
14600
|
+
functionality: `${_exhaustiveCheck} mode`
|
|
14601
|
+
});
|
|
14602
|
+
}
|
|
14603
|
+
}
|
|
14604
|
+
}
|
|
14605
|
+
async doGenerate(options) {
|
|
14606
|
+
var _b, _c, _d, _e, _f;
|
|
14607
|
+
const args = this.getArgs(options);
|
|
14608
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
14609
|
+
url: this.config.url({
|
|
14610
|
+
path: "/completions",
|
|
14611
|
+
modelId: this.modelId
|
|
14612
|
+
}),
|
|
14613
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
14614
|
+
body: args,
|
|
14615
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
14616
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
14617
|
+
OpenRouterCompletionChunkSchema
|
|
14618
|
+
),
|
|
14619
|
+
abortSignal: options.abortSignal,
|
|
14620
|
+
fetch: this.config.fetch
|
|
14621
|
+
});
|
|
14622
|
+
const _a = args, { prompt: rawPrompt } = _a, rawSettings = __objRest(_a, ["prompt"]);
|
|
14623
|
+
if ("error" in response) {
|
|
14624
|
+
throw new Error(`${response.error.message}`);
|
|
14625
|
+
}
|
|
14626
|
+
const choice = response.choices[0];
|
|
14627
|
+
if (!choice) {
|
|
14628
|
+
throw new Error("No choice in OpenRouter completion response");
|
|
14629
|
+
}
|
|
14630
|
+
return {
|
|
14631
|
+
response: {
|
|
14632
|
+
id: response.id,
|
|
14633
|
+
modelId: response.model
|
|
14634
|
+
},
|
|
14635
|
+
text: (_b = choice.text) != null ? _b : "",
|
|
14636
|
+
reasoning: choice.reasoning || void 0,
|
|
14637
|
+
usage: {
|
|
14638
|
+
promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
|
|
14639
|
+
completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0
|
|
14640
|
+
},
|
|
14641
|
+
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
14642
|
+
logprobs: mapOpenRouterCompletionLogProbs(choice.logprobs),
|
|
14643
|
+
rawCall: { rawPrompt, rawSettings },
|
|
14644
|
+
rawResponse: { headers: responseHeaders },
|
|
14645
|
+
warnings: []
|
|
14646
|
+
};
|
|
14647
|
+
}
|
|
14648
|
+
async doStream(options) {
|
|
14649
|
+
const args = this.getArgs(options);
|
|
14650
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
14651
|
+
url: this.config.url({
|
|
14652
|
+
path: "/completions",
|
|
14653
|
+
modelId: this.modelId
|
|
14654
|
+
}),
|
|
14655
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
14656
|
+
body: __spreadProps(__spreadValues({}, this.getArgs(options)), {
|
|
14657
|
+
stream: true,
|
|
14658
|
+
// only include stream_options when in strict compatibility mode:
|
|
14659
|
+
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
|
|
14660
|
+
}),
|
|
14661
|
+
failedResponseHandler: openrouterFailedResponseHandler,
|
|
14662
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
14663
|
+
OpenRouterCompletionChunkSchema
|
|
14664
|
+
),
|
|
14665
|
+
abortSignal: options.abortSignal,
|
|
14666
|
+
fetch: this.config.fetch
|
|
14667
|
+
});
|
|
14668
|
+
const _a = args, { prompt: rawPrompt } = _a, rawSettings = __objRest(_a, ["prompt"]);
|
|
14669
|
+
let finishReason = "other";
|
|
14670
|
+
let usage = {
|
|
14671
|
+
promptTokens: Number.NaN,
|
|
14672
|
+
completionTokens: Number.NaN
|
|
14673
|
+
};
|
|
14674
|
+
let logprobs;
|
|
14675
|
+
return {
|
|
14676
|
+
stream: response.pipeThrough(
|
|
14677
|
+
new TransformStream({
|
|
14678
|
+
transform(chunk, controller) {
|
|
14679
|
+
if (!chunk.success) {
|
|
14680
|
+
finishReason = "error";
|
|
14681
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
14682
|
+
return;
|
|
14683
|
+
}
|
|
14684
|
+
const value = chunk.value;
|
|
14685
|
+
if ("error" in value) {
|
|
14686
|
+
finishReason = "error";
|
|
14687
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
14688
|
+
return;
|
|
14689
|
+
}
|
|
14690
|
+
if (value.usage != null) {
|
|
14691
|
+
usage = {
|
|
14692
|
+
promptTokens: value.usage.prompt_tokens,
|
|
14693
|
+
completionTokens: value.usage.completion_tokens
|
|
14694
|
+
};
|
|
14695
|
+
}
|
|
14696
|
+
const choice = value.choices[0];
|
|
14697
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
14698
|
+
finishReason = mapOpenRouterFinishReason(choice.finish_reason);
|
|
14699
|
+
}
|
|
14700
|
+
if ((choice == null ? void 0 : choice.text) != null) {
|
|
14701
|
+
controller.enqueue({
|
|
14702
|
+
type: "text-delta",
|
|
14703
|
+
textDelta: choice.text
|
|
14704
|
+
});
|
|
14705
|
+
}
|
|
14706
|
+
const mappedLogprobs = mapOpenRouterCompletionLogProbs(
|
|
14707
|
+
choice == null ? void 0 : choice.logprobs
|
|
14708
|
+
);
|
|
14709
|
+
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
14710
|
+
if (logprobs === void 0) logprobs = [];
|
|
14711
|
+
logprobs.push(...mappedLogprobs);
|
|
14712
|
+
}
|
|
14713
|
+
},
|
|
14714
|
+
flush(controller) {
|
|
14715
|
+
controller.enqueue({
|
|
14716
|
+
type: "finish",
|
|
14717
|
+
finishReason,
|
|
14718
|
+
logprobs,
|
|
14719
|
+
usage
|
|
14720
|
+
});
|
|
14721
|
+
}
|
|
14722
|
+
})
|
|
14723
|
+
),
|
|
14724
|
+
rawCall: { rawPrompt, rawSettings },
|
|
14725
|
+
rawResponse: { headers: responseHeaders },
|
|
14726
|
+
warnings: []
|
|
14727
|
+
};
|
|
14728
|
+
}
|
|
14729
|
+
};
|
|
14730
|
+
var OpenRouterCompletionChunkSchema = z.union([
|
|
14731
|
+
z.object({
|
|
14732
|
+
id: z.string().optional(),
|
|
14733
|
+
model: z.string().optional(),
|
|
14734
|
+
choices: z.array(
|
|
14735
|
+
z.object({
|
|
14736
|
+
text: z.string(),
|
|
14737
|
+
reasoning: z.string().nullish().optional(),
|
|
14738
|
+
finish_reason: z.string().nullish(),
|
|
14739
|
+
index: z.number(),
|
|
14740
|
+
logprobs: z.object({
|
|
14741
|
+
tokens: z.array(z.string()),
|
|
14742
|
+
token_logprobs: z.array(z.number()),
|
|
14743
|
+
top_logprobs: z.array(z.record(z.string(), z.number())).nullable()
|
|
14744
|
+
}).nullable().optional()
|
|
14745
|
+
})
|
|
14746
|
+
),
|
|
14747
|
+
usage: z.object({
|
|
14748
|
+
prompt_tokens: z.number(),
|
|
14749
|
+
completion_tokens: z.number()
|
|
14750
|
+
}).optional().nullable()
|
|
14751
|
+
}),
|
|
14752
|
+
OpenRouterErrorResponseSchema
|
|
14753
|
+
]);
|
|
14754
|
+
function createOpenRouter(options = {}) {
|
|
14755
|
+
var _a, _b, _c;
|
|
14756
|
+
const baseURL = (_b = withoutTrailingSlash((_a = options.baseURL) != null ? _a : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
14757
|
+
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
14758
|
+
const getHeaders = () => __spreadValues({
|
|
14759
|
+
Authorization: `Bearer ${loadApiKey({
|
|
14760
|
+
apiKey: options.apiKey,
|
|
14761
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
14762
|
+
description: "OpenRouter"
|
|
14763
|
+
})}`
|
|
14764
|
+
}, options.headers);
|
|
14765
|
+
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
14766
|
+
provider: "openrouter.chat",
|
|
14767
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
14768
|
+
headers: getHeaders,
|
|
14769
|
+
compatibility,
|
|
14770
|
+
fetch: options.fetch,
|
|
14771
|
+
extraBody: options.extraBody
|
|
14772
|
+
});
|
|
14773
|
+
const createCompletionModel = (modelId, settings = {}) => new OpenRouterCompletionLanguageModel(modelId, settings, {
|
|
14774
|
+
provider: "openrouter.completion",
|
|
14775
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
14776
|
+
headers: getHeaders,
|
|
14777
|
+
compatibility,
|
|
14778
|
+
fetch: options.fetch,
|
|
14779
|
+
extraBody: options.extraBody
|
|
14780
|
+
});
|
|
14781
|
+
const createLanguageModel = (modelId, settings) => {
|
|
14782
|
+
if (new.target) {
|
|
14783
|
+
throw new Error(
|
|
14784
|
+
"The OpenRouter model function cannot be called with the new keyword."
|
|
14785
|
+
);
|
|
14786
|
+
}
|
|
14787
|
+
if (modelId === "openai/gpt-3.5-turbo-instruct") {
|
|
14788
|
+
return createCompletionModel(
|
|
14789
|
+
modelId,
|
|
14790
|
+
settings
|
|
14791
|
+
);
|
|
14792
|
+
}
|
|
14793
|
+
return createChatModel(modelId, settings);
|
|
14794
|
+
};
|
|
14795
|
+
const provider = function(modelId, settings) {
|
|
14796
|
+
return createLanguageModel(modelId, settings);
|
|
14797
|
+
};
|
|
14798
|
+
provider.languageModel = createLanguageModel;
|
|
14799
|
+
provider.chat = createChatModel;
|
|
14800
|
+
provider.completion = createCompletionModel;
|
|
14801
|
+
return provider;
|
|
14802
|
+
}
|
|
14803
|
+
createOpenRouter({
|
|
14804
|
+
compatibility: "strict"
|
|
14805
|
+
// strict for OpenRouter API
|
|
14806
|
+
});
|
|
14807
|
+
|
|
14808
|
+
class RetryLanguageModel {
|
|
14809
|
+
constructor(llms, names, stream_first_timeout) {
|
|
14810
|
+
this.llms = llms;
|
|
14811
|
+
this.names = names || [];
|
|
14812
|
+
this.stream_first_timeout = stream_first_timeout || 30000;
|
|
14813
|
+
if (this.names.indexOf("default") == -1) {
|
|
14814
|
+
this.names.push("default");
|
|
14815
|
+
}
|
|
14816
|
+
}
|
|
14817
|
+
async call(request) {
|
|
14818
|
+
return await this.doGenerate({
|
|
14819
|
+
inputFormat: "messages",
|
|
14820
|
+
mode: {
|
|
14821
|
+
type: "regular",
|
|
14822
|
+
tools: request.tools,
|
|
14823
|
+
toolChoice: request.toolChoice,
|
|
14824
|
+
},
|
|
14825
|
+
prompt: request.messages,
|
|
14826
|
+
maxTokens: request.maxTokens || config.maxTokens,
|
|
14827
|
+
temperature: request.temperature,
|
|
14828
|
+
topP: request.topP,
|
|
14829
|
+
topK: request.topK,
|
|
14830
|
+
providerMetadata: {},
|
|
12795
14831
|
abortSignal: request.abortSignal,
|
|
12796
14832
|
});
|
|
12797
14833
|
}
|
|
@@ -12906,7 +14942,7 @@ class RetryLanguageModel {
|
|
|
12906
14942
|
baseURL: llm.config?.baseURL,
|
|
12907
14943
|
}).languageModel(llm.model, {
|
|
12908
14944
|
// disable_parallel_tool_use
|
|
12909
|
-
parallelToolCalls: false,
|
|
14945
|
+
parallelToolCalls: llm.config?.parallelToolCalls || false,
|
|
12910
14946
|
});
|
|
12911
14947
|
}
|
|
12912
14948
|
else if (llm.provider == "anthropic") {
|
|
@@ -12924,10 +14960,16 @@ class RetryLanguageModel {
|
|
|
12924
14960
|
else if (llm.provider == "aws") {
|
|
12925
14961
|
let keys = llm.apiKey.split("=");
|
|
12926
14962
|
return createAmazonBedrock({
|
|
12927
|
-
region: llm.config?.region || "us-east-2",
|
|
12928
|
-
baseURL: llm.config?.baseURL,
|
|
12929
14963
|
accessKeyId: keys[0],
|
|
12930
14964
|
secretAccessKey: keys[1],
|
|
14965
|
+
baseURL: llm.config?.baseURL,
|
|
14966
|
+
region: llm.config?.region || "us-east-1",
|
|
14967
|
+
}).languageModel(llm.model);
|
|
14968
|
+
}
|
|
14969
|
+
else if (llm.provider == "openrouter") {
|
|
14970
|
+
return createOpenRouter({
|
|
14971
|
+
apiKey: llm.apiKey,
|
|
14972
|
+
baseURL: llm.config?.baseURL,
|
|
12931
14973
|
}).languageModel(llm.model);
|
|
12932
14974
|
}
|
|
12933
14975
|
else {
|