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}
20
21impl Agent {
22    /// The `tool` value stamped on the stream request. The server selects its
23    /// per-agent adapter from this string.
24    pub fn tool_name(&self) -> &'static str {
25        match self {
26            Agent::ClaudeCode => "claude-code",
27            Agent::Codex => "codex",
28            Agent::Gsd => "gsd",
29            Agent::OpenCode => "opencode",
30        }
31    }
32
33    /// Wire protocol version. Claude Code stays on v1 (behaviour-preserving);
34    /// Codex uses v2, where the server honours the explicit `tool`.
35    pub fn protocol_version(&self) -> u8 {
36        match self {
37            Agent::ClaudeCode => 1,
38            Agent::Codex => 2,
39            Agent::Gsd => 2,
40            Agent::OpenCode => 2,
41        }
42    }
43
44    /// Human-readable label for CLI output. Preserves the original wording so
45    /// the default Claude Code path prints identically to before.
46    pub fn label(&self) -> &'static str {
47        match self {
48            Agent::ClaudeCode => "Claude Code",
49            Agent::Codex => "Codex",
50            Agent::Gsd => "GSD",
51            Agent::OpenCode => "OpenCode",
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use clap::ValueEnum;
60
61    #[test]
62    fn tool_name_and_protocol_version() {
63        assert_eq!(Agent::ClaudeCode.tool_name(), "claude-code");
64        assert_eq!(Agent::ClaudeCode.protocol_version(), 1);
65        assert_eq!(Agent::Codex.tool_name(), "codex");
66        assert_eq!(Agent::Codex.protocol_version(), 2);
67    }
68
69    #[test]
70    fn value_enum_parses_kebab_case() {
71        assert_eq!(
72            Agent::from_str("claude-code", true).unwrap(),
73            Agent::ClaudeCode
74        );
75        assert_eq!(Agent::from_str("codex", true).unwrap(), Agent::Codex);
76        assert!(Agent::from_str("cursor", true).is_err());
77    }
78
79    #[test]
80    fn label_wording() {
81        assert_eq!(Agent::ClaudeCode.label(), "Claude Code");
82        assert_eq!(Agent::Codex.label(), "Codex");
83    }
84
85    #[test]
86    fn gsd_agent_mapping() {
87        assert_eq!(Agent::Gsd.tool_name(), "gsd");
88        assert_eq!(Agent::Gsd.protocol_version(), 2);
89        assert_eq!(Agent::Gsd.label(), "GSD");
90    }
91
92    #[test]
93    fn gsd_parses_from_str_and_alias() {
94        use clap::ValueEnum;
95        assert_eq!(Agent::from_str("gsd", true).unwrap(), Agent::Gsd);
96        assert_eq!(Agent::from_str("gsd2", true).unwrap(), Agent::Gsd);
97    }
98
99    #[test]
100    fn opencode_agent_mapping() {
101        assert_eq!(Agent::OpenCode.tool_name(), "opencode");
102        assert_eq!(Agent::OpenCode.protocol_version(), 2);
103        assert_eq!(Agent::OpenCode.label(), "OpenCode");
104        use clap::ValueEnum;
105        assert_eq!(Agent::from_str("opencode", true).unwrap(), Agent::OpenCode);
106    }
107}