import { createCase, listCases } from "@/lib/research/boards"; import { CaseError } from "@/lib/research/cases.mjs"; import { fail, ok } from "../_respond"; export const dynamic = "force-dynamic"; // --------------------------------------------------------------------------- // THE BOARD API (plans/umtool-redesign.md, R2). Research cases, umtool-side // under REPORTS_ROOT/.umtool/research//case.json (lib/research/cases.mjs) // -- never in the corpus. A case's piece id is `c:`. P5's Create // consumes this; the shapes are lib/research/case-types.ts. Every answer is // JSON, `no-store`; an error is `{ error }` in words with its status (400 bad // input, 404 no such case or quote or recording, 409 a case.json that cannot // be read -- never overwritten -- or a name taken, 502 a corpus or the editor // that cannot be read). // // GET /api/research/cases // -> { cases: [{ id, piece, title, quotes, sections, updatedAt, scope, drifted, error? }] } // POST /api/research/cases { title?, id?, query?, scope?, notes? } // a case from a query and scope (the title defaults to the query), // or empty (a title); the id is the title's slug unless named // -> { case: CaseDoc } // GET /api/research/cases/ // -> CaseView { case, piece, stored, sourceLabel, reading, localOnly } // PATCH /api/research/cases/ { title?, notes?, scope?, sections?, order? } // sections: the whole ordered list [{ id?, title }] (no id = new; // left out = removed, its quotes to Unsorted); order: every quote // [{ id, section }] in board order -> CaseView // POST /api/research/cases//quotes // { source, channel, video, t, until?, track?, kind?, claim?, note?, // section?, search?, scope?, at? } // PIN: the cite is read from the corpus NOW and kept as a snapshot // (the words, the cite, the house-format citation); the same cite // twice answers the quote already there with `duplicate: true`. // `search` ({ session, query, total, complete, notReached, // sourceLabel }) makes that search's reading log the case's. // -> { quote, duplicate, case: CaseView } // PATCH /api/research/cases//quotes/ // { claim?, note?, section?, speakerChecked?, useCurrentWords? } // useCurrentWords: true takes the words the last re-verify read // (only for a drifted quote; the old words kept in its history) // -> { quote, case: CaseView } // DELETE /api/research/cases//quotes/ // -> { quote, index, case: CaseView } (POST it back with `at` to undo) // POST /api/research/cases//verify { quotes?: [qid] } // re-read every quote (or those) at its cite in the CURRENT // transcript (R1's window, ±30 s); drifted / missing with the words // there now, and en vs en-orig where both exist. Never changes a // quote's words. -> { results: [{ id, verify }], case: CaseView } // GET /api/research/cases//export.md[?download=1] // the board as cited markdown in the house format (text/markdown) // POST /api/research/cases//handoff { to: "article" | "video" | "sweep", confirm?, ... } // without confirm -> { plan } (ArticlePlan | VideoPlan; writes nothing) // with confirm: true -> { href } (article, sweep: { href, jobId, iteration }) // article, sweep: { article?, model?, question? } video: { slug?, title? } // sweep (R3): the case's scope with no seed, on the policy's sweep // route; its href is the run's page, /research/runs/
/ // (a quote may carry `by: "agent"` and a case `run: { article, n, at }`: // a run's findings opened as a board, POST /api/research/runs///board) // --------------------------------------------------------------------------- export async function GET() { try { return ok({ cases: await listCases() }); } catch (e) { return fail(e); } } export async function POST(request: Request) { try { const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object" || Array.isArray(body)) throw new CaseError("expected a JSON object: { title?, id?, query?, scope?, notes? }"); const scope = body.scope && typeof body.scope === "object" && !Array.isArray(body.scope) ? (body.scope as Record) : undefined; return ok({ case: await createCase({ ...(typeof body.title === "string" ? { title: body.title } : {}), ...(typeof body.id === "string" ? { id: body.id } : {}), ...(typeof body.query === "string" ? { query: body.query } : {}), ...(typeof body.notes === "string" ? { notes: body.notes } : {}), ...(scope ? { scope } : {}), }), }); } catch (e) { return fail(e); } }