Skip to main content

orch/engine/agents/
registry.rs

1//! What orch knows about each agent name. Several names run the same CLI, so a fact about a
2//! name is a row here, and a fact about an output format is a method on its [`AgentRunner`]
3
4use 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/// The runner type that builds an agent's command line and reads its output
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15enum Cli {
16    Claude,
17    Kimi,
18    MiniMax,
19    Codex,
20    OpenCode,
21    Grok,
22}
23
24/// One agent name and the facts that belong to the name
25#[derive(Debug)]
26pub struct AgentSpec {
27    pub name: &'static str,
28    cli: Cli,
29    /// The CLI takes `--session-id` for a new session and `--resume` to continue it
30    pub sessions: bool,
31    /// It runs Claude Code's CLI, and so honours a tool allowlist
32    pub claude_compatible: bool,
33    /// Env vars that hold the login, so a set one counts as signed in
34    pub login_env: &'static [&'static str],
35    /// Arguments of a status command that exits without opening a session
36    pub login_status: Option<&'static [&'static str]>,
37    /// Credential files, each as path segments under the home directory
38    credential_files: &'static [&'static [&'static str]],
39    /// A folder under `$XDG_DATA_HOME` and `$XDG_CONFIG_HOME` that also holds an `auth.json`
40    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    /// The credential files under `home`
121    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
144/// The row for `name`, `None` for a name orch does not know
145pub fn spec(name: &str) -> Option<&'static AgentSpec> {
146    AGENTS.iter().find(|spec| spec.name == name)
147}
148
149/// The runner for `name`. An unknown name gets the Claude Code runner under its own name
150pub 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}