import { Info, KeyRound, Trash2, Clipboard } from "lucide-react"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; import { AzureConfiguration } from "./AzureConfiguration"; import { VertexConfiguration } from "./VertexConfiguration"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { UserSettings } from "@/lib/schemas"; import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip"; import { showError } from "@/lib/toast"; // Helper function to mask ENV API keys (move or duplicate if needed elsewhere) const maskEnvApiKey = (key: string | undefined): string => { if (!key) return "Not Set"; if (key.length < 8) return "****"; return `${key.substring(0, 4)}...${key.substring(key.length - 4)}`; }; interface ApiKeyConfigurationProps { provider: string; providerDisplayName: string; settings: UserSettings | null | undefined; envVars: Record; envVarName?: string; isSaving: boolean; saveError: string | null; apiKeyInput: string; onApiKeyInputChange: (value: string) => void; onSaveKey: (value: string) => Promise; onDeleteKey: () => Promise; isDyad: boolean; updateSettings: (settings: Partial) => Promise; } export function ApiKeyConfiguration({ provider, providerDisplayName, settings, envVars, envVarName, isSaving, saveError, apiKeyInput, onApiKeyInputChange, onSaveKey, onDeleteKey, isDyad, updateSettings, }: ApiKeyConfigurationProps) { // Special handling for Azure OpenAI which requires environment variables if (provider === "azure") { return ( ); } // Special handling for Google Vertex AI which uses service account credentials if (provider === "vertex") { return ; } const envApiKey = envVarName ? envVars[envVarName] : undefined; const userApiKey = settings?.providerSettings?.[provider]?.apiKey?.value; const isValidUserKey = !!userApiKey && !userApiKey.startsWith("Invalid Key") && userApiKey !== "Not Set"; const hasEnvKey = !!envApiKey; const activeKeySource = isValidUserKey ? "settings" : hasEnvKey ? "env" : "none"; const defaultAccordionValue = []; if (isValidUserKey || !hasEnvKey) { defaultAccordionValue.push("settings-key"); } if (!isDyad && hasEnvKey) { defaultAccordionValue.push("env-key"); } return ( API Key from Settings {isValidUserKey && ( Current Key (Settings)

{userApiKey}

{activeKeySource === "settings" && (

This key is currently active.

)}
)}
onApiKeyInputChange(e.target.value)} placeholder={`Enter new ${providerDisplayName} API Key here`} className={`flex-grow ${saveError ? "border-red-500" : ""}`} /> Paste from clipboard and save
{saveError &&

{saveError}

}

Setting a key here will override the environment variable (if set).

{!isDyad && envVarName && ( API Key from Environment Variable {hasEnvKey ? ( Environment Variable Key ({envVarName})

{maskEnvApiKey(envApiKey)}

{activeKeySource === "env" && (

This key is currently active (no settings key set).

)} {activeKeySource === "settings" && (

This key is currently being overridden by the key set in Settings.

)}
) : ( Environment Variable Not Set The{" "} {envVarName} {" "} environment variable is not set. )}

This key is set outside the application. If present, it will be used only if no key is configured in the Settings section above. Requires app restart to detect changes.

)}
); }