// `/reactions.json` (release 35 C1): the clip finder's analysis of // one recording — speakers and their roles, turns, laughs, moments, and the // proposals with the clip each became. Written whole by the analysis // (`umtool new --seed reactions`, `umtool clips analyze|propose`); the clip // editor reads it, and changes one thing only: a speaker's role, through // setSpeakerRole below (the operator's "This is the host"). // // The shape is plans/release-35/clip-finder.md's, plus a few optional keys // (FACTS.md, "reactions.json"). // // Plain ESM with a .d.mts beside it. import { readFile, rename, writeFile } from "node:fs/promises"; import path from "node:path"; import { REACTIONS_VERSION, ROLES } from "./clips.mjs"; import { tmpPathFor } from "../atomicFile.mjs"; export const REACTIONS_FILE = "reactions.json"; export { REACTIONS_VERSION }; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); export const reactionsFileOf = (dir) => path.join(/* turbopackIgnore: true */ dir, REACTIONS_FILE); /** `/reactions.json`, or null when there is none (or it is not one). */ export async function readReactions(dir) { try { const doc = JSON.parse(await readFile(reactionsFileOf(dir), "utf8")); return isObj(doc) && doc.version === REACTIONS_VERSION && Array.isArray(doc.speakers) && Array.isArray(doc.proposals) ? doc : null; } catch { return null; } } /** Written whole: tmp + rename, so a reader never sees half a file. */ export async function writeReactions(dir, doc) { const file = reactionsFileOf(dir); const tmp = tmpPathFor(file); await writeFile(tmp, `${JSON.stringify(doc, null, 1)}\n`); await rename(tmp, file); return file; } /** The roles the operator has set, as `{index: role}` — what a re-analysis keeps. */ export function operatorRoles(doc) { const out = {}; for (const s of doc?.speakers ?? []) if (isObj(s) && s.how === "operator" && ROLES.includes(s.role)) out[s.index] = s.role; return out; } const labelOf = (role, index) => (role === "host" ? "Host" : role === "subject" ? "Subject" : `Speaker ${index + 1}`); /** * The operator's word on one speaker: `role` "host" | "subject" | "other", * marked `how: "operator"` (a re-analysis keeps it); `null` hands the speaker * back to the analysis (`how: "auto"`, the role as it is until the next * proposal pass). Marking a host or a subject takes that role off every * other speaker that held it (they become "other", handed back to the * analysis, until the next pass) — one host, one subject; a cluster merged * into this one (`sameAs`) keeps it. Records `rolesChangedAt`, so a page can say * the proposals were made before the roles changed (and offer Re-propose). * * @param {string} dir the project * @param {number} index the speaker (diarization cluster) * @param {"host"|"subject"|"other"|null} role * @param {{ now?: () => Date }} [opts] * @returns {Promise} the document as written */ export async function setSpeakerRole(dir, index, role, { now = () => new Date() } = {}) { const doc = await readReactions(dir); if (!doc) throw new Error(`no ${REACTIONS_FILE} in ${dir}`); if (role !== null && !ROLES.includes(role)) throw new Error(`a role is one of ${ROLES.join(", ")} (or null), not ${JSON.stringify(role)}`); const s = doc.speakers.find((x) => isObj(x) && x.index === index); if (!s) throw new Error(`no speaker ${index} in ${REACTIONS_FILE}`); if (role === null) { s.how = "auto"; } else { if (role !== "other") { for (const o of doc.speakers) { if (o !== s && isObj(o) && o.role === role && o.sameAs !== index) { o.role = "other"; o.label = labelOf("other", o.index); o.how = "auto"; delete o.sameAs; } } } s.role = role; s.label = labelOf(role, index); s.how = "operator"; delete s.sameAs; } doc.rolesChangedAt = now().toISOString(); await writeReactions(dir, doc); return doc; } /** * A person's name for one speaker (release 36 lane C): what the speaker * captions head that voice's lines with. Trimmed, up to 40 characters; null * or empty gives back the default ("Host", "Subject", "Speaker N"), which the * captions never print. A role change (setSpeakerRole) resets it. * * @param {string} dir the project * @param {number} index the speaker (diarization cluster) * @param {string|null} label * @returns {Promise} the document as written */ export async function setSpeakerLabel(dir, index, label) { const doc = await readReactions(dir); if (!doc) throw new Error(`no ${REACTIONS_FILE} in ${dir}`); const s = doc.speakers.find((x) => isObj(x) && x.index === index); if (!s) throw new Error(`no speaker ${index} in ${REACTIONS_FILE}`); const text = label === null || label === undefined ? "" : String(label).replace(/\s+/g, " ").trim(); if (text.length > 40) throw new Error("a speaker's label is up to 40 characters"); s.label = text || labelOf(s.role, index); await writeReactions(dir, doc); return doc; }