import { client } from '@/ui/api/client' import { Card, CardContent } from '@/ui/components/ui/card' import { Button } from '@/ui/components/ui/button' import { Badge } from '@/ui/components/ui/badge' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { Loader2, Plus, Trash2, Bot, MoreVertical, MessageSquare, Zap, Cpu, Puzzle, } from 'lucide-react' import { useState } from 'react' import { toast } from 'sonner' import { AgentInfo, AgentType } from '@shumai/dtos' import { AgentFormDialog } from './AgentFormDialog' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/ui/components/ui/dropdown-menu' import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/ui/components/ui/alert-dialog' import { cn } from '@/ui/lib/utils' import { ChevronDown } from 'lucide-react' import { Switch } from '@/ui/components/ui/switch' interface AgentsSettingsProps { teamId: string } const AGENT_TYPES: { type: AgentType label: string icon: React.FC> }[] = [ { type: 'chat', label: 'Chat', icon: MessageSquare }, { type: 'autofill', label: 'Autofill', icon: Zap }, { type: 'embedding', label: 'Embedding', icon: Cpu }, ] export function AgentsSettings({ teamId }: AgentsSettingsProps) { const queryClient = useQueryClient() const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false) const [createType, setCreateType] = useState('chat') const [editingAgent, setAgentToEdit] = useState(null) const [agentToDelete, setAgentToDelete] = useState(null) const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) const [expandedSections, setExpandedSections] = useState>({ chat: true, autofill: true, embedding: true, }) const { data: agents = [], isLoading } = useQuery({ queryKey: ['agents', teamId], queryFn: async () => { const res = await client.api.teams[':teamId'].agents.$get({ param: { teamId }, }) if (!res.ok) throw new Error('failed to fetch agents') const data = await res.json() return [...data].sort((a, b) => b.id.localeCompare(a.id)) }, }) const deleteAgentMutation = useMutation({ mutationFn: async (agentId: string) => { const res = await client.api.agents[':agentId'].$delete({ param: { agentId }, }) if (!res.ok) throw new Error('failed to delete agent') }, onSuccess: () => { setIsDeleteDialogOpen(false) queryClient.invalidateQueries({ queryKey: ['agents', teamId] }) toast.success('Agent deleted successfully') }, onError: (error) => { toast.error(error.message) }, }) const updateAgentMutation = useMutation({ mutationFn: async (params: { teamId: string agentId: string name: string type: AgentType enabled: boolean avatar?: string providerId?: string modelId?: string thinkingLevel?: string systemPrompt?: string soul?: string skills?: string[] }) => { const res = await client.api.agents[':agentId'].$put({ param: { agentId: params.agentId }, json: { name: params.name, type: params.type, enabled: params.enabled, avatar: params.avatar, providerId: params.providerId, modelId: params.modelId, thinkingLevel: params.thinkingLevel, systemPrompt: params.systemPrompt, soul: params.soul, skills: params.skills, }, }) if (!res.ok) throw new Error('failed to update agent') return res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['agents', teamId] }) toast.success('Agent updated successfully') }, onError: (error) => { toast.error(error.message) }, }) const toggleSection = (type: string) => { setExpandedSections((prev) => ({ ...prev, [type]: !prev[type], })) } return ( <> {(isCreateDialogOpen || editingAgent) && ( { setIsCreateDialogOpen(false) setAgentToEdit(null) }} teamId={teamId} type={createType} initialValues={editingAgent || undefined} /> )} Are you absolutely sure? This action cannot be undone. This will permanently delete the agent " {agentToDelete?.name}" and remove all its data from our servers. Cancel { if (deleteAgentMutation.isPending) { e.preventDefault() return } if (agentToDelete) { deleteAgentMutation.mutate(agentToDelete.id) } }} > {deleteAgentMutation.isPending ? ( ) : ( 'Delete Agent' )} {isLoading && agents.length === 0 ? (
) : (
{isLoading && (
)} {AGENT_TYPES.map((section) => { const typeAgents = agents.filter((a) => a.type === section.type) const Icon = section.icon return (
toggleSection(section.type)} >

{section.label} Agents

{typeAgents.length}
{!( (section.type === 'autofill' || section.type === 'embedding') && typeAgents.length > 0 ) && ( )}
{expandedSections[section.type] && (
{typeAgents.map((agent) => ( setAgentToEdit(agent)} >
{agent.avatar ? ( ) : ( )}
{agent.name}
{agent.skills?.length || 0} Skills
e.stopPropagation()}> { updateAgentMutation.mutate({ teamId, agentId: agent.id, name: agent.name, type: agent.type, enabled, avatar: agent.avatar, providerId: agent.providerId, modelId: agent.modelId, thinkingLevel: agent.thinkingLevel, systemPrompt: agent.systemPrompt, soul: agent.soul, skills: agent.skills?.map((s) => s.skillId), }) }} />
e.stopPropagation()}> { e.stopPropagation() setAgentToDelete(agent) setIsDeleteDialogOpen(true) }} className="text-red-500 focus:text-red-500 gap-2 cursor-pointer" > Delete {' '}
))} {typeAgents.length === 0 && (

No {section.label} agents found

)}
)}
) })}
)} ) }