okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1import { tr } from '../lib/i18n' ; 2import { useState } from 'react' ; 3import { Btn , Dialog , ErrorNotice , Field , Icon } from './ui' ; 4import { changeModelDirectory , getState , lifecycleStatus , removeVariant , runtimeModelAction , unloadModel , useStore } from '../lib/store' ; 5import { pickerGroups , pickerVersionState } from '../lib/model-picker' ; 6import { sizeGb } from '../lib/format' ; 7import { useNative } from '../lib/native' ; 8import { NativeDeleteDialog } from './NativeDelete' ; 9import { ModelsFolderCard } from './ModelsFolderDialog' ; 10import { nativeShortName } from '../lib/native-names' ; 11export function SettingsStorage () { 12const state = useStore ( s => s ); 13const native = useNative (); 14const [ selected , setSelected ] = useState < string | null >( null ); 15const [ nativeDeleting , setNativeDeleting ] = useState < string | null >( null ); 16const [ pending , setPending ] = useState ( false ), [ error , setError ] = useState ( '' ); 17const [ editing , setEditing ] = useState ( false ), [ directory , setDirectory ] = useState ( '' ); 18const [ directoryPending , setDirectoryPending ] = useState ( false ), [ directoryError , setDirectoryError ] = useState ( '' ), [ notice , setNotice ] = useState ( '' ); 19const library = state . library ; 20const versions = library ?pickerGroups ( library ). flatMap ( group => group . versions ) :[]; 21// ONE list and ONE total: the optimized models (sizes from their real files, shared files counted once) and the other local models together. 22const nativeModels = ( native ?. installed ?. models ?? []). filter ( m => ! m . retired && ( m . ready || ( m . diskGb ?? 0 ) > 0 )); // a model whose files are all gone (an old config) is not a 0.0 GB download 23const sharedGb = ( native ?. installed ?. usage ?. shared_bytes ?? 0 ) / 1e9 ; 24const nativeGb = native ?. installed ?. usage ?native . installed . usage . total_gb :nativeModels . reduce (( n , m ) => n + ( m . diskGb ?? 0 ), 0 ); 25const totalGb = nativeGb + versions . reduce (( n , v ) => n + ( v . diskGb ?? 0 ), 0 ); 26const rows = nativeModels . length + versions . length ; 27const version = versions . find ( v => v . id === selected ); 28const running = !! version && ( state . runtime ?. instances . some ( i => i . variantId === version . id ) || library ?. loaded === version . id ); 29const lifecycle = lifecycleStatus ( state ); 30const currentDirectory = library ?. disk . modelsDir ?? '' ; 31const loadedCount = ( state . runtime ?. instances . length ?? ( library ?. loaded ?1 :0 )) + ( native ?. instances . length ?? 0 ); 32const directoryBlocked = !! native ?. jobs . some ( j => j . state === 'running' ) || ! lifecycle . allowed || !! library ?. downloads . active || !! state . imageModels ?. variants . some ( v => v . state === 'downloading' ) || !! library ?. variants . some ( v => v . state === 'loading' ); 33const changing = editing && directory . trim () !== currentDirectory ; 34async function saveDirectory ( path :string ) { 35setDirectoryPending ( true ); 36setDirectoryError ( '' ); 37setNotice ( '' ); 38try { 39const result = await changeModelDirectory ( path , path . trim () !== currentDirectory ); // checking the same folder unloads nothing 40const count = result . library . variants . filter ( v => [ 'installed' , 'loaded' ]. includes ( v . state )). length + result . imageModels . variants . filter ( v => v . state === 'installed' ). length ; 41setEditing ( false ); 42setNotice ( ` ${ count } ${ count === 1 ? 'version' : 'versions' } found ${ result . library . mode === 'simulated' ? ' in this example library' : '' } . Future downloads will use this folder.` ); 43} 44catch ( e ) { 45setDirectoryError (( e as Error ). message ); 46} 47finally { 48setDirectoryPending ( false ); 49} 50} 51const unavailable = ! version || pickerVersionState ( version , library ?. loaded ?? null ). busy || ( running && ! lifecycle . allowed ); 52function close () { 53if ( ! pending ) { 54setSelected ( null ); 55setError ( '' ); 56} 57} 58async function remove () { 59if ( ! version || unavailable ) 60return ; 61setPending ( true ); 62setError ( '' ); 63try { 64const instances = getState (). runtime ?. instances . filter ( i => i . variantId === version . id ) ?? []; 65if ( instances . length ) 66for ( const instance of instances ) 67await runtimeModelAction ( instance . id , 'unload' ); 68else if ( getState (). library ?. loaded === version . id && ! await unloadModel ()) 69throw new Error ( 'Could not unload the model. Try again when the engine is idle.' ); 70if ( ! await removeVariant ( version . id )) 71throw new Error ( 'Could not delete this version. Check the notification and try again.' ); 72setSelected ( null ); 73} 74catch ( e ) { 75setError (( e as Error ). message ); 76} 77finally { 78setPending ( false ); 79} 80} 81const downloading = !! native ?. jobs . some ( j => j . state === 'running' && j . kind !== 'models-move' ) || !! library ?. downloads . active || !! state . imageModels ?. variants . some ( v => v . state === 'downloading' ); 82return <> 83{ native ?. available && native . downloadDirectory ?< ModelsFolderCard blocked = { downloading }/> :null } 84< section className = "storage-directory" aria-labelledby = "storage-directory-title" > 85< div className = "storage-directory__heading" >< span >< Icon name = "folder" size = { 18 }/>< strong id = "storage-directory-title" >{ tr ( "Model directory" )}</ strong ></ span >< Btn size = "sm" disabled = { ! currentDirectory || directoryPending || pending } onClick = {() => { setDirectory ( currentDirectory ); setEditing ( ! editing ); setDirectoryError ( '' ); setNotice ( '' ); }}>{ tr ( editing ?'Cancel' :'Change directory' )}</ Btn ></ div > 86< p className = "small muted" >{ tr ( "Your models are saved here. Use an existing folder to reuse supported downloads." )}</ p > 87< code className = "storage-directory__path" >{ currentDirectory || 'Reading model directory…' }</ code > 88{ tr ( native ?. available && native . downloadDirectory ?< div className = "storage-native-directory" >< span className = "small muted" >{ tr ( "Optimized Strata model files" )}</ span >< code className = "storage-directory__path" >{ native . downloadDirectory }</ code >< p className = "xs muted" >{ tr ( "Chosen during setup. Prepared models keep using their existing files." )}</ p ></ div > :null )} 89{ tr ( editing ?< form className = "storage-directory__edit" onSubmit = { e => { 90e . preventDefault (); 91if ( ! directoryBlocked && directory . trim ()) 92void saveDirectory ( directory ); 93}}> 94< label htmlFor = "model-directory-path" className = "small" >{ tr ( "Directory path" )}</ label > 95< input id = "model-directory-path" className = "st-input" value = { directory } onChange = { e => { setDirectory ( e . target . value ); setDirectoryError ( '' ); }} autoFocus disabled = { directoryPending } placeholder = { tr ( "Full path to an existing folder" )} spellCheck = { false }/> 96< p className = "small muted" >{ tr ( "Existing files stay where they are. Strata checks this folder for supported GGUF files." )}</ p > 97{ tr ( changing && loadedCount > 0 ?< p className = "small" >{ tr ( loadedCount === 1 ?'The loaded model will be unloaded.' :`All ${ loadedCount } loaded models will be unloaded.` )}{ tr ( " Load a model again after switching." )}</ p > :null )} 98< div className = "row" >< Btn variant = "primary" type = "submit" disabled = { directoryPending || directoryBlocked || ! directory . trim ()}>{ tr ( directoryPending ?'Checking files…' :changing && loadedCount > 0 ?'Unload and change directory' :changing ?'Use this directory' :'Check folder' )}</ Btn ></ div > 99</ form > :< Btn size = "sm" icon = "refresh" disabled = { directoryPending || directoryBlocked || ! currentDirectory } onClick = {() => void saveDirectory ( currentDirectory )}>{ tr ( directoryPending ?'Checking files…' :'Check folder' )}</ Btn >)} 100{ tr ( directoryBlocked && ! directoryPending ?< p className = "small muted" >{ tr ( ! lifecycle . allowed ?lifecycle . reason :'Finish or cancel downloads and loading to change the directory.' )}</ p > :null )} 101{ tr ( state . setup ?. app . modelsDirNotice && ! notice ?< p className = "small storage-directory__notice" role = "status" >< Icon name = "info" size = { 15 }/>{ tr ( state . setup . app . modelsDirNotice )}</ p > :null )} 102{ tr ( notice ?< p className = "small storage-directory__notice" role = "status" >< Icon name = "check" size = { 15 }/>{ tr ( notice )}</ p > :null )} 103{ tr ( directoryError ?< ErrorNotice message = { directoryError }/> :null )} 104</ section > 105< Field wide label = { tr ( "Downloaded models" )} hint = { tr ( "{0} on this computer. Choose a version to delete its files." , [ sizeGb ( totalGb )])}> 106{ tr ( rows ?< ul className = "storage-models" aria-label = { tr ( "Downloaded models" )}> 107{ tr ( nativeModels . map ( m => < li className = "storage-model" key = { m . model }>< span className = "storage-model__name" title = { m . model_name || m . model }>< strong >{ nativeShortName ( m . model )}</ strong >{ tr ( native ?. instances . some ( i => i . model === m . model ) ?< small className = "muted" >{ tr ( 'Loaded' )}</ small > :null )}</ span >< span className = "storage-model__size muted" >{ tr ( sizeGb ( m . diskGb ?? 0 ))}</ span >< Btn size = "sm" variant = "danger" disabled = { !! native ?. jobs . some ( j => j . state === 'running' )} onClick = {() => setNativeDeleting ( m . model )}>{ tr ( "Delete" )}</ Btn ></ li >))} 108{ tr ( versions . map ( v => < li className = "storage-model" key = { v . id }>< span className = "storage-model__name" title = { ` ${ v . family } · ${ v . quant } ` }>< strong >{ ` ${ v . local ? v . name : v . family } · ${ v . quant } ` }</ strong >{ tr ( v . local ?< small className = "muted" >{ tr ( 'Local' )}</ small > :null )}{ tr ( v . id === library ?. loaded ?< small className = "muted" >{ tr ( 'Loaded' )}</ small > :null )}</ span >< span className = "storage-model__size muted" >{ tr ( sizeGb ( v . diskGb ))}</ span >< Btn size = "sm" variant = "danger" disabled = { pickerVersionState ( v , library ?. loaded ?? null ). busy || pending || directoryPending } onClick = {() => { setError ( '' ); setSelected ( v . id ); }}>{ tr ( "Delete" )}</ Btn ></ li >))} 109{ tr ( sharedGb > 0 ?< li className = "storage-model storage-model--shared" >< span className = "storage-model__name" >< strong >{ tr ( "Shared files" )}</ strong >< small className = "muted" >{ tr ( "Used by more than one model" )}</ small ></ span >< span className = "storage-model__size muted" >{ tr ( sizeGb ( sharedGb ))}</ span >< span /></ li > :null )} 110</ ul > :< p className = "small muted" >{ tr ( "No downloaded models" )}</ p >)} 111</ Field > 112< NativeDeleteDialog id = { nativeDeleting } onClose = {() => setNativeDeleting ( null )}/> 113< Dialog open = { !! selected } compact title = { tr ( version ?. local ?'Remove from Strata?' :`Delete ${ version ?. quant ?? 'model version' } ?` )} onClose = { close } actions = {<>< Btn disabled = { pending } onClick = { close }>{ tr ( "Keep version" )}</ Btn >< Btn variant = "danger" disabled = { pending || unavailable } onClick = {() => void remove ()}>{ tr ( pending ?'Working…' :version ?. local ?'Remove model' :'Delete version' )}</ Btn ></>}> 114< p >{ tr ( version ?` ${ version . local ? version . name : version . family } · ${ version . quant } · ${ sizeGb ( version . diskGb )} ` :'This version is no longer downloaded.' )}</ p > 115{ tr ( version ?< p className = "small muted storage-delete-note" >{ tr ( version . local ?` ${ running ? 'The model will be unloaded and removed from Strata.' : 'The model will be removed from Strata.' } Your original GGUF files stay in their folder.` :` ${ running ? 'The running model will be unloaded and its files deleted.' : 'Its files will be deleted from this computer.' } You can download it again in Models.` )}</ p > :null )} 116{ tr ( running && ! lifecycle . allowed ?< p className = "small storage-delete-note" >{ tr ( lifecycle . reason )}</ p > :null )} 117{ tr ( error ?< ErrorNotice message = { error }/> :null )} 118</ Dialog > 119</>; 120}