import { tr } from '../lib/i18n'; import { useState } from 'react'; import { Btn, Dialog, ErrorNotice, Field, Icon } from './ui'; import { changeModelDirectory, getState, lifecycleStatus, removeVariant, runtimeModelAction, unloadModel, useStore } from '../lib/store'; import { pickerGroups, pickerVersionState } from '../lib/model-picker'; import { sizeGb } from '../lib/format'; import { useNative } from '../lib/native'; import { NativeDeleteDialog } from './NativeDelete'; import { ModelsFolderCard } from './ModelsFolderDialog'; import { nativeShortName } from '../lib/native-names'; export function SettingsStorage() { const state = useStore(s => s); const native = useNative(); const [selected, setSelected] = useState(null); const [nativeDeleting, setNativeDeleting] = useState(null); const [pending, setPending] = useState(false), [error, setError] = useState(''); const [editing, setEditing] = useState(false), [directory, setDirectory] = useState(''); const [directoryPending, setDirectoryPending] = useState(false), [directoryError, setDirectoryError] = useState(''), [notice, setNotice] = useState(''); const library = state.library; const versions = library ? pickerGroups(library).flatMap(group => group.versions) : []; // ONE list and ONE total: the optimized models (sizes from their real files, shared files counted once) and the other local models together. const 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 const sharedGb = (native?.installed?.usage?.shared_bytes ?? 0) / 1e9; const nativeGb = native?.installed?.usage ? native.installed.usage.total_gb : nativeModels.reduce((n, m) => n + (m.diskGb ?? 0), 0); const totalGb = nativeGb + versions.reduce((n, v) => n + (v.diskGb ?? 0), 0); const rows = nativeModels.length + versions.length; const version = versions.find(v => v.id === selected); const running = !!version && (state.runtime?.instances.some(i => i.variantId === version.id) || library?.loaded === version.id); const lifecycle = lifecycleStatus(state); const currentDirectory = library?.disk.modelsDir ?? ''; const loadedCount = (state.runtime?.instances.length ?? (library?.loaded ? 1 : 0)) + (native?.instances.length ?? 0); const 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'); const changing = editing && directory.trim() !== currentDirectory; async function saveDirectory(path: string) { setDirectoryPending(true); setDirectoryError(''); setNotice(''); try { const result = await changeModelDirectory(path, path.trim() !== currentDirectory); // checking the same folder unloads nothing const count = result.library.variants.filter(v => ['installed', 'loaded'].includes(v.state)).length + result.imageModels.variants.filter(v => v.state === 'installed').length; setEditing(false); setNotice(`${count} ${count === 1 ? 'version' : 'versions'} found${result.library.mode === 'simulated' ? ' in this example library' : ''}. Future downloads will use this folder.`); } catch (e) { setDirectoryError((e as Error).message); } finally { setDirectoryPending(false); } } const unavailable = !version || pickerVersionState(version, library?.loaded ?? null).busy || (running && !lifecycle.allowed); function close() { if (!pending) { setSelected(null); setError(''); } } async function remove() { if (!version || unavailable) return; setPending(true); setError(''); try { const instances = getState().runtime?.instances.filter(i => i.variantId === version.id) ?? []; if (instances.length) for (const instance of instances) await runtimeModelAction(instance.id, 'unload'); else if (getState().library?.loaded === version.id && !await unloadModel()) throw new Error('Could not unload the model. Try again when the engine is idle.'); if (!await removeVariant(version.id)) throw new Error('Could not delete this version. Check the notification and try again.'); setSelected(null); } catch (e) { setError((e as Error).message); } finally { setPending(false); } } const downloading = !!native?.jobs.some(j => j.state === 'running' && j.kind !== 'models-move') || !!library?.downloads.active || !!state.imageModels?.variants.some(v => v.state === 'downloading'); return <> {native?.available && native.downloadDirectory ? : null}
{tr("Model directory")} { setDirectory(currentDirectory); setEditing(!editing); setDirectoryError(''); setNotice(''); }}>{tr(editing ? 'Cancel' : 'Change directory')}

{tr("Your models are saved here. Use an existing folder to reuse supported downloads.")}

{currentDirectory || 'Reading model directory…'} {tr(native?.available && native.downloadDirectory ?
{tr("Optimized Strata model files")}{native.downloadDirectory}

{tr("Chosen during setup. Prepared models keep using their existing files.")}

: null)} {tr(editing ?
{ e.preventDefault(); if (!directoryBlocked && directory.trim()) void saveDirectory(directory); }}> { setDirectory(e.target.value); setDirectoryError(''); }} autoFocus disabled={directoryPending} placeholder={tr("Full path to an existing folder")} spellCheck={false}/>

{tr("Existing files stay where they are. Strata checks this folder for supported GGUF files.")}

{tr(changing && loadedCount > 0 ?

{tr(loadedCount === 1 ? 'The loaded model will be unloaded.' : `All ${loadedCount} loaded models will be unloaded.`)}{tr(" Load a model again after switching.")}

: null)}
{tr(directoryPending ? 'Checking files…' : changing && loadedCount > 0 ? 'Unload and change directory' : changing ? 'Use this directory' : 'Check folder')}
: void saveDirectory(currentDirectory)}>{tr(directoryPending ? 'Checking files…' : 'Check folder')})} {tr(directoryBlocked && !directoryPending ?

{tr(!lifecycle.allowed ? lifecycle.reason : 'Finish or cancel downloads and loading to change the directory.')}

: null)} {tr(state.setup?.app.modelsDirNotice && !notice ?

{tr(state.setup.app.modelsDirNotice)}

: null)} {tr(notice ?

{tr(notice)}

: null)} {tr(directoryError ? : null)}
{tr(rows ?
    {tr(nativeModels.map(m =>
  • {nativeShortName(m.model)}{tr(native?.instances.some(i => i.model === m.model) ? {tr('Loaded')} : null)}{tr(sizeGb(m.diskGb ?? 0))} j.state === 'running')} onClick={() => setNativeDeleting(m.model)}>{tr("Delete")}
  • ))} {tr(versions.map(v =>
  • {`${v.local ? v.name : v.family} · ${v.quant}`}{tr(v.local ? {tr('Local')} : null)}{tr(v.id === library?.loaded ? {tr('Loaded')} : null)}{tr(sizeGb(v.diskGb))} { setError(''); setSelected(v.id); }}>{tr("Delete")}
  • ))} {tr(sharedGb > 0 ?
  • {tr("Shared files")}{tr("Used by more than one model")}{tr(sizeGb(sharedGb))}
  • : null)}
:

{tr("No downloaded models")}

)}
setNativeDeleting(null)}/> {tr("Keep version")} void remove()}>{tr(pending ? 'Working…' : version?.local ? 'Remove model' : 'Delete version')}}>

{tr(version ? `${version.local ? version.name : version.family} · ${version.quant} · ${sizeGb(version.diskGb)}` : 'This version is no longer downloaded.')}

{tr(version ?

{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.`)}

: null)} {tr(running && !lifecycle.allowed ?

{tr(lifecycle.reason)}

: null)} {tr(error ? : null)}
; }