okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
361c8d94a
main
1/** 2* The engine comes with the model. 3* 4* A model install job (packaged app) also makes sure this PC has a usable engine: if none is installed, or the signed feed offers 5* a newer compatible one, the job runs Control's component updater for the engine (same feed, signature and hash checks, staged 6* test start) next to the model files. It reports through the job's own progress fields, so the UI shows one combined total. 7* 8* It never throws: a failed engine download leaves the model files alone and becomes `engine.state === 'failed'` with a retry 9* message. Loading, starting and serving never download (STRATA_NO_ACQUIRE stays on for them); this runs only inside the install 10* job the user started. Contract: see phaseA-tests/notes-split-s6.md ("Job contract"). 11*/ 12 13import { plainUpdateError , PHASE_TEXT } from './update-messages.mjs' ; 14 15const GB = 1e9 ; 16const round = ( value , digits = 2 ) => Math . round ( value * 10 ** digits ) / 10 ** digits ; 17 18export const RETRY = 'Update engine' ; 19 20export function createEngineStep ({ control, signal= null , onChange= () => {} } = {}) { 21const part = { kind :'engine' , name :'Engine' , version :null , state :'pending' , percent :0 , done_gb :0 , total_gb :0 , message :'Waiting for the model download to start.' }; 22let note = null ; 23const set = patch => { Object . assign ( part , patch ); onChange ({ ...part }); }; 24 25async function run () { 26if ( ! control || typeof control . update !== 'function' ) { set ({ state :'skipped' , message :'This build has no component updater.' }); return { ...part }; } 27try { 28set ({ state :'checking' , message :'Checking the engine for this PC…' }); 29let usable = false ; 30try { usable = !! ( await control . engine ()). usable ; } catch { /* treated as not usable */ } 31let report = null ; 32try { 33report = await control . updateCheck (); 34} catch ( error ) { 35if ( usable ) { set ({ state :'skipped' , message :'The engine for this PC is installed.' }); return { ...part }; } 36throw error ; 37} 38const engine = report . engine ?? {}; 39const wanted = engine . status === 'install' || engine . status === 'available' ; 40if ( ! wanted ) { 41if ( ! usable && engine . status !== 'current' ) throw new Error ( engine . reasons ?.[ 0 ] ?? 'The update feed has no engine for this PC.' ); 42set ({ state :'current' , version :engine . installed ?. version ?? null , message :'The engine for this PC is ready.' , percent :100 }); 43return { ...part }; 44} 45const version = engine . offered ?. version ?? null ; 46// An engine already works here: say what is about to change and that it can be undone (#4823), not only "downloading". 47const before = engine . installed ?. version ?? null ; 48const change = before && version && before !== version ?`Updating the engine from ${ before } to ${ version } . The engine you have now is kept, and Settings can go back to it.` :null ; 49set ({ state :'downloading' , version, name :`Engine ${ version ?? '' } ` . trim (), total_gb :round (( engine . download_bytes ?? 0 ) / GB ), message :change ?? 'Downloading the engine for this PC…' }); 50const result = await control . update ({ 51engine :true , control :false , signal, 52onEvent :event => { 53if ( event . event === 'progress' ) { 54set ({ state :'downloading' , percent :event . percent ?? 0 , done_gb :round (( event . done ?? 0 ) / GB ), total_gb :round (( event . total ?? 0 ) / GB ), message :` ${ change ? `Updating the engine from ${ before } to ${ version } ` : 'Downloading the engine for this PC' } : ${ event . percent ?? 0 } %` }); 55} else if ( event . event === 'phase' ) { 56const state = { verify :'verifying' , test :'testing' , switch :'switching' }[ event . phase ]; 57if ( state ) set ({ state, message :PHASE_TEXT [ event . phase ] ?? part . message }); 58} else if ( event . event === 'waiting' ) { 59set ({ message :'Waiting for the model to stop before finishing the engine…' }); 60} 61}, 62}); 63const done = ( result . results ?? []). find ( r => r . component === 'engine' ); 64set ({ state :'done' , percent :100 , done_gb :part . total_gb , version :done ?. installed_after ?. version ?? version , message :change ?`The engine was updated from ${ before } to ${ version } . Settings can go back to the one you had.` :'The engine for this PC is ready.' }); 65} catch ( error ) { 66const plain = plainUpdateError ( error , 'engine' ); 67note = `Your model is ready. The engine for this PC couldn't be downloaded. ${ plain . next } ` ; 68set ({ state :'failed' , error :plain . headline , details :plain . details , retry :RETRY , message :note }); 69} 70return { ...part }; 71} 72 73/** The install progress with the engine part added and one combined download total. Pure: returns a new object. */ 74function merge ( install ) { 75if ( ! install || typeof install !== 'object' ) return install ; 76const model = install . download ?? null ; 77const modelDone = Number . isFinite ( model ?. done_gb ) ?model . done_gb :null ; 78const modelRemaining = Number . isFinite ( model ?. remaining_gb ) ?model . remaining_gb :null ; 79const active = ! [ 'done' , 'current' , 'skipped' , 'failed' ]. includes ( part . state ); 80const engineRemaining = active || part . state === 'done' ?Math . max ( 0 , part . total_gb - part . done_gb ) :0 ; 81/* The install knows its files (strata_mcp files_progress): the model's parts and the picture reader, each counted. Without that, one 82model part from the combined figures. The engine is added either way. */ 83const own = Array . isArray ( install . parts ) && install . parts . some ( p => p ?. kind === 'model' ) ?install . parts . filter ( p => p ?. kind !== 'engine' ) :null ; 84const parts = [ 85 ...( own ?? [{ kind :'model' , name :model ?. file ?? install . model ?? 'Model' , state :install . running === false ?'done' :'downloading' , done_gb :modelDone ?? 0 , total_gb :round (( modelDone ?? 0 ) + ( modelRemaining ?? 0 )) }]), 86{ ...part }, 87]; 88const merged = { ...install , engine :{ ...part }, parts}; 89// setup checking files that are all on disk: its figure is the check's, the engine's bytes are not part of it (DL99) 90if ( model && model . file !== 'checking files' ) { 91const done = round (( modelDone ?? 0 ) + ( part . state === 'pending' || part . state === 'checking' ?0 :part . done_gb )); 92const remaining = round (( modelRemaining ?? 0 ) + ( part . state === 'pending' || part . state === 'checking' ?0 :engineRemaining )); 93const total = done + remaining ; 94merged . download = { ...model , done_gb :done , remaining_gb :remaining , percent :total > 0 ?Math . min ( 100 , Math . round ( 100 * done / total )) :model . percent , 95file :active && part . state === 'downloading' && ( modelRemaining ?? 0 ) <= 0 ?part . name :model . file }; 96} 97return merged ; 98} 99 100/** What to tell the user when the model is ready but the engine is not. Null when all is well. */ 101function summaryNote () { 102return part . state === 'failed' ?note :null ; 103} 104 105return { run, merge, summaryNote, part :() => ({ ...part }) }; 106}