|
| 1 | +import { type NextRequest, NextResponse } from 'next/server' |
| 2 | +import { env } from '@/lib/env' |
| 3 | +import { createLogger } from '@/lib/logs/console/logger' |
| 4 | + |
| 5 | +const logger = createLogger('CopilotTrainingExamplesAPI') |
| 6 | + |
| 7 | +export const runtime = 'nodejs' |
| 8 | +export const dynamic = 'force-dynamic' |
| 9 | + |
| 10 | +export async function POST(request: NextRequest) { |
| 11 | + const baseUrl = env.AGENT_INDEXER_URL |
| 12 | + if (!baseUrl) { |
| 13 | + logger.error('Missing AGENT_INDEXER_URL environment variable') |
| 14 | + return NextResponse.json({ error: 'Missing AGENT_INDEXER_URL env' }, { status: 500 }) |
| 15 | + } |
| 16 | + |
| 17 | + const apiKey = env.AGENT_INDEXER_API_KEY |
| 18 | + if (!apiKey) { |
| 19 | + logger.error('Missing AGENT_INDEXER_API_KEY environment variable') |
| 20 | + return NextResponse.json({ error: 'Missing AGENT_INDEXER_API_KEY env' }, { status: 500 }) |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + const body = await request.json() |
| 25 | + |
| 26 | + logger.info('Sending workflow example to agent indexer', { |
| 27 | + hasJsonField: typeof body?.json === 'string', |
| 28 | + }) |
| 29 | + |
| 30 | + const upstream = await fetch(`${baseUrl}/examples/add`, { |
| 31 | + method: 'POST', |
| 32 | + headers: { |
| 33 | + 'Content-Type': 'application/json', |
| 34 | + 'x-api-key': apiKey, |
| 35 | + }, |
| 36 | + body: JSON.stringify(body), |
| 37 | + }) |
| 38 | + |
| 39 | + if (!upstream.ok) { |
| 40 | + const errorText = await upstream.text() |
| 41 | + logger.error('Agent indexer rejected the example', { |
| 42 | + status: upstream.status, |
| 43 | + error: errorText, |
| 44 | + }) |
| 45 | + return NextResponse.json({ error: errorText }, { status: upstream.status }) |
| 46 | + } |
| 47 | + |
| 48 | + const data = await upstream.json() |
| 49 | + logger.info('Successfully sent workflow example to agent indexer') |
| 50 | + |
| 51 | + return NextResponse.json(data, { |
| 52 | + headers: { 'content-type': 'application/json' }, |
| 53 | + }) |
| 54 | + } catch (err) { |
| 55 | + const errorMessage = err instanceof Error ? err.message : 'Failed to add example' |
| 56 | + logger.error('Failed to send workflow example', { error: err }) |
| 57 | + return NextResponse.json({ error: errorMessage }, { status: 502 }) |
| 58 | + } |
| 59 | +} |
0 commit comments