import { useState } from 'react'; import { localeTag, tr } from '../lib/i18n'; import { useNative } from '../lib/native'; import { changeInstalledEngine, engineGroupTitle, engineMake, engineSize, modelsNeedingNewer, useEngineList, type InstalledEngine } from '../lib/engines'; import { ApiError } from '../lib/client'; import { Btn, Dialog, ErrorNotice, Icon } from './ui'; /** Why this computer cannot run an engine: Control gives a code and its values, the page words it (so every language has it). */ function unfitWords(engine: InstalledEngine): string { const a = engine.why_args ?? []; switch (engine.why_code) { case 'os': return tr("This engine was made for a different operating system."); case 'gpu_make': return tr("This engine was made for a different kind of graphics card than this computer has."); case 'strata': return tr("This engine needs Strata {0} or newer.", [a[0] ?? '']); case 'gpu_arch': return tr("This engine doesn't support this computer's graphics card."); case 'driver': return tr("This engine needs NVIDIA driver {0} or newer. This computer has {1}.", [a[0] ?? '', a[1] ?? '']); case 'files': return tr("Some of this engine's files are missing or changed, so it can't be started."); default: return tr("This engine's description is missing or damaged, so it can't be started."); } } /** Settings > Engine: the engines installed on this computer. Switch to any of them (the newest too) or delete one. Updating is in About. */ export function SettingsEngine() { const native = useNative(); const { list, failed, reload } = useEngineList(); const [use, setUse] = useState(null); const [del, setDel] = useState(null); const [pending, setPending] = useState(false); const [error, setError] = useState<{ message: string; detail?: string } | null>(null); const [said, setSaid] = useState(''); const running = (native?.instances.length ?? 0) > 0; const busy = !!native?.jobs.some(j => j.state === 'running') || pending; const groups = list?.families ?? []; const count = groups.reduce((sum, g) => sum + g.engines.length, 0); async function run(action: 'use' | 'remove', engine: InstalledEngine) { setPending(true); setError(null); setSaid(''); try { await changeInstalledEngine(action, engine); setSaid(action === 'use' ? tr("Engine {0} is now in use.", [engine.version]) : tr("Engine {0} was deleted.", [engine.version])); } catch (e) { const body = e instanceof ApiError ? e.body as { details?: string } | undefined : undefined; setError({ message: e instanceof Error ? e.message : String(e), detail: body?.details }); } finally { setUse(null); setDel(null); setPending(false); void reload(); } } function row(engine: InstalledEngine) { const facts = [engineMake(engine.backend), engineSize(engine.bytes, localeTag()), engine.newest ? tr("Newest") : ''].filter(Boolean).join(' · '); return
{engine.version} {facts} {engine.fits ? null : {unfitWords(engine)}} {engine.active ? {tr("In use")} : <> {engine.can_use ? { setError(null); setSaid(''); setUse(engine); }}>{tr("Use this")} : null} { setError(null); setSaid(''); setDel(engine); }}>{tr("Delete")} }
; } const needNewer = use ? modelsNeedingNewer(native, use) : []; const useGroup = use ? groups.find(g => g.family === use.family) : undefined; return

{tr("Installed engines")}

{tr("Switch to an older engine to test something, then switch back to the newest. Updating to the latest is in About.")}

{list && !list.available ?

{tr("Engines can be switched and deleted in the installed Strata app.")}

: null} {failed ?

{tr("The installed engines couldn't be read. Try again in a moment.")}

: null} {list?.available && count === 0 ?

{tr("No engine is installed on this computer yet. Update in About downloads the right one.")}

: null} {groups.map(group =>
{groups.length > 1 ?

{group.title ? tr(group.title) : engineGroupTitle(group, native?.catalog?.families)}

: null} {group.engines.map(row)}
)} {count > 0 ?

{tr("Engines take {0} on this computer.", [engineSize(list?.total_bytes ?? 0, localeTag())])}

: null} {count > 0 ?
{tr("Details")}{groups.flatMap(g => g.engines).map(e =>

{e.version} · {e.path}{e.build_id ? ` · ${e.build_id}` : ''}

)}
: null}
{said ?

{said}

: null}
{error ? : null}
{ if (!pending) setUse(null); }} actions={<> setUse(null)}>{tr("Cancel")} use && void run('use', use)}>{pending ? tr("Working…") : tr("Use engine {0}", [use?.version ?? ''])}}> {/* a Qwen4 model shape's engine runs only that model (owner 11-09): its own sentence, named like the group's heading */}

{useGroup?.family.startsWith('shape:') ? tr("{0} runs on engine {1} from its next start. Your models and settings stay as they are. You can switch back at any time.", [engineGroupTitle(useGroup, native?.catalog?.families), use?.version ?? '']) : tr("Models you start from now on run on engine {0}. Your models and settings stay as they are. You can switch back at any time.", [use?.version ?? ''])}

{needNewer.length ?

{tr("These models need a newer engine and may not start on it: {0}.", [needNewer.join(', ')])}

: null} {running ?

{tr("A model is running. Unload it in Models first, then switch.")}

: null}
{ if (!pending) setDel(null); }} actions={<> setDel(null)}>{tr("Keep engine")} del && void run('remove', del)}>{pending ? tr("Working…") : tr("Delete engine")}}>

{tr("This frees {0}. Your models and settings are kept.", [engineSize(del?.bytes ?? 0, localeTag())])}

{del?.newest ? tr("The newest engine can be downloaded again with Update in About.") : tr("Strata can only download the newest engine, so a deleted older engine can't be brought back.")}

; }