import { Check, Loader2, Sparkles } from 'lucide-react'; import React, { useId, useState } from 'react'; import { useTranslation } from '../../../i18n'; import ToggleSwitch from '../../Settings/ToggleSwitch'; import AiConnectPanel, { ProviderMark } from '../../Settings/ai/AiConnectPanel'; import { CONNECTION_STATUS } from '../../Settings/ai/aiConnectModel'; import { SETTINGS_BUTTON, SETTINGS_BUTTON_DANGER, SettingsHint, StatusPill } from '../../Settings/settingsKit'; import ConfirmDialog from '../../shared/ConfirmDialog'; import CustomSelect from '../../shared/CustomSelect'; import { DialogButton, DialogFooter, DialogHeader, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs, } from '../../shared/DialogShell'; import { NumericInput } from '../../shared/NumericInput'; import { EditorField, INPUT, Segmented } from '../../shared/dialogParts'; import ParsingSettings from './ParsingSettings'; import type { Addon } from './addonModel'; import { POLICY_CHOICES, useAiAdminConfig } from './useAiAdminConfig'; type AiAdmin = ReturnType; const EYEBROW = 'font-geist font-bold uppercase tracking-[.08em] text-content-faint'; /** Who pays, as three rows to pick one from: the labels are too long for a segmented track in a shelf. */ function PolicyChoice({ A }: { A: AiAdmin }) { const { t } = useTranslation(); return (
{t('assistant.admin.policy')}
{POLICY_CHOICES.map((choice) => { const on = A.policy === choice.value; return ( ); })}
{A.policyHintKey && {t(A.policyHintKey)}}
); } /** The connection everyone runs on, once set up: who answers, its model, and replacing or removing it. */ function LiveConnection({ A }: { A: AiAdmin }) { const { t } = useTranslation(); const { live, provider, models, busy, setModel, replace, askRemove } = A.instance; if (!live) return null; const status = CONNECTION_STATUS[live.status]; return (

{provider}

{live.hint ?? live.host}

{t(status.labelKey)}
{live.status === 'ok' && ( setModel(String(v))} options={models} size="sm" disabled={busy} searchable={models.length > 8} /> )}
); } function InstanceConnection({ A }: { A: AiAdmin }) { const { t } = useTranslation(); const { live, connection, replacing, cancelReplace, onConnected } = A.instance; return (
{t('assistant.admin.connection')} {!replacing && } {!live && {t('assistant.admin.connectionNone')}} {A.showPanel && ( )} {replacing && ( )}
); } /** The assistant's own limits: turns a day while the instance pays, how long chats are kept, whether it searches the web. */ function AssistantLimits({ A }: { A: AiAdmin }) { const { t } = useTranslation(); const fieldId = useId(); return (
{A.instancePays && ( { if (e.key === 'Enter') e.currentTarget.blur(); }} /> )} A.setRetention(String(v))} options={A.retentionOptions} size="sm" /> {A.settings && (
{t('assistant.admin.webSearch')} A.setWebSearch(!A.settings?.webSearch)} label={t('assistant.admin.webSearch')} />
{t('assistant.admin.webSearchHint')}
)}
); } type AiTab = 'connection' | 'assistant' | 'parsing'; /** * The AI settings behind the AI tile's button, in one dialog so the addon page * stays short: the connection the assistant and AI parsing share (and who pays * for it), the assistant's limits, and how bookings are read. A tab shows only * while its addon is on. Each change saves at once. */ export default function AiSettingsDialog({ addons, onClose, }: { addons: Addon[]; onClose: () => void; }): React.ReactElement { const { t } = useTranslation(); const labelId = useId(); const A = useAiAdminConfig(); const assistantOn = addons.some((a) => a.id === 'ai_assistant' && a.enabled); const parsing = addons.find((a) => a.id === 'llm_parsing' && a.enabled) ?? null; const tabs: { value: AiTab; label: string }[] = [ { value: 'connection', label: t('admin.addons.ai.tab.connection') }, ...(assistantOn ? [{ value: 'assistant' as const, label: t('admin.addons.catalog.ai_assistant.name') }] : []), ...(parsing ? [{ value: 'parsing' as const, label: t('admin.addons.catalog.llm_parsing.name') }] : []), ]; const [tab, setTab] = useState('connection'); const shown = tabs.some((x) => x.value === tab) ? tab : 'connection'; return ( } tint={NEUTRAL_TINT} labelId={labelId} eyebrow={t('admin.addons.ai.name')} title={t('admin.addons.ai.setup')} sub={t('admin.addons.ai.dialogSub')} subWraps onClose={onClose} /> } footer={ {t('common.close')} } > {tabs.length > 1 && ( label={t('admin.addons.ai.setup')} value={shown} onChange={setTab} options={tabs} fill /> )} {!A.loaded ? (
) : ( <> {shown === 'connection' && (
{A.instancePays && }
)} {shown === 'assistant' && } {shown === 'parsing' && parsing && ( setTab('connection')} /> )} )}
); }