// THE WORDS OF A RE-READ (lane reads-find, the orchestrator's ruling 3: the queue's read is the bench's): the umtool
// argv a bench button runs to read a line again -- the screen line's and the thread's Read again (the post-narration
// route's screen-read and thread-read ops), and the re-read queue's step for an item (readsQueue.mjs), built from the
// same pieces so the two can never drift apart. Pure.
//
// a card `narration
--read`: its own words, reading and tuning (what its Play makes)
// a post read alone `posts narrate - --take --reading next --no-pick [tuning flags] --bench-slurs` -- the
// bench's take op, as the CLI asks it (postTakeAsk.mjs: one ask), its slur rule the bench's
// a thread read `posts screen
- read …` with screens, else `posts narrate
- --take --thread
// …`: ONE stale piece exactly as its line's Read again
// (`--for --reading next [tuning flags] --no-pick`), several each `--tuned
// =reading:next,` -- the others from the cache, as they were read
//
// The tuning a re-read asks is the PLAIN PRESS's (tuneAsk.mjs plainPressAskOf, carried by the read as `ask`): only
// what the line owned; the character's and the settings' are resolved anew (ruling 2).
//
// Plain ESM.
import { parseTtsTuning, ttsReadingError } from "yt-dlp-transcript-common/lib/ttsTuning.mjs";
import { threadArgvOf } from "./castProject.mjs";
/** Each tuning field's flag (readTuning.mjs TUNING_FLAGS, the other way round). */
export const TUNING_FLAG_OF = Object.freeze({ temperature: "--temperature", top_p: "--top-p", top_k: "--top-k", min_p: "--min-p", repeat_penalty: "--repeat-penalty", takes: "--chunk-takes" });
const ROLE_RE = /^(post|quoted|parent:\d|linked:\d|screen:[a-z0-9][a-z0-9-]{0,23}:\d{1,2})$/;
/** A tuning as flags (`--temperature 0.7 …`), in the fields' order. */
export function tuningFlagsOf(tuning) {
const a = [];
for (const [k, flag] of Object.entries(TUNING_FLAG_OF)) if (typeof tuning?.[k] === "number") a.push(flag, String(tuning[k]));
return a;
}
/**
* A READ AGAIN's words for the CLI (the post-narration route's screen-read / thread-read ops; lane line-rerun):
* `--for`, `--reading`, the tuning flags, `--no-pick`. Throws a sentence for a body it refuses.
* @param {Record} body
*/
export function readAgainArgv(body) {
const a = [];
if (body.for !== undefined && body.for !== null) {
if (typeof body.for !== "string" || !ROLE_RE.test(body.for)) throw new Error("for must name one piece of the read (screen::, post, quoted, parent:)");
a.push("--for", body.for);
}
if (body.reading !== undefined && body.reading !== null) {
if (body.reading !== "next") {
const err = ttsReadingError(body.reading);
if (err) throw new Error(err);
}
a.push("--reading", String(body.reading));
}
if (body.tuning !== undefined && body.tuning !== null) {
const t = parseTtsTuning(body.tuning);
if (!t.ok) throw new Error(t.error);
for (const [k, v] of Object.entries(t.value)) a.push(TUNING_FLAG_OF[k], String(v));
}
if (a.includes("--for") && !a.includes("--reading") && !Object.values(TUNING_FLAG_OF).some((f) => a.includes(f))) throw new Error("for goes with a reading or a tuning");
if (body.keepPick === true || body.op === "thread-read") a.push("--no-pick");
return a;
}
/** One piece's `--tuned` value: the next reading, and the tuning a plain press asks (`ask`). */
const tunedOf = (role, ask) => `${role}=${["reading:next", ...Object.entries(ask ?? {}).filter(([k, v]) => k in TUNING_FLAG_OF && typeof v === "number").map(([k, v]) => `${k}:${v}`)].join(",")}`;
/**
* THE RE-READ OF ONE ITEM's stale reads (`reads`: reads.mjs rows of that item, each with its plain press's `ask`) as
* the umtool argv its bench button runs. `e` the item as the manifest has it.
* @param {string} projectId
* @param {Record} e
* @param {Array<{ kind: string, role: string | null, ask?: Record | null }>} reads
*/
export function itemRereadArgv(projectId, e, reads) {
if (!reads.length) throw new Error(`${e?.id}: nothing to re-read`);
if (e?.type === "card") return ["narration", projectId, e.id, "--read"];
const pieces = reads.filter((r) => r.kind === "thread" || r.kind === "screen");
if (!pieces.length) return ["posts", "narrate", projectId, e.id, "--take", "--reading", "next", "--no-pick", ...tuningFlagsOf(reads[0].ask), "--bench-slurs"];
const withScreens = Array.isArray(e.screens) && e.screens.length > 0;
const one = pieces.length === 1;
const asked = one
? readAgainArgv({ for: pieces[0].role, reading: "next", ...(pieces[0].ask ? { tuning: pieces[0].ask } : {}), keepPick: true })
: [...pieces.flatMap((r) => ["--tuned", tunedOf(r.role, r.ask)]), "--no-pick"];
if (withScreens) return ["posts", "screen", projectId, e.id, "read", ...asked];
const takes = e.narration?.takes ?? [];
const t = takes[Number.isInteger(e.narration?.pick) ? e.narration.pick : 0] ?? null;
// As the read that plays was asked -- but a stale piece's own recorded `--tuned` gives way to its re-read (a later
// --tuned of a role beats an earlier one, but --for is laid UNDER every --tuned).
const stale = new Set(pieces.map((r) => r.role));
const base = threadArgvOf(t?.read, { takes });
const kept = base.filter((x, i) => !(x === "--tuned" && stale.has(String(base[i + 1]).split("=")[0])) && !(base[i - 1] === "--tuned" && stale.has(String(x).split("=")[0])));
return ["posts", "narrate", projectId, e.id, "--take", "--thread", ...kept, ...asked];
}