UNPKG

ai-functions

Version:

Core AI primitives for building intelligent applications

53 lines 1.9 kB
/** * AI Schemas - Schema-based AI function generation * * This module provides functionality for creating AI functions from * simple schema definitions using the SimpleSchema format. */ import { generateObject } from './generate.js'; /** * Build a prompt by extracting descriptions from the schema */ function buildPromptFromSchema(schema, path = '') { if (typeof schema === 'string') { return schema; } if (Array.isArray(schema)) { return schema[0] || 'Generate items'; } if (typeof schema === 'object' && schema !== null) { const descriptions = []; for (const [key, value] of Object.entries(schema)) { const subPrompt = buildPromptFromSchema(value, path ? `${path}.${key}` : key); if (subPrompt) { descriptions.push(`${key}: ${subPrompt}`); } } return descriptions.length > 0 ? `Generate the following:\n${descriptions.join('\n')}` : ''; } return ''; } /** * Create schema-based functions from a map of schemas */ export function createSchemaFunctions(schemas, defaultOptions = {}) { const functions = {}; for (const [name, schema] of Object.entries(schemas)) { functions[name] = async (prompt, options) => { const mergedOptions = { ...defaultOptions, ...options }; const { model = 'sonnet', system, ...rest } = mergedOptions; // Build prompt from schema descriptions if none provided const schemaPrompt = prompt || buildPromptFromSchema(schema); const result = await generateObject({ model, schema, prompt: schemaPrompt, ...(system !== undefined && { system }), ...rest, }); return result.object; }; } return functions; } //# sourceMappingURL=ai-schemas.js.map