okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { mkdtempSync , rmSync } from 'node:fs' ; 4import { tmpdir } from 'node:os' ; 5import { join } from 'node:path' ; 6import { createVisionProbe , effectiveVision , modelReadsPictures } from '../server/vision-probe.mjs' ; 7import { createModelProviders } from '../server/model-providers.mjs' ; 8 9const answer = ( routes , seen = []) => async ( url , init ) => { 10const path = new URL ( url ). pathname ; 11seen . push ( path ); 12const body = routes [ path ]; 13return body === undefined ?{ ok :false , status :404 , json :async () => ({}) } :{ ok :true , status :200 , json :async () => body }; 14}; 15 16test ( 'a model list row says whether the model reads pictures in every common shape' , () => { 17assert . equal ( modelReadsPictures ({ architecture :{ input_modalities :[ 'text' , 'image' ] } }), true ); 18assert . equal ( modelReadsPictures ({ architecture :{ input_modalities :[ 'text' ] } }), false ); 19assert . equal ( modelReadsPictures ({ modalities :{ input :[ 'text' , 'image' ] } }), true ); 20assert . equal ( modelReadsPictures ({ capabilities :[ 'completion' , 'vision' ] }), true ); 21assert . equal ( modelReadsPictures ({ capabilities :{ vision :false } }), false ); 22assert . equal ( modelReadsPictures ({ id :'x' }), null ); 23assert . equal ( modelReadsPictures ( null ), null ); 24}); 25 26test ( 'what the person ticked wins over what the server said' , () => { 27assert . equal ( effectiveVision ({ vision :true }, false ), true ); 28assert . equal ( effectiveVision ({ vision :false }, true ), false ); 29assert . equal ( effectiveVision ({ vision :null }, true ), true ); 30assert . equal ( effectiveVision ({}, undefined ), undefined ); 31}); 32 33test ( 'a llama-server started with --mmproj says so in /props, next to /v1, and the answer is cached' , async () => { 34const seen = []; 35const probe = createVisionProbe ({ fetcher :answer ({ '/v1/models' :{ data :[{ id :'smol' }] }, '/props' :{ modalities :{ vision :true } } }, seen ) }); 36const conn = { id :'c1' , baseUrl :'http://127.0.0.1:8080/v1' }; 37assert . equal ( probe . known ( conn , 'smol' ), undefined ); 38assert . equal ( await probe . check ( conn , 'smol' ), true ); 39assert . equal ( await probe . check ( conn , 'smol' ), true ); 40assert . equal ( probe . known ( conn , 'smol' ), true ); 41assert . deepEqual ( seen , [ '/v1/models' , '/props' ]); 42}); 43 44test ( 'a text-only server counts as not reading pictures; an unreachable one is unknown (pictures stay on by default)' , async () => { 45const text = createVisionProbe ({ fetcher :answer ({ '/v1/models' :{ data :[{ id :'m' }] }, '/props' :{ modalities :{ vision :false } } }) }); 46assert . equal ( await text . check ({ id :'c' , baseUrl :'http://h/v1' }, 'm' ), false ); 47const down = createVisionProbe ({ fetcher :async () => { throw new Error ( 'refused' ); } }); 48assert . equal ( await down . check ({ id :'c' , baseUrl :'http://h/v1' }, 'm' ), null ); 49}); 50 51test ( 'a provider list that names image input is believed without asking /props' , async () => { 52const seen = []; 53const probe = createVisionProbe ({ fetcher :answer ({ '/v1/models' :{ data :[{ id :'gpt' , architecture :{ input_modalities :[ 'text' , 'image' ] } }] } }, seen ) }); 54assert . equal ( await probe . check ({ id :'c' , baseUrl :'https://api.example.com/v1' }, 'gpt' ), true ); 55assert . deepEqual ( seen , [ '/v1/models' ]); 56}); 57 58test ( 'the tick "This model reads pictures" is saved with the model and survives a reload' , t => { 59const directory = mkdtempSync ( join ( tmpdir (), 'strata-vision-' )); 60t . after (() => rmSync ( directory , { recursive :true , force :true })); 61const service = createModelProviders ({ directory}); 62const saved = service . save ({ kind :'custom' , name :'Other PC' , baseUrl :'http://192.168.1.20:8080/v1' , protocol :'openai-chat' , apiKey :'' , models :[{ id :'smol' , name :'SmolVLM' , vision :true }, { id :'text' , name :'Text only' }] }). connections [ 0 ]; 63assert . deepEqual ( saved . models . map ( m => m . vision ), [ true , null ]); 64const again = createModelProviders ({ directory}). list (). connections [ 0 ]; 65assert . deepEqual ( again . models . map ( m => m . vision ), [ true , null ]); 66});