okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import { tr } from '../lib/i18n' ; 2import { useRef , useState } from 'react' ; 3import { Btn , Icon , Select } from './ui' ; 4import { draftParams , readConfig , type ConfigDocument } from '../lib/config' ; 5import type { EngineConfig , ExpertParam } from '../lib/types' ; 6export function RunExpertEditor ({ engine, config, text, onChange, onText, onRestore, onError} :{ 7engine :EngineConfig ; 8config :ConfigDocument | null ; 9text :string ; 10onChange :( config :ConfigDocument ) => void ; 11onText :( text :string ) => void ; 12onRestore :() => void ; 13onError :( message :string ) => void ; 14}) { 15const [ tab , setTab ] = useState ( 'parameters' ); 16const [ query , setQuery ] = useState ( '' ); 17const [ group , setGroup ] = useState ( 'all' ); 18const [ changedOnly , setChangedOnly ] = useState ( false ); 19const [ unsupported , setUnsupported ] = useState ( false ); 20const [ descriptions , setDescriptions ] = useState ( false ); 21const file = useRef < HTMLInputElement >( null ); 22const params = config ?draftParams ( engine , config ) :[]; 23const changed = params . filter ( p => p . value !== p . recommended ); 24const groups = [ ...new Set ( params . map ( p => p . group ))]; 25const rows = params . filter ( p => p . id !== 'server.host' && ( unsupported || !! p . applies && ! p . applies . startsWith ( 'Preserved' ))) 26. filter ( p => ( group === 'all' || p . group === group ) && ( ! changedOnly || p . value !== p . recommended ) && ` ${ p . label } ${ p . id } ${ p . effect } ` . toLowerCase (). includes ( query . toLowerCase ())); 27function changeParam ( id :string , value :ExpertParam [ 'value' ]) { 28if ( ! config ) 29return ; 30const next :ConfigDocument = { ...config , beginner :{ ...config . beginner }, expert :{ ...config . expert , [ id ] :value } }; 31if ( id === 'ctx.length' ) 32next . beginner . contextLength = String ( value ). toLowerCase () as ConfigDocument [ 'beginner' ][ 'contextLength' ]; 33if ( id === 'perf.batch' ) 34next . batchAutomatic = false ; 35onChange ( next ); 36} 37function download () { 38const url = URL . createObjectURL ( new Blob ([ text ], { type :'application/json' })); 39const link = document . createElement ( 'a' ); 40link . href = url ; 41link . download = 'strata-run-config.json' ; 42link . click (); 43URL . revokeObjectURL ( url ); 44} 45return < section className = "run-expert run-card" aria-label = { tr ( "Expert settings" )}> 46< div className = "run-card__head" >< span >< Icon name = "sliders" size = { 19 }/>< h2 >{ tr ( "Expert settings" )}</ h2 ></ span >< span className = "xs muted" >{ tr ( changed . length )}{ tr ( " custom values" )}</ span ></ div > 47< div className = "run-expert__tabs" role = "tablist" aria-label = { tr ( "Expert editor" )}>{ tr ([ 'parameters' , 'json' ]. map ( item => < button key = { item } role = "tab" aria-selected = { tab === item } aria-controls = { `run-editor- ${ item } ` } id = { `run-tab- ${ item } ` } onClick = {() => setTab ( item )}>{ tr ( item === 'parameters' ?<>< Icon name = "sliders" size = { 15 }/>{ tr ( "Parameters" )}</> :<>< Icon name = "file" size = { 15 }/>{ tr ( "JSON" )}</>)}</ button >))}</ div > 48< div role = "tabpanel" id = { `run-editor- ${ tab } ` } aria-labelledby = { `run-tab- ${ tab } ` }> 49{ tr ( tab === 'json' ?< div className = "stack" > 50< div className = "row" >< span className = "small muted" >{ tr ( "One configuration for every control." )}</ span >< span className = "spacer" />< Btn size = "sm" onClick = { download }>{ tr ( "Export" )}</ Btn >< Btn size = "sm" onClick = {() => file . current ?. click ()}>{ tr ( "Import" )}</ Btn ></ div > 51< textarea className = "st-input config-editor" spellCheck = { false } aria-label = { tr ( "Run configuration JSON" )} value = { text } onChange = { e => onText ( e . target . value )}/> 52</ div > :<> 53< div className = "run-expert__filter" >< input className = "st-input" type = "search" aria-label = { tr ( "Search engine parameters" )} placeholder = { tr ( "Search parameters…" )} value = { query } onChange = { e => setQuery ( e . target . value )}/>< Select ariaLabel = { tr ( "Parameter group" )} value = { group } options = {[{ value :'all' , label :'All groups' }, ...groups . map ( g => ({ value :g , label :g }))]} onChange = { setGroup }/></ div > 54< div className = "run-expert__options" >< label >< input type = "checkbox" checked = { changedOnly } onChange = { e => setChangedOnly ( e . target . checked )}/>{ tr ( "Changed only" )}</ label >< label >< input type = "checkbox" checked = { descriptions } onChange = { e => setDescriptions ( e . target . checked )}/>{ tr ( "Descriptions" )}</ label >< label >< input type = "checkbox" checked = { unsupported } onChange = { e => setUnsupported ( e . target . checked )}/>{ tr ( "Unsupported" )}</ label ></ div > 55{ tr ( groups . filter ( g => rows . some ( p => p . group === g )). map ( g => < section className = "run-expert__group" key = { g }>< h3 >{ tr ( g )}</ h3 >{ tr ( rows . filter ( p => p . group === g ). map ( p => { 56const disabled = ! p . applies || p . applies . startsWith ( 'Preserved' ); 57return < div className = "run-expert__row" key = { p . id } data-changed = { p . value !== p . recommended || undefined }>< div >< label htmlFor = { `run-param- ${ p . id } ` }>{ tr ( p . label )}</ label >< code >{ p . id }</ code >{ tr ( descriptions ?< p className = "xs muted" >{ tr ( p . effect )}</ p > :null )}< p className = "xs muted" >{ tr ( disabled ?'Not supported by this engine' :p . applies )}</ p ></ div >< div className = "run-expert__control" >< ExpertInput param = { p } disabled = { disabled } onChange = { value => changeParam ( p . id , value )}/>< button className = "run-field-reset" title = { tr ( "Reset {0} to recommended" , [ tr ( p . label )])} aria-label = { tr ( "Reset {0} to recommended" , [ tr ( p . label )])} disabled = { disabled || p . value === p . recommended } onClick = {() => changeParam ( p . id , p . recommended )}>< Icon name = "refresh" size = { 15 }/></ button ></ div ></ div >; 58}))}</ section >))} 59{ tr ( ! rows . length ?< p className = "small muted" >{ tr ( "No parameters match your filters." )}</ p > :null )} 60</>)} 61</ div > 62< div className = "run-expert__foot" >< Icon name = "info" size = { 15 }/>< span className = "xs muted" >{ tr ( "Load settings take effect when you run the model." )}</ span >< Btn size = "sm" variant = "ghost" onClick = { onRestore }>{ tr ( "Restore saved settings" )}</ Btn ></ div > 63< input ref = { file } type = "file" accept = ".json,application/json" hidden onChange = { e => { 64const selected = e . target . files ?.[ 0 ]; 65e . target . value = '' ; 66if ( selected ) 67void selected . text (). then ( value => { onChange ( readConfig ( value )); setTab ( 'json' ); }). catch ( error => onError ( error . message )); 68}}/> 69</ section >; 70} 71function ExpertInput ({ param, disabled, onChange} :{ 72param :ExpertParam ; 73disabled :boolean ; 74onChange :( value :ExpertParam [ 'value' ]) => void ; 75}) { 76if ( param . type === 'select' && param . options ) 77return < Select ariaLabel = { tr ( param . label )} value = { String ( param . value )} disabled = { disabled } options = { param . options . map ( value => ({ value, label :value }))} onChange = { onChange }/>; 78if ( param . type === 'toggle' ) 79return < button className = "st-toggle" role = "switch" aria-label = { tr ( param . label )} aria-checked = { !! param . value } disabled = { disabled } onClick = {() => onChange ( ! param . value )}/>; 80return < input id = { `run-param- ${ param . id } ` } className = "st-input" disabled = { disabled } aria-label = { tr ( param . label )} inputMode = { param . type === 'number' ?'decimal' :undefined } value = { String ( param . value )} onChange = { e => { const v = e . target . value ; onChange ( param . type === 'number' && v . trim () !== '' && Number . isFinite ( Number ( v )) ?Number ( v ) :v ); }}/>; 81}