UNPKG

hataraku

Version:

An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.

92 lines 2.69 kB
import { z } from 'zod'; /** * Convert a Zod schema to JSON Schema format */ export function zodToJsonSchema(schema) { if (schema instanceof z.ZodObject) { const shape = schema._def.shape(); const properties = {}; const required = []; Object.entries(shape).forEach(([key, value]) => { if (value instanceof z.ZodType) { properties[key] = zodTypeToJsonSchema(value); if (!value.isOptional()) { required.push(key); } } }); return { type: 'object', properties, ...(required.length > 0 ? { required } : {}) }; } // Default to empty object schema if not an object schema return { type: 'object', properties: {} }; } function zodTypeToJsonSchema(schema) { if (schema instanceof z.ZodString) { return { type: 'string' }; } if (schema instanceof z.ZodNumber) { return { type: 'number' }; } if (schema instanceof z.ZodBoolean) { return { type: 'boolean' }; } if (schema instanceof z.ZodArray) { return { type: 'array', items: zodTypeToJsonSchema(schema.element) }; } if (schema instanceof z.ZodObject) { return zodToJsonSchema(schema); } if (schema instanceof z.ZodEnum) { return { type: 'string', enum: schema._def.values }; } if (schema instanceof z.ZodUnion) { return { oneOf: schema._def.options.map((opt) => zodTypeToJsonSchema(opt)) }; } if (schema instanceof z.ZodOptional) { return zodTypeToJsonSchema(schema._def.innerType); } if (schema instanceof z.ZodNullable) { return { oneOf: [ zodTypeToJsonSchema(schema._def.innerType), { type: 'null' } ] }; } // Default to any type if unknown return {}; } export function zodToSchemaString(schema) { if (schema instanceof z.ZodObject) { const shape = schema._def.shape(); const shapeEntries = Object.entries(shape).map(([key, value]) => { if (value instanceof z.ZodString) { return `"${key}": z.string()`; } if (value instanceof z.ZodNumber) { return `"${key}": z.number()`; } // Add more types as needed return `"${key}": z.unknown()`; }); return `{${shapeEntries.join(', ')}}`; } // Handle other schema types as needed return 'z.unknown()'; } //# sourceMappingURL=schema.js.map