orch-cli 0.118.0

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
//! What orch knows about each agent name. Several names run the same CLI, so a fact about a
//! name is a row here, and a fact about an output format is a method on its [`AgentRunner`]

use super::AgentRunner;
use super::claude::ClaudeRunner;
use super::codex::CodexRunner;
use super::grok::GrokRunner;
use super::kimi::KimiClaudeRunner;
use super::minimax::MiniMaxClaudeRunner;
use super::opencode::OpenCodeRunner;
use std::path::{Path, PathBuf};

/// The runner type that builds an agent's command line and reads its output
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Cli {
    Claude,
    Kimi,
    MiniMax,
    Codex,
    OpenCode,
    Grok,
}

/// One agent name and the facts that belong to the name
#[derive(Debug)]
pub struct AgentSpec {
    pub name: &'static str,
    cli: Cli,
    /// The CLI takes `--session-id` for a new session and `--resume` to continue it
    pub sessions: bool,
    /// It runs Claude Code's CLI, and so honours a tool allowlist
    pub claude_compatible: bool,
    /// Env vars that hold the login, so a set one counts as signed in
    pub login_env: &'static [&'static str],
    /// Arguments of a status command that exits without opening a session
    pub login_status: Option<&'static [&'static str]>,
    /// Credential files, each as path segments under the home directory
    credential_files: &'static [&'static [&'static str]],
    /// A folder under `$XDG_DATA_HOME` and `$XDG_CONFIG_HOME` that also holds an `auth.json`
    pub xdg_auth_dir: Option<&'static str>,
}

const NO_LOGIN: AgentSpec = AgentSpec {
    name: "",
    cli: Cli::Claude,
    sessions: false,
    claude_compatible: false,
    login_env: &[],
    login_status: None,
    credential_files: &[],
    xdg_auth_dir: None,
};

const AGENTS: &[AgentSpec] = &[
    AgentSpec {
        name: "claude",
        sessions: true,
        claude_compatible: true,
        login_env: &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"],
        login_status: Some(&["auth", "status", "--text"]),
        credential_files: &[&[".claude", ".credentials.json"]],
        ..NO_LOGIN
    },
    AgentSpec {
        name: "kimi",
        cli: Cli::Kimi,
        sessions: true,
        claude_compatible: true,
        login_env: &["KIMI_API_KEY"],
        ..NO_LOGIN
    },
    AgentSpec {
        name: "minimax",
        cli: Cli::MiniMax,
        sessions: true,
        claude_compatible: true,
        login_env: &["MINIMAX_API_KEY"],
        ..NO_LOGIN
    },
    AgentSpec {
        name: "olm",
        claude_compatible: true,
        ..NO_LOGIN
    },
    AgentSpec {
        name: "glm",
        claude_compatible: true,
        login_env: &["GLM_API_KEY"],
        ..NO_LOGIN
    },
    AgentSpec {
        name: "codex",
        cli: Cli::Codex,
        login_env: &["OPENAI_API_KEY", "CODEX_API_KEY", "CODEX_ACCESS_TOKEN"],
        login_status: Some(&["login", "status"]),
        credential_files: &[&[".codex", "auth.json"]],
        ..NO_LOGIN
    },
    AgentSpec {
        name: "opencode",
        cli: Cli::OpenCode,
        credential_files: &[
            &[".local", "share", "opencode", "auth.json"],
            &[".config", "opencode", "auth.json"],
        ],
        xdg_auth_dir: Some("opencode"),
        ..NO_LOGIN
    },
    AgentSpec {
        name: "grok",
        cli: Cli::Grok,
        sessions: true,
        login_env: &["XAI_API_KEY"],
        credential_files: &[&[".grok", "auth.json"]],
        ..NO_LOGIN
    },
];

impl AgentSpec {
    /// The credential files under `home`
    pub fn credential_paths(&self, home: &Path) -> Vec<PathBuf> {
        self.credential_files
            .iter()
            .map(|segments| {
                segments
                    .iter()
                    .fold(home.to_path_buf(), |path, s| path.join(s))
            })
            .collect()
    }

    fn runner(&self) -> Box<dyn AgentRunner> {
        match self.cli {
            Cli::Claude => Box::new(ClaudeRunner::new(self.name)),
            Cli::Kimi => Box::new(KimiClaudeRunner::new()),
            Cli::MiniMax => Box::new(MiniMaxClaudeRunner::new(self.name)),
            Cli::Codex => Box::new(CodexRunner),
            Cli::OpenCode => Box::new(OpenCodeRunner::new()),
            Cli::Grok => Box::new(GrokRunner),
        }
    }
}

/// The row for `name`, `None` for a name orch does not know
pub fn spec(name: &str) -> Option<&'static AgentSpec> {
    AGENTS.iter().find(|spec| spec.name == name)
}

/// The runner for `name`. An unknown name gets the Claude Code runner under its own name
pub fn runner(name: &str) -> Box<dyn AgentRunner> {
    if let Some(spec) = spec(name) {
        return spec.runner();
    }
    tracing::warn!(
        agent = name,
        "unknown agent, using claude-compatible runner"
    );
    Box::new(ClaudeRunner::new(name))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_name_is_listed_once() {
        for spec in AGENTS {
            let count = AGENTS
                .iter()
                .filter(|other| other.name == spec.name)
                .count();
            assert_eq!(count, 1, "{} is listed {count} times", spec.name);
            assert!(!spec.name.is_empty(), "a row has no name");
        }
    }

    #[test]
    fn a_known_name_gets_its_own_runner() {
        for spec in AGENTS {
            assert_eq!(runner(spec.name).name(), spec.name);
        }
        assert_eq!(runner("unknown-agent").name(), "unknown-agent");
    }

    #[test]
    fn an_unknown_name_has_no_row() {
        assert!(spec("unknown-agent").is_none());
        assert!(spec("").is_none());
    }
}