"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { badgeVariants } from "@/components/ui/badge"; import type { PolicyView } from "@/lib/articles/generation"; import { plannedModelOf, type GenerationTask } from "yt-dlp-transcript-common/lib/generationSettings"; /** A task's last run, from the usage ledger (demo epic D3). */ export type TaskLastRun = { at: string; jobId: string; models: string[]; route: "api" | "local" | "mixed" }; // THE GENERATION POLICY (release 27 S8): where each task runs (Claude Code, // the API, the local model), the presets that set all four at once, the // models, the caps. Written through the editor's settings action // (/api/articles/policy -> POST /api/ops/settings). The API key is never here: // the key FILE's path, and whether a file is there. export type Today = { day: string; usd: number; jobs: number; daily: number }; const ROUTE_LABEL: Record = { code: "Claude Code", api: "API", local: "local" }; const CAP_KEYS = ["sweep", "revision", "narration", "daily", "max"] as const; const field = "rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 text-[12px] text-[var(--color-text)]"; export const usd = (v: number) => `$${v.toFixed(2)}`; export default function PolicyPanel({ initial, today: initialToday, lastRuns = {}, onChange, }: { initial: PolicyView; today: Today | null; /** Each task's last run (the ledger's newest job of it). */ lastRuns?: Record; onChange?: (p: PolicyView) => void; }) { const [policy, setPolicy] = useState(initial); const [today] = useState(initialToday); const [caps, setCaps] = useState(() => Object.fromEntries(CAP_KEYS.map((k) => [k, String(initial.generation.caps[k])]))); const triOf = (t: PolicyView["generation"]["triage"]) => ({ model: t.model, strong: String(t.strong), weak: String(t.weak), cap: String(t.maxEscalationsPerDay) }); const [tri, setTri] = useState(() => triOf(initial.generation.triage)); const [status, setStatus] = useState<{ ok: boolean; text: string } | null>(null); const [busy, setBusy] = useState(false); const g = policy.generation; useEffect(() => onChange?.(policy), [policy, onChange]); async function save(patch: Record, what: string) { setBusy(true); setStatus(null); try { const r = await fetch("/api/articles/policy", { method: "POST", body: JSON.stringify(patch) }); const body = await r.json(); if (!r.ok) throw new Error(body.error ?? `HTTP ${r.status}`); setPolicy(body.policy); setCaps(Object.fromEntries(CAP_KEYS.map((k) => [k, String(body.policy.generation.caps[k])]))); setTri(triOf(body.policy.generation.triage)); setStatus({ ok: true, text: `saved ${what}` }); } catch (e) { setStatus({ ok: false, text: (e as Error).message }); } finally { setBusy(false); } } return (
policy
{Object.keys(policy.presets).map((p) => ( ))}
{g.preset === "custom" && custom} {today && ( today {usd(today.usd)} {today.daily > 0 ? ` of ${usd(today.daily)}` : " (no daily cap)"} )}
{/* Per task (demo epic D3): the route, the model it will run on as the policy stands, "no API key configured" when it would call the API with none, and the model(s) of its last run in the ledger. */}
routes
{policy.tasks.map((t) => { const plan = plannedModelOf(g, t as GenerationTask); const noKey = plan.needsKey && !policy.apiKey.configured; const last = lastRuns[t]; return ( ); })}
task route will run on last run
{t} {plan.model ? {plan.model} : a Claude Code session} {noKey && ( no API key configured )} {last ? ( {last.models.join(", ")} ยท {last.at.slice(0, 10)} ) : ( none in the ledger )}
models
{(["api", "local", "extractor"] as const).map((k) => { const list = k === "local" ? policy.models.local : k === "extractor" ? [...policy.models.api, ...policy.models.local] : policy.models.api; const opts = [...new Set([g.models[k], ...list])]; return ( ); })}
caps
{ e.preventDefault(); save({ caps: Object.fromEntries(CAP_KEYS.map((k) => [k, Number(caps[k])])) }, "caps"); }} > {CAP_KEYS.map((k) => ( ))}
triage first
{ e.preventDefault(); save({ triage: { ...g.triage, model: tri.model, strong: Number(tri.strong), weak: Number(tri.weak), maxEscalationsPerDay: Number(tri.cap) } }, "triage"); }} >
local sweep
API key
{policy.keyFile.path} {policy.keyFile.exists ? "file found" : "no file"} {policy.apiKey.from === "env" && ANTHROPIC_API_KEY is set} {!policy.apiKey.configured && no API key configured: an API route cannot run}
{status && (

{status.text}

)}
); }