Skip to main content

objectiveai_sdk/cli/command/functions/
mod.rs

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