okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1/** 2* Does a model server read pictures? Asked of the server itself, cached for a few minutes: 3* - its model list (`/models`): `architecture.input_modalities`, `input_modalities`, `modalities`, or a `capabilities` list that names image / vision / multimodal; 4* - a llama-server's `/props` (`modalities.vision`), which is how `llama-server --mmproj` says it can read pictures (its `/v1/models` does not). 5* Unknown or unreachable counts as "no" (a picture sent to a server without a vision encoder makes the whole request fail), but the person can 6* always tick "This model reads pictures" in the model's settings: that answer wins over anything asked here. 7*/ 8const TTL_MS = 5 * 60_000 ; 9 10/** True / false / null (the row does not say) from one entry of a server's model list. */ 11export function modelReadsPictures ( row ) { 12if ( ! row || typeof row !== 'object' ) return null ; 13const lists = [ row . architecture ?. input_modalities , row . input_modalities , row . modalities ?. input , Array . isArray ( row . modalities ) ?row . modalities :null ]; 14for ( const list of lists ) if ( Array . isArray ( list )) return list . some ( x => / ^ ( image | vision | images ) $ / i . test ( String ( x ))); 15if ( row . modalities && typeof row . modalities === 'object' && typeof row . modalities . vision === 'boolean' ) return row . modalities . vision ; 16const caps = row . capabilities ?? row . meta ?. capabilities ; 17if ( Array . isArray ( caps )) return caps . some ( x => / vision | image | multimodal / i . test ( String ( x ))); 18if ( caps && typeof caps === 'object' && typeof caps . vision === 'boolean' ) return caps . vision ; 19return null ; 20} 21 22/** 23* Does a model list row say the model can think (reasoning)? OpenRouter lists `supported_parameters` per model: `reasoning` or 24* `include_reasoning` in it means the model takes a reasoning effort. True / false / null (the row does not say). 25*/ 26export function modelSupportsReasoning ( row ) { 27const list = row ?. supported_parameters ?? row ?. supportedParameters ; 28if ( ! Array . isArray ( list )) return null ; 29return list . some ( x => / ^ ( include_ ) ? reasoning$ / i . test ( String ( x ))); 30} 31 32/** 33* Pictures are ON by default (owner rule): a model whose server does not say either way is sent pictures; only an explicit "no" (the person's 34* own switch, or the server's own list or /props) turns them off. 35*/ 36export function readsPicturesByDefault ( model , asked ) { 37return effectiveVision ( model , asked ) ?? true ; 38} 39 40/** The effective answer for a saved model: what the person ticked, else what the server said (undefined: never asked). */ 41export function effectiveVision ( model , asked ) { 42if ( model ?. vision === true ) return true ; 43if ( model ?. vision === false ) return false ; 44return asked ; 45} 46 47export function createVisionProbe ({ fetcher= fetch , now= Date . now } = {}) { 48const cache = new Map (); // key -> { at, value } 49const keyOf = ( conn , modelId ) => ` ${ conn . id } | ${ modelId } ` ; 50return { 51/** Last known answer for a model (true / false), or undefined when never asked. */ 52known :( conn , modelId ) => { const e = cache . get ( keyOf ( conn , modelId )); return e ?e . value :undefined ; }, 53/** Whether the model takes a reasoning effort, as its server's list said (true / false), null when it did not say, undefined when never asked. */ 54knownThinks :( conn , modelId ) => { const e = cache . get ( keyOf ( conn , modelId )); return e ?e . thinks :undefined ; }, 55/** Ask now (at most once per TTL per model). Answers true / false / null (the server did not say). */ 56async check ( conn , modelId ) { 57const k = keyOf ( conn , modelId ); const hit = cache . get ( k ); 58if ( hit && now () - hit . at < TTL_MS ) return hit . value ; 59let value = null , thinks = null ; 60const base = String ( conn . baseUrl ). replace ( / \/ + $ / , '' ); 61const headers = conn . apiKey ?{ authorization :`Bearer ${ conn . apiKey } ` } :{}; 62const get = async path => { const res = await fetcher ( base + path , { headers, signal :AbortSignal . timeout ( 4000 ) }); return res . ok ?res . json () :null ; }; 63try { 64const list = ( await get ( '/models' ))?. data ?? []; 65const row = list . find ( m => m ?. id === modelId ) ?? ( list . length === 1 ?list [ 0 ] :null ); 66value = modelReadsPictures ( row ); 67thinks = modelSupportsReasoning ( row ); 68if ( value === null ) { // the list did not say: a llama-server tells in /props (next to /v1, not under it) 69try { 70const props = await ( async () => { const res = await fetcher ( base . replace ( / \/v1$ / , '' ) + '/props' , { headers, signal :AbortSignal . timeout ( 4000 ) }); return res . ok ?res . json () :null ; })(); 71value = props ?. modalities ?. vision === true ?true :props ?. modalities ?. vision === false ?false :null ; 72} catch { /* no /props: not a llama-server */ } 73} 74} catch { value = null ; } 75cache . set ( k , { at :now (), value, thinks}); 76return value ; 77}, 78}; 79}