import React, { useCallback, useEffect, useState } from "react"; import { Check, ChevronDown, ChevronRight, Eye, EyeOff } from "lucide-react"; import type { ProviderConfig, ProviderAdvancedParams } from "../../../../infrastructure/ai/types"; import { PROVIDER_PRESETS } from "../../../../infrastructure/ai/types"; import { encryptField, decryptField } from "../../../../infrastructure/persistence/secureFieldAdapter"; import { useI18n } from "../../../../application/i18n/I18nProvider"; import { Button } from "../../../ui/button"; import type { ProviderFormState } from "./types"; import { ModelSelector } from "./ModelSelector"; export const ProviderConfigForm: React.FC<{ provider: ProviderConfig; onSave: (updates: Partial) => void; onCancel: () => void; }> = ({ provider, onSave, onCancel }) => { const { t } = useI18n(); const [form, setForm] = useState({ name: provider.name ?? PROVIDER_PRESETS[provider.providerId]?.name ?? "", apiKey: "", baseURL: provider.baseURL ?? PROVIDER_PRESETS[provider.providerId]?.defaultBaseURL ?? "", defaultModel: provider.defaultModel ?? "", skipTLSVerify: provider.skipTLSVerify ?? false, advancedParams: provider.advancedParams ?? {}, }); const isCustom = provider.providerId === "custom"; const [showApiKey, setShowApiKey] = useState(false); const [isDecrypting, setIsDecrypting] = useState(false); const [showAdvanced, setShowAdvanced] = useState(false); const preset = PROVIDER_PRESETS[provider.providerId]; // Decrypt and load existing API key on mount useEffect(() => { if (provider.apiKey) { setIsDecrypting(true); decryptField(provider.apiKey) .then((decrypted) => { setForm((prev) => ({ ...prev, apiKey: decrypted ?? "" })); }) .catch(() => { // If decryption fails, show raw value setForm((prev) => ({ ...prev, apiKey: provider.apiKey ?? "" })); }) .finally(() => setIsDecrypting(false)); } }, [provider.apiKey]); const [advancedParamRaw, setAdvancedParamRaw] = useState>({}); const handleAdvancedParam = useCallback((key: keyof ProviderAdvancedParams, raw: string) => { setAdvancedParamRaw((prev) => ({ ...prev, [key]: raw })); setForm((prev) => { const next = { ...prev.advancedParams }; if (raw.trim() === "" || raw.trim() === "-") { delete next[key]; } else { const num = Number(raw); if (!Number.isNaN(num)) { next[key] = num; } } return { ...prev, advancedParams: next }; }); }, []); const handleSave = useCallback(async () => { const cleanedParams: ProviderAdvancedParams = {}; const ap = form.advancedParams; if (ap.maxTokens != null && Number.isFinite(ap.maxTokens) && ap.maxTokens > 0) cleanedParams.maxTokens = Math.max(1, Math.round(ap.maxTokens)); if (ap.temperature != null) cleanedParams.temperature = Math.min(2, Math.max(0, ap.temperature)); if (ap.topP != null) cleanedParams.topP = Math.min(1, Math.max(0, ap.topP)); if (ap.frequencyPenalty != null) cleanedParams.frequencyPenalty = Math.min(2, Math.max(-2, ap.frequencyPenalty)); if (ap.presencePenalty != null) cleanedParams.presencePenalty = Math.min(2, Math.max(-2, ap.presencePenalty)); const updates: Partial = { baseURL: form.baseURL || undefined, defaultModel: form.defaultModel || undefined, skipTLSVerify: form.skipTLSVerify || undefined, advancedParams: Object.keys(cleanedParams).length > 0 ? cleanedParams : undefined, ...(isCustom && form.name.trim() ? { name: form.name.trim() } : {}), }; // Encrypt API key before saving if (form.apiKey) { updates.apiKey = await encryptField(form.apiKey); } else { updates.apiKey = undefined; } onSave(updates); }, [form, onSave, isCustom]); return (
{/* Name (custom providers only) */} {isCustom && (
setForm((prev) => ({ ...prev, name: e.target.value }))} placeholder={t('ai.providers.name.placeholder')} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
)} {/* API Key */}
setForm((prev) => ({ ...prev, apiKey: e.target.value }))} placeholder={isDecrypting ? t('ai.providers.apiKey.decrypting') : t('ai.providers.apiKey.placeholder')} disabled={isDecrypting} className="w-full h-8 rounded-md border border-input bg-background px-3 pr-9 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50" />
{/* Base URL */}
setForm((prev) => ({ ...prev, baseURL: e.target.value }))} placeholder={preset?.defaultBaseURL || "https://"} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
{/* Default Model */}
setForm((prev) => ({ ...prev, defaultModel: val }))} baseURL={form.baseURL || preset?.defaultBaseURL || ""} modelsEndpoint={preset?.modelsEndpoint} apiKey={form.apiKey} providerId={provider.providerId} skipTLSVerify={form.skipTLSVerify} />
{/* Skip TLS Verification */} {/* Advanced Parameters */}
{showAdvanced && (

{t('ai.providers.advancedParams.hint')}

{/* max_tokens */}
handleAdvancedParam("maxTokens", e.target.value)} placeholder={t('ai.providers.advancedParams.maxTokens.placeholder')} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
{/* temperature */}
handleAdvancedParam("temperature", e.target.value)} placeholder={t('ai.providers.advancedParams.default')} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
{/* top_p */}
handleAdvancedParam("topP", e.target.value)} placeholder={t('ai.providers.advancedParams.default')} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
{/* frequency_penalty */}
handleAdvancedParam("frequencyPenalty", e.target.value)} placeholder={t('ai.providers.advancedParams.default')} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
{/* presence_penalty */}
handleAdvancedParam("presencePenalty", e.target.value)} placeholder={t('ai.providers.advancedParams.default')} className="w-full h-8 rounded-md border border-input bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" />
)}
{/* Actions */}
); };