okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1import type { MonitorState , Session } from './types' ; 2 3export function chatMetrics ( session :Session | null , monitor :MonitorState | null , configuredContext ?:number , now :number = Date . now ()) { 4const messages = session ?. messages ?? []; 5const replies = messages . filter ( m => m . role === 'assistant' &&! m . timeSkipId ); 6const latest = [ ...replies ]. reverse (). find ( m => m . inputTokens !== undefined ); 7const remote = replies . at ( - 1 )?. remoteModel === true ; 8const working = replies . some ( m => m . streaming ); 9// The Monitor's reading belongs to this chat when it names it, and also while this chat is the one working: a goal or an agent runs under its own 10// session ids, so matching on the chat id alone left the numbers frozen during a goal. 11const active =! remote &&!! session &&!! monitor && ( monitor . context ?. sessionId === session . id || ( working && [ 'reading' , 'thinking' , 'generating' ]. includes ( monitor . phase ))); 12// One source: what the context held after the last reply (the engine's own count for that request); the monitor's live figure only while a reply streams. 13const finished = latest ?( latest . contextUsed ?? latest . inputTokens !+ ( latest . tokens ?? 0 )) :null ; 14const liveUsed = active && Number . isFinite ( monitor ! . context . usedTokens ) && monitor ! . context . usedTokens > 0 ?monitor ! . context . usedTokens :null ; 15const live = replies . some ( m => m . streaming ) || ( active && [ 'reading' , 'thinking' , 'generating' ]. includes ( monitor ! . phase )); 16// While a turn runs, its own figure comes first: the host reports what the context holds after each model request of the turn. The engine's live 17// figure belongs to the request in flight (a small one between two tools read as 1%), and `finished` to an earlier reply. 18const turnUsed = replies . find ( m => m . streaming && Number . isFinite ( m . contextUsed ) && m . contextUsed !> 0 )?. contextUsed ?? null ; 19const used = live ?( turnUsed ?? liveUsed ?? finished ) :( finished ?? liveUsed ); 20const max = remote ?( replies . at ( - 1 )?. contextMax || null ) :configuredContext ?? ( active ?monitor ! . context . maxTokens :latest ?. contextMax ?? null ); 21const streaming = replies . find ( m => m . streaming ); 22// Answer speed: everything the model generated over its decode time. While a reply streams it is that reply's; after it, the LAST reply's own 23// (never an older reply's number under the words "last reply"). 24const lastReply = streaming ?? replies . at ( - 1 ); 25const speed = lastReply ?. tps ?? null , promptSpeed = lastReply ?. promptTps ?? null ; 26const ttfts = replies . flatMap ( m => m . ttftMs === undefined ?[] :[ m . ttftMs ]); 27const hasUsage = session ?. statsAvailable === true ; 28const llmTotal = replies . reduce (( n , m ) => n + ( m . durationMs ?? ( m . streaming ?Math . max ( 0 , now - ( Date . parse ( m . at ) || now )) :0 )), 0 ), thinkTotal = replies . reduce (( n , m ) => n + ( m . reasoningMs ?? ( m . streaming && m . reasoningStartedAt ?Math . max ( 0 , now - m . reasoningStartedAt ) :0 )), 0 ); 29return { streaming :!! streaming , writeMs :Math . max ( 0 , llmTotal - thinkTotal ), estimated :streaming ?. usageSource === 'estimated' , hasReplies :replies . length > 0 , example :!! session ?. showcase || replies . some ( m => m . usageSource === 'demo' ), 30speed :active && [ 'generating' , 'thinking' ]. includes ( monitor ?. phase ?? '' ) && monitor ! . outputTps > 0 ?monitor ! . outputTps :speed , promptSpeed, 31total :session ?. statsAvailable === false ?null :hasUsage ?session . tokensIn + session . tokensOut + ( streaming ?. tokens ?? 0 ) + ( streaming ?. inputTokens ?? 0 ) :replies . every ( m => m . inputTokens !== undefined && m . tokens !== undefined ) ?replies . reduce (( n , m ) => n + m . inputTokens !+ m . tokens ! , 0 ) :null , 32input :hasUsage ?session . tokensIn :null , output :hasUsage ?session . tokensOut :null , 33llmMs :replies . reduce (( n , m ) => n + ( m . durationMs ?? ( m . streaming ?Math . max ( 0 , now - ( Date . parse ( m . at ) || now )) :0 )), 0 ), 34thinkMs :replies . reduce (( n , m ) => n + ( m . reasoningMs ?? ( m . streaming && m . reasoningStartedAt ?Math . max ( 0 , now - m . reasoningStartedAt ) :0 )), 0 ), 35toolMs :replies . reduce (( n , m ) => n + ( m . tools ?? []). reduce (( v , t ) => v + ( t . durationMs ?? 0 ), 0 ), 0 ), 36ttft :ttfts . length ?ttfts . reduce (( n , v ) => n + v , 0 ) / ttfts . length :null , 37 used, max, pct :used !== null && max ?Math . min ( 100 , 100 * used / max ) :null , 38turns :messages . filter ( m => m . role === 'user' ). length , 39steps :messages . reduce (( n , m ) => n + ( m . tools ?. length ?? 0 ), 0 )}; 40} 41export const compactTokens = ( n :number | null ) => n === null ?'—' :new Intl . NumberFormat ( 'en' ,{ notation :'compact' , maximumFractionDigits :1 }). format ( n ); 42export function elapsed ( ms :number ){ if ( ms < 1000 ) return ` ${ Math . round ( ms )} ms` ; const sec = Math . round ( ms / 1000 ); return sec < 60 ?` ${ sec } s` :` ${ Math . floor ( sec / 60 )} m ${ sec % 60 } s` ;}