* Convert JSON schema to Zod schema format using json-schema-to-zod. * This is done once during loading to avoid repeated conversions. * Throws descriptive errors for validation failures.
( inputPromptSchema?: Record<string, any>, paramsSchema?: Record<string, any>, filePath?: string, )
| 309 | * Throws descriptive errors for validation failures. |
| 310 | */ |
| 311 | function convertInputSchema( |
| 312 | inputPromptSchema?: Record<string, any>, |
| 313 | paramsSchema?: Record<string, any>, |
| 314 | filePath?: string, |
| 315 | ): AgentTemplate['inputSchema'] { |
| 316 | const result: any = {} |
| 317 | const fileContext = filePath ? ` in ${filePath}` : '' |
| 318 | |
| 319 | // Handle prompt schema |
| 320 | if (inputPromptSchema) { |
| 321 | try { |
| 322 | if ( |
| 323 | typeof inputPromptSchema !== 'object' || |
| 324 | Object.keys(inputPromptSchema).length === 0 |
| 325 | ) { |
| 326 | throw new Error( |
| 327 | `Invalid inputSchema.prompt${fileContext}: Schema must be a valid non-empty JSON schema object. Found: ${typeof inputPromptSchema}`, |
| 328 | ) |
| 329 | } |
| 330 | const promptZodSchema = convertJsonSchemaToZod(inputPromptSchema) |
| 331 | // Validate that the schema results in string or undefined |
| 332 | const testResult = promptZodSchema.safeParse('test') |
| 333 | const testUndefined = promptZodSchema.safeParse(undefined) |
| 334 | |
| 335 | if (!testResult.success && !testUndefined.success) { |
| 336 | const errorDetails = |
| 337 | testResult.error?.issues?.[0]?.message || 'validation failed' |
| 338 | throw new Error( |
| 339 | `Invalid inputSchema.prompt${fileContext}: Schema must allow string or undefined values. ` + |
| 340 | `Current schema validation error: ${errorDetails}. ` + |
| 341 | `Please ensure your JSON schema accepts string types.`, |
| 342 | ) |
| 343 | } |
| 344 | |
| 345 | result.prompt = promptZodSchema |
| 346 | } catch (error) { |
| 347 | if (error instanceof Error && error.message.includes('inputSchema')) { |
| 348 | // Re-throw our custom validation errors |
| 349 | throw error |
| 350 | } |
| 351 | |
| 352 | // Handle json-schema-to-zod conversion errors |
| 353 | const errorMessage = |
| 354 | error instanceof Error ? error.message : 'Unknown error' |
| 355 | throw new Error( |
| 356 | `Failed to convert inputSchema.prompt to Zod${fileContext}: ${errorMessage}. ` + |
| 357 | `Please check that your inputSchema.prompt is a valid non-empty JSON schema object.`, |
| 358 | ) |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Handle params schema |
| 363 | if (paramsSchema) { |
| 364 | try { |
| 365 | if ( |
| 366 | typeof paramsSchema !== 'object' || |
| 367 | Object.keys(paramsSchema).length === 0 |
| 368 | ) { |
no outgoing calls
no test coverage detected