okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
34e7c5cb1
main
1/** The engine options a person set by hand in a model's run config (strata_control/user_args.py is the same rule in Python). 2* "user_args" holds them; "args_written" is the whole "args" as setup or the app last wrote it, so an option in "args" that is not in 3* that copy was added or changed by hand since. Every load and every write puts the person's options over the generated ones. */ 4export const USER_KEY = 'user_args' , WRITTEN_KEY = 'args_written' ; 5/** Options the app or setup choose from their own answers (context, KV type, pictures, draft layer, layer split): never taken from "args" as hand edits. */ 6export const CHOICE_FLAGS = new Set ([ '--max-context' , '--kv' , '--vision' , '--mtp' , '--spec-min-p' , '--batch-mtp' , '--layer-split' ]); 7 8export function groups ( args ){ 9const out = []; 10for ( const raw of Array . isArray ( args ) ?args :[]){ const a = String ( raw ); if ( a . startsWith ( '--' ) ||! out . length ) out . push ([ a ]); else out [ out . length - 1 ]. push ( a );} 11return out ; 12} 13export const flat = gs => gs . flat (); 14export const flagOf = g => g ?.[ 0 ]?. startsWith ( '--' ) ?g [ 0 ] :'' ; 15/** `args` with every option the person set replaced by theirs (appended after the generated ones). */ 16export function merge ( args , user ){ 17const ug = groups ( user ), mine = new Set ( ug . map ( flagOf ). filter ( Boolean )); 18return flat ([ ...groups ( args ). filter ( g =>! mine . has ( flagOf ( g ))), ...ug ]); 19} 20function byFlag ( gs ){ const m = new Map (); for ( const g of gs ){ const f = flagOf ( g ); if ( ! m . has ( f )) m . set ( f ,[]); m . get ( f ). push ( g );} return m ;} 21const same = ( a , b ) => JSON . stringify ( a ?? null ) === JSON . stringify ( b ?? null ); 22/** The person's options of a config: its user_args, with the options of "args" that differ from "args_written" on top. */ 23export function capture ( cfg , skip = CHOICE_FLAGS ){ 24const mine = byFlag ( groups ( cfg ?.[ USER_KEY ])); 25if ( Array . isArray ( cfg ?.[ WRITTEN_KEY ]) && Array . isArray ( cfg ?. args )){ 26const cur = byFlag ( groups ( cfg . args )), was = byFlag ( groups ( cfg [ WRITTEN_KEY ])); 27for ( const [ flag , gs ] of cur ) if ( flag &&! skip . has ( flag ) &&! same ( was . get ( flag ), gs )) mine . set ( flag , gs ); 28for ( const flag of [ ...mine . keys ()]) if ( was . has ( flag ) &&! cur . has ( flag )) mine . delete ( flag ); 29} 30return flat ([ ...mine . values ()]. flat ()); 31} 32export function setFlag ( user , flag , value ){ 33const rest = groups ( user ). filter ( g => flagOf ( g ) !== flag ); 34if ( value !== null && value !== undefined ) rest . push ([ flag , String ( value )]); 35return flat ( rest ); 36} 37export function flagValue ( args , flag ){ 38const list = ( Array . isArray ( args ) ?args :[]). map ( String ), at = list . indexOf ( flag ); 39return at >= 0 && at + 1 < list . length ?list [ at + 1 ] :null ; 40} 41/** `config` with the person's options merged into "args", recorded in "user_args", and "args_written" refreshed (only when the config has one). */ 42export function stamped ( config , user ){ 43const next = { ...config }; 44if ( user . length ){ next . args = merge ( next . args , user ); next [ USER_KEY ] = user ;} else delete next [ USER_KEY ]; 45if ( WRITTEN_KEY in config ) next [ WRITTEN_KEY ] = [ ...( next . args ?? [])]; 46return next ; 47}