import React from 'react';
import { Button } from '@/components/ui/button';
import { ButtonLarge } from '@/components/ui/button-large';
import { Input } from '@/components/ui/input';
import { toast } from 'sonner';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@/components/ui/dialog';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { RiAddLine, RiAiAgentFill, RiAiAgentLine, RiDeleteBinLine, RiFileCopyLine, RiMore2Line, RiRobot2Line, RiRobotLine } from '@remixicon/react';
import { useAgentsStore } from '@/stores/useAgentsStore';
import { useUIStore } from '@/stores/useUIStore';
import { useDeviceInfo } from '@/lib/device';
import { cn } from '@/lib/utils';
import type { Agent } from '@opencode-ai/sdk';
import { ScrollableOverlay } from '@/components/ui/ScrollableOverlay';
export const AgentsSidebar: React.FC = () => {
const [newAgentName, setNewAgentName] = React.useState('');
const [isCreateDialogOpen, setIsCreateDialogOpen] = React.useState(false);
const {
selectedAgentName,
agents,
setSelectedAgent,
deleteAgent,
loadAgents,
} = useAgentsStore();
const { setSidebarOpen } = useUIStore();
const { isMobile } = useDeviceInfo();
React.useEffect(() => {
loadAgents();
}, [loadAgents]);
const handleCreateAgent = () => {
if (!newAgentName.trim()) {
toast.error('Agent name is required');
return;
}
if (agents.some((agent) => agent.name === newAgentName)) {
toast.error('An agent with this name already exists');
return;
}
setSelectedAgent(newAgentName);
setNewAgentName('');
setIsCreateDialogOpen(false);
if (isMobile) {
setSidebarOpen(false);
}
};
const handleDeleteAgent = async (agent: Agent) => {
if (agent.builtIn) {
toast.error('Built-in agents cannot be deleted');
return;
}
if (window.confirm(`Are you sure you want to delete agent "${agent.name}"?`)) {
const success = await deleteAgent(agent.name);
if (success) {
toast.success(`Agent "${agent.name}" deleted successfully`);
} else {
toast.error('Failed to delete agent');
}
}
};
const handleDuplicateAgent = (agent: Agent) => {
const baseName = agent.name;
let copyNumber = 1;
let newName = `${baseName} Copy`;
while (agents.some((a) => a.name === newName)) {
copyNumber++;
newName = `${baseName} Copy ${copyNumber}`;
}
setSelectedAgent(newName);
setIsCreateDialogOpen(false);
if (isMobile) {
setSidebarOpen(false);
}
};
const getAgentModeIcon = (mode?: string) => {
switch (mode) {
case 'primary':
return