import { tr } from '../lib/i18n'; import { modelFamilyIcon } from '../lib/model-icons'; import { useId, useRef, useState } from 'react'; import { FloatingMenu } from './FloatingMenu'; import { Bar, Btn, Dialog, ErrorNotice, Icon } from './ui'; import { go, useLifecycle, loadVariant, selectRemoteModel, setSettingsSection, useStore } from '../lib/store'; import { pickerGroups, pickerNativeModels, pickerVersionState } from '../lib/model-picker'; import { versionFit } from '../lib/model-versions'; import type { ModelVariant } from '../lib/types'; import { sizeGb } from '../lib/format'; import { contextTokens } from '../lib/experience'; import { nativeAction, useNative } from '../lib/native'; /** The loaded model is shared by all chats; switching uses the existing lifecycle gates. */ export function ChatModelPicker() { const native = useNative(), remote = useStore(s => s.remoteModel); return native?.instances.length && !remote ? : ; } function NativeChatModelPicker() { const native = useNative(), providers = useStore(s => s.providers), streaming = useStore(s => s.streaming); const [open, setOpen] = useState(false), [error, setError] = useState(''); const ref = useRef(null); const current = native?.instances.find(i => i.id === native.defaultId); return <>{tr(open ? setOpen(false)}>{tr(native?.instances.filter(i => i.status !== 'error').map(i => ))}{tr(providers?.connections.flatMap(c => c.models.map(m => )))}{tr(error ?

{tr(error)}

: null)}
: null)}; } function LegacyChatModelPicker() { const native = useNative(); const library = useStore(s => s.library), hardware = useStore(s => s.hardware), engine = useStore(s => s.engine); const providers = useStore(s => s.providers), remote = useStore(s => s.remoteModel), streaming = useStore(s => s.streaming); const connection = providers?.connections.find(c => c.id === remote?.connectionId); const apiModel = connection?.models.find(m => m.id === remote?.modelId); const lifecycle = useLifecycle(); const [open, setOpen] = useState(false), [selected, setSelected] = useState(null); const [pending, setPending] = useState(false), [error, setError] = useState(''); const [expanded, setExpanded] = useState([]); const groupsId = useId(); const trigger = useRef(null); const running = library?.variants.find(v => v.id === library.loaded); const version = library?.variants.find(v => v.id === selected); const groups = library && library.mode !== 'real' ? pickerGroups(library) : []; // real engine: its own models (installedNative) only const installedNative = pickerNativeModels(native?.installed?.models, native?.instances); const status = version ? pickerVersionState(version, library?.loaded ?? null) : null; const tooBig = !!version && library?.mode === 'real' && versionFit(version, hardware, contextTokens(engine?.beginner.contextLength ?? '32K')).zone === 'too-big'; async function apply() { if (!version) return; setPending(true); setError(''); try { const success = await loadVariant(version.id); if (!success) throw new Error('The version could not load. Try again when the engine is ready.'); selectRemoteModel(null); setSelected(null); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } function chooseVersion(v: ModelVariant) { setOpen(false); if (pickerVersionState(v, library?.loaded ?? null).running) selectRemoteModel(null); else { setSelected(v.id); setError(''); } } function versionOption(v: ModelVariant) { const state = pickerVersionState(v, library?.loaded ?? null); return ; } return <> {tr(open ? setOpen(false)}> {tr(native?.instances.filter(i => i.status !== 'error').map(i => ))}

{tr("Local models")}

{tr(installedNative.map(m => ))} {tr(!groups.length && !installedNative.length ?

{tr("No local models downloaded yet.")}

: null)} {tr(groups.map(({ family, versions }, index) => { const main = versions[0], state = pickerVersionState(main, library!.loaded); const others = versions.slice(1), isExpanded = expanded.includes(family), id = `${groupsId}-${index}`; return
{tr(others.length ? : null)}
{tr(isExpanded && others.length ?
{tr(others.map(versionOption))}
: null)}
; }))} {tr(providers?.connections.map(c =>

{c.name}

{tr(c.models.map(m => ))}
))}
: null)} !pending && setSelected(null)} actions={<> setSelected(null)}>{tr("Cancel")} void apply()}>{tr(pending ? 'Working…' : status?.busy ? status.label : status?.action)}}> {tr(version ?

{tr(version.family)}
{tr(version.name)} · {tr(version.quant)} · {tr(sizeGb(version.diskGb))}

{tr("This switches the model version for all chats. Your conversations stay saved.")}

{tr(status?.busy ? : null)} {tr(tooBig ?

{tr("This version needs more memory at the current context. Choose a smaller version or reduce context in Engine.")}

: null)} {tr(!lifecycle.allowed ?

{tr(lifecycle.reason)}

: null)} {tr(error || version.error ? : null)}
: null)}
; }