okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import type { SessionAgent } from './types' ; 2 3const statuses = new Set ([ 'working' , 'completed' , 'failed' , 'queued' , 'cancelled' ]); 4const metric = ( value :unknown , fallback = 0 ) => typeof value === 'number' && Number . isFinite ( value ) && value >= 0 ?value :fallback ; 5 6/** Merge partial agent frames without replacing other agents or their readings. */ 7export function updateAgent ( agents :SessionAgent [], frame :unknown , now = Date . now ()) :SessionAgent [] { 8if ( ! frame || typeof frame !== 'object' ) return agents ; 9const next = frame as Record < string , unknown >; 10if ( typeof next . id !== 'string' || ! next . id . trim ()) return agents ; 11const previous = agents . find ( a => a . id === next . id ); 12const at = metric ( next . updatedAt , now ); 13if ( previous ?. updatedAt && at < previous . updatedAt ) return agents ; 14const previousDuration = ( previous ?. durationMs ?? 0 ) + ( previous ?. status === 'working' && previous . updatedAt ?Math . max ( 0 , at - previous . updatedAt ) :0 ); 15const agent :SessionAgent = { 16id :next . id , 17name :typeof next . name === 'string' ?next . name :previous ?. name ?? 'Subagent' , 18description :typeof next . description === 'string' ?next . description :previous ?. description ?? '' , 19status :statuses . has ( String ( next . status )) ?next . status as SessionAgent [ 'status' ] :previous ?. status ?? 'queued' , 20durationMs :metric ( next . durationMs , previousDuration ), 21tokens :metric ( next . tokens , previous ?. tokens ), 22updatedAt :at , 23}; 24return previous ?agents . map ( a => a . id === agent . id ?agent :a ) :[ ...agents , agent ]; 25} 26 27export const runningAgents = ( agents :SessionAgent []) => agents . filter ( a => a . status === 'working' ). length ; 28 29/** A page that was closed or reloaded holds no live helper: a saved row still "working" is shown as stopped until a newer frame says otherwise 30* (updateAgent takes any frame newer than the row's own time, so a helper that really runs comes back by itself). #4218. */ 31export const settleAgents = ( agents :SessionAgent [] | undefined ) :SessionAgent [] | undefined => 32agents ?. some ( a => a . status === 'working' ) ?agents . map ( a => a . status === 'working' ?{ ...a , status :'cancelled' as const } :a ) :agents ;