- A/B with betterbench ab between two deployments of the same model, with verdict, confidence interval and caveats. - Sweeps vary one or two deployment settings, deploy, benchmark and remove each variant, and compare them in one table. - Every run records CPU, RAM, VRAM, GPU utilization and power. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
374 lines
16 KiB
TypeScript
374 lines
16 KiB
TypeScript
"use client";
|
||
|
||
import { ExternalLinkIcon, PencilIcon, PlayIcon, SendIcon, SquareIcon, Trash2Icon } from "lucide-react";
|
||
import Link from "next/link";
|
||
import { useRouter } from "next/navigation";
|
||
import { useEffect, useState } from "react";
|
||
import { CopyButton } from "@/components/copy-button";
|
||
import { DeploymentStateBadge } from "@/components/deployment-state";
|
||
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 { Progress } from "@/components/ui/progress";
|
||
import { useHostname, webUiUrl } from "@/hooks/use-hostname";
|
||
import { ENGINE_PORT } from "@/lib/deploy-command";
|
||
import { ACCELERATOR_LABEL } from "@/lib/runtimes-shared";
|
||
import type { DeploymentRow } from "@/lib/deployments";
|
||
import { formatBytes, percent } from "@/lib/format";
|
||
import { quantGroup } from "@/lib/selection";
|
||
import { cn } from "@/lib/utils";
|
||
import { deleteDeployment, startDeployment, stopDeployment } from "../actions";
|
||
import type { ListRow } from "../../benchmarks/benchmark-list";
|
||
import { BenchmarkCard } from "./benchmark-card";
|
||
|
||
type Deployment = Omit<DeploymentRow, "startedAt" | "readyAt" | "stoppedAt" | "createdAt"> & {
|
||
startedAt: string | null;
|
||
readyAt: string | null;
|
||
stoppedAt: string | null;
|
||
createdAt: string;
|
||
};
|
||
|
||
function Stat({ label, value, children }: { label: string; value: React.ReactNode; children?: React.ReactNode }) {
|
||
return (
|
||
<div className="flex flex-col gap-1">
|
||
<span className="text-xs text-muted-foreground">{label}</span>
|
||
<span className="text-lg font-semibold tabular-nums">{value}</span>
|
||
{children}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export function DeploymentDetail({ initial, benchmarks }: { initial: Deployment; benchmarks: ListRow[] }) {
|
||
const router = useRouter();
|
||
const [d, setD] = useState(initial);
|
||
const [now, setNow] = useState(() => Date.now());
|
||
const active = d.desiredState === "running" || d.state === "starting" || d.state === "running" || d.state === "stopping";
|
||
|
||
useEffect(() => {
|
||
const t = setInterval(async () => {
|
||
setNow(Date.now());
|
||
const res = await fetch(`/api/deployments/${d.id}`, { cache: "no-store" }).catch(() => null);
|
||
if (res?.status === 404) router.push("/deployments");
|
||
else if (res?.ok) setD(await res.json());
|
||
}, 2_000);
|
||
return () => clearInterval(t);
|
||
}, [d.id, router]);
|
||
|
||
const file = d.options.modelFile ? (quantGroup(d.options.modelFile) ?? d.options.modelFile) : null;
|
||
const loadSecs = d.readyAt && d.startedAt ? (new Date(d.readyAt).getTime() - new Date(d.startedAt).getTime()) / 1000 : null;
|
||
const startingSecs = d.state === "starting" && d.startedAt ? Math.max(0, (now - new Date(d.startedAt).getTime()) / 1000) : null;
|
||
const port = ENGINE_PORT[d.engine];
|
||
const internal = d.containerName ? `http://${d.containerName}:${port}/v1` : null;
|
||
const hostname = useHostname();
|
||
const published = d.hostPort && hostname ? `http://${hostname}:${d.hostPort}/v1` : null;
|
||
const webUi = webUiUrl(d, hostname);
|
||
|
||
return (
|
||
<div className="flex flex-col gap-4">
|
||
<Card>
|
||
<CardContent className="flex flex-wrap items-start justify-between gap-4">
|
||
<div className="flex min-w-0 flex-col gap-1">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<h1 className="text-xl font-semibold">{d.name}</h1>
|
||
<DeploymentStateBadge state={d.state} desiredState={d.desiredState} />
|
||
</div>
|
||
<p className="text-sm text-muted-foreground">
|
||
<Link href={`/${d.repoId}`} className="hover:underline">
|
||
{d.repoId}
|
||
</Link>
|
||
{file && ` · ${file}`}
|
||
{d.options.mmproj && " · vision"}
|
||
{d.options.draftFile && ` · ${d.options.draftType === "draft-mtp" ? "MTP" : "draft"}`} · {d.runtimeName}
|
||
</p>
|
||
<code className="font-mono text-xs text-muted-foreground">{d.runtimeImage}</code>
|
||
</div>
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
{webUi && (
|
||
<a
|
||
href={webUi}
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
className={cn(buttonVariants({ variant: "outline" }), d.state !== "running" && "pointer-events-none opacity-50")}
|
||
aria-disabled={d.state !== "running"}
|
||
>
|
||
<ExternalLinkIcon />
|
||
Web UI
|
||
</a>
|
||
)}
|
||
{active ? (
|
||
<Button variant="outline" onClick={() => stopDeployment(d.id)}>
|
||
<SquareIcon />
|
||
Stop
|
||
</Button>
|
||
) : (
|
||
<Button onClick={() => startDeployment(d.id)}>
|
||
<PlayIcon />
|
||
Start
|
||
</Button>
|
||
)}
|
||
<Link
|
||
href={`/deployments/${d.id}/edit`}
|
||
className={cn(buttonVariants({ variant: "outline" }), active && "pointer-events-none opacity-50")}
|
||
aria-disabled={active}
|
||
title={active ? "Stop it first" : undefined}
|
||
>
|
||
<PencilIcon />
|
||
Edit
|
||
</Link>
|
||
<AlertDialog>
|
||
<AlertDialogTrigger render={<Button variant="outline" className="text-destructive hover:bg-destructive/10 hover:text-destructive" />}>
|
||
<Trash2Icon />
|
||
Delete
|
||
</AlertDialogTrigger>
|
||
<AlertDialogContent>
|
||
<AlertDialogHeader>
|
||
<AlertDialogTitle>Delete {d.name}?</AlertDialogTitle>
|
||
<AlertDialogDescription>The container is stopped and removed. The model files stay in the library.</AlertDialogDescription>
|
||
</AlertDialogHeader>
|
||
<AlertDialogFooter>
|
||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||
<AlertDialogAction
|
||
variant="destructive"
|
||
onClick={async () => {
|
||
if (await deleteDeployment(d.id)) router.push("/deployments");
|
||
}}
|
||
>
|
||
Delete
|
||
</AlertDialogAction>
|
||
</AlertDialogFooter>
|
||
</AlertDialogContent>
|
||
</AlertDialog>
|
||
</div>
|
||
</CardContent>
|
||
</Card>
|
||
|
||
{d.state === "failed" && d.error && (
|
||
<pre className="max-h-72 overflow-auto rounded-xl bg-destructive/10 px-4 py-3 text-xs whitespace-pre-wrap text-destructive">{d.error}</pre>
|
||
)}
|
||
|
||
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_380px]">
|
||
<div className="flex min-w-0 flex-col gap-4">
|
||
<Card>
|
||
<CardContent className="grid grid-cols-2 gap-4 md:grid-cols-4">
|
||
<Stat
|
||
label="CPU"
|
||
value={d.state === "running" && d.stats ? `${(d.stats.cpuPercent / 100).toFixed(1)} cores` : "–"}
|
||
>
|
||
<span className="text-xs text-muted-foreground">{d.cpus ? `limit ${d.cpus}` : "no limit"}</span>
|
||
</Stat>
|
||
<Stat label="Memory" value={d.stats && d.state === "running" ? formatBytes(d.stats.memoryBytes) : "–"}>
|
||
{d.stats && d.state === "running" && d.memoryMb ? (
|
||
<Progress value={percent(d.stats.memoryBytes, d.memoryMb * 2 ** 20)} />
|
||
) : (
|
||
<span className="text-xs text-muted-foreground">{d.memoryMb ? `limit ${formatBytes(d.memoryMb * 2 ** 20)}` : "no limit"}</span>
|
||
)}
|
||
</Stat>
|
||
<Stat
|
||
label={d.state === "starting" ? "Loading" : "Load time"}
|
||
value={startingSecs != null ? `${startingSecs.toFixed(0)} s` : loadSecs != null ? `${loadSecs.toFixed(1)} s` : "–"}
|
||
>
|
||
<span className="text-xs text-muted-foreground">until /health answered</span>
|
||
</Stat>
|
||
<Stat label="Context" value={d.options.contextSize.toLocaleString("en-US")}>
|
||
<span className="text-xs text-muted-foreground">
|
||
{d.engine === "vllm" ? `seqs ${d.options.maxNumSeqs ?? "default"}` : `slots ${d.options.parallel ?? "auto"}`}
|
||
</span>
|
||
</Stat>
|
||
</CardContent>
|
||
</Card>
|
||
|
||
<LiveLog url={`/api/deployments/${d.id}/logs`} />
|
||
</div>
|
||
|
||
<div className="flex flex-col gap-4">
|
||
<Chat id={d.id} running={d.state === "running"} />
|
||
<BenchmarkCard deploymentId={d.id} modelId={d.modelId} running={d.state === "running"} initial={benchmarks} />
|
||
<Card size="sm">
|
||
<CardHeader>
|
||
<CardTitle>Endpoint</CardTitle>
|
||
</CardHeader>
|
||
<CardContent className="flex flex-col gap-2 text-xs">
|
||
{internal ? (
|
||
<div className="flex items-center justify-between gap-2">
|
||
<code className="font-mono break-all">{internal}</code>
|
||
<CopyButton value={internal} label="Copy URL" />
|
||
</div>
|
||
) : (
|
||
<span className="text-muted-foreground">Not started yet.</span>
|
||
)}
|
||
<span className="text-muted-foreground">On the Docker network, for Kaname and other containers.</span>
|
||
{published && (
|
||
<div className="flex items-center justify-between gap-2 border-t pt-2">
|
||
<code className="font-mono break-all">{published}</code>
|
||
<CopyButton value={published} label="Copy URL" />
|
||
</div>
|
||
)}
|
||
<span className="text-muted-foreground">
|
||
OpenAI-compatible; model name <code className="font-mono">{d.repoId}</code>.
|
||
{d.options.requireApiKey && (
|
||
<>
|
||
{" "}
|
||
Requires one of the{" "}
|
||
<Link href="/settings/api-keys" className="underline">
|
||
API keys
|
||
</Link>{" "}
|
||
as <code className="font-mono">Authorization: Bearer</code>.
|
||
</>
|
||
)}
|
||
</span>
|
||
</CardContent>
|
||
</Card>
|
||
<Card size="sm">
|
||
<CardHeader>
|
||
<CardTitle>Details</CardTitle>
|
||
</CardHeader>
|
||
<CardContent>
|
||
<dl className="grid grid-cols-[max-content_1fr] gap-x-4 gap-y-1 text-xs">
|
||
<dt className="text-muted-foreground">Container</dt>
|
||
<dd className="font-mono break-all">{d.containerName ?? "–"}</dd>
|
||
<dt className="text-muted-foreground">Runs on</dt>
|
||
<dd>
|
||
{ACCELERATOR_LABEL[d.accelerator]}
|
||
{d.accelerator === "nvidia" && ` · ${d.options.gpus?.length ? `${d.options.gpus.length} selected GPU${d.options.gpus.length === 1 ? "" : "s"}` : "all GPUs"}`}
|
||
</dd>
|
||
<dt className="text-muted-foreground">Started</dt>
|
||
<dd>
|
||
<LocalTime value={d.startedAt} />
|
||
</dd>
|
||
<dt className="text-muted-foreground">Stopped</dt>
|
||
<dd>
|
||
<LocalTime value={d.stoppedAt} />
|
||
</dd>
|
||
{d.command && (
|
||
<>
|
||
<dt className="text-muted-foreground">Arguments</dt>
|
||
<dd className="flex items-start gap-1">
|
||
<code className="font-mono break-all">{d.command.join(" ")}</code>
|
||
<CopyButton value={d.command.join(" ")} label="Copy arguments" />
|
||
</dd>
|
||
</>
|
||
)}
|
||
</dl>
|
||
</CardContent>
|
||
</Card>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
/** Live container log (polls new lines every 1.5 s). */
|
||
/** Smoke test over the OpenAI API: streams the answer and measures time to first token and speed. */
|
||
function Chat({ id, running }: { id: string; running: boolean }) {
|
||
const [prompt, setPrompt] = useState("Write one sentence about the sea.");
|
||
const [answer, setAnswer] = useState("");
|
||
const [busy, setBusy] = useState(false);
|
||
const [stats, setStats] = useState<{ ttft: number | null; total: number; tokens: number | null; promptTokens: number | null } | null>(null);
|
||
const [error, setError] = useState<string | null>(null);
|
||
|
||
async function send() {
|
||
setBusy(true);
|
||
setAnswer("");
|
||
setError(null);
|
||
setStats(null);
|
||
const t0 = performance.now();
|
||
let first: number | null = null;
|
||
let usage: { completion_tokens?: number; prompt_tokens?: number } | null = null;
|
||
let chunks = 0;
|
||
try {
|
||
const res = await fetch(`/api/deployments/${id}/chat`, {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ messages: [{ role: "user", content: prompt }], maxTokens: 256 }),
|
||
});
|
||
if (!res.ok || !res.body) {
|
||
const body = await res.json().catch(() => ({}));
|
||
throw new Error(body.error?.message ?? `HTTP ${res.status}`);
|
||
}
|
||
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
|
||
let buf = "";
|
||
for (;;) {
|
||
const { value, done } = await reader.read();
|
||
if (done) break;
|
||
buf += value;
|
||
let nl: number;
|
||
while ((nl = buf.indexOf("\n")) >= 0) {
|
||
const line = buf.slice(0, nl).trim();
|
||
buf = buf.slice(nl + 1);
|
||
if (!line.startsWith("data:")) continue;
|
||
const data = line.slice(5).trim();
|
||
if (data === "[DONE]") continue;
|
||
const j = JSON.parse(data);
|
||
if (j.usage) usage = j.usage;
|
||
const delta: string = j.choices?.[0]?.delta?.content ?? j.choices?.[0]?.delta?.reasoning_content ?? "";
|
||
if (delta) {
|
||
first ??= performance.now();
|
||
chunks++;
|
||
setAnswer((a) => a + delta);
|
||
}
|
||
}
|
||
}
|
||
const total = (performance.now() - t0) / 1000;
|
||
setStats({ ttft: first ? (first - t0) / 1000 : null, total, tokens: usage?.completion_tokens ?? chunks, promptTokens: usage?.prompt_tokens ?? null });
|
||
} catch (err) {
|
||
setError(err instanceof Error ? err.message : String(err));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
const genSecs = stats && stats.ttft != null ? stats.total - stats.ttft : null;
|
||
return (
|
||
<Card size="sm">
|
||
<CardHeader>
|
||
<CardTitle>Try it</CardTitle>
|
||
</CardHeader>
|
||
<CardContent className="flex flex-col gap-2">
|
||
<textarea
|
||
value={prompt}
|
||
onChange={(e) => setPrompt(e.target.value)}
|
||
rows={3}
|
||
disabled={!running}
|
||
className="w-full rounded-lg border border-input bg-transparent px-2.5 py-2 text-sm outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:opacity-50 dark:bg-input/30"
|
||
/>
|
||
<Button size="sm" onClick={send} disabled={!running || busy || !prompt.trim()} className="self-end">
|
||
<SendIcon />
|
||
{busy ? "Generating…" : running ? "Send" : "Start the deployment first"}
|
||
</Button>
|
||
{error && <p className="rounded-md bg-destructive/10 px-2.5 py-2 text-xs text-destructive">{error}</p>}
|
||
{answer && <div className="max-h-60 overflow-auto rounded-md bg-muted/50 px-2.5 py-2 text-sm whitespace-pre-wrap">{answer}</div>}
|
||
{stats && (
|
||
<div className="grid grid-cols-3 gap-2 text-center text-xs">
|
||
<div>
|
||
<div className="font-semibold tabular-nums">{stats.ttft != null ? `${(stats.ttft * 1000).toFixed(0)} ms` : "–"}</div>
|
||
<div className="text-muted-foreground">first token</div>
|
||
</div>
|
||
<div>
|
||
<div className="font-semibold tabular-nums">
|
||
{stats.tokens && genSecs && genSecs > 0 ? `${(stats.tokens / genSecs).toFixed(1)} tok/s` : "–"}
|
||
</div>
|
||
<div className="text-muted-foreground">generation</div>
|
||
</div>
|
||
<div>
|
||
<div className="font-semibold tabular-nums">{stats.tokens ?? "–"}</div>
|
||
<div className="text-muted-foreground">tokens in {stats.total.toFixed(1)} s</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</CardContent>
|
||
</Card>
|
||
);
|
||
}
|