okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1import { OFFLINE_MESSAGE , looksOffline , plainUpdateError } from './update-messages.mjs' ; 2 3/** Read-only release checks. Installing/rolling back needs a managed installer. */ 4const RELEASES = 'https://api.github.com/repos/Niko1221/Strata/releases?per_page=30' ; 5const RELEASE_PREFIX = 'https://github.com/Niko1221/Strata/releases/' ; 6 7function versionParts ( value ) { 8const match = / ^v ? ( \d + ) \. ( \d + ) \. ( \d + )(?: \+ [ \w .- ] + ) ? $ / . exec ( value ?? '' ); 9return match ?match . slice ( 1 ). map ( Number ) :null ; 10} 11function compare ( a , b ) { 12for ( let i = 0 ; i < 3 ; i ++ ) if ( a [ i ] !== b [ i ]) return a [ i ] > b [ i ] ?1 :- 1 ; 13return 0 ; 14} 15 16export async function checkUpdates ({ appVersion, engineVersion}, fetcher = fetch ) { 17let strata ; 18try { 19const response = await fetcher ( RELEASES , { headers :{ accept :'application/vnd.github+json' }, signal :AbortSignal . timeout ( 10000 ) }); 20if ( ! response . ok ) throw new Error ( response . status === 403 || response . status === 429 ?'The release service is busy. Try again later.' :'Could not reach the Strata release feed. Try again.' ); 21const releases = await response . json (); 22if ( ! Array . isArray ( releases )) throw new Error ( 'The release service returned an invalid response.' ); 23const candidates = releases . filter ( r => ! r . draft && ! r . prerelease && versionParts ( r . tag_name )); 24candidates . sort (( a , b ) => compare ( versionParts ( b . tag_name ), versionParts ( a . tag_name ))); 25const latest = candidates [ 0 ]; 26const installed = versionParts ( appVersion ); 27if ( ! latest ) strata = { status :'no-releases' , message :'No stable Strata release is published in this feed yet.' }; 28else if ( ! installed ) strata = { status :'unavailable' , message :'This installation has no comparable version number.' }; 29else { 30const newer = compare ( versionParts ( latest . tag_name ), installed ) > 0 ; 31// Release content is untrusted. Only link back to the known repository. 32const releaseUrl = typeof latest . html_url === 'string' && latest . html_url . startsWith ( RELEASE_PREFIX ) ?latest . html_url :undefined ; 33strata = { status :newer ?'available' :'current' , latestVersion :latest . tag_name , releaseUrl, message :newer ?`Strata ${ latest . tag_name } is available.` :'Strata is up to date.' }; 34} 35} catch ( error ) { 36strata = { status :'error' , message :looksOffline ( error ) ?OFFLINE_MESSAGE :error . name === 'TimeoutError' ?'The release check timed out. Try again.' :error . message ?? 'Could not check for updates.' }; 37} 38return { 39checkedAt :new Date (). toISOString (), 40strata :{ installedVersion :appVersion , ...strata , canInstall :false }, 41// The existing binary downloader has no tested-release compatibility feed 42// or trustworthy installed build ID. Upstream's latest is not an update verdict. 43engine :{ installedVersion :engineVersion , status :'unavailable' , canInstall :false , message :'Engine update checks are not available in this build.' }, 44}; 45} 46 47/** The engine row after a version failed its test start on this computer (owner-approved wording, 11-09). */ 48export const engineFailedBeforeText = ( offered , installed ) => `Engine ${ offered ?? 'update' } didn't start correctly on this computer, so Strata kept engine ${ installed ?? 'in use' } . Press Update engine to try again.` ; 49 50/** 51* The packaged app's update check: the signed feed, read by Control (`strata update --check`), plus the shell's own verdict 52* on the app. Same result shape as `checkUpdates`, so the Settings screen needs no change. This is the only place the host 53* reaches the feed, and only when the user presses "Check for updates". 54*/ 55export async function checkComponentUpdates ({ control, appUpdater= null , appVersion= null }) { 56const checkedAt = new Date (). toISOString (); 57let report ; 58try { 59report = await control . updateCheck ({ appVersion}); 60} catch ( error ) { 61const plain = plainUpdateError ( error , 'Strata' ); 62const message = / update server | aren't available | No internet / . test ( plain . headline ) ?plain . message :`Couldn't check for updates. Try again in a moment.` ; 63return { checkedAt, strata :{ installedVersion :appVersion ?? control . version ?? null , status :'error' , message, details :plain . details , canInstall :false }, engine :{ installedVersion :null , status :'error' , message, details :plain . details , canInstall :false } }; 64} 65const mib = bytes => ` ${ Math . round (( bytes ?? 0 ) / 1_000_000 )} MB` ; // decimal megabytes, like the Terminal and the release page 66const engine = report . engine ?? {}; 67const installedEngine = engine . installed ?. version ?? null ; 68const offered = engine . offered ?. version ?? null ; 69const engineView = { installedVersion :installedEngine , latestVersion :offered ?? undefined , canInstall :engine . status === 'available' || engine . status === 'install' }; 70if ( engine . status === 'available' && engine . installed_locally ) Object . assign ( engineView , { status :'available' , message :'A newer engine is already on this computer. Update switches to it; nothing is downloaded.' }); 71else if ( engine . status === 'available' ) Object . assign ( engineView , { status :'available' , message :`A new engine is available ( ${ mib ( engine . download_bytes )} ).` }); 72else if ( engine . status === 'install' ) Object . assign ( engineView , { status :'available' , message :`This PC needs an engine to run models. Press Update engine to download it ( ${ mib ( engine . download_bytes )} ).` }); 73// A version that failed its test start here is held back by Control for a day (status failed-before). It is not 'up to date': the row says what happened and 74// offers the update again (the app's Update engine passes --force, so the press really tries it again). QAGUI-LIN 0213. 75else if ( engine . status === 'failed-before' ) Object . assign ( engineView , { status :'available' , canInstall :true , retry :true , message :engineFailedBeforeText ( offered , installedEngine ), details :engine . note }); 76else if ( engine . status === 'current' || engine . status === 'skipped' ) Object . assign ( engineView , { status :'current' , message :'The engine is up to date.' , details :engine . note }); 77else if ( engine . status === 'wrong-make' ) Object . assign ( engineView , { status :'unavailable' , message :engine . note ?? 'Nothing was changed: the update was for another make of graphics card.' , details :( engine . reasons ?? []). join ( '\n' ) || undefined }); 78else if ( engine . status === 'not-in-feed' ) Object . assign ( engineView , { status :'current' , message :'No engine update is listed right now.' }); 79else Object . assign ( engineView , { status :'unavailable' , message :"There isn't an engine for this PC's graphics card yet." , details :( engine . reasons ?? []). join ( '\n' ) || undefined }); 80const control_ = report . control ?? {}; 81const app = report . app ?? {}; 82let appNew = app . status === 'available' ?app . offered :null ; 83let appNotes = app . notes ?? '' ; 84let appCheckFailed = null ; // the shell's own (Tauri) check threw: before this, the Settings check hid it and showed only Control's text 85let appCannot = null ; // browser mode: the app window's updater is not running, so the app itself cannot be updated from here 86if ( appUpdater ) { 87try { 88const verdict = await appUpdater . check (); 89appNew = verdict . available ?verdict . version :null ; 90appNotes = verdict . notes ?? appNotes ; 91} catch ( error ) { 92if ( error ?. code === 'app-update-unavailable' && appUpdater . unavailable ) appCannot = appUpdater . unavailable ; 93else appCheckFailed = String ( error ?. message ?? error ?? 'The app update check failed.' ); // the app's own check did not work: say so, raw text behind Details 94} 95} 96const controlNew = control_ . status === 'available' ?control_ . offered :null ; 97const strata = { installedVersion :appVersion ?? control_ . installed ?? control . version ?? null , canInstall :!! (( appNew && ! appCannot ) || controlNew ) }; 98const appCheckLine = "Strata couldn't check for a new version of the app itself. Try again later." ; 99if ( appNew || controlNew ) Object . assign ( strata , { status :'available' , latestVersion :appNew ?? controlNew , message :`A new version of Strata is available. ${ appNotes ? ` ${ appNotes } ` : '' } ${ appNew && appCannot ? ` ${ appCannot } ` : '' } ${ appCheckFailed ? ` ${ appCheckLine } ` : '' } ` }); 100else if ( appCheckFailed ) Object . assign ( strata , { status :'error' , message :appCheckLine }); 101else Object . assign ( strata , { status :'current' , message :'Strata is up to date.' }); 102if ( appCheckFailed ) strata . details = appCheckFailed ; 103return { checkedAt, feed :report . feed , strata, engine :engineView , serving :report . serving ?? [] }; 104} 105 106/** 107* What the first-run card shows: this PC's graphics card(s), the engine package the signed feed offers for it and how big it is. 108* One feed read, nothing downloaded. `status`: offer (a package fits), none (nothing fits; `reasons` say why), offline (the feed could not be 109* read), installed (an engine is already here), wrong-make (an engine for another make of card is installed). 110*/ 111export async function engineFirstRun ({ control, gpus= [] }) { 112const cards = gpus . map ( g => ({ name :g . name ?? 'Graphics card' , vramGb :g . vramGb ?? null })); 113let report ; 114try { 115report = await control . updateCheck ({}); 116} catch ( error ) { 117const plain = plainUpdateError ( error , 'Strata' ); 118return { status :looksOffline ( error ) || / update server | No internet / . test ( plain . headline ?? '' ) ?'offline' :'error' , gpus :cards , message :plain . message , details :plain . details }; 119} 120const engine = report . engine ?? {}; 121const hardware = report . hardware ?? {}; 122const base = { gpus :cards , driver :hardware . nvidia_driver ?? null , backend :hardware . backend ?? null }; 123if ( engine . installed ) return { ...base , status :'installed' , installedVersion :engine . installed . version ?? null }; 124if ( engine . status === 'wrong-make' ) return { ...base , status :'wrong-make' , message :engine . note ?? null , reasons :engine . reasons ?? [] }; 125if ( engine . offered && ( engine . status === 'install' || engine . status === 'available' )) { 126// A newer Strata Control in the same feed comes with the engine (an Arc user who has just installed the app needed 0.1.59.1's fix and had to find Settings > Updates; ARCFIX 0213). 127const ctl = report . control ?? {}; 128const control = ctl . status === 'available' && ctl . offered ?{ version :ctl . offered , bytes :ctl . download_bytes || 0 } :null ; 129return { ...base , status :'offer' , offer :{ version :engine . offered . version , bytes :engine . download_bytes || engine . offered . size || 0 , variant :engine . offered . variant ?? '' , backend :engine . offered . backend ?? null }, control}; 130} 131return { ...base , status :'none' , message :engine . note ?? null , reasons :engine . reasons ?? [] }; 132}