|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { settings, user } from '@sim/db/schema' |
| 3 | +import { getErrorMessage } from '@sim/utils/errors' |
| 4 | +import { eq } from 'drizzle-orm' |
| 5 | +import { type NextRequest, NextResponse } from 'next/server' |
| 6 | +import { getSession } from '@/lib/auth' |
| 7 | +import { SIM_AGENT_API_URL } from '@/lib/copilot/constants' |
| 8 | +import { getMothershipSourceEnvHeaders } from '@/lib/copilot/server/agent-url' |
| 9 | +import { env } from '@/lib/core/config/env' |
| 10 | +import { withRouteHandler } from '@/lib/core/utils/with-route-handler' |
| 11 | + |
| 12 | +/** |
| 13 | + * Enterprise BYOK key management for the current workspace's mothership. |
| 14 | + * |
| 15 | + * Unlike the cross-environment admin inspector (`/api/admin/mothership`), this |
| 16 | + * talks to the SAME copilot the workspace's mothership actually runs on — |
| 17 | + * `SIM_AGENT_API_URL` (local in dev, prod copilot in prod) — and authenticates |
| 18 | + * with the hosted internal key (`COPILOT_API_KEY`), the exact credential |
| 19 | + * mothership chat uses. Copilot requires that key (`SIM_AGENT_API_KEY`) and |
| 20 | + * rejects self-hosted callers, so BYOK can only ever be written through our |
| 21 | + * hosted Sim. The route is superuser-gated; the workspace id rides in the |
| 22 | + * request and is resolved by the caller from the route. |
| 23 | + */ |
| 24 | +async function getAuthorizedSuperUserId(): Promise<string | null> { |
| 25 | + const session = await getSession() |
| 26 | + if (!session?.user?.id) return null |
| 27 | + |
| 28 | + const [currentUser] = await db |
| 29 | + .select({ role: user.role, superUserModeEnabled: settings.superUserModeEnabled }) |
| 30 | + .from(user) |
| 31 | + .leftJoin(settings, eq(settings.userId, user.id)) |
| 32 | + .where(eq(user.id, session.user.id)) |
| 33 | + .limit(1) |
| 34 | + |
| 35 | + const authorized = currentUser?.role === 'admin' && (currentUser.superUserModeEnabled ?? false) |
| 36 | + return authorized ? session.user.id : null |
| 37 | +} |
| 38 | + |
| 39 | +async function proxyToCopilot(req: NextRequest, method: 'GET' | 'POST' | 'DELETE') { |
| 40 | + const userId = await getAuthorizedSuperUserId() |
| 41 | + if (!userId) { |
| 42 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 43 | + } |
| 44 | + |
| 45 | + const headers: Record<string, string> = { ...getMothershipSourceEnvHeaders() } |
| 46 | + if (env.COPILOT_API_KEY) headers['x-api-key'] = env.COPILOT_API_KEY |
| 47 | + |
| 48 | + let body: string | undefined |
| 49 | + if (method === 'POST') { |
| 50 | + const raw = await req.text() |
| 51 | + // Bind the audit field to the authenticated superuser, ignoring any |
| 52 | + // client-supplied createdBy so provisioning is always attributable. |
| 53 | + try { |
| 54 | + const parsed = raw ? JSON.parse(raw) : {} |
| 55 | + body = JSON.stringify({ ...parsed, createdBy: userId }) |
| 56 | + } catch { |
| 57 | + body = raw |
| 58 | + } |
| 59 | + headers['Content-Type'] = 'application/json' |
| 60 | + } |
| 61 | + |
| 62 | + const { search } = new URL(req.url) |
| 63 | + const targetUrl = `${SIM_AGENT_API_URL}/api/admin/byok${search}` |
| 64 | + |
| 65 | + try { |
| 66 | + const upstream = await fetch(targetUrl, { method, headers, ...(body ? { body } : {}) }) |
| 67 | + const text = await upstream.text() |
| 68 | + // boundary-raw-fetch: copilot returns JSON; tolerate an empty body. |
| 69 | + const data = text ? JSON.parse(text) : {} |
| 70 | + return NextResponse.json(data, { status: upstream.status }) |
| 71 | + } catch (error) { |
| 72 | + return NextResponse.json( |
| 73 | + { error: `Failed to reach copilot: ${getErrorMessage(error, 'Unknown error')}` }, |
| 74 | + { status: 502 } |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export const GET = withRouteHandler((req: NextRequest) => proxyToCopilot(req, 'GET')) |
| 80 | +export const POST = withRouteHandler((req: NextRequest) => proxyToCopilot(req, 'POST')) |
| 81 | +export const DELETE = withRouteHandler((req: NextRequest) => proxyToCopilot(req, 'DELETE')) |
0 commit comments