'use client' import { useId, useState } from 'react' import { useTranslation } from 'react-i18next' import ProviderIcon from '@/components/common/ProviderIcon' import { formatProviderLabel } from '@/lib/provider-branding' import type { ApiFormat, ServiceName } from '@/lib/model-catalog-types' import { SERVICE_TITLES } from '@/lib/provider-registry' import { inputClass, selectClass, subPanelClass } from './shared' import { registryButton, registryPrimary } from './RegistryControls' export type ProviderCatalogOption = { value: string; label: string; services: ServiceName[] } export type CustomProviderInput = { name: string; base_url: string } const FILTERS = [ 'all', 'multi', 'llm', 'embedding', 'voice', 'search', 'imagegen', 'videogen', 'custom', ] as const const PROTOCOLS = { openai_chat: 'OpenAI Chat Completions', openai_responses: 'OpenAI Responses', anthropic: 'Anthropic Messages', } as const export function ProviderProtocol({ value, onChange, formats, }: { value: ApiFormat onChange: (value: ApiFormat) => void formats?: readonly string[] }) { const { t } = useTranslation() return ( {t('settings.providerServices.protocol')} onChange(e.target.value as ApiFormat)} > {(!formats || formats.includes('auto')) && {t('Auto')}} {Object.entries(PROTOCOLS).filter(([key]) => !formats || formats.includes(key)).map(([key, label]) => ( {t(label)} ))} ) } export function AddProviderPanel({ options, vendor, onVendor, onCreate, onCancel, }: { options: ProviderCatalogOption[] vendor: string onVendor: (value: string) => void onCreate: (custom?: CustomProviderInput) => void onCancel: () => void }) { const { t, i18n } = useTranslation() const uiLanguage = i18n?.resolvedLanguage ?? i18n?.language ?? 'en' const radioName = useId() const [filter, setFilter] = useState('all') const [query, setQuery] = useState('') const [name, setName] = useState('') const [url, setUrl] = useState('') const custom = vendor === 'custom' const visible = options.map(option => ({ ...option, label: formatProviderLabel(option.value, option.label, uiLanguage), })).filter(option => { const category = filter === 'all' || (filter === 'custom' ? option.value === 'custom' : option.value !== 'custom' && (filter === 'multi' ? option.services.length > 1 : filter === 'voice' ? option.services.some(s => s === 'tts' || s === 'stt') : option.services.includes(filter as ServiceName))) return ( category && `${option.label} ${option.value}`.toLowerCase().includes(query.trim().toLowerCase()) ) }) let validUrl = false try { const parsed = new URL(url) validUrl = ['https:', 'http:'].includes(parsed.protocol) && !parsed.username && !parsed.password && Boolean(parsed.hostname) } catch {} return ( {t('Add provider')} setQuery(e.target.value)} /> {FILTERS.map(item => ( { setFilter(item) onVendor('') }} className={`rounded-full border px-3 py-1.5 text-[13px] ${filter === item ? 'border-[var(--foreground)] bg-[var(--foreground)] text-[var(--background)]' : 'border-[var(--border)] hover:bg-[var(--accent)]'}`} > {t(`settings.providerServices.filter.${item}`)} ))} {visible.map(option => ( {/* Anchor the hidden radio to its card so focus cannot scroll the settings shell. */} onVendor(option.value)} /> {option.value === 'custom' ? t('Custom') : option.label} {option.value === 'custom' ? ( {t('settings.providerServices.customHint')} ) : ( option.services.map(service => ( {t(SERVICE_TITLES[service])} )) )} ))} {!visible.length && ( {t('settings.providerServices.noMatch')} )} {custom && ( {t('settings.providerServices.name')} setName(e.target.value)} /> {t('Provider URL')} setUrl(e.target.value)} /> {url && !validUrl && ( {t('Enter a valid HTTP or HTTPS provider address.')} )} {t('settings.providerServices.customDescription')} )} {t('settings.providerServices.addHint')} p.value === vendor) || (custom && (!name.trim() || !validUrl)) } onClick={() => onCreate( custom ? { name: name.trim(), base_url: url.trim() } : undefined ) } > {t('Continue')} {t('Cancel')} ) }
{t('settings.providerServices.noMatch')}
{t('Enter a valid HTTP or HTTPS provider address.')}
{t('settings.providerServices.customDescription')}
{t('settings.providerServices.addHint')}