okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
7.8 KiB103 linesraw
1import { useState } from 'react';
2import { localeTag, tr } from '../lib/i18n';
3import { useNative } from '../lib/native';
4import { changeInstalledEngine, engineGroupTitle, engineMake, engineSize, modelsNeedingNewer, useEngineList, type InstalledEngine } from '../lib/engines';
5import { ApiError } from '../lib/client';
6import { Btn, Dialog, ErrorNotice, Icon } from './ui';
7
8/** Why this computer cannot run an engine: Control gives a code and its values, the page words it (so every language has it). */
9function unfitWords(engine: InstalledEngine): string {
10  const a = engine.why_args ?? [];
11  switch (engine.why_code) {
12    case 'os': return tr("This engine was made for a different operating system.");
13    case 'gpu_make': return tr("This engine was made for a different kind of graphics card than this computer has.");
14    case 'strata': return tr("This engine needs Strata {0} or newer.", [a[0] ?? '']);
15    case 'gpu_arch': return tr("This engine doesn't support this computer's graphics card.");
16    case 'driver': return tr("This engine needs NVIDIA driver {0} or newer. This computer has {1}.", [a[0] ?? '', a[1] ?? '']);
17    case 'files': return tr("Some of this engine's files are missing or changed, so it can't be started.");
18    default: return tr("This engine's description is missing or damaged, so it can't be started.");
19  }
20}
21
22/** Settings > Engine: the engines installed on this computer. Switch to any of them (the newest too) or delete one. Updating is in About. */
23export function SettingsEngine() {
24  const native = useNative();
25  const { list, failed, reload } = useEngineList();
26  const [use, setUse] = useState<InstalledEngine | null>(null);
27  const [del, setDel] = useState<InstalledEngine | null>(null);
28  const [pending, setPending] = useState(false);
29  const [error, setError] = useState<{ message: string; detail?: string } | null>(null);
30  const [said, setSaid] = useState('');
31  const running = (native?.instances.length ?? 0) > 0;
32  const busy = !!native?.jobs.some(j => j.state === 'running') || pending;
33  const groups = list?.families ?? [];
34  const count = groups.reduce((sum, g) => sum + g.engines.length, 0);
35
36  async function run(action: 'use' | 'remove', engine: InstalledEngine) {
37    setPending(true);
38    setError(null);
39    setSaid('');
40    try {
41      await changeInstalledEngine(action, engine);
42      setSaid(action === 'use' ? tr("Engine {0} is now in use.", [engine.version]) : tr("Engine {0} was deleted.", [engine.version]));
43    } catch (e) {
44      const body = e instanceof ApiError ? e.body as { details?: string } | undefined : undefined;
45      setError({ message: e instanceof Error ? e.message : String(e), detail: body?.details });
46    } finally {
47      setUse(null);
48      setDel(null);
49      setPending(false);
50      void reload();
51    }
52  }
53
54  function row(engine: InstalledEngine) {
55    const facts = [engineMake(engine.backend), engineSize(engine.bytes, localeTag()), engine.newest ? tr("Newest") : ''].filter(Boolean).join(' · ');
56    return <div className="engine-row" key={`${engine.root ?? ''}${engine.folder}`} data-active={engine.active ? 'true' : undefined} data-unfit={engine.fits ? undefined : 'true'}>
57      <Icon name="engine" size={20}/>
58      <span className="engine-row__text">
59        <strong>{engine.version}</strong>
60        <small>{facts}</small>
61        {engine.fits ? null : <small className="engine-row__why">{unfitWords(engine)}</small>}
62      </span>
63      {engine.active ? <span className="engine-row__state">{tr("In use")}</span> : <>
64        {engine.can_use ? <Btn size="sm" disabled={busy} onClick={() => { setError(null); setSaid(''); setUse(engine); }}>{tr("Use this")}</Btn> : null}
65        <Btn size="sm" variant="ghost" disabled={busy || !engine.can_delete} title={engine.blocked_code === 'running' ? tr("A running program is using this engine. Unload your models first.") : undefined} onClick={() => { setError(null); setSaid(''); setDel(engine); }}>{tr("Delete")}</Btn>
66      </>}
67    </div>;
68  }
69
70  const needNewer = use ? modelsNeedingNewer(native, use) : [];
71  const useGroup = use ? groups.find(g => g.family === use.family) : undefined;
72  return <div className="stack stack--lg native-engine-settings">
73    <section className="st-card engine-list">
74      <div className="section-toolbar"><h2 className="h3">{tr("Installed engines")}</h2></div>
75      <p className="small muted">{tr("Switch to an older engine to test something, then switch back to the newest. Updating to the latest is in About.")}</p>
76      {list && !list.available ? <p className="small muted">{tr("Engines can be switched and deleted in the installed Strata app.")}</p> : null}
77      {failed ? <p className="small inline-error" role="alert">{tr("The installed engines couldn't be read. Try again in a moment.")}</p> : null}
78      {list?.available && count === 0 ? <p className="small muted">{tr("No engine is installed on this computer yet. Update in About downloads the right one.")}</p> : null}
79      {groups.map(group => <div key={group.family} className="engine-group">
80        {groups.length > 1 ? <h3 className="engine-group__title">{group.title ? tr(group.title) : engineGroupTitle(group, native?.catalog?.families)}</h3> : null}
81        {group.engines.map(row)}
82      </div>)}
83      {count > 0 ? <p className="small muted engine-total">{tr("Engines take {0} on this computer.", [engineSize(list?.total_bytes ?? 0, localeTag())])}</p> : null}
84      {count > 0 ? <details className="native-settings-diagnostic engine-details"><summary className="small">{tr("Details")}</summary>{groups.flatMap(g => g.engines).map(e => <p key={`${e.root ?? ''}${e.folder}`} className="small muted">{e.version} · {e.path}{e.build_id ? ` · ${e.build_id}` : ''}</p>)}</details> : null}
85      <div role="status" aria-live="polite">{said ? <p className="small">{said}</p> : null}</div>
86      {error ? <ErrorNotice message={error.message} detail={error.detail}/> : null}
87    </section>
88
89    <Dialog open={!!use} compact title={tr("Use engine {0}?", [use?.version ?? ''])} onClose={() => { if (!pending) setUse(null); }}
90      actions={<><Btn disabled={pending} onClick={() => setUse(null)}>{tr("Cancel")}</Btn><Btn variant="primary" disabled={pending || running} onClick={() => use && void run('use', use)}>{pending ? tr("Working…") : tr("Use engine {0}", [use?.version ?? ''])}</Btn></>}>
91      {/* a Qwen4 model shape's engine runs only that model (owner 11-09): its own sentence, named like the group's heading */}
92      <p>{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 ?? ''])}</p>
93      {needNewer.length ? <p className="small muted">{tr("These models need a newer engine and may not start on it: {0}.", [needNewer.join(', ')])}</p> : null}
94      {running ? <p className="small muted" role="status">{tr("A model is running. Unload it in Models first, then switch.")}</p> : null}
95    </Dialog>
96
97    <Dialog open={!!del} compact title={tr("Delete engine {0}?", [del?.version ?? ''])} onClose={() => { if (!pending) setDel(null); }}
98      actions={<><Btn disabled={pending} onClick={() => setDel(null)}>{tr("Keep engine")}</Btn><Btn variant="danger" disabled={pending} onClick={() => del && void run('remove', del)}>{pending ? tr("Working…") : tr("Delete engine")}</Btn></>}>
99      <p>{tr("This frees {0}. Your models and settings are kept.", [engineSize(del?.bytes ?? 0, localeTag())])}</p>
100      <p className="small muted">{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.")}</p>
101    </Dialog>
102  </div>;
103}