// THE INTRO, EDITED (`render.intro`; operator, 8 Nov 2026: "more first-class // intro support (view in play the cut)"): one reading and the writes behind // the Cut tab's intro editor (components/projects/report/IntroEditor.tsx, // /api/report/chrome/intro) and `umtool intro`, so a terminal and the page // cannot disagree about what the cut opens on. // // introState(project) the intro as written, the entry the cut opens with // (its words resolved), each version's on/off // introPatch(current, pairs) `key=value …` onto the intro as written (the CLI's) // writeIntro(dir, value, { token }) the project's render.intro -- updateRender, the one // look writer, so the build's own checks refuse it // setVersionIntro(dir, take, on, …) a VERSION (a made take) without the intro: its patch // carries `render.intro: null` (a style variant), written // by the take's own writer (takeEdit.mjs editTakeManifest) // // Plain ESM: `umtool intro` runs it under bare node. import { INTRO_KEYS, INTRO_STYLE_DEFAULT, introErrors } from "yt-dlp-transcript-common/lib/report/intro.mjs"; import { readManifest } from "../projects/report.mjs"; import { manifestToken, updateRender } from "./manifest.mjs"; import { listTakes } from "./takes.mjs"; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const clone = (v) => (v === undefined ? undefined : JSON.parse(JSON.stringify(v))); /** `render.intro` as written, always its object form (a bare style name is `{ style }`); null for none. */ export function introObject(v) { if (v === undefined || v === null || v === false) return null; if (typeof v === "string") return { style: v }; return isObj(v) ? clone(v) : null; } /** * The object written back: a lone `style` stays the bare name it was * (`"intro": "boot-dark"`), so an edit that only changes the style does not * reshape the manifest. */ export function introWritten(o) { if (!o) return null; const keys = Object.keys(o); return keys.length === 1 && keys[0] === "style" ? o.style : o; } /** A take patch that switches the intro off: `render.intro: null`. */ export const takeIntroOff = (patch) => isObj(patch) && isObj(patch.render) && "intro" in patch.render && patch.render.intro === null; /** * One `key=value` read for the intro: `seconds` a number, `lines` a JSON list * or `a|b|c`, `sound=false` silence, `url=false|true`, `projectUrl=false`, * `site` a JSON object or a word, anything else the text. `key=` (empty) * clears the key. * * @param {string} key * @param {string} raw */ export function introValue(key, raw) { const s = String(raw ?? ""); if (s === "") return undefined; if (key === "seconds") { const n = Number(s); return Number.isFinite(n) ? n : s; } if (key === "lines") { if (s.trim().startsWith("[")) { try { return JSON.parse(s); } catch { return s; } } return s.split("|").map((l) => l.trim()).filter(Boolean); } if ((key === "sound" || key === "projectUrl") && s === "false") return false; if (key === "url" && (s === "false" || s === "true")) return s === "true"; if (key === "site" && s.trim().startsWith("{")) { try { return JSON.parse(s); } catch { return s; } } return s; } /** * `pairs` (`[key, raw]`, the CLI's `key=value`) onto the intro as written: * a new intro starts as the brand sting (INTRO_STYLE_DEFAULT); a key not of * the intro's is refused by name. Returns the object to write (introErrors * still decides whether it builds). * * @param {unknown} current render.intro as written * @param {Array<[string, string]>} pairs */ export function introPatch(current, pairs) { const o = introObject(current) ?? { style: INTRO_STYLE_DEFAULT }; for (const [k, raw] of pairs) { if (!INTRO_KEYS.includes(k)) throw new Error(`render.intro.${k} is not a key (${INTRO_KEYS.join(", ")})`); const v = introValue(k, raw); if (v === undefined) { if (k === "style") throw new Error("render.intro.style cannot be cleared: --off takes the intro off"); delete o[k]; } else o[k] = v; } return o; } /** * The intro of a project: as written, the entry the cut opens with (selectVariant's, its title-card * words and its site resolved -- or the reason it does not resolve), and every made version's state. * * @param {{ id: string, dir: string }} project */ export async function introState(project) { const manifest = await readManifest(project.dir); if (!manifest) throw new Error("the project has no video.manifest.json"); const written = manifest.render?.intro ?? null; const errors = introErrors(written); let entry = null; let entryError = null; if (written !== null && !errors.length) { try { const { selectVariant } = await import("umtool-report-to-video/build-video"); const { isIntro } = await import("umtool-report-to-video/intro"); entry = (selectVariant(manifest).timeline ?? []).find(isIntro) ?? null; } catch (e) { entryError = e instanceof Error ? e.message : String(e); } } const { takes } = await listTakes(project.dir); const versions = takes.map((t) => ({ id: t.id, label: t.label, draft: !!t.draft, // A version's own word: its patch takes the intro off, or puts one on of its own (one the same as the cut's, // which `on` writes, is the cut's). intro: takeIntroOff(t.patch) ? "off" : isObj(t.patch?.render) && "intro" in t.patch.render && JSON.stringify(t.patch.render.intro) !== JSON.stringify(written) ? "own" : "cut", })); return { project: project.id, token: await manifestToken(project.dir), title: typeof manifest.title === "string" ? manifest.title : null, generatedOn: typeof manifest.generatedOn === "string" ? manifest.generatedOn : null, intro: written, errors, entry, entryError, versions, }; } /** * The project's `render.intro`, through the one look writer (updateRender: the build's checks, the * manifest's token). `value` null takes the intro off. * * @param {string} dir * @param {unknown} value * @param {{ token?: string | null }} [opts] */ export async function writeIntro(dir, value, { token = null } = {}) { return updateRender(dir, { intro: value === null ? null : introWritten(introObject(value)) }, { token }); } /** * A VERSION (a made take) with or without its intro (umtool redesign L3: a saved version is frozen, so * its intro is a STYLE of the version -- a field of its patch over the frozen base, never a copy of * anything). Off writes `render.intro: null` into its patch: that version opens on no intro, the draft * untouched. On REMOVES the key from its patch (takeEdit.mjs editTakePatch: the take writer that can * delete), so the version opens on the intro of what it is laid over -- a saved version's own frozen * base, a legacy take's draft -- with no stale copy left behind. Refused when that has no intro to open on. * * @param {string} dir * @param {string} take * @param {boolean} on * @param {{ token?: string | null }} [opts] */ export async function setVersionIntro(dir, take, on, { token = null } = {}) { const manifest = await readManifest(dir); if (!manifest) throw new Error("the project has no video.manifest.json"); if (on) { const { frozenBaseText } = await import("./takes.mjs"); const baseText = await frozenBaseText(dir, take); const under = baseText !== null ? JSON.parse(baseText) : manifest; const intro = under?.render?.intro ?? null; if (intro === null || intro === undefined) { throw new Error( baseText !== null ? `version ${take} was saved with no intro: nothing to put back (the draft's intro is written with umtool intro

style=…)` : "the cut has no intro to put back: write one first (umtool intro

style=…)", ); } } const { editTakeManifest, editTakePatch } = await import("./takeEdit.mjs"); if (on) { const r = await editTakePatch(dir, take, "intro-on", token, (p) => { if (!isObj(p.render) || !("intro" in p.render)) return; delete p.render.intro; if (!Object.keys(p.render).length) delete p.render; }); return { ...r, intro: "cut" }; } return editTakeManifest(dir, take, "intro-off", token, (m) => { m.render = isObj(m.render) ? m.render : {}; delete m.render.intro; return { intro: "off" }; }); }