okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
361c8d94a
main
1import { useEffect , useRef , useState } from 'react' ; 2import { tr } from './i18n' ; 3 4/** Minutes left from recent samples of "GB done": the rate over the last minute and a half, so a stall or a burst does not 5* swing the estimate. Null until there is enough to say (about 10 s of samples) or when nothing is moving. */ 6export function etaMinutes ( samples :{ at :number ; done :number }[], remainingGb :number | undefined ) :number | null { 7if ( ! Number . isFinite ( remainingGb ) || samples . length < 2 ) return null ; 8const first = samples [ 0 ], last = samples [ samples . length - 1 ]; 9const seconds = ( last . at - first . at ) / 1000 , gained = last . done - first . done ; 10if ( seconds < 10 || gained <= 0 ) return null ; 11return Math . max ( 1 , Math . ceil (( remainingGb ! / ( gained / seconds )) / 60 )); 12} 13 14export function etaLabel ( minutes :number | null ) :string { 15if ( minutes === null ) return '' ; 16if ( minutes >= 90 ) { 17const h = Math . floor ( minutes / 60 ), m = minutes % 60 ; 18return m ?tr ( 'About {0} h {1} min left' , [ h , m ]) :tr ( 'About {0} h left' , [ h ]); 19} 20return tr ( 'About {0} min left' , [ minutes ]); 21} 22 23/** The approximate time left of a running download, for the progress bar. `key` restarts the estimate for another job. */ 24export function useDownloadEta ( download :{ done_gb ?:number ; remaining_gb ?:number } | undefined , key :string | undefined ) :string { 25const samples = useRef <{ at :number ; done :number }[]>([]); 26const owner = useRef < string | undefined >( undefined ); 27const [ label , setLabel ] = useState ( '' ); 28const done = download ?. done_gb , remaining = download ?. remaining_gb ; 29useEffect (() => { 30if ( owner . current !== key ) { owner . current = key ; samples . current = []; } 31if ( ! Number . isFinite ( done )) { setLabel ( '' ); return ; } 32const now = Date . now (); 33const list = samples . current ; 34if ( ! list . length || list [ list . length - 1 ]. done !== done ) list . push ({ at :now , done :done ! }); 35while ( list . length > 2 && now - list [ 0 ]. at > 90_000 ) list . shift (); 36setLabel ( etaLabel ( etaMinutes ( list , remaining ))); 37}, [ done , remaining , key ]); 38return label ; 39} 40 41/** What the progress UI reads from an install job's `install` object: the parts it is downloading (the model's files and, 42* when the job adds it, the engine) summed into ONE count. A part is {kind, name, done_gb, total_gb, state}. Without `parts` 43* the job's combined `download` fields ({done_gb, total_gb, remaining_gb, percent}) are used as they are. */ 44export interface InstallParts { parts ?:{ done_gb ?:number ; total_gb ?:number ; state ?:string }[]; download ?:{ done_gb ?:number ; total_gb ?:number ; remaining_gb ?:number ; percent ?:number ; file ?:string } } 45export function installProgress ( install :InstallParts | undefined ) :{ done_gb :number ; total_gb :number ; remaining_gb :number ; percent :number ; file ?:string } | undefined { 46/* setup is checking files that are all on disk (strata_mcp combined_progress: file "checking files"): the check's own figure, not the 47parts' (which stood at 96% for the whole check, DL99) */ 48const check = install ?. download ; 49if ( check ?. file === 'checking files' && Number . isFinite ( check . done_gb ) && Number . isFinite ( check . total_gb ) && ( check . total_gb ?? 0 ) > 0 ) 50return { done_gb :check . done_gb ! , total_gb :check . total_gb ! , remaining_gb :Math . max ( 0 , check . total_gb ! - check . done_gb ! ), percent :Math . min ( 100 , Math . max ( 0 , check . percent ?? Math . floor ( 100 * check . done_gb ! / check . total_gb ! ))), file :check . file }; 51const parts = ( install ?. parts ?? []). filter ( p => Number . isFinite ( p . total_gb ) && ( p . total_gb ?? 0 ) > 0 && p . state !== 'skipped' && p . state !== 'current' ); 52if ( parts . length ) { 53const total = parts . reduce (( n , p ) => n + ( p . total_gb ?? 0 ), 0 ); 54const done = Math . min ( total , parts . reduce (( n , p ) => n + Math . min ( p . done_gb ?? 0 , p . total_gb ?? 0 ), 0 )); 55// 100 once every part is whole (the engine's too); 99 at most while one is still coming 56const whole = parts . every ( p => p . state === 'done' ); 57return { done_gb :done , total_gb :total , remaining_gb :Math . max ( 0 , total - done ), percent :whole ?100 :Math . min ( 99 , Math . floor ( 100 * done / total )), file :install ?. download ?. file }; 58} 59const d = install ?. download ; 60if ( d && Number . isFinite ( d . done_gb ) && Number . isFinite ( d . total_gb )) return { done_gb :d . done_gb ! , total_gb :d . total_gb ! , remaining_gb :Number . isFinite ( d . remaining_gb ) ?d . remaining_gb ! :Math . max ( 0 , d . total_gb ! - d . done_gb ! ), percent :d . percent ?? 0 , file :d . file }; 61return undefined ; 62} 63 64/** Which part of a multi-part install is being fetched now: the model's parts (with "part 2 of 4"), the picture reader or the engine. */ 65export interface InstallStage { kind :'model' | 'vision' | 'engine' | 'draft' | 'other' ; index ?:number ; count ?:number } 66export function installStage ( install :{ parts ?:{ kind ?:string ; state ?:string ; files_done ?:number ; files_total ?:number }[]; running ?:boolean } | undefined ) :InstallStage | undefined { 67const parts = install ?. parts ?? []; 68const now = parts . find ( p => p . state === 'downloading' ) ?? parts . find ( p => ! [ 'done' , 'current' , 'skipped' , 'failed' ]. includes ( p . state ?? '' )); 69if ( ! now ) return undefined ; 70const kind = now . kind === 'model' || now . kind === 'vision' || now . kind === 'engine' || now . kind === 'draft' ?now . kind :'other' ; 71const count = Number ( now . files_total ) > 1 ?Number ( now . files_total ) :undefined ; 72return { kind, ...( count ?{ index :Math . min ( count , Number ( now . files_done ?? 0 ) + 1 ), count} :{}) }; 73} 74/** While a download server has not answered yet (setup's waiting phase, strata_control/netpolicy.py): setup's own line, translated (net-<lang>.json); undefined otherwise. */ 75export function waitingText ( install :{ phase ?:string ; label ?:string } | undefined ) :string | undefined { 76return install ?. phase === 'waiting' && install . label ?tr ( install . label ) :undefined ; 77} 78/** The sentence for it, in plain words. */ 79export function stageText ( stage :InstallStage | undefined ) :string { 80if ( ! stage ) return tr ( 'Downloading model files…' ); 81if ( stage . kind === 'vision' ) return tr ( 'Downloading the picture reader…' ); 82if ( stage . kind === 'draft' ) return tr ( 'Preparing your model…' ); // the speed-up layer's own download (setup step 6) 83if ( stage . kind === 'engine' ) return tr ( 'Downloading the engine for this PC…' ); 84if ( stage . count && stage . index ) return tr ( 'Downloading model files · part {0} of {1}' , [ stage . index , stage . count ]); 85return tr ( 'Downloading model files…' ); 86} 87/** The setup wizard's status line: a server that has not answered yet, else setup's own sentence for what it does now (checking the files, 88* preparing the tokenizer, ...; strata_control/progress_words.py, the line the Models page shows), else the part being fetched. Without it the 89* wizard said "Downloading the picture reader…" while the model file was checked and "Downloading model files…" after the last file (DL99). */ 90export function guidedStatusText ( install :{ phase ?:string ; label ?:string ; parts ?:{ kind ?:string ; state ?:string ; files_done ?:number ; files_total ?:number }[] } | undefined , 91progress :unknown ) :string { 92return waitingText ( install ) ?? install ?. label ?? ( progress ?stageText ( installStage ( install )) :'Preparing your model…' ); 93}