@openrouter/ai-sdk-provider 1.5.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -24
- package/dist/index.d.mts +28 -108
- package/dist/index.d.ts +28 -108
- package/dist/index.js +1516 -218
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1516 -214
- 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 -15
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") {
|
|
@@ -49,17 +61,16 @@ var index_exports = {};
|
|
|
49
61
|
__export(index_exports, {
|
|
50
62
|
OpenRouter: () => OpenRouter,
|
|
51
63
|
createOpenRouter: () => createOpenRouter,
|
|
52
|
-
decodeToon: () => decodeToon,
|
|
53
|
-
encodeToon: () => encodeToon,
|
|
54
64
|
openrouter: () => openrouter
|
|
55
65
|
});
|
|
56
66
|
module.exports = __toCommonJS(index_exports);
|
|
57
67
|
|
|
58
|
-
// node_modules/.pnpm/@ai-sdk+provider@
|
|
68
|
+
// node_modules/.pnpm/@ai-sdk+provider@3.0.0/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
59
69
|
var marker = "vercel.ai.error";
|
|
60
70
|
var symbol = Symbol.for(marker);
|
|
61
71
|
var _a;
|
|
62
|
-
var
|
|
72
|
+
var _b;
|
|
73
|
+
var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
63
74
|
/**
|
|
64
75
|
* Creates an AI SDK Error.
|
|
65
76
|
*
|
|
@@ -69,13 +80,13 @@ var _AISDKError = class _AISDKError2 extends Error {
|
|
|
69
80
|
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
70
81
|
*/
|
|
71
82
|
constructor({
|
|
72
|
-
name:
|
|
83
|
+
name: name142,
|
|
73
84
|
message,
|
|
74
85
|
cause
|
|
75
86
|
}) {
|
|
76
87
|
super(message);
|
|
77
88
|
this[_a] = true;
|
|
78
|
-
this.name =
|
|
89
|
+
this.name = name142;
|
|
79
90
|
this.cause = cause;
|
|
80
91
|
}
|
|
81
92
|
/**
|
|
@@ -84,20 +95,19 @@ var _AISDKError = class _AISDKError2 extends Error {
|
|
|
84
95
|
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
85
96
|
*/
|
|
86
97
|
static isInstance(error) {
|
|
87
|
-
return
|
|
98
|
+
return _AISDKError.hasMarker(error, marker);
|
|
88
99
|
}
|
|
89
|
-
static hasMarker(error,
|
|
90
|
-
const markerSymbol = Symbol.for(
|
|
100
|
+
static hasMarker(error, marker152) {
|
|
101
|
+
const markerSymbol = Symbol.for(marker152);
|
|
91
102
|
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
92
103
|
}
|
|
93
104
|
};
|
|
94
|
-
_a = symbol;
|
|
95
|
-
var AISDKError = _AISDKError;
|
|
96
105
|
var name = "AI_APICallError";
|
|
97
106
|
var marker2 = `vercel.ai.error.${name}`;
|
|
98
107
|
var symbol2 = Symbol.for(marker2);
|
|
99
108
|
var _a2;
|
|
100
|
-
var
|
|
109
|
+
var _b2;
|
|
110
|
+
var APICallError = class extends (_b2 = AISDKError, _a2 = symbol2, _b2) {
|
|
101
111
|
constructor({
|
|
102
112
|
message,
|
|
103
113
|
url,
|
|
@@ -127,12 +137,12 @@ var APICallError = class extends AISDKError {
|
|
|
127
137
|
return AISDKError.hasMarker(error, marker2);
|
|
128
138
|
}
|
|
129
139
|
};
|
|
130
|
-
_a2 = symbol2;
|
|
131
140
|
var name2 = "AI_EmptyResponseBodyError";
|
|
132
141
|
var marker3 = `vercel.ai.error.${name2}`;
|
|
133
142
|
var symbol3 = Symbol.for(marker3);
|
|
134
143
|
var _a3;
|
|
135
|
-
var
|
|
144
|
+
var _b3;
|
|
145
|
+
var EmptyResponseBodyError = class extends (_b3 = AISDKError, _a3 = symbol3, _b3) {
|
|
136
146
|
// used in isInstance
|
|
137
147
|
constructor({ message = "Empty response body" } = {}) {
|
|
138
148
|
super({ name: name2, message });
|
|
@@ -142,7 +152,6 @@ var EmptyResponseBodyError = class extends AISDKError {
|
|
|
142
152
|
return AISDKError.hasMarker(error, marker3);
|
|
143
153
|
}
|
|
144
154
|
};
|
|
145
|
-
_a3 = symbol3;
|
|
146
155
|
function getErrorMessage(error) {
|
|
147
156
|
if (error == null) {
|
|
148
157
|
return "unknown error";
|
|
@@ -159,7 +168,8 @@ var name3 = "AI_InvalidArgumentError";
|
|
|
159
168
|
var marker4 = `vercel.ai.error.${name3}`;
|
|
160
169
|
var symbol4 = Symbol.for(marker4);
|
|
161
170
|
var _a4;
|
|
162
|
-
var
|
|
171
|
+
var _b4;
|
|
172
|
+
var InvalidArgumentError = class extends (_b4 = AISDKError, _a4 = symbol4, _b4) {
|
|
163
173
|
constructor({
|
|
164
174
|
message,
|
|
165
175
|
cause,
|
|
@@ -173,12 +183,12 @@ var InvalidArgumentError = class extends AISDKError {
|
|
|
173
183
|
return AISDKError.hasMarker(error, marker4);
|
|
174
184
|
}
|
|
175
185
|
};
|
|
176
|
-
_a4 = symbol4;
|
|
177
186
|
var name4 = "AI_InvalidPromptError";
|
|
178
187
|
var marker5 = `vercel.ai.error.${name4}`;
|
|
179
188
|
var symbol5 = Symbol.for(marker5);
|
|
180
189
|
var _a5;
|
|
181
|
-
var
|
|
190
|
+
var _b5;
|
|
191
|
+
var InvalidPromptError = class extends (_b5 = AISDKError, _a5 = symbol5, _b5) {
|
|
182
192
|
constructor({
|
|
183
193
|
prompt,
|
|
184
194
|
message,
|
|
@@ -192,12 +202,12 @@ var InvalidPromptError = class extends AISDKError {
|
|
|
192
202
|
return AISDKError.hasMarker(error, marker5);
|
|
193
203
|
}
|
|
194
204
|
};
|
|
195
|
-
_a5 = symbol5;
|
|
196
205
|
var name5 = "AI_InvalidResponseDataError";
|
|
197
206
|
var marker6 = `vercel.ai.error.${name5}`;
|
|
198
207
|
var symbol6 = Symbol.for(marker6);
|
|
199
208
|
var _a6;
|
|
200
|
-
var
|
|
209
|
+
var _b6;
|
|
210
|
+
var InvalidResponseDataError = class extends (_b6 = AISDKError, _a6 = symbol6, _b6) {
|
|
201
211
|
constructor({
|
|
202
212
|
data,
|
|
203
213
|
message = `Invalid response data: ${JSON.stringify(data)}.`
|
|
@@ -210,12 +220,12 @@ var InvalidResponseDataError = class extends AISDKError {
|
|
|
210
220
|
return AISDKError.hasMarker(error, marker6);
|
|
211
221
|
}
|
|
212
222
|
};
|
|
213
|
-
_a6 = symbol6;
|
|
214
223
|
var name6 = "AI_JSONParseError";
|
|
215
224
|
var marker7 = `vercel.ai.error.${name6}`;
|
|
216
225
|
var symbol7 = Symbol.for(marker7);
|
|
217
226
|
var _a7;
|
|
218
|
-
var
|
|
227
|
+
var _b7;
|
|
228
|
+
var JSONParseError = class extends (_b7 = AISDKError, _a7 = symbol7, _b7) {
|
|
219
229
|
constructor({ text, cause }) {
|
|
220
230
|
super({
|
|
221
231
|
name: name6,
|
|
@@ -230,12 +240,12 @@ Error message: ${getErrorMessage(cause)}`,
|
|
|
230
240
|
return AISDKError.hasMarker(error, marker7);
|
|
231
241
|
}
|
|
232
242
|
};
|
|
233
|
-
_a7 = symbol7;
|
|
234
243
|
var name7 = "AI_LoadAPIKeyError";
|
|
235
244
|
var marker8 = `vercel.ai.error.${name7}`;
|
|
236
245
|
var symbol8 = Symbol.for(marker8);
|
|
237
246
|
var _a8;
|
|
238
|
-
var
|
|
247
|
+
var _b8;
|
|
248
|
+
var LoadAPIKeyError = class extends (_b8 = AISDKError, _a8 = symbol8, _b8) {
|
|
239
249
|
// used in isInstance
|
|
240
250
|
constructor({ message }) {
|
|
241
251
|
super({ name: name7, message });
|
|
@@ -245,17 +255,27 @@ var LoadAPIKeyError = class extends AISDKError {
|
|
|
245
255
|
return AISDKError.hasMarker(error, marker8);
|
|
246
256
|
}
|
|
247
257
|
};
|
|
248
|
-
_a8 = symbol8;
|
|
249
258
|
var name8 = "AI_LoadSettingError";
|
|
250
259
|
var marker9 = `vercel.ai.error.${name8}`;
|
|
251
260
|
var symbol9 = Symbol.for(marker9);
|
|
252
261
|
var _a9;
|
|
253
|
-
|
|
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
|
+
};
|
|
254
273
|
var name9 = "AI_NoContentGeneratedError";
|
|
255
274
|
var marker10 = `vercel.ai.error.${name9}`;
|
|
256
275
|
var symbol10 = Symbol.for(marker10);
|
|
257
276
|
var _a10;
|
|
258
|
-
var
|
|
277
|
+
var _b10;
|
|
278
|
+
var NoContentGeneratedError = class extends (_b10 = AISDKError, _a10 = symbol10, _b10) {
|
|
259
279
|
// used in isInstance
|
|
260
280
|
constructor({
|
|
261
281
|
message = "No content generated."
|
|
@@ -267,22 +287,54 @@ var NoContentGeneratedError = class extends AISDKError {
|
|
|
267
287
|
return AISDKError.hasMarker(error, marker10);
|
|
268
288
|
}
|
|
269
289
|
};
|
|
270
|
-
_a10 = symbol10;
|
|
271
290
|
var name10 = "AI_NoSuchModelError";
|
|
272
291
|
var marker11 = `vercel.ai.error.${name10}`;
|
|
273
292
|
var symbol11 = Symbol.for(marker11);
|
|
274
293
|
var _a11;
|
|
275
|
-
|
|
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
|
+
};
|
|
276
311
|
var name11 = "AI_TooManyEmbeddingValuesForCallError";
|
|
277
312
|
var marker12 = `vercel.ai.error.${name11}`;
|
|
278
313
|
var symbol12 = Symbol.for(marker12);
|
|
279
314
|
var _a12;
|
|
280
|
-
|
|
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
|
+
};
|
|
281
332
|
var name12 = "AI_TypeValidationError";
|
|
282
333
|
var marker13 = `vercel.ai.error.${name12}`;
|
|
283
334
|
var symbol13 = Symbol.for(marker13);
|
|
284
335
|
var _a13;
|
|
285
|
-
var
|
|
336
|
+
var _b13;
|
|
337
|
+
var TypeValidationError = class _TypeValidationError extends (_b13 = AISDKError, _a13 = symbol13, _b13) {
|
|
286
338
|
constructor({ value, cause }) {
|
|
287
339
|
super({
|
|
288
340
|
name: name12,
|
|
@@ -310,16 +362,15 @@ Error message: ${getErrorMessage(cause)}`,
|
|
|
310
362
|
value,
|
|
311
363
|
cause
|
|
312
364
|
}) {
|
|
313
|
-
return
|
|
365
|
+
return _TypeValidationError.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError({ value, cause });
|
|
314
366
|
}
|
|
315
367
|
};
|
|
316
|
-
_a13 = symbol13;
|
|
317
|
-
var TypeValidationError = _TypeValidationError;
|
|
318
368
|
var name13 = "AI_UnsupportedFunctionalityError";
|
|
319
369
|
var marker14 = `vercel.ai.error.${name13}`;
|
|
320
370
|
var symbol14 = Symbol.for(marker14);
|
|
321
371
|
var _a14;
|
|
322
|
-
var
|
|
372
|
+
var _b14;
|
|
373
|
+
var UnsupportedFunctionalityError = class extends (_b14 = AISDKError, _a14 = symbol14, _b14) {
|
|
323
374
|
constructor({
|
|
324
375
|
functionality,
|
|
325
376
|
message = `'${functionality}' functionality not supported.`
|
|
@@ -332,9 +383,14 @@ var UnsupportedFunctionalityError = class extends AISDKError {
|
|
|
332
383
|
return AISDKError.hasMarker(error, marker14);
|
|
333
384
|
}
|
|
334
385
|
};
|
|
335
|
-
_a14 = symbol14;
|
|
336
386
|
|
|
337
|
-
// 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
|
|
338
394
|
var ParseError = class extends Error {
|
|
339
395
|
constructor(message, options) {
|
|
340
396
|
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
@@ -425,7 +481,7 @@ function splitLines(chunk) {
|
|
|
425
481
|
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
426
482
|
`, searchIndex);
|
|
427
483
|
let lineEnd = -1;
|
|
428
|
-
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) {
|
|
429
485
|
incompleteLine = chunk.slice(searchIndex);
|
|
430
486
|
break;
|
|
431
487
|
} else {
|
|
@@ -437,7 +493,7 @@ function splitLines(chunk) {
|
|
|
437
493
|
return [lines, incompleteLine];
|
|
438
494
|
}
|
|
439
495
|
|
|
440
|
-
// node_modules/.pnpm/eventsource-parser@3.0.
|
|
496
|
+
// node_modules/.pnpm/eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/stream.js
|
|
441
497
|
var EventSourceParserStream = class extends TransformStream {
|
|
442
498
|
constructor({ onError, onRetry, onComment } = {}) {
|
|
443
499
|
let parser;
|
|
@@ -461,25 +517,7 @@ var EventSourceParserStream = class extends TransformStream {
|
|
|
461
517
|
}
|
|
462
518
|
};
|
|
463
519
|
|
|
464
|
-
// node_modules/.pnpm/@ai-sdk+provider-utils@
|
|
465
|
-
var z4 = __toESM(require("zod/v4"), 1);
|
|
466
|
-
|
|
467
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/Options.js
|
|
468
|
-
var ignoreOverride = Symbol("Let zodToJsonSchema decide on which parser to use");
|
|
469
|
-
|
|
470
|
-
// node_modules/.pnpm/zod-to-json-schema@3.24.6_zod@3.25.76/node_modules/zod-to-json-schema/dist/esm/selectParser.js
|
|
471
|
-
var import_zod3 = require("zod");
|
|
472
|
-
|
|
473
|
-
// 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
|
|
474
|
-
var import_zod = require("zod");
|
|
475
|
-
|
|
476
|
-
// 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
|
|
477
|
-
var import_zod2 = require("zod");
|
|
478
|
-
|
|
479
|
-
// 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
|
|
480
|
-
var ALPHA_NUMERIC = new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
481
|
-
|
|
482
|
-
// 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
|
|
483
521
|
function combineHeaders(...headers) {
|
|
484
522
|
return headers.reduce(
|
|
485
523
|
(combinedHeaders, currentHeaders) => __spreadValues(__spreadValues({}, combinedHeaders), currentHeaders != null ? currentHeaders : {}),
|
|
@@ -489,6 +527,37 @@ function combineHeaders(...headers) {
|
|
|
489
527
|
function extractResponseHeaders(response) {
|
|
490
528
|
return Object.fromEntries([...response.headers]);
|
|
491
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
|
+
};
|
|
492
561
|
var createIdGenerator = ({
|
|
493
562
|
prefix,
|
|
494
563
|
size = 16,
|
|
@@ -543,11 +612,53 @@ function handleFetchError({
|
|
|
543
612
|
}
|
|
544
613
|
return error;
|
|
545
614
|
}
|
|
546
|
-
function
|
|
547
|
-
|
|
548
|
-
|
|
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(" ")
|
|
549
658
|
);
|
|
659
|
+
return Object.fromEntries(normalizedHeaders.entries());
|
|
550
660
|
}
|
|
661
|
+
var VERSION = true ? "4.0.1" : "0.0.0-test";
|
|
551
662
|
function loadApiKey({
|
|
552
663
|
apiKey,
|
|
553
664
|
environmentVariableName,
|
|
@@ -616,34 +727,1195 @@ function filter(obj) {
|
|
|
616
727
|
}
|
|
617
728
|
function secureJsonParse(text) {
|
|
618
729
|
const { stackTraceLimit } = Error;
|
|
619
|
-
|
|
730
|
+
try {
|
|
731
|
+
Error.stackTraceLimit = 0;
|
|
732
|
+
} catch (e) {
|
|
733
|
+
return _parse(text);
|
|
734
|
+
}
|
|
620
735
|
try {
|
|
621
736
|
return _parse(text);
|
|
622
737
|
} finally {
|
|
623
738
|
Error.stackTraceLimit = stackTraceLimit;
|
|
624
739
|
}
|
|
625
740
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
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;
|
|
629
765
|
}
|
|
630
|
-
|
|
631
|
-
|
|
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 {};
|
|
632
795
|
}
|
|
633
|
-
function
|
|
634
|
-
|
|
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;
|
|
635
817
|
}
|
|
636
|
-
function
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
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;
|
|
846
|
+
}
|
|
847
|
+
function parseBooleanDef() {
|
|
848
|
+
return { type: "boolean" };
|
|
849
|
+
}
|
|
850
|
+
function parseBrandedDef(_def, refs) {
|
|
851
|
+
return parseDef(_def.type._def, refs);
|
|
852
|
+
}
|
|
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))
|
|
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
|
+
}
|
|
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
|
+
]
|
|
645
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
|
+
)
|
|
646
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
|
+
}
|
|
647
1919
|
}
|
|
648
1920
|
async function validateTypes({
|
|
649
1921
|
value,
|
|
@@ -659,12 +1931,12 @@ async function safeValidateTypes({
|
|
|
659
1931
|
value,
|
|
660
1932
|
schema
|
|
661
1933
|
}) {
|
|
662
|
-
const
|
|
1934
|
+
const actualSchema = asSchema(schema);
|
|
663
1935
|
try {
|
|
664
|
-
if (
|
|
1936
|
+
if (actualSchema.validate == null) {
|
|
665
1937
|
return { success: true, value, rawValue: value };
|
|
666
1938
|
}
|
|
667
|
-
const result = await
|
|
1939
|
+
const result = await actualSchema.validate(value);
|
|
668
1940
|
if (result.success) {
|
|
669
1941
|
return { success: true, value: result.value, rawValue: value };
|
|
670
1942
|
}
|
|
@@ -747,7 +2019,7 @@ var postJsonToApi = async ({
|
|
|
747
2019
|
failedResponseHandler,
|
|
748
2020
|
successfulResponseHandler,
|
|
749
2021
|
abortSignal,
|
|
750
|
-
fetch
|
|
2022
|
+
fetch: fetch2
|
|
751
2023
|
}) => postToApi({
|
|
752
2024
|
url,
|
|
753
2025
|
headers: __spreadValues({
|
|
@@ -760,7 +2032,7 @@ var postJsonToApi = async ({
|
|
|
760
2032
|
failedResponseHandler,
|
|
761
2033
|
successfulResponseHandler,
|
|
762
2034
|
abortSignal,
|
|
763
|
-
fetch
|
|
2035
|
+
fetch: fetch2
|
|
764
2036
|
});
|
|
765
2037
|
var postToApi = async ({
|
|
766
2038
|
url,
|
|
@@ -769,12 +2041,16 @@ var postToApi = async ({
|
|
|
769
2041
|
successfulResponseHandler,
|
|
770
2042
|
failedResponseHandler,
|
|
771
2043
|
abortSignal,
|
|
772
|
-
fetch = getOriginalFetch2()
|
|
2044
|
+
fetch: fetch2 = getOriginalFetch2()
|
|
773
2045
|
}) => {
|
|
774
2046
|
try {
|
|
775
|
-
const response = await
|
|
2047
|
+
const response = await fetch2(url, {
|
|
776
2048
|
method: "POST",
|
|
777
|
-
headers:
|
|
2049
|
+
headers: withUserAgentSuffix(
|
|
2050
|
+
headers,
|
|
2051
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
|
2052
|
+
getRuntimeEnvironmentUserAgent()
|
|
2053
|
+
),
|
|
778
2054
|
body: body.content,
|
|
779
2055
|
signal: abortSignal
|
|
780
2056
|
});
|
|
@@ -918,15 +2194,6 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
|
918
2194
|
rawValue: parsedResult.rawValue
|
|
919
2195
|
};
|
|
920
2196
|
};
|
|
921
|
-
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
|
922
|
-
var { btoa, atob } = globalThis;
|
|
923
|
-
function convertUint8ArrayToBase64(array) {
|
|
924
|
-
let latin1string = "";
|
|
925
|
-
for (let i = 0; i < array.length; i++) {
|
|
926
|
-
latin1string += String.fromCodePoint(array[i]);
|
|
927
|
-
}
|
|
928
|
-
return btoa(latin1string);
|
|
929
|
-
}
|
|
930
2197
|
function withoutTrailingSlash(url) {
|
|
931
2198
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
932
2199
|
}
|
|
@@ -1057,7 +2324,7 @@ var OpenRouterProviderOptionsSchema = import_v43.z.object({
|
|
|
1057
2324
|
}).optional();
|
|
1058
2325
|
|
|
1059
2326
|
// src/utils/map-finish-reason.ts
|
|
1060
|
-
function
|
|
2327
|
+
function mapToUnified(finishReason) {
|
|
1061
2328
|
switch (finishReason) {
|
|
1062
2329
|
case "stop":
|
|
1063
2330
|
return "stop";
|
|
@@ -1069,9 +2336,18 @@ function mapOpenRouterFinishReason(finishReason) {
|
|
|
1069
2336
|
case "tool_calls":
|
|
1070
2337
|
return "tool-calls";
|
|
1071
2338
|
default:
|
|
1072
|
-
return "
|
|
2339
|
+
return "other";
|
|
1073
2340
|
}
|
|
1074
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
|
+
}
|
|
1075
2351
|
|
|
1076
2352
|
// src/types/openrouter-chat-completions-input.ts
|
|
1077
2353
|
var OPENROUTER_AUDIO_FORMATS = [
|
|
@@ -1104,10 +2380,10 @@ function getFileUrl({
|
|
|
1104
2380
|
part,
|
|
1105
2381
|
defaultMediaType
|
|
1106
2382
|
}) {
|
|
1107
|
-
var
|
|
2383
|
+
var _a16, _b16;
|
|
1108
2384
|
if (part.data instanceof Uint8Array) {
|
|
1109
2385
|
const base64 = convertUint8ArrayToBase64(part.data);
|
|
1110
|
-
return `data:${(
|
|
2386
|
+
return `data:${(_a16 = part.mediaType) != null ? _a16 : defaultMediaType};base64,${base64}`;
|
|
1111
2387
|
}
|
|
1112
2388
|
const stringUrl = part.data.toString();
|
|
1113
2389
|
if (isUrl({
|
|
@@ -1116,12 +2392,12 @@ function getFileUrl({
|
|
|
1116
2392
|
})) {
|
|
1117
2393
|
return stringUrl;
|
|
1118
2394
|
}
|
|
1119
|
-
return stringUrl.startsWith("data:") ? stringUrl : `data:${(
|
|
2395
|
+
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b16 = part.mediaType) != null ? _b16 : defaultMediaType};base64,${stringUrl}`;
|
|
1120
2396
|
}
|
|
1121
2397
|
function getMediaType(dataUrl, defaultMediaType) {
|
|
1122
|
-
var
|
|
2398
|
+
var _a16;
|
|
1123
2399
|
const match = dataUrl.match(/^data:([^;]+)/);
|
|
1124
|
-
return match ? (
|
|
2400
|
+
return match ? (_a16 = match[1]) != null ? _a16 : defaultMediaType : defaultMediaType;
|
|
1125
2401
|
}
|
|
1126
2402
|
function getBase64FromDataUrl(dataUrl) {
|
|
1127
2403
|
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
@@ -1196,13 +2472,13 @@ Learn more: https://openrouter.ai/docs/features/multimodal/audio`
|
|
|
1196
2472
|
|
|
1197
2473
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
1198
2474
|
function getCacheControl(providerMetadata) {
|
|
1199
|
-
var
|
|
2475
|
+
var _a16, _b16, _c;
|
|
1200
2476
|
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
1201
2477
|
const openrouter2 = providerMetadata == null ? void 0 : providerMetadata.openrouter;
|
|
1202
|
-
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;
|
|
1203
2479
|
}
|
|
1204
2480
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1205
|
-
var
|
|
2481
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1206
2482
|
const messages = [];
|
|
1207
2483
|
for (const { role, content, providerOptions } of prompt) {
|
|
1208
2484
|
switch (role) {
|
|
@@ -1215,8 +2491,8 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1215
2491
|
break;
|
|
1216
2492
|
}
|
|
1217
2493
|
case "user": {
|
|
1218
|
-
if (content.length === 1 && ((
|
|
1219
|
-
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);
|
|
1220
2496
|
const contentWithCacheControl = cacheControl ? [
|
|
1221
2497
|
{
|
|
1222
2498
|
type: "text",
|
|
@@ -1233,8 +2509,8 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1233
2509
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1234
2510
|
const contentParts = content.map(
|
|
1235
2511
|
(part) => {
|
|
1236
|
-
var
|
|
1237
|
-
const cacheControl = (
|
|
2512
|
+
var _a17, _b17, _c2, _d2, _e2, _f2, _g2;
|
|
2513
|
+
const cacheControl = (_a17 = getCacheControl(part.providerOptions)) != null ? _a17 : messageCacheControl;
|
|
1238
2514
|
switch (part.type) {
|
|
1239
2515
|
case "text":
|
|
1240
2516
|
return {
|
|
@@ -1244,7 +2520,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1244
2520
|
cache_control: cacheControl
|
|
1245
2521
|
};
|
|
1246
2522
|
case "file": {
|
|
1247
|
-
if ((
|
|
2523
|
+
if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
|
|
1248
2524
|
const url = getFileUrl({
|
|
1249
2525
|
part,
|
|
1250
2526
|
defaultMediaType: "image/jpeg"
|
|
@@ -1371,6 +2647,9 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1371
2647
|
}
|
|
1372
2648
|
case "tool": {
|
|
1373
2649
|
for (const toolResponse of content) {
|
|
2650
|
+
if (toolResponse.type === "tool-approval-response") {
|
|
2651
|
+
continue;
|
|
2652
|
+
}
|
|
1374
2653
|
const content2 = getToolResultContent(toolResponse);
|
|
1375
2654
|
messages.push({
|
|
1376
2655
|
role: "tool",
|
|
@@ -1389,7 +2668,18 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1389
2668
|
return messages;
|
|
1390
2669
|
}
|
|
1391
2670
|
function getToolResultContent(input) {
|
|
1392
|
-
|
|
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
|
+
}
|
|
1393
2683
|
}
|
|
1394
2684
|
|
|
1395
2685
|
// src/chat/get-tool-choice.ts
|
|
@@ -1632,7 +2922,7 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
|
1632
2922
|
// src/chat/index.ts
|
|
1633
2923
|
var OpenRouterChatLanguageModel = class {
|
|
1634
2924
|
constructor(modelId, settings, config) {
|
|
1635
|
-
this.specificationVersion = "
|
|
2925
|
+
this.specificationVersion = "v3";
|
|
1636
2926
|
this.provider = "openrouter";
|
|
1637
2927
|
this.defaultObjectGenerationMode = "tool";
|
|
1638
2928
|
this.supportsImageUrls = true;
|
|
@@ -1662,7 +2952,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1662
2952
|
tools,
|
|
1663
2953
|
toolChoice
|
|
1664
2954
|
}) {
|
|
1665
|
-
var
|
|
2955
|
+
var _a16;
|
|
1666
2956
|
const baseArgs = __spreadValues(__spreadValues({
|
|
1667
2957
|
// model id:
|
|
1668
2958
|
model: this.modelId,
|
|
@@ -1686,7 +2976,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1686
2976
|
json_schema: __spreadValues({
|
|
1687
2977
|
schema: responseFormat.schema,
|
|
1688
2978
|
strict: true,
|
|
1689
|
-
name: (
|
|
2979
|
+
name: (_a16 = responseFormat.name) != null ? _a16 : "response"
|
|
1690
2980
|
}, responseFormat.description && {
|
|
1691
2981
|
description: responseFormat.description
|
|
1692
2982
|
})
|
|
@@ -1725,7 +3015,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1725
3015
|
return baseArgs;
|
|
1726
3016
|
}
|
|
1727
3017
|
async doGenerate(options) {
|
|
1728
|
-
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;
|
|
1729
3019
|
const providerOptions = options.providerOptions || {};
|
|
1730
3020
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1731
3021
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1765,19 +3055,31 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1765
3055
|
});
|
|
1766
3056
|
}
|
|
1767
3057
|
const usageInfo = response.usage ? {
|
|
1768
|
-
inputTokens:
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
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
|
+
}
|
|
1773
3069
|
} : {
|
|
1774
|
-
inputTokens:
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
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
|
+
}
|
|
1779
3081
|
};
|
|
1780
|
-
const reasoningDetails = (
|
|
3082
|
+
const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
|
|
1781
3083
|
const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
|
|
1782
3084
|
switch (detail.type) {
|
|
1783
3085
|
case "reasoning.text" /* Text */: {
|
|
@@ -1845,7 +3147,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1845
3147
|
for (const toolCall of choice.message.tool_calls) {
|
|
1846
3148
|
content.push({
|
|
1847
3149
|
type: "tool-call",
|
|
1848
|
-
toolCallId: (
|
|
3150
|
+
toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
|
|
1849
3151
|
toolName: toolCall.function.name,
|
|
1850
3152
|
input: toolCall.function.arguments,
|
|
1851
3153
|
providerMetadata: {
|
|
@@ -1883,7 +3185,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1883
3185
|
}
|
|
1884
3186
|
}
|
|
1885
3187
|
}
|
|
1886
|
-
const fileAnnotations = (
|
|
3188
|
+
const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
|
|
1887
3189
|
(a) => a.type === "file"
|
|
1888
3190
|
);
|
|
1889
3191
|
const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
|
|
@@ -1891,7 +3193,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1891
3193
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
1892
3194
|
);
|
|
1893
3195
|
const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
|
|
1894
|
-
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);
|
|
1895
3197
|
return {
|
|
1896
3198
|
content,
|
|
1897
3199
|
finishReason: effectiveFinishReason,
|
|
@@ -1899,13 +3201,13 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1899
3201
|
warnings: [],
|
|
1900
3202
|
providerMetadata: {
|
|
1901
3203
|
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1902
|
-
provider: (
|
|
1903
|
-
reasoning_details: (
|
|
3204
|
+
provider: (_k = response.provider) != null ? _k : "",
|
|
3205
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1904
3206
|
annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
|
|
1905
3207
|
usage: __spreadValues(__spreadValues(__spreadValues({
|
|
1906
|
-
promptTokens: (
|
|
1907
|
-
completionTokens: (
|
|
1908
|
-
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),
|
|
1909
3211
|
cost: (_q = response.usage) == null ? void 0 : _q.cost
|
|
1910
3212
|
}, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
|
|
1911
3213
|
promptTokensDetails: {
|
|
@@ -1931,7 +3233,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1931
3233
|
};
|
|
1932
3234
|
}
|
|
1933
3235
|
async doStream(options) {
|
|
1934
|
-
var
|
|
3236
|
+
var _a16;
|
|
1935
3237
|
const providerOptions = options.providerOptions || {};
|
|
1936
3238
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1937
3239
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1946,7 +3248,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1946
3248
|
// only include stream_options when in strict compatibility mode:
|
|
1947
3249
|
stream_options: this.config.compatibility === "strict" ? __spreadValues({
|
|
1948
3250
|
include_usage: true
|
|
1949
|
-
}, ((
|
|
3251
|
+
}, ((_a16 = this.settings.usage) == null ? void 0 : _a16.include) ? { include_usage: true } : {}) : void 0
|
|
1950
3252
|
}),
|
|
1951
3253
|
failedResponseHandler: openrouterFailedResponseHandler,
|
|
1952
3254
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
@@ -1956,13 +3258,19 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1956
3258
|
fetch: this.config.fetch
|
|
1957
3259
|
});
|
|
1958
3260
|
const toolCalls = [];
|
|
1959
|
-
let finishReason = "other";
|
|
3261
|
+
let finishReason = createFinishReason("other");
|
|
1960
3262
|
const usage = {
|
|
1961
|
-
inputTokens:
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
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
|
+
}
|
|
1966
3274
|
};
|
|
1967
3275
|
const openrouterUsage = {};
|
|
1968
3276
|
const accumulatedReasoningDetails = [];
|
|
@@ -1977,15 +3285,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1977
3285
|
stream: response.pipeThrough(
|
|
1978
3286
|
new TransformStream({
|
|
1979
3287
|
transform(chunk, controller) {
|
|
1980
|
-
var
|
|
3288
|
+
var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
1981
3289
|
if (!chunk.success) {
|
|
1982
|
-
finishReason = "error";
|
|
3290
|
+
finishReason = createFinishReason("error");
|
|
1983
3291
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
1984
3292
|
return;
|
|
1985
3293
|
}
|
|
1986
3294
|
const value = chunk.value;
|
|
1987
3295
|
if ("error" in value) {
|
|
1988
|
-
finishReason = "error";
|
|
3296
|
+
finishReason = createFinishReason("error");
|
|
1989
3297
|
controller.enqueue({ type: "error", error: value.error });
|
|
1990
3298
|
return;
|
|
1991
3299
|
}
|
|
@@ -2006,21 +3314,20 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2006
3314
|
});
|
|
2007
3315
|
}
|
|
2008
3316
|
if (value.usage != null) {
|
|
2009
|
-
usage.inputTokens = value.usage.prompt_tokens;
|
|
2010
|
-
usage.outputTokens = value.usage.completion_tokens;
|
|
2011
|
-
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;
|
|
2012
3319
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
2013
3320
|
if (value.usage.prompt_tokens_details) {
|
|
2014
|
-
const cachedInputTokens = (
|
|
2015
|
-
usage.
|
|
3321
|
+
const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
|
|
3322
|
+
usage.inputTokens.cacheRead = cachedInputTokens;
|
|
2016
3323
|
openrouterUsage.promptTokensDetails = {
|
|
2017
3324
|
cachedTokens: cachedInputTokens
|
|
2018
3325
|
};
|
|
2019
3326
|
}
|
|
2020
3327
|
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
2021
3328
|
if (value.usage.completion_tokens_details) {
|
|
2022
|
-
const reasoningTokens = (
|
|
2023
|
-
usage.
|
|
3329
|
+
const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
|
|
3330
|
+
usage.outputTokens.reasoning = reasoningTokens;
|
|
2024
3331
|
openrouterUsage.completionTokensDetails = {
|
|
2025
3332
|
reasoningTokens
|
|
2026
3333
|
};
|
|
@@ -2279,20 +3586,20 @@ var OpenRouterChatLanguageModel = class {
|
|
|
2279
3586
|
}
|
|
2280
3587
|
},
|
|
2281
3588
|
flush(controller) {
|
|
2282
|
-
var
|
|
3589
|
+
var _a17;
|
|
2283
3590
|
const hasToolCalls = toolCalls.length > 0;
|
|
2284
3591
|
const hasEncryptedReasoning = accumulatedReasoningDetails.some(
|
|
2285
3592
|
(d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
|
|
2286
3593
|
);
|
|
2287
|
-
if (hasToolCalls && hasEncryptedReasoning && finishReason === "stop") {
|
|
2288
|
-
finishReason = "tool-calls";
|
|
3594
|
+
if (hasToolCalls && hasEncryptedReasoning && finishReason.unified === "stop") {
|
|
3595
|
+
finishReason = createFinishReason("tool-calls", finishReason.raw);
|
|
2289
3596
|
}
|
|
2290
|
-
if (finishReason === "tool-calls") {
|
|
3597
|
+
if (finishReason.unified === "tool-calls") {
|
|
2291
3598
|
for (const toolCall of toolCalls) {
|
|
2292
3599
|
if (toolCall && !toolCall.sent) {
|
|
2293
3600
|
controller.enqueue({
|
|
2294
3601
|
type: "tool-call",
|
|
2295
|
-
toolCallId: (
|
|
3602
|
+
toolCallId: (_a17 = toolCall.id) != null ? _a17 : generateId(),
|
|
2296
3603
|
toolName: toolCall.function.name,
|
|
2297
3604
|
// Coerce invalid arguments to an empty JSON object
|
|
2298
3605
|
input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
|
|
@@ -2493,7 +3800,7 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
|
|
|
2493
3800
|
// src/completion/index.ts
|
|
2494
3801
|
var OpenRouterCompletionLanguageModel = class {
|
|
2495
3802
|
constructor(modelId, settings, config) {
|
|
2496
|
-
this.specificationVersion = "
|
|
3803
|
+
this.specificationVersion = "v3";
|
|
2497
3804
|
this.provider = "openrouter";
|
|
2498
3805
|
this.supportsImageUrls = true;
|
|
2499
3806
|
this.supportedUrls = {
|
|
@@ -2564,7 +3871,7 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2564
3871
|
}, this.config.extraBody), this.settings.extraBody);
|
|
2565
3872
|
}
|
|
2566
3873
|
async doGenerate(options) {
|
|
2567
|
-
var
|
|
3874
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
2568
3875
|
const providerOptions = options.providerOptions || {};
|
|
2569
3876
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
2570
3877
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -2606,16 +3913,22 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2606
3913
|
content: [
|
|
2607
3914
|
{
|
|
2608
3915
|
type: "text",
|
|
2609
|
-
text: (
|
|
3916
|
+
text: (_a16 = choice.text) != null ? _a16 : ""
|
|
2610
3917
|
}
|
|
2611
3918
|
],
|
|
2612
3919
|
finishReason: mapOpenRouterFinishReason(choice.finish_reason),
|
|
2613
3920
|
usage: {
|
|
2614
|
-
inputTokens:
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
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
|
+
}
|
|
2619
3932
|
},
|
|
2620
3933
|
warnings: [],
|
|
2621
3934
|
response: {
|
|
@@ -2645,47 +3958,52 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2645
3958
|
abortSignal: options.abortSignal,
|
|
2646
3959
|
fetch: this.config.fetch
|
|
2647
3960
|
});
|
|
2648
|
-
let finishReason = "other";
|
|
3961
|
+
let finishReason = createFinishReason("other");
|
|
2649
3962
|
const usage = {
|
|
2650
|
-
inputTokens:
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
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
|
+
}
|
|
2655
3974
|
};
|
|
2656
3975
|
const openrouterUsage = {};
|
|
2657
3976
|
return {
|
|
2658
3977
|
stream: response.pipeThrough(
|
|
2659
3978
|
new TransformStream({
|
|
2660
3979
|
transform(chunk, controller) {
|
|
2661
|
-
var
|
|
3980
|
+
var _a16, _b16, _c;
|
|
2662
3981
|
if (!chunk.success) {
|
|
2663
|
-
finishReason = "error";
|
|
3982
|
+
finishReason = createFinishReason("error");
|
|
2664
3983
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
2665
3984
|
return;
|
|
2666
3985
|
}
|
|
2667
3986
|
const value = chunk.value;
|
|
2668
3987
|
if ("error" in value) {
|
|
2669
|
-
finishReason = "error";
|
|
3988
|
+
finishReason = createFinishReason("error");
|
|
2670
3989
|
controller.enqueue({ type: "error", error: value.error });
|
|
2671
3990
|
return;
|
|
2672
3991
|
}
|
|
2673
3992
|
if (value.usage != null) {
|
|
2674
|
-
usage.inputTokens = value.usage.prompt_tokens;
|
|
2675
|
-
usage.outputTokens = value.usage.completion_tokens;
|
|
2676
|
-
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;
|
|
2677
3995
|
openrouterUsage.promptTokens = value.usage.prompt_tokens;
|
|
2678
3996
|
if (value.usage.prompt_tokens_details) {
|
|
2679
|
-
const cachedInputTokens = (
|
|
2680
|
-
usage.
|
|
3997
|
+
const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
|
|
3998
|
+
usage.inputTokens.cacheRead = cachedInputTokens;
|
|
2681
3999
|
openrouterUsage.promptTokensDetails = {
|
|
2682
4000
|
cachedTokens: cachedInputTokens
|
|
2683
4001
|
};
|
|
2684
4002
|
}
|
|
2685
4003
|
openrouterUsage.completionTokens = value.usage.completion_tokens;
|
|
2686
4004
|
if (value.usage.completion_tokens_details) {
|
|
2687
|
-
const reasoningTokens = (
|
|
2688
|
-
usage.
|
|
4005
|
+
const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
|
|
4006
|
+
usage.outputTokens.reasoning = reasoningTokens;
|
|
2689
4007
|
openrouterUsage.completionTokensDetails = {
|
|
2690
4008
|
reasoningTokens
|
|
2691
4009
|
};
|
|
@@ -2755,7 +4073,7 @@ var OpenRouterEmbeddingResponseSchema = import_v48.z.object({
|
|
|
2755
4073
|
// src/embedding/index.ts
|
|
2756
4074
|
var OpenRouterEmbeddingModel = class {
|
|
2757
4075
|
constructor(modelId, settings, config) {
|
|
2758
|
-
this.specificationVersion = "
|
|
4076
|
+
this.specificationVersion = "v3";
|
|
2759
4077
|
this.provider = "openrouter";
|
|
2760
4078
|
this.maxEmbeddingsPerCall = void 0;
|
|
2761
4079
|
this.supportsParallelCalls = true;
|
|
@@ -2764,7 +4082,7 @@ var OpenRouterEmbeddingModel = class {
|
|
|
2764
4082
|
this.config = config;
|
|
2765
4083
|
}
|
|
2766
4084
|
async doEmbed(options) {
|
|
2767
|
-
var
|
|
4085
|
+
var _a16;
|
|
2768
4086
|
const { values, abortSignal, headers } = options;
|
|
2769
4087
|
const args = __spreadValues(__spreadValues({
|
|
2770
4088
|
model: this.modelId,
|
|
@@ -2789,7 +4107,7 @@ var OpenRouterEmbeddingModel = class {
|
|
|
2789
4107
|
return {
|
|
2790
4108
|
embeddings: responseValue.data.map((item) => item.embedding),
|
|
2791
4109
|
usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
|
|
2792
|
-
providerMetadata: ((
|
|
4110
|
+
providerMetadata: ((_a16 = responseValue.usage) == null ? void 0 : _a16.cost) ? {
|
|
2793
4111
|
openrouter: {
|
|
2794
4112
|
usage: {
|
|
2795
4113
|
cost: responseValue.usage.cost
|
|
@@ -2799,7 +4117,8 @@ var OpenRouterEmbeddingModel = class {
|
|
|
2799
4117
|
response: {
|
|
2800
4118
|
headers: responseHeaders,
|
|
2801
4119
|
body: responseValue
|
|
2802
|
-
}
|
|
4120
|
+
},
|
|
4121
|
+
warnings: []
|
|
2803
4122
|
};
|
|
2804
4123
|
}
|
|
2805
4124
|
};
|
|
@@ -2810,8 +4129,8 @@ var OpenRouter = class {
|
|
|
2810
4129
|
* Creates a new OpenRouter provider instance.
|
|
2811
4130
|
*/
|
|
2812
4131
|
constructor(options = {}) {
|
|
2813
|
-
var
|
|
2814
|
-
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";
|
|
2815
4134
|
this.apiKey = options.apiKey;
|
|
2816
4135
|
this.headers = options.headers;
|
|
2817
4136
|
this.api_keys = options.api_keys;
|
|
@@ -2862,15 +4181,15 @@ var OpenRouter = class {
|
|
|
2862
4181
|
};
|
|
2863
4182
|
|
|
2864
4183
|
// src/utils/remove-undefined.ts
|
|
2865
|
-
function
|
|
4184
|
+
function removeUndefinedEntries(record) {
|
|
2866
4185
|
return Object.fromEntries(
|
|
2867
4186
|
Object.entries(record).filter(([, value]) => value !== null)
|
|
2868
4187
|
);
|
|
2869
4188
|
}
|
|
2870
4189
|
|
|
2871
4190
|
// src/utils/with-user-agent-suffix.ts
|
|
2872
|
-
function
|
|
2873
|
-
const cleanedHeaders =
|
|
4191
|
+
function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
|
|
4192
|
+
const cleanedHeaders = removeUndefinedEntries(
|
|
2874
4193
|
headers != null ? headers : {}
|
|
2875
4194
|
);
|
|
2876
4195
|
const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
|
|
@@ -2881,14 +4200,14 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
2881
4200
|
}
|
|
2882
4201
|
|
|
2883
4202
|
// src/version.ts
|
|
2884
|
-
var
|
|
4203
|
+
var VERSION2 = false ? "0.0.0-test" : "2.0.0";
|
|
2885
4204
|
|
|
2886
4205
|
// src/provider.ts
|
|
2887
4206
|
function createOpenRouter(options = {}) {
|
|
2888
|
-
var
|
|
2889
|
-
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";
|
|
2890
4209
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2891
|
-
const getHeaders = () =>
|
|
4210
|
+
const getHeaders = () => withUserAgentSuffix2(
|
|
2892
4211
|
__spreadValues(__spreadValues({
|
|
2893
4212
|
Authorization: `Bearer ${loadApiKey({
|
|
2894
4213
|
apiKey: options.apiKey,
|
|
@@ -2898,7 +4217,7 @@ function createOpenRouter(options = {}) {
|
|
|
2898
4217
|
}, options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
|
|
2899
4218
|
"X-Provider-API-Keys": JSON.stringify(options.api_keys)
|
|
2900
4219
|
}),
|
|
2901
|
-
`ai-sdk/openrouter/${
|
|
4220
|
+
`ai-sdk/openrouter/${VERSION2}`
|
|
2902
4221
|
);
|
|
2903
4222
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2904
4223
|
provider: "openrouter.chat",
|
|
@@ -2949,31 +4268,10 @@ var openrouter = createOpenRouter({
|
|
|
2949
4268
|
compatibility: "strict"
|
|
2950
4269
|
// strict for OpenRouter API
|
|
2951
4270
|
});
|
|
2952
|
-
|
|
2953
|
-
// src/toon/index.ts
|
|
2954
|
-
async function getToonModule() {
|
|
2955
|
-
try {
|
|
2956
|
-
return await import("@toon-format/toon");
|
|
2957
|
-
} catch (e) {
|
|
2958
|
-
throw new Error(
|
|
2959
|
-
"The @toon-format/toon package is required for TOON encoding/decoding. Install it with: npm install @toon-format/toon"
|
|
2960
|
-
);
|
|
2961
|
-
}
|
|
2962
|
-
}
|
|
2963
|
-
async function encodeToon(value, options) {
|
|
2964
|
-
const toon = await getToonModule();
|
|
2965
|
-
return toon.encode(value, options);
|
|
2966
|
-
}
|
|
2967
|
-
async function decodeToon(input, options) {
|
|
2968
|
-
const toon = await getToonModule();
|
|
2969
|
-
return toon.decode(input, options);
|
|
2970
|
-
}
|
|
2971
4271
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2972
4272
|
0 && (module.exports = {
|
|
2973
4273
|
OpenRouter,
|
|
2974
4274
|
createOpenRouter,
|
|
2975
|
-
decodeToon,
|
|
2976
|
-
encodeToon,
|
|
2977
4275
|
openrouter
|
|
2978
4276
|
});
|
|
2979
4277
|
//# sourceMappingURL=index.js.map
|