Skip to main content

subspace_board_command/
composition.rs

1use std::process::ExitCode;
2
3use subspace_agent_tools::ToolEnvironment;
4use subspace_product::ProductContext;
5
6use crate::{cli, fail, tree};
7
8/// Product-owned commands merged with generated capability routes.
9/// Completion receives the merged tree. Fallback receives the original
10/// arguments for product commands and unknown root commands. Parse errors on
11/// declared commands never reach fallback, with or without a catalogue.
12pub struct Composition {
13    tree: fn(&ProductContext) -> Result<usage::Spec, String>,
14    completion: fn(&usage::Spec, &[String]) -> Vec<String>,
15    fallback: fn(&ProductContext, &[String]) -> ExitCode,
16    specialized_tools: &'static [&'static str],
17}
18
19impl Composition {
20    pub const fn new(
21        tree: fn(&ProductContext) -> Result<usage::Spec, String>,
22        completion: fn(&usage::Spec, &[String]) -> Vec<String>,
23        fallback: fn(&ProductContext, &[String]) -> ExitCode,
24    ) -> Self {
25        Self {
26            tree,
27            completion,
28            fallback,
29            specialized_tools: &[],
30        }
31    }
32
33    /// Exclude exact tool identities from CLI generation, never from MCP.
34    // qual:api (products replace selected tools with their own CLI handlers)
35    pub fn with_specialized_tools(mut self, names: &'static [&'static str]) -> Self {
36        self.specialized_tools = names;
37        self
38    }
39
40    /// Headless product callback using the same served catalogue as MCP.
41    /// Without one, the product's own tree still parses, with no generated
42    /// commands, after a notice on standard error.
43    // qual:api (products supply this callback to shell::launch)
44    pub fn run(
45        &self,
46        context: &ProductContext,
47        boards: &[subspace_mcp::Board],
48        environment: &ToolEnvironment,
49        args: &[String],
50    ) -> ExitCode {
51        let (capabilities, notice) = available(cli::capabilities(context, boards, environment));
52        notice.iter().for_each(|notice| eprintln!("{notice}"));
53        self.run_catalog(context, &capabilities, args)
54    }
55
56    pub(crate) fn commands(
57        &self,
58        context: &ProductContext,
59        capabilities: &tree::Capabilities,
60    ) -> Result<tree::Commands, String> {
61        tree::Commands::new(context, capabilities, self.tree, self.specialized_tools)
62    }
63
64    pub(crate) fn run_catalog(
65        &self,
66        context: &ProductContext,
67        capabilities: &tree::Capabilities,
68        args: &[String],
69    ) -> ExitCode {
70        let commands = match self.commands(context, capabilities) {
71            Ok(commands) => commands,
72            Err(error) => return fail(&error, 1),
73        };
74        cli::run_commands(context, &commands, args, self.completion, self.fallback)
75    }
76}
77
78/// The served capabilities, or none with the notice that explains why a
79/// generated command is missing.
80pub(crate) fn available(
81    capabilities: Result<tree::Capabilities, String>,
82) -> (tree::Capabilities, Option<String>) {
83    match capabilities {
84        Ok(capabilities) => (capabilities, None),
85        Err(error) => (
86            tree::Capabilities::default(),
87            Some(format!("Space commands unavailable: {error}")),
88        ),
89    }
90}