mirror of
https://github.com/open-multi-agent/open-multi-agent.git
synced 2026-10-19 01:31:07 -05:00
fix(core): align JSON Schema required fields with Zod (#549)
This commit is contained in:
1 parent
16562ed894
commit
5735cd69e7
2 files changed
+21
-6
No files matched your search
@@ -370,12 +370,7 @@ function convertZodType(schema: ZodSchema): Record<string, unknown> {
|
||||
for (const [key, value] of Object.entries(objectDef.shape())) {
|
||||
properties[key] = convertZodType(value as ZodSchema)
|
||||
|
||||
const innerDef = ((value as ZodSchema) as unknown as { _def: ZodTypeDef })._def
|
||||
const isOptional =
|
||||
innerDef.typeName === ZodTypeName.ZodOptional ||
|
||||
innerDef.typeName === ZodTypeName.ZodDefault ||
|
||||
innerDef.typeName === ZodTypeName.ZodNullable
|
||||
if (!isOptional) {
|
||||
if (!(value as ZodSchema).isOptional()) {
|
||||
required.push(key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,6 +317,26 @@ describe('ToolRegistry', () => {
|
||||
expect(defs[0].name).toBe('echo')
|
||||
expect(defs[0].inputSchema).toHaveProperty('properties')
|
||||
})
|
||||
|
||||
it('marks nullable inputs as required when Zod rejects omission', () => {
|
||||
const inputSchema = z.object({
|
||||
requiredNullable: z.string().nullable(),
|
||||
optional: z.string().optional(),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(defineTool({
|
||||
name: 'nullable_input',
|
||||
description: 'Accepts a required nullable value.',
|
||||
inputSchema,
|
||||
execute: async () => ({ data: 'ok' }),
|
||||
}))
|
||||
|
||||
const [definition] = registry.toToolDefs()
|
||||
|
||||
expect(inputSchema.safeParse({}).success).toBe(false)
|
||||
expect(inputSchema.safeParse({ requiredNullable: null }).success).toBe(true)
|
||||
expect(definition.inputSchema).toMatchObject({ required: ['requiredNullable'] })
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user