import { useEffect, useState } from "react" import { Activity, Gauge, HardDrive, Timer } from "lucide-react" import { getProfile, type ProfileTurn } from "@/lib/api" import { useLocale } from "./i18n" const PHASES = [ { key: "expert_wait_s", i18n: "profile.ioWait", color: "#3987e5" }, { key: "expert_matmul_s", i18n: "profile.expertMatmul", color: "#199e70" }, { key: "attention_s", i18n: "profile.attention", color: "#c98500" }, { key: "lm_head_s", i18n: "profile.lmHead", color: "#008300" }, { key: "other_s", i18n: "profile.other", color: "#9085e9" }, ] as const interface Turn extends ProfileTurn { other_s: number; toks: number } const derive = (turn: ProfileTurn): Turn => ({ ...turn, other_s: Math.max(0, turn.wall_s - turn.expert_wait_s - turn.expert_matmul_s - turn.attention_s - turn.lm_head_s), toks: turn.wall_s > 0 ? turn.completion_tokens / turn.wall_s : 0, }) const seconds = (value: number) => (value >= 10 ? value.toFixed(1) : value.toFixed(2)) + "s" function ShareBar({ label, turns }: { label: string; turns: Turn[] }) { const { t } = useLocale() const total = turns.reduce((sum, turn) => sum + turn.wall_s, 0) const parts = PHASES.map((phase) => ({ ...phase, name: t(phase.i18n), value: turns.reduce((sum, turn) => sum + turn[phase.key], 0) })) return (
{label}{seconds(total)}
`${part.name} ${seconds(part.value)}`).join(", ")}> {parts.map((part) => { const share = total > 0 ? part.value / total : 0 return share > 0.001 ? ( {share >= 0.09 ? `${Math.round(100 * share)}%` : ""} ) : null })}
) } function TurnColumns({ turns, stacked, height, format, footLabel, footLabelOne }: { turns: Turn[]; stacked: boolean; height: number; format: (turn: Turn) => string; footLabel: string; footLabelOne: string }) { const [hover, setHover] = useState(null) const peak = Math.max(...turns.map((turn) => (stacked ? turn.wall_s : turn.toks)), 1e-9) const gap = 2 const width = Math.max(1, (100 - gap * (turns.length - 1)) / turns.length) return (
setHover(null)}>
{turns.length > 1 ? footLabel : footLabelOne} {hover !== null && turns[hover] ? format(turns[hover]) : `peak ${stacked ? seconds(peak) : peak.toFixed(1) + " tok/s"}`}
) } export function Profiling({ baseUrl, apiKey, connected }: { baseUrl: string; apiKey: string; connected: boolean }) { const { t } = useLocale() const [turns, setTurns] = useState([]) useEffect(() => { if (!connected) return let disposed = false const poll = async () => { if (document.visibilityState === "hidden") return try { const result = await getProfile(baseUrl, apiKey) if (!disposed) setTurns(result.turns.map(derive)) } catch { /* engine busy or restarting — keep the last snapshot */ } } void poll() const timer = window.setInterval(() => void poll(), 2000) return () => { disposed = true; window.clearInterval(timer) } }, [baseUrl, apiKey, connected]) const latest = turns[turns.length - 1] const recent = turns.slice(-40) const diskService = turns.reduce((sum, turn) => sum + turn.expert_disk_s, 0) return (
{t("profile.title")}
{PHASES.map((phase) => {t(phase.i18n)})}
{!latest ? (

{connected ? t("profile.empty") : t("profile.connectHint")}

) : ( <>
{t("profile.lastTurn")}{latest.toks.toFixed(1)}tok/s
{t("profile.wallTime")}{seconds(latest.wall_s)}{latest.prompt_tokens} → {latest.completion_tokens} tokens
{t("profile.batching")}{latest.forwards > 0 ? (latest.completion_tokens / latest.forwards).toFixed(2) : "—"}{t("profile.tokensPerForward")}
{t("profile.diskService")}{seconds(latest.expert_disk_s)}{t("profile.overlapped")}
{turns.length > 1 ? : null}
{t("profile.throughputTitle")}
`${turn.toks.toFixed(1)} tok/s · ${turn.completion_tokens} tokens`} />
{t("profile.phaseTitle")}
`${seconds(turn.wall_s)} · ${PHASES.map((phase) => `${t(phase.i18n)} ${seconds(turn[phase.key])}`).join(" · ")}`} />
{PHASES.map((phase) => )} {recent.slice().reverse().map((turn, index) => ( {PHASES.map((phase) => )} ))}
{t("profile.turnCol")}{t("profile.tokensCol")}tok/s{t("profile.wallCol")}{t(phase.i18n)}{t("profile.diskService")}
{turns.length - index} {turn.prompt_tokens} → {turn.completion_tokens} {turn.toks.toFixed(1)} {seconds(turn.wall_s)}{seconds(turn[phase.key])}{seconds(turn.expert_disk_s)}
{diskService > 0 ?

{t("profile.diskNote")}

: null}
)}
) }