From 7ef2cfa46f702426d9ea0d46c7bd8fc659547a8b Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 6 Feb 2025 01:36:23 +0000 Subject: [PATCH] feat: add schema utility Co-Authored-By: Han Xiao --- src/utils/schema.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/utils/schema.ts diff --git a/src/utils/schema.ts b/src/utils/schema.ts new file mode 100644 index 0000000..516d1d1 --- /dev/null +++ b/src/utils/schema.ts @@ -0,0 +1,31 @@ +import { z } from 'zod'; + +export function createResponseSchema(config: { + type: 'object'; + properties: Record; + required: string[]; +}) { + const properties: Record = {}; + + 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); +}