| 117 | } |
| 118 | |
| 119 | private validateArgs(toolName: string, tool: Tool, args: unknown): unknown { |
| 120 | // Access the tool's Zod schema - AI SDK tools use 'inputSchema', some use 'parameters' |
| 121 | const toolRecord = tool as { inputSchema?: z.ZodType; parameters?: z.ZodType }; |
| 122 | const schema = toolRecord.inputSchema ?? toolRecord.parameters; |
| 123 | if (!schema) return args; |
| 124 | |
| 125 | const result = schema.safeParse(args); |
| 126 | if (!result.success) { |
| 127 | const issues = result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; "); |
| 128 | throw new Error(`Invalid arguments for ${toolName}: ${issues}`); |
| 129 | } |
| 130 | return result.data; |
| 131 | } |
| 132 | |
| 133 | private serializeResult(result: unknown): unknown { |
| 134 | try { |