@ai-sdk/provider-utils 4.0.0-beta.14 → 4.0.0-beta.16
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/CHANGELOG.md +12 -0
- package/dist/index.d.mts +49 -81
- package/dist/index.d.ts +49 -81
- package/dist/index.js +1672 -1718
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1676 -1717
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -34,7 +34,6 @@ __export(src_exports, {
|
|
34
34
|
EventSourceParserStream: () => import_stream2.EventSourceParserStream,
|
35
35
|
VERSION: () => VERSION,
|
36
36
|
asSchema: () => asSchema,
|
37
|
-
asValidator: () => asValidator,
|
38
37
|
combineHeaders: () => combineHeaders,
|
39
38
|
convertAsyncIteratorToReadableStream: () => convertAsyncIteratorToReadableStream,
|
40
39
|
convertBase64ToUint8Array: () => convertBase64ToUint8Array,
|
@@ -61,10 +60,8 @@ __export(src_exports, {
|
|
61
60
|
isAbortError: () => isAbortError,
|
62
61
|
isParsableJson: () => isParsableJson,
|
63
62
|
isUrlSupported: () => isUrlSupported,
|
64
|
-
isValidator: () => isValidator,
|
65
63
|
jsonSchema: () => jsonSchema,
|
66
64
|
lazySchema: () => lazySchema,
|
67
|
-
lazyValidator: () => lazyValidator,
|
68
65
|
loadApiKey: () => loadApiKey,
|
69
66
|
loadOptionalSetting: () => loadOptionalSetting,
|
70
67
|
loadSetting: () => loadSetting,
|
@@ -79,10 +76,8 @@ __export(src_exports, {
|
|
79
76
|
resolve: () => resolve,
|
80
77
|
safeParseJSON: () => safeParseJSON,
|
81
78
|
safeValidateTypes: () => safeValidateTypes,
|
82
|
-
standardSchemaValidator: () => standardSchemaValidator,
|
83
79
|
tool: () => tool,
|
84
80
|
validateTypes: () => validateTypes,
|
85
|
-
validator: () => validator,
|
86
81
|
withUserAgentSuffix: () => withUserAgentSuffix,
|
87
82
|
withoutTrailingSlash: () => withoutTrailingSlash,
|
88
83
|
zodSchema: () => zodSchema
|
@@ -285,7 +280,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
285
280
|
}
|
286
281
|
|
287
282
|
// src/version.ts
|
288
|
-
var VERSION = true ? "4.0.0-beta.
|
283
|
+
var VERSION = true ? "4.0.0-beta.16" : "0.0.0-test";
|
289
284
|
|
290
285
|
// src/get-from-api.ts
|
291
286
|
var getOriginalFetch = () => globalThis.fetch;
|
@@ -562,1861 +557,1825 @@ function secureJsonParse(text) {
|
|
562
557
|
// src/validate-types.ts
|
563
558
|
var import_provider7 = require("@ai-sdk/provider");
|
564
559
|
|
565
|
-
// src/
|
560
|
+
// src/schema.ts
|
566
561
|
var import_provider6 = require("@ai-sdk/provider");
|
567
|
-
var
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
562
|
+
var z4 = __toESM(require("zod/v4"));
|
563
|
+
|
564
|
+
// src/to-json-schema/arktype-to-json-schema.ts
|
565
|
+
var arktypeToJsonSchema = (schema) => () => {
|
566
|
+
return schema.toJsonSchema();
|
567
|
+
};
|
568
|
+
|
569
|
+
// src/to-json-schema/effect-to-json-schema.ts
|
570
|
+
var effectToJsonSchema = (schema) => async () => {
|
571
|
+
try {
|
572
|
+
const { JSONSchema } = await import("effect");
|
573
|
+
return JSONSchema.make(schema);
|
574
|
+
} catch (e) {
|
575
|
+
throw new Error(`Failed to import module 'effect'`);
|
576
|
+
}
|
577
|
+
};
|
578
|
+
|
579
|
+
// src/to-json-schema/valibot-to-json-schema.ts
|
580
|
+
var valibotToJsonSchema = (schema) => {
|
581
|
+
return async () => {
|
582
|
+
try {
|
583
|
+
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
584
|
+
return toJsonSchema(schema);
|
585
|
+
} catch (e) {
|
586
|
+
throw new Error(`Failed to import module '@valibot/to-json-schema'`);
|
579
587
|
}
|
580
|
-
return validator2;
|
581
588
|
};
|
582
|
-
}
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
589
|
+
};
|
590
|
+
|
591
|
+
// src/to-json-schema/zod3-to-json-schema/options.ts
|
592
|
+
var ignoreOverride = Symbol(
|
593
|
+
"Let zodToJsonSchema decide on which parser to use"
|
594
|
+
);
|
595
|
+
var defaultOptions = {
|
596
|
+
name: void 0,
|
597
|
+
$refStrategy: "root",
|
598
|
+
basePath: ["#"],
|
599
|
+
effectStrategy: "input",
|
600
|
+
pipeStrategy: "all",
|
601
|
+
dateStrategy: "format:date-time",
|
602
|
+
mapStrategy: "entries",
|
603
|
+
removeAdditionalStrategy: "passthrough",
|
604
|
+
allowedAdditionalProperties: true,
|
605
|
+
rejectedAdditionalProperties: false,
|
606
|
+
definitionPath: "definitions",
|
607
|
+
strictUnions: false,
|
608
|
+
definitions: {},
|
609
|
+
errorMessages: false,
|
610
|
+
patternStrategy: "escape",
|
611
|
+
applyRegexFlags: false,
|
612
|
+
emailStrategy: "format:email",
|
613
|
+
base64Strategy: "contentEncoding:base64",
|
614
|
+
nameStrategy: "ref"
|
615
|
+
};
|
616
|
+
var getDefaultOptions = (options) => typeof options === "string" ? {
|
617
|
+
...defaultOptions,
|
618
|
+
name: options
|
619
|
+
} : {
|
620
|
+
...defaultOptions,
|
621
|
+
...options
|
622
|
+
};
|
623
|
+
|
624
|
+
// src/to-json-schema/zod3-to-json-schema/select-parser.ts
|
625
|
+
var import_v33 = require("zod/v3");
|
626
|
+
|
627
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/any.ts
|
628
|
+
function parseAnyDef() {
|
629
|
+
return {};
|
597
630
|
}
|
598
631
|
|
599
|
-
// src/
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
632
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/array.ts
|
633
|
+
var import_v3 = require("zod/v3");
|
634
|
+
function parseArrayDef(def, refs) {
|
635
|
+
var _a, _b, _c;
|
636
|
+
const res = {
|
637
|
+
type: "array"
|
638
|
+
};
|
639
|
+
if (((_a = def.type) == null ? void 0 : _a._def) && ((_c = (_b = def.type) == null ? void 0 : _b._def) == null ? void 0 : _c.typeName) !== import_v3.ZodFirstPartyTypeKind.ZodAny) {
|
640
|
+
res.items = parseDef(def.type._def, {
|
641
|
+
...refs,
|
642
|
+
currentPath: [...refs.currentPath, "items"]
|
643
|
+
});
|
607
644
|
}
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
}) {
|
614
|
-
const validator2 = asValidator(schema);
|
615
|
-
try {
|
616
|
-
if (validator2.validate == null) {
|
617
|
-
return { success: true, value, rawValue: value };
|
618
|
-
}
|
619
|
-
const result = await validator2.validate(value);
|
620
|
-
if (result.success) {
|
621
|
-
return { success: true, value: result.value, rawValue: value };
|
622
|
-
}
|
623
|
-
return {
|
624
|
-
success: false,
|
625
|
-
error: import_provider7.TypeValidationError.wrap({ value, cause: result.error }),
|
626
|
-
rawValue: value
|
627
|
-
};
|
628
|
-
} catch (error) {
|
629
|
-
return {
|
630
|
-
success: false,
|
631
|
-
error: import_provider7.TypeValidationError.wrap({ value, cause: error }),
|
632
|
-
rawValue: value
|
633
|
-
};
|
645
|
+
if (def.minLength) {
|
646
|
+
res.minItems = def.minLength.value;
|
647
|
+
}
|
648
|
+
if (def.maxLength) {
|
649
|
+
res.maxItems = def.maxLength.value;
|
634
650
|
}
|
651
|
+
if (def.exactLength) {
|
652
|
+
res.minItems = def.exactLength.value;
|
653
|
+
res.maxItems = def.exactLength.value;
|
654
|
+
}
|
655
|
+
return res;
|
635
656
|
}
|
636
657
|
|
637
|
-
// src/
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
658
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts
|
659
|
+
function parseBigintDef(def) {
|
660
|
+
const res = {
|
661
|
+
type: "integer",
|
662
|
+
format: "int64"
|
663
|
+
};
|
664
|
+
if (!def.checks) return res;
|
665
|
+
for (const check of def.checks) {
|
666
|
+
switch (check.kind) {
|
667
|
+
case "min":
|
668
|
+
if (check.inclusive) {
|
669
|
+
res.minimum = check.value;
|
670
|
+
} else {
|
671
|
+
res.exclusiveMinimum = check.value;
|
672
|
+
}
|
673
|
+
break;
|
674
|
+
case "max":
|
675
|
+
if (check.inclusive) {
|
676
|
+
res.maximum = check.value;
|
677
|
+
} else {
|
678
|
+
res.exclusiveMaximum = check.value;
|
679
|
+
}
|
680
|
+
break;
|
681
|
+
case "multipleOf":
|
682
|
+
res.multipleOf = check.value;
|
683
|
+
break;
|
651
684
|
}
|
652
|
-
throw new import_provider8.JSONParseError({ text, cause: error });
|
653
685
|
}
|
686
|
+
return res;
|
654
687
|
}
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
688
|
+
|
689
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/boolean.ts
|
690
|
+
function parseBooleanDef() {
|
691
|
+
return { type: "boolean" };
|
692
|
+
}
|
693
|
+
|
694
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/branded.ts
|
695
|
+
function parseBrandedDef(_def, refs) {
|
696
|
+
return parseDef(_def.type._def, refs);
|
697
|
+
}
|
698
|
+
|
699
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/catch.ts
|
700
|
+
var parseCatchDef = (def, refs) => {
|
701
|
+
return parseDef(def.innerType._def, refs);
|
702
|
+
};
|
703
|
+
|
704
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/date.ts
|
705
|
+
function parseDateDef(def, refs, overrideDateStrategy) {
|
706
|
+
const strategy = overrideDateStrategy != null ? overrideDateStrategy : refs.dateStrategy;
|
707
|
+
if (Array.isArray(strategy)) {
|
666
708
|
return {
|
667
|
-
|
668
|
-
error: import_provider8.JSONParseError.isInstance(error) ? error : new import_provider8.JSONParseError({ text, cause: error }),
|
669
|
-
rawValue: void 0
|
709
|
+
anyOf: strategy.map((item, i) => parseDateDef(def, refs, item))
|
670
710
|
};
|
671
711
|
}
|
712
|
+
switch (strategy) {
|
713
|
+
case "string":
|
714
|
+
case "format:date-time":
|
715
|
+
return {
|
716
|
+
type: "string",
|
717
|
+
format: "date-time"
|
718
|
+
};
|
719
|
+
case "format:date":
|
720
|
+
return {
|
721
|
+
type: "string",
|
722
|
+
format: "date"
|
723
|
+
};
|
724
|
+
case "integer":
|
725
|
+
return integerDateParser(def);
|
726
|
+
}
|
672
727
|
}
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
}
|
678
|
-
|
728
|
+
var integerDateParser = (def) => {
|
729
|
+
const res = {
|
730
|
+
type: "integer",
|
731
|
+
format: "unix-time"
|
732
|
+
};
|
733
|
+
for (const check of def.checks) {
|
734
|
+
switch (check.kind) {
|
735
|
+
case "min":
|
736
|
+
res.minimum = check.value;
|
737
|
+
break;
|
738
|
+
case "max":
|
739
|
+
res.maximum = check.value;
|
740
|
+
break;
|
741
|
+
}
|
679
742
|
}
|
743
|
+
return res;
|
744
|
+
};
|
745
|
+
|
746
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/default.ts
|
747
|
+
function parseDefaultDef(_def, refs) {
|
748
|
+
return {
|
749
|
+
...parseDef(_def.innerType._def, refs),
|
750
|
+
default: _def.defaultValue()
|
751
|
+
};
|
680
752
|
}
|
681
753
|
|
682
|
-
// src/
|
683
|
-
|
684
|
-
|
685
|
-
stream,
|
686
|
-
schema
|
687
|
-
}) {
|
688
|
-
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough(
|
689
|
-
new TransformStream({
|
690
|
-
async transform({ data }, controller) {
|
691
|
-
if (data === "[DONE]") {
|
692
|
-
return;
|
693
|
-
}
|
694
|
-
controller.enqueue(await safeParseJSON({ text: data, schema }));
|
695
|
-
}
|
696
|
-
})
|
697
|
-
);
|
754
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/effects.ts
|
755
|
+
function parseEffectsDef(_def, refs) {
|
756
|
+
return refs.effectStrategy === "input" ? parseDef(_def.schema._def, refs) : parseAnyDef();
|
698
757
|
}
|
699
758
|
|
700
|
-
// src/
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
}) {
|
707
|
-
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
708
|
-
return void 0;
|
709
|
-
}
|
710
|
-
const parsedProviderOptions = await safeValidateTypes({
|
711
|
-
value: providerOptions[provider],
|
712
|
-
schema
|
713
|
-
});
|
714
|
-
if (!parsedProviderOptions.success) {
|
715
|
-
throw new import_provider9.InvalidArgumentError({
|
716
|
-
argument: "providerOptions",
|
717
|
-
message: `invalid ${provider} provider options`,
|
718
|
-
cause: parsedProviderOptions.error
|
719
|
-
});
|
720
|
-
}
|
721
|
-
return parsedProviderOptions.value;
|
759
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/enum.ts
|
760
|
+
function parseEnumDef(def) {
|
761
|
+
return {
|
762
|
+
type: "string",
|
763
|
+
enum: Array.from(def.values)
|
764
|
+
};
|
722
765
|
}
|
723
766
|
|
724
|
-
// src/
|
725
|
-
var
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
});
|
750
|
-
var postFormDataToApi = async ({
|
751
|
-
url,
|
752
|
-
headers,
|
753
|
-
formData,
|
754
|
-
failedResponseHandler,
|
755
|
-
successfulResponseHandler,
|
756
|
-
abortSignal,
|
757
|
-
fetch
|
758
|
-
}) => postToApi({
|
759
|
-
url,
|
760
|
-
headers,
|
761
|
-
body: {
|
762
|
-
content: formData,
|
763
|
-
values: Object.fromEntries(formData.entries())
|
764
|
-
},
|
765
|
-
failedResponseHandler,
|
766
|
-
successfulResponseHandler,
|
767
|
-
abortSignal,
|
768
|
-
fetch
|
769
|
-
});
|
770
|
-
var postToApi = async ({
|
771
|
-
url,
|
772
|
-
headers = {},
|
773
|
-
body,
|
774
|
-
successfulResponseHandler,
|
775
|
-
failedResponseHandler,
|
776
|
-
abortSignal,
|
777
|
-
fetch = getOriginalFetch2()
|
778
|
-
}) => {
|
779
|
-
try {
|
780
|
-
const response = await fetch(url, {
|
781
|
-
method: "POST",
|
782
|
-
headers: withUserAgentSuffix(
|
783
|
-
headers,
|
784
|
-
`ai-sdk/provider-utils/${VERSION}`,
|
785
|
-
getRuntimeEnvironmentUserAgent()
|
786
|
-
),
|
787
|
-
body: body.content,
|
788
|
-
signal: abortSignal
|
789
|
-
});
|
790
|
-
const responseHeaders = extractResponseHeaders(response);
|
791
|
-
if (!response.ok) {
|
792
|
-
let errorInformation;
|
793
|
-
try {
|
794
|
-
errorInformation = await failedResponseHandler({
|
795
|
-
response,
|
796
|
-
url,
|
797
|
-
requestBodyValues: body.values
|
798
|
-
});
|
799
|
-
} catch (error) {
|
800
|
-
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
801
|
-
throw error;
|
802
|
-
}
|
803
|
-
throw new import_provider10.APICallError({
|
804
|
-
message: "Failed to process error response",
|
805
|
-
cause: error,
|
806
|
-
statusCode: response.status,
|
807
|
-
url,
|
808
|
-
responseHeaders,
|
809
|
-
requestBodyValues: body.values
|
810
|
-
});
|
811
|
-
}
|
812
|
-
throw errorInformation.value;
|
813
|
-
}
|
814
|
-
try {
|
815
|
-
return await successfulResponseHandler({
|
816
|
-
response,
|
817
|
-
url,
|
818
|
-
requestBodyValues: body.values
|
819
|
-
});
|
820
|
-
} catch (error) {
|
821
|
-
if (error instanceof Error) {
|
822
|
-
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
823
|
-
throw error;
|
824
|
-
}
|
767
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts
|
768
|
+
var isJsonSchema7AllOfType = (type) => {
|
769
|
+
if ("type" in type && type.type === "string") return false;
|
770
|
+
return "allOf" in type;
|
771
|
+
};
|
772
|
+
function parseIntersectionDef(def, refs) {
|
773
|
+
const allOf = [
|
774
|
+
parseDef(def.left._def, {
|
775
|
+
...refs,
|
776
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
777
|
+
}),
|
778
|
+
parseDef(def.right._def, {
|
779
|
+
...refs,
|
780
|
+
currentPath: [...refs.currentPath, "allOf", "1"]
|
781
|
+
})
|
782
|
+
].filter((x) => !!x);
|
783
|
+
const mergedAllOf = [];
|
784
|
+
allOf.forEach((schema) => {
|
785
|
+
if (isJsonSchema7AllOfType(schema)) {
|
786
|
+
mergedAllOf.push(...schema.allOf);
|
787
|
+
} else {
|
788
|
+
let nestedSchema = schema;
|
789
|
+
if ("additionalProperties" in schema && schema.additionalProperties === false) {
|
790
|
+
const { additionalProperties, ...rest } = schema;
|
791
|
+
nestedSchema = rest;
|
825
792
|
}
|
826
|
-
|
827
|
-
message: "Failed to process successful response",
|
828
|
-
cause: error,
|
829
|
-
statusCode: response.status,
|
830
|
-
url,
|
831
|
-
responseHeaders,
|
832
|
-
requestBodyValues: body.values
|
833
|
-
});
|
793
|
+
mergedAllOf.push(nestedSchema);
|
834
794
|
}
|
835
|
-
} catch (error) {
|
836
|
-
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
837
|
-
}
|
838
|
-
};
|
839
|
-
|
840
|
-
// src/types/tool.ts
|
841
|
-
function tool(tool2) {
|
842
|
-
return tool2;
|
843
|
-
}
|
844
|
-
function dynamicTool(tool2) {
|
845
|
-
return { ...tool2, type: "dynamic" };
|
846
|
-
}
|
847
|
-
|
848
|
-
// src/provider-defined-tool-factory.ts
|
849
|
-
function createProviderDefinedToolFactory({
|
850
|
-
id,
|
851
|
-
name,
|
852
|
-
inputSchema
|
853
|
-
}) {
|
854
|
-
return ({
|
855
|
-
execute,
|
856
|
-
outputSchema,
|
857
|
-
needsApproval,
|
858
|
-
toModelOutput,
|
859
|
-
onInputStart,
|
860
|
-
onInputDelta,
|
861
|
-
onInputAvailable,
|
862
|
-
...args
|
863
|
-
}) => tool({
|
864
|
-
type: "provider-defined",
|
865
|
-
id,
|
866
|
-
name,
|
867
|
-
args,
|
868
|
-
inputSchema,
|
869
|
-
outputSchema,
|
870
|
-
execute,
|
871
|
-
needsApproval,
|
872
|
-
toModelOutput,
|
873
|
-
onInputStart,
|
874
|
-
onInputDelta,
|
875
|
-
onInputAvailable
|
876
|
-
});
|
877
|
-
}
|
878
|
-
function createProviderDefinedToolFactoryWithOutputSchema({
|
879
|
-
id,
|
880
|
-
name,
|
881
|
-
inputSchema,
|
882
|
-
outputSchema
|
883
|
-
}) {
|
884
|
-
return ({
|
885
|
-
execute,
|
886
|
-
needsApproval,
|
887
|
-
toModelOutput,
|
888
|
-
onInputStart,
|
889
|
-
onInputDelta,
|
890
|
-
onInputAvailable,
|
891
|
-
...args
|
892
|
-
}) => tool({
|
893
|
-
type: "provider-defined",
|
894
|
-
id,
|
895
|
-
name,
|
896
|
-
args,
|
897
|
-
inputSchema,
|
898
|
-
outputSchema,
|
899
|
-
execute,
|
900
|
-
needsApproval,
|
901
|
-
toModelOutput,
|
902
|
-
onInputStart,
|
903
|
-
onInputDelta,
|
904
|
-
onInputAvailable
|
905
795
|
});
|
796
|
+
return mergedAllOf.length ? { allOf: mergedAllOf } : void 0;
|
906
797
|
}
|
907
798
|
|
908
|
-
// src/
|
909
|
-
|
910
|
-
|
911
|
-
|
799
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/literal.ts
|
800
|
+
function parseLiteralDef(def) {
|
801
|
+
const parsedType = typeof def.value;
|
802
|
+
if (parsedType !== "bigint" && parsedType !== "number" && parsedType !== "boolean" && parsedType !== "string") {
|
803
|
+
return {
|
804
|
+
type: Array.isArray(def.value) ? "array" : "object"
|
805
|
+
};
|
912
806
|
}
|
913
|
-
return
|
807
|
+
return {
|
808
|
+
type: parsedType === "bigint" ? "integer" : parsedType,
|
809
|
+
const: def.value
|
810
|
+
};
|
914
811
|
}
|
915
812
|
|
916
|
-
// src/
|
917
|
-
var
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
}
|
813
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/record.ts
|
814
|
+
var import_v32 = require("zod/v3");
|
815
|
+
|
816
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/string.ts
|
817
|
+
var emojiRegex = void 0;
|
818
|
+
var zodPatterns = {
|
819
|
+
/**
|
820
|
+
* `c` was changed to `[cC]` to replicate /i flag
|
821
|
+
*/
|
822
|
+
cuid: /^[cC][^\s-]{8,}$/,
|
823
|
+
cuid2: /^[0-9a-z]+$/,
|
824
|
+
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
825
|
+
/**
|
826
|
+
* `a-z` was added to replicate /i flag
|
827
|
+
*/
|
828
|
+
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
829
|
+
/**
|
830
|
+
* Constructed a valid Unicode RegExp
|
831
|
+
*
|
832
|
+
* Lazily instantiate since this type of regex isn't supported
|
833
|
+
* in all envs (e.g. React Native).
|
834
|
+
*
|
835
|
+
* See:
|
836
|
+
* https://github.com/colinhacks/zod/issues/2433
|
837
|
+
* Fix in Zod:
|
838
|
+
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
839
|
+
*/
|
840
|
+
emoji: () => {
|
841
|
+
if (emojiRegex === void 0) {
|
842
|
+
emojiRegex = RegExp(
|
843
|
+
"^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
|
844
|
+
"u"
|
845
|
+
);
|
846
|
+
}
|
847
|
+
return emojiRegex;
|
848
|
+
},
|
849
|
+
/**
|
850
|
+
* Unused
|
851
|
+
*/
|
852
|
+
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}$/,
|
853
|
+
/**
|
854
|
+
* Unused
|
855
|
+
*/
|
856
|
+
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])$/,
|
857
|
+
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])$/,
|
858
|
+
/**
|
859
|
+
* Unused
|
860
|
+
*/
|
861
|
+
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})))$/,
|
862
|
+
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])$/,
|
863
|
+
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
864
|
+
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
865
|
+
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
866
|
+
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
971
867
|
};
|
972
|
-
|
973
|
-
const
|
974
|
-
|
975
|
-
throw new import_provider11.EmptyResponseBodyError({});
|
976
|
-
}
|
977
|
-
return {
|
978
|
-
responseHeaders,
|
979
|
-
value: parseJsonEventStream({
|
980
|
-
stream: response.body,
|
981
|
-
schema: chunkSchema
|
982
|
-
})
|
868
|
+
function parseStringDef(def, refs) {
|
869
|
+
const res = {
|
870
|
+
type: "string"
|
983
871
|
};
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
} else {
|
1005
|
-
buffer += chunkText;
|
872
|
+
if (def.checks) {
|
873
|
+
for (const check of def.checks) {
|
874
|
+
switch (check.kind) {
|
875
|
+
case "min":
|
876
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
877
|
+
break;
|
878
|
+
case "max":
|
879
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
880
|
+
break;
|
881
|
+
case "email":
|
882
|
+
switch (refs.emailStrategy) {
|
883
|
+
case "format:email":
|
884
|
+
addFormat(res, "email", check.message, refs);
|
885
|
+
break;
|
886
|
+
case "format:idn-email":
|
887
|
+
addFormat(res, "idn-email", check.message, refs);
|
888
|
+
break;
|
889
|
+
case "pattern:zod":
|
890
|
+
addPattern(res, zodPatterns.email, check.message, refs);
|
891
|
+
break;
|
1006
892
|
}
|
893
|
+
break;
|
894
|
+
case "url":
|
895
|
+
addFormat(res, "uri", check.message, refs);
|
896
|
+
break;
|
897
|
+
case "uuid":
|
898
|
+
addFormat(res, "uuid", check.message, refs);
|
899
|
+
break;
|
900
|
+
case "regex":
|
901
|
+
addPattern(res, check.regex, check.message, refs);
|
902
|
+
break;
|
903
|
+
case "cuid":
|
904
|
+
addPattern(res, zodPatterns.cuid, check.message, refs);
|
905
|
+
break;
|
906
|
+
case "cuid2":
|
907
|
+
addPattern(res, zodPatterns.cuid2, check.message, refs);
|
908
|
+
break;
|
909
|
+
case "startsWith":
|
910
|
+
addPattern(
|
911
|
+
res,
|
912
|
+
RegExp(`^${escapeLiteralCheckValue(check.value, refs)}`),
|
913
|
+
check.message,
|
914
|
+
refs
|
915
|
+
);
|
916
|
+
break;
|
917
|
+
case "endsWith":
|
918
|
+
addPattern(
|
919
|
+
res,
|
920
|
+
RegExp(`${escapeLiteralCheckValue(check.value, refs)}$`),
|
921
|
+
check.message,
|
922
|
+
refs
|
923
|
+
);
|
924
|
+
break;
|
925
|
+
case "datetime":
|
926
|
+
addFormat(res, "date-time", check.message, refs);
|
927
|
+
break;
|
928
|
+
case "date":
|
929
|
+
addFormat(res, "date", check.message, refs);
|
930
|
+
break;
|
931
|
+
case "time":
|
932
|
+
addFormat(res, "time", check.message, refs);
|
933
|
+
break;
|
934
|
+
case "duration":
|
935
|
+
addFormat(res, "duration", check.message, refs);
|
936
|
+
break;
|
937
|
+
case "length":
|
938
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
939
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
940
|
+
break;
|
941
|
+
case "includes": {
|
942
|
+
addPattern(
|
943
|
+
res,
|
944
|
+
RegExp(escapeLiteralCheckValue(check.value, refs)),
|
945
|
+
check.message,
|
946
|
+
refs
|
947
|
+
);
|
948
|
+
break;
|
1007
949
|
}
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
}
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
};
|
1066
|
-
|
1067
|
-
|
1068
|
-
const responseBody = await response.text();
|
1069
|
-
return {
|
1070
|
-
responseHeaders,
|
1071
|
-
value: new import_provider11.APICallError({
|
1072
|
-
message: response.statusText,
|
1073
|
-
url,
|
1074
|
-
requestBodyValues,
|
1075
|
-
statusCode: response.status,
|
1076
|
-
responseHeaders,
|
1077
|
-
responseBody
|
1078
|
-
})
|
1079
|
-
};
|
1080
|
-
};
|
1081
|
-
|
1082
|
-
// src/schema.ts
|
1083
|
-
var import_provider12 = require("@ai-sdk/provider");
|
1084
|
-
var z4 = __toESM(require("zod/v4"));
|
1085
|
-
|
1086
|
-
// src/to-json-schema/arktype-to-json-schema.ts
|
1087
|
-
var arktypeToJsonSchema = async (schema) => {
|
1088
|
-
return schema.toJsonSchema();
|
1089
|
-
};
|
1090
|
-
|
1091
|
-
// src/to-json-schema/effect-to-json-schema.ts
|
1092
|
-
var effectToJsonSchema = async (schema) => {
|
1093
|
-
try {
|
1094
|
-
const { JSONSchema } = await import("effect");
|
1095
|
-
return JSONSchema.make(schema);
|
1096
|
-
} catch (e) {
|
1097
|
-
throw new Error(`Failed to import module 'effect'`);
|
1098
|
-
}
|
1099
|
-
};
|
1100
|
-
|
1101
|
-
// src/to-json-schema/valibot-to-json-schema.ts
|
1102
|
-
var valibotToJsonSchema = async (schema) => {
|
1103
|
-
try {
|
1104
|
-
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
1105
|
-
return toJsonSchema(schema);
|
1106
|
-
} catch (e) {
|
1107
|
-
throw new Error(`Failed to import module '@valibot/to-json-schema'`);
|
1108
|
-
}
|
1109
|
-
};
|
1110
|
-
|
1111
|
-
// src/zod-to-json-schema/get-relative-path.ts
|
1112
|
-
var getRelativePath = (pathA, pathB) => {
|
1113
|
-
let i = 0;
|
1114
|
-
for (; i < pathA.length && i < pathB.length; i++) {
|
1115
|
-
if (pathA[i] !== pathB[i]) break;
|
950
|
+
case "ip": {
|
951
|
+
if (check.version !== "v6") {
|
952
|
+
addFormat(res, "ipv4", check.message, refs);
|
953
|
+
}
|
954
|
+
if (check.version !== "v4") {
|
955
|
+
addFormat(res, "ipv6", check.message, refs);
|
956
|
+
}
|
957
|
+
break;
|
958
|
+
}
|
959
|
+
case "base64url":
|
960
|
+
addPattern(res, zodPatterns.base64url, check.message, refs);
|
961
|
+
break;
|
962
|
+
case "jwt":
|
963
|
+
addPattern(res, zodPatterns.jwt, check.message, refs);
|
964
|
+
break;
|
965
|
+
case "cidr": {
|
966
|
+
if (check.version !== "v6") {
|
967
|
+
addPattern(res, zodPatterns.ipv4Cidr, check.message, refs);
|
968
|
+
}
|
969
|
+
if (check.version !== "v4") {
|
970
|
+
addPattern(res, zodPatterns.ipv6Cidr, check.message, refs);
|
971
|
+
}
|
972
|
+
break;
|
973
|
+
}
|
974
|
+
case "emoji":
|
975
|
+
addPattern(res, zodPatterns.emoji(), check.message, refs);
|
976
|
+
break;
|
977
|
+
case "ulid": {
|
978
|
+
addPattern(res, zodPatterns.ulid, check.message, refs);
|
979
|
+
break;
|
980
|
+
}
|
981
|
+
case "base64": {
|
982
|
+
switch (refs.base64Strategy) {
|
983
|
+
case "format:binary": {
|
984
|
+
addFormat(res, "binary", check.message, refs);
|
985
|
+
break;
|
986
|
+
}
|
987
|
+
case "contentEncoding:base64": {
|
988
|
+
res.contentEncoding = "base64";
|
989
|
+
break;
|
990
|
+
}
|
991
|
+
case "pattern:zod": {
|
992
|
+
addPattern(res, zodPatterns.base64, check.message, refs);
|
993
|
+
break;
|
994
|
+
}
|
995
|
+
}
|
996
|
+
break;
|
997
|
+
}
|
998
|
+
case "nanoid": {
|
999
|
+
addPattern(res, zodPatterns.nanoid, check.message, refs);
|
1000
|
+
}
|
1001
|
+
case "toLowerCase":
|
1002
|
+
case "toUpperCase":
|
1003
|
+
case "trim":
|
1004
|
+
break;
|
1005
|
+
default:
|
1006
|
+
/* @__PURE__ */ ((_) => {
|
1007
|
+
})(check);
|
1008
|
+
}
|
1009
|
+
}
|
1116
1010
|
}
|
1117
|
-
return
|
1118
|
-
}
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1011
|
+
return res;
|
1012
|
+
}
|
1013
|
+
function escapeLiteralCheckValue(literal, refs) {
|
1014
|
+
return refs.patternStrategy === "escape" ? escapeNonAlphaNumeric(literal) : literal;
|
1015
|
+
}
|
1016
|
+
var ALPHA_NUMERIC = new Set(
|
1017
|
+
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
1123
1018
|
);
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
allowedAdditionalProperties: true,
|
1134
|
-
rejectedAdditionalProperties: false,
|
1135
|
-
definitionPath: "definitions",
|
1136
|
-
strictUnions: false,
|
1137
|
-
definitions: {},
|
1138
|
-
errorMessages: false,
|
1139
|
-
patternStrategy: "escape",
|
1140
|
-
applyRegexFlags: false,
|
1141
|
-
emailStrategy: "format:email",
|
1142
|
-
base64Strategy: "contentEncoding:base64",
|
1143
|
-
nameStrategy: "ref"
|
1144
|
-
};
|
1145
|
-
var getDefaultOptions = (options) => typeof options === "string" ? {
|
1146
|
-
...defaultOptions,
|
1147
|
-
name: options
|
1148
|
-
} : {
|
1149
|
-
...defaultOptions,
|
1150
|
-
...options
|
1151
|
-
};
|
1152
|
-
|
1153
|
-
// src/zod-to-json-schema/select-parser.ts
|
1154
|
-
var import_v33 = require("zod/v3");
|
1155
|
-
|
1156
|
-
// src/zod-to-json-schema/parsers/any.ts
|
1157
|
-
function parseAnyDef() {
|
1158
|
-
return {};
|
1019
|
+
function escapeNonAlphaNumeric(source) {
|
1020
|
+
let result = "";
|
1021
|
+
for (let i = 0; i < source.length; i++) {
|
1022
|
+
if (!ALPHA_NUMERIC.has(source[i])) {
|
1023
|
+
result += "\\";
|
1024
|
+
}
|
1025
|
+
result += source[i];
|
1026
|
+
}
|
1027
|
+
return result;
|
1159
1028
|
}
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1029
|
+
function addFormat(schema, value, message, refs) {
|
1030
|
+
var _a;
|
1031
|
+
if (schema.format || ((_a = schema.anyOf) == null ? void 0 : _a.some((x) => x.format))) {
|
1032
|
+
if (!schema.anyOf) {
|
1033
|
+
schema.anyOf = [];
|
1034
|
+
}
|
1035
|
+
if (schema.format) {
|
1036
|
+
schema.anyOf.push({
|
1037
|
+
format: schema.format
|
1038
|
+
});
|
1039
|
+
delete schema.format;
|
1040
|
+
}
|
1041
|
+
schema.anyOf.push({
|
1042
|
+
format: value,
|
1043
|
+
...message && refs.errorMessages && { errorMessage: { format: message } }
|
1172
1044
|
});
|
1045
|
+
} else {
|
1046
|
+
schema.format = value;
|
1173
1047
|
}
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
if (
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1048
|
+
}
|
1049
|
+
function addPattern(schema, regex, message, refs) {
|
1050
|
+
var _a;
|
1051
|
+
if (schema.pattern || ((_a = schema.allOf) == null ? void 0 : _a.some((x) => x.pattern))) {
|
1052
|
+
if (!schema.allOf) {
|
1053
|
+
schema.allOf = [];
|
1054
|
+
}
|
1055
|
+
if (schema.pattern) {
|
1056
|
+
schema.allOf.push({
|
1057
|
+
pattern: schema.pattern
|
1058
|
+
});
|
1059
|
+
delete schema.pattern;
|
1060
|
+
}
|
1061
|
+
schema.allOf.push({
|
1062
|
+
pattern: stringifyRegExpWithFlags(regex, refs),
|
1063
|
+
...message && refs.errorMessages && { errorMessage: { pattern: message } }
|
1064
|
+
});
|
1065
|
+
} else {
|
1066
|
+
schema.pattern = stringifyRegExpWithFlags(regex, refs);
|
1183
1067
|
}
|
1184
|
-
return res;
|
1185
1068
|
}
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1069
|
+
function stringifyRegExpWithFlags(regex, refs) {
|
1070
|
+
var _a;
|
1071
|
+
if (!refs.applyRegexFlags || !regex.flags) {
|
1072
|
+
return regex.source;
|
1073
|
+
}
|
1074
|
+
const flags = {
|
1075
|
+
i: regex.flags.includes("i"),
|
1076
|
+
// Case-insensitive
|
1077
|
+
m: regex.flags.includes("m"),
|
1078
|
+
// `^` and `$` matches adjacent to newline characters
|
1079
|
+
s: regex.flags.includes("s")
|
1080
|
+
// `.` matches newlines
|
1192
1081
|
};
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1082
|
+
const source = flags.i ? regex.source.toLowerCase() : regex.source;
|
1083
|
+
let pattern = "";
|
1084
|
+
let isEscaped = false;
|
1085
|
+
let inCharGroup = false;
|
1086
|
+
let inCharRange = false;
|
1087
|
+
for (let i = 0; i < source.length; i++) {
|
1088
|
+
if (isEscaped) {
|
1089
|
+
pattern += source[i];
|
1090
|
+
isEscaped = false;
|
1091
|
+
continue;
|
1092
|
+
}
|
1093
|
+
if (flags.i) {
|
1094
|
+
if (inCharGroup) {
|
1095
|
+
if (source[i].match(/[a-z]/)) {
|
1096
|
+
if (inCharRange) {
|
1097
|
+
pattern += source[i];
|
1098
|
+
pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
|
1099
|
+
inCharRange = false;
|
1100
|
+
} else if (source[i + 1] === "-" && ((_a = source[i + 2]) == null ? void 0 : _a.match(/[a-z]/))) {
|
1101
|
+
pattern += source[i];
|
1102
|
+
inCharRange = true;
|
1103
|
+
} else {
|
1104
|
+
pattern += `${source[i]}${source[i].toUpperCase()}`;
|
1105
|
+
}
|
1106
|
+
continue;
|
1208
1107
|
}
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1108
|
+
} else if (source[i].match(/[a-z]/)) {
|
1109
|
+
pattern += `[${source[i]}${source[i].toUpperCase()}]`;
|
1110
|
+
continue;
|
1111
|
+
}
|
1112
|
+
}
|
1113
|
+
if (flags.m) {
|
1114
|
+
if (source[i] === "^") {
|
1115
|
+
pattern += `(^|(?<=[\r
|
1116
|
+
]))`;
|
1117
|
+
continue;
|
1118
|
+
} else if (source[i] === "$") {
|
1119
|
+
pattern += `($|(?=[\r
|
1120
|
+
]))`;
|
1121
|
+
continue;
|
1122
|
+
}
|
1123
|
+
}
|
1124
|
+
if (flags.s && source[i] === ".") {
|
1125
|
+
pattern += inCharGroup ? `${source[i]}\r
|
1126
|
+
` : `[${source[i]}\r
|
1127
|
+
]`;
|
1128
|
+
continue;
|
1129
|
+
}
|
1130
|
+
pattern += source[i];
|
1131
|
+
if (source[i] === "\\") {
|
1132
|
+
isEscaped = true;
|
1133
|
+
} else if (inCharGroup && source[i] === "]") {
|
1134
|
+
inCharGroup = false;
|
1135
|
+
} else if (!inCharGroup && source[i] === "[") {
|
1136
|
+
inCharGroup = true;
|
1213
1137
|
}
|
1214
1138
|
}
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
}
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
return
|
1139
|
+
try {
|
1140
|
+
new RegExp(pattern);
|
1141
|
+
} catch (e) {
|
1142
|
+
console.warn(
|
1143
|
+
`Could not convert regex pattern at ${refs.currentPath.join(
|
1144
|
+
"/"
|
1145
|
+
)} to a flag-independent form! Falling back to the flag-ignorant source`
|
1146
|
+
);
|
1147
|
+
return regex.source;
|
1148
|
+
}
|
1149
|
+
return pattern;
|
1226
1150
|
}
|
1227
1151
|
|
1228
|
-
// src/
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1152
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/record.ts
|
1153
|
+
function parseRecordDef(def, refs) {
|
1154
|
+
var _a, _b, _c, _d, _e, _f;
|
1155
|
+
const schema = {
|
1156
|
+
type: "object",
|
1157
|
+
additionalProperties: (_a = parseDef(def.valueType._def, {
|
1158
|
+
...refs,
|
1159
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
1160
|
+
})) != null ? _a : refs.allowedAdditionalProperties
|
1161
|
+
};
|
1162
|
+
if (((_b = def.keyType) == null ? void 0 : _b._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
|
1163
|
+
const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
|
1237
1164
|
return {
|
1238
|
-
|
1165
|
+
...schema,
|
1166
|
+
propertyNames: keyType
|
1167
|
+
};
|
1168
|
+
} else if (((_d = def.keyType) == null ? void 0 : _d._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodEnum) {
|
1169
|
+
return {
|
1170
|
+
...schema,
|
1171
|
+
propertyNames: {
|
1172
|
+
enum: def.keyType._def.values
|
1173
|
+
}
|
1174
|
+
};
|
1175
|
+
} else if (((_e = def.keyType) == null ? void 0 : _e._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodBranded && def.keyType._def.type._def.typeName === import_v32.ZodFirstPartyTypeKind.ZodString && ((_f = def.keyType._def.type._def.checks) == null ? void 0 : _f.length)) {
|
1176
|
+
const { type, ...keyType } = parseBrandedDef(
|
1177
|
+
def.keyType._def,
|
1178
|
+
refs
|
1179
|
+
);
|
1180
|
+
return {
|
1181
|
+
...schema,
|
1182
|
+
propertyNames: keyType
|
1239
1183
|
};
|
1240
1184
|
}
|
1241
|
-
|
1242
|
-
case "string":
|
1243
|
-
case "format:date-time":
|
1244
|
-
return {
|
1245
|
-
type: "string",
|
1246
|
-
format: "date-time"
|
1247
|
-
};
|
1248
|
-
case "format:date":
|
1249
|
-
return {
|
1250
|
-
type: "string",
|
1251
|
-
format: "date"
|
1252
|
-
};
|
1253
|
-
case "integer":
|
1254
|
-
return integerDateParser(def);
|
1255
|
-
}
|
1185
|
+
return schema;
|
1256
1186
|
}
|
1257
|
-
var integerDateParser = (def) => {
|
1258
|
-
const res = {
|
1259
|
-
type: "integer",
|
1260
|
-
format: "unix-time"
|
1261
|
-
};
|
1262
|
-
for (const check of def.checks) {
|
1263
|
-
switch (check.kind) {
|
1264
|
-
case "min":
|
1265
|
-
res.minimum = check.value;
|
1266
|
-
break;
|
1267
|
-
case "max":
|
1268
|
-
res.maximum = check.value;
|
1269
|
-
break;
|
1270
|
-
}
|
1271
|
-
}
|
1272
|
-
return res;
|
1273
|
-
};
|
1274
1187
|
|
1275
|
-
// src/
|
1276
|
-
function
|
1188
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/map.ts
|
1189
|
+
function parseMapDef(def, refs) {
|
1190
|
+
if (refs.mapStrategy === "record") {
|
1191
|
+
return parseRecordDef(def, refs);
|
1192
|
+
}
|
1193
|
+
const keys = parseDef(def.keyType._def, {
|
1194
|
+
...refs,
|
1195
|
+
currentPath: [...refs.currentPath, "items", "items", "0"]
|
1196
|
+
}) || parseAnyDef();
|
1197
|
+
const values = parseDef(def.valueType._def, {
|
1198
|
+
...refs,
|
1199
|
+
currentPath: [...refs.currentPath, "items", "items", "1"]
|
1200
|
+
}) || parseAnyDef();
|
1277
1201
|
return {
|
1278
|
-
|
1279
|
-
|
1202
|
+
type: "array",
|
1203
|
+
maxItems: 125,
|
1204
|
+
items: {
|
1205
|
+
type: "array",
|
1206
|
+
items: [keys, values],
|
1207
|
+
minItems: 2,
|
1208
|
+
maxItems: 2
|
1209
|
+
}
|
1210
|
+
};
|
1211
|
+
}
|
1212
|
+
|
1213
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts
|
1214
|
+
function parseNativeEnumDef(def) {
|
1215
|
+
const object = def.values;
|
1216
|
+
const actualKeys = Object.keys(def.values).filter((key) => {
|
1217
|
+
return typeof object[object[key]] !== "number";
|
1218
|
+
});
|
1219
|
+
const actualValues = actualKeys.map((key) => object[key]);
|
1220
|
+
const parsedTypes = Array.from(
|
1221
|
+
new Set(actualValues.map((values) => typeof values))
|
1222
|
+
);
|
1223
|
+
return {
|
1224
|
+
type: parsedTypes.length === 1 ? parsedTypes[0] === "string" ? "string" : "number" : ["string", "number"],
|
1225
|
+
enum: actualValues
|
1280
1226
|
};
|
1281
1227
|
}
|
1282
1228
|
|
1283
|
-
// src/
|
1284
|
-
function
|
1285
|
-
return
|
1229
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/never.ts
|
1230
|
+
function parseNeverDef() {
|
1231
|
+
return { not: parseAnyDef() };
|
1286
1232
|
}
|
1287
1233
|
|
1288
|
-
// src/
|
1289
|
-
function
|
1234
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/null.ts
|
1235
|
+
function parseNullDef() {
|
1290
1236
|
return {
|
1291
|
-
type: "
|
1292
|
-
enum: Array.from(def.values)
|
1237
|
+
type: "null"
|
1293
1238
|
};
|
1294
1239
|
}
|
1295
1240
|
|
1296
|
-
// src/
|
1297
|
-
var
|
1298
|
-
|
1299
|
-
|
1241
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/union.ts
|
1242
|
+
var primitiveMappings = {
|
1243
|
+
ZodString: "string",
|
1244
|
+
ZodNumber: "number",
|
1245
|
+
ZodBigInt: "integer",
|
1246
|
+
ZodBoolean: "boolean",
|
1247
|
+
ZodNull: "null"
|
1300
1248
|
};
|
1301
|
-
function
|
1302
|
-
const
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
...
|
1309
|
-
|
1310
|
-
})
|
1311
|
-
].filter((x) => !!x);
|
1312
|
-
const mergedAllOf = [];
|
1313
|
-
allOf.forEach((schema) => {
|
1314
|
-
if (isJsonSchema7AllOfType(schema)) {
|
1315
|
-
mergedAllOf.push(...schema.allOf);
|
1316
|
-
} else {
|
1317
|
-
let nestedSchema = schema;
|
1318
|
-
if ("additionalProperties" in schema && schema.additionalProperties === false) {
|
1319
|
-
const { additionalProperties, ...rest } = schema;
|
1320
|
-
nestedSchema = rest;
|
1321
|
-
}
|
1322
|
-
mergedAllOf.push(nestedSchema);
|
1323
|
-
}
|
1324
|
-
});
|
1325
|
-
return mergedAllOf.length ? { allOf: mergedAllOf } : void 0;
|
1326
|
-
}
|
1327
|
-
|
1328
|
-
// src/zod-to-json-schema/parsers/literal.ts
|
1329
|
-
function parseLiteralDef(def) {
|
1330
|
-
const parsedType = typeof def.value;
|
1331
|
-
if (parsedType !== "bigint" && parsedType !== "number" && parsedType !== "boolean" && parsedType !== "string") {
|
1249
|
+
function parseUnionDef(def, refs) {
|
1250
|
+
const options = def.options instanceof Map ? Array.from(def.options.values()) : def.options;
|
1251
|
+
if (options.every(
|
1252
|
+
(x) => x._def.typeName in primitiveMappings && (!x._def.checks || !x._def.checks.length)
|
1253
|
+
)) {
|
1254
|
+
const types = options.reduce((types2, x) => {
|
1255
|
+
const type = primitiveMappings[x._def.typeName];
|
1256
|
+
return type && !types2.includes(type) ? [...types2, type] : types2;
|
1257
|
+
}, []);
|
1332
1258
|
return {
|
1333
|
-
type:
|
1259
|
+
type: types.length > 1 ? types : types[0]
|
1334
1260
|
};
|
1335
|
-
}
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
1354
|
-
/**
|
1355
|
-
* `a-z` was added to replicate /i flag
|
1356
|
-
*/
|
1357
|
-
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
1358
|
-
/**
|
1359
|
-
* Constructed a valid Unicode RegExp
|
1360
|
-
*
|
1361
|
-
* Lazily instantiate since this type of regex isn't supported
|
1362
|
-
* in all envs (e.g. React Native).
|
1363
|
-
*
|
1364
|
-
* See:
|
1365
|
-
* https://github.com/colinhacks/zod/issues/2433
|
1366
|
-
* Fix in Zod:
|
1367
|
-
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
1368
|
-
*/
|
1369
|
-
emoji: () => {
|
1370
|
-
if (emojiRegex === void 0) {
|
1371
|
-
emojiRegex = RegExp(
|
1372
|
-
"^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
|
1373
|
-
"u"
|
1374
|
-
);
|
1375
|
-
}
|
1376
|
-
return emojiRegex;
|
1377
|
-
},
|
1378
|
-
/**
|
1379
|
-
* Unused
|
1380
|
-
*/
|
1381
|
-
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}$/,
|
1382
|
-
/**
|
1383
|
-
* Unused
|
1384
|
-
*/
|
1385
|
-
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])$/,
|
1386
|
-
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])$/,
|
1387
|
-
/**
|
1388
|
-
* Unused
|
1389
|
-
*/
|
1390
|
-
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})))$/,
|
1391
|
-
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])$/,
|
1392
|
-
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
1393
|
-
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
1394
|
-
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
1395
|
-
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
1396
|
-
};
|
1397
|
-
function parseStringDef(def, refs) {
|
1398
|
-
const res = {
|
1399
|
-
type: "string"
|
1400
|
-
};
|
1401
|
-
if (def.checks) {
|
1402
|
-
for (const check of def.checks) {
|
1403
|
-
switch (check.kind) {
|
1404
|
-
case "min":
|
1405
|
-
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
1406
|
-
break;
|
1407
|
-
case "max":
|
1408
|
-
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
1409
|
-
break;
|
1410
|
-
case "email":
|
1411
|
-
switch (refs.emailStrategy) {
|
1412
|
-
case "format:email":
|
1413
|
-
addFormat(res, "email", check.message, refs);
|
1414
|
-
break;
|
1415
|
-
case "format:idn-email":
|
1416
|
-
addFormat(res, "idn-email", check.message, refs);
|
1417
|
-
break;
|
1418
|
-
case "pattern:zod":
|
1419
|
-
addPattern(res, zodPatterns.email, check.message, refs);
|
1420
|
-
break;
|
1421
|
-
}
|
1422
|
-
break;
|
1423
|
-
case "url":
|
1424
|
-
addFormat(res, "uri", check.message, refs);
|
1425
|
-
break;
|
1426
|
-
case "uuid":
|
1427
|
-
addFormat(res, "uuid", check.message, refs);
|
1428
|
-
break;
|
1429
|
-
case "regex":
|
1430
|
-
addPattern(res, check.regex, check.message, refs);
|
1431
|
-
break;
|
1432
|
-
case "cuid":
|
1433
|
-
addPattern(res, zodPatterns.cuid, check.message, refs);
|
1434
|
-
break;
|
1435
|
-
case "cuid2":
|
1436
|
-
addPattern(res, zodPatterns.cuid2, check.message, refs);
|
1437
|
-
break;
|
1438
|
-
case "startsWith":
|
1439
|
-
addPattern(
|
1440
|
-
res,
|
1441
|
-
RegExp(`^${escapeLiteralCheckValue(check.value, refs)}`),
|
1442
|
-
check.message,
|
1443
|
-
refs
|
1444
|
-
);
|
1445
|
-
break;
|
1446
|
-
case "endsWith":
|
1447
|
-
addPattern(
|
1448
|
-
res,
|
1449
|
-
RegExp(`${escapeLiteralCheckValue(check.value, refs)}$`),
|
1450
|
-
check.message,
|
1451
|
-
refs
|
1452
|
-
);
|
1453
|
-
break;
|
1454
|
-
case "datetime":
|
1455
|
-
addFormat(res, "date-time", check.message, refs);
|
1456
|
-
break;
|
1457
|
-
case "date":
|
1458
|
-
addFormat(res, "date", check.message, refs);
|
1459
|
-
break;
|
1460
|
-
case "time":
|
1461
|
-
addFormat(res, "time", check.message, refs);
|
1462
|
-
break;
|
1463
|
-
case "duration":
|
1464
|
-
addFormat(res, "duration", check.message, refs);
|
1465
|
-
break;
|
1466
|
-
case "length":
|
1467
|
-
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
1468
|
-
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
1469
|
-
break;
|
1470
|
-
case "includes": {
|
1471
|
-
addPattern(
|
1472
|
-
res,
|
1473
|
-
RegExp(escapeLiteralCheckValue(check.value, refs)),
|
1474
|
-
check.message,
|
1475
|
-
refs
|
1476
|
-
);
|
1477
|
-
break;
|
1478
|
-
}
|
1479
|
-
case "ip": {
|
1480
|
-
if (check.version !== "v6") {
|
1481
|
-
addFormat(res, "ipv4", check.message, refs);
|
1482
|
-
}
|
1483
|
-
if (check.version !== "v4") {
|
1484
|
-
addFormat(res, "ipv6", check.message, refs);
|
1485
|
-
}
|
1486
|
-
break;
|
1487
|
-
}
|
1488
|
-
case "base64url":
|
1489
|
-
addPattern(res, zodPatterns.base64url, check.message, refs);
|
1490
|
-
break;
|
1491
|
-
case "jwt":
|
1492
|
-
addPattern(res, zodPatterns.jwt, check.message, refs);
|
1493
|
-
break;
|
1494
|
-
case "cidr": {
|
1495
|
-
if (check.version !== "v6") {
|
1496
|
-
addPattern(res, zodPatterns.ipv4Cidr, check.message, refs);
|
1497
|
-
}
|
1498
|
-
if (check.version !== "v4") {
|
1499
|
-
addPattern(res, zodPatterns.ipv6Cidr, check.message, refs);
|
1500
|
-
}
|
1501
|
-
break;
|
1502
|
-
}
|
1503
|
-
case "emoji":
|
1504
|
-
addPattern(res, zodPatterns.emoji(), check.message, refs);
|
1505
|
-
break;
|
1506
|
-
case "ulid": {
|
1507
|
-
addPattern(res, zodPatterns.ulid, check.message, refs);
|
1508
|
-
break;
|
1509
|
-
}
|
1510
|
-
case "base64": {
|
1511
|
-
switch (refs.base64Strategy) {
|
1512
|
-
case "format:binary": {
|
1513
|
-
addFormat(res, "binary", check.message, refs);
|
1514
|
-
break;
|
1515
|
-
}
|
1516
|
-
case "contentEncoding:base64": {
|
1517
|
-
res.contentEncoding = "base64";
|
1518
|
-
break;
|
1519
|
-
}
|
1520
|
-
case "pattern:zod": {
|
1521
|
-
addPattern(res, zodPatterns.base64, check.message, refs);
|
1522
|
-
break;
|
1523
|
-
}
|
1524
|
-
}
|
1525
|
-
break;
|
1526
|
-
}
|
1527
|
-
case "nanoid": {
|
1528
|
-
addPattern(res, zodPatterns.nanoid, check.message, refs);
|
1261
|
+
} else if (options.every((x) => x._def.typeName === "ZodLiteral" && !x.description)) {
|
1262
|
+
const types = options.reduce(
|
1263
|
+
(acc, x) => {
|
1264
|
+
const type = typeof x._def.value;
|
1265
|
+
switch (type) {
|
1266
|
+
case "string":
|
1267
|
+
case "number":
|
1268
|
+
case "boolean":
|
1269
|
+
return [...acc, type];
|
1270
|
+
case "bigint":
|
1271
|
+
return [...acc, "integer"];
|
1272
|
+
case "object":
|
1273
|
+
if (x._def.value === null) return [...acc, "null"];
|
1274
|
+
case "symbol":
|
1275
|
+
case "undefined":
|
1276
|
+
case "function":
|
1277
|
+
default:
|
1278
|
+
return acc;
|
1529
1279
|
}
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1280
|
+
},
|
1281
|
+
[]
|
1282
|
+
);
|
1283
|
+
if (types.length === options.length) {
|
1284
|
+
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
1285
|
+
return {
|
1286
|
+
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
1287
|
+
enum: options.reduce(
|
1288
|
+
(acc, x) => {
|
1289
|
+
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
1290
|
+
},
|
1291
|
+
[]
|
1292
|
+
)
|
1293
|
+
};
|
1538
1294
|
}
|
1295
|
+
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
|
1296
|
+
return {
|
1297
|
+
type: "string",
|
1298
|
+
enum: options.reduce(
|
1299
|
+
(acc, x) => [
|
1300
|
+
...acc,
|
1301
|
+
...x._def.values.filter((x2) => !acc.includes(x2))
|
1302
|
+
],
|
1303
|
+
[]
|
1304
|
+
)
|
1305
|
+
};
|
1539
1306
|
}
|
1540
|
-
return
|
1307
|
+
return asAnyOf(def, refs);
|
1541
1308
|
}
|
1542
|
-
|
1543
|
-
|
1309
|
+
var asAnyOf = (def, refs) => {
|
1310
|
+
const anyOf = (def.options instanceof Map ? Array.from(def.options.values()) : def.options).map(
|
1311
|
+
(x, i) => parseDef(x._def, {
|
1312
|
+
...refs,
|
1313
|
+
currentPath: [...refs.currentPath, "anyOf", `${i}`]
|
1314
|
+
})
|
1315
|
+
).filter(
|
1316
|
+
(x) => !!x && (!refs.strictUnions || typeof x === "object" && Object.keys(x).length > 0)
|
1317
|
+
);
|
1318
|
+
return anyOf.length ? { anyOf } : void 0;
|
1319
|
+
};
|
1320
|
+
|
1321
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts
|
1322
|
+
function parseNullableDef(def, refs) {
|
1323
|
+
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
|
1324
|
+
def.innerType._def.typeName
|
1325
|
+
) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) {
|
1326
|
+
return {
|
1327
|
+
type: [
|
1328
|
+
primitiveMappings[def.innerType._def.typeName],
|
1329
|
+
"null"
|
1330
|
+
]
|
1331
|
+
};
|
1332
|
+
}
|
1333
|
+
const base = parseDef(def.innerType._def, {
|
1334
|
+
...refs,
|
1335
|
+
currentPath: [...refs.currentPath, "anyOf", "0"]
|
1336
|
+
});
|
1337
|
+
return base && { anyOf: [base, { type: "null" }] };
|
1544
1338
|
}
|
1545
|
-
|
1546
|
-
|
1547
|
-
)
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1339
|
+
|
1340
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/number.ts
|
1341
|
+
function parseNumberDef(def) {
|
1342
|
+
const res = {
|
1343
|
+
type: "number"
|
1344
|
+
};
|
1345
|
+
if (!def.checks) return res;
|
1346
|
+
for (const check of def.checks) {
|
1347
|
+
switch (check.kind) {
|
1348
|
+
case "int":
|
1349
|
+
res.type = "integer";
|
1350
|
+
break;
|
1351
|
+
case "min":
|
1352
|
+
if (check.inclusive) {
|
1353
|
+
res.minimum = check.value;
|
1354
|
+
} else {
|
1355
|
+
res.exclusiveMinimum = check.value;
|
1356
|
+
}
|
1357
|
+
break;
|
1358
|
+
case "max":
|
1359
|
+
if (check.inclusive) {
|
1360
|
+
res.maximum = check.value;
|
1361
|
+
} else {
|
1362
|
+
res.exclusiveMaximum = check.value;
|
1363
|
+
}
|
1364
|
+
break;
|
1365
|
+
case "multipleOf":
|
1366
|
+
res.multipleOf = check.value;
|
1367
|
+
break;
|
1553
1368
|
}
|
1554
|
-
result += source[i];
|
1555
1369
|
}
|
1556
|
-
return
|
1370
|
+
return res;
|
1557
1371
|
}
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1372
|
+
|
1373
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/object.ts
|
1374
|
+
function parseObjectDef(def, refs) {
|
1375
|
+
const result = {
|
1376
|
+
type: "object",
|
1377
|
+
properties: {}
|
1378
|
+
};
|
1379
|
+
const required = [];
|
1380
|
+
const shape = def.shape();
|
1381
|
+
for (const propName in shape) {
|
1382
|
+
let propDef = shape[propName];
|
1383
|
+
if (propDef === void 0 || propDef._def === void 0) {
|
1384
|
+
continue;
|
1563
1385
|
}
|
1564
|
-
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1386
|
+
const propOptional = safeIsOptional(propDef);
|
1387
|
+
const parsedDef = parseDef(propDef._def, {
|
1388
|
+
...refs,
|
1389
|
+
currentPath: [...refs.currentPath, "properties", propName],
|
1390
|
+
propertyPath: [...refs.currentPath, "properties", propName]
|
1391
|
+
});
|
1392
|
+
if (parsedDef === void 0) {
|
1393
|
+
continue;
|
1569
1394
|
}
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1395
|
+
result.properties[propName] = parsedDef;
|
1396
|
+
if (!propOptional) {
|
1397
|
+
required.push(propName);
|
1398
|
+
}
|
1399
|
+
}
|
1400
|
+
if (required.length) {
|
1401
|
+
result.required = required;
|
1402
|
+
}
|
1403
|
+
const additionalProperties = decideAdditionalProperties(def, refs);
|
1404
|
+
if (additionalProperties !== void 0) {
|
1405
|
+
result.additionalProperties = additionalProperties;
|
1406
|
+
}
|
1407
|
+
return result;
|
1408
|
+
}
|
1409
|
+
function decideAdditionalProperties(def, refs) {
|
1410
|
+
if (def.catchall._def.typeName !== "ZodNever") {
|
1411
|
+
return parseDef(def.catchall._def, {
|
1412
|
+
...refs,
|
1413
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
1573
1414
|
});
|
1415
|
+
}
|
1416
|
+
switch (def.unknownKeys) {
|
1417
|
+
case "passthrough":
|
1418
|
+
return refs.allowedAdditionalProperties;
|
1419
|
+
case "strict":
|
1420
|
+
return refs.rejectedAdditionalProperties;
|
1421
|
+
case "strip":
|
1422
|
+
return refs.removeAdditionalStrategy === "strict" ? refs.allowedAdditionalProperties : refs.rejectedAdditionalProperties;
|
1423
|
+
}
|
1424
|
+
}
|
1425
|
+
function safeIsOptional(schema) {
|
1426
|
+
try {
|
1427
|
+
return schema.isOptional();
|
1428
|
+
} catch (e) {
|
1429
|
+
return true;
|
1430
|
+
}
|
1431
|
+
}
|
1432
|
+
|
1433
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/optional.ts
|
1434
|
+
var parseOptionalDef = (def, refs) => {
|
1435
|
+
var _a;
|
1436
|
+
if (refs.currentPath.toString() === ((_a = refs.propertyPath) == null ? void 0 : _a.toString())) {
|
1437
|
+
return parseDef(def.innerType._def, refs);
|
1438
|
+
}
|
1439
|
+
const innerSchema = parseDef(def.innerType._def, {
|
1440
|
+
...refs,
|
1441
|
+
currentPath: [...refs.currentPath, "anyOf", "1"]
|
1442
|
+
});
|
1443
|
+
return innerSchema ? { anyOf: [{ not: parseAnyDef() }, innerSchema] } : parseAnyDef();
|
1444
|
+
};
|
1445
|
+
|
1446
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts
|
1447
|
+
var parsePipelineDef = (def, refs) => {
|
1448
|
+
if (refs.pipeStrategy === "input") {
|
1449
|
+
return parseDef(def.in._def, refs);
|
1450
|
+
} else if (refs.pipeStrategy === "output") {
|
1451
|
+
return parseDef(def.out._def, refs);
|
1452
|
+
}
|
1453
|
+
const a = parseDef(def.in._def, {
|
1454
|
+
...refs,
|
1455
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
1456
|
+
});
|
1457
|
+
const b = parseDef(def.out._def, {
|
1458
|
+
...refs,
|
1459
|
+
currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
|
1460
|
+
});
|
1461
|
+
return {
|
1462
|
+
allOf: [a, b].filter((x) => x !== void 0)
|
1463
|
+
};
|
1464
|
+
};
|
1465
|
+
|
1466
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/promise.ts
|
1467
|
+
function parsePromiseDef(def, refs) {
|
1468
|
+
return parseDef(def.type._def, refs);
|
1469
|
+
}
|
1470
|
+
|
1471
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/set.ts
|
1472
|
+
function parseSetDef(def, refs) {
|
1473
|
+
const items = parseDef(def.valueType._def, {
|
1474
|
+
...refs,
|
1475
|
+
currentPath: [...refs.currentPath, "items"]
|
1476
|
+
});
|
1477
|
+
const schema = {
|
1478
|
+
type: "array",
|
1479
|
+
uniqueItems: true,
|
1480
|
+
items
|
1481
|
+
};
|
1482
|
+
if (def.minSize) {
|
1483
|
+
schema.minItems = def.minSize.value;
|
1484
|
+
}
|
1485
|
+
if (def.maxSize) {
|
1486
|
+
schema.maxItems = def.maxSize.value;
|
1487
|
+
}
|
1488
|
+
return schema;
|
1489
|
+
}
|
1490
|
+
|
1491
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts
|
1492
|
+
function parseTupleDef(def, refs) {
|
1493
|
+
if (def.rest) {
|
1494
|
+
return {
|
1495
|
+
type: "array",
|
1496
|
+
minItems: def.items.length,
|
1497
|
+
items: def.items.map(
|
1498
|
+
(x, i) => parseDef(x._def, {
|
1499
|
+
...refs,
|
1500
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
1501
|
+
})
|
1502
|
+
).reduce(
|
1503
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
1504
|
+
[]
|
1505
|
+
),
|
1506
|
+
additionalItems: parseDef(def.rest._def, {
|
1507
|
+
...refs,
|
1508
|
+
currentPath: [...refs.currentPath, "additionalItems"]
|
1509
|
+
})
|
1510
|
+
};
|
1574
1511
|
} else {
|
1575
|
-
|
1512
|
+
return {
|
1513
|
+
type: "array",
|
1514
|
+
minItems: def.items.length,
|
1515
|
+
maxItems: def.items.length,
|
1516
|
+
items: def.items.map(
|
1517
|
+
(x, i) => parseDef(x._def, {
|
1518
|
+
...refs,
|
1519
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
1520
|
+
})
|
1521
|
+
).reduce(
|
1522
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
1523
|
+
[]
|
1524
|
+
)
|
1525
|
+
};
|
1576
1526
|
}
|
1577
1527
|
}
|
1578
|
-
|
1528
|
+
|
1529
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts
|
1530
|
+
function parseUndefinedDef() {
|
1531
|
+
return {
|
1532
|
+
not: parseAnyDef()
|
1533
|
+
};
|
1534
|
+
}
|
1535
|
+
|
1536
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts
|
1537
|
+
function parseUnknownDef() {
|
1538
|
+
return parseAnyDef();
|
1539
|
+
}
|
1540
|
+
|
1541
|
+
// src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts
|
1542
|
+
var parseReadonlyDef = (def, refs) => {
|
1543
|
+
return parseDef(def.innerType._def, refs);
|
1544
|
+
};
|
1545
|
+
|
1546
|
+
// src/to-json-schema/zod3-to-json-schema/select-parser.ts
|
1547
|
+
var selectParser = (def, typeName, refs) => {
|
1548
|
+
switch (typeName) {
|
1549
|
+
case import_v33.ZodFirstPartyTypeKind.ZodString:
|
1550
|
+
return parseStringDef(def, refs);
|
1551
|
+
case import_v33.ZodFirstPartyTypeKind.ZodNumber:
|
1552
|
+
return parseNumberDef(def);
|
1553
|
+
case import_v33.ZodFirstPartyTypeKind.ZodObject:
|
1554
|
+
return parseObjectDef(def, refs);
|
1555
|
+
case import_v33.ZodFirstPartyTypeKind.ZodBigInt:
|
1556
|
+
return parseBigintDef(def);
|
1557
|
+
case import_v33.ZodFirstPartyTypeKind.ZodBoolean:
|
1558
|
+
return parseBooleanDef();
|
1559
|
+
case import_v33.ZodFirstPartyTypeKind.ZodDate:
|
1560
|
+
return parseDateDef(def, refs);
|
1561
|
+
case import_v33.ZodFirstPartyTypeKind.ZodUndefined:
|
1562
|
+
return parseUndefinedDef();
|
1563
|
+
case import_v33.ZodFirstPartyTypeKind.ZodNull:
|
1564
|
+
return parseNullDef();
|
1565
|
+
case import_v33.ZodFirstPartyTypeKind.ZodArray:
|
1566
|
+
return parseArrayDef(def, refs);
|
1567
|
+
case import_v33.ZodFirstPartyTypeKind.ZodUnion:
|
1568
|
+
case import_v33.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
1569
|
+
return parseUnionDef(def, refs);
|
1570
|
+
case import_v33.ZodFirstPartyTypeKind.ZodIntersection:
|
1571
|
+
return parseIntersectionDef(def, refs);
|
1572
|
+
case import_v33.ZodFirstPartyTypeKind.ZodTuple:
|
1573
|
+
return parseTupleDef(def, refs);
|
1574
|
+
case import_v33.ZodFirstPartyTypeKind.ZodRecord:
|
1575
|
+
return parseRecordDef(def, refs);
|
1576
|
+
case import_v33.ZodFirstPartyTypeKind.ZodLiteral:
|
1577
|
+
return parseLiteralDef(def);
|
1578
|
+
case import_v33.ZodFirstPartyTypeKind.ZodEnum:
|
1579
|
+
return parseEnumDef(def);
|
1580
|
+
case import_v33.ZodFirstPartyTypeKind.ZodNativeEnum:
|
1581
|
+
return parseNativeEnumDef(def);
|
1582
|
+
case import_v33.ZodFirstPartyTypeKind.ZodNullable:
|
1583
|
+
return parseNullableDef(def, refs);
|
1584
|
+
case import_v33.ZodFirstPartyTypeKind.ZodOptional:
|
1585
|
+
return parseOptionalDef(def, refs);
|
1586
|
+
case import_v33.ZodFirstPartyTypeKind.ZodMap:
|
1587
|
+
return parseMapDef(def, refs);
|
1588
|
+
case import_v33.ZodFirstPartyTypeKind.ZodSet:
|
1589
|
+
return parseSetDef(def, refs);
|
1590
|
+
case import_v33.ZodFirstPartyTypeKind.ZodLazy:
|
1591
|
+
return () => def.getter()._def;
|
1592
|
+
case import_v33.ZodFirstPartyTypeKind.ZodPromise:
|
1593
|
+
return parsePromiseDef(def, refs);
|
1594
|
+
case import_v33.ZodFirstPartyTypeKind.ZodNaN:
|
1595
|
+
case import_v33.ZodFirstPartyTypeKind.ZodNever:
|
1596
|
+
return parseNeverDef();
|
1597
|
+
case import_v33.ZodFirstPartyTypeKind.ZodEffects:
|
1598
|
+
return parseEffectsDef(def, refs);
|
1599
|
+
case import_v33.ZodFirstPartyTypeKind.ZodAny:
|
1600
|
+
return parseAnyDef();
|
1601
|
+
case import_v33.ZodFirstPartyTypeKind.ZodUnknown:
|
1602
|
+
return parseUnknownDef();
|
1603
|
+
case import_v33.ZodFirstPartyTypeKind.ZodDefault:
|
1604
|
+
return parseDefaultDef(def, refs);
|
1605
|
+
case import_v33.ZodFirstPartyTypeKind.ZodBranded:
|
1606
|
+
return parseBrandedDef(def, refs);
|
1607
|
+
case import_v33.ZodFirstPartyTypeKind.ZodReadonly:
|
1608
|
+
return parseReadonlyDef(def, refs);
|
1609
|
+
case import_v33.ZodFirstPartyTypeKind.ZodCatch:
|
1610
|
+
return parseCatchDef(def, refs);
|
1611
|
+
case import_v33.ZodFirstPartyTypeKind.ZodPipeline:
|
1612
|
+
return parsePipelineDef(def, refs);
|
1613
|
+
case import_v33.ZodFirstPartyTypeKind.ZodFunction:
|
1614
|
+
case import_v33.ZodFirstPartyTypeKind.ZodVoid:
|
1615
|
+
case import_v33.ZodFirstPartyTypeKind.ZodSymbol:
|
1616
|
+
return void 0;
|
1617
|
+
default:
|
1618
|
+
return /* @__PURE__ */ ((_) => void 0)(typeName);
|
1619
|
+
}
|
1620
|
+
};
|
1621
|
+
|
1622
|
+
// src/to-json-schema/zod3-to-json-schema/get-relative-path.ts
|
1623
|
+
var getRelativePath = (pathA, pathB) => {
|
1624
|
+
let i = 0;
|
1625
|
+
for (; i < pathA.length && i < pathB.length; i++) {
|
1626
|
+
if (pathA[i] !== pathB[i]) break;
|
1627
|
+
}
|
1628
|
+
return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
|
1629
|
+
};
|
1630
|
+
|
1631
|
+
// src/to-json-schema/zod3-to-json-schema/parse-def.ts
|
1632
|
+
function parseDef(def, refs, forceResolution = false) {
|
1579
1633
|
var _a;
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1634
|
+
const seenItem = refs.seen.get(def);
|
1635
|
+
if (refs.override) {
|
1636
|
+
const overrideResult = (_a = refs.override) == null ? void 0 : _a.call(
|
1637
|
+
refs,
|
1638
|
+
def,
|
1639
|
+
refs,
|
1640
|
+
seenItem,
|
1641
|
+
forceResolution
|
1642
|
+
);
|
1643
|
+
if (overrideResult !== ignoreOverride) {
|
1644
|
+
return overrideResult;
|
1583
1645
|
}
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1646
|
+
}
|
1647
|
+
if (seenItem && !forceResolution) {
|
1648
|
+
const seenSchema = get$ref(seenItem, refs);
|
1649
|
+
if (seenSchema !== void 0) {
|
1650
|
+
return seenSchema;
|
1589
1651
|
}
|
1590
|
-
schema.allOf.push({
|
1591
|
-
pattern: stringifyRegExpWithFlags(regex, refs),
|
1592
|
-
...message && refs.errorMessages && { errorMessage: { pattern: message } }
|
1593
|
-
});
|
1594
|
-
} else {
|
1595
|
-
schema.pattern = stringifyRegExpWithFlags(regex, refs);
|
1596
1652
|
}
|
1597
|
-
}
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1601
|
-
|
1653
|
+
const newItem = { def, path: refs.currentPath, jsonSchema: void 0 };
|
1654
|
+
refs.seen.set(def, newItem);
|
1655
|
+
const jsonSchemaOrGetter = selectParser(def, def.typeName, refs);
|
1656
|
+
const jsonSchema2 = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
|
1657
|
+
if (jsonSchema2) {
|
1658
|
+
addMeta(def, refs, jsonSchema2);
|
1602
1659
|
}
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
// `^` and `$` matches adjacent to newline characters
|
1608
|
-
s: regex.flags.includes("s")
|
1609
|
-
// `.` matches newlines
|
1610
|
-
};
|
1611
|
-
const source = flags.i ? regex.source.toLowerCase() : regex.source;
|
1612
|
-
let pattern = "";
|
1613
|
-
let isEscaped = false;
|
1614
|
-
let inCharGroup = false;
|
1615
|
-
let inCharRange = false;
|
1616
|
-
for (let i = 0; i < source.length; i++) {
|
1617
|
-
if (isEscaped) {
|
1618
|
-
pattern += source[i];
|
1619
|
-
isEscaped = false;
|
1620
|
-
continue;
|
1621
|
-
}
|
1622
|
-
if (flags.i) {
|
1623
|
-
if (inCharGroup) {
|
1624
|
-
if (source[i].match(/[a-z]/)) {
|
1625
|
-
if (inCharRange) {
|
1626
|
-
pattern += source[i];
|
1627
|
-
pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
|
1628
|
-
inCharRange = false;
|
1629
|
-
} else if (source[i + 1] === "-" && ((_a = source[i + 2]) == null ? void 0 : _a.match(/[a-z]/))) {
|
1630
|
-
pattern += source[i];
|
1631
|
-
inCharRange = true;
|
1632
|
-
} else {
|
1633
|
-
pattern += `${source[i]}${source[i].toUpperCase()}`;
|
1634
|
-
}
|
1635
|
-
continue;
|
1636
|
-
}
|
1637
|
-
} else if (source[i].match(/[a-z]/)) {
|
1638
|
-
pattern += `[${source[i]}${source[i].toUpperCase()}]`;
|
1639
|
-
continue;
|
1640
|
-
}
|
1641
|
-
}
|
1642
|
-
if (flags.m) {
|
1643
|
-
if (source[i] === "^") {
|
1644
|
-
pattern += `(^|(?<=[\r
|
1645
|
-
]))`;
|
1646
|
-
continue;
|
1647
|
-
} else if (source[i] === "$") {
|
1648
|
-
pattern += `($|(?=[\r
|
1649
|
-
]))`;
|
1650
|
-
continue;
|
1651
|
-
}
|
1652
|
-
}
|
1653
|
-
if (flags.s && source[i] === ".") {
|
1654
|
-
pattern += inCharGroup ? `${source[i]}\r
|
1655
|
-
` : `[${source[i]}\r
|
1656
|
-
]`;
|
1657
|
-
continue;
|
1658
|
-
}
|
1659
|
-
pattern += source[i];
|
1660
|
-
if (source[i] === "\\") {
|
1661
|
-
isEscaped = true;
|
1662
|
-
} else if (inCharGroup && source[i] === "]") {
|
1663
|
-
inCharGroup = false;
|
1664
|
-
} else if (!inCharGroup && source[i] === "[") {
|
1665
|
-
inCharGroup = true;
|
1666
|
-
}
|
1660
|
+
if (refs.postProcess) {
|
1661
|
+
const postProcessResult = refs.postProcess(jsonSchema2, def, refs);
|
1662
|
+
newItem.jsonSchema = jsonSchema2;
|
1663
|
+
return postProcessResult;
|
1667
1664
|
}
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1665
|
+
newItem.jsonSchema = jsonSchema2;
|
1666
|
+
return jsonSchema2;
|
1667
|
+
}
|
1668
|
+
var get$ref = (item, refs) => {
|
1669
|
+
switch (refs.$refStrategy) {
|
1670
|
+
case "root":
|
1671
|
+
return { $ref: item.path.join("/") };
|
1672
|
+
case "relative":
|
1673
|
+
return { $ref: getRelativePath(refs.currentPath, item.path) };
|
1674
|
+
case "none":
|
1675
|
+
case "seen": {
|
1676
|
+
if (item.path.length < refs.currentPath.length && item.path.every((value, index) => refs.currentPath[index] === value)) {
|
1677
|
+
console.warn(
|
1678
|
+
`Recursive reference detected at ${refs.currentPath.join(
|
1679
|
+
"/"
|
1680
|
+
)}! Defaulting to any`
|
1681
|
+
);
|
1682
|
+
return parseAnyDef();
|
1683
|
+
}
|
1684
|
+
return refs.$refStrategy === "seen" ? parseAnyDef() : void 0;
|
1685
|
+
}
|
1677
1686
|
}
|
1678
|
-
|
1679
|
-
|
1687
|
+
};
|
1688
|
+
var addMeta = (def, refs, jsonSchema2) => {
|
1689
|
+
if (def.description) {
|
1690
|
+
jsonSchema2.description = def.description;
|
1691
|
+
}
|
1692
|
+
return jsonSchema2;
|
1693
|
+
};
|
1680
1694
|
|
1681
|
-
// src/
|
1682
|
-
|
1683
|
-
|
1684
|
-
const
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1695
|
+
// src/to-json-schema/zod3-to-json-schema/refs.ts
|
1696
|
+
var getRefs = (options) => {
|
1697
|
+
const _options = getDefaultOptions(options);
|
1698
|
+
const currentPath = _options.name !== void 0 ? [..._options.basePath, _options.definitionPath, _options.name] : _options.basePath;
|
1699
|
+
return {
|
1700
|
+
..._options,
|
1701
|
+
currentPath,
|
1702
|
+
propertyPath: void 0,
|
1703
|
+
seen: new Map(
|
1704
|
+
Object.entries(_options.definitions).map(([name, def]) => [
|
1705
|
+
def._def,
|
1706
|
+
{
|
1707
|
+
def: def._def,
|
1708
|
+
path: [..._options.basePath, _options.definitionPath, name],
|
1709
|
+
// Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
|
1710
|
+
jsonSchema: void 0
|
1711
|
+
}
|
1712
|
+
])
|
1713
|
+
)
|
1690
1714
|
};
|
1691
|
-
|
1692
|
-
const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
|
1693
|
-
return {
|
1694
|
-
...schema,
|
1695
|
-
propertyNames: keyType
|
1696
|
-
};
|
1697
|
-
} else if (((_d = def.keyType) == null ? void 0 : _d._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodEnum) {
|
1698
|
-
return {
|
1699
|
-
...schema,
|
1700
|
-
propertyNames: {
|
1701
|
-
enum: def.keyType._def.values
|
1702
|
-
}
|
1703
|
-
};
|
1704
|
-
} else if (((_e = def.keyType) == null ? void 0 : _e._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodBranded && def.keyType._def.type._def.typeName === import_v32.ZodFirstPartyTypeKind.ZodString && ((_f = def.keyType._def.type._def.checks) == null ? void 0 : _f.length)) {
|
1705
|
-
const { type, ...keyType } = parseBrandedDef(
|
1706
|
-
def.keyType._def,
|
1707
|
-
refs
|
1708
|
-
);
|
1709
|
-
return {
|
1710
|
-
...schema,
|
1711
|
-
propertyNames: keyType
|
1712
|
-
};
|
1713
|
-
}
|
1714
|
-
return schema;
|
1715
|
-
}
|
1715
|
+
};
|
1716
1716
|
|
1717
|
-
// src/
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1717
|
+
// src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts
|
1718
|
+
var zod3ToJsonSchema = (schema, options) => {
|
1719
|
+
var _a;
|
1720
|
+
const refs = getRefs(options);
|
1721
|
+
let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce(
|
1722
|
+
(acc, [name2, schema2]) => {
|
1723
|
+
var _a2;
|
1724
|
+
return {
|
1725
|
+
...acc,
|
1726
|
+
[name2]: (_a2 = parseDef(
|
1727
|
+
schema2._def,
|
1728
|
+
{
|
1729
|
+
...refs,
|
1730
|
+
currentPath: [...refs.basePath, refs.definitionPath, name2]
|
1731
|
+
},
|
1732
|
+
true
|
1733
|
+
)) != null ? _a2 : parseAnyDef()
|
1734
|
+
};
|
1735
|
+
},
|
1736
|
+
{}
|
1737
|
+
) : void 0;
|
1738
|
+
const name = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
|
1739
|
+
const main = (_a = parseDef(
|
1740
|
+
schema._def,
|
1741
|
+
name === void 0 ? refs : {
|
1742
|
+
...refs,
|
1743
|
+
currentPath: [...refs.basePath, refs.definitionPath, name]
|
1744
|
+
},
|
1745
|
+
false
|
1746
|
+
)) != null ? _a : parseAnyDef();
|
1747
|
+
const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
|
1748
|
+
if (title !== void 0) {
|
1749
|
+
main.title = title;
|
1721
1750
|
}
|
1722
|
-
const
|
1723
|
-
...
|
1724
|
-
|
1725
|
-
}
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
type: "array",
|
1735
|
-
items: [keys, values],
|
1736
|
-
minItems: 2,
|
1737
|
-
maxItems: 2
|
1751
|
+
const combined = name === void 0 ? definitions ? {
|
1752
|
+
...main,
|
1753
|
+
[refs.definitionPath]: definitions
|
1754
|
+
} : main : {
|
1755
|
+
$ref: [
|
1756
|
+
...refs.$refStrategy === "relative" ? [] : refs.basePath,
|
1757
|
+
refs.definitionPath,
|
1758
|
+
name
|
1759
|
+
].join("/"),
|
1760
|
+
[refs.definitionPath]: {
|
1761
|
+
...definitions,
|
1762
|
+
[name]: main
|
1738
1763
|
}
|
1739
1764
|
};
|
1740
|
-
|
1765
|
+
combined.$schema = "http://json-schema.org/draft-07/schema#";
|
1766
|
+
return combined;
|
1767
|
+
};
|
1741
1768
|
|
1742
|
-
// src/
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
);
|
1752
|
-
return {
|
1753
|
-
type: parsedTypes.length === 1 ? parsedTypes[0] === "string" ? "string" : "number" : ["string", "number"],
|
1754
|
-
enum: actualValues
|
1769
|
+
// src/schema.ts
|
1770
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
1771
|
+
function lazySchema(createSchema) {
|
1772
|
+
let schema;
|
1773
|
+
return () => {
|
1774
|
+
if (schema == null) {
|
1775
|
+
schema = createSchema();
|
1776
|
+
}
|
1777
|
+
return schema;
|
1755
1778
|
};
|
1756
1779
|
}
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
1760
|
-
return { not: parseAnyDef() };
|
1761
|
-
}
|
1762
|
-
|
1763
|
-
// src/zod-to-json-schema/parsers/null.ts
|
1764
|
-
function parseNullDef() {
|
1780
|
+
function jsonSchema(jsonSchema2, {
|
1781
|
+
validate
|
1782
|
+
} = {}) {
|
1765
1783
|
return {
|
1766
|
-
|
1784
|
+
[schemaSymbol]: true,
|
1785
|
+
_type: void 0,
|
1786
|
+
// should never be used directly
|
1787
|
+
get jsonSchema() {
|
1788
|
+
if (typeof jsonSchema2 === "function") {
|
1789
|
+
jsonSchema2 = jsonSchema2();
|
1790
|
+
}
|
1791
|
+
return jsonSchema2;
|
1792
|
+
},
|
1793
|
+
validate
|
1767
1794
|
};
|
1768
1795
|
}
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
}
|
1778
|
-
function
|
1779
|
-
const
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
|
1802
|
-
|
1803
|
-
|
1804
|
-
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
}
|
1809
|
-
},
|
1810
|
-
[]
|
1811
|
-
);
|
1812
|
-
if (types.length === options.length) {
|
1813
|
-
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
1814
|
-
return {
|
1815
|
-
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
1816
|
-
enum: options.reduce(
|
1817
|
-
(acc, x) => {
|
1818
|
-
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
1819
|
-
},
|
1820
|
-
[]
|
1821
|
-
)
|
1822
|
-
};
|
1796
|
+
function isSchema(value) {
|
1797
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
1798
|
+
}
|
1799
|
+
function asSchema(schema) {
|
1800
|
+
return schema == null ? jsonSchema({
|
1801
|
+
properties: {},
|
1802
|
+
additionalProperties: false
|
1803
|
+
}) : isSchema(schema) ? schema : "~standard" in schema ? standardSchema(schema) : schema();
|
1804
|
+
}
|
1805
|
+
function standardSchema(standardSchema2) {
|
1806
|
+
const vendor = standardSchema2["~standard"].vendor;
|
1807
|
+
switch (vendor) {
|
1808
|
+
case "zod": {
|
1809
|
+
return zodSchema(
|
1810
|
+
standardSchema2
|
1811
|
+
);
|
1812
|
+
}
|
1813
|
+
case "arktype": {
|
1814
|
+
return standardSchemaWithJsonSchemaResolver(
|
1815
|
+
standardSchema2,
|
1816
|
+
arktypeToJsonSchema
|
1817
|
+
);
|
1818
|
+
}
|
1819
|
+
case "effect": {
|
1820
|
+
return standardSchemaWithJsonSchemaResolver(
|
1821
|
+
standardSchema2,
|
1822
|
+
effectToJsonSchema
|
1823
|
+
);
|
1824
|
+
}
|
1825
|
+
case "valibot": {
|
1826
|
+
return standardSchemaWithJsonSchemaResolver(
|
1827
|
+
standardSchema2,
|
1828
|
+
valibotToJsonSchema
|
1829
|
+
);
|
1830
|
+
}
|
1831
|
+
default: {
|
1832
|
+
return standardSchemaWithJsonSchemaResolver(standardSchema2, () => () => {
|
1833
|
+
throw new Error(`Unsupported standard schema vendor: ${vendor}`);
|
1834
|
+
});
|
1823
1835
|
}
|
1824
|
-
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
|
1825
|
-
return {
|
1826
|
-
type: "string",
|
1827
|
-
enum: options.reduce(
|
1828
|
-
(acc, x) => [
|
1829
|
-
...acc,
|
1830
|
-
...x._def.values.filter((x2) => !acc.includes(x2))
|
1831
|
-
],
|
1832
|
-
[]
|
1833
|
-
)
|
1834
|
-
};
|
1835
1836
|
}
|
1836
|
-
return asAnyOf(def, refs);
|
1837
1837
|
}
|
1838
|
-
|
1839
|
-
|
1840
|
-
(
|
1841
|
-
|
1842
|
-
|
1843
|
-
|
1844
|
-
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
};
|
1849
|
-
|
1850
|
-
// src/zod-to-json-schema/parsers/nullable.ts
|
1851
|
-
function parseNullableDef(def, refs) {
|
1852
|
-
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
|
1853
|
-
def.innerType._def.typeName
|
1854
|
-
) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) {
|
1855
|
-
return {
|
1856
|
-
type: [
|
1857
|
-
primitiveMappings[def.innerType._def.typeName],
|
1858
|
-
"null"
|
1859
|
-
]
|
1860
|
-
};
|
1861
|
-
}
|
1862
|
-
const base = parseDef(def.innerType._def, {
|
1863
|
-
...refs,
|
1864
|
-
currentPath: [...refs.currentPath, "anyOf", "0"]
|
1838
|
+
function standardSchemaWithJsonSchemaResolver(standardSchema2, jsonSchemaResolver) {
|
1839
|
+
return jsonSchema(jsonSchemaResolver(standardSchema2), {
|
1840
|
+
validate: async (value) => {
|
1841
|
+
const result = await standardSchema2["~standard"].validate(value);
|
1842
|
+
return "value" in result ? { success: true, value: result.value } : {
|
1843
|
+
success: false,
|
1844
|
+
error: new import_provider6.TypeValidationError({
|
1845
|
+
value,
|
1846
|
+
cause: result.issues
|
1847
|
+
})
|
1848
|
+
};
|
1849
|
+
}
|
1865
1850
|
});
|
1866
|
-
return base && { anyOf: [base, { type: "null" }] };
|
1867
1851
|
}
|
1868
|
-
|
1869
|
-
|
1870
|
-
|
1871
|
-
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
|
1879
|
-
|
1880
|
-
|
1881
|
-
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1886
|
-
|
1887
|
-
|
1888
|
-
|
1889
|
-
|
1890
|
-
|
1891
|
-
|
1892
|
-
|
1893
|
-
|
1894
|
-
|
1895
|
-
|
1896
|
-
|
1852
|
+
function zod3Schema(zodSchema2, options) {
|
1853
|
+
var _a;
|
1854
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
1855
|
+
return jsonSchema(
|
1856
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
1857
|
+
() => zod3ToJsonSchema(zodSchema2, {
|
1858
|
+
$refStrategy: useReferences ? "root" : "none"
|
1859
|
+
}),
|
1860
|
+
{
|
1861
|
+
validate: async (value) => {
|
1862
|
+
const result = await zodSchema2.safeParseAsync(value);
|
1863
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
1864
|
+
}
|
1865
|
+
}
|
1866
|
+
);
|
1867
|
+
}
|
1868
|
+
function zod4Schema(zodSchema2, options) {
|
1869
|
+
var _a;
|
1870
|
+
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
1871
|
+
return jsonSchema(
|
1872
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
1873
|
+
() => z4.toJSONSchema(zodSchema2, {
|
1874
|
+
target: "draft-7",
|
1875
|
+
io: "output",
|
1876
|
+
reused: useReferences ? "ref" : "inline"
|
1877
|
+
}),
|
1878
|
+
{
|
1879
|
+
validate: async (value) => {
|
1880
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
1881
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
1882
|
+
}
|
1897
1883
|
}
|
1884
|
+
);
|
1885
|
+
}
|
1886
|
+
function isZod4Schema(zodSchema2) {
|
1887
|
+
return "_zod" in zodSchema2;
|
1888
|
+
}
|
1889
|
+
function zodSchema(zodSchema2, options) {
|
1890
|
+
if (isZod4Schema(zodSchema2)) {
|
1891
|
+
return zod4Schema(zodSchema2, options);
|
1892
|
+
} else {
|
1893
|
+
return zod3Schema(zodSchema2, options);
|
1898
1894
|
}
|
1899
|
-
return res;
|
1900
1895
|
}
|
1901
1896
|
|
1902
|
-
// src/
|
1903
|
-
function
|
1904
|
-
|
1905
|
-
|
1906
|
-
|
1907
|
-
};
|
1908
|
-
|
1909
|
-
|
1910
|
-
|
1911
|
-
|
1912
|
-
|
1913
|
-
|
1914
|
-
|
1915
|
-
|
1916
|
-
|
1917
|
-
|
1918
|
-
|
1919
|
-
|
1920
|
-
|
1921
|
-
if (parsedDef === void 0) {
|
1922
|
-
continue;
|
1897
|
+
// src/validate-types.ts
|
1898
|
+
async function validateTypes({
|
1899
|
+
value,
|
1900
|
+
schema
|
1901
|
+
}) {
|
1902
|
+
const result = await safeValidateTypes({ value, schema });
|
1903
|
+
if (!result.success) {
|
1904
|
+
throw import_provider7.TypeValidationError.wrap({ value, cause: result.error });
|
1905
|
+
}
|
1906
|
+
return result.value;
|
1907
|
+
}
|
1908
|
+
async function safeValidateTypes({
|
1909
|
+
value,
|
1910
|
+
schema
|
1911
|
+
}) {
|
1912
|
+
const actualSchema = asSchema(schema);
|
1913
|
+
try {
|
1914
|
+
if (actualSchema.validate == null) {
|
1915
|
+
return { success: true, value, rawValue: value };
|
1923
1916
|
}
|
1924
|
-
result
|
1925
|
-
if (
|
1926
|
-
|
1917
|
+
const result = await actualSchema.validate(value);
|
1918
|
+
if (result.success) {
|
1919
|
+
return { success: true, value: result.value, rawValue: value };
|
1927
1920
|
}
|
1921
|
+
return {
|
1922
|
+
success: false,
|
1923
|
+
error: import_provider7.TypeValidationError.wrap({ value, cause: result.error }),
|
1924
|
+
rawValue: value
|
1925
|
+
};
|
1926
|
+
} catch (error) {
|
1927
|
+
return {
|
1928
|
+
success: false,
|
1929
|
+
error: import_provider7.TypeValidationError.wrap({ value, cause: error }),
|
1930
|
+
rawValue: value
|
1931
|
+
};
|
1928
1932
|
}
|
1929
|
-
if (required.length) {
|
1930
|
-
result.required = required;
|
1931
|
-
}
|
1932
|
-
const additionalProperties = decideAdditionalProperties(def, refs);
|
1933
|
-
if (additionalProperties !== void 0) {
|
1934
|
-
result.additionalProperties = additionalProperties;
|
1935
|
-
}
|
1936
|
-
return result;
|
1937
1933
|
}
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
|
1946
|
-
|
1947
|
-
return
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1934
|
+
|
1935
|
+
// src/parse-json.ts
|
1936
|
+
async function parseJSON({
|
1937
|
+
text,
|
1938
|
+
schema
|
1939
|
+
}) {
|
1940
|
+
try {
|
1941
|
+
const value = secureJsonParse(text);
|
1942
|
+
if (schema == null) {
|
1943
|
+
return value;
|
1944
|
+
}
|
1945
|
+
return validateTypes({ value, schema });
|
1946
|
+
} catch (error) {
|
1947
|
+
if (import_provider8.JSONParseError.isInstance(error) || import_provider8.TypeValidationError.isInstance(error)) {
|
1948
|
+
throw error;
|
1949
|
+
}
|
1950
|
+
throw new import_provider8.JSONParseError({ text, cause: error });
|
1952
1951
|
}
|
1953
1952
|
}
|
1954
|
-
function
|
1953
|
+
async function safeParseJSON({
|
1954
|
+
text,
|
1955
|
+
schema
|
1956
|
+
}) {
|
1955
1957
|
try {
|
1956
|
-
|
1957
|
-
|
1958
|
-
|
1958
|
+
const value = secureJsonParse(text);
|
1959
|
+
if (schema == null) {
|
1960
|
+
return { success: true, value, rawValue: value };
|
1961
|
+
}
|
1962
|
+
return await safeValidateTypes({ value, schema });
|
1963
|
+
} catch (error) {
|
1964
|
+
return {
|
1965
|
+
success: false,
|
1966
|
+
error: import_provider8.JSONParseError.isInstance(error) ? error : new import_provider8.JSONParseError({ text, cause: error }),
|
1967
|
+
rawValue: void 0
|
1968
|
+
};
|
1959
1969
|
}
|
1960
1970
|
}
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
return
|
1967
|
-
}
|
1968
|
-
const innerSchema = parseDef(def.innerType._def, {
|
1969
|
-
...refs,
|
1970
|
-
currentPath: [...refs.currentPath, "anyOf", "1"]
|
1971
|
-
});
|
1972
|
-
return innerSchema ? { anyOf: [{ not: parseAnyDef() }, innerSchema] } : parseAnyDef();
|
1973
|
-
};
|
1974
|
-
|
1975
|
-
// src/zod-to-json-schema/parsers/pipeline.ts
|
1976
|
-
var parsePipelineDef = (def, refs) => {
|
1977
|
-
if (refs.pipeStrategy === "input") {
|
1978
|
-
return parseDef(def.in._def, refs);
|
1979
|
-
} else if (refs.pipeStrategy === "output") {
|
1980
|
-
return parseDef(def.out._def, refs);
|
1971
|
+
function isParsableJson(input) {
|
1972
|
+
try {
|
1973
|
+
secureJsonParse(input);
|
1974
|
+
return true;
|
1975
|
+
} catch (e) {
|
1976
|
+
return false;
|
1981
1977
|
}
|
1982
|
-
|
1983
|
-
...refs,
|
1984
|
-
currentPath: [...refs.currentPath, "allOf", "0"]
|
1985
|
-
});
|
1986
|
-
const b = parseDef(def.out._def, {
|
1987
|
-
...refs,
|
1988
|
-
currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
|
1989
|
-
});
|
1990
|
-
return {
|
1991
|
-
allOf: [a, b].filter((x) => x !== void 0)
|
1992
|
-
};
|
1993
|
-
};
|
1978
|
+
}
|
1994
1979
|
|
1995
|
-
// src/
|
1996
|
-
|
1997
|
-
|
1980
|
+
// src/parse-json-event-stream.ts
|
1981
|
+
var import_stream = require("eventsource-parser/stream");
|
1982
|
+
function parseJsonEventStream({
|
1983
|
+
stream,
|
1984
|
+
schema
|
1985
|
+
}) {
|
1986
|
+
return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough(
|
1987
|
+
new TransformStream({
|
1988
|
+
async transform({ data }, controller) {
|
1989
|
+
if (data === "[DONE]") {
|
1990
|
+
return;
|
1991
|
+
}
|
1992
|
+
controller.enqueue(await safeParseJSON({ text: data, schema }));
|
1993
|
+
}
|
1994
|
+
})
|
1995
|
+
);
|
1998
1996
|
}
|
1999
1997
|
|
2000
|
-
// src/
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
|
2005
|
-
|
2006
|
-
|
2007
|
-
|
2008
|
-
|
2009
|
-
items
|
2010
|
-
};
|
2011
|
-
if (def.minSize) {
|
2012
|
-
schema.minItems = def.minSize.value;
|
1998
|
+
// src/parse-provider-options.ts
|
1999
|
+
var import_provider9 = require("@ai-sdk/provider");
|
2000
|
+
async function parseProviderOptions({
|
2001
|
+
provider,
|
2002
|
+
providerOptions,
|
2003
|
+
schema
|
2004
|
+
}) {
|
2005
|
+
if ((providerOptions == null ? void 0 : providerOptions[provider]) == null) {
|
2006
|
+
return void 0;
|
2013
2007
|
}
|
2014
|
-
|
2015
|
-
|
2008
|
+
const parsedProviderOptions = await safeValidateTypes({
|
2009
|
+
value: providerOptions[provider],
|
2010
|
+
schema
|
2011
|
+
});
|
2012
|
+
if (!parsedProviderOptions.success) {
|
2013
|
+
throw new import_provider9.InvalidArgumentError({
|
2014
|
+
argument: "providerOptions",
|
2015
|
+
message: `invalid ${provider} provider options`,
|
2016
|
+
cause: parsedProviderOptions.error
|
2017
|
+
});
|
2016
2018
|
}
|
2017
|
-
return
|
2019
|
+
return parsedProviderOptions.value;
|
2018
2020
|
}
|
2019
2021
|
|
2020
|
-
// src/
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2029
|
-
|
2030
|
-
|
2031
|
-
|
2032
|
-
|
2033
|
-
|
2022
|
+
// src/post-to-api.ts
|
2023
|
+
var import_provider10 = require("@ai-sdk/provider");
|
2024
|
+
var getOriginalFetch2 = () => globalThis.fetch;
|
2025
|
+
var postJsonToApi = async ({
|
2026
|
+
url,
|
2027
|
+
headers,
|
2028
|
+
body,
|
2029
|
+
failedResponseHandler,
|
2030
|
+
successfulResponseHandler,
|
2031
|
+
abortSignal,
|
2032
|
+
fetch
|
2033
|
+
}) => postToApi({
|
2034
|
+
url,
|
2035
|
+
headers: {
|
2036
|
+
"Content-Type": "application/json",
|
2037
|
+
...headers
|
2038
|
+
},
|
2039
|
+
body: {
|
2040
|
+
content: JSON.stringify(body),
|
2041
|
+
values: body
|
2042
|
+
},
|
2043
|
+
failedResponseHandler,
|
2044
|
+
successfulResponseHandler,
|
2045
|
+
abortSignal,
|
2046
|
+
fetch
|
2047
|
+
});
|
2048
|
+
var postFormDataToApi = async ({
|
2049
|
+
url,
|
2050
|
+
headers,
|
2051
|
+
formData,
|
2052
|
+
failedResponseHandler,
|
2053
|
+
successfulResponseHandler,
|
2054
|
+
abortSignal,
|
2055
|
+
fetch
|
2056
|
+
}) => postToApi({
|
2057
|
+
url,
|
2058
|
+
headers,
|
2059
|
+
body: {
|
2060
|
+
content: formData,
|
2061
|
+
values: Object.fromEntries(formData.entries())
|
2062
|
+
},
|
2063
|
+
failedResponseHandler,
|
2064
|
+
successfulResponseHandler,
|
2065
|
+
abortSignal,
|
2066
|
+
fetch
|
2067
|
+
});
|
2068
|
+
var postToApi = async ({
|
2069
|
+
url,
|
2070
|
+
headers = {},
|
2071
|
+
body,
|
2072
|
+
successfulResponseHandler,
|
2073
|
+
failedResponseHandler,
|
2074
|
+
abortSignal,
|
2075
|
+
fetch = getOriginalFetch2()
|
2076
|
+
}) => {
|
2077
|
+
try {
|
2078
|
+
const response = await fetch(url, {
|
2079
|
+
method: "POST",
|
2080
|
+
headers: withUserAgentSuffix(
|
2081
|
+
headers,
|
2082
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
2083
|
+
getRuntimeEnvironmentUserAgent()
|
2034
2084
|
),
|
2035
|
-
|
2036
|
-
|
2037
|
-
|
2038
|
-
|
2039
|
-
|
2040
|
-
|
2041
|
-
|
2042
|
-
|
2043
|
-
|
2044
|
-
|
2045
|
-
|
2046
|
-
|
2047
|
-
|
2048
|
-
|
2049
|
-
|
2050
|
-
|
2051
|
-
|
2052
|
-
|
2053
|
-
|
2054
|
-
|
2085
|
+
body: body.content,
|
2086
|
+
signal: abortSignal
|
2087
|
+
});
|
2088
|
+
const responseHeaders = extractResponseHeaders(response);
|
2089
|
+
if (!response.ok) {
|
2090
|
+
let errorInformation;
|
2091
|
+
try {
|
2092
|
+
errorInformation = await failedResponseHandler({
|
2093
|
+
response,
|
2094
|
+
url,
|
2095
|
+
requestBodyValues: body.values
|
2096
|
+
});
|
2097
|
+
} catch (error) {
|
2098
|
+
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
2099
|
+
throw error;
|
2100
|
+
}
|
2101
|
+
throw new import_provider10.APICallError({
|
2102
|
+
message: "Failed to process error response",
|
2103
|
+
cause: error,
|
2104
|
+
statusCode: response.status,
|
2105
|
+
url,
|
2106
|
+
responseHeaders,
|
2107
|
+
requestBodyValues: body.values
|
2108
|
+
});
|
2109
|
+
}
|
2110
|
+
throw errorInformation.value;
|
2111
|
+
}
|
2112
|
+
try {
|
2113
|
+
return await successfulResponseHandler({
|
2114
|
+
response,
|
2115
|
+
url,
|
2116
|
+
requestBodyValues: body.values
|
2117
|
+
});
|
2118
|
+
} catch (error) {
|
2119
|
+
if (error instanceof Error) {
|
2120
|
+
if (isAbortError(error) || import_provider10.APICallError.isInstance(error)) {
|
2121
|
+
throw error;
|
2122
|
+
}
|
2123
|
+
}
|
2124
|
+
throw new import_provider10.APICallError({
|
2125
|
+
message: "Failed to process successful response",
|
2126
|
+
cause: error,
|
2127
|
+
statusCode: response.status,
|
2128
|
+
url,
|
2129
|
+
responseHeaders,
|
2130
|
+
requestBodyValues: body.values
|
2131
|
+
});
|
2132
|
+
}
|
2133
|
+
} catch (error) {
|
2134
|
+
throw handleFetchError({ error, url, requestBodyValues: body.values });
|
2055
2135
|
}
|
2136
|
+
};
|
2137
|
+
|
2138
|
+
// src/types/tool.ts
|
2139
|
+
function tool(tool2) {
|
2140
|
+
return tool2;
|
2141
|
+
}
|
2142
|
+
function dynamicTool(tool2) {
|
2143
|
+
return { ...tool2, type: "dynamic" };
|
2056
2144
|
}
|
2057
2145
|
|
2058
|
-
// src/
|
2059
|
-
function
|
2060
|
-
|
2061
|
-
|
2062
|
-
|
2146
|
+
// src/provider-defined-tool-factory.ts
|
2147
|
+
function createProviderDefinedToolFactory({
|
2148
|
+
id,
|
2149
|
+
name,
|
2150
|
+
inputSchema
|
2151
|
+
}) {
|
2152
|
+
return ({
|
2153
|
+
execute,
|
2154
|
+
outputSchema,
|
2155
|
+
needsApproval,
|
2156
|
+
toModelOutput,
|
2157
|
+
onInputStart,
|
2158
|
+
onInputDelta,
|
2159
|
+
onInputAvailable,
|
2160
|
+
...args
|
2161
|
+
}) => tool({
|
2162
|
+
type: "provider-defined",
|
2163
|
+
id,
|
2164
|
+
name,
|
2165
|
+
args,
|
2166
|
+
inputSchema,
|
2167
|
+
outputSchema,
|
2168
|
+
execute,
|
2169
|
+
needsApproval,
|
2170
|
+
toModelOutput,
|
2171
|
+
onInputStart,
|
2172
|
+
onInputDelta,
|
2173
|
+
onInputAvailable
|
2174
|
+
});
|
2063
2175
|
}
|
2064
|
-
|
2065
|
-
|
2066
|
-
|
2067
|
-
|
2176
|
+
function createProviderDefinedToolFactoryWithOutputSchema({
|
2177
|
+
id,
|
2178
|
+
name,
|
2179
|
+
inputSchema,
|
2180
|
+
outputSchema
|
2181
|
+
}) {
|
2182
|
+
return ({
|
2183
|
+
execute,
|
2184
|
+
needsApproval,
|
2185
|
+
toModelOutput,
|
2186
|
+
onInputStart,
|
2187
|
+
onInputDelta,
|
2188
|
+
onInputAvailable,
|
2189
|
+
...args
|
2190
|
+
}) => tool({
|
2191
|
+
type: "provider-defined",
|
2192
|
+
id,
|
2193
|
+
name,
|
2194
|
+
args,
|
2195
|
+
inputSchema,
|
2196
|
+
outputSchema,
|
2197
|
+
execute,
|
2198
|
+
needsApproval,
|
2199
|
+
toModelOutput,
|
2200
|
+
onInputStart,
|
2201
|
+
onInputDelta,
|
2202
|
+
onInputAvailable
|
2203
|
+
});
|
2068
2204
|
}
|
2069
2205
|
|
2070
|
-
// src/
|
2071
|
-
|
2072
|
-
|
2073
|
-
|
2074
|
-
|
2075
|
-
// src/zod-to-json-schema/select-parser.ts
|
2076
|
-
var selectParser = (def, typeName, refs) => {
|
2077
|
-
switch (typeName) {
|
2078
|
-
case import_v33.ZodFirstPartyTypeKind.ZodString:
|
2079
|
-
return parseStringDef(def, refs);
|
2080
|
-
case import_v33.ZodFirstPartyTypeKind.ZodNumber:
|
2081
|
-
return parseNumberDef(def);
|
2082
|
-
case import_v33.ZodFirstPartyTypeKind.ZodObject:
|
2083
|
-
return parseObjectDef(def, refs);
|
2084
|
-
case import_v33.ZodFirstPartyTypeKind.ZodBigInt:
|
2085
|
-
return parseBigintDef(def);
|
2086
|
-
case import_v33.ZodFirstPartyTypeKind.ZodBoolean:
|
2087
|
-
return parseBooleanDef();
|
2088
|
-
case import_v33.ZodFirstPartyTypeKind.ZodDate:
|
2089
|
-
return parseDateDef(def, refs);
|
2090
|
-
case import_v33.ZodFirstPartyTypeKind.ZodUndefined:
|
2091
|
-
return parseUndefinedDef();
|
2092
|
-
case import_v33.ZodFirstPartyTypeKind.ZodNull:
|
2093
|
-
return parseNullDef();
|
2094
|
-
case import_v33.ZodFirstPartyTypeKind.ZodArray:
|
2095
|
-
return parseArrayDef(def, refs);
|
2096
|
-
case import_v33.ZodFirstPartyTypeKind.ZodUnion:
|
2097
|
-
case import_v33.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
2098
|
-
return parseUnionDef(def, refs);
|
2099
|
-
case import_v33.ZodFirstPartyTypeKind.ZodIntersection:
|
2100
|
-
return parseIntersectionDef(def, refs);
|
2101
|
-
case import_v33.ZodFirstPartyTypeKind.ZodTuple:
|
2102
|
-
return parseTupleDef(def, refs);
|
2103
|
-
case import_v33.ZodFirstPartyTypeKind.ZodRecord:
|
2104
|
-
return parseRecordDef(def, refs);
|
2105
|
-
case import_v33.ZodFirstPartyTypeKind.ZodLiteral:
|
2106
|
-
return parseLiteralDef(def);
|
2107
|
-
case import_v33.ZodFirstPartyTypeKind.ZodEnum:
|
2108
|
-
return parseEnumDef(def);
|
2109
|
-
case import_v33.ZodFirstPartyTypeKind.ZodNativeEnum:
|
2110
|
-
return parseNativeEnumDef(def);
|
2111
|
-
case import_v33.ZodFirstPartyTypeKind.ZodNullable:
|
2112
|
-
return parseNullableDef(def, refs);
|
2113
|
-
case import_v33.ZodFirstPartyTypeKind.ZodOptional:
|
2114
|
-
return parseOptionalDef(def, refs);
|
2115
|
-
case import_v33.ZodFirstPartyTypeKind.ZodMap:
|
2116
|
-
return parseMapDef(def, refs);
|
2117
|
-
case import_v33.ZodFirstPartyTypeKind.ZodSet:
|
2118
|
-
return parseSetDef(def, refs);
|
2119
|
-
case import_v33.ZodFirstPartyTypeKind.ZodLazy:
|
2120
|
-
return () => def.getter()._def;
|
2121
|
-
case import_v33.ZodFirstPartyTypeKind.ZodPromise:
|
2122
|
-
return parsePromiseDef(def, refs);
|
2123
|
-
case import_v33.ZodFirstPartyTypeKind.ZodNaN:
|
2124
|
-
case import_v33.ZodFirstPartyTypeKind.ZodNever:
|
2125
|
-
return parseNeverDef();
|
2126
|
-
case import_v33.ZodFirstPartyTypeKind.ZodEffects:
|
2127
|
-
return parseEffectsDef(def, refs);
|
2128
|
-
case import_v33.ZodFirstPartyTypeKind.ZodAny:
|
2129
|
-
return parseAnyDef();
|
2130
|
-
case import_v33.ZodFirstPartyTypeKind.ZodUnknown:
|
2131
|
-
return parseUnknownDef();
|
2132
|
-
case import_v33.ZodFirstPartyTypeKind.ZodDefault:
|
2133
|
-
return parseDefaultDef(def, refs);
|
2134
|
-
case import_v33.ZodFirstPartyTypeKind.ZodBranded:
|
2135
|
-
return parseBrandedDef(def, refs);
|
2136
|
-
case import_v33.ZodFirstPartyTypeKind.ZodReadonly:
|
2137
|
-
return parseReadonlyDef(def, refs);
|
2138
|
-
case import_v33.ZodFirstPartyTypeKind.ZodCatch:
|
2139
|
-
return parseCatchDef(def, refs);
|
2140
|
-
case import_v33.ZodFirstPartyTypeKind.ZodPipeline:
|
2141
|
-
return parsePipelineDef(def, refs);
|
2142
|
-
case import_v33.ZodFirstPartyTypeKind.ZodFunction:
|
2143
|
-
case import_v33.ZodFirstPartyTypeKind.ZodVoid:
|
2144
|
-
case import_v33.ZodFirstPartyTypeKind.ZodSymbol:
|
2145
|
-
return void 0;
|
2146
|
-
default:
|
2147
|
-
return /* @__PURE__ */ ((_) => void 0)(typeName);
|
2206
|
+
// src/resolve.ts
|
2207
|
+
async function resolve(value) {
|
2208
|
+
if (typeof value === "function") {
|
2209
|
+
value = value();
|
2148
2210
|
}
|
2149
|
-
|
2211
|
+
return Promise.resolve(value);
|
2212
|
+
}
|
2150
2213
|
|
2151
|
-
// src/
|
2152
|
-
|
2153
|
-
|
2154
|
-
|
2155
|
-
|
2156
|
-
|
2157
|
-
|
2158
|
-
|
2159
|
-
|
2160
|
-
|
2161
|
-
|
2162
|
-
|
2163
|
-
|
2164
|
-
|
2165
|
-
|
2166
|
-
|
2167
|
-
|
2168
|
-
|
2169
|
-
|
2170
|
-
|
2171
|
-
|
2172
|
-
|
2173
|
-
const newItem = { def, path: refs.currentPath, jsonSchema: void 0 };
|
2174
|
-
refs.seen.set(def, newItem);
|
2175
|
-
const jsonSchemaOrGetter = selectParser(def, def.typeName, refs);
|
2176
|
-
const jsonSchema2 = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
|
2177
|
-
if (jsonSchema2) {
|
2178
|
-
addMeta(def, refs, jsonSchema2);
|
2179
|
-
}
|
2180
|
-
if (refs.postProcess) {
|
2181
|
-
const postProcessResult = refs.postProcess(jsonSchema2, def, refs);
|
2182
|
-
newItem.jsonSchema = jsonSchema2;
|
2183
|
-
return postProcessResult;
|
2214
|
+
// src/response-handler.ts
|
2215
|
+
var import_provider11 = require("@ai-sdk/provider");
|
2216
|
+
var createJsonErrorResponseHandler = ({
|
2217
|
+
errorSchema,
|
2218
|
+
errorToMessage,
|
2219
|
+
isRetryable
|
2220
|
+
}) => async ({ response, url, requestBodyValues }) => {
|
2221
|
+
const responseBody = await response.text();
|
2222
|
+
const responseHeaders = extractResponseHeaders(response);
|
2223
|
+
if (responseBody.trim() === "") {
|
2224
|
+
return {
|
2225
|
+
responseHeaders,
|
2226
|
+
value: new import_provider11.APICallError({
|
2227
|
+
message: response.statusText,
|
2228
|
+
url,
|
2229
|
+
requestBodyValues,
|
2230
|
+
statusCode: response.status,
|
2231
|
+
responseHeaders,
|
2232
|
+
responseBody,
|
2233
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
2234
|
+
})
|
2235
|
+
};
|
2184
2236
|
}
|
2185
|
-
|
2186
|
-
|
2187
|
-
|
2188
|
-
|
2189
|
-
|
2190
|
-
|
2191
|
-
|
2192
|
-
|
2193
|
-
|
2194
|
-
|
2195
|
-
|
2196
|
-
|
2197
|
-
|
2198
|
-
|
2199
|
-
|
2200
|
-
|
2201
|
-
|
2202
|
-
|
2203
|
-
|
2204
|
-
|
2205
|
-
|
2237
|
+
try {
|
2238
|
+
const parsedError = await parseJSON({
|
2239
|
+
text: responseBody,
|
2240
|
+
schema: errorSchema
|
2241
|
+
});
|
2242
|
+
return {
|
2243
|
+
responseHeaders,
|
2244
|
+
value: new import_provider11.APICallError({
|
2245
|
+
message: errorToMessage(parsedError),
|
2246
|
+
url,
|
2247
|
+
requestBodyValues,
|
2248
|
+
statusCode: response.status,
|
2249
|
+
responseHeaders,
|
2250
|
+
responseBody,
|
2251
|
+
data: parsedError,
|
2252
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
|
2253
|
+
})
|
2254
|
+
};
|
2255
|
+
} catch (parseError) {
|
2256
|
+
return {
|
2257
|
+
responseHeaders,
|
2258
|
+
value: new import_provider11.APICallError({
|
2259
|
+
message: response.statusText,
|
2260
|
+
url,
|
2261
|
+
requestBodyValues,
|
2262
|
+
statusCode: response.status,
|
2263
|
+
responseHeaders,
|
2264
|
+
responseBody,
|
2265
|
+
isRetryable: isRetryable == null ? void 0 : isRetryable(response)
|
2266
|
+
})
|
2267
|
+
};
|
2206
2268
|
}
|
2207
2269
|
};
|
2208
|
-
var
|
2209
|
-
|
2210
|
-
|
2270
|
+
var createEventSourceResponseHandler = (chunkSchema) => async ({ response }) => {
|
2271
|
+
const responseHeaders = extractResponseHeaders(response);
|
2272
|
+
if (response.body == null) {
|
2273
|
+
throw new import_provider11.EmptyResponseBodyError({});
|
2211
2274
|
}
|
2212
|
-
return jsonSchema2;
|
2213
|
-
};
|
2214
|
-
|
2215
|
-
// src/zod-to-json-schema/refs.ts
|
2216
|
-
var getRefs = (options) => {
|
2217
|
-
const _options = getDefaultOptions(options);
|
2218
|
-
const currentPath = _options.name !== void 0 ? [..._options.basePath, _options.definitionPath, _options.name] : _options.basePath;
|
2219
2275
|
return {
|
2220
|
-
|
2221
|
-
|
2222
|
-
|
2223
|
-
|
2224
|
-
|
2225
|
-
|
2226
|
-
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2276
|
+
responseHeaders,
|
2277
|
+
value: parseJsonEventStream({
|
2278
|
+
stream: response.body,
|
2279
|
+
schema: chunkSchema
|
2280
|
+
})
|
2281
|
+
};
|
2282
|
+
};
|
2283
|
+
var createJsonStreamResponseHandler = (chunkSchema) => async ({ response }) => {
|
2284
|
+
const responseHeaders = extractResponseHeaders(response);
|
2285
|
+
if (response.body == null) {
|
2286
|
+
throw new import_provider11.EmptyResponseBodyError({});
|
2287
|
+
}
|
2288
|
+
let buffer = "";
|
2289
|
+
return {
|
2290
|
+
responseHeaders,
|
2291
|
+
value: response.body.pipeThrough(new TextDecoderStream()).pipeThrough(
|
2292
|
+
new TransformStream({
|
2293
|
+
async transform(chunkText, controller) {
|
2294
|
+
if (chunkText.endsWith("\n")) {
|
2295
|
+
controller.enqueue(
|
2296
|
+
await safeParseJSON({
|
2297
|
+
text: buffer + chunkText,
|
2298
|
+
schema: chunkSchema
|
2299
|
+
})
|
2300
|
+
);
|
2301
|
+
buffer = "";
|
2302
|
+
} else {
|
2303
|
+
buffer += chunkText;
|
2304
|
+
}
|
2231
2305
|
}
|
2232
|
-
|
2306
|
+
})
|
2233
2307
|
)
|
2234
2308
|
};
|
2235
2309
|
};
|
2236
|
-
|
2237
|
-
|
2238
|
-
|
2239
|
-
|
2240
|
-
|
2241
|
-
|
2242
|
-
|
2243
|
-
|
2244
|
-
|
2245
|
-
|
2246
|
-
|
2247
|
-
|
2248
|
-
|
2249
|
-
|
2250
|
-
|
2251
|
-
|
2252
|
-
|
2253
|
-
)) != null ? _a2 : parseAnyDef()
|
2254
|
-
};
|
2255
|
-
},
|
2256
|
-
{}
|
2257
|
-
) : void 0;
|
2258
|
-
const name = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
|
2259
|
-
const main = (_a = parseDef(
|
2260
|
-
schema._def,
|
2261
|
-
name === void 0 ? refs : {
|
2262
|
-
...refs,
|
2263
|
-
currentPath: [...refs.basePath, refs.definitionPath, name]
|
2264
|
-
},
|
2265
|
-
false
|
2266
|
-
)) != null ? _a : parseAnyDef();
|
2267
|
-
const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
|
2268
|
-
if (title !== void 0) {
|
2269
|
-
main.title = title;
|
2310
|
+
var createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
2311
|
+
const responseBody = await response.text();
|
2312
|
+
const parsedResult = await safeParseJSON({
|
2313
|
+
text: responseBody,
|
2314
|
+
schema: responseSchema
|
2315
|
+
});
|
2316
|
+
const responseHeaders = extractResponseHeaders(response);
|
2317
|
+
if (!parsedResult.success) {
|
2318
|
+
throw new import_provider11.APICallError({
|
2319
|
+
message: "Invalid JSON response",
|
2320
|
+
cause: parsedResult.error,
|
2321
|
+
statusCode: response.status,
|
2322
|
+
responseHeaders,
|
2323
|
+
responseBody,
|
2324
|
+
url,
|
2325
|
+
requestBodyValues
|
2326
|
+
});
|
2270
2327
|
}
|
2271
|
-
const combined = name === void 0 ? definitions ? {
|
2272
|
-
...main,
|
2273
|
-
[refs.definitionPath]: definitions
|
2274
|
-
} : main : {
|
2275
|
-
$ref: [
|
2276
|
-
...refs.$refStrategy === "relative" ? [] : refs.basePath,
|
2277
|
-
refs.definitionPath,
|
2278
|
-
name
|
2279
|
-
].join("/"),
|
2280
|
-
[refs.definitionPath]: {
|
2281
|
-
...definitions,
|
2282
|
-
[name]: main
|
2283
|
-
}
|
2284
|
-
};
|
2285
|
-
combined.$schema = "http://json-schema.org/draft-07/schema#";
|
2286
|
-
return combined;
|
2287
|
-
};
|
2288
|
-
|
2289
|
-
// src/zod-to-json-schema/index.ts
|
2290
|
-
var zod_to_json_schema_default = zodToJsonSchema;
|
2291
|
-
|
2292
|
-
// src/schema.ts
|
2293
|
-
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
2294
|
-
function lazySchema(createSchema) {
|
2295
|
-
let schema;
|
2296
|
-
return () => {
|
2297
|
-
if (schema == null) {
|
2298
|
-
schema = createSchema();
|
2299
|
-
}
|
2300
|
-
return schema;
|
2301
|
-
};
|
2302
|
-
}
|
2303
|
-
function jsonSchema(jsonSchema2, {
|
2304
|
-
validate
|
2305
|
-
} = {}) {
|
2306
2328
|
return {
|
2307
|
-
|
2308
|
-
|
2309
|
-
|
2310
|
-
[validatorSymbol]: true,
|
2311
|
-
get jsonSchema() {
|
2312
|
-
if (typeof jsonSchema2 === "function") {
|
2313
|
-
jsonSchema2 = jsonSchema2();
|
2314
|
-
}
|
2315
|
-
return jsonSchema2;
|
2316
|
-
},
|
2317
|
-
validate
|
2329
|
+
responseHeaders,
|
2330
|
+
value: parsedResult.value,
|
2331
|
+
rawValue: parsedResult.rawValue
|
2318
2332
|
};
|
2319
|
-
}
|
2320
|
-
|
2321
|
-
|
2322
|
-
|
2323
|
-
|
2324
|
-
|
2325
|
-
|
2326
|
-
|
2327
|
-
|
2328
|
-
|
2329
|
-
|
2330
|
-
|
2331
|
-
switch (vendor) {
|
2332
|
-
case "zod": {
|
2333
|
-
return zodSchema(
|
2334
|
-
standardSchema2
|
2335
|
-
);
|
2336
|
-
}
|
2337
|
-
case "arktype": {
|
2338
|
-
return standardSchemaWithJsonSchemaResolver(
|
2339
|
-
standardSchema2,
|
2340
|
-
arktypeToJsonSchema
|
2341
|
-
);
|
2342
|
-
}
|
2343
|
-
case "effect": {
|
2344
|
-
return standardSchemaWithJsonSchemaResolver(
|
2345
|
-
standardSchema2,
|
2346
|
-
effectToJsonSchema
|
2347
|
-
);
|
2348
|
-
}
|
2349
|
-
case "valibot": {
|
2350
|
-
return standardSchemaWithJsonSchemaResolver(
|
2351
|
-
standardSchema2,
|
2352
|
-
valibotToJsonSchema
|
2353
|
-
);
|
2354
|
-
}
|
2355
|
-
default: {
|
2356
|
-
return standardSchemaWithJsonSchemaResolver(standardSchema2, () => {
|
2357
|
-
throw new Error(`Unsupported standard schema vendor: ${vendor}`);
|
2358
|
-
});
|
2359
|
-
}
|
2333
|
+
};
|
2334
|
+
var createBinaryResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
2335
|
+
const responseHeaders = extractResponseHeaders(response);
|
2336
|
+
if (!response.body) {
|
2337
|
+
throw new import_provider11.APICallError({
|
2338
|
+
message: "Response body is empty",
|
2339
|
+
url,
|
2340
|
+
requestBodyValues,
|
2341
|
+
statusCode: response.status,
|
2342
|
+
responseHeaders,
|
2343
|
+
responseBody: void 0
|
2344
|
+
});
|
2360
2345
|
}
|
2361
|
-
|
2362
|
-
|
2363
|
-
|
2364
|
-
|
2365
|
-
|
2366
|
-
|
2367
|
-
|
2368
|
-
|
2369
|
-
|
2370
|
-
|
2371
|
-
|
2372
|
-
|
2373
|
-
|
2374
|
-
|
2375
|
-
|
2376
|
-
|
2377
|
-
var _a;
|
2378
|
-
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
2379
|
-
return jsonSchema(
|
2380
|
-
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
2381
|
-
() => zod_to_json_schema_default(zodSchema2, {
|
2382
|
-
$refStrategy: useReferences ? "root" : "none"
|
2383
|
-
}),
|
2384
|
-
{
|
2385
|
-
validate: async (value) => {
|
2386
|
-
const result = await zodSchema2.safeParseAsync(value);
|
2387
|
-
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
2388
|
-
}
|
2389
|
-
}
|
2390
|
-
);
|
2391
|
-
}
|
2392
|
-
function zod4Schema(zodSchema2, options) {
|
2393
|
-
var _a;
|
2394
|
-
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
2395
|
-
return jsonSchema(
|
2396
|
-
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
2397
|
-
() => z4.toJSONSchema(zodSchema2, {
|
2398
|
-
target: "draft-7",
|
2399
|
-
io: "output",
|
2400
|
-
reused: useReferences ? "ref" : "inline"
|
2401
|
-
}),
|
2402
|
-
{
|
2403
|
-
validate: async (value) => {
|
2404
|
-
const result = await z4.safeParseAsync(zodSchema2, value);
|
2405
|
-
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
2406
|
-
}
|
2407
|
-
}
|
2408
|
-
);
|
2409
|
-
}
|
2410
|
-
function isZod4Schema(zodSchema2) {
|
2411
|
-
return "_zod" in zodSchema2;
|
2412
|
-
}
|
2413
|
-
function zodSchema(zodSchema2, options) {
|
2414
|
-
if (isZod4Schema(zodSchema2)) {
|
2415
|
-
return zod4Schema(zodSchema2, options);
|
2416
|
-
} else {
|
2417
|
-
return zod3Schema(zodSchema2, options);
|
2346
|
+
try {
|
2347
|
+
const buffer = await response.arrayBuffer();
|
2348
|
+
return {
|
2349
|
+
responseHeaders,
|
2350
|
+
value: new Uint8Array(buffer)
|
2351
|
+
};
|
2352
|
+
} catch (error) {
|
2353
|
+
throw new import_provider11.APICallError({
|
2354
|
+
message: "Failed to read response as array buffer",
|
2355
|
+
url,
|
2356
|
+
requestBodyValues,
|
2357
|
+
statusCode: response.status,
|
2358
|
+
responseHeaders,
|
2359
|
+
responseBody: void 0,
|
2360
|
+
cause: error
|
2361
|
+
});
|
2418
2362
|
}
|
2419
|
-
}
|
2363
|
+
};
|
2364
|
+
var createStatusCodeErrorResponseHandler = () => async ({ response, url, requestBodyValues }) => {
|
2365
|
+
const responseHeaders = extractResponseHeaders(response);
|
2366
|
+
const responseBody = await response.text();
|
2367
|
+
return {
|
2368
|
+
responseHeaders,
|
2369
|
+
value: new import_provider11.APICallError({
|
2370
|
+
message: response.statusText,
|
2371
|
+
url,
|
2372
|
+
requestBodyValues,
|
2373
|
+
statusCode: response.status,
|
2374
|
+
responseHeaders,
|
2375
|
+
responseBody
|
2376
|
+
})
|
2377
|
+
};
|
2378
|
+
};
|
2420
2379
|
|
2421
2380
|
// src/uint8-utils.ts
|
2422
2381
|
var { btoa, atob } = globalThis;
|
@@ -2473,7 +2432,6 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
2473
2432
|
EventSourceParserStream,
|
2474
2433
|
VERSION,
|
2475
2434
|
asSchema,
|
2476
|
-
asValidator,
|
2477
2435
|
combineHeaders,
|
2478
2436
|
convertAsyncIteratorToReadableStream,
|
2479
2437
|
convertBase64ToUint8Array,
|
@@ -2500,10 +2458,8 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
2500
2458
|
isAbortError,
|
2501
2459
|
isParsableJson,
|
2502
2460
|
isUrlSupported,
|
2503
|
-
isValidator,
|
2504
2461
|
jsonSchema,
|
2505
2462
|
lazySchema,
|
2506
|
-
lazyValidator,
|
2507
2463
|
loadApiKey,
|
2508
2464
|
loadOptionalSetting,
|
2509
2465
|
loadSetting,
|
@@ -2518,10 +2474,8 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
2518
2474
|
resolve,
|
2519
2475
|
safeParseJSON,
|
2520
2476
|
safeValidateTypes,
|
2521
|
-
standardSchemaValidator,
|
2522
2477
|
tool,
|
2523
2478
|
validateTypes,
|
2524
|
-
validator,
|
2525
2479
|
withUserAgentSuffix,
|
2526
2480
|
withoutTrailingSlash,
|
2527
2481
|
zodSchema,
|