import { useEffect, useState } from "react"; import { useNavigate, useParams } from "react-router-dom"; import { createAgent, getAgent, updateAgent } from "../../lib/api"; import { getErrorMessage } from "../../lib/errors"; import type { AgentRole, AgentTool } from "../../lib/types"; export default function AgentForm() { const navigate = useNavigate(); const { agentId } = useParams<{ agentId: string }>(); const isEditing = Boolean(agentId); const [name, setName] = useState(""); const [role, setRole] = useState("analyst"); const [tool, setTool] = useState("codex"); const [customPrompt, setCustomPrompt] = useState(""); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [initializing, setInitializing] = useState(false); useEffect(() => { async function loadAgent() { if (!agentId) return; setInitializing(true); setError(null); try { const agent = await getAgent(agentId); setName(agent.name); setRole(agent.role); setTool(agent.tool); setCustomPrompt(agent.custom_prompt); } catch (err: unknown) { setError(getErrorMessage(err)); } finally { setInitializing(false); } } void loadAgent(); }, [agentId]); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); setLoading(true); setError(null); try { if (isEditing && agentId) { await updateAgent(agentId, name, role, tool, customPrompt); } else { await createAgent(name, role, tool, customPrompt); } navigate("/agents"); } catch (err: unknown) { setError(getErrorMessage(err)); } finally { setLoading(false); } } return (

{isEditing ? "Edit agent" : "New agent"}

{initializing &&
Loading agent...
}
setName(e.target.value)} required className="w-full rounded border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500" />