okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1/** 2* UI preview only (STRATA_UI_PREVIEW=1): several installed engines for Settings > Engine and an engine update for About, kept in memory. 3* The demo host refuses mutations and simulates the hardware, so this answers the same routes the real host has (/api/engines, /api/engines/use, 4* /api/engines/remove, /api/updates/check, the update-engine job) with believable behaviour: switching changes what is in use, the engine in use 5* cannot be deleted, a switch while a model is loaded answers with the same plain sentence, an update makes the new engine the one in use. 6* Nothing here touches a disk or a process. 7*/ 8const GB = 1e9 ; 9 10function engine ( family , version , bytes , extra = {}) { 11const backend = extra . backend ?? 'cuda' ; 12const variant = extra . variant ?? ( backend === 'cuda' ?'cuda13' :'' ); 13const folder = ` ${ version } -windows-x86_64- ${ backend } ${ variant ? `- ${ variant } ` : '' } ` ; 14return { family, folder, version, backend, platform :'windows-x86_64' , variant, bytes, signed :true , fits :true , why_not :null , why_code :null , why_args :[], 15active :false , newest :false , pinned :false , running :false , can_use :true , can_delete :true , delete_blocked :null , blocked_code :null , 16build_id :`preview- ${ version } ` , path :`C:\\Users\\you\\AppData\\Local\\Strata\\components\\engine\\ ${ folder } ` , ...extra }; 17} 18 19export function createEnginePreview ( native ) { 20const qwen = [ engine ( 'qwen' , '0.1.55' , 1.02 * GB ), engine ( 'qwen' , '0.1.54' , 0.98 * GB ), engine ( 'qwen' , '0.1.50' , 0.91 * GB ), 21engine ( 'qwen' , '0.1.49' , 0.88 * GB , { backend :'hip' , fits :false , why_not :'x' , why_code :'gpu_make' })]; 22const groups = () => [{ family :'qwen' , title :'Qwen and other models' , engines :qwen }]; 23const latest = '0.1.56' ; 24let checked = false ; 25const jobs = []; 26 27function settle ( list , activeFolder ) { 28const fitting = list . filter ( e => e . fits ); 29const newest = fitting . reduce (( a , b ) => ( a && a . version . localeCompare ( b . version , undefined , { numeric :true }) >= 0 ?a :b ), null ); 30for ( const e of list ) { 31e . active = e . folder === activeFolder ; 32e . newest = e === newest ; 33e . can_use = e . fits && ! e . active ; 34e . can_delete = ! e . active ; 35e . blocked_code = e . active ?'active' :null ; 36e . delete_blocked = e . active ?'This is the engine in use. Switch to another one first.' :null ; 37} 38} 39const activeOf = list => list . find ( e => e . active )?. folder ; 40settle ( qwen , qwen [ 0 ]. folder ); 41const syncVersion = () => { native . installed = { ...( native . installed ?? {}), engine :{ version :qwen . find ( e => e . active )?. version ?? null } }; }; 42syncVersion (); 43native . capabilities = { ...( native . capabilities ?? {}), componentUpdates :true }; 44 45const total = () => qwen . reduce (( sum , e ) => sum + e . bytes , 0 ); 46const loaded = () => ( native . instances ?? []). some ( i => i . status === 'ready' || i . status === 'loading' ); 47 48return { 49/** True when the request was answered here. */ 50async handle ( path , method , body , json ) { 51if ( path === '/api/engines' && method === 'GET' ) return json ({ available :true , families :groups (), total_bytes :total (), serving :loaded () }), true ; 52if (( path === '/api/engines/use' || path === '/api/engines/remove' ) && method === 'POST' ) { 53const list = qwen ; 54const row = list . find ( e => e . folder === body . folder ); 55if ( ! row ) return json ({ error :'That engine is already gone. The list has been refreshed.' }, 400 ), true ; 56await new Promise ( r => setTimeout ( r , 700 )); 57if ( path . endsWith ( '/use' )) { 58if ( loaded ()) return json ({ error :'A model is running, so nothing was changed. Unload the model in Models, then try again.' }, 409 ), true ; 59if ( ! row . fits ) return json ({ error :"This engine can't run on this computer, so nothing was changed." }, 400 ), true ; 60settle ( list , row . folder ); 61if ( body . family === 'qwen' ) syncVersion (); 62return json ({ ok :true , status :'switched' , to :row . version }), true ; 63} 64if ( row . active ) return json ({ error :'This is the engine in use. Switch to another one first, then delete it.' }, 400 ), true ; 65list . splice ( list . indexOf ( row ), 1 ); 66settle ( list , activeOf ( list )); 67return json ({ ok :true , status :'deleted' , version :row . version , freed_bytes :row . bytes }), true ; 68} 69// The demo host refuses every change; unloading the demo model is allowed so the switch can be tried after the "a model is running" answer. 70const unload = / ^\/api\/native\/instances\/ ([ ^ / ] + ) \/unload$ / . exec ( path ); 71if ( unload && method === 'POST' ) { 72native . instances = ( native . instances ?? []). filter ( i => i . id !== unload [ 1 ]); 73if ( native . defaultId === unload [ 1 ]) native . defaultId = native . instances [ 0 ]?. id ?? null ; 74return json ( native ), true ; 75} 76if ( path === '/api/updates/check' && method === 'GET' ) { 77checked = true ; 78const inUse = qwen . find ( e => e . active )?. version ?? null ; 79const newer = inUse && inUse . localeCompare ( latest , undefined , { numeric :true }) < 0 && ! qwen . some ( e => e . version === latest ); 80return json ({ 81checkedAt :new Date (). toISOString (), 82strata :{ installedVersion :'0.2.9' , status :'current' , message :'Strata is up to date.' , canInstall :false }, 83engine :newer ?{ installedVersion :inUse , latestVersion :latest , status :'available' , message :'A new engine is available (1,030 MB).' , canInstall :true } 84 :{ installedVersion :inUse , status :'current' , message :'The engine is up to date.' , canInstall :false }, 85}), true ; 86} 87if ( path === '/api/native/operations' && method === 'POST' && body . kind === 'update-engine' ) { 88if ( loaded ()) return json ({ error :'Unload native models before changing engine files.' }, 400 ), true ; 89const job = { id :`preview-update- ${ jobs . length } ` , kind :'update-engine' , state :'running' , startedAt :new Date (). toISOString (), summary :'Downloading: 12%' , error :null , 90result :{ install :{ step :'Downloading: 12%' , download :{ file :`engine ${ latest } ` , percent :12 } } } }; 91jobs . push ( job ); 92native . jobs . push ( job ); 93const step = ( delay , fn ) => setTimeout ( fn , delay ). unref (); 94step ( 1500 , () => { job . summary = 'Downloading: 64%' ; job . result . install . download . percent = 64 ; }); 95step ( 3000 , () => { job . summary = 'Making sure it works…' ; job . result = null ; }); 96step ( 4500 , () => { 97for ( const e of qwen ) e . active = false ; 98qwen . unshift ( engine ( 'qwen' , latest , 1.03 * GB )); 99settle ( qwen , qwen [ 0 ]. folder ); 100syncVersion (); 101Object . assign ( job , { state :'done' , summary :'The engine is updated and ready.' , result :{ results :[{ component :'engine' , status :'updated' }] } }); 102}); 103return json ( job ), true ; 104} 105return false ; 106}, 107get checked () { return checked ; }, 108}; 109}