import { access } from "node:fs/promises"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { ITERATION_FILES, iterationDir, iterationFile, readIteration, readIterations, } from "yt-dlp-transcript-common/lib/report/iterations"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { modelLogFilesIn, readModelLog } from "yt-dlp-transcript-common/lib/llm/callLog-server"; import { summarizeCalls } from "yt-dlp-transcript-common/lib/llm/callLog"; import { readRoute } from "../_read"; import { opsFail } from "../_lib"; export const dynamic = "force-dynamic"; // GET ?article=[&n=] // -> { ok, article, current, iterations: [iteration.json… + calls], problems } // -> with n: { ok, article, current, iteration, dir, files: {: | null}, calls } // // `calls` (demo epic D3): the iteration's model log counted — {calls, seconds, // failed, models: [{model, route, calls}], phases: [{phase, calls, seconds}]} // — read from calls.jsonl only (never the prompts); `umtool model-log` and // the Model log panel show the calls themselves. // // An article's iterations (release 27 S9; lib/report/iterations.ts), read // only: every one with which is current, or one with where it lives on the // editor's disk — its directory and the path of each file it has (report.json, // sweep.md, turns.jsonl, proposals.json; null when absent). The MCP's // `generate` summarises a finished job's iteration from it, and names the // files Claude Code reviews. Nothing here writes; making one current is // umtool's Iterations tab (or the chain's `current` step). export async function GET(request: Request) { return readRoute(request, ["article", "n"], async (q) => { const article = q.get("article")?.trim() ?? ""; if (!article) return opsFail('"article" is required (an article id)'); if (!isReportId(article)) return opsFail(`"${article}" is not an article id`); const paths = getPaths(); const rawN = q.get("n"); const read = await readIterations(article, paths); if (rawN === null) { if (read.iterations.length === 0 && read.problems.length === 0) { return opsFail(`"${article}" has no iterations`, 404); } const iterations = await Promise.all(read.iterations.map(async (it) => ({ ...it, calls: await callsOf(paths, article, it.n) }))); return { article, current: read.current, iterations, problems: read.problems }; } const n = Number(rawN); if (!Number.isInteger(n) || n < 1) return opsFail('"n" is an iteration number (1, 2, …)'); const it = await readIteration(article, n, paths).catch((e: Error) => e); if (it instanceof Error) return opsFail(`"${article}" i${n}: ${it.message}`, 500); if (!it) return opsFail(`"${article}" has no iteration i${n}`, 404); const files: Record = {}; for (const name of ITERATION_FILES) { const file = iterationFile(paths, article, n, name); files[name] = await access(file).then( () => file, () => null, ); } return { article, current: read.current, iteration: it, dir: iterationDir(paths, article, n), files, calls: await callsOf(paths, article, n) }; }); } async function callsOf(paths: ReturnType, article: string, n: number) { const { calls } = await readModelLog(modelLogFilesIn(iterationDir(paths, article, n))).catch(() => ({ calls: [] })); const s = summarizeCalls(calls); return { calls: s.calls, seconds: s.seconds, failed: s.failed, models: s.models, phases: s.phases }; }