okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1/** Disposable API-edge fixtures for manual CUA review. Never used by the app host. 2* STRATA_FIXTURE_FILE=/tmp/...json node tests/ui-fixture-host.mjs 3* Proxies the isolated demo host at 8794; serves the same built UI at 8796. 4*/ 5import http from 'node:http' ; 6import { readFileSync } from 'node:fs' ; 7const file = process . env . STRATA_FIXTURE_FILE ; 8const upstreamUrl = process . env . STRATA_FIXTURE_UPSTREAM ?? 'http://127.0.0.1:8794' ; 9const port = Number ( process . env . STRATA_FIXTURE_PORT ?? 8796 ); 10if ( ! file ) throw new Error ( 'Provide a disposable fixture JSON path.' ); 11const read = () => JSON . parse ( readFileSync ( file , 'utf8' )); 12let failed = false ; 13function transform ( path , value ) { 14const { scenario, responses= {} } = read (); 15if ( Object . hasOwn ( responses , path )) return responses [ path ]; 16if ( path === '/api/hardware' && scenario === 'unknown-hardware' ) return null ; 17if ( path === '/api/serving' && [ 'queued' , 'unknown-status' ]. includes ( scenario )) return { ...value , stats :{ ...value . stats , running :scenario === 'queued' ?1 :null , waiting :scenario === 'queued' ?2 :null } }; 18if ( path === '/api/models' ) { 19if ( scenario === 'missing-evidence' ) return { ...value , mode :'real' }; 20if ( scenario === 'large-catalog' ) { 21const original = value . variants [ 0 ]; 22const variants = Array . from ({ length :1200 }, ( _ , i ) => ({ ...original , id :`fixture- ${ i } ` , family :`A deliberately long model family name / multilingual research preview release ${ Math . floor ( i / 20 )} / 0.5B-Instruct` , quant :`Q ${ i % 20 } _Fixture_Quantization_With_A_Long_Exact_Name` , state :'available' , installedAt :undefined , catalog :{ ...original . catalog , family :`Fixture family ${ Math . floor ( i / 100 )} ` , version :`Release ${ Math . floor ( i / 20 )} ` , balancedRank :i }})); 23return { ...value , loaded :null , variants}; 24} 25if ( scenario === 'failed-load' && failed ) return { ...value , loaded :null , variants :value . variants . map ( v => v . id === 'qwen38-q4_k_m' ?{ ...v , state :'error' , error :'Fixture: model load failed; installed files retained.' } :v )}; 26} 27return value ; 28} 29http . createServer ( async ( req , res ) => { 30try { 31const path = new URL ( req . url , 'http://localhost' ). pathname ; 32const fixture = read (); 33if ( fixture . errors ?.[ path ]) { res . writeHead ( 503 ,{ 'content-type' :'application/json' }); res . end ( JSON . stringify ({ error :fixture . errors [ path ]})); return ; } 34if ( ! [ 'GET' , 'HEAD' ]. includes ( req . method ) && fixture . readOnly ) { res . writeHead ( 400 ,{ 'content-type' :'application/json' }); res . end ( JSON . stringify ({ error :'Fixture: this review action is intentionally unavailable.' })); return ;} 35if ( path . endsWith ( '/load' ) && req . method === 'POST' && read (). scenario === 'failed-load' ) { 36failed = true ; const lib = await ( await fetch ( upstreamUrl + '/api/models' )). json (); 37res . writeHead ( 200 ,{ 'content-type' :'application/json' }); res . end ( JSON . stringify ( transform ( '/api/models' , lib ))); return ; 38} 39if ( read (). scenario !== 'failed-load' ) failed = false ; 40const chunks = []; for await ( const chunk of req ) chunks . push ( chunk ); 41const upstream = await fetch ( upstreamUrl + req . url ,{ method :req . method , headers :req . headers [ 'content-type' ] ?{ 'content-type' :req . headers [ 'content-type' ]} :{}, body :chunks . length ?Buffer . concat ( chunks ) :undefined }); 42if ( path === '/api/events' ) { 43res . writeHead ( 200 ,{ 'content-type' :'text/event-stream' , 'cache-control' :'no-cache' }); 44const reader = upstream . body . getReader (); const decoder = new TextDecoder (); let buffer = '' , event = '' ; 45req . on ( 'close' ,() => void reader . cancel ()); 46while ( true ) { 47const { value, done} = await reader . read (); if ( done ) break ; 48buffer += decoder . decode ( value ,{ stream :true }); 49let pos ; while (( pos = buffer . indexOf ( '\n' )) >= 0 ) { 50let line = buffer . slice ( 0 , pos ); buffer = buffer . slice ( pos + 1 ); 51if ( line . startsWith ( 'event:' )) event = line . slice ( 6 ). trim (); 52if ( line . startsWith ( 'data:' )) { try { line = 'data: ' + JSON . stringify ( transform ( '/api/' + event , JSON . parse ( line . slice ( 5 )))); } catch {} } 53res . write ( line + '\n' ); 54} 55} 56res . end (); return ; 57} 58const contentType = upstream . headers . get ( 'content-type' ) ?? 'application/octet-stream' ; 59res . writeHead ( upstream . status ,{ 'content-type' :contentType }); 60if ( contentType . includes ( 'application/json' )) res . end ( JSON . stringify ( transform ( path , await upstream . json ()))); 61else res . end ( Buffer . from ( await upstream . arrayBuffer ())); 62} catch ( error ) { if ( ! res . headersSent ) res . writeHead ( 500 ); res . end ( String ( error )); } 63}). listen ( port , '127.0.0.1' ,() => console . log ( `Disposable CUA fixture host: http://127.0.0.1: ${ port } ` ));