/* ┌──────────────────────────────────────────────────────────────────────────────┐ │ @author: Davidson Gomes │ │ @file: /app/agents/dialogs/ApiKeysDialog.tsx │ │ Developed by: Davidson Gomes │ │ Creation date: May 13, 2025 │ │ Contact: contato@evolution-api.com │ ├──────────────────────────────────────────────────────────────────────────────┤ │ @copyright © Evolution API 2025. All rights reserved. │ │ Licensed under the Apache License, Version 2.0 │ │ │ │ You may not use this file except in compliance with the License. │ │ You may obtain a copy of the License at │ │ │ │ http://www.apache.org/licenses/LICENSE-2.0 │ │ │ │ Unless required by applicable law or agreed to in writing, software │ │ distributed under the License is distributed on an "AS IS" BASIS, │ │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ │ See the License for the specific language governing permissions and │ │ limitations under the License. │ ├──────────────────────────────────────────────────────────────────────────────┤ │ @important │ │ For any future changes to the code in this file, it is recommended to │ │ include, together with the modification, the information of the developer │ │ who changed it and the date of modification. │ └──────────────────────────────────────────────────────────────────────────────┘ */ "use client"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { ConfirmationDialog } from "./ConfirmationDialog"; import { ApiKey } from "@/services/agentService"; import { Edit, Eye, Key, Plus, Trash2, X } from "lucide-react"; import { useState, useEffect } from "react"; import { availableModelProviders } from "@/types/aiModels"; interface ApiKeysDialogProps { open: boolean; onOpenChange: (open: boolean) => void; apiKeys: ApiKey[]; isLoading: boolean; onAddApiKey: (apiKey: { name: string; provider: string; key_value: string; }) => Promise; onUpdateApiKey: ( id: string, apiKey: { name: string; provider: string; key_value?: string; is_active: boolean; } ) => Promise; onDeleteApiKey: (id: string) => Promise; } export function ApiKeysDialog({ open, onOpenChange, apiKeys, isLoading, onAddApiKey, onUpdateApiKey, onDeleteApiKey, }: ApiKeysDialogProps) { const [isAddingApiKey, setIsAddingApiKey] = useState(false); const [isEditingApiKey, setIsEditingApiKey] = useState(false); const [currentApiKey, setCurrentApiKey] = useState< Partial >({}); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [apiKeyToDelete, setApiKeyToDelete] = useState(null); const [isApiKeyVisible, setIsApiKeyVisible] = useState(false); const handleAddClick = () => { setCurrentApiKey({}); setIsAddingApiKey(true); setIsEditingApiKey(false); }; const handleEditClick = (apiKey: ApiKey) => { setCurrentApiKey({ ...apiKey, key_value: "" }); setIsAddingApiKey(true); setIsEditingApiKey(true); }; const handleDeleteClick = (apiKey: ApiKey) => { setApiKeyToDelete(apiKey); setIsDeleteDialogOpen(true); }; const handleSaveApiKey = async () => { if ( !currentApiKey.name || !currentApiKey.provider || (!isEditingApiKey && !currentApiKey.key_value) ) { return; } try { if (currentApiKey.id) { await onUpdateApiKey(currentApiKey.id, { name: currentApiKey.name, provider: currentApiKey.provider, key_value: currentApiKey.key_value, is_active: currentApiKey.is_active !== false, }); } else { await onAddApiKey({ name: currentApiKey.name, provider: currentApiKey.provider, key_value: currentApiKey.key_value!, }); } setCurrentApiKey({}); setIsAddingApiKey(false); setIsEditingApiKey(false); } catch (error) { console.error("Error saving API key:", error); } }; const handleDeleteConfirm = async () => { if (!apiKeyToDelete) return; try { await onDeleteApiKey(apiKeyToDelete.id); setApiKeyToDelete(null); setIsDeleteDialogOpen(false); } catch (error) { console.error("Error deleting API key:", error); } }; return ( Manage API Keys Add and manage API keys for use in your agents
{isAddingApiKey ? (

{isEditingApiKey ? "Edit Key" : "New Key"}

setCurrentApiKey({ ...currentApiKey, name: e.target.value, }) } className="col-span-3 bg-[#333] border-[#444] text-white" placeholder="OpenAI GPT-4" />
setCurrentApiKey({ ...currentApiKey, key_value: e.target.value, }) } className="bg-[#333] border-[#444] text-white pr-10" type={isApiKeyVisible ? "text" : "password"} placeholder={ isEditingApiKey ? "Leave blank to keep the current value" : "sk-..." } />
{isEditingApiKey && (
setCurrentApiKey({ ...currentApiKey, is_active: !!checked, }) } className="mr-2 data-[state=checked]:bg-emerald-400 data-[state=checked]:border-emerald-400" />
)}
) : ( <>

Available Keys

{isLoading ? (
) : apiKeys.length > 0 ? (
{apiKeys.map((apiKey) => (

{apiKey.name}

{apiKey.provider.toUpperCase()}

Created on{" "} {new Date(apiKey.created_at).toLocaleDateString()}

{!apiKey.is_active && ( Inactive )}
))}
) : (

You don't have any API keys registered

Add your API keys to use them in your agents

)} )}
); }