@livekit/agents 1.0.14 → 1.0.15

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.
Files changed (50) hide show
  1. package/dist/llm/tool_context.cjs +3 -2
  2. package/dist/llm/tool_context.cjs.map +1 -1
  3. package/dist/llm/tool_context.d.cts +37 -11
  4. package/dist/llm/tool_context.d.ts +37 -11
  5. package/dist/llm/tool_context.d.ts.map +1 -1
  6. package/dist/llm/tool_context.js +4 -3
  7. package/dist/llm/tool_context.js.map +1 -1
  8. package/dist/llm/tool_context.test.cjs +197 -0
  9. package/dist/llm/tool_context.test.cjs.map +1 -1
  10. package/dist/llm/tool_context.test.js +175 -0
  11. package/dist/llm/tool_context.test.js.map +1 -1
  12. package/dist/llm/utils.cjs +17 -11
  13. package/dist/llm/utils.cjs.map +1 -1
  14. package/dist/llm/utils.d.cts +1 -2
  15. package/dist/llm/utils.d.ts +1 -2
  16. package/dist/llm/utils.d.ts.map +1 -1
  17. package/dist/llm/utils.js +17 -11
  18. package/dist/llm/utils.js.map +1 -1
  19. package/dist/llm/zod-utils.cjs +99 -0
  20. package/dist/llm/zod-utils.cjs.map +1 -0
  21. package/dist/llm/zod-utils.d.cts +65 -0
  22. package/dist/llm/zod-utils.d.ts +65 -0
  23. package/dist/llm/zod-utils.d.ts.map +1 -0
  24. package/dist/llm/zod-utils.js +61 -0
  25. package/dist/llm/zod-utils.js.map +1 -0
  26. package/dist/llm/zod-utils.test.cjs +389 -0
  27. package/dist/llm/zod-utils.test.cjs.map +1 -0
  28. package/dist/llm/zod-utils.test.js +372 -0
  29. package/dist/llm/zod-utils.test.js.map +1 -0
  30. package/dist/vad.cjs +16 -0
  31. package/dist/vad.cjs.map +1 -1
  32. package/dist/vad.d.cts +6 -0
  33. package/dist/vad.d.ts +6 -0
  34. package/dist/vad.d.ts.map +1 -1
  35. package/dist/vad.js +16 -0
  36. package/dist/vad.js.map +1 -1
  37. package/dist/voice/generation.cjs +8 -3
  38. package/dist/voice/generation.cjs.map +1 -1
  39. package/dist/voice/generation.d.ts.map +1 -1
  40. package/dist/voice/generation.js +8 -3
  41. package/dist/voice/generation.js.map +1 -1
  42. package/package.json +5 -4
  43. package/src/llm/__snapshots__/zod-utils.test.ts.snap +341 -0
  44. package/src/llm/tool_context.test.ts +210 -1
  45. package/src/llm/tool_context.ts +57 -17
  46. package/src/llm/utils.ts +18 -15
  47. package/src/llm/zod-utils.test.ts +476 -0
  48. package/src/llm/zod-utils.ts +144 -0
  49. package/src/vad.ts +18 -0
  50. package/src/voice/generation.ts +8 -3
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var zod_utils_exports = {};
30
+ __export(zod_utils_exports, {
31
+ isZod4Schema: () => isZod4Schema,
32
+ isZodObjectSchema: () => isZodObjectSchema,
33
+ isZodSchema: () => isZodSchema,
34
+ parseZodSchema: () => parseZodSchema,
35
+ zodSchemaToJsonSchema: () => zodSchemaToJsonSchema
36
+ });
37
+ module.exports = __toCommonJS(zod_utils_exports);
38
+ var import_zod_to_json_schema = require("zod-to-json-schema");
39
+ var z4 = __toESM(require("zod/v4"), 1);
40
+ function isZod4Schema(schema) {
41
+ return "_zod" in schema;
42
+ }
43
+ function isZodSchema(value) {
44
+ if (typeof value !== "object" || value === null) {
45
+ return false;
46
+ }
47
+ if ("_zod" in value) {
48
+ return true;
49
+ }
50
+ if ("_def" in value && typeof value._def === "object" && value._def !== null) {
51
+ const def = value._def;
52
+ if ("typeName" in def) {
53
+ return true;
54
+ }
55
+ }
56
+ return false;
57
+ }
58
+ function isZodObjectSchema(schema) {
59
+ var _a, _b, _c, _d;
60
+ const schemaWithInternals = schema;
61
+ if (isZod4Schema(schema)) {
62
+ return ((_a = schemaWithInternals._def) == null ? void 0 : _a.type) === "object" || ((_c = (_b = schemaWithInternals._zod) == null ? void 0 : _b.traits) == null ? void 0 : _c.has("ZodObject")) || false;
63
+ }
64
+ return ((_d = schemaWithInternals._def) == null ? void 0 : _d.typeName) === "ZodObject";
65
+ }
66
+ function zodSchemaToJsonSchema(schema, isOpenai = true) {
67
+ if (isZod4Schema(schema)) {
68
+ return z4.toJSONSchema(schema, {
69
+ target: "draft-7",
70
+ io: "output",
71
+ reused: "inline"
72
+ // Don't use references by default (to support openapi conversion for google)
73
+ });
74
+ } else {
75
+ return (0, import_zod_to_json_schema.zodToJsonSchema)(schema, {
76
+ target: isOpenai ? "openAi" : "jsonSchema7",
77
+ $refStrategy: "none"
78
+ // Don't use references by default (to support openapi conversion for google)
79
+ });
80
+ }
81
+ }
82
+ async function parseZodSchema(schema, value) {
83
+ if (isZod4Schema(schema)) {
84
+ const result = await z4.safeParseAsync(schema, value);
85
+ return result;
86
+ } else {
87
+ const result = await schema.safeParseAsync(value);
88
+ return result;
89
+ }
90
+ }
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ isZod4Schema,
94
+ isZodObjectSchema,
95
+ isZodSchema,
96
+ parseZodSchema,
97
+ zodSchemaToJsonSchema
98
+ });
99
+ //# sourceMappingURL=zod-utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/llm/zod-utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JSONSchema7 } from 'json-schema';\nimport { zodToJsonSchema as zodToJsonSchemaV3 } from 'zod-to-json-schema';\nimport type * as z3 from 'zod/v3';\nimport * as z4 from 'zod/v4';\n\n/**\n * Result type from Zod schema parsing.\n */\nexport type ZodParseResult<T = unknown> =\n | { success: true; data: T }\n | { success: false; error: unknown };\n\n/**\n * Type definition for Zod schemas that works with both v3 and v4.\n * Uses a union type of both Zod v3 and v4 schema types.\n *\n * Adapted from Vercel AI SDK's zodSchema function signature.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L278-L281\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ZodSchema = z4.core.$ZodType<any, any> | z3.Schema<any, z3.ZodTypeDef, any>;\n\n/**\n * Detects if a schema is a Zod v4 schema.\n * Zod v4 schemas have a `_zod` property that v3 schemas don't have.\n *\n * @param schema - The schema to check\n * @returns True if the schema is a Zod v4 schema\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required to match Zod v4's type signature\nexport function isZod4Schema(schema: ZodSchema): schema is z4.core.$ZodType<any, any> {\n // https://zod.dev/library-authors?id=how-to-support-zod-3-and-zod-4-simultaneously\n return '_zod' in schema;\n}\n\n/**\n * Checks if a value is a Zod schema (either v3 or v4).\n *\n * @param value - The value to check\n * @returns True if the value is a Zod schema\n */\nexport function isZodSchema(value: unknown): value is ZodSchema {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n // Check for v4 schema (_zod property)\n if ('_zod' in value) {\n return true;\n }\n\n // Check for v3 schema (_def property with typeName)\n if ('_def' in value && typeof value._def === 'object' && value._def !== null) {\n const def = value._def as Record<string, unknown>;\n if ('typeName' in def) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Checks if a Zod schema is an object schema.\n *\n * @param schema - The schema to check\n * @returns True if the schema is an object schema\n */\nexport function isZodObjectSchema(schema: ZodSchema): boolean {\n // Need to access internal Zod properties to check schema type\n const schemaWithInternals = schema as {\n _def?: { type?: string; typeName?: string };\n _zod?: { traits?: Set<string> };\n };\n\n // Check for v4 schema first\n if (isZod4Schema(schema)) {\n // v4 uses _def.type and _zod.traits\n return (\n schemaWithInternals._def?.type === 'object' ||\n schemaWithInternals._zod?.traits?.has('ZodObject') ||\n false\n );\n }\n\n // v3 uses _def.typeName\n return schemaWithInternals._def?.typeName === 'ZodObject';\n}\n\n/**\n * Converts a Zod schema to JSON Schema format.\n * Handles both Zod v3 and v4 schemas automatically.\n *\n * Adapted from Vercel AI SDK's zod3Schema and zod4Schema functions.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L237-L269\n *\n * @param schema - The Zod schema to convert\n * @param isOpenai - Whether to use OpenAI-specific formatting (default: true)\n * @returns A JSON Schema representation of the Zod schema\n */\nexport function zodSchemaToJsonSchema(schema: ZodSchema, isOpenai: boolean = true): JSONSchema7 {\n if (isZod4Schema(schema)) {\n // Zod v4 has native toJSONSchema support\n // Configuration adapted from Vercel AI SDK to support OpenAPI conversion for Google\n // Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L255-L258\n return z4.toJSONSchema(schema, {\n target: 'draft-7',\n io: 'output',\n reused: 'inline', // Don't use references by default (to support openapi conversion for google)\n }) as JSONSchema7;\n } else {\n // Zod v3 requires the zod-to-json-schema library\n // Configuration adapted from Vercel AI SDK\n // $refStrategy: 'none' is equivalent to v4's reused: 'inline'\n return zodToJsonSchemaV3(schema, {\n target: isOpenai ? 'openAi' : 'jsonSchema7',\n $refStrategy: 'none', // Don't use references by default (to support openapi conversion for google)\n }) as JSONSchema7;\n }\n}\n\n/**\n * Parses a value against a Zod schema.\n * Handles both Zod v3 and v4 parse APIs automatically.\n *\n * @param schema - The Zod schema to parse against\n * @param value - The value to parse\n * @returns A promise that resolves to the parse result\n */\nexport async function parseZodSchema<T = unknown>(\n schema: ZodSchema,\n value: unknown,\n): Promise<ZodParseResult<T>> {\n if (isZod4Schema(schema)) {\n const result = await z4.safeParseAsync(schema, value);\n return result as ZodParseResult<T>;\n } else {\n const result = await schema.safeParseAsync(value);\n return result as ZodParseResult<T>;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,gCAAqD;AAErD,SAAoB;AA2Bb,SAAS,aAAa,QAAyD;AAEpF,SAAO,UAAU;AACnB;AAQO,SAAS,YAAY,OAAoC;AAC9D,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,WAAO;AAAA,EACT;AAGA,MAAI,UAAU,OAAO;AACnB,WAAO;AAAA,EACT;AAGA,MAAI,UAAU,SAAS,OAAO,MAAM,SAAS,YAAY,MAAM,SAAS,MAAM;AAC5E,UAAM,MAAM,MAAM;AAClB,QAAI,cAAc,KAAK;AACrB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;AAQO,SAAS,kBAAkB,QAA4B;AAvE9D;AAyEE,QAAM,sBAAsB;AAM5B,MAAI,aAAa,MAAM,GAAG;AAExB,aACE,yBAAoB,SAApB,mBAA0B,UAAS,cACnC,+BAAoB,SAApB,mBAA0B,WAA1B,mBAAkC,IAAI,iBACtC;AAAA,EAEJ;AAGA,WAAO,yBAAoB,SAApB,mBAA0B,cAAa;AAChD;AAaO,SAAS,sBAAsB,QAAmB,WAAoB,MAAmB;AAC9F,MAAI,aAAa,MAAM,GAAG;AAIxB,WAAO,GAAG,aAAa,QAAQ;AAAA,MAC7B,QAAQ;AAAA,MACR,IAAI;AAAA,MACJ,QAAQ;AAAA;AAAA,IACV,CAAC;AAAA,EACH,OAAO;AAIL,eAAO,0BAAAA,iBAAkB,QAAQ;AAAA,MAC/B,QAAQ,WAAW,WAAW;AAAA,MAC9B,cAAc;AAAA;AAAA,IAChB,CAAC;AAAA,EACH;AACF;AAUA,eAAsB,eACpB,QACA,OAC4B;AAC5B,MAAI,aAAa,MAAM,GAAG;AACxB,UAAM,SAAS,MAAM,GAAG,eAAe,QAAQ,KAAK;AACpD,WAAO;AAAA,EACT,OAAO;AACL,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,WAAO;AAAA,EACT;AACF;","names":["zodToJsonSchemaV3"]}
@@ -0,0 +1,65 @@
1
+ import type { JSONSchema7 } from 'json-schema';
2
+ import type * as z3 from 'zod/v3';
3
+ import * as z4 from 'zod/v4';
4
+ /**
5
+ * Result type from Zod schema parsing.
6
+ */
7
+ export type ZodParseResult<T = unknown> = {
8
+ success: true;
9
+ data: T;
10
+ } | {
11
+ success: false;
12
+ error: unknown;
13
+ };
14
+ /**
15
+ * Type definition for Zod schemas that works with both v3 and v4.
16
+ * Uses a union type of both Zod v3 and v4 schema types.
17
+ *
18
+ * Adapted from Vercel AI SDK's zodSchema function signature.
19
+ * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L278-L281
20
+ */
21
+ export type ZodSchema = z4.core.$ZodType<any, any> | z3.Schema<any, z3.ZodTypeDef, any>;
22
+ /**
23
+ * Detects if a schema is a Zod v4 schema.
24
+ * Zod v4 schemas have a `_zod` property that v3 schemas don't have.
25
+ *
26
+ * @param schema - The schema to check
27
+ * @returns True if the schema is a Zod v4 schema
28
+ */
29
+ export declare function isZod4Schema(schema: ZodSchema): schema is z4.core.$ZodType<any, any>;
30
+ /**
31
+ * Checks if a value is a Zod schema (either v3 or v4).
32
+ *
33
+ * @param value - The value to check
34
+ * @returns True if the value is a Zod schema
35
+ */
36
+ export declare function isZodSchema(value: unknown): value is ZodSchema;
37
+ /**
38
+ * Checks if a Zod schema is an object schema.
39
+ *
40
+ * @param schema - The schema to check
41
+ * @returns True if the schema is an object schema
42
+ */
43
+ export declare function isZodObjectSchema(schema: ZodSchema): boolean;
44
+ /**
45
+ * Converts a Zod schema to JSON Schema format.
46
+ * Handles both Zod v3 and v4 schemas automatically.
47
+ *
48
+ * Adapted from Vercel AI SDK's zod3Schema and zod4Schema functions.
49
+ * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L237-L269
50
+ *
51
+ * @param schema - The Zod schema to convert
52
+ * @param isOpenai - Whether to use OpenAI-specific formatting (default: true)
53
+ * @returns A JSON Schema representation of the Zod schema
54
+ */
55
+ export declare function zodSchemaToJsonSchema(schema: ZodSchema, isOpenai?: boolean): JSONSchema7;
56
+ /**
57
+ * Parses a value against a Zod schema.
58
+ * Handles both Zod v3 and v4 parse APIs automatically.
59
+ *
60
+ * @param schema - The Zod schema to parse against
61
+ * @param value - The value to parse
62
+ * @returns A promise that resolves to the parse result
63
+ */
64
+ export declare function parseZodSchema<T = unknown>(schema: ZodSchema, value: unknown): Promise<ZodParseResult<T>>;
65
+ //# sourceMappingURL=zod-utils.d.ts.map
@@ -0,0 +1,65 @@
1
+ import type { JSONSchema7 } from 'json-schema';
2
+ import type * as z3 from 'zod/v3';
3
+ import * as z4 from 'zod/v4';
4
+ /**
5
+ * Result type from Zod schema parsing.
6
+ */
7
+ export type ZodParseResult<T = unknown> = {
8
+ success: true;
9
+ data: T;
10
+ } | {
11
+ success: false;
12
+ error: unknown;
13
+ };
14
+ /**
15
+ * Type definition for Zod schemas that works with both v3 and v4.
16
+ * Uses a union type of both Zod v3 and v4 schema types.
17
+ *
18
+ * Adapted from Vercel AI SDK's zodSchema function signature.
19
+ * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L278-L281
20
+ */
21
+ export type ZodSchema = z4.core.$ZodType<any, any> | z3.Schema<any, z3.ZodTypeDef, any>;
22
+ /**
23
+ * Detects if a schema is a Zod v4 schema.
24
+ * Zod v4 schemas have a `_zod` property that v3 schemas don't have.
25
+ *
26
+ * @param schema - The schema to check
27
+ * @returns True if the schema is a Zod v4 schema
28
+ */
29
+ export declare function isZod4Schema(schema: ZodSchema): schema is z4.core.$ZodType<any, any>;
30
+ /**
31
+ * Checks if a value is a Zod schema (either v3 or v4).
32
+ *
33
+ * @param value - The value to check
34
+ * @returns True if the value is a Zod schema
35
+ */
36
+ export declare function isZodSchema(value: unknown): value is ZodSchema;
37
+ /**
38
+ * Checks if a Zod schema is an object schema.
39
+ *
40
+ * @param schema - The schema to check
41
+ * @returns True if the schema is an object schema
42
+ */
43
+ export declare function isZodObjectSchema(schema: ZodSchema): boolean;
44
+ /**
45
+ * Converts a Zod schema to JSON Schema format.
46
+ * Handles both Zod v3 and v4 schemas automatically.
47
+ *
48
+ * Adapted from Vercel AI SDK's zod3Schema and zod4Schema functions.
49
+ * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L237-L269
50
+ *
51
+ * @param schema - The Zod schema to convert
52
+ * @param isOpenai - Whether to use OpenAI-specific formatting (default: true)
53
+ * @returns A JSON Schema representation of the Zod schema
54
+ */
55
+ export declare function zodSchemaToJsonSchema(schema: ZodSchema, isOpenai?: boolean): JSONSchema7;
56
+ /**
57
+ * Parses a value against a Zod schema.
58
+ * Handles both Zod v3 and v4 parse APIs automatically.
59
+ *
60
+ * @param schema - The Zod schema to parse against
61
+ * @param value - The value to parse
62
+ * @returns A promise that resolves to the parse result
63
+ */
64
+ export declare function parseZodSchema<T = unknown>(schema: ZodSchema, value: unknown): Promise<ZodParseResult<T>>;
65
+ //# sourceMappingURL=zod-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod-utils.d.ts","sourceRoot":"","sources":["../../src/llm/zod-utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,KAAK,KAAK,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE7B;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,GAAG,OAAO,IAClC;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,GAC1B;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAEvC;;;;;;GAMG;AAEH,MAAM,MAAM,SAAS,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AAExF;;;;;;GAMG;AAEH,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,IAAI,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,CAGpF;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAmB9D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAmB5D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,GAAE,OAAc,GAAG,WAAW,CAmB9F;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,CAAC,GAAG,OAAO,EAC9C,MAAM,EAAE,SAAS,EACjB,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAQ5B"}
@@ -0,0 +1,61 @@
1
+ import { zodToJsonSchema as zodToJsonSchemaV3 } from "zod-to-json-schema";
2
+ import * as z4 from "zod/v4";
3
+ function isZod4Schema(schema) {
4
+ return "_zod" in schema;
5
+ }
6
+ function isZodSchema(value) {
7
+ if (typeof value !== "object" || value === null) {
8
+ return false;
9
+ }
10
+ if ("_zod" in value) {
11
+ return true;
12
+ }
13
+ if ("_def" in value && typeof value._def === "object" && value._def !== null) {
14
+ const def = value._def;
15
+ if ("typeName" in def) {
16
+ return true;
17
+ }
18
+ }
19
+ return false;
20
+ }
21
+ function isZodObjectSchema(schema) {
22
+ var _a, _b, _c, _d;
23
+ const schemaWithInternals = schema;
24
+ if (isZod4Schema(schema)) {
25
+ return ((_a = schemaWithInternals._def) == null ? void 0 : _a.type) === "object" || ((_c = (_b = schemaWithInternals._zod) == null ? void 0 : _b.traits) == null ? void 0 : _c.has("ZodObject")) || false;
26
+ }
27
+ return ((_d = schemaWithInternals._def) == null ? void 0 : _d.typeName) === "ZodObject";
28
+ }
29
+ function zodSchemaToJsonSchema(schema, isOpenai = true) {
30
+ if (isZod4Schema(schema)) {
31
+ return z4.toJSONSchema(schema, {
32
+ target: "draft-7",
33
+ io: "output",
34
+ reused: "inline"
35
+ // Don't use references by default (to support openapi conversion for google)
36
+ });
37
+ } else {
38
+ return zodToJsonSchemaV3(schema, {
39
+ target: isOpenai ? "openAi" : "jsonSchema7",
40
+ $refStrategy: "none"
41
+ // Don't use references by default (to support openapi conversion for google)
42
+ });
43
+ }
44
+ }
45
+ async function parseZodSchema(schema, value) {
46
+ if (isZod4Schema(schema)) {
47
+ const result = await z4.safeParseAsync(schema, value);
48
+ return result;
49
+ } else {
50
+ const result = await schema.safeParseAsync(value);
51
+ return result;
52
+ }
53
+ }
54
+ export {
55
+ isZod4Schema,
56
+ isZodObjectSchema,
57
+ isZodSchema,
58
+ parseZodSchema,
59
+ zodSchemaToJsonSchema
60
+ };
61
+ //# sourceMappingURL=zod-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/llm/zod-utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JSONSchema7 } from 'json-schema';\nimport { zodToJsonSchema as zodToJsonSchemaV3 } from 'zod-to-json-schema';\nimport type * as z3 from 'zod/v3';\nimport * as z4 from 'zod/v4';\n\n/**\n * Result type from Zod schema parsing.\n */\nexport type ZodParseResult<T = unknown> =\n | { success: true; data: T }\n | { success: false; error: unknown };\n\n/**\n * Type definition for Zod schemas that works with both v3 and v4.\n * Uses a union type of both Zod v3 and v4 schema types.\n *\n * Adapted from Vercel AI SDK's zodSchema function signature.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L278-L281\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ZodSchema = z4.core.$ZodType<any, any> | z3.Schema<any, z3.ZodTypeDef, any>;\n\n/**\n * Detects if a schema is a Zod v4 schema.\n * Zod v4 schemas have a `_zod` property that v3 schemas don't have.\n *\n * @param schema - The schema to check\n * @returns True if the schema is a Zod v4 schema\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required to match Zod v4's type signature\nexport function isZod4Schema(schema: ZodSchema): schema is z4.core.$ZodType<any, any> {\n // https://zod.dev/library-authors?id=how-to-support-zod-3-and-zod-4-simultaneously\n return '_zod' in schema;\n}\n\n/**\n * Checks if a value is a Zod schema (either v3 or v4).\n *\n * @param value - The value to check\n * @returns True if the value is a Zod schema\n */\nexport function isZodSchema(value: unknown): value is ZodSchema {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n // Check for v4 schema (_zod property)\n if ('_zod' in value) {\n return true;\n }\n\n // Check for v3 schema (_def property with typeName)\n if ('_def' in value && typeof value._def === 'object' && value._def !== null) {\n const def = value._def as Record<string, unknown>;\n if ('typeName' in def) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Checks if a Zod schema is an object schema.\n *\n * @param schema - The schema to check\n * @returns True if the schema is an object schema\n */\nexport function isZodObjectSchema(schema: ZodSchema): boolean {\n // Need to access internal Zod properties to check schema type\n const schemaWithInternals = schema as {\n _def?: { type?: string; typeName?: string };\n _zod?: { traits?: Set<string> };\n };\n\n // Check for v4 schema first\n if (isZod4Schema(schema)) {\n // v4 uses _def.type and _zod.traits\n return (\n schemaWithInternals._def?.type === 'object' ||\n schemaWithInternals._zod?.traits?.has('ZodObject') ||\n false\n );\n }\n\n // v3 uses _def.typeName\n return schemaWithInternals._def?.typeName === 'ZodObject';\n}\n\n/**\n * Converts a Zod schema to JSON Schema format.\n * Handles both Zod v3 and v4 schemas automatically.\n *\n * Adapted from Vercel AI SDK's zod3Schema and zod4Schema functions.\n * Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L237-L269\n *\n * @param schema - The Zod schema to convert\n * @param isOpenai - Whether to use OpenAI-specific formatting (default: true)\n * @returns A JSON Schema representation of the Zod schema\n */\nexport function zodSchemaToJsonSchema(schema: ZodSchema, isOpenai: boolean = true): JSONSchema7 {\n if (isZod4Schema(schema)) {\n // Zod v4 has native toJSONSchema support\n // Configuration adapted from Vercel AI SDK to support OpenAPI conversion for Google\n // Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L255-L258\n return z4.toJSONSchema(schema, {\n target: 'draft-7',\n io: 'output',\n reused: 'inline', // Don't use references by default (to support openapi conversion for google)\n }) as JSONSchema7;\n } else {\n // Zod v3 requires the zod-to-json-schema library\n // Configuration adapted from Vercel AI SDK\n // $refStrategy: 'none' is equivalent to v4's reused: 'inline'\n return zodToJsonSchemaV3(schema, {\n target: isOpenai ? 'openAi' : 'jsonSchema7',\n $refStrategy: 'none', // Don't use references by default (to support openapi conversion for google)\n }) as JSONSchema7;\n }\n}\n\n/**\n * Parses a value against a Zod schema.\n * Handles both Zod v3 and v4 parse APIs automatically.\n *\n * @param schema - The Zod schema to parse against\n * @param value - The value to parse\n * @returns A promise that resolves to the parse result\n */\nexport async function parseZodSchema<T = unknown>(\n schema: ZodSchema,\n value: unknown,\n): Promise<ZodParseResult<T>> {\n if (isZod4Schema(schema)) {\n const result = await z4.safeParseAsync(schema, value);\n return result as ZodParseResult<T>;\n } else {\n const result = await schema.safeParseAsync(value);\n return result as ZodParseResult<T>;\n }\n}\n"],"mappings":"AAIA,SAAS,mBAAmB,yBAAyB;AAErD,YAAY,QAAQ;AA2Bb,SAAS,aAAa,QAAyD;AAEpF,SAAO,UAAU;AACnB;AAQO,SAAS,YAAY,OAAoC;AAC9D,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,WAAO;AAAA,EACT;AAGA,MAAI,UAAU,OAAO;AACnB,WAAO;AAAA,EACT;AAGA,MAAI,UAAU,SAAS,OAAO,MAAM,SAAS,YAAY,MAAM,SAAS,MAAM;AAC5E,UAAM,MAAM,MAAM;AAClB,QAAI,cAAc,KAAK;AACrB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;AAQO,SAAS,kBAAkB,QAA4B;AAvE9D;AAyEE,QAAM,sBAAsB;AAM5B,MAAI,aAAa,MAAM,GAAG;AAExB,aACE,yBAAoB,SAApB,mBAA0B,UAAS,cACnC,+BAAoB,SAApB,mBAA0B,WAA1B,mBAAkC,IAAI,iBACtC;AAAA,EAEJ;AAGA,WAAO,yBAAoB,SAApB,mBAA0B,cAAa;AAChD;AAaO,SAAS,sBAAsB,QAAmB,WAAoB,MAAmB;AAC9F,MAAI,aAAa,MAAM,GAAG;AAIxB,WAAO,GAAG,aAAa,QAAQ;AAAA,MAC7B,QAAQ;AAAA,MACR,IAAI;AAAA,MACJ,QAAQ;AAAA;AAAA,IACV,CAAC;AAAA,EACH,OAAO;AAIL,WAAO,kBAAkB,QAAQ;AAAA,MAC/B,QAAQ,WAAW,WAAW;AAAA,MAC9B,cAAc;AAAA;AAAA,IAChB,CAAC;AAAA,EACH;AACF;AAUA,eAAsB,eACpB,QACA,OAC4B;AAC5B,MAAI,aAAa,MAAM,GAAG;AACxB,UAAM,SAAS,MAAM,GAAG,eAAe,QAAQ,KAAK;AACpD,WAAO;AAAA,EACT,OAAO;AACL,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,WAAO;AAAA,EACT;AACF;","names":[]}