Skip to main content

tracevault_cli/
agent.rs

1//! The AI coding agent a hook invocation belongs to. Carries the two
2//! agent-specific values stamped on an outgoing `StreamEventRequest`:
3//! the `tool` name (which the server keys its adapter off) and the
4//! `protocol_version`. Everything else in the capture path is agent-agnostic.
5
6#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
7pub enum Agent {
8    ClaudeCode,
9    Codex,
10    /// GSD (Get Shit Done) — runs on the pi coding-agent framework.
11    #[value(alias = "gsd2")]
12    Gsd,
13    /// OpenCode.
14    // Named explicitly: clap's default kebab-case derivation would otherwise
15    // split the CamelCase word boundary into `open-code`, not the single-word
16    // `opencode` the server/CLI convention expects.
17    #[value(name = "opencode")]
18    OpenCode,
19    /// Amp (ampcode.com).
20    Amp,
21}
22
23impl Agent {
24    /// The `tool` value stamped on the stream request. The server selects its
25    /// per-agent adapter from this string.
26    pub fn tool_name(&self) -> &'static str {
27        match self {
28            Agent::ClaudeCode => "claude-code",
29            Agent::Codex => "codex",
30            Agent::Gsd => "gsd",
31            Agent::OpenCode => "opencode",
32            Agent::Amp => "amp",
33        }
34    }
35
36    /// Wire protocol version. Claude Code stays on v1 (behaviour-preserving);
37    /// Codex uses v2, where the server honours the explicit `tool`.
38    pub fn protocol_version(&self) -> u8 {
39        match self {
40            Agent::ClaudeCode => 1,
41            Agent::Codex => 2,
42            Agent::Gsd => 2,
43            Agent::OpenCode => 2,
44            Agent::Amp => 2,
45        }
46    }
47
48    /// Human-readable label for CLI output. Preserves the original wording so
49    /// the default Claude Code path prints identically to before.
50    pub fn label(&self) -> &'static str {
51        match self {
52            Agent::ClaudeCode => "Claude Code",
53            Agent::Codex => "Codex",
54            Agent::Gsd => "GSD",
55            Agent::OpenCode => "OpenCode",
56            Agent::Amp => "Amp",
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use clap::ValueEnum;
65
66    #[test]
67    fn tool_name_and_protocol_version() {
68        assert_eq!(Agent::ClaudeCode.tool_name(), "claude-code");
69        assert_eq!(Agent::ClaudeCode.protocol_version(), 1);
70        assert_eq!(Agent::Codex.tool_name(), "codex");
71        assert_eq!(Agent::Codex.protocol_version(), 2);
72    }
73
74    #[test]
75    fn value_enum_parses_kebab_case() {
76        assert_eq!(
77            Agent::from_str("claude-code", true).unwrap(),
78            Agent::ClaudeCode
79        );
80        assert_eq!(Agent::from_str("codex", true).unwrap(), Agent::Codex);
81        assert!(Agent::from_str("cursor", true).is_err());
82    }
83
84    #[test]
85    fn label_wording() {
86        assert_eq!(Agent::ClaudeCode.label(), "Claude Code");
87        assert_eq!(Agent::Codex.label(), "Codex");
88    }
89
90    #[test]
91    fn gsd_agent_mapping() {
92        assert_eq!(Agent::Gsd.tool_name(), "gsd");
93        assert_eq!(Agent::Gsd.protocol_version(), 2);
94        assert_eq!(Agent::Gsd.label(), "GSD");
95    }
96
97    #[test]
98    fn gsd_parses_from_str_and_alias() {
99        use clap::ValueEnum;
100        assert_eq!(Agent::from_str("gsd", true).unwrap(), Agent::Gsd);
101        assert_eq!(Agent::from_str("gsd2", true).unwrap(), Agent::Gsd);
102    }
103
104    #[test]
105    fn opencode_agent_mapping() {
106        assert_eq!(Agent::OpenCode.tool_name(), "opencode");
107        assert_eq!(Agent::OpenCode.protocol_version(), 2);
108        assert_eq!(Agent::OpenCode.label(), "OpenCode");
109        use clap::ValueEnum;
110        assert_eq!(Agent::from_str("opencode", true).unwrap(), Agent::OpenCode);
111    }
112
113    #[test]
114    fn amp_agent_mapping() {
115        assert_eq!(Agent::Amp.tool_name(), "amp");
116        assert_eq!(Agent::Amp.protocol_version(), 2);
117        assert_eq!(Agent::Amp.label(), "Amp");
118        use clap::ValueEnum;
119        assert_eq!(Agent::from_str("amp", true).unwrap(), Agent::Amp);
120    }
121}