/* Copyright 2026 Marimo. All rights reserved. */ import { useAtom, useSetAtom } from "jotai"; import { ChevronDownIcon } from "lucide-react"; import React, { memo, useState } from "react"; import useEvent from "react-use-event-hook"; import { AiProviderIcon } from "@/components/ai/ai-provider-icon"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { cn } from "@/utils/cn"; import { AgentDocs } from "./agent-docs"; import { type AgentSession, addSession, agentSessionStateAtom, type ExternalAgentId, getAgentSessionSupport, selectedTabAtom, } from "./state"; interface AgentSelectorProps { onSessionCreated?: (agentId: ExternalAgentId) => void; className?: string; } const AVAILABLE_AGENTS = [ { id: "claude", displayName: "Claude", iconId: "anthropic", }, { id: "gemini", displayName: "Gemini", iconId: "google", }, { id: "codex", displayName: "Codex", iconId: "openai", }, { id: "opencode", displayName: "OpenCode", iconId: "opencode", }, { id: "cursor", displayName: "Cursor", iconId: "cursor" }, ] as const; interface AgentMenuItemProps { agent: (typeof AVAILABLE_AGENTS)[number]; onSelect: (agentId: ExternalAgentId) => void; existingSessions: AgentSession[]; } const AgentMenuItem = memo( ({ agent, onSelect, existingSessions }) => { const sessionSupport = getAgentSessionSupport(agent.id); const hasExistingSession = existingSessions.some( (s) => s.agentId === agent.id, ); const resetSession = sessionSupport === "single" && hasExistingSession; const text = resetSession ? `Reset ${agent.displayName} session` : `New ${agent.displayName} session`; if (resetSession) { return ( onSelect(agent.id)} className="cursor-pointer" >
{text}
); } return ( onSelect(agent.id)} >
{text}
To start a {agent.displayName} agent, run the following command in your terminal.
); }, ); AgentMenuItem.displayName = "AgentMenuItem"; export const AgentSelector: React.FC = memo( ({ onSessionCreated, className }) => { const [sessionState, setSessionState] = useAtom(agentSessionStateAtom); const setActiveTab = useSetAtom(selectedTabAtom); const [isOpen, setIsOpen] = useState(false); const handleCreateSession = useEvent(async (agentId: ExternalAgentId) => { const newState = addSession(sessionState, { agentId, }); setSessionState(newState); setActiveTab(newState.activeTabId); setIsOpen(false); onSessionCreated?.(agentId); }); return ( {AVAILABLE_AGENTS.map((agent) => ( ))} ); }, ); AgentSelector.displayName = "AgentSelector";