// THE VIDEO KNOBS, writer side (plans/video-knobs-ui.md): what a person sets // in the clip editor, the fact-check editor, the markers editor or Deliver -- // and an agent sets with `umtool set` / `umtool look` -- before manifest.mjs // writes it. // // a claimed entry claim.at / hold / stamp / count / label / eventDate (factcheck.mjs normalizeClaim, validateClaims) // a clip audio: { offset, gain, fadeIn, fadeOut } (clip-audio.mjs validateClipAudio) // the fact-check render.chrome.factcheck.stamp.scale / motion / opacity, // verdicts[V].stampLabel (factcheck.mjs validateFactcheck) // the deck render.chrome.deck.markers[].align, deck.markerLayout (deck.mjs validateChrome) // the encode ladder render.variants (variants.mjs validateVariants) // // The RULES are the renderer's (report-to-video, plans/video-knobs-core.md): // a value the writer accepts is one the build accepts. What is here is the // writer's side of them: a patch typed by a person or an agent ("12.4", // "false", "") read into the value it means, a knob at its default removed, // and which entry's knobs the build reads. // // PURE: no fs. Plain ESM so `umtool set` runs it under bare node. import { CLAIM_KEYS, CLAIM_LIMITS, CLAIM_MARKS_MAX, FACTCHECK_DEFAULTS, FACTCHECK_LIMITS, STAMP_MOTIONS, VERDICTS, rawMarksOf, resolveFactcheck, } from "umtool-report-to-video/factcheck"; import { resolveCounter } from "umtool-report-to-video/counter"; import { MARKER_ALIGNS, MARKER_LAYOUTS } from "umtool-report-to-video/deck"; import { isIsoDate } from "umtool-report-to-video/claims"; import { DEFAULT_VARIANTS, validateVariants, variantsOf } from "umtool-report-to-video/variants"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const num = (v) => typeof v === "number" && Number.isFinite(v); const numIn = (v, lo, hi) => num(v) && v >= lo && v <= hi; const q = (v) => JSON.stringify(v); const round = (n, dp) => Number(Number(n).toFixed(dp)); export { MARKER_ALIGNS, MARKER_LAYOUTS, STAMP_MOTIONS, variantsOf }; // --------------------------------------------------------------------------- // A claim's knobs (per entry). // --------------------------------------------------------------------------- /** The knobs a claim may carry beside its id and verdict, in the editor's order. */ export const CLAIM_KNOBS = Object.freeze(CLAIM_KEYS.filter((k) => k !== "id" && k !== "verdict")); /** Their limits: the renderer's. */ export const CLAIM_KNOB_LIMITS = CLAIM_LIMITS; /** The defaults a claim with no knob gets (hold's is the cut's `factcheck.stamp.seconds`). */ export const CLAIM_KNOB_DEFAULTS = Object.freeze({ stamp: true, count: 1 }); /** * One knob's value as typed, read and checked. `null` (or "") = clear the * knob. Throws a sentence naming `claim.`. That `at` lies in the entry's * window is validateClaims' (it has the entry). * * @param {string} key * @param {unknown} v * @returns {unknown} */ export function claimKnobValue(key, v) { if (v === null || v === undefined || v === "") return null; switch (key) { case "at": { const x = typeof v === "string" ? Number(v) : v; if (!num(x) || x < 0) throw new Error(`claim.at must be a source second, 0 or more (got ${q(v)})`); return round(x, 2); } case "hold": { const x = typeof v === "string" ? Number(v) : v; const [lo, hi] = CLAIM_LIMITS.hold; if (!numIn(x, lo, hi)) throw new Error(`claim.hold must be from ${lo} to ${hi} seconds (got ${q(v)})`); return round(x, 2); } case "stamp": { const x = v === "true" ? true : v === "false" ? false : v; if (typeof x !== "boolean") throw new Error(`claim.stamp must be true or false (got ${q(v)})`); return x; } case "count": { const x = typeof v === "string" ? Number(v) : v; const [lo, hi] = CLAIM_LIMITS.count; if (!(Number.isInteger(x) && x >= lo && x <= hi)) throw new Error(`claim.count must be a whole number from ${lo} to ${hi} (got ${q(v)})`); return x; } case "label": { if (typeof v !== "string") throw new Error(`claim.label must be words (got ${q(v)})`); const s = v.trim(); if (!s) return null; if (/[\r\n]/.test(s)) throw new Error("claim.label must be one line"); if (s.length > CLAIM_LIMITS.label) throw new Error(`claim.label is ${s.length} characters (at most ${CLAIM_LIMITS.label})`); return s; } case "eventDate": { // The day the counted event happened, when not its clip's (a deck claim's window reads it). const d = typeof v === "string" ? v.trim() : v; if (!isIsoDate(d)) throw new Error(`claim.eventDate must be a date, YYYY-MM-DD (got ${q(v)})`); return d; } default: throw new Error(`claim.${key} is not a claim knob (${CLAIM_KNOBS.join(", ")})`); } } /** * A patch of claim knobs: `{ at?, hold?, stamp?, count?, label?, eventDate? }`, each a * value or null (clear). A knob set to its default is CLEARED -- `stamp: true` * and `count: 1` are what an absent key already says. * * @param {unknown} patch * @returns {Record} key -> value, or null to remove */ export function normalizeClaimKnobPatch(patch) { if (!isObj(patch)) throw new Error("the claim knobs are an object of at, hold, stamp, count, label, eventDate"); const keys = Object.keys(patch); if (!keys.length) throw new Error("nothing to change"); /** @type {Record} */ const out = {}; for (const k of keys) { if (!CLAIM_KNOBS.includes(k)) throw new Error(`claim.${k} is not a claim knob (${CLAIM_KNOBS.join(", ")})`); const v = claimKnobValue(k, /** @type {Record} */ (patch)[k]); out[k] = v !== null && k in CLAIM_KNOB_DEFAULTS && v === CLAIM_KNOB_DEFAULTS[/** @type {"stamp"|"count"} */ (k)] ? null : v; } return out; } /** The knobs of a claim as written (absent keys left out). */ export function claimKnobsOf(claim) { /** @type {Record} */ const out = {}; if (!isObj(claim)) return out; for (const k of CLAIM_KNOBS) if (claim[k] !== undefined) out[k] = claim[k]; return out; } /** * The entry that STAMPS a claim: the last in the timeline carrying its id * (per variant: an entry with `variant` set counts only in that cut, one with * none in every cut). Its knobs are the ones the build reads. * * @param {{ timeline?: unknown[] }} manifest * @param {string} claimId * @param {string | null} [variant] null: the last in the whole timeline */ export function stampingEntryId(manifest, claimId, variant = null) { let last = null; for (const e of Array.isArray(manifest?.timeline) ? manifest.timeline : []) { if (!isObj(e) || !rawMarksOf(e).some((m) => isObj(m) && m.id === claimId)) continue; if (variant && e.variant && e.variant !== variant) continue; last = e.id; } return last; } // --------------------------------------------------------------------------- // An entry's MARKS (plans/clip-marks.md): `claims: [mark, …]`, or a lone // `claim` for one. Every writer stores one mark as `claim` -- so a manifest // with one mark per clip is the bytes it always was -- and two or more as // `claims`. // --------------------------------------------------------------------------- export { CLAIM_MARKS_MAX, rawMarksOf }; /** An entry's marks as written, copied (a writer edits the copy, then setMarks). */ export const marksCopyOf = (entry) => rawMarksOf(entry).filter(isObj).map((m) => ({ ...m })); /** * Store `marks` on `entry`: none removes `claim` and `claims`; one is `claim`; * two or more are `claims`. Mutates `entry`. */ export function setMarks(entry, marks) { const list = Array.isArray(marks) ? marks.filter(isObj) : []; const key = list.length === 0 ? null : list.length === 1 ? "claim" : "claims"; const value = list.length === 1 ? list[0] : list; // In the place the marks had among the entry's keys (else last): a mark // added and removed again leaves the entry the bytes it was. const keys = Object.keys(entry); const at = keys.findIndex((k) => k === "claim" || k === "claims"); if (at === -1) { if (key) entry[key] = value; return entry; } const after = keys.slice(at + 1).filter((k) => k !== "claim" && k !== "claims").map((k) => [k, entry[k]]); for (const k of keys.slice(at)) delete entry[k]; if (key) entry[key] = value; for (const [k, v] of after) entry[k] = v; return entry; } /** Every mark id the manifest uses. */ export function markIdsOf(manifest) { const ids = new Set(); for (const e of Array.isArray(manifest?.timeline) ? manifest.timeline : []) { for (const m of rawMarksOf(e)) if (isObj(m) && typeof m.id === "string") ids.add(m.id); } return ids; } /** * A NEW mark id for a mark added to `entryId`: the entry's id when no mark * uses it, else `.2`, `.3`, … -- never an id another mark has (a * shared id is one event, counted once). */ export function freshMarkId(manifest, entryId) { const used = markIdsOf(manifest); const base = String(entryId ?? "").replace(/[^A-Za-z0-9_.:-]/g, "-").replace(/^[^A-Za-z0-9]+/, "").slice(0, 56) || "mark"; if (!used.has(base)) return base; for (let n = 2; ; n += 1) { const id = `${base}.${n}`; if (!used.has(id)) return id; } } /** * A verdict as typed: its name (`CORROBORATED`, any case) or its label in this * cut (`Episodes`, any case -- the fact-check's `verdicts..label` or * `stampLabel`). Throws a sentence naming the choices. * * @param {unknown} v * @param {Record} [render] */ export function verdictOfInput(v, render) { const s = typeof v === "string" ? v.trim() : ""; // The counter's series (report-to-video counter.mjs): the verdicts with // their relabels, or a `deck.counter`'s named series -- by id or by word. const counter = resolveCounter(render); const ids = counter.order; if (ids.includes(s)) return s; const upper = s.toUpperCase().replace(/[\s-]+/g, "_"); if (ids.includes(upper)) return upper; const byLower = ids.find((id) => id.toLowerCase() === s.toLowerCase()); if (byLower) return byLower; for (const V of ids) { const x = counter.byId[V] ?? {}; const words = [x.label, x.stampLabel].filter((w) => typeof w === "string"); if (words.some((w) => w.trim().toLowerCase() === s.toLowerCase())) return V; } const names = ids.map((V) => (counter.byId[V]?.label && counter.byId[V].label !== V ? `${V} (${counter.byId[V].label})` : V)); throw new Error(`${q(v)} is not a ${counter.custom ? "counter series" : "verdict"}: ${names.join(", ")}`); } /** * The verdicts a mark is picked from, for the clip editor: each with its tally * label, stamp word and colour, the cut's own first (`stock`: the ones the * fact-check relabels, or any mark already uses), then the rest. * * @returns {Array<{ verdict: string, label: string, stampLabel: string, color: string, stock: boolean }>} */ export function markVerdictsOf(manifest) { const render = manifest?.render; const counter = resolveCounter(render); // A counter's series are all the cut's own; the verdicts', the relabelled. const given = counter.custom ? counter.byId : isObj(render?.chrome?.factcheck?.verdicts) ? render.chrome.factcheck.verdicts : {}; const used = new Set(); for (const e of Array.isArray(manifest?.timeline) ? manifest.timeline : []) { for (const m of rawMarksOf(e)) if (isObj(m) && typeof m.verdict === "string") used.add(m.verdict); } const all = counter.order.map((V) => { const own = isObj(given[V]) && typeof given[V].stampLabel === "string" && given[V].stampLabel.trim() ? given[V].stampLabel.trim() : null; const x = counter.byId[V]; return { verdict: V, label: x.label, stampLabel: own ?? x.label, color: x.color, stock: V in given || used.has(V) }; }); return [...all.filter((x) => x.stock), ...all.filter((x) => !x.stock)]; } // --------------------------------------------------------------------------- // The fact-check's stamp knobs, for the editor's placeholders. // --------------------------------------------------------------------------- /** The stamp knobs `render.chrome.factcheck.stamp` carries beside show/seconds/position. */ export const STAMP_KNOBS = Object.freeze(["scale", "motion", "opacity"]); export const STAMP_KNOB_DEFAULTS = Object.freeze(Object.fromEntries(STAMP_KNOBS.map((k) => [k, FACTCHECK_DEFAULTS.stamp[k]]))); export const STAMP_KNOB_LIMITS = Object.freeze({ scale: FACTCHECK_LIMITS.scale, opacity: FACTCHECK_LIMITS.opacity }); // --------------------------------------------------------------------------- // The encode ladder (render.variants). // --------------------------------------------------------------------------- /** The ladder "Make variants" encodes when the manifest names none. */ export const VARIANTS_DEFAULT = DEFAULT_VARIANTS; /** A variant's name, as the encoder's rule: what a file may be served by. */ export const VARIANT_NAME_RE = /^[a-z0-9][a-z0-9-]{0,23}$/; /** `render.variants`' problems: the encoder's validateVariants; null (remove it) has none. */ export const variantsProblems = (v) => (v === null ? [] : validateVariants(v));