1use super::AgentRunner;
5use super::claude::ClaudeRunner;
6use super::codex::CodexRunner;
7use super::grok::GrokRunner;
8use super::kimi::KimiClaudeRunner;
9use super::minimax::MiniMaxClaudeRunner;
10use super::opencode::OpenCodeRunner;
11use std::path::{Path, PathBuf};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15enum Cli {
16 Claude,
17 Kimi,
18 MiniMax,
19 Codex,
20 OpenCode,
21 Grok,
22}
23
24#[derive(Debug)]
26pub struct AgentSpec {
27 pub name: &'static str,
28 cli: Cli,
29 pub sessions: bool,
31 pub claude_compatible: bool,
33 pub login_env: &'static [&'static str],
35 pub login_status: Option<&'static [&'static str]>,
37 credential_files: &'static [&'static [&'static str]],
39 pub xdg_auth_dir: Option<&'static str>,
41}
42
43const NO_LOGIN: AgentSpec = AgentSpec {
44 name: "",
45 cli: Cli::Claude,
46 sessions: false,
47 claude_compatible: false,
48 login_env: &[],
49 login_status: None,
50 credential_files: &[],
51 xdg_auth_dir: None,
52};
53
54const AGENTS: &[AgentSpec] = &[
55 AgentSpec {
56 name: "claude",
57 sessions: true,
58 claude_compatible: true,
59 login_env: &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"],
60 login_status: Some(&["auth", "status", "--text"]),
61 credential_files: &[&[".claude", ".credentials.json"]],
62 ..NO_LOGIN
63 },
64 AgentSpec {
65 name: "kimi",
66 cli: Cli::Kimi,
67 sessions: true,
68 claude_compatible: true,
69 login_env: &["KIMI_API_KEY"],
70 ..NO_LOGIN
71 },
72 AgentSpec {
73 name: "minimax",
74 cli: Cli::MiniMax,
75 sessions: true,
76 claude_compatible: true,
77 login_env: &["MINIMAX_API_KEY"],
78 ..NO_LOGIN
79 },
80 AgentSpec {
81 name: "olm",
82 claude_compatible: true,
83 ..NO_LOGIN
84 },
85 AgentSpec {
86 name: "glm",
87 claude_compatible: true,
88 login_env: &["GLM_API_KEY"],
89 ..NO_LOGIN
90 },
91 AgentSpec {
92 name: "codex",
93 cli: Cli::Codex,
94 login_env: &["OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_ACCESS_TOKEN"],
95 login_status: Some(&["login", "status"]),
96 credential_files: &[&[".codex", "auth.json"]],
97 ..NO_LOGIN
98 },
99 AgentSpec {
100 name: "opencode",
101 cli: Cli::OpenCode,
102 credential_files: &[
103 &[".local", "share", "opencode", "auth.json"],
104 &[".config", "opencode", "auth.json"],
105 ],
106 xdg_auth_dir: Some("opencode"),
107 ..NO_LOGIN
108 },
109 AgentSpec {
110 name: "grok",
111 cli: Cli::Grok,
112 sessions: true,
113 login_env: &["XAI_API_KEY"],
114 credential_files: &[&[".grok", "auth.json"]],
115 ..NO_LOGIN
116 },
117];
118
119impl AgentSpec {
120 pub fn credential_paths(&self, home: &Path) -> Vec<PathBuf> {
122 self.credential_files
123 .iter()
124 .map(|segments| {
125 segments
126 .iter()
127 .fold(home.to_path_buf(), |path, s| path.join(s))
128 })
129 .collect()
130 }
131
132 fn runner(&self) -> Box<dyn AgentRunner> {
133 match self.cli {
134 Cli::Claude => Box::new(ClaudeRunner::new(self.name)),
135 Cli::Kimi => Box::new(KimiClaudeRunner::new()),
136 Cli::MiniMax => Box::new(MiniMaxClaudeRunner::new(self.name)),
137 Cli::Codex => Box::new(CodexRunner),
138 Cli::OpenCode => Box::new(OpenCodeRunner::new()),
139 Cli::Grok => Box::new(GrokRunner),
140 }
141 }
142}
143
144pub fn spec(name: &str) -> Option<&'static AgentSpec> {
146 AGENTS.iter().find(|spec| spec.name == name)
147}
148
149pub fn runner(name: &str) -> Box<dyn AgentRunner> {
151 if let Some(spec) = spec(name) {
152 return spec.runner();
153 }
154 tracing::warn!(
155 agent = name,
156 "unknown agent, using claude-compatible runner"
157 );
158 Box::new(ClaudeRunner::new(name))
159}
160
161#[cfg(test)]
162mod tests {
163 use super::*;
164
165 #[test]
166 fn every_name_is_listed_once() {
167 for spec in AGENTS {
168 let count = AGENTS
169 .iter()
170 .filter(|other| other.name == spec.name)
171 .count();
172 assert_eq!(count, 1, "{} is listed {count} times", spec.name);
173 assert!(!spec.name.is_empty(), "a row has no name");
174 }
175 }
176
177 #[test]
178 fn a_known_name_gets_its_own_runner() {
179 for spec in AGENTS {
180 assert_eq!(runner(spec.name).name(), spec.name);
181 }
182 assert_eq!(runner("unknown-agent").name(), "unknown-agent");
183 }
184
185 #[test]
186 fn an_unknown_name_has_no_row() {
187 assert!(spec("unknown-agent").is_none());
188 assert!(spec("").is_none());
189 }
190}