// THE MODEL LOG AS A PANEL SHOWS IT (demo epic D3) — pure: a log (./callLog.ts // foldModelLog) and a query → the runs, the run shown, the count header, and // the calls grouped by phase, each group one page at a time. A row carries // heads, never whole texts (those are fetched per row), and at most // LIST_ITEMS of its items: the page never ships megabytes. umtool's panel, // the editor's job page and `umtool model-log` all read this. import { CALLS_MAX_BYTES, CALLS_MAX_CALLS, CALL_OUTCOMES, callMatches, phaseLabel, phaseOrder, summarizeCalls, type CallOutcome, type ModelCall, type ModelLog, type ModelLogRunRow, type ModelLogSummary, } from "./callLog"; export const PAGE_ROWS = 25; export const MAX_PAGE_ROWS = 200; export const LIST_ITEMS = 5; export type ModelLogViewQuery = { // Only this task's runs (a media draft's panel: its task). task?: string; // The run of this job, else `run`, else (with `latest`) the newest run that // matches `task`; none of them: every run. jobId?: string; run?: number; latest?: boolean; // The run that began nearest this time (a trace's run start: the run page). near?: string; outcome?: CallOutcome; // One phase's page (a "Show more"): only that group, from `offset`. phase?: string; offset?: number; limit?: number; }; export type ModelLogViewRow = Omit & { items: ModelCall["items"]; itemsTotal: number }; export type ModelLogViewGroup = { phase: string; label: string; calls: number; seconds: number; offset: number; rows: ModelLogViewRow[]; more: number }; export type ModelLogView = { runs: ModelLogRunRow[]; // The run shown; null: every run (the iteration's whole log). run: number | null; summary: ModelLogSummary; // How many calls each filter would show (before the outcome filter). counts: Record<"all" | CallOutcome, number>; outcome: CallOutcome | null; groups: ModelLogViewGroup[]; // The log is at its cap: calls made after it were not recorded. full: boolean; cap: { calls: number; megabytes: number }; }; const listRow = (c: ModelCall): ModelLogViewRow => ({ ...c, items: c.items?.slice(0, LIST_ITEMS), itemsTotal: c.items?.length ?? 0 }); export function pickRun(log: ModelLog, q: Pick): number | null { const runs = log.runs.filter((r) => !q.task || r.task === q.task); if (q.jobId) return runs.filter((r) => r.jobId === q.jobId).at(-1)?.run ?? -1; if (q.run !== undefined) return runs.some((r) => r.run === q.run) ? q.run : -1; if (q.near) { const t = Date.parse(q.near); if (!Number.isFinite(t) || !runs.length) return runs.length ? null : -1; return [...runs].sort((a, b) => Math.abs(Date.parse(a.at) - t) - Math.abs(Date.parse(b.at) - t))[0]!.run; } if (q.latest) { // The newest run that made a call, else the newest at all. const made = new Set(log.calls.map((c) => c.run)); return [...runs].reverse().find((r) => made.has(r.run))?.run ?? runs.at(-1)?.run ?? null; } return null; } export function modelLogView(log: ModelLog, q: ModelLogViewQuery = {}): ModelLogView { const run = pickRun(log, q); const scoped = log.calls.filter((c) => callMatches(c, { ...(q.task ? { task: q.task } : {}), ...(run !== null ? { run } : {}) })); const counts = { all: scoped.length } as ModelLogView["counts"]; for (const o of CALL_OUTCOMES) counts[o] = scoped.filter((c) => callMatches(c, { outcome: o })).length; const shown = q.outcome ? scoped.filter((c) => callMatches(c, { outcome: q.outcome })) : scoped; const limit = Math.max(1, Math.min(MAX_PAGE_ROWS, Math.floor(q.limit ?? PAGE_ROWS))); const groups: ModelLogViewGroup[] = []; for (const phase of phaseOrder(scoped.map((c) => c.phase))) { if (q.phase && phase !== q.phase) continue; const all = shown.filter((c) => c.phase === phase); const offset = q.phase ? Math.max(0, Math.floor(q.offset ?? 0)) : 0; const rows = all.slice(offset, offset + limit).map(listRow); groups.push({ phase, label: phaseLabel(phase), calls: all.length, seconds: Math.round(all.reduce((s, c) => s + (c.seconds || 0), 0) * 10) / 10, offset, rows, more: Math.max(0, all.length - offset - rows.length), }); } return { runs: log.runs.filter((r) => !q.task || r.task === q.task), run, summary: summarizeCalls(scoped), counts, outcome: q.outcome ?? null, groups: q.phase ? groups : groups.filter((g) => g.calls > 0 || !q.outcome), full: !!log.full, cap: { calls: CALLS_MAX_CALLS, megabytes: Math.round(CALLS_MAX_BYTES / (1024 * 1024)) }, }; } // A query from URL search params (the umtool and editor routes'); a bad // value is an error a person can read. export function viewQueryOf(sp: URLSearchParams): ModelLogViewQuery { const q: ModelLogViewQuery = {}; const str = (k: string) => sp.get(k)?.trim() || undefined; const int = (k: string) => { const v = str(k); if (v === undefined) return undefined; const n = Number(v); if (!Number.isInteger(n) || n < 0) throw new Error(`"${k}" is a whole number`); return n; }; const task = str("task"); if (task) q.task = task; const jobId = str("jobId"); if (jobId) q.jobId = jobId; const run = int("run"); if (run !== undefined) q.run = run; if (sp.get("latest") === "1" || sp.get("latest") === "true") q.latest = true; const near = str("near"); if (near) { if (!Number.isFinite(Date.parse(near))) throw new Error('"near" is an ISO time'); q.near = near; } const outcome = str("outcome"); if (outcome) { if (!(CALL_OUTCOMES as readonly string[]).includes(outcome)) throw new Error(`"outcome" is one of ${CALL_OUTCOMES.join(", ")}`); q.outcome = outcome as CallOutcome; } const phase = str("phase"); if (phase) q.phase = phase; const offset = int("offset"); if (offset !== undefined) q.offset = offset; const limit = int("limit"); if (limit !== undefined) q.limit = limit; return q; }