"use client"; import { DownloadIcon, ExternalLinkIcon, RotateCcwIcon, SquareIcon, Trash2Icon, TriangleAlertIcon, } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { BenchmarkStateBadge } from "@/components/benchmark-state"; import { CopyButton } from "@/components/copy-button"; import { LiveLog } from "@/components/live-log"; import { LocalTime } from "@/components/local-time"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Button, buttonVariants } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { reportSummary, weightsLabel } from "@/lib/benchmark-report"; import { CONFIG_ROWS, describeSettings, engineName, isEndpointSnapshot, modelName, targetName, } from "@/lib/benchmark-view"; import type { RunRow } from "../benchmark-list"; import { PageHero } from "@/components/hero/page-hero"; import { RunMetaCard } from "@/components/run-meta"; import { cancelBenchmark, deleteBenchmark, describeBenchmark, renameBenchmark, } from "../actions"; import { AbReport } from "./ab-report"; import { QualityReport } from "./quality-report"; import { BenchmarkReport } from "./benchmark-report"; import { PublishCard } from "./publish-card"; function duration(a: string | null, b: string | null, now: number) { if (!a) return null; const s = Math.max( 0, ((b ? new Date(b).getTime() : now) - new Date(a).getTime()) / 1000, ); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); return h ? `${h} h ${m} min` : m ? `${m} min ${Math.round(s % 60)} s` : `${Math.round(s)} s`; } export function BenchmarkDetail({ initial }: { initial: RunRow }) { const router = useRouter(); const [r, setR] = useState(initial); const [now, setNow] = useState(() => Date.now()); const [label, setLabel] = useState(initial.label ?? ""); const [description, setDescription] = useState(initial.description ?? ""); const active = r.state === "queued" || r.state === "running"; useEffect(() => { if (!active) return; const t = setInterval(async () => { setNow(Date.now()); const res = await fetch(`/api/benchmarks/${r.id}`, { cache: "no-store", }).catch(() => null); if (res?.ok) setR(await res.json()); }, 2_000); return () => clearInterval(t); }, [active, r.id]); async function again() { if (!r.endpointId) return; const res = await fetch("/api/benchmarks", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ endpointId: r.endpointId, endpointBId: r.kind === "ab" ? r.endpointBId : null, model: modelName(r.snapshot), kind: r.kind, label: r.label, description: r.description, ...r.settings, }), }); const json = await res.json().catch(() => ({})); if (!res.ok) return toast.error( json.error?.message ?? `Could not start (${res.status})`, ); router.push(`/benchmarks/${json.id}`); } const s = r.snapshot; const sB = r.snapshotB; const sum = r.summary; // Runs from before 0.15 measured a deployment (gone now); newer ones an endpoint. const legacy = !isEndpointSnapshot(s); const endpointLink = (id: string | null, name: string) => legacy ? ( name ) : id ? ( {name} ) : ( `${name} (deleted)` ); const took = duration(r.startedAt, r.finishedAt, now); return ( {r.label ?? (r.kind === "ab" && sB ? `${targetName(s)} vs ${targetName(sB)}` : targetName(s))} {r.kind === "ab" && ( A/B )} } description={

{endpointLink(r.endpointId, targetName(s))} ·{" "} {r.modelId ? ( {modelName(s)} ) : ( modelName(s) )}{" "} · {engineName(s)} {r.kind === "ab" && sB && ( <> {" "} · B: {endpointLink(r.endpointBId, targetName(sB))} ( {engineName(sB)}) )}

{r.kind === "eval" ? "Kaname" : "BetterBench"} ·{" "} {r.kind === "ab" ? `paired A/B, target ±${r.settings.mdePct ?? 1}%, up to ${r.settings.maxPairs ?? 200} pairs` : describeSettings(r.settings)}{" "} · created {took && ` · ${r.state === "running" ? "running for" : "took"} ${took}`}

} actions={ <> {r.hasReport && ( Full report )} {r.state === "completed" && ( {r.kind === "ab" ? "ab.json" : "results.json"} )} {active ? ( ) : ( r.endpointId && (r.kind !== "ab" || r.endpointBId) && ( ) )} } > Delete Delete this benchmark? Its numbers, log, results.json and HTML report are removed. Cancel { if (await deleteBenchmark(r.id)) { router.push("/benchmarks"); router.refresh(); } }} > Delete } > {r.error && (
            {r.error}
          
)} {r.state === "queued" && (

Waiting: benchmarks run one at a time.

)} {r.state === "running" && r.phase && (

Now: {r.phase}

)}
{r.kind === "ab" ? ( ) : r.kind === "eval" ? ( ) : ( )}