okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db5be2aa5
main
1#!/usr/bin/env node 2/** 3* Picture reading in the APP, end to end on a test box, through the installed app layout (packaged host + bundled Control + a real engine component 4* that carries strata-vision): 5* Download and prepare with "Read pictures" ON (the picture-reader file is fetched, the vision settings are written), load the model with the picture 6* reader, send a real picture through the host's own /v1 endpoint and check that the answer names what is in it, unload, shut down. 7* 8* node vision-e2e.mjs --install <folder with node/ host/ control/> --components <folder holding engine/<package with strata-vision>> \ 9* --work <scratch> --family qwen --model IQ3_XXS --data <data folder that has (or will get) the model files> --image shapes.png [--device cpu|gpu] 10* 11* The picture has a red circle, a blue square, a green triangle and the text "STRATA 42". The run exits 1 if any step fails. 12*/ 13import { spawn } from 'node:child_process' ; 14import { existsSync , mkdirSync , readFileSync , readdirSync , statSync } from 'node:fs' ; 15import { createServer } from 'node:net' ; 16import { join , resolve } from 'node:path' ; 17 18const args = Object . fromEntries ( process . argv . slice ( 2 ). reduce (( acc , a , i , all ) => ( a . startsWith ( '--' ) ?[ ...acc , [ a . slice ( 2 ), all [ i + 1 ]?. startsWith ( '--' ) || all [ i + 1 ] === undefined ?true :all [ i + 1 ]]] :acc ), [])); 19for ( const need of [ 'install' , 'components' , 'work' , 'model' , 'image' ]) if ( ! args [ need ]) { console . error ( 'missing --' + need ); process . exit ( 2 ); } 20const install = resolve ( args . install ), work = resolve ( args . work ), components = resolve ( args . components ), family = String ( args . family ?? 'qwen' ), modelId = String ( args . model ); 21const device = String ( args . device ?? 'cpu' ); 22const node = join ( install , 'node' , 'node' ), hostEntry = join ( install , 'host' , 'server' , 'index.mjs' ), controlDir = join ( install , 'control' ); 23const dirs = { models :join ( work , 'models' ), profiles :join ( work , 'profiles' ), runtime :join ( work , 'runtime' ), home :join ( work , 'home' ), xdg :join ( work , 'xdg' ), appdata :join ( work , 'appdata' ) }; 24for ( const d of [ dirs . models , dirs . home , dirs . xdg , dirs . appdata ]) mkdirSync ( d , { recursive :true }); 25const delay = ms => new Promise ( r => setTimeout ( r , ms )); 26let failed = false ; 27const record = ( name , ok , detail = '' ) => { if ( ! ok ) failed = true ; console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ name } ${ detail ? ' ' + String ( detail ). slice ( 0 , 600 ) : '' } ` ); }; 28const freePort = async () => { const s = createServer (); await new Promise ( r => s . listen ( 0 , '127.0.0.1' , r )); const p = s . address (). port ; await new Promise ( r => s . close ( r )); return p ; }; 29const env = { ...process . env , STRATA_COMPONENTS_DIR :components , STRATA_PROFILE_DIR :dirs . profiles , STRATA_RUNTIME_DIR :dirs . runtime , STRATA_INSTALL_DIR :dirs . models , 30STRATA_HOME :dirs . home , STRATA_MODELS_DIR :join ( work , 'models-unused' ), APPDATA :dirs . appdata , XDG_CONFIG_HOME :dirs . xdg , PYTHONDONTWRITEBYTECODE :'1' }; 31delete env . STRATA_API_KEY ; 32const port = await freePort (); 33const base = `http://127.0.0.1: ${ port } ` ; 34const host = spawn ( node , [ hostEntry ], { cwd :join ( install , 'host' ), stdio :[ 'ignore' , 'pipe' , 'pipe' ], 35env :{ ...env , STRATA_PORT :String ( port ), STRATA_DIST :join ( install , 'host' , 'dist' ), NODE_ENV :'production' , STRATA_PACKAGED :'1' , STRATA_REAL_ENGINE :'1' , STRATA_NO_ACQUIRE :'1' , 36STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()) } }); 37let hostLog = '' ; 38host . stdout . on ( 'data' , c => { hostLog += c ; }); host . stderr . on ( 'data' , c => { hostLog += c ; }); 39const call = async ( path , method = 'GET' , body ) => { 40const response = await fetch ( base + path , { method, headers :{ 'content-type' :'application/json' }, body :body === undefined ?undefined :JSON . stringify ( body ) }); 41return { status :response . status , body :await response . json (). catch (() => null ) }; 42}; 43async function waitJob ( id , seconds ) { 44let last = '' ; 45for ( let i = 0 ; i < seconds * 2 ; i ++ ) { 46const state = ( await call ( '/api/native' )). body ; 47const job = state . jobs . find ( j => j . id === id ); 48if ( job && job . summary !== last ) { last = job . summary ; console . log ( ' ... ' + String ( last ). slice ( 0 , 120 )); } 49if ( job && job . state !== 'running' ) return { job, state}; 50await delay ( 500 ); 51} 52throw new Error ( 'operation did not finish' ); 53} 54async function main () { 55let up = false ; 56for ( let i = 0 ; i < 200 && ! up ; i ++ ) { try { up = ( await fetch ( base + '/api/health' )). ok ; } catch { /* not yet */ } if ( ! up ) await delay ( 150 ); } 57record ( 'host healthy' , up , up ?base :hostLog . slice ( - 500 )); 58if ( ! up ) return ; 59const packages = existsSync ( join ( components , 'engine' )) ?readdirSync ( join ( components , 'engine' )) :[]; 60const reader = packages . some ( p => existsSync ( join ( components , 'engine' , p , 'strata-vision' ))); 61record ( 'the engine component carries the picture reader (strata-vision)' , reader , packages . join ( ', ' )); 62const status = ( await call ( '/api/control/status' )). body ; 63record ( 'Control: engine usable' , status . engine ?. usable === true , `engine= ${ status . engine ?. engine ?. version } ` ); 64 65// ---- Download and prepare, "Read pictures" on (the app sends vision "yes" for the download) 66const values = { family, model :modelId , context :32768 , vision :'yes' , loadVision :device , ...( args . data ?{ data_dir :resolve ( args . data ) } :{}) }; 67const plan = await call ( '/api/native/plan' , 'POST' , values ); 68record ( 'the plan for the download is made' , plan . status === 200 , JSON . stringify ( plan . body ). slice ( 0 , 300 )); 69const started = await call ( '/api/native/operations' , 'POST' , { kind :'install' , args :values }); 70record ( 'the install operation starts' , started . status === 200 || started . status === 202 , JSON . stringify ( started . body ). slice ( 0 , 200 )); 71const done = await waitJob ( started . body . id , 3600 ); 72record ( 'Download and prepare finishes' , done . job . state === 'done' , ` ${ done . job . state } ${ done . job . error ?? done . job . summary } ` ); 73const native = ( await call ( '/api/native?refresh=1' )). body ; 74console . log ( ' installed models: ' + JSON . stringify (( native . installed ?. models ?? []). map ( m => ({ id :m . id , model :m . model , ready :m . ready , visionPrepared :m . visionPrepared , images :m . images , config :m . config })))); 75const want = modelId . toLowerCase (); 76const model = native . installed ?. models ?. find ( m => [ m . id , m . model , m . tag ]. some ( x => String ( x ?? '' ). toLowerCase () === want )); 77record ( 'the model is installed with the picture reader prepared (visionPrepared)' , !! model ?. visionPrepared , JSON . stringify ({ ready :model ?. ready , visionPrepared :model ?. visionPrepared , images :model ?. images })); 78const configFile = model ?. configPath ?? ( model ?. config ?join ( dirs . models , model . config ) :null ); 79let cfg = {}; 80try { cfg = JSON . parse ( readFileSync ( configFile , 'utf8' )); } catch { /* reported below */ } 81const mm = cfg . vision ?. mmproj ; 82record ( 'the run settings carry the vision entry and --vision, and the mmproj file is on disk' , 83!! cfg . vision && ( cfg . args ?? []). includes ( '--vision' ) && !! mm && existsSync ( mm ) && statSync ( mm ). size > 800e6 , 84`vision= ${ cfg . vision ? 'yes' : 'no' } --vision= ${( cfg . args ?? []). includes ( '--vision' )} mmproj= ${ mm ? Math . round ( statSync ( mm ). size / 1048576 ) + ' MB' : 'none' } ` ); 85 86// ---- load with the picture reader 87const load = await call ( '/api/native/operations' , 'POST' , { kind :'load' , args :{ model :model ?. model ?? modelId . toLowerCase (), vision :device } }); 88const loaded = await waitJob ( load . body . id , 900 ); 89const instance = loaded . state . instances [ 0 ]; 90record ( 'load starts the engine with the picture reader' , loaded . job . state === 'done' && instance ?. status === 'ready' , ` ${ loaded . job . state } ${ loaded . job . error ?? '' } ; instance= ${ instance ?. status } ; vision= ${ instance ?. vision } ` ); 91record ( 'the host says the loaded model reads pictures' , !! instance ?. vision , `vision= ${ instance ?. vision } ` ); 92 93// ---- a real picture through the host's own /v1 endpoint 94const png = readFileSync ( resolve ( args . image )). toString ( 'base64' ); 95const t = Date . now (); 96let answer = '' , finish = '' ; 97for ( const budget of [ 1536 , 4096 ]) { 98const chat = await call ( '/v1/chat/completions' , 'POST' , { model :instance ?. apiModelId ?? modelId , max_tokens :budget , stream :false , messages :[{ role :'user' , content :[ 99{ type :'text' , text :'Describe this picture: name each shape with its colour and read out any text.' }, 100{ type :'image_url' , image_url :{ url :'data:image/png;base64,' + png } }] }] }); 101const choice = chat . body ?. choices ?.[ 0 ] ?? {}; 102answer = choice . message ?. content ?? '' ; 103finish = choice . finish_reason ?? String ( chat . status ); 104if ( answer . trim () || finish !== 'length' ) break ; 105} 106console . log ( ` answer ( ${ Math . round (( Date . now () - t ) / 1000 )} s, finish= ${ finish } ): ${ JSON . stringify ( answer . slice ( 0 , 300 ))} ` ); 107const low = answer . toLowerCase (); 108record ( 'the answer names the red circle' , / red | circle / . test ( low )); 109record ( 'the answer names the blue square' , / blue | square / . test ( low )); 110record ( 'the answer names the green triangle' , / green | triangle / . test ( low )); 111record ( 'the answer reads STRATA 42' , low . includes ( '42' )); 112 113// ---- unload 114const unloaded = await call ( `/api/native/instances/ ${ instance ?. id } /unload` , 'POST' , {}); 115record ( 'unload' , unloaded . status === 200 , unloaded . status ); 116} 117try { await main (); } catch ( e ) { record ( 'run' , false , e . stack ?? e ); } 118host . kill (); 119await delay ( 1500 ); 120console . log ( failed ?'VISION-E2E FAIL' :'VISION-E2E PASS' ); 121process . exit ( failed ?1 :0 );