import { projectRef } from "@/lib/projects"; import { isTakeId, listTakes, readVerdicts, setTakeVerdict } from "@/lib/report/takes.mjs"; export const dynamic = "force-dynamic"; // GET ?project= /takes/verdicts.json, as read // POST { project, take, verdict?, note? } set one take's verdict ("like" | // "maybe" | "no" | null) and/or note; // an absent field keeps its value // // The take must be one the listing returns. A verdict on a take that is not // there would be written into a file another agent reads as a decision. export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); return Response.json({ verdicts: await readVerdicts(project.dir) }, { headers: { "cache-control": "no-store" } }); } export async function POST(request: Request) { const body = (await request.json().catch(() => null)) as { project?: string; take?: string; verdict?: string | null; note?: string; } | null; if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400 }); if (!isTakeId(body.take)) return Response.json({ error: "take must be a take id" }, { status: 400 }); const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const { takes } = await listTakes(project.dir); if (!takes.some((t) => t.id === body.take)) return Response.json({ error: "no such take" }, { status: 404 }); try { const r = await setTakeVerdict(project.dir, body.take, { verdict: body.verdict, note: body.note }); return Response.json({ ok: true, ...r }, { headers: { "cache-control": "no-store" } }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }