// THE `cast-assign` TASK (characters C3, 2026-11-12): for a umtool video project's post authors, a model's PROPOSAL of
// a character each -- a name, one of the STOCK voices, a speed and a direction line -- asked only on request
// (`umtool cast assign
--model`, the Voices page's "Ask the model too"); umtool's own rule has made the proposed
// rows already, and these are laid over them there, field by field.
//
// in a video project's directory (its video.manifest.json) and the handles to ask about (umtool sends the
// authors its rule proposed for; none: every post item's author), and on the `code` route a Claude Code
// session's answer (`answers`, read exactly as a model's)
// ask ONE schema-bound ask (CAST_ASSIGN_SCHEMA: `{authors: [{handle, character, voice, speed?, direction?}]}`) over
// each author's display name, bio (when the capture has one) and a few of their posts' words -- the
// AUTHORS' OWN TEXT, untrusted: fenced as data, never instructions -- fitted to the prompt cap on a local model
// check each answer field by field (castAssignAnswers): an asked handle only; `voice` one of the stock voices
// (common/lib/ttsApps.ts); `speed` a number 0.5-2; `character` and `direction` one plain line, control
// characters stripped, cut to 80 and 200 characters. A field that fails is dropped and said.
// out `/narration/cast-assign.json`: the checked answers and what was refused. NOTHING IS APPLIED and no
// characters file is written: umtool's one writer (castStore.mjs) merges them into its proposed rows, and a
// person accepts or rejects each there.
//
// It never writes the manifest or narration/characters.json. An API model is held to `maxUsd`.
import { CallSlot, recordSessionAnswer } from "../../../llm/callLog-server";
import { lstat, mkdir, readFile, realpath, rename, writeFile } from "node:fs/promises";
import path from "node:path";
import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs";
import { Meter, priceOf, providerOf, StructuredAnswerError, structuredAnswer, structuredPromptEstimate, type LlmConfig } from "../../../llm";
import { KOKORO_VOICES, ORPHEUS_VOICES } from "../../../ttsApps";
import { fitLocalAsk } from "./revise-report";
export const CAST_ASSIGN_TASK = "cast-assign" as const;
/** The `model` a Claude Code session's answer is written under (the `code` route). */
export const CAST_CODE_MODEL = "claude-code";
export const CAST_ASSIGN_PROMPT_VERSION = "cast-assign-1";
/** Where the checked answers land (relative to the project). */
export const CAST_ASSIGN_FILE = "narration/cast-assign.json";
/** At most this many authors in one ask. */
export const CAST_ASSIGN_MAX_AUTHORS = 60;
const ANSWER_TOKENS = 2500;
const API_VIEW_CHARS = 40_000;
const SPEED = [0.5, 2] as const;
const NAME_MAX = 80;
const DIRECTION_MAX = 200;
/** Every stock voice a proposal may name, with its engine ("kokoro:am_michael", "orpheus:zac"). */
export const STOCK_VOICE_SPECS: readonly string[] = Object.freeze([...KOKORO_VOICES.map((v) => `kokoro:${v}`), ...ORPHEUS_VOICES.map((v) => `orpheus:${v}`)]);
export const CAST_ASSIGN_SCHEMA = Object.freeze({
type: "object",
properties: {
authors: {
type: "array",
items: {
type: "object",
properties: {
handle: { type: "string" },
character: { type: "string" },
voice: { type: "string", enum: [...STOCK_VOICE_SPECS] },
speed: { type: "number" },
direction: { type: "string" },
},
required: ["handle", "character", "voice"],
},
},
},
required: ["authors"],
});
export class CastAssignRefusal extends Error {}
export type CastAssignDeps = {
llm: LlmConfig;
// The model, stubbed (tests).
ask?: (system: string, user: string, schema: object, model: string, maxTokens: number) => Promise;
log?: (line: string) => void;
signal?: AbortSignal;
now?: () => Date;
};
export type CastAssignInput = { project: string; model: string; handles?: string[]; maxUsd?: number; answers?: unknown };
const HANDLE_RE = /^[a-z0-9_.-]{1,64}$/;
const handleKey = (h: unknown) => (typeof h === "string" ? h.trim().replace(/^@+/, "").toLowerCase() : "");
/** One plain line: control characters out, white space folded, at most `max`. */
export const plainLine = (v: unknown, max: number) =>
(typeof v === "string" ? v : "").replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g, " ").replace(/\s+/g, " ").trim().slice(0, max);
/** A video project: its manifest (refused without one). */
export async function loadCastProject(dir: string): Promise<{ dir: string; manifest: Record }> {
let raw: string;
try {
raw = await readFile(path.join(dir, "video.manifest.json"), "utf8");
} catch {
throw new CastAssignRefusal(`${dir} holds no video.manifest.json: characters are proposed for a video project's post authors`);
}
try {
const manifest = JSON.parse(raw) as Record;
if (!manifest || typeof manifest !== "object" || !Array.isArray(manifest.timeline)) throw new Error("no timeline");
return { dir, manifest };
} catch (e) {
throw new CastAssignRefusal(`${dir}/video.manifest.json is not a cut (${(e as Error).message})`);
}
}
type Post = { type?: unknown; id?: unknown; handle?: unknown; author?: unknown; bio?: unknown; pronouns?: unknown; text?: unknown };
export type CastAuthor = { handle: string; name: string; bio: string; posts: string[] };
/**
* THE AUTHORS NEVER SENT TO A MODEL: every handle an anonymous character of the project lists (and an anonymous
* proposal's), and the author handle of every post item that names an anonymous character by id -- umtool's
* post-anon.mjs rule, read here from `/narration/characters.json` (common never imports umtool). No file:
* none. A file that cannot be read or parsed REFUSES the ask (an anonymous author could be sent).
*/
export async function anonymousAuthorsOf(dir: string, manifest: Record): Promise