// A manifest clip's `assist` key (release 35 C1): which clips the clip finder // proposed, what it proposed, and what the operator did to them since. // // "assist": { "by": "assistant"|"operator", "proposal": "p001", "score": 0.83, "why": "…", // "original": { "start", "end", "cutStart", "cutEnd" }, // "edits": [ { "at": ISO, "by": "operator", "what": "trim"|"split"|"merge"|"add"|"restore", // "from": {…}, "to": {…} } ] } // // `by` flips to "operator" on the first human edit, and from then on a // re-proposal never touches the clip. A clip the operator adds has // `by: "operator"` and no `proposal`. The renderer ignores the key; the // validator here (run by the build and by `umtool check`) refuses a malformed // one so a hand edit that breaks it is caught before a render. // // Also here: applyProposals, the one place a proposal becomes a timeline // entry — the seed and `umtool clips propose` both call it. // // Plain ESM with a .d.mts beside it. import { textIn } from "./clips.mjs"; import { seekPlatformOfUrl, seekRuleOf, seekValue } from "../platformSeek.mjs"; export const ASSIST_BY = Object.freeze(["assistant", "operator"]); export const ASSIST_EDITS = Object.freeze(["trim", "split", "merge", "add", "restore"]); /** The seconds either side of a proposed cut the clip's window (start/end) leaves for trimming. */ export const WINDOW_PAD_S = 1.5; /** A clip's quote: the cut's words, at most this many characters. */ export const QUOTE_MAX = 160; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const num = (v) => typeof v === "number" && Number.isFinite(v); const round2 = (n) => Math.round(n * 100) / 100; /** * What is wrong with every clip's `assist`, as sentences. Unknown keys are * left alone (the editor may add its own); the known ones must have their * types. * @param {any} manifest * @returns {string[]} */ export function validateAssist(manifest) { const errors = []; for (const e of manifest?.timeline ?? []) { if (!isObj(e) || e.assist === undefined) continue; const W = `timeline entry ${e.id ?? "?"}: assist`; const a = e.assist; if (e.type !== "clip") { errors.push(`${W} is only for clips`); continue; } if (!isObj(a)) { errors.push(`${W} must be an object ({ by, proposal, score, why, original, edits })`); continue; } if (!ASSIST_BY.includes(a.by)) errors.push(`${W}.by must be "assistant" or "operator", not ${JSON.stringify(a.by)}`); if (a.by === "assistant" && (typeof a.proposal !== "string" || !a.proposal)) errors.push(`${W}.proposal must name the proposal an assistant clip came from`); if (a.proposal !== undefined && a.proposal !== null && (typeof a.proposal !== "string" || !a.proposal)) errors.push(`${W}.proposal must be a proposal id`); if (a.score !== undefined && a.score !== null && !(num(a.score) && a.score >= 0 && a.score <= 1)) errors.push(`${W}.score must be a number from 0 to 1`); if (a.why !== undefined && a.why !== null && typeof a.why !== "string") errors.push(`${W}.why must be text`); if (a.original !== undefined && a.original !== null) { if (!isObj(a.original)) errors.push(`${W}.original must be { start, end, cutStart, cutEnd }`); else { for (const k of ["start", "end", "cutStart", "cutEnd"]) { if (!(num(a.original[k]) && a.original[k] >= 0)) errors.push(`${W}.original.${k} must be a number of seconds ≥ 0`); } if (num(a.original.cutStart) && num(a.original.cutEnd) && a.original.cutEnd <= a.original.cutStart) errors.push(`${W}.original: cutEnd must be after cutStart`); } } if (a.edits !== undefined && a.edits !== null) { if (!Array.isArray(a.edits)) errors.push(`${W}.edits must be a list`); else { a.edits.forEach((ed, i) => { const EW = `${W}.edits[${i}]`; if (!isObj(ed)) { errors.push(`${EW} must be { at, by, what, from, to }`); return; } if (typeof ed.at !== "string" || Number.isNaN(Date.parse(ed.at))) errors.push(`${EW}.at must be an ISO time`); if (!ASSIST_BY.includes(ed.by)) errors.push(`${EW}.by must be "assistant" or "operator"`); if (!ASSIST_EDITS.includes(ed.what)) errors.push(`${EW}.what must be one of ${ASSIST_EDITS.join(", ")}, not ${JSON.stringify(ed.what)}`); for (const k of ["from", "to"]) if (ed[k] !== undefined && ed[k] !== null && !isObj(ed[k])) errors.push(`${EW}.${k} must be an object`); }); } } } return errors; } /** An assistant's clip that no person has touched: the only kind a re-proposal may move or remove. */ export const isAssistantClip = (e) => isObj(e) && e.type === "clip" && isObj(e.assist) && e.assist.by === "assistant"; /** The span a clip's cut covers (cutStart–cutEnd, else start–end). */ export const cutOf = (e) => (num(e?.cutStart) && num(e?.cutEnd) ? { start: e.cutStart, end: e.cutEnd } : { start: e.start, end: e.end }); /** * A platform's watch page at a second (release 36: a clip of a channel that is * on no site links its source there): YouTube `t=s`, Twitch `t=hms`, * Rumble `start=`, anything else `t=` — added with `&` or `?`, an * existing one of the same name replaced. Null for a URL that is not one. * The rule per platform, and how sure it is (Kick's is NOT confirmed), is * ../platformSeek.mjs; this writer writes every platform's parameter, sure or not. * @param {string | null | undefined} url the recording's `webpage_url` * @param {number} seconds * @returns {string | null} */ export function watchUrlAt(url, seconds) { if (typeof url !== "string" || !/^https?:\/\//i.test(url)) return null; try { new URL(url); } catch { return null; } const rule = seekRuleOf(seekPlatformOfUrl(url)); const key = rule.key; const value = seekValue(rule, seconds); const hash = url.indexOf("#"); const base = hash >= 0 ? url.slice(0, hash) : url; const frag = hash >= 0 ? url.slice(hash) : ""; // A targeted edit (no URLSearchParams round trip, which re-escapes every other parameter). const stripped = base.replace(new RegExp(`([?&])${key}=[^&]*(&|$)`), (_m, a, b) => (b ? a : "")).replace(/[?&]$/, ""); return `${stripped}${stripped.includes("?") ? "&" : "?"}${key}=${value}${frag}`; } /** * A proposal as a clip entry (or the fields that change on one already in * the cut): the cut is the proposal; the window leaves WINDOW_PAD_S either * side for trimming, inside the recording. `citeUrlAt` (release 36: a * site-less channel) gives the clip a `citeUrl` at its cite. * @param {{ id: string, start: number, end: number, score: number, why: string }} p * @param {{ id: string, channel: string, video: string, duration?: number, words?: { w: string, start: number, end: number }[], citeUrlAt?: ((s: number) => string | null) | null }} o */ export function clipFromProposal(p, { id, channel, video, duration = Infinity, words = [], citeUrlAt = null }) { const cutStart = round2(p.start); const cutEnd = round2(p.end); const start = round2(Math.max(0, cutStart - WINDOW_PAD_S)); const end = round2(Math.min(Number.isFinite(duration) && duration > 0 ? duration : Infinity, cutEnd + WINDOW_PAD_S)); const link = citeUrlAt ? citeUrlAt(Math.floor(cutStart)) : null; return { type: "clip", id, channel, video, start, end: Math.max(end, cutEnd), cutStart, cutEnd, cite: Math.floor(cutStart), quote: textIn(words, cutStart, cutEnd, QUOTE_MAX), assist: { by: "assistant", proposal: p.id, score: p.score, why: p.why, original: { start, end: Math.max(end, cutEnd), cutStart, cutEnd }, edits: [], }, ...(link ? { citeUrl: link } : {}), }; } /** The next free `r` id in a timeline. */ function nextClipId(used) { let n = 0; for (const id of used) { const m = /^r(\d+)$/.exec(id); if (m) n = Math.max(n, Number(m[1])); } let id; do { n += 1; id = `r${String(n).padStart(3, "0")}`; } while (used.has(id)); used.add(id); return id; } /** * The clips of the source recording a re-proposal may not move: every clip of * it that is not an untouched assistant clip — the operator's, a hand-made * one — and an assistant clip the operator has ruled on or locked. * @param {any[]} timeline * @param {{ channel: string, video: string, isProtected?: (e: any) => boolean }} o */ export function fixedClips(timeline, { channel, video, isProtected = () => false }) { return (timeline ?? []) .filter((e) => isObj(e) && e.type === "clip" && e.video === video && (e.channel ?? channel) === channel) .filter((e) => !isAssistantClip(e) || isProtected(e) || e.lock || e.lockStart || e.lockEnd || e.lockCut) .map((e) => ({ id: String(e.id), ...cutOf(e) })); } /** * The timeline after a proposal pass. Untouched assistant clips (isAssistantClip, * not protected) are the assistant's to move: one whose proposal is still used * is updated in place (same id); one whose proposal is not used is removed. * Every used proposal with no clip yet becomes one (`r`). Everything * else in the timeline stays as it is. The recording's clips are kept in * time order in the slots they occupy; other entries do not move. * * Returns the timeline, the proposals with their `clipId` (null when unused), * and what changed. * @param {any[]} timeline * With `citeUrlAt` (release 36: the channel is on no site), every clip the * pass adds or moves carries `citeUrl` = the platform's page at its cite; a * moved clip's link is re-pointed only when it is that derived link (or it has * none) — a link somebody wrote stays. * @param {{ proposals: { id: string, start: number, end: number, score: number, why: string, used: boolean, fixedClip?: string }[], * channel: string, video: string, duration?: number, words?: { w: string, start: number, end: number }[], isProtected?: (e: any) => boolean, * citeUrlAt?: ((s: number) => string | null) | null }} o */ export function applyProposals(timeline, { proposals, channel, video, duration = Infinity, words = [], isProtected = () => false, citeUrlAt = null }) { const entries = (timeline ?? []).map((e) => (isObj(e) ? { ...e } : e)); const movable = (e) => isAssistantClip(e) && e.video === video && (e.channel ?? channel) === channel && !isProtected(e) && !(e.lock || e.lockStart || e.lockEnd || e.lockCut); const byProposal = new Map(); for (const e of entries) if (movable(e)) byProposal.set(e.assist.proposal, e); const used = new Set(entries.filter(isObj).map((e) => String(e.id))); const usedIds = new Set(proposals.filter((p) => p.used && !p.fixedClip).map((p) => p.id)); const changes = { added: [], updated: [], removed: [], kept: 0 }; const out = []; for (const e of entries) { if (movable(e) && !usedIds.has(e.assist.proposal)) { changes.removed.push(e.id); continue; } out.push(e); } const withClips = proposals.map((p) => { if (!p.used) return { ...p, clipId: null }; if (p.fixedClip) return { ...p, clipId: p.fixedClip }; const have = byProposal.get(p.id); if (have) { const fresh = clipFromProposal(p, { id: have.id, channel: have.channel ?? channel, video, duration, words, citeUrlAt }); const same = have.cutStart === fresh.cutStart && have.cutEnd === fresh.cutEnd && have.assist.score === fresh.assist.score && have.assist.why === fresh.assist.why; // The derived link follows the cite; one written by somebody else stays. const relink = !!fresh.citeUrl && have.citeUrl !== fresh.citeUrl && (!have.citeUrl || have.citeUrl === citeUrlAt?.(have.cite ?? Math.floor(have.cutStart ?? have.start))); if (same && !relink) changes.kept += 1; else { Object.assign(have, { start: fresh.start, end: fresh.end, cutStart: fresh.cutStart, cutEnd: fresh.cutEnd, cite: fresh.cite, quote: fresh.quote }); if (relink) have.citeUrl = fresh.citeUrl; have.assist = { ...have.assist, score: fresh.assist.score, why: fresh.assist.why, original: fresh.assist.original }; changes.updated.push(have.id); } return { ...p, clipId: have.id }; } const clip = clipFromProposal(p, { id: nextClipId(used), channel, video, duration, words, citeUrlAt }); out.push(clip); changes.added.push(clip.id); return { ...p, clipId: clip.id }; }); // The recording's clips in time order, in the slots they hold. const isOurs = (e) => isObj(e) && e.type === "clip" && e.video === video && (e.channel ?? channel) === channel; const slots = []; const ours = []; out.forEach((e, i) => { if (isOurs(e)) { slots.push(i); ours.push(e); } }); ours.sort((a, b) => cutOf(a).start - cutOf(b).start || String(a.id).localeCompare(String(b.id))); slots.forEach((slot, k) => { out[slot] = ours[k]; }); return { timeline: out, proposals: withClips, changes }; }