(params: {
template: any
filePath?: string
})
| 158 | * @returns Validation result with either the converted AgentTemplate or an error |
| 159 | */ |
| 160 | export function validateSingleAgent(params: { |
| 161 | template: any |
| 162 | filePath?: string |
| 163 | }): { |
| 164 | success: boolean |
| 165 | agentTemplate?: AgentTemplate |
| 166 | dynamicAgentTemplate?: DynamicAgentTemplate |
| 167 | error?: string |
| 168 | } { |
| 169 | const { template, filePath = 'unknown' } = params |
| 170 | |
| 171 | try { |
| 172 | // First validate against the Zod schema |
| 173 | let validatedConfig: DynamicAgentTemplate |
| 174 | try { |
| 175 | const typedAgentDefinition = DynamicAgentDefinitionSchema.parse(template) |
| 176 | |
| 177 | // Convert handleSteps function to string if present |
| 178 | let handleStepsString: string | undefined |
| 179 | if (template.handleSteps) { |
| 180 | handleStepsString = template.handleSteps.toString() |
| 181 | } |
| 182 | |
| 183 | validatedConfig = DynamicAgentTemplateSchema.parse({ |
| 184 | ...typedAgentDefinition, |
| 185 | systemPrompt: typedAgentDefinition.systemPrompt || '', |
| 186 | instructionsPrompt: typedAgentDefinition.instructionsPrompt || '', |
| 187 | stepPrompt: typedAgentDefinition.stepPrompt || '', |
| 188 | handleSteps: handleStepsString, |
| 189 | }) |
| 190 | } catch (error: any) { |
| 191 | // Try to extract agent context for better error messages |
| 192 | const agentContext = template.id |
| 193 | ? `Agent "${template.id}"${template.displayName ? ` (${template.displayName})` : ''}` |
| 194 | : filePath |
| 195 | ? `Agent in ${filePath}` |
| 196 | : 'Agent' |
| 197 | |
| 198 | return { |
| 199 | success: false, |
| 200 | error: `${agentContext}: Schema validation failed: ${error.message}`, |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Convert schemas and handle validation errors |
| 205 | let inputSchema: AgentTemplate['inputSchema'] |
| 206 | try { |
| 207 | inputSchema = convertInputSchema( |
| 208 | validatedConfig.inputSchema?.prompt, |
| 209 | validatedConfig.inputSchema?.params, |
| 210 | filePath, |
| 211 | ) |
| 212 | } catch (error) { |
| 213 | // Try to extract agent context for better error messages |
| 214 | const agentContext = validatedConfig.id |
| 215 | ? `Agent "${validatedConfig.id}"${validatedConfig.displayName ? ` (${validatedConfig.displayName})` : ''}` |
| 216 | : filePath |
| 217 | ? `Agent in ${filePath}` |
no test coverage detected