Skip to main content

visdom_cli/
cli.rs

1//! The `visdom` command line.
2
3use clap::{Parser, Subcommand};
4use tracevault_cli::agent::Agent;
5use tracevault_cli::cli::{Command, InitArgs, LoginArgs};
6
7use crate::mode::TracingMode;
8
9#[derive(Parser)]
10#[command(
11    name = "visdom",
12    version,
13    about = "Visdom developer CLI",
14    long_about = "Visdom developer CLI.\n\nWithout a subcommand: checks that this project is wired \
15                  for the coding agent (tracing, bazaar MCP), offers to fix what is not, and \
16                  launches the agent — with hooks tracing, or through the AI Gateway (--mode). \
17                  Arguments after `--` go to the agent."
18)]
19pub struct VisdomCli {
20    /// The agent to check and launch: claude-code or codex. Default: the
21    /// VISDOM_AGENT variable, then `.visdom/config.toml` in the repository,
22    /// then the user config, then claude-code.
23    #[arg(long, value_enum)]
24    pub agent: Option<Agent>,
25    /// How the session is traced: by hooks, or through the AI Gateway.
26    /// Default: the VISDOM_TRACING_MODE variable, then `.visdom/config.toml`
27    /// in the repository, then the user config (`tracing_mode`), then hooks.
28    /// The workspace's AI traffic policy may require one of them.
29    #[arg(long, value_enum)]
30    pub mode: Option<TracingMode>,
31    /// Apply every offered fix without asking.
32    #[arg(long)]
33    pub yes: bool,
34    #[command(subcommand)]
35    pub command: Option<VisdomCommand>,
36    /// Arguments passed to the agent unchanged.
37    #[arg(last = true)]
38    pub agent_args: Vec<String>,
39}
40
41#[derive(Subcommand)]
42pub enum VisdomCommand {
43    /// Check this project's Visdom wiring without launching an agent. Exits
44    /// with 1 when a check fails.
45    Doctor {
46        /// Offer the fixes (asking for each; with --yes, apply all).
47        #[arg(long)]
48        fix: bool,
49        /// Print the findings as JSON.
50        #[arg(long)]
51        json: bool,
52        /// With --fix: apply every fix without asking.
53        #[arg(long)]
54        yes: bool,
55        /// The agent to check (see `visdom --help`).
56        #[arg(long, value_enum)]
57        agent: Option<Agent>,
58        /// The tracing mode to check (see `visdom --help`).
59        #[arg(long, value_enum)]
60        mode: Option<TracingMode>,
61    },
62    /// Print the developer's AI Gateway key for a project: what a launched
63    /// agent runs to get, and renew, its key.
64    #[command(hide = true)]
65    GatewayKey {
66        /// The gateway console's address.
67        #[arg(long)]
68        console: String,
69        /// The gateway project's id.
70        #[arg(long)]
71        project: String,
72    },
73    /// Log in to Visdom
74    Login(LoginArgs),
75    /// Log out from Visdom
76    Logout,
77    /// Set up this repository for Visdom: tracing (as `tracevault init`), and
78    /// the bazaar MCP server for the agent.
79    Init {
80        #[command(flatten)]
81        args: InitArgs,
82    },
83    /// Tracing commands (everything `tracevault` does).
84    Trace {
85        #[command(subcommand)]
86        command: Command,
87    },
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    fn parse(args: &[&str]) -> VisdomCli {
95        match VisdomCli::try_parse_from(args) {
96            Ok(cli) => cli,
97            Err(e) => panic!("{args:?} must parse: {e}"),
98        }
99    }
100
101    #[test]
102    fn the_mode_is_an_option_of_the_launch_and_of_doctor() {
103        let cli = parse(&["visdom", "--mode", "gateway", "--", "--verbose"]);
104        assert_eq!(cli.mode, Some(TracingMode::Gateway));
105        assert_eq!(cli.agent_args, ["--verbose"]);
106        let cli = parse(&["visdom", "doctor", "--mode", "hooks"]);
107        assert!(matches!(
108            cli.command,
109            Some(VisdomCommand::Doctor {
110                mode: Some(TracingMode::Hooks),
111                ..
112            })
113        ));
114        assert!(VisdomCli::try_parse_from(["visdom", "--mode", "proxy"]).is_err());
115    }
116
117    #[test]
118    fn the_key_helper_takes_a_console_and_a_project() {
119        let cli = parse(&[
120            "visdom",
121            "gateway-key",
122            "--console",
123            "https://gw.example.com",
124            "--project",
125            "p-1",
126        ]);
127        match cli.command {
128            Some(VisdomCommand::GatewayKey { console, project }) => {
129                assert_eq!(
130                    (console.as_str(), project.as_str()),
131                    ("https://gw.example.com", "p-1")
132                );
133            }
134            _ => panic!("expected gateway-key"),
135        }
136    }
137
138    #[test]
139    fn command_definition_is_consistent() {
140        use clap::CommandFactory;
141        VisdomCli::command().debug_assert();
142    }
143
144    #[test]
145    fn parses_trace_status() {
146        let cli = parse(&["visdom", "trace", "status"]);
147        assert!(matches!(
148            cli.command,
149            Some(VisdomCommand::Trace {
150                command: Command::Status { .. }
151            })
152        ));
153    }
154
155    #[test]
156    fn parses_hidden_trace_stream() {
157        let cli = parse(&["visdom", "trace", "stream", "--event", "stop"]);
158        assert!(matches!(
159            cli.command,
160            Some(VisdomCommand::Trace {
161                command: Command::Stream { .. }
162            })
163        ));
164    }
165
166    #[test]
167    fn bare_visdom_with_agent_args() {
168        let cli = parse(&["visdom", "--agent", "codex", "--", "--model", "x"]);
169        assert!(cli.command.is_none());
170        assert_eq!(cli.agent, Some(Agent::Codex));
171        assert_eq!(cli.agent_args, ["--model", "x"]);
172    }
173
174    #[test]
175    fn bare_visdom_without_arguments() {
176        let cli = parse(&["visdom"]);
177        assert!(cli.command.is_none());
178        assert!(cli.agent.is_none() && !cli.yes && cli.agent_args.is_empty());
179    }
180
181    #[test]
182    fn parses_doctor_json() {
183        let cli = parse(&["visdom", "doctor", "--json"]);
184        assert!(matches!(
185            cli.command,
186            Some(VisdomCommand::Doctor {
187                json: true,
188                fix: false,
189                yes: false,
190                agent: None,
191                mode: None
192            })
193        ));
194    }
195
196    #[test]
197    fn init_takes_tracevault_init_flags() {
198        let cli = parse(&["visdom", "init", "--agent", "codex", "--no-gitignore"]);
199        match cli.command {
200            Some(VisdomCommand::Init { args }) => {
201                assert_eq!(args.agent, Agent::Codex);
202                assert!(args.no_gitignore);
203            }
204            _ => panic!("expected init"),
205        }
206    }
207}