import { tr } from '../lib/i18n'; import { useEffect, useRef, useState, type ReactNode } from 'react'; import { Icon, type IconName } from './ui'; import { contextTokens } from '../lib/experience'; import { ContextRing } from './ContextRing'; import { Popover } from './Popover'; import { chatMetrics, compactTokens, elapsed } from '../lib/chat-metrics'; import { useStore } from '../lib/store'; import { Num } from './Num'; import { useNative } from '../lib/native'; import type { Session } from '../lib/types'; function Chip({ icon, visual, label, live, title, children }: { icon?: IconName; /** The same words with the number as a moving element (the plain `label` stays for assistive technology). */ live?: ReactNode; visual?: ReactNode; label: string; title: string; children: ReactNode; }) { const [open, setOpen] = useState(false); const ref = useRef(null); return <>{tr(open ? setOpen(false)} above>{tr(children)} : null)}; } function Row({ label, value }: { label: string; value: ReactNode; }) { return
{tr(label)}{tr(value)}
; } export function ChatTelemetry({ session }: { session: Session | null; }) { const monitor = useStore(s => s.monitor); const engine = useStore(s => s.engine); // The context shown is the one the loaded model runs with (the engine's own --max-context), not a catalog or settings default. const native = useNative(); const running = native?.instances.find(i => i.id === native.defaultId && i.status === 'ready') ?? native?.instances.find(i => i.status === 'ready'); const runningContext = running ? Number(running.metrics?.engine?.max_context) || running.context || undefined : undefined; // While a reply is being written the durations keep counting once a second; the readings come from the Monitor's own stream. const [now, setNow] = useState(() => Date.now()); const working = !!session?.messages.some(x => x.streaming); useEffect(() => { if (!working) return; setNow(Date.now()); const t = setInterval(() => setNow(Date.now()), 1000); return () => clearInterval(t); }, [working]); const m = chatMetrics(session, monitor, runningContext ?? (engine ? contextTokens(engine.beginner.contextLength) : undefined), now); const speed = m.speed; if (!m.hasReplies) return null; const int = (n: number) => String(Math.round(n)); const PLACE = ''; /** A translated sentence with its number as a moving element: the words around the number keep the language's own order. */ const around = (template: string, before: string, node: ReactNode) => { const [head, tail = ''] = tr(template, [before, PLACE]).split(PLACE); return <>{head}{node}{tail}; }; const approx = m.estimated ? tr('≈ ') : tr(''); const dur = (n: number) => elapsed(n); return
)} title={tr("Session statistics")}>{tr(m.estimated ?

{tr("Live estimate from response length and elapsed time. Final usage comes from the engine.")}

: null)}}/>}/>}/>}/>{}{tr(' tokens/s')}}/>{}{tr(' tokens/s')}}/> / }/>
)} title={tr("Token usage")}>}/>}/>}/> } label={tr(m.pct === null ? '—' : `${Math.round(m.pct)}% of context`)} live={m.pct === null ? undefined : <>{tr('% of context')}} title={tr("Context length")}>%}/>
}/>}/> : '—'}/>
; }