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 owner: String,
10    pub name: String,
11    pub version: String,
12    pub args: Vec<String>,
13    pub jq: Option<String>,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
17#[schemars(rename = "cli.command.tools.run.Path")]
18pub enum Path {
19    #[serde(rename = "tools/run")]
20    ToolsRun,
21}
22
23impl CommandRequest for Request {
24    fn into_command(&self) -> Vec<String> {
25        let mut argv = vec!["tools".to_string(), "run".to_string()];
26        argv.push("--owner".to_string());
27        argv.push(self.owner.clone());
28        argv.push("--name".to_string());
29        argv.push(self.name.clone());
30        argv.push("--version".to_string());
31        argv.push(self.version.clone());
32        if let Some(jq) = &self.jq {
33            argv.push("--jq".to_string());
34            argv.push(jq.clone());
35        }
36        if !self.args.is_empty() {
37            argv.push("--args".to_string());
38            argv.push(serde_json::to_string(&self.args).expect("Vec<String> serializes"));
39        }
40        argv
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
45#[serde(untagged)]
46#[schemars(rename = "cli.command.tools.run.ResponseItem")]
47pub enum ResponseItem {
48    #[schemars(title = "Stdout")]
49    Stdout(String),
50    #[schemars(title = "Stderr")]
51    Stderr(crate::cli::Error),
52}
53
54#[derive(clap::Args)]
55pub struct Args {
56    /// Tool owner (GitHub `<owner>` segment). Required.
57    #[arg(long)]
58    pub owner: String,
59    /// Tool name (repository segment). Required.
60    #[arg(long)]
61    pub name: String,
62    /// Tool version. Required.
63    #[arg(long)]
64    pub version: String,
65    /// Arguments appended to the tool's exec vector, as a JSON array
66    /// of strings (e.g. `--args '["--flag","value"]'`).
67    #[arg(long)]
68    pub args: Option<String>,
69    /// jq filter applied to the JSON output.
70    #[arg(long)]
71    pub jq: Option<String>,
72}
73
74#[derive(clap::Args)]
75#[command(args_conflicts_with_subcommands = true)]
76pub struct Command {
77    #[command(flatten)]
78    pub args: Args,
79    #[command(subcommand)]
80    pub schema: Option<Schema>,
81}
82
83#[derive(clap::Subcommand)]
84pub enum Schema {
85    /// Emit the JSON Schema for this leaf's `Request` type and exit.
86    RequestSchema(request_schema::Args),
87    /// Emit the JSON Schema for this leaf's `Response` type and exit.
88    ResponseSchema(response_schema::Args),
89}
90
91impl TryFrom<Args> for Request {
92    type Error = crate::cli::command::FromArgsError;
93    fn try_from(args: Args) -> Result<Self, Self::Error> {
94        let parsed_args: Vec<String> = match args.args {
95            Some(s) => {
96                let mut de = serde_json::Deserializer::from_str(&s);
97                serde_path_to_error::deserialize(&mut de).map_err(|source| {
98                    crate::cli::command::FromArgsError {
99                        field: "args",
100                        source: source.into(),
101                    }
102                })?
103            }
104            None => Vec::new(),
105        };
106        Ok(Self {
107            path_type: Path::ToolsRun,
108            owner: args.owner,
109            name: args.name,
110            version: args.version,
111            args: parsed_args,
112            jq: args.jq,
113        })
114    }
115}
116
117#[cfg(feature = "cli-executor")]
118pub async fn execute<E: crate::cli::command::CommandExecutor>(
119    executor: &E,
120    mut request: Request,
121
122        agent_arguments: Option<&crate::cli::command::AgentArguments>,
123    ) -> Result<E::Stream<ResponseItem>, E::Error> {
124    request.jq = None;
125    executor.execute(request, agent_arguments).await
126}
127
128#[cfg(feature = "cli-executor")]
129pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
130    executor: &E,
131    mut request: Request,
132    jq: String,
133
134        agent_arguments: Option<&crate::cli::command::AgentArguments>,
135    ) -> Result<E::Stream<serde_json::Value>, E::Error> {
136    request.jq = Some(jq);
137    executor.execute(request, agent_arguments).await
138}
139
140#[cfg(feature = "mcp")]
141impl crate::cli::command::CommandResponse for ResponseItem {
142    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
143        use crate::agent::completions::message::RichContentPart;
144        use crate::cli::command::McpResponseItem;
145        match self {
146            ResponseItem::Stdout(s) => {
147                // Stdout line that happens to be a `data:<mime>;base64,...`
148                // URL gets upgraded to a typed media block; otherwise
149                // it rides through as a bare `Value::String`.
150                if let Some((mime, payload)) = crate::data_url::parse_data_url(&s) {
151                    let part = RichContentPart::from_blob(
152                        mime,
153                        payload.to_string(),
154                        None,
155                    );
156                    return McpResponseItem::Media(part.into());
157                }
158                McpResponseItem::JSONL(serde_json::Value::String(s))
159            }
160            ResponseItem::Stderr(e) => e.into_mcp(),
161        }
162    }
163}
164
165pub mod request_schema;
166
167
168pub mod response_schema;