"use client";
import { useState } from "react";
import { buttonVariants } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { CharacterTuning, type StaleRead, type TuningFields } from "./CharacterTuning";
// ---------------------------------------------------------------------------
// A CHARACTER IS A PRESET (components/projects/VoicesPanel.tsx; /api/report/voices; the CLI is `umtool cast set`,
// `umtool cast project
add|set|rm|undo|pool|keep`, `umtool posts character`). Works with the operator alone:
// every field is a form field, every write the CLI's own command -- nothing here needs an agent.
//
// PresetEditor one character's two presets: SYNTHETIC (the stock voice, its speed, its Orpheus tuning and a
// line to try it on: CharacterTuning.tsx) and RECORDED (what a
// recording of them is made into: the stock voice it is converted to, the engine, the pitch
// and semitones, cleaned or not, its edges trimmed or not and the room kept, and a DIRECTION
// -- one line for the person reading, never sent to a model) -- the cast's or this video's own
// ProjectCharacters THIS VIDEO's OWN characters (narration/characters.json): people who must not enter the pool
// (anonymous ones go by "Anon 1" … here, in recording notes and on the cut's cards), add one, remove one, undo, "Add to the
// pool"; and the post items that name a character of their own
// ---------------------------------------------------------------------------
export type Convert = { engine?: string; voice?: string; pitch?: "target" | "source" | "contour"; semitones?: number };
export type Synthetic = { voice?: string; speed?: number; tuning?: TuningFields };
export type Recorded = { convert?: Convert; enhance?: boolean; trim?: { on?: boolean; padMs?: number }; direction?: string };
export type PresetCharacter = {
id: string; name: string; handles?: string[]; voice?: string; convert?: Convert; synthetic?: Synthetic; recorded?: Recorded;
speakers?: string[]; note?: string; narrator?: boolean; anonymous?: boolean; shown?: string; layer?: "cast" | "project";
};
export type Engine = { id: string; label: string; voices: string[]; defaultVoice: string; weight: string };
export type ItemCharacter = { id: string; handle: string | null; character: string; found: boolean; layer: "cast" | "project" | null; name?: string; voice: { spec: string; from: string; speed?: number } };
type Post = { id: string; handle: string; author: string; text: string };
type PostFn = (body: Record) => Promise | null>;
const input = "rounded-[3px] border border-seam-2 bg-ground px-1.5 py-[3px] text-[11.5px] text-text outline-none focus:border-signal";
const pretty = (spec: string) => spec.replace(":", " · ");
/** A recorded preset in words: "a recording → kokoro · am_michael (seed-vc), pitch contour +2 st · not cleaned · …". */
export function recordedWords(c: PresetCharacter): string {
const rec = c.recorded;
if (!rec) return "";
const bits: string[] = [];
const cv = rec.convert;
if (cv) {
const n = Number(cv.semitones) || 0;
const pitch = cv.pitch && cv.pitch !== "target" ? `, pitch ${cv.pitch}${n ? ` ${n > 0 ? "+" : "−"}${Math.abs(n)} st` : ""}` : "";
bits.push(`a recording → ${pretty(cv.voice ?? c.voice ?? "?")}${cv.engine ? ` (${cv.engine})` : ""}${pitch}`);
}
if (rec.enhance === false) bits.push("not cleaned");
if (rec.trim?.on === false) bits.push("edges not trimmed");
else if (typeof rec.trim?.padMs === "number") bits.push(`trim pad ${rec.trim.padMs} ms`);
if (rec.direction) bits.push(`direction: “${rec.direction}”`);
return bits.join(" · ");
}
/** A stock voice picked as engine + voice; `none` (a label) offers "no voice of its own". */
export function VoicePick({ value, onChange, engines, none = null, name }: { value: string; onChange: (v: string) => void; engines: Engine[]; none?: string | null; name: string }) {
const at = value.indexOf(":");
const eng = at > 0 ? value.slice(0, at) : "";
const voice = at > 0 ? value.slice(at + 1) : "";
const engine = engines.find((e) => e.id === eng) ?? null;
// A voice the editor's list does not carry (an older cast entry) stays selectable as it is.
const voices = engine ? (engine.voices.includes(voice) || !voice ? engine.voices : [voice, ...engine.voices]) : [];
return (
{engine && (
)}
);
}
type Form = {
name: string; shown: string; voice: string; speed: string; convertVoice: string; convertEngine: string; convertPitch: string; convertSemitones: string;
enhance: boolean; trim: boolean; trimPad: string; direction: string;
};
const formOf = (c: PresetCharacter): Form => ({
name: c.name ?? "",
shown: c.shown ?? "",
voice: c.synthetic?.voice ?? c.voice ?? "",
speed: typeof c.synthetic?.speed === "number" ? String(c.synthetic.speed) : "",
convertVoice: c.recorded?.convert?.voice ?? "",
convertEngine: c.recorded?.convert?.engine ?? "",
convertPitch: c.recorded?.convert?.pitch ?? "",
convertSemitones: typeof c.recorded?.convert?.semitones === "number" ? String(c.recorded.convert.semitones) : "",
enhance: c.recorded?.enhance !== false,
trim: c.recorded?.trim?.on !== false,
trimPad: typeof c.recorded?.trim?.padMs === "number" ? String(c.recorded.trim.padMs) : "",
direction: c.recorded?.direction ?? "",
});
/**
* ONE CHARACTER's PRESETS, editable: Save is `umtool cast set …` (scope "cast": every project) or `umtool cast
* project
set …` (scope "project": this video only). A field left empty is its default again.
*/
export function PresetEditor({
c, scope, engines, convertEngines, busy, post, onSaved, onClose, project = null, stale = [],
}: {
c: PresetCharacter; scope: "cast" | "project"; engines: Engine[]; convertEngines: string[]; busy: boolean; post: PostFn; onSaved: (what: string) => void; onClose: () => void;
/** The project the page is of: its tuning tried on this project's lines (none: no tuning row). */
project?: string | null;
/** The reads by this character made with other settings than its tuning now (cast.mjs tuningStale). */
stale?: StaleRead[];
}) {
// (The panel keys this editor by the character as it is stored: another one opened, or this one saved, is a new form.
// Its Orpheus tuning is left out of the key: Keep these settings must not throw away unsaved edits above it.)
const [f, setF] = useState