okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import type { BeginnerConfig , EngineConfig , ExpertParam } from './types' ; 2 3/** Portable saved configuration. Metadata stays separate from editable values. */ 4export interface ConfigDocument { 5schema :'strata.engine/1' ; 6beginner :BeginnerConfig ; 7expert :Record < string , string | number | boolean >; 8[ key :string ] :unknown ; 9} 10 11export function configDocument ( engine :EngineConfig ) :ConfigDocument { 12return { 13 ...( engine . extra ?? {}), 14schema :'strata.engine/1' , 15beginner :{ ...engine . beginner }, 16expert :{ ...Object . fromEntries ( engine . expert . map ( p => [ p . id , p . value ])), 'ctx.length' :engine . beginner . contextLength === 'recommended' ?'128K' :engine . beginner . contextLength . toUpperCase () }, 17}; 18} 19 20export function readConfig ( text :string ) :ConfigDocument { 21let value :Record < string , unknown >; 22try { value = JSON . parse ( text ); } 23catch ( error ) { 24const offset = jsonErrorOffset ( text ); 25const before = text . slice ( 0 , offset ); 26const line = before . split ( '\n' ). length , column = offset - before . lastIndexOf ( '\n' ); 27throw new Error ( `Line ${ line } , column ${ column } : ${ error instanceof Error ? error . message : 'Invalid JSON' } ` ); 28} 29if ( ! value || typeof value !== 'object' || Array . isArray ( value )) throw new Error ( 'Configuration must be a JSON object.' ); 30if ( ! value . beginner || typeof value . beginner !== 'object' || Array . isArray ( value . beginner )) throw new Error ( 'Add a beginner settings object.' ); 31if ( ! value . expert || typeof value . expert !== 'object' ) throw new Error ( 'Add an expert settings object.' ); 32// Accept the original array export when opening older saved files. 33const expert = Array . isArray ( value . expert ) 34 ?Object . fromEntries ( value . expert . map ( p => [ p . id , p . value ])) 35 :value . expert ; 36for ( const [ key , v ] of Object . entries ( expert )) { 37if ( ! [ 'string' , 'number' , 'boolean' ]. includes ( typeof v )) throw new Error ( ` ${ key } : use a text, number or true/false value.` ); 38} 39const { model :_model , exportedAt :_time , ...rest } = value ; 40return { ...rest , schema :'strata.engine/1' , beginner :value . beginner as BeginnerConfig , expert} as ConfigDocument ; 41} 42 43export function configPatch ( doc :ConfigDocument ) { 44const { schema :_schema , beginner, expert, ...extra } = doc ; 45return { beginner, expert :Object . entries ( expert ). map (([ id , value ]) => ({ id, value})), extra, replaceExpert :true }; 46} 47 48export function draftParams ( engine :EngineConfig , doc :ConfigDocument ) :ExpertParam [] { 49const known = new Set ( engine . expert . map ( p => p . id )); 50return [ 51 ...engine . expert . map ( p => ({ ...p , value :doc . expert [ p . id ] ?? p . value })), 52 ...Object . entries ( doc . expert ). filter (([ id ]) => ! known . has ( id )). map (([ id , value ]) => ({ 53 id, value, recommended :value , label :id , group :'Additional settings' , 54effect :'Preserved; no verified engine mapping in this build.' , 55type :( typeof value === 'boolean' ?'toggle' :typeof value === 'number' ?'number' :'text' ) as ExpertParam [ 'type' ], 56})), 57]; 58} 59 60/** Locate the first grammar error consistently across browser JSON.parse messages. */ 61export function jsonErrorOffset ( text :string ) :number { 62let i = 0 ; 63const whitespace = () => { while ( / [\t\r\n ] / . test ( text [ i ] ?? '!' )) i ++ ;}; 64const fail = () => { throw i ;}; 65const string = () => { 66if ( text [ i ++ ] !== '"' ) { i -- ; fail ();} 67while ( i < text . length ){ 68const c = text [ i ++ ]; 69if ( c === '"' ) return ; 70if ( c . charCodeAt ( 0 ) < 32 ){ i -- ; fail ();} 71if ( c === '\\' ){ 72const escape = text [ i ++ ]; 73if ( escape === 'u' ){ for ( let j = 0 ; j < 4 ; j ++ ) if ( ! /[a-fA-F0-9] / . test ( text [ i ++ ] ?? '!' )){ i -- ; fail ();}} 74else if ( ! escape ||! [ '"' , '\\' , '/' , 'b' , 'f' , 'n' , 'r' , 't' ]. includes ( escape )){ i -- ; fail ();} 75} 76} 77fail (); 78}; 79const value = () => { 80whitespace (); const c = text [ i ]; 81if ( c === '"' ){ string (); return ;} 82if ( c === '{' || c === '[' ){ 83const object = c === '{' , end = object ?'}' :']' ; i ++ ; whitespace (); if ( text [ i ] === end ){ i ++ ; return ;} 84while ( i < text . length ){ 85if ( object ){ string (); whitespace (); if ( text [ i ++ ] !== ':' ){ i -- ; fail ();}} 86value (); whitespace (); if ( text [ i ] === end ){ i ++ ; return ;} 87if ( text [ i ++ ] !== ',' ){ i -- ; fail ();} whitespace (); 88} fail (); 89} 90const match = text . slice ( i ). match ( / ^(?:true|false|null|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?) / ); 91if ( ! match ) fail (); else i += match [ 0 ]. length ; 92}; 93try { value (); whitespace (); if ( i !== text . length ) fail ();} catch { return Math . min ( i , text . length );} 94return i ; 95}