pomljs
Version:
Prompt Orchestration Markup Language
1 lines • 11.7 kB
Source Map (JSON)
{"version":3,"file":"schema.cjs","sources":["../../.build/util/schema.js"],"sourcesContent":["import { z } from 'zod';\nimport { jsonSchemaToZod } from 'json-schema-to-zod';\n/**\n * A unified schema representation that can work with both Zod and OpenAPI schemas.\n * Provides conversion methods between different schema formats.\n */\nexport class Schema {\n zodSchema;\n openApiSchema;\n constructor(zodSchema, openApiSchema) {\n this.zodSchema = zodSchema;\n this.openApiSchema = openApiSchema;\n if (!zodSchema && !openApiSchema) {\n throw new Error(\"At least one schema must be provided\");\n }\n this.zodSchema = zodSchema;\n this.openApiSchema = openApiSchema;\n }\n /**\n * Creates a Schema instance from an OpenAPI schema object.\n * @param openApiSchema The OpenAPI schema object\n * @returns A new Schema instance\n */\n static fromOpenAPI(openApiSchema) {\n const schema = new Schema(undefined, openApiSchema);\n // TODO: openapi schema full validation should be added here\n if (typeof openApiSchema !== 'object' || openApiSchema === null) {\n throw new Error(\"Invalid OpenAPI schema provided\");\n }\n return schema;\n }\n /**\n * Creates a Schema instance from a Zod schema.\n * @param zodSchema The Zod schema\n * @returns A new Schema instance\n */\n static fromZod(zodSchema) {\n return new Schema(zodSchema);\n }\n /**\n * Converts the schema to a Zod schema.\n * @returns The Zod schema representation\n * @throws Error if no schema is available\n */\n toZod() {\n if (this.zodSchema) {\n return this.zodSchema;\n }\n else if (this.openApiSchema) {\n // It's not safe and even prohibited to use eval in browser environments.\n // We need to make z available in the eval context\n const zodSchemaString = jsonSchemaToZod(this.openApiSchema);\n // Create a function that has z in scope and evaluate the schema string\n const evalWithZ = new Function('z', `return ${zodSchemaString}`);\n return evalWithZ(z);\n }\n else {\n throw new Error(\"No Zod schema available\");\n }\n }\n /**\n * Converts the schema to an OpenAPI/JSON schema format.\n * @returns The OpenAPI schema representation\n * @throws Error if no schema is available\n */\n toOpenAPI() {\n if (this.openApiSchema) {\n return this.openApiSchema;\n }\n else if (this.zodSchema) {\n const schema = z.toJSONSchema(this.zodSchema);\n // pop the $schema property if it exists\n if (schema.$schema) {\n delete schema.$schema;\n }\n return schema;\n }\n else {\n throw new Error(\"No schema available\");\n }\n }\n}\n/**\n * Manages a collection of tool schemas and provides conversion methods\n * for different AI provider formats (OpenAI, Vercel, etc.).\n */\nexport class ToolsSchema {\n tools;\n constructor() {\n this.tools = new Map();\n }\n /**\n * Adds a tool with a Zod schema to the collection.\n * @param name The name of the tool\n * @param description A description of what the tool does\n * @param zodSchema The Zod schema for the tool's input parameters\n */\n addZodTool(name, description, zodSchema) {\n const schema = Schema.fromZod(zodSchema);\n if (this.tools.has(name)) {\n throw new Error(`Tool with name \"${name}\" already exists`);\n }\n this.tools.set(name, {\n name,\n description,\n inputSchema: schema\n });\n }\n /**\n * Adds a tool with an OpenAPI schema to the collection.\n * @param name The name of the tool\n * @param description A description of what the tool does\n * @param openApiSchema The OpenAPI schema for the tool's input parameters\n */\n addOpenAPITool(name, description, openApiSchema) {\n const schema = Schema.fromOpenAPI(openApiSchema);\n if (this.tools.has(name)) {\n throw new Error(`Tool with name \"${name}\" already exists`);\n }\n this.tools.set(name, {\n name,\n description,\n inputSchema: schema\n });\n }\n /**\n * Add a tool with pre-parsed schema.\n * @param name The name of the tool\n * @param description A description of what the tool does\n * @param schema The pre-parsed schema for the tool's input parameters\n */\n addTool(name, description, schema) {\n if (this.tools.has(name)) {\n throw new Error(`Tool with name \"${name}\" already exists`);\n }\n this.tools.set(name, {\n name,\n description,\n inputSchema: schema\n });\n }\n /**\n * Converts the tools collection to Vercel AI SDK format.\n * @returns An object mapping tool names to their Vercel AI SDK representations\n */\n toVercel() {\n const vercelTools = {};\n for (const [name, tool] of this.tools) {\n vercelTools[name] = {\n description: tool.description,\n parameters: tool.inputSchema.toZod()\n };\n }\n return vercelTools;\n }\n /**\n * Converts the tools collection to OpenAI function calling format.\n * @returns An array of OpenAI function definitions\n * @example\n * [\n * {\n * \"type\": \"function\",\n * \"name\": \"get_horoscope\",\n * \"description\": \"Get today's horoscope for an astrological sign.\",\n * \"parameters\": {\n * \"type\": \"object\",\n * \"properties\": {\n * \"sign\": {\n * \"type\": \"string\",\n * \"description\": \"An astrological sign like Taurus or Aquarius\"\n * }\n * },\n * \"required\": [\"sign\"]\n * }\n * }\n * ]\n */\n toOpenAI() {\n const openAITools = [];\n // FIXME: Handle error gracefully here.\n for (const [_, tool] of this.tools) {\n openAITools.push({\n type: 'function',\n name: tool.name,\n description: tool.description,\n parameters: tool.inputSchema.toOpenAPI()\n });\n }\n return openAITools;\n }\n /**\n * Gets a tool by name.\n * @param name The name of the tool to retrieve\n * @returns The tool schema if found, undefined otherwise\n */\n getTool(name) {\n return this.tools.get(name);\n }\n /**\n * Gets all tools in the collection.\n * @returns An array of all tool schemas\n */\n getTools() {\n return Array.from(this.tools.values());\n }\n /**\n * Removes a tool from the collection.\n * @param name The name of the tool to remove\n * @returns true if the tool was removed, false if it didn't exist\n */\n removeTool(name) {\n return this.tools.delete(name);\n }\n /**\n * Gets the number of tools in the collection.\n * @returns The number of tools\n */\n size() {\n return this.tools.size;\n }\n /**\n * Clears all tools from the collection.\n */\n clear() {\n this.tools.clear();\n }\n}\n//# sourceMappingURL=schema.js.map"],"names":["jsonSchemaToZod","z"],"mappings":";;;;;AAEA;AACA;AACA;AACA;AACO,MAAM,MAAM,CAAC;AACpB,IAAI,SAAS;AACb,IAAI,aAAa;AACjB,IAAI,WAAW,CAAC,SAAS,EAAE,aAAa,EAAE;AAC1C,QAAQ,IAAI,CAAC,SAAS,GAAG,SAAS;AAClC,QAAQ,IAAI,CAAC,aAAa,GAAG,aAAa;AAC1C,QAAQ,IAAI,CAAC,SAAS,IAAI,CAAC,aAAa,EAAE;AAC1C,YAAY,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC;AACnE;AACA,QAAQ,IAAI,CAAC,SAAS,GAAG,SAAS;AAClC,QAAQ,IAAI,CAAC,aAAa,GAAG,aAAa;AAC1C;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,OAAO,W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