Files
strata/apps/host/src/components/NativeLocalModel.tsx
T

86 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { tr } from '../lib/i18n';
import { useState } from 'react';
import { Screen } from './layout';
import { Btn, ErrorNotice, Icon } from './ui';
import { api } from '../lib/client';
import { nativeAction, useNative, type NativeJob } from '../lib/native';
import { nativeCatalogModels, catalogVersionLabel } from '../lib/native-catalog';
import { sizeGb } from '../lib/format';
import { NativeProgress } from './NativeModels';
interface Preview { path: string; preview: { file_count: number; total_bytes: number; file_set_status: string; issues: string[]; candidate_selection: { family: string; model: string } | null } }
/** The words for why a folder of GGUF files cannot be used: a sentence and what to do, the checker's own codes under Details. */
function problem(issues: string[]): { message: string; next: string } {
const has = (re: RegExp) => issues.some(i => re.test(i));
if (has(/incomplete_or_invalid_shard_set|multiple_model_groups/))
return { message: 'Some parts of this model are missing, or the folder holds more than one model.', next: 'Put every part of one model in one folder, then check again.' };
if (has(/unrecognized_or_unsupported_model_variant|unsupported_architecture|unsupported_or_missing_architecture|expert_quantization|tokenizer/))
return { message: 'Strata can’t run these files yet.', next: 'Strata runs the Qwen3.8-Flash-Next models. Download one from Models, or choose a folder with its files.' };
return { message: 'These files could not be checked.', next: 'Check that the folder holds complete GGUF files, then try again.' };
}
/** "Use a local GGUF": choose the folder of a model you already have, Strata checks it, and prepares and loads it without copying
* or downloading the model files. The same page layout and cards as the optimized models page. */
export function NativeLocalModel({ onBack }: { onBack: () => void }) {
const native = useNative();
const [typed, setTyped] = useState(''), [checked, setChecked] = useState<Preview | null>(null), [pending, setPending] = useState(false), [error, setError] = useState(''), [jobId, setJobId] = useState('');
const job = native?.jobs.find(j => j.id === jobId);
const active = native?.jobs.find(j => j.state === 'running');
const selection = checked?.preview.candidate_selection;
const usable = !!checked && checked.preview.file_set_status === 'plausible' && !!selection;
const version = selection ? nativeCatalogModels(native?.catalog?.families ?? []).flatMap(f => f.sizes).find(s => s.sourceFamily === selection.family && s.model === selection.model) : undefined;
const family = selection ? nativeCatalogModels(native?.catalog?.families ?? []).find(f => f.sizes.some(s => s.sourceFamily === selection.family && s.model === selection.model)) : undefined;
async function check(path: string) {
setPending(true);
setError('');
setChecked(null);
try {
const result = await api.post<Preview & { cancelled?: boolean }>('/api/native/local/preview', { path });
if (!result.cancelled) {
setChecked(result);
setTyped(result.path);
}
}
catch (e) { setError(e instanceof Error ? e.message : String(e)); }
finally { setPending(false); }
}
async function prepare() {
if (!usable || !selection || !checked)
return;
setPending(true);
setError('');
try {
const result = await nativeAction<NativeJob>('operations', { kind: 'install', args: { family: selection.family, model: selection.model, gguf_dir: checked.path, context: native?.catalog?.recommendation?.context ?? 32768, vision: 'no', kv: 'int8', loadAfter: true } });
setJobId(result.id);
}
catch (e) { setError(e instanceof Error ? e.message : String(e)); }
finally { setPending(false); }
}
const found = checked ? problem(checked.preview.issues) : null;
return <Screen plainHeader title={tr("Model")} actions={<Btn onClick={onBack}>{tr("Strata optimized models")}</Btn>}>
<div className="native-model-layout">
<div className="stack stack--lg">
<section className="st-card native-choice"><h2 className="h3">{tr("Use a local GGUF…")}</h2>
<p className="small muted">{tr("Already have a supported model on this computer? Choose the folder that holds its files. Strata checks them, prepares the model and loads it. Nothing is copied or downloaded.")}</p>
<label className="input-field"><span>{tr("Folder with the GGUF files")}</span><input className="st-input" value={typed} placeholder={tr("Full path to the folder")} spellCheck={false} onChange={e => setTyped(e.target.value)} onKeyDown={e => { if (e.key === 'Enter' && typed.trim()) void check(typed); }}/></label>
<div className="native-settings-actions"><Btn icon="folder" disabled={pending || !!active} onClick={() => void check('')}>{tr("Choose folder")}</Btn><Btn disabled={pending || !typed.trim() || !!active} onClick={() => void check(typed)}>{tr(pending && !jobId ? 'Checking…' : 'Check files')}</Btn></div>
</section>
{tr(checked ? <section className="st-card native-choice">{tr(usable ? <>
<h2 className="h3">{tr(family ? family.title : 'Model found')}</h2>
<p><strong>{tr(version ? catalogVersionLabel(version) : selection?.model ?? '')}</strong> · {tr(selection?.model ?? '')}</p>
<p className="small muted">{tr("{0} files · {1}", [checked.preview.file_count, sizeGb(checked.preview.total_bytes / 1024 ** 3)])}</p>
<div className="native-model-actions"><Btn variant="primary" icon="models" disabled={pending || !!active} onClick={() => void prepare()}>{tr(pending ? 'Checking…' : 'Prepare and load')}</Btn></div>
</> : <>
<h2 className="h3">{tr(found!.message)}</h2>
<p className="small muted">{tr(found!.next)}</p>
{tr(checked.preview.issues.length ? <details><summary className="small">{tr("Details")}</summary><pre className="small muted error-detail-text">{checked.preview.issues.join(String.fromCharCode(10))}</pre></details> : null)}
</>)}</section> : null)}
{tr(active ? <NativeProgress job={active}/> : job?.state === 'error' ? <ErrorNotice message={job.error ?? job.summary} detail={job.details}/> : job?.state === 'done' ? <p className="small" role="status"><Icon name="check" size={15}/> {tr(job.summary)}</p> : null)}
{tr(error ? <ErrorNotice message={error}/> : null)}
</div>
<aside className="stack stack--lg"><section className="st-card"><h2 className="h4">{tr("This computer")}</h2><p>{tr(native?.hardware?.ram_gb || '—')}{tr(" GB RAM")}</p>{tr(native?.hardware?.gpus.length ? native.hardware.gpus.map(g => <p className="small" key={g.index}>{tr(g.name)} · {tr(g.vram_gb)}{tr(" GB")}</p>) : null)}</section></aside>
</div>
</Screen>;
}