import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Eye, EyeOff, Key, Trash2 } from "lucide-react"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { cn } from "@/lib/utils"; import { useConfigStore, DEFAULT_CONFIG_KEY } from "@/hooks/useConfigStore"; interface ApiKey { id: string; name: string; description?: string; value: string; isVisible: boolean; lastUsed?: string; } interface ApiKeySection { title: string; keys: ApiKey[]; } const API_KEY_SECTIONS: Record> = { llms: { title: "LLMs", }, infrastructure: { title: "Infrastructure", }, }; const API_KEY_DEFINITIONS = { llms: [ { id: "anthropicApiKey", name: "Anthropic" }, { id: "openaiApiKey", name: "OpenAI" }, { id: "googleApiKey", name: "Google Gen AI" }, ], infrastructure: [ { id: "daytonaApiKey", name: "Daytona", description: "Users not required to set this if using the demo", }, ], }; export function APIKeysTab() { const { getConfig, updateConfig } = useConfigStore(); const config = getConfig(DEFAULT_CONFIG_KEY); const [visibilityState, setVisibilityState] = useState< Record >({}); const toggleKeyVisibility = (keyId: string) => { setVisibilityState((prev) => ({ ...prev, [keyId]: !prev[keyId], })); }; const updateApiKey = (keyId: string, value: string) => { const currentApiKeys = config.apiKeys || {}; updateConfig(DEFAULT_CONFIG_KEY, "apiKeys", { ...currentApiKeys, [keyId]: value, }); }; const deleteApiKey = (keyId: string) => { const currentApiKeys = config.apiKeys || {}; const updatedApiKeys = { ...currentApiKeys }; delete updatedApiKeys[keyId]; updateConfig(DEFAULT_CONFIG_KEY, "apiKeys", updatedApiKeys); }; const getApiKeySections = (): Record => { const sections: Record = {}; const apiKeys = config.apiKeys || {}; Object.entries(API_KEY_SECTIONS).forEach(([sectionKey, sectionInfo]) => { sections[sectionKey] = { ...sectionInfo, keys: API_KEY_DEFINITIONS[ sectionKey as keyof typeof API_KEY_DEFINITIONS ].map((keyDef) => ({ ...keyDef, value: apiKeys[keyDef.id] || "", isVisible: visibilityState[keyDef.id] || false, })), }; }); return sections; }; const apiKeySections = getApiKeySections(); return (
{Object.entries(apiKeySections).map(([sectionKey, section]) => ( {section.title} Manage API keys for {section.title.toLowerCase()} services {section.keys.map((apiKey) => (

{apiKey.name}

{apiKey.value && ( Configured )} {apiKey.lastUsed && ( Last used {apiKey.lastUsed} )}
{apiKey.description && (

{apiKey.description}

)}
updateApiKey(apiKey.id, e.target.value) } placeholder={`Enter your ${apiKey.name} API key`} className="font-mono text-sm" /> {apiKey.value && ( )}

Your API key is stored

))}
))}
); }