feat(confidence-engine): establish current understanding synthesis seam
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 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 provider resolution ───────────
|
||||
const result = await synthesizeCurrentUnderstanding(
|
||||
{ situationGraph, findings },
|
||||
{ provider: getProvider() }
|
||||
);
|
||||
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user