okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1import { tr } from '../lib/i18n' ; 2import { useRef , useState } from 'react' ; 3import { ActionError , Bar , Btn , Dialog , ErrorNotice , Icon , Select } from './ui' ; 4import { FloatingMenu } from './FloatingMenu' ; 5import { LifecycleNotice } from './LifecycleNotice' ; 6import { VersionLadder } from './VersionLadder' ; 7import { LocalModelImport } from './LocalModelImport' ; 8import { modelFamilyIcon } from '../lib/model-icons' ; 9import { familyVersion , modelFamilies , modelVersions , versionFit } from '../lib/model-versions' ; 10import { contextTokens , estimate , execution } from '../lib/experience' ; 11import { planMemory } from '../lib/run-memory' ; 12import type { ModelVariant } from '../lib/types' ; 13import { cancelInstall , installVariant , useLifecycle , loadVariant , removeVariant , runtimeModelAction , useStore } from '../lib/store' ; 14export function ManagedModelLibrary ({ contextOverride, familyOverride, selectedVariant, onChoose} :{ 15contextOverride ?:number ; 16familyOverride ?:string ; 17selectedVariant ?:string ; 18onChoose ?:( id :string ) => void ; 19}) { 20const library = useStore ( s => s . library ), hardware = useStore ( s => s . hardware ), engine = useStore ( s => s . engine ); 21const runtime = useStore ( s => s . runtime ), serving = useStore ( s => s . serving ); 22const lifecycle = useLifecycle (); 23const [ selected , setSelected ] = useState ( selectedVariant ?? '' ); 24const [ familyChoice , setFamilyChoice ] = useState ( familyOverride ?? '' ); 25const [ pending , setPending ] = useState ( false ), [ error , setError ] = useState ( '' ); 26const [ localModelOpen , setLocalModelOpen ] = useState ( false ); 27const [ menu , setMenu ] = useState ( false ), [ confirm , setConfirm ] = useState < 'delete' | 'switch' | null >( null ); 28const more = useRef < HTMLButtonElement >( null ); 29if ( ! library ) 30return < p className = "small muted" >{ tr ( "Loading model versions\u2026" )}</ p >; 31const families = modelFamilies ( library ); 32const family = families . includes ( familyChoice ) ?familyChoice :library . variants . find ( v => v . id === library . loaded )?. family ?? families [ 0 ]; 33const versions = modelVersions ( library , family ); 34const version = versions . find ( v => v . id === selected ) ?? familyVersion ( library , family ); 35if ( ! version ) 36return < p className = "small muted" >{ tr ( "No versions are available." )}</ p >; 37const context = contextOverride ?? contextTokens ( engine ?. beginner . contextLength ?? 'recommended' ); 38const fit = versionFit ( version , hardware , context ); 39const instances = runtime ?. instances . filter ( i => i . variantId === version . id ) ?? []; 40const loaded = instances . length > 0 || version . state === 'loaded' ; 41const instanceBusy = instances . some ( i => serving ?. requests . some ( r => r . instanceId === i . id && [ 'generating' , 'queued' ]. includes ( r . state ))); 42const canUnload = instances . length > 0 && ! instanceBusy ; 43const working = [ 'downloading' , 'loading' , 'unloading' ]. includes ( version . state ); 44const installed = [ 'loaded' , 'installed' , 'loading' , 'unloading' ]. includes ( version . state ) || !! version . installedAt ; 45const noSpace = version . downloadGb > library . disk . freeGb ; 46function memoryNote ( v :ModelVariant ) { 47if ( ! [ 'installed' , 'loaded' ]. includes ( v . state ) && ! v . installedAt ) 48return ; 49const memory = planMemory ( estimate ( v , hardware , execution ( undefined , context )), hardware , 'shared' ); 50if ( memory && memory . ssd > .05 ) 51return 'SSD spill likely. Part of this model may be read from storage, so responses can be slower.' ; 52} 53async function act ( action :() => Promise < boolean | void >) { 54setPending ( true ); 55setError ( '' ); 56try { 57const ok = await action (); 58if ( ok === false ) 59throw new Error ( 'The operation could not finish. Check the engine connection and try again.' ); 60setConfirm ( null ); 61} 62catch ( reason ) { 63setError ( reason instanceof Error ?reason . message :String ( reason )); 64} 65finally { 66setPending ( false ); 67} 68} 69const useVersion = () => { if ( onChoose ) 70onChoose ( version . id ); 71else 72setConfirm ( 'switch' ); }; 73return < div className = "managed-models" > 74< ActionError /> 75< div className = "managed-models__family" >< Icon name = { modelFamilyIcon ( family )} size = { 23 }/>< Select ariaLabel = { tr ( "Manage model family" )} value = { family } disabled = { pending } menuWidth = { 340 } menuClassName = "model-family-menu" optionIcon = { modelFamilyIcon } options = { families . map ( f => ({ value :f , label :f }))} onChange = { value => { setFamilyChoice ( value ); setSelected ( '' ); setError ( '' ); setMenu ( false ); }}/>< span >{ tr ( versions . length )}{ tr ( " versions" )}</ span ></ div > 76< div className = "managed-version-head" aria-hidden = "true" >< span >{ tr ( "Version" )}</ span >< span >{ tr ( "Download size" )}</ span >< span >{ tr ( "Status" )}</ span ></ div > 77< VersionLadder compact versions = { versions } hardware = { hardware } context = { context } selected = { version . id } disabled = { pending } memoryNote = { memoryNote } onSelect = { id => { setSelected ( id ); setError ( '' ); setMenu ( false ); }}/> 78{ tr ( version . state === 'downloading' ?< div className = "managed-models__progress" role = "status" >< Bar value = { version . progress ?. pct ?? 0 } label = { tr ( "Downloading" )}/>< span >{ tr ( version . progress ?. stage ?? 'Downloading model files…' )}</ span ></ div > :null )} 79< div className = "managed-models__footer" > 80< div className = "managed-models__memory" >< span >{ tr ( "Estimated memory" )}</ span >< strong >{ tr ( fit . needed . toFixed ( 1 ))} < small >{ tr ( "GB" )}</ small ></ strong ></ div > 81< div className = "managed-models__actions" > 82{ tr (( installed || version . local ) && ! working ?< button ref = { more } className = "st-btn st-btn--secondary managed-models__more" aria-label = { tr ( "Version actions" )} aria-haspopup = "menu" aria-expanded = { menu } disabled = { pending } onClick = {() => setMenu ( ! menu )}>< Icon name = "ellipsis" size = { 19 }/></ button > :null )} 83{ tr ( working ?version . state === 'downloading' ?< Btn disabled = { pending } onClick = {() => void act (() => cancelInstall ( version . id ))}>{ tr ( "Cancel download" )}</ Btn > :< Btn disabled >{ tr ( version . state === 'loading' ?'Loading…' :'Unloading…' )}</ Btn > :installed ?< Btn variant = "primary" iconRight = "chevron" className = "managed-models__use" disabled = { pending } onClick = { useVersion }>{ tr ( "Use version" )}</ Btn > :version . local ?< Btn icon = "folder" onClick = {() => setLocalModelOpen ( true )}>{ tr ( "Reconnect file" )}</ Btn > :< Btn variant = "primary" icon = "download" disabled = { pending || noSpace } onClick = {() => void act (() => installVariant ( version . id ))}>{ tr ( noSpace ?'Not enough storage' :pending ?'Starting…' :'Download' )}</ Btn >)} 84</ div > 85</ div > 86{ tr ( version . error || error ?< ErrorNotice message = { error || version . error ! }/> :null )} 87< LifecycleNotice showScope = { false }/> 88{ tr ( menu ?< FloatingMenu anchor = { more . current } title = { tr ( "Version actions" )} width = { 230 } onClose = {() => setMenu ( false )}> 89{ tr ( loaded ?< button role = "menuitem" className = "menu__item" disabled = { ! canUnload } onClick = {() => { setMenu ( false ); void act ( async () => { for ( const instance of instances ) 90await runtimeModelAction ( instance . id , 'unload' ); }); }}>< Icon name = "stop" size = { 17 }/>{ tr ( instances . length > 1 ?`Unload ${ instances . length } instances` :'Unload model' )}</ button > :null )} 91< button role = "menuitem" className = "menu__item" disabled = { loaded || pending } title = { tr ( loaded ?'Unload this version first' :undefined )} onClick = {() => { setMenu ( false ); setConfirm ( 'delete' ); }}>< Icon name = "trash" size = { 17 }/>{ tr ( version . local ?'Remove from Strata…' :'Delete files…' )}</ button > 92</ FloatingMenu > :null )} 93< Dialog compact open = { !! confirm } title = { tr ( confirm === 'delete' ?version . local ?'Remove from Strata?' :'Delete this version?' :'Switch version?' )} onClose = {() => ! pending && setConfirm ( null )} actions = {<>< Btn disabled = { pending } onClick = {() => setConfirm ( null )}>{ tr ( "Cancel" )}</ Btn >< Btn variant = { confirm === 'delete' ?'danger' :'primary' } disabled = { pending || ( confirm === 'switch' && ! lifecycle . allowed )} onClick = {() => void act (() => confirm === 'delete' ?removeVariant ( version . id ) :loadVariant ( version . id ))}>{ tr ( pending ?'Working…' :confirm === 'delete' ?version . local ?'Remove model' :'Delete files' :'Switch version' )}</ Btn ></>}> 94< p >{ tr ( confirm === 'delete' ?version . local ?'Remove this model from Strata. Your original GGUF files stay in their folder.' :`Remove ${ Math . round ( version . diskGb )} GB of downloaded files. You can download this version again.` :`Load ${ version . name } for all chats. Loading may take a few moments.` )}</ p > 95{ tr ( error ?< ErrorNotice message = { error }/> :null )} 96</ Dialog > 97< LocalModelImport open = { localModelOpen } directory = { library . disk . modelsDir } onClose = {() => setLocalModelOpen ( false )} onChoose = { id => { setFamilyChoice ( 'Local models' ); setSelected ( id ); if ( onChoose ) 98onChoose ( id ); }}/> 99</ div >; 100}