// `umtool model-log` -- the model log from a terminal (demo epic D3; the // record is common's lib/llm/callLog.ts, the page's panel // components/model-log/ModelLog.tsx). Run by bin/umtool.mjs through tsx, from // umtool/ (the `@/` paths). Read only. // // umtool model-log
[--phase P] [--outcome kept|dropped|rewritten|n/a] // [--task T] [--run K] [--call ] [--json] // umtool model-log --job [the same] // every model call of the iteration's runs (or the job's), by phase: // the seconds, what code did and why, what was asked, what came back, // and each item's fate; --call prints one call's whole text // // Every line printed for a person goes through `say`: a recorded prompt or // answer is a model's (or a transcript's) text, and an ESC in it would be the // terminal's to obey. --json prints JSON through `sayJson`, which escapes what // JSON.stringify leaves (DEL, C1, bidi controls) as \u…: the same document. import { CALLS_MAX_BYTES, CALLS_MAX_CALLS, CALL_OUTCOMES, callMatches, phaseLabel, phaseOrder, summarizeCalls, terminalSafe, terminalSafeJson, type CallOutcome, type ModelCall } from "yt-dlp-transcript-common/lib/llm/callLog"; import { readModelLog } from "yt-dlp-transcript-common/lib/llm/callLog-server"; import { ModelLogError, jobLogOf, modelLogFilesOf, readModelCall, type ModelLogSource } from "../lib/model-log"; const args = process.argv.slice(2); const VALUED = ["--phase", "--outcome", "--task", "--run", "--call", "--job"]; const has = (f: string) => args.includes(f); const val = (f: string) => { const i = args.indexOf(f); return i >= 0 ? args[i + 1] : undefined; }; const json = has("--json"); const positional = args.filter((a, i) => !a.startsWith("--") && !VALUED.includes(args[i - 1] ?? "")); const USAGE = "usage: umtool model-log
| --job [--phase P] [--outcome kept|dropped|rewritten|n/a] [--task T] [--run K] [--call ] [--json]"; const say = (line: string) => process.stdout.write(`${terminalSafe(line)}\n`); const sayJson = (v: unknown) => process.stdout.write(`${terminalSafeJson(JSON.stringify(v, null, 2))}\n`); const die = (msg: string, code = 2): never => { console.error(terminalSafe(`umtool model-log: ${msg}`)); process.exit(code); }; const job = val("--job"); const src: ModelLogSource = job ? { job } : { article: positional[0] ?? "", n: Number(positional[1]) }; if (!job && (!positional[0] || !Number.isInteger(Number(positional[1])))) die(USAGE); const outcome = val("--outcome"); if (outcome && !(CALL_OUTCOMES as readonly string[]).includes(outcome)) die(`--outcome is one of ${CALL_OUTCOMES.join(", ")}`); const runArg = val("--run"); const run = runArg === undefined ? undefined : Number(runArg); if (run !== undefined && !Number.isInteger(run)) die("--run is a run number (1, 2, …)"); const where = "job" in src ? `job ${src.job}` : `${src.article} i${src.n}`; const secs = (s: number) => `${s.toFixed(1)} s`; const one = (s: string) => s.replace(/\s+/g, " ").trim(); try { const id = val("--call"); if (id) { const { call, text } = await readModelCall(src, id); if (json) { sayJson({ call, text }); process.exit(0); } say(`${where} · call ${call.id} · ${call.phase}${call.label ? ` · ${call.label}` : ""} · ${call.model} (${call.route}) · ${secs(call.seconds)} · ${call.decision.outcome}${call.decision.why ? `: ${call.decision.why}` : ""}`); for (const it of call.items ?? []) say(` ${it.outcome.padEnd(9)} ${one(it.text)}${it.why ? ` — ${it.why}` : ""}`); if (!text) say("\n(this call's text was not kept)"); else { if (text.system) say(`\n── system ──\n${text.system}`); say(`\n── asked ──\n${text.prompt}${text.promptCut ? "\n… (cut at 40,000 characters)" : ""}`); say(`\n── came back ──\n${text.answer}${text.answerCut ? "\n… (cut at 40,000 characters)" : ""}`); } process.exit(0); } // A job's calls in an iteration's log: only its run(s). const ofJob = "job" in src ? await jobLogOf(src.job) : null; const log = await readModelLog(ofJob ? ofJob.files : await modelLogFilesOf(src)); const jobRuns = ofJob?.jobId ? new Set(log.runs.filter((r) => r.jobId === ofJob.jobId).map((r) => r.run)) : null; const task = val("--task"); const phase = val("--phase"); const scoped = log.calls.filter((c) => (!jobRuns || jobRuns.has(c.run)) && callMatches(c, { ...(task ? { task } : {}), ...(run !== undefined ? { run } : {}) })); const shown = scoped.filter((c) => callMatches(c, { ...(phase ? { phase } : {}), ...(outcome ? { outcome: outcome as CallOutcome } : {}) })); const summary = summarizeCalls(scoped); if (json) { sayJson({ source: src, runs: log.runs, summary, full: !!log.full, calls: shown }); process.exit(0); } if (!scoped.length) { say(`${where}: no model call recorded`); process.exit(0); } say(`${where} · ${summary.calls} call${summary.calls === 1 ? "" : "s"} · ${secs(summary.seconds)} · ${summary.models.map((m) => `${m.model} (${m.route}, ${m.calls})`).join(", ")}${summary.failed ? ` · ${summary.failed} failed` : ""}`); if (log.full) say(`the log is full (${CALLS_MAX_CALLS.toLocaleString("en-US")} calls or ${Math.round(CALLS_MAX_BYTES / (1024 * 1024))} MB): calls made after that were not recorded`); const runs = log.runs.filter((r) => scoped.some((c) => c.run === r.run)); if (runs.length > 1) say(`runs: ${runs.map((r) => `${r.run} ${r.task} ${r.at.slice(0, 16)}`).join(" · ")}`); for (const p of phaseOrder(shown.map((c) => c.phase))) { const list: ModelCall[] = shown.filter((c) => c.phase === p); say(`\n${phaseLabel(p)} · ${list.length} call${list.length === 1 ? "" : "s"} · ${secs(list.reduce((s, c) => s + c.seconds, 0))}`); for (const c of list) { say(` ${c.id.padEnd(6)} ${secs(c.seconds).padStart(7)} ${c.decision.outcome.padEnd(9)} ${c.label ?? ""}${c.decision.why ? ` — ${c.decision.why}` : ""}`); say(` asked: ${one(c.promptHead).slice(0, 160)}`); say(` back: ${c.error ? `FAILED ${one(c.error)}` : one(c.answerHead).slice(0, 160)}`); for (const it of c.items ?? []) if (!outcome || it.outcome === outcome) say(` ${it.outcome.padEnd(9)} ${one(it.text).slice(0, 140)}${it.why ? ` — ${it.why}` : ""}`); } } say(`\nthe whole text of one: umtool model-log ${"job" in src ? `--job ${src.job}` : `${src.article} ${src.n}`} --call `); } catch (e) { die((e as Error).message, e instanceof ModelLogError && e.status === 400 ? 2 : 1); }