import { fetchClip, NO_EDITOR_TEXT, resolveClipTarget, validateFetchClipArgs, type FetchClipDeps, type FetchClipOutcome, } from "yt-dlp-transcript-mcp/research"; import { openSource, SourceError } from "./sources"; import type { FetchAnswer } from "./types"; // --------------------------------------------------------------------------- // FETCH CLIP from the reader: the seconds behind a cite, through the editor's // fetch-window route -- EXACTLY as the MCP's fetch_clip asks (its client, // mcp/src/fetchClip.ts, and its cite-to-directory mapping, clipTarget.ts), // never yt-dlp by hand. The editor paces it, uses its cookies, writes the file // into the corpus and records who asked ("umtool") and why. // // The route never waits: a fetch is queued and the page polls by job id // (`{ job }`, fetch_clip's resume), one short answer per poll. // --------------------------------------------------------------------------- const deps: FetchClipDeps = { env: process.env, fetch: (url, init) => globalThis.fetch(url, init), sleep: (ms) => new Promise((r) => setTimeout(r, ms)), now: () => Date.now(), }; /** The answer, in the page's words. */ export function answerOf(o: FetchClipOutcome): FetchAnswer { switch (o.kind) { case "cached": return { state: "ready", file: o.file, from: o.from, to: o.to, cached: true }; case "fetched": return o.file ? { state: "ready", file: o.file, ...(o.from !== undefined ? { from: o.from } : {}), ...(o.to !== undefined ? { to: o.to } : {}), cached: false } : { state: "failed", message: "the editor finished but named no file", job: o.jobId }; case "queued": return { state: "queued", job: o.jobId, status: o.status }; case "no_editor": return { state: "failed", message: NO_EDITOR_TEXT.replace(/^fetch_clip: /, "") }; case "cooldown": return { state: "failed", message: `${o.platform} is cooling down after a 429 (${Math.ceil(o.cooldownMs / 60000)} min): ${o.error}` }; case "refused": return { state: "failed", message: `the editor refused it (${o.status}): ${o.error}`, ...(o.jobId ? { job: o.jobId } : {}) }; case "failed": return { state: "failed", message: `the editor's job ${o.status}: ${o.log || "no reason given"}`, job: o.jobId }; case "unreachable": return { state: "failed", message: `the editor at ${o.url} did not answer: ${o.message}`, ...(o.jobId ? { job: o.jobId } : {}) }; } } export type FetchRequest = | { job: string } | { source: string; channel: string; video: string; start: number; end: number; reason: string; storedUrl?: "fallback" | "only" }; export async function fetchForReader(req: FetchRequest): Promise { const args = "job" in req ? { job: req.job, wait_seconds: 0 } : { channel: req.channel, video: req.video, start: req.start, end: req.end, reason: req.reason, ...(req.storedUrl ? { storedUrl: req.storedUrl } : {}), wait_seconds: 0, }; const v = validateFetchClipArgs(args); if (!v.ok) throw new SourceError(v.error.replace(/^fetch_clip: /, "")); if (!("job" in req)) { const opened = await openSource(req.source); await resolveClipTarget(opened.source, opened.option.handle, v.request); } return answerOf(await fetchClip(v.request, deps, { requestedBy: "umtool" })); }