import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { readIteration, iterationDir } from "yt-dlp-transcript-common/lib/report/iterations"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { modelLogFilesIn, readModelCallText, readModelLog, type ModelLogFiles } from "yt-dlp-transcript-common/lib/llm/callLog-server"; import { modelLogView, type ModelLogView, type ModelLogViewQuery } from "yt-dlp-transcript-common/lib/llm/callLogView"; import type { ModelCall, ModelCallText } from "yt-dlp-transcript-common/lib/llm/callLog"; import { jobModelLogFiles, locateJobModelLog } from "yt-dlp-transcript-common/controller/jobModelLog"; import { sitesPaths } from "@/lib/articles/sites"; // --------------------------------------------------------------------------- // THE MODEL LOG, read (demo epic D3; the record is common's lib/llm/callLog.ts): // an iteration's calls.jsonl -- its runs and every step on it, a media draft's // among them -- or a job's with no iteration (`.generation/calls//`). // READ ONLY: the editor's runs write it. The panel // (components/model-log/ModelLog.tsx) and `umtool model-log` read through here. // --------------------------------------------------------------------------- export class ModelLogError extends Error { constructor( message: string, readonly status = 400, ) { super(message); } } export type ModelLogSource = { article: string; n: number } | { job: string }; export function sourceOf(sp: URLSearchParams): ModelLogSource { const job = sp.get("job")?.trim(); if (job) return { job }; const article = sp.get("article")?.trim() ?? ""; const n = Number(sp.get("n") ?? ""); return { article, n }; } /** A job's: its own log, or the iteration's its pointer names (then only its run). */ export async function jobLogOf(job: string): Promise<{ files: ModelLogFiles; jobId?: string }> { try { const at = await locateJobModelLog(sitesPaths(), job); if (at) return { files: at.files, ...(at.where ? { jobId: job } : {}) }; return { files: jobModelLogFiles(getPaths(), job) }; } catch (e) { throw new ModelLogError((e as Error).message); } } export async function modelLogFilesOf(src: ModelLogSource): Promise { if ("job" in src) return (await jobLogOf(src.job)).files; if (!isReportId(src.article)) throw new ModelLogError(`not an article id: ${JSON.stringify(src.article)}`); if (!Number.isInteger(src.n) || src.n < 1) throw new ModelLogError(`not an iteration number: ${JSON.stringify(src.n)} (1, 2, …)`); const paths = sitesPaths(); const it = await readIteration(src.article, src.n, paths).catch((e: Error) => { throw new ModelLogError(`${src.article} i${src.n} cannot be read: ${e.message}`, 409); }); if (!it) throw new ModelLogError(`"${src.article}" has no iteration i${src.n}`, 404); return modelLogFilesIn(iterationDir(paths, src.article, src.n)); } export async function readModelLogView(src: ModelLogSource, q: ModelLogViewQuery): Promise { if ("job" in src) { const at = await jobLogOf(src.job); return modelLogView(await readModelLog(at.files), { ...q, ...(at.jobId ? { jobId: at.jobId } : {}) }); } return modelLogView(await readModelLog(await modelLogFilesOf(src)), q); } /** One call: its row (every item) and its whole text. */ export async function readModelCall(src: ModelLogSource, id: string): Promise<{ call: ModelCall; text: ModelCallText | null }> { if (!/^\d+\.\d+$/.test(id)) throw new ModelLogError(`not a call id: ${JSON.stringify(id)} (run.call, e.g. 1.4)`); const files = await modelLogFilesOf(src); const call = (await readModelLog(files)).calls.find((c) => c.id === id); if (!call) throw new ModelLogError(`no call ${id}`, 404); return { call, text: await readModelCallText(files, id) }; } /** An iteration's call count and seconds, for a list (calls.jsonl only, never the texts). */ export async function callCountOf(article: string, n: number): Promise<{ calls: number; seconds: number } | null> { try { const { calls } = await readModelLog(modelLogFilesIn(iterationDir(sitesPaths(), article, n))); if (!calls.length) return null; return { calls: calls.length, seconds: Math.round(calls.reduce((s, c) => s + (c.seconds || 0), 0) * 10) / 10 }; } catch { return null; } }