// FILL BY HAND (release 32 R2): beside Draft everything on the Media tab, every // media field the cut leaves EMPTY offered as a blank a person fills -- the // clips' on-screen titles, the teasers' lines, the flips or the threads, the // YouTube title and description, the X and Bluesky posts -- so a run with no // compute at all reaches a buildable manifest. // // What a person writes goes through the manifest writer as the operator's edit // (manifest.mjs writeDraftedField: the field's own validators, a snapshot, the // write token) and carries NO drafted mark: by the drafted-fields contract // (common/lib/report/drafted.mjs) a non-empty field with no mark is the // operator's -- "written by hand" -- and no draft ever replaces it. A blank is // a form field, never a placeholder written into the manifest: an empty value // is nobody's (a draft may fill it), and a placeholder would ship in a build. // // The flips and the threads have no other hand editor, so they are offered // whole (their pairs or list as they stand) whenever the deck draws them. // // Plain ESM: `node --test` runs it. import { isEmptyField } from "yt-dlp-transcript-common/lib/report/drafted.mjs"; import { FIELD_LABELS } from "umtool-report-to-video/drafts-review"; import { writeDraftedField } from "./manifest.mjs"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** The fields Fill by hand writes, in the order it saves them. */ export const HAND_FIELDS = Object.freeze(["onscreen", "lines", "render.chrome.flips", "render.chrome.threads", "publish.youtube", "publish.x", "publish.bluesky"]); const firstOf = (timeline, type) => { const seen = new Set(); return timeline.filter((e) => isObj(e) && e.type === type && typeof e.id === "string" && !seen.has(e.id) && seen.add(e.id)); }; const readPath = (m, field) => field.split(".").reduce((o, k) => (isObj(o) ? o[k] : undefined), m); /** * What Fill by hand offers for a manifest: * empty every empty field: { address, entry, field, label, context } * clips the cut's clips (id, a label), for the flips' and threads' selects * deck whether the on-screen deck is on (the flips and threads need it) * flips the flips as they stand, else null; threads likewise, with each * clip's thread (`clips`) * * @param {any} manifest */ export function handFillView(manifest) { const m = isObj(manifest) ? manifest : {}; const timeline = Array.isArray(m.timeline) ? m.timeline : []; const empty = []; for (const e of firstOf(timeline, "clip")) { if (!isEmptyField("onscreen", e.onscreen)) continue; empty.push({ address: `onscreen/${e.id}`, entry: e.id, field: "onscreen", label: FIELD_LABELS.onscreen, context: [typeof e.quote === "string" ? e.quote : "", typeof e.chapter === "string" ? e.chapter : ""].filter(Boolean).join(" ยท "), }); } for (const e of firstOf(timeline, "teaser")) { if (!isEmptyField("lines", e.lines)) continue; empty.push({ address: `teaser/${e.id}/lines`, entry: e.id, field: "lines", label: FIELD_LABELS.lines, context: "" }); } const chrome = isObj(m.render?.chrome) ? m.render.chrome : null; const flips = chrome && !isEmptyField("render.chrome.flips", chrome.flips) ? chrome.flips : null; const threadsList = chrome && !isEmptyField("render.chrome.threads", chrome.threads) ? chrome.threads.list : null; for (const field of ["publish.youtube", "publish.x", "publish.bluesky"]) { if (isEmptyField(field, readPath(m, field))) empty.push({ address: field, entry: null, field, label: FIELD_LABELS[field] ?? field, context: "" }); } const clips = firstOf(timeline, "clip").map((e) => ({ id: e.id, label: [e.onscreen?.title, typeof e.quote === "string" ? e.quote.slice(0, 60) : ""].find((x) => typeof x === "string" && x.trim()) ?? e.id, thread: typeof e.thread === "string" && e.thread ? e.thread : null, })); return { empty, clips, deck: !!chrome, flips: flips ? { pairs: flips.pairs } : null, threads: threadsList ? { list: threadsList, clips: Object.fromEntries(clips.filter((c) => c.thread).map((c) => [c.id, c.thread])) } : null, }; } /** A fill as the form sends it, checked for shape (the writer checks the rest). */ function cleanFill(f, i) { if (!isObj(f)) throw new Error(`fill ${i + 1} is an object`); const field = String(f.field ?? ""); if (!HAND_FIELDS.includes(field)) throw new Error(`fill ${i + 1}: ${field || "(no field)"} is not a field filled by hand`); const onEntry = field === "onscreen" || field === "lines"; const entry = onEntry ? String(f.entry ?? "") : null; if (onEntry && !entry) throw new Error(`fill ${i + 1}: ${field} names its entry`); return { entry, field, value: f.value }; } /** * Write the fills, one field at a time through writeDraftedField (each its own * checked, snapshotted write; the token carried from one to the next). Stops at * the first refusal: what was written before it stays, and is said. * * @param {string} dir * @param {unknown[]} fills * @param {{ token?: string | null }} [opts] * @returns {Promise<{ written: string[], token: string | null, refused: { field: string, entry: string | null, error: string, errors: string[] } | null }>} */ export async function applyHandFill(dir, fills, { token = null } = {}) { if (!Array.isArray(fills) || !fills.length) throw new Error("nothing to write: fill a field first"); const clean = fills.map(cleanFill).sort((a, b) => HAND_FIELDS.indexOf(a.field) - HAND_FIELDS.indexOf(b.field)); const written = []; let t = token; for (const f of clean) { try { const w = await writeDraftedField(dir, { entry: f.entry, field: f.field, value: f.value }, { token: t }); t = w.token ?? null; written.push(f.entry ? `${f.field}/${f.entry}` : f.field); } catch (e) { if (e?.name === "StaleToken") throw e; return { written, token: t, refused: { field: f.field, entry: f.entry, error: e instanceof Error ? e.message : String(e), errors: Array.isArray(e?.errors) ? e.errors : [] } }; } } return { written, token: t, refused: null }; }