@@ -59,7 +59,32 @@ export async function POST(request: NextRequest) {
5959 }
6060
6161 const { data, authToken } = parseResult . data
62- const agents = data as DynamicAgentTemplate [ ] // data is now an array of agents
62+ const agentDefinitions = data
63+
64+ // First use validateAgents to convert to DynamicAgentTemplate types
65+ const agentMap = agentDefinitions . reduce (
66+ ( acc : Record < string , any > , agent : any ) => {
67+ acc [ agent . id ] = agent
68+ return acc
69+ } ,
70+ { } as Record < string , any >
71+ )
72+
73+ const { validationErrors, dynamicTemplates } = validateAgents ( agentMap )
74+ const agents = Object . values ( dynamicTemplates )
75+
76+ if ( validationErrors . length > 0 ) {
77+ const errorDetails = validationErrors . map ( ( err ) => err . message ) . join ( '\n' )
78+
79+ return NextResponse . json (
80+ {
81+ error : 'Agent config validation failed' ,
82+ details : errorDetails ,
83+ validationErrors,
84+ } ,
85+ { status : 400 }
86+ )
87+ }
6388
6489 // Try cookie-based auth first, then fall back to authToken validation using proper function
6590 let userId : string | undefined
@@ -107,34 +132,6 @@ export async function POST(request: NextRequest) {
107132
108133 const requestedPublisherId = publisherIds [ 0 ] !
109134
110- // Validate all agents
111- const agentMap = agents . reduce (
112- (
113- acc : Record < string , DynamicAgentTemplate > ,
114- agent : DynamicAgentTemplate
115- ) => {
116- acc [ agent . id ] = agent
117- return acc
118- } ,
119- { } as Record < string , DynamicAgentTemplate >
120- )
121-
122- const validationResult = validateAgents ( agentMap )
123-
124- if ( validationResult . validationErrors . length > 0 ) {
125- const errorDetails = validationResult . validationErrors
126- . map ( ( err ) => err . message )
127- . join ( '\n' )
128-
129- return NextResponse . json (
130- {
131- error : 'Agent config validation failed' ,
132- details : errorDetails ,
133- validationErrors : validationResult . validationErrors ,
134- } ,
135- { status : 400 }
136- )
137- }
138135
139136 // Verify user has access to the requested publisher
140137 const publisherResult = await db
0 commit comments