import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { REPORT_DRAFT_BODY_KEYS, enqueueReportDraft, parseReportDraftBody } from "yt-dlp-transcript-common/controller/reportDraft"; import { planRefresh } from "yt-dlp-transcript-common/controller/reportRefresh"; import { planClipsSuggestJob } from "yt-dlp-transcript-common/controller/clipsSuggest"; import { planVideoAssistJob } from "yt-dlp-transcript-common/controller/videoAssist"; import { planProposeGroupsJob } from "yt-dlp-transcript-common/controller/proposeGroups"; import { planCastAssignJob } from "yt-dlp-transcript-common/controller/castAssign"; import { createCorpusTools } from "yt-dlp-transcript-mcp/callTool"; import { jobResponse, ops, opsFail } from "../_lib"; import { refreshEditor, researchEditor } from "../_researchEditor"; export const dynamic = "force-dynamic"; // POST { task, article, iteration?, model?, extractorModel?, feedback?, // question?, channels?, from?, to?, copyOf?, maxUsd?, source?, build?, // cards?, entries?, force?, autonomy? } // -> { ok: true, jobId, iteration, warnings? } // // A generation over an article's iteration (release 27 S6), as a // `report-draft` job on `draft:api` or `draft:local` by the model's provider: // - task "sweep": a new iteration from `question` over `channels` (and // `from`/`to`); the API model runs the agent, a local model the pipeline; // - "revise-report" / "revise-video": a new iteration revising `iteration` // from `feedback`; // - a media draft — "narrate", "cut", "onscreen", "teaser", "flips", // "threads", "youtube" (release 28), "social" (release 29): a step on the done iteration // `iteration` that drafts fields of its video manifest (`entries`: only // these entry ids; `force`: re-draft a field that is a draft already — // never one the operator wrote; narrate's `cards`); // - "research" (S11): a new research run from `question` (`channels` // optional, `autonomy` propose | limits | auto, default the settings'): // it discovers, proposes, and enqueues what its autonomy allows through // this editor's own ops actions (research-approve settles the rest); // - "sweep" with `iteration` N: CONTINUES research run N — its question and // its channels widened by what it acquired, the sweep's parent N (refused // while N still waits, unless `force`); // - `copyOf` N: iteration N again on `model` (same kind and inputs); // - "refresh" (umtool R4): re-fetch the iteration's scope (`iteration`, // default the current report's) through this editor's own sync / // fetch-posts / refresh-report / transcribe-bucket, build its site // locally (never a deploy), search again for what changed, and — when // something did, within the caps — sweep the new recordings and revise // the report: a new `refresh` iteration, one job on `draft:refresh`. // `fetch: false` skips the fetch and the build; `dryRun: true` answers // `{ ok, plan }` and writes nothing. The log ends `@@report-draft {…, // refresh: {status, newIds, changed, drifted, deleted, summary}}`; // - `ids` on a sweep: only those recordings (kept in its scope); on a // refresh (umtool R6): the update reads those (a triage's flags); // - "triage" (umtool R6): the cheap pass for an OWNER (`article`: the // schedule it is for) — tier 0 (what is new in `channels` by id since its // last run, matching `terms` or digest `topics`; or `ids` named) and tier // 1 (the window judge on settings.generation.triage's model: local on // `draft:local` through the GPU arbiter, or the extractor on // `draft:api`), then whether its flags reach `threshold` {strong, weak}. // A run of reports/.triage/// (triage.json, trace.jsonl); // answers `{ ok, jobId, iteration: k, triage: {owner, k} }`. A sweep's // `triage: "/"` names the triage it escalates; // - "clips-suggest" (release 36 lane D, controller/clipsSuggest.ts): on a // umtool reactions project, `project` (its absolute directory) instead of // `article` — labels, a why and a rank for its proposals // (`clip-suggestions.json`; `proposals`: only these), or with `note` an // answer to that comment posted to umtool's notes as the agent's // (UMTOOL_URL, WORKER_TOKEN); `answers`: a Claude Code session's labels // (the `code` route), written as a model's; `dryRun: true` answers // `{ ok, plan }` (the proposals, calls, the estimate, the request) and // writes nothing. Answers `{ ok, jobId, iteration: 0 }`; // - "video-assist" (release 38 lane E, controller/videoAssist.ts): on a // umtool video project, `project` (its absolute directory) and `request` // (what to change, in words; or `note`, a note on the whole project whose // text is the request — the proposals are posted to it as the agent's // reply), `scope` look | clips | fx | all — ONE ask whose checked ops land // in `/assist-proposals.json` as proposals (nothing applied; // umtool's Apply does that); `answers`: a Claude Code session's // `{ops, reply}` (the `code` route); `dryRun: true` answers `{ ok, plan }` // (the prompt, the estimate) and writes nothing. Answers // `{ ok, jobId, iteration: 0 }`; // - "propose-groups" (lane L4, controller/proposeGroups.ts): on a umtool // video project, `project` (its absolute directory) and optional // `request` (words for the ask) — ONE ask whose checked claim groups land // in `/group-proposals.json` as proposals (nothing applied; // umtool's Accept does that, one group at a time); `answers`: a Claude // Code session's `{groups: [{section, claim, ids, why}]}` (the `code` // route); `dryRun: true` answers `{ ok, plan }`. Answers // `{ ok, jobId, iteration: 0 }`; // - "cast-assign" (characters C3, controller/castAssign.ts): on a umtool // video project, `project` and optional `handles` (the post authors to ask // about) — ONE ask whose checked answers (a name, a stock voice, a speed, // a direction each) land in `/narration/cast-assign.json` // (nothing applied; umtool lays them over its proposed rows); `answers`: // a Claude Code session's `{authors: [...]}`; `dryRun: true` answers // `{ ok, plan }`. Answers `{ ok, jobId, iteration: 0 }`. // `model` defaults to settings.generation's route for the task (a task routed // to `code` needs one named). `extractorModel` is an API sweep's extractor // (a Claude model id; default settings.generation.models.extractor). An API job's cap is `maxUsd`, else the task's // default (settings.generation.caps), never above `caps.max`; a job that would // start past the day's API total (`caps.daily`) is refused, naming it. The // corpus is `source` (a handle), `build` (a composed build's id), or the newest // build holding the channels. `job cancel` stops it (the iteration ends // `cancelled`). The job's log ends `@@report-draft {json}` with the result. // // AN ADAPTER: the refusals are the controller's (controller/reportDraft.ts); // the corpus tools are the MCP's own, in process (mcp/src/callTool.ts). export async function POST(request: Request) { return ops(request, REPORT_DRAFT_BODY_KEYS, async (body) => { const deps = { paths: getPaths(), corpusTools: (source: string) => createCorpusTools({ source }), researchEditor, refreshEditor, }; if (body.task === "refresh" && body.dryRun === true) { // The plan: what a refresh would do, nothing written. try { const plan = await planRefresh(parseReportDraftBody(body), deps); return NextResponse.json({ ok: true, plan }); } catch (e) { return opsFail((e as Error).message); } } if (body.task === "clips-suggest" && body.dryRun === true) { try { const plan = await planClipsSuggestJob(parseReportDraftBody(body), deps); return NextResponse.json({ ok: true, plan }); } catch (e) { return opsFail((e as Error).message); } } if (body.task === "propose-groups" && body.dryRun === true) { try { const plan = await planProposeGroupsJob(parseReportDraftBody(body), deps); return NextResponse.json({ ok: true, plan }); } catch (e) { return opsFail((e as Error).message); } } if (body.task === "cast-assign" && body.dryRun === true) { try { const plan = await planCastAssignJob(parseReportDraftBody(body), deps); return NextResponse.json({ ok: true, plan }); } catch (e) { return opsFail((e as Error).message); } } if (body.task === "video-assist" && body.dryRun === true) { try { const plan = await planVideoAssistJob(parseReportDraftBody(body), deps); return NextResponse.json({ ok: true, plan }); } catch (e) { return opsFail((e as Error).message); } } const out = await enqueueReportDraft(body, deps); if (!out.result.ok || !("iteration" in out)) return jobResponse(out.result); void out.result.stream.cancel(); return NextResponse.json({ ok: true, jobId: out.result.jobId, iteration: out.iteration, ...(out.triage ? { triage: out.triage } : {}), ...(out.warnings?.length ? { warnings: out.warnings } : {}), }); }); }