okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
be885026f
main
1/** 2* Engine installer. 3* 4* Strata ships no binaries: it fetches the llama.cpp release build that matches 5* this platform into $STRATA_HOME/engine, so "Install inference engine" in the 6* first-run wizard is a real step rather than a progress bar that lies. 7*/ 8import { createWriteStream , existsSync , mkdirSync , readdirSync , rmSync , statSync } from 'node:fs' ; 9import { join } from 'node:path' ; 10import { Readable } from 'node:stream' ; 11import { pipeline } from 'node:stream/promises' ; 12import { exec } from './util.mjs' ; 13import { HOME } from './store.mjs' ; 14import { tarBinary } from './platform-paths.mjs' ; 15import { nowIso } from './util.mjs' ; 16import { require as requireAcquisition } from './acquisition.mjs' ; 17 18const ENGINE_DIR = join ( HOME , 'engine' ); 19const RELEASES = 'https://api.github.com/repos/ggml-org/llama.cpp/releases?per_page=8' ; 20 21const state = { 22status :'idle' , // idle | checking | downloading | extracting | done | error 23pct :0 , 24mbps :0 , 25stage :'' , 26asset :null , 27version :null , 28error :null , 29installedPath :null , 30}; 31 32let hubRef = null ; 33let running = null ; 34 35export function init ( hub ) { 36hubRef = hub ; 37} 38 39export function status () { 40return { ...state , engineDir :ENGINE_DIR }; 41} 42 43function emit () { 44hubRef ?. send ( 'engine-install' , status ()); 45} 46 47/** Which release asset belongs on this machine. */ 48function assetMatcher () { 49const { platform, arch} = process ; 50if ( platform === 'darwin' ) { 51return { 52test :( name ) => / macos / i . test ( name ) && ( arch === 'arm64' ?/ arm64 / i . test ( name ) :/ x64 | x86_64 / i . test ( name )) && / \.tar\.gz$ / . test ( name ), 53ext :'tar.gz' , 54}; 55} 56if ( platform === 'linux' ) { 57return { 58test :( name ) => / ubuntu | linux / i . test ( name ) && ( arch === 'arm64' ?/ arm64 | aarch64 / i . test ( name ) :/ x64 | x86_64 / i . test ( name )) && / \.tar\.gz$ / . test ( name ), 59ext :'tar.gz' , 60}; 61} 62return { 63test :( name ) => / win / i . test ( name ) && / x64 | x86_64 / i . test ( name ) && / \.zip$ / . test ( name ), 64ext :'zip' , 65}; 66} 67 68export async function findRelease () { 69requireAcquisition ( 'The llama.cpp engine' ); 70state . status = 'checking' ; 71state . stage = 'Looking up the current llama.cpp build' ; 72emit (); 73 74const res = await fetch ( RELEASES , { headers :{ accept :'application/vnd.github+json' }, signal :AbortSignal . timeout ( 15000 ) }); 75if ( ! res . ok ) throw new Error ( `GitHub responded ${ res . status } ` ); 76const releases = await res . json (); 77const matcher = assetMatcher (); 78 79for ( const release of releases ) { 80const asset = ( release . assets ?? []). find (( a ) => matcher . test ( a . name )); 81if ( asset ) return { asset, version :release . tag_name , ext :matcher . ext }; 82} 83throw new Error ( 'No matching llama.cpp build found for this platform' ); 84} 85 86export async function install ({ force= false } = {}) { 87requireAcquisition ( 'The llama.cpp engine' ); 88if ( running ) return running ; 89 90running = ( async () => { 91try { 92const found = await findRelease (); 93state . asset = found . asset . name ; 94state . version = found . version ; 95 96if ( ! force ) { 97const existing = findInstalledBinary (); 98if ( existing ) { 99state . status = 'done' ; 100state . pct = 100 ; 101state . stage = `Already installed ( ${ found . version } )` ; 102state . installedPath = existing ; 103emit (); 104return status (); 105} 106} 107 108mkdirSync ( ENGINE_DIR , { recursive :true }); 109const archive = join ( ENGINE_DIR , found . asset . name ); 110 111state . status = 'downloading' ; 112state . stage = `Downloading ${ found . asset . name } ` ; 113state . pct = 0 ; 114emit (); 115 116const dl = await fetch ( found . asset . browser_download_url , { redirect :'follow' , signal :AbortSignal . timeout ( 600_000 ) }); 117if ( ! dl . ok || ! dl . body ) throw new Error ( `Download failed ( ${ dl . status } )` ); 118 119const total = Number ( dl . headers . get ( 'content-length' )) || found . asset . size || 1 ; 120const startedAt = Date . now (); 121let received = 0 ; 122let last = 0 ; 123 124const progress = new ReadableStream ({ 125start ( controller ) { 126const reader = dl . body . getReader (); 127void ( async () => { 128try { 129for (;;) { 130const { done, value} = await reader . read (); 131if ( done ) break ; 132received += value . byteLength ; 133controller . enqueue ( value ); 134const now = Date . now (); 135if ( now - last > 250 ) { 136last = now ; 137const seconds = ( now - startedAt ) / 1000 ; 138const mbps = received / 1024 ** 2 / Math . max ( seconds , 0.001 ); 139state . pct = Math . min ( 99 , ( received / total ) * 100 ); 140state . mbps = mbps ; 141state . stage = `Downloading ${ found . asset . name } ` ; 142emit (); 143} 144} 145controller . close (); 146} catch ( err ) { 147controller . error ( err ); 148} 149})(); 150}, 151}); 152 153await pipeline ( Readable . fromWeb ( progress ), createWriteStream ( archive )); 154 155state . status = 'extracting' ; 156state . pct = 100 ; 157state . stage = 'Unpacking the engine' ; 158emit (); 159 160await exec ( tarBinary (), [ '-xf' , archive , '-C' , ENGINE_DIR ], { timeout :180_000 }); 161rmSync ( archive , { force :true }); 162 163const binary = findInstalledBinary (); 164if ( ! binary ) throw new Error ( 'Engine unpacked but no llama-server binary was found' ); 165 166state . status = 'done' ; 167state . stage = `llama.cpp ${ found . version } ready` ; 168state . installedPath = binary ; 169state . error = null ; 170hubRef ?. send ( 'log' , { level :'info' , message :`Engine installed: ${ binary } ` , at :nowIso () }); 171emit (); 172return status (); 173} catch ( err ) { 174state . status = 'error' ; 175state . error = err . message ; 176state . stage = 'Engine install failed' ; 177emit (); 178throw err ; 179} finally { 180running = null ; 181} 182})(); 183 184return running ; 185} 186 187export function findInstalledBinary () { 188if ( ! existsSync ( ENGINE_DIR )) return null ; 189try { 190for ( const entry of readdirSync ( ENGINE_DIR )) { 191const dir = join ( ENGINE_DIR , entry ); 192if ( ! statSync ( dir ). isDirectory ()) continue ; 193const candidate = join ( dir , 'llama-server' ); 194if ( existsSync ( candidate )) return candidate ; 195} 196} catch { 197/* ignore */ 198} 199return null ; 200}