okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1#!/usr/bin/env node 2/** 3* Attached pictures reaching a model, end to end on a test box, through the installed app layout (packaged host + bundled Control + an engine 4* component that carries strata-vision): 5* 1. the model catalog marks the picture-capable models (images: true) when the picture reader is installed; 6* 2. API models: a llama-server with a small picture model (--mmproj) is saved as a Custom model API; the host works out that it reads pictures, 7* and a real picture goes out as image_url and is answered. A server without a picture reader, a model set "not to read pictures", and an API 8* of another style each get a plain refusal (409) that says how to fix it; ticking "This model reads pictures" lets the picture through; 9* 3. a local model (unless --skip-local): Download and prepare with Read pictures ON, load, and a picture through the app's own chat. 10* 11* node pics-e2e.mjs --install <node/ host/ control/> --components <engine components> --work <scratch> --image shapes.png \ 12* Also: --silent <url> (a server whose model list says nothing about pictures: ON by default), --agent (the same picture through the agent; needs a Harness) 13* --vlm http://127.0.0.1:18090/v1 --vlm-model smolvlm --text-only http://127.0.0.1:18091/v1 [--skip-local --family qwen --model IQ3_XXS --data <folder> --device cpu] 14*/ 15import { spawn } from 'node:child_process' ; 16import { existsSync , mkdirSync , readFileSync , readdirSync } from 'node:fs' ; 17import { createServer } from 'node:net' ; 18import { join , resolve } from 'node:path' ; 19 20const 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 ), [])); 21for ( const need of [ 'install' , 'components' , 'work' , 'image' ]) if ( ! args [ need ]) { console . error ( 'missing --' + need ); process . exit ( 2 ); } 22const install = resolve ( args . install ), work = resolve ( args . work ), components = resolve ( args . components ); 23const want = String ( args . match ?? args . model ?? 'IQ3_XXS' ). toLowerCase (), contextSize = Number ( args . context ?? 32768 ), skipLocal = args [ 'skip-local' ] === true , device = String ( args . device ?? 'cpu' ), family = String ( args . family ?? 'qwen' ), modelId = String ( args . model ?? 'IQ3_XXS' ); 24const node = join ( install , 'node' , 'node' ), hostEntry = join ( install , 'host' , 'server' , 'index.mjs' ); 25const 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' ) }; 26for ( const d of [ dirs . models , dirs . home , dirs . xdg , dirs . appdata ]) mkdirSync ( d , { recursive :true }); 27const delay = ms => new Promise ( r => setTimeout ( r , ms )); 28let failed = false ; 29const record = ( name , ok , detail = '' ) => { if ( ! ok ) failed = true ; console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ name } ${ detail ? ' ' + String ( detail ). slice ( 0 , 600 ) : '' } ` ); }; 30const 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 ; }; 31const env = { ...process . env , STRATA_COMPONENTS_DIR :components , STRATA_PROFILE_DIR :dirs . profiles , STRATA_RUNTIME_DIR :dirs . runtime , STRATA_INSTALL_DIR :dirs . models , 32STRATA_HOME :dirs . home , STRATA_MODELS_DIR :join ( work , 'models-unused' ), APPDATA :dirs . appdata , XDG_CONFIG_HOME :dirs . xdg , PYTHONDONTWRITEBYTECODE :'1' }; 33delete env . STRATA_API_KEY ; 34const port = await freePort (); 35const base = `http://127.0.0.1: ${ port } ` ; 36const host = spawn ( node , [ hostEntry ], { cwd :join ( install , 'host' ), stdio :[ 'ignore' , 'pipe' , 'pipe' ], 37env :{ ...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' , 38STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()) } }); 39let hostLog = '' ; 40host . stdout . on ( 'data' , c => { hostLog += c ; }); host . stderr . on ( 'data' , c => { hostLog += c ; }); 41const call = async ( path , method = 'GET' , body ) => { 42const response = await fetch ( base + path , { method, headers :{ 'content-type' :'application/json' }, body :body === undefined ?undefined :JSON . stringify ( body ) }); 43return { status :response . status , body :await response . json (). catch (() => null ) }; 44}; 45const png = 'data:image/png;base64,' + readFileSync ( resolve ( args . image )). toString ( 'base64' ); 46const QUESTION = 'Name each shape in this picture with its colour, and read out any text.' ; 47 48/** One chat turn through the app's own endpoint: the plain-text answer, or the refusal (status + message). */ 49async function chat ( extra , message = QUESTION ) { 50const response = await fetch ( base + '/api/chat/stream' , { method :'POST' , headers :{ 'content-type' :'application/json' }, 51body :JSON . stringify ({ message, agent :false , images :[{ url :png , name :'shapes.png' }], ...extra }) }); 52if ( response . status !== 200 ) { const b = await response . json (). catch (() => ({})); return { status :response . status , message :b . error ?? b . message ?? '' }; } 53let text = '' , error = '' ; 54for ( const line of ( await response . text ()). split ( '\n' )) { 55if ( ! line . trim ()) continue ; 56let f ; try { f = JSON . parse ( line ); } catch { continue ; } 57if ( f . type === 'error' ) error = f . message ?? 'error' ; 58else if ( typeof f . text === 'string' && f . type === 'delta' ) text += f . text ; 59} 60return { status :200 , text, error}; 61} 62const saveApi = async ( name , baseUrl , models , protocol = 'openai-chat' ) => { 63const existing = ( await call ( '/api/model-providers' )). body . connections . find ( c => c . name === name ); 64return ( await call ( '/api/model-providers' , 'POST' , { ...( existing ?{ id :existing . id } :{}), kind :'custom' , provider :null , name, baseUrl, protocol, apiKey :'' , models})). body ; 65}; 66async function readsAfter ( connectionName , id , want , seconds = 20 ) { 67for ( let i = 0 ; i < seconds * 2 ; i ++ ) { 68const c = ( await call ( '/api/model-providers' )). body . connections . find ( x => x . name === connectionName ); 69const m = c ?. models . find ( x => x . id === id ); 70if ( m && m . reads === want ) return { c, m}; 71await delay ( 500 ); 72} 73const c = ( await call ( '/api/model-providers' )). body . connections . find ( x => x . name === connectionName ); 74return { c, m :c ?. models . find ( x => x . id === id ) }; 75} 76async function waitJob ( id , seconds ) { 77let last = '' ; 78for ( let i = 0 ; i < seconds * 2 ; i ++ ) { 79const state = ( await call ( '/api/native' )). body ; 80const job = state . jobs . find ( j => j . id === id ); 81if ( job && job . summary !== last ) { last = job . summary ; console . log ( ' ... ' + String ( last ). slice ( 0 , 120 )); } 82if ( job && job . state !== 'running' ) return { job, state}; 83await delay ( 500 ); 84} 85throw new Error ( 'operation did not finish' ); 86} 87const names = low => / ( red | blue | green | circle | square | triangle | strata | 42 ) / . test ( low ); 88 89async function main () { 90let up = false ; 91for ( let i = 0 ; i < 200 && ! up ; i ++ ) { try { up = ( await fetch ( base + '/api/health' )). ok ; } catch { /* not yet */ } if ( ! up ) await delay ( 150 ); } 92record ( 'host healthy' , up , up ?base :hostLog . slice ( - 500 )); 93if ( ! up ) return ; 94 95// ---- 1. the catalog 96const packages = existsSync ( join ( components , 'engine' )) ?readdirSync ( join ( components , 'engine' )) :[]; 97const reader = packages . some ( p => existsSync ( join ( components , 'engine' , p , 'strata-vision' ))); 98const native = ( await call ( '/api/native?refresh=1' )). body ; 99record ( 'the engine component carries the picture reader and the host knows it (visionReader)' , reader && native . visionReader === true , `component= ${ reader } host= ${ native . visionReader } ` ); 100const rows = ( native . catalog ?. families ?? []). flatMap ( f => ( f . sizes ?? []). map ( s => ({ family :f . title ?? f . family , size :s . id , images :s . images , capable :s . imagesCapable }))); 101console . log ( ' catalog (family / size / images / imagesCapable):' ); 102for ( const r of rows ) console . log ( ` ${ String ( r . family ). padEnd ( 26 )} ${ String ( r . size ). padEnd ( 30 )} images= ${ r . images } capable= ${ r . capable } ` ); 103record ( 'some models in the catalog read pictures' , rows . some ( r => r . images === true ), ` ${ rows . filter ( r => r . images ). length } of ${ rows . length } ` ); 104record ( 'every picture-capable model reads pictures when the reader is installed' , rows . every ( r => r . capable === r . images ), '' ); 105const flash = rows . filter ( r => / flash-next / i . test ( ` ${ r . family } ${ r . size } ` ) && ! /gyro | qad / i . test ( ` ${ r . family } ${ r . size } ` )); 106record ( 'the Qwen3.8-Flash-Next models are marked images: true' , flash . length > 0 && flash . every ( r => r . images === true ), flash . map ( r => r . size + '=' + r . images ). join ( ' ' )); 107 108// ---- 2. API models 109if ( args . vlm ) { 110const smol = args [ 'vlm-model' ] ?? 'smolvlm' ; 111await saveApi ( 'Small picture model' , args . vlm , [{ id :smol , name :'SmolVLM' }]); 112const found = await readsAfter ( 'Small picture model' , smol , true ); 113record ( 'the host works out that the llama-server reads pictures (no tick needed)' , found . m ?. reads === true , JSON . stringify ( found . m )); 114const conn = found . c ; 115const sent = await chat ({ remoteModel :{ connectionId :conn ?. id , modelId :smol } }); 116console . log ( ` answer from the API model: ${ JSON . stringify (( sent . text ?? sent . message ?? '' ). slice ( 0 , 300 ))} ` ); 117record ( 'the picture goes to the API model and is answered' , sent . status === 200 && ! sent . error && ( sent . text ?? '' ). trim (). length > 0 && names (( sent . text ?? '' ). toLowerCase ()), `status= ${ sent . status } err= ${ sent . error ?? '' } ` ); 118 119if ( args [ 'text-only' ]) { 120await saveApi ( 'Text only server' , args [ 'text-only' ], [{ id :'plain' , name :'Plain model' }]); 121const t = await readsAfter ( 'Text only server' , 'plain' , false ); 122record ( 'a server without a picture reader is known not to read pictures' , t . m ?. reads === false , JSON . stringify ( t . m )); 123const refused = await chat ({ remoteModel :{ connectionId :t . c . id , modelId :'plain' } }); 124console . log ( ` refusal: ${ JSON . stringify ( refused . message )} ` ); 125record ( 'a picture for it is refused in plain words that say how to fix it' , refused . status === 409 && / Read pictures with this model / . test ( refused . message ?? '' ) && / Settings / . test ( refused . message ?? '' ), ` ${ refused . status } ` ); 126const ticked = await saveApi ( 'Text only server' , args [ 'text-only' ], [{ id :'plain' , name :'Plain model' , vision :true }]); 127const after = ( await call ( '/api/model-providers' )). body . connections . find ( x => x . name === 'Text only server' ); 128record ( 'ticking "Read pictures with this model" is saved and makes the model read pictures' , after ?. models [ 0 ]?. vision === true && after ?. models [ 0 ]?. reads === true , JSON . stringify ( after ?. models [ 0 ])); 129const through = await chat ({ remoteModel :{ connectionId :after . id , modelId :'plain' } }); 130record ( 'with the tick the picture is sent on (the server itself decides what to do with it)' , through . status === 200 && ( through . error ?? '' ) !== '' ?/ turn off / . test ( through . error ) :through . status === 200 , ` ${ through . status } ${ through . message ?? through . error ?? '' } ` ); 131} 132 133await saveApi ( 'Small picture model' , args . vlm , [{ id :smol , name :'SmolVLM' , vision :false }]); 134const off = await readsAfter ( 'Small picture model' , smol , false ); 135const notRead = await chat ({ remoteModel :{ connectionId :off . c . id , modelId :smol } }); 136record ( 'a model switched OFF refuses pictures and says where to turn them on again' , notRead . status === 409 && / not to read pictures / . test ( notRead . message ?? '' ) && / Settings / . test ( notRead . message ?? '' ), ` ${ notRead . status } ${ notRead . message ?? '' } ` ); 137await saveApi ( 'Small picture model' , args . vlm , [{ id :smol , name :'SmolVLM' , vision :null }]); 138 139// unknown is ON by default: a server whose list and /props say nothing is sent the picture 140if ( args . silent ) { 141await saveApi ( 'Silent server' , args . silent , [{ id :'quiet' , name :'Quiet model' }]); 142const q = await readsAfter ( 'Silent server' , 'quiet' , true , 8 ); 143record ( 'a server that says nothing about pictures is ON by default' , q . m ?. reads === true && q . m ?. vision === null , JSON . stringify ( q . m )); 144const sentQuiet = await chat ({ remoteModel :{ connectionId :q . c . id , modelId :'quiet' } }); 145record ( 'and the picture is sent to it (not refused)' , sentQuiet . status === 200 , ` ${ sentQuiet . status } ${ sentQuiet . message ?? sentQuiet . error ?? '' } ` ); 146} 147await saveApi ( 'Other style' , 'https://api.anthropic.com/v1' , [{ id :'claude-x' , name :'Claude' }], 'anthropic' ); 148const other = ( await call ( '/api/model-providers' )). body . connections . find ( x => x . name === 'Other style' ); 149const style = await chat ({ remoteModel :{ connectionId :other . id , modelId :'claude-x' } }); 150console . log ( ` other style: ${ JSON . stringify ( style . message )} ` ); 151record ( 'an API of another style says plainly why pictures stay in the preview' , style . status === 409 && / OpenAI / . test ( style . message ?? '' ), ` ${ style . status } ` ); 152 153} 154if ( skipLocal ) return ; 155 156// ---- 3. a local model 157const values = { family, model :modelId , context :contextSize , vision :'yes' , loadVision :device , ...( args . data ?{ data_dir :resolve ( args . data ) } :{}) }; 158const started = await call ( '/api/native/operations' , 'POST' , { kind :'install' , args :values }); 159record ( 'the install operation starts' , started . status === 200 || started . status === 202 , JSON . stringify ( started . body ). slice ( 0 , 200 )); 160const done = await waitJob ( started . body . id , 3600 ); 161record ( 'Download and prepare finishes' , done . job . state === 'done' , ` ${ done . job . state } ${ done . job . error ?? done . job . summary } ` ); 162const refreshed = ( await call ( '/api/native?refresh=1' )). body ; 163const model = refreshed . installed ?. models ?. find ( m => [ m . id , m . model , m . tag ]. some ( x => String ( x ?? '' ). toLowerCase () === want )); 164record ( 'the model is prepared with its picture reader' , !! model ?. visionPrepared , JSON . stringify ({ ready :model ?. ready , visionPrepared :model ?. visionPrepared })); 165const load = await call ( '/api/native/operations' , 'POST' , { kind :'load' , args :{ model :model ?. model ?? want } }); 166const loaded = await waitJob ( load . body . id , 900 ); 167const instance = loaded . state . instances [ 0 ]; 168record ( 'the model loads with the picture reader' , loaded . job . state === 'done' && instance ?. status === 'ready' && instance ?. vision === true , ` ${ loaded . job . state } ${ loaded . job . error ?? '' } vision= ${ instance ?. vision } ` ); 169record ( 'the model loaded with pictures ON without being asked per load (default)' , instance ?. vision === true , '' ); 170const t0 = Date . now (); 171let local = { status :0 }; 172for ( let i = 0 ; i < 2 && ! ( local . text ?? '' ). trim (); i ++ ) local = await chat ({ nativeModelId :instance ?. id , maxTokens :4096 }); 173console . log ( ` answer from the local model ( ${ Math . round (( Date . now () - t0 ) / 1000 )} s): ${ JSON . stringify (( local . text ?? local . message ?? '' ). slice ( 0 , 300 ))} ` ); 174const low = ( local . text ?? '' ). toLowerCase (); 175record ( 'the local model answers about the picture (names the shapes)' , local . status === 200 && / red | circle / . test ( low ) && / blue | square / . test ( low ) && / green | triangle / . test ( low ), `status= ${ local . status } err= ${ local . error ?? '' } ` ); 176record ( 'the local model reads STRATA 42' , low . includes ( '42' )); 177// the same picture through the agent (the Harness route must declare image input: the "does not declare image input" error of the tester) 178if ( args . agent ) { // needs a Harness beside the host (the installed layout has one; a bare test box does not) 179const t1 = Date . now (); let a = { status :0 }; 180for ( let i = 0 ; i < 2 && ! /red | circle | square | triangle / . test (( a . text ?? '' ). toLowerCase ()); i ++ ) a = await chat ({ nativeModelId :instance ?. id , agent :true , sessionId :'pics-agent-' + i , mode :'plan' , maxTokens :4096 }); 181console . log ( ` answer through the agent ( ${ Math . round (( Date . now () - t1 ) / 1000 )} s): ${ JSON . stringify (( a . text ?? a . message ?? '' ). slice ( 0 , 300 ))} ` ); 182record ( 'the agent route reads the picture too' , a . status === 200 && / red | circle | square | triangle / . test (( a . text ?? '' ). toLowerCase ()) && ! /does not declare image input / . test ( a . text ?? '' ), `status= ${ a . status } err= ${ a . error ?? '' } ` ); 183} 184// OFF: loading with Read pictures off refuses a picture in plain words and says what to do 185await call ( `/api/native/instances/ ${ instance ?. id } /unload` , 'POST' , {}); 186const loadOff = await call ( '/api/native/operations' , 'POST' , { kind :'load' , args :{ model :model ?. model ?? want , vision :'no' } }); 187const loadedOff = await waitJob ( loadOff . body . id , 900 ); 188const off2 = loadedOff . state . instances [ 0 ]; 189record ( 'loaded with Read pictures OFF' , loadedOff . job . state === 'done' && off2 ?. vision === false , ` ${ loadedOff . job . state } vision= ${ off2 ?. vision } ` ); 190const refusedLocal = await chat ({ nativeModelId :off2 ?. id , maxTokens :64 }); 191console . log ( ` refusal: ${ JSON . stringify ( refusedLocal . message )} ` ); 192record ( 'a picture for the OFF model is refused with a next step' , refusedLocal . status === 409 && / Read pictures / . test ( refusedLocal . message ?? '' ) && / Models / . test ( refusedLocal . message ?? '' ), ` ${ refusedLocal . status } ` ); 193const unloaded = await call ( `/api/native/instances/ ${ off2 ?. id } /unload` , 'POST' , {}); 194record ( 'unload' , unloaded . status === 200 , unloaded . status ); 195} 196try { await main (); } catch ( e ) { record ( 'run' , false , e . stack ?? e ); } 197host . kill (); 198await delay ( 1500 ); 199console . log ( failed ?'PICS-E2E FAIL' :'PICS-E2E PASS' ); 200process . exit ( failed ?1 :0 );