@openrouter/ai-sdk-provider 1.5.4 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -24
- package/dist/index.d.mts +27 -25
- package/dist/index.d.ts +27 -25
- package/dist/index.js +1516 -195
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1516 -193
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +20 -20
- package/dist/internal/index.d.ts +20 -20
- package/dist/internal/index.js +1510 -180
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1510 -178
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +9 -8
package/dist/index.js
CHANGED
|
@@ -22,9 +22,21 @@ var __spreadValues = (a, b) => {
|
|
|
22
22
|
return a;
|
|
23
23
|
};
|
|
24
24
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __objRest = (source, exclude) => {
|
|
26
|
+
var target = {};
|
|
27
|
+
for (var prop in source)
|
|
28
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
if (source != null && __getOwnPropSymbols)
|
|
31
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
+
target[prop] = source[prop];
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
36
|
+
};
|
|
25
37
|
var __export = (target, all) => {
|
|
26
|
-
for (var
|
|
27
|
-
__defProp(target,
|
|
38
|
+
for (var name15 in all)
|
|
39
|
+
__defProp(target, name15, { get: all[name15], enumerable: true });
|
|
28
40
|
};
|
|
29
41
|
var __copyProps = (to, from, except, desc) => {
|
|
30
42
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -53,11 +65,12 @@ __export(index_exports, {
|
|
|
53
65
|
});
|
|
54
66
|
module.exports = __toCommonJS(index_exports);
|
|
55
67
|
|
|
56
|
-
// node_modules/.pnpm/@ai-sdk+provider@
|
|
68
|
+
// node_modules/.pnpm/@ai-sdk+provider@3.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
57
69
|
var marker = "vercel.ai.error";
|
|
58
70
|
var symbol = Symbol.for(marker);
|
|
59
71
|
var _a;
|
|
60
|
-
var
|
|
72
|
+
var _b;
|
|
73
|
+
var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
61
74
|
/**
|
|
62
75
|
* Creates an AI SDK Error.
|
|
63
76
|
*
|
|
@@ -67,13 +80,13 @@ var _AISDKError = class _AISDKError2 extends Error {
|
|
|
67
80
|
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
68
81
|
*/
|
|
69
82
|
constructor({
|
|
70
|
-
name:
|
|
83
|
+
name: name142,
|
|
71
84
|
message,
|
|
72
85
|
cause
|
|
73
86
|
}) {
|
|
74
87
|
super(message);
|
|
75
88
|
this[_a] = true;
|
|
76
|
-
this.name =
|
|
89
|
+
this.name = name142;
|
|
77
90
|
this.cause = cause;
|
|
78
91
|
}
|
|
79
92
|
/**
|
|
@@ -82,20 +95,19 @@ var _AISDKError = class _AISDKError2 extends Error {
|
|
|
82
95
|
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
83
96
|
*/
|
|
84
97
|
static isInstance(error) {
|
|
85
|
-
return
|
|
98
|
+
return _AISDKError.hasMarker(error, marker);
|
|
86
99
|
}
|
|
87
|
-
static hasMarker(error,
|
|
88
|
-
const markerSymbol = Symbol.for(
|
|
100
|
+
static hasMarker(error, marker152) {
|
|
101
|
+
const markerSymbol = Symbol.for(marker152);
|
|
89
102
|
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
90
103
|
}
|
|
91
104
|
};
|
|
92
|
-
_a = symbol;
|
|
93
|
-
var AISDKError = _AISDKError;
|
|
94
105
|
var name = "AI_APICallError";
|
|
95
106
|
var marker2 = `vercel.ai.error.${name}`;
|
|
96
107
|
var symbol2 = Symbol.for(marker2);
|
|
97
108
|
var _a2;
|
|
98
|
-
var
|
|
109
|
+
var _b2;
|
|
110
|
+
var APICallError = class extends (_b2 = AISDKError, _a2 = symbol2, _b2) {
|
|
99
111
|
constructor({
|
|
100
112
|
message,
|
|
101
113
|
url,
|
|
@@ -125,12 +137,12 @@ var APICallError = class extends AISDKError {
|
|
|
125
137
|
return AISDKError.hasMarker(error, marker2);
|
|
126
138
|
}
|
|
127
139
|
};
|
|
128
|
-
_a2 = symbol2;
|
|
129
140
|
var name2 = "AI_EmptyResponseBodyError";
|
|
130
141
|
var marker3 = `vercel.ai.error.${name2}`;
|
|
131
142
|
var symbol3 = Symbol.for(marker3);
|
|
132
143
|
var _a3;
|
|
133
|
-
var
|
|
144
|
+
var _b3;
|
|
145
|
+
var EmptyResponseBodyError = class extends (_b3 = AISDKError, _a3 = symbol3, _b3) {
|
|
134
146
|
// used in isInstance
|
|
135
147
|
constructor({ message = "Empty response body" } = {}) {
|
|
136
148
|
super({ name: name2, message });
|
|
@@ -140,7 +152,6 @@ var EmptyResponseBodyError = class extends AISDKError {
|
|
|
140
152
|
return AISDKError.hasMarker(error, marker3);
|
|
141
153
|
}
|
|
142
154
|
};
|
|
143
|
-
_a3 = symbol3;
|
|
144
155
|
function getErrorMessage(error) {
|
|
145
156
|
if (error == null) {
|
|
146
157
|
return "unknown error";
|
|
@@ -157,7 +168,8 @@ var name3 = "AI_InvalidArgumentError";
|
|
|
157
168
|
var marker4 = `vercel.ai.error.${name3}`;
|
|
158
169
|
var symbol4 = Symbol.for(marker4);
|
|
159
170
|
var _a4;
|
|
160
|
-
var
|
|
171
|
+
var _b4;
|
|
172
|
+
var InvalidArgumentError = class extends (_b4 = AISDKError, _a4 = symbol4, _b4) {
|
|
161
173
|
constructor({
|
|
162
174
|
message,
|
|
163
175
|
cause,
|
|
@@ -171,12 +183,12 @@ var InvalidArgumentError = class extends AISDKError {
|
|
|
171
183
|
return AISDKError.hasMarker(error, marker4);
|
|
172
184
|
}
|
|
173
185
|
};
|
|
174
|
-
_a4 = symbol4;
|
|
175
186
|
var name4 = "AI_InvalidPromptError";
|
|
176
187
|
var marker5 = `vercel.ai.error.${name4}`;
|
|
177
188
|
var symbol5 = Symbol.for(marker5);
|
|
178
189
|
var _a5;
|
|
179
|
-
var
|
|
190
|
+
var _b5;
|
|
191
|
+
var InvalidPromptError = class extends (_b5 = AISDKError, _a5 = symbol5, _b5) {
|
|
180
192
|
constructor({
|
|
181
193
|
prompt,
|
|
182
194
|
message,
|
|
@@ -190,12 +202,12 @@ var InvalidPromptError = class extends AISDKError {
|
|
|
190
202
|
return AISDKError.hasMarker(error, marker5);
|
|
191
203
|
}
|
|
192
204
|
};
|
|
193
|
-
_a5 = symbol5;
|
|
194
205
|
var name5 = "AI_InvalidResponseDataError";
|
|
195
206
|
var marker6 = `vercel.ai.error.${name5}`;
|
|
196
207
|
var symbol6 = Symbol.for(marker6);
|
|
197
208
|
var _a6;
|
|
198
|
-
var
|
|
209
|
+
var _b6;
|
|
210
|
+
var InvalidResponseDataError = class extends (_b6 = AISDKError, _a6 = symbol6, _b6) {
|
|
199
211
|
constructor({
|
|
200
212
|
data,
|
|
201
213
|
message = `Invalid response data: ${JSON.stringify(data)}.`
|
|
@@ -208,12 +220,12 @@ var InvalidResponseDataError = class extends AISDKError {
|
|
|
208
220
|
return AISDKError.hasMarker(error, marker6);
|
|
209
221
|
}
|
|
210
222
|
};
|
|
211
|
-
_a6 = symbol6;
|
|
212
223
|
var name6 = "AI_JSONParseError";
|
|
213
224
|
var marker7 = `vercel.ai.error.${name6}`;
|
|
214
225
|
var symbol7 = Symbol.for(marker7);
|
|
215
226
|
var _a7;
|
|
216
|
-
var
|
|
227
|
+
var _b7;
|
|
228
|
+
var JSONParseError = class extends (_b7 = AISDKError, _a7 = symbol7, _b7) {
|
|
217
229
|
constructor({ text, cause }) {
|
|
218
230
|
super({
|
|
219
231
|
name: name6,
|
|
@@ -228,12 +240,12 @@ Error message: ${getErrorMessage(cause)}`,
|
|
|
228
240
|
return AISDKError.hasMarker(error, marker7);
|
|
229
241
|
}
|
|
230
242
|
};
|
|
231
|
-
_a7 = symbol7;
|
|
232
243
|
var name7 = "AI_LoadAPIKeyError";
|
|
233
244
|
var marker8 = `vercel.ai.error.${name7}`;
|
|
234
245
|
var symbol8 = Symbol.for(marker8);
|
|
235
246
|
var _a8;
|
|
236
|
-
var
|
|
247
|
+
var _b8;
|
|
248
|
+
var LoadAPIKeyError = class extends (_b8 = AISDKError, _a8 = symbol8, _b8) {
|
|
237
249
|
// used in isInstance
|
|
238
250
|
constructor({ message }) {
|
|
239
251
|
super({ name: name7, message });
|
|
@@ -243,17 +255,27 @@ var LoadAPIKeyError = class extends AISDKError {
|
|
|
243
255
|
return AISDKError.hasMarker(error, marker8);
|
|
244
256
|
}
|
|
245
257
|
};
|
|
246
|
-
_a8 = symbol8;
|
|
247
258
|
var name8 = "AI_LoadSettingError";
|
|
248
259
|
var marker9 = `vercel.ai.error.${name8}`;
|
|
249
260
|
var symbol9 = Symbol.for(marker9);
|
|
250
261
|
var _a9;
|
|
251
|
-
|
|
262
|
+
var _b9;
|
|
263
|
+
var LoadSettingError = class extends (_b9 = AISDKError, _a9 = symbol9, _b9) {
|
|
264
|
+
// used in isInstance
|
|
265
|
+
constructor({ message }) {
|
|
266
|
+
super({ name: name8, message });
|
|
267
|
+
this[_a9] = true;
|
|
268
|
+
}
|
|
269
|
+
static isInstance(error) {
|
|
270
|
+
return AISDKError.hasMarker(error, marker9);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
252
273
|
var name9 = "AI_NoContentGeneratedError";
|
|
253
274
|
var marker10 = `vercel.ai.error.${name9}`;
|
|
254
275
|
var symbol10 = Symbol.for(marker10);
|
|
255
276
|
var _a10;
|
|
256
|
-
var
|
|
277
|
+
var _b10;
|
|
278
|
+
var NoContentGeneratedError = class extends (_b10 = AISDKError, _a10 = symbol10, _b10) {
|
|
257
279
|
// used in isInstance
|
|
258
280
|
constructor({
|
|
259
281
|
message = "No content generated."
|
|
@@ -265,22 +287,54 @@ var NoContentGeneratedError = class extends AISDKError {
|
|
|
265
287
|
return AISDKError.hasMarker(error, marker10);
|
|
266
288
|
}
|
|
267
289
|
};
|
|
268
|
-
_a10 = symbol10;
|
|
269
290
|
var name10 = "AI_NoSuchModelError";
|
|
270
291
|
var marker11 = `vercel.ai.error.${name10}`;
|
|
271
292
|
var symbol11 = Symbol.for(marker11);
|
|
272
293
|
var _a11;
|
|
273
|
-
|
|
294
|
+
var _b11;
|
|
295
|
+
var NoSuchModelError = class extends (_b11 = AISDKError, _a11 = symbol11, _b11) {
|
|
296
|
+
constructor({
|
|
297
|
+
errorName = name10,
|
|
298
|
+
modelId,
|
|
299
|
+
modelType,
|
|
300
|
+
message = `No such ${modelType}: ${modelId}`
|
|
301
|
+
}) {
|
|
302
|
+
super({ name: errorName, message });
|
|
303
|
+
this[_a11] = true;
|
|
304
|
+
this.modelId = modelId;
|
|
305
|
+
this.modelType = modelType;
|
|
306
|
+
}
|
|
307
|
+
static isInstance(error) {
|
|
308
|
+
return AISDKError.hasMarker(error, marker11);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
274
311
|
var name11 = "AI_TooManyEmbeddingValuesForCallError";
|
|
275
312
|
var marker12 = `vercel.ai.error.${name11}`;
|
|
276
313
|
var symbol12 = Symbol.for(marker12);
|
|
277
314
|
var _a12;
|
|
278
|
-
|
|
315
|
+
var _b12;
|
|
316
|
+
var TooManyEmbeddingValuesForCallError = class extends (_b12 = AISDKError, _a12 = symbol12, _b12) {
|
|
317
|
+
constructor(options) {
|
|
318
|
+
super({
|
|
319
|
+
name: name11,
|
|
320
|
+
message: `Too many values for a single embedding call. The ${options.provider} model "${options.modelId}" can only embed up to ${options.maxEmbeddingsPerCall} values per call, but ${options.values.length} values were provided.`
|
|
321
|
+
});
|
|
322
|
+
this[_a12] = true;
|
|
323
|
+
this.provider = options.provider;
|
|
324
|
+
this.modelId = options.modelId;
|
|
325
|
+
this.maxEmbeddingsPerCall = options.maxEmbeddingsPerCall;
|
|
326
|
+
this.values = options.values;
|
|
327
|
+
}
|
|
328
|
+
static isInstance(error) {
|
|
329
|
+
return AISDKError.hasMarker(error, marker12);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
279
332
|
var name12 = "AI_TypeValidationError";
|
|
280
333
|
var marker13 = `vercel.ai.error.${name12}`;
|
|
281
334
|
var symbol13 = Symbol.for(marker13);
|
|
282
335
|
var _a13;
|
|
283
|
-
var
|
|
336
|
+
var _b13;
|
|
337
|
+
var TypeValidationError = class _TypeValidationError extends (_b13 = AISDKError, _a13 = symbol13, _b13) {
|
|
284
338
|
constructor({ value, cause }) {
|
|
285
339
|
super({
|
|
286
340
|
name: name12,
|
|
@@ -308,16 +362,15 @@ Error message: ${getErrorMessage(cause)}`,
|
|
|
308
362
|
value,
|
|
309
363
|
cause
|
|
310
364
|
}) {
|
|
311
|
-
return
|
|
365
|
+
return _TypeValidationError.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError({ value, cause });
|
|
312
366
|
}
|
|
313
367
|
};
|
|
314
|
-
_a13 = symbol13;
|
|
315
|
-
var TypeValidationError = _TypeValidationError;
|
|
316
368
|
var name13 = "AI_UnsupportedFunctionalityError";
|
|
317
369
|
var marker14 = `vercel.ai.error.${name13}`;
|
|
318
370
|
var symbol14 = Symbol.for(marker14);
|
|
319
371
|
var _a14;
|
|
320
|
-
var
|
|
372
|
+
var _b14;
|
|
373
|
+
var UnsupportedFunctionalityError = class extends (_b14 = AISDKError, _a14 = symbol14, _b14) {
|
|
321
374
|
constructor({
|
|
322
375
|
functionality,
|
|
323
376
|
message = `'${functionality}' functionality not supported.`
|
|
@@ -330,9 +383,14 @@ var UnsupportedFunctionalityError = class extends AISDKError {
|
|
|
330
383
|
return AISDKError.hasMarker(error, marker14);
|
|
331
384
|
}
|
|
332
385
|
};
|
|
333
|
-
_a14 = symbol14;
|
|
334
386
|
|
|
335
|
-
// node_modules/.pnpm
|
|
387
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@4.0.1_zod@4.3.5/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
388
|
+
var z4 = __toESM(require("zod/v4"), 1);
|
|
389
|
+
var import_v3 = require("zod/v3");
|
|
390
|
+
var import_v32 = require("zod/v3");
|
|
391
|
+
var import_v33 = require("zod/v3");
|
|
392
|
+
|
|
393
|
+
// node_modules/.pnpm/eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/index.js
|
|
336
394
|
var ParseError = class extends Error {
|
|
337
395
|
constructor(message, options) {
|
|
338
396
|
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
@@ -423,7 +481,7 @@ function splitLines(chunk) {
|
|
|
423
481
|
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
424
482
|
`, searchIndex);
|
|
425
483
|
let lineEnd = -1;
|
|
426
|
-
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
|
|
484
|
+
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
|
|
427
485
|
incompleteLine = chunk.slice(searchIndex);
|
|
428
486
|
break;
|
|
429
487
|
} else {
|
|
@@ -435,7 +493,7 @@ function splitLines(chunk) {
|
|
|
435
493
|
return [lines, incompleteLine];
|
|
436
494
|
}
|
|
437
495
|
|
|
438
|
-
// node_modules/.pnpm/eventsource-parser@3.0.
|
|
496
|
+
// node_modules/.pnpm/eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/stream.js
|
|
439
497
|
var EventSourceParserStream = class extends TransformStream {
|
|
440
498
|
constructor({ onError, onRetry, onComment } = {}) {
|
|
441
499
|
let parser;
|
|
@@ -459,25 +517,7 @@ var EventSourceParserStream = class extends TransformStream {
|
|
|
459
517
|
}
|
|
460
518
|
};
|
|
461
519
|
|
|
462
|
-
// node_modules/.pnpm/@ai-sdk+provider-utils@
|
|
463
|
-
var z4 = __toESM(require("zod/v4"), 1);
|
|
464
|
-
|
|
465
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/Options.js
|
|
466
|
-
var ignoreOverride = Symbol("Let zodToJsonSchema decide on which parser to use");
|
|
467
|
-
|
|
468
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/selectParser.js
|
|
469
|
-
var import_zod3 = require("zod");
|
|
470
|
-
|
|
471
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/array.js
|
|
472
|
-
var import_zod = require("zod");
|
|
473
|
-
|
|
474
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/record.js
|
|
475
|
-
var import_zod2 = require("zod");
|
|
476
|
-
|
|
477
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/parsers/string.js
|
|
478
|
-
var ALPHA_NUMERIC = new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
479
|
-
|
|
480
|
-
// node_modules/.pnpm/@ai-sdk+provider-utils@3.0.1_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
520
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@4.0.1_zod@4.3.5/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
481
521
|
function combineHeaders(...headers) {
|
|
482
522
|
return headers.reduce(
|
|
483
523
|
(combinedHeaders, currentHeaders) => __spreadValues(__spreadValues({}, combinedHeaders), currentHeaders != null ? currentHeaders : {}),
|
|
@@ -487,6 +527,37 @@ function combineHeaders(...headers) {
|
|
|
487
527
|
function extractResponseHeaders(response) {
|
|
488
528
|
return Object.fromEntries([...response.headers]);
|
|
489
529
|
}
|
|
530
|
+
var { btoa, atob } = globalThis;
|
|
531
|
+
function convertUint8ArrayToBase64(array) {
|
|
532
|
+
let latin1string = "";
|
|
533
|
+
for (let i = 0; i < array.length; i++) {
|
|
534
|
+
latin1string += String.fromCodePoint(array[i]);
|
|
535
|
+
}
|
|
536
|
+
return btoa(latin1string);
|
|
537
|
+
}
|
|
538
|
+
var name14 = "AI_DownloadError";
|
|
539
|
+
var marker15 = `vercel.ai.error.${name14}`;
|
|
540
|
+
var symbol15 = Symbol.for(marker15);
|
|
541
|
+
var _a15;
|
|
542
|
+
var _b15;
|
|
543
|
+
var DownloadError = class extends (_b15 = AISDKError, _a15 = symbol15, _b15) {
|
|
544
|
+
constructor({
|
|
545
|
+
url,
|
|
546
|
+
statusCode,
|
|
547
|
+
statusText,
|
|
548
|
+
cause,
|
|
549
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
|
550
|
+
}) {
|
|
551
|
+
super({ name: name14, message, cause });
|
|
552
|
+
this[_a15] = true;
|
|
553
|
+
this.url = url;
|
|
554
|
+
this.statusCode = statusCode;
|
|
555
|
+
this.statusText = statusText;
|
|
556
|
+
}
|
|
557
|
+
static isInstance(error) {
|
|
558
|
+
return AISDKError.hasMarker(error, marker15);
|
|
559
|
+
}
|
|
560
|
+
};
|
|
490
561
|
var createIdGenerator = ({
|
|
491
562
|
prefix,
|
|
492
563
|
size = 16,
|
|
@@ -541,11 +612,53 @@ function handleFetchError({
|
|
|
541
612
|
}
|
|
542
613
|
return error;
|
|
543
614
|
}
|
|
544
|
-
function
|
|
545
|
-
|
|
546
|
-
|
|
615
|
+
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
|
616
|
+
var _a22, _b22, _c;
|
|
617
|
+
if (globalThisAny.window) {
|
|
618
|
+
return `runtime/browser`;
|
|
619
|
+
}
|
|
620
|
+
if ((_a22 = globalThisAny.navigator) == null ? void 0 : _a22.userAgent) {
|
|
621
|
+
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
|
622
|
+
}
|
|
623
|
+
if ((_c = (_b22 = globalThisAny.process) == null ? void 0 : _b22.versions) == null ? void 0 : _c.node) {
|
|
624
|
+
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
|
625
|
+
}
|
|
626
|
+
if (globalThisAny.EdgeRuntime) {
|
|
627
|
+
return `runtime/vercel-edge`;
|
|
628
|
+
}
|
|
629
|
+
return "runtime/unknown";
|
|
630
|
+
}
|
|
631
|
+
function normalizeHeaders(headers) {
|
|
632
|
+
if (headers == null) {
|
|
633
|
+
return {};
|
|
634
|
+
}
|
|
635
|
+
const normalized = {};
|
|
636
|
+
if (headers instanceof Headers) {
|
|
637
|
+
headers.forEach((value, key) => {
|
|
638
|
+
normalized[key.toLowerCase()] = value;
|
|
639
|
+
});
|
|
640
|
+
} else {
|
|
641
|
+
if (!Array.isArray(headers)) {
|
|
642
|
+
headers = Object.entries(headers);
|
|
643
|
+
}
|
|
644
|
+
for (const [key, value] of headers) {
|
|
645
|
+
if (value != null) {
|
|
646
|
+
normalized[key.toLowerCase()] = value;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return normalized;
|
|
651
|
+
}
|
|
652
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
653
|
+
const normalizedHeaders = new Headers(normalizeHeaders(headers));
|
|
654
|
+
const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
|
|
655
|
+
normalizedHeaders.set(
|
|
656
|
+
"user-agent",
|
|
657
|
+
[currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
|
|
547
658
|
);
|
|
659
|
+
return Object.fromEntries(normalizedHeaders.entries());
|
|
548
660
|
}
|
|
661
|
+
var VERSION = true ? "4.0.1" : "0.0.0-test";
|
|
549
662
|
function loadApiKey({
|
|
550
663
|
apiKey,
|
|
551
664
|
environmentVariableName,
|
|
@@ -614,34 +727,1195 @@ function filter(obj) {
|
|
|
614
727
|
}
|
|
615
728
|
function secureJsonParse(text) {
|
|
616
729
|
const { stackTraceLimit } = Error;
|
|
617
|
-
|
|
730
|
+
try {
|
|
731
|
+
Error.stackTraceLimit = 0;
|
|
732
|
+
} catch (e) {
|
|
733
|
+
return _parse(text);
|
|
734
|
+
}
|
|
618
735
|
try {
|
|
619
736
|
return _parse(text);
|
|
620
737
|
} finally {
|
|
621
738
|
Error.stackTraceLimit = stackTraceLimit;
|
|
622
739
|
}
|
|
623
740
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
741
|
+
function addAdditionalPropertiesToJsonSchema(jsonSchema2) {
|
|
742
|
+
if (jsonSchema2.type === "object") {
|
|
743
|
+
jsonSchema2.additionalProperties = false;
|
|
744
|
+
const properties = jsonSchema2.properties;
|
|
745
|
+
if (properties != null) {
|
|
746
|
+
for (const property in properties) {
|
|
747
|
+
properties[property] = addAdditionalPropertiesToJsonSchema(
|
|
748
|
+
properties[property]
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
if (jsonSchema2.type === "array" && jsonSchema2.items != null) {
|
|
754
|
+
if (Array.isArray(jsonSchema2.items)) {
|
|
755
|
+
jsonSchema2.items = jsonSchema2.items.map(
|
|
756
|
+
(item) => addAdditionalPropertiesToJsonSchema(item)
|
|
757
|
+
);
|
|
758
|
+
} else {
|
|
759
|
+
jsonSchema2.items = addAdditionalPropertiesToJsonSchema(
|
|
760
|
+
jsonSchema2.items
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
return jsonSchema2;
|
|
765
|
+
}
|
|
766
|
+
var ignoreOverride = Symbol(
|
|
767
|
+
"Let zodToJsonSchema decide on which parser to use"
|
|
768
|
+
);
|
|
769
|
+
var defaultOptions = {
|
|
770
|
+
name: void 0,
|
|
771
|
+
$refStrategy: "root",
|
|
772
|
+
basePath: ["#"],
|
|
773
|
+
effectStrategy: "input",
|
|
774
|
+
pipeStrategy: "all",
|
|
775
|
+
dateStrategy: "format:date-time",
|
|
776
|
+
mapStrategy: "entries",
|
|
777
|
+
removeAdditionalStrategy: "passthrough",
|
|
778
|
+
allowedAdditionalProperties: true,
|
|
779
|
+
rejectedAdditionalProperties: false,
|
|
780
|
+
definitionPath: "definitions",
|
|
781
|
+
strictUnions: false,
|
|
782
|
+
definitions: {},
|
|
783
|
+
errorMessages: false,
|
|
784
|
+
patternStrategy: "escape",
|
|
785
|
+
applyRegexFlags: false,
|
|
786
|
+
emailStrategy: "format:email",
|
|
787
|
+
base64Strategy: "contentEncoding:base64",
|
|
788
|
+
nameStrategy: "ref"
|
|
789
|
+
};
|
|
790
|
+
var getDefaultOptions = (options) => typeof options === "string" ? __spreadProps(__spreadValues({}, defaultOptions), {
|
|
791
|
+
name: options
|
|
792
|
+
}) : __spreadValues(__spreadValues({}, defaultOptions), options);
|
|
793
|
+
function parseAnyDef() {
|
|
794
|
+
return {};
|
|
795
|
+
}
|
|
796
|
+
function parseArrayDef(def, refs) {
|
|
797
|
+
var _a22, _b22, _c;
|
|
798
|
+
const res = {
|
|
799
|
+
type: "array"
|
|
800
|
+
};
|
|
801
|
+
if (((_a22 = def.type) == null ? void 0 : _a22._def) && ((_c = (_b22 = def.type) == null ? void 0 : _b22._def) == null ? void 0 : _c.typeName) !== import_v32.ZodFirstPartyTypeKind.ZodAny) {
|
|
802
|
+
res.items = parseDef(def.type._def, __spreadProps(__spreadValues({}, refs), {
|
|
803
|
+
currentPath: [...refs.currentPath, "items"]
|
|
804
|
+
}));
|
|
805
|
+
}
|
|
806
|
+
if (def.minLength) {
|
|
807
|
+
res.minItems = def.minLength.value;
|
|
808
|
+
}
|
|
809
|
+
if (def.maxLength) {
|
|
810
|
+
res.maxItems = def.maxLength.value;
|
|
811
|
+
}
|
|
812
|
+
if (def.exactLength) {
|
|
813
|
+
res.minItems = def.exactLength.value;
|
|
814
|
+
res.maxItems = def.exactLength.value;
|
|
815
|
+
}
|
|
816
|
+
return res;
|
|
817
|
+
}
|
|
818
|
+
function parseBigintDef(def) {
|
|
819
|
+
const res = {
|
|
820
|
+
type: "integer",
|
|
821
|
+
format: "int64"
|
|
822
|
+
};
|
|
823
|
+
if (!def.checks) return res;
|
|
824
|
+
for (const check of def.checks) {
|
|
825
|
+
switch (check.kind) {
|
|
826
|
+
case "min":
|
|
827
|
+
if (check.inclusive) {
|
|
828
|
+
res.minimum = check.value;
|
|
829
|
+
} else {
|
|
830
|
+
res.exclusiveMinimum = check.value;
|
|
831
|
+
}
|
|
832
|
+
break;
|
|
833
|
+
case "max":
|
|
834
|
+
if (check.inclusive) {
|
|
835
|
+
res.maximum = check.value;
|
|
836
|
+
} else {
|
|
837
|
+
res.exclusiveMaximum = check.value;
|
|
838
|
+
}
|
|
839
|
+
break;
|
|
840
|
+
case "multipleOf":
|
|
841
|
+
res.multipleOf = check.value;
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return res;
|
|
627
846
|
}
|
|
628
|
-
function
|
|
629
|
-
return
|
|
847
|
+
function parseBooleanDef() {
|
|
848
|
+
return { type: "boolean" };
|
|
630
849
|
}
|
|
631
|
-
function
|
|
632
|
-
return
|
|
850
|
+
function parseBrandedDef(_def, refs) {
|
|
851
|
+
return parseDef(_def.type._def, refs);
|
|
633
852
|
}
|
|
634
|
-
|
|
635
|
-
return
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
})
|
|
853
|
+
var parseCatchDef = (def, refs) => {
|
|
854
|
+
return parseDef(def.innerType._def, refs);
|
|
855
|
+
};
|
|
856
|
+
function parseDateDef(def, refs, overrideDateStrategy) {
|
|
857
|
+
const strategy = overrideDateStrategy != null ? overrideDateStrategy : refs.dateStrategy;
|
|
858
|
+
if (Array.isArray(strategy)) {
|
|
859
|
+
return {
|
|
860
|
+
anyOf: strategy.map((item, i) => parseDateDef(def, refs, item))
|
|
643
861
|
};
|
|
862
|
+
}
|
|
863
|
+
switch (strategy) {
|
|
864
|
+
case "string":
|
|
865
|
+
case "format:date-time":
|
|
866
|
+
return {
|
|
867
|
+
type: "string",
|
|
868
|
+
format: "date-time"
|
|
869
|
+
};
|
|
870
|
+
case "format:date":
|
|
871
|
+
return {
|
|
872
|
+
type: "string",
|
|
873
|
+
format: "date"
|
|
874
|
+
};
|
|
875
|
+
case "integer":
|
|
876
|
+
return integerDateParser(def);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
var integerDateParser = (def) => {
|
|
880
|
+
const res = {
|
|
881
|
+
type: "integer",
|
|
882
|
+
format: "unix-time"
|
|
883
|
+
};
|
|
884
|
+
for (const check of def.checks) {
|
|
885
|
+
switch (check.kind) {
|
|
886
|
+
case "min":
|
|
887
|
+
res.minimum = check.value;
|
|
888
|
+
break;
|
|
889
|
+
case "max":
|
|
890
|
+
res.maximum = check.value;
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
return res;
|
|
895
|
+
};
|
|
896
|
+
function parseDefaultDef(_def, refs) {
|
|
897
|
+
return __spreadProps(__spreadValues({}, parseDef(_def.innerType._def, refs)), {
|
|
898
|
+
default: _def.defaultValue()
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
function parseEffectsDef(_def, refs) {
|
|
902
|
+
return refs.effectStrategy === "input" ? parseDef(_def.schema._def, refs) : parseAnyDef();
|
|
903
|
+
}
|
|
904
|
+
function parseEnumDef(def) {
|
|
905
|
+
return {
|
|
906
|
+
type: "string",
|
|
907
|
+
enum: Array.from(def.values)
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
var isJsonSchema7AllOfType = (type) => {
|
|
911
|
+
if ("type" in type && type.type === "string") return false;
|
|
912
|
+
return "allOf" in type;
|
|
913
|
+
};
|
|
914
|
+
function parseIntersectionDef(def, refs) {
|
|
915
|
+
const allOf = [
|
|
916
|
+
parseDef(def.left._def, __spreadProps(__spreadValues({}, refs), {
|
|
917
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
918
|
+
})),
|
|
919
|
+
parseDef(def.right._def, __spreadProps(__spreadValues({}, refs), {
|
|
920
|
+
currentPath: [...refs.currentPath, "allOf", "1"]
|
|
921
|
+
}))
|
|
922
|
+
].filter((x) => !!x);
|
|
923
|
+
const mergedAllOf = [];
|
|
924
|
+
allOf.forEach((schema) => {
|
|
925
|
+
if (isJsonSchema7AllOfType(schema)) {
|
|
926
|
+
mergedAllOf.push(...schema.allOf);
|
|
927
|
+
} else {
|
|
928
|
+
let nestedSchema = schema;
|
|
929
|
+
if ("additionalProperties" in schema && schema.additionalProperties === false) {
|
|
930
|
+
const _a16 = schema, { additionalProperties } = _a16, rest = __objRest(_a16, ["additionalProperties"]);
|
|
931
|
+
nestedSchema = rest;
|
|
932
|
+
}
|
|
933
|
+
mergedAllOf.push(nestedSchema);
|
|
934
|
+
}
|
|
644
935
|
});
|
|
936
|
+
return mergedAllOf.length ? { allOf: mergedAllOf } : void 0;
|
|
937
|
+
}
|
|
938
|
+
function parseLiteralDef(def) {
|
|
939
|
+
const parsedType = typeof def.value;
|
|
940
|
+
if (parsedType !== "bigint" && parsedType !== "number" && parsedType !== "boolean" && parsedType !== "string") {
|
|
941
|
+
return {
|
|
942
|
+
type: Array.isArray(def.value) ? "array" : "object"
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
return {
|
|
946
|
+
type: parsedType === "bigint" ? "integer" : parsedType,
|
|
947
|
+
const: def.value
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
var emojiRegex = void 0;
|
|
951
|
+
var zodPatterns = {
|
|
952
|
+
/**
|
|
953
|
+
* `c` was changed to `[cC]` to replicate /i flag
|
|
954
|
+
*/
|
|
955
|
+
cuid: /^[cC][^\s-]{8,}$/,
|
|
956
|
+
cuid2: /^[0-9a-z]+$/,
|
|
957
|
+
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
958
|
+
/**
|
|
959
|
+
* `a-z` was added to replicate /i flag
|
|
960
|
+
*/
|
|
961
|
+
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
|
962
|
+
/**
|
|
963
|
+
* Constructed a valid Unicode RegExp
|
|
964
|
+
*
|
|
965
|
+
* Lazily instantiate since this type of regex isn't supported
|
|
966
|
+
* in all envs (e.g. React Native).
|
|
967
|
+
*
|
|
968
|
+
* See:
|
|
969
|
+
* https://github.com/colinhacks/zod/issues/2433
|
|
970
|
+
* Fix in Zod:
|
|
971
|
+
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
|
972
|
+
*/
|
|
973
|
+
emoji: () => {
|
|
974
|
+
if (emojiRegex === void 0) {
|
|
975
|
+
emojiRegex = RegExp(
|
|
976
|
+
"^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
|
|
977
|
+
"u"
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
return emojiRegex;
|
|
981
|
+
},
|
|
982
|
+
/**
|
|
983
|
+
* Unused
|
|
984
|
+
*/
|
|
985
|
+
uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
|
|
986
|
+
/**
|
|
987
|
+
* Unused
|
|
988
|
+
*/
|
|
989
|
+
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
|
|
990
|
+
ipv4Cidr: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/,
|
|
991
|
+
/**
|
|
992
|
+
* Unused
|
|
993
|
+
*/
|
|
994
|
+
ipv6: /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/,
|
|
995
|
+
ipv6Cidr: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
|
|
996
|
+
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
|
997
|
+
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
|
998
|
+
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
|
999
|
+
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
|
1000
|
+
};
|
|
1001
|
+
function parseStringDef(def, refs) {
|
|
1002
|
+
const res = {
|
|
1003
|
+
type: "string"
|
|
1004
|
+
};
|
|
1005
|
+
if (def.checks) {
|
|
1006
|
+
for (const check of def.checks) {
|
|
1007
|
+
switch (check.kind) {
|
|
1008
|
+
case "min":
|
|
1009
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
|
1010
|
+
break;
|
|
1011
|
+
case "max":
|
|
1012
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
|
1013
|
+
break;
|
|
1014
|
+
case "email":
|
|
1015
|
+
switch (refs.emailStrategy) {
|
|
1016
|
+
case "format:email":
|
|
1017
|
+
addFormat(res, "email", check.message, refs);
|
|
1018
|
+
break;
|
|
1019
|
+
case "format:idn-email":
|
|
1020
|
+
addFormat(res, "idn-email", check.message, refs);
|
|
1021
|
+
break;
|
|
1022
|
+
case "pattern:zod":
|
|
1023
|
+
addPattern(res, zodPatterns.email, check.message, refs);
|
|
1024
|
+
break;
|
|
1025
|
+
}
|
|
1026
|
+
break;
|
|
1027
|
+
case "url":
|
|
1028
|
+
addFormat(res, "uri", check.message, refs);
|
|
1029
|
+
break;
|
|
1030
|
+
case "uuid":
|
|
1031
|
+
addFormat(res, "uuid", check.message, refs);
|
|
1032
|
+
break;
|
|
1033
|
+
case "regex":
|
|
1034
|
+
addPattern(res, check.regex, check.message, refs);
|
|
1035
|
+
break;
|
|
1036
|
+
case "cuid":
|
|
1037
|
+
addPattern(res, zodPatterns.cuid, check.message, refs);
|
|
1038
|
+
break;
|
|
1039
|
+
case "cuid2":
|
|
1040
|
+
addPattern(res, zodPatterns.cuid2, check.message, refs);
|
|
1041
|
+
break;
|
|
1042
|
+
case "startsWith":
|
|
1043
|
+
addPattern(
|
|
1044
|
+
res,
|
|
1045
|
+
RegExp(`^${escapeLiteralCheckValue(check.value, refs)}`),
|
|
1046
|
+
check.message,
|
|
1047
|
+
refs
|
|
1048
|
+
);
|
|
1049
|
+
break;
|
|
1050
|
+
case "endsWith":
|
|
1051
|
+
addPattern(
|
|
1052
|
+
res,
|
|
1053
|
+
RegExp(`${escapeLiteralCheckValue(check.value, refs)}$`),
|
|
1054
|
+
check.message,
|
|
1055
|
+
refs
|
|
1056
|
+
);
|
|
1057
|
+
break;
|
|
1058
|
+
case "datetime":
|
|
1059
|
+
addFormat(res, "date-time", check.message, refs);
|
|
1060
|
+
break;
|
|
1061
|
+
case "date":
|
|
1062
|
+
addFormat(res, "date", check.message, refs);
|
|
1063
|
+
break;
|
|
1064
|
+
case "time":
|
|
1065
|
+
addFormat(res, "time", check.message, refs);
|
|
1066
|
+
break;
|
|
1067
|
+
case "duration":
|
|
1068
|
+
addFormat(res, "duration", check.message, refs);
|
|
1069
|
+
break;
|
|
1070
|
+
case "length":
|
|
1071
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
|
1072
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
|
1073
|
+
break;
|
|
1074
|
+
case "includes": {
|
|
1075
|
+
addPattern(
|
|
1076
|
+
res,
|
|
1077
|
+
RegExp(escapeLiteralCheckValue(check.value, refs)),
|
|
1078
|
+
check.message,
|
|
1079
|
+
refs
|
|
1080
|
+
);
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
case "ip": {
|
|
1084
|
+
if (check.version !== "v6") {
|
|
1085
|
+
addFormat(res, "ipv4", check.message, refs);
|
|
1086
|
+
}
|
|
1087
|
+
if (check.version !== "v4") {
|
|
1088
|
+
addFormat(res, "ipv6", check.message, refs);
|
|
1089
|
+
}
|
|
1090
|
+
break;
|
|
1091
|
+
}
|
|
1092
|
+
case "base64url":
|
|
1093
|
+
addPattern(res, zodPatterns.base64url, check.message, refs);
|
|
1094
|
+
break;
|
|
1095
|
+
case "jwt":
|
|
1096
|
+
addPattern(res, zodPatterns.jwt, check.message, refs);
|
|
1097
|
+
break;
|
|
1098
|
+
case "cidr": {
|
|
1099
|
+
if (check.version !== "v6") {
|
|
1100
|
+
addPattern(res, zodPatterns.ipv4Cidr, check.message, refs);
|
|
1101
|
+
}
|
|
1102
|
+
if (check.version !== "v4") {
|
|
1103
|
+
addPattern(res, zodPatterns.ipv6Cidr, check.message, refs);
|
|
1104
|
+
}
|
|
1105
|
+
break;
|
|
1106
|
+
}
|
|
1107
|
+
case "emoji":
|
|
1108
|
+
addPattern(res, zodPatterns.emoji(), check.message, refs);
|
|
1109
|
+
break;
|
|
1110
|
+
case "ulid": {
|
|
1111
|
+
addPattern(res, zodPatterns.ulid, check.message, refs);
|
|
1112
|
+
break;
|
|
1113
|
+
}
|
|
1114
|
+
case "base64": {
|
|
1115
|
+
switch (refs.base64Strategy) {
|
|
1116
|
+
case "format:binary": {
|
|
1117
|
+
addFormat(res, "binary", check.message, refs);
|
|
1118
|
+
break;
|
|
1119
|
+
}
|
|
1120
|
+
case "contentEncoding:base64": {
|
|
1121
|
+
res.contentEncoding = "base64";
|
|
1122
|
+
break;
|
|
1123
|
+
}
|
|
1124
|
+
case "pattern:zod": {
|
|
1125
|
+
addPattern(res, zodPatterns.base64, check.message, refs);
|
|
1126
|
+
break;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
break;
|
|
1130
|
+
}
|
|
1131
|
+
case "nanoid": {
|
|
1132
|
+
addPattern(res, zodPatterns.nanoid, check.message, refs);
|
|
1133
|
+
}
|
|
1134
|
+
case "toLowerCase":
|
|
1135
|
+
case "toUpperCase":
|
|
1136
|
+
case "trim":
|
|
1137
|
+
break;
|
|
1138
|
+
default:
|
|
1139
|
+
/* @__PURE__ */ ((_) => {
|
|
1140
|
+
})(check);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
return res;
|
|
1145
|
+
}
|
|
1146
|
+
function escapeLiteralCheckValue(literal, refs) {
|
|
1147
|
+
return refs.patternStrategy === "escape" ? escapeNonAlphaNumeric(literal) : literal;
|
|
1148
|
+
}
|
|
1149
|
+
var ALPHA_NUMERIC = new Set(
|
|
1150
|
+
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
1151
|
+
);
|
|
1152
|
+
function escapeNonAlphaNumeric(source) {
|
|
1153
|
+
let result = "";
|
|
1154
|
+
for (let i = 0; i < source.length; i++) {
|
|
1155
|
+
if (!ALPHA_NUMERIC.has(source[i])) {
|
|
1156
|
+
result += "\\";
|
|
1157
|
+
}
|
|
1158
|
+
result += source[i];
|
|
1159
|
+
}
|
|
1160
|
+
return result;
|
|
1161
|
+
}
|
|
1162
|
+
function addFormat(schema, value, message, refs) {
|
|
1163
|
+
var _a22;
|
|
1164
|
+
if (schema.format || ((_a22 = schema.anyOf) == null ? void 0 : _a22.some((x) => x.format))) {
|
|
1165
|
+
if (!schema.anyOf) {
|
|
1166
|
+
schema.anyOf = [];
|
|
1167
|
+
}
|
|
1168
|
+
if (schema.format) {
|
|
1169
|
+
schema.anyOf.push({
|
|
1170
|
+
format: schema.format
|
|
1171
|
+
});
|
|
1172
|
+
delete schema.format;
|
|
1173
|
+
}
|
|
1174
|
+
schema.anyOf.push(__spreadValues({
|
|
1175
|
+
format: value
|
|
1176
|
+
}, message && refs.errorMessages && { errorMessage: { format: message } }));
|
|
1177
|
+
} else {
|
|
1178
|
+
schema.format = value;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
function addPattern(schema, regex, message, refs) {
|
|
1182
|
+
var _a22;
|
|
1183
|
+
if (schema.pattern || ((_a22 = schema.allOf) == null ? void 0 : _a22.some((x) => x.pattern))) {
|
|
1184
|
+
if (!schema.allOf) {
|
|
1185
|
+
schema.allOf = [];
|
|
1186
|
+
}
|
|
1187
|
+
if (schema.pattern) {
|
|
1188
|
+
schema.allOf.push({
|
|
1189
|
+
pattern: schema.pattern
|
|
1190
|
+
});
|
|
1191
|
+
delete schema.pattern;
|
|
1192
|
+
}
|
|
1193
|
+
schema.allOf.push(__spreadValues({
|
|
1194
|
+
pattern: stringifyRegExpWithFlags(regex, refs)
|
|
1195
|
+
}, message && refs.errorMessages && { errorMessage: { pattern: message } }));
|
|
1196
|
+
} else {
|
|
1197
|
+
schema.pattern = stringifyRegExpWithFlags(regex, refs);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
function stringifyRegExpWithFlags(regex, refs) {
|
|
1201
|
+
var _a22;
|
|
1202
|
+
if (!refs.applyRegexFlags || !regex.flags) {
|
|
1203
|
+
return regex.source;
|
|
1204
|
+
}
|
|
1205
|
+
const flags = {
|
|
1206
|
+
i: regex.flags.includes("i"),
|
|
1207
|
+
// Case-insensitive
|
|
1208
|
+
m: regex.flags.includes("m"),
|
|
1209
|
+
// `^` and `$` matches adjacent to newline characters
|
|
1210
|
+
s: regex.flags.includes("s")
|
|
1211
|
+
// `.` matches newlines
|
|
1212
|
+
};
|
|
1213
|
+
const source = flags.i ? regex.source.toLowerCase() : regex.source;
|
|
1214
|
+
let pattern = "";
|
|
1215
|
+
let isEscaped = false;
|
|
1216
|
+
let inCharGroup = false;
|
|
1217
|
+
let inCharRange = false;
|
|
1218
|
+
for (let i = 0; i < source.length; i++) {
|
|
1219
|
+
if (isEscaped) {
|
|
1220
|
+
pattern += source[i];
|
|
1221
|
+
isEscaped = false;
|
|
1222
|
+
continue;
|
|
1223
|
+
}
|
|
1224
|
+
if (flags.i) {
|
|
1225
|
+
if (inCharGroup) {
|
|
1226
|
+
if (source[i].match(/[a-z]/)) {
|
|
1227
|
+
if (inCharRange) {
|
|
1228
|
+
pattern += source[i];
|
|
1229
|
+
pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
|
|
1230
|
+
inCharRange = false;
|
|
1231
|
+
} else if (source[i + 1] === "-" && ((_a22 = source[i + 2]) == null ? void 0 : _a22.match(/[a-z]/))) {
|
|
1232
|
+
pattern += source[i];
|
|
1233
|
+
inCharRange = true;
|
|
1234
|
+
} else {
|
|
1235
|
+
pattern += `${source[i]}${source[i].toUpperCase()}`;
|
|
1236
|
+
}
|
|
1237
|
+
continue;
|
|
1238
|
+
}
|
|
1239
|
+
} else if (source[i].match(/[a-z]/)) {
|
|
1240
|
+
pattern += `[${source[i]}${source[i].toUpperCase()}]`;
|
|
1241
|
+
continue;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
if (flags.m) {
|
|
1245
|
+
if (source[i] === "^") {
|
|
1246
|
+
pattern += `(^|(?<=[\r
|
|
1247
|
+
]))`;
|
|
1248
|
+
continue;
|
|
1249
|
+
} else if (source[i] === "$") {
|
|
1250
|
+
pattern += `($|(?=[\r
|
|
1251
|
+
]))`;
|
|
1252
|
+
continue;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
if (flags.s && source[i] === ".") {
|
|
1256
|
+
pattern += inCharGroup ? `${source[i]}\r
|
|
1257
|
+
` : `[${source[i]}\r
|
|
1258
|
+
]`;
|
|
1259
|
+
continue;
|
|
1260
|
+
}
|
|
1261
|
+
pattern += source[i];
|
|
1262
|
+
if (source[i] === "\\") {
|
|
1263
|
+
isEscaped = true;
|
|
1264
|
+
} else if (inCharGroup && source[i] === "]") {
|
|
1265
|
+
inCharGroup = false;
|
|
1266
|
+
} else if (!inCharGroup && source[i] === "[") {
|
|
1267
|
+
inCharGroup = true;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
try {
|
|
1271
|
+
new RegExp(pattern);
|
|
1272
|
+
} catch (e) {
|
|
1273
|
+
console.warn(
|
|
1274
|
+
`Could not convert regex pattern at ${refs.currentPath.join(
|
|
1275
|
+
"/"
|
|
1276
|
+
)} to a flag-independent form! Falling back to the flag-ignorant source`
|
|
1277
|
+
);
|
|
1278
|
+
return regex.source;
|
|
1279
|
+
}
|
|
1280
|
+
return pattern;
|
|
1281
|
+
}
|
|
1282
|
+
function parseRecordDef(def, refs) {
|
|
1283
|
+
var _a22, _b22, _c, _d, _e, _f;
|
|
1284
|
+
const schema = {
|
|
1285
|
+
type: "object",
|
|
1286
|
+
additionalProperties: (_a22 = parseDef(def.valueType._def, __spreadProps(__spreadValues({}, refs), {
|
|
1287
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1288
|
+
}))) != null ? _a22 : refs.allowedAdditionalProperties
|
|
1289
|
+
};
|
|
1290
|
+
if (((_b22 = def.keyType) == null ? void 0 : _b22._def.typeName) === import_v33.ZodFirstPartyTypeKind.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
|
|
1291
|
+
const _a16 = parseStringDef(def.keyType._def, refs), { type } = _a16, keyType = __objRest(_a16, ["type"]);
|
|
1292
|
+
return __spreadProps(__spreadValues({}, schema), {
|
|
1293
|
+
propertyNames: keyType
|
|
1294
|
+
});
|
|
1295
|
+
} else if (((_d = def.keyType) == null ? void 0 : _d._def.typeName) === import_v33.ZodFirstPartyTypeKind.ZodEnum) {
|
|
1296
|
+
return __spreadProps(__spreadValues({}, schema), {
|
|
1297
|
+
propertyNames: {
|
|
1298
|
+
enum: def.keyType._def.values
|
|
1299
|
+
}
|
|
1300
|
+
});
|
|
1301
|
+
} else if (((_e = def.keyType) == null ? void 0 : _e._def.typeName) === import_v33.ZodFirstPartyTypeKind.ZodBranded && def.keyType._def.type._def.typeName === import_v33.ZodFirstPartyTypeKind.ZodString && ((_f = def.keyType._def.type._def.checks) == null ? void 0 : _f.length)) {
|
|
1302
|
+
const _b16 = parseBrandedDef(
|
|
1303
|
+
def.keyType._def,
|
|
1304
|
+
refs
|
|
1305
|
+
), { type } = _b16, keyType = __objRest(_b16, ["type"]);
|
|
1306
|
+
return __spreadProps(__spreadValues({}, schema), {
|
|
1307
|
+
propertyNames: keyType
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
return schema;
|
|
1311
|
+
}
|
|
1312
|
+
function parseMapDef(def, refs) {
|
|
1313
|
+
if (refs.mapStrategy === "record") {
|
|
1314
|
+
return parseRecordDef(def, refs);
|
|
1315
|
+
}
|
|
1316
|
+
const keys = parseDef(def.keyType._def, __spreadProps(__spreadValues({}, refs), {
|
|
1317
|
+
currentPath: [...refs.currentPath, "items", "items", "0"]
|
|
1318
|
+
})) || parseAnyDef();
|
|
1319
|
+
const values = parseDef(def.valueType._def, __spreadProps(__spreadValues({}, refs), {
|
|
1320
|
+
currentPath: [...refs.currentPath, "items", "items", "1"]
|
|
1321
|
+
})) || parseAnyDef();
|
|
1322
|
+
return {
|
|
1323
|
+
type: "array",
|
|
1324
|
+
maxItems: 125,
|
|
1325
|
+
items: {
|
|
1326
|
+
type: "array",
|
|
1327
|
+
items: [keys, values],
|
|
1328
|
+
minItems: 2,
|
|
1329
|
+
maxItems: 2
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
function parseNativeEnumDef(def) {
|
|
1334
|
+
const object = def.values;
|
|
1335
|
+
const actualKeys = Object.keys(def.values).filter((key) => {
|
|
1336
|
+
return typeof object[object[key]] !== "number";
|
|
1337
|
+
});
|
|
1338
|
+
const actualValues = actualKeys.map((key) => object[key]);
|
|
1339
|
+
const parsedTypes = Array.from(
|
|
1340
|
+
new Set(actualValues.map((values) => typeof values))
|
|
1341
|
+
);
|
|
1342
|
+
return {
|
|
1343
|
+
type: parsedTypes.length === 1 ? parsedTypes[0] === "string" ? "string" : "number" : ["string", "number"],
|
|
1344
|
+
enum: actualValues
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
function parseNeverDef() {
|
|
1348
|
+
return { not: parseAnyDef() };
|
|
1349
|
+
}
|
|
1350
|
+
function parseNullDef() {
|
|
1351
|
+
return {
|
|
1352
|
+
type: "null"
|
|
1353
|
+
};
|
|
1354
|
+
}
|
|
1355
|
+
var primitiveMappings = {
|
|
1356
|
+
ZodString: "string",
|
|
1357
|
+
ZodNumber: "number",
|
|
1358
|
+
ZodBigInt: "integer",
|
|
1359
|
+
ZodBoolean: "boolean",
|
|
1360
|
+
ZodNull: "null"
|
|
1361
|
+
};
|
|
1362
|
+
function parseUnionDef(def, refs) {
|
|
1363
|
+
const options = def.options instanceof Map ? Array.from(def.options.values()) : def.options;
|
|
1364
|
+
if (options.every(
|
|
1365
|
+
(x) => x._def.typeName in primitiveMappings && (!x._def.checks || !x._def.checks.length)
|
|
1366
|
+
)) {
|
|
1367
|
+
const types = options.reduce((types2, x) => {
|
|
1368
|
+
const type = primitiveMappings[x._def.typeName];
|
|
1369
|
+
return type && !types2.includes(type) ? [...types2, type] : types2;
|
|
1370
|
+
}, []);
|
|
1371
|
+
return {
|
|
1372
|
+
type: types.length > 1 ? types : types[0]
|
|
1373
|
+
};
|
|
1374
|
+
} else if (options.every((x) => x._def.typeName === "ZodLiteral" && !x.description)) {
|
|
1375
|
+
const types = options.reduce(
|
|
1376
|
+
(acc, x) => {
|
|
1377
|
+
const type = typeof x._def.value;
|
|
1378
|
+
switch (type) {
|
|
1379
|
+
case "string":
|
|
1380
|
+
case "number":
|
|
1381
|
+
case "boolean":
|
|
1382
|
+
return [...acc, type];
|
|
1383
|
+
case "bigint":
|
|
1384
|
+
return [...acc, "integer"];
|
|
1385
|
+
case "object":
|
|
1386
|
+
if (x._def.value === null) return [...acc, "null"];
|
|
1387
|
+
case "symbol":
|
|
1388
|
+
case "undefined":
|
|
1389
|
+
case "function":
|
|
1390
|
+
default:
|
|
1391
|
+
return acc;
|
|
1392
|
+
}
|
|
1393
|
+
},
|
|
1394
|
+
[]
|
|
1395
|
+
);
|
|
1396
|
+
if (types.length === options.length) {
|
|
1397
|
+
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
|
1398
|
+
return {
|
|
1399
|
+
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
|
1400
|
+
enum: options.reduce(
|
|
1401
|
+
(acc, x) => {
|
|
1402
|
+
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
|
1403
|
+
},
|
|
1404
|
+
[]
|
|
1405
|
+
)
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
|
|
1409
|
+
return {
|
|
1410
|
+
type: "string",
|
|
1411
|
+
enum: options.reduce(
|
|
1412
|
+
(acc, x) => [
|
|
1413
|
+
...acc,
|
|
1414
|
+
...x._def.values.filter((x2) => !acc.includes(x2))
|
|
1415
|
+
],
|
|
1416
|
+
[]
|
|
1417
|
+
)
|
|
1418
|
+
};
|
|
1419
|
+
}
|
|
1420
|
+
return asAnyOf(def, refs);
|
|
1421
|
+
}
|
|
1422
|
+
var asAnyOf = (def, refs) => {
|
|
1423
|
+
const anyOf = (def.options instanceof Map ? Array.from(def.options.values()) : def.options).map(
|
|
1424
|
+
(x, i) => parseDef(x._def, __spreadProps(__spreadValues({}, refs), {
|
|
1425
|
+
currentPath: [...refs.currentPath, "anyOf", `${i}`]
|
|
1426
|
+
}))
|
|
1427
|
+
).filter(
|
|
1428
|
+
(x) => !!x && (!refs.strictUnions || typeof x === "object" && Object.keys(x).length > 0)
|
|
1429
|
+
);
|
|
1430
|
+
return anyOf.length ? { anyOf } : void 0;
|
|
1431
|
+
};
|
|
1432
|
+
function parseNullableDef(def, refs) {
|
|
1433
|
+
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
|
|
1434
|
+
def.innerType._def.typeName
|
|
1435
|
+
) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) {
|
|
1436
|
+
return {
|
|
1437
|
+
type: [
|
|
1438
|
+
primitiveMappings[def.innerType._def.typeName],
|
|
1439
|
+
"null"
|
|
1440
|
+
]
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
const base = parseDef(def.innerType._def, __spreadProps(__spreadValues({}, refs), {
|
|
1444
|
+
currentPath: [...refs.currentPath, "anyOf", "0"]
|
|
1445
|
+
}));
|
|
1446
|
+
return base && { anyOf: [base, { type: "null" }] };
|
|
1447
|
+
}
|
|
1448
|
+
function parseNumberDef(def) {
|
|
1449
|
+
const res = {
|
|
1450
|
+
type: "number"
|
|
1451
|
+
};
|
|
1452
|
+
if (!def.checks) return res;
|
|
1453
|
+
for (const check of def.checks) {
|
|
1454
|
+
switch (check.kind) {
|
|
1455
|
+
case "int":
|
|
1456
|
+
res.type = "integer";
|
|
1457
|
+
break;
|
|
1458
|
+
case "min":
|
|
1459
|
+
if (check.inclusive) {
|
|
1460
|
+
res.minimum = check.value;
|
|
1461
|
+
} else {
|
|
1462
|
+
res.exclusiveMinimum = check.value;
|
|
1463
|
+
}
|
|
1464
|
+
break;
|
|
1465
|
+
case "max":
|
|
1466
|
+
if (check.inclusive) {
|
|
1467
|
+
res.maximum = check.value;
|
|
1468
|
+
} else {
|
|
1469
|
+
res.exclusiveMaximum = check.value;
|
|
1470
|
+
}
|
|
1471
|
+
break;
|
|
1472
|
+
case "multipleOf":
|
|
1473
|
+
res.multipleOf = check.value;
|
|
1474
|
+
break;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
return res;
|
|
1478
|
+
}
|
|
1479
|
+
function parseObjectDef(def, refs) {
|
|
1480
|
+
const result = {
|
|
1481
|
+
type: "object",
|
|
1482
|
+
properties: {}
|
|
1483
|
+
};
|
|
1484
|
+
const required = [];
|
|
1485
|
+
const shape = def.shape();
|
|
1486
|
+
for (const propName in shape) {
|
|
1487
|
+
let propDef = shape[propName];
|
|
1488
|
+
if (propDef === void 0 || propDef._def === void 0) {
|
|
1489
|
+
continue;
|
|
1490
|
+
}
|
|
1491
|
+
const propOptional = safeIsOptional(propDef);
|
|
1492
|
+
const parsedDef = parseDef(propDef._def, __spreadProps(__spreadValues({}, refs), {
|
|
1493
|
+
currentPath: [...refs.currentPath, "properties", propName],
|
|
1494
|
+
propertyPath: [...refs.currentPath, "properties", propName]
|
|
1495
|
+
}));
|
|
1496
|
+
if (parsedDef === void 0) {
|
|
1497
|
+
continue;
|
|
1498
|
+
}
|
|
1499
|
+
result.properties[propName] = parsedDef;
|
|
1500
|
+
if (!propOptional) {
|
|
1501
|
+
required.push(propName);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
if (required.length) {
|
|
1505
|
+
result.required = required;
|
|
1506
|
+
}
|
|
1507
|
+
const additionalProperties = decideAdditionalProperties(def, refs);
|
|
1508
|
+
if (additionalProperties !== void 0) {
|
|
1509
|
+
result.additionalProperties = additionalProperties;
|
|
1510
|
+
}
|
|
1511
|
+
return result;
|
|
1512
|
+
}
|
|
1513
|
+
function decideAdditionalProperties(def, refs) {
|
|
1514
|
+
if (def.catchall._def.typeName !== "ZodNever") {
|
|
1515
|
+
return parseDef(def.catchall._def, __spreadProps(__spreadValues({}, refs), {
|
|
1516
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1517
|
+
}));
|
|
1518
|
+
}
|
|
1519
|
+
switch (def.unknownKeys) {
|
|
1520
|
+
case "passthrough":
|
|
1521
|
+
return refs.allowedAdditionalProperties;
|
|
1522
|
+
case "strict":
|
|
1523
|
+
return refs.rejectedAdditionalProperties;
|
|
1524
|
+
case "strip":
|
|
1525
|
+
return refs.removeAdditionalStrategy === "strict" ? refs.allowedAdditionalProperties : refs.rejectedAdditionalProperties;
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
function safeIsOptional(schema) {
|
|
1529
|
+
try {
|
|
1530
|
+
return schema.isOptional();
|
|
1531
|
+
} catch (e) {
|
|
1532
|
+
return true;
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
var parseOptionalDef = (def, refs) => {
|
|
1536
|
+
var _a22;
|
|
1537
|
+
if (refs.currentPath.toString() === ((_a22 = refs.propertyPath) == null ? void 0 : _a22.toString())) {
|
|
1538
|
+
return parseDef(def.innerType._def, refs);
|
|
1539
|
+
}
|
|
1540
|
+
const innerSchema = parseDef(def.innerType._def, __spreadProps(__spreadValues({}, refs), {
|
|
1541
|
+
currentPath: [...refs.currentPath, "anyOf", "1"]
|
|
1542
|
+
}));
|
|
1543
|
+
return innerSchema ? { anyOf: [{ not: parseAnyDef() }, innerSchema] } : parseAnyDef();
|
|
1544
|
+
};
|
|
1545
|
+
var parsePipelineDef = (def, refs) => {
|
|
1546
|
+
if (refs.pipeStrategy === "input") {
|
|
1547
|
+
return parseDef(def.in._def, refs);
|
|
1548
|
+
} else if (refs.pipeStrategy === "output") {
|
|
1549
|
+
return parseDef(def.out._def, refs);
|
|
1550
|
+
}
|
|
1551
|
+
const a = parseDef(def.in._def, __spreadProps(__spreadValues({}, refs), {
|
|
1552
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
1553
|
+
}));
|
|
1554
|
+
const b = parseDef(def.out._def, __spreadProps(__spreadValues({}, refs), {
|
|
1555
|
+
currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
|
|
1556
|
+
}));
|
|
1557
|
+
return {
|
|
1558
|
+
allOf: [a, b].filter((x) => x !== void 0)
|
|
1559
|
+
};
|
|
1560
|
+
};
|
|
1561
|
+
function parsePromiseDef(def, refs) {
|
|
1562
|
+
return parseDef(def.type._def, refs);
|
|
1563
|
+
}
|
|
1564
|
+
function parseSetDef(def, refs) {
|
|
1565
|
+
const items = parseDef(def.valueType._def, __spreadProps(__spreadValues({}, refs), {
|
|
1566
|
+
currentPath: [...refs.currentPath, "items"]
|
|
1567
|
+
}));
|
|
1568
|
+
const schema = {
|
|
1569
|
+
type: "array",
|
|
1570
|
+
uniqueItems: true,
|
|
1571
|
+
items
|
|
1572
|
+
};
|
|
1573
|
+
if (def.minSize) {
|
|
1574
|
+
schema.minItems = def.minSize.value;
|
|
1575
|
+
}
|
|
1576
|
+
if (def.maxSize) {
|
|
1577
|
+
schema.maxItems = def.maxSize.value;
|
|
1578
|
+
}
|
|
1579
|
+
return schema;
|
|
1580
|
+
}
|
|
1581
|
+
function parseTupleDef(def, refs) {
|
|
1582
|
+
if (def.rest) {
|
|
1583
|
+
return {
|
|
1584
|
+
type: "array",
|
|
1585
|
+
minItems: def.items.length,
|
|
1586
|
+
items: def.items.map(
|
|
1587
|
+
(x, i) => parseDef(x._def, __spreadProps(__spreadValues({}, refs), {
|
|
1588
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1589
|
+
}))
|
|
1590
|
+
).reduce(
|
|
1591
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
|
1592
|
+
[]
|
|
1593
|
+
),
|
|
1594
|
+
additionalItems: parseDef(def.rest._def, __spreadProps(__spreadValues({}, refs), {
|
|
1595
|
+
currentPath: [...refs.currentPath, "additionalItems"]
|
|
1596
|
+
}))
|
|
1597
|
+
};
|
|
1598
|
+
} else {
|
|
1599
|
+
return {
|
|
1600
|
+
type: "array",
|
|
1601
|
+
minItems: def.items.length,
|
|
1602
|
+
maxItems: def.items.length,
|
|
1603
|
+
items: def.items.map(
|
|
1604
|
+
(x, i) => parseDef(x._def, __spreadProps(__spreadValues({}, refs), {
|
|
1605
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1606
|
+
}))
|
|
1607
|
+
).reduce(
|
|
1608
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
|
1609
|
+
[]
|
|
1610
|
+
)
|
|
1611
|
+
};
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
function parseUndefinedDef() {
|
|
1615
|
+
return {
|
|
1616
|
+
not: parseAnyDef()
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
function parseUnknownDef() {
|
|
1620
|
+
return parseAnyDef();
|
|
1621
|
+
}
|
|
1622
|
+
var parseReadonlyDef = (def, refs) => {
|
|
1623
|
+
return parseDef(def.innerType._def, refs);
|
|
1624
|
+
};
|
|
1625
|
+
var selectParser = (def, typeName, refs) => {
|
|
1626
|
+
switch (typeName) {
|
|
1627
|
+
case import_v3.ZodFirstPartyTypeKind.ZodString:
|
|
1628
|
+
return parseStringDef(def, refs);
|
|
1629
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNumber:
|
|
1630
|
+
return parseNumberDef(def);
|
|
1631
|
+
case import_v3.ZodFirstPartyTypeKind.ZodObject:
|
|
1632
|
+
return parseObjectDef(def, refs);
|
|
1633
|
+
case import_v3.ZodFirstPartyTypeKind.ZodBigInt:
|
|
1634
|
+
return parseBigintDef(def);
|
|
1635
|
+
case import_v3.ZodFirstPartyTypeKind.ZodBoolean:
|
|
1636
|
+
return parseBooleanDef();
|
|
1637
|
+
case import_v3.ZodFirstPartyTypeKind.ZodDate:
|
|
1638
|
+
return parseDateDef(def, refs);
|
|
1639
|
+
case import_v3.ZodFirstPartyTypeKind.ZodUndefined:
|
|
1640
|
+
return parseUndefinedDef();
|
|
1641
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNull:
|
|
1642
|
+
return parseNullDef();
|
|
1643
|
+
case import_v3.ZodFirstPartyTypeKind.ZodArray:
|
|
1644
|
+
return parseArrayDef(def, refs);
|
|
1645
|
+
case import_v3.ZodFirstPartyTypeKind.ZodUnion:
|
|
1646
|
+
case import_v3.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
1647
|
+
return parseUnionDef(def, refs);
|
|
1648
|
+
case import_v3.ZodFirstPartyTypeKind.ZodIntersection:
|
|
1649
|
+
return parseIntersectionDef(def, refs);
|
|
1650
|
+
case import_v3.ZodFirstPartyTypeKind.ZodTuple:
|
|
1651
|
+
return parseTupleDef(def, refs);
|
|
1652
|
+
case import_v3.ZodFirstPartyTypeKind.ZodRecord:
|
|
1653
|
+
return parseRecordDef(def, refs);
|
|
1654
|
+
case import_v3.ZodFirstPartyTypeKind.ZodLiteral:
|
|
1655
|
+
return parseLiteralDef(def);
|
|
1656
|
+
case import_v3.ZodFirstPartyTypeKind.ZodEnum:
|
|
1657
|
+
return parseEnumDef(def);
|
|
1658
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNativeEnum:
|
|
1659
|
+
return parseNativeEnumDef(def);
|
|
1660
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNullable:
|
|
1661
|
+
return parseNullableDef(def, refs);
|
|
1662
|
+
case import_v3.ZodFirstPartyTypeKind.ZodOptional:
|
|
1663
|
+
return parseOptionalDef(def, refs);
|
|
1664
|
+
case import_v3.ZodFirstPartyTypeKind.ZodMap:
|
|
1665
|
+
return parseMapDef(def, refs);
|
|
1666
|
+
case import_v3.ZodFirstPartyTypeKind.ZodSet:
|
|
1667
|
+
return parseSetDef(def, refs);
|
|
1668
|
+
case import_v3.ZodFirstPartyTypeKind.ZodLazy:
|
|
1669
|
+
return () => def.getter()._def;
|
|
1670
|
+
case import_v3.ZodFirstPartyTypeKind.ZodPromise:
|
|
1671
|
+
return parsePromiseDef(def, refs);
|
|
1672
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNaN:
|
|
1673
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNever:
|
|
1674
|
+
return parseNeverDef();
|
|
1675
|
+
case import_v3.ZodFirstPartyTypeKind.ZodEffects:
|
|
1676
|
+
return parseEffectsDef(def, refs);
|
|
1677
|
+
case import_v3.ZodFirstPartyTypeKind.ZodAny:
|
|
1678
|
+
return parseAnyDef();
|
|
1679
|
+
case import_v3.ZodFirstPartyTypeKind.ZodUnknown:
|
|
1680
|
+
return parseUnknownDef();
|
|
1681
|
+
case import_v3.ZodFirstPartyTypeKind.ZodDefault:
|
|
1682
|
+
return parseDefaultDef(def, refs);
|
|
1683
|
+
case import_v3.ZodFirstPartyTypeKind.ZodBranded:
|
|
1684
|
+
return parseBrandedDef(def, refs);
|
|
1685
|
+
case import_v3.ZodFirstPartyTypeKind.ZodReadonly:
|
|
1686
|
+
return parseReadonlyDef(def, refs);
|
|
1687
|
+
case import_v3.ZodFirstPartyTypeKind.ZodCatch:
|
|
1688
|
+
return parseCatchDef(def, refs);
|
|
1689
|
+
case import_v3.ZodFirstPartyTypeKind.ZodPipeline:
|
|
1690
|
+
return parsePipelineDef(def, refs);
|
|
1691
|
+
case import_v3.ZodFirstPartyTypeKind.ZodFunction:
|
|
1692
|
+
case import_v3.ZodFirstPartyTypeKind.ZodVoid:
|
|
1693
|
+
case import_v3.ZodFirstPartyTypeKind.ZodSymbol:
|
|
1694
|
+
return void 0;
|
|
1695
|
+
default:
|
|
1696
|
+
return /* @__PURE__ */ ((_) => void 0)(typeName);
|
|
1697
|
+
}
|
|
1698
|
+
};
|
|
1699
|
+
var getRelativePath = (pathA, pathB) => {
|
|
1700
|
+
let i = 0;
|
|
1701
|
+
for (; i < pathA.length && i < pathB.length; i++) {
|
|
1702
|
+
if (pathA[i] !== pathB[i]) break;
|
|
1703
|
+
}
|
|
1704
|
+
return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
|
|
1705
|
+
};
|
|
1706
|
+
function parseDef(def, refs, forceResolution = false) {
|
|
1707
|
+
var _a22;
|
|
1708
|
+
const seenItem = refs.seen.get(def);
|
|
1709
|
+
if (refs.override) {
|
|
1710
|
+
const overrideResult = (_a22 = refs.override) == null ? void 0 : _a22.call(
|
|
1711
|
+
refs,
|
|
1712
|
+
def,
|
|
1713
|
+
refs,
|
|
1714
|
+
seenItem,
|
|
1715
|
+
forceResolution
|
|
1716
|
+
);
|
|
1717
|
+
if (overrideResult !== ignoreOverride) {
|
|
1718
|
+
return overrideResult;
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
if (seenItem && !forceResolution) {
|
|
1722
|
+
const seenSchema = get$ref(seenItem, refs);
|
|
1723
|
+
if (seenSchema !== void 0) {
|
|
1724
|
+
return seenSchema;
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
const newItem = { def, path: refs.currentPath, jsonSchema: void 0 };
|
|
1728
|
+
refs.seen.set(def, newItem);
|
|
1729
|
+
const jsonSchemaOrGetter = selectParser(def, def.typeName, refs);
|
|
1730
|
+
const jsonSchema2 = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
|
|
1731
|
+
if (jsonSchema2) {
|
|
1732
|
+
addMeta(def, refs, jsonSchema2);
|
|
1733
|
+
}
|
|
1734
|
+
if (refs.postProcess) {
|
|
1735
|
+
const postProcessResult = refs.postProcess(jsonSchema2, def, refs);
|
|
1736
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1737
|
+
return postProcessResult;
|
|
1738
|
+
}
|
|
1739
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1740
|
+
return jsonSchema2;
|
|
1741
|
+
}
|
|
1742
|
+
var get$ref = (item, refs) => {
|
|
1743
|
+
switch (refs.$refStrategy) {
|
|
1744
|
+
case "root":
|
|
1745
|
+
return { $ref: item.path.join("/") };
|
|
1746
|
+
case "relative":
|
|
1747
|
+
return { $ref: getRelativePath(refs.currentPath, item.path) };
|
|
1748
|
+
case "none":
|
|
1749
|
+
case "seen": {
|
|
1750
|
+
if (item.path.length < refs.currentPath.length && item.path.every((value, index) => refs.currentPath[index] === value)) {
|
|
1751
|
+
console.warn(
|
|
1752
|
+
`Recursive reference detected at ${refs.currentPath.join(
|
|
1753
|
+
"/"
|
|
1754
|
+
)}! Defaulting to any`
|
|
1755
|
+
);
|
|
1756
|
+
return parseAnyDef();
|
|
1757
|
+
}
|
|
1758
|
+
return refs.$refStrategy === "seen" ? parseAnyDef() : void 0;
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
};
|
|
1762
|
+
var addMeta = (def, refs, jsonSchema2) => {
|
|
1763
|
+
if (def.description) {
|
|
1764
|
+
jsonSchema2.description = def.description;
|
|
1765
|
+
}
|
|
1766
|
+
return jsonSchema2;
|
|
1767
|
+
};
|
|
1768
|
+
var getRefs = (options) => {
|
|
1769
|
+
const _options = getDefaultOptions(options);
|
|
1770
|
+
const currentPath = _options.name !== void 0 ? [..._options.basePath, _options.definitionPath, _options.name] : _options.basePath;
|
|
1771
|
+
return __spreadProps(__spreadValues({}, _options), {
|
|
1772
|
+
currentPath,
|
|
1773
|
+
propertyPath: void 0,
|
|
1774
|
+
seen: new Map(
|
|
1775
|
+
Object.entries(_options.definitions).map(([name22, def]) => [
|
|
1776
|
+
def._def,
|
|
1777
|
+
{
|
|
1778
|
+
def: def._def,
|
|
1779
|
+
path: [..._options.basePath, _options.definitionPath, name22],
|
|
1780
|
+
// Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
|
|
1781
|
+
jsonSchema: void 0
|
|
1782
|
+
}
|
|
1783
|
+
])
|
|
1784
|
+
)
|
|
1785
|
+
});
|
|
1786
|
+
};
|
|
1787
|
+
var zod3ToJsonSchema = (schema, options) => {
|
|
1788
|
+
var _a22;
|
|
1789
|
+
const refs = getRefs(options);
|
|
1790
|
+
let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce(
|
|
1791
|
+
(acc, [name32, schema2]) => {
|
|
1792
|
+
var _a32;
|
|
1793
|
+
return __spreadProps(__spreadValues({}, acc), {
|
|
1794
|
+
[name32]: (_a32 = parseDef(
|
|
1795
|
+
schema2._def,
|
|
1796
|
+
__spreadProps(__spreadValues({}, refs), {
|
|
1797
|
+
currentPath: [...refs.basePath, refs.definitionPath, name32]
|
|
1798
|
+
}),
|
|
1799
|
+
true
|
|
1800
|
+
)) != null ? _a32 : parseAnyDef()
|
|
1801
|
+
});
|
|
1802
|
+
},
|
|
1803
|
+
{}
|
|
1804
|
+
) : void 0;
|
|
1805
|
+
const name22 = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
|
|
1806
|
+
const main = (_a22 = parseDef(
|
|
1807
|
+
schema._def,
|
|
1808
|
+
name22 === void 0 ? refs : __spreadProps(__spreadValues({}, refs), {
|
|
1809
|
+
currentPath: [...refs.basePath, refs.definitionPath, name22]
|
|
1810
|
+
}),
|
|
1811
|
+
false
|
|
1812
|
+
)) != null ? _a22 : parseAnyDef();
|
|
1813
|
+
const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
|
|
1814
|
+
if (title !== void 0) {
|
|
1815
|
+
main.title = title;
|
|
1816
|
+
}
|
|
1817
|
+
const combined = name22 === void 0 ? definitions ? __spreadProps(__spreadValues({}, main), {
|
|
1818
|
+
[refs.definitionPath]: definitions
|
|
1819
|
+
}) : main : {
|
|
1820
|
+
$ref: [
|
|
1821
|
+
...refs.$refStrategy === "relative" ? [] : refs.basePath,
|
|
1822
|
+
refs.definitionPath,
|
|
1823
|
+
name22
|
|
1824
|
+
].join("/"),
|
|
1825
|
+
[refs.definitionPath]: __spreadProps(__spreadValues({}, definitions), {
|
|
1826
|
+
[name22]: main
|
|
1827
|
+
})
|
|
1828
|
+
};
|
|
1829
|
+
combined.$schema = "http://json-schema.org/draft-07/schema#";
|
|
1830
|
+
return combined;
|
|
1831
|
+
};
|
|
1832
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
|
1833
|
+
function jsonSchema(jsonSchema2, {
|
|
1834
|
+
validate
|
|
1835
|
+
} = {}) {
|
|
1836
|
+
return {
|
|
1837
|
+
[schemaSymbol]: true,
|
|
1838
|
+
_type: void 0,
|
|
1839
|
+
// should never be used directly
|
|
1840
|
+
get jsonSchema() {
|
|
1841
|
+
if (typeof jsonSchema2 === "function") {
|
|
1842
|
+
jsonSchema2 = jsonSchema2();
|
|
1843
|
+
}
|
|
1844
|
+
return jsonSchema2;
|
|
1845
|
+
},
|
|
1846
|
+
validate
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
function isSchema(value) {
|
|
1850
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
|
1851
|
+
}
|
|
1852
|
+
function asSchema(schema) {
|
|
1853
|
+
return schema == null ? jsonSchema({ properties: {}, additionalProperties: false }) : isSchema(schema) ? schema : "~standard" in schema ? schema["~standard"].vendor === "zod" ? zodSchema(schema) : standardSchema(schema) : schema();
|
|
1854
|
+
}
|
|
1855
|
+
function standardSchema(standardSchema2) {
|
|
1856
|
+
return jsonSchema(
|
|
1857
|
+
() => standardSchema2["~standard"].jsonSchema.input({
|
|
1858
|
+
target: "draft-07"
|
|
1859
|
+
}),
|
|
1860
|
+
{
|
|
1861
|
+
validate: async (value) => {
|
|
1862
|
+
const result = await standardSchema2["~standard"].validate(value);
|
|
1863
|
+
return "value" in result ? { success: true, value: result.value } : {
|
|
1864
|
+
success: false,
|
|
1865
|
+
error: new TypeValidationError({
|
|
1866
|
+
value,
|
|
1867
|
+
cause: result.issues
|
|
1868
|
+
})
|
|
1869
|
+
};
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
);
|
|
1873
|
+
}
|
|
1874
|
+
function zod3Schema(zodSchema2, options) {
|
|
1875
|
+
var _a22;
|
|
1876
|
+
const useReferences = (_a22 = options == null ? void 0 : options.useReferences) != null ? _a22 : false;
|
|
1877
|
+
return jsonSchema(
|
|
1878
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
1879
|
+
() => zod3ToJsonSchema(zodSchema2, {
|
|
1880
|
+
$refStrategy: useReferences ? "root" : "none"
|
|
1881
|
+
}),
|
|
1882
|
+
{
|
|
1883
|
+
validate: async (value) => {
|
|
1884
|
+
const result = await zodSchema2.safeParseAsync(value);
|
|
1885
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
);
|
|
1889
|
+
}
|
|
1890
|
+
function zod4Schema(zodSchema2, options) {
|
|
1891
|
+
var _a22;
|
|
1892
|
+
const useReferences = (_a22 = options == null ? void 0 : options.useReferences) != null ? _a22 : false;
|
|
1893
|
+
return jsonSchema(
|
|
1894
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
1895
|
+
() => addAdditionalPropertiesToJsonSchema(
|
|
1896
|
+
z4.toJSONSchema(zodSchema2, {
|
|
1897
|
+
target: "draft-7",
|
|
1898
|
+
io: "input",
|
|
1899
|
+
reused: useReferences ? "ref" : "inline"
|
|
1900
|
+
})
|
|
1901
|
+
),
|
|
1902
|
+
{
|
|
1903
|
+
validate: async (value) => {
|
|
1904
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
|
1905
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
);
|
|
1909
|
+
}
|
|
1910
|
+
function isZod4Schema(zodSchema2) {
|
|
1911
|
+
return "_zod" in zodSchema2;
|
|
1912
|
+
}
|
|
1913
|
+
function zodSchema(zodSchema2, options) {
|
|
1914
|
+
if (isZod4Schema(zodSchema2)) {
|
|
1915
|
+
return zod4Schema(zodSchema2, options);
|
|
1916
|
+
} else {
|
|
1917
|
+
return zod3Schema(zodSchema2, options);
|
|
1918
|
+
}
|
|
645
1919
|
}
|
|
646
1920
|
async function validateTypes({
|
|
647
1921
|
value,
|
|
@@ -657,12 +1931,12 @@ async function safeValidateTypes({
|
|
|
657
1931
|
value,
|
|
658
1932
|
schema
|
|
659
1933
|
}) {
|
|
660
|
-
const
|
|
1934
|
+
const actualSchema = asSchema(schema);
|
|
661
1935
|
try {
|
|
662
|
-
if (
|
|
1936
|
+
if (actualSchema.validate == null) {
|
|
663
1937
|
return { success: true, value, rawValue: value };
|
|
664
1938
|
}
|
|
665
|
-
const result = await
|
|
1939
|
+
const result = await actualSchema.validate(value);
|
|
666
1940
|
if (result.success) {
|
|
667
1941
|
return { success: true, value: result.value, rawValue: value };
|
|
668
1942
|
}
|
|
@@ -745,7 +2019,7 @@ var postJsonToApi = async ({
|
|
|
745
2019
|
failedResponseHandler,
|
|
746
2020
|
successfulResponseHandler,
|
|
747
2021
|
abortSignal,
|
|
748
|
-
fetch
|
|
2022
|
+
fetch: fetch2
|
|
749
2023
|
}) => postToApi({
|
|
750
2024
|
url,
|
|
751
2025
|
headers: __spreadValues({
|
|
@@ -758,7 +2032,7 @@ var postJsonToApi = async ({
|
|
|
758
2032
|
failedResponseHandler,
|
|
759
2033
|
successfulResponseHandler,
|
|
760
2034
|
abortSignal,
|
|
761
|
-
fetch
|
|
2035
|
+
fetch: fetch2
|
|
762
2036
|
});
|
|
763
2037
|
var postToApi = async ({
|
|
764
2038
|
url,
|
|
@@ -767,12 +2041,16 @@ var postToApi = async ({
|
|
|
767
2041
|
successfulResponseHandler,
|
|
768
2042
|
failedResponseHandler,
|
|
769
2043
|
abortSignal,
|
|
770
|
-
fetch = getOriginalFetch2()
|
|
2044
|
+
fetch: fetch2 = getOriginalFetch2()
|
|
771
2045
|
}) => {
|
|
772
2046
|
try {
|
|
773
|
-
const response = await
|
|
2047
|
+
const response = await fetch2(url, {
|
|
774
2048
|
method: "POST",
|
|
775
|
-
headers:
|
|
2049
|
+
headers: withUserAgentSuffix(
|
|
2050
|
+
headers,
|
|
2051
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
|
2052
|
+
getRuntimeEnvironmentUserAgent()
|
|
2053
|
+
),
|
|
776
2054
|
body: body.content,
|
|
777
2055
|
signal: abortSignal
|
|
778
2056
|
});
|
|
@@ -916,15 +2194,6 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
|
916
2194
|
rawValue: parsedResult.rawValue
|
|
917
2195
|
};
|
|
918
2196
|
};
|
|
919
|
-
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
|
920
|
-
var { btoa, atob } = globalThis;
|
|
921
|
-
function convertUint8ArrayToBase64(array) {
|
|
922
|
-
let latin1string = "";
|
|
923
|
-
for (let i = 0; i < array.length; i++) {
|
|
924
|
-
latin1string += String.fromCodePoint(array[i]);
|
|
925
|
-
}
|
|
926
|
-
return btoa(latin1string);
|
|
927
|
-
}
|
|
928
2197
|
function withoutTrailingSlash(url) {
|
|
929
2198
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
930
2199
|
}
|
|
@@ -1055,7 +2324,7 @@ var OpenRouterProviderOptionsSchema = import_v43.z.object({
|
|
|
1055
2324
|
}).optional();
|
|
1056
2325
|
|
|
1057
2326
|
// src/utils/map-finish-reason.ts
|
|
1058
|
-
function
|
|
2327
|
+
function mapToUnified(finishReason) {
|
|
1059
2328
|
switch (finishReason) {
|
|
1060
2329
|
case "stop":
|
|
1061
2330
|
return "stop";
|
|
@@ -1067,9 +2336,18 @@ function mapOpenRouterFinishReason(finishReason) {
|
|
|
1067
2336
|
case "tool_calls":
|
|
1068
2337
|
return "tool-calls";
|
|
1069
2338
|
default:
|
|
1070
|
-
return "
|
|
2339
|
+
return "other";
|
|
1071
2340
|
}
|
|
1072
2341
|
}
|
|
2342
|
+
function mapOpenRouterFinishReason(finishReason) {
|
|
2343
|
+
return {
|
|
2344
|
+
unified: mapToUnified(finishReason),
|
|
2345
|
+
raw: finishReason != null ? finishReason : void 0
|
|
2346
|
+
};
|
|
2347
|
+
}
|
|
2348
|
+
function createFinishReason(unified, raw) {
|
|
2349
|
+
return { unified, raw };
|
|
2350
|
+
}
|
|
1073
2351
|
|
|
1074
2352
|
// src/types/openrouter-chat-completions-input.ts
|
|
1075
2353
|
var OPENROUTER_AUDIO_FORMATS = [
|
|
@@ -1102,10 +2380,10 @@ function getFileUrl({
|
|
|
1102
2380
|
part,
|
|
1103
2381
|
defaultMediaType
|
|
1104
2382
|
}) {
|
|
1105
|
-
var
|
|
2383
|
+
var _a16, _b16;
|
|
1106
2384
|
if (part.data instanceof Uint8Array) {
|
|
1107
2385
|
const base64 = convertUint8ArrayToBase64(part.data);
|
|
1108
|
-
return `data:${(
|
|
2386
|
+
return `data:${(_a16 = part.mediaType) != null ? _a16 : defaultMediaType};base64,${base64}`;
|
|
1109
2387
|
}
|
|
1110
2388
|
const stringUrl = part.data.toString();
|
|
1111
2389
|
if (isUrl({
|
|
@@ -1114,12 +2392,12 @@ function getFileUrl({
|
|
|
1114
2392
|
})) {
|
|
1115
2393
|
return stringUrl;
|
|
1116
2394
|
}
|
|
1117
|
-
return stringUrl.startsWith("data:") ? stringUrl : `data:${(
|
|
2395
|
+
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b16 = part.mediaType) != null ? _b16 : defaultMediaType};base64,${stringUrl}`;
|
|
1118
2396
|
}
|
|
1119
2397
|
function getMediaType(dataUrl, defaultMediaType) {
|
|
1120
|
-
var
|
|
2398
|
+
var _a16;
|
|
1121
2399
|
const match = dataUrl.match(/^data:([^;]+)/);
|
|
1122
|
-
return match ? (
|
|
2400
|
+
return match ? (_a16 = match[1]) != null ? _a16 : defaultMediaType : defaultMediaType;
|
|
1123
2401
|
}
|
|
1124
2402
|
function getBase64FromDataUrl(dataUrl) {
|
|
1125
2403
|
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
@@ -1194,13 +2472,13 @@ Learn more: https://openrouter.ai/docs/features/multimodal/audio`
|
|
|
1194
2472
|
|
|
1195
2473
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
1196
2474
|
function getCacheControl(providerMetadata) {
|
|
1197
|
-
var
|
|
2475
|
+
var _a16, _b16, _c;
|
|
1198
2476
|
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
1199
2477
|
const openrouter2 = providerMetadata == null ? void 0 : providerMetadata.openrouter;
|
|
1200
|
-
return (_c = (
|
|
2478
|
+
return (_c = (_b16 = (_a16 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a16 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b16 : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
1201
2479
|
}
|
|
1202
2480
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1203
|
-
var
|
|
2481
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1204
2482
|
const messages = [];
|
|
1205
2483
|
for (const { role, content, providerOptions } of prompt) {
|
|
1206
2484
|
switch (role) {
|
|
@@ -1213,8 +2491,8 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1213
2491
|
break;
|
|
1214
2492
|
}
|
|
1215
2493
|
case "user": {
|
|
1216
|
-
if (content.length === 1 && ((
|
|
1217
|
-
const cacheControl = (
|
|
2494
|
+
if (content.length === 1 && ((_a16 = content[0]) == null ? void 0 : _a16.type) === "text") {
|
|
2495
|
+
const cacheControl = (_b16 = getCacheControl(providerOptions)) != null ? _b16 : getCacheControl(content[0].providerOptions);
|
|
1218
2496
|
const contentWithCacheControl = cacheControl ? [
|
|
1219
2497
|
{
|
|
1220
2498
|
type: "text",
|
|
@@ -1231,8 +2509,8 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1231
2509
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1232
2510
|
const contentParts = content.map(
|
|
1233
2511
|
(part) => {
|
|
1234
|
-
var
|
|
1235
|
-
const cacheControl = (
|
|
2512
|
+
var _a17, _b17, _c2, _d2, _e2, _f2, _g2;
|
|
2513
|
+
const cacheControl = (_a17 = getCacheControl(part.providerOptions)) != null ? _a17 : messageCacheControl;
|
|
1236
2514
|
switch (part.type) {
|
|
1237
2515
|
case "text":
|
|
1238
2516
|
return {
|
|
@@ -1242,7 +2520,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1242
2520
|
cache_control: cacheControl
|
|
1243
2521
|
};
|
|
1244
2522
|
case "file": {
|
|
1245
|
-
if ((
|
|
2523
|
+
if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
|
|
1246
2524
|
const url = getFileUrl({
|
|
1247
2525
|
part,
|
|
1248
2526
|
defaultMediaType: "image/jpeg"
|
|
@@ -1369,6 +2647,9 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1369
2647
|
}
|
|
1370
2648
|
case "tool": {
|
|
1371
2649
|
for (const toolResponse of content) {
|
|
2650
|
+
if (toolResponse.type === "tool-approval-response") {
|
|
2651
|
+
continue;
|
|
2652
|
+
}
|
|
1372
2653
|
const content2 = getToolResultContent(toolResponse);
|
|
1373
2654
|
messages.push({
|
|
1374
2655
|
role: "tool",
|
|
@@ -1387,7 +2668,18 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1387
2668
|
return messages;
|
|
1388
2669
|
}
|
|
1389
2670
|
function getToolResultContent(input) {
|
|
1390
|
-
|
|
2671
|
+
var _a16;
|
|
2672
|
+
switch (input.output.type) {
|
|
2673
|
+
case "text":
|
|
2674
|
+
case "error-text":
|
|
2675
|
+
return input.output.value;
|
|
2676
|
+
case "json":
|
|
2677
|
+
case "error-json":
|
|
2678
|
+
case "content":
|
|
2679
|
+
return JSON.stringify(input.output.value);
|
|
2680
|
+
case "execution-denied":
|
|
2681
|
+
return (_a16 = input.output.reason) != null ? _a16 : "Tool execution denied";
|
|
2682
|
+
}
|
|
1391
2683
|
}
|
|
1392
2684
|
|
|
1393
2685
|
// src/chat/get-tool-choice.ts
|
|
@@ -1630,7 +2922,7 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
|
1630
2922
|
// src/chat/index.ts
|
|
1631
2923
|
var OpenRouterChatLanguageModel = class {
|
|
1632
2924
|
constructor(modelId, settings, config) {
|
|
1633
|
-
this.specificationVersion = "
|
|
2925
|
+
this.specificationVersion = "v3";
|
|
1634
2926
|
this.provider = "openrouter";
|
|
1635
2927
|
this.defaultObjectGenerationMode = "tool";
|
|
1636
2928
|
this.supportsImageUrls = true;
|
|
@@ -1660,7 +2952,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1660
2952
|
tools,
|
|
1661
2953
|
toolChoice
|
|
1662
2954
|
}) {
|
|
1663
|
-
var
|
|
2955
|
+
var _a16;
|
|
1664
2956
|
const baseArgs = __spreadValues(__spreadValues({
|
|
1665
2957
|
// model id:
|
|
1666
2958
|
model: this.modelId,
|
|
@@ -1684,7 +2976,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1684
2976
|
json_schema: __spreadValues({
|
|
1685
2977
|
schema: responseFormat.schema,
|
|
1686
2978
|
strict: true,
|
|
1687
|
-
name: (
|
|
2979
|
+
name: (_a16 = responseFormat.name) != null ? _a16 : "response"
|
|
1688
2980
|
}, responseFormat.description && {
|
|
1689
2981
|
description: responseFormat.description
|
|
1690
2982
|
})
|
|
@@ -1723,7 +3015,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1723
3015
|
return baseArgs;
|
|
1724
3016
|
}
|
|
1725
3017
|
async doGenerate(options) {
|
|
1726
|
-
var
|
|
3018
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
1727
3019
|
const providerOptions = options.providerOptions || {};
|
|
1728
3020
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1729
3021
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1763,19 +3055,31 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1763
3055
|
});
|
|
1764
3056
|
}
|
|
1765
3057
|
const usageInfo = response.usage ? {
|
|
1766
|
-
inputTokens:
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
3058
|
+
inputTokens: {
|
|
3059
|
+
total: (_a16 = response.usage.prompt_tokens) != null ? _a16 : 0,
|
|
3060
|
+
noCache: void 0,
|
|
3061
|
+
cacheRead: (_c = (_b16 = response.usage.prompt_tokens_details) == null ? void 0 : _b16.cached_tokens) != null ? _c : void 0,
|
|
3062
|
+
cacheWrite: void 0
|
|
3063
|
+
},
|
|
3064
|
+
outputTokens: {
|
|
3065
|
+
total: (_d = response.usage.completion_tokens) != null ? _d : 0,
|
|
3066
|
+
text: void 0,
|
|
3067
|
+
reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
|
|
3068
|
+
}
|
|
1771
3069
|
} : {
|
|
1772
|
-
inputTokens:
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
3070
|
+
inputTokens: {
|
|
3071
|
+
total: 0,
|
|
3072
|
+
noCache: void 0,
|
|
3073
|
+
cacheRead: void 0,
|
|
3074
|
+
cacheWrite: void 0
|
|
3075
|
+
},
|
|
3076
|
+
outputTokens: {
|
|
3077
|
+
total: 0,
|
|
3078
|
+
text: void 0,
|
|
3079
|
+
reasoning: void 0
|
|
3080
|
+
}
|
|
1777
3081
|
};
|
|
1778
|
-
const reasoningDetails = (
|
|
3082
|
+
const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
|
|
1779
3083
|
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
1780
3084
|
switch (detail.type) {
|
|
1781
3085
|
case "reasoning.text" /* Text */: {
|
|
@@ -1843,7 +3147,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1843
3147
|
for (const toolCall of choice.message.tool_calls) {
|
|
1844
3148
|
content.push({
|
|
1845
3149
|
type: "tool-call",
|
|
1846
|
-
toolCallId: (
|
|
3150
|
+
toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
|
|
1847
3151
|
toolName: toolCall.function.name,
|
|
1848
3152
|
input: toolCall.function.arguments,
|
|
1849
3153
|
providerMetadata: {
|
|
@@ -1881,7 +3185,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1881
3185
|
}
|
|
1882
3186
|
}
|
|
1883
3187
|
}
|
|
1884
|
-
const fileAnnotations = (
|
|
3188
|
+
const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
|
|
1885
3189
|
(a) => a.type === "file"
|
|
1886
3190
|
);
|
|
1887
3191
|
const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
|
|
@@ -1889,7 +3193,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1889
3193
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
1890
3194
|
);
|
|
1891
3195
|
const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
|
|
1892
|
-
const effectiveFinishReason = shouldOverrideFinishReason ? "tool-calls" : mapOpenRouterFinishReason(choice.finish_reason);
|
|
3196
|
+
const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
|
|
1893
3197
|
return {
|
|
1894
3198
|
content,
|
|
1895
3199
|
finishReason: effectiveFinishReason,
|
|
@@ -1897,13 +3201,13 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1897
3201
|
warnings: [],
|
|
1898
3202
|
providerMetadata: {
|
|
1899
3203
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1900
|
-
provider: (
|
|
1901
|
-
reasoning_details: (
|
|
3204
|
+
provider: (_k = response.provider) != null ? _k : "",
|
|
3205
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1902
3206
|
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
1903
3207
|
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
1904
|
-
promptTokens: (
|
|
1905
|
-
completionTokens: (
|
|
1906
|
-
totalTokens: (_p = usageInfo.
|
|
3208
|
+
promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
|
|
3209
|
+
completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
|
|
3210
|
+
totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
|
|
1907
3211
|
cost: (_q = response.usage) == null ? void 0 : _q.cost
|
|
1908
3212
|
}, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
|
|
1909
3213
|
promptTokensDetails: {
|
|
@@ -1929,7 +3233,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1929
3233
|
};
|
|
1930
3234
|
}
|
|
1931
3235
|
async doStream(options) {
|
|
1932
|
-
var
|
|
3236
|
+
var _a16;
|
|
1933
3237
|
const providerOptions = options.providerOptions || {};
|
|
1934
3238
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1935
3239
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1944,7 +3248,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1944
3248
|
// only include stream_options when in strict compatibility mode:
|
|
1945
3249
|
stream_options: this.config.compatibility === "strict" ? __spreadValues({
|
|
1946
3250
|
include_usage: true
|
|
1947
|
-
}, ((
|
|
3251
|
+
}, ((_a16 = this.settings.usage) == null ? void 0 : _a16.include) ? { include_usage: true } : {}) : void 0
|
|
1948
3252
|
}),
|
|
1949
3253
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
1950
3254
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
@@ -1954,13 +3258,19 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1954
3258
|
fetch: this.config.fetch
|
|
1955
3259
|
});
|
|
1956
3260
|
const toolCalls = [];
|
|
1957
|
-
let finishReason = "other";
|
|
3261
|
+
let finishReason = createFinishReason("other");
|
|
1958
3262
|
const usage = {
|
|
1959
|
-
inputTokens:
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
3263
|
+
inputTokens: {
|
|
3264
|
+
total: void 0,
|
|
3265
|
+
noCache: void 0,
|
|
3266
|
+
cacheRead: void 0,
|
|
3267
|
+
cacheWrite: void 0
|
|
3268
|
+
},
|
|
3269
|
+
outputTokens: {
|
|
3270
|
+
total: void 0,
|
|
3271
|
+
text: void 0,
|
|
3272
|
+
reasoning: void 0
|
|
3273
|
+
}
|
|
1964
3274
|
};
|
|
1965
3275
|
const openrouterUsage = {};
|
|
1966
3276
|
const accumulatedReasoningDetails = [];
|
|
@@ -1975,15 +3285,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1975
3285
|
stream: response.pipeThrough(
|
|
1976
3286
|
new TransformStream({
|
|
1977
3287
|
transform(chunk, controller) {
|
|
1978
|
-
var
|
|
3288
|
+
var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
1979
3289
|
if (!chunk.success) {
|
|
1980
|
-
finishReason = "error";
|
|
3290
|
+
finishReason = createFinishReason("error");
|
|
1981
3291
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
1982
3292
|
return;
|
|
1983
3293
|
}
|
|
1984
3294
|
const value = chunk.value;
|
|
1985
3295
|
if ("error" in value) {
|
|
1986
|
-
finishReason = "error";
|
|
3296
|
+
finishReason = createFinishReason("error");
|
|
1987
3297
|
controller.enqueue({ type: "error", error: value.error });
|
|
1988
3298
|
return;
|
|
1989
3299
|
}
|
|
@@ -2004,21 +3314,20 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2004
3314
|
});
|
|
2005
3315
|
}
|
|
2006
3316
|
if (value.usage != null) {
|
|
2007
|
-
usage.inputTokens = value.usage.prompt_tokens;
|
|
2008
|
-
usage.outputTokens = value.usage.completion_tokens;
|
|
2009
|
-
usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
|
|
3317
|
+
usage.inputTokens.total = value.usage.prompt_tokens;
|
|
3318
|
+
usage.outputTokens.total = value.usage.completion_tokens;
|
|
2010
3319
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
2011
3320
|
if (value.usage.prompt_tokens_details) {
|
|
2012
|
-
const cachedInputTokens = (
|
|
2013
|
-
usage.
|
|
3321
|
+
const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
|
|
3322
|
+
usage.inputTokens.cacheRead = cachedInputTokens;
|
|
2014
3323
|
openrouterUsage.promptTokensDetails = {
|
|
2015
3324
|
cachedTokens: cachedInputTokens
|
|
2016
3325
|
};
|
|
2017
3326
|
}
|
|
2018
3327
|
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
2019
3328
|
if (value.usage.completion_tokens_details) {
|
|
2020
|
-
const reasoningTokens = (
|
|
2021
|
-
usage.
|
|
3329
|
+
const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
|
|
3330
|
+
usage.outputTokens.reasoning = reasoningTokens;
|
|
2022
3331
|
openrouterUsage.completionTokensDetails = {
|
|
2023
3332
|
reasoningTokens
|
|
2024
3333
|
};
|
|
@@ -2277,20 +3586,20 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2277
3586
|
}
|
|
2278
3587
|
},
|
|
2279
3588
|
flush(controller) {
|
|
2280
|
-
var
|
|
3589
|
+
var _a17;
|
|
2281
3590
|
const hasToolCalls = toolCalls.length > 0;
|
|
2282
3591
|
const hasEncryptedReasoning = accumulatedReasoningDetails.some(
|
|
2283
3592
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
2284
3593
|
);
|
|
2285
|
-
if (hasToolCalls && hasEncryptedReasoning && finishReason === "stop") {
|
|
2286
|
-
finishReason = "tool-calls";
|
|
3594
|
+
if (hasToolCalls && hasEncryptedReasoning && finishReason.unified === "stop") {
|
|
3595
|
+
finishReason = createFinishReason("tool-calls", finishReason.raw);
|
|
2287
3596
|
}
|
|
2288
|
-
if (finishReason === "tool-calls") {
|
|
3597
|
+
if (finishReason.unified === "tool-calls") {
|
|
2289
3598
|
for (const toolCall of toolCalls) {
|
|
2290
3599
|
if (toolCall && !toolCall.sent) {
|
|
2291
3600
|
controller.enqueue({
|
|
2292
3601
|
type: "tool-call",
|
|
2293
|
-
toolCallId: (
|
|
3602
|
+
toolCallId: (_a17 = toolCall.id) != null ? _a17 : generateId(),
|
|
2294
3603
|
toolName: toolCall.function.name,
|
|
2295
3604
|
// Coerce invalid arguments to an empty JSON object
|
|
2296
3605
|
input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
|
|
@@ -2491,7 +3800,7 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
|
|
|
2491
3800
|
// src/completion/index.ts
|
|
2492
3801
|
var OpenRouterCompletionLanguageModel = class {
|
|
2493
3802
|
constructor(modelId, settings, config) {
|
|
2494
|
-
this.specificationVersion = "
|
|
3803
|
+
this.specificationVersion = "v3";
|
|
2495
3804
|
this.provider = "openrouter";
|
|
2496
3805
|
this.supportsImageUrls = true;
|
|
2497
3806
|
this.supportedUrls = {
|
|
@@ -2562,7 +3871,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2562
3871
|
}, this.config.extraBody), this.settings.extraBody);
|
|
2563
3872
|
}
|
|
2564
3873
|
async doGenerate(options) {
|
|
2565
|
-
var
|
|
3874
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
2566
3875
|
const providerOptions = options.providerOptions || {};
|
|
2567
3876
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
2568
3877
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -2604,16 +3913,22 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2604
3913
|
content: [
|
|
2605
3914
|
{
|
|
2606
3915
|
type: "text",
|
|
2607
|
-
text: (
|
|
3916
|
+
text: (_a16 = choice.text) != null ? _a16 : ""
|
|
2608
3917
|
}
|
|
2609
3918
|
],
|
|
2610
3919
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
2611
3920
|
usage: {
|
|
2612
|
-
inputTokens:
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
3921
|
+
inputTokens: {
|
|
3922
|
+
total: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
|
|
3923
|
+
noCache: void 0,
|
|
3924
|
+
cacheRead: (_f = (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : void 0,
|
|
3925
|
+
cacheWrite: void 0
|
|
3926
|
+
},
|
|
3927
|
+
outputTokens: {
|
|
3928
|
+
total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
|
|
3929
|
+
text: void 0,
|
|
3930
|
+
reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
|
|
3931
|
+
}
|
|
2617
3932
|
},
|
|
2618
3933
|
warnings: [],
|
|
2619
3934
|
response: {
|
|
@@ -2643,47 +3958,52 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2643
3958
|
abortSignal: options.abortSignal,
|
|
2644
3959
|
fetch: this.config.fetch
|
|
2645
3960
|
});
|
|
2646
|
-
let finishReason = "other";
|
|
3961
|
+
let finishReason = createFinishReason("other");
|
|
2647
3962
|
const usage = {
|
|
2648
|
-
inputTokens:
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
3963
|
+
inputTokens: {
|
|
3964
|
+
total: void 0,
|
|
3965
|
+
noCache: void 0,
|
|
3966
|
+
cacheRead: void 0,
|
|
3967
|
+
cacheWrite: void 0
|
|
3968
|
+
},
|
|
3969
|
+
outputTokens: {
|
|
3970
|
+
total: void 0,
|
|
3971
|
+
text: void 0,
|
|
3972
|
+
reasoning: void 0
|
|
3973
|
+
}
|
|
2653
3974
|
};
|
|
2654
3975
|
const openrouterUsage = {};
|
|
2655
3976
|
return {
|
|
2656
3977
|
stream: response.pipeThrough(
|
|
2657
3978
|
new TransformStream({
|
|
2658
3979
|
transform(chunk, controller) {
|
|
2659
|
-
var
|
|
3980
|
+
var _a16, _b16, _c;
|
|
2660
3981
|
if (!chunk.success) {
|
|
2661
|
-
finishReason = "error";
|
|
3982
|
+
finishReason = createFinishReason("error");
|
|
2662
3983
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
2663
3984
|
return;
|
|
2664
3985
|
}
|
|
2665
3986
|
const value = chunk.value;
|
|
2666
3987
|
if ("error" in value) {
|
|
2667
|
-
finishReason = "error";
|
|
3988
|
+
finishReason = createFinishReason("error");
|
|
2668
3989
|
controller.enqueue({ type: "error", error: value.error });
|
|
2669
3990
|
return;
|
|
2670
3991
|
}
|
|
2671
3992
|
if (value.usage != null) {
|
|
2672
|
-
usage.inputTokens = value.usage.prompt_tokens;
|
|
2673
|
-
usage.outputTokens = value.usage.completion_tokens;
|
|
2674
|
-
usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
|
|
3993
|
+
usage.inputTokens.total = value.usage.prompt_tokens;
|
|
3994
|
+
usage.outputTokens.total = value.usage.completion_tokens;
|
|
2675
3995
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
2676
3996
|
if (value.usage.prompt_tokens_details) {
|
|
2677
|
-
const cachedInputTokens = (
|
|
2678
|
-
usage.
|
|
3997
|
+
const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
|
|
3998
|
+
usage.inputTokens.cacheRead = cachedInputTokens;
|
|
2679
3999
|
openrouterUsage.promptTokensDetails = {
|
|
2680
4000
|
cachedTokens: cachedInputTokens
|
|
2681
4001
|
};
|
|
2682
4002
|
}
|
|
2683
4003
|
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
2684
4004
|
if (value.usage.completion_tokens_details) {
|
|
2685
|
-
const reasoningTokens = (
|
|
2686
|
-
usage.
|
|
4005
|
+
const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
|
|
4006
|
+
usage.outputTokens.reasoning = reasoningTokens;
|
|
2687
4007
|
openrouterUsage.completionTokensDetails = {
|
|
2688
4008
|
reasoningTokens
|
|
2689
4009
|
};
|
|
@@ -2753,7 +4073,7 @@ var OpenRouterEmbeddingResponseSchema = import_v48.z.object({
|
|
|
2753
4073
|
// src/embedding/index.ts
|
|
2754
4074
|
var OpenRouterEmbeddingModel = class {
|
|
2755
4075
|
constructor(modelId, settings, config) {
|
|
2756
|
-
this.specificationVersion = "
|
|
4076
|
+
this.specificationVersion = "v3";
|
|
2757
4077
|
this.provider = "openrouter";
|
|
2758
4078
|
this.maxEmbeddingsPerCall = void 0;
|
|
2759
4079
|
this.supportsParallelCalls = true;
|
|
@@ -2762,7 +4082,7 @@ var OpenRouterEmbeddingModel = class {
|
|
|
2762
4082
|
this.config = config;
|
|
2763
4083
|
}
|
|
2764
4084
|
async doEmbed(options) {
|
|
2765
|
-
var
|
|
4085
|
+
var _a16;
|
|
2766
4086
|
const { values, abortSignal, headers } = options;
|
|
2767
4087
|
const args = __spreadValues(__spreadValues({
|
|
2768
4088
|
model: this.modelId,
|
|
@@ -2787,7 +4107,7 @@ var OpenRouterEmbeddingModel = class {
|
|
|
2787
4107
|
return {
|
|
2788
4108
|
embeddings: responseValue.data.map((item) => item.embedding),
|
|
2789
4109
|
usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
|
|
2790
|
-
providerMetadata: ((
|
|
4110
|
+
providerMetadata: ((_a16 = responseValue.usage) == null ? void 0 : _a16.cost) ? {
|
|
2791
4111
|
openrouter: {
|
|
2792
4112
|
usage: {
|
|
2793
4113
|
cost: responseValue.usage.cost
|
|
@@ -2797,7 +4117,8 @@ var OpenRouterEmbeddingModel = class {
|
|
|
2797
4117
|
response: {
|
|
2798
4118
|
headers: responseHeaders,
|
|
2799
4119
|
body: responseValue
|
|
2800
|
-
}
|
|
4120
|
+
},
|
|
4121
|
+
warnings: []
|
|
2801
4122
|
};
|
|
2802
4123
|
}
|
|
2803
4124
|
};
|
|
@@ -2808,8 +4129,8 @@ var OpenRouter = class {
|
|
|
2808
4129
|
* Creates a new OpenRouter provider instance.
|
|
2809
4130
|
*/
|
|
2810
4131
|
constructor(options = {}) {
|
|
2811
|
-
var
|
|
2812
|
-
this.baseURL = (
|
|
4132
|
+
var _a16, _b16;
|
|
4133
|
+
this.baseURL = (_b16 = withoutTrailingSlash((_a16 = options.baseURL) != null ? _a16 : options.baseUrl)) != null ? _b16 : "https://openrouter.ai/api/v1";
|
|
2813
4134
|
this.apiKey = options.apiKey;
|
|
2814
4135
|
this.headers = options.headers;
|
|
2815
4136
|
this.api_keys = options.api_keys;
|
|
@@ -2860,15 +4181,15 @@ var OpenRouter = class {
|
|
|
2860
4181
|
};
|
|
2861
4182
|
|
|
2862
4183
|
// src/utils/remove-undefined.ts
|
|
2863
|
-
function
|
|
4184
|
+
function removeUndefinedEntries(record) {
|
|
2864
4185
|
return Object.fromEntries(
|
|
2865
4186
|
Object.entries(record).filter(([, value]) => value !== null)
|
|
2866
4187
|
);
|
|
2867
4188
|
}
|
|
2868
4189
|
|
|
2869
4190
|
// src/utils/with-user-agent-suffix.ts
|
|
2870
|
-
function
|
|
2871
|
-
const cleanedHeaders =
|
|
4191
|
+
function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
|
|
4192
|
+
const cleanedHeaders = removeUndefinedEntries(
|
|
2872
4193
|
headers != null ? headers : {}
|
|
2873
4194
|
);
|
|
2874
4195
|
const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
|
|
@@ -2879,14 +4200,14 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
2879
4200
|
}
|
|
2880
4201
|
|
|
2881
4202
|
// src/version.ts
|
|
2882
|
-
var
|
|
4203
|
+
var VERSION2 = false ? "0.0.0-test" : "2.0.1";
|
|
2883
4204
|
|
|
2884
4205
|
// src/provider.ts
|
|
2885
4206
|
function createOpenRouter(options = {}) {
|
|
2886
|
-
var
|
|
2887
|
-
const baseURL = (
|
|
4207
|
+
var _a16, _b16, _c;
|
|
4208
|
+
const baseURL = (_b16 = withoutTrailingSlash((_a16 = options.baseURL) != null ? _a16 : options.baseUrl)) != null ? _b16 : "https://openrouter.ai/api/v1";
|
|
2888
4209
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2889
|
-
const getHeaders = () =>
|
|
4210
|
+
const getHeaders = () => withUserAgentSuffix2(
|
|
2890
4211
|
__spreadValues(__spreadValues({
|
|
2891
4212
|
Authorization: `Bearer ${loadApiKey({
|
|
2892
4213
|
apiKey: options.apiKey,
|
|
@@ -2896,7 +4217,7 @@ function createOpenRouter(options = {}) {
|
|
|
2896
4217
|
}, options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
|
|
2897
4218
|
"X-Provider-API-Keys": JSON.stringify(options.api_keys)
|
|
2898
4219
|
}),
|
|
2899
|
-
`ai-sdk/openrouter/${
|
|
4220
|
+
`ai-sdk/openrouter/${VERSION2}`
|
|
2900
4221
|
);
|
|
2901
4222
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2902
4223
|
provider: "openrouter.chat",
|