import { asLlmVision, LLM_VISION_MODES, type LlmVision } from '@trek/shared'; import { Check, RefreshCw } from 'lucide-react'; import React, { useId } from 'react'; import { useTranslation } from '../../../i18n'; import { ProviderMark } from '../../Settings/ai/AiConnectPanel'; import { SETTINGS_BUTTON, SETTINGS_BUTTON_PRIMARY, SettingsHint, StatusPill } from '../../Settings/settingsKit'; import { DialogSection, fs } from '../../shared/DialogShell'; import { EditorField, INPUT, PANEL, Segmented } from '../../shared/dialogParts'; import { type Addon, ANTHROPIC_HINT, MASKED, RECOMMENDED_MODELS } from './addonModel'; import type { useAiAdminConfig } from './useAiAdminConfig'; import { useLlmParsingConfig } from './useLlmParsingConfig'; type AiAdmin = ReturnType; type Parsing = ReturnType; type MarkKind = Parameters[0]['kind']; /** Parsing's own providers, with the mark the assistant shows for each. */ const OWN_PROVIDERS = [ { value: 'local', labelKey: 'settings.aiParsing.providerLocal', mark: 'custom' }, { value: 'openai', labelKey: 'settings.aiParsing.providerOpenai', mark: 'openai' }, { value: 'anthropic', labelKey: 'settings.aiParsing.providerAnthropic', mark: 'anthropic' }, ] as const; const markOf = (provider: string): MarkKind => OWN_PROVIDERS.find((p) => p.value === provider)?.mark ?? 'custom'; /** The connection parsing reads with, as one line: a mark, who answers, the model and where it comes from. */ function ReadsWith({ mark, title, model, tag }: { mark: MarkKind; title: string; model: string; tag: string }) { return (

{title}

{model}

{tag}
); } /** Which connection parsing reads with now, and the way to the other one. */ function ParsingSource({ A, P, onOpenConnection }: { A: AiAdmin; P: Parsing; onOpenConnection: () => void }) { const { t } = useTranslation(); const live = A.instancePays ? A.instance.live : null; if (P.ownConnection) { const own = OWN_PROVIDERS.find((p) => p.value === P.savedProvider); return (
{t('admin.addons.ai.parsing.ownHint')}
); } return (
{live ? ( ) : ( {t(A.policy === 'user' ? 'admin.addons.ai.parsing.perUser' : 'admin.addons.ai.parsing.none')} )}
{!live && A.policy !== 'user' && ( )} {!P.formOpen && ( )}
); } /** The models on the local server to pick from, and the recommended ones to pull. */ function LocalModels({ P }: { P: Parsing }) { const { t } = useTranslation(); return (
{t('admin.addons.ai.parsing.installed')}
{P.modelsErr && (

{P.modelsErr}

)} {!P.modelsErr && P.installed.length === 0 && !P.loadingModels && ( {t('admin.addons.ai.parsing.noModels')} )} {P.installed.length > 0 && (
{P.installed.map((name) => ( ))}
)}
{t('admin.addons.ai.parsing.pullTitle')} {RECOMMENDED_MODELS.map((m) => { const here = P.isInstalled(m.id); const chosen = P.model === m.id; return (
{m.label} {m.recommended && {t('assistant.model.recommended')}} {here ? ( ) : ( )}
{P.pulling === m.id && (
{P.pullStatus} {P.pullPct ? ` ${P.pullPct}%` : ''}
)}
); })}
); } /** A connection of parsing's own: the provider as tiles, its address, key and model. */ function OwnConnectionForm({ P }: { P: Parsing }) { const { t } = useTranslation(); const fieldId = useId(); return (
{OWN_PROVIDERS.map((p) => { const on = P.provider === p.value; return ( ); })}
P.setBaseUrl(e.target.value)} onBlur={() => void P.loadModels()} placeholder={P.baseUrlPlaceholder} /> P.setApiKey(e.target.value)} placeholder={ P.apiKey === MASKED ? MASKED : P.provider === 'local' ? t('admin.addons.ai.parsing.apiKeyOptional') : '' } /> P.setModel(e.target.value)} /> {P.provider === 'local' && }
{!P.ownConnection && ( )}
); } /** * AI parsing in the AI dialog: which connection reads the bookings (the shared * one, or one of its own an older install kept), a connection of its own when * wanted, and whether the model reads images. Each part saves on its own. */ export default function ParsingSettings({ addon, A, onOpenConnection, }: { addon: Addon; A: AiAdmin; onOpenConnection: () => void; }): React.ReactElement { const { t } = useTranslation(); const P = useLlmParsingConfig(addon); const visionOptions = LLM_VISION_MODES.map((value) => ({ value, label: t(`admin.addons.llm.vision.${value}`) })); const local = (P.ownConnection ? P.savedProvider : A.instance.live?.kind === 'custom' ? 'local' : 'cloud') === 'local'; return (
{P.formOpen && ( )} label={t('settings.aiParsing.multimodal')} value={P.vision} onChange={(v) => P.setVision(asLlmVision(v))} options={visionOptions} fill />
{t(local ? 'admin.addons.llm.vision.hintLocal' : 'admin.addons.llm.vision.hintCloud')}
); }