(request: NextRequest)
| 38 | } |
| 39 | |
| 40 | export async function POST(request: NextRequest) { |
| 41 | try { |
| 42 | // Parse request body |
| 43 | const body = await request.json() |
| 44 | const parseResult = publishAgentsRequestSchema.safeParse(body) |
| 45 | if (!parseResult.success) { |
| 46 | const errorMessages = parseResult.error.issues.map((issue) => { |
| 47 | const path = issue.path.length > 0 ? `${issue.path.join('.')}: ` : '' |
| 48 | return `${path}${issue.message}` |
| 49 | }) |
| 50 | |
| 51 | return NextResponse.json( |
| 52 | { |
| 53 | error: 'Invalid request body', |
| 54 | details: errorMessages.join('; '), |
| 55 | validationErrors: parseResult.error.issues, |
| 56 | }, |
| 57 | { status: 400 }, |
| 58 | ) |
| 59 | } |
| 60 | |
| 61 | // DEPRECATED: authToken in body is for backwards compatibility with older CLI versions. |
| 62 | // New clients should use the Authorization header instead. |
| 63 | const { |
| 64 | data, |
| 65 | authToken: bodyAuthToken, |
| 66 | allLocalAgentIds, |
| 67 | } = parseResult.data |
| 68 | const agentDefinitions = data |
| 69 | |
| 70 | // Prefer Authorization header, fall back to body authToken for backwards compatibility |
| 71 | const authToken = extractApiKeyFromHeader(request) ?? bodyAuthToken |
| 72 | |
| 73 | // First use validateAgents to convert to DynamicAgentTemplate types |
| 74 | const agentMap = agentDefinitions.reduce( |
| 75 | (acc: Record<string, any>, agent: any) => { |
| 76 | acc[agent.id] = agent |
| 77 | return acc |
| 78 | }, |
| 79 | {} as Record<string, any>, |
| 80 | ) |
| 81 | |
| 82 | const { validationErrors, dynamicTemplates } = |
| 83 | await validateAgentsWithSpawnableAgents({ |
| 84 | agentTemplates: agentMap, |
| 85 | allLocalAgentIds, |
| 86 | logger, |
| 87 | }) |
| 88 | const agents = Object.values(dynamicTemplates) |
| 89 | |
| 90 | if (validationErrors.length > 0) { |
| 91 | const errorDetails = validationErrors.map((err) => err.message).join('\n') |
| 92 | |
| 93 | return NextResponse.json( |
| 94 | { |
| 95 | error: 'Agent config validation failed', |
| 96 | details: errorDetails, |
| 97 | validationErrors, |
nothing calls this directly
no test coverage detected