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    /// Start the agent without telling it about Visdom's tools (see `visdom
35    /// instructions`). To leave them out always: `agent_instructions = false`
36    /// in the user config.
37    #[arg(long)]
38    pub no_instructions: bool,
39    #[command(subcommand)]
40    pub command: Option<VisdomCommand>,
41    /// Arguments passed to the agent unchanged.
42    #[arg(last = true)]
43    pub agent_args: Vec<String>,
44}
45
46#[derive(Subcommand)]
47pub enum VisdomCommand {
48    /// Check this project's Visdom wiring without launching an agent. Exits
49    /// with 1 when a check fails.
50    Doctor {
51        /// Offer the fixes (asking for each; with --yes, apply all).
52        #[arg(long)]
53        fix: bool,
54        /// Print the findings as JSON.
55        #[arg(long)]
56        json: bool,
57        /// With --fix: apply every fix without asking.
58        #[arg(long)]
59        yes: bool,
60        /// The agent to check (see `visdom --help`).
61        #[arg(long, value_enum)]
62        agent: Option<Agent>,
63        /// The tracing mode to check (see `visdom --help`).
64        #[arg(long, value_enum)]
65        mode: Option<TracingMode>,
66    },
67    /// Print the developer's AI Gateway key for a project: what a launched
68    /// agent runs to get, and renew, its key.
69    #[command(hide = true)]
70    GatewayKey {
71        /// The gateway console's address.
72        #[arg(long)]
73        console: String,
74        /// The gateway project's id.
75        #[arg(long)]
76        project: String,
77    },
78    /// Print shell commands that send an agent started without `visdom`
79    /// through the AI Gateway: `source <(visdom env)`. The key is not in the
80    /// output, only the command that gets it.
81    Env {
82        /// The agent the commands are for (see `visdom --help`).
83        #[arg(long, value_enum)]
84        agent: Option<Agent>,
85    },
86    /// Print what an agent started here with `visdom` is told about Visdom's
87    /// tools: the tracing commands, the bazaar MCP server, the AI Gateway —
88    /// each only if this session has it. Prints nothing when it has none.
89    Instructions {
90        /// The agent the text is for (see `visdom --help`).
91        #[arg(long, value_enum)]
92        agent: Option<Agent>,
93        /// The tracing mode it is for (see `visdom --help`).
94        #[arg(long, value_enum)]
95        mode: Option<TracingMode>,
96    },
97    /// Log in to Visdom
98    Login(LoginArgs),
99    /// Log out from Visdom
100    Logout,
101    /// Set up this repository for Visdom: tracing, and
102    /// the bazaar MCP server for the agent.
103    Init {
104        #[command(flatten)]
105        args: InitArgs,
106    },
107    /// Tracing commands: status, check, repo, project, context and the rest.
108    Trace {
109        #[command(subcommand)]
110        command: Command,
111    },
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    fn parse(args: &[&str]) -> VisdomCli {
119        match VisdomCli::try_parse_from(args) {
120            Ok(cli) => cli,
121            Err(e) => panic!("{args:?} must parse: {e}"),
122        }
123    }
124
125    #[test]
126    fn the_mode_is_an_option_of_the_launch_and_of_doctor() {
127        let cli = parse(&["visdom", "--mode", "gateway", "--", "--verbose"]);
128        assert_eq!(cli.mode, Some(TracingMode::Gateway));
129        assert_eq!(cli.agent_args, ["--verbose"]);
130        let cli = parse(&["visdom", "doctor", "--mode", "hooks"]);
131        assert!(matches!(
132            cli.command,
133            Some(VisdomCommand::Doctor {
134                mode: Some(TracingMode::Hooks),
135                ..
136            })
137        ));
138        assert!(VisdomCli::try_parse_from(["visdom", "--mode", "proxy"]).is_err());
139    }
140
141    #[test]
142    fn the_key_helper_takes_a_console_and_a_project() {
143        let cli = parse(&[
144            "visdom",
145            "gateway-key",
146            "--console",
147            "https://gw.example.com",
148            "--project",
149            "p-1",
150        ]);
151        match cli.command {
152            Some(VisdomCommand::GatewayKey { console, project }) => {
153                assert_eq!(
154                    (console.as_str(), project.as_str()),
155                    ("https://gw.example.com", "p-1")
156                );
157            }
158            _ => panic!("expected gateway-key"),
159        }
160    }
161
162    #[test]
163    fn command_definition_is_consistent() {
164        use clap::CommandFactory;
165        VisdomCli::command().debug_assert();
166    }
167
168    #[test]
169    fn parses_trace_status() {
170        let cli = parse(&["visdom", "trace", "status"]);
171        assert!(matches!(
172            cli.command,
173            Some(VisdomCommand::Trace {
174                command: Command::Status { .. }
175            })
176        ));
177    }
178
179    #[test]
180    fn parses_hidden_trace_stream() {
181        let cli = parse(&["visdom", "trace", "stream", "--event", "stop"]);
182        assert!(matches!(
183            cli.command,
184            Some(VisdomCommand::Trace {
185                command: Command::Stream { .. }
186            })
187        ));
188    }
189
190    #[test]
191    fn bare_visdom_with_agent_args() {
192        let cli = parse(&["visdom", "--agent", "codex", "--", "--model", "x"]);
193        assert!(cli.command.is_none());
194        assert_eq!(cli.agent, Some(Agent::Codex));
195        assert_eq!(cli.agent_args, ["--model", "x"]);
196    }
197
198    #[test]
199    fn bare_visdom_without_arguments() {
200        let cli = parse(&["visdom"]);
201        assert!(cli.command.is_none());
202        assert!(cli.agent.is_none() && !cli.yes && cli.agent_args.is_empty());
203    }
204
205    #[test]
206    fn parses_doctor_json() {
207        let cli = parse(&["visdom", "doctor", "--json"]);
208        assert!(matches!(
209            cli.command,
210            Some(VisdomCommand::Doctor {
211                json: true,
212                fix: false,
213                yes: false,
214                agent: None,
215                mode: None
216            })
217        ));
218    }
219
220    #[test]
221    fn init_takes_tracevault_init_flags() {
222        let cli = parse(&["visdom", "init", "--agent", "codex", "--no-gitignore"]);
223        match cli.command {
224            Some(VisdomCommand::Init { args }) => {
225                assert_eq!(args.agent, Agent::Codex);
226                assert!(args.no_gitignore);
227            }
228            _ => panic!("expected init"),
229        }
230    }
231}