Skip to main content

objectiveai_sdk/cli/command/tools/run/
mod.rs

1//! `tools run` — async handler stub.
2
3use crate::cli::command::CommandRequest;
4
5#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
6#[schemars(rename = "cli.command.tools.run.Request")]
7pub struct Request {
8    pub path_type: Path,
9    pub name: String,
10    pub args: Vec<String>,
11    pub jq: Option<String>,
12}
13
14#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
15#[schemars(rename = "cli.command.tools.run.Path")]
16pub enum Path {
17    #[serde(rename = "tools/run")]
18    ToolsRun,
19}
20
21impl CommandRequest for Request {
22    fn into_command(&self) -> Vec<String> {
23        let mut argv = vec!["tools".to_string(), "run".to_string()];
24        if let Some(jq) = &self.jq {
25            argv.push("--jq".to_string());
26            argv.push(jq.clone());
27        }
28        argv.push(self.name.clone());
29        argv.extend(self.args.iter().cloned());
30        argv
31    }
32}
33
34#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
35#[serde(untagged)]
36#[schemars(rename = "cli.command.tools.run.ResponseItem")]
37pub enum ResponseItem {
38    #[schemars(title = "Stdout")]
39    Stdout(String),
40    #[schemars(title = "Stderr")]
41    Stderr(crate::cli::Error),
42}
43
44#[derive(clap::Args)]
45pub struct Args {
46    /// Plugin/tool name.
47    pub name: String,
48    /// Arguments passed through to the invoked binary.
49    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
50    pub args: Vec<String>,
51    /// jq filter applied to the JSON output.
52    #[arg(long)]
53    pub jq: Option<String>,
54}
55
56#[derive(clap::Args)]
57#[command(args_conflicts_with_subcommands = true)]
58pub struct Command {
59    #[command(flatten)]
60    pub args: Args,
61    #[command(subcommand)]
62    pub schema: Option<Schema>,
63}
64
65#[derive(clap::Subcommand)]
66pub enum Schema {
67    /// Emit the JSON Schema for this leaf's `Request` type and exit.
68    RequestSchema(request_schema::Args),
69    /// Emit the JSON Schema for this leaf's `Response` type and exit.
70    ResponseSchema(response_schema::Args),
71}
72
73impl TryFrom<Args> for Request {
74    type Error = crate::cli::command::FromArgsError;
75    fn try_from(args: Args) -> Result<Self, Self::Error> {
76        Ok(Self { path_type: Path::ToolsRun,
77            name: args.name,
78            args: args.args,
79            jq: args.jq,
80        })
81    }
82}
83
84#[cfg(feature = "cli-executor")]
85pub async fn execute<E: crate::cli::command::CommandExecutor>(
86    executor: &E,
87    mut request: Request,
88
89        agent_arguments: Option<&crate::cli::command::AgentArguments>,
90    ) -> Result<E::Stream<ResponseItem>, E::Error> {
91    request.jq = None;
92    executor.execute(request, agent_arguments).await
93}
94
95#[cfg(feature = "cli-executor")]
96pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
97    executor: &E,
98    mut request: Request,
99    jq: String,
100
101        agent_arguments: Option<&crate::cli::command::AgentArguments>,
102    ) -> Result<E::Stream<serde_json::Value>, E::Error> {
103    request.jq = Some(jq);
104    executor.execute(request, agent_arguments).await
105}
106
107#[cfg(feature = "mcp")]
108impl crate::cli::command::CommandResponse for ResponseItem {
109    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
110        use crate::agent::completions::message::RichContentPart;
111        use crate::cli::command::McpResponseItem;
112        match self {
113            ResponseItem::Stdout(s) => {
114                // Stdout line that happens to be a `data:<mime>;base64,...`
115                // URL gets upgraded to a typed media block; otherwise
116                // it rides through as a bare `Value::String`.
117                if let Some((mime, payload)) = crate::data_url::parse_data_url(&s) {
118                    let part = RichContentPart::from_blob(
119                        mime,
120                        payload.to_string(),
121                        None,
122                    );
123                    return McpResponseItem::Media(part.into());
124                }
125                McpResponseItem::JSONL(serde_json::Value::String(s))
126            }
127            ResponseItem::Stderr(e) => e.into_mcp(),
128        }
129    }
130}
131
132pub mod request_schema;
133
134
135pub mod response_schema;