import React, { useState, useEffect } from "react"; import { Card, Text, Button, Icon, TextInput, } from "@tremor/react"; import { PlusIcon, } from "@heroicons/react/outline"; import { Modal, message } from "antd"; import { getGuardrailsList, deleteGuardrailCall } from "./networking"; import AddGuardrailForm from "./guardrails/add_guardrail_form"; import GuardrailTable from "./guardrails/guardrail_table"; import { isAdminRole } from "@/utils/roles"; interface GuardrailsPanelProps { accessToken: string | null; userRole?: string; } interface GuardrailItem { guardrail_id?: string; guardrail_name: string | null; litellm_params: { guardrail: string; mode: string; default_on: boolean; }; guardrail_info: Record | null; created_at?: string; updated_at?: string; } interface GuardrailsResponse { guardrails: GuardrailItem[]; } const GuardrailsPanel: React.FC = ({ accessToken, userRole }) => { const [guardrailsList, setGuardrailsList] = useState([]); const [isAddModalVisible, setIsAddModalVisible] = useState(false); const [isLoading, setIsLoading] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [guardrailToDelete, setGuardrailToDelete] = useState<{id: string, name: string} | null>(null); const [isViewingGuardrailInfo, setIsViewingGuardrailInfo] = useState(false); const isAdmin = userRole ? isAdminRole(userRole) : false; const fetchGuardrails = async () => { if (!accessToken) { return; } setIsLoading(true); try { const response: GuardrailsResponse = await getGuardrailsList(accessToken); console.log(`guardrails: ${JSON.stringify(response)}`); setGuardrailsList(response.guardrails); } catch (error) { console.error('Error fetching guardrails:', error); } finally { setIsLoading(false); } }; useEffect(() => { fetchGuardrails(); }, [accessToken]); const handleAddGuardrail = () => { setIsAddModalVisible(true); }; const handleCloseModal = () => { setIsAddModalVisible(false); }; const handleSuccess = () => { fetchGuardrails(); }; const handleDeleteClick = (guardrailId: string, guardrailName: string) => { setGuardrailToDelete({id: guardrailId, name: guardrailName}); }; const handleDeleteConfirm = async () => { if (!guardrailToDelete || !accessToken) return; // Log removed to maintain clean production code setIsDeleting(true); try { await deleteGuardrailCall(accessToken, guardrailToDelete.id); message.success(`Guardrail "${guardrailToDelete.name}" deleted successfully`); fetchGuardrails(); // Refresh the list } catch (error) { console.error('Error deleting guardrail:', error); message.error('Failed to delete guardrail'); } finally { setIsDeleting(false); setGuardrailToDelete(null); } }; const handleDeleteCancel = () => { setGuardrailToDelete(null); }; return (
{!isViewingGuardrailInfo && (
)} {guardrailToDelete && (

Are you sure you want to delete guardrail: {guardrailToDelete.name} ?

This action cannot be undone.

)}
); }; export default GuardrailsPanel;