// THE TUNE's NUMBERS ON THE BENCH (lane character-tuning, review 2; components/projects/read/ReadAgain.tsx): where a // line's Tune starts, what a press asks as the line's OWN, and what a "Keep for " keeps -- pure, so the // rule is one and tested. // // the BASELINE what a plain press reads the piece with now: its character's tuning (none: the defaults), unless // its last read was the line's own (`tuningAsked` true; made before that was recorded: a sampler value // off the defaults) -- then those numbers, and they stay the line's own // a PRESS with no character, S3's rule (what differs from the defaults); with one, what differs from the // character's tuning (all of the sampler when the last read was the line's own) -- nothing changed on // a character's line asks none of the sampler, so the character's is asked as it is then; the takes // only when the LINE owns them (lane reads-find, the orchestrator's ruling, 17 Nov 2026: superseding S3's // "a recorded takes is passed back") -- the last read's own takes, or a takes typed -- else they are // resolved now (the character's, else the editor's setting), never copied from the last read // a KEEP the sampler as it stands (top k, min p only when not their defaults) and the takes only when typed // // Plain ESM, pure. import { ORPHEUS_TUNING_DEFAULTS, TTS_TUNING_FIELDS } from "yt-dlp-transcript-common/lib/ttsTuning.mjs"; export const TUNE_SAMPLER = Object.freeze(["temperature", "top_p", "top_k", "min_p", "repeat_penalty"]); const DEFAULTS = ORPHEUS_TUNING_DEFAULTS; const num = (v) => (v !== "" && v !== null && v !== undefined && Number.isFinite(Number(v)) ? Number(v) : null); /** Was a read the line's own? `tuningAsked` true -- or, made before it was recorded, a sampler value off the defaults. */ export function madeOwnOf(tuning, tuningAsked) { if (tuningAsked === true) return true; if (tuningAsked !== undefined && tuningAsked !== null) return false; return TUNE_SAMPLER.some((k) => typeof tuning?.[k] === "number" && tuning[k] !== DEFAULTS[k]); } /** * THE VALUES A READ's LINE OWNED (lane reads-find): its `tuningOwn` (the line's own values as asked, recorded on every * take made since) when it has one; an OLDER take records no ownership field by field, so it reads as owning every * recorded value when `tuningAsked` is true, and none otherwise -- a known limit: an old take that owned only its takes * (recorded "preset") re-reads with the character's or the settings' takes. Null: the line owns none. * @param {{ tuning?: Record | null, tuningAsked?: unknown, tuningOwn?: Record | null }} [made] */ export function lineOwnOf(made) { const own = made?.tuningOwn; if (own && typeof own === "object" && !Array.isArray(own)) return Object.keys(own).length ? { ...own } : null; if (made?.tuningAsked === true && made?.tuning && typeof made.tuning === "object") return Object.keys(made.tuning).length ? { ...made.tuning } : null; return null; } /** * The values one PIECE of a thread read owned (`role`): its `read.tuned[role]` (the read's own values for that piece, * postThreadRead.mjs readRecordOf) when the read records the piece there, else lineOwnOf of its part. Null: none. * @param {any} take * @param {string} role */ export function pieceOwnOf(take, role) { const t = take?.read?.tuned; if (t && typeof t === "object" && t[role] && typeof t[role] === "object") { const own = t[role].tuning; return own && typeof own === "object" && Object.keys(own).length ? { ...own } : null; } const part = (Array.isArray(take?.parts) ? take.parts : []).find((p) => p?.role === role && typeof p?.take !== "string") ?? null; return lineOwnOf(part); } /** S3's ask (ReadAgain.tsx tuningAsked): the sampler values off the defaults, and the takes whenever given. */ export function defaultsAskOf(draft) { const out = {}; for (const k of TTS_TUNING_FIELDS) { const v = num(draft?.[k]); if (v !== null && (k === "takes" || v !== DEFAULTS[k])) out[k] = v; } return Object.keys(out).length ? out : null; } /** * The Tune's starting numbers, as its fields hold them (strings): the baseline's sampler (`baseline` null: S3's -- the * last read's); the takes the LINE owns (`own`: lineOwnOf's, null none -- or the baseline's `ownTakes`), else the * character's, else empty (the editor's setting): a takes the line does not own is resolved now, never the last read's. * @param {{ madeWith?: Record | null, baseline?: { character: { tuning: Record | null } | null, madeOwn: boolean, ownTakes?: number | null } | null, own?: Record | null }} o */ export function tuneStartOf({ madeWith = null, baseline = null, own = null } = {}) { // No character (none now, or one since removed): S3's rule -- the last read's numbers (review 3 #4). const b = !baseline || baseline.madeOwn || !baseline.character ? madeWith : baseline.character.tuning ?? null; const takes = ownTakesOf(own, baseline) ?? baseline?.character?.tuning?.takes ?? null; return Object.fromEntries(TTS_TUNING_FIELDS.map((k) => { const v = k === "takes" ? takes : b?.[k]; return [k, v !== undefined && v !== null ? String(v) : k in DEFAULTS ? String(DEFAULTS[k]) : ""]; })); } /** The takes the line owns: `own`'s, else the baseline's `ownTakes`; null when it owns none. */ const ownTakesOf = (own, baseline) => { const t = own?.takes ?? baseline?.ownTakes; return typeof t === "number" && Number.isFinite(t) ? t : null; }; /** * WHAT A PLAIN PRESS ASKS (lane reads-find: the bench's untouched Read again, and the re-read queue's -- one rule): * the Tune's start, asked as an untouched press asks it. `own` the values the line owned (lineOwnOf). * @param {{ madeWith?: Record | null, baseline?: { character: { tuning: Record | null } | null, madeOwn: boolean, ownTakes?: number | null } | null, own?: Record | null }} o */ export function plainPressAskOf({ madeWith = null, baseline = null, own = null } = {}) { const b = baseline ? { ...baseline, ownTakes: ownTakesOf(own, baseline) } : baseline; const draft = tuneStartOf({ madeWith, baseline: b, own }); return b?.character ? tuneAskOf(draft, b) : defaultsAskOf(draft); } /** * What a press asks as the line's own (null: nothing of its own -- the character's, or the defaults, as they are now). * @param {Record} draft * @param {{ character: { tuning: Record | null } | null, madeOwn: boolean } | null} [baseline] */ export function tuneAskOf(draft, baseline = null) { if (!baseline?.character) return defaultsAskOf(draft); const out = {}; for (const k of TUNE_SAMPLER) { const v = num(draft?.[k]); const was = baseline.character.tuning?.[k] ?? DEFAULTS[k]; if (v !== null && (baseline.madeOwn || v !== was)) out[k] = v; } // The takes: the line's own (its last read's, `ownTakes`; or typed other than the character's) -- never the // character's own takes sent back as the line's (they are resolved now; lane reads-find). const takes = num(draft?.takes); const charTakes = baseline.character.tuning?.takes; if (takes !== null && (takes !== charTakes || takes === baseline.ownTakes)) out.takes = takes; return Object.keys(out).length ? out : null; } /** * What a Keep keeps: the sampler as it stands (top k and min p only when not their defaults), the takes only when typed. * @param {Record} draft * @param {{ takesTyped?: boolean }} [o] */ export function keepAskOf(draft, { takesTyped = false } = {}) { const out = {}; for (const k of TUNE_SAMPLER) { const v = num(draft?.[k]); if (v !== null && ((k !== "top_k" && k !== "min_p") || v !== DEFAULTS[k])) out[k] = v; } const takes = num(draft?.takes); if (takesTyped && takes !== null) out.takes = takes; return out; } /** * A thread read's Tune starts from HIS piece (the `post` part) -- never the first tuned part of another piece. * @param {any} parts */ export function threadTuneMadeOf(parts) { const p = (Array.isArray(parts) ? parts : []).find((x) => x?.role === "post" && typeof x?.take !== "string") ?? null; return p ? { tuning: p.tuning ?? null, tuningAsked: p.tuningAsked } : null; } /** Does an ask name a sampler value (anything but the takes)? -- a press that does is checked against a fresh baseline. */ export const asksSampler = (ask) => !!ask && Object.keys(ask).some((k) => k !== "takes"); /** Two baselines the same (the character, its tuning, whose the last read was)? */ export function sameBaseline(a, b) { const key = (x) => JSON.stringify(x ? [x.character?.name ?? null, x.character ? x.character.tuning ?? {} : null, !!x.madeOwn] : null); return key(a) === key(b); } /** * A press while the baseline is NOT KNOWN (being read, or its read failed): an untouched draft asks nothing of its * own -- only the takes -- unless the last read was the line's own (then S3's rule: its numbers again); an edited * draft waits for the baseline (null: no press). */ export function unknownBaselineAskOf(draft, { madeOwn = false, dirty = false } = {}) { if (dirty) return { waits: true, ask: null }; if (madeOwn) return { waits: false, ask: defaultsAskOf(draft) }; const takes = num(draft?.takes); return { waits: false, ask: takes !== null ? { takes } : null }; } /** * "Use 's" (review 3 #3): a line whose last read was its own, back to its character -- the draft at the * character's tuning, and the baseline as if the last read were the character's, so the next press asks nothing of * its own (the takes only) and records "preset". * @param {Record | null} madeWith * @param {{ character: { tuning: Record | null } | null, madeOwn: boolean }} baseline */ export function characterTuneOf(madeWith, baseline) { const b = { ...baseline, madeOwn: false, ownTakes: null }; return { baseline: b, draft: tuneStartOf({ madeWith, baseline: b }) }; }