import { useEffect, useState } from "react"; import { useNavigate, useParams } from "react-router-dom"; import { createAgent, getAgent, improveAgentPrompt, updateAgent } from "../../lib/api"; import { getErrorMessage } from "../../lib/errors"; import type { AgentRole, AgentTool } from "../../lib/types"; import { buttonClass, inputClass, labelClass, noticeClass, pageTitleClass, textAreaClass, } from "../ui/primitives"; 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 [isDefaultAgent, setIsDefaultAgent] = useState(false); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [initializing, setInitializing] = useState(false); const [improvingPrompt, setImprovingPrompt] = 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); setIsDefaultAgent(agent.is_default); } 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); } } async function handleImprovePrompt() { setImprovingPrompt(true); setError(null); try { const improvedPrompt = await improveAgentPrompt(name, role, tool, customPrompt); setCustomPrompt(improvedPrompt); } catch (err: unknown) { setError(getErrorMessage(err)); } finally { setImprovingPrompt(false); } } return (

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

{initializing &&
Loading agent...
} {isEditing && isDefaultAgent && (
This is a default agent. Its tool and script/prompt can be modified, but its name and role are fixed.
)}
setName(e.target.value)} disabled={isEditing && isDefaultAgent} required className={inputClass} />