import { getWorkerToken } from "yt-dlp-transcript-common/lib/workerToken"; import type { ResearchEditor } from "yt-dlp-transcript-common/lib/report/draft/research/tools"; // A RESEARCH RUN'S EDITOR IS THIS EDITOR (release 27 S11). // // A research job's archive.org reads and acquisitions are ops actions — the // same routes `pnpm ops` posts to — called IN PROCESS: the route handler with // a Request carrying the editor's own worker token. So an acquisition meets // exactly the door an agent over HTTP meets (the body's keys, the slug check, // the platform's hold, the queue), with the same refusal in the same words, // and no URL of its own to know. Only the actions a research run may ask for // are here. type Post = (request: Request) => Promise; const ROUTES: Record Promise<{ POST: Post }>> = { "archive-search": () => import("./archive-search/route"), "archive-item": () => import("./archive-item/route"), "import-archive-org": () => import("./import-archive-org/route"), "import-video": () => import("./import-video/route"), "create-channel": () => import("./create-channel/route"), "fetch-posts": () => import("./fetch-posts/route"), "capture-posts": () => import("./capture-posts/route"), "import-post": () => import("./import-post/route"), "transcribe-one": () => import("./transcribe-one/route"), "channel-priority": () => import("./channel-priority/route"), "persist-videos": () => import("./persist-videos/route"), }; export const RESEARCH_EDITOR_ACTIONS = Object.keys(ROUTES); // An in-process door over `routes`: each action is its route's POST with a // Request carrying the editor's own worker token. `who` names the caller in // the refusal of an action it may not ask for. export function inProcessEditor(routes: Record Promise<{ POST: Post }>>, who: string): ResearchEditor { return { async run(action, args) { const load = routes[action]; if (!load) throw new Error(`"${action}" is not an action ${who} may ask the editor for`); const token = getWorkerToken(); if (!token) throw new Error("the editor's ops actions are off (WORKER_TOKEN is unset)"); const { POST } = await load(); const res = await POST( new Request(`http://localhost/api/ops/${action}`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(args), }), ); let body: Record = {}; try { const j = await res.json(); if (j && typeof j === "object" && !Array.isArray(j)) body = j as Record; } catch { // not JSON: refused below with the status } if (res.status < 200 || res.status >= 300 || body.ok === false) { throw new Error(typeof body.error === "string" && body.error ? body.error : `HTTP ${res.status}`); } return body; }, }; } export const researchEditor: ResearchEditor = inProcessEditor(ROUTES, "a research run"); // A REFRESH'S EDITOR (umtool R4, controller/reportRefresh.ts): the fetches it // runs — sync, fetch-posts, refresh-report, transcribe-bucket — and the build // (`publish {verb: "build"}`; a refresh never deploys), through the same door. const REFRESH_ROUTES: Record Promise<{ POST: Post }>> = { sync: () => import("./sync/route"), "fetch-posts": () => import("./fetch-posts/route"), "refresh-report": () => import("./refresh-report/route"), "transcribe-bucket": () => import("./transcribe-bucket/route"), publish: () => import("./publish/route"), }; export const REFRESH_EDITOR_ACTIONS = Object.keys(REFRESH_ROUTES); const refreshDoor = inProcessEditor(REFRESH_ROUTES, "a refresh"); export const refreshEditor: ResearchEditor = { async run(action, args) { // A refresh builds; it never deploys (nor runs Publish now). if (action === "publish" && (args as { verb?: unknown }).verb !== "build") throw new Error("a refresh only builds: publish {verb: \"build\"}"); return refreshDoor.run(action, args); }, }; // FIND CLIPS' EDITOR (release 36 lane A, controller/findClips.ts): the steps // the chain hands to the editor's own ops actions — persist the source, the // corpus transcript, the chat replay — through the same door. const FIND_CLIPS_ROUTES: Record Promise<{ POST: Post }>> = { "persist-videos": () => import("./persist-videos/route"), "transcribe-one": () => import("./transcribe-one/route"), "fetch-chat": () => import("./fetch-chat/route"), }; export const FIND_CLIPS_DOOR_ACTIONS = Object.keys(FIND_CLIPS_ROUTES); export const findClipsEditor: ResearchEditor = inProcessEditor(FIND_CLIPS_ROUTES, "find-clips");