okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
656478c68
main
1import { tr } from '../lib/i18n' ; 2import { useEffect , useRef , useState , type ReactNode } from 'react' ; 3import { Icon , type IconName } from './ui' ; 4import { contextTokens } from '../lib/experience' ; 5import { ContextRing } from './ContextRing' ; 6import { Popover } from './Popover' ; 7import { chatMetrics , compactTokens , elapsed } from '../lib/chat-metrics' ; 8import { useStore } from '../lib/store' ; 9import { Num } from './Num' ; 10import { useNative } from '../lib/native' ; 11import type { Session } from '../lib/types' ; 12function Chip ({ icon, visual, label, live, title, children} :{ 13icon ?:IconName ; 14/** The same words with the number as a moving element (the plain `label` stays for assistive technology). */ 15live ?:ReactNode ; 16visual ?:ReactNode ; 17label :string ; 18title :string ; 19children :ReactNode ; 20}) { 21const [ open , setOpen ] = useState ( false ); 22const ref = useRef < HTMLButtonElement >( null ); 23return <>< button ref = { ref } className = "telemetry__chip" aria-label = { tr ( "{0} \u2014 show details" , [ title ])} aria-description = { tr ( label )} aria-haspopup = "dialog" aria-expanded = { open } onClick = {() => setOpen ( v => ! v )}>{ tr ( visual ?? ( icon ?< Icon name = { icon } size = { 14 }/> :null ))}{ live ?? tr ( label )}</ button >{ tr ( open ?< Popover anchor = { ref . current } title = { tr ( title )} onClose = {() => setOpen ( false )} above >{ tr ( children )}</ Popover > :null )}</>; 24} 25function Row ({ label, value} :{ 26label :string ; 27value :ReactNode ; 28}) { return < div className = "telepop__row" >< span className = "telepop__label" >{ tr ( label )}</ span >< span className = "telepop__value num" >{ tr ( value )}</ span ></ div >; } 29export function ChatTelemetry ({ session} :{ 30session :Session | null ; 31}) { 32const monitor = useStore ( s => s . monitor ); 33const engine = useStore ( s => s . engine ); 34// The context shown is the one the loaded model runs with (the engine's own --max-context), not a catalog or settings default. 35const native = useNative (); 36const running = native ?. instances . find ( i => i . id === native . defaultId && i . status === 'ready' ) ?? native ?. instances . find ( i => i . status === 'ready' ); 37const runningContext = running ?Number ( running . metrics ?. engine ?. max_context ) || running . context || undefined :undefined ; 38// While a reply is being written the durations keep counting once a second; the readings come from the Monitor's own stream. 39const [ now , setNow ] = useState (() => Date . now ()); 40const working = !! session ?. messages . some ( x => x . streaming ); 41useEffect (() => { if ( ! working ) return ; setNow ( Date . now ()); const t = setInterval (() => setNow ( Date . now ()), 1000 ); return () => clearInterval ( t ); }, [ working ]); 42const m = chatMetrics ( session , monitor , runningContext ?? ( engine ?contextTokens ( engine . beginner . contextLength ) :undefined ), now ); 43const speed = m . speed ; 44if ( ! m . hasReplies ) 45return null ; 46const int = ( n :number ) => String ( Math . round ( n )); 47const PLACE = '' ; 48/** A translated sentence with its number as a moving element: the words around the number keep the language's own order. */ 49const around = ( template :string , before :string , node :ReactNode ) => { const [ head , tail = '' ] = tr ( template , [ before , PLACE ]). split ( PLACE ); return <>{ head }{ node }{ tail }</>; }; 50const approx = m . estimated ?tr ( '≈ ' ) :tr ( '' ); 51const dur = ( n :number ) => elapsed ( n ); 52return < div className = "telemetry xs muted" aria-label = { tr ( "Chat statistics" )}> 53< Chip icon = "gauge" label = { tr ( "{0}{1} tok/s{2}" , [ approx , speed === null ?tr ( '—' ) :Math . round ( speed ), m . streaming ?tr ( '' ) :tr ( ' · last reply' )])} live = { around ( m . streaming ?"{0}{1} tok/s" :"{0}{1} tok/s · last reply" , approx , < Num value = { speed } format = { int }/>)} title = { tr ( "Session statistics" )}>{ tr ( m . estimated ?< p className = "xs muted" >{ tr ( "Live estimate from response length and elapsed time. Final usage comes from the engine." )}</ p > :null )}< Row label = { tr ( "Thinking time" )} value = {< Num value = { m . thinkMs } format = { dur }/>}/>< Row label = { tr ( "Writing time" )} value = {< Num value = { m . writeMs } format = { dur }/>}/>< Row label = { tr ( "Tool time" )} value = {< Num value = { m . toolMs } format = { dur }/>}/>< Row label = { tr ( "Average time to first token" )} value = { m . ttft === null ?tr ( 'Not reported' ) :< Num value = { m . ttft } format = { dur }/>}/>< Row label = { tr ( "Latest response speed" )} value = { speed === null ?tr ( 'Not reported' ) :<>{< Num value = { speed } format = { int }/>}{ tr ( ' tokens/s' )}</>}/>< Row label = { tr ( "Prompt reading" )} value = { m . promptSpeed === null ?tr ( 'Not reported' ) :<>{< Num value = { m . promptSpeed } format = { int }/>}{ tr ( ' tokens/s' )}</>}/>< Row label = { tr ( "Turns / tool calls" )} value = {<>< Num value = { m . turns } format = { int }/> / < Num value = { m . steps } format = { int }/></>}/></ Chip > 54< Chip icon = "memory" label = { tr ( "{0}{1} total tokens" , [ approx , compactTokens ( m . total )])} live = { around ( "{0}{1} total tokens" , approx , < Num value = { m . total } format = { compactTokens }/>)} title = { tr ( "Token usage" )}>< Row label = { tr ( "Total in this chat" )} value = {< Num value = { m . total } format = { compactTokens }/>}/>< Row label = { tr ( "Input tokens" )} value = {< Num value = { m . input } format = { compactTokens }/>}/>< Row label = { tr ( "Output tokens" )} value = {< Num value = { m . output } format = { compactTokens }/>}/></ Chip > 55< Chip visual = {< ContextRing percent = { m . pct }/>} label = { tr ( m . pct === null ?'—' :` ${ Math . round ( m . pct )} % of context` )} live = { m . pct === null ?undefined :<>< Num value = { m . pct } format = { int }/>{ tr ( '% of context' )}</>} title = { tr ( "Context length" )}>< Row label = { tr ( "Context used" )} value = { m . pct === null ?tr ( 'Not reported' ) :<>< Num value = { m . pct } format = { int }/> % </>}/>< div className = "context-meter" role = "meter" aria-label = { tr ( "Context window used" )} aria-valuemin = { 0 } aria-valuemax = { 100 } aria-valuenow = { m . pct ?? undefined }>< span style = {{ width :` ${ m . pct ?? 0 } %` }}/></ div >< Row label = { tr ( "Current context" )} value = {< Num value = { m . used } format = { compactTokens }/>}/>< Row label = { tr ( "Context limit" )} value = {< Num value = { m . max } format = { compactTokens }/>}/>< Row label = { tr ( "Space remaining" )} value = { m . used !== null && m . max !== null ?< Num value = { Math . max ( 0 , m . max - m . used )} format = { compactTokens }/> :'—' }/></ Chip > 56</ div >; 57}