mirror of
https://github.com/jina-ai/node-DeepResearch.git
synced 2025-12-25 22:16:49 +08:00
feat: add schema utility
Co-Authored-By: Han Xiao <han.xiao@jina.ai>
This commit is contained in:
parent
30cc26d6a2
commit
7ef2cfa46f
31
src/utils/schema.ts
Normal file
31
src/utils/schema.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export function createResponseSchema(config: {
|
||||
type: 'object';
|
||||
properties: Record<string, {
|
||||
type: string;
|
||||
description: string;
|
||||
items?: any;
|
||||
maxItems?: number;
|
||||
minItems?: number;
|
||||
}>;
|
||||
required: string[];
|
||||
}) {
|
||||
const properties: Record<string, any> = {};
|
||||
|
||||
for (const [key, prop] of Object.entries(config.properties)) {
|
||||
if (prop.type === 'STRING') {
|
||||
properties[key] = z.string().describe(prop.description);
|
||||
} else if (prop.type === 'BOOLEAN') {
|
||||
properties[key] = z.boolean().describe(prop.description);
|
||||
} else if (prop.type === 'ARRAY') {
|
||||
const itemSchema = prop.items.type === 'STRING' ? z.string() : z.object({});
|
||||
properties[key] = z.array(itemSchema)
|
||||
.describe(prop.description)
|
||||
.max(prop.maxItems || Infinity)
|
||||
.min(prop.minItems || 0);
|
||||
}
|
||||
}
|
||||
|
||||
return z.object(properties);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user