import type { DiscoveredAgent, ExternalAgentConfig } from './types'; import { getCommandBasename, isPathLikeCommand } from './shared/pathLikeCommand'; export { isPathLikeCommand, getCommandBasename }; export type ManagedAgentKey = 'codex' | 'claude' | 'copilot' | 'cursor' | 'codebuddy' | 'opencode' | 'grok'; const MANAGED_AGENT_META: Record = { codex: { commandNames: ['codex'], sdkBackend: 'codex' }, claude: { commandNames: ['claude'], sdkBackend: 'claude' }, copilot: { commandNames: ['copilot'], sdkBackend: 'copilot' }, cursor: { commandNames: ['cursor'], sdkBackend: 'cursor' }, codebuddy: { commandNames: ['codebuddy'], sdkBackend: 'codebuddy' }, opencode: { commandNames: ['opencode'], sdkBackend: 'opencode' }, grok: { commandNames: ['grok'], sdkBackend: 'grok' }, }; function matchesPrimaryCliBasename(command: string | undefined, agentKey: ManagedAgentKey): boolean { const basename = getCommandBasename(command); return basename === agentKey || basename.startsWith(`${agentKey}.`); } export function isSettingsManagedDiscoveredAgent( agent: Pick, ): agent is Pick & { command: ManagedAgentKey } { return agent.command === 'codex' || agent.command === 'claude' || agent.command === 'copilot' || agent.command === 'cursor' || agent.command === 'codebuddy' || agent.command === 'opencode' || agent.command === 'grok'; } export function matchesManagedAgentConfig( agent: Pick, agentKey: ManagedAgentKey, ): boolean { const meta = MANAGED_AGENT_META[agentKey]; const basename = getCommandBasename(agent.command); if (agentKey === 'claude') { return ( agent.id === 'discovered_claude' || basename === 'claude' || basename.startsWith('claude.') ); } return ( agent.id === `discovered_${agentKey}` || getExternalAgentSdkBackend(agent) === meta.sdkBackend || meta.commandNames.some((commandName) => basename === commandName || basename.startsWith(`${commandName}.`)) ); } export function getExternalAgentSdkBackend( agent: Pick | undefined, ): string | undefined { return agent?.sdkBackend || agent?.acpCommand || undefined; } export function getManagedAgentStoredPath( agents: ExternalAgentConfig[], agentKey: ManagedAgentKey, ): string | null { const managedId = `discovered_${agentKey}`; const preferredAgent = agents.find( (agent) => agent.id === managedId && isPathLikeCommand(agent.command) && matchesPrimaryCliBasename(agent.command, agentKey), ); if (preferredAgent) { return preferredAgent.command; } const fallbackAgent = agents.find( (agent) => matchesManagedAgentConfig(agent, agentKey) && isPathLikeCommand(agent.command) && matchesPrimaryCliBasename(agent.command, agentKey), ); return fallbackAgent?.command ?? null; } export function getManualAgentCommand( config: Pick | null | undefined, ): string | undefined { const command = String(config?.command || '').trim(); return config?.commandSource === 'manual' && command ? command : undefined; }