import { projectRef } from "@/lib/projects"; import { EditorError, editorConfig, followRun, lastStartedRun, postFetchWindows, projectFetchBody, readFetchRuns, recordFetchRun, summarizeAnswer, TERMINAL, } from "@/lib/report/fetchWindows.mjs"; import { benchPrepAfterWrite } from "@/lib/report/benchPrepTrigger.mjs"; export const dynamic = "force-dynamic"; // FETCH WINDOWS (release 35 O1): the project's clip windows asked of the editor // as ONE fetch-windows batch — the Cut tab's and the clip deck's button, and // `umtool fetch` from a terminal (lib/report/fetchWindows.mjs builds the body // for both). The editor fetches, paces and writes; this asks and records each // run in /fetch-runs.json. The token never reaches the browser. // // GET ?project= the body's size, what is skipped, the // last run, whether this umtool can ask // POST { project, action: "dry-run" } the editor's plan: groups per platform, // cached, in flight, unresolved // POST { project, action: "start" } one paced job per platform queue // POST { project, action: "follow" } the last started run's jobs, once: their // status and progress; an ended job's // refused windows read from its log const headers = { "cache-control": "no-store" }; function fail(e: unknown) { if (e instanceof EditorError) return Response.json({ error: e.message }, { status: e.status, headers }); return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500, headers }); } 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, headers }); try { const { body, skipped } = await projectFetchBody(project); const runs = (await readFetchRuns(project.dir)).runs; const cfg = editorConfig(); return Response.json( { project: project.id, configured: !!cfg.token, editor: cfg.url, items: body.items.length, skipped, lastRun: lastStartedRun({ runs }), runs: runs.length }, { headers }, ); } catch (e) { return fail(e); } } export async function POST(request: Request) { const b = (await request.json().catch(() => null)) as Record | null; if (!b || typeof b !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const project = await projectRef(String(b.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404, headers }); const action = String(b.action ?? ""); const cfg = editorConfig(); try { if (action === "follow") { const run = lastStartedRun(await readFetchRuns(project.dir)); if (!run) return Response.json({ error: "no fetch run has started jobs yet" }, { status: 404, headers }); const jobs = await followRun(cfg, project.dir, run); // A run that has ended landed what it landed: the bench's prep for it, // in the background (lib/report/benchPrep.mjs). if (jobs.length && jobs.every((j: { status: string }) => TERMINAL.has(j.status)) && jobs.some((j: { status: string }) => j.status === "done")) { benchPrepAfterWrite(project.dir, null, { excerptsOnly: false }); } return Response.json({ run: { at: run.at, jobIds: run.jobIds, summary: run.summary }, jobs }, { headers }); } if (action !== "dry-run" && action !== "start") { return Response.json({ error: 'action must be "dry-run", "start" or "follow"' }, { status: 400, headers }); } const { body, skipped } = await projectFetchBody(project, { localIds: true }); if (!body.items.length) return Response.json({ error: "no clip windows to fetch" }, { status: 400, headers }); const answer = await postFetchWindows(cfg, body, { dryRun: action === "dry-run" }); const run = await recordFetchRun(project.dir, { body, answer }); return Response.json({ items: body.items.length, skipped, summary: summarizeAnswer(answer), run }, { headers }); } catch (e) { return fail(e); } }