Skip to main content

ask/skills/
agent_detect.rs

1//! Detect which coding agents a project targets, by the presence of their
2//! marker directory. Rust port of `skills/agent-detect.ts`.
3
4use std::path::{Path, PathBuf};
5
6/// A resolved agent install target.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct AgentTarget {
9    /// Stable identifier used in the lock file, e.g. `claude`.
10    pub name: String,
11    /// Human label shown in prompts, e.g. `Claude Code`.
12    pub label: String,
13    /// Marker directory whose presence enables this agent (absolute).
14    pub marker_dir: PathBuf,
15    /// Absolute path where `skills/<skill-name>` symlinks are created.
16    pub skills_dir: PathBuf,
17}
18
19/// Static agent registry: (name, label, marker dir, skills-dir relative path).
20const AGENTS: &[(&str, &str, &str, &str)] = &[
21    ("claude", "Claude Code", ".claude", ".claude/skills"),
22    ("cursor", "Cursor", ".cursor", ".cursor/skills"),
23    ("opencode", "OpenCode", ".opencode", ".opencode/skills"),
24    ("codex", "Codex", ".codex", ".codex/skills"),
25];
26
27fn target_for(
28    project_dir: &Path,
29    name: &str,
30    label: &str,
31    marker: &str,
32    skills_rel: &str,
33) -> AgentTarget {
34    AgentTarget {
35        name: name.to_string(),
36        label: label.to_string(),
37        marker_dir: project_dir.join(marker),
38        skills_dir: project_dir.join(skills_rel),
39    }
40}
41
42/// Every supported agent whose marker directory is present under `project_dir`.
43/// `AGENTS.md` alone is NOT an agent (it is a cross-agent convention file).
44pub fn detect_agents(project_dir: &Path) -> Vec<AgentTarget> {
45    AGENTS
46        .iter()
47        .filter(|(_, _, marker, _)| project_dir.join(marker).exists())
48        .map(|(name, label, marker, skills_rel)| {
49            target_for(project_dir, name, label, marker, skills_rel)
50        })
51        .collect()
52}
53
54/// Resolve a user-supplied `--agent` list into targets WITHOUT requiring the
55/// marker dir to exist. Unknown names error (loud failure over silently
56/// installing into an unintended location). Parity with `resolveAgentNames`.
57pub fn resolve_agent_names(
58    project_dir: &Path,
59    names: &[String],
60) -> anyhow::Result<Vec<AgentTarget>> {
61    names
62        .iter()
63        .map(|name| {
64            AGENTS
65                .iter()
66                .find(|(n, _, _, _)| *n == name)
67                .map(|(n, label, marker, skills_rel)| {
68                    target_for(project_dir, n, label, marker, skills_rel)
69                })
70                .ok_or_else(|| {
71                    anyhow::anyhow!(
72                        "unknown agent '{name}'. Supported: {}",
73                        supported_agents().join(", ")
74                    )
75                })
76        })
77        .collect()
78}
79
80/// The supported agent identifiers, in registry order.
81pub fn supported_agents() -> Vec<&'static str> {
82    AGENTS.iter().map(|(n, _, _, _)| *n).collect()
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn detects_only_present_markers() {
91        let dir = tempfile::tempdir().unwrap();
92        std::fs::create_dir_all(dir.path().join(".claude")).unwrap();
93        std::fs::create_dir_all(dir.path().join(".cursor")).unwrap();
94        let found = detect_agents(dir.path());
95        let names: Vec<&str> = found.iter().map(|a| a.name.as_str()).collect();
96        assert_eq!(names, vec!["claude", "cursor"]);
97        assert_eq!(found[0].skills_dir, dir.path().join(".claude/skills"));
98    }
99
100    #[test]
101    fn resolve_names_does_not_require_marker() {
102        let dir = tempfile::tempdir().unwrap();
103        let got = resolve_agent_names(dir.path(), &["codex".to_string()]).unwrap();
104        assert_eq!(got.len(), 1);
105        assert_eq!(got[0].name, "codex");
106        assert_eq!(got[0].skills_dir, dir.path().join(".codex/skills"));
107    }
108
109    #[test]
110    fn resolve_unknown_name_errors() {
111        let dir = tempfile::tempdir().unwrap();
112        assert!(resolve_agent_names(dir.path(), &["bogus".to_string()]).is_err());
113    }
114
115    #[test]
116    fn supported_agents_order() {
117        assert_eq!(
118            supported_agents(),
119            vec!["claude", "cursor", "opencode", "codex"]
120        );
121    }
122}