import { projectRef } from "@/lib/projects"; import { refuseCaller } from "@/lib/report/caller"; import { sayCommand, voicesCommand } from "@/lib/report/postSay.mjs"; import { postsCommand } from "@/lib/report/postsCli.mjs"; import { readManifest } from "@/lib/projects/report.mjs"; import { addStep } from "@/lib/queue/store.mjs"; import { kickQueue, queueDir } from "@/lib/queue/runner"; export const dynamic = "force-dynamic"; // A POST PROJECT's PRONUNCIATION LEXICON and VOICE MAP in the app (lane/ui-simplify, 5 Nov; terminal-only // before: `umtool posts say` / `umtool posts voices`). The CLI's own commands (lib/report/postSay.mjs), run // here with --json -- the files' one writer each (narration/lexicon.json, narration/voices.json). // // GET ?project -> { lexicon: say ls, voices: voices ls, stale: say stale, redact: posts redact ls, // slurs: posts slurs ls } (the redaction and the slur filter: their own commands) // POST { project, op: "say-add", word, say, case?, item?, note? } posts say

add "" [--case] [--item] [--note] // { project, op: "say-remove", n } posts say

remove (1-based, as ls lists it) // { project, op: "say-try", text, item? } posts say

try "" // { project, op: "voices-set", handle, voice } posts voices

set // { project, op: "voices-remove", handle } posts voices

remove // { project, op: "mix-cut", key, value } posts narrate

--cut = (render.postNarration) // { project, op: "redact-add", term, substring?, note? } posts redact

add "" [--substring] [--note] // { project, op: "redact-remove", n } posts redact

remove // { project, op: "slurs", verb: on|off|level|visual|voice|add|except|remove|edges, value?, off? } posts slurs

[value] [--off] // { project, op: "queue-narrate", what: "scripts" | "takes" | "context" | "threads" } QUEUED (lib/queue/, /jobs): posts narrate

--all // --draft-scripts | --all --take -- long, so a step of // the queue, never a request held open; the argv is built // here, never taken from the page type Flags = Map; async function run(fn: (out: (s: string) => void) => Promise) { const out: string[] = []; try { const exit = await fn((s) => out.push(s)); return { exit, json: JSON.parse(out.join("\n") || "null") as Record | null }; } catch (e) { return { exit: 1, json: null, error: e instanceof Error ? e.message : String(e) }; } } const say = (project: NonNullable>>, rest: string[], flags: Flags = new Map()) => run((out) => sayCommand(project, rest, { flags, str: (k: string) => String(flags.get(k) ?? ""), json: true, out })); // `umtool posts

…` in this process (postsCli.mjs postsCommand), --json. const posts = async (args: string[]) => { const said: string[] = []; const r: { exit: number; json: Record | null; error?: string } = await run((out) => postsCommand([...args, "--json"], { out, err: (x: string) => said.push(x) })); if (r.exit !== 0 && !r.error) r.error = said.join("\n") || `posts ${args[0]} refused`; return r; }; const voices = (project: NonNullable>>, rest: string[]) => run((out) => voicesCommand(project, rest, { json: true, out })); export async function GET(request: Request) { const project = await projectRef(new URL(request.url).searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const manifest = (await readManifest(project.dir).catch(() => null)) as { render?: { postNarration?: unknown } } | null; const [lexicon, voiceMap, stale, redact, slurs] = await Promise.all([ say(project, ["ls"]), voices(project, ["ls"]), say(project, ["stale"]), posts(["redact", project.id, "ls"]), posts(["slurs", project.id, "ls"]), ]); return Response.json({ lexicon, voices: voiceMap, stale, redact, slurs, mix: manifest?.render?.postNarration ?? {} }, { headers: { "cache-control": "no-store" } }); } export async function POST(request: Request) { const refused = refuseCaller(request, "/api/report/pronunciation"); if (refused) return refused; const b = (await request.json().catch(() => null)) as Record | null; if (!b) return Response.json({ error: "bad json" }, { status: 400 }); const project = await projectRef(String(b.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const s = (k: string) => (typeof b[k] === "string" ? (b[k] as string) : ""); const flags: Flags = new Map(); if (s("item")) flags.set("--item", s("item")); let r; if (b.op === "say-add") { if (b.case === true) flags.set("--case", true); if (s("note")) flags.set("--note", s("note")); r = await say(project, ["add", s("word"), s("say")], flags); } else if (b.op === "say-remove") r = await say(project, ["remove", String(Number(b.n))], flags); else if (b.op === "say-try") r = await say(project, ["try", s("text")], flags); else if (b.op === "voices-set") r = await voices(project, ["set", s("handle"), s("voice")]); else if (b.op === "voices-remove") r = await voices(project, ["remove", s("handle")]); // The cut's narration mix (posts narrate

--cut k=v): one knob a press; the CLI's own words refuse a wrong one. else if (b.op === "mix-cut") r = await posts(["narrate", project.id, "--cut", `${s("key")}=${s("value")}`]); else if (b.op === "redact-add") r = await posts(["redact", project.id, "add", s("term"), ...(b.substring === true ? ["--substring"] : []), ...(s("note") ? ["--note", s("note")] : [])]); else if (b.op === "redact-remove") r = await posts(["redact", project.id, "remove", String(Number(b.n))]); else if (b.op === "slurs") { const verb = s("verb"); if (!["on", "off", "level", "visual", "voice", "add", "except", "remove", "edges"].includes(verb)) return Response.json({ error: "slurs: on, off, level, visual, voice, add, except, remove or edges" }, { status: 400 }); r = await posts(["slurs", project.id, verb, ...(verb === "on" || verb === "off" ? [] : [s("value")]), ...(verb === "edges" && b.off === true ? ["--off"] : [])]); } else if (b.op === "queue-narrate") { // Also the cut-wide post passes that are terminal-only otherwise: fill each item's context from its capture // (posts context), gather each conversation together (timeline threads --write). const what = b.what === "takes" ? "takes" : b.what === "context" ? "context" : b.what === "threads" ? "threads" : "scripts"; const argv = what === "context" ? ["posts", "context", project.id] : what === "threads" ? ["timeline", project.id, "threads", "--write"] : ["posts", "narrate", project.id, "--all", what === "takes" ? "--take" : "--draft-scripts"]; const words = { scripts: "draft every post script", takes: "read every missing post take", context: "fill every post's context from its capture", threads: "gather each conversation" }[what]; try { const step = (await addStep(queueDir(), { argv, label: `${words} of ${project.id}`, target: { project: project.id, take: null }, by: "the umtool app (Reads and voices)", })) as { qid: string }; kickQueue(); return Response.json({ exit: 0, json: { queued: step.qid } }, { headers: { "cache-control": "no-store" } }); } catch (e) { return Response.json({ exit: 1, json: null, error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } } else return Response.json({ error: "op must be say-add, say-remove, say-try, voices-set, voices-remove or queue-narrate" }, { status: 400 }); return Response.json(r, { status: r.exit === 0 ? 200 : 400, headers: { "cache-control": "no-store" } }); }