import {localeTag} from '../lib/i18n'; import { tr } from '../lib/i18n'; import { useEffect, useState } from 'react'; import { Btn, Dialog, ErrorNotice, Icon } from './ui'; import { api } from '../lib/client'; import { addLocalModel } from '../lib/store'; import { sizeGb } from '../lib/format'; import type { LocalModelFolder, LocalModelPreview } from '../lib/types'; const fileSize = (bytes: number) => bytes < 1024 ** 2 ? `${Math.max(1, Math.round(bytes / 1024))} KB` : bytes < 1024 ** 3 ? `${Math.round(bytes / 1024 ** 2)} MB` : sizeGb(bytes / 1024 ** 3); export function LocalModelImport({ open, directory, onClose, onChoose }: { open: boolean; directory: string; onClose: () => void; onChoose: (id: string) => void; }) { const [folder, setFolder] = useState(null), [path, setPath] = useState(''), [preview, setPreview] = useState(null); const [name, setName] = useState(''), [pending, setPending] = useState(false), [error, setError] = useState(''); useEffect(() => { if (open) { setPreview(null); setError(''); void browse(directory); } }, [open]); async function browse(input: string) { setPending(true); setError(''); try { const next = await api.get(`/api/local-models/browse?directory=${encodeURIComponent(input)}`); setFolder(next); setPath(next.directory); setPreview(null); } catch (e) { setPath(input); setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } async function inspect(file: string) { setPending(true); setError(''); try { const next = await api.post('/api/local-models/inspect', { path: file }); setPreview(next); setName(next.variant.name); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } async function useModel() { if (!preview) return; setPending(true); setError(''); try { const id = await addLocalModel(preview.variant.local!.path, name); onChoose(id); onClose(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } const close = () => { if (!pending) onClose(); }; return { setPreview(null); setError(''); } : close}>{tr(preview ? 'Choose another file' : 'Cancel')}{tr(preview ? void useModel()}>{tr(pending ? 'Adding…' : 'Use this model')} : null)}}> {preview ?
{preview.variant.name}{tr("Local GGUF \u00B7 ")}{tr(preview.variant.local!.shards === 1 ? 'One file' : `${preview.variant.local!.shards} files found`)}
{tr("Architecture")}
{tr(preview.variant.local!.architecture)}
{tr("Quantization")}
{tr(preview.variant.quant)}
{tr("Size")}
{tr(fileSize(preview.variant.local!.files.reduce((n, f) => n + f.bytes, 0)))}
{tr("Context limit")}
{tr(Math.floor(preview.variant.local!.contextLength / 1024).toLocaleString(localeTag()))}{tr("K tokens")}
{tr(preview.variant.local!.shards === 1 ? 'File location' : 'Model files')}{preview.variant.local!.path}{tr(preview.files.length > 1 ?
    {tr(preview.files.map(file =>
  • {tr(file)}
  • ))}
: null)}

{tr("Strata uses these files in place. You can load the model after adding it.")}

{tr("Custom model \u00B7 engine compatibility is checked when loading. Memory estimates use the file\u2019s metadata.")}

{tr(!preview.variant.local!.hasChatTemplate ?

{tr("No chat template is declared; response formatting depends on the engine.")}

: null)}
:

{tr("Choose the model’s .gguf file. For a split model, choose any shard; Strata finds the first and checks the full set.")}

{ e.preventDefault(); if (!pending) void (/\.gguf$/i.test(path.trim()) ? inspect(path) : browse(path)); }}> setPath(e.target.value)}/>{tr("Go")}
{folder?.shortcuts.map(shortcut => void browse(shortcut.path)}>{tr(shortcut.name)})}
folder?.parent && void browse(folder.parent)}>{tr("Up a folder")}
{pending ?

{tr("Reading files\u2026")}

: folder?.entries.length ? folder.entries.map(entry => ) :
{tr("No GGUF files here")}

{tr("Open another folder or enter a model\u2019s full path above.")}

}
{tr(folder?.truncated ?

{tr("Showing the first 1,000 items. Enter a path to open a specific file.")}

: null)}

{tr("Only folders and GGUF files are shown. LoRA adapters and image projectors need separate support.")}

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