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

73 lines
2.1 KiB
JavaScript

import { startCase } from "@/lib/graph/orchestrator.js";
import { withAuthenticatedApi } from "@/lib/supabase/api-auth.js";
async function post(request) {
try {
const body = await request.json();
const result = await startCase(body);
if (result.success) {
return Response.json(result, { status: 200 });
}
if (result.code === "PROVIDER_UNAVAILABLE") {
console.error("[api/cases/start] provider unavailable", {
error: result.error ?? "Start case failed",
providerApiPath: result.providerApiPath,
});
return Response.json(
{ success: false, error: "Reasoning service is temporarily unavailable." },
{ status: 503 },
);
}
const status =
result.statusCode === 400
? 400
: result.statusCode >= 500
? result.statusCode
: 500;
const diagnostics = {
status,
error: result.error ?? "Start case failed",
validationErrors: result.validationErrors,
analysisErrors: result.analysisErrors,
validationIssues: result.validationIssues,
providerApiPath: result.providerApiPath,
providerExecution: result.providerExecution,
rawResponse: result.rawResponse ?? undefined,
};
if (status >= 500) {
console.error("[api/cases/start] error response", diagnostics);
} else {
console.warn("[api/cases/start] error response", diagnostics);
}
return Response.json(
{
success: false,
error: status === 400 ? diagnostics.error : "Reasoning request could not be completed.",
...(status === 400 ? { validationErrors: result.validationErrors } : {}),
},
{ status },
);
} catch (error) {
console.error("[api/cases/start] unhandled exception", {
message: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
error,
});
return Response.json(
{
success: false,
error: "Internal server error",
},
{ status: 500 },
);
}
}
export const POST = withAuthenticatedApi(post);