okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3d9c21509
main
1import { useSyncExternalStore } from 'react' ; 2import { tr } from './i18n' ; 3 4/** 5* The one transient line under the newest message while the model works (frames from the host: `status`, see notes-split-agent3.md). 6* Only the LATEST frame of a turn is kept; it is removed on `idle`, on `done` and `error`, and when the stream ends, so the line can never 7* show something that stopped being true. 8*/ 9export interface StatusFrame { 10seq :number ; 11phase :string ; 12code :string ; 13text :string ; 14args ?:Record < string , unknown >; 15tokens ?:number ; 16elapsedMs :number ; 17/** When this page received the frame; elapsed time keeps counting from it between frames. */ 18receivedAt :number ; 19} 20 21const frames = new Map < string , StatusFrame >(); 22const listeners = new Set <() => void >(); 23const emit = () => listeners . forEach ( fn => fn ()); 24 25/** Take a status frame for a chat. An older frame (lower `seq`) never replaces a newer one. Returns whether the line changed. */ 26export function applyStatusFrame ( sessionId :string , raw :Record < string , unknown >, now = Date . now ()) :boolean { 27const seq = Number ( raw . seq ); 28const phase = String ( raw . phase ?? '' ); 29const current = frames . get ( sessionId ); 30if ( Number . isFinite ( seq ) && current && seq < current . seq ) return false ; 31if ( phase === 'idle' || ! phase ) { const had = frames . delete ( sessionId ); if ( had ) emit (); return had ; } 32frames . set ( sessionId , { 33seq :Number . isFinite ( seq ) ?seq :( current ?. seq ?? 0 ) + 1 , 34 phase, code :String ( raw . code ?? phase ), text :String ( raw . text ?? '' ), 35args :raw . args && typeof raw . args === 'object' ?raw . args as Record < string , unknown > :undefined , 36tokens :typeof raw . tokens === 'number' ?raw . tokens :undefined , 37elapsedMs :Number ( raw . elapsedMs ) || 0 , receivedAt :now , 38}); 39emit (); 40return true ; 41} 42 43export function clearStatus ( sessionId :string ) { if ( frames . delete ( sessionId )) emit (); } 44export const statusOf = ( sessionId :string | null | undefined ) :StatusFrame | null => ( sessionId ?frames . get ( sessionId ) ?? null :null ); 45 46export function useLiveStatus ( sessionId :string | null | undefined ) :StatusFrame | null { 47return useSyncExternalStore ( 48fn => { listeners . add ( fn ); return () => { listeners . delete ( fn ); }; }, 49() => statusOf ( sessionId ), 50() => null , 51); 52} 53 54/** The playful words shown while the model is thinking, in the order they rotate. Translated through the catalog. */ 55export const PLAYFUL_VERBS = [ 56'Thinking…' , 'Pondering…' , 'Cooking up an answer…' , 'Mulling it over…' , 'Connecting the dots…' , 'Chasing an idea…' , 57'Sketching it out…' , 'Weighing the options…' , 'Reading between the lines…' , 'Brewing a reply…' , 'Putting it together…' , 'Following a hunch…' , 58] as const ; 59 60/** Phases where the model is working things out and nothing concrete can be named: the playful words rotate here and only here. */ 61export function isPlayful ( frame :StatusFrame ) :boolean { 62return frame . phase === 'thinking' || ( frame . phase === 'waiting' && frame . code === 'model-working' ); 63} 64 65/** The words for a frame. `rotation` counts how many times the playful word has advanced (the page advances it every few seconds). */ 66export function statusWords ( frame :StatusFrame , rotation = 0 ) :string { 67if ( isPlayful ( frame )) return tr ( PLAYFUL_VERBS [(( rotation % PLAYFUL_VERBS . length ) + PLAYFUL_VERBS . length ) % PLAYFUL_VERBS . length ]); 68const a = frame . args ?? {}; 69const target = typeof a . target === 'string' && a . target ?a . target :'' ; 70switch ( frame . code ) { 71case 'starting' :return tr ( 'Starting…' ); 72case 'reading-prompt' :return tr ( 'Reading your message…' ); 73case 'answering' :return tr ( 'Writing the answer…' ); 74case 'preparing-tool' :case 'tool' :{ 75const verb = typeof a . verb === 'string' && a . verb ?tr ( a . verb ) :'' ; 76return verb ?` ${ verb } ${ target ? ` · ${ target } ` : '' } …` :tr ( frame . text ); 77} 78case 'waiting-approval' :return tr ( 'Waiting for your answer…' ); 79case 'helpers' :return tr ( 'Helpers are working ({0})…' , [ Number ( a . n ) || 1 ]); 80case 'goal-step' :return tr ( 'Working on the goal…' ); 81default :return tr ( frame . text ); 82} 83} 84 85/** "14s", "1m 04s": how long the turn has been going. */ 86export function elapsedText ( startedAt :number , now :number ) :string { 87const total = Math . max ( 0 , Math . round (( now - startedAt ) / 1000 )); 88return total < 60 ?` ${ total } s` :` ${ Math . floor ( total / 60 )} m ${ String ( total % 60 ). padStart ( 2 , '0' )} s` ; 89}