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::billing::Billing;
8use crate::mode::TracingMode;
9
10#[derive(Parser)]
11#[command(
12    name = "visdom",
13    version,
14    about = "Visdom developer CLI",
15    long_about = "Visdom developer CLI.\n\nWithout a subcommand: checks that this project is wired \
16                  for the coding agent (tracing, bazaar MCP), offers to fix what is not, and \
17                  launches the agent — with hooks tracing, or through the AI Gateway (--mode). \
18                  Arguments after `--` go to the agent."
19)]
20pub struct VisdomCli {
21    /// The agent to check and launch: claude-code or codex. Default: the
22    /// VISDOM_AGENT variable, then `.visdom/config.toml` in the repository,
23    /// then the user config, then claude-code.
24    #[arg(long, value_enum)]
25    pub agent: Option<Agent>,
26    /// How the session is traced: by hooks, or through the AI Gateway.
27    /// Default: the VISDOM_TRACING_MODE variable, then `.visdom/config.toml`
28    /// in the repository, then the user config (`tracing_mode`), then hooks.
29    /// The workspace's AI traffic policy may require one of them.
30    #[arg(long, value_enum)]
31    pub mode: Option<TracingMode>,
32    /// Who pays for the agent's calls through the AI Gateway: the project
33    /// (its gateway key) or your Claude subscription (Claude Code only).
34    /// Default: the VISDOM_GATEWAY_BILLING variable, then `billing` under
35    /// [gateway] in `.visdom/config.toml`, then in the user config, then
36    /// project. `visdom --billing … init` saves it.
37    #[arg(long, value_enum)]
38    pub billing: Option<Billing>,
39    /// Apply every offered fix without asking.
40    #[arg(long)]
41    pub yes: bool,
42    /// Start the agent without telling it about Visdom's tools (see `visdom
43    /// instructions`). To leave them out always: `agent_instructions = false`
44    /// in the user config.
45    #[arg(long)]
46    pub no_instructions: bool,
47    #[command(subcommand)]
48    pub command: Option<VisdomCommand>,
49    /// Arguments passed to the agent unchanged.
50    #[arg(last = true)]
51    pub agent_args: Vec<String>,
52}
53
54#[derive(Subcommand)]
55pub enum VisdomCommand {
56    /// Check this project's Visdom wiring without launching an agent. Exits
57    /// with 1 when a check fails.
58    Doctor {
59        /// Offer the fixes (asking for each; with --yes, apply all).
60        #[arg(long)]
61        fix: bool,
62        /// Print the findings as JSON.
63        #[arg(long)]
64        json: bool,
65        /// With --fix: apply every fix without asking.
66        #[arg(long)]
67        yes: bool,
68        /// The agent to check (see `visdom --help`).
69        #[arg(long, value_enum)]
70        agent: Option<Agent>,
71        /// The tracing mode to check (see `visdom --help`).
72        #[arg(long, value_enum)]
73        mode: Option<TracingMode>,
74        /// Who pays through the gateway, to check (see `visdom --help`).
75        #[arg(long, value_enum)]
76        billing: Option<Billing>,
77    },
78    /// Print the developer's AI Gateway key for a project: what a launched
79    /// agent runs to get, and renew, its key.
80    #[command(hide = true)]
81    GatewayKey {
82        /// The gateway console's address.
83        #[arg(long)]
84        console: String,
85        /// The gateway project's id.
86        #[arg(long)]
87        project: String,
88        /// The kind of key: an ordinary personal key (the project pays) or a
89        /// subscription key (Claude Code on your claude.ai login).
90        #[arg(long, value_enum, default_value_t = Billing::Project)]
91        billing: Billing,
92    },
93    /// Print shell commands that send an agent started without `visdom`
94    /// through the AI Gateway: `source <(visdom env)`. The key is not in the
95    /// output, only the command that gets it.
96    Env {
97        /// The agent the commands are for (see `visdom --help`).
98        #[arg(long, value_enum)]
99        agent: Option<Agent>,
100    },
101    /// Print what an agent started here with `visdom` is told about Visdom's
102    /// tools: the tracing commands, the bazaar MCP server, the AI Gateway —
103    /// each only if this session has it. Prints nothing when it has none.
104    Instructions {
105        /// The agent the text is for (see `visdom --help`).
106        #[arg(long, value_enum)]
107        agent: Option<Agent>,
108        /// The tracing mode it is for (see `visdom --help`).
109        #[arg(long, value_enum)]
110        mode: Option<TracingMode>,
111    },
112    /// Log in to Visdom
113    Login(LoginArgs),
114    /// Log out from Visdom
115    Logout,
116    /// Set up this repository for Visdom: tracing, and
117    /// the bazaar MCP server for the agent.
118    Init {
119        #[command(flatten)]
120        args: InitArgs,
121    },
122    /// Tracing commands: status, check, repo, project, context and the rest.
123    Trace {
124        #[command(subcommand)]
125        command: Command,
126    },
127}
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    fn parse(args: &[&str]) -> VisdomCli {
134        match VisdomCli::try_parse_from(args) {
135            Ok(cli) => cli,
136            Err(e) => panic!("{args:?} must parse: {e}"),
137        }
138    }
139
140    #[test]
141    fn the_mode_is_an_option_of_the_launch_and_of_doctor() {
142        let cli = parse(&["visdom", "--mode", "gateway", "--", "--verbose"]);
143        assert_eq!(cli.mode, Some(TracingMode::Gateway));
144        assert_eq!(cli.agent_args, ["--verbose"]);
145        let cli = parse(&["visdom", "doctor", "--mode", "hooks"]);
146        assert!(matches!(
147            cli.command,
148            Some(VisdomCommand::Doctor {
149                mode: Some(TracingMode::Hooks),
150                ..
151            })
152        ));
153        assert!(VisdomCli::try_parse_from(["visdom", "--mode", "proxy"]).is_err());
154    }
155
156    #[test]
157    fn who_pays_is_an_option_of_the_launch_init_and_doctor() {
158        let cli = parse(&[
159            "visdom",
160            "--mode",
161            "gateway",
162            "--billing",
163            "subscription",
164            "init",
165        ]);
166        assert_eq!(cli.billing, Some(Billing::Subscription));
167        assert!(matches!(cli.command, Some(VisdomCommand::Init { .. })));
168        let cli = parse(&["visdom", "doctor", "--billing", "project"]);
169        assert!(matches!(
170            cli.command,
171            Some(VisdomCommand::Doctor {
172                billing: Some(Billing::Project),
173                ..
174            })
175        ));
176        assert!(VisdomCli::try_parse_from(["visdom", "--billing", "free"]).is_err());
177    }
178
179    #[test]
180    fn the_key_helper_takes_a_console_and_a_project() {
181        let cli = parse(&[
182            "visdom",
183            "gateway-key",
184            "--console",
185            "https://gw.example.com",
186            "--project",
187            "p-1",
188        ]);
189        match cli.command {
190            Some(VisdomCommand::GatewayKey {
191                console,
192                project,
193                billing,
194            }) => {
195                assert_eq!(
196                    (console.as_str(), project.as_str(), billing),
197                    ("https://gw.example.com", "p-1", Billing::Project)
198                );
199            }
200            _ => panic!("expected gateway-key"),
201        }
202        let cli = parse(&[
203            "visdom",
204            "gateway-key",
205            "--console",
206            "https://gw.example.com",
207            "--project",
208            "p-1",
209            "--billing",
210            "subscription",
211        ]);
212        assert!(matches!(
213            cli.command,
214            Some(VisdomCommand::GatewayKey {
215                billing: Billing::Subscription,
216                ..
217            })
218        ));
219    }
220
221    #[test]
222    fn command_definition_is_consistent() {
223        use clap::CommandFactory;
224        VisdomCli::command().debug_assert();
225    }
226
227    #[test]
228    fn parses_trace_status() {
229        let cli = parse(&["visdom", "trace", "status"]);
230        assert!(matches!(
231            cli.command,
232            Some(VisdomCommand::Trace {
233                command: Command::Status { .. }
234            })
235        ));
236    }
237
238    #[test]
239    fn parses_hidden_trace_stream() {
240        let cli = parse(&["visdom", "trace", "stream", "--event", "stop"]);
241        assert!(matches!(
242            cli.command,
243            Some(VisdomCommand::Trace {
244                command: Command::Stream { .. }
245            })
246        ));
247    }
248
249    #[test]
250    fn bare_visdom_with_agent_args() {
251        let cli = parse(&["visdom", "--agent", "codex", "--", "--model", "x"]);
252        assert!(cli.command.is_none());
253        assert_eq!(cli.agent, Some(Agent::Codex));
254        assert_eq!(cli.agent_args, ["--model", "x"]);
255    }
256
257    #[test]
258    fn bare_visdom_without_arguments() {
259        let cli = parse(&["visdom"]);
260        assert!(cli.command.is_none());
261        assert!(cli.agent.is_none() && !cli.yes && cli.agent_args.is_empty());
262    }
263
264    #[test]
265    fn parses_doctor_json() {
266        let cli = parse(&["visdom", "doctor", "--json"]);
267        assert!(matches!(
268            cli.command,
269            Some(VisdomCommand::Doctor {
270                json: true,
271                fix: false,
272                yes: false,
273                agent: None,
274                mode: None,
275                billing: None
276            })
277        ));
278    }
279
280    #[test]
281    fn init_takes_tracevault_init_flags() {
282        let cli = parse(&["visdom", "init", "--agent", "codex", "--no-gitignore"]);
283        match cli.command {
284            Some(VisdomCommand::Init { args }) => {
285                assert_eq!(args.agent, Agent::Codex);
286                assert!(args.no_gitignore);
287            }
288            _ => panic!("expected init"),
289        }
290    }
291}