cli_adapter.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. """
  2. CLI Adapter for Multi-Platform Support.
  3. Abstracts differences between Claude Code, OpenCode, Cursor, iFlow, Codex, Kilo, Kiro Code, Gemini CLI, Antigravity, Devin, Qoder, CodeBuddy, GitHub Copilot, Factory Droid, and Pi Agent interfaces.
  4. Supported platforms:
  5. - claude: Claude Code (default)
  6. - opencode: OpenCode
  7. - cursor: Cursor IDE
  8. - iflow: iFlow CLI
  9. - codex: Codex CLI (skills-based)
  10. - kilo: Kilo CLI
  11. - kiro: Kiro Code (skills-based)
  12. - gemini: Gemini CLI
  13. - antigravity: Antigravity (workflow-based)
  14. - devin: Devin (formerly Windsurf; workflow-based)
  15. - qoder: Qoder
  16. - codebuddy: CodeBuddy
  17. - copilot: GitHub Copilot (VS Code)
  18. - droid: Factory Droid (commands-based)
  19. - pi: Pi Agent (extension-backed)
  20. - trae: Trae IDE (IDE-only, hooks-based)
  21. Usage:
  22. from common.cli_adapter import CLIAdapter
  23. adapter = CLIAdapter("opencode")
  24. cmd = adapter.build_run_command(
  25. agent="dispatch",
  26. session_id="abc123",
  27. prompt="Start the pipeline"
  28. )
  29. """
  30. from __future__ import annotations
  31. from dataclasses import dataclass
  32. from pathlib import Path
  33. from typing import ClassVar, Literal
  34. Platform = Literal[
  35. "claude",
  36. "opencode",
  37. "cursor",
  38. "iflow",
  39. "codex",
  40. "kilo",
  41. "kiro",
  42. "gemini",
  43. "antigravity",
  44. "devin",
  45. "qoder",
  46. "codebuddy",
  47. "copilot",
  48. "droid",
  49. "pi",
  50. "trae",
  51. ]
  52. @dataclass
  53. class CLIAdapter:
  54. """Adapter for different AI coding CLI tools."""
  55. platform: Platform
  56. # =========================================================================
  57. # Agent Name Mapping
  58. # =========================================================================
  59. # OpenCode has built-in agents that cannot be overridden
  60. # See: https://github.com/sst/opencode/issues/4271
  61. # Note: Class-level constant, not a dataclass field
  62. _AGENT_NAME_MAP: ClassVar[dict[Platform, dict[str, str]]] = {
  63. "claude": {}, # No mapping needed
  64. "opencode": {
  65. "plan": "trellis-plan", # 'plan' is built-in in OpenCode
  66. },
  67. }
  68. def get_agent_name(self, agent: str) -> str:
  69. """Get platform-specific agent name.
  70. Args:
  71. agent: Original agent name (e.g., 'plan', 'dispatch')
  72. Returns:
  73. Platform-specific agent name (e.g., 'trellis-plan' for OpenCode)
  74. """
  75. mapping = self._AGENT_NAME_MAP.get(self.platform, {})
  76. return mapping.get(agent, agent)
  77. # =========================================================================
  78. # Agent Path
  79. # =========================================================================
  80. @property
  81. def config_dir_name(self) -> str:
  82. """Get platform-specific config directory name.
  83. Returns:
  84. Directory name ('.claude', '.opencode', '.cursor', '.iflow', '.codex', '.kilocode', '.kiro', '.gemini', '.agent', '.devin', '.qoder', '.codebuddy', '.github/copilot', '.factory', '.pi', or '.trae')
  85. """
  86. if self.platform == "opencode":
  87. return ".opencode"
  88. elif self.platform == "cursor":
  89. return ".cursor"
  90. elif self.platform == "iflow":
  91. return ".iflow"
  92. elif self.platform == "codex":
  93. return ".codex"
  94. elif self.platform == "kilo":
  95. return ".kilocode"
  96. elif self.platform == "kiro":
  97. return ".kiro"
  98. elif self.platform == "gemini":
  99. return ".gemini"
  100. elif self.platform == "antigravity":
  101. return ".agent"
  102. elif self.platform == "devin":
  103. return ".devin"
  104. elif self.platform == "qoder":
  105. return ".qoder"
  106. elif self.platform == "codebuddy":
  107. return ".codebuddy"
  108. elif self.platform == "copilot":
  109. return ".github/copilot"
  110. elif self.platform == "droid":
  111. return ".factory"
  112. elif self.platform == "pi":
  113. return ".pi"
  114. elif self.platform == "trae":
  115. return ".trae"
  116. else:
  117. return ".claude"
  118. def get_config_dir(self, project_root: Path) -> Path:
  119. """Get platform-specific config directory.
  120. Args:
  121. project_root: Project root directory
  122. Returns:
  123. Path to config directory (.claude, .opencode, .cursor, .iflow, .codex, .kilocode, .kiro, .gemini, .agent, .devin, .qoder, .codebuddy, .github/copilot, .factory, .pi, or .trae)
  124. """
  125. return project_root / self.config_dir_name
  126. def get_agent_path(self, agent: str, project_root: Path) -> Path:
  127. """Get path to agent definition file.
  128. Args:
  129. agent: Agent name (original, before mapping)
  130. project_root: Project root directory
  131. Returns:
  132. Path to agent definition file (.md for most platforms, .toml for Codex)
  133. """
  134. mapped_name = self.get_agent_name(agent)
  135. if self.platform == "codex":
  136. return self.get_config_dir(project_root) / "agents" / f"{mapped_name}.toml"
  137. return self.get_config_dir(project_root) / "agents" / f"{mapped_name}.md"
  138. def get_commands_path(self, project_root: Path, *parts: str) -> Path:
  139. """Get path to commands directory or specific command file.
  140. Args:
  141. project_root: Project root directory
  142. *parts: Additional path parts (e.g., 'trellis', 'finish-work.md')
  143. Returns:
  144. Path to commands directory or file
  145. Note:
  146. Cursor uses prefix naming: .cursor/commands/trellis-<name>.md
  147. Antigravity uses workflow directory: .agent/workflows/<name>.md
  148. Devin uses workflow directory: .devin/workflows/trellis-<name>.md
  149. Copilot uses prompt files: .github/prompts/<name>.prompt.md
  150. Pi uses prompt templates: .pi/prompts/trellis-<name>.md
  151. Claude/OpenCode use subdirectory: .claude/commands/trellis/<name>.md
  152. """
  153. if self.platform == "pi":
  154. prompts_dir = self.get_config_dir(project_root) / "prompts"
  155. if not parts:
  156. return prompts_dir
  157. if len(parts) >= 2 and parts[0] == "trellis":
  158. filename = parts[-1]
  159. if filename.endswith(".md"):
  160. filename = filename[:-3]
  161. return prompts_dir / f"trellis-{filename}.md"
  162. return prompts_dir / Path(*parts)
  163. if self.platform == "devin":
  164. workflow_dir = self.get_config_dir(project_root) / "workflows"
  165. if not parts:
  166. return workflow_dir
  167. if len(parts) >= 2 and parts[0] == "trellis":
  168. filename = parts[-1]
  169. return workflow_dir / f"trellis-{filename}"
  170. return workflow_dir / Path(*parts)
  171. if self.platform in ("antigravity", "kilo"):
  172. workflow_dir = self.get_config_dir(project_root) / "workflows"
  173. if not parts:
  174. return workflow_dir
  175. if len(parts) >= 2 and parts[0] == "trellis":
  176. filename = parts[-1]
  177. return workflow_dir / filename
  178. return workflow_dir / Path(*parts)
  179. if self.platform == "copilot":
  180. prompts_dir = project_root / ".github" / "prompts"
  181. if not parts:
  182. return prompts_dir
  183. if len(parts) >= 2 and parts[0] == "trellis":
  184. filename = parts[-1]
  185. if filename.endswith(".md"):
  186. filename = filename[:-3]
  187. return prompts_dir / f"{filename}.prompt.md"
  188. return prompts_dir / Path(*parts)
  189. if not parts:
  190. return self.get_config_dir(project_root) / "commands"
  191. # Cursor uses prefix naming instead of subdirectory
  192. if self.platform == "cursor" and len(parts) >= 2 and parts[0] == "trellis":
  193. # Convert trellis/<name>.md to trellis-<name>.md
  194. filename = parts[-1]
  195. return (
  196. self.get_config_dir(project_root) / "commands" / f"trellis-{filename}"
  197. )
  198. return self.get_config_dir(project_root) / "commands" / Path(*parts)
  199. def get_trellis_command_path(self, name: str) -> str:
  200. """Get relative path to a trellis command file.
  201. Args:
  202. name: Command name without extension (e.g., 'finish-work', 'check')
  203. Returns:
  204. Relative path string for use in JSONL entries
  205. Note:
  206. Cursor: .cursor/commands/trellis-<name>.md
  207. Codex: .agents/skills/trellis-<name>/SKILL.md
  208. Kiro: .kiro/skills/trellis-<name>/SKILL.md
  209. Gemini: .gemini/commands/trellis/<name>.toml
  210. Antigravity: .agent/workflows/<name>.md
  211. Devin: .devin/workflows/trellis-<name>.md
  212. Pi: .pi/prompts/trellis-<name>.md
  213. Others: .{platform}/commands/trellis/<name>.md
  214. """
  215. if self.platform == "cursor":
  216. return f".cursor/commands/trellis-{name}.md"
  217. elif self.platform == "codex":
  218. # 0.5.0-beta.0 renamed all skill dirs to add the `trellis-` prefix
  219. # (see that release's manifest for the 60+ rename entries).
  220. return f".agents/skills/trellis-{name}/SKILL.md"
  221. elif self.platform == "kiro":
  222. return f".kiro/skills/trellis-{name}/SKILL.md"
  223. elif self.platform == "gemini":
  224. return f".gemini/commands/trellis/{name}.toml"
  225. elif self.platform == "antigravity":
  226. return f".agent/workflows/{name}.md"
  227. elif self.platform == "devin":
  228. return f".devin/workflows/trellis-{name}.md"
  229. elif self.platform == "kilo":
  230. return f".kilocode/workflows/{name}.md"
  231. elif self.platform == "copilot":
  232. return f".github/prompts/{name}.prompt.md"
  233. elif self.platform == "droid":
  234. return f".factory/commands/trellis/{name}.md"
  235. elif self.platform == "pi":
  236. return f".pi/prompts/trellis-{name}.md"
  237. else:
  238. return f"{self.config_dir_name}/commands/trellis/{name}.md"
  239. # =========================================================================
  240. # Environment Variables
  241. # =========================================================================
  242. def get_non_interactive_env(self) -> dict[str, str]:
  243. """Get environment variables for non-interactive mode.
  244. Returns:
  245. Dict of environment variables to set
  246. """
  247. if self.platform == "opencode":
  248. return {"OPENCODE_NON_INTERACTIVE": "1"}
  249. elif self.platform == "iflow":
  250. return {"IFLOW_NON_INTERACTIVE": "1"}
  251. elif self.platform == "codex":
  252. return {"CODEX_NON_INTERACTIVE": "1"}
  253. elif self.platform == "kiro":
  254. return {"KIRO_NON_INTERACTIVE": "1"}
  255. elif self.platform == "gemini":
  256. return {} # Gemini CLI doesn't have a non-interactive env var
  257. elif self.platform == "antigravity":
  258. return {}
  259. elif self.platform == "devin":
  260. return {}
  261. elif self.platform == "qoder":
  262. return {}
  263. elif self.platform == "codebuddy":
  264. return {}
  265. elif self.platform == "copilot":
  266. return {}
  267. elif self.platform == "droid":
  268. return {}
  269. elif self.platform == "pi":
  270. return {}
  271. elif self.platform == "trae":
  272. return {}
  273. else:
  274. return {"CLAUDE_NON_INTERACTIVE": "1"}
  275. # =========================================================================
  276. # CLI Command Building
  277. # =========================================================================
  278. def build_run_command(
  279. self,
  280. agent: str,
  281. prompt: str,
  282. session_id: str | None = None,
  283. skip_permissions: bool = True,
  284. verbose: bool = True,
  285. json_output: bool = True,
  286. ) -> list[str]:
  287. """Build CLI command for running an agent.
  288. Args:
  289. agent: Agent name (will be mapped if needed)
  290. prompt: Prompt to send to the agent
  291. session_id: Optional session ID (Claude Code only for creation)
  292. skip_permissions: Whether to skip permission prompts
  293. verbose: Whether to enable verbose output
  294. json_output: Whether to use JSON output format
  295. Returns:
  296. List of command arguments
  297. """
  298. mapped_agent = self.get_agent_name(agent)
  299. if self.platform == "opencode":
  300. cmd = ["opencode", "run"]
  301. cmd.extend(["--agent", mapped_agent])
  302. # Note: OpenCode 'run' mode is non-interactive by default
  303. # No equivalent to Claude Code's --dangerously-skip-permissions
  304. # See: https://github.com/anomalyco/opencode/issues/9070
  305. if json_output:
  306. cmd.extend(["--format", "json"])
  307. if verbose:
  308. cmd.extend(["--log-level", "DEBUG", "--print-logs"])
  309. # Note: OpenCode doesn't support --session-id on creation
  310. # Session ID must be extracted from logs after startup
  311. cmd.append(prompt)
  312. elif self.platform == "iflow":
  313. cmd = ["iflow", "-y", "-p"]
  314. cmd.append(f"${mapped_agent} {prompt}")
  315. elif self.platform == "codex":
  316. cmd = ["codex", "exec"]
  317. cmd.append(prompt)
  318. elif self.platform == "kiro":
  319. cmd = ["kiro", "run", prompt]
  320. elif self.platform == "gemini":
  321. cmd = ["gemini"]
  322. cmd.append(prompt)
  323. elif self.platform == "antigravity":
  324. raise ValueError(
  325. "Antigravity workflows are UI slash commands; CLI agent run is not supported."
  326. )
  327. elif self.platform == "devin":
  328. raise ValueError(
  329. "Devin workflows are UI slash commands; CLI agent run is not supported."
  330. )
  331. elif self.platform == "qoder":
  332. cmd = ["qodercli", "-p", prompt]
  333. elif self.platform == "codebuddy":
  334. raise ValueError(
  335. "CodeBuddy does not support non-interactive mode (no CLI agent)"
  336. )
  337. elif self.platform == "copilot":
  338. raise ValueError(
  339. "GitHub Copilot is IDE-only; CLI agent run is not supported."
  340. )
  341. elif self.platform == "droid":
  342. raise ValueError(
  343. "Factory Droid CLI agent run is not yet supported."
  344. )
  345. elif self.platform == "pi":
  346. cmd = ["pi", "-p", prompt]
  347. elif self.platform == "trae":
  348. raise ValueError(
  349. "Trae is IDE-only; CLI agent run is not supported."
  350. )
  351. else: # claude
  352. cmd = ["claude", "-p"]
  353. cmd.extend(["--agent", mapped_agent])
  354. if session_id:
  355. cmd.extend(["--session-id", session_id])
  356. if skip_permissions:
  357. cmd.append("--dangerously-skip-permissions")
  358. if json_output:
  359. cmd.extend(["--output-format", "stream-json"])
  360. if verbose:
  361. cmd.append("--verbose")
  362. cmd.append(prompt)
  363. return cmd
  364. def build_resume_command(self, session_id: str) -> list[str]:
  365. """Build CLI command for resuming a session.
  366. Args:
  367. session_id: Session ID to resume (ignored for iFlow)
  368. Returns:
  369. List of command arguments
  370. """
  371. if self.platform == "opencode":
  372. return ["opencode", "run", "--session", session_id]
  373. elif self.platform == "iflow":
  374. # iFlow uses -c to continue most recent conversation
  375. # session_id is ignored as iFlow doesn't support session IDs
  376. return ["iflow", "-c"]
  377. elif self.platform == "codex":
  378. return ["codex", "resume", session_id]
  379. elif self.platform == "kiro":
  380. return ["kiro", "resume", session_id]
  381. elif self.platform == "gemini":
  382. return ["gemini", "--resume", session_id]
  383. elif self.platform == "antigravity":
  384. raise ValueError(
  385. "Antigravity workflows are UI slash commands; CLI resume is not supported."
  386. )
  387. elif self.platform == "devin":
  388. raise ValueError(
  389. "Devin workflows are UI slash commands; CLI resume is not supported."
  390. )
  391. elif self.platform == "qoder":
  392. return ["qodercli", "--resume", session_id]
  393. elif self.platform == "codebuddy":
  394. raise ValueError(
  395. "CodeBuddy does not support non-interactive mode (no CLI agent)"
  396. )
  397. elif self.platform == "copilot":
  398. raise ValueError(
  399. "GitHub Copilot is IDE-only; CLI resume is not supported."
  400. )
  401. elif self.platform == "droid":
  402. raise ValueError(
  403. "Factory Droid CLI resume is not yet supported."
  404. )
  405. elif self.platform == "pi":
  406. return ["pi", "-c", session_id]
  407. elif self.platform == "trae":
  408. raise ValueError(
  409. "Trae is IDE-only; CLI resume is not supported."
  410. )
  411. else:
  412. return ["claude", "--resume", session_id]
  413. def get_resume_command_str(self, session_id: str, cwd: str | None = None) -> str:
  414. """Get human-readable resume command string.
  415. Args:
  416. session_id: Session ID to resume
  417. cwd: Optional working directory to cd into
  418. Returns:
  419. Command string for display
  420. """
  421. cmd = self.build_resume_command(session_id)
  422. cmd_str = " ".join(cmd)
  423. if cwd:
  424. return f"cd {cwd} && {cmd_str}"
  425. return cmd_str
  426. # =========================================================================
  427. # Platform Detection Helpers
  428. # =========================================================================
  429. @property
  430. def is_opencode(self) -> bool:
  431. """Check if platform is OpenCode."""
  432. return self.platform == "opencode"
  433. @property
  434. def is_claude(self) -> bool:
  435. """Check if platform is Claude Code."""
  436. return self.platform == "claude"
  437. @property
  438. def is_cursor(self) -> bool:
  439. """Check if platform is Cursor."""
  440. return self.platform == "cursor"
  441. @property
  442. def is_iflow(self) -> bool:
  443. """Check if platform is iFlow CLI."""
  444. return self.platform == "iflow"
  445. @property
  446. def cli_name(self) -> str:
  447. """Get CLI executable name.
  448. Note: Cursor doesn't have a CLI tool, returns None-like value.
  449. """
  450. if self.is_opencode:
  451. return "opencode"
  452. elif self.is_cursor:
  453. return "cursor" # Note: Cursor is IDE-only, no CLI
  454. elif self.platform == "iflow":
  455. return "iflow"
  456. elif self.platform == "kiro":
  457. return "kiro"
  458. elif self.platform == "gemini":
  459. return "gemini"
  460. elif self.platform == "antigravity":
  461. return "agy"
  462. elif self.platform == "devin":
  463. return "devin"
  464. elif self.platform == "qoder":
  465. return "qodercli"
  466. elif self.platform == "codebuddy":
  467. return "codebuddy"
  468. elif self.platform == "copilot":
  469. return "copilot"
  470. elif self.platform == "droid":
  471. return "droid"
  472. elif self.platform == "pi":
  473. return "pi"
  474. elif self.platform == "trae":
  475. return "trae"
  476. else:
  477. return "claude"
  478. @property
  479. def supports_cli_agents(self) -> bool:
  480. """Check if platform supports running agents via CLI.
  481. Claude Code, OpenCode, iFlow, and Codex support CLI agent execution.
  482. Cursor is IDE-only and doesn't support CLI agents.
  483. """
  484. return self.platform in ("claude", "opencode", "iflow", "codex", "pi")
  485. @property
  486. def requires_agent_definition_file(self) -> bool:
  487. """Check if platform requires an agent definition file (.md/.toml) to run.
  488. Claude Code, OpenCode, iFlow: require agent .md files (--agent flag).
  489. Codex: auto-discovers agents from .codex/agents/*.toml, no --agent flag.
  490. """
  491. return self.platform in ("claude", "opencode", "iflow")
  492. # =========================================================================
  493. # Session ID Handling
  494. # =========================================================================
  495. @property
  496. def supports_session_id_on_create(self) -> bool:
  497. """Check if platform supports specifying session ID on creation.
  498. Claude Code: Yes (--session-id)
  499. OpenCode: No (auto-generated, extract from logs)
  500. iFlow: No (no session ID support)
  501. """
  502. return self.platform == "claude"
  503. def extract_session_id_from_log(self, log_content: str) -> str | None:
  504. """Extract session ID from log output (OpenCode only).
  505. OpenCode generates session IDs in format: ses_xxx
  506. Args:
  507. log_content: Log file content
  508. Returns:
  509. Session ID if found, None otherwise
  510. """
  511. import re
  512. # OpenCode session ID pattern
  513. match = re.search(r"ses_[a-zA-Z0-9]+", log_content)
  514. if match:
  515. return match.group(0)
  516. return None
  517. # =============================================================================
  518. # Factory Function
  519. # =============================================================================
  520. def get_cli_adapter(platform: str = "claude") -> CLIAdapter:
  521. """Get CLI adapter for the specified platform.
  522. Args:
  523. platform: Platform name ('claude', 'opencode', 'cursor', 'iflow', 'codex', 'kilo', 'kiro', 'gemini', 'antigravity', 'devin', 'qoder', 'codebuddy', 'copilot', 'droid', 'pi', or 'trae')
  524. Returns:
  525. CLIAdapter instance
  526. Raises:
  527. ValueError: If platform is not supported
  528. Note:
  529. 'windsurf' is accepted as a deprecated alias for 'devin' (Windsurf was
  530. renamed to Devin) and normalized before validation.
  531. """
  532. # Deprecated alias: Windsurf was renamed to Devin.
  533. if platform == "windsurf":
  534. platform = "devin"
  535. if platform not in (
  536. "claude",
  537. "opencode",
  538. "cursor",
  539. "iflow",
  540. "codex",
  541. "kilo",
  542. "kiro",
  543. "gemini",
  544. "antigravity",
  545. "devin",
  546. "qoder",
  547. "codebuddy",
  548. "copilot",
  549. "droid",
  550. "pi",
  551. "trae",
  552. ):
  553. raise ValueError(
  554. f"Unsupported platform: {platform} (must be 'claude', 'opencode', 'cursor', 'iflow', 'codex', 'kilo', 'kiro', 'gemini', 'antigravity', 'devin', 'qoder', 'codebuddy', 'copilot', 'droid', 'pi', or 'trae')"
  555. )
  556. return CLIAdapter(platform=platform) # type: ignore
  557. _ALL_PLATFORM_CONFIG_DIRS = (
  558. ".claude",
  559. ".cursor",
  560. ".iflow",
  561. ".opencode",
  562. ".codex",
  563. ".kilocode",
  564. ".kiro",
  565. ".gemini",
  566. ".agent",
  567. ".devin",
  568. ".windsurf", # deprecated: pre-rename Devin config dir (still a platform signal)
  569. ".qoder",
  570. ".codebuddy",
  571. ".github/copilot",
  572. ".factory",
  573. ".pi",
  574. ".trae",
  575. )
  576. """Platform-specific config directory names used by detect_platform exclusion
  577. checks. `.agents/skills/` is NOT listed here: it is a shared cross-platform
  578. layer (written by Codex, also consumed by Amp/Cline/Warp/etc. via the
  579. agentskills.io standard), not a single-platform signal. Its presence must not
  580. block detection of Kiro, Antigravity, Devin, or other platforms."""
  581. def _has_other_platform_dir(project_root: Path, exclude: set[str]) -> bool:
  582. """Check if any platform config dir exists besides those in *exclude*."""
  583. return any(
  584. (project_root / d).is_dir()
  585. for d in _ALL_PLATFORM_CONFIG_DIRS
  586. if d not in exclude
  587. )
  588. def detect_platform(project_root: Path) -> Platform:
  589. """Auto-detect platform based on existing config directories.
  590. Detection order:
  591. 1. TRELLIS_PLATFORM environment variable (if set)
  592. 2. .opencode directory exists → opencode
  593. 3. .iflow directory exists → iflow
  594. 4. .cursor directory exists (without .claude) → cursor
  595. 5. .gemini directory exists → gemini
  596. 6. .codex exists and no other platform dirs → codex
  597. 7. .kilocode directory exists → kilo
  598. 8. .kiro/skills exists and no other platform dirs → kiro
  599. 9. .agent/workflows exists and no other platform dirs → antigravity
  600. 10. .devin/workflows (or legacy .windsurf/workflows) exists and no other platform dirs → devin
  601. 11. .codebuddy directory exists → codebuddy
  602. 12. .qoder directory exists → qoder
  603. 13. .github/copilot directory exists → copilot
  604. 14. .factory directory exists → droid
  605. 15. .pi directory exists → pi
  606. 16. .trae directory exists → trae
  607. 17. Default → claude
  608. Args:
  609. project_root: Project root directory
  610. Returns:
  611. Detected platform ('claude', 'opencode', 'cursor', 'iflow', 'codex', 'kilo', 'kiro', 'gemini', 'antigravity', 'devin', 'qoder', 'codebuddy', 'copilot', 'droid', 'pi', 'trae', or default 'claude')
  612. """
  613. import os
  614. # Check environment variable first
  615. env_platform = os.environ.get("TRELLIS_PLATFORM", "").lower()
  616. # Deprecated alias: Windsurf was renamed to Devin.
  617. if env_platform == "windsurf":
  618. env_platform = "devin"
  619. if env_platform in (
  620. "claude",
  621. "opencode",
  622. "cursor",
  623. "iflow",
  624. "codex",
  625. "kilo",
  626. "kiro",
  627. "gemini",
  628. "antigravity",
  629. "devin",
  630. "qoder",
  631. "codebuddy",
  632. "copilot",
  633. "droid",
  634. "pi",
  635. "trae",
  636. ):
  637. return env_platform # type: ignore
  638. # Check for .opencode directory (OpenCode-specific)
  639. if (project_root / ".opencode").is_dir():
  640. return "opencode"
  641. # Check for .iflow directory (iFlow-specific)
  642. if (project_root / ".iflow").is_dir():
  643. return "iflow"
  644. # Check for .cursor directory (Cursor-specific)
  645. # Only detect as cursor if .claude doesn't exist (to avoid confusion)
  646. if (project_root / ".cursor").is_dir() and not (project_root / ".claude").is_dir():
  647. return "cursor"
  648. # Check for .gemini directory (Gemini CLI-specific)
  649. if (project_root / ".gemini").is_dir():
  650. return "gemini"
  651. # Check for .codex directory (Codex-specific)
  652. # .agents/skills/ alone does NOT trigger codex detection (it's a shared standard)
  653. if (project_root / ".codex").is_dir() and not _has_other_platform_dir(
  654. project_root, {".codex", ".agents"}
  655. ):
  656. return "codex"
  657. # Check for .kilocode directory (Kilo-specific)
  658. if (project_root / ".kilocode").is_dir():
  659. return "kilo"
  660. # Check for Kiro skills directory only when no other platform config exists
  661. if (project_root / ".kiro" / "skills").is_dir() and not _has_other_platform_dir(
  662. project_root, {".kiro"}
  663. ):
  664. return "kiro"
  665. # Check for Antigravity workflow directory only when no other platform config exists
  666. if (
  667. project_root / ".agent" / "workflows"
  668. ).is_dir() and not _has_other_platform_dir(
  669. project_root, {".agent", ".gemini"}
  670. ):
  671. return "antigravity"
  672. # Check for Devin workflow directory only when no other platform config
  673. # exists. `.windsurf/workflows` is the legacy pre-rename path (still detected
  674. # as devin for back-compat until users migrate via `trellis update --migrate`).
  675. if (
  676. (project_root / ".devin" / "workflows").is_dir()
  677. or (project_root / ".windsurf" / "workflows").is_dir()
  678. ) and not _has_other_platform_dir(
  679. project_root, {".devin", ".windsurf"}
  680. ):
  681. return "devin"
  682. # Check for .codebuddy directory (CodeBuddy-specific)
  683. if (project_root / ".codebuddy").is_dir():
  684. return "codebuddy"
  685. # Check for .qoder directory (Qoder-specific)
  686. if (project_root / ".qoder").is_dir():
  687. return "qoder"
  688. # Check for .github/copilot directory (GitHub Copilot-specific)
  689. if (project_root / ".github" / "copilot").is_dir():
  690. return "copilot"
  691. # Check for .factory directory (Factory Droid-specific)
  692. if (project_root / ".factory").is_dir():
  693. return "droid"
  694. # Check for .pi directory (Pi Agent-specific)
  695. if (project_root / ".pi").is_dir():
  696. return "pi"
  697. # Check for .trae directory (Trae IDE-specific)
  698. if (project_root / ".trae").is_dir():
  699. return "trae"
  700. # Fallback: checkout only has the Codex shared-skills layer
  701. # (.agents/skills/trellis-* dirs) and no explicit platform config dir.
  702. # Happens on fresh clones where .codex/ is gitignored/absent but the
  703. # shared skills were committed to git. Must guard against the case
  704. # where .claude/ or any other platform dir also exists — .agents/skills/
  705. # can legitimately coexist with any platform as a shared consumption
  706. # layer for Amp/Cline/Warp/etc.
  707. agents_skills = project_root / ".agents" / "skills"
  708. if agents_skills.is_dir() and not _has_other_platform_dir(
  709. project_root, set()
  710. ):
  711. try:
  712. for entry in agents_skills.iterdir():
  713. if entry.is_dir() and entry.name.startswith("trellis-"):
  714. return "codex"
  715. except OSError:
  716. pass
  717. return "claude"
  718. def get_cli_adapter_auto(project_root: Path) -> CLIAdapter:
  719. """Get CLI adapter with auto-detected platform.
  720. Args:
  721. project_root: Project root directory
  722. Returns:
  723. CLIAdapter instance for detected platform
  724. """
  725. platform = detect_platform(project_root)
  726. return CLIAdapter(platform=platform)