import { useCallback, useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { useHuman, useI18n } from "@/i18n"; import { api, asUiError, type PbcConnection, type PbcConnectionSetup, type UiError } from "@/lib/api"; import type { UnsavedChangesGuard } from "@/lib/unsavedGuard"; type Provider = "openai" | "ollama" | "anthropic" | "compatible"; const presets: Record = { openai: { apiType: "openai", baseUrl: "https://api.openai.com/v1", model: "gpt-5.4-mini" }, ollama: { apiType: "ollama", baseUrl: "http://127.0.0.1:11434", model: "" }, anthropic: { apiType: "anthropic", baseUrl: "https://api.anthropic.com/v1", model: "" }, compatible: { apiType: "openai", baseUrl: "", model: "" }, }; const providerOf = (connection: PbcConnection): Provider => connection.apiType !== "openai" ? connection.apiType : connection.baseUrl === presets.openai.baseUrl ? "openai" : "compatible"; const inputClass = "selectable w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none focus:border-gold"; export function PbcConnectionSettings({ serverId, onSaved, onRegisterGuard }: { serverId: string; onSaved: () => void; onRegisterGuard: (guard: UnsavedChangesGuard | null) => void; }) { const { t } = useI18n(); const human = useHuman(); const [setup, setSetup] = useState(null); const [connection, setConnection] = useState(presets.openai); const [provider, setProvider] = useState("openai"); const [apiKey, setApiKey] = useState(""); const [removeKey, setRemoveKey] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [saved, setSaved] = useState(false); const reset = useCallback((value: PbcConnectionSetup) => { const current = value.connection ?? presets.openai; setConnection(current); setProvider(providerOf(current)); setApiKey(""); setRemoveKey(false); }, []); useEffect(() => { let cancelled = false; setSetup(null); setSaved(false); setError(null); void api.pbcConnection(serverId).then(value => { if (cancelled) return; setSetup(value); reset(value); }).catch(e => { if (!cancelled) setError(asUiError(e)); }); return () => { cancelled = true; }; }, [serverId, reset]); const previous = setup?.connection ?? presets.openai; const dirty = !!setup && (connection.apiType !== previous.apiType || connection.baseUrl !== previous.baseUrl || connection.model !== previous.model || !!apiKey || removeKey); const sameEndpoint = connection.apiType === previous.apiType && connection.baseUrl.trim().replace(/\/+$/, "") === previous.baseUrl; const hasKey = !!setup?.connection?.hasKey && sameEndpoint; const valid = !!connection.baseUrl.trim() && !!connection.model.trim(); const save = useCallback(async () => { if (!setup || !valid || busy) return false; setBusy(true); setSaved(false); setError(null); try { const next = await api.pbcSaveConnection(serverId, { ...connection, apiKey: apiKey || undefined, removeKey }); setSetup(next); reset(next); setSaved(true); onSaved(); return true; } catch (e) { setError(asUiError(e)); return false; } finally { setBusy(false); } }, [setup, valid, busy, serverId, connection, apiKey, removeKey, reset, onSaved]); useEffect(() => { if (!setup || !dirty) return; onRegisterGuard({ isDirty: () => true, save, discard: () => reset(setup) }); return () => onRegisterGuard(null); }, [setup, dirty, save, reset, onRegisterGuard]); function changeProvider(value: Provider) { setProvider(value); setConnection(presets[value]); setApiKey(""); setRemoveKey(false); setSaved(false); } return (

{t("mod.pbc.connection")}

{error &&

{error.human.code === "unknown" ? error.technical : human(error.human).message}

} {setup && (
{ event.preventDefault(); void save(); }}> {!setup.active &&

{t("mod.pbc.useConnection")}

}
{hasKey && }

{t("mod.pbc.keyHint")}

{saved &&

{t("mod.pbc.saved")}

}
)}
); }