import { readFile } from "node:fs/promises"; import path from "node:path"; import { channelFor, readManifest } from "@/lib/projects/report.mjs"; import { listTakes, readVerdicts } from "@/lib/report/takes.mjs"; import { latestWorkingTake } from "@/lib/report/workingCut.mjs"; import { clipMomentKey } from "umtool-report-to-video/montage-rulings"; import { earlierRulingsOffer, montageClipsOf, readRulingsState } from "@/lib/report/montageRulings.mjs"; import type { CitedTarget } from "@/lib/clips/types"; // --------------------------------------------------------------------------- // THE CLIP DECK's page data (release 34 N1): one card per montage instance -- // a clip carrying a `montage` object -- with what the deck needs to rule it by // ear: the cut and the clip's whole extent to play (the project's cached // windows first, through the cited clip), the quote with its phrase, the date // and source, the clip's note (the seed's "voice check", a stand-in upload), // and, when release 34 N2's voice-match.json is beside the manifest, its score. // Nothing here calls a model; voice-match.json is read if it is there. // --------------------------------------------------------------------------- export type ClipRuling = { verdict: "keep" | "cut" | "not-him" | "read-aloud"; reason: string; match?: string; at: string }; export type VoiceMatch = { score: number; label: "likely" | "unsure" | "unlikely"; speechSeconds: number | null }; export type ClipCard = { id: string; key: string; /** The counter as the manifest has it (the render numbers the kept ones again). */ n: number | null; of: number | null; /** The phrase the cut is cut to: the manifest's `montage.match`, else its on-screen title. */ phrase: string; quote: string | null; /** "2 May 2019". */ date: string | null; year: number | null; platform: string | null; channel: string; video: string; note: string; /** The seed's voice check (a co-host show), or a note that says so. */ voiceCheck: boolean; voice: VoiceMatch | null; cut: CitedTarget | null; extent: CitedTarget | null; }; export type RerenderTarget = { take: string; label: string; builtAt: string | null; why: "latest" | "picked" | "whole cut" }; export type ClipDeckView = { cards: ClipCard[]; rulings: Record; rulingsError: string | null; voiceMatch: { model: string | null; count: number } | null; offer: { count: number; left: number; matched: number; unmapped: number }; /** The take "Re-render the take" makes again, or null (the Takes page chooses). */ rerender: RerenderTarget | null; takes: number; }; const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const niceDate = (d: unknown) => { const s = String(d ?? ""); if (!/^\d{8}$/.test(s)) return null; return `${Number(s.slice(6, 8))} ${MONTHS[Number(s.slice(4, 6)) - 1] ?? "?"} ${s.slice(0, 4)}`; }; const num = (v: unknown) => (typeof v === "number" && Number.isFinite(v) ? v : null); /** release 34 N2's `/voice-match.json`, read leniently: a row is used only when it is the shape. */ export async function readVoiceMatch(dir: string): Promise<{ model: string | null; clips: Map } | null> { let raw: unknown; try { raw = JSON.parse(await readFile(path.join(/* turbopackIgnore: true */ dir, "voice-match.json"), "utf8")); } catch { return null; } if (!raw || typeof raw !== "object") return null; const r = raw as { model?: unknown; clips?: Record }; const clips = new Map(); for (const [k, v] of Object.entries(r.clips && typeof r.clips === "object" ? r.clips : {})) { const row = v as { score?: unknown; label?: unknown; speechSeconds?: unknown }; const score = num(row?.score); if (score === null || !["likely", "unsure", "unlikely"].includes(String(row?.label))) continue; clips.set(k, { score, label: row.label as VoiceMatch["label"], speechSeconds: num(row.speechSeconds) }); } return { model: typeof r.model === "string" ? r.model : null, clips }; } /** How doubtful a card is: 0 voice unlikely, 1 voice unsure, 2 a voice check in its note, 3 nothing says so. */ export const doubtOf = (c: Pick) => c.voice?.label === "unlikely" ? 0 : c.voice?.label === "unsure" ? 1 : c.voiceCheck ? 2 : 3; type Entry = Record & { id: string; video?: string; start?: number; end?: number; cutStart?: number; cutEnd?: number; quote?: string; note?: string; onscreen?: { title?: string }; montage?: { n?: number; of?: number; match?: string; date?: string; platform?: string; speakerCheck?: boolean }; }; export async function clipDeckView(project: { id: string; dir: string }): Promise { const manifest = await readManifest(project.dir); if (!manifest) return null; const [state, voice, listing, verdicts] = await Promise.all([ readRulingsState(project.dir), readVoiceMatch(project.dir), listTakes(project.dir), readVerdicts(project.dir), ]); const offer = await earlierRulingsOffer(project.dir, manifest, state.doc); const cards: ClipCard[] = []; for (const e of montageClipsOf(manifest) as Entry[]) { const key = clipMomentKey(e, manifest) as string | null; const channel = channelFor(manifest, e) as string | null; if (!key || !channel || !e.video) continue; const m = e.montage ?? {}; const span = (from: unknown, to: unknown): CitedTarget | null => { const f = num(from); const t = num(to); return f !== null && t !== null && f < t ? { channel, video: e.video as string, from: f, to: t, project: project.id, clip: e.id } : null; }; const note = typeof e.note === "string" ? e.note.replace(/^[;\s]+/, "").trim() : ""; cards.push({ id: e.id, key, n: num(m.n), of: num(m.of), phrase: (typeof m.match === "string" && m.match) || e.onscreen?.title || "", quote: typeof e.quote === "string" ? e.quote : null, date: niceDate(m.date), year: /^\d{8}$/.test(String(m.date ?? "")) ? Number(String(m.date).slice(0, 4)) : null, platform: typeof m.platform === "string" ? m.platform : null, channel, video: e.video, note, voiceCheck: m.speakerCheck === true || /voice check/i.test(note), voice: voice?.clips.get(key) ?? null, cut: span(e.cutStart, e.cutEnd) ?? span(e.start, e.end), extent: span(e.start, e.end), }); } // The take "Re-render the take" makes again: the LATEST whole-cut take of the // manifest (operator, 1 Nov 2026 -- the working cut's name, workingCut.mjs), // else the one picked, else the one whole-cut take. It used to be the pick // first, and with two whole-cut takes and none picked there was no target at // all: "Choose a take to render", which only a pick unblocked. type T = { id: string; label: string; builtAt: string | null; window?: unknown; baseHash?: string | null; order: number }; const made = (listing.takes as T[]).filter((t) => t.baseHash); const latest = latestWorkingTake(listing.takes as Parameters[0]) as T | null; const picked = made.filter((t) => (verdicts as Record)[t.id]?.picked); const whole = made.filter((t) => t.window === "full"); const target = latest ? { t: latest, why: "latest" as const } : picked.length === 1 ? { t: picked[0], why: "picked" as const } : whole.length >= 1 ? { t: [...whole].sort((a, b) => String(b.builtAt ?? "").localeCompare(String(a.builtAt ?? "")))[0], why: "whole cut" as const } : null; const left = offer.rulings.filter((r: { ruling: { verdict: string } }) => r.ruling.verdict !== "keep").length; return { cards, rulings: state.doc.rulings as Record, rulingsError: state.error, voiceMatch: voice ? { model: voice.model, count: voice.clips.size } : null, offer: { count: offer.rulings.length, left, matched: offer.rulings.length - left, unmapped: offer.unmapped.length }, rerender: target ? { take: target.t.id, label: target.t.label, builtAt: target.t.builtAt, why: target.why } : null, takes: listing.takes.length, }; }