Skip to main content

objectiveai_sdk/cli/command/plugins/
mod.rs

1pub mod get;
2pub mod install;
3pub mod list;
4pub mod run;
5
6#[derive(clap::Subcommand)]
7pub enum Command {
8    Get(get::Command),
9    Install {
10        #[command(subcommand)]
11        command: install::Command,
12    },
13    List(list::Command),
14    Run(run::Command),
15}
16
17#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
18#[serde(untagged)]
19#[schemars(rename = "cli.command.plugins.Request")]
20pub enum Request {
21    #[schemars(title = "Get")]
22    Get(get::Request),
23    #[schemars(title = "GetRequestSchema")]
24    GetRequestSchema(get::request_schema::Request),
25    #[schemars(title = "GetResponseSchema")]
26    GetResponseSchema(get::response_schema::Request),
27    #[schemars(title = "Install")]
28    Install(install::Request),
29    #[schemars(title = "List")]
30    List(list::Request),
31    #[schemars(title = "ListRequestSchema")]
32    ListRequestSchema(list::request_schema::Request),
33    #[schemars(title = "ListResponseSchema")]
34    ListResponseSchema(list::response_schema::Request),
35    #[schemars(title = "Run")]
36    Run(run::Request),
37    #[schemars(title = "RunRequestSchema")]
38    RunRequestSchema(run::request_schema::Request),
39    #[schemars(title = "RunResponseSchema")]
40    RunResponseSchema(run::response_schema::Request),
41}
42
43// Exempt from json-schema coverage: tier aggregate (see the root
44// `ResponseItem` in command.rs - TS7056).
45#[objectiveai_sdk_macros::json_schema_ignore]
46#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
47#[schemars(rename = "cli.command.plugins.ResponseItem")]
48#[serde(untagged)]
49pub enum ResponseItem {
50    #[schemars(title = "Get")]
51    Get(get::Response),
52    #[schemars(title = "GetRequestSchema")]
53    GetRequestSchema(get::request_schema::Response),
54    #[schemars(title = "GetResponseSchema")]
55    GetResponseSchema(get::response_schema::Response),
56    #[schemars(title = "Install")]
57    Install(install::Response),
58    #[schemars(title = "List")]
59    List(list::ResponseItem),
60    #[schemars(title = "ListRequestSchema")]
61    ListRequestSchema(list::request_schema::Response),
62    #[schemars(title = "ListResponseSchema")]
63    ListResponseSchema(list::response_schema::Response),
64    #[schemars(title = "Run")]
65    Run(run::ResponseItem),
66    #[schemars(title = "RunRequestSchema")]
67    RunRequestSchema(run::request_schema::Response),
68    #[schemars(title = "RunResponseSchema")]
69    RunResponseSchema(run::response_schema::Response),
70}
71
72#[cfg(feature = "mcp")]
73impl crate::cli::command::CommandResponse for ResponseItem {
74    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
75        match self {
76            ResponseItem::Get(v) => v.into_mcp(),
77            ResponseItem::GetRequestSchema(v) => v.into_mcp(),
78            ResponseItem::GetResponseSchema(v) => v.into_mcp(),
79            ResponseItem::Install(v) => v.into_mcp(),
80            ResponseItem::List(v) => v.into_mcp(),
81            ResponseItem::ListRequestSchema(v) => v.into_mcp(),
82            ResponseItem::ListResponseSchema(v) => v.into_mcp(),
83            ResponseItem::Run(v) => v.into_mcp(),
84            ResponseItem::RunRequestSchema(v) => v.into_mcp(),
85            ResponseItem::RunResponseSchema(v) => v.into_mcp(),
86        }
87    }
88}
89
90impl TryFrom<Command> for Request {
91    type Error = crate::cli::command::FromArgsError;
92    fn try_from(command: Command) -> Result<Self, Self::Error> {
93        match command {
94            Command::Get(cmd) => match cmd.schema {
95                None => Ok(Request::Get(get::Request::try_from(cmd.args)?)),
96                Some(get::Schema::RequestSchema(args)) =>
97                    Ok(Request::GetRequestSchema(get::request_schema::Request::try_from(args)?)),
98                Some(get::Schema::ResponseSchema(args)) =>
99                    Ok(Request::GetResponseSchema(get::response_schema::Request::try_from(args)?)),
100            },
101            Command::Install { command } =>
102                Ok(Request::Install(install::Request::try_from(command)?)),
103            Command::List(cmd) => match cmd.schema {
104                None => Ok(Request::List(list::Request::try_from(cmd.args)?)),
105                Some(list::Schema::RequestSchema(args)) =>
106                    Ok(Request::ListRequestSchema(list::request_schema::Request::try_from(args)?)),
107                Some(list::Schema::ResponseSchema(args)) =>
108                    Ok(Request::ListResponseSchema(list::response_schema::Request::try_from(args)?)),
109            },
110            Command::Run(cmd) => match cmd.schema {
111                None => Ok(Request::Run(run::Request::try_from(cmd.args)?)),
112                Some(run::Schema::RequestSchema(args)) =>
113                    Ok(Request::RunRequestSchema(run::request_schema::Request::try_from(args)?)),
114                Some(run::Schema::ResponseSchema(args)) =>
115                    Ok(Request::RunResponseSchema(run::response_schema::Request::try_from(args)?)),
116            },
117        }
118    }
119}
120
121impl crate::cli::command::CommandRequest for Request {
122    fn into_command(&self) -> Vec<String> {
123        match self {
124            Request::Get(inner) => inner.into_command(),
125            Request::GetRequestSchema(inner) => inner.into_command(),
126            Request::GetResponseSchema(inner) => inner.into_command(),
127            Request::Install(inner) => inner.into_command(),
128            Request::List(inner) => inner.into_command(),
129            Request::ListRequestSchema(inner) => inner.into_command(),
130            Request::ListResponseSchema(inner) => inner.into_command(),
131            Request::Run(inner) => inner.into_command(),
132            Request::RunRequestSchema(inner) => inner.into_command(),
133            Request::RunResponseSchema(inner) => inner.into_command(),
134        }
135    }
136}
137
138#[cfg(feature = "cli-executor")]
139pub async fn execute<E: crate::cli::command::CommandExecutor>(
140    executor: &E,
141    request: Request,
142
143        agent_arguments: Option<&crate::cli::command::AgentArguments>,
144    ) -> Result<
145    std::pin::Pin<Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>>,
146    E::Error,
147> {
148    use futures::StreamExt;
149    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>> =
150        match request {
151            Request::Get(req) => {
152                let value = get::execute(executor, req, agent_arguments).await?;
153                Box::pin(crate::cli::command::StreamOnce::new(Ok(
154                    ResponseItem::Get(value),
155                )))
156            }
157            Request::GetRequestSchema(req) => {
158                let value = get::request_schema::execute(executor, req, agent_arguments).await?;
159                Box::pin(crate::cli::command::StreamOnce::new(Ok(
160                    ResponseItem::GetRequestSchema(value),
161                )))
162            }
163            Request::GetResponseSchema(req) => {
164                let value = get::response_schema::execute(executor, req, agent_arguments).await?;
165                Box::pin(crate::cli::command::StreamOnce::new(Ok(
166                    ResponseItem::GetResponseSchema(value),
167                )))
168            }
169            Request::Install(req) => {
170                let inner = install::execute(executor, req, agent_arguments).await?;
171                Box::pin(inner.map(|r| r.map(ResponseItem::Install)))
172            }
173            Request::List(req) => {
174                let inner = list::execute(executor, req, agent_arguments).await?;
175                Box::pin(inner.map(|r| r.map(ResponseItem::List)))
176            }
177            Request::ListRequestSchema(req) => {
178                let value = list::request_schema::execute(executor, req, agent_arguments).await?;
179                Box::pin(crate::cli::command::StreamOnce::new(Ok(
180                    ResponseItem::ListRequestSchema(value),
181                )))
182            }
183            Request::ListResponseSchema(req) => {
184                let value = list::response_schema::execute(executor, req, agent_arguments).await?;
185                Box::pin(crate::cli::command::StreamOnce::new(Ok(
186                    ResponseItem::ListResponseSchema(value),
187                )))
188            }
189            Request::Run(req) => {
190                let inner = run::execute(executor, req, agent_arguments).await?;
191                Box::pin(inner.map(|r| r.map(ResponseItem::Run)))
192            }
193            Request::RunRequestSchema(req) => {
194                let value = run::request_schema::execute(executor, req, agent_arguments).await?;
195                Box::pin(crate::cli::command::StreamOnce::new(Ok(
196                    ResponseItem::RunRequestSchema(value),
197                )))
198            }
199            Request::RunResponseSchema(req) => {
200                let value = run::response_schema::execute(executor, req, agent_arguments).await?;
201                Box::pin(crate::cli::command::StreamOnce::new(Ok(
202                    ResponseItem::RunResponseSchema(value),
203                )))
204            }
205        };
206    Ok(stream)
207}
208
209#[cfg(feature = "cli-executor")]
210pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
211    executor: &E,
212    request: Request,
213    jq: String,
214
215        agent_arguments: Option<&crate::cli::command::AgentArguments>,
216    ) -> Result<
217    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
218    E::Error,
219> {
220    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>> =
221        match request {
222            Request::Get(req) => {
223                let value = get::execute_jq(executor, req, jq, agent_arguments).await?;
224                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
225            }
226            Request::GetRequestSchema(req) => {
227                let value = get::request_schema::execute_jq(executor, req, jq, agent_arguments).await?;
228                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
229            }
230            Request::GetResponseSchema(req) => {
231                let value = get::response_schema::execute_jq(executor, req, jq, agent_arguments).await?;
232                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
233            }
234            Request::Install(req) => {
235                let inner = install::execute_jq(executor, req, jq, agent_arguments).await?;
236                Box::pin(inner)
237            }
238            Request::List(req) => {
239                let inner = list::execute_jq(executor, req, jq, agent_arguments).await?;
240                Box::pin(inner)
241            }
242            Request::ListRequestSchema(req) => {
243                let value = list::request_schema::execute_jq(executor, req, jq, agent_arguments).await?;
244                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
245            }
246            Request::ListResponseSchema(req) => {
247                let value = list::response_schema::execute_jq(executor, req, jq, agent_arguments).await?;
248                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
249            }
250            Request::Run(req) => {
251                let inner = run::execute_jq(executor, req, jq, agent_arguments).await?;
252                Box::pin(inner)
253            }
254            Request::RunRequestSchema(req) => {
255                let value = run::request_schema::execute_jq(executor, req, jq, agent_arguments).await?;
256                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
257            }
258            Request::RunResponseSchema(req) => {
259                let value = run::response_schema::execute_jq(executor, req, jq, agent_arguments).await?;
260                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
261            }
262        };
263    Ok(stream)
264}