import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; 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"; const commandFormSchema = z.object({ name: z .string() .min(1, "Command name is required") .regex( /^[a-z0-9-]+$/, "Must be lowercase letters, numbers, and hyphens only", ), template: z.string().min(1, "Template is required"), description: z.string().optional(), agent: z.string().optional(), model: z.string().optional(), subtask: z.boolean(), topP: z.number().min(0).max(1).optional(), }); type CommandFormValues = z.infer; interface Command { template: string; description?: string; agent?: string; model?: string; subtask?: boolean; topP?: number; } interface CommandDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onSubmit: (name: string, command: Command) => void; editingCommand?: { name: string; command: Command } | null; } export function CommandDialog({ open, onOpenChange, onSubmit, editingCommand, }: CommandDialogProps) { const form = useForm({ resolver: zodResolver(commandFormSchema), defaultValues: { name: editingCommand?.name || "", template: editingCommand?.command.template || "", description: editingCommand?.command.description || "", agent: editingCommand?.command.agent || "", model: editingCommand?.command.model || "", subtask: editingCommand?.command.subtask || false, topP: editingCommand?.command.topP ?? 1, }, }); const handleSubmit = (values: CommandFormValues) => { const command: Command = { template: values.template, description: values.description || undefined, agent: values.agent || undefined, model: values.model || undefined, subtask: values.subtask || undefined, topP: values.topP || undefined, }; onSubmit(values.name, command); form.reset(); onOpenChange(false); }; const handleOpenChange = (isOpen: boolean) => { if (!isOpen) { form.reset(); } onOpenChange(isOpen); }; return ( {editingCommand ? "Edit Command" : "Create Command"}
( Command Name Use lowercase letters, numbers, and hyphens only )} /> ( Description