// THE WALK, as data: which clips a walk visits, in what order, and where `p` // and `n` go -- for the project's own cut, or for a TAKE (walk a take): the // project's manifest with the take's patches applied, resolveTakeManifest's // merge (takeManifest.mjs), the one the take build makes. // // One reading for the bench page (ClipBenchPage), the walk's data route // (GET /api/report/walk) and `umtool walk`, so a terminal and the page cannot // disagree about what is left to judge. // // WHAT THE WALK VISITS (umtool/docs/clip-bench.md, "What the walk skips"): // the clips that still NEED JUDGEMENT (no verdict) and are FETCHED (a cached // file holds the clip's own window end to end), in walk order (by source -- // lib/projects/report.mjs walkOrder). `all` widens it to every FETCHED clip, // judged or not: a take's clips are new windows, often carrying the verdict // of the clip they were cut from, and "watch the tight version through" is a // walk of all of them. // // THE INTRO (lane intro-play): a cut that opens on a brand intro (render.intro) lists it FIRST -- `intro`, the // walk's first stop (`stops`), watched in Play (it has no verdict to give, so `p`/`n` and `first` stay on clips). // // Plain ESM: `umtool walk` runs it under bare node. import { clipVerdict, readClipDetail, readManifest, walkOrder, walkReadiness } from "../projects/report.mjs"; import { rejectionOf } from "./rejectReasons.mjs"; import { manifestToken } from "./manifest.mjs"; /** * The manifest a walk reads: the project's, or the take's. * * @param {{ dir: string }} project * @param {string | null} take * @returns {Promise<{ manifest: Record, token: string | null, take: null | { id: string, label: string, draft: boolean, made: boolean, editable: boolean }, note: string | null }>} */ export async function walkManifest(project, take = null) { if (!take) { const manifest = await readManifest(project.dir); if (!manifest) throw new Error("the project has no video.manifest.json"); return { manifest, token: await manifestToken(project.dir), take: null, note: null }; } const [{ resolveTakeManifest }, { takeToken }, { listTakes }] = await Promise.all([ import("./takeManifest.mjs"), import("./takeEdit.mjs"), import("./takes.mjs"), ]); const r = await resolveTakeManifest(project.dir, take); const token = await takeToken(project.dir, take); const listed = (await listTakes(project.dir)).takes.find((t) => t.id === take); return { manifest: r.manifest, token, take: { id: take, label: r.take.label, draft: !!listed?.draft, made: !!listed && !listed.draft, // Edits land in takes//take.json: a take a recipe only names has none to land in. editable: token !== null, }, note: r.note, }; } /** * Where `p` and `n` go from `clipId`: the nearest clip on the walk either side * in walk order (never the clip itself). `order` is walk order; `onWalk` the * ids the walk visits. * * @param {string[]} order * @param {Set} onWalk * @param {string} clipId */ export function walkNeighbours(order, onWalk, clipId) { const i = order.indexOf(clipId); const ok = (id) => id !== clipId && onWalk.has(id); const prev = [...order.slice(0, Math.max(0, i))].reverse().find(ok) ?? null; const next = (i < 0 ? order : order.slice(i + 1)).find(ok) ?? null; return { prev, next }; } /** * The intro a cut opens on, as the walk lists it: its id, style, length and words -- or null (none, or one the * build would refuse, which the check says). Read through the build's own entry (introEntry), so a title-card's * words are the project's title and date. * * @param {Record} manifest * @returns {Promise} */ export async function walkIntroOf(manifest) { if (manifest?.render?.intro === undefined || manifest?.render?.intro === null) return null; try { const { introEntry } = await import("umtool-report-to-video/intro"); const e = introEntry(manifest.render, { provenance: manifest.provenance ?? null, manifest }); if (!e) return null; return { id: e.id, style: e.style, seconds: e.seconds, title: typeof e.title === "string" ? e.title : null, lines: Array.isArray(e.lines) ? e.lines : [] }; } catch { return null; } } /** * @typedef {{ * id: string, cut: number | null, channel: string | null, video: string, start: number, end: number, * seconds: number, cutStart: number | null, cutEnd: number | null, * verdict: "confirmed" | "incorrect" | "rejected" | "unreviewed", correction: string | null, quote: string | null, * rejected: { reason: string, label: string, fact: boolean, note: string, why: string } | null, * fetched: boolean, onWalk: boolean, * }} WalkClip */ /** * The walk of a project's cut or of a take. * * @param {{ id: string, dir: string }} project * @param {{ take?: string | null, all?: boolean }} [opts] */ export async function walkOf(project, { take = null, all = false } = {}) { const base = await walkManifest(project, take); const detail = await readClipDetail(project.dir, { manifest: base.manifest }); if (!detail) throw new Error("the project has no video.manifest.json"); const ordered = walkOrder(detail.entries); const readiness = walkReadiness(ordered); const onWalk = new Set(all ? ordered.filter((e) => !!e.fetched).map((e) => e.id) : readiness.readyIds); const cutIndex = new Map( (detail.entries ?? []).filter((e) => e.kind === "clip").map((e, i) => [e.id, i + 1]), ); /** @type {WalkClip[]} */ const clips = ordered.map((/** @type {any} */ e) => ({ id: e.id, /** 1-based place among the cut's clips (the timeline's order). */ cut: cutIndex.get(e.id) ?? null, channel: e.channel ?? null, video: e.video, start: e.start, end: e.end, seconds: Number((Number(e.cutEnd ?? e.end) - Number(e.cutStart ?? e.start)).toFixed(2)), cutStart: e.cutStart ?? null, cutEnd: e.cutEnd ?? null, verdict: clipVerdict(e), correction: typeof e.correction === "string" && e.correction.trim() ? e.correction.trim() : null, // Why it was rejected (a fact error or a cut reason), in words; null when it was not. rejected: (() => { const r = rejectionOf(e); return r ? { reason: r.reason, label: r.label, fact: r.fact, note: r.note, why: r.why } : null; })(), quote: typeof e.quote === "string" ? e.quote : null, fetched: !!e.fetched, onWalk: onWalk.has(e.id), })); const order = clips.map((c) => c.id); const first = order.find((id) => onWalk.has(id)) ?? null; const intro = await walkIntroOf(base.manifest); const reviewed = clips.filter((c) => c.verdict !== "unreviewed").length; return { project: project.id, take: base.take, note: base.note, all, token: base.token, manifest: base.manifest, detail, clips, order, onWalk, first, /** The intro the cut opens on, listed first (null: none). */ intro, /** What the walk lists, in order: the intro (when there is one), then the clips in walk order. */ stops: [...(intro ? [intro.id] : []), ...order], /** Clips that need an answer, and how many of them can be judged today (fetched). */ needing: readiness.needing, ready: readiness.ready, reviewed, total: clips.length, seconds: Number(clips.reduce((s, c) => s + c.seconds, 0).toFixed(2)), }; } /** walkOf's answer as JSON for the route and `umtool walk --json`: no manifest, no detail, no Set. */ export function walkJson(w) { const { manifest: _m, detail: _d, onWalk: _o, ...rest } = w; return rest; }