import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { useMemo, useEffect } from 'react' import { useQuery } from '@tanstack/react-query' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form' import { Textarea } from '@/components/ui/textarea' import { Switch } from '@/components/ui/switch' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Combobox, type ComboboxOption } from '@/components/ui/combobox' import { getProvidersWithModels } from '@/api/providers' const agentFormSchema = z.object({ name: z.string().min(1, 'Agent name is required').regex(/^[a-z0-9-]+$/, 'Must be lowercase letters, numbers, and hyphens only'), description: z.string().optional(), prompt: z.string().min(1, 'Prompt is required'), mode: z.enum(['subagent', 'primary', 'all']), temperature: z.number().min(0).max(2), topP: z.number().min(0).max(1), modelId: z.string().optional(), providerId: z.string().optional(), write: z.boolean(), edit: z.boolean(), bash: z.boolean(), webfetch: z.boolean(), editPermission: z.enum(['ask', 'allow', 'deny']), bashPermission: z.enum(['ask', 'allow', 'deny']), webfetchPermission: z.enum(['ask', 'allow', 'deny']), disable: z.boolean() }) type AgentFormValues = z.infer 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 } function parseModelString(model?: string): { providerId: string; modelId: string } { if (!model) return { providerId: '', modelId: '' } const [providerId, ...rest] = model.split('/') return { providerId: providerId || '', modelId: rest.join('/') || '' } } interface AgentDialogProps { open: boolean onOpenChange: (open: boolean) => void onSubmit: (name: string, agent: Agent) => void editingAgent?: { name: string; agent: Agent } | null } export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: AgentDialogProps) { const { data: providers = [] } = useQuery({ queryKey: ['providers-with-models'], queryFn: () => getProvidersWithModels(), enabled: open, staleTime: 5 * 60 * 1000, }) const providerOptions: ComboboxOption[] = useMemo(() => { const sourceLabels: Record = { configured: 'Custom', local: 'Local', builtin: 'Built-in', } return providers.map(p => ({ value: p.id, label: p.name || p.id, description: p.models.length > 0 ? `${p.models.length} models` : undefined, group: sourceLabels[p.source] || 'Other', })) }, [providers]) const getDefaultValues = (agent?: { name: string; agent: Agent } | null): AgentFormValues => { const parsed = parseModelString(agent?.agent.model) return { name: agent?.name || '', description: agent?.agent.description || '', prompt: agent?.agent.prompt || '', mode: agent?.agent.mode || 'subagent', temperature: agent?.agent.temperature ?? 0.7, topP: agent?.agent.topP ?? agent?.agent.top_p ?? 1, modelId: parsed.modelId, providerId: parsed.providerId, write: agent?.agent.tools?.write ?? true, edit: agent?.agent.tools?.edit ?? true, bash: agent?.agent.tools?.bash ?? true, webfetch: agent?.agent.tools?.webfetch ?? true, editPermission: agent?.agent.permission?.edit ?? 'allow', bashPermission: typeof agent?.agent.permission?.bash === 'string' ? agent.agent.permission.bash : 'allow', webfetchPermission: agent?.agent.permission?.webfetch ?? 'allow', disable: agent?.agent.disable ?? false } } const form = useForm({ resolver: zodResolver(agentFormSchema), defaultValues: getDefaultValues(editingAgent) }) useEffect(() => { if (open) { form.reset(getDefaultValues(editingAgent)) } }, [open, editingAgent, form]) const selectedProviderId = form.watch('providerId') const modelOptions: ComboboxOption[] = useMemo(() => { const selectedProvider = providers.find(p => p.id === selectedProviderId) if (selectedProvider && selectedProvider.models.length > 0) { return selectedProvider.models.map(m => ({ value: m.id, label: m.name || m.id, })) } return providers.flatMap(p => p.models.map(m => ({ value: m.id, label: m.name || m.id, group: p.name || p.id, }))) }, [providers, selectedProviderId]) const handleSubmit = (values: AgentFormValues) => { const agent: Agent = { prompt: values.prompt, description: values.description || undefined, mode: values.mode, temperature: values.temperature, topP: values.topP, disable: values.disable, tools: { write: values.write, edit: values.edit, bash: values.bash, webfetch: values.webfetch }, permission: { edit: values.editPermission, bash: values.bashPermission, webfetch: values.webfetchPermission } } if (values.modelId && values.providerId) { agent.model = `${values.providerId}/${values.modelId}` } onSubmit(values.name, agent) form.reset() onOpenChange(false) } const handleOpenChange = (isOpen: boolean) => { if (!isOpen) { form.reset() } onOpenChange(isOpen) } return ( {editingAgent ? 'Edit Agent' : 'Create Agent'}
( Agent Name Use lowercase letters, numbers, and hyphens only )} /> ( Description