import { addFindings, batchFile, changePrivateNames, gapAction, handoffToBoard, pipelineView, reportFile, saveSummary, startStage } from "@/lib/research/pipeline"; import { CasePipelineError } from "yt-dlp-transcript-common/lib/research/casePipeline"; import { fail, ok } from "../../../../_respond"; export const dynamic = "force-dynamic"; // A CASE'S PIPELINE, its actions (lib/research/pipeline.ts). The door an agent uses too (the MCP's case tools, // lane D, through UMTOOL_URL); `archilyzer case …` runs the same stages from a shell. // // POST coverage | candidates {window?, maxSpan?} | date-checks {aroundDays?, latest?} // -> { started: true, view }: runs in the background; follow GET ../ // POST batches {budget?} | export {minRelevance?} // -> { view } when done // POST private-names {add?: [hex], remove?: [hex], clear?: true} // -> { count, changed }: sha256 hex digests ONLY (the page hashes a name // in the browser); a term is refused // POST findings {rows: [FindingRow…]} -> { rows } appended: each checked, the whole write behind the gate // POST summary {text, by?, model?} -> { summary }: a person's (by hand), or a model's draft // POST handoff {confirm?, minRelevance?} // -> { plan } / { plan, pinned, duplicates }: the merged findings onto // the board, one section per reader group, behind the gate // POST gap {action: download-missing | transcribe | persist | focus, slug, confirm?} // -> { plan } / { done, route, answer }: the editor's own ops // GET batch?batch= -> the batch's wrapped text (text/plain) // GET report?format=md|html[&download=1] // -> REPORT.md / REPORT.html as exported // A PrivacyBlocked is a 409 whose words are counts and row ids. const STAGES = new Set(["coverage", "candidates", "batches", "export", "date-checks"]); type Ctx = { params: Promise<{ id: string; op: string }> }; export async function POST(request: Request, ctx: Ctx) { try { const { id: raw, op } = await ctx.params; const id = decodeURIComponent(raw); const body = ((await request.json().catch(() => ({}))) ?? {}) as Record; if (typeof body !== "object" || Array.isArray(body)) throw new CasePipelineError("expected a JSON object"); if (STAGES.has(op)) { const r = await startStage(id, op, body); return ok({ ...r, view: await pipelineView(id) }); } if (op === "private-names") return ok(await changePrivateNames(id, body)); if (op === "findings") return ok({ rows: await addFindings(id, body.rows) }); if (op === "summary") return ok({ summary: await saveSummary(id, body) }); if (op === "handoff") return ok(await handoffToBoard(id, { confirm: body.confirm === true, minRelevance: body.minRelevance as number | undefined })); if (op === "gap") return ok(await gapAction(id, body)); throw new CasePipelineError(`no pipeline action "${op}"`, 404); } catch (e) { return fail(e); } } export async function GET(request: Request, ctx: Ctx) { try { const { id: raw, op } = await ctx.params; const id = decodeURIComponent(raw); const q = new URL(request.url).searchParams; if (op === "batch") { return new Response(await batchFile(id, q.get("batch") ?? ""), { headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" } }); } if (op === "report") { const format = q.get("format") === "html" ? "html" : "md"; const r = await reportFile(id, format); if (!r) throw new CasePipelineError("nothing exported yet: Export the report first", 404); return new Response(r.body, { headers: { "content-type": format === "html" ? "text/html; charset=utf-8" : "text/markdown; charset=utf-8", "cache-control": "no-store", ...(q.get("download") === "1" ? { "content-disposition": `attachment; filename="${r.name}"` } : {}), }, }); } throw new CasePipelineError(`no pipeline read "${op}"`, 404); } catch (e) { return fail(e); } }