import { tr } from '../lib/i18n'; import { modelFamilyIcon } from '../lib/model-icons'; import { ManagedModelLibrary } from '../components/ManagedModelLibrary'; import { useState } from 'react'; import { ActionError, Bar, Btn, Dialog, ErrorNotice, Icon, Select } from '../components/ui'; import { Screen } from '../components/layout'; import { VersionLadder } from '../components/VersionLadder'; import { LifecycleNotice } from '../components/LifecycleNotice'; import { modelFamilies, modelVersions, recommendedVersion, versionFit, VERSION_FIT_LABEL } from '../lib/model-versions'; import { contextTokens, estimate, execution } from '../lib/experience'; import { planMemory } from '../lib/run-memory'; import { useMedia } from '../lib/responsive'; import { formatModelName, sizeGb } from '../lib/format'; import { cancelInstall, go, installVariant, loadVariant, removeVariant, unloadModel, useStore, useLifecycle } from '../lib/store'; export function ModelsScreen({ embedded = false, ...props }: { embedded?: boolean; contextOverride?: number; familyOverride?: string; selectedVariant?: string; onChoose?: (id: string) => void; } = {}) { return embedded ? : ; } function ModelLibraryScreen({ embedded = false, contextOverride, onChoose }: { embedded?: boolean; contextOverride?: number; onChoose?: (id: string) => void; } = {}) { const inlineDetails = useMedia('(max-width: 850px)'); const library = useStore(s => s.library), hardware = useStore(s => s.hardware), engine = useStore(s => s.engine); const lifecycle = useLifecycle(); const [selected, setSelected] = useState(''), [selectedFamily, setSelectedFamily] = useState(''), [confirm, setConfirm] = useState<'switch' | 'delete' | null>(null), [pending, setPending] = useState(false), [error, setError] = useState(''); if (!library) return

{tr("Loading model versions\u2026")}

; const running = library.variants.find(v => v.id === library.loaded), families = modelFamilies(library), family = families.includes(selectedFamily) ? selectedFamily : running?.family ?? families[0]; const context = contextOverride ?? contextTokens(engine?.beginner.contextLength ?? 'recommended'), versions = modelVersions(library, family), recommended = recommendedVersion(versions, hardware, context); const version = versions.find(v => v.id === selected) ?? versions.find(v => v.id === running?.id) ?? recommended ?? versions[0]; if (!version) return

{tr("No model versions are available.")}

; function planningLabel(v: typeof version) { const preview = planMemory(estimate(v, hardware, execution(undefined, context)), hardware, 'shared'); return context > v.contextMax ? 'Context too long' : !preview ? 'Fit unknown' : !preview.cacheFits ? 'Context needs more memory' : preview.ssd > .05 ? 'SSD spill likely' : 'Fits in memory'; } const fit = versionFit(version, hardware, context), loaded = version.id === library.loaded, installed = ['loaded', 'installed'].includes(version.state), working = ['loading', 'downloading', 'unloading'].includes(version.state); async function act(action: () => Promise) { setPending(true); setError(''); try { const ok = await action(); if (ok === false) throw new Error('The operation could not finish. Check the engine connection and try again.'); setConfirm(null); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } const versionDetails =
{tr(version.quant)}{tr(loaded ? 'Running' : installed ? 'Downloaded' : embedded ? planningLabel(version) : VERSION_FIT_LABEL[fit.zone])}

{tr(version.name)}

{tr(formatModelName(version))}

{tr(sizeGb(version.diskGb))}{tr("Download size")}
{tr(sizeGb(fit.needed))}{tr("Estimated memory")}

{tr("Select this version to review context, memory and performance.")}

{tr(onChoose && installed ?
onChoose(version.id)}>{tr("Use this version")} setConfirm('delete')}>{tr("Delete")}{tr(loaded ? void act(unloadModel)}>{tr("Unload model")} : null)}
: working ? <>

{tr(version.progress?.stage ?? 'Working…')}

{tr(version.state === 'downloading' ? void act(() => cancelInstall(version.id))}>{tr("Cancel download")} : null)} : loaded ? void act(unloadModel)}>{tr("Unload model")} : installed ?
setConfirm('switch')}>{tr("Switch to ")}{tr(version.name)} setConfirm('delete')}>{tr("Delete")}
: library.disk.freeGb} onClick={() => void act(() => installVariant(version.id))}>{tr(version.downloadGb > library.disk.freeGb ? 'Not enough disk space' : !embedded && fit.zone === 'too-big' ? 'Too big for this PC' : `Download · ${sizeGb(version.downloadGb)}`)})} {tr(version.error || error ? : null)}
; const content = <>