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