@apertis/ai-sdk-provider 2.1.0 → 3.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 +49 -27
- package/dist/index.cjs +2137 -327
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2137 -327
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -6,8 +6,8 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
8
|
var __export = (target, all) => {
|
|
9
|
-
for (var
|
|
10
|
-
__defProp(target,
|
|
9
|
+
for (var name15 in all)
|
|
10
|
+
__defProp(target, name15, { get: all[name15], enumerable: true });
|
|
11
11
|
};
|
|
12
12
|
var __copyProps = (to, from, except, desc) => {
|
|
13
13
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -35,7 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
37
|
|
|
38
|
-
// node_modules/.pnpm/@ai-sdk+provider@
|
|
38
|
+
// node_modules/.pnpm/@ai-sdk+provider@3.0.8/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
39
39
|
var marker = "vercel.ai.error";
|
|
40
40
|
var symbol = Symbol.for(marker);
|
|
41
41
|
var _a;
|
|
@@ -50,13 +50,13 @@ var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
|
50
50
|
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
51
51
|
*/
|
|
52
52
|
constructor({
|
|
53
|
-
name:
|
|
53
|
+
name: name142,
|
|
54
54
|
message,
|
|
55
55
|
cause
|
|
56
56
|
}) {
|
|
57
57
|
super(message);
|
|
58
58
|
this[_a] = true;
|
|
59
|
-
this.name =
|
|
59
|
+
this.name = name142;
|
|
60
60
|
this.cause = cause;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
@@ -67,8 +67,8 @@ var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
|
67
67
|
static isInstance(error) {
|
|
68
68
|
return _AISDKError.hasMarker(error, marker);
|
|
69
69
|
}
|
|
70
|
-
static hasMarker(error,
|
|
71
|
-
const markerSymbol = Symbol.for(
|
|
70
|
+
static hasMarker(error, marker152) {
|
|
71
|
+
const markerSymbol = Symbol.for(marker152);
|
|
72
72
|
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
73
73
|
}
|
|
74
74
|
};
|
|
@@ -305,34 +305,61 @@ var symbol13 = Symbol.for(marker13);
|
|
|
305
305
|
var _a13;
|
|
306
306
|
var _b13;
|
|
307
307
|
var TypeValidationError = class _TypeValidationError extends (_b13 = AISDKError, _a13 = symbol13, _b13) {
|
|
308
|
-
constructor({
|
|
308
|
+
constructor({
|
|
309
|
+
value,
|
|
310
|
+
cause,
|
|
311
|
+
context
|
|
312
|
+
}) {
|
|
313
|
+
let contextPrefix = "Type validation failed";
|
|
314
|
+
if (context == null ? void 0 : context.field) {
|
|
315
|
+
contextPrefix += ` for ${context.field}`;
|
|
316
|
+
}
|
|
317
|
+
if ((context == null ? void 0 : context.entityName) || (context == null ? void 0 : context.entityId)) {
|
|
318
|
+
contextPrefix += " (";
|
|
319
|
+
const parts = [];
|
|
320
|
+
if (context.entityName) {
|
|
321
|
+
parts.push(context.entityName);
|
|
322
|
+
}
|
|
323
|
+
if (context.entityId) {
|
|
324
|
+
parts.push(`id: "${context.entityId}"`);
|
|
325
|
+
}
|
|
326
|
+
contextPrefix += parts.join(", ");
|
|
327
|
+
contextPrefix += ")";
|
|
328
|
+
}
|
|
309
329
|
super({
|
|
310
330
|
name: name12,
|
|
311
|
-
message:
|
|
331
|
+
message: `${contextPrefix}: Value: ${JSON.stringify(value)}.
|
|
312
332
|
Error message: ${getErrorMessage(cause)}`,
|
|
313
333
|
cause
|
|
314
334
|
});
|
|
315
335
|
this[_a13] = true;
|
|
316
336
|
this.value = value;
|
|
337
|
+
this.context = context;
|
|
317
338
|
}
|
|
318
339
|
static isInstance(error) {
|
|
319
340
|
return AISDKError.hasMarker(error, marker13);
|
|
320
341
|
}
|
|
321
342
|
/**
|
|
322
343
|
* Wraps an error into a TypeValidationError.
|
|
323
|
-
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
344
|
+
* If the cause is already a TypeValidationError with the same value and context, it returns the cause.
|
|
324
345
|
* Otherwise, it creates a new TypeValidationError.
|
|
325
346
|
*
|
|
326
347
|
* @param {Object} params - The parameters for wrapping the error.
|
|
327
348
|
* @param {unknown} params.value - The value that failed validation.
|
|
328
349
|
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
350
|
+
* @param {TypeValidationContext} params.context - Optional context about what is being validated.
|
|
329
351
|
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
330
352
|
*/
|
|
331
353
|
static wrap({
|
|
332
354
|
value,
|
|
333
|
-
cause
|
|
355
|
+
cause,
|
|
356
|
+
context
|
|
334
357
|
}) {
|
|
335
|
-
|
|
358
|
+
var _a152, _b152, _c;
|
|
359
|
+
if (_TypeValidationError.isInstance(cause) && cause.value === value && ((_a152 = cause.context) == null ? void 0 : _a152.field) === (context == null ? void 0 : context.field) && ((_b152 = cause.context) == null ? void 0 : _b152.entityName) === (context == null ? void 0 : context.entityName) && ((_c = cause.context) == null ? void 0 : _c.entityId) === (context == null ? void 0 : context.entityId)) {
|
|
360
|
+
return cause;
|
|
361
|
+
}
|
|
362
|
+
return new _TypeValidationError({ value, cause, context });
|
|
336
363
|
}
|
|
337
364
|
};
|
|
338
365
|
var name13 = "AI_UnsupportedFunctionalityError";
|
|
@@ -354,6 +381,12 @@ var UnsupportedFunctionalityError = class extends (_b14 = AISDKError, _a14 = sym
|
|
|
354
381
|
}
|
|
355
382
|
};
|
|
356
383
|
|
|
384
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@4.0.23_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
385
|
+
var z4 = __toESM(require("zod/v4"), 1);
|
|
386
|
+
var import_v3 = require("zod/v3");
|
|
387
|
+
var import_v32 = require("zod/v3");
|
|
388
|
+
var import_v33 = require("zod/v3");
|
|
389
|
+
|
|
357
390
|
// node_modules/.pnpm/eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/index.js
|
|
358
391
|
var ParseError = class extends Error {
|
|
359
392
|
constructor(message, options) {
|
|
@@ -481,11 +514,7 @@ var EventSourceParserStream = class extends TransformStream {
|
|
|
481
514
|
}
|
|
482
515
|
};
|
|
483
516
|
|
|
484
|
-
// node_modules/.pnpm/@ai-sdk+provider-utils@
|
|
485
|
-
var z4 = __toESM(require("zod/v4"), 1);
|
|
486
|
-
var import_v3 = require("zod/v3");
|
|
487
|
-
var import_v32 = require("zod/v3");
|
|
488
|
-
var import_v33 = require("zod/v3");
|
|
517
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@4.0.23_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
489
518
|
function combineHeaders(...headers) {
|
|
490
519
|
return headers.reduce(
|
|
491
520
|
(combinedHeaders, currentHeaders) => ({
|
|
@@ -498,6 +527,248 @@ function combineHeaders(...headers) {
|
|
|
498
527
|
function extractResponseHeaders(response) {
|
|
499
528
|
return Object.fromEntries([...response.headers]);
|
|
500
529
|
}
|
|
530
|
+
var { btoa, atob } = globalThis;
|
|
531
|
+
function convertBase64ToUint8Array(base64String) {
|
|
532
|
+
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
|
|
533
|
+
const latin1string = atob(base64Url);
|
|
534
|
+
return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
|
|
535
|
+
}
|
|
536
|
+
function convertUint8ArrayToBase64(array) {
|
|
537
|
+
let latin1string = "";
|
|
538
|
+
for (let i = 0; i < array.length; i++) {
|
|
539
|
+
latin1string += String.fromCodePoint(array[i]);
|
|
540
|
+
}
|
|
541
|
+
return btoa(latin1string);
|
|
542
|
+
}
|
|
543
|
+
function convertToBase64(value) {
|
|
544
|
+
return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
|
|
545
|
+
}
|
|
546
|
+
function convertToFormData(input, options = {}) {
|
|
547
|
+
const { useArrayBrackets = true } = options;
|
|
548
|
+
const formData = new FormData();
|
|
549
|
+
for (const [key, value] of Object.entries(input)) {
|
|
550
|
+
if (value == null) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (Array.isArray(value)) {
|
|
554
|
+
if (value.length === 1) {
|
|
555
|
+
formData.append(key, value[0]);
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
const arrayKey = useArrayBrackets ? `${key}[]` : key;
|
|
559
|
+
for (const item of value) {
|
|
560
|
+
formData.append(arrayKey, item);
|
|
561
|
+
}
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
formData.append(key, value);
|
|
565
|
+
}
|
|
566
|
+
return formData;
|
|
567
|
+
}
|
|
568
|
+
var name14 = "AI_DownloadError";
|
|
569
|
+
var marker15 = `vercel.ai.error.${name14}`;
|
|
570
|
+
var symbol15 = Symbol.for(marker15);
|
|
571
|
+
var _a15;
|
|
572
|
+
var _b15;
|
|
573
|
+
var DownloadError = class extends (_b15 = AISDKError, _a15 = symbol15, _b15) {
|
|
574
|
+
constructor({
|
|
575
|
+
url,
|
|
576
|
+
statusCode,
|
|
577
|
+
statusText,
|
|
578
|
+
cause,
|
|
579
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
|
580
|
+
}) {
|
|
581
|
+
super({ name: name14, message, cause });
|
|
582
|
+
this[_a15] = true;
|
|
583
|
+
this.url = url;
|
|
584
|
+
this.statusCode = statusCode;
|
|
585
|
+
this.statusText = statusText;
|
|
586
|
+
}
|
|
587
|
+
static isInstance(error) {
|
|
588
|
+
return AISDKError.hasMarker(error, marker15);
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
var DEFAULT_MAX_DOWNLOAD_SIZE = 2 * 1024 * 1024 * 1024;
|
|
592
|
+
async function readResponseWithSizeLimit({
|
|
593
|
+
response,
|
|
594
|
+
url,
|
|
595
|
+
maxBytes = DEFAULT_MAX_DOWNLOAD_SIZE
|
|
596
|
+
}) {
|
|
597
|
+
const contentLength = response.headers.get("content-length");
|
|
598
|
+
if (contentLength != null) {
|
|
599
|
+
const length = parseInt(contentLength, 10);
|
|
600
|
+
if (!isNaN(length) && length > maxBytes) {
|
|
601
|
+
throw new DownloadError({
|
|
602
|
+
url,
|
|
603
|
+
message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes (Content-Length: ${length}).`
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const body = response.body;
|
|
608
|
+
if (body == null) {
|
|
609
|
+
return new Uint8Array(0);
|
|
610
|
+
}
|
|
611
|
+
const reader = body.getReader();
|
|
612
|
+
const chunks = [];
|
|
613
|
+
let totalBytes = 0;
|
|
614
|
+
try {
|
|
615
|
+
while (true) {
|
|
616
|
+
const { done, value } = await reader.read();
|
|
617
|
+
if (done) {
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
totalBytes += value.length;
|
|
621
|
+
if (totalBytes > maxBytes) {
|
|
622
|
+
throw new DownloadError({
|
|
623
|
+
url,
|
|
624
|
+
message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes.`
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
chunks.push(value);
|
|
628
|
+
}
|
|
629
|
+
} finally {
|
|
630
|
+
try {
|
|
631
|
+
await reader.cancel();
|
|
632
|
+
} finally {
|
|
633
|
+
reader.releaseLock();
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const result = new Uint8Array(totalBytes);
|
|
637
|
+
let offset = 0;
|
|
638
|
+
for (const chunk of chunks) {
|
|
639
|
+
result.set(chunk, offset);
|
|
640
|
+
offset += chunk.length;
|
|
641
|
+
}
|
|
642
|
+
return result;
|
|
643
|
+
}
|
|
644
|
+
function validateDownloadUrl(url) {
|
|
645
|
+
let parsed;
|
|
646
|
+
try {
|
|
647
|
+
parsed = new URL(url);
|
|
648
|
+
} catch (e) {
|
|
649
|
+
throw new DownloadError({
|
|
650
|
+
url,
|
|
651
|
+
message: `Invalid URL: ${url}`
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
if (parsed.protocol === "data:") {
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
658
|
+
throw new DownloadError({
|
|
659
|
+
url,
|
|
660
|
+
message: `URL scheme must be http, https, or data, got ${parsed.protocol}`
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
const hostname = parsed.hostname;
|
|
664
|
+
if (!hostname) {
|
|
665
|
+
throw new DownloadError({
|
|
666
|
+
url,
|
|
667
|
+
message: `URL must have a hostname`
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
if (hostname === "localhost" || hostname.endsWith(".local") || hostname.endsWith(".localhost")) {
|
|
671
|
+
throw new DownloadError({
|
|
672
|
+
url,
|
|
673
|
+
message: `URL with hostname ${hostname} is not allowed`
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
if (hostname.startsWith("[") && hostname.endsWith("]")) {
|
|
677
|
+
const ipv6 = hostname.slice(1, -1);
|
|
678
|
+
if (isPrivateIPv6(ipv6)) {
|
|
679
|
+
throw new DownloadError({
|
|
680
|
+
url,
|
|
681
|
+
message: `URL with IPv6 address ${hostname} is not allowed`
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
if (isIPv4(hostname)) {
|
|
687
|
+
if (isPrivateIPv4(hostname)) {
|
|
688
|
+
throw new DownloadError({
|
|
689
|
+
url,
|
|
690
|
+
message: `URL with IP address ${hostname} is not allowed`
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
function isIPv4(hostname) {
|
|
697
|
+
const parts = hostname.split(".");
|
|
698
|
+
if (parts.length !== 4) return false;
|
|
699
|
+
return parts.every((part) => {
|
|
700
|
+
const num = Number(part);
|
|
701
|
+
return Number.isInteger(num) && num >= 0 && num <= 255 && String(num) === part;
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
function isPrivateIPv4(ip) {
|
|
705
|
+
const parts = ip.split(".").map(Number);
|
|
706
|
+
const [a, b] = parts;
|
|
707
|
+
if (a === 0) return true;
|
|
708
|
+
if (a === 10) return true;
|
|
709
|
+
if (a === 127) return true;
|
|
710
|
+
if (a === 169 && b === 254) return true;
|
|
711
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
712
|
+
if (a === 192 && b === 168) return true;
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
function isPrivateIPv6(ip) {
|
|
716
|
+
const normalized = ip.toLowerCase();
|
|
717
|
+
if (normalized === "::1") return true;
|
|
718
|
+
if (normalized === "::") return true;
|
|
719
|
+
if (normalized.startsWith("::ffff:")) {
|
|
720
|
+
const mappedPart = normalized.slice(7);
|
|
721
|
+
if (isIPv4(mappedPart)) {
|
|
722
|
+
return isPrivateIPv4(mappedPart);
|
|
723
|
+
}
|
|
724
|
+
const hexParts = mappedPart.split(":");
|
|
725
|
+
if (hexParts.length === 2) {
|
|
726
|
+
const high = parseInt(hexParts[0], 16);
|
|
727
|
+
const low = parseInt(hexParts[1], 16);
|
|
728
|
+
if (!isNaN(high) && !isNaN(low)) {
|
|
729
|
+
const a = high >> 8 & 255;
|
|
730
|
+
const b = high & 255;
|
|
731
|
+
const c = low >> 8 & 255;
|
|
732
|
+
const d = low & 255;
|
|
733
|
+
return isPrivateIPv4(`${a}.${b}.${c}.${d}`);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (normalized.startsWith("fc") || normalized.startsWith("fd")) return true;
|
|
738
|
+
if (normalized.startsWith("fe80")) return true;
|
|
739
|
+
return false;
|
|
740
|
+
}
|
|
741
|
+
async function downloadBlob(url, options) {
|
|
742
|
+
var _a22, _b22;
|
|
743
|
+
validateDownloadUrl(url);
|
|
744
|
+
try {
|
|
745
|
+
const response = await fetch(url, {
|
|
746
|
+
signal: options == null ? void 0 : options.abortSignal
|
|
747
|
+
});
|
|
748
|
+
if (response.redirected) {
|
|
749
|
+
validateDownloadUrl(response.url);
|
|
750
|
+
}
|
|
751
|
+
if (!response.ok) {
|
|
752
|
+
throw new DownloadError({
|
|
753
|
+
url,
|
|
754
|
+
statusCode: response.status,
|
|
755
|
+
statusText: response.statusText
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
const data = await readResponseWithSizeLimit({
|
|
759
|
+
response,
|
|
760
|
+
url,
|
|
761
|
+
maxBytes: (_a22 = options == null ? void 0 : options.maxBytes) != null ? _a22 : DEFAULT_MAX_DOWNLOAD_SIZE
|
|
762
|
+
});
|
|
763
|
+
const contentType = (_b22 = response.headers.get("content-type")) != null ? _b22 : void 0;
|
|
764
|
+
return new Blob([data], contentType ? { type: contentType } : void 0);
|
|
765
|
+
} catch (error) {
|
|
766
|
+
if (DownloadError.isInstance(error)) {
|
|
767
|
+
throw error;
|
|
768
|
+
}
|
|
769
|
+
throw new DownloadError({ url, cause: error });
|
|
770
|
+
}
|
|
771
|
+
}
|
|
501
772
|
var createIdGenerator = ({
|
|
502
773
|
prefix,
|
|
503
774
|
size = 16,
|
|
@@ -529,6 +800,25 @@ function isAbortError(error) {
|
|
|
529
800
|
error.name === "TimeoutError");
|
|
530
801
|
}
|
|
531
802
|
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
|
803
|
+
var BUN_ERROR_CODES = [
|
|
804
|
+
"ConnectionRefused",
|
|
805
|
+
"ConnectionClosed",
|
|
806
|
+
"FailedToOpenSocket",
|
|
807
|
+
"ECONNRESET",
|
|
808
|
+
"ECONNREFUSED",
|
|
809
|
+
"ETIMEDOUT",
|
|
810
|
+
"EPIPE"
|
|
811
|
+
];
|
|
812
|
+
function isBunNetworkError(error) {
|
|
813
|
+
if (!(error instanceof Error)) {
|
|
814
|
+
return false;
|
|
815
|
+
}
|
|
816
|
+
const code = error.code;
|
|
817
|
+
if (typeof code === "string" && BUN_ERROR_CODES.includes(code)) {
|
|
818
|
+
return true;
|
|
819
|
+
}
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
532
822
|
function handleFetchError({
|
|
533
823
|
error,
|
|
534
824
|
url,
|
|
@@ -550,17 +840,26 @@ function handleFetchError({
|
|
|
550
840
|
});
|
|
551
841
|
}
|
|
552
842
|
}
|
|
843
|
+
if (isBunNetworkError(error)) {
|
|
844
|
+
return new APICallError({
|
|
845
|
+
message: `Cannot connect to API: ${error.message}`,
|
|
846
|
+
cause: error,
|
|
847
|
+
url,
|
|
848
|
+
requestBodyValues,
|
|
849
|
+
isRetryable: true
|
|
850
|
+
});
|
|
851
|
+
}
|
|
553
852
|
return error;
|
|
554
853
|
}
|
|
555
854
|
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
|
556
|
-
var
|
|
855
|
+
var _a22, _b22, _c;
|
|
557
856
|
if (globalThisAny.window) {
|
|
558
857
|
return `runtime/browser`;
|
|
559
858
|
}
|
|
560
|
-
if ((
|
|
859
|
+
if ((_a22 = globalThisAny.navigator) == null ? void 0 : _a22.userAgent) {
|
|
561
860
|
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
|
562
861
|
}
|
|
563
|
-
if ((_c = (
|
|
862
|
+
if ((_c = (_b22 = globalThisAny.process) == null ? void 0 : _b22.versions) == null ? void 0 : _c.node) {
|
|
564
863
|
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
|
565
864
|
}
|
|
566
865
|
if (globalThisAny.EdgeRuntime) {
|
|
@@ -598,9 +897,9 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
598
897
|
);
|
|
599
898
|
return Object.fromEntries(normalizedHeaders.entries());
|
|
600
899
|
}
|
|
601
|
-
var VERSION = true ? "
|
|
602
|
-
var suspectProtoRx = /"
|
|
603
|
-
var suspectConstructorRx = /"
|
|
900
|
+
var VERSION = true ? "4.0.23" : "0.0.0-test";
|
|
901
|
+
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
902
|
+
var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
604
903
|
function _parse(text) {
|
|
605
904
|
const obj = JSON.parse(text);
|
|
606
905
|
if (obj === null || typeof obj !== "object") {
|
|
@@ -620,7 +919,7 @@ function filter(obj) {
|
|
|
620
919
|
if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
621
920
|
throw new SyntaxError("Object contains forbidden prototype property");
|
|
622
921
|
}
|
|
623
|
-
if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
922
|
+
if (Object.prototype.hasOwnProperty.call(node, "constructor") && node.constructor !== null && typeof node.constructor === "object" && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
624
923
|
throw new SyntaxError("Object contains forbidden prototype property");
|
|
625
924
|
}
|
|
626
925
|
for (const key in node) {
|
|
@@ -631,75 +930,1278 @@ function filter(obj) {
|
|
|
631
930
|
}
|
|
632
931
|
}
|
|
633
932
|
}
|
|
634
|
-
return obj;
|
|
933
|
+
return obj;
|
|
934
|
+
}
|
|
935
|
+
function secureJsonParse(text) {
|
|
936
|
+
const { stackTraceLimit } = Error;
|
|
937
|
+
try {
|
|
938
|
+
Error.stackTraceLimit = 0;
|
|
939
|
+
} catch (e) {
|
|
940
|
+
return _parse(text);
|
|
941
|
+
}
|
|
942
|
+
try {
|
|
943
|
+
return _parse(text);
|
|
944
|
+
} finally {
|
|
945
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
function addAdditionalPropertiesToJsonSchema(jsonSchema2) {
|
|
949
|
+
if (jsonSchema2.type === "object" || Array.isArray(jsonSchema2.type) && jsonSchema2.type.includes("object")) {
|
|
950
|
+
jsonSchema2.additionalProperties = false;
|
|
951
|
+
const { properties } = jsonSchema2;
|
|
952
|
+
if (properties != null) {
|
|
953
|
+
for (const key of Object.keys(properties)) {
|
|
954
|
+
properties[key] = visit(properties[key]);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
if (jsonSchema2.items != null) {
|
|
959
|
+
jsonSchema2.items = Array.isArray(jsonSchema2.items) ? jsonSchema2.items.map(visit) : visit(jsonSchema2.items);
|
|
960
|
+
}
|
|
961
|
+
if (jsonSchema2.anyOf != null) {
|
|
962
|
+
jsonSchema2.anyOf = jsonSchema2.anyOf.map(visit);
|
|
963
|
+
}
|
|
964
|
+
if (jsonSchema2.allOf != null) {
|
|
965
|
+
jsonSchema2.allOf = jsonSchema2.allOf.map(visit);
|
|
966
|
+
}
|
|
967
|
+
if (jsonSchema2.oneOf != null) {
|
|
968
|
+
jsonSchema2.oneOf = jsonSchema2.oneOf.map(visit);
|
|
969
|
+
}
|
|
970
|
+
const { definitions } = jsonSchema2;
|
|
971
|
+
if (definitions != null) {
|
|
972
|
+
for (const key of Object.keys(definitions)) {
|
|
973
|
+
definitions[key] = visit(definitions[key]);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return jsonSchema2;
|
|
977
|
+
}
|
|
978
|
+
function visit(def) {
|
|
979
|
+
if (typeof def === "boolean") return def;
|
|
980
|
+
return addAdditionalPropertiesToJsonSchema(def);
|
|
981
|
+
}
|
|
982
|
+
var ignoreOverride = /* @__PURE__ */ Symbol(
|
|
983
|
+
"Let zodToJsonSchema decide on which parser to use"
|
|
984
|
+
);
|
|
985
|
+
var defaultOptions = {
|
|
986
|
+
name: void 0,
|
|
987
|
+
$refStrategy: "root",
|
|
988
|
+
basePath: ["#"],
|
|
989
|
+
effectStrategy: "input",
|
|
990
|
+
pipeStrategy: "all",
|
|
991
|
+
dateStrategy: "format:date-time",
|
|
992
|
+
mapStrategy: "entries",
|
|
993
|
+
removeAdditionalStrategy: "passthrough",
|
|
994
|
+
allowedAdditionalProperties: true,
|
|
995
|
+
rejectedAdditionalProperties: false,
|
|
996
|
+
definitionPath: "definitions",
|
|
997
|
+
strictUnions: false,
|
|
998
|
+
definitions: {},
|
|
999
|
+
errorMessages: false,
|
|
1000
|
+
patternStrategy: "escape",
|
|
1001
|
+
applyRegexFlags: false,
|
|
1002
|
+
emailStrategy: "format:email",
|
|
1003
|
+
base64Strategy: "contentEncoding:base64",
|
|
1004
|
+
nameStrategy: "ref"
|
|
1005
|
+
};
|
|
1006
|
+
var getDefaultOptions = (options) => typeof options === "string" ? {
|
|
1007
|
+
...defaultOptions,
|
|
1008
|
+
name: options
|
|
1009
|
+
} : {
|
|
1010
|
+
...defaultOptions,
|
|
1011
|
+
...options
|
|
1012
|
+
};
|
|
1013
|
+
function parseAnyDef() {
|
|
1014
|
+
return {};
|
|
1015
|
+
}
|
|
1016
|
+
function parseArrayDef(def, refs) {
|
|
1017
|
+
var _a22, _b22, _c;
|
|
1018
|
+
const res = {
|
|
1019
|
+
type: "array"
|
|
1020
|
+
};
|
|
1021
|
+
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) {
|
|
1022
|
+
res.items = parseDef(def.type._def, {
|
|
1023
|
+
...refs,
|
|
1024
|
+
currentPath: [...refs.currentPath, "items"]
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
if (def.minLength) {
|
|
1028
|
+
res.minItems = def.minLength.value;
|
|
1029
|
+
}
|
|
1030
|
+
if (def.maxLength) {
|
|
1031
|
+
res.maxItems = def.maxLength.value;
|
|
1032
|
+
}
|
|
1033
|
+
if (def.exactLength) {
|
|
1034
|
+
res.minItems = def.exactLength.value;
|
|
1035
|
+
res.maxItems = def.exactLength.value;
|
|
1036
|
+
}
|
|
1037
|
+
return res;
|
|
1038
|
+
}
|
|
1039
|
+
function parseBigintDef(def) {
|
|
1040
|
+
const res = {
|
|
1041
|
+
type: "integer",
|
|
1042
|
+
format: "int64"
|
|
1043
|
+
};
|
|
1044
|
+
if (!def.checks) return res;
|
|
1045
|
+
for (const check of def.checks) {
|
|
1046
|
+
switch (check.kind) {
|
|
1047
|
+
case "min":
|
|
1048
|
+
if (check.inclusive) {
|
|
1049
|
+
res.minimum = check.value;
|
|
1050
|
+
} else {
|
|
1051
|
+
res.exclusiveMinimum = check.value;
|
|
1052
|
+
}
|
|
1053
|
+
break;
|
|
1054
|
+
case "max":
|
|
1055
|
+
if (check.inclusive) {
|
|
1056
|
+
res.maximum = check.value;
|
|
1057
|
+
} else {
|
|
1058
|
+
res.exclusiveMaximum = check.value;
|
|
1059
|
+
}
|
|
1060
|
+
break;
|
|
1061
|
+
case "multipleOf":
|
|
1062
|
+
res.multipleOf = check.value;
|
|
1063
|
+
break;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
return res;
|
|
1067
|
+
}
|
|
1068
|
+
function parseBooleanDef() {
|
|
1069
|
+
return { type: "boolean" };
|
|
1070
|
+
}
|
|
1071
|
+
function parseBrandedDef(_def, refs) {
|
|
1072
|
+
return parseDef(_def.type._def, refs);
|
|
1073
|
+
}
|
|
1074
|
+
var parseCatchDef = (def, refs) => {
|
|
1075
|
+
return parseDef(def.innerType._def, refs);
|
|
1076
|
+
};
|
|
1077
|
+
function parseDateDef(def, refs, overrideDateStrategy) {
|
|
1078
|
+
const strategy = overrideDateStrategy != null ? overrideDateStrategy : refs.dateStrategy;
|
|
1079
|
+
if (Array.isArray(strategy)) {
|
|
1080
|
+
return {
|
|
1081
|
+
anyOf: strategy.map((item, i) => parseDateDef(def, refs, item))
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
switch (strategy) {
|
|
1085
|
+
case "string":
|
|
1086
|
+
case "format:date-time":
|
|
1087
|
+
return {
|
|
1088
|
+
type: "string",
|
|
1089
|
+
format: "date-time"
|
|
1090
|
+
};
|
|
1091
|
+
case "format:date":
|
|
1092
|
+
return {
|
|
1093
|
+
type: "string",
|
|
1094
|
+
format: "date"
|
|
1095
|
+
};
|
|
1096
|
+
case "integer":
|
|
1097
|
+
return integerDateParser(def);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
var integerDateParser = (def) => {
|
|
1101
|
+
const res = {
|
|
1102
|
+
type: "integer",
|
|
1103
|
+
format: "unix-time"
|
|
1104
|
+
};
|
|
1105
|
+
for (const check of def.checks) {
|
|
1106
|
+
switch (check.kind) {
|
|
1107
|
+
case "min":
|
|
1108
|
+
res.minimum = check.value;
|
|
1109
|
+
break;
|
|
1110
|
+
case "max":
|
|
1111
|
+
res.maximum = check.value;
|
|
1112
|
+
break;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
return res;
|
|
1116
|
+
};
|
|
1117
|
+
function parseDefaultDef(_def, refs) {
|
|
1118
|
+
return {
|
|
1119
|
+
...parseDef(_def.innerType._def, refs),
|
|
1120
|
+
default: _def.defaultValue()
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
function parseEffectsDef(_def, refs) {
|
|
1124
|
+
return refs.effectStrategy === "input" ? parseDef(_def.schema._def, refs) : parseAnyDef();
|
|
1125
|
+
}
|
|
1126
|
+
function parseEnumDef(def) {
|
|
1127
|
+
return {
|
|
1128
|
+
type: "string",
|
|
1129
|
+
enum: Array.from(def.values)
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
var isJsonSchema7AllOfType = (type) => {
|
|
1133
|
+
if ("type" in type && type.type === "string") return false;
|
|
1134
|
+
return "allOf" in type;
|
|
1135
|
+
};
|
|
1136
|
+
function parseIntersectionDef(def, refs) {
|
|
1137
|
+
const allOf = [
|
|
1138
|
+
parseDef(def.left._def, {
|
|
1139
|
+
...refs,
|
|
1140
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
1141
|
+
}),
|
|
1142
|
+
parseDef(def.right._def, {
|
|
1143
|
+
...refs,
|
|
1144
|
+
currentPath: [...refs.currentPath, "allOf", "1"]
|
|
1145
|
+
})
|
|
1146
|
+
].filter((x) => !!x);
|
|
1147
|
+
const mergedAllOf = [];
|
|
1148
|
+
allOf.forEach((schema) => {
|
|
1149
|
+
if (isJsonSchema7AllOfType(schema)) {
|
|
1150
|
+
mergedAllOf.push(...schema.allOf);
|
|
1151
|
+
} else {
|
|
1152
|
+
let nestedSchema = schema;
|
|
1153
|
+
if ("additionalProperties" in schema && schema.additionalProperties === false) {
|
|
1154
|
+
const { additionalProperties, ...rest } = schema;
|
|
1155
|
+
nestedSchema = rest;
|
|
1156
|
+
}
|
|
1157
|
+
mergedAllOf.push(nestedSchema);
|
|
1158
|
+
}
|
|
1159
|
+
});
|
|
1160
|
+
return mergedAllOf.length ? { allOf: mergedAllOf } : void 0;
|
|
1161
|
+
}
|
|
1162
|
+
function parseLiteralDef(def) {
|
|
1163
|
+
const parsedType = typeof def.value;
|
|
1164
|
+
if (parsedType !== "bigint" && parsedType !== "number" && parsedType !== "boolean" && parsedType !== "string") {
|
|
1165
|
+
return {
|
|
1166
|
+
type: Array.isArray(def.value) ? "array" : "object"
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
return {
|
|
1170
|
+
type: parsedType === "bigint" ? "integer" : parsedType,
|
|
1171
|
+
const: def.value
|
|
1172
|
+
};
|
|
1173
|
+
}
|
|
1174
|
+
var emojiRegex = void 0;
|
|
1175
|
+
var zodPatterns = {
|
|
1176
|
+
/**
|
|
1177
|
+
* `c` was changed to `[cC]` to replicate /i flag
|
|
1178
|
+
*/
|
|
1179
|
+
cuid: /^[cC][^\s-]{8,}$/,
|
|
1180
|
+
cuid2: /^[0-9a-z]+$/,
|
|
1181
|
+
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
1182
|
+
/**
|
|
1183
|
+
* `a-z` was added to replicate /i flag
|
|
1184
|
+
*/
|
|
1185
|
+
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
|
1186
|
+
/**
|
|
1187
|
+
* Constructed a valid Unicode RegExp
|
|
1188
|
+
*
|
|
1189
|
+
* Lazily instantiate since this type of regex isn't supported
|
|
1190
|
+
* in all envs (e.g. React Native).
|
|
1191
|
+
*
|
|
1192
|
+
* See:
|
|
1193
|
+
* https://github.com/colinhacks/zod/issues/2433
|
|
1194
|
+
* Fix in Zod:
|
|
1195
|
+
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
|
1196
|
+
*/
|
|
1197
|
+
emoji: () => {
|
|
1198
|
+
if (emojiRegex === void 0) {
|
|
1199
|
+
emojiRegex = RegExp(
|
|
1200
|
+
"^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
|
|
1201
|
+
"u"
|
|
1202
|
+
);
|
|
1203
|
+
}
|
|
1204
|
+
return emojiRegex;
|
|
1205
|
+
},
|
|
1206
|
+
/**
|
|
1207
|
+
* Unused
|
|
1208
|
+
*/
|
|
1209
|
+
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}$/,
|
|
1210
|
+
/**
|
|
1211
|
+
* Unused
|
|
1212
|
+
*/
|
|
1213
|
+
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])$/,
|
|
1214
|
+
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])$/,
|
|
1215
|
+
/**
|
|
1216
|
+
* Unused
|
|
1217
|
+
*/
|
|
1218
|
+
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})))$/,
|
|
1219
|
+
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])$/,
|
|
1220
|
+
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
|
1221
|
+
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
|
1222
|
+
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
|
1223
|
+
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
|
1224
|
+
};
|
|
1225
|
+
function parseStringDef(def, refs) {
|
|
1226
|
+
const res = {
|
|
1227
|
+
type: "string"
|
|
1228
|
+
};
|
|
1229
|
+
if (def.checks) {
|
|
1230
|
+
for (const check of def.checks) {
|
|
1231
|
+
switch (check.kind) {
|
|
1232
|
+
case "min":
|
|
1233
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
|
1234
|
+
break;
|
|
1235
|
+
case "max":
|
|
1236
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
|
1237
|
+
break;
|
|
1238
|
+
case "email":
|
|
1239
|
+
switch (refs.emailStrategy) {
|
|
1240
|
+
case "format:email":
|
|
1241
|
+
addFormat(res, "email", check.message, refs);
|
|
1242
|
+
break;
|
|
1243
|
+
case "format:idn-email":
|
|
1244
|
+
addFormat(res, "idn-email", check.message, refs);
|
|
1245
|
+
break;
|
|
1246
|
+
case "pattern:zod":
|
|
1247
|
+
addPattern(res, zodPatterns.email, check.message, refs);
|
|
1248
|
+
break;
|
|
1249
|
+
}
|
|
1250
|
+
break;
|
|
1251
|
+
case "url":
|
|
1252
|
+
addFormat(res, "uri", check.message, refs);
|
|
1253
|
+
break;
|
|
1254
|
+
case "uuid":
|
|
1255
|
+
addFormat(res, "uuid", check.message, refs);
|
|
1256
|
+
break;
|
|
1257
|
+
case "regex":
|
|
1258
|
+
addPattern(res, check.regex, check.message, refs);
|
|
1259
|
+
break;
|
|
1260
|
+
case "cuid":
|
|
1261
|
+
addPattern(res, zodPatterns.cuid, check.message, refs);
|
|
1262
|
+
break;
|
|
1263
|
+
case "cuid2":
|
|
1264
|
+
addPattern(res, zodPatterns.cuid2, check.message, refs);
|
|
1265
|
+
break;
|
|
1266
|
+
case "startsWith":
|
|
1267
|
+
addPattern(
|
|
1268
|
+
res,
|
|
1269
|
+
RegExp(`^${escapeLiteralCheckValue(check.value, refs)}`),
|
|
1270
|
+
check.message,
|
|
1271
|
+
refs
|
|
1272
|
+
);
|
|
1273
|
+
break;
|
|
1274
|
+
case "endsWith":
|
|
1275
|
+
addPattern(
|
|
1276
|
+
res,
|
|
1277
|
+
RegExp(`${escapeLiteralCheckValue(check.value, refs)}$`),
|
|
1278
|
+
check.message,
|
|
1279
|
+
refs
|
|
1280
|
+
);
|
|
1281
|
+
break;
|
|
1282
|
+
case "datetime":
|
|
1283
|
+
addFormat(res, "date-time", check.message, refs);
|
|
1284
|
+
break;
|
|
1285
|
+
case "date":
|
|
1286
|
+
addFormat(res, "date", check.message, refs);
|
|
1287
|
+
break;
|
|
1288
|
+
case "time":
|
|
1289
|
+
addFormat(res, "time", check.message, refs);
|
|
1290
|
+
break;
|
|
1291
|
+
case "duration":
|
|
1292
|
+
addFormat(res, "duration", check.message, refs);
|
|
1293
|
+
break;
|
|
1294
|
+
case "length":
|
|
1295
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
|
1296
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
|
1297
|
+
break;
|
|
1298
|
+
case "includes": {
|
|
1299
|
+
addPattern(
|
|
1300
|
+
res,
|
|
1301
|
+
RegExp(escapeLiteralCheckValue(check.value, refs)),
|
|
1302
|
+
check.message,
|
|
1303
|
+
refs
|
|
1304
|
+
);
|
|
1305
|
+
break;
|
|
1306
|
+
}
|
|
1307
|
+
case "ip": {
|
|
1308
|
+
if (check.version !== "v6") {
|
|
1309
|
+
addFormat(res, "ipv4", check.message, refs);
|
|
1310
|
+
}
|
|
1311
|
+
if (check.version !== "v4") {
|
|
1312
|
+
addFormat(res, "ipv6", check.message, refs);
|
|
1313
|
+
}
|
|
1314
|
+
break;
|
|
1315
|
+
}
|
|
1316
|
+
case "base64url":
|
|
1317
|
+
addPattern(res, zodPatterns.base64url, check.message, refs);
|
|
1318
|
+
break;
|
|
1319
|
+
case "jwt":
|
|
1320
|
+
addPattern(res, zodPatterns.jwt, check.message, refs);
|
|
1321
|
+
break;
|
|
1322
|
+
case "cidr": {
|
|
1323
|
+
if (check.version !== "v6") {
|
|
1324
|
+
addPattern(res, zodPatterns.ipv4Cidr, check.message, refs);
|
|
1325
|
+
}
|
|
1326
|
+
if (check.version !== "v4") {
|
|
1327
|
+
addPattern(res, zodPatterns.ipv6Cidr, check.message, refs);
|
|
1328
|
+
}
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
case "emoji":
|
|
1332
|
+
addPattern(res, zodPatterns.emoji(), check.message, refs);
|
|
1333
|
+
break;
|
|
1334
|
+
case "ulid": {
|
|
1335
|
+
addPattern(res, zodPatterns.ulid, check.message, refs);
|
|
1336
|
+
break;
|
|
1337
|
+
}
|
|
1338
|
+
case "base64": {
|
|
1339
|
+
switch (refs.base64Strategy) {
|
|
1340
|
+
case "format:binary": {
|
|
1341
|
+
addFormat(res, "binary", check.message, refs);
|
|
1342
|
+
break;
|
|
1343
|
+
}
|
|
1344
|
+
case "contentEncoding:base64": {
|
|
1345
|
+
res.contentEncoding = "base64";
|
|
1346
|
+
break;
|
|
1347
|
+
}
|
|
1348
|
+
case "pattern:zod": {
|
|
1349
|
+
addPattern(res, zodPatterns.base64, check.message, refs);
|
|
1350
|
+
break;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
break;
|
|
1354
|
+
}
|
|
1355
|
+
case "nanoid": {
|
|
1356
|
+
addPattern(res, zodPatterns.nanoid, check.message, refs);
|
|
1357
|
+
}
|
|
1358
|
+
case "toLowerCase":
|
|
1359
|
+
case "toUpperCase":
|
|
1360
|
+
case "trim":
|
|
1361
|
+
break;
|
|
1362
|
+
default:
|
|
1363
|
+
/* @__PURE__ */ ((_) => {
|
|
1364
|
+
})(check);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
return res;
|
|
1369
|
+
}
|
|
1370
|
+
function escapeLiteralCheckValue(literal, refs) {
|
|
1371
|
+
return refs.patternStrategy === "escape" ? escapeNonAlphaNumeric(literal) : literal;
|
|
1372
|
+
}
|
|
1373
|
+
var ALPHA_NUMERIC = new Set(
|
|
1374
|
+
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
1375
|
+
);
|
|
1376
|
+
function escapeNonAlphaNumeric(source) {
|
|
1377
|
+
let result = "";
|
|
1378
|
+
for (let i = 0; i < source.length; i++) {
|
|
1379
|
+
if (!ALPHA_NUMERIC.has(source[i])) {
|
|
1380
|
+
result += "\\";
|
|
1381
|
+
}
|
|
1382
|
+
result += source[i];
|
|
1383
|
+
}
|
|
1384
|
+
return result;
|
|
1385
|
+
}
|
|
1386
|
+
function addFormat(schema, value, message, refs) {
|
|
1387
|
+
var _a22;
|
|
1388
|
+
if (schema.format || ((_a22 = schema.anyOf) == null ? void 0 : _a22.some((x) => x.format))) {
|
|
1389
|
+
if (!schema.anyOf) {
|
|
1390
|
+
schema.anyOf = [];
|
|
1391
|
+
}
|
|
1392
|
+
if (schema.format) {
|
|
1393
|
+
schema.anyOf.push({
|
|
1394
|
+
format: schema.format
|
|
1395
|
+
});
|
|
1396
|
+
delete schema.format;
|
|
1397
|
+
}
|
|
1398
|
+
schema.anyOf.push({
|
|
1399
|
+
format: value,
|
|
1400
|
+
...message && refs.errorMessages && { errorMessage: { format: message } }
|
|
1401
|
+
});
|
|
1402
|
+
} else {
|
|
1403
|
+
schema.format = value;
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
function addPattern(schema, regex, message, refs) {
|
|
1407
|
+
var _a22;
|
|
1408
|
+
if (schema.pattern || ((_a22 = schema.allOf) == null ? void 0 : _a22.some((x) => x.pattern))) {
|
|
1409
|
+
if (!schema.allOf) {
|
|
1410
|
+
schema.allOf = [];
|
|
1411
|
+
}
|
|
1412
|
+
if (schema.pattern) {
|
|
1413
|
+
schema.allOf.push({
|
|
1414
|
+
pattern: schema.pattern
|
|
1415
|
+
});
|
|
1416
|
+
delete schema.pattern;
|
|
1417
|
+
}
|
|
1418
|
+
schema.allOf.push({
|
|
1419
|
+
pattern: stringifyRegExpWithFlags(regex, refs),
|
|
1420
|
+
...message && refs.errorMessages && { errorMessage: { pattern: message } }
|
|
1421
|
+
});
|
|
1422
|
+
} else {
|
|
1423
|
+
schema.pattern = stringifyRegExpWithFlags(regex, refs);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
function stringifyRegExpWithFlags(regex, refs) {
|
|
1427
|
+
var _a22;
|
|
1428
|
+
if (!refs.applyRegexFlags || !regex.flags) {
|
|
1429
|
+
return regex.source;
|
|
1430
|
+
}
|
|
1431
|
+
const flags = {
|
|
1432
|
+
i: regex.flags.includes("i"),
|
|
1433
|
+
// Case-insensitive
|
|
1434
|
+
m: regex.flags.includes("m"),
|
|
1435
|
+
// `^` and `$` matches adjacent to newline characters
|
|
1436
|
+
s: regex.flags.includes("s")
|
|
1437
|
+
// `.` matches newlines
|
|
1438
|
+
};
|
|
1439
|
+
const source = flags.i ? regex.source.toLowerCase() : regex.source;
|
|
1440
|
+
let pattern = "";
|
|
1441
|
+
let isEscaped = false;
|
|
1442
|
+
let inCharGroup = false;
|
|
1443
|
+
let inCharRange = false;
|
|
1444
|
+
for (let i = 0; i < source.length; i++) {
|
|
1445
|
+
if (isEscaped) {
|
|
1446
|
+
pattern += source[i];
|
|
1447
|
+
isEscaped = false;
|
|
1448
|
+
continue;
|
|
1449
|
+
}
|
|
1450
|
+
if (flags.i) {
|
|
1451
|
+
if (inCharGroup) {
|
|
1452
|
+
if (source[i].match(/[a-z]/)) {
|
|
1453
|
+
if (inCharRange) {
|
|
1454
|
+
pattern += source[i];
|
|
1455
|
+
pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
|
|
1456
|
+
inCharRange = false;
|
|
1457
|
+
} else if (source[i + 1] === "-" && ((_a22 = source[i + 2]) == null ? void 0 : _a22.match(/[a-z]/))) {
|
|
1458
|
+
pattern += source[i];
|
|
1459
|
+
inCharRange = true;
|
|
1460
|
+
} else {
|
|
1461
|
+
pattern += `${source[i]}${source[i].toUpperCase()}`;
|
|
1462
|
+
}
|
|
1463
|
+
continue;
|
|
1464
|
+
}
|
|
1465
|
+
} else if (source[i].match(/[a-z]/)) {
|
|
1466
|
+
pattern += `[${source[i]}${source[i].toUpperCase()}]`;
|
|
1467
|
+
continue;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
if (flags.m) {
|
|
1471
|
+
if (source[i] === "^") {
|
|
1472
|
+
pattern += `(^|(?<=[\r
|
|
1473
|
+
]))`;
|
|
1474
|
+
continue;
|
|
1475
|
+
} else if (source[i] === "$") {
|
|
1476
|
+
pattern += `($|(?=[\r
|
|
1477
|
+
]))`;
|
|
1478
|
+
continue;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
if (flags.s && source[i] === ".") {
|
|
1482
|
+
pattern += inCharGroup ? `${source[i]}\r
|
|
1483
|
+
` : `[${source[i]}\r
|
|
1484
|
+
]`;
|
|
1485
|
+
continue;
|
|
1486
|
+
}
|
|
1487
|
+
pattern += source[i];
|
|
1488
|
+
if (source[i] === "\\") {
|
|
1489
|
+
isEscaped = true;
|
|
1490
|
+
} else if (inCharGroup && source[i] === "]") {
|
|
1491
|
+
inCharGroup = false;
|
|
1492
|
+
} else if (!inCharGroup && source[i] === "[") {
|
|
1493
|
+
inCharGroup = true;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
try {
|
|
1497
|
+
new RegExp(pattern);
|
|
1498
|
+
} catch (e) {
|
|
1499
|
+
console.warn(
|
|
1500
|
+
`Could not convert regex pattern at ${refs.currentPath.join(
|
|
1501
|
+
"/"
|
|
1502
|
+
)} to a flag-independent form! Falling back to the flag-ignorant source`
|
|
1503
|
+
);
|
|
1504
|
+
return regex.source;
|
|
1505
|
+
}
|
|
1506
|
+
return pattern;
|
|
1507
|
+
}
|
|
1508
|
+
function parseRecordDef(def, refs) {
|
|
1509
|
+
var _a22, _b22, _c, _d, _e, _f;
|
|
1510
|
+
const schema = {
|
|
1511
|
+
type: "object",
|
|
1512
|
+
additionalProperties: (_a22 = parseDef(def.valueType._def, {
|
|
1513
|
+
...refs,
|
|
1514
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1515
|
+
})) != null ? _a22 : refs.allowedAdditionalProperties
|
|
1516
|
+
};
|
|
1517
|
+
if (((_b22 = def.keyType) == null ? void 0 : _b22._def.typeName) === import_v33.ZodFirstPartyTypeKind.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
|
|
1518
|
+
const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
|
|
1519
|
+
return {
|
|
1520
|
+
...schema,
|
|
1521
|
+
propertyNames: keyType
|
|
1522
|
+
};
|
|
1523
|
+
} else if (((_d = def.keyType) == null ? void 0 : _d._def.typeName) === import_v33.ZodFirstPartyTypeKind.ZodEnum) {
|
|
1524
|
+
return {
|
|
1525
|
+
...schema,
|
|
1526
|
+
propertyNames: {
|
|
1527
|
+
enum: def.keyType._def.values
|
|
1528
|
+
}
|
|
1529
|
+
};
|
|
1530
|
+
} 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)) {
|
|
1531
|
+
const { type, ...keyType } = parseBrandedDef(
|
|
1532
|
+
def.keyType._def,
|
|
1533
|
+
refs
|
|
1534
|
+
);
|
|
1535
|
+
return {
|
|
1536
|
+
...schema,
|
|
1537
|
+
propertyNames: keyType
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
return schema;
|
|
1541
|
+
}
|
|
1542
|
+
function parseMapDef(def, refs) {
|
|
1543
|
+
if (refs.mapStrategy === "record") {
|
|
1544
|
+
return parseRecordDef(def, refs);
|
|
1545
|
+
}
|
|
1546
|
+
const keys = parseDef(def.keyType._def, {
|
|
1547
|
+
...refs,
|
|
1548
|
+
currentPath: [...refs.currentPath, "items", "items", "0"]
|
|
1549
|
+
}) || parseAnyDef();
|
|
1550
|
+
const values = parseDef(def.valueType._def, {
|
|
1551
|
+
...refs,
|
|
1552
|
+
currentPath: [...refs.currentPath, "items", "items", "1"]
|
|
1553
|
+
}) || parseAnyDef();
|
|
1554
|
+
return {
|
|
1555
|
+
type: "array",
|
|
1556
|
+
maxItems: 125,
|
|
1557
|
+
items: {
|
|
1558
|
+
type: "array",
|
|
1559
|
+
items: [keys, values],
|
|
1560
|
+
minItems: 2,
|
|
1561
|
+
maxItems: 2
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
}
|
|
1565
|
+
function parseNativeEnumDef(def) {
|
|
1566
|
+
const object = def.values;
|
|
1567
|
+
const actualKeys = Object.keys(def.values).filter((key) => {
|
|
1568
|
+
return typeof object[object[key]] !== "number";
|
|
1569
|
+
});
|
|
1570
|
+
const actualValues = actualKeys.map((key) => object[key]);
|
|
1571
|
+
const parsedTypes = Array.from(
|
|
1572
|
+
new Set(actualValues.map((values) => typeof values))
|
|
1573
|
+
);
|
|
1574
|
+
return {
|
|
1575
|
+
type: parsedTypes.length === 1 ? parsedTypes[0] === "string" ? "string" : "number" : ["string", "number"],
|
|
1576
|
+
enum: actualValues
|
|
1577
|
+
};
|
|
1578
|
+
}
|
|
1579
|
+
function parseNeverDef() {
|
|
1580
|
+
return { not: parseAnyDef() };
|
|
1581
|
+
}
|
|
1582
|
+
function parseNullDef() {
|
|
1583
|
+
return {
|
|
1584
|
+
type: "null"
|
|
1585
|
+
};
|
|
1586
|
+
}
|
|
1587
|
+
var primitiveMappings = {
|
|
1588
|
+
ZodString: "string",
|
|
1589
|
+
ZodNumber: "number",
|
|
1590
|
+
ZodBigInt: "integer",
|
|
1591
|
+
ZodBoolean: "boolean",
|
|
1592
|
+
ZodNull: "null"
|
|
1593
|
+
};
|
|
1594
|
+
function parseUnionDef(def, refs) {
|
|
1595
|
+
const options = def.options instanceof Map ? Array.from(def.options.values()) : def.options;
|
|
1596
|
+
if (options.every(
|
|
1597
|
+
(x) => x._def.typeName in primitiveMappings && (!x._def.checks || !x._def.checks.length)
|
|
1598
|
+
)) {
|
|
1599
|
+
const types = options.reduce((types2, x) => {
|
|
1600
|
+
const type = primitiveMappings[x._def.typeName];
|
|
1601
|
+
return type && !types2.includes(type) ? [...types2, type] : types2;
|
|
1602
|
+
}, []);
|
|
1603
|
+
return {
|
|
1604
|
+
type: types.length > 1 ? types : types[0]
|
|
1605
|
+
};
|
|
1606
|
+
} else if (options.every((x) => x._def.typeName === "ZodLiteral" && !x.description)) {
|
|
1607
|
+
const types = options.reduce(
|
|
1608
|
+
(acc, x) => {
|
|
1609
|
+
const type = typeof x._def.value;
|
|
1610
|
+
switch (type) {
|
|
1611
|
+
case "string":
|
|
1612
|
+
case "number":
|
|
1613
|
+
case "boolean":
|
|
1614
|
+
return [...acc, type];
|
|
1615
|
+
case "bigint":
|
|
1616
|
+
return [...acc, "integer"];
|
|
1617
|
+
case "object":
|
|
1618
|
+
if (x._def.value === null) return [...acc, "null"];
|
|
1619
|
+
case "symbol":
|
|
1620
|
+
case "undefined":
|
|
1621
|
+
case "function":
|
|
1622
|
+
default:
|
|
1623
|
+
return acc;
|
|
1624
|
+
}
|
|
1625
|
+
},
|
|
1626
|
+
[]
|
|
1627
|
+
);
|
|
1628
|
+
if (types.length === options.length) {
|
|
1629
|
+
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
|
1630
|
+
return {
|
|
1631
|
+
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
|
1632
|
+
enum: options.reduce(
|
|
1633
|
+
(acc, x) => {
|
|
1634
|
+
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
|
1635
|
+
},
|
|
1636
|
+
[]
|
|
1637
|
+
)
|
|
1638
|
+
};
|
|
1639
|
+
}
|
|
1640
|
+
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
|
|
1641
|
+
return {
|
|
1642
|
+
type: "string",
|
|
1643
|
+
enum: options.reduce(
|
|
1644
|
+
(acc, x) => [
|
|
1645
|
+
...acc,
|
|
1646
|
+
...x._def.values.filter((x2) => !acc.includes(x2))
|
|
1647
|
+
],
|
|
1648
|
+
[]
|
|
1649
|
+
)
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
return asAnyOf(def, refs);
|
|
1653
|
+
}
|
|
1654
|
+
var asAnyOf = (def, refs) => {
|
|
1655
|
+
const anyOf = (def.options instanceof Map ? Array.from(def.options.values()) : def.options).map(
|
|
1656
|
+
(x, i) => parseDef(x._def, {
|
|
1657
|
+
...refs,
|
|
1658
|
+
currentPath: [...refs.currentPath, "anyOf", `${i}`]
|
|
1659
|
+
})
|
|
1660
|
+
).filter(
|
|
1661
|
+
(x) => !!x && (!refs.strictUnions || typeof x === "object" && Object.keys(x).length > 0)
|
|
1662
|
+
);
|
|
1663
|
+
return anyOf.length ? { anyOf } : void 0;
|
|
1664
|
+
};
|
|
1665
|
+
function parseNullableDef(def, refs) {
|
|
1666
|
+
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
|
|
1667
|
+
def.innerType._def.typeName
|
|
1668
|
+
) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) {
|
|
1669
|
+
return {
|
|
1670
|
+
type: [
|
|
1671
|
+
primitiveMappings[def.innerType._def.typeName],
|
|
1672
|
+
"null"
|
|
1673
|
+
]
|
|
1674
|
+
};
|
|
1675
|
+
}
|
|
1676
|
+
const base = parseDef(def.innerType._def, {
|
|
1677
|
+
...refs,
|
|
1678
|
+
currentPath: [...refs.currentPath, "anyOf", "0"]
|
|
1679
|
+
});
|
|
1680
|
+
return base && { anyOf: [base, { type: "null" }] };
|
|
1681
|
+
}
|
|
1682
|
+
function parseNumberDef(def) {
|
|
1683
|
+
const res = {
|
|
1684
|
+
type: "number"
|
|
1685
|
+
};
|
|
1686
|
+
if (!def.checks) return res;
|
|
1687
|
+
for (const check of def.checks) {
|
|
1688
|
+
switch (check.kind) {
|
|
1689
|
+
case "int":
|
|
1690
|
+
res.type = "integer";
|
|
1691
|
+
break;
|
|
1692
|
+
case "min":
|
|
1693
|
+
if (check.inclusive) {
|
|
1694
|
+
res.minimum = check.value;
|
|
1695
|
+
} else {
|
|
1696
|
+
res.exclusiveMinimum = check.value;
|
|
1697
|
+
}
|
|
1698
|
+
break;
|
|
1699
|
+
case "max":
|
|
1700
|
+
if (check.inclusive) {
|
|
1701
|
+
res.maximum = check.value;
|
|
1702
|
+
} else {
|
|
1703
|
+
res.exclusiveMaximum = check.value;
|
|
1704
|
+
}
|
|
1705
|
+
break;
|
|
1706
|
+
case "multipleOf":
|
|
1707
|
+
res.multipleOf = check.value;
|
|
1708
|
+
break;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
return res;
|
|
1712
|
+
}
|
|
1713
|
+
function parseObjectDef(def, refs) {
|
|
1714
|
+
const result = {
|
|
1715
|
+
type: "object",
|
|
1716
|
+
properties: {}
|
|
1717
|
+
};
|
|
1718
|
+
const required = [];
|
|
1719
|
+
const shape = def.shape();
|
|
1720
|
+
for (const propName in shape) {
|
|
1721
|
+
let propDef = shape[propName];
|
|
1722
|
+
if (propDef === void 0 || propDef._def === void 0) {
|
|
1723
|
+
continue;
|
|
1724
|
+
}
|
|
1725
|
+
const propOptional = safeIsOptional(propDef);
|
|
1726
|
+
const parsedDef = parseDef(propDef._def, {
|
|
1727
|
+
...refs,
|
|
1728
|
+
currentPath: [...refs.currentPath, "properties", propName],
|
|
1729
|
+
propertyPath: [...refs.currentPath, "properties", propName]
|
|
1730
|
+
});
|
|
1731
|
+
if (parsedDef === void 0) {
|
|
1732
|
+
continue;
|
|
1733
|
+
}
|
|
1734
|
+
result.properties[propName] = parsedDef;
|
|
1735
|
+
if (!propOptional) {
|
|
1736
|
+
required.push(propName);
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
if (required.length) {
|
|
1740
|
+
result.required = required;
|
|
1741
|
+
}
|
|
1742
|
+
const additionalProperties = decideAdditionalProperties(def, refs);
|
|
1743
|
+
if (additionalProperties !== void 0) {
|
|
1744
|
+
result.additionalProperties = additionalProperties;
|
|
1745
|
+
}
|
|
1746
|
+
return result;
|
|
1747
|
+
}
|
|
1748
|
+
function decideAdditionalProperties(def, refs) {
|
|
1749
|
+
if (def.catchall._def.typeName !== "ZodNever") {
|
|
1750
|
+
return parseDef(def.catchall._def, {
|
|
1751
|
+
...refs,
|
|
1752
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1753
|
+
});
|
|
1754
|
+
}
|
|
1755
|
+
switch (def.unknownKeys) {
|
|
1756
|
+
case "passthrough":
|
|
1757
|
+
return refs.allowedAdditionalProperties;
|
|
1758
|
+
case "strict":
|
|
1759
|
+
return refs.rejectedAdditionalProperties;
|
|
1760
|
+
case "strip":
|
|
1761
|
+
return refs.removeAdditionalStrategy === "strict" ? refs.allowedAdditionalProperties : refs.rejectedAdditionalProperties;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
function safeIsOptional(schema) {
|
|
1765
|
+
try {
|
|
1766
|
+
return schema.isOptional();
|
|
1767
|
+
} catch (e) {
|
|
1768
|
+
return true;
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
var parseOptionalDef = (def, refs) => {
|
|
1772
|
+
var _a22;
|
|
1773
|
+
if (refs.currentPath.toString() === ((_a22 = refs.propertyPath) == null ? void 0 : _a22.toString())) {
|
|
1774
|
+
return parseDef(def.innerType._def, refs);
|
|
1775
|
+
}
|
|
1776
|
+
const innerSchema = parseDef(def.innerType._def, {
|
|
1777
|
+
...refs,
|
|
1778
|
+
currentPath: [...refs.currentPath, "anyOf", "1"]
|
|
1779
|
+
});
|
|
1780
|
+
return innerSchema ? { anyOf: [{ not: parseAnyDef() }, innerSchema] } : parseAnyDef();
|
|
1781
|
+
};
|
|
1782
|
+
var parsePipelineDef = (def, refs) => {
|
|
1783
|
+
if (refs.pipeStrategy === "input") {
|
|
1784
|
+
return parseDef(def.in._def, refs);
|
|
1785
|
+
} else if (refs.pipeStrategy === "output") {
|
|
1786
|
+
return parseDef(def.out._def, refs);
|
|
1787
|
+
}
|
|
1788
|
+
const a = parseDef(def.in._def, {
|
|
1789
|
+
...refs,
|
|
1790
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
1791
|
+
});
|
|
1792
|
+
const b = parseDef(def.out._def, {
|
|
1793
|
+
...refs,
|
|
1794
|
+
currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
|
|
1795
|
+
});
|
|
1796
|
+
return {
|
|
1797
|
+
allOf: [a, b].filter((x) => x !== void 0)
|
|
1798
|
+
};
|
|
1799
|
+
};
|
|
1800
|
+
function parsePromiseDef(def, refs) {
|
|
1801
|
+
return parseDef(def.type._def, refs);
|
|
1802
|
+
}
|
|
1803
|
+
function parseSetDef(def, refs) {
|
|
1804
|
+
const items = parseDef(def.valueType._def, {
|
|
1805
|
+
...refs,
|
|
1806
|
+
currentPath: [...refs.currentPath, "items"]
|
|
1807
|
+
});
|
|
1808
|
+
const schema = {
|
|
1809
|
+
type: "array",
|
|
1810
|
+
uniqueItems: true,
|
|
1811
|
+
items
|
|
1812
|
+
};
|
|
1813
|
+
if (def.minSize) {
|
|
1814
|
+
schema.minItems = def.minSize.value;
|
|
1815
|
+
}
|
|
1816
|
+
if (def.maxSize) {
|
|
1817
|
+
schema.maxItems = def.maxSize.value;
|
|
1818
|
+
}
|
|
1819
|
+
return schema;
|
|
1820
|
+
}
|
|
1821
|
+
function parseTupleDef(def, refs) {
|
|
1822
|
+
if (def.rest) {
|
|
1823
|
+
return {
|
|
1824
|
+
type: "array",
|
|
1825
|
+
minItems: def.items.length,
|
|
1826
|
+
items: def.items.map(
|
|
1827
|
+
(x, i) => parseDef(x._def, {
|
|
1828
|
+
...refs,
|
|
1829
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1830
|
+
})
|
|
1831
|
+
).reduce(
|
|
1832
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
|
1833
|
+
[]
|
|
1834
|
+
),
|
|
1835
|
+
additionalItems: parseDef(def.rest._def, {
|
|
1836
|
+
...refs,
|
|
1837
|
+
currentPath: [...refs.currentPath, "additionalItems"]
|
|
1838
|
+
})
|
|
1839
|
+
};
|
|
1840
|
+
} else {
|
|
1841
|
+
return {
|
|
1842
|
+
type: "array",
|
|
1843
|
+
minItems: def.items.length,
|
|
1844
|
+
maxItems: def.items.length,
|
|
1845
|
+
items: def.items.map(
|
|
1846
|
+
(x, i) => parseDef(x._def, {
|
|
1847
|
+
...refs,
|
|
1848
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1849
|
+
})
|
|
1850
|
+
).reduce(
|
|
1851
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
|
1852
|
+
[]
|
|
1853
|
+
)
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
function parseUndefinedDef() {
|
|
1858
|
+
return {
|
|
1859
|
+
not: parseAnyDef()
|
|
1860
|
+
};
|
|
1861
|
+
}
|
|
1862
|
+
function parseUnknownDef() {
|
|
1863
|
+
return parseAnyDef();
|
|
1864
|
+
}
|
|
1865
|
+
var parseReadonlyDef = (def, refs) => {
|
|
1866
|
+
return parseDef(def.innerType._def, refs);
|
|
1867
|
+
};
|
|
1868
|
+
var selectParser = (def, typeName, refs) => {
|
|
1869
|
+
switch (typeName) {
|
|
1870
|
+
case import_v3.ZodFirstPartyTypeKind.ZodString:
|
|
1871
|
+
return parseStringDef(def, refs);
|
|
1872
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNumber:
|
|
1873
|
+
return parseNumberDef(def);
|
|
1874
|
+
case import_v3.ZodFirstPartyTypeKind.ZodObject:
|
|
1875
|
+
return parseObjectDef(def, refs);
|
|
1876
|
+
case import_v3.ZodFirstPartyTypeKind.ZodBigInt:
|
|
1877
|
+
return parseBigintDef(def);
|
|
1878
|
+
case import_v3.ZodFirstPartyTypeKind.ZodBoolean:
|
|
1879
|
+
return parseBooleanDef();
|
|
1880
|
+
case import_v3.ZodFirstPartyTypeKind.ZodDate:
|
|
1881
|
+
return parseDateDef(def, refs);
|
|
1882
|
+
case import_v3.ZodFirstPartyTypeKind.ZodUndefined:
|
|
1883
|
+
return parseUndefinedDef();
|
|
1884
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNull:
|
|
1885
|
+
return parseNullDef();
|
|
1886
|
+
case import_v3.ZodFirstPartyTypeKind.ZodArray:
|
|
1887
|
+
return parseArrayDef(def, refs);
|
|
1888
|
+
case import_v3.ZodFirstPartyTypeKind.ZodUnion:
|
|
1889
|
+
case import_v3.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
1890
|
+
return parseUnionDef(def, refs);
|
|
1891
|
+
case import_v3.ZodFirstPartyTypeKind.ZodIntersection:
|
|
1892
|
+
return parseIntersectionDef(def, refs);
|
|
1893
|
+
case import_v3.ZodFirstPartyTypeKind.ZodTuple:
|
|
1894
|
+
return parseTupleDef(def, refs);
|
|
1895
|
+
case import_v3.ZodFirstPartyTypeKind.ZodRecord:
|
|
1896
|
+
return parseRecordDef(def, refs);
|
|
1897
|
+
case import_v3.ZodFirstPartyTypeKind.ZodLiteral:
|
|
1898
|
+
return parseLiteralDef(def);
|
|
1899
|
+
case import_v3.ZodFirstPartyTypeKind.ZodEnum:
|
|
1900
|
+
return parseEnumDef(def);
|
|
1901
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNativeEnum:
|
|
1902
|
+
return parseNativeEnumDef(def);
|
|
1903
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNullable:
|
|
1904
|
+
return parseNullableDef(def, refs);
|
|
1905
|
+
case import_v3.ZodFirstPartyTypeKind.ZodOptional:
|
|
1906
|
+
return parseOptionalDef(def, refs);
|
|
1907
|
+
case import_v3.ZodFirstPartyTypeKind.ZodMap:
|
|
1908
|
+
return parseMapDef(def, refs);
|
|
1909
|
+
case import_v3.ZodFirstPartyTypeKind.ZodSet:
|
|
1910
|
+
return parseSetDef(def, refs);
|
|
1911
|
+
case import_v3.ZodFirstPartyTypeKind.ZodLazy:
|
|
1912
|
+
return () => def.getter()._def;
|
|
1913
|
+
case import_v3.ZodFirstPartyTypeKind.ZodPromise:
|
|
1914
|
+
return parsePromiseDef(def, refs);
|
|
1915
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNaN:
|
|
1916
|
+
case import_v3.ZodFirstPartyTypeKind.ZodNever:
|
|
1917
|
+
return parseNeverDef();
|
|
1918
|
+
case import_v3.ZodFirstPartyTypeKind.ZodEffects:
|
|
1919
|
+
return parseEffectsDef(def, refs);
|
|
1920
|
+
case import_v3.ZodFirstPartyTypeKind.ZodAny:
|
|
1921
|
+
return parseAnyDef();
|
|
1922
|
+
case import_v3.ZodFirstPartyTypeKind.ZodUnknown:
|
|
1923
|
+
return parseUnknownDef();
|
|
1924
|
+
case import_v3.ZodFirstPartyTypeKind.ZodDefault:
|
|
1925
|
+
return parseDefaultDef(def, refs);
|
|
1926
|
+
case import_v3.ZodFirstPartyTypeKind.ZodBranded:
|
|
1927
|
+
return parseBrandedDef(def, refs);
|
|
1928
|
+
case import_v3.ZodFirstPartyTypeKind.ZodReadonly:
|
|
1929
|
+
return parseReadonlyDef(def, refs);
|
|
1930
|
+
case import_v3.ZodFirstPartyTypeKind.ZodCatch:
|
|
1931
|
+
return parseCatchDef(def, refs);
|
|
1932
|
+
case import_v3.ZodFirstPartyTypeKind.ZodPipeline:
|
|
1933
|
+
return parsePipelineDef(def, refs);
|
|
1934
|
+
case import_v3.ZodFirstPartyTypeKind.ZodFunction:
|
|
1935
|
+
case import_v3.ZodFirstPartyTypeKind.ZodVoid:
|
|
1936
|
+
case import_v3.ZodFirstPartyTypeKind.ZodSymbol:
|
|
1937
|
+
return void 0;
|
|
1938
|
+
default:
|
|
1939
|
+
return /* @__PURE__ */ ((_) => void 0)(typeName);
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
var getRelativePath = (pathA, pathB) => {
|
|
1943
|
+
let i = 0;
|
|
1944
|
+
for (; i < pathA.length && i < pathB.length; i++) {
|
|
1945
|
+
if (pathA[i] !== pathB[i]) break;
|
|
1946
|
+
}
|
|
1947
|
+
return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
|
|
1948
|
+
};
|
|
1949
|
+
function parseDef(def, refs, forceResolution = false) {
|
|
1950
|
+
var _a22;
|
|
1951
|
+
const seenItem = refs.seen.get(def);
|
|
1952
|
+
if (refs.override) {
|
|
1953
|
+
const overrideResult = (_a22 = refs.override) == null ? void 0 : _a22.call(
|
|
1954
|
+
refs,
|
|
1955
|
+
def,
|
|
1956
|
+
refs,
|
|
1957
|
+
seenItem,
|
|
1958
|
+
forceResolution
|
|
1959
|
+
);
|
|
1960
|
+
if (overrideResult !== ignoreOverride) {
|
|
1961
|
+
return overrideResult;
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
if (seenItem && !forceResolution) {
|
|
1965
|
+
const seenSchema = get$ref(seenItem, refs);
|
|
1966
|
+
if (seenSchema !== void 0) {
|
|
1967
|
+
return seenSchema;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
const newItem = { def, path: refs.currentPath, jsonSchema: void 0 };
|
|
1971
|
+
refs.seen.set(def, newItem);
|
|
1972
|
+
const jsonSchemaOrGetter = selectParser(def, def.typeName, refs);
|
|
1973
|
+
const jsonSchema2 = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
|
|
1974
|
+
if (jsonSchema2) {
|
|
1975
|
+
addMeta(def, refs, jsonSchema2);
|
|
1976
|
+
}
|
|
1977
|
+
if (refs.postProcess) {
|
|
1978
|
+
const postProcessResult = refs.postProcess(jsonSchema2, def, refs);
|
|
1979
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1980
|
+
return postProcessResult;
|
|
1981
|
+
}
|
|
1982
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1983
|
+
return jsonSchema2;
|
|
635
1984
|
}
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
1985
|
+
var get$ref = (item, refs) => {
|
|
1986
|
+
switch (refs.$refStrategy) {
|
|
1987
|
+
case "root":
|
|
1988
|
+
return { $ref: item.path.join("/") };
|
|
1989
|
+
case "relative":
|
|
1990
|
+
return { $ref: getRelativePath(refs.currentPath, item.path) };
|
|
1991
|
+
case "none":
|
|
1992
|
+
case "seen": {
|
|
1993
|
+
if (item.path.length < refs.currentPath.length && item.path.every((value, index) => refs.currentPath[index] === value)) {
|
|
1994
|
+
console.warn(
|
|
1995
|
+
`Recursive reference detected at ${refs.currentPath.join(
|
|
1996
|
+
"/"
|
|
1997
|
+
)}! Defaulting to any`
|
|
1998
|
+
);
|
|
1999
|
+
return parseAnyDef();
|
|
2000
|
+
}
|
|
2001
|
+
return refs.$refStrategy === "seen" ? parseAnyDef() : void 0;
|
|
2002
|
+
}
|
|
642
2003
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
2004
|
+
};
|
|
2005
|
+
var addMeta = (def, refs, jsonSchema2) => {
|
|
2006
|
+
if (def.description) {
|
|
2007
|
+
jsonSchema2.description = def.description;
|
|
2008
|
+
}
|
|
2009
|
+
return jsonSchema2;
|
|
2010
|
+
};
|
|
2011
|
+
var getRefs = (options) => {
|
|
2012
|
+
const _options = getDefaultOptions(options);
|
|
2013
|
+
const currentPath = _options.name !== void 0 ? [..._options.basePath, _options.definitionPath, _options.name] : _options.basePath;
|
|
2014
|
+
return {
|
|
2015
|
+
..._options,
|
|
2016
|
+
currentPath,
|
|
2017
|
+
propertyPath: void 0,
|
|
2018
|
+
seen: new Map(
|
|
2019
|
+
Object.entries(_options.definitions).map(([name22, def]) => [
|
|
2020
|
+
def._def,
|
|
2021
|
+
{
|
|
2022
|
+
def: def._def,
|
|
2023
|
+
path: [..._options.basePath, _options.definitionPath, name22],
|
|
2024
|
+
// Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
|
|
2025
|
+
jsonSchema: void 0
|
|
2026
|
+
}
|
|
2027
|
+
])
|
|
2028
|
+
)
|
|
2029
|
+
};
|
|
2030
|
+
};
|
|
2031
|
+
var zod3ToJsonSchema = (schema, options) => {
|
|
2032
|
+
var _a22;
|
|
2033
|
+
const refs = getRefs(options);
|
|
2034
|
+
let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce(
|
|
2035
|
+
(acc, [name32, schema2]) => {
|
|
2036
|
+
var _a32;
|
|
2037
|
+
return {
|
|
2038
|
+
...acc,
|
|
2039
|
+
[name32]: (_a32 = parseDef(
|
|
2040
|
+
schema2._def,
|
|
2041
|
+
{
|
|
2042
|
+
...refs,
|
|
2043
|
+
currentPath: [...refs.basePath, refs.definitionPath, name32]
|
|
2044
|
+
},
|
|
2045
|
+
true
|
|
2046
|
+
)) != null ? _a32 : parseAnyDef()
|
|
2047
|
+
};
|
|
2048
|
+
},
|
|
2049
|
+
{}
|
|
2050
|
+
) : void 0;
|
|
2051
|
+
const name22 = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
|
|
2052
|
+
const main = (_a22 = parseDef(
|
|
2053
|
+
schema._def,
|
|
2054
|
+
name22 === void 0 ? refs : {
|
|
2055
|
+
...refs,
|
|
2056
|
+
currentPath: [...refs.basePath, refs.definitionPath, name22]
|
|
2057
|
+
},
|
|
2058
|
+
false
|
|
2059
|
+
)) != null ? _a22 : parseAnyDef();
|
|
2060
|
+
const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
|
|
2061
|
+
if (title !== void 0) {
|
|
2062
|
+
main.title = title;
|
|
647
2063
|
}
|
|
2064
|
+
const combined = name22 === void 0 ? definitions ? {
|
|
2065
|
+
...main,
|
|
2066
|
+
[refs.definitionPath]: definitions
|
|
2067
|
+
} : main : {
|
|
2068
|
+
$ref: [
|
|
2069
|
+
...refs.$refStrategy === "relative" ? [] : refs.basePath,
|
|
2070
|
+
refs.definitionPath,
|
|
2071
|
+
name22
|
|
2072
|
+
].join("/"),
|
|
2073
|
+
[refs.definitionPath]: {
|
|
2074
|
+
...definitions,
|
|
2075
|
+
[name22]: main
|
|
2076
|
+
}
|
|
2077
|
+
};
|
|
2078
|
+
combined.$schema = "http://json-schema.org/draft-07/schema#";
|
|
2079
|
+
return combined;
|
|
2080
|
+
};
|
|
2081
|
+
var schemaSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.schema");
|
|
2082
|
+
function jsonSchema(jsonSchema2, {
|
|
2083
|
+
validate
|
|
2084
|
+
} = {}) {
|
|
2085
|
+
return {
|
|
2086
|
+
[schemaSymbol]: true,
|
|
2087
|
+
_type: void 0,
|
|
2088
|
+
// should never be used directly
|
|
2089
|
+
get jsonSchema() {
|
|
2090
|
+
if (typeof jsonSchema2 === "function") {
|
|
2091
|
+
jsonSchema2 = jsonSchema2();
|
|
2092
|
+
}
|
|
2093
|
+
return jsonSchema2;
|
|
2094
|
+
},
|
|
2095
|
+
validate
|
|
2096
|
+
};
|
|
648
2097
|
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
return { [validatorSymbol]: true, validate };
|
|
2098
|
+
function isSchema(value) {
|
|
2099
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
|
652
2100
|
}
|
|
653
|
-
function
|
|
654
|
-
return
|
|
2101
|
+
function asSchema(schema) {
|
|
2102
|
+
return schema == null ? jsonSchema({ properties: {}, additionalProperties: false }) : isSchema(schema) ? schema : "~standard" in schema ? schema["~standard"].vendor === "zod" ? zodSchema(schema) : standardSchema(schema) : schema();
|
|
655
2103
|
}
|
|
656
|
-
function
|
|
657
|
-
return
|
|
2104
|
+
function standardSchema(standardSchema2) {
|
|
2105
|
+
return jsonSchema(
|
|
2106
|
+
() => addAdditionalPropertiesToJsonSchema(
|
|
2107
|
+
standardSchema2["~standard"].jsonSchema.input({
|
|
2108
|
+
target: "draft-07"
|
|
2109
|
+
})
|
|
2110
|
+
),
|
|
2111
|
+
{
|
|
2112
|
+
validate: async (value) => {
|
|
2113
|
+
const result = await standardSchema2["~standard"].validate(value);
|
|
2114
|
+
return "value" in result ? { success: true, value: result.value } : {
|
|
2115
|
+
success: false,
|
|
2116
|
+
error: new TypeValidationError({
|
|
2117
|
+
value,
|
|
2118
|
+
cause: result.issues
|
|
2119
|
+
})
|
|
2120
|
+
};
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
);
|
|
658
2124
|
}
|
|
659
|
-
function
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
2125
|
+
function zod3Schema(zodSchema2, options) {
|
|
2126
|
+
var _a22;
|
|
2127
|
+
const useReferences = (_a22 = options == null ? void 0 : options.useReferences) != null ? _a22 : false;
|
|
2128
|
+
return jsonSchema(
|
|
2129
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
2130
|
+
() => zod3ToJsonSchema(zodSchema2, {
|
|
2131
|
+
$refStrategy: useReferences ? "root" : "none"
|
|
2132
|
+
}),
|
|
2133
|
+
{
|
|
2134
|
+
validate: async (value) => {
|
|
2135
|
+
const result = await zodSchema2.safeParseAsync(value);
|
|
2136
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
);
|
|
2140
|
+
}
|
|
2141
|
+
function zod4Schema(zodSchema2, options) {
|
|
2142
|
+
var _a22;
|
|
2143
|
+
const useReferences = (_a22 = options == null ? void 0 : options.useReferences) != null ? _a22 : false;
|
|
2144
|
+
return jsonSchema(
|
|
2145
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
2146
|
+
() => addAdditionalPropertiesToJsonSchema(
|
|
2147
|
+
z4.toJSONSchema(zodSchema2, {
|
|
2148
|
+
target: "draft-7",
|
|
2149
|
+
io: "input",
|
|
2150
|
+
reused: useReferences ? "ref" : "inline"
|
|
667
2151
|
})
|
|
668
|
-
|
|
669
|
-
|
|
2152
|
+
),
|
|
2153
|
+
{
|
|
2154
|
+
validate: async (value) => {
|
|
2155
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
|
2156
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
);
|
|
2160
|
+
}
|
|
2161
|
+
function isZod4Schema(zodSchema2) {
|
|
2162
|
+
return "_zod" in zodSchema2;
|
|
2163
|
+
}
|
|
2164
|
+
function zodSchema(zodSchema2, options) {
|
|
2165
|
+
if (isZod4Schema(zodSchema2)) {
|
|
2166
|
+
return zod4Schema(zodSchema2, options);
|
|
2167
|
+
} else {
|
|
2168
|
+
return zod3Schema(zodSchema2, options);
|
|
2169
|
+
}
|
|
670
2170
|
}
|
|
671
2171
|
async function validateTypes({
|
|
672
2172
|
value,
|
|
673
|
-
schema
|
|
2173
|
+
schema,
|
|
2174
|
+
context
|
|
674
2175
|
}) {
|
|
675
|
-
const result = await safeValidateTypes({ value, schema });
|
|
2176
|
+
const result = await safeValidateTypes({ value, schema, context });
|
|
676
2177
|
if (!result.success) {
|
|
677
|
-
throw TypeValidationError.wrap({ value, cause: result.error });
|
|
2178
|
+
throw TypeValidationError.wrap({ value, cause: result.error, context });
|
|
678
2179
|
}
|
|
679
2180
|
return result.value;
|
|
680
2181
|
}
|
|
681
2182
|
async function safeValidateTypes({
|
|
682
2183
|
value,
|
|
683
|
-
schema
|
|
2184
|
+
schema,
|
|
2185
|
+
context
|
|
684
2186
|
}) {
|
|
685
|
-
const
|
|
2187
|
+
const actualSchema = asSchema(schema);
|
|
686
2188
|
try {
|
|
687
|
-
if (
|
|
2189
|
+
if (actualSchema.validate == null) {
|
|
688
2190
|
return { success: true, value, rawValue: value };
|
|
689
2191
|
}
|
|
690
|
-
const result = await
|
|
2192
|
+
const result = await actualSchema.validate(value);
|
|
691
2193
|
if (result.success) {
|
|
692
2194
|
return { success: true, value: result.value, rawValue: value };
|
|
693
2195
|
}
|
|
694
2196
|
return {
|
|
695
2197
|
success: false,
|
|
696
|
-
error: TypeValidationError.wrap({ value, cause: result.error }),
|
|
2198
|
+
error: TypeValidationError.wrap({ value, cause: result.error, context }),
|
|
697
2199
|
rawValue: value
|
|
698
2200
|
};
|
|
699
2201
|
} catch (error) {
|
|
700
2202
|
return {
|
|
701
2203
|
success: false,
|
|
702
|
-
error: TypeValidationError.wrap({ value, cause: error }),
|
|
2204
|
+
error: TypeValidationError.wrap({ value, cause: error, context }),
|
|
703
2205
|
rawValue: value
|
|
704
2206
|
};
|
|
705
2207
|
}
|
|
@@ -791,7 +2293,7 @@ var postJsonToApi = async ({
|
|
|
791
2293
|
failedResponseHandler,
|
|
792
2294
|
successfulResponseHandler,
|
|
793
2295
|
abortSignal,
|
|
794
|
-
fetch
|
|
2296
|
+
fetch: fetch2
|
|
795
2297
|
}) => postToApi({
|
|
796
2298
|
url,
|
|
797
2299
|
headers: {
|
|
@@ -805,7 +2307,27 @@ var postJsonToApi = async ({
|
|
|
805
2307
|
failedResponseHandler,
|
|
806
2308
|
successfulResponseHandler,
|
|
807
2309
|
abortSignal,
|
|
808
|
-
fetch
|
|
2310
|
+
fetch: fetch2
|
|
2311
|
+
});
|
|
2312
|
+
var postFormDataToApi = async ({
|
|
2313
|
+
url,
|
|
2314
|
+
headers,
|
|
2315
|
+
formData,
|
|
2316
|
+
failedResponseHandler,
|
|
2317
|
+
successfulResponseHandler,
|
|
2318
|
+
abortSignal,
|
|
2319
|
+
fetch: fetch2
|
|
2320
|
+
}) => postToApi({
|
|
2321
|
+
url,
|
|
2322
|
+
headers,
|
|
2323
|
+
body: {
|
|
2324
|
+
content: formData,
|
|
2325
|
+
values: Object.fromEntries(formData.entries())
|
|
2326
|
+
},
|
|
2327
|
+
failedResponseHandler,
|
|
2328
|
+
successfulResponseHandler,
|
|
2329
|
+
abortSignal,
|
|
2330
|
+
fetch: fetch2
|
|
809
2331
|
});
|
|
810
2332
|
var postToApi = async ({
|
|
811
2333
|
url,
|
|
@@ -814,10 +2336,10 @@ var postToApi = async ({
|
|
|
814
2336
|
successfulResponseHandler,
|
|
815
2337
|
failedResponseHandler,
|
|
816
2338
|
abortSignal,
|
|
817
|
-
fetch = getOriginalFetch2()
|
|
2339
|
+
fetch: fetch2 = getOriginalFetch2()
|
|
818
2340
|
}) => {
|
|
819
2341
|
try {
|
|
820
|
-
const response = await
|
|
2342
|
+
const response = await fetch2(url, {
|
|
821
2343
|
method: "POST",
|
|
822
2344
|
headers: withUserAgentSuffix(
|
|
823
2345
|
headers,
|
|
@@ -967,25 +2489,11 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
|
967
2489
|
rawValue: parsedResult.rawValue
|
|
968
2490
|
};
|
|
969
2491
|
};
|
|
970
|
-
var ALPHA_NUMERIC = new Set(
|
|
971
|
-
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
972
|
-
);
|
|
973
|
-
var { btoa, atob } = globalThis;
|
|
974
|
-
function convertUint8ArrayToBase64(array) {
|
|
975
|
-
let latin1string = "";
|
|
976
|
-
for (let i = 0; i < array.length; i++) {
|
|
977
|
-
latin1string += String.fromCodePoint(array[i]);
|
|
978
|
-
}
|
|
979
|
-
return btoa(latin1string);
|
|
980
|
-
}
|
|
981
|
-
function convertToBase64(value) {
|
|
982
|
-
return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
|
|
983
|
-
}
|
|
984
2492
|
function withoutTrailingSlash(url) {
|
|
985
2493
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
986
2494
|
}
|
|
987
2495
|
|
|
988
|
-
// node_modules/.pnpm/@ai-sdk+openai-compatible@
|
|
2496
|
+
// node_modules/.pnpm/@ai-sdk+openai-compatible@2.0.41_zod@3.25.76/node_modules/@ai-sdk/openai-compatible/dist/index.mjs
|
|
989
2497
|
var import_v4 = require("zod/v4");
|
|
990
2498
|
var import_v42 = require("zod/v4");
|
|
991
2499
|
var import_v43 = require("zod/v4");
|
|
@@ -994,11 +2502,85 @@ var import_v45 = require("zod/v4");
|
|
|
994
2502
|
var import_v46 = require("zod/v4");
|
|
995
2503
|
var import_v47 = require("zod/v4");
|
|
996
2504
|
var import_v48 = require("zod/v4");
|
|
2505
|
+
function toCamelCase(str) {
|
|
2506
|
+
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
2507
|
+
}
|
|
2508
|
+
function resolveProviderOptionsKey(rawName, providerOptions) {
|
|
2509
|
+
const camelName = toCamelCase(rawName);
|
|
2510
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[camelName]) != null) {
|
|
2511
|
+
return camelName;
|
|
2512
|
+
}
|
|
2513
|
+
return rawName;
|
|
2514
|
+
}
|
|
2515
|
+
var openaiCompatibleErrorDataSchema = import_v42.z.object({
|
|
2516
|
+
error: import_v42.z.object({
|
|
2517
|
+
message: import_v42.z.string(),
|
|
2518
|
+
// The additional information below is handled loosely to support
|
|
2519
|
+
// OpenAI-compatible providers that have slightly different error
|
|
2520
|
+
// responses:
|
|
2521
|
+
type: import_v42.z.string().nullish(),
|
|
2522
|
+
param: import_v42.z.any().nullish(),
|
|
2523
|
+
code: import_v42.z.union([import_v42.z.string(), import_v42.z.number()]).nullish()
|
|
2524
|
+
})
|
|
2525
|
+
});
|
|
2526
|
+
var defaultOpenAICompatibleErrorStructure = {
|
|
2527
|
+
errorSchema: openaiCompatibleErrorDataSchema,
|
|
2528
|
+
errorToMessage: (data) => data.error.message
|
|
2529
|
+
};
|
|
2530
|
+
function convertOpenAICompatibleChatUsage(usage) {
|
|
2531
|
+
var _a16, _b16, _c, _d, _e, _f;
|
|
2532
|
+
if (usage == null) {
|
|
2533
|
+
return {
|
|
2534
|
+
inputTokens: {
|
|
2535
|
+
total: void 0,
|
|
2536
|
+
noCache: void 0,
|
|
2537
|
+
cacheRead: void 0,
|
|
2538
|
+
cacheWrite: void 0
|
|
2539
|
+
},
|
|
2540
|
+
outputTokens: {
|
|
2541
|
+
total: void 0,
|
|
2542
|
+
text: void 0,
|
|
2543
|
+
reasoning: void 0
|
|
2544
|
+
},
|
|
2545
|
+
raw: void 0
|
|
2546
|
+
};
|
|
2547
|
+
}
|
|
2548
|
+
const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
|
|
2549
|
+
const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
|
|
2550
|
+
const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
|
|
2551
|
+
const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
|
|
2552
|
+
return {
|
|
2553
|
+
inputTokens: {
|
|
2554
|
+
total: promptTokens,
|
|
2555
|
+
noCache: promptTokens - cacheReadTokens,
|
|
2556
|
+
cacheRead: cacheReadTokens,
|
|
2557
|
+
cacheWrite: void 0
|
|
2558
|
+
},
|
|
2559
|
+
outputTokens: {
|
|
2560
|
+
total: completionTokens,
|
|
2561
|
+
text: completionTokens - reasoningTokens,
|
|
2562
|
+
reasoning: reasoningTokens
|
|
2563
|
+
},
|
|
2564
|
+
raw: usage
|
|
2565
|
+
};
|
|
2566
|
+
}
|
|
997
2567
|
function getOpenAIMetadata(message) {
|
|
998
|
-
var
|
|
999
|
-
return (
|
|
2568
|
+
var _a16, _b16;
|
|
2569
|
+
return (_b16 = (_a16 = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a16.openaiCompatible) != null ? _b16 : {};
|
|
2570
|
+
}
|
|
2571
|
+
function getAudioFormat(mediaType) {
|
|
2572
|
+
switch (mediaType) {
|
|
2573
|
+
case "audio/wav":
|
|
2574
|
+
return "wav";
|
|
2575
|
+
case "audio/mp3":
|
|
2576
|
+
case "audio/mpeg":
|
|
2577
|
+
return "mp3";
|
|
2578
|
+
default:
|
|
2579
|
+
return null;
|
|
2580
|
+
}
|
|
1000
2581
|
}
|
|
1001
2582
|
function convertToOpenAICompatibleChatMessages(prompt) {
|
|
2583
|
+
var _a16, _b16, _c;
|
|
1002
2584
|
const messages = [];
|
|
1003
2585
|
for (const { role, content, ...message } of prompt) {
|
|
1004
2586
|
const metadata = getOpenAIMetadata({ ...message });
|
|
@@ -1019,6 +2601,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1019
2601
|
messages.push({
|
|
1020
2602
|
role: "user",
|
|
1021
2603
|
content: content.map((part) => {
|
|
2604
|
+
var _a22;
|
|
1022
2605
|
const partMetadata = getOpenAIMetadata(part);
|
|
1023
2606
|
switch (part.type) {
|
|
1024
2607
|
case "text": {
|
|
@@ -1034,11 +2617,56 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1034
2617
|
},
|
|
1035
2618
|
...partMetadata
|
|
1036
2619
|
};
|
|
1037
|
-
} else {
|
|
1038
|
-
throw new UnsupportedFunctionalityError({
|
|
1039
|
-
functionality: `file part media type ${part.mediaType}`
|
|
1040
|
-
});
|
|
1041
2620
|
}
|
|
2621
|
+
if (part.mediaType.startsWith("audio/")) {
|
|
2622
|
+
if (part.data instanceof URL) {
|
|
2623
|
+
throw new UnsupportedFunctionalityError({
|
|
2624
|
+
functionality: "audio file parts with URLs"
|
|
2625
|
+
});
|
|
2626
|
+
}
|
|
2627
|
+
const format = getAudioFormat(part.mediaType);
|
|
2628
|
+
if (format === null) {
|
|
2629
|
+
throw new UnsupportedFunctionalityError({
|
|
2630
|
+
functionality: `audio media type ${part.mediaType}`
|
|
2631
|
+
});
|
|
2632
|
+
}
|
|
2633
|
+
return {
|
|
2634
|
+
type: "input_audio",
|
|
2635
|
+
input_audio: {
|
|
2636
|
+
data: convertToBase64(part.data),
|
|
2637
|
+
format
|
|
2638
|
+
},
|
|
2639
|
+
...partMetadata
|
|
2640
|
+
};
|
|
2641
|
+
}
|
|
2642
|
+
if (part.mediaType === "application/pdf") {
|
|
2643
|
+
if (part.data instanceof URL) {
|
|
2644
|
+
throw new UnsupportedFunctionalityError({
|
|
2645
|
+
functionality: "PDF file parts with URLs"
|
|
2646
|
+
});
|
|
2647
|
+
}
|
|
2648
|
+
return {
|
|
2649
|
+
type: "file",
|
|
2650
|
+
file: {
|
|
2651
|
+
filename: (_a22 = part.filename) != null ? _a22 : "document.pdf",
|
|
2652
|
+
file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`
|
|
2653
|
+
},
|
|
2654
|
+
...partMetadata
|
|
2655
|
+
};
|
|
2656
|
+
}
|
|
2657
|
+
if (part.mediaType.startsWith("text/")) {
|
|
2658
|
+
const textContent = part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(
|
|
2659
|
+
convertBase64ToUint8Array(part.data)
|
|
2660
|
+
) : new TextDecoder().decode(part.data);
|
|
2661
|
+
return {
|
|
2662
|
+
type: "text",
|
|
2663
|
+
text: textContent,
|
|
2664
|
+
...partMetadata
|
|
2665
|
+
};
|
|
2666
|
+
}
|
|
2667
|
+
throw new UnsupportedFunctionalityError({
|
|
2668
|
+
functionality: `file part media type ${part.mediaType}`
|
|
2669
|
+
});
|
|
1042
2670
|
}
|
|
1043
2671
|
}
|
|
1044
2672
|
}),
|
|
@@ -1048,6 +2676,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1048
2676
|
}
|
|
1049
2677
|
case "assistant": {
|
|
1050
2678
|
let text = "";
|
|
2679
|
+
let reasoning = "";
|
|
1051
2680
|
const toolCalls = [];
|
|
1052
2681
|
for (const part of content) {
|
|
1053
2682
|
const partMetadata = getOpenAIMetadata(part);
|
|
@@ -1056,7 +2685,12 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1056
2685
|
text += part.text;
|
|
1057
2686
|
break;
|
|
1058
2687
|
}
|
|
2688
|
+
case "reasoning": {
|
|
2689
|
+
reasoning += part.text;
|
|
2690
|
+
break;
|
|
2691
|
+
}
|
|
1059
2692
|
case "tool-call": {
|
|
2693
|
+
const thoughtSignature = (_b16 = (_a16 = part.providerOptions) == null ? void 0 : _a16.google) == null ? void 0 : _b16.thoughtSignature;
|
|
1060
2694
|
toolCalls.push({
|
|
1061
2695
|
id: part.toolCallId,
|
|
1062
2696
|
type: "function",
|
|
@@ -1064,7 +2698,15 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1064
2698
|
name: part.toolName,
|
|
1065
2699
|
arguments: JSON.stringify(part.input)
|
|
1066
2700
|
},
|
|
1067
|
-
...partMetadata
|
|
2701
|
+
...partMetadata,
|
|
2702
|
+
// Include extra_content for Google Gemini thought signatures
|
|
2703
|
+
...thoughtSignature ? {
|
|
2704
|
+
extra_content: {
|
|
2705
|
+
google: {
|
|
2706
|
+
thought_signature: String(thoughtSignature)
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
} : {}
|
|
1068
2710
|
});
|
|
1069
2711
|
break;
|
|
1070
2712
|
}
|
|
@@ -1073,6 +2715,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1073
2715
|
messages.push({
|
|
1074
2716
|
role: "assistant",
|
|
1075
2717
|
content: text,
|
|
2718
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
1076
2719
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1077
2720
|
...metadata
|
|
1078
2721
|
});
|
|
@@ -1080,6 +2723,9 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1080
2723
|
}
|
|
1081
2724
|
case "tool": {
|
|
1082
2725
|
for (const toolResponse of content) {
|
|
2726
|
+
if (toolResponse.type === "tool-approval-response") {
|
|
2727
|
+
continue;
|
|
2728
|
+
}
|
|
1083
2729
|
const output = toolResponse.output;
|
|
1084
2730
|
let contentValue;
|
|
1085
2731
|
switch (output.type) {
|
|
@@ -1087,6 +2733,9 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1087
2733
|
case "error-text":
|
|
1088
2734
|
contentValue = output.value;
|
|
1089
2735
|
break;
|
|
2736
|
+
case "execution-denied":
|
|
2737
|
+
contentValue = (_c = output.reason) != null ? _c : "Tool execution denied.";
|
|
2738
|
+
break;
|
|
1090
2739
|
case "content":
|
|
1091
2740
|
case "json":
|
|
1092
2741
|
case "error-json":
|
|
@@ -1134,39 +2783,32 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
|
1134
2783
|
case "tool_calls":
|
|
1135
2784
|
return "tool-calls";
|
|
1136
2785
|
default:
|
|
1137
|
-
return "
|
|
2786
|
+
return "other";
|
|
1138
2787
|
}
|
|
1139
2788
|
}
|
|
1140
|
-
var
|
|
2789
|
+
var openaiCompatibleLanguageModelChatOptions = import_v43.z.object({
|
|
1141
2790
|
/**
|
|
1142
2791
|
* A unique identifier representing your end-user, which can help the provider to
|
|
1143
2792
|
* monitor and detect abuse.
|
|
1144
2793
|
*/
|
|
1145
|
-
user:
|
|
2794
|
+
user: import_v43.z.string().optional(),
|
|
1146
2795
|
/**
|
|
1147
2796
|
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
1148
2797
|
*/
|
|
1149
|
-
reasoningEffort:
|
|
2798
|
+
reasoningEffort: import_v43.z.string().optional(),
|
|
1150
2799
|
/**
|
|
1151
2800
|
* Controls the verbosity of the generated text. Defaults to `medium`.
|
|
1152
2801
|
*/
|
|
1153
|
-
textVerbosity:
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
param: import_v43.z.any().nullish(),
|
|
1163
|
-
code: import_v43.z.union([import_v43.z.string(), import_v43.z.number()]).nullish()
|
|
1164
|
-
})
|
|
2802
|
+
textVerbosity: import_v43.z.string().optional(),
|
|
2803
|
+
/**
|
|
2804
|
+
* Whether to use strict JSON schema validation.
|
|
2805
|
+
* When true, the model uses constrained decoding to guarantee schema compliance.
|
|
2806
|
+
* Only used when the provider supports structured outputs and a schema is provided.
|
|
2807
|
+
*
|
|
2808
|
+
* @default true
|
|
2809
|
+
*/
|
|
2810
|
+
strictJsonSchema: import_v43.z.boolean().optional()
|
|
1165
2811
|
});
|
|
1166
|
-
var defaultOpenAICompatibleErrorStructure = {
|
|
1167
|
-
errorSchema: openaiCompatibleErrorDataSchema,
|
|
1168
|
-
errorToMessage: (data) => data.error.message
|
|
1169
|
-
};
|
|
1170
2812
|
function prepareTools({
|
|
1171
2813
|
tools,
|
|
1172
2814
|
toolChoice
|
|
@@ -1178,15 +2820,19 @@ function prepareTools({
|
|
|
1178
2820
|
}
|
|
1179
2821
|
const openaiCompatTools = [];
|
|
1180
2822
|
for (const tool of tools) {
|
|
1181
|
-
if (tool.type === "provider
|
|
1182
|
-
toolWarnings.push({
|
|
2823
|
+
if (tool.type === "provider") {
|
|
2824
|
+
toolWarnings.push({
|
|
2825
|
+
type: "unsupported",
|
|
2826
|
+
feature: `provider-defined tool ${tool.id}`
|
|
2827
|
+
});
|
|
1183
2828
|
} else {
|
|
1184
2829
|
openaiCompatTools.push({
|
|
1185
2830
|
type: "function",
|
|
1186
2831
|
function: {
|
|
1187
2832
|
name: tool.name,
|
|
1188
2833
|
description: tool.description,
|
|
1189
|
-
parameters: tool.inputSchema
|
|
2834
|
+
parameters: tool.inputSchema,
|
|
2835
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
1190
2836
|
}
|
|
1191
2837
|
});
|
|
1192
2838
|
}
|
|
@@ -1220,16 +2866,16 @@ function prepareTools({
|
|
|
1220
2866
|
var OpenAICompatibleChatLanguageModel = class {
|
|
1221
2867
|
// type inferred via constructor
|
|
1222
2868
|
constructor(modelId, config) {
|
|
1223
|
-
this.specificationVersion = "
|
|
1224
|
-
var
|
|
2869
|
+
this.specificationVersion = "v3";
|
|
2870
|
+
var _a16, _b16;
|
|
1225
2871
|
this.modelId = modelId;
|
|
1226
2872
|
this.config = config;
|
|
1227
|
-
const errorStructure = (
|
|
2873
|
+
const errorStructure = (_a16 = config.errorStructure) != null ? _a16 : defaultOpenAICompatibleErrorStructure;
|
|
1228
2874
|
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
|
1229
2875
|
errorStructure.errorSchema
|
|
1230
2876
|
);
|
|
1231
2877
|
this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
|
|
1232
|
-
this.supportsStructuredOutputs = (
|
|
2878
|
+
this.supportsStructuredOutputs = (_b16 = config.supportsStructuredOutputs) != null ? _b16 : false;
|
|
1233
2879
|
}
|
|
1234
2880
|
get provider() {
|
|
1235
2881
|
return this.config.provider;
|
|
@@ -1238,8 +2884,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1238
2884
|
return this.config.provider.split(".")[0].trim();
|
|
1239
2885
|
}
|
|
1240
2886
|
get supportedUrls() {
|
|
1241
|
-
var
|
|
1242
|
-
return (_c = (
|
|
2887
|
+
var _a16, _b16, _c;
|
|
2888
|
+
return (_c = (_b16 = (_a16 = this.config).supportedUrls) == null ? void 0 : _b16.call(_a16)) != null ? _c : {};
|
|
2889
|
+
}
|
|
2890
|
+
transformRequestBody(args) {
|
|
2891
|
+
var _a16, _b16, _c;
|
|
2892
|
+
return (_c = (_b16 = (_a16 = this.config).transformRequestBody) == null ? void 0 : _b16.call(_a16, args)) != null ? _c : args;
|
|
1243
2893
|
}
|
|
1244
2894
|
async getArgs({
|
|
1245
2895
|
prompt,
|
|
@@ -1256,27 +2906,45 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1256
2906
|
toolChoice,
|
|
1257
2907
|
tools
|
|
1258
2908
|
}) {
|
|
1259
|
-
var
|
|
2909
|
+
var _a16, _b16, _c, _d, _e;
|
|
1260
2910
|
const warnings = [];
|
|
2911
|
+
const deprecatedOptions = await parseProviderOptions({
|
|
2912
|
+
provider: "openai-compatible",
|
|
2913
|
+
providerOptions,
|
|
2914
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2915
|
+
});
|
|
2916
|
+
if (deprecatedOptions != null) {
|
|
2917
|
+
warnings.push({
|
|
2918
|
+
type: "other",
|
|
2919
|
+
message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
|
|
2920
|
+
});
|
|
2921
|
+
}
|
|
1261
2922
|
const compatibleOptions = Object.assign(
|
|
1262
|
-
|
|
1263
|
-
|
|
2923
|
+
deprecatedOptions != null ? deprecatedOptions : {},
|
|
2924
|
+
(_a16 = await parseProviderOptions({
|
|
2925
|
+
provider: "openaiCompatible",
|
|
1264
2926
|
providerOptions,
|
|
1265
|
-
schema:
|
|
1266
|
-
})) != null ?
|
|
1267
|
-
(
|
|
2927
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2928
|
+
})) != null ? _a16 : {},
|
|
2929
|
+
(_b16 = await parseProviderOptions({
|
|
1268
2930
|
provider: this.providerOptionsName,
|
|
1269
2931
|
providerOptions,
|
|
1270
|
-
schema:
|
|
1271
|
-
})) != null ?
|
|
2932
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2933
|
+
})) != null ? _b16 : {},
|
|
2934
|
+
(_c = await parseProviderOptions({
|
|
2935
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
2936
|
+
providerOptions,
|
|
2937
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2938
|
+
})) != null ? _c : {}
|
|
1272
2939
|
);
|
|
2940
|
+
const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
|
|
1273
2941
|
if (topK != null) {
|
|
1274
|
-
warnings.push({ type: "unsupported
|
|
2942
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1275
2943
|
}
|
|
1276
2944
|
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
|
|
1277
2945
|
warnings.push({
|
|
1278
|
-
type: "unsupported
|
|
1279
|
-
|
|
2946
|
+
type: "unsupported",
|
|
2947
|
+
feature: "responseFormat",
|
|
1280
2948
|
details: "JSON response format schema is only supported with structuredOutputs"
|
|
1281
2949
|
});
|
|
1282
2950
|
}
|
|
@@ -1288,7 +2956,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1288
2956
|
tools,
|
|
1289
2957
|
toolChoice
|
|
1290
2958
|
});
|
|
2959
|
+
const metadataKey = resolveProviderOptionsKey(
|
|
2960
|
+
this.providerOptionsName,
|
|
2961
|
+
providerOptions
|
|
2962
|
+
);
|
|
1291
2963
|
return {
|
|
2964
|
+
metadataKey,
|
|
1292
2965
|
args: {
|
|
1293
2966
|
// model id:
|
|
1294
2967
|
model: this.modelId,
|
|
@@ -1304,17 +2977,21 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1304
2977
|
type: "json_schema",
|
|
1305
2978
|
json_schema: {
|
|
1306
2979
|
schema: responseFormat.schema,
|
|
1307
|
-
|
|
2980
|
+
strict: strictJsonSchema,
|
|
2981
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
1308
2982
|
description: responseFormat.description
|
|
1309
2983
|
}
|
|
1310
2984
|
} : { type: "json_object" } : void 0,
|
|
1311
2985
|
stop: stopSequences,
|
|
1312
2986
|
seed,
|
|
1313
2987
|
...Object.fromEntries(
|
|
1314
|
-
Object.entries(
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
2988
|
+
Object.entries({
|
|
2989
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
2990
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
|
|
2991
|
+
}).filter(
|
|
2992
|
+
([key]) => !Object.keys(
|
|
2993
|
+
openaiCompatibleLanguageModelChatOptions.shape
|
|
2994
|
+
).includes(key)
|
|
1318
2995
|
)
|
|
1319
2996
|
),
|
|
1320
2997
|
reasoning_effort: compatibleOptions.reasoningEffort,
|
|
@@ -1329,9 +3006,10 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1329
3006
|
};
|
|
1330
3007
|
}
|
|
1331
3008
|
async doGenerate(options) {
|
|
1332
|
-
var
|
|
1333
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
1334
|
-
const
|
|
3009
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h;
|
|
3010
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
3011
|
+
const transformedBody = this.transformRequestBody(args);
|
|
3012
|
+
const body = JSON.stringify(transformedBody);
|
|
1335
3013
|
const {
|
|
1336
3014
|
responseHeaders,
|
|
1337
3015
|
value: responseBody,
|
|
@@ -1342,7 +3020,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1342
3020
|
modelId: this.modelId
|
|
1343
3021
|
}),
|
|
1344
3022
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
1345
|
-
body:
|
|
3023
|
+
body: transformedBody,
|
|
1346
3024
|
failedResponseHandler: this.failedResponseHandler,
|
|
1347
3025
|
successfulResponseHandler: createJsonResponseHandler(
|
|
1348
3026
|
OpenAICompatibleChatResponseSchema
|
|
@@ -1356,7 +3034,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1356
3034
|
if (text != null && text.length > 0) {
|
|
1357
3035
|
content.push({ type: "text", text });
|
|
1358
3036
|
}
|
|
1359
|
-
const reasoning = (
|
|
3037
|
+
const reasoning = (_a16 = choice.message.reasoning_content) != null ? _a16 : choice.message.reasoning;
|
|
1360
3038
|
if (reasoning != null && reasoning.length > 0) {
|
|
1361
3039
|
content.push({
|
|
1362
3040
|
type: "reasoning",
|
|
@@ -1365,37 +3043,40 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1365
3043
|
}
|
|
1366
3044
|
if (choice.message.tool_calls != null) {
|
|
1367
3045
|
for (const toolCall of choice.message.tool_calls) {
|
|
3046
|
+
const thoughtSignature = (_c = (_b16 = toolCall.extra_content) == null ? void 0 : _b16.google) == null ? void 0 : _c.thought_signature;
|
|
1368
3047
|
content.push({
|
|
1369
3048
|
type: "tool-call",
|
|
1370
|
-
toolCallId: (
|
|
3049
|
+
toolCallId: (_d = toolCall.id) != null ? _d : generateId(),
|
|
1371
3050
|
toolName: toolCall.function.name,
|
|
1372
|
-
input: toolCall.function.arguments
|
|
3051
|
+
input: toolCall.function.arguments,
|
|
3052
|
+
...thoughtSignature ? {
|
|
3053
|
+
providerMetadata: {
|
|
3054
|
+
[metadataKey]: { thoughtSignature }
|
|
3055
|
+
}
|
|
3056
|
+
} : {}
|
|
1373
3057
|
});
|
|
1374
3058
|
}
|
|
1375
3059
|
}
|
|
1376
3060
|
const providerMetadata = {
|
|
1377
|
-
[
|
|
1378
|
-
...await ((
|
|
3061
|
+
[metadataKey]: {},
|
|
3062
|
+
...await ((_f = (_e = this.config.metadataExtractor) == null ? void 0 : _e.extractMetadata) == null ? void 0 : _f.call(_e, {
|
|
1379
3063
|
parsedBody: rawResponse
|
|
1380
3064
|
}))
|
|
1381
3065
|
};
|
|
1382
|
-
const completionTokenDetails = (
|
|
3066
|
+
const completionTokenDetails = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens_details;
|
|
1383
3067
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
1384
|
-
providerMetadata[
|
|
3068
|
+
providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
1385
3069
|
}
|
|
1386
3070
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
|
|
1387
|
-
providerMetadata[
|
|
3071
|
+
providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
1388
3072
|
}
|
|
1389
3073
|
return {
|
|
1390
3074
|
content,
|
|
1391
|
-
finishReason:
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0,
|
|
1395
|
-
totalTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.total_tokens) != null ? _k : void 0,
|
|
1396
|
-
reasoningTokens: (_n = (_m = (_l = responseBody.usage) == null ? void 0 : _l.completion_tokens_details) == null ? void 0 : _m.reasoning_tokens) != null ? _n : void 0,
|
|
1397
|
-
cachedInputTokens: (_q = (_p = (_o = responseBody.usage) == null ? void 0 : _o.prompt_tokens_details) == null ? void 0 : _p.cached_tokens) != null ? _q : void 0
|
|
3075
|
+
finishReason: {
|
|
3076
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
3077
|
+
raw: (_h = choice.finish_reason) != null ? _h : void 0
|
|
1398
3078
|
},
|
|
3079
|
+
usage: convertOpenAICompatibleChatUsage(responseBody.usage),
|
|
1399
3080
|
providerMetadata,
|
|
1400
3081
|
request: { body },
|
|
1401
3082
|
response: {
|
|
@@ -1407,15 +3088,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1407
3088
|
};
|
|
1408
3089
|
}
|
|
1409
3090
|
async doStream(options) {
|
|
1410
|
-
var
|
|
1411
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
1412
|
-
const body = {
|
|
3091
|
+
var _a16;
|
|
3092
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
3093
|
+
const body = this.transformRequestBody({
|
|
1413
3094
|
...args,
|
|
1414
3095
|
stream: true,
|
|
1415
3096
|
// only include stream_options when in strict compatibility mode:
|
|
1416
3097
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
1417
|
-
};
|
|
1418
|
-
const metadataExtractor = (
|
|
3098
|
+
});
|
|
3099
|
+
const metadataExtractor = (_a16 = this.config.metadataExtractor) == null ? void 0 : _a16.createStreamExtractor();
|
|
1419
3100
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
1420
3101
|
url: this.config.url({
|
|
1421
3102
|
path: "/chat/completions",
|
|
@@ -1431,22 +3112,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1431
3112
|
fetch: this.config.fetch
|
|
1432
3113
|
});
|
|
1433
3114
|
const toolCalls = [];
|
|
1434
|
-
let finishReason =
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
completionTokensDetails: {
|
|
1438
|
-
reasoningTokens: void 0,
|
|
1439
|
-
acceptedPredictionTokens: void 0,
|
|
1440
|
-
rejectedPredictionTokens: void 0
|
|
1441
|
-
},
|
|
1442
|
-
promptTokens: void 0,
|
|
1443
|
-
promptTokensDetails: {
|
|
1444
|
-
cachedTokens: void 0
|
|
1445
|
-
},
|
|
1446
|
-
totalTokens: void 0
|
|
3115
|
+
let finishReason = {
|
|
3116
|
+
unified: "other",
|
|
3117
|
+
raw: void 0
|
|
1447
3118
|
};
|
|
3119
|
+
let usage = void 0;
|
|
1448
3120
|
let isFirstChunk = true;
|
|
1449
|
-
const providerOptionsName =
|
|
3121
|
+
const providerOptionsName = metadataKey;
|
|
1450
3122
|
let isActiveReasoning = false;
|
|
1451
3123
|
let isActiveText = false;
|
|
1452
3124
|
return {
|
|
@@ -1455,24 +3127,26 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1455
3127
|
start(controller) {
|
|
1456
3128
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1457
3129
|
},
|
|
1458
|
-
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
|
1459
3130
|
transform(chunk, controller) {
|
|
1460
|
-
var _a22,
|
|
3131
|
+
var _a22, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
|
1461
3132
|
if (options.includeRawChunks) {
|
|
1462
3133
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1463
3134
|
}
|
|
1464
3135
|
if (!chunk.success) {
|
|
1465
|
-
finishReason = "error";
|
|
3136
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
1466
3137
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
1467
3138
|
return;
|
|
1468
3139
|
}
|
|
1469
|
-
const value = chunk.value;
|
|
1470
3140
|
metadataExtractor == null ? void 0 : metadataExtractor.processChunk(chunk.rawValue);
|
|
1471
|
-
if ("error" in value) {
|
|
1472
|
-
finishReason = "error";
|
|
1473
|
-
controller.enqueue({
|
|
3141
|
+
if ("error" in chunk.value) {
|
|
3142
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
3143
|
+
controller.enqueue({
|
|
3144
|
+
type: "error",
|
|
3145
|
+
error: chunk.value.error.message
|
|
3146
|
+
});
|
|
1474
3147
|
return;
|
|
1475
3148
|
}
|
|
3149
|
+
const value = chunk.value;
|
|
1476
3150
|
if (isFirstChunk) {
|
|
1477
3151
|
isFirstChunk = false;
|
|
1478
3152
|
controller.enqueue({
|
|
@@ -1481,40 +3155,20 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1481
3155
|
});
|
|
1482
3156
|
}
|
|
1483
3157
|
if (value.usage != null) {
|
|
1484
|
-
|
|
1485
|
-
prompt_tokens,
|
|
1486
|
-
completion_tokens,
|
|
1487
|
-
total_tokens,
|
|
1488
|
-
prompt_tokens_details,
|
|
1489
|
-
completion_tokens_details
|
|
1490
|
-
} = value.usage;
|
|
1491
|
-
usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
|
|
1492
|
-
usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
|
|
1493
|
-
usage.totalTokens = total_tokens != null ? total_tokens : void 0;
|
|
1494
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
|
|
1495
|
-
usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
|
|
1496
|
-
}
|
|
1497
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
|
|
1498
|
-
usage.completionTokensDetails.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
|
|
1499
|
-
}
|
|
1500
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
|
|
1501
|
-
usage.completionTokensDetails.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
|
|
1502
|
-
}
|
|
1503
|
-
if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
|
|
1504
|
-
usage.promptTokensDetails.cachedTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
|
|
1505
|
-
}
|
|
3158
|
+
usage = value.usage;
|
|
1506
3159
|
}
|
|
1507
3160
|
const choice = value.choices[0];
|
|
1508
3161
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
1509
|
-
finishReason =
|
|
1510
|
-
choice.finish_reason
|
|
1511
|
-
|
|
3162
|
+
finishReason = {
|
|
3163
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
3164
|
+
raw: (_a22 = choice.finish_reason) != null ? _a22 : void 0
|
|
3165
|
+
};
|
|
1512
3166
|
}
|
|
1513
3167
|
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
1514
3168
|
return;
|
|
1515
3169
|
}
|
|
1516
3170
|
const delta = choice.delta;
|
|
1517
|
-
const reasoningContent = (
|
|
3171
|
+
const reasoningContent = (_b16 = delta.reasoning_content) != null ? _b16 : delta.reasoning;
|
|
1518
3172
|
if (reasoningContent) {
|
|
1519
3173
|
if (!isActiveReasoning) {
|
|
1520
3174
|
controller.enqueue({
|
|
@@ -1530,6 +3184,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1530
3184
|
});
|
|
1531
3185
|
}
|
|
1532
3186
|
if (delta.content) {
|
|
3187
|
+
if (isActiveReasoning) {
|
|
3188
|
+
controller.enqueue({
|
|
3189
|
+
type: "reasoning-end",
|
|
3190
|
+
id: "reasoning-0"
|
|
3191
|
+
});
|
|
3192
|
+
isActiveReasoning = false;
|
|
3193
|
+
}
|
|
1533
3194
|
if (!isActiveText) {
|
|
1534
3195
|
controller.enqueue({ type: "text-start", id: "txt-0" });
|
|
1535
3196
|
isActiveText = true;
|
|
@@ -1541,8 +3202,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1541
3202
|
});
|
|
1542
3203
|
}
|
|
1543
3204
|
if (delta.tool_calls != null) {
|
|
3205
|
+
if (isActiveReasoning) {
|
|
3206
|
+
controller.enqueue({
|
|
3207
|
+
type: "reasoning-end",
|
|
3208
|
+
id: "reasoning-0"
|
|
3209
|
+
});
|
|
3210
|
+
isActiveReasoning = false;
|
|
3211
|
+
}
|
|
1544
3212
|
for (const toolCallDelta of delta.tool_calls) {
|
|
1545
|
-
const index = toolCallDelta.index;
|
|
3213
|
+
const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length;
|
|
1546
3214
|
if (toolCalls[index] == null) {
|
|
1547
3215
|
if (toolCallDelta.id == null) {
|
|
1548
3216
|
throw new InvalidResponseDataError({
|
|
@@ -1550,7 +3218,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1550
3218
|
message: `Expected 'id' to be a string.`
|
|
1551
3219
|
});
|
|
1552
3220
|
}
|
|
1553
|
-
if (((
|
|
3221
|
+
if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
|
|
1554
3222
|
throw new InvalidResponseDataError({
|
|
1555
3223
|
data: toolCallDelta,
|
|
1556
3224
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -1566,12 +3234,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1566
3234
|
type: "function",
|
|
1567
3235
|
function: {
|
|
1568
3236
|
name: toolCallDelta.function.name,
|
|
1569
|
-
arguments: (
|
|
3237
|
+
arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
|
|
1570
3238
|
},
|
|
1571
|
-
hasFinished: false
|
|
3239
|
+
hasFinished: false,
|
|
3240
|
+
thoughtSignature: (_h = (_g = (_f = toolCallDelta.extra_content) == null ? void 0 : _f.google) == null ? void 0 : _g.thought_signature) != null ? _h : void 0
|
|
1572
3241
|
};
|
|
1573
3242
|
const toolCall2 = toolCalls[index];
|
|
1574
|
-
if (((
|
|
3243
|
+
if (((_i = toolCall2.function) == null ? void 0 : _i.name) != null && ((_j = toolCall2.function) == null ? void 0 : _j.arguments) != null) {
|
|
1575
3244
|
if (toolCall2.function.arguments.length > 0) {
|
|
1576
3245
|
controller.enqueue({
|
|
1577
3246
|
type: "tool-input-delta",
|
|
@@ -1586,9 +3255,16 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1586
3255
|
});
|
|
1587
3256
|
controller.enqueue({
|
|
1588
3257
|
type: "tool-call",
|
|
1589
|
-
toolCallId: (
|
|
3258
|
+
toolCallId: (_k = toolCall2.id) != null ? _k : generateId(),
|
|
1590
3259
|
toolName: toolCall2.function.name,
|
|
1591
|
-
input: toolCall2.function.arguments
|
|
3260
|
+
input: toolCall2.function.arguments,
|
|
3261
|
+
...toolCall2.thoughtSignature ? {
|
|
3262
|
+
providerMetadata: {
|
|
3263
|
+
[providerOptionsName]: {
|
|
3264
|
+
thoughtSignature: toolCall2.thoughtSignature
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
3267
|
+
} : {}
|
|
1592
3268
|
});
|
|
1593
3269
|
toolCall2.hasFinished = true;
|
|
1594
3270
|
}
|
|
@@ -1599,24 +3275,31 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1599
3275
|
if (toolCall.hasFinished) {
|
|
1600
3276
|
continue;
|
|
1601
3277
|
}
|
|
1602
|
-
if (((
|
|
1603
|
-
toolCall.function.arguments += (
|
|
3278
|
+
if (((_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null) {
|
|
3279
|
+
toolCall.function.arguments += (_n = (_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null ? _n : "";
|
|
1604
3280
|
}
|
|
1605
3281
|
controller.enqueue({
|
|
1606
3282
|
type: "tool-input-delta",
|
|
1607
3283
|
id: toolCall.id,
|
|
1608
|
-
delta: (
|
|
3284
|
+
delta: (_o = toolCallDelta.function.arguments) != null ? _o : ""
|
|
1609
3285
|
});
|
|
1610
|
-
if (((
|
|
3286
|
+
if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
1611
3287
|
controller.enqueue({
|
|
1612
3288
|
type: "tool-input-end",
|
|
1613
3289
|
id: toolCall.id
|
|
1614
3290
|
});
|
|
1615
3291
|
controller.enqueue({
|
|
1616
3292
|
type: "tool-call",
|
|
1617
|
-
toolCallId: (
|
|
3293
|
+
toolCallId: (_r = toolCall.id) != null ? _r : generateId(),
|
|
1618
3294
|
toolName: toolCall.function.name,
|
|
1619
|
-
input: toolCall.function.arguments
|
|
3295
|
+
input: toolCall.function.arguments,
|
|
3296
|
+
...toolCall.thoughtSignature ? {
|
|
3297
|
+
providerMetadata: {
|
|
3298
|
+
[providerOptionsName]: {
|
|
3299
|
+
thoughtSignature: toolCall.thoughtSignature
|
|
3300
|
+
}
|
|
3301
|
+
}
|
|
3302
|
+
} : {}
|
|
1620
3303
|
});
|
|
1621
3304
|
toolCall.hasFinished = true;
|
|
1622
3305
|
}
|
|
@@ -1624,7 +3307,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1624
3307
|
}
|
|
1625
3308
|
},
|
|
1626
3309
|
flush(controller) {
|
|
1627
|
-
var _a22,
|
|
3310
|
+
var _a22, _b16, _c, _d, _e;
|
|
1628
3311
|
if (isActiveReasoning) {
|
|
1629
3312
|
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
1630
3313
|
}
|
|
@@ -1642,29 +3325,30 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1642
3325
|
type: "tool-call",
|
|
1643
3326
|
toolCallId: (_a22 = toolCall.id) != null ? _a22 : generateId(),
|
|
1644
3327
|
toolName: toolCall.function.name,
|
|
1645
|
-
input: toolCall.function.arguments
|
|
3328
|
+
input: toolCall.function.arguments,
|
|
3329
|
+
...toolCall.thoughtSignature ? {
|
|
3330
|
+
providerMetadata: {
|
|
3331
|
+
[providerOptionsName]: {
|
|
3332
|
+
thoughtSignature: toolCall.thoughtSignature
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
} : {}
|
|
1646
3336
|
});
|
|
1647
3337
|
}
|
|
1648
3338
|
const providerMetadata = {
|
|
1649
3339
|
[providerOptionsName]: {},
|
|
1650
3340
|
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
|
1651
3341
|
};
|
|
1652
|
-
if (usage.
|
|
1653
|
-
providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.
|
|
3342
|
+
if (((_b16 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b16.accepted_prediction_tokens) != null) {
|
|
3343
|
+
providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c.accepted_prediction_tokens;
|
|
1654
3344
|
}
|
|
1655
|
-
if (usage.
|
|
1656
|
-
providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.
|
|
3345
|
+
if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) {
|
|
3346
|
+
providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
|
|
1657
3347
|
}
|
|
1658
3348
|
controller.enqueue({
|
|
1659
3349
|
type: "finish",
|
|
1660
3350
|
finishReason,
|
|
1661
|
-
usage:
|
|
1662
|
-
inputTokens: (_b15 = usage.promptTokens) != null ? _b15 : void 0,
|
|
1663
|
-
outputTokens: (_c = usage.completionTokens) != null ? _c : void 0,
|
|
1664
|
-
totalTokens: (_d = usage.totalTokens) != null ? _d : void 0,
|
|
1665
|
-
reasoningTokens: (_e = usage.completionTokensDetails.reasoningTokens) != null ? _e : void 0,
|
|
1666
|
-
cachedInputTokens: (_f = usage.promptTokensDetails.cachedTokens) != null ? _f : void 0
|
|
1667
|
-
},
|
|
3351
|
+
usage: convertOpenAICompatibleChatUsage(usage),
|
|
1668
3352
|
providerMetadata
|
|
1669
3353
|
});
|
|
1670
3354
|
}
|
|
@@ -1675,7 +3359,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1675
3359
|
};
|
|
1676
3360
|
}
|
|
1677
3361
|
};
|
|
1678
|
-
var openaiCompatibleTokenUsageSchema = import_v4.z.
|
|
3362
|
+
var openaiCompatibleTokenUsageSchema = import_v4.z.looseObject({
|
|
1679
3363
|
prompt_tokens: import_v4.z.number().nullish(),
|
|
1680
3364
|
completion_tokens: import_v4.z.number().nullish(),
|
|
1681
3365
|
total_tokens: import_v4.z.number().nullish(),
|
|
@@ -1688,7 +3372,7 @@ var openaiCompatibleTokenUsageSchema = import_v4.z.object({
|
|
|
1688
3372
|
rejected_prediction_tokens: import_v4.z.number().nullish()
|
|
1689
3373
|
}).nullish()
|
|
1690
3374
|
}).nullish();
|
|
1691
|
-
var OpenAICompatibleChatResponseSchema = import_v4.z.
|
|
3375
|
+
var OpenAICompatibleChatResponseSchema = import_v4.z.looseObject({
|
|
1692
3376
|
id: import_v4.z.string().nullish(),
|
|
1693
3377
|
created: import_v4.z.number().nullish(),
|
|
1694
3378
|
model: import_v4.z.string().nullish(),
|
|
@@ -1705,7 +3389,13 @@ var OpenAICompatibleChatResponseSchema = import_v4.z.object({
|
|
|
1705
3389
|
function: import_v4.z.object({
|
|
1706
3390
|
name: import_v4.z.string(),
|
|
1707
3391
|
arguments: import_v4.z.string()
|
|
1708
|
-
})
|
|
3392
|
+
}),
|
|
3393
|
+
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
3394
|
+
extra_content: import_v4.z.object({
|
|
3395
|
+
google: import_v4.z.object({
|
|
3396
|
+
thought_signature: import_v4.z.string().nullish()
|
|
3397
|
+
}).nullish()
|
|
3398
|
+
}).nullish()
|
|
1709
3399
|
})
|
|
1710
3400
|
).nullish()
|
|
1711
3401
|
}),
|
|
@@ -1714,38 +3404,78 @@ var OpenAICompatibleChatResponseSchema = import_v4.z.object({
|
|
|
1714
3404
|
),
|
|
1715
3405
|
usage: openaiCompatibleTokenUsageSchema
|
|
1716
3406
|
});
|
|
1717
|
-
var
|
|
1718
|
-
import_v4.z.
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
import_v4.z.object({
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
import_v4.z.
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
3407
|
+
var chunkBaseSchema = import_v4.z.looseObject({
|
|
3408
|
+
id: import_v4.z.string().nullish(),
|
|
3409
|
+
created: import_v4.z.number().nullish(),
|
|
3410
|
+
model: import_v4.z.string().nullish(),
|
|
3411
|
+
choices: import_v4.z.array(
|
|
3412
|
+
import_v4.z.object({
|
|
3413
|
+
delta: import_v4.z.object({
|
|
3414
|
+
role: import_v4.z.enum(["assistant"]).nullish(),
|
|
3415
|
+
content: import_v4.z.string().nullish(),
|
|
3416
|
+
// Most openai-compatible models set `reasoning_content`, but some
|
|
3417
|
+
// providers serving `gpt-oss` set `reasoning`. See #7866
|
|
3418
|
+
reasoning_content: import_v4.z.string().nullish(),
|
|
3419
|
+
reasoning: import_v4.z.string().nullish(),
|
|
3420
|
+
tool_calls: import_v4.z.array(
|
|
3421
|
+
import_v4.z.object({
|
|
3422
|
+
index: import_v4.z.number().nullish(),
|
|
3423
|
+
//google does not send index
|
|
3424
|
+
id: import_v4.z.string().nullish(),
|
|
3425
|
+
function: import_v4.z.object({
|
|
3426
|
+
name: import_v4.z.string().nullish(),
|
|
3427
|
+
arguments: import_v4.z.string().nullish()
|
|
3428
|
+
}),
|
|
3429
|
+
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
3430
|
+
extra_content: import_v4.z.object({
|
|
3431
|
+
google: import_v4.z.object({
|
|
3432
|
+
thought_signature: import_v4.z.string().nullish()
|
|
3433
|
+
}).nullish()
|
|
3434
|
+
}).nullish()
|
|
3435
|
+
})
|
|
3436
|
+
).nullish()
|
|
3437
|
+
}).nullish(),
|
|
3438
|
+
finish_reason: import_v4.z.string().nullish()
|
|
3439
|
+
})
|
|
3440
|
+
),
|
|
3441
|
+
usage: openaiCompatibleTokenUsageSchema
|
|
3442
|
+
});
|
|
3443
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_v4.z.union([chunkBaseSchema, errorSchema]);
|
|
3444
|
+
function convertOpenAICompatibleCompletionUsage(usage) {
|
|
3445
|
+
var _a16, _b16;
|
|
3446
|
+
if (usage == null) {
|
|
3447
|
+
return {
|
|
3448
|
+
inputTokens: {
|
|
3449
|
+
total: void 0,
|
|
3450
|
+
noCache: void 0,
|
|
3451
|
+
cacheRead: void 0,
|
|
3452
|
+
cacheWrite: void 0
|
|
3453
|
+
},
|
|
3454
|
+
outputTokens: {
|
|
3455
|
+
total: void 0,
|
|
3456
|
+
text: void 0,
|
|
3457
|
+
reasoning: void 0
|
|
3458
|
+
},
|
|
3459
|
+
raw: void 0
|
|
3460
|
+
};
|
|
3461
|
+
}
|
|
3462
|
+
const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
|
|
3463
|
+
const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
|
|
3464
|
+
return {
|
|
3465
|
+
inputTokens: {
|
|
3466
|
+
total: promptTokens,
|
|
3467
|
+
noCache: promptTokens,
|
|
3468
|
+
cacheRead: void 0,
|
|
3469
|
+
cacheWrite: void 0
|
|
3470
|
+
},
|
|
3471
|
+
outputTokens: {
|
|
3472
|
+
total: completionTokens,
|
|
3473
|
+
text: completionTokens,
|
|
3474
|
+
reasoning: void 0
|
|
3475
|
+
},
|
|
3476
|
+
raw: usage
|
|
3477
|
+
};
|
|
3478
|
+
}
|
|
1749
3479
|
function convertToOpenAICompatibleCompletionPrompt({
|
|
1750
3480
|
prompt,
|
|
1751
3481
|
user = "user",
|
|
@@ -1841,10 +3571,10 @@ function mapOpenAICompatibleFinishReason2(finishReason) {
|
|
|
1841
3571
|
case "tool_calls":
|
|
1842
3572
|
return "tool-calls";
|
|
1843
3573
|
default:
|
|
1844
|
-
return "
|
|
3574
|
+
return "other";
|
|
1845
3575
|
}
|
|
1846
3576
|
}
|
|
1847
|
-
var
|
|
3577
|
+
var openaiCompatibleLanguageModelCompletionOptions = import_v45.z.object({
|
|
1848
3578
|
/**
|
|
1849
3579
|
* Echo back the prompt in addition to the completion.
|
|
1850
3580
|
*/
|
|
@@ -1869,11 +3599,11 @@ var openaiCompatibleCompletionProviderOptions = import_v45.z.object({
|
|
|
1869
3599
|
var OpenAICompatibleCompletionLanguageModel = class {
|
|
1870
3600
|
// type inferred via constructor
|
|
1871
3601
|
constructor(modelId, config) {
|
|
1872
|
-
this.specificationVersion = "
|
|
1873
|
-
var
|
|
3602
|
+
this.specificationVersion = "v3";
|
|
3603
|
+
var _a16;
|
|
1874
3604
|
this.modelId = modelId;
|
|
1875
3605
|
this.config = config;
|
|
1876
|
-
const errorStructure = (
|
|
3606
|
+
const errorStructure = (_a16 = config.errorStructure) != null ? _a16 : defaultOpenAICompatibleErrorStructure;
|
|
1877
3607
|
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
|
1878
3608
|
errorStructure.errorSchema
|
|
1879
3609
|
);
|
|
@@ -1886,8 +3616,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1886
3616
|
return this.config.provider.split(".")[0].trim();
|
|
1887
3617
|
}
|
|
1888
3618
|
get supportedUrls() {
|
|
1889
|
-
var
|
|
1890
|
-
return (_c = (
|
|
3619
|
+
var _a16, _b16, _c;
|
|
3620
|
+
return (_c = (_b16 = (_a16 = this.config).supportedUrls) == null ? void 0 : _b16.call(_a16)) != null ? _c : {};
|
|
1891
3621
|
}
|
|
1892
3622
|
async getArgs({
|
|
1893
3623
|
prompt,
|
|
@@ -1904,26 +3634,33 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1904
3634
|
tools,
|
|
1905
3635
|
toolChoice
|
|
1906
3636
|
}) {
|
|
1907
|
-
var
|
|
3637
|
+
var _a16, _b16;
|
|
1908
3638
|
const warnings = [];
|
|
1909
|
-
const completionOptions = (
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
3639
|
+
const completionOptions = Object.assign(
|
|
3640
|
+
(_a16 = await parseProviderOptions({
|
|
3641
|
+
provider: this.providerOptionsName,
|
|
3642
|
+
providerOptions,
|
|
3643
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
3644
|
+
})) != null ? _a16 : {},
|
|
3645
|
+
(_b16 = await parseProviderOptions({
|
|
3646
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
3647
|
+
providerOptions,
|
|
3648
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
3649
|
+
})) != null ? _b16 : {}
|
|
3650
|
+
);
|
|
1914
3651
|
if (topK != null) {
|
|
1915
|
-
warnings.push({ type: "unsupported
|
|
3652
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1916
3653
|
}
|
|
1917
3654
|
if (tools == null ? void 0 : tools.length) {
|
|
1918
|
-
warnings.push({ type: "unsupported
|
|
3655
|
+
warnings.push({ type: "unsupported", feature: "tools" });
|
|
1919
3656
|
}
|
|
1920
3657
|
if (toolChoice != null) {
|
|
1921
|
-
warnings.push({ type: "unsupported
|
|
3658
|
+
warnings.push({ type: "unsupported", feature: "toolChoice" });
|
|
1922
3659
|
}
|
|
1923
3660
|
if (responseFormat != null && responseFormat.type !== "text") {
|
|
1924
3661
|
warnings.push({
|
|
1925
|
-
type: "unsupported
|
|
1926
|
-
|
|
3662
|
+
type: "unsupported",
|
|
3663
|
+
feature: "responseFormat",
|
|
1927
3664
|
details: "JSON response format is not supported."
|
|
1928
3665
|
});
|
|
1929
3666
|
}
|
|
@@ -1946,6 +3683,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1946
3683
|
presence_penalty: presencePenalty,
|
|
1947
3684
|
seed,
|
|
1948
3685
|
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
3686
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
|
|
1949
3687
|
// prompt:
|
|
1950
3688
|
prompt: completionPrompt,
|
|
1951
3689
|
// stop sequences:
|
|
@@ -1955,7 +3693,6 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1955
3693
|
};
|
|
1956
3694
|
}
|
|
1957
3695
|
async doGenerate(options) {
|
|
1958
|
-
var _a15, _b15, _c, _d, _e, _f;
|
|
1959
3696
|
const { args, warnings } = await this.getArgs(options);
|
|
1960
3697
|
const {
|
|
1961
3698
|
responseHeaders,
|
|
@@ -1982,12 +3719,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1982
3719
|
}
|
|
1983
3720
|
return {
|
|
1984
3721
|
content,
|
|
1985
|
-
usage:
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
3722
|
+
usage: convertOpenAICompatibleCompletionUsage(response.usage),
|
|
3723
|
+
finishReason: {
|
|
3724
|
+
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
3725
|
+
raw: choice.finish_reason
|
|
1989
3726
|
},
|
|
1990
|
-
finishReason: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
1991
3727
|
request: { body: args },
|
|
1992
3728
|
response: {
|
|
1993
3729
|
...getResponseMetadata2(response),
|
|
@@ -2019,12 +3755,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
2019
3755
|
abortSignal: options.abortSignal,
|
|
2020
3756
|
fetch: this.config.fetch
|
|
2021
3757
|
});
|
|
2022
|
-
let finishReason =
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
outputTokens: void 0,
|
|
2026
|
-
totalTokens: void 0
|
|
3758
|
+
let finishReason = {
|
|
3759
|
+
unified: "other",
|
|
3760
|
+
raw: void 0
|
|
2027
3761
|
};
|
|
3762
|
+
let usage = void 0;
|
|
2028
3763
|
let isFirstChunk = true;
|
|
2029
3764
|
return {
|
|
2030
3765
|
stream: response.pipeThrough(
|
|
@@ -2033,18 +3768,18 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
2033
3768
|
controller.enqueue({ type: "stream-start", warnings });
|
|
2034
3769
|
},
|
|
2035
3770
|
transform(chunk, controller) {
|
|
2036
|
-
var
|
|
3771
|
+
var _a16;
|
|
2037
3772
|
if (options.includeRawChunks) {
|
|
2038
3773
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2039
3774
|
}
|
|
2040
3775
|
if (!chunk.success) {
|
|
2041
|
-
finishReason = "error";
|
|
3776
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
2042
3777
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
2043
3778
|
return;
|
|
2044
3779
|
}
|
|
2045
3780
|
const value = chunk.value;
|
|
2046
3781
|
if ("error" in value) {
|
|
2047
|
-
finishReason = "error";
|
|
3782
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
2048
3783
|
controller.enqueue({ type: "error", error: value.error });
|
|
2049
3784
|
return;
|
|
2050
3785
|
}
|
|
@@ -2060,15 +3795,14 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
2060
3795
|
});
|
|
2061
3796
|
}
|
|
2062
3797
|
if (value.usage != null) {
|
|
2063
|
-
usage
|
|
2064
|
-
usage.outputTokens = (_b15 = value.usage.completion_tokens) != null ? _b15 : void 0;
|
|
2065
|
-
usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
|
|
3798
|
+
usage = value.usage;
|
|
2066
3799
|
}
|
|
2067
3800
|
const choice = value.choices[0];
|
|
2068
3801
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
2069
|
-
finishReason =
|
|
2070
|
-
choice.finish_reason
|
|
2071
|
-
|
|
3802
|
+
finishReason = {
|
|
3803
|
+
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
3804
|
+
raw: (_a16 = choice.finish_reason) != null ? _a16 : void 0
|
|
3805
|
+
};
|
|
2072
3806
|
}
|
|
2073
3807
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
2074
3808
|
controller.enqueue({
|
|
@@ -2085,7 +3819,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
2085
3819
|
controller.enqueue({
|
|
2086
3820
|
type: "finish",
|
|
2087
3821
|
finishReason,
|
|
2088
|
-
usage
|
|
3822
|
+
usage: convertOpenAICompatibleCompletionUsage(usage)
|
|
2089
3823
|
});
|
|
2090
3824
|
}
|
|
2091
3825
|
})
|
|
@@ -2128,7 +3862,7 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_v44.z.
|
|
|
2128
3862
|
}),
|
|
2129
3863
|
errorSchema
|
|
2130
3864
|
]);
|
|
2131
|
-
var
|
|
3865
|
+
var openaiCompatibleEmbeddingModelOptions = import_v47.z.object({
|
|
2132
3866
|
/**
|
|
2133
3867
|
* The number of dimensions the resulting output embeddings should have.
|
|
2134
3868
|
* Only supported in text-embedding-3 and later models.
|
|
@@ -2142,7 +3876,7 @@ var openaiCompatibleEmbeddingProviderOptions = import_v47.z.object({
|
|
|
2142
3876
|
});
|
|
2143
3877
|
var OpenAICompatibleEmbeddingModel = class {
|
|
2144
3878
|
constructor(modelId, config) {
|
|
2145
|
-
this.specificationVersion = "
|
|
3879
|
+
this.specificationVersion = "v3";
|
|
2146
3880
|
this.modelId = modelId;
|
|
2147
3881
|
this.config = config;
|
|
2148
3882
|
}
|
|
@@ -2150,12 +3884,12 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
2150
3884
|
return this.config.provider;
|
|
2151
3885
|
}
|
|
2152
3886
|
get maxEmbeddingsPerCall() {
|
|
2153
|
-
var
|
|
2154
|
-
return (
|
|
3887
|
+
var _a16;
|
|
3888
|
+
return (_a16 = this.config.maxEmbeddingsPerCall) != null ? _a16 : 2048;
|
|
2155
3889
|
}
|
|
2156
3890
|
get supportsParallelCalls() {
|
|
2157
|
-
var
|
|
2158
|
-
return (
|
|
3891
|
+
var _a16;
|
|
3892
|
+
return (_a16 = this.config.supportsParallelCalls) != null ? _a16 : true;
|
|
2159
3893
|
}
|
|
2160
3894
|
get providerOptionsName() {
|
|
2161
3895
|
return this.config.provider.split(".")[0].trim();
|
|
@@ -2166,18 +3900,31 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
2166
3900
|
abortSignal,
|
|
2167
3901
|
providerOptions
|
|
2168
3902
|
}) {
|
|
2169
|
-
var
|
|
3903
|
+
var _a16, _b16, _c;
|
|
3904
|
+
const warnings = [];
|
|
3905
|
+
const deprecatedOptions = await parseProviderOptions({
|
|
3906
|
+
provider: "openai-compatible",
|
|
3907
|
+
providerOptions,
|
|
3908
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
3909
|
+
});
|
|
3910
|
+
if (deprecatedOptions != null) {
|
|
3911
|
+
warnings.push({
|
|
3912
|
+
type: "other",
|
|
3913
|
+
message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
|
|
3914
|
+
});
|
|
3915
|
+
}
|
|
2170
3916
|
const compatibleOptions = Object.assign(
|
|
2171
|
-
|
|
2172
|
-
|
|
3917
|
+
deprecatedOptions != null ? deprecatedOptions : {},
|
|
3918
|
+
(_a16 = await parseProviderOptions({
|
|
3919
|
+
provider: "openaiCompatible",
|
|
2173
3920
|
providerOptions,
|
|
2174
|
-
schema:
|
|
2175
|
-
})) != null ?
|
|
2176
|
-
(
|
|
3921
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
3922
|
+
})) != null ? _a16 : {},
|
|
3923
|
+
(_b16 = await parseProviderOptions({
|
|
2177
3924
|
provider: this.providerOptionsName,
|
|
2178
3925
|
providerOptions,
|
|
2179
|
-
schema:
|
|
2180
|
-
})) != null ?
|
|
3926
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
3927
|
+
})) != null ? _b16 : {}
|
|
2181
3928
|
);
|
|
2182
3929
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
2183
3930
|
throw new TooManyEmbeddingValuesForCallError({
|
|
@@ -2214,6 +3961,7 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
2214
3961
|
fetch: this.config.fetch
|
|
2215
3962
|
});
|
|
2216
3963
|
return {
|
|
3964
|
+
warnings,
|
|
2217
3965
|
embeddings: response.data.map((item) => item.embedding),
|
|
2218
3966
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
2219
3967
|
providerMetadata: response.providerMetadata,
|
|
@@ -2230,12 +3978,25 @@ var OpenAICompatibleImageModel = class {
|
|
|
2230
3978
|
constructor(modelId, config) {
|
|
2231
3979
|
this.modelId = modelId;
|
|
2232
3980
|
this.config = config;
|
|
2233
|
-
this.specificationVersion = "
|
|
3981
|
+
this.specificationVersion = "v3";
|
|
2234
3982
|
this.maxImagesPerCall = 10;
|
|
2235
3983
|
}
|
|
2236
3984
|
get provider() {
|
|
2237
3985
|
return this.config.provider;
|
|
2238
3986
|
}
|
|
3987
|
+
/**
|
|
3988
|
+
* The provider options key used to extract provider-specific options.
|
|
3989
|
+
*/
|
|
3990
|
+
get providerOptionsKey() {
|
|
3991
|
+
return this.config.provider.split(".")[0].trim();
|
|
3992
|
+
}
|
|
3993
|
+
// TODO: deprecate non-camelCase keys and remove in future major version
|
|
3994
|
+
getArgs(providerOptions) {
|
|
3995
|
+
return {
|
|
3996
|
+
...providerOptions[this.providerOptionsKey],
|
|
3997
|
+
...providerOptions[toCamelCase(this.providerOptionsKey)]
|
|
3998
|
+
};
|
|
3999
|
+
}
|
|
2239
4000
|
async doGenerate({
|
|
2240
4001
|
prompt,
|
|
2241
4002
|
n,
|
|
@@ -2244,21 +4005,59 @@ var OpenAICompatibleImageModel = class {
|
|
|
2244
4005
|
seed,
|
|
2245
4006
|
providerOptions,
|
|
2246
4007
|
headers,
|
|
2247
|
-
abortSignal
|
|
4008
|
+
abortSignal,
|
|
4009
|
+
files,
|
|
4010
|
+
mask
|
|
2248
4011
|
}) {
|
|
2249
|
-
var
|
|
4012
|
+
var _a16, _b16, _c, _d, _e;
|
|
2250
4013
|
const warnings = [];
|
|
2251
4014
|
if (aspectRatio != null) {
|
|
2252
4015
|
warnings.push({
|
|
2253
|
-
type: "unsupported
|
|
2254
|
-
|
|
4016
|
+
type: "unsupported",
|
|
4017
|
+
feature: "aspectRatio",
|
|
2255
4018
|
details: "This model does not support aspect ratio. Use `size` instead."
|
|
2256
4019
|
});
|
|
2257
4020
|
}
|
|
2258
4021
|
if (seed != null) {
|
|
2259
|
-
warnings.push({ type: "unsupported
|
|
4022
|
+
warnings.push({ type: "unsupported", feature: "seed" });
|
|
4023
|
+
}
|
|
4024
|
+
const currentDate = (_c = (_b16 = (_a16 = this.config._internal) == null ? void 0 : _a16.currentDate) == null ? void 0 : _b16.call(_a16)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4025
|
+
const args = this.getArgs(providerOptions);
|
|
4026
|
+
if (files != null && files.length > 0) {
|
|
4027
|
+
const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
|
|
4028
|
+
url: this.config.url({
|
|
4029
|
+
path: "/images/edits",
|
|
4030
|
+
modelId: this.modelId
|
|
4031
|
+
}),
|
|
4032
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
4033
|
+
formData: convertToFormData({
|
|
4034
|
+
model: this.modelId,
|
|
4035
|
+
prompt,
|
|
4036
|
+
image: await Promise.all(files.map((file) => fileToBlob(file))),
|
|
4037
|
+
mask: mask != null ? await fileToBlob(mask) : void 0,
|
|
4038
|
+
n,
|
|
4039
|
+
size,
|
|
4040
|
+
...args
|
|
4041
|
+
}),
|
|
4042
|
+
failedResponseHandler: createJsonErrorResponseHandler(
|
|
4043
|
+
(_d = this.config.errorStructure) != null ? _d : defaultOpenAICompatibleErrorStructure
|
|
4044
|
+
),
|
|
4045
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
4046
|
+
openaiCompatibleImageResponseSchema
|
|
4047
|
+
),
|
|
4048
|
+
abortSignal,
|
|
4049
|
+
fetch: this.config.fetch
|
|
4050
|
+
});
|
|
4051
|
+
return {
|
|
4052
|
+
images: response2.data.map((item) => item.b64_json),
|
|
4053
|
+
warnings,
|
|
4054
|
+
response: {
|
|
4055
|
+
timestamp: currentDate,
|
|
4056
|
+
modelId: this.modelId,
|
|
4057
|
+
headers: responseHeaders2
|
|
4058
|
+
}
|
|
4059
|
+
};
|
|
2260
4060
|
}
|
|
2261
|
-
const currentDate = (_c = (_b15 = (_a15 = this.config._internal) == null ? void 0 : _a15.currentDate) == null ? void 0 : _b15.call(_a15)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2262
4061
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
2263
4062
|
url: this.config.url({
|
|
2264
4063
|
path: "/images/generations",
|
|
@@ -2270,7 +4069,7 @@ var OpenAICompatibleImageModel = class {
|
|
|
2270
4069
|
prompt,
|
|
2271
4070
|
n,
|
|
2272
4071
|
size,
|
|
2273
|
-
...
|
|
4072
|
+
...args,
|
|
2274
4073
|
response_format: "b64_json"
|
|
2275
4074
|
},
|
|
2276
4075
|
failedResponseHandler: createJsonErrorResponseHandler(
|
|
@@ -2296,7 +4095,14 @@ var OpenAICompatibleImageModel = class {
|
|
|
2296
4095
|
var openaiCompatibleImageResponseSchema = import_v48.z.object({
|
|
2297
4096
|
data: import_v48.z.array(import_v48.z.object({ b64_json: import_v48.z.string() }))
|
|
2298
4097
|
});
|
|
2299
|
-
|
|
4098
|
+
async function fileToBlob(file) {
|
|
4099
|
+
if (file.type === "url") {
|
|
4100
|
+
return downloadBlob(file.url);
|
|
4101
|
+
}
|
|
4102
|
+
const data = file.data instanceof Uint8Array ? file.data : convertBase64ToUint8Array(file.data);
|
|
4103
|
+
return new Blob([data], { type: file.mediaType });
|
|
4104
|
+
}
|
|
4105
|
+
var VERSION2 = true ? "2.0.41" : "0.0.0-test";
|
|
2300
4106
|
function createOpenAICompatible(options) {
|
|
2301
4107
|
const baseURL = withoutTrailingSlash(options.baseURL);
|
|
2302
4108
|
const providerName = options.name;
|
|
@@ -2321,7 +4127,9 @@ function createOpenAICompatible(options) {
|
|
|
2321
4127
|
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
|
|
2322
4128
|
...getCommonModelConfig("chat"),
|
|
2323
4129
|
includeUsage: options.includeUsage,
|
|
2324
|
-
supportsStructuredOutputs: options.supportsStructuredOutputs
|
|
4130
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
4131
|
+
transformRequestBody: options.transformRequestBody,
|
|
4132
|
+
metadataExtractor: options.metadataExtractor
|
|
2325
4133
|
});
|
|
2326
4134
|
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
2327
4135
|
...getCommonModelConfig("completion"),
|
|
@@ -2332,9 +4140,11 @@ function createOpenAICompatible(options) {
|
|
|
2332
4140
|
});
|
|
2333
4141
|
const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
|
|
2334
4142
|
const provider = (modelId) => createLanguageModel(modelId);
|
|
4143
|
+
provider.specificationVersion = "v3";
|
|
2335
4144
|
provider.languageModel = createLanguageModel;
|
|
2336
4145
|
provider.chatModel = createChatModel;
|
|
2337
4146
|
provider.completionModel = createCompletionModel;
|
|
4147
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
2338
4148
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
2339
4149
|
provider.imageModel = createImageModel;
|
|
2340
4150
|
return provider;
|