okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1/** 2* The run config of a model (strata-<model>.json in the installation folder) is the text file a power user can edit by hand: 3* every engine option goes in "args", every STRATA_* environment flag in "env", the rest are named keys. Changes apply the next 4* time the model starts. This checks the file BEFORE the model starts and says what is wrong in plain words, with the line, so a 5* typo is never a start that just fails. 6* 7* Errors (the start is held back until they are fixed): the file is not valid JSON, or a key the app knows has a value it cannot use. 8* Warnings (the start goes on): a key nothing reads (probably a typo; the nearest real name is offered), an option the app manages itself. 9* 10* Each issue is { level, code, key, line, params, message }: `code` and `params` are what the screen translates (src/lib/run-config-issues.ts), 11* `message` is the same sentence in English for logs and the Details of a refused start. 12*/ 13import { NATIVE_FIELDS } from './native-settings.mjs' ; 14 15/** Every top-level key a run config may carry: what setup writes, what serve/server.py reads, the app's own fields. */ 16export const KNOWN_KEYS = new Set ([ 17 ...NATIVE_FIELDS . map ( f => f . id ), 18'exe' , 'args' , 'args_written' , 'user_args' , 'gpus_by' , 'cwd' , 'tokenizer' , 'model_name' , 'model' , 'arch' , 'log' , 'lib_dirs' , 'port' , 'host' , 'api_key' , 'cuda' , 'backend' , 'gpu' , 'gpus_asked' , 19'gpu_policy' , 'hip_ordinal' , 'env' , 'env_to_user' , 'sampling' , 'mcp_servers' , 'mcpServers' , 'before_load' , 'before_load_ignore_failure' , 20'before_load_strict' , 'before_load_timeout_s' , 'cors_origins' , 'trusted_origins' , 'allowed_hosts' , 'open_browser' , 'prevent_sleep' , 21'max_queue' , 'queue_timeout' , 'max_tokens' , 'min_tokens' , 'answer_room' , 'threads' , 'replicas' , 'replica_cpus' , 'replica_load_tokens' , 22'elastic' , 'elastic_idle_s' , 'elastic_mib' , 'min_free_vram_mib' , 'vram_segment_mib' , 'slot_save_path' , 'telemetry_read_key' , 'batch_telemetry' , 23'reasoning_effort' , 'reasoning_close_retry' , 'reasoning_loop_recovery' , 'repeat_stop_tokens' , 'tool_call_recovery' , 'codex_compaction_cache' , 24'codex_thread_titles' , 'expert_profile_save' , 'expert_profile_save_every' , 'images' , 'vision' , 'mmproj' , 'draft_vocab' , 'ui_resource_profile' , 25'ui_vision_enabled' , 'ui_vision_chosen' , 'ui_context_chosen' , 'component' , 'native' , 'uma' , 'remote_expert_opt' , 'diffusion_model' , 'llm' , 'vae' , 'quant' , 'type' , 'url' , 'timeout_s' , 26]); 27 28const BOOLS = new Set ([ 'open_browser' , 'prevent_sleep' , 'elastic' , 'env_to_user' , 'gpus_asked' , 'batch_telemetry' , 'tool_call_recovery' , 'reasoning_loop_recovery' ]); 29const NUMBERS = { port :[ 1 , 65535 ], max_queue :[ 0 , 1e6 ], queue_timeout :[ 0 , 1e6 ], threads :[ 1 , 4096 ], elastic_idle_s :[ 0 , 1e7 ], min_free_vram_mib :[ 0 , 1e7 ] }; 30const REASONING = [ 'none' , 'low' , 'medium' , 'high' ]; 31const SAMPLING = { temperature :[ 0 , 10 ], top_p :[ 0 , 1 ], top_k :[ 1 , 64 ], min_p :[ 0 , 1 ], presence_penalty :[ - 10 , 10 ], repetition_penalty :[ 0 , 10 ], repeat_penalty :[ 0 , 10 ], frequency_penalty :[ - 10 , 10 ], penalty_last_n :[ 0 , 1048576 ], repeat_last_n :[ 0 , 1048576 ], seed :[ 0 , 4294967295 ] }; 32const WHOLE = new Set ([ 'top_k' , 'penalty_last_n' , 'repeat_last_n' , 'seed' ]); 33 34function distance ( a , b ) { 35const row = Array . from ({ length :b . length + 1 }, ( _ , i ) => i ); 36for ( let i = 1 ; i <= a . length ; i ++ ) { 37let prev = row [ 0 ]; row [ 0 ] = i ; 38for ( let j = 1 ; j <= b . length ; j ++ ) { const keep = row [ j ]; row [ j ] = Math . min ( row [ j ] + 1 , row [ j - 1 ] + 1 , prev + ( a [ i - 1 ] === b [ j - 1 ] ?0 :1 )); prev = keep ; } 39} 40return row [ b . length ]; 41} 42/** The real key a mistyped one most likely meant, or null. */ 43export function nearestKey ( key , known = KNOWN_KEYS ) { 44let best = null , score = Infinity ; 45const lower = key . toLowerCase (). replace ( / [ - \s ] / g , '_' ); 46for ( const candidate of known ) { 47const d = lower === candidate ?0 :distance ( lower , candidate ); 48if ( d < score ) { score = d ; best = candidate ; } 49} 50return score <= Math . max ( 2 , Math . floor ( key . length / 4 )) ?best :null ; 51} 52 53const escape = s => s . replace ( / [ .*+?^${}()|[ \]\\ ] / g , '\\$&' ); 54/** 1-based line of a top-level key in the text, or null. */ 55function lineOf ( text , key ) { 56const m = new RegExp ( `^[ \\t]{0,8}" ${ escape ( key )} "\\s*:` , 'm' ). exec ( text ); 57return m ?text . slice ( 0 , m . index ). split ( '\n' ). length :null ; 58} 59 60const show = v => { const t = JSON . stringify ( v ); return t === undefined ?'nothing' :t . length > 40 ?` ${ t . slice ( 0 , 37 )} …` :t ; }; 61 62/** 63* `text` is the file as it is on disk. Returns { ok, issues } where ok means no errors (warnings may remain). Never throws. 64*/ 65export function checkRunConfig ( text , { known= KNOWN_KEYS } = {}) { 66text = String ( text ?? '' ); 67const issues = []; 68const add = ( level , code , key , params , message ) => issues . push ({ level, code, key, line :key ?lineOf ( text , key ) :null , params, message}); 69let config ; 70try { config = JSON . parse ( text . replace ( / ^ / , '' )); } 71catch ( e ) { 72const position = / position ( \d + ) / . exec ( e . message ?? '' ); 73const line = position ?text . slice ( 0 , Number ( position [ 1 ])). split ( '\n' ). length :null ; 74issues . push ({ level :'error' , code :'json' , key :null , line, params :{ line :line ?? 0 }, 75message :`The settings file is not valid JSON ${ line ? `; look near line ${ line } ` : '' } . Check for a missing comma between two entries, a comma after the last entry, or a missing quote.` }); 76return { ok :false , issues}; 77} 78if ( ! config || typeof config !== 'object' || Array . isArray ( config )) { 79issues . push ({ level :'error' , code :'not-object' , key :null , line :null , params :{}, message :'The settings file must hold one object, { … }, with the settings inside.' }); 80return { ok :false , issues}; 81} 82const fields = new Map ( NATIVE_FIELDS . map ( f => [ f . id , f ])); 83for ( const [ key , value ] of Object . entries ( config )) { 84if ( ! known . has ( key ) && ! key . startsWith ( 'ui_' )) { // ui_* are the app's own notes in the file (resource profile, picture device) 85const near = nearestKey ( key , known ); 86add ( 'warning' , near ?'unknown-key-near' :'unknown-key' , key , { key, near :near ?? '' }, `" ${ key } " is not a setting Strata reads, so it is ignored. ${ near ? ` Did you mean " ${ near } "?` : '' } ` ); 87continue ; 88} 89if ( value === null ) continue ; 90if ( key === 'args' ) { 91if ( ! Array . isArray ( value ) || value . some ( v => typeof v !== 'string' )) add ( 'error' , 'args-type' , key , {}, '"args" must be a list of quoted text entries, for example ["--ple-io", "mmap"]. Numbers need quotes too.' ); 92else if ( value . some ( v => [ '--host' , '--port' , '--api-key' ]. includes ( v ))) add ( 'warning' , 'args-managed' , key , {}, 'Strata manages the network address, port and API key itself, so --host, --port and --api-key in "args" are not honoured.' ); 93} else if ( key === 'env' ) { 94if ( ! value || typeof value !== 'object' || Array . isArray ( value )) add ( 'error' , 'env-type' , key , {}, '"env" must be an object of names with quoted text values, for example { "STRATA_X": "1" }.' ); 95else for ( const [ name , v ] of Object . entries ( value )) { 96if ( typeof v !== 'string' ) add ( 'error' , 'env-value' , key , { name}, `"env" entry ${ name } must be quoted text, for example "1", not ${ show ( v )} .` ); 97} 98} else if ( key === 'sampling' ) { 99if ( ! value || typeof value !== 'object' || Array . isArray ( value )) add ( 'error' , 'sampling-type' , key , {}, '"sampling" must be an object such as { "temperature": 0.7 }.' ); 100else for ( const [ name , n ] of Object . entries ( value )) { 101if ( n === null ) continue ; 102if ( name === 'experimental_speed_projection' ) { if ( typeof n !== 'boolean' ) add ( 'error' , 'bool' , key , { key :`sampling. ${ name } ` }, `"sampling. ${ name } " must be true or false.` ); continue ; } 103if ( ! SAMPLING [ name ]) { add ( 'warning' , 'unknown-key' , key , { key :`sampling. ${ name } ` , near :'' }, `"sampling. ${ name } " is not a setting Strata reads, so it is ignored.` ); continue ; } 104const [ lo , hi ] = SAMPLING [ name ]; 105if ( typeof n !== 'number' || ! Number . isFinite ( n ) || n < lo || n > hi || ( WHOLE . has ( name ) && ! Number . isInteger ( n ))) add ( 'error' , WHOLE . has ( name ) ?'whole-range' :'number-range' , key , { key :`sampling. ${ name } ` , min :lo , max :hi , value :show ( n ) }, `"sampling. ${ name } " must be ${ WHOLE . has ( name ) ? 'a whole ' : 'a ' } number from ${ lo } to ${ hi } , not ${ show ( n )} .` ); 106} 107} else if ( fields . has ( key )) { 108const f = fields . get ( key ); 109if ( value === '' ) continue ; 110if ( f . type === 'number' && ( typeof value !== 'number' || ! Number . isInteger ( value ) || value < f . min || value > f . max )) add ( 'error' , 'whole-range' , key , { key, min :f . min , max :f . max , value :show ( value ) }, `" ${ key } " must be a whole number from ${ f . min } to ${ f . max } , not ${ show ( value )} .` ); 111else if ( f . type === 'boolean' && typeof value !== 'boolean' ) add ( 'error' , 'bool' , key , { key, value :show ( value ) }, `" ${ key } " must be true or false, not ${ show ( value )} .` ); 112else if ( f . type === 'enum' && ! f . choices . includes ( value )) add ( 'error' , 'choice' , key , { key, choices :f . choices . join ( ', ' ), value :show ( value ) }, `" ${ key } " must be one of ${ f . choices . join ( ', ' )} , not ${ show ( value )} .` ); 113else if ( f . type === 'string' && ( typeof value !== 'string' || value . length > 256 )) add ( 'error' , 'text' , key , { key}, `" ${ key } " must be text of at most 256 characters.` ); 114else if ( f . type === 'array' ) { 115const list = typeof value === 'string' ?value . split ( ',' ). map ( v => v . trim ()). filter ( Boolean ) :value ; 116if ( ! Array . isArray ( list ) || list . length > 24 || list . some ( v => typeof v !== 'string' || ! v . trim () || v . length > 80 )) add ( 'error' , 'text' , key , { key}, `" ${ key } " must be a list of short names, for example ["chat", "coding"].` ); 117} 118if ( key === 'layer_split' && typeof value === 'string' && value !== 'auto' ) { 119const points = value . split ( ',' ). map ( Number ); 120if ( ! points . length || points . some (( n , i ) => ! Number . isInteger ( n ) || n < 1 || n >= 48 || ( i > 0 && n <= points [ i - 1 ]))) add ( 'error' , 'layer-split' , key , {}, '"layer_split" points must increase from 1 to 47, for example "16,32", or be "auto".' ); 121} 122} else if ( BOOLS . has ( key )) { 123if ( typeof value !== 'boolean' ) add ( 'error' , 'bool' , key , { key, value :show ( value ) }, `" ${ key } " must be true or false, not ${ show ( value )} .` ); 124} else if ( key in NUMBERS ) { 125const [ lo , hi ] = NUMBERS [ key ]; 126if ( typeof value !== 'number' || ! Number . isFinite ( value ) || value < lo || value > hi ) add ( 'error' , 'number-range' , key , { key, min :lo , max :hi , value :show ( value ) }, `" ${ key } " must be a number from ${ lo } to ${ hi } , not ${ show ( value )} .` ); 127} else if ( key === 'reasoning_effort' ) { 128if ( ! REASONING . includes ( value )) add ( 'error' , 'choice' , key , { key, choices :REASONING . join ( ', ' ), value :show ( value ) }, `"reasoning_effort" must be one of ${ REASONING . join ( ', ' )} , not ${ show ( value )} .` ); 129} else if ( key === 'gpu' ) { 130const ok = Number . isInteger ( value ) && value >= 0 || Array . isArray ( value ) && value . length > 0 && value . every ( n => Number . isInteger ( n ) && n >= 0 ); 131if ( ! ok ) add ( 'error' , 'gpu' , key , {}, '"gpu" must be a graphics card number such as 0, or a list such as [0, 1].' ); 132} 133} 134return { ok :! issues . some ( i => i . level === 'error' ), issues}; 135} 136 137/** Validate and parse one captured text value, so callers use the checked snapshot. */ 138export function checkAndParseRunConfig ( text , options ) { 139text = String ( text ?? '' ); 140const checked = checkRunConfig ( text , options ); 141if ( ! checked . ok ) return { ...checked , config :null }; 142return { ...checked , config :JSON . parse ( text . replace ( / ^\uFEFF / , '' )) }; 143} 144 145/** The English lines of the issues, `Line 12: …`, for the Details of a refused start. */ 146export function describeIssues ( issues , level ) { 147return issues . filter ( i => ! level || i . level === level ). map ( i => ` ${ i . line ? `Line ${ i . line } : ` : '' } ${ i . message } ` ); 148}