import { jobView, runningJob, startJob } from "@/lib/jobs"; import { invalidateProjects, projectRef } from "@/lib/projects"; import { agentToken, routeCaller } from "@/lib/annotations/agentAuth.mjs"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { LiveShareError, editorWindowAsker, prepareLiveShare } from "@/lib/report/liveShare.mjs"; import { setShortAside, shortSteps } from "@/lib/report/shorts.mjs"; export const dynamic = "force-dynamic"; // A SHAREABLE CLIP OF A STREAM STILL LIVE (plans/live-preclip.md; // lib/report/liveShare.mjs): the clip (added at [start, end] when given by // seconds), its window cut NOW by the editor from the recording in progress // (`/api/media/live-window`), then its short (`build-video --short`, no // network) as ONE job on the app's runner — followed on /api/report/shorts // (`?job=`), listed there like any short. The clip editor's Share now; `pnpm // ops live-clip` asks it with the WORKER_TOKEN. // // POST { project, clip | start, end, from?: "/", replace? } // → { ok, clip, added, window, windowSource, job } // → 409 { error } past what is recorded ("ask again in N s"), a job running, // or a short already there (needsReplace) const headers = { "cache-control": "no-store" }; export async function POST(request: Request) { const who = routeCaller(request.headers.get("authorization"), agentToken(), "/api/report/live-share"); if (!who.ok) return Response.json({ error: who.error }, { status: who.status, headers }); const body = (await request.json().catch(() => null)) as Record | null; if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400, headers }); const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: `no project ${String(body.project ?? "")}` }, { status: 404, headers }); if (!kindMakesTakes(project.kind)) return Response.json({ error: "a share is made of a report video's clip" }, { status: 400, headers }); const running = runningJob(); if (running) return Response.json({ error: `a job is already running (${running.kind})`, running: jobView(running) }, { status: 409, headers }); let prepared: Awaited>; try { prepared = await prepareLiveShare(project, body, { askWindow: editorWindowAsker() }); } catch (e) { const status = e instanceof LiveShareError ? e.status : 500; return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status, headers }); } invalidateProjects(); let aside: string | null = null; try { aside = await setShortAside(project.dir, prepared.clip, { replace: body.replace === true || prepared.added }); } catch (e) { const needsReplace = !!(e as { needsReplace?: boolean })?.needsReplace; return Response.json({ error: e instanceof Error ? e.message : String(e), needsReplace, clip: prepared.clip }, { status: 409, headers }); } try { const job = startJob(`short ${project.id} ${prepared.clip}`, shortSteps(project, prepared.clip, { offline: true }), { project: project.id }); return Response.json({ ok: true, ...prepared, aside, job: jobView(job) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e), clip: prepared.clip }, { status: 409, headers }); } }