77 lines
2.4 KiB
JavaScript
77 lines
2.4 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, getProviderModelName } from "@/lib/llm/provider.js";
|
|
import { synthesizeCurrentUnderstanding } from "@/lib/graph/current-understanding-synthesis.js";
|
|
import { withAuthenticatedApi } from "@/lib/supabase/api-auth.js";
|
|
|
|
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: getProviderModelName(),
|
|
}
|
|
);
|
|
|
|
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.statusCode === 400
|
|
? "Invalid synthesis request"
|
|
: "Reasoning request could not be completed.",
|
|
},
|
|
{ status: error.statusCode }
|
|
);
|
|
}
|
|
|
|
// Unexpected error
|
|
return Response.json(
|
|
{ success: false, stage: "internal", error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export const POST = withAuthenticatedApi(post);
|