import { useState } from 'react' import { Plus } from 'lucide-react' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Dialog, DialogTrigger } from '@/components/ui/dialog' import { SettingsList, SettingsListRow } from '@/components/ui/settings-list' import { AgentDialog } from './AgentDialog' import { UploadFolderButton } from './UploadFolderButton' import { DirectoryFilesList } from './DirectoryFilesList' import type { OpenCodeDirectoryFileInfo } from '@/api/types/settings' interface Agent { prompt?: string description?: string mode?: 'subagent' | 'primary' | 'all' temperature?: number topP?: number top_p?: number model?: string tools?: Record permission?: { edit?: 'ask' | 'allow' | 'deny' bash?: 'ask' | 'allow' | 'deny' | Record webfetch?: 'ask' | 'allow' | 'deny' } disable?: boolean [key: string]: unknown } interface AgentsEditorProps { agents: Record directoryAgents?: OpenCodeDirectoryFileInfo[] onChange: (agents: Record) => void } export function AgentsEditor({ agents, directoryAgents = [], onChange }: AgentsEditorProps) { const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false) const [editingAgent, setEditingAgent] = useState<{ name: string; agent: Agent } | null>(null) const hasAgents = Object.keys(agents).length > 0 || directoryAgents.length > 0 const handleAgentSubmit = (name: string, agent: Agent) => { if (editingAgent) { const updatedAgents = { ...agents } delete updatedAgents[editingAgent.name] updatedAgents[name] = agent onChange(updatedAgents) setEditingAgent(null) } else { const updatedAgents = { ...agents, [name]: agent } onChange(updatedAgents) setIsCreateDialogOpen(false) } } const deleteAgent = (name: string) => { const updatedAgents = { ...agents } delete updatedAgents[name] onChange(updatedAgents) } const startEdit = (name: string, agent: Agent) => { setEditingAgent({ name, agent }) } return (
{Object.entries(agents).map(([name, agent]) => ( {agent.mode && {agent.mode}} {agent.disable && Disabled} } onClick={() => startEdit(name, agent)} primaryAction={{ label: 'Edit', onClick: () => startEdit(name, agent) }} actions={[{ label: 'Delete', destructive: true, onClick: () => deleteAgent(name) }]} actionsLabel={`Actions for ${name}`} /> ))} setEditingAgent(null)} onSubmit={handleAgentSubmit} editingAgent={editingAgent} />
) }