okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1/** Disposable UI fixture. All native operations are mocked; no weights or external programs run. */ 2import http from 'node:http' ; 3import { readFile } from 'node:fs/promises' ; 4import { resolve , extname } from 'node:path' ; 5import { nativeFixture } from '../helpers/native-fixture.mjs' ; 6const fixture = nativeFixture (), control = fixture . control , root = resolve ( import . meta. dirname , '../../dist' ), upstream = 'http://127.0.0.1:5161' ; 7await control . refresh ( true ); 8const server = http . createServer ( async ( req , res ) => { try { 9const path = new URL ( req . url , 'http://fixture' ). pathname ; let text = '' ; if ( req . method !== 'GET' ) for await ( const chunk of req ) text += chunk ; const body = text ?JSON . parse ( text ) :{}; 10const json = value => { res . writeHead ( 200 ,{ 'content-type' :'application/json' }); res . end ( JSON . stringify ( value ));}; 11if ( path === '/api/native' && req . method === 'GET' ) return json ( await control . refresh ()); 12if ( path === '/api/native/plan' ) return json ( await control . plan ( body )); 13if ( path === '/api/native/operations' ) return json ( control . operation ( body . kind , body . args )); 14if ( path === '/api/native/benchmark' ) return json ( control . benchmarkReport ()); 15if ( path === '/api/native/logs' ) return json ({ logs :control . logs ()}); 16let match = /^\/api\/native\/models\/ ([ ^ / ] + ) \/settings$ / . exec ( path ); if ( match ) return json ( req . method === 'GET' ?control . settings ( match [ 1 ]) :control . save ( match [ 1 ], body )); 17match = /^\/api\/native\/instances\/ ([ ^ / ] + ) \/ ( default | unload ) $ / . exec ( path ); if ( match ) return json ( match [ 2 ] === 'default' ?control . setDefault ( match [ 1 ]) :await control . unload ( match [ 1 ])); 18match = /^\/api\/native\/operations\/ ([ ^ / ] + ) \/cancel$ / . exec ( path ); if ( match ) return json ( await control . cancel ( match [ 1 ])); 19if ( path === '/api/hardware' ){ const value = await ( await fetch ( upstream + path )). json (); return json ({ ...value , gpus :[{ index :0 , id :'gpu-0' , vendor :'nvidia' , name :'NVIDIA RTX 4090' , vramGb :24 , backend :'cuda' , unified :false },{ index :1 , id :'gpu-1' , vendor :'nvidia' , name :'NVIDIA RTX 3090' , vramGb :24 , backend :'cuda' , unified :false }], memory :{ totalGb :128 , usedGb :48 , freeGb :80 , pressure :'low' }});} 20if ( path . startsWith ( '/api/' )){ const response = await fetch ( upstream + req . url ,{ method :req . method , headers :text ?{ 'content-type' :'application/json' } :{}, body :text || undefined }); res . writeHead ( response . status ,{ 'content-type' :response . headers . get ( 'content-type' ) ?? 'application/json' }); for await ( const chunk of response . body ) res . write ( chunk ); res . end (); return ;} 21const file = path === '/' ?'/index.html' :path ; if ( file . includes ( '..' )) throw new Error ( 'Invalid fixture path' ); const content = await readFile ( root + file ); res . writeHead ( 200 ,{ 'content-type' :({ '.html' :'text/html' , '.js' :'text/javascript' , '.css' :'text/css' , '.svg' :'image/svg+xml' , '.woff2' :'font/woff2' , '.png' :'image/png' })[ extname ( file )] ?? 'application/octet-stream' }); res . end ( content ); 22} catch ( e ){ res . writeHead ( 400 ,{ 'content-type' :'application/json' }); res . end ( JSON . stringify ({ error :e . message }));}}); 23server . listen ( 5270 , '127.0.0.1' ,() => console . log ( 'Disposable native UI fixture: http://127.0.0.1:5270' )); 24let stopping = false ; async function stop (){ if ( stopping ) return ; stopping = true ; server . closeAllConnections (); server . close (); await fixture . cleanup (); process . exit ( 0 );} process . on ( 'SIGTERM' , stop ); process . on ( 'SIGINT' , stop );