@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.
- package/dist/llm/tool_context.cjs +3 -2
- package/dist/llm/tool_context.cjs.map +1 -1
- package/dist/llm/tool_context.d.cts +37 -11
- package/dist/llm/tool_context.d.ts +37 -11
- package/dist/llm/tool_context.d.ts.map +1 -1
- package/dist/llm/tool_context.js +4 -3
- package/dist/llm/tool_context.js.map +1 -1
- package/dist/llm/tool_context.test.cjs +197 -0
- package/dist/llm/tool_context.test.cjs.map +1 -1
- package/dist/llm/tool_context.test.js +175 -0
- package/dist/llm/tool_context.test.js.map +1 -1
- package/dist/llm/utils.cjs +17 -11
- package/dist/llm/utils.cjs.map +1 -1
- package/dist/llm/utils.d.cts +1 -2
- package/dist/llm/utils.d.ts +1 -2
- package/dist/llm/utils.d.ts.map +1 -1
- package/dist/llm/utils.js +17 -11
- package/dist/llm/utils.js.map +1 -1
- package/dist/llm/zod-utils.cjs +99 -0
- package/dist/llm/zod-utils.cjs.map +1 -0
- package/dist/llm/zod-utils.d.cts +65 -0
- package/dist/llm/zod-utils.d.ts +65 -0
- package/dist/llm/zod-utils.d.ts.map +1 -0
- package/dist/llm/zod-utils.js +61 -0
- package/dist/llm/zod-utils.js.map +1 -0
- package/dist/llm/zod-utils.test.cjs +389 -0
- package/dist/llm/zod-utils.test.cjs.map +1 -0
- package/dist/llm/zod-utils.test.js +372 -0
- package/dist/llm/zod-utils.test.js.map +1 -0
- package/dist/vad.cjs +16 -0
- package/dist/vad.cjs.map +1 -1
- package/dist/vad.d.cts +6 -0
- package/dist/vad.d.ts +6 -0
- package/dist/vad.d.ts.map +1 -1
- package/dist/vad.js +16 -0
- package/dist/vad.js.map +1 -1
- package/dist/voice/generation.cjs +8 -3
- package/dist/voice/generation.cjs.map +1 -1
- package/dist/voice/generation.d.ts.map +1 -1
- package/dist/voice/generation.js +8 -3
- package/dist/voice/generation.js.map +1 -1
- package/package.json +5 -4
- package/src/llm/__snapshots__/zod-utils.test.ts.snap +341 -0
- package/src/llm/tool_context.test.ts +210 -1
- package/src/llm/tool_context.ts +57 -17
- package/src/llm/utils.ts +18 -15
- package/src/llm/zod-utils.test.ts +476 -0
- package/src/llm/zod-utils.ts +144 -0
- package/src/vad.ts +18 -0
- package/src/voice/generation.ts +8 -3
package/src/llm/utils.ts
CHANGED
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
import { VideoBufferType, VideoFrame } from '@livekit/rtc-node';
|
|
5
5
|
import type { JSONSchema7 } from 'json-schema';
|
|
6
6
|
import sharp from 'sharp';
|
|
7
|
-
import { ZodObject } from 'zod';
|
|
8
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
9
7
|
import type { UnknownUserData } from '../voice/run_context.js';
|
|
10
8
|
import type { ChatContext } from './chat_context.js';
|
|
11
9
|
import {
|
|
@@ -15,6 +13,7 @@ import {
|
|
|
15
13
|
type ImageContent,
|
|
16
14
|
} from './chat_context.js';
|
|
17
15
|
import type { ToolContext, ToolInputSchema, ToolOptions } from './tool_context.js';
|
|
16
|
+
import { isZodSchema, parseZodSchema, zodSchemaToJsonSchema } from './zod-utils.js';
|
|
18
17
|
|
|
19
18
|
export interface SerializedImage {
|
|
20
19
|
inferenceDetail: 'auto' | 'high' | 'low';
|
|
@@ -151,15 +150,10 @@ export const createToolOptions = <UserData extends UnknownUserData>(
|
|
|
151
150
|
|
|
152
151
|
/** @internal */
|
|
153
152
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
154
|
-
export const oaiParams = (
|
|
155
|
-
p: ZodObject<any>,
|
|
156
|
-
isOpenai: boolean = true,
|
|
157
|
-
): OpenAIFunctionParameters => {
|
|
153
|
+
export const oaiParams = (schema: any, isOpenai: boolean = true): OpenAIFunctionParameters => {
|
|
158
154
|
// Adapted from https://github.com/vercel/ai/blob/56eb0ee9/packages/provider-utils/src/zod-schema.ts
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
target: isOpenai ? 'openAi' : 'jsonSchema7',
|
|
162
|
-
}) as OpenAIFunctionParameters;
|
|
155
|
+
const jsonSchema = zodSchemaToJsonSchema(schema, isOpenai);
|
|
156
|
+
const { properties, required, additionalProperties } = jsonSchema as OpenAIFunctionParameters;
|
|
163
157
|
|
|
164
158
|
return {
|
|
165
159
|
type: 'object',
|
|
@@ -209,8 +203,17 @@ export async function executeToolCall(
|
|
|
209
203
|
|
|
210
204
|
// Ensure valid arguments schema
|
|
211
205
|
try {
|
|
212
|
-
if (tool.parameters
|
|
213
|
-
|
|
206
|
+
if (isZodSchema(tool.parameters)) {
|
|
207
|
+
const result = await parseZodSchema<object>(tool.parameters, args);
|
|
208
|
+
if (result.success) {
|
|
209
|
+
params = result.data;
|
|
210
|
+
} else {
|
|
211
|
+
return FunctionCallOutput.create({
|
|
212
|
+
callId: toolCall.callId,
|
|
213
|
+
output: `Arguments parsing failed: ${result.error}`,
|
|
214
|
+
isError: true,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
214
217
|
} else {
|
|
215
218
|
params = args;
|
|
216
219
|
}
|
|
@@ -321,8 +324,8 @@ export function computeChatCtxDiff(oldCtx: ChatContext, newCtx: ChatContext): Di
|
|
|
321
324
|
}
|
|
322
325
|
|
|
323
326
|
export function toJsonSchema(schema: ToolInputSchema<any>, isOpenai: boolean = true): JSONSchema7 {
|
|
324
|
-
if (schema
|
|
325
|
-
return
|
|
327
|
+
if (isZodSchema(schema)) {
|
|
328
|
+
return zodSchemaToJsonSchema(schema, isOpenai);
|
|
326
329
|
}
|
|
327
|
-
return schema;
|
|
330
|
+
return schema as JSONSchema7;
|
|
328
331
|
}
|
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import * as z3 from 'zod/v3';
|
|
7
|
+
import * as z4 from 'zod/v4';
|
|
8
|
+
import {
|
|
9
|
+
isZod4Schema,
|
|
10
|
+
isZodObjectSchema,
|
|
11
|
+
isZodSchema,
|
|
12
|
+
parseZodSchema,
|
|
13
|
+
zodSchemaToJsonSchema,
|
|
14
|
+
} from './zod-utils.js';
|
|
15
|
+
|
|
16
|
+
type JSONSchemaProperties = Record<string, Record<string, unknown>>;
|
|
17
|
+
|
|
18
|
+
describe('Zod Utils', () => {
|
|
19
|
+
describe('isZod4Schema', () => {
|
|
20
|
+
it('should detect Zod v4 schemas', () => {
|
|
21
|
+
const v4Schema = z4.string();
|
|
22
|
+
expect(isZod4Schema(v4Schema)).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should detect Zod v3 schemas', () => {
|
|
26
|
+
const v3Schema = z3.string();
|
|
27
|
+
expect(isZod4Schema(v3Schema)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should handle default z import (follows installed version)', () => {
|
|
31
|
+
const schema = z.string();
|
|
32
|
+
expect(typeof isZod4Schema(schema)).toBe('boolean');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('isZodSchema', () => {
|
|
37
|
+
it('should detect Zod v4 schemas', () => {
|
|
38
|
+
const v4Schema = z4.object({ name: z4.string() });
|
|
39
|
+
expect(isZodSchema(v4Schema)).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should detect Zod v3 schemas', () => {
|
|
43
|
+
const v3Schema = z3.object({ name: z3.string() });
|
|
44
|
+
expect(isZodSchema(v3Schema)).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should return false for non-Zod values', () => {
|
|
48
|
+
expect(isZodSchema({})).toBe(false);
|
|
49
|
+
expect(isZodSchema(null)).toBe(false);
|
|
50
|
+
expect(isZodSchema(undefined)).toBe(false);
|
|
51
|
+
expect(isZodSchema('string')).toBe(false);
|
|
52
|
+
expect(isZodSchema(123)).toBe(false);
|
|
53
|
+
expect(isZodSchema({ _def: {} })).toBe(false); // missing typeName
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('isZodObjectSchema', () => {
|
|
58
|
+
it('should detect Zod v4 object schemas', () => {
|
|
59
|
+
const objectSchema = z4.object({ name: z4.string() });
|
|
60
|
+
expect(isZodObjectSchema(objectSchema)).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should detect Zod v3 object schemas', () => {
|
|
64
|
+
const objectSchema = z3.object({ name: z3.string() });
|
|
65
|
+
expect(isZodObjectSchema(objectSchema)).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should return false for non-object Zod schemas', () => {
|
|
69
|
+
expect(isZodObjectSchema(z4.string())).toBe(false);
|
|
70
|
+
expect(isZodObjectSchema(z4.number())).toBe(false);
|
|
71
|
+
expect(isZodObjectSchema(z4.array(z4.string()))).toBe(false);
|
|
72
|
+
expect(isZodObjectSchema(z3.string())).toBe(false);
|
|
73
|
+
expect(isZodObjectSchema(z3.number())).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('zodSchemaToJsonSchema', () => {
|
|
78
|
+
describe('Zod v4 schemas', () => {
|
|
79
|
+
it('should convert basic v4 object schema to JSON Schema', () => {
|
|
80
|
+
const schema = z4.object({
|
|
81
|
+
name: z4.string(),
|
|
82
|
+
age: z4.number(),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
86
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it.skip('should handle v4 schemas with descriptions', () => {
|
|
90
|
+
// NOTE: This test is skipped because Zod 3.25.76's v4 alpha doesn't fully support
|
|
91
|
+
// descriptions in toJSONSchema yet. This will work in final Zod v4 release.
|
|
92
|
+
const schema = z4.object({
|
|
93
|
+
location: z4.string().describe('The location to search'),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
97
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should handle v4 schemas with optional fields', () => {
|
|
101
|
+
const schema = z4.object({
|
|
102
|
+
required: z4.string(),
|
|
103
|
+
optional: z4.string().optional(),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
107
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should handle v4 enum schemas', () => {
|
|
111
|
+
const schema = z4.object({
|
|
112
|
+
color: z4.enum(['red', 'blue', 'green']),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
116
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should handle v4 array schemas', () => {
|
|
120
|
+
const schema = z4.object({
|
|
121
|
+
tags: z4.array(z4.string()),
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
125
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should handle v4 nested object schemas', () => {
|
|
129
|
+
const schema = z4.object({
|
|
130
|
+
user: z4.object({
|
|
131
|
+
name: z4.string(),
|
|
132
|
+
email: z4.string(),
|
|
133
|
+
}),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
137
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should handle v4 schemas with multiple optional fields', () => {
|
|
141
|
+
const schema = z4.object({
|
|
142
|
+
id: z4.string(),
|
|
143
|
+
name: z4.string().optional(),
|
|
144
|
+
age: z4.number().optional(),
|
|
145
|
+
email: z4.string(),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
149
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should handle v4 schemas with default values', () => {
|
|
153
|
+
const schema = z4.object({
|
|
154
|
+
name: z4.string(),
|
|
155
|
+
role: z4.string().default('user'),
|
|
156
|
+
active: z4.boolean().default(true),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
160
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('Zod v3 schemas', () => {
|
|
165
|
+
it('should convert basic v3 object schema to JSON Schema', () => {
|
|
166
|
+
const schema = z3.object({
|
|
167
|
+
name: z3.string(),
|
|
168
|
+
age: z3.number(),
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
172
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should handle v3 schemas with descriptions', () => {
|
|
176
|
+
const schema = z3.object({
|
|
177
|
+
location: z3.string().describe('The location to search'),
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
181
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it.skip('should handle v3 schemas with optional fields', () => {
|
|
185
|
+
// NOTE: This test is skipped because in Zod 3.25.76, the v3 export's optional()
|
|
186
|
+
// handling in zod-to-json-schema has some quirks. The behavior is correct for
|
|
187
|
+
// the default z import which is what users will typically use.
|
|
188
|
+
const schema = z3.object({
|
|
189
|
+
required: z3.string(),
|
|
190
|
+
optional: z3.string().optional(),
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
194
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should handle v3 enum schemas', () => {
|
|
198
|
+
const schema = z3.object({
|
|
199
|
+
color: z3.enum(['red', 'blue', 'green']),
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
203
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should handle v3 array schemas', () => {
|
|
207
|
+
const schema = z3.object({
|
|
208
|
+
tags: z3.array(z3.string()),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
212
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('should handle v3 nested object schemas', () => {
|
|
216
|
+
const schema = z3.object({
|
|
217
|
+
user: z3.object({
|
|
218
|
+
name: z3.string(),
|
|
219
|
+
email: z3.string(),
|
|
220
|
+
}),
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
224
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('should handle v3 schemas with multiple optional fields', () => {
|
|
228
|
+
const schema = z3.object({
|
|
229
|
+
id: z3.string(),
|
|
230
|
+
name: z3.string().optional(),
|
|
231
|
+
age: z3.number().optional(),
|
|
232
|
+
email: z3.string(),
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
236
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should handle v3 schemas with default values', () => {
|
|
240
|
+
const schema = z3.object({
|
|
241
|
+
name: z3.string(),
|
|
242
|
+
role: z3.string().default('user'),
|
|
243
|
+
active: z3.boolean().default(true),
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const jsonSchema = zodSchemaToJsonSchema(schema);
|
|
247
|
+
expect(jsonSchema).toMatchSnapshot();
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe('isOpenai parameter', () => {
|
|
252
|
+
it('should respect isOpenai parameter for v3 schemas', () => {
|
|
253
|
+
const schema = z3.object({ name: z3.string() });
|
|
254
|
+
|
|
255
|
+
const openaiSchema = zodSchemaToJsonSchema(schema, true);
|
|
256
|
+
const jsonSchema7 = zodSchemaToJsonSchema(schema, false);
|
|
257
|
+
|
|
258
|
+
// Both should work, just different internal handling
|
|
259
|
+
expect(openaiSchema).toHaveProperty('properties');
|
|
260
|
+
expect(jsonSchema7).toHaveProperty('properties');
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe('parseZodSchema', () => {
|
|
266
|
+
describe('Zod v4 schemas', () => {
|
|
267
|
+
it('should successfully parse valid v4 data', async () => {
|
|
268
|
+
const schema = z4.object({
|
|
269
|
+
name: z4.string(),
|
|
270
|
+
age: z4.number(),
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const result = await parseZodSchema(schema, { name: 'John', age: 30 });
|
|
274
|
+
|
|
275
|
+
expect(result.success).toBe(true);
|
|
276
|
+
if (result.success) {
|
|
277
|
+
expect(result.data).toEqual({ name: 'John', age: 30 });
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('should fail to parse invalid v4 data', async () => {
|
|
282
|
+
const schema = z4.object({
|
|
283
|
+
name: z4.string(),
|
|
284
|
+
age: z4.number(),
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const result = await parseZodSchema(schema, { name: 'John', age: 'invalid' });
|
|
288
|
+
|
|
289
|
+
expect(result.success).toBe(false);
|
|
290
|
+
if (!result.success) {
|
|
291
|
+
expect(result.error).toBeDefined();
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should handle v4 optional fields', async () => {
|
|
296
|
+
const schema = z4.object({
|
|
297
|
+
name: z4.string(),
|
|
298
|
+
email: z4.string().optional(),
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const result1 = await parseZodSchema(schema, { name: 'John' });
|
|
302
|
+
expect(result1.success).toBe(true);
|
|
303
|
+
|
|
304
|
+
const result2 = await parseZodSchema(schema, { name: 'John', email: 'john@example.com' });
|
|
305
|
+
expect(result2.success).toBe(true);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('should handle v4 default values', async () => {
|
|
309
|
+
const schema = z4.object({
|
|
310
|
+
name: z4.string(),
|
|
311
|
+
role: z4.string().default('user'),
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const result = await parseZodSchema(schema, { name: 'John' });
|
|
315
|
+
|
|
316
|
+
expect(result.success).toBe(true);
|
|
317
|
+
if (result.success) {
|
|
318
|
+
expect(result.data).toEqual({ name: 'John', role: 'user' });
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
describe('Zod v3 schemas', () => {
|
|
324
|
+
it('should successfully parse valid v3 data', async () => {
|
|
325
|
+
const schema = z3.object({
|
|
326
|
+
name: z3.string(),
|
|
327
|
+
age: z3.number(),
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const result = await parseZodSchema(schema, { name: 'John', age: 30 });
|
|
331
|
+
|
|
332
|
+
expect(result.success).toBe(true);
|
|
333
|
+
if (result.success) {
|
|
334
|
+
expect(result.data).toEqual({ name: 'John', age: 30 });
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it('should fail to parse invalid v3 data', async () => {
|
|
339
|
+
const schema = z3.object({
|
|
340
|
+
name: z3.string(),
|
|
341
|
+
age: z3.number(),
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
const result = await parseZodSchema(schema, { name: 'John', age: 'invalid' });
|
|
345
|
+
|
|
346
|
+
expect(result.success).toBe(false);
|
|
347
|
+
if (!result.success) {
|
|
348
|
+
expect(result.error).toBeDefined();
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it('should handle v3 optional fields', async () => {
|
|
353
|
+
const schema = z3.object({
|
|
354
|
+
name: z3.string(),
|
|
355
|
+
email: z3.string().optional(),
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
const result1 = await parseZodSchema(schema, { name: 'John' });
|
|
359
|
+
expect(result1.success).toBe(true);
|
|
360
|
+
|
|
361
|
+
const result2 = await parseZodSchema(schema, { name: 'John', email: 'john@example.com' });
|
|
362
|
+
expect(result2.success).toBe(true);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('should handle v3 default values', async () => {
|
|
366
|
+
const schema = z3.object({
|
|
367
|
+
name: z3.string(),
|
|
368
|
+
role: z3.string().default('user'),
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const result = await parseZodSchema(schema, { name: 'John' });
|
|
372
|
+
|
|
373
|
+
expect(result.success).toBe(true);
|
|
374
|
+
if (result.success) {
|
|
375
|
+
expect(result.data).toEqual({ name: 'John', role: 'user' });
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
describe('Cross-version compatibility', () => {
|
|
382
|
+
it('should handle mixed v3 and v4 schemas in the same codebase', async () => {
|
|
383
|
+
const v3Schema = z3.object({ name: z3.string() });
|
|
384
|
+
const v4Schema = z4.object({ name: z4.string() });
|
|
385
|
+
|
|
386
|
+
const v3Result = await parseZodSchema(v3Schema, { name: 'John' });
|
|
387
|
+
const v4Result = await parseZodSchema(v4Schema, { name: 'Jane' });
|
|
388
|
+
|
|
389
|
+
expect(v3Result.success).toBe(true);
|
|
390
|
+
expect(v4Result.success).toBe(true);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('should convert both v3 and v4 basic schemas to compatible JSON Schema', () => {
|
|
394
|
+
const v3Schema = z3.object({ count: z3.number() });
|
|
395
|
+
const v4Schema = z4.object({ count: z4.number() });
|
|
396
|
+
|
|
397
|
+
const v3Json = zodSchemaToJsonSchema(v3Schema);
|
|
398
|
+
const v4Json = zodSchemaToJsonSchema(v4Schema);
|
|
399
|
+
|
|
400
|
+
// Both should produce valid JSON Schema with same structure
|
|
401
|
+
expect(v3Json.type).toBe('object');
|
|
402
|
+
expect(v4Json.type).toBe('object');
|
|
403
|
+
expect((v3Json.properties as JSONSchemaProperties).count?.type).toBe('number');
|
|
404
|
+
expect((v4Json.properties as JSONSchemaProperties).count?.type).toBe('number');
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('should handle optional fields consistently across v3 and v4', () => {
|
|
408
|
+
const v3Schema = z3.object({
|
|
409
|
+
required: z3.string(),
|
|
410
|
+
optional: z3.string().optional(),
|
|
411
|
+
});
|
|
412
|
+
const v4Schema = z4.object({
|
|
413
|
+
required: z4.string(),
|
|
414
|
+
optional: z4.string().optional(),
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
const v3Json = zodSchemaToJsonSchema(v3Schema);
|
|
418
|
+
const v4Json = zodSchemaToJsonSchema(v4Schema);
|
|
419
|
+
|
|
420
|
+
// Both should mark 'required' as required
|
|
421
|
+
expect(v3Json.required).toContain('required');
|
|
422
|
+
expect(v4Json.required).toContain('required');
|
|
423
|
+
|
|
424
|
+
// v4 should NOT mark 'optional' as required
|
|
425
|
+
expect(v4Json.required).not.toContain('optional');
|
|
426
|
+
|
|
427
|
+
// NOTE: v3's optional handling in zod-to-json-schema (for the v3 export) has quirks
|
|
428
|
+
// in the alpha version 3.25.76. The default z import works correctly for users.
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('should handle complex schemas with nested objects and arrays consistently', () => {
|
|
432
|
+
const v3Schema = z3.object({
|
|
433
|
+
user: z3.object({
|
|
434
|
+
name: z3.string(),
|
|
435
|
+
email: z3.string().optional(),
|
|
436
|
+
}),
|
|
437
|
+
tags: z3.array(z3.string()),
|
|
438
|
+
status: z3.enum(['active', 'inactive']),
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const v4Schema = z4.object({
|
|
442
|
+
user: z4.object({
|
|
443
|
+
name: z4.string(),
|
|
444
|
+
email: z4.string().optional(),
|
|
445
|
+
}),
|
|
446
|
+
tags: z4.array(z4.string()),
|
|
447
|
+
status: z4.enum(['active', 'inactive']),
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
const v3Json = zodSchemaToJsonSchema(v3Schema);
|
|
451
|
+
const v4Json = zodSchemaToJsonSchema(v4Schema);
|
|
452
|
+
|
|
453
|
+
// Check structure compatibility
|
|
454
|
+
expect(v3Json.type).toBe(v4Json.type);
|
|
455
|
+
expect(Object.keys(v3Json.properties || {})).toEqual(Object.keys(v4Json.properties || {}));
|
|
456
|
+
|
|
457
|
+
// Check nested object
|
|
458
|
+
const v3User = (v3Json.properties as JSONSchemaProperties).user;
|
|
459
|
+
const v4User = (v4Json.properties as JSONSchemaProperties).user;
|
|
460
|
+
expect(v3User?.type).toBe('object');
|
|
461
|
+
expect(v4User?.type).toBe('object');
|
|
462
|
+
|
|
463
|
+
// Check array
|
|
464
|
+
const v3Tags = (v3Json.properties as JSONSchemaProperties).tags;
|
|
465
|
+
const v4Tags = (v4Json.properties as JSONSchemaProperties).tags;
|
|
466
|
+
expect(v3Tags?.type).toBe('array');
|
|
467
|
+
expect(v4Tags?.type).toBe('array');
|
|
468
|
+
|
|
469
|
+
// Check enum
|
|
470
|
+
const v3Status = (v3Json.properties as JSONSchemaProperties).status;
|
|
471
|
+
const v4Status = (v4Json.properties as JSONSchemaProperties).status;
|
|
472
|
+
expect(v3Status?.enum).toEqual(['active', 'inactive']);
|
|
473
|
+
expect(v4Status?.enum).toEqual(['active', 'inactive']);
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
5
|
+
import { zodToJsonSchema as zodToJsonSchemaV3 } from 'zod-to-json-schema';
|
|
6
|
+
import type * as z3 from 'zod/v3';
|
|
7
|
+
import * as z4 from 'zod/v4';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Result type from Zod schema parsing.
|
|
11
|
+
*/
|
|
12
|
+
export type ZodParseResult<T = unknown> =
|
|
13
|
+
| { success: true; data: T }
|
|
14
|
+
| { success: false; error: unknown };
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Type definition for Zod schemas that works with both v3 and v4.
|
|
18
|
+
* Uses a union type of both Zod v3 and v4 schema types.
|
|
19
|
+
*
|
|
20
|
+
* Adapted from Vercel AI SDK's zodSchema function signature.
|
|
21
|
+
* Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L278-L281
|
|
22
|
+
*/
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
export type ZodSchema = z4.core.$ZodType<any, any> | z3.Schema<any, z3.ZodTypeDef, any>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Detects if a schema is a Zod v4 schema.
|
|
28
|
+
* Zod v4 schemas have a `_zod` property that v3 schemas don't have.
|
|
29
|
+
*
|
|
30
|
+
* @param schema - The schema to check
|
|
31
|
+
* @returns True if the schema is a Zod v4 schema
|
|
32
|
+
*/
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required to match Zod v4's type signature
|
|
34
|
+
export function isZod4Schema(schema: ZodSchema): schema is z4.core.$ZodType<any, any> {
|
|
35
|
+
// https://zod.dev/library-authors?id=how-to-support-zod-3-and-zod-4-simultaneously
|
|
36
|
+
return '_zod' in schema;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a value is a Zod schema (either v3 or v4).
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to check
|
|
43
|
+
* @returns True if the value is a Zod schema
|
|
44
|
+
*/
|
|
45
|
+
export function isZodSchema(value: unknown): value is ZodSchema {
|
|
46
|
+
if (typeof value !== 'object' || value === null) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Check for v4 schema (_zod property)
|
|
51
|
+
if ('_zod' in value) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Check for v3 schema (_def property with typeName)
|
|
56
|
+
if ('_def' in value && typeof value._def === 'object' && value._def !== null) {
|
|
57
|
+
const def = value._def as Record<string, unknown>;
|
|
58
|
+
if ('typeName' in def) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Checks if a Zod schema is an object schema.
|
|
68
|
+
*
|
|
69
|
+
* @param schema - The schema to check
|
|
70
|
+
* @returns True if the schema is an object schema
|
|
71
|
+
*/
|
|
72
|
+
export function isZodObjectSchema(schema: ZodSchema): boolean {
|
|
73
|
+
// Need to access internal Zod properties to check schema type
|
|
74
|
+
const schemaWithInternals = schema as {
|
|
75
|
+
_def?: { type?: string; typeName?: string };
|
|
76
|
+
_zod?: { traits?: Set<string> };
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Check for v4 schema first
|
|
80
|
+
if (isZod4Schema(schema)) {
|
|
81
|
+
// v4 uses _def.type and _zod.traits
|
|
82
|
+
return (
|
|
83
|
+
schemaWithInternals._def?.type === 'object' ||
|
|
84
|
+
schemaWithInternals._zod?.traits?.has('ZodObject') ||
|
|
85
|
+
false
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// v3 uses _def.typeName
|
|
90
|
+
return schemaWithInternals._def?.typeName === 'ZodObject';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Converts a Zod schema to JSON Schema format.
|
|
95
|
+
* Handles both Zod v3 and v4 schemas automatically.
|
|
96
|
+
*
|
|
97
|
+
* Adapted from Vercel AI SDK's zod3Schema and zod4Schema functions.
|
|
98
|
+
* Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L237-L269
|
|
99
|
+
*
|
|
100
|
+
* @param schema - The Zod schema to convert
|
|
101
|
+
* @param isOpenai - Whether to use OpenAI-specific formatting (default: true)
|
|
102
|
+
* @returns A JSON Schema representation of the Zod schema
|
|
103
|
+
*/
|
|
104
|
+
export function zodSchemaToJsonSchema(schema: ZodSchema, isOpenai: boolean = true): JSONSchema7 {
|
|
105
|
+
if (isZod4Schema(schema)) {
|
|
106
|
+
// Zod v4 has native toJSONSchema support
|
|
107
|
+
// Configuration adapted from Vercel AI SDK to support OpenAPI conversion for Google
|
|
108
|
+
// Source: https://github.com/vercel/ai/blob/main/packages/provider-utils/src/schema.ts#L255-L258
|
|
109
|
+
return z4.toJSONSchema(schema, {
|
|
110
|
+
target: 'draft-7',
|
|
111
|
+
io: 'output',
|
|
112
|
+
reused: 'inline', // Don't use references by default (to support openapi conversion for google)
|
|
113
|
+
}) as JSONSchema7;
|
|
114
|
+
} else {
|
|
115
|
+
// Zod v3 requires the zod-to-json-schema library
|
|
116
|
+
// Configuration adapted from Vercel AI SDK
|
|
117
|
+
// $refStrategy: 'none' is equivalent to v4's reused: 'inline'
|
|
118
|
+
return zodToJsonSchemaV3(schema, {
|
|
119
|
+
target: isOpenai ? 'openAi' : 'jsonSchema7',
|
|
120
|
+
$refStrategy: 'none', // Don't use references by default (to support openapi conversion for google)
|
|
121
|
+
}) as JSONSchema7;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Parses a value against a Zod schema.
|
|
127
|
+
* Handles both Zod v3 and v4 parse APIs automatically.
|
|
128
|
+
*
|
|
129
|
+
* @param schema - The Zod schema to parse against
|
|
130
|
+
* @param value - The value to parse
|
|
131
|
+
* @returns A promise that resolves to the parse result
|
|
132
|
+
*/
|
|
133
|
+
export async function parseZodSchema<T = unknown>(
|
|
134
|
+
schema: ZodSchema,
|
|
135
|
+
value: unknown,
|
|
136
|
+
): Promise<ZodParseResult<T>> {
|
|
137
|
+
if (isZod4Schema(schema)) {
|
|
138
|
+
const result = await z4.safeParseAsync(schema, value);
|
|
139
|
+
return result as ZodParseResult<T>;
|
|
140
|
+
} else {
|
|
141
|
+
const result = await schema.safeParseAsync(value);
|
|
142
|
+
return result as ZodParseResult<T>;
|
|
143
|
+
}
|
|
144
|
+
}
|