import type { TuiAgent } from './types' import { isTuiAgent } from './tui-agent-config' // Keep this order in sync with the desktop agent catalog. It defines the // automatic fallback priority when the user has not chosen a default agent. export const TUI_AGENT_AUTO_PICK_ORDER = [ 'claude', 'claude-agent-teams', 'openclaude', 'codex', 'grok', 'copilot', 'opencode', 'pi', 'omp', 'gemini', 'antigravity', 'aider', 'goose', 'amp', 'kilo', 'kiro', 'crush', 'aug', 'autohand', 'cline', 'codebuff', 'command-code', 'continue', 'cursor', 'droid', 'kimi', 'mistral-vibe', 'qwen-code', 'rovo', 'hermes', 'openclaw' ] as const satisfies readonly TuiAgent[] export const DEFAULT_DISABLED_TUI_AGENTS = [ 'claude-agent-teams' ] as const satisfies readonly TuiAgent[] export function pickTuiAgent( preferred: TuiAgent | 'blank' | null | undefined, detected: Iterable, disabled?: Iterable | null ): TuiAgent | null { if (preferred === 'blank') { return null } const disabledSet = new Set(normalizeDisabledTuiAgents(disabled)) const detectedSet = detected instanceof Set ? detected : new Set(detected) if (preferred && detectedSet.has(preferred) && !disabledSet.has(preferred)) { return preferred } for (const agent of TUI_AGENT_AUTO_PICK_ORDER) { if (detectedSet.has(agent) && !disabledSet.has(agent)) { return agent } } return null } export function normalizeDisabledTuiAgents(value: unknown): TuiAgent[] { if (!Array.isArray(value)) { return [] } const seen = new Set() for (const item of value) { if (isTuiAgent(item)) { seen.add(item) } } return [...seen] } export function isTuiAgentEnabled(agent: TuiAgent, disabled?: Iterable | null): boolean { return !normalizeDisabledTuiAgents(disabled).includes(agent) } export function filterEnabledTuiAgents( agents: Iterable, disabled?: Iterable | null ): T[] { const disabledSet = new Set(normalizeDisabledTuiAgents(disabled)) return [...agents].filter((agent) => !disabledSet.has(agent)) }