( method: 'GET' | 'POST' | 'DELETE', query: URLSearchParams, body?: string )
| 43 | } |
| 44 | |
| 45 | async function forwardToCopilot( |
| 46 | method: 'GET' | 'POST' | 'DELETE', |
| 47 | query: URLSearchParams, |
| 48 | body?: string |
| 49 | ) { |
| 50 | const headers: Record<string, string> = { ...getMothershipSourceEnvHeaders() } |
| 51 | if (env.COPILOT_API_KEY) headers['x-api-key'] = env.COPILOT_API_KEY |
| 52 | if (body !== undefined) headers['Content-Type'] = 'application/json' |
| 53 | |
| 54 | const qs = query.toString() |
| 55 | const targetUrl = `${SIM_AGENT_API_URL}/api/admin/byok${qs ? `?${qs}` : ''}` |
| 56 | |
| 57 | try { |
| 58 | const upstream = await fetch(targetUrl, { |
| 59 | method, |
| 60 | headers, |
| 61 | ...(body !== undefined ? { body } : {}), |
| 62 | }) |
| 63 | const text = await upstream.text() |
| 64 | // boundary-raw-fetch: copilot returns JSON; tolerate an empty body. |
| 65 | const data = text ? JSON.parse(text) : {} |
| 66 | return NextResponse.json(data, { status: upstream.status }) |
| 67 | } catch (error) { |
| 68 | return NextResponse.json( |
| 69 | { error: `Failed to reach copilot: ${getErrorMessage(error, 'Unknown error')}` }, |
| 70 | { status: 502 } |
| 71 | ) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | export const GET = withRouteHandler(async (req: NextRequest) => { |
| 76 | const userId = await getAuthorizedSuperUserId() |
no test coverage detected