Skip to main content

subspace_board_command/
lib.rs

1//! Commands derived from the same capability catalog as MCP. No GUI initialization.
2
3mod approval;
4mod cli;
5mod input;
6mod root;
7#[cfg(test)]
8mod tests;
9mod tree;
10
11use std::{process::ExitCode, sync::Arc};
12use subspace_agent_tools::{Invocation, Unmonitored};
13
14pub use cli::run;
15
16fn execute(route: &tree::Route, output: &usage::parse::ParseOutput) -> ExitCode {
17    let arguments = match input::arguments(route, output) {
18        Ok(arguments) => arguments,
19        Err(error) => return fail(&error, 2),
20    };
21    let yes = input::switch(output, "yes");
22    if route.tool.spec().access == subspace_agent_tools::Access::Write
23        && !yes
24        && !approval::interactive()
25    {
26        return fail(
27            "Confirmation requires a TTY or explicit --yes; no operation was executed.",
28            1,
29        );
30    }
31    let invocation = Invocation {
32        arguments,
33        approval: Arc::new(approval::CliApproval { yes }),
34        monitor: Arc::new(Unmonitored),
35    };
36    finish(call(route, invocation), input::switch(output, "json"))
37}
38
39fn call(route: &tree::Route, invocation: Invocation) -> Result<serde_json::Value, String> {
40    let runtime = tokio::runtime::Builder::new_multi_thread()
41        .enable_all()
42        .build()
43        .map_err(|error| error.to_string())?;
44    runtime.block_on(route.tool.call(invocation))
45}
46
47fn finish(result: Result<serde_json::Value, String>, json: bool) -> ExitCode {
48    match result {
49        Ok(value) => {
50            if json {
51                println!("{value}");
52            } else {
53                println!("{value:#}");
54            }
55            if failed(&value) {
56                ExitCode::FAILURE
57            } else {
58                ExitCode::SUCCESS
59            }
60        }
61        Err(error) => fail(&error, 1),
62    }
63}
64
65fn failed(value: &serde_json::Value) -> bool {
66    value["approved"] == false
67        || value["results"]
68            .as_array()
69            .is_some_and(|results| results.iter().any(|result| result["success"] == false))
70}
71
72fn fail(error: &str, code: u8) -> ExitCode {
73    eprintln!("{error}");
74    ExitCode::from(code)
75}