11import { validateAgents } from '@codebuff/common/templates/agent-validation'
22import { NextResponse } from 'next/server'
3- import { getServerSession } from 'next-auth'
43
5- import { authOptions } from '@/app/api/auth/[...nextauth]/auth-options'
64import { logger } from '@/util/logger'
75
86import type { NextRequest } from 'next/server'
97
108interface ValidateAgentsRequest {
11- agentConfigs : Record < string , any >
9+ agentConfigs : any [ ]
1210}
1311
1412export async function POST ( request : NextRequest ) : Promise < NextResponse > {
1513 try {
16- const session = await getServerSession ( authOptions )
17- if ( ! session ?. user ?. id ) {
18- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
19- }
20-
2114 const body = ( await request . json ( ) ) as ValidateAgentsRequest
2215 const { agentConfigs } = body
2316
24- if ( ! agentConfigs || typeof agentConfigs !== 'object' ) {
17+ if ( ! agentConfigs || ! Array . isArray ( agentConfigs ) ) {
2518 return NextResponse . json (
2619 {
2720 error :
28- 'Invalid request: agentConfigs must be an object, with keys being the agent IDs and values of type AgentConfig' ,
21+ 'Invalid request: agentConfigs must be an array of AgentConfig objects ' ,
2922 } ,
3023 { status : 400 }
3124 )
3225 }
3326
34- const { templates : configs , validationErrors } = validateAgents ( agentConfigs )
27+ const configsObject = Object . fromEntries (
28+ agentConfigs . map ( ( config ) => [ config . id , config ] )
29+ )
30+ const { templates : configs , validationErrors } =
31+ validateAgents ( configsObject )
3532
3633 if ( validationErrors . length > 0 ) {
3734 logger . warn (
38- { errorCount : validationErrors . length , userId : session . user . id } ,
39- 'Agent config validation errors found' ,
35+ { errorCount : validationErrors . length } ,
36+ 'Agent config validation errors found'
4037 )
4138 }
4239
@@ -49,11 +46,11 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
4946 } catch ( error ) {
5047 logger . error (
5148 { error : error instanceof Error ? error . message : String ( error ) } ,
52- 'Error validating agent configs' ,
49+ 'Error validating agent configs'
5350 )
5451 return NextResponse . json (
5552 { error : 'Internal server error' } ,
56- { status : 500 } ,
53+ { status : 500 }
5754 )
5855 }
5956}
0 commit comments