"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import type { CallOutcome, ModelCall, ModelCallText } from "yt-dlp-transcript-common/lib/llm/callLog"; import type { ModelLogView, ModelLogViewRow } from "yt-dlp-transcript-common/lib/llm/callLogView"; // A JOB'S MODEL LOG (demo epic D3; the record is common's lib/llm/callLog.ts): // every model call the job made, grouped by phase — what was asked (its first // lines), what came back, what code did with it and why, the seconds — each // row opening to the whole prompt and answer, fetched for that row alone. The // count header: calls, seconds, model and route. Filter: kept / dropped / // rewritten. Live while the job runs (re-read every POLL_MS, as the log tail // beside it is). umtool shows the same log on the run page, the iteration and // the media draft. const POLL_MS = 1500; const FILTERS: ("all" | CallOutcome)[] = ["all", "kept", "dropped", "rewritten"]; const ROUTE: Record = { local: "local", api: "Claude API", "claude-code": "Claude Code" }; const TONE: Record = { kept: "text-green-700 dark:text-green-400", dropped: "text-red-700 dark:text-red-400", rewritten: "text-amber-700 dark:text-amber-400", "n/a": "text-muted-foreground" }; type Answer = { status: string | null; where: { article: string; n: number } | null; view: ModelLogView | null }; function Row({ jobId, row }: { jobId: string; row: ModelLogViewRow }) { const [open, setOpen] = useState(false); const [full, setFull] = useState<{ call: ModelCall; text: ModelCallText | null } | null>(null); const [error, setError] = useState(""); const toggle = async () => { setOpen(!open); if (open || full) return; const r = await fetch(`/api/jobs/${jobId}/model-log/call?id=${encodeURIComponent(row.id)}`, { cache: "no-store" }).catch(() => null); const b = r ? await r.json().catch(() => ({})) : {}; if (r?.ok) setFull(b); else setError(b.error ?? "the call could not be read"); }; const items = (open && full ? full.call.items : row.items) ?? []; return (
  • {items.length > 0 && (
      {items.map((it, i) => (
    • {it.outcome} {it.text} {it.why && — {it.why}}
    • ))} {!open && row.itemsTotal > items.length &&
    • + {row.itemsTotal - items.length} more
    • }
    )} {open && (
    {error &&

    {error}

    } {full?.text && ( <>

    {row.model} · {ROUTE[row.route] ?? row.route} · {row.promptChars.toLocaleString("en-US")} characters asked {row.tokensIn !== undefined ? ` · ${row.tokensIn} tokens in, ${row.tokensOut ?? 0} out` : ""}

    {full.text.system && (
    System prompt
    {full.text.system}
    )}
                    {full.text.prompt + (full.text.promptCut ? "\n… (cut at 40,000 characters)" : "")}
                  
    {full.text.answer || "(empty)"}
    )}
    )}
  • ); } export function JobModelLog({ jobId, initiallyRunning }: { jobId: string; initiallyRunning: boolean }) { const [data, setData] = useState(null); const [filter, setFilter] = useState<"all" | CallOutcome>("all"); const [running, setRunning] = useState(initiallyRunning); // Rows a group shows (a page more each "Show more"). const [limit, setLimit] = useState(25); const busy = useRef(false); const load = useCallback(async () => { if (busy.current) return; busy.current = true; try { const q = `?limit=${limit}${filter === "all" ? "" : `&outcome=${filter}`}`; const r = await fetch(`/api/jobs/${jobId}/model-log${q}`, { cache: "no-store" }); if (!r.ok) return; const b = (await r.json()) as Answer; setData(b); setRunning(b.status === "running" || b.status === "queued"); } catch { // the next tick asks again } finally { busy.current = false; } }, [jobId, filter, limit]); useEffect(() => { void load(); }, [load]); useEffect(() => { if (!running) return; const t = window.setInterval(() => document.visibilityState === "visible" && void load(), POLL_MS); return () => window.clearInterval(t); }, [running, load]); const v = data?.view ?? null; const s = v?.summary; return (

    Model log

    {s && s.calls > 0 && ( {s.calls} {s.calls === 1 ? "call" : "calls"} · {s.seconds.toFixed(1)} s · {s.models.map((m) => `${m.model} (${ROUTE[m.route] ?? m.route})`).join(", ")} {s.failed > 0 ? ` · ${s.failed} failed` : ""} )} {data?.where && {data.where.article} i{data.where.n}} {FILTERS.map((f) => ( ))}
    {v?.full && (

    The log is full ({v.cap.calls.toLocaleString("en-US")} calls or {v.cap.megabytes} MB): calls made after that were not recorded.

    )} {!v || v.counts.all === 0 ? (

    {running ? "No model call yet. Each call appears here as it is made: what was asked, what came back, and what code kept or dropped." : "This job recorded no model call."}

    ) : ( v.groups.map((g) => (

    {g.label} {g.calls} · {g.seconds.toFixed(1)} s

      {g.rows.map((r) => ( ))}
    {g.more > 0 && ( )}
    )) )}
    ); }