// VIDEO OPS — a revision of a report video's cut (`video.manifest.json`), as // checked edit operations a model asks for and code applies (the stopgap's // revise.py, release 27 S2). // // drop {id} remove an entry // move {id, after} reorder: after another entry, or "start" // heading {id, text} a card's on-screen heading // narration {id, text} a card's spoken narration ("" removes it) — through grounding // trim {id, start, end} a clip's in/out, within the fetched window ±3 s // add_clip {citation, after?} a clip for a video citation of the report not in the cut // title {text} / subtitle {text} // look {key, value} a look setting (release 38: the video assistant's ops, // slide {key, value} ../videoAssistOps.mjs — checked by the look's own rules, // room {key, value} common lib/report/look.mjs, as umtool's updateRender is) // // The rules, in code: // - A narration passes ./grounding.ts's hard checks, or the op is refused // with them; its soft warnings come back with the result. // - A trim stays within the window already fetched: the PARENT cut's clip // (the one the revision started from) ±TRIM_SLACK_SECONDS. A clip added in // this revision is bounded by its own citation's span. // - add_clip only takes the report's own VIDEO citations, once each. // - Every refusal carries its reason, and refuses that op only. Ops name // entries by id, so a batch is not renumbered by its own drops. // // Pure: the manifest in, a new one out. import { checkNarration, groundingInputOf, type GroundingInput } from "./grounding"; import { checkAssistOps } from "../videoAssistOps.mjs"; export const VIDEO_OP_NAMES = ["drop", "move", "heading", "narration", "trim", "add_clip", "title", "subtitle", "look", "slide", "room"] as const; export const TRIM_SLACK_SECONDS = 3; export type ManifestEntry = { type: string; id: string; style?: string; heading?: string; narration?: { text: string } & Record; channel?: string; video?: string; start?: number; end?: number; cite?: number; quote?: string; } & Record; export type VideoManifest = { title?: string; subtitle?: string; timeline: ManifestEntry[] } & Record; type CitationLike = { kind: string; channel?: string; id?: string; start?: number; end?: number; quote?: string }; export type ReportForVideo = Parameters[0] & { citations?: Record | null; }; export type VideoOp = { op: string } & Record; export type AppliedVideoOp = { index: number; op: VideoOp; warnings?: string[] }; export type RefusedVideoOp = { index: number; op: VideoOp; why: string }; export type VideoOpsContext = { // The report the cut is made from: its citations, and the text grounding // checks a narration against. report: ReportForVideo; // The cut the revision started from (its clips' windows bound a trim); // default: the manifest passed in. parent?: VideoManifest; // Grounding's input; default: from the report. grounding?: GroundingInput; // The operator's words: a look op may name more seconds than a step allows. feedback?: string; }; export type VideoOpsResult = { manifest: VideoManifest; applied: AppliedVideoOp[]; refused: RefusedVideoOp[] }; class Refusal extends Error {} const refuse = (why: string): never => { throw new Refusal(why); }; const str = (v: unknown, what: string): string => typeof v === "string" ? v : refuse(`${what} must be text`); export function applyVideoOps(input: VideoManifest, ops: readonly unknown[], ctx: VideoOpsContext): VideoOpsResult { const manifest = structuredClone(input); if (!Array.isArray(manifest.timeline)) manifest.timeline = []; const parent = ctx.parent ?? input; const parentClips = new Map( (parent.timeline ?? []).filter((e) => e.type === "clip").map((e) => [e.id, e] as const), ); const grounding = ctx.grounding ?? groundingInputOf(ctx.report); const citations = ctx.report.citations ?? {}; const applied: AppliedVideoOp[] = []; const refused: RefusedVideoOp[] = []; for (const [index, raw] of ops.entries()) { const op = (raw && typeof raw === "object" ? raw : { op: String(raw) }) as VideoOp; const tl = manifest.timeline; const entry = (v: unknown): ManifestEntry => { const id = str(v, "id"); return tl.find((e) => e.id === id) ?? refuse(`no entry "${id}" in the cut`); }; const card = (v: unknown): ManifestEntry => { const e = entry(v); return e.type === "card" ? e : refuse(`"${e.id}" is a ${e.type}, not a card`); }; let warnings: string[] | undefined; try { switch (op.op) { case "drop": tl.splice(tl.indexOf(entry(op.id)), 1); break; case "move": { const e = entry(op.id); const after = str(op.after, "after"); if (after === e.id) refuse("an entry cannot move after itself"); const anchor = after === "start" ? null : entry(after); tl.splice(tl.indexOf(e), 1); tl.splice(anchor ? tl.indexOf(anchor) + 1 : 0, 0, e); break; } case "heading": { const c = card(op.id); const t = str(op.text, "text").trim(); if (!t) refuse("a heading needs its text"); c.heading = t; break; } case "narration": { const c = card(op.id); const t = str(op.text, "text").replace(/\s+/g, " ").trim(); if (!t) { delete c.narration; break; } const g = checkNarration(t, grounding); if (g.hard.length) refuse(`narration refused: ${g.hard.join("; ")}`); c.narration = { text: t }; if (g.soft.length) warnings = g.soft; break; } case "trim": { const e = entry(op.id); if (e.type !== "clip") refuse(`"${e.id}" is a ${e.type}, not a clip`); const s = Number(op.start); const en = Number(op.end); if (!Number.isFinite(s) || !Number.isFinite(en)) refuse("trim needs start and end in seconds"); if (!(s < en)) refuse("a trim's start is before its end"); const bound = parentClips.get(e.id) ?? e; const lo = (bound.start ?? 0) - TRIM_SLACK_SECONDS; const hi = (bound.end ?? 0) + TRIM_SLACK_SECONDS; if (s < lo || en > hi) refuse(`outside the fetched window (${lo.toFixed(2)}–${hi.toFixed(2)} s)`); e.start = s; e.end = en; break; } case "add_clip": { const cid = str(op.citation, "citation"); const c = citations[cid] ?? refuse(`no citation "${cid}" in the report`); if (c.kind !== "video") refuse(`only a video citation becomes a clip ("${cid}" is a ${c.kind})`); if (tl.some((x) => x.id === cid)) refuse(`"${cid}" is already in the cut`); if (typeof c.start !== "number" || typeof c.end !== "number" || !c.channel || !c.id) { refuse(`citation "${cid}" has no span`); } const e: ManifestEntry = { type: "clip", id: cid, channel: c.channel!, video: c.id!, start: c.start!, end: c.end!, cite: Math.floor(c.start!), quote: c.quote ?? "", }; const after = op.after === undefined || op.after === null || op.after === "" ? null : str(op.after, "after"); const anchor = after === null || after === "end" ? null : after === "start" ? "start" : entry(after); tl.splice(anchor === null ? tl.length : anchor === "start" ? 0 : tl.indexOf(anchor) + 1, 0, e); // A clip added here is bounded by its own citation. parentClips.set(cid, e); break; } case "look": case "slide": case "room": { // The video assistant's look ops (release 38): the look's own rules decide. const r = checkAssistOps(manifest, [op], { request: ctx.feedback ?? "", scope: "look" }); if (r.refused.length) refuse(r.refused[0]!.why); manifest.render = r.manifest.render; if (r.ops[0]?.warning) warnings = [r.ops[0].warning]; break; } case "title": case "subtitle": { const t = str(op.text, "text").trim(); if (op.op === "title" && !t) refuse("a title needs its text"); manifest[op.op] = t; break; } default: refuse(`no op "${String(op.op)}" — the ops are ${VIDEO_OP_NAMES.join(", ")}`); } applied.push({ index, op, ...(warnings ? { warnings } : {}) }); } catch (e) { if (!(e instanceof Refusal)) throw e; refused.push({ index, op, why: e.message }); } } return { manifest, applied, refused }; } // The cut as a model reads it. export function cutView(m: VideoManifest): string { const lines = [`TITLE: ${m.title ?? ""}`, `SUBTITLE: ${m.subtitle ?? ""}`]; for (const e of m.timeline ?? []) { if (e.type === "card") { const n = e.narration?.text ?? ""; lines.push( `- card ${e.id} (${e.style ?? ""}) heading: ${JSON.stringify(e.heading ?? "")}` + (n ? ` narration: ${JSON.stringify(n)}` : ""), ); } else if (e.type === "clip") { lines.push(`- clip ${e.id} ${e.channel}/${e.video} ${e.start}–${e.end} s quote: ${JSON.stringify(e.quote ?? "")}`); } else { lines.push(`- ${e.type} ${e.id}`); } } return lines.join("\n"); } // ─── The trimmed view (release 28 Q3: a local model's one ops batch) ─── // // The cut in timeline order under a character budget: the chapters the // feedback names (a card's heading or an entry's id, or "chapter N") are shown // in full first, then the rest in order while the budget lasts; an entry left // out is listed by its id alone, so an op may still name it. Quotes are // shortened IN THE VIEW ONLY. export const CUT_VIEW_QUOTE_WORDS = 12; const shortQ = (q: string, words: number) => { const w = q.split(/\s+/).filter(Boolean); return w.length > words ? `${w.slice(0, Math.max(4, words - 4)).join(" ")} …` : q; }; const lowWords = (s: string) => ` ${s .toLowerCase() .replace(/[^\p{L}\p{N}\s-]/gu, " ") .replace(/\s+/g, " ") .trim()} `; export function entryLine(e: ManifestEntry, quoteWords = CUT_VIEW_QUOTE_WORDS): string { if (e.type === "card") { const n = e.narration?.text ?? ""; return `- card ${e.id} (${e.style ?? ""}) heading: ${JSON.stringify(e.heading ?? "")}` + (n ? ` narration: ${JSON.stringify(n)}` : ""); } if (e.type === "clip") return `- clip ${e.id} ${e.channel}/${e.video} ${e.start}–${e.end} s quote: ${JSON.stringify(shortQ(e.quote ?? "", quoteWords))}`; return `- ${e.type} ${e.id}`; } // The chapters of a cut: each card starts one; entries before the first card // are chapter 0's. export function chaptersOf(m: VideoManifest): ManifestEntry[][] { const out: ManifestEntry[][] = [[]]; for (const e of m.timeline ?? []) { if (e.type === "card" && out.at(-1)!.length) out.push([]); out.at(-1)!.push(e); } return out.filter((c) => c.length); } export function chaptersNamedBy(m: VideoManifest, feedback: string): Set { const fb = lowWords(feedback); const out = new Set(); chaptersOf(m).forEach((ch, i) => { for (const e of ch) { if (fb.includes(lowWords(e.id))) out.add(i); const h = lowWords(e.heading ?? "").trim(); if (e.type === "card" && h.length >= 3 && fb.includes(` ${h} `)) out.add(i); } if (new RegExp(`\\bchapter\\s*${i + 1}\\b`, "i").test(feedback)) out.add(i); }); return out; } export function trimmedCutView( m: VideoManifest, o: { maxChars: number; feedback?: string; quoteWords?: number }, ): { text: string; full: string[]; short: string[] } { const head = [`TITLE: ${m.title ?? ""}`, `SUBTITLE: ${m.subtitle ?? ""}`]; const chapters = chaptersOf(m); const named = o.feedback ? chaptersNamedBy(m, o.feedback) : new Set(); const order = [...chapters.keys()].sort((a, b) => Number(named.has(b)) - Number(named.has(a)) || a - b); // Every entry costs at least its id line; the full line is bought from what is left. const idLine = (e: ManifestEntry) => `- ${e.type} ${e.id} (not shown in full)`; let used = head.join("\n").length + (m.timeline ?? []).reduce((n, e) => n + idLine(e).length + 1, 0); const full = new Set(); for (const i of order) { for (const e of chapters[i]!) { const extra = entryLine(e, o.quoteWords).length - idLine(e).length; if (used + extra > o.maxChars) continue; used += extra; full.add(e); } } const lines = [...head]; for (const e of m.timeline ?? []) lines.push(full.has(e) ? entryLine(e, o.quoteWords) : idLine(e)); return { text: lines.join("\n"), full: [...full].map((e) => e.id), short: (m.timeline ?? []).filter((e) => !full.has(e)).map((e) => e.id), }; }