import { CHANNELS_DIR, REPORTS_ROOT } from "@/lib/paths"; import { jobView, runningJob, startJob } from "@/lib/jobs"; import { refuseCaller } from "@/lib/report/caller"; import { crossSiteRefusal } from "@/lib/report/sameOrigin.mjs"; import { UMTOOL_DIR } from "@/lib/report/driver.mjs"; import { EditorError, editorConfig } from "@/lib/report/fetchWindows.mjs"; import { ActionError, parseClipList, planAction, quoteCheckArgv, startAction } from "@/lib/ingredients/actions.mjs"; import { resolveProject } from "@/lib/projects/core.mjs"; export const dynamic = "force-dynamic"; // THE INGREDIENT RAIL'S ACTIONS (lane L5; lib/ingredients/actions.mjs): a stage's one click, in two steps. // // POST { action, ch, id, step: "plan", project?, clips?, window?, fix? } // -> { action, what, cost: "network" | "gpu" | "disk" | null, queue, start: { route, body }, plan? } // what the action will do, what it costs and which queue it lands on (the editor's own dry run for // fetch-windows and persist-videos); the page shows it and asks // POST { ...start.body, step: "start" } // persist | transcribe | fetch | evict -> { ok, action, jobIds, message } the editor's ops, asked here // quote-check -> 202 { ok, job } `umtool quote-check` on the job runner // (links: the plan's start names /api/report/links, umtool's own link check) // // The token never reaches the browser; the editor decides queues, pacing and whole-recording downloads. const headers = { "cache-control": "no-store" }; export async function POST(request: Request) { // Another site's page may not start a download, a transcription or an evict: a cross-site or foreign-Origin // POST is refused (sameOrigin.mjs), and the body must be JSON -- which a no-cors form post cannot send. const cross = crossSiteRefusal(request.headers); if (cross) return Response.json({ error: cross }, { status: 403, headers }); if (!/^application\/json\b/i.test(request.headers.get("content-type") ?? "")) return Response.json({ error: "the body must be application/json" }, { status: 415, headers }); const refused = refuseCaller(request, "/api/ingredients/action"); if (refused) return refused; const b = (await request.json().catch(() => null)) as Record | null; if (!b || typeof b !== "object") return Response.json({ error: "expected JSON" }, { status: 400, headers }); const req = { action: String(b.action ?? ""), ch: String(b.ch ?? ""), id: String(b.id ?? ""), ...(b.project !== undefined ? { project: String(b.project) } : {}), ...(b.clips !== undefined ? { clips: b.clips as string[] } : {}), ...(b.window !== undefined ? { window: String(b.window) } : {}), ...(b.fix === true ? { fix: true } : {}), ...(typeof b.key === "string" ? { key: b.key } : {}), }; const ctx = { cfg: editorConfig(), reportsRoot: REPORTS_ROOT, channelsDir: CHANNELS_DIR }; try { if (b.step === "plan") return Response.json(await planAction(req, ctx), { headers }); if (b.step !== "start") return Response.json({ error: 'step is "plan" or "start"' }, { status: 400, headers }); if (req.action === "quote-check") { const r = await resolveProject(String(req.project ?? ""), REPORTS_ROOT); if (!r?.project) return Response.json({ error: `no project "${req.project}"` }, { status: 404, headers }); const clips = parseClipList(req.clips) as string[]; const running = runningJob(); if (running) return Response.json({ error: `a job is already running (${running.kind}) — check the quotes when it is done`, running: jobView(running) }, { status: 409, headers }); const argv = quoteCheckArgv(UMTOOL_DIR, r.project.id, clips); const job = startJob(`quote-check ${r.project.id} ${clips.join(",")}`, [{ cwd: UMTOOL_DIR, env: {}, label: `quote check: ${clips.join(", ")} (${r.project.id})`, argv, timeoutMs: 60 * 60_000 }], { project: r.project.id }); return Response.json({ ok: true, action: "quote-check", job: jobView(job), jobIds: [job.id], message: `quote check started (${job.id})` }, { status: 202, headers }); } if (req.action === "links") return Response.json({ error: "a link check starts at /api/report/links" }, { status: 400, headers }); return Response.json(await startAction(req, ctx), { headers }); } catch (e) { if (e instanceof ActionError) return Response.json({ error: e.message }, { status: (e as ActionError & { status: number }).status, headers }); if (e instanceof EditorError) return Response.json({ error: e.message }, { status: (e as EditorError & { status: number }).status, headers }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500, headers }); } }