import { readFile } from "node:fs/promises"; import path from "node:path"; import { readIteration } from "yt-dlp-transcript-common/lib/report/iterations"; import { draftedFields, ownerOf, reviewAddress } from "yt-dlp-transcript-common/lib/report/drafted.mjs"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { draftFileOf } from "@/lib/report/chain-draft.mjs"; import { sitesPaths } from "./sites"; // "DRAFT WITH" ON A PROJECT PAGE (release 28 M9): which iteration a video // project was made from — the media tasks are report-draft STEPS on a done // iteration, so a project that names none has nothing to draft with — and // what each task's last run left in its `-draft.json` beside the // manifest (what it wrote, what it skipped, what a check refused, what it // kept as an alternative). Read only. SERVER-ONLY. /** The media tasks a project page drafts, in Draft everything's order. */ export const PROJECT_DRAFT_TASKS = ["cut", "onscreen", "teaser", "flips", "threads", "narrate", "youtube", "social"] as const; export type ProjectDraftTask = (typeof PROJECT_DRAFT_TASKS)[number]; export type ProjectIteration = | { ok: true; article: string; n: number; status: string; running: Record } | { ok: false; why: string }; /** * The iteration a project directory belongs to: `/
/i/` * (common's projectOf), whose iteration exists and names this directory as its * project (or names none). Else why not, in one line. */ export async function projectIteration(dir: string): Promise { const abs = path.resolve(dir); const m = /^i(\d+)$/.exec(path.basename(abs)); const article = path.basename(path.dirname(abs)); if (!m || !isReportId(article)) { return { ok: false, why: "this project was not made from an article's iteration (its folder is not
/i), so no draft task can name one" }; } const n = Number(m[1]); const it = await readIteration(article, n, sitesPaths()).catch(() => null); if (!it) return { ok: false, why: `no iteration i${n} of the article "${article}": the drafts are steps on an iteration` }; if (it.projectDir && path.resolve(it.projectDir) !== abs) { return { ok: false, why: `${article} i${n} names another project (${it.projectDir})` }; } // A draft still running on it (a step with its job): the page follows it. const running: Record = {}; for (const s of it.steps) if (s.status === "running" && s.jobId && !s.task.startsWith("chain:")) running[s.task] = s.jobId; return { ok: true, article, n, status: it.status, running }; } export type DraftUnitSummary = { id: string; label: string | null; /** applied | skipped | refused (a hard check) | kept (an alternative: the field is the operator's, or a draft without force) | none */ outcome: "applied" | "skipped" | "refused" | "kept" | "none"; why: string | null; }; export type DraftRunSummary = { task: ProjectDraftTask; file: string; model: string | null; at: string | null; units: DraftUnitSummary[]; counts: Record; checks: { name: string; ok: boolean; detail: string | null }[]; alternatives: number; }; type RawUnit = { label?: unknown; skipped?: unknown; applied?: unknown; chosen?: { hard?: unknown } | null; }; const strings = (v: unknown) => (Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : []); /** One task's last run, from its draft file, or null (never run here, or unreadable). */ export async function draftRunSummary(dir: string, task: ProjectDraftTask): Promise { const file = draftFileOf(task); let doc: Record; try { doc = JSON.parse(await readFile(path.join(dir, file), "utf8")); } catch { return null; } if (!doc || typeof doc !== "object" || Array.isArray(doc)) return null; const units: DraftUnitSummary[] = []; const raw = doc.units && typeof doc.units === "object" ? (doc.units as Record) : {}; for (const [id, u] of Object.entries(raw)) { const label = typeof u?.label === "string" ? u.label : null; if (typeof u?.skipped === "string") units.push({ id, label, outcome: "skipped", why: u.skipped }); else if (u?.applied === "applied") units.push({ id, label, outcome: "applied", why: null }); else if (typeof u?.applied === "string") units.push({ id, label, outcome: "kept", why: u.applied === "operator" ? "the field is yours: kept as an alternative" : `kept as an alternative (${u.applied})` }); else { const hard = strings(u?.chosen?.hard); units.push({ id, label, outcome: hard.length ? "refused" : "none", why: hard.length ? hard.join("; ") : u?.chosen ? null : "no draft" }); } } const counts = { applied: 0, skipped: 0, refused: 0, kept: 0, none: 0 }; for (const u of units) counts[u.outcome] += 1; const checks = Array.isArray(doc.checks) ? (doc.checks as { name?: unknown; ok?: unknown; detail?: unknown }[]) .filter((c) => typeof c?.name === "string") .map((c) => ({ name: String(c.name), ok: c.ok === true, detail: typeof c.detail === "string" ? c.detail : null })) : []; return { task, file, model: typeof doc.model === "string" ? doc.model : null, at: typeof doc.at === "string" ? doc.at : null, units, counts, checks, alternatives: Array.isArray(doc.alternatives) ? doc.alternatives.length : 0, }; } /** Every task's last run in this project. */ export async function draftRuns(dir: string): Promise>> { const out: Partial> = {}; for (const t of PROJECT_DRAFT_TASKS) { const s = await draftRunSummary(dir, t); if (s) out[t] = s; } return out; } // ─── The rows the project page's Draft section draws ─── export type DraftRowView = { task: ProjectDraftTask; /** The section's name. */ label: string; /** What the task writes, and the code's limits. */ writes: string; /** What the field holds now, in a few words. */ state: string; /** The review address of a whole-cut field (its badge and alternatives), else null (per entry: the review page). */ address: string | null; /** The entries a per-entry task may be limited to, else null. */ entries: { id: string; label: string }[] | null; /** Why it cannot draft on this cut (no deck for flips and threads), else null. */ blocked: string | null; }; type Entry = { id?: unknown; type?: unknown; heading?: unknown; onscreen?: { title?: unknown; subtitle?: unknown }; narration?: { text?: unknown }; lines?: unknown }; const ownerWords = (o: "empty" | "drafted" | "operator") => (o === "empty" ? "empty" : o === "drafted" ? "drafted" : "yours (never replaced)"); /** The Draft section's rows for a manifest, in Draft everything's order. */ export function draftRowsOf(manifest: Record): DraftRowView[] { const tl = (Array.isArray(manifest.timeline) ? manifest.timeline : []) as Entry[]; const drafted = draftedFields(manifest); const nDrafted = (field: string) => drafted.filter((d) => d.field === field).length; const ids = (es: Entry[]) => [...new Set(es.map((e) => (typeof e.id === "string" ? e.id : "")).filter(Boolean))]; const labelOf = (e: Entry) => `${String(e.id)}${typeof e.heading === "string" && e.heading ? ` — ${e.heading}` : ""}`; const listOf = (es: Entry[]) => ids(es).map((id) => ({ id, label: labelOf(es.find((e) => e.id === id)!) })); const render = (manifest.render ?? {}) as { chrome?: unknown }; const deck = !!render.chrome && typeof render.chrome === "object"; const clips = tl.filter((e) => e.type === "clip"); const cards = tl.filter((e) => e.type === "card"); const teasers = tl.filter((e) => e.type === "teaser"); const withOnscreen = tl.filter((e) => e.onscreen && (typeof e.onscreen.title === "string" || typeof e.onscreen.subtitle === "string")).length; const narrated = cards.filter((e) => typeof e.narration?.text === "string" && e.narration.text.trim()).length; const withLines = teasers.filter((e) => Array.isArray(e.lines) && e.lines.length > 0).length; const whole = (field: string) => reviewAddress({ field }); return [ { task: "cut", label: "the cut", writes: "which cited clips, in what order (ids from the citations only; windows untouched)", state: `${clips.length} clip(s); the order is ${ownerWords(ownerOf(manifest, { field: "timeline" }))}`, address: whole("timeline"), entries: null, blocked: null, }, { task: "onscreen", label: "on-screen titles", writes: "a title (≤ 60) and a subtitle (≤ 90) per entry, grounded in its claim and quote", state: `${withOnscreen} of ${tl.length} entries written, ${nDrafted("onscreen")} drafted`, address: null, entries: listOf(tl), blocked: null, }, { task: "teaser", label: "the teaser", writes: "3 to 5 lines of ≤ 70, no quote read out", state: teasers.length ? `${withLines} of ${teasers.length} teaser(s) written, ${nDrafted("lines")} drafted` : "no teaser entry in the cut", address: null, entries: teasers.length ? listOf(teasers) : null, blocked: null, }, { task: "flips", label: "flips", writes: "THEN/NOW pairs over the cut's clips, of different dates (the left panel)", state: deck ? `the flips are ${ownerWords(ownerOf(manifest, { field: "render.chrome.flips" }))}` : "no on-screen deck", address: whole("render.chrome.flips"), entries: null, blocked: deck ? null : "flips are drawn by the on-screen deck, and this cut has none (render.chrome)", }, { task: "threads", label: "threads", writes: "2 to 4 threads and each clip's thread (the left panel; not with flips)", state: deck ? `the threads are ${ownerWords(ownerOf(manifest, { field: "render.chrome.threads" }))}` : "no on-screen deck", address: whole("render.chrome.threads"), entries: null, blocked: deck ? null : "threads are drawn by the on-screen deck, and this cut has none (render.chrome)", }, { task: "narrate", label: "narration", writes: "each card's narration, third person, every line grounded", state: `${narrated} of ${cards.length} card(s) narrated, ${nDrafted("narration")} drafted`, address: null, entries: cards.length ? listOf(cards) : null, blocked: null, }, { task: "youtube", label: "YouTube text", writes: "a title (≤ 100) and a summary (≤ 900); the build adds chapters and sources to out/youtube.txt", state: `the title and description are ${ownerWords(ownerOf(manifest, { field: "publish.youtube" }))}`, address: whole("publish.youtube"), entries: null, blocked: null, }, { task: "social", label: "social posts", writes: "a post for X (280, a link 23) and for Bluesky (300 graphemes), each with up to 4 more under it; the link added by code; text only, never posted", state: `the X post is ${ownerWords(ownerOf(manifest, { field: "publish.x" }))}, the Bluesky post ${ownerWords(ownerOf(manifest, { field: "publish.bluesky" }))}`, address: null, entries: null, blocked: null, }, ]; }