diff --git a/src/workspaces/officerdev/src/apps/Chat/useAgentPanel.ts b/src/workspaces/officerdev/src/apps/Chat/useAgentPanel.ts index d806743b..383b02f8 100644 --- a/src/workspaces/officerdev/src/apps/Chat/useAgentPanel.ts +++ b/src/workspaces/officerdev/src/apps/Chat/useAgentPanel.ts @@ -84,8 +84,14 @@ export function useAgentPanel(panelId: string) { }, [agent?.id, agent?.panelId, panelId, dashboardId, queryClient]); const claim = useMutation({ - mutationFn: (input: { name: string; cwd?: string; rolePrompt?: string }) => - clientRef.current.post<{ agent: AgentPanelView; created: boolean }>('/chat/agent-panels', { + mutationFn: (input: { name: string; cwd?: string; rolePrompt?: string }) => { + // Reject a name already on this dashboard before asking the server. The uniqueness that matters is + // the server's — an address that resolved to two agents would make a handoff ambiguous — but the + // list is already in hand, and the answer here is a sentence the human can act on. + if (agents.some((a) => a.name === input.name)) { + return Promise.reject(new Error(`There is already an agent named "${input.name}" on this dashboard`)); + } + return clientRef.current.post<{ agent: AgentPanelView; created: boolean }>('/chat/agent-panels', { dashboardId, panelId, // Record the directory the panel is already scoped to. A row with no cwd runs incoming handoffs @@ -93,7 +99,8 @@ export function useAgentPanel(panelId: string) { // agent working in two places depending on who spoke to it. ...(cwd && cwd !== '~' ? { cwd } : {}), ...input, - }), + }); + }, onSuccess: ({ agent: created }) => { // Write the name into the panel before invalidating: the config is what survives a drag, and a // refetch that landed first would show an agent this panel does not yet claim.