use crate::discovery::profile::{Logo, LogoSource, Profile};
use std::collections::BTreeMap;
use std::path::{Component, Path, PathBuf};
use std::sync::OnceLock;
use serde::{Deserialize, Serialize};
use super::launch::Launch;
pub const INDEX_URL: &str = "https://cdn.agentclientprotocol.com/registry/v1/latest/registry.json";
const BUNDLED: &str = include_str!("registry.json");
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Registry {
pub version: String,
pub agents: Vec<Entry>,
}
impl Registry {
pub fn parse(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
pub fn bundled() -> &'static Registry {
static REGISTRY: OnceLock<Registry> = OnceLock::new();
REGISTRY.get_or_init(|| Registry::parse(BUNDLED).expect("the bundled registry parses"))
}
pub fn get(&self, id: &str) -> Option<&Entry> {
self.agents.iter().find(|entry| entry.id == id)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Entry {
pub id: String,
pub name: String,
pub version: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub repository: Option<String>,
#[serde(default)]
pub website: Option<String>,
#[serde(default)]
pub authors: Vec<String>,
#[serde(default)]
pub license: Option<String>,
#[serde(default, rename = "license_url")]
pub license_url: Option<String>,
#[serde(default)]
pub icon: Option<String>,
pub distribution: Distribution,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Distribution {
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub binary: BTreeMap<String, Binary>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub npx: Option<Package>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub uvx: Option<Package>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Binary {
pub archive: String,
#[serde(default)]
pub sha256: Option<String>,
pub cmd: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub env: BTreeMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Package {
pub package: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub env: BTreeMap<String, String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum Method {
Binary,
Npx,
Uvx,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum LaunchError {
#[error("{agent} is not distributed through {method:?}")]
NotDistributed { agent: String, method: Method },
#[error("{agent} has no binary for this platform ({platform})")]
NoBinaryForPlatform { agent: String, platform: String },
#[error("{agent}'s binary needs an install folder")]
NoInstallDir { agent: String },
#[error("{agent}'s command {cmd:?} would leave its install folder")]
UnsafeCommand { agent: String, cmd: String },
#[error("{agent} cannot be started here: {detail}")]
Unavailable { agent: String, detail: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Availability {
pub method: Method,
pub ready: bool,
pub path: Option<PathBuf>,
pub detail: Option<String>,
}
pub fn platform() -> Option<&'static str> {
let os = match std::env::consts::OS {
"linux" => "linux",
"macos" => "darwin",
"windows" => "windows",
_ => return None,
};
let arch = match std::env::consts::ARCH {
"x86_64" => "x86_64",
"aarch64" => "aarch64",
_ => return None,
};
Some(match (os, arch, cfg!(target_env = "musl")) {
("linux", "x86_64", true) => "linux-x86_64-musl",
("linux", "aarch64", true) => "linux-aarch64-musl",
("linux", "x86_64", false) => "linux-x86_64",
("linux", "aarch64", false) => "linux-aarch64",
("darwin", "x86_64", _) => "darwin-x86_64",
("darwin", "aarch64", _) => "darwin-aarch64",
("windows", "x86_64", _) => "windows-x86_64",
_ => "windows-aarch64",
})
}
impl Entry {
pub fn methods(&self, platform: Option<&str>) -> Vec<Method> {
let mut methods = Vec::new();
if platform.is_some_and(|platform| self.binary_for(platform).is_some()) {
methods.push(Method::Binary);
}
if self.distribution.npx.is_some() {
methods.push(Method::Npx);
}
if self.distribution.uvx.is_some() {
methods.push(Method::Uvx);
}
methods
}
pub fn binary_for(&self, platform: &str) -> Option<&Binary> {
let binaries = &self.distribution.binary;
binaries.get(platform).or_else(|| {
platform
.strip_suffix("-musl")
.and_then(|glibc| binaries.get(glibc))
})
}
pub fn install_dir(&self, root: &Path) -> PathBuf {
root.join(sanitize(&self.id)).join(sanitize(&self.version))
}
pub fn launch(
&self,
method: Method,
install_dir: Option<&Path>,
) -> Result<Launch, LaunchError> {
self.launch_on(method, platform(), install_dir)
}
fn launch_on(
&self,
method: Method,
platform: Option<&str>,
install_dir: Option<&Path>,
) -> Result<Launch, LaunchError> {
let not_distributed = || LaunchError::NotDistributed {
agent: self.id.clone(),
method,
};
let package = |package: &Option<Package>, program: &str, prefix: &[&str]| {
let package = package.as_ref().ok_or_else(not_distributed)?;
let mut launch = Launch::new(program)
.args(prefix.iter().copied())
.args([package.package.as_str()])
.args(package.args.iter().cloned());
for (name, value) in &package.env {
launch = launch.env(name, value);
}
Ok(launch)
};
match method {
Method::Npx => package(&self.distribution.npx, "npx", &["-y"]),
Method::Uvx => package(&self.distribution.uvx, "uvx", &[]),
Method::Binary => {
if self.distribution.binary.is_empty() {
return Err(not_distributed());
}
let platform = platform.unwrap_or("unknown");
let binary =
self.binary_for(platform)
.ok_or_else(|| LaunchError::NoBinaryForPlatform {
agent: self.id.clone(),
platform: platform.to_owned(),
})?;
let dir = install_dir.ok_or_else(|| LaunchError::NoInstallDir {
agent: self.id.clone(),
})?;
let program =
binary_path(dir, &binary.cmd).ok_or_else(|| LaunchError::UnsafeCommand {
agent: self.id.clone(),
cmd: binary.cmd.clone(),
})?;
let mut launch = Launch::new(program).args(binary.args.iter().cloned());
for (name, value) in &binary.env {
launch = launch.env(name, value);
}
Ok(launch)
}
}
}
pub async fn availability(&self, install_dir: Option<&Path>) -> Vec<Availability> {
let mut found = Vec::new();
for method in self.methods(platform()) {
found.push(self.check(method, install_dir).await);
}
found
}
async fn check(&self, method: Method, install_dir: Option<&Path>) -> Availability {
let launch = match self.launch(method, install_dir) {
Ok(launch) => launch,
Err(error) => {
return Availability {
method,
ready: false,
path: None,
detail: Some(error.to_string()),
};
}
};
let path = match method {
Method::Binary => crate::core::discovery::executable::is_executable(&launch.program)
.then(|| launch.program.clone()),
Method::Npx | Method::Uvx => {
crate::core::discovery::executable::resolve(&launch.program).await
}
};
let detail = path.is_none().then(|| match method {
Method::Binary => format!("{} is not installed", launch.program.display()),
_ => format!("`{}` was not found", launch.program.display()),
});
Availability {
method,
ready: path.is_some(),
path,
detail,
}
}
pub async fn resolve(&self, install_dir: Option<&Path>) -> Result<Launch, LaunchError> {
let checks = self.availability(install_dir).await;
if let Some(ready) = checks.iter().find(|check| check.ready) {
let mut launch = self.launch(ready.method, install_dir)?;
if let Some(path) = &ready.path {
launch.program.clone_from(path);
}
return Ok(launch);
}
let detail = if checks.is_empty() {
"no distribution for this platform".to_owned()
} else {
checks
.iter()
.filter_map(|check| check.detail.clone())
.collect::<Vec<_>>()
.join("; ")
};
Err(LaunchError::Unavailable {
agent: self.id.clone(),
detail,
})
}
}
fn binary_path(dir: &Path, cmd: &str) -> Option<PathBuf> {
let relative = Path::new(cmd);
let mut path = dir.to_path_buf();
for component in relative.components() {
match component {
Component::CurDir => {}
Component::Normal(part) => path.push(part),
_ => return None,
}
}
(path != dir).then_some(path)
}
fn sanitize(text: &str) -> String {
let clean: String = text
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || "-_.+@".contains(c) {
c
} else {
'_'
}
})
.collect();
if clean.is_empty() || clean.chars().all(|c| c == '.') {
"_".to_owned()
} else {
clean
}
}
impl Entry {
pub fn profile(&self) -> Profile {
Profile {
id: self.id.clone(),
name: self.name.clone(),
maker: (!self.authors.is_empty()).then(|| self.authors.join(", ")),
description: self.description.clone(),
homepage: self.website.clone(),
repository: self.repository.clone(),
logo: Logo {
source: match &self.icon {
Some(url) => LogoSource::Url(url.clone()),
None => LogoSource::Svg(String::new()),
},
monochrome: true,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn registry_agents_name_their_logo() {
let gemini = Registry::bundled()
.get("gemini")
.expect("gemini is in the bundled registry")
.profile();
assert_eq!(gemini.name, "Gemini CLI");
assert!(gemini.logo.monochrome);
}
const SAMPLE: &str = r#"{
"version": "1.0.0",
"agents": [
{
"id": "both", "name": "Both", "version": "1.2.3",
"description": "Binary and npx", "repository": "https://example.com/both",
"authors": ["A"], "license": "MIT", "icon": "https://example.com/icon.svg",
"distribution": {
"binary": {
"linux-x86_64": { "archive": "https://example.com/b.tar.gz", "sha256": "ab", "cmd": "./bin/both", "args": ["acp"], "env": { "MODE": "acp" } },
"darwin-aarch64": { "archive": "https://example.com/b.zip", "cmd": "both" }
},
"npx": { "package": "@example/both@1.2.3", "args": ["--acp"], "env": { "NO_UPDATE": "1" } }
},
"unknownField": true
},
{
"id": "py", "name": "Py", "version": "0.1.0",
"distribution": { "uvx": { "package": "py-agent==0.1.0", "args": ["acp"] } }
},
{
"id": "evil", "name": "Evil", "version": "1",
"distribution": { "binary": { "linux-x86_64": { "archive": "x", "cmd": "../../bin/sh" } } }
}
],
"extensions": []
}"#;
#[test]
fn parses_entries_and_ignores_unknown_fields() {
let registry = Registry::parse(SAMPLE).unwrap();
assert_eq!(registry.version, "1.0.0");
let both = registry.get("both").unwrap();
assert_eq!(both.license.as_deref(), Some("MIT"));
let binary = both.binary_for("linux-x86_64").unwrap();
assert_eq!(binary.sha256.as_deref(), Some("ab"));
assert_eq!(binary.env["MODE"], "acp");
assert_eq!(
both.binary_for("linux-x86_64-musl"),
Some(binary),
"musl falls back to glibc"
);
assert_eq!(both.binary_for("windows-x86_64"), None);
let py = registry.get("py").unwrap();
assert_eq!(py.description, "");
assert!(py.distribution.binary.is_empty());
assert!(Registry::parse("{}").is_err());
}
#[test]
fn package_launches() {
let registry = Registry::parse(SAMPLE).unwrap();
let npx = registry
.get("both")
.unwrap()
.launch(Method::Npx, None)
.unwrap();
assert_eq!(npx.program, PathBuf::from("npx"));
assert_eq!(npx.args, ["-y", "@example/both@1.2.3", "--acp"]);
assert_eq!(npx.env, [("NO_UPDATE".to_owned(), "1".to_owned())]);
let uvx = registry
.get("py")
.unwrap()
.launch(Method::Uvx, None)
.unwrap();
assert_eq!(uvx.program, PathBuf::from("uvx"));
assert_eq!(uvx.args, ["py-agent==0.1.0", "acp"]);
assert_eq!(
registry.get("py").unwrap().launch(Method::Npx, None),
Err(LaunchError::NotDistributed {
agent: "py".into(),
method: Method::Npx
})
);
}
#[test]
fn binary_launches_stay_in_their_install_folder() {
let registry = Registry::parse(SAMPLE).unwrap();
let both = registry.get("both").unwrap();
let dir = Path::new("/opt/agents/both/1.2.3");
let launch = both
.launch_on(Method::Binary, Some("linux-x86_64"), Some(dir))
.unwrap();
assert_eq!(launch.program, dir.join("bin/both"));
assert_eq!(launch.args, ["acp"]);
assert_eq!(
both.launch_on(Method::Binary, Some("linux-x86_64"), None),
Err(LaunchError::NoInstallDir {
agent: "both".into()
})
);
assert!(matches!(
both.launch_on(Method::Binary, Some("windows-x86_64"), Some(dir)),
Err(LaunchError::NoBinaryForPlatform { .. })
));
let evil = registry.get("evil").unwrap();
assert!(matches!(
evil.launch_on(Method::Binary, Some("linux-x86_64"), Some(dir)),
Err(LaunchError::UnsafeCommand { .. })
));
assert_eq!(binary_path(dir, "/bin/sh"), None);
assert_eq!(binary_path(dir, "."), None);
assert_eq!(
both.install_dir(Path::new("/opt/agents")),
Path::new("/opt/agents/both/1.2.3")
);
assert_eq!(sanitize(".."), "_");
assert_eq!(sanitize("a/b"), "a_b");
}
#[test]
fn methods_follow_the_preferred_order() {
let registry = Registry::parse(SAMPLE).unwrap();
let both = registry.get("both").unwrap();
assert_eq!(
both.methods(Some("linux-x86_64")),
[Method::Binary, Method::Npx]
);
assert_eq!(both.methods(Some("windows-x86_64")), [Method::Npx]);
assert_eq!(both.methods(None), [Method::Npx]);
}
#[tokio::test]
async fn an_installed_binary_is_available() {
let registry = Registry::parse(SAMPLE).unwrap();
let both = registry.get("both").unwrap();
let root = std::env::temp_dir().join(format!("stubb-registry-{}", std::process::id()));
let dir = both.install_dir(&root);
let binary = both.check(Method::Binary, Some(&dir)).await;
if platform() == Some("linux-x86_64") {
assert!(!binary.ready);
std::fs::create_dir_all(dir.join("bin")).unwrap();
#[cfg(unix)]
crate::process::write_executable(&dir.join("bin/both"), "#!/bin/sh\n");
let binary = both.check(Method::Binary, Some(&dir)).await;
assert!(binary.ready, "{binary:?}");
let launch = both.resolve(Some(&dir)).await.unwrap();
assert_eq!(launch.program, dir.join("bin/both"));
}
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_bundled_registry_parses() {
let registry = Registry::bundled();
assert!(registry.agents.len() >= 40);
assert!(registry.agents.iter().all(|entry| {
!entry.methods(Some("linux-x86_64")).is_empty()
|| !entry.methods(Some("darwin-aarch64")).is_empty()
}));
let claude = registry.get("claude-acp").unwrap();
assert!(claude.distribution.npx.is_some());
assert!(registry.agents.iter().all(|entry| entry.icon.is_none()));
}
}