// `umtool candidates` (UI notes item 14): the candidates inbox from a // terminal -- what an agent or a sweep PROPOSES. It never inserts and never // sets a row aside: those are the operator's presses on the Cut tab's // Candidates panel (lib/report/candidates.mjs). // // umtool candidates [ls] [--all] [--json] the waiting rows (--all: inserted and set aside too) // umtool candidates import [--dry-run] [--json] // umtool candidates add [--json] one proposal or a list // // Plain ESM: the CLI runs under bare node. import { readFile } from "node:fs/promises"; import path from "node:path"; import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { CANDIDATES_FILE, addCandidates, candidatesView, normalizeProposal, parseSweepMarkdown } from "./candidates.mjs"; export const CANDIDATES_USAGE = [ "usage: umtool candidates [ls] [--all] [--json] the proposed clips waiting on the Cut tab's Candidates panel", " (--all: the inserted and the set-aside too): id, verdict, recording @ second, the place it proposes", " umtool candidates import [--dry-run] [--json] a sweep's bullets", " `- [verdict] [title @ time](link) (date) \"quote\" — why`, under their headings; a Top N", " section marks the moments it restates; a bullet with no verdict or link is skipped and said", " umtool candidates add [--json] an agent's proposal(s): {channel?, video, t, quote?, why?,", " verdict?, score?, section?, title?, date?} or {link, …}, one or a list", "", `Proposals only: rows land in /${CANDIDATES_FILE} as proposed. Insert into a section or a group, and`, "Not this, are the operator's presses on the panel. The same moment proposed again changes nothing it holds.", ].join("\n"); class CliError extends Error {} /** * @param {string[]} args the words after `candidates` * @param {{ root?: string, out?: (s: string) => void, err?: (s: string) => void, by?: string }} [opts] * @returns {Promise} */ export async function candidatesCommand(args, { root = REPORTS_ROOT, out = (s) => console.log(s), err = (s) => console.error(s), by = "cli" } = {}) { const words = args.filter((a) => !a.startsWith("--")); const flags = new Set(args.filter((a) => a.startsWith("--"))); const json = flags.has("--json"); try { const [projectArg, verb = "ls", arg] = words; if (!projectArg || flags.has("--help")) { err(CANDIDATES_USAGE); return 2; } const r = await resolveProject(projectArg, root); if (r.ambiguous) throw new CliError(`"${projectArg}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${projectArg}"`); const project = r.project; if (verb === "ls") { const view = await candidatesView(project.dir); const rows = flags.has("--all") ? view.candidates : view.candidates.filter((c) => c.status === "proposed"); if (json) { out(JSON.stringify({ project: project.id, ...view, candidates: rows }, null, 2)); return view.error ? 1 : 0; } if (view.error) err(view.error); for (const c of rows) out(candidateLine(c)); out(`${view.counts.proposed} proposed, ${view.counts.inserted} inserted, ${view.counts.dismissed} set aside — ${project.id}/${CANDIDATES_FILE}`); return view.error ? 1 : 0; } if (verb === "import") { if (!arg) throw new CliError("import needs a sweep's markdown file"); const file = path.resolve(arg); const md = await readFile(file, "utf8"); const { candidates, skipped } = parseSweepMarkdown(md, { file: path.basename(file) }); if (flags.has("--dry-run")) { if (json) out(JSON.stringify({ project: project.id, dryRun: true, candidates, skipped }, null, 2)); else { for (const c of candidates) out(candidateLine({ ...c, id: "--", status: "proposed" })); for (const s of skipped) out(` skipped line ${s.line}: ${s.why}`); out(`${candidates.length} proposal(s), ${skipped.length} skipped — nothing written (--dry-run)`); } return 0; } const res = await addCandidates(project.dir, candidates, { by }); if (json) out(JSON.stringify({ project: project.id, ...res, skipped }, null, 2)); else { for (const s of skipped) out(` skipped line ${s.line}: ${s.why}`); out(`${res.added.length} added${res.kept.length ? `, ${res.kept.length} already there (unchanged)` : ""} — the Cut tab's Candidates panel`); } return 0; } if (verb === "add") { if (!arg) throw new CliError("add needs a JSON proposal (or @file)"); const fromFile = arg.startsWith("@"); const raw = fromFile ? await readFile(path.resolve(arg.slice(1)), "utf8") : arg; let v; try { v = JSON.parse(raw); } catch (e) { // A file's words are never quoted back (JSON.parse's message carries its head): its name only. throw new CliError(fromFile ? `add: ${path.basename(arg.slice(1))} is not JSON` : `add: not JSON (${e instanceof Error ? e.message : e})`); } const list = (Array.isArray(v) ? v : Array.isArray(v?.candidates) ? v.candidates : [v]).map((p) => normalizeProposal(p)); const res = await addCandidates(project.dir, list, { by }); if (json) out(JSON.stringify({ project: project.id, ...res }, null, 2)); else out(`${res.added.length} added${res.kept.length ? `, ${res.kept.length} already there (unchanged)` : ""}`); return 0; } throw new CliError(`unknown: candidates ${verb} — ls, import, add`); } catch (e) { const msg = e instanceof Error ? e.message : String(e); if (json) out(JSON.stringify({ ok: false, error: msg }, null, 2)); else err(`umtool candidates: ${msg}`); return 1; } } const clock = (t) => { const s = Math.floor(t); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); return `${h ? `${h}:${String(m).padStart(2, "0")}` : m}:${String(s % 60).padStart(2, "0")}`; }; export function candidateLine(c) { const where = c.place?.section !== null && c.place?.section !== undefined ? ` → §${c.place.section}${c.place.group ? ` ${c.place.group}` : ""} (${c.place.why})` : c.heading ? ` (${c.heading})` : ""; const state = c.status === "inserted" ? ` [inserted as ${c.inserted?.clip}]` : c.status === "dismissed" ? " [not this]" : c.left ? ` [was ${c.left}, no longer in the cut]` : c.inCut ? ` [in the cut as ${c.inCut}]` : ""; const rank = c.top ? `#${c.top} ` : ""; return `${String(c.id).padEnd(5)} ${rank}[${c.verdict ?? c.kind}] ${c.channel ? `${c.channel}/` : ""}${c.video} @ ${clock(c.t)}${c.date ? ` ${c.date}` : ""}${where}${state}${c.quote ? `\n "${c.quote.slice(0, 110)}${c.quote.length > 110 ? "…" : ""}"` : ""}`; }