import { analyseScenario, PROMPT_VERSIONS, DEFAULT_PROMPT_VERSION, } from "@/lib/analysis"; import { withAuthenticatedApi } from "@/lib/supabase/api-auth.js"; async function post(request) { try { const body = await request.json(); if (!body.scenario || typeof body.scenario !== "string") { return Response.json( { error: "Request must include a 'scenario' string field" }, { status: 400 }, ); } // Optional prompt version override let promptVersion = DEFAULT_PROMPT_VERSION; if (body.promptVersion && PROMPT_VERSIONS.includes(body.promptVersion)) { promptVersion = body.promptVersion; } const result = await analyseScenario(body.scenario, { promptVersion }); if (!result.success) { return Response.json( { error: "Reasoning request could not be completed." }, { status: Number(result.statusCode) || 500 }, ); } return Response.json({ inputClassification: result.inputClassification, reconstruction: result.reconstruction, evidence: result.evidence, nextQuestion: result.nextQuestion, modelName: result.modelName, responseDurationMs: result.responseDurationMs, validationStatus: result.validationStatus, promptVersion: result.promptVersion, }); } catch (e) { if (e.code === "PROVIDER_UNAVAILABLE") { return Response.json( { error: "Reasoning service is temporarily unavailable." }, { status: 503 }, ); } const validationFailed = e?.validationFailed || e?.code === "VALIDATION_FAILED"; if (validationFailed) { return Response.json( { success: false, error: "Reasoning request could not be completed." }, { status: Number(e.statusCode) || 500 }, ); } const statusCode = Number(e.statusCode) || 500; return Response.json( { error: "Reasoning request could not be completed." }, { status: statusCode }, ); } } export const POST = withAuthenticatedApi(post);