okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1import type { NativeState } from './native' ; 2import type { Hardware , MonitorState , MonitorSample , GpuReading , ServeRequest } from './types' ; 3import { gpuId , gpuCapacity , hasReading } from './monitor-hardware' ; 4const value = ( n :unknown ) => hasReading ( n ) ?n :null ; 5const bytes = ( n :unknown ) => hasReading ( n ) ?n / 1024 ** 3 :null ; 6const reading = ( n :unknown ) => value ( n ) ?? NaN ; 7const total = ( list :( number | null )[]) => list . length && list . every ( hasReading ) ?list . reduce (( sum , n ) => sum + n , 0 ) :NaN ; 8 9/** Native engine facts projected onto the existing visual dashboard. No demo fallback. */ 10export function nativeMonitorData ( native :NativeState , inventory :Hardware | null , selection = 'all' ) :MonitorState { 11const instances = native . instances , selected = instances . find ( instance => instance . id === selection ) ?? ( instances . length === 1 ?instances [ 0 ] :undefined ); 12const scoped = selected ?[ selected ] :instances ; 13const raw = selected ?. metrics ?? scoped . filter ( instance => instance . metrics ). sort (( a , b ) => ( b . metrics ?. time ?? 0 ) - ( a . metrics ?. time ?? 0 ))[ 0 ]?. metrics ; 14const live = selected ?. metrics ?. live , engine = selected ?. metrics ?. engine ; 15const hardware = instances . filter ( instance => instance . metrics ?. hardware ). sort (( a , b ) => ( b . metrics ?. time ?? 0 ) - ( a . metrics ?. time ?? 0 ))[ 0 ]?. metrics ?. hardware ; 16const devices = new Map < number , Record < string , any >>(); 17for ( const instance of [ ...instances ]. sort (( a , b ) => ( a . metrics ?. time ?? 0 ) - ( b . metrics ?. time ?? 0 ))){ 18const h = instance . metrics ?. hardware ; if ( ! h ) continue ; 19if ( h . gpus ?. length ) for ( const gpu of h . gpus ) devices . set ( gpu . index , gpu ); 20else { const index = instance . gpus . length === 1 ?instance . gpus [ 0 ] :native . hardware ?. gpus . length === 1 ?native . hardware . gpus [ 0 ]. index :null ; if ( index !== null ) devices . set ( index ,{ util :h . gpu_util , mem_used :h . gpu_mem_used , temp :h . gpu_temp , power :h . gpu_power , power_limit :h . gpu_power_limit , pcie_rx_mb :h . gpu_pcie_rx_mb , pcie_gen :h . gpu_pcie_gen , pcie_gen_max :h . gpu_pcie_gen_max , pcie_width :h . gpu_pcie_width });} 21} 22const gpus :GpuReading [] = ( inventory ?. gpus ?? []). map ( gpu => { const r = devices . get ( gpu . index ); return { id :gpuId ( gpu ), utilizationPct :value ( r ?. util ), memoryUsedGb :bytes ( r ?. mem_used ), temperatureC :value ( r ?. temp ), powerW :value ( r ?. power ), powerLimitW :value ( r ?. power_limit ), pcieRxMbs :value ( r ?. pcie_rx_mb ), pcieGen :value ( r ?. pcie_gen ), pcieGenMax :value ( r ?. pcie_gen_max ), pcieWidth :value ( r ?. pcie_width ), source :r ?'sensor' :'unavailable' , sampledAt :new Date (( raw ?. time ?? Date . now () / 1000 ) * 1000 ). toISOString ()};}); 23const requests :ServeRequest [] = scoped . flatMap ( instance => ( instance . metrics ?. requests ?? []). map (( r :Record < string , any >, index :number ) => ({ id :String ( r . id ?? ` ${ instance . id } : ${ r . time } : ${ index } ` ), instanceId :instance . id , at :new Date (( r . time ?? 0 ) * 1000 ). toISOString (), client :'Native engine' , endpoint :'/v1/chat/completions' , state :r . error ?'error' as const :'done' as const , tokensIn :value ( r . prompt_tokens ), tokensOut :value ( r . output_tokens ), cachedTokens :value ( r . reused ), durationMs :reading ( r . duration_s ) * 1000 , model :instance . label , tps :value ( r . decode_tok_s ), promptTps :hasReading ( r . prompt_ms ) && r . prompt_ms > 0 && hasReading ( r . prompt_tokens ) && hasReading ( r . reused ) ?( r . prompt_tokens - r . reused ) / ( r . prompt_ms / 1000 ) :null , usageSource :'reported' as const , finishReason :r . finish_reason }))). sort (( a , b ) => Date . parse ( b . at ) - Date . parse ( a . at )); 24const last = requests [ 0 ], phase = selected ?. phase ?? 'idle' , active = [ 'reading' , 'thinking' , 'generating' ]. includes ( phase ); 25const used = active ?hasReading ( live ?. prompt_tokens ) && hasReading ( live ?. generated ) ?live . prompt_tokens + live . generated :NaN :last && hasReading ( last . tokensIn ) && hasReading ( last . tokensOut ) ?last . tokensIn + last . tokensOut :NaN ; 26const maxContext = reading ( engine ?. max_context ?? selected ?. context ); 27const historyRaw = raw ?. history ?? {}, count = Math . max ( 0 , ...Object . values ( historyRaw ). filter ( Array . isArray ). map ( series => series . length )); 28const at = ( key :string , index :number ) => { const series = historyRaw [ key ]; return Array . isArray ( series ) ?value ( series [ index - ( count - series . length )]) :null ;}; 29const history :MonitorSample [] = Array . from ({ length :count },( _ , index ) => ({ t :(( raw ?. time ?? Date . now () / 1000 ) - ( count - index - 1 )) * 1000 , outputTps :selected ?at ( 'tok_s' , index ) :null , promptTps :selected ?at ( 'prefill_tok_s_mean' , index ) :null , cpuLoad :at ( 'cpu' , index ), ramUsedGb :bytes ( at ( 'ram_used' , index )), gpuLoad :at ( 'gpu_util' , index ), vramUsedGb :bytes ( at ( 'gpu_mem_used' , index )), gpuTempC :at ( 'gpu_temp' , index ), powerW :at ( 'gpu_power' , index ), pcieRxMbs :at ( 'gpu_pcie_rx_mb' , index ), diskReadGbs :hasReading ( at ( 'disk_read_mb' , index )) ?at ( 'disk_read_mb' , index ) !/ 1024 :null })); 30const metricInstance = selected ?? scoped . filter ( instance => instance . metrics ). sort (( a , b ) => ( b . metrics ?. time ?? 0 ) - ( a . metrics ?. time ?? 0 ))[ 0 ]; 31const historyIndices = raw ?. hardware ?. gpus ?. map (( gpu :Record < string , any >) => gpu . index ) ?? ( metricInstance ?. gpus . length ?metricInstance . gpus :native . hardware ?. gpus . length === 1 ?[ native . hardware . gpus [ 0 ]. index ] :[]); 32const historyDevices = ( inventory ?. gpus ?? []). filter ( gpu => historyIndices . includes ( gpu . index )); 33for ( const sample of history ){ 34sample . gpuIds = historyDevices . map ( gpuId ); 35if ( historyDevices . length === 1 ) sample . gpus = [{ id :gpuId ( historyDevices [ 0 ]), source :'sensor' , sampledAt :new Date ( sample . t ). toISOString (), utilizationPct :sample . gpuLoad , memoryUsedGb :sample . vramUsedGb , temperatureC :sample . gpuTempC , powerW :sample . powerW , pcieRxMbs :sample . pcieRxMbs }]; 36} 37const totals = scoped . map ( instance => instance . metrics ?. totals ); 38const tokenTotal = ( key :string ) => total ( totals . map ( item => value ( item ?.[ key ]))); 39return { phase :[ 'idle' , 'reading' , 'thinking' , 'generating' , 'loading' , 'error' ]. includes ( phase ) ?phase as MonitorState [ 'phase' ] :'error' , phaseLabel :selected ?. error ?? ( phase === 'idle' ?'Waiting for a request' :phase === 'reading' ?'Reading your message' :phase === 'generating' ?'Generating a reply' :phase === 'loading' ?'Loading model' :'Model unavailable' ), variantId :selected ?. model ?? null , 40telemetry :{ throughput :raw ?'engine' :'unavailable' , thermals :hardware ?'sensor' :'unavailable' , memory :hardware ?'sensor' :'unavailable' , context :selected && hasReading ( used ) ?'reported' :'unavailable' }, 41prompt :{ done :reading ( live ?. prompt_read ), total :reading ( live ?. prompt_total ), pct :hasReading ( live ?. prompt_read ) && live . prompt_total > 0 ?live . prompt_read / live . prompt_total * 100 :0 }, generation :{ done :value ( live ?. generated ), total :value ( live ?. max_tokens )}, outputTps :reading ( live ?. tok_s ), promptTps :reading ( live ?. prefill_tok_s_mean ), 42queue :{ running :total ( scoped . map ( instance => instance . metrics ?. live ?value ( instance . metrics . live . running ) ?? ([ 'reading' , 'generating' ]. includes ( instance . metrics . live . state ) ?1 :0 ) :null )), waiting :total ( scoped . map ( instance => value ( instance . metrics ?. live ?. queued )))}, 43context :{ usedTokens :used , maxTokens :maxContext , pct :hasReading ( used ) && maxContext > 0 ?used / maxContext * 100 :NaN }, expertCache :{ used :reading ( engine ?. expert_slots ), total :reading ( engine ?. experts ), memoryGb :hasReading ( engine ?. expert_cache_mib ) ?engine . expert_cache_mib / 1024 :null , source :engine && hasReading ( engine . expert_slots ) ?'engine' :'unavailable' }, 44hardware :{ gpus, cpuLoad :value ( hardware ?. cpu_pct ?? hardware ?. cpu ), gpuLoad :value ( hardware ?. gpu_util ), vramUsedGb :bytes ( hardware ?. gpu_mem_used ), vramTotalGb :gpuCapacity ( inventory ?. gpus ?? []), ramUsedGb :bytes ( hardware ?. ram_used ), ramTotalGb :bytes ( hardware ?. ram_total ) ?? inventory ?. memory . totalGb ?? NaN , gpuTempC :value ( hardware ?. gpu_temp ), tempLimitC :90 , powerW :value ( hardware ?. gpu_power ), powerLimitW :value ( hardware ?. gpu_power_limit ), pcieRxMbs :value ( hardware ?. gpu_pcie_rx_mb ), diskReadGbs :hasReading ( hardware ?. disk_read_mb ) ?hardware . disk_read_mb / 1024 :null , diskWriteMbs :value ( hardware ?. disk_write_mb )}, history, requests, 45mcp :raw ?. mcp ?{ servers :raw . mcp . servers ?? [], tools :raw . mcp . tools ?? 0 } :undefined , 46totals :{ tokensIn :tokenTotal ( 'prompt_tokens' ), tokensOut :tokenTotal ( 'output_tokens' ), requests :tokenTotal ( 'requests' ), avgTps :NaN , since :selected ?. loadedAt ?? instances [ 0 ]?. loadedAt ?? new Date (). toISOString (), ttftMs :NaN , cachedTokens :tokenTotal ( 'reused' ), uncachedTokens :tokenTotal ( 'prompt_tokens' ) - tokenTotal ( 'reused' ), promptMs :hasReading ( tokenTotal ( 'prompt_ms' )) ?tokenTotal ( 'prompt_ms' ) :undefined , decodeMs :hasReading ( tokenTotal ( 'decode_ms' )) ?tokenTotal ( 'decode_ms' ) :undefined } 47}; 48} 49 50/** GPU temperature, load, VRAM, power and PCIe come from the engine's metrics while a model runs. With no model loaded (or an engine that 51* reports none of them) the host's own sensors fill in: nvidia-smi, or the AMD/Intel sysfs readings the host probes itself. */ 52export function withHostSensors ( live :MonitorState , host :MonitorState | null ) :MonitorState { 53const s = host ?. hardware , h = live . hardware ; if ( ! s ||! h ) return live ; 54const empty = ( g :GpuReading ) =>! [ g . utilizationPct , g . temperatureC , g . memoryUsedGb , g . powerW ]. some ( hasReading ); 55const pick =< T , > ( a :T , b :T ) => hasReading ( a as unknown ) ?a :b ; 56return { ...live , hardware :{ ...h , 57// The fan and the clock are the host's own readings; the engine's metrics do not carry them (#3628). 58gpus :( h . gpus ?? []). map ( g => { const sensor = s . gpus ?. find ( x => x . id === g . id ); const own = sensor && empty ( g ) ?{ ...g , ...sensor } :g ; return sensor ?{ ...own , fanPct :own . fanPct ?? sensor . fanPct , clockMhz :own . clockMhz ?? sensor . clockMhz } :own ;}), 59cpuTempC :s . cpuTempC , ssdTempC :s . ssdTempC , energyWh :s . energyWh , 60gpuLoad :pick ( h . gpuLoad , s . gpuLoad ), vramUsedGb :pick ( h . vramUsedGb , s . vramUsedGb ), vramTotalGb :pick ( h . vramTotalGb , s . vramTotalGb ), gpuTempC :pick ( h . gpuTempC , s . gpuTempC ), 61powerW :pick ( h . powerW , s . powerW ), powerLimitW :pick ( h . powerLimitW , s . powerLimitW ), pcieRxMbs :pick ( h . pcieRxMbs , s . pcieRxMbs )}}; 62}