import type { AiConnectionKind, AiConnectionView } from '@trek/shared'; import { Loader2, LogOut, MessageCircle, Sparkles, Unplug } from 'lucide-react'; import React from 'react'; import { useTranslation } from '../../../i18n'; import { useAssistantCostEstimates, useAssistantMapFollow } from '../../Assistant/useAssistantMapFollow'; import DemoLock from '../../Demo/DemoLock'; import { useDemoAccountLock } from '../../Demo/useDemoLock'; import ConfirmDialog from '../../shared/ConfirmDialog'; import CustomSelect from '../../shared/CustomSelect'; import { fs } from '../../shared/DialogShell'; import Section from '../Section'; import { SettingRow, SettingRows, SETTINGS_BUTTON, SETTINGS_BUTTON_DANGER, SETTINGS_BUTTON_PRIMARY, SettingsHint, StatusPill, } from '../settingsKit'; import ToggleSwitch from '../ToggleSwitch'; import { aiSectionText, CONNECTION_STATUS } from './aiConnectModel'; import AiConnectPanel, { ProviderMark } from './AiConnectPanel'; import { useAiConnections } from './useAiConnections'; type AiConnections = ReturnType; /** What a connection that stopped working says before its one way back. */ const BROKEN: Partial> = { needs_reauth: { messageKey: 'assistant.error.needsReauth' }, error: { messageKey: 'assistant.error.invalidKey' }, }; /** A connection as one row: the provider's tile, who it is and the detail under it, the actions on the right. */ function ConnectionRow({ kind, title, detail, children, }: { kind: AiConnectionKind; title: string; detail?: React.ReactNode; children?: React.ReactNode; }) { return (

{title}

{detail && (

{detail}

)}
{children &&
{children}
}
); } function OpenAssistantButton({ S }: { S: AiConnections }) { const { t } = useTranslation(); if (!S.canOpen) return null; return ( ); } /** The user's own connection: who answers, which model, and the way out (or back in, once it broke). */ function OwnConnection({ S, own }: { S: AiConnections; own: AiConnectionView }) { const { t } = useTranslation(); const { provider, models, busy, setModel, signOut, askDisconnect } = S.ownActions; const broken = BROKEN[own.status]; const repairMode = S.repairMode; return ( {repairMode ? ( ) : ( )} {own.kind === 'chatgpt' && own.status === 'ok' ? ( ) : ( )} {broken && (

{t(broken.messageKey, { provider })}

)} {own.status === 'ok' && ( void setModel(String(v))} options={models} size="sm" disabled={busy} searchable={models.length > 8} /> } /> )}
); } /** The admin's connection this user runs on, with today's share of the limit while the instance pays. */ function InstanceConnection({ S }: { S: AiConnections }) { const { t } = useTranslation(); const instance = S.status?.instance; if (!instance) return null; return ( {S.status?.canConnectOwn && !S.panel && ( )} {S.quota && (

{t('assistant.connect.quota', { used: S.quota.used, count: S.quota.limit })}

)}
); } /** How the assistant behaves once it runs: whether the trip map follows it. */ function AssistantBehaviour() { const { t } = useTranslation(); const follow = useAssistantMapFollow(); const costs = useAssistantCostEstimates(); return ( void follow.set(!follow.on)} label={t('assistant.settings.followMap')} /> } /> void costs.set(!costs.on)} label={t('assistant.settings.costEstimates')} /> } /> ); } /** Where the shared demo account would connect a provider of its own: shown, and locked. */ function LockedOwnConnection() { const { t } = useTranslation(); return ( {t('assistant.errorAction.connectOwn')} } /> ); } /** The connect panel, opened on purpose or because nothing is connected yet. */ function ConnectPanel({ S }: { S: AiConnections }) { const { t } = useTranslation(); return (
{S.view !== 'setup' && ( )}
); } /** How long chats are kept, and for an admin the way to the setting for everyone. */ function SectionFooter({ S }: { S: AiConnections }) { const { t } = useTranslation(); const retention = S.view !== 'setup' && !!S.retentionText; if (!S.isAdmin && !retention) return null; return (
{S.view !== 'setup' && {S.retentionText}} {S.isAdmin && ( )}
); } /** * Settings → Integrations → the user's AI connection: connect a provider, see * which one answers, pick its model, leave again. The assistant runs on it and * AI parsing reads bookings with it, so it shows while either addon is on and * says which it serves. */ export default function AiConnectionSection({ assistant = true, parsing = false, }: { assistant?: boolean; parsing?: boolean; }): React.ReactElement | null { const { t } = useTranslation(); const S = useAiConnections(); const demoLocked = useDemoAccountLock(); const text = aiSectionText(assistant, parsing); if (S.view === 'hidden') return null; const own = S.own; return (
{t(CONNECTION_STATUS[own.status].labelKey)} ) : undefined } > {S.view === 'loading' && (
)} {S.view === 'own' && own && } {S.view === 'instance' && } {assistant && (S.view === 'own' || S.view === 'instance') && } {S.view === 'adminOnly' && !demoLocked && {t('assistant.setup.adminOnly')}} {demoLocked && S.view !== 'loading' && } {S.panel && } void S.ownActions.disconnect()} title={t('assistant.connect.disconnect')} message={t('assistant.connect.disconnectConfirm', { provider: S.ownActions.provider })} confirmLabel={t('assistant.connect.disconnect')} danger />
); }