use crate::{adapter, config, process::resolve};
use anyhow::Result;
use std::{ffi::OsStr, path::Path};
pub const CLAUDE: &str = "claude";
pub const AUTO: &str = "auto";
pub const ENV: &str = "AGENGO_AGENT";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
Flag,
Env,
Config,
OnlyOneInstalled,
}
impl Source {
pub fn describe(self) -> &'static str {
match self {
Self::Flag => "--agent",
Self::Env => ENV,
Self::Config => config::PATH,
Self::OnlyOneInstalled => "the only agent installed",
}
}
}
#[derive(Debug, PartialEq)]
pub enum Selection {
Chosen {
name: String,
source: Source,
},
Several(Vec<String>),
Nothing,
}
pub fn known() -> Vec<String> {
let mut names = vec![CLAUDE.to_owned()];
names.extend(super::known_agents());
names
}
pub fn program(name: &str) -> Option<String> {
if name == CLAUDE {
Some(CLAUDE.to_owned())
} else {
super::known_program(name)
}
}
pub fn installed(cwd: &Path) -> Vec<String> {
known()
.into_iter()
.filter(|name| {
program(name).is_some_and(|program| resolve(OsStr::new(&program), cwd).is_some())
})
.collect()
}
pub fn decide(
flag: Option<&str>,
env: Option<&str>,
config: Option<&str>,
installed: &[String],
) -> Selection {
for (value, source) in [
(flag, Source::Flag),
(env, Source::Env),
(config, Source::Config),
] {
if let Some(name) = value
.map(str::trim)
.filter(|name| !name.is_empty() && *name != AUTO)
{
return Selection::Chosen {
name: name.to_owned(),
source,
};
}
}
match installed {
[] => Selection::Nothing,
[only] => Selection::Chosen {
name: only.clone(),
source: Source::OnlyOneInstalled,
},
several => Selection::Several(several.to_vec()),
}
}
pub fn selection(flag: Option<&str>, root: &Path) -> Result<Selection> {
let configured = config::load(root)?.agent;
let env = std::env::var(ENV).ok();
Ok(decide(
flag,
env.as_deref(),
configured.as_deref(),
&installed(root),
))
}
pub fn show(root: &Path) -> Result<()> {
println!("[AGENTS] coding agents agengo can start");
let cwd = std::env::current_dir()?;
for name in known() {
let program = program(&name).unwrap_or_default();
match resolve(OsStr::new(&program), &cwd) {
Some(path) => println!("- {name}: found ({})", path.display()),
None => println!("- {name}: not found (agengo runs `{program}`)"),
}
}
match selection(None, root)? {
Selection::Chosen { name, source } => {
println!("- Commands will use: {name} ({})", source.describe())
}
Selection::Several(names) => println!(
"- Several are installed ({}) and none is chosen: run `agengo agent use NAME` to choose one for this repository, or pass --agent NAME.",
names.join(", ")
),
Selection::Nothing => println!(
"- None found: install Claude Code, Codex or Antigravity, or pass --agent EXECUTABLE with a custom adapter."
),
}
Ok(())
}
pub fn use_agent(root: &Path, name: &str) -> Result<()> {
if name == AUTO {
config::set_agent(root, None)?;
println!(
"agengo: {} no longer names an agent; the only agent installed is used",
config::PATH
);
return Ok(());
}
adapter::executable(Some(name))?;
config::set_agent(root, Some(name))?;
println!(
"agengo: this repository now uses {name} ({}); `agengo agent use auto` undoes it",
config::PATH
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn names(items: &[&str]) -> Vec<String> {
items.iter().map(|name| (*name).to_owned()).collect()
}
#[test]
fn the_flag_beats_the_environment_beats_the_configuration() {
let both = names(&["claude", "codex"]);
let chosen = |flag, env, config| match decide(flag, env, config, &both) {
Selection::Chosen { name, source } => (name, source),
other => panic!("{other:?}"),
};
assert_eq!(
chosen(Some("agy"), Some("codex"), Some("claude")),
("agy".into(), Source::Flag)
);
assert_eq!(
chosen(None, Some("codex"), Some("claude")),
("codex".into(), Source::Env)
);
assert_eq!(
chosen(None, None, Some("claude")),
("claude".into(), Source::Config)
);
assert_eq!(
chosen(Some(" "), Some("auto"), Some("codex")),
("codex".into(), Source::Config)
);
}
#[test]
fn without_a_choice_the_installed_agents_decide() {
assert_eq!(decide(None, None, Some("auto"), &[]), Selection::Nothing);
assert_eq!(
decide(None, None, None, &names(&["codex"])),
Selection::Chosen {
name: "codex".into(),
source: Source::OnlyOneInstalled
}
);
assert_eq!(
decide(None, None, None, &names(&["claude", "agy"])),
Selection::Several(names(&["claude", "agy"]))
);
}
#[test]
fn a_choice_is_kept_even_when_that_agent_is_not_installed() {
assert!(matches!(
decide(Some("codex"), None, None, &[]),
Selection::Chosen { .. }
));
}
#[test]
fn every_agent_has_a_program() {
for name in known() {
assert!(program(&name).is_some(), "{name}");
}
assert_eq!(program("./adapter"), None);
}
}