import { projectRef } from "@/lib/projects"; import { kindMakesTakes } from "@/lib/projects/kinds.mjs"; import { askEditor, cancelDraftJob, EditorError, readDraftJob, readPolicy } from "@/lib/articles/generation"; import { SuggestRequestError, clearSuggestJob, draftBodyOf, parseSuggestRequest, readSuggestJob, suggestJobView, suggestRouteOf, suggestionsView, writeSuggestJob, } from "@/lib/report/clipSuggest.mjs"; export const dynamic = "force-dynamic"; // THE ASSISTANT IN THE CLIP EDITOR (release 36 lane D) -- optional, every // part of it: the labels (Suggest), and an answer to a comment (Ask the // assistant). The model runs in the EDITOR, routed by settings.generation (a // `report-draft` job, task `clips-suggest`); this route asks it with the // shared WORKER_TOKEN (lib/articles/generation.ts askEditor), remembers the // job for the page and follows it. It never writes the manifest or a clip. // // GET ?project= the labels, their stale state, the route, the job being followed // GET ?project=&job= that job (ended: forgotten) // POST { project, mode?: "labels", proposals? } | { project, mode: "ask", note } → { jobId, job } // POST ?cancel=&project= stop it const headers = { "cache-control": "no-store" }; async function suggestProject(id: unknown) { const project = await projectRef(String(id ?? "")); if (!project) return { error: Response.json({ error: "no such project" }, { status: 404, headers }) }; if (!kindMakesTakes(project.kind)) return { error: Response.json({ error: "the assistant's labels are a report video's" }, { status: 400, headers }) }; return { project }; } const failed = (e: unknown) => { const status = e instanceof EditorError ? e.status : e instanceof SuggestRequestError ? 400 : 500; return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status, headers }); }; async function jobOf(dir: string, id: string) { const rec = await readSuggestJob(dir); const job = await readDraftJob(id, 40); const view = suggestJobView(rec && rec.jobId === id ? rec : null, job); if (view.ended) await clearSuggestJob(dir, id); return view; } export async function GET(request: Request) { const url = new URL(request.url); const r = await suggestProject(url.searchParams.get("project")); if ("error" in r) return r.error; const dir = r.project.dir; const id = url.searchParams.get("job"); if (id) { try { return Response.json(await jobOf(dir, id), { headers }); } catch (e) { return failed(e); } } const view = await suggestionsView(dir); // The route and the followed job need the editor; without one the labels still show. const policy = await readPolicy().catch((e: unknown) => e as Error); const rec = await readSuggestJob(dir); let job = null; if (rec) { job = await jobOf(dir, rec.jobId).catch(async (e: unknown) => { // The editor no longer knows it (restarted): forget it. if (e instanceof EditorError && e.status === 404) await clearSuggestJob(dir, rec.jobId); return null; }); } return Response.json( { ...view, route: policy instanceof Error ? null : suggestRouteOf(policy.generation, dir), editorError: policy instanceof Error ? policy.message : null, job, }, { headers }, ); } export async function POST(request: Request) { const url = new URL(request.url); const cancel = url.searchParams.get("cancel"); if (cancel) { const r = await suggestProject(url.searchParams.get("project")); if ("error" in r) return r.error; try { await cancelDraftJob(cancel); return Response.json({ cancelled: true }, { headers }); } catch (e) { return failed(e); } } 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 r = await suggestProject(body.project); if ("error" in r) return r.error; const dir = r.project.dir; try { const req = parseSuggestRequest(body); const running = await readSuggestJob(dir); if (running) { const view = await jobOf(dir, running.jobId).catch(() => null); if (view && !view.ended) return Response.json({ error: "the assistant is already working on this project", job: view }, { status: 409, headers }); } const answer = await askEditor<{ jobId?: string }>("/api/ops/report-draft", { method: "POST", body: draftBodyOf(dir, req) }); const jobId = String(answer.jobId ?? ""); if (!jobId) return Response.json({ error: "the editor started no job" }, { status: 502, headers }); await writeSuggestJob(dir, { jobId, mode: req.mode, ...(req.mode === "ask" ? { note: req.note } : {}), at: new Date().toISOString() }); const job = await jobOf(dir, jobId).catch(() => ({ id: jobId, mode: req.mode, status: "queued", ended: false, queue: null, progress: null, last: null, summary: null })); return Response.json({ ok: true, jobId, job }, { headers }); } catch (e) { return failed(e); } }