11import { convertJsonSchemaToZod } from 'zod-from-json-schema'
22
3- import {
4- formatSpawnableAgentError ,
5- validateSpawnableAgents ,
6- } from '../util/agent-template-validation'
3+ import { validateSpawnableAgents } from '../util/agent-template-validation'
74import { logger } from '../util/logger'
85import {
96 DynamicAgentDefinitionSchema ,
@@ -22,8 +19,9 @@ export interface DynamicAgentValidationError {
2219 */
2320export function collectAgentIds (
2421 agentTemplates : Record < string , DynamicAgentTemplate > = { } ,
25- ) : string [ ] {
22+ ) : { agentIds : string [ ] ; spawnableAgentIds : string [ ] } {
2623 const agentIds : string [ ] = [ ]
24+ const spawnableAgentIds : string [ ] = [ ]
2725 const jsonFiles = Object . keys ( agentTemplates )
2826
2927 for ( const filePath of jsonFiles ) {
@@ -37,6 +35,9 @@ export function collectAgentIds(
3735 if ( content . id && typeof content . id === 'string' ) {
3836 agentIds . push ( content . id )
3937 }
38+ if ( Array . isArray ( content . spawnableAgents ) ) {
39+ spawnableAgentIds . push ( ...content . spawnableAgents )
40+ }
4041 } catch ( error ) {
4142 // Log but don't fail the collection process for other errors
4243 logger . debug (
@@ -46,7 +47,29 @@ export function collectAgentIds(
4647 }
4748 }
4849
49- return agentIds
50+ return { agentIds, spawnableAgentIds }
51+ }
52+
53+ export async function validateAgentsWithSpawnableAgents (
54+ agentTemplates : Record < string , any > = { } ,
55+ ) : Promise < {
56+ templates : Record < string , AgentTemplate >
57+ dynamicTemplates : Record < string , DynamicAgentTemplate >
58+ validationErrors : DynamicAgentValidationError [ ]
59+ } > {
60+ const { agentIds, spawnableAgentIds } = collectAgentIds ( agentTemplates )
61+ const { validationErrors } = await validateSpawnableAgents (
62+ spawnableAgentIds ,
63+ agentIds ,
64+ )
65+ if ( validationErrors . length > 0 ) {
66+ return {
67+ templates : { } ,
68+ dynamicTemplates : { } ,
69+ validationErrors,
70+ }
71+ }
72+ return validateAgents ( agentTemplates )
5073}
5174
5275/**
@@ -73,10 +96,7 @@ export function validateAgents(agentTemplates: Record<string, any> = {}): {
7396
7497 const agentKeys = Object . keys ( agentTemplates )
7598
76- // Pass 1: Collect all agent IDs from template files
77- const dynamicAgentIds = collectAgentIds ( agentTemplates )
78-
79- // Pass 2: Load and validate each agent template
99+ // Load and validate each agent template
80100 for ( const agentKey of agentKeys ) {
81101 const content = agentTemplates [ agentKey ]
82102 try {
@@ -86,7 +106,6 @@ export function validateAgents(agentTemplates: Record<string, any> = {}): {
86106
87107 const validationResult = validateSingleAgent ( content , {
88108 filePath : agentKey ,
89- dynamicAgentIds,
90109 } )
91110
92111 if ( ! validationResult . success ) {
@@ -154,21 +173,15 @@ export function validateAgents(agentTemplates: Record<string, any> = {}): {
154173export function validateSingleAgent (
155174 template : any ,
156175 options ?: {
157- dynamicAgentIds ?: string [ ]
158176 filePath ?: string
159- skipSubagentValidation ?: boolean
160177 } ,
161178) : {
162179 success : boolean
163180 agentTemplate ?: AgentTemplate
164181 dynamicAgentTemplate ?: DynamicAgentTemplate
165182 error ?: string
166183} {
167- const {
168- filePath,
169- skipSubagentValidation = true ,
170- dynamicAgentIds = [ ] ,
171- } = options || { }
184+ const { filePath } = options || { }
172185
173186 try {
174187 // First validate against the Zod schema
@@ -203,23 +216,6 @@ export function validateSingleAgent(
203216 }
204217 }
205218
206- // Validate spawnable agents (skip if requested, e.g., for database agents)
207- if ( ! skipSubagentValidation ) {
208- const spawnableAgentValidation = validateSpawnableAgents (
209- validatedConfig . spawnableAgents ,
210- dynamicAgentIds ,
211- )
212- if ( ! spawnableAgentValidation . valid ) {
213- return {
214- success : false ,
215- error : formatSpawnableAgentError (
216- spawnableAgentValidation . invalidAgents ,
217- spawnableAgentValidation . availableAgents ,
218- ) ,
219- }
220- }
221- }
222-
223219 // Convert schemas and handle validation errors
224220 let inputSchema : AgentTemplate [ 'inputSchema' ]
225221 try {
0 commit comments