| 46 | } |
| 47 | |
| 48 | func detectWith(lookup func(string) (string, bool)) AgentName { |
| 49 | isSet := func(key string) bool { |
| 50 | v, ok := lookup(key) |
| 51 | return ok && v != "" |
| 52 | } |
| 53 | |
| 54 | valueOf := func(key string) string { |
| 55 | v, _ := lookup(key) |
| 56 | return v |
| 57 | } |
| 58 | |
| 59 | // Generic agent identifiers - checked first because they are the most specific signal. |
| 60 | if v, ok := lookup("AI_AGENT"); ok && v != "" { |
| 61 | if name, err := parseAgentName(v); err == nil { |
| 62 | return name |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Tool-specific variables. |
| 67 | |
| 68 | // Check AGENT=amp before the more generic CLAUDECODE=1 since Amp sets both. |
| 69 | if valueOf("AGENT") == "amp" { |
| 70 | return agentAmp |
| 71 | } |
| 72 | |
| 73 | // OpenAI Codex CLI - https://github.com/openai/codex |
| 74 | // CODEX_SANDBOX: https://github.com/openai/codex/blob/95e1d5993985019ce0ce0d10689caf1375f95120/codex-rs/core/src/spawn.rs#L25 |
| 75 | // CODEX_THREAD_ID: https://github.com/openai/codex/blob/95e1d5993985019ce0ce0d10689caf1375f95120/codex-rs/core/src/exec_env.rs#L8 |
| 76 | // CODEX_CI: https://github.com/openai/codex/blob/95e1d5993985019ce0ce0d10689caf1375f95120/codex-rs/core/src/unified_exec/process_manager.rs#L64 |
| 77 | if isSet("CODEX_SANDBOX") || isSet("CODEX_CI") || isSet("CODEX_THREAD_ID") { |
| 78 | return agentCodex |
| 79 | } |
| 80 | |
| 81 | // Google Gemini CLI - https://github.com/google-gemini/gemini-cli |
| 82 | // GEMINI_CLI: https://github.com/google-gemini/gemini-cli/blob/46fd7b4864111032a1c7dfa1821b2000fc7531da/docs/tools/shell.md#L96-L97 |
| 83 | if isSet("GEMINI_CLI") { |
| 84 | return agentGeminiCLI |
| 85 | } |
| 86 | |
| 87 | // GitHub Copilot CLI |
| 88 | // No first-party docs |
| 89 | if isSet("COPILOT_CLI") { |
| 90 | return agentCopilotCLI |
| 91 | } |
| 92 | |
| 93 | // OpenCode - https://github.com/anomalyco/opencode |
| 94 | // OPENCODE: https://github.com/anomalyco/opencode/blob/fde201c286a83ff32dda9b41d61d734a4449fe70/packages/opencode/src/index.ts#L78-L80 |
| 95 | // Not OPENCODE_CALLER or OPENCODE_CLIENT: they name the client that launched |
| 96 | // opencode (e.g. the VS Code extension), not the running agent. |
| 97 | if isSet("OPENCODE") { |
| 98 | return agentOpencode |
| 99 | } |
| 100 | |
| 101 | // Antigravity |
| 102 | // No first-party docs |
| 103 | if isSet("ANTIGRAVITY_AGENT") { |
| 104 | return agentAntigravity |
| 105 | } |