// `archilyzer reports iterations [--json]` (release 27 S4) — an // article's iterations (lib/report/iterations.ts): each with its kind, model, // status, cost, lineage and what it revised or copied, the current article // and current video marked. Reads only. // // Exit 0 with the list (an article with none says so); 1 when an // iteration.json is unsound (it is named); 2 for a bad id, or an id that is // neither an article nor an article in the making. import { getPaths, type Paths } from "../lib/paths"; import { readArticleState } from "../lib/report/registry"; import { iterationLabel, iterationUsd, readIterations, type Iteration, type IterationsCurrent, } from "../lib/report/iterations"; import { isReportId } from "../lib/report/schema"; type Out = { log: (s: string) => void; error: (s: string) => void }; export type IterationRow = Iteration & { lineage: number[]; usd: number; currentReport: boolean; currentVideo: boolean; }; // The lineage of each iteration from the list itself (no further reads): its // ancestors by parent, first to it. An unsound ancestor ends the walk. export function iterationRows(iterations: readonly Iteration[], current: IterationsCurrent): IterationRow[] { const byN = new Map(iterations.map((i) => [i.n, i])); return iterations.map((it) => { const line: number[] = []; let at: Iteration | undefined = it; while (at) { line.unshift(at.n); at = at.parent === null ? undefined : byN.get(at.parent); } return { ...it, lineage: line, usd: iterationUsd(it), currentReport: current.report === it.n, currentVideo: current.video === it.n, }; }); } const one = (s: string, max = 60) => { const t = s.replace(/\s+/g, " ").trim(); return t.length > max ? `${t.slice(0, max - 1)}…` : t; }; export function formatIterationRow(r: IterationRow): string { const marks = [r.currentReport ? "current article" : "", r.currentVideo ? "current video" : ""].filter(Boolean); const from = [ r.parent !== null ? `revises ${iterationLabel(r.parent)}` : "", r.copyOf !== null ? `copy of ${iterationLabel(r.copyOf)}` : "", ].filter(Boolean); const words = r.feedback ?? r.question; return [ `${r.currentReport || r.currentVideo ? "*" : " "} ${iterationLabel(r.n).padEnd(4)}`, r.kind.padEnd(13), r.model.padEnd(20), r.status.padEnd(9), (r.usd > 0 ? `$${r.usd.toFixed(2)}` : "—").padEnd(7), r.createdAt.slice(0, 16).replace("T", " "), ` ${r.lineage.map(iterationLabel).join(" › ")}`, ...(from.length ? [`(${from.join(", ")})`] : []), ...(words ? [`“${one(words)}”`] : []), ...(marks.length ? [`← ${marks.join(", ")}`] : []), ].join(" "); } export async function iterationsMain( opts: { article: string; json?: boolean; paths?: Pick }, out: Out = console, ): Promise { const paths = opts.paths ?? getPaths(); const id = opts.article; if (!isReportId(id)) { out.error(`reports iterations: "${id}" is not an article id`); return 2; } let read; try { read = await readIterations(id, paths); } catch (err) { out.error(`reports iterations: ${(err as Error).message}`); return 2; } const exists = (await readArticleState(id, paths)) !== null; if (!exists && read.iterations.length === 0 && read.problems.length === 0) { out.error(`reports iterations: no article "${id}" (no reports/${id}/)`); return 2; } const rows = iterationRows(read.iterations, read.current); if (opts.json) { out.log( JSON.stringify( { article: id, inTheMaking: !exists, current: read.current, iterations: rows, problems: read.problems }, null, 2, ), ); } else { out.log( `reports iterations ${id}: ${rows.length} iteration(s)${exists ? "" : " — an article in the making (no report.json yet)"}`, ); for (const r of rows) out.log(formatIterationRow(r)); for (const p of read.problems) out.error(` ${iterationLabel(p.n)}: ${p.problems.join("; ")}`); } return read.problems.length > 0 ? 1 : 0; }