okami.mom/strata

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

Niko1221Drafting Off in the app: a model saved with Off loads after a restart, and the Models screen shows the saved choice (0.2.15 release test)c77572806

main
44.2 KiB354 linesraw
1import { tr } from '../lib/i18n';
2import { useEffect, useMemo, useRef, useState } from 'react';
3import { Screen } from './layout';
4import { VersionPicker } from './VersionPicker';
5import { FamilySwitch } from './FamilySwitch';
6import { PcComputer } from './PcComputer';
7import { cardRoomGb, contextCacheGb, fitOn, memoryNeeded, versionFit } from '../lib/native-fit';
8import { contextIsTight, defaultContext, DEFAULT_CONTEXT, openingContext } from '../lib/default-context';
9import { GpuRoles } from './GpuRoles';
10import { fromLegacy, normalize as normalizeRoles, order as roleOrder, recommend as recommendRoles, rows as roleRows, sameRoles, vramOn, type RoleCard, type Roles } from '../lib/gpu-roles';
11import { Btn, Dialog, ErrorNotice, Icon, Select } from './ui';
12import { installProgress, installStage, stageText, useDownloadEta } from '../lib/download-eta';
13import { useKept } from '../lib/kept-state';
14import { DownloadParts } from './DownloadParts';
15import { EngineWarning } from './EngineWarning';
16import { EngineUpdateCard } from './EngineUpdateCard';
17import { EngineUpdateDialog } from './EngineUpdateDialog';
18import { sizeGb } from '../lib/format';
19import { NativeDeleteDialog } from './NativeDelete';
20import { plainFailureText } from '../lib/plain-errors';
21import { api } from '../lib/client';
22import { go, setSettingsSection, selectRemoteModel, pushToast } from '../lib/store';
23import { installedListPending, nativeAction, useNative, type NativeJob } from '../lib/native';
24import { tuneEffective } from '../lib/native-tune';
25import { tunedFields } from '../lib/tune-values';
26import { shownVision, steadyNoReader } from '../lib/pictures-choice';
27import { draftingChoices as draftingOptions, resolveDrafter } from '../lib/drafting';
28import { NativeSettings } from './NativeSettings';
29import { NativeSettingsFile } from './NativeSettingsFile';
30import { RunImageModel } from './RunImageModel';
31import { IMAGE_GENERATION_VISIBLE } from '../lib/features';
32import { useStore } from '../lib/store';
33import { NativePresets } from './NativePresets';
34import { NativeTune } from './NativeTune';
35import { NativeTuneBadge } from './NativeTuneBadge';
36import { nativeCatalogModels, catalogVersionLabel, fetchesPictures } from '../lib/native-catalog';
37export function NativeModels({ onOtherModels, onReady, onImport, onConnectEngine }: {
38    onOtherModels: () => void;
39    onImport?: () => void;
40    onConnectEngine?: () => void;
41    onReady?: () => void;
42}) {
43    const native = useNative(), imageLibrary = useStore(s => s.imageModels), hardware = useStore(s => s.hardware);
44    // The choices are kept while the page is closed (Models -> Monitor -> Models used to start them over and a person lost track of what was downloading).
45    const [family, setFamily] = useKept('models.family', ''), [size, setSize] = useKept('models.size', ''), [context, setContext] = useKept('models.context', DEFAULT_CONTEXT), [contextPicked, setContextPicked] = useKept('models.contextPicked', false), [vision, setVision] = useKept('models.vision', 'yes');
46    const manual = useStore(s => s.setup?.choices?.onboarding?.manual ?? false);
47    const [expert, setExpert] = useKept('models.expert', manual), [multiple, setMultiple] = useKept('models.multiple', false), [roles, setRoles] = useKept<Roles | null>('models.roles', null), [kv, setKv] = useKept('models.kv', 'int8'), [lowRam, setLowRam] = useKept('models.lowRam', 'auto'), [split, setSplit] = useKept('models.split', 'auto');
48    const [resourceProfile, setResourceProfile] = useKept<'work' | 'performance'>('models.profile', 'work');
49    const [drafting, setDrafting] = useState('auto');
50    const [pending, setPending] = useState(false), [error, setError] = useState(''), [plan, setPlan] = useState<Record<string, any> | null>(null), [drafterGb, setDrafterGb] = useState<number | null>(null), [loadedError, setLoadedError] = useState(''), [tuneOpen, setTuneOpen] = useState(false), [jobId, setJobId] = useKept('models.job', ''), [manage, setManage] = useState(false);
51    const [toastedJob, setToastedJob] = useKept('models.toasted', ''), [appliedSig, setAppliedSig] = useKept('models.applied', '');
52    const presetPending = useRef(false), visionPicks = useRef(new Map<string, string>());   // Read pictures per version: what the person set, or what the version opened with; later reports of the host never overwrite it
53    const [deleting, setDeleting] = useState<string | null>(null);
54    const families = nativeCatalogModels(native?.catalog?.families ?? []);
55    const expertSection = useRef<HTMLElement>(null);
56    // The card is inserted above the fold and the browser's scroll anchoring
57    // immediately compensates by scrolling down, which cancelled the smooth
58    // scroll started in the same frame: the page stopped 121px in and clipped
59    // the cards above. Scroll after the layout has settled, and never animate
60    // when motion is reduced.
61    useEffect(() => {
62        if (!expert)
63            return;
64        const frame = requestAnimationFrame(() => expertSection.current?.scrollIntoView({
65            block: 'start',
66            behavior: matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth',
67        }));
68        return () => cancelAnimationFrame(frame);
69    }, [expert]);
70    const selectedFamily = families.find(f => f.family === family || f.sizes.some(size => size.sourceFamily === family)) ?? families[0], selectedSize = selectedFamily?.sizes.find(s => s.id === size) ?? selectedFamily?.sizes[0];
71    const installed = native?.installed?.models.find(m => m.model === selectedSize?.id), listPending = installedListPending(native);
72    // The sizes this model offers for the conversation context; it opens at 128K (or its largest size when that is smaller).
73    const contextList = native?.catalog?.contexts ?? [8192, 32768, 65536, 131072, 262144];
74    // The model that is running is shown as running, with Unload: not offered for loading again.
75    const downloaded = native?.installed?.models ?? [], allSizes = families.flatMap(f => f.sizes);
76    // A DeepSeek model of an earlier Strata (Control: no longer run): one line with where its files are, nothing to load or delete here.
77    const retiredModels = downloaded.filter(m => m.retired);
78    // This computer for the fit figures: the biggest usable graphics card, or the ones chosen under Expert settings.
79    const engineVendor = native?.hardware?.engine_vendor ?? null, roleCards: RoleCard[] = roleRows(native?.hardware?.gpus as RoleCard[] | undefined, engineVendor);
80    const pcGpus = (native?.hardware?.gpus ?? []).filter(g => g.usable !== false), pcGpu = [...pcGpus].sort((x, y) => y.vram_gb - x.vram_gb)[0] ?? null;
81    const pcRam = native?.hardware?.ram_gb || hardware?.memory.totalGb || null, pcVram = pcGpu?.vram_gb ?? 0;
82    // What the models that stay loaded (Run multiple models) already take: the fit figures are for what is left.
83    const sizeOf = (id: string) => allSizes.find(s => s.id === id);
84    const keptModels = expert && multiple ? (native?.instances ?? []).filter(i => i.status !== 'error' && i.model !== selectedSize?.id) : [];
85    const keptUse = keptModels.map(i => { const s = sizeOf(i.model); const card = pcGpus.find(g => g.index === i.gpus[0]); const f = s && pcRam ? fitOn(s, { ram_gb: pcRam, vram_gb: card?.vram_gb ?? pcVram }) : null; return { gpus: i.gpus, ram: f?.ramUseGb ?? 0, vram: f?.vramUseGb ?? 0 }; });
86    const keptRam = keptUse.reduce((n, u) => n + u.ram, 0);
87    const keptVramOn = (cards: number[]) => keptUse.filter(u => u.gpus.some(g => cards.includes(g))).reduce((n, u) => n + u.vram, 0);
88    // The models that are loaded now, with what each takes of graphics memory, RAM and SSD (the app's estimate from the same rule the fit uses: the engine does not report this split).
89    const loadedModels = (native?.instances ?? []).filter(i => i.status === 'ready' || i.status === 'loading').flatMap(i => { const s = sizeOf(i.model); if (!s || !pcRam) return []; const card = pcGpus.find(g => g.index === i.gpus[0]); return [{ id: i.model, label: `${i.label} · ${s.model}`, fit: fitOn(s, { ram_gb: pcRam, vram_gb: cardRoomGb([card ?? pcGpu].filter(c => !!c)) }, true, { context: i.context ?? context, kv }) }]; });
90    // The versions that are really on this computer: a config whose files are all there (every part of a model in parts).
91    const readyIds = useMemo(() => new Set(downloaded.filter(m => m.ready).map(m => m.model)), [downloaded]);
92    // What a running job works on, by its name in the list: "Qwen3.8-Flash-Next · Gyro · Smallest".
93    const jobLabel = (j: NativeJob) => { const v = allSizes.find(s => s.id === j.model || (s.model === j.model && (!j.family || s.sourceFamily === j.family))); const f = v && families.find(x => x.sizes.includes(v)); return v ? [f?.title, catalogVersionLabel(v)].filter(Boolean).join(' · ') : ''; };
94    const loadedSelected = native?.instances.find(i => i.model === selectedSize?.id && i.status === 'ready');
95    // What the running model was really started with is what the page shows: after Load the highlight must still be the profile that was applied.
96    useEffect(() => { if (loadedSelected?.resourceProfile)
97        setResourceProfile(loadedSelected.resourceProfile); }, [loadedSelected?.id, loadedSelected?.resourceProfile]);
98    // After a tuning, Expert mode shows the values the tuning set (the active preset's), not the ones the model was prepared with.
99    const [tunedNow, setTunedNow] = useState<{ name: string; fields: ReturnType<typeof tunedFields> } | null>(null);
100    useEffect(() => {
101        if (!selectedSize || !installed?.ready) { setTunedNow(null); return; }
102        let alive = true;
103        tuneEffective(selectedSize.id, resourceProfile, selectedSize.images ? vision : 'no').then(t => { if (alive) setTunedNow(t.applied && t.preset ? { name: t.preset.name, fields: tunedFields(t.changed) } : null); }).catch(() => { if (alive) setTunedNow(null); });
104        return () => { alive = false; };
105    }, [selectedSize?.id, installed?.ready, resourceProfile, vision, native?.jobs.filter(j => j.state === 'done').length]);
106    useEffect(() => { if (tunedNow?.fields.kv) setKv(tunedNow.fields.kv as typeof kv); if (tunedNow?.fields.lowRam) setLowRam(tunedNow.fields.lowRam as typeof lowRam); }, [tunedNow]);
107    const active = native?.jobs.find(j => j.state === 'running');
108    // Catalog groups share quantization names. A remembered job must still belong
109    // to the selected version; another family's failure stays in diagnostics.
110    const selectedJob = (j: NativeJob) => !!selectedSize && (j.model === selectedSize.id ||
111        (j.model === selectedSize.model && j.family === selectedSize.sourceFamily));
112    const job = native?.jobs.find(j => j.id === jobId && selectedJob(j)) ??
113        native?.jobs.filter(j => ['load', 'install'].includes(j.kind) && selectedJob(j)).at(-1);
114    // This model can read pictures but the installed engine has no picture reader: pictures run off for now, nothing is saved, and the row says nothing wordy.
115    // While a job runs the host is busy and its picture-reader probe can drop out for a moment: the checkbox then keeps what it showed, so the person's choice never flickers.
116    const noReaderNow = !!selectedSize?.imagesCapable && !selectedSize.images, noReaderSeen = useRef({ id: '', value: false });
117    noReaderSeen.current = steadyNoReader(noReaderNow, !!active, noReaderSeen.current, selectedSize?.id ?? '');
118    const noReader = noReaderSeen.current.value;
119    // Graphics cards (strata_control/gpu_fit.py: the one rule of the app, the Terminal and setup): the recommended option is pre-selected, the person may pick another
120    // A card a loaded model already uses (a second model on it usually does not fit): said on its row, and not the pre-selected one while another is free.
121    const usedBy = (cards: number[]) => (native?.instances ?? []).filter(i => i.status !== 'error' && i.model !== selectedSize?.id && i.gpus.some(g => cards.includes(g))).map(i => i.label);
122    // Main / Helper / Off per card (src/lib/gpu-roles.ts, strata_control/gpu_roles.py): the recommendation until the person changes a role; a loaded model shows what it was started with.
123    // With Run multiple models, a card another loaded model already uses starts Off for this one (while a free card exists).
124    const busy = (i: number) => expert && multiple && usedBy([i]).length > 0, freeCards = roleCards.filter(c => !busy(c.index));
125    const recommended = freeCards.length && freeCards.length < roleCards.length ? { ...recommendRoles(freeCards, engineVendor), roles: { ...Object.fromEntries(roleCards.map(c => [c.index, 'off'])), ...recommendRoles(freeCards, engineVendor).roles } as Roles } : recommendRoles(roleCards, engineVendor);
126    const loadedRoles = loadedSelected && roleCards.length > 1 ? fromLegacy(loadedSelected.gpus, roleCards, 'as_given') : null;
127    const effectiveRoles: Roles = roleCards.length > 1 ? (roles ? normalizeRoles(roles, roleCards) : loadedRoles ?? recommended.roles) : recommended.roles;
128    const customRoles = !sameRoles(effectiveRoles, recommended.roles);
129    const changeRoles = (next: Roles) => setRoles(sameRoles(next, recommended.roles) ? null : next);
130    const cardsGpus: number[] = roleCards.length > 1 ? roleOrder(effectiveRoles, roleCards) : [];
131    const chosenCards = cardsGpus;
132    const fitHardware = { ram_gb: pcRam ? Math.max(0, pcRam - keptRam) : pcRam, vram_gb: Math.max(0, (roleCards.length > 1 ? vramOn(effectiveRoles, roleCards) : pcVram) - keptVramOn(chosenCards)) };
133    // The fit label's own figures (strata_control/fit_label.py): a unified-memory chip counts only its carve-out beside the RAM (lib/native-fit.ts cardRoomGb).
134    const labelHardware = { ram_gb: fitHardware.ram_gb, vram_gb: roleCards.length > 1 ? fitHardware.vram_gb : Math.max(0, cardRoomGb(pcGpu ? [pcGpu] : []) - keptVramOn(chosenCards)) };
135    // How the chosen version sits on this computer (the same rule as the version list): the page says a word only for "may run slower".
136    const fitState = selectedSize ? versionFit(selectedSize, labelHardware, { context, kv }) : 'unknown';
137    // A layer split written for another number of cards no longer fits: back to automatic.
138    useEffect(() => { if (cardsGpus.length <= 1 && split !== 'auto') setSplit('auto'); }, [cardsGpus.length]);
139    // Drafting (which drafter guesses ahead): a choice exists only for Qwen3.8-Flash-Next (the qwen and gyro families) on a computer whose engine can run DFlash2; every other model has no dropdown.
140    const draftingChoices = selectedSize ? draftingOptions(selectedSize.sourceFamily ?? selectedFamily?.family, native?.capabilities?.dflash2) : null;
141    // Control's plan says the all-cards split engine takes an Auto start on this computer (it runs the MTP draft layer); it needs two or more cards, so one card chosen in the roles is not it.
142    const splitCards = native?.capabilities?.dflash2Split === true && cardsGpus.length !== 1;
143    // The drafter this run loads (one per run): the fit and the plan use its graphics memory (DFlash2 0.89 GB, MTP 1.08 GB).
144    const nowDrafter = draftingChoices ? resolveDrafter(drafting, selectedSize?.sourceFamily ?? selectedFamily?.family, native?.capabilities?.dflash2, splitCards) : 'mtp';
145    // DFlash2 with the converted pack (the selector as Q4_0 rows) takes 175 MiB less graphics memory: the fit says the true number (Control's `selq4`, strata_control/drafting.py vram_gb).
146    const fitDrafter = nowDrafter === 'dflash2' && native?.capabilities?.dflash2Selq4 === true ? 'dflash2_selq4' : nowDrafter;
147    const draftingWords: Record<string, string> = { auto: 'Auto (recommended)', mtp: 'MTP', dflash2: 'DFlash2', off: 'Off' };
148    const values = useMemo(() => ({ family: selectedSize?.sourceFamily, model: selectedSize?.model, context, vision: selectedSize?.images ? vision : 'no', kv, low_ram: lowRam, resourceProfile, ...(cardsGpus.length > 1 ? { gpus: cardsGpus.join(',') } : cardsGpus.length === 1 ? { gpu: cardsGpus[0] } : {}), loadAfter: true, multiple: expert && multiple }), [selectedSize?.sourceFamily, selectedSize?.model, context, vision, kv, lowRam, resourceProfile, cardsGpus.join(','), expert, multiple]);
149    const downloadValues = { ...values, ...(draftingChoices ? { drafting } : {}), vision: selectedSize?.images && vision !== 'no' ? 'yes' : 'no', loadVision: selectedSize?.images ? vision : 'no', ...(noReader ? { visionUnavailable: true } : {}), ...(contextPicked ? { contextChosen: true } : {}) };
150    useEffect(() => { if (!native?.catalog || family)
151        return; const current = native.instances.find(i => i.id === native.defaultId); const rec = native.catalog.recommendation ?? { family: null, model: null }; const loadedFamily = families.find(f => f.sizes.some(s => s.id === current?.model)); const have = new Set((native.installed?.models ?? []).filter(m => m.ready).map(m => m.model)); const installedFamily = families.find(f => f.sizes.some(s => have.has(s.id))); const f = loadedFamily ?? installedFamily ?? families.find(f => f.family === rec.family || f.sizes.some(s => s.sourceFamily === rec.family)) ?? families[0]; if (f) {
152        setFamily(f.family);
153        const first = f.sizes.find(s => s.id === current?.model) ?? f.sizes.find(s => have.has(s.id)) ?? f.sizes.find(s => s.model === rec.model) ?? f.sizes[0];
154        setSize(first?.id ?? '');
155        // 128K for every configuration (the engine's own recommendation is not what the app opens with).
156        setContext(defaultContext(native.catalog.contexts));
157    } }, [native?.catalog]);
158    // The version that is selected: a model already on this PC shows the settings it was prepared with (so loading it never asks to download);
159    // a model that is not yet downloaded starts with Read pictures ON (owner rule: pictures on by default). This runs when the SELECTION or the
160    // installed settings change, not when the page opens again: coming back from another tab keeps what was chosen.
161    useEffect(() => {
162        if (presetPending.current) {
163            presetPending.current = false;
164            return;
165        }
166        if (!selectedSize)
167            return;
168        const sig = installed ? ['have', installed.model, installed.ready, installed.context, installed.contextChosen, installed.kv, installed.lowRamMode, installed.drafting, installed.resourceProfile].join('|') : `new|${selectedSize.id}`;
169        if (sig === appliedSig)
170            return;
171        setAppliedSig(sig);
172        if (installed) {
173            // The context it was prepared with when someone chose it; a model that holds the old default (32K) without the marker opens at the new default.
174            if (installed.context)
175                setContext(openingContext(installed.context, contextList, installed.contextChosen === true));
176            // Off only when the person turned it off (a saved choice on a model that has the picture files); a model prepared without them (images 'off', or an old "off" saved while the engine had no reader) starts with it ON.
177            // The first time this version is seen as installed it opens from its saved settings; from then on that value is the person's (a host report that changes while a job runs never changes it).
178            const known = visionPicks.current.get(selectedSize.id), value = shownVision(known, installed);
179            if (known === undefined)
180                visionPicks.current.set(selectedSize.id, value);
181            setVision(value);
182            setKv(installed.kv ?? 'int8');
183            setLowRam(installed.lowRamMode ?? 'auto');
184            setDrafting(installed.drafting ?? 'auto');
185            // A model never tuned has no saved choice: keep the page's own (Leave room for my work) rather than flipping to Full performance.
186            if (installed.resourceProfile && !loadedSelected?.resourceProfile)
187                setResourceProfile(installed.resourceProfile);
188        }
189        else {
190            setDrafting('auto');    // a model that is not installed has no saved choice: Auto (the page never carries the last model's Drafting over to another one)
191            setVision(shownVision(visionPicks.current.get(selectedSize.id), undefined));
192            if (!contextPicked)    // a context the person picked stays; otherwise 128K
193                setContext(defaultContext(contextList));
194        }
195    }, [selectedSize?.id, installed?.model, installed?.ready, installed?.context, installed?.contextChosen, installed?.kv, installed?.lowRamMode, installed?.drafting, installed?.resourceProfile]);    // drafting too: the saved choice that arrives after the page opened (an app restart) is the one shown, not Auto (0.2.15 release test)
196    const pickVision = (next: string) => { if (selectedSize) visionPicks.current.set(selectedSize.id, next); setVision(next); };
197    useEffect(() => { if (jobId && job?.state === 'done' && job.id !== toastedJob && (job.kind === 'load' || job.kind === 'install')) {
198        setToastedJob(job.id);
199        selectRemoteModel(null);
200        pushToast('success', 'Model ready', 'Start a new chat when you’re ready.');
201    } }, [job?.state]);
202    // The picture files came without a load: a model that is loaded right now still runs without pictures until it is loaded again.
203    useEffect(() => { if (jobId && job?.state === 'done' && job.id !== toastedJob && job.kind === 'pictures') {
204        setToastedJob(job.id);
205        pushToast('success', 'Picture files ready', loadedSelected && !loadedSelected.vision ? 'Load the model again to read pictures.' : 'Picture reading is ready.');
206    } }, [job?.state]);
207    async function perform(action: () => Promise<unknown>) { setPending(true); setError(''); try {
208        await action();
209    }
210    catch (e) {
211        setError(e instanceof Error ? e.message : String(e));
212    }
213    finally {
214        setPending(false);
215    } }
216    /* Load model: no pop-up. The press itself is the yes. The plan is read first (Control checks the free disk space); a disk that is too small is said
217       here, in the page, and nothing starts. Otherwise the download or the apply-and-load starts at once, with its size and notes in the page. */
218    // The model is on this computer and only its picture files are missing: the Load fetches those files itself (about 0.9 GB, once) and goes on; it does not run the installer again.
219    const picturesByLoad = fetchesPictures(selectedSize, native?.packaged);
220    async function load() { if (!selectedSize)
221        return; setPlan(null); setDrafterGb(null); await perform(async () => { if (installed?.ready && context === installed.context && (!selectedSize?.images || vision === 'no' || picturesByLoad || installed.images && installed.images !== 'off') && (!installed.kv || kv === installed.kv || kv === tunedNow?.fields.kv) && (lowRam === (installed.lowRamMode ?? 'auto') || lowRam === tunedNow?.fields.lowRam)) {
222        const result = await nativeAction<NativeJob>('operations', { kind: 'load', args: { model: selectedSize.id, resourceProfile, vision: selectedSize?.images ? vision : 'no', ...(noReader ? { visionUnavailable: true } : {}), ...(contextPicked && !installed.contextChosen ? { contextChosen: true } : {}), multiple: expert && multiple, ...(draftingChoices ? { values: { drafting } } : {}), ...(roleCards.length > 1 ? (roles && customRoles ? { gpuRoles: effectiveRoles, layerSplit: expert && cardsGpus.length > 1 ? split : 'auto' } : cardsGpus.length ? { gpus: cardsGpus, gpusFromRecommendation: true, layerSplit: expert && cardsGpus.length > 1 ? split : 'auto' } : {}) : {}) } });
223        setJobId(result.id);
224    }
225    else {
226        const planned = await api.post<Record<string, any>>('/api/native/plan', downloadValues);
227        if (planned?.plan?.disk_short) {
228            setPlan(planned);
229            return;
230        }
231        setDrafterGb(planned?.drafting?.needed ? Number(planned.drafting.gb) || null : null);
232        const result = await nativeAction<NativeJob>('operations', { kind: 'install', args: downloadValues });
233        setJobId(result.id);
234    } }); }
235    /* Unload / Use in chat: a refusal (a chat is still using the model) is said here, beside the button, not far below the page. */
236    async function loadedAction(path: string) { setLoadedError(''); setPending(true); try {
237        await nativeAction(path);
238    }
239    catch (e) {
240        setLoadedError(e instanceof Error ? e.message : String(e));
241    }
242    finally {
243        setPending(false);
244    } }
245    // The drafter note belongs to the download that is running; the not-enough-disk message belongs to the version it was said for.
246    useEffect(() => { if (!active) setDrafterGb(null); }, [!!active]);
247    useEffect(() => { setPlan(null); }, [selectedSize?.id]);
248    return <Screen plainHeader title={<span className="sr-only">{tr("Models")}</span>} titleDetail={<FamilySwitch families={families} selected={selectedFamily?.family} onPick={f => { setFamily(f.family); setSize((f.sizes.find(s => native?.installed?.models.some(m => m.model === s.id && m.ready)) ?? f.sizes[0])?.id ?? ''); setVision('yes'); setError(''); }}/>} actions={<Btn icon="sliders" aria-pressed={expert} aria-expanded={expert} aria-controls="native-expert-settings" onClick={() => { setExpert(v => !v); setMultiple(false); }}>{tr(expert ? 'Close expert mode' : 'Expert mode')}</Btn>}>
249  <div className="native-model-layout native-model-layout--fit">
250   <div className="stack">
251    {tr(retiredModels.length ? <section className="st-card native-retired" role="status">{retiredModels.map(m => <p key={m.model} className="small"><strong>{m.model_name ?? m.model}</strong> <span className="muted">{m.retired?.line}</span></p>)}</section> : null)}
252    {tr(native?.instances.length && expert ? <section className="st-card native-running"><h2 className="h4">{tr(native.instances.length === 1 ? 'Loaded model' : 'Loaded models')}</h2>{tr(native.instances.map(i => <div key={i.id}><span><strong>{tr(i.label)}</strong><small>{tr(i.gpus.length ? i.gpus.map((g, n) => `GPU ${g + 1}${i.gpus.length > 1 && n === i.gpus.length - 1 ? ' (Main)' : ''}`).join(' + ') : 'Automatic GPU')} · {tr(i.status === 'ready' ? i.phase : i.status)}{tr(native.defaultId === i.id ? ' · Chat default' : '')}</small>{tr(i.error ? <small className="inline-error">{tr(plainFailureText(i.error).message)}</small> : null)}</span>{tr(expert && multiple && native.defaultId !== i.id ? <Btn size="sm" variant="ghost" onClick={() => void loadedAction(`instances/${i.id}/default`)}>{tr("Use in chat")}</Btn> : null)}<Btn size="sm" disabled={pending || !!active} onClick={() => void loadedAction(`instances/${i.id}/unload`)}>{tr("Unload")}</Btn></div>))}{tr(loadedError ? <ErrorNotice message={loadedError} next={tr("Stop or answer the chat that is still using this model, then try again.")}/> : null)}</section> : null)}
253    <section className="st-card native-choice">{tr(selectedFamily && (!selectedSize || selectedSize.needsNewerEngine) ? <><h2 className="h3">{tr("This model needs a newer engine.")}</h2><p className="small muted">{tr("Update your engine or import a local GGUF for this model.")}</p><Btn onClick={() => { setSettingsSection('Engine'); go('settings'); }}>{tr("Update engine")}</Btn><Btn variant="ghost" onClick={onImport ?? onOtherModels}>{tr("Use a local GGUF\u2026")}</Btn></> : null)}{tr(!selectedFamily && !listPending ? <><h2 className="h3">{tr("Set up Strata")}</h2><p className="small muted">{tr("Connect your local installation to choose an optimized model.")}</p><Btn variant="primary" onClick={onConnectEngine ?? (() => { setSettingsSection('Engine'); go('settings'); })}>{tr("Connect engine")}</Btn>{tr(native?.error ? <p className="small inline-error">{tr(native.error)}</p> : null)}<Btn variant="ghost" onClick={onOtherModels}>{tr("Other local models")}</Btn></> : null)}
254     {tr(selectedSize ? <>
255      <div className="native-choice__head"><span className="native-choice__label">{tr("Version")}</span><span className="native-choice__tools"><NativePresets values={values} onApply={v => { const f = families.find(f => f.family === v.family || f.sizes.some(s => s.sourceFamily === v.family)); if (!f)
256        return; presetPending.current = true; setFamily(f.family); setSize(f.sizes.find(s => s.model === v.model && (s.sourceFamily === v.family || f.family === v.family))?.id ?? ''); setContext(v.context ?? 32768); setVision(v.vision ?? 'no'); setKv(v.kv ?? 'int8'); setLowRam(v.low_ram ?? 'auto'); }}/><Btn icon="models" size="sm" variant="ghost" onClick={() => setManage(true)}>{tr("Manage models")}</Btn></span></div>
257      <div className="native-version"><VersionPicker pick={native?.catalog?.recommendation} versions={selectedFamily?.sizes ?? []} details={expert} selectedId={selectedSize.id} hardware={labelHardware} cache={{ context, kv, drafter: fitDrafter }} downloaded={readyIds} onSelect={id => { const f = families.find(f => f.sizes.some(s => s.id === id)); if (f) { setFamily(f.family); setSize(id); setError(''); } }} onOwnGguf={() => (onImport ?? onOtherModels)()}/></div>
258      {tr(fitState === 'ssd' ? <p className="small muted native-spill-note"><Icon name="disk" size={16}/>{tr("may run slower")}</p> : null)}
259      <div className="native-choice__row">
260       <div className="native-choice__col"><span className="native-choice__label">{tr("Conversation context")}</span><Select ariaLabel={tr("Conversation context")} value={String(context)} options={contextChoices(contextList, context).map(n => ({ value: String(n), label: n % 1024 === 0 ? `${n / 1024}K` : `${n.toLocaleString()} tokens` }))} onChange={n => { setContextPicked(true); setContext(Number(n)); }}/></div>
261       {tr(draftingChoices ? <div className="native-choice__col"><span className="native-choice__label">{tr("Drafting")}</span><Select ariaLabel={tr("Drafting")} value={drafting} disabled={!!active} options={draftingChoices.map(v => ({ value: v, label: tr(draftingWords[v]) }))} onChange={v => setDrafting(String(v))}/></div> : null)}
262       <div className="native-choice__col"><span className="native-choice__label">{tr("Computer use")}</span><div className="native-resource-choice"><button aria-pressed={resourceProfile === 'work'} data-selected={resourceProfile === 'work'} onClick={() => setResourceProfile('work')}><Icon name="computer" size={20}/><span><strong>{tr("Leave room for my work")}</strong><small>{tr("Keeps 5-8 GB of memory free for other programs.")}</small></span>{tr(resourceProfile === 'work' ? <Icon name="check" size={15}/> : null)}</button><button aria-pressed={resourceProfile === 'performance'} data-selected={resourceProfile === 'performance'} onClick={() => setResourceProfile('performance')}><Icon name="bolt" size={20}/><span><strong>{tr("Full performance")}</strong><small>{tr("Give Strata priority")}</small></span>{tr(resourceProfile === 'performance' ? <Icon name="check" size={15}/> : null)}</button></div></div>
263      </div>
264      {tr(contextIsTight(fitHardware.vram_gb, context, kv, selectedSize?.context_fit) ? <p className="small muted native-spill-note"><Icon name="warning" size={16}/>{tr("This much context takes about {0} GB of graphics memory and leaves little room for the model on this card. Strata will try it; if it does not start, choose a shorter context.", [contextCacheGb(context, kv, selectedSize?.context_fit).toFixed(1)])}</p> : null)}
265      {tr(loadedSelected?.metrics?.engine?.drafting_note ? <p className="small muted native-spill-note">{tr(String(loadedSelected.metrics.engine.drafting_note))}</p> : null)}
266      {roleCards.length > 1 ? <GpuRoles cards={roleCards} roles={effectiveRoles} custom={customRoles} recommendedRoles={recommended.roles} vendor={engineVendor} disabled={!!active} onChange={changeRoles} onBack={() => setRoles(null)} memoryGb={selectedSize ? memoryNeeded(selectedSize) || null : null} ramGb={pcRam} spillGb={selectedSize ? fitOn(selectedSize, fitHardware).spillGb : 0} usedBy={i => usedBy([i])}/> : null}
267      {tr(roleCards.length > 1 && (native?.hardware?.gpus ?? []).length > roleCards.length ? <p className="small muted native-cards__note">{tr('Not used: {0} (another make of graphics card than this engine drives).', [(native?.hardware?.gpus ?? []).filter(g => !roleCards.some(r => r.index === g.index && r.vendor === g.vendor)).map(g => g.name).join(', ')])}</p> : null)}
268      {tr(selectedSize?.images || noReader ? <><div className="native-image-reading"><label title={noReader ? tr("Needs an engine update") : undefined}><input type="checkbox" checked={!noReader && vision !== 'no'} disabled={noReader || !!active} onChange={e => pickVision(e.target.checked ? 'yes' : 'no')}/><span><strong>{tr("Read pictures")}</strong></span></label>{tr(expert && vision !== 'no' ? <Select ariaLabel={tr("Image encoder device")} value={vision === 'yes' ? 'yes' : vision} options={[{ value: 'yes', label: 'Automatic' }, { value: 'gpu', label: 'GPU' }, { value: 'cpu', label: 'CPU' }]} onChange={pickVision}/> : null)}</div>{tr(vision !== 'no' && !noReader && !active && loadedSelected?.picturesNote ? <p className="small muted native-vision-note" role="status">{tr(loadedSelected.picturesNote)}</p> : null)}</> : null)}
269     </> : null)}
270     <EngineWarning native={native}/>
271     <EngineUpdateCard native={native} busy={!!active}/>
272     <EngineUpdateDialog open={false} onClose={() => {}}/>
273     {tr(active ? <NativeProgress job={active} label={jobLabel(active)}/> : null)}
274     {tr(active && drafterGb ? <p className="small muted native-spill-note"><Icon name="info" size={16}/>{tr(nowDrafter === 'mtp' ? (splitCards && drafting === 'auto' ? "Auto uses the all-cards split engine with the MTP draft layer: about {0} GB to download, once." : "Choosing MTP downloads its drafter: about {0} GB, once.") : "Choosing DFlash2 downloads its drafter: about {0} GB, once.", [drafterGb])}</p> : null)}
275     {tr(job?.state === 'error' ? <ErrorNotice message={job.error ?? job.summary} action={tr("Retry")} onRetry={() => void load()}/> : null)}
276     {tr(plan?.plan?.disk_short ? <ErrorNotice message={plan.plan.disk_short}/> : null)}
277     {tr(error ? <ErrorNotice message={error}/> : null)}
278     {selectedSize && installed?.ready ? <NativeTuneBadge model={selectedSize.id} resourceProfile={resourceProfile} vision={selectedSize.images ? vision : 'no'}/> : null}
279     <div className="native-model-actions">{tr(!loadedSelected && !listPending && !installed?.ready && selectedSize?.download_gb && !active ? <span className="native-download-size small muted">{tr("{0} download", [sizeGb(selectedSize.download_gb)])}</span> : null)}{tr(loadedSelected ? <><Btn disabled={pending || !!active} onClick={() => void perform(() => nativeAction(`instances/${loadedSelected.id}/unload`))}>{tr("Unload")}</Btn>{tr(onReady ? <Btn variant="primary" onClick={onReady}>{tr("Start chatting")}</Btn> : <span className="native-ready">{tr("Ready")}</span>)}</> : onReady && native?.instances.some(i => i.status === 'ready') ? <Btn variant="primary" onClick={onReady}>{tr("Start chatting")}</Btn> : <><Btn icon="gauge" disabled={listPending || !installed?.ready || !native?.available || !!active || !selectedSize || selectedSize.needsNewerEngine} onClick={() => setTuneOpen(true)}>{tr("Tune model")}</Btn><Btn variant="primary" icon="models" disabled={pending || listPending || !native?.available || !!active || !selectedSize || !!selectedSize.needsNewerEngine} onClick={() => void load()}>{tr(pending || listPending ? 'Checking…' : installed?.ready ? 'Load model' : 'Download & load')}</Btn></>)}</div>
280    </section>
281    {tr(expert && IMAGE_GENERATION_VISIBLE ? <><details className="native-generation"><summary>{tr("Image generation ")}<span className="muted">{tr("Optional")}</span></summary><RunImageModel library={imageLibrary} disabled={pending || !!active} onChange={() => { }}/></details></> : null)}
282    {tr(expert ? <section id="native-expert-settings" ref={expertSection} className="st-card native-execution"><h2 className="h4">{tr("Expert settings")}</h2>{tr(tunedNow ? <p className="small muted native-tuned-note">{tr("Showing the values of your tuning '{0}'.", [tunedNow.name])}</p> : null)}<label className="native-toggle"><span><strong>{tr("Run multiple models")}</strong><small>{tr("Loading another model keeps the others loaded.")}</small></span><button className="st-toggle" role="switch" aria-label={tr("Run multiple models")} aria-checked={multiple} onClick={() => setMultiple(v => !v)}/></label>{tr(cardsGpus.length > 1 ? <label className="input-field"><span>{tr("Layer split")}</span><input className="st-input" value={split} placeholder={tr("auto or 16,32")} onChange={e => setSplit(e.target.value)}/></label> : null)}<div className="native-field-grid"><label className="input-field"><span>{tr("Context memory")}</span><Select ariaLabel={tr("Native KV format")} value={kv} options={KV_CHOICES.map(c => ({ value: c.value, label: c.label, title: c.hint }))} onChange={setKv}/>{tr(KV_CHOICES.find(c => c.value === kv)?.hint ? <small className="muted native-kv-hint">{tr(KV_CHOICES.find(c => c.value === kv)!.hint)}</small> : null)}</label><label className="input-field"><span>{tr("Low-RAM mode")}</span><Select ariaLabel={tr("Native low RAM mode")} value={lowRam} options={['auto', 'resident', 'mmap'].map(v => ({ value: v, label: v === 'auto' ? 'Automatic' : v === 'resident' ? 'Keep experts in RAM' : 'Read experts from disk' }))} onChange={setLowRam}/></label></div>{tr(installed ? <><NativeSettingsFile model={installed.model}/><NativeSettings model={installed.model}/></> : <p className="small muted">{tr("More engine parameters become available after preparing this version.")}</p>)}</section> : null)}
283   </div>
284   <aside className="stack stack--lg"><PcComputer gpuName={pcGpu?.name} ramGb={pcRam} vramGb={pcVram} vramAllGb={pcGpus.reduce((n, g) => n + g.vram_gb, 0) || undefined} loaded={loadedModels} selectedLoaded={!!selectedSize && loadedModels.some(m => m.id === selectedSize.id)} inUse={keptModels.length ? { ramGb: Math.round(keptRam), vramGb: Math.round(keptVramOn(chosenCards)) } : null} fit={selectedSize ? fitOn(selectedSize, labelHardware, true, { context, kv }) : null}/><section className="st-card"><h2 className="h4">{tr("Model files")}</h2><code className="native-path">{installed?.dataFolder ?? native?.downloadDirectory ?? native?.installed?.data_folder ?? 'Choose an installation'}</code><div className="model-files__actions"><Btn size="sm" variant="ghost" onClick={() => { setSettingsSection('Storage'); go('settings'); }}>{tr("Storage settings")}</Btn><Btn size="sm" variant="ghost" onClick={onOtherModels} className="other-models">{tr("Other local models\u2026")}</Btn></div></section></aside>
285  </div>
286  
287  <NativeDeleteDialog id={deleting} onClose={() => setDeleting(null)}/>
288  <Dialog open={manage} compact title={tr("Manage models")} onClose={() => setManage(false)} actions={<Btn onClick={() => setManage(false)}>{tr("Done")}</Btn>}><div className="native-manage">
289   {tr(downloaded.some(m => !m.retired && (m.ready || m.diskGb)) ? <div className="native-manage-list" role="list">{tr(downloaded.filter(m => !m.retired && (m.ready || m.diskGb)).map(m => { const v = allSizes.find(x => x.id === m.model), loaded = native?.instances.some(i => i.model === m.model && i.status === 'ready'); return <div className="native-manage-row" role="listitem" key={m.model} data-selected={selectedSize?.id === m.model || undefined}>
290     <span className="native-manage-name"><strong>{tr(v ? `${catalogVersionLabel(v)}` : m.model_name ?? m.model)}</strong><small>{tr(v ? v.model : m.model)}{tr(m.diskGb ? ` · ${sizeGb(m.diskGb)}` : '')}</small></span>
291     <span className={loaded ? 'st-pill st-pill--accent' : 'st-pill'}>{tr(loaded ? 'Loaded' : 'Downloaded')}</span>
292     <span className="native-manage-actions"><Btn size="sm" disabled={!!active || !v} onClick={() => { if (v) setSize(v.id); setManage(false); }}>{tr(selectedSize?.id === m.model ? 'Selected' : 'Use version')}</Btn><Btn size="sm" variant="danger" disabled={!!active} onClick={() => setDeleting(m.model)}>{tr("Delete")}</Btn></span>
293    </div>; }))}</div> : <p className="small muted native-manage-empty">{tr("No downloaded models")}</p>)}
294   <div className="native-manage-foot"><Btn variant="ghost" icon="folder" onClick={() => { setManage(false); (onImport ?? onOtherModels)(); }}>{tr("Use a local GGUF…")}</Btn></div>
295  </div></Dialog>
296 {selectedSize ? <NativeTune open={tuneOpen} onClose={() => setTuneOpen(false)} model={selectedSize.id} resourceProfile={resourceProfile} vision={selectedSize.images ? vision : 'no'} visionUnavailable={noReader}/> : null}
297 </Screen>;
298}
299/** setup's "5/7: downloading …" as a sentence a person can read. */
300/** The listed context sizes, plus the model's own when it is a size set by hand in its run config (the select would show nothing, and a load would silently pick another). */
301export function contextChoices(listed: number[], current: number): number[] {
302    return current > 0 && !listed.includes(current) ? [...listed, current].sort((a, b) => a - b) : listed;
303}
304function installStepLabel(step?: string): string {
305    const n = Number(step?.match(/^(\d+)\//)?.[1]);
306    if (!n)
307        return step ? 'Getting things ready…' : 'Downloading and preparing model files…';
308    return n <= 2 ? 'Checking your computer…' : n <= 4 ? 'Getting things ready…' : n === 5 ? 'Downloading your model…' : n === 6 ? 'Preparing your model for Strata…' : 'Finishing up…';
309}
310/** The formats of the conversation cache and what each costs, in one line (the memory figures are from docs/DETAILS.md: Q4 half of 8-bit, K8/V4 23% less). */
311const KV_CHOICES = [
312    { value: 'int8', label: 'INT8', hint: 'INT8 is the standard: good quality and a medium amount of memory.' },
313    { value: 'q4_0', label: 'Q4', hint: 'Q4 uses about half the memory of INT8, but is less precise on very long documents.' },
314    { value: 'k8v4', label: 'K8 / V4', hint: 'K8 / V4 uses about a quarter less memory than INT8 and is more precise than Q4. Prompts are 2 to 5% slower.' },
315];
316/**
317 * What a running job is doing, and nothing else: one line with the step, one small line with the numbers when there are any, a thin bar, and Stop as a
318 * plain word. A soft ring breathes beside the step (transform and opacity only; still under reduced motion). The bar fills with the real percent, or
319 * glides while no percent is known.
320 */
321export function NativeProgress({ job, label }: {
322    job: NativeJob;
323    label?: string;
324}) {
325    const download = installProgress(job.result?.install), detailsText = [job.details, job.result?.install?.engine?.details].filter(Boolean).join('\n');
326    const eta = useDownloadEta(job.kind === 'install' ? download : undefined, job.id);
327    const [stopping, setStopping] = useState(false), [error, setError] = useState('');
328    async function stop() { setStopping(true); setError(''); try {
329        await nativeAction(`operations/${job.id}/cancel`);
330    }
331    catch (e) {
332        setError(e instanceof Error ? e.message : String(e));
333    }
334    finally {
335        setStopping(false);
336    } }
337    const running = job.state === 'running', stoppable = ['install', 'load', 'pictures', 'autotune', 'recheck'].includes(job.kind);
338    const kindTitle = job.kind === 'install' ? (label ? tr('Preparing {0}', [label]) : 'Preparing your model') : job.kind === 'load' ? 'Loading model' : job.kind === 'pictures' ? 'Downloading the picture files' : job.kind === 'autotune' || job.kind === 'recheck' ? 'Tuning your model' : 'Engine operation';
339    const step = job.kind === 'install' ? (job.result?.install?.label ?? (download && /^5\//.test(job.result?.install?.step ?? '') ? stageText(installStage(job.result?.install)) : installStepLabel(job.result?.install?.step))) : job.summary;
340    const percent = download && Number.isFinite(download.percent) ? Math.max(0, Math.min(100, Number(download.percent))) : null;
341    const numbers = [percent !== null ? `${Math.round(percent)}%` : '', download && Number.isFinite(download.done_gb) && Number.isFinite(download.total_gb) ? `${download.done_gb!.toFixed(1)} / ${download.total_gb!.toFixed(1)} GB` : '', eta].filter(Boolean).join(' · ');
342    const words = tr(step || kindTitle);
343    return <section className="st-card native-progress" aria-live="polite" data-state={job.state}>
344        <div className="native-progress__row">
345            <span className="native-progress__ring" aria-hidden="true"/>
346            <div className="native-progress__text"><p className="native-progress__step">{words}</p>{numbers ? <p className="native-progress__numbers">{numbers}</p> : null}</div>
347            {stoppable && running ? <button type="button" className="native-progress__stop" disabled={stopping} onClick={() => void stop()}>{tr(stopping ? 'Stopping…' : 'Stop')}</button> : null}
348        </div>
349        {running ? <div className="native-progress__bar" role="progressbar" aria-label={words} aria-valuemin={0} aria-valuemax={100} aria-valuenow={percent ?? undefined} data-indeterminate={percent === null || undefined}><i style={percent !== null ? { transform: `scaleX(${percent / 100})` } : undefined}/></div> : null}
350        <DownloadParts parts={job.result?.install?.parts}/>
351        {detailsText ? <details><summary className="small">{tr("Details")}</summary><pre className="small muted error-detail-text">{detailsText}</pre></details> : null}
352        {error ? <ErrorNotice message={error}/> : null}
353    </section>;
354}