Files
confidence-engine/app/api/cases/synthesis/route.js
T

72 lines
2.2 KiB
JavaScript

/**
* Dedicated Current Understanding synthesis API route.
*
* Route: POST /api/cases/synthesis
*
* Follows the thin route pattern established by cases/start and cases/update routes:
* parse request → invoke domain seam → return validated result → map failure status
*
* No synthesis business logic belongs in this file.
*/
import { getProvider } from "@/lib/llm/provider.js";
import { synthesizeCurrentUnderstanding } from "@/lib/graph/current-understanding-synthesis.js";
export async function POST(request) {
try {
const body = await request.json();
// ── Parse / validate input contract ───────────────────────
if (!body || typeof body !== "object") {
return Response.json(
{ success: false, stage: "request_validation", error: "Invalid request body" },
{ status: 400 }
);
}
const { situationGraph, findings } = body;
if (!situationGraph) {
return Response.json(
{ success: false, stage: "request_validation", error: "Missing situationGraph" },
{ status: 400 }
);
}
// ── Invoke domain seam with configured model ──────────────
const result = await synthesizeCurrentUnderstanding(
{ situationGraph, findings },
{
provider: getProvider(),
modelName: process.env.OLLAMA_MODEL ?? null,
}
);
return Response.json({ success: true, currentUnderstanding: result.currentUnderstanding }, { status: 200 });
} catch (error) {
if (error instanceof SyntaxError) {
return Response.json(
{ success: false, stage: "request_validation", error: "Invalid JSON request body" },
{ status: 400 }
);
}
if (error.statusCode) {
return Response.json(
{
success: false,
stage: error.statusCode === 400 ? "request_validation" : "provider",
error: error.message ?? "Synthesis failed",
},
{ status: error.statusCode }
);
}
// Unexpected error
return Response.json(
{ success: false, stage: "internal", error: "Internal server error" },
{ status: 500 }
);
}
}