68 lines
1.9 KiB
JavaScript
68 lines
1.9 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 });
|
|
}
|
|
|
|
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: diagnostics.error,
|
|
validationErrors: result.validationErrors,
|
|
diagnostics: result.diagnostics,
|
|
analysisErrors: result.analysisErrors,
|
|
validationIssues: result.validationIssues,
|
|
providerApiPath: result.providerApiPath,
|
|
providerExecution: result.providerExecution,
|
|
rawResponse: result.rawResponse ?? undefined,
|
|
},
|
|
{ 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);
|