okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f5ef26889
main
1import { tr } from '../lib/i18n' ; 2import { modelFamilyIcon } from '../lib/model-icons' ; 3import { useId , useRef , useState } from 'react' ; 4import { FloatingMenu } from './FloatingMenu' ; 5import { Bar , Btn , Dialog , ErrorNotice , Icon } from './ui' ; 6import { go , useLifecycle , loadVariant , selectRemoteModel , setSettingsSection , useStore } from '../lib/store' ; 7import { pickerGroups , pickerNativeModels , pickerVersionState } from '../lib/model-picker' ; 8import { versionFit } from '../lib/model-versions' ; 9import type { ModelVariant } from '../lib/types' ; 10import { sizeGb } from '../lib/format' ; 11import { contextTokens } from '../lib/experience' ; 12import { nativeAction , useNative } from '../lib/native' ; 13/** The loaded model is shared by all chats; switching uses the existing lifecycle gates. */ 14export function ChatModelPicker () { 15const native = useNative (), remote = useStore ( s => s . remoteModel ); 16return native ?. instances . length && ! remote ?< NativeChatModelPicker /> :< LegacyChatModelPicker />; 17} 18function NativeChatModelPicker () { 19const native = useNative (), providers = useStore ( s => s . providers ), streaming = useStore ( s => s . streaming ); 20const [ open , setOpen ] = useState ( false ), [ error , setError ] = useState ( '' ); 21const ref = useRef < HTMLButtonElement >( null ); 22const current = native ?. instances . find ( i => i . id === native . defaultId ); 23return <>< button className = "chat-model-badge chat-model-picker" ref = { ref } aria-label = { tr ( "Choose model: {0}" , [ current ?. label ?? 'Native model' ])} aria-haspopup = "menu" aria-expanded = { open } onClick = {() => setOpen ( v => ! v )}>< Icon name = "models" size = { 16 }/>< span >{ tr ( current ?. label ?? 'Native model' )}</ span >< Icon name = "chevron" size = { 12 }/></ button >{ tr ( open ?< FloatingMenu anchor = { ref . current } title = { tr ( "Chat model" )} width = { 340 } onClose = {() => setOpen ( false )}>{ tr ( native ?. instances . filter ( i => i . status !== 'error' ). map ( i => < button className = "menu__item" role = "menuitemradio" aria-checked = { current ?. id === i . id } disabled = { streaming || i . status !== 'ready' } key = { i . id } onClick = {() => void nativeAction ( `instances/ ${ i . id } /default` ). then (() => setOpen ( false )). catch ( e => setError ( e . message ))}>< Icon name = "models" />< span >< strong >{ tr ( i . label )}</ strong >< small >{ tr ( i . gpus . map ( g => `GPU ${ g + 1 } ` ). join ( " + " ) || "Automatic GPU" )}</ small ></ span >{ tr ( current ?. id === i . id ?< Icon name = "check" size = { 16 } className = "menu__check" /> :null )}</ button >))}{ tr ( providers ?. connections . flatMap ( c => c . models . map ( m => < button className = "menu__item" role = "menuitem" disabled = { streaming } key = { ` ${ c . id } - ${ m . id } ` } onClick = {() => { selectRemoteModel ({ connectionId :c . id , modelId :m . id }); setOpen ( false ); }}>< Icon name = "link" />{ m . name } · { c . name }</ button >)))}< button className = "menu__item" role = "menuitem" onClick = {() => { setOpen ( false ); go ( 'models' ); }}>{ tr ( "Manage models" )}</ button >{ tr ( error ?< p className = "small inline-error" >{ tr ( error )}</ p > :null )}</ FloatingMenu > :null )}</>; 24} 25function LegacyChatModelPicker () { 26const native = useNative (); 27const library = useStore ( s => s . library ), hardware = useStore ( s => s . hardware ), engine = useStore ( s => s . engine ); 28const providers = useStore ( s => s . providers ), remote = useStore ( s => s . remoteModel ), streaming = useStore ( s => s . streaming ); 29const connection = providers ?. connections . find ( c => c . id === remote ?. connectionId ); 30const apiModel = connection ?. models . find ( m => m . id === remote ?. modelId ); 31const lifecycle = useLifecycle (); 32const [ open , setOpen ] = useState ( false ), [ selected , setSelected ] = useState < string | null >( null ); 33const [ pending , setPending ] = useState ( false ), [ error , setError ] = useState ( '' ); 34const [ expanded , setExpanded ] = useState < string []>([]); 35const groupsId = useId (); 36const trigger = useRef < HTMLButtonElement >( null ); 37const running = library ?. variants . find ( v => v . id === library . loaded ); 38const version = library ?. variants . find ( v => v . id === selected ); 39const groups = library && library . mode !== 'real' ?pickerGroups ( library ) :[]; // real engine: its own models (installedNative) only 40const installedNative = pickerNativeModels ( native ?. installed ?. models , native ?. instances ); 41const status = version ?pickerVersionState ( version , library ?. loaded ?? null ) :null ; 42const tooBig = !! version && library ?. mode === 'real' && versionFit ( version , hardware , contextTokens ( engine ?. beginner . contextLength ?? '32K' )). zone === 'too-big' ; 43async function apply () { 44if ( ! version ) 45return ; 46setPending ( true ); 47setError ( '' ); 48try { 49const success = await loadVariant ( version . id ); 50if ( ! success ) 51throw new Error ( 'The version could not load. Try again when the engine is ready.' ); 52selectRemoteModel ( null ); 53setSelected ( null ); 54} 55catch ( e ) { 56setError ( e instanceof Error ?e . message :String ( e )); 57} 58finally { 59setPending ( false ); 60} 61} 62function chooseVersion ( v :ModelVariant ) { 63setOpen ( false ); 64if ( pickerVersionState ( v , library ?. loaded ?? null ). running ) 65selectRemoteModel ( null ); 66else { 67setSelected ( v . id ); 68setError ( '' ); 69} 70} 71function versionOption ( v :ModelVariant ) { 72const state = pickerVersionState ( v , library ?. loaded ?? null ); 73return < button key = { v . id } role = "menuitemradio" disabled = { streaming || state . busy } aria-checked = { state . running && ! apiModel } className = "menu__item model-picker-option" onClick = {() => chooseVersion ( v )}> 74< span >< strong >{ tr ( v . name )}</ strong >< span className = "model-picker-spec" > · { tr ( v . quant )} · { tr ( sizeGb ( v . diskGb ))}</ span ></ span > 75< span className = "model-picker-state" >{ tr ( state . label )}</ span > 76</ button >; 77} 78return <> 79< button ref = { trigger } className = "chat-model-badge chat-model-picker" aria-label = { tr ( "Choose model: {0}" , [ apiModel ?. name ?? ( running ?. local ?running . name :running ?. family ) ?? 'No model loaded' ])} title = { tr ( apiModel ?` ${ connection ?. name } · ${ apiModel . id } ` :'View and switch models' )} disabled = { ! library && ! providers } aria-haspopup = "menu" aria-expanded = { open } onClick = {() => setOpen ( v => ! v )}>< Icon name = { apiModel ?'link' :modelFamilyIcon ( running ?. family ?? groups [ 0 ]?. family )} size = { 16 }/>< span >{ tr ( apiModel ?. name ?? ( running ?. local ?running . name :running ?. family ) ?? groups [ 0 ]?. family ?? 'No model loaded' )}</ span >< Icon name = "chevron" size = { 12 }/></ button > 80{ tr ( open ?< FloatingMenu anchor = { trigger . current } title = { tr ( "Models" )} width = { 380 } onClose = {() => setOpen ( false )}> 81{ tr ( native ?. instances . filter ( i => i . status !== 'error' ). map ( i => < button key = { i . id } className = "menu__item" role = "menuitem" disabled = { streaming || i . status !== 'ready' } onClick = {() => void nativeAction ( `instances/ ${ i . id } /default` ). then (() => { selectRemoteModel ( null ); setOpen ( false ); }). catch ( e => setError ( e . message ))}>< Icon name = "models" />< span >{ tr ( i . label )}< small >{ tr ( i . gpus . map ( g => `GPU ${ g + 1 } ` ). join ( " + " ) || "Automatic GPU" )}</ small ></ span ></ button >))}< p className = "model-picker-heading" >{ tr ( "Local models" )}</ p > 82{ tr ( installedNative . map ( m => < button key = { m . config || m . model } className = "menu__item model-picker-option model-picker-api" role = "menuitem" disabled = { streaming } onClick = {() => { setOpen ( false ); go ( 'models' ); }}>< span >< strong >{ tr ( m . model_name || m . model )}</ strong >< small >{ tr ( "Downloaded, not loaded. Open Models to load it." )}</ small ></ span ></ button >))} 83{ tr ( ! groups . length && ! installedNative . length ?< p className = "model-picker-empty" >{ tr ( "No local models downloaded yet." )}</ p > :null )} 84{ tr ( groups . map (({ family, versions}, index ) => { 85const main = versions [ 0 ], state = pickerVersionState ( main , library ! . loaded ); 86const others = versions . slice ( 1 ), isExpanded = expanded . includes ( family ), id = ` ${ groupsId } - ${ index } ` ; 87return < div key = { family } role = "group" aria-label = { tr ( family )} className = "model-picker-group" > 88< div className = "model-picker-family-row" > 89< button role = "menuitemradio" disabled = { streaming || state . busy } aria-checked = { state . running && ! apiModel } className = "menu__item model-picker-family" onClick = {() => chooseVersion ( main )}> 90< Icon name = { modelFamilyIcon ( family )} size = { 20 }/> 91< span className = "model-picker-family-copy" >< strong >{ tr ( family )}</ strong >< small >< span >{ tr ( main . name )}</ span >< span >{ tr ( main . quant )} · { tr ( sizeGb ( main . diskGb ))}</ span ></ small ></ span > 92< span className = "model-picker-state" >{ tr ( state . running && ! apiModel ?< Icon name = "check" size = { 14 }/> :null )}{ tr ( state . label )}</ span > 93</ button > 94{ tr ( others . length ?< button role = "menuitem" className = "menu__item model-picker-expand" aria-label = { tr ( "Downloaded versions for {0}" , [ family ])} aria-expanded = { isExpanded } aria-controls = { isExpanded ?id :undefined } onClick = {() => setExpanded ( current => isExpanded ?current . filter ( f => f !== family ) :[ ...current , family ])}>< Icon name = "chevron" size = { 14 }/></ button > :null )} 95</ div > 96{ tr ( isExpanded && others . length ?< div id = { id } className = "model-picker-versions" >{ tr ( others . map ( versionOption ))}</ div > :null )} 97</ div >; 98}))} 99{ tr ( providers ?. connections . map ( c => < div key = { c . id }>< p className = "model-picker-heading" >{ c . name }</ p >{ tr ( c . models . map ( m => < button key = { m . id } role = "menuitemradio" disabled = { streaming } aria-checked = { remote ?. connectionId === c . id && remote . modelId === m . id } className = "menu__item model-picker-option model-picker-api" onClick = {() => { selectRemoteModel ({ connectionId :c . id , modelId :m . id }); setOpen ( false ); }}>< span >< strong >{ m . name }</ strong >< small >{ tr ( m . id )}</ small ></ span >< span className = "model-picker-state" >{ tr ( remote ?. connectionId === c . id && remote . modelId === m . id ?< Icon name = "check" size = { 15 }/> :'API' )}</ span ></ button >))}</ div >))} 100< button role = "menuitem" className = "menu__item model-picker-manage" onClick = {() => { setOpen ( false ); setSettingsSection ( 'Models' ); go ( 'settings' ); }}>< Icon name = "plus" size = { 16 }/>{ tr ( "Manage API models" )}</ button > 101</ FloatingMenu > :null )} 102< Dialog open = { !! version } compact title = { tr ( status ?. busy ?` ${ status . label } ${ version ?. name } ` :`Switch to ${ version ?. name } ?` )} onClose = {() => ! pending && setSelected ( null )} actions = {<>< Btn disabled = { pending } onClick = {() => setSelected ( null )}>{ tr ( "Cancel" )}</ Btn >< Btn variant = "primary" disabled = { pending || status ?. busy || ! status ?. installed || tooBig || ! lifecycle . allowed } onClick = {() => void apply ()}>{ tr ( pending ?'Working…' :status ?. busy ?status . label :status ?. action )}</ Btn ></>}> 103{ tr ( version ?< div className = "stack stack--sm" >< p >< strong >{ tr ( version . family )}</ strong >< br />< span className = "small muted" >{ tr ( version . name )} · { tr ( version . quant )} · { tr ( sizeGb ( version . diskGb ))}</ span ></ p > 104< p className = "small" >{ tr ( "This switches the model version for all chats. Your conversations stay saved." )}</ p > 105{ tr ( status ?. busy ?< Bar value = { version . progress ?. pct ?? 0 } label = { tr ( version . progress ?. stage ?? status . label )}/> :null )} 106{ tr ( tooBig ?< p className = "small inline-error" >{ tr ( "This version needs more memory at the current context. Choose a smaller version or reduce context in Engine." )}</ p > :null )} 107{ tr ( ! lifecycle . allowed ?< p className = "small muted" >{ tr ( lifecycle . reason )}</ p > :null )} 108{ tr ( error || version . error ?< ErrorNotice message = { error || version . error ! }/> :null )} 109</ div > :null )} 110</ Dialog > 111</>; 112}