UNPKG

ai-functions

Version:

A powerful TypeScript library for building AI-powered applications with template literals and structured outputs

51 lines 1.88 kB
import { z } from 'zod'; /** * Creates a Zod schema from a template object where: * - String values become descriptions * - Pipe-separated values become enums * - Word count constraints (e.g. "3-5 words") are enforced */ export function createSchemaFromTemplate(template) { const shape = Object.entries(template).reduce((acc, [key, value]) => { // Check if value contains pipe-separated options if (value.includes('|')) { const options = value .split('|') .map((opt) => opt.trim()) .filter(Boolean); // Remove empty strings if (options.length >= 1) { acc[key] = z.enum([options[0], ...options.slice(1)]).describe(value); } else { acc[key] = z.string().describe(value); } } else { // Check for word count constraints const wordCountMatch = value.match(/(\d+)-(\d+)\s+words/); if (wordCountMatch) { const [, minStr, maxStr] = wordCountMatch; const min = parseInt(minStr, 10); const max = parseInt(maxStr, 10); acc[key] = z .string() .min(1) .refine((val) => { const words = val.trim().split(/\s+/).length; return words >= min && words <= max; }, { message: `Must be between ${min} and ${max} words`, params: { min, max }, }) .describe(value); } else { // Regular string field with description acc[key] = z.string().describe(value); } } return acc; }, {}); return z.object(shape); } //# sourceMappingURL=schema.js.map