import { NextResponse } from "next/server" import { getSandboxInferenceConfig, saveSandboxInferenceConfig, type SandboxInferenceRoute, } from "@/app/lib/sandboxInferenceStore" function validateSandboxId(value: string) { if (!value || value.length > 128) throw new Error("sandbox id is required") if (!/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/.test(value)) { throw new Error("sandbox id contains unsupported characters") } return value } function parseModels(value: unknown) { if (!Array.isArray(value)) return [] return value .map((item) => typeof item === "string" ? item.trim() : "") .filter(Boolean) } function parseRoutes(value: unknown): SandboxInferenceRoute[] { if (!Array.isArray(value)) return [] return value.map((item) => { const provider = typeof item?.provider === "string" ? item.provider.trim() : "" const model = typeof item?.model === "string" ? item.model.trim() : "" const id = `${provider}::${model}` return { id, provider, model, enabled: item?.enabled !== false, label: typeof item?.label === "string" ? item.label.trim() : "", } }).filter((item) => item.provider && item.model) } export async function GET( _request: Request, { params }: { params: Promise<{ sandboxId: string }> } ) { try { const { sandboxId } = await params const config = await getSandboxInferenceConfig(validateSandboxId(sandboxId)) return NextResponse.json({ ok: true, config }) } catch (error) { const message = error instanceof Error ? error.message : "Failed to fetch sandbox inference config" return NextResponse.json({ ok: false, error: message }, { status: /required|unsupported/.test(message) ? 400 : 500 }) } } export async function POST( request: Request, { params }: { params: Promise<{ sandboxId: string }> } ) { try { const { sandboxId } = await params const body = await request.json() const provider = typeof body?.provider === "string" ? body.provider.trim() : "" const primaryModel = typeof body?.primaryModel === "string" ? body.primaryModel.trim() : "" const models = parseModels(body?.models) const routes = parseRoutes(body?.routes) const primaryRouteId = typeof body?.primaryRouteId === "string" ? body.primaryRouteId.trim() : "" if (routes.length === 0 && !provider) throw new Error("provider is required") if (routes.length === 0 && !primaryModel && models.length === 0) throw new Error("at least one model is required") const config = await saveSandboxInferenceConfig(validateSandboxId(sandboxId), { provider, primaryModel: primaryModel || models[0], models, routes, primaryRouteId, }) return NextResponse.json({ ok: true, config }) } catch (error) { const message = error instanceof Error ? error.message : "Failed to save sandbox inference config" return NextResponse.json({ ok: false, error: message }, { status: /required|unsupported|model/.test(message) ? 400 : 500 }) } }