Skip to main content

objectiveai_sdk/cli/command/config/functions/
mod.rs

1pub mod favorites;
2pub mod get;
3pub mod inventions;
4pub mod profiles;
5
6#[derive(clap::Subcommand)]
7pub enum Command {
8    Get(get::Command),
9    Favorites {
10        #[command(subcommand)]
11        command: favorites::Command,
12    },
13    Inventions {
14        #[command(subcommand)]
15        command: inventions::Command,
16    },
17    Profiles {
18        #[command(subcommand)]
19        command: profiles::Command,
20    },
21}
22
23#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
24#[serde(untagged)]
25#[schemars(rename = "cli.command.config.functions.Request")]
26pub enum Request {
27    #[schemars(title = "Get")]
28    Get(get::Request),
29    #[schemars(title = "GetRequestSchema")]
30    GetRequestSchema(get::request_schema::Request),
31    #[schemars(title = "GetResponseSchema")]
32    GetResponseSchema(get::response_schema::Request),
33    #[schemars(title = "Favorites")]
34    Favorites(favorites::Request),
35    #[schemars(title = "Inventions")]
36    Inventions(inventions::Request),
37    #[schemars(title = "Profiles")]
38    Profiles(profiles::Request),
39}
40
41// Exempt from json-schema coverage: tier aggregate (see the root
42// `ResponseItem` in command.rs - TS7056).
43#[objectiveai_sdk_macros::json_schema_ignore]
44#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
45#[schemars(rename = "cli.command.config.functions.ResponseItem")]
46#[serde(untagged)]
47pub enum ResponseItem {
48    #[schemars(title = "Get")]
49    Get(get::Response),
50    #[schemars(title = "GetRequestSchema")]
51    GetRequestSchema(get::request_schema::Response),
52    #[schemars(title = "GetResponseSchema")]
53    GetResponseSchema(get::response_schema::Response),
54    #[schemars(title = "Favorites")]
55    Favorites(favorites::ResponseItem),
56    #[schemars(title = "Inventions")]
57    Inventions(inventions::Response),
58    #[schemars(title = "Profiles")]
59    Profiles(profiles::ResponseItem),
60}
61
62#[cfg(feature = "mcp")]
63impl crate::cli::command::CommandResponse for ResponseItem {
64    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
65        match self {
66            ResponseItem::Get(v) => v.into_mcp(),
67            ResponseItem::GetRequestSchema(v) => v.into_mcp(),
68            ResponseItem::GetResponseSchema(v) => v.into_mcp(),
69            ResponseItem::Favorites(v) => v.into_mcp(),
70            ResponseItem::Inventions(v) => v.into_mcp(),
71            ResponseItem::Profiles(v) => v.into_mcp(),
72        }
73    }
74}
75
76impl TryFrom<Command> for Request {
77    type Error = crate::cli::command::FromArgsError;
78    fn try_from(command: Command) -> Result<Self, Self::Error> {
79        match command {
80            Command::Get(cmd) => match cmd.schema {
81                None => Ok(Request::Get(get::Request::try_from(cmd.args)?)),
82                Some(get::Schema::RequestSchema(args)) =>
83                    Ok(Request::GetRequestSchema(get::request_schema::Request::try_from(args)?)),
84                Some(get::Schema::ResponseSchema(args)) =>
85                    Ok(Request::GetResponseSchema(get::response_schema::Request::try_from(args)?)),
86            },
87            Command::Favorites { command } =>
88                Ok(Request::Favorites(favorites::Request::try_from(command)?)),
89            Command::Inventions { command } =>
90                Ok(Request::Inventions(inventions::Request::try_from(command)?)),
91            Command::Profiles { command } =>
92                Ok(Request::Profiles(profiles::Request::try_from(command)?)),
93        }
94    }
95}
96
97impl crate::cli::command::CommandRequest for Request {
98    fn into_command(&self) -> Vec<String> {
99        match self {
100            Request::Get(inner) => inner.into_command(),
101            Request::GetRequestSchema(inner) => inner.into_command(),
102            Request::GetResponseSchema(inner) => inner.into_command(),
103            Request::Favorites(inner) => inner.into_command(),
104            Request::Inventions(inner) => inner.into_command(),
105            Request::Profiles(inner) => inner.into_command(),
106        }
107    }
108}
109
110#[cfg(feature = "cli-executor")]
111pub async fn execute<E: crate::cli::command::CommandExecutor>(
112    executor: &E,
113    request: Request,
114
115        agent_arguments: Option<&crate::cli::command::AgentArguments>,
116    ) -> Result<
117    std::pin::Pin<Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>>,
118    E::Error,
119> {
120    use futures::StreamExt;
121    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>> =
122        match request {
123            Request::Get(req) => {
124                let value = get::execute(executor, req, agent_arguments).await?;
125                Box::pin(crate::cli::command::StreamOnce::new(Ok(
126                    ResponseItem::Get(value),
127                )))
128            }
129            Request::GetRequestSchema(req) => {
130                let value = get::request_schema::execute(executor, req, agent_arguments).await?;
131                Box::pin(crate::cli::command::StreamOnce::new(Ok(
132                    ResponseItem::GetRequestSchema(value),
133                )))
134            }
135            Request::GetResponseSchema(req) => {
136                let value = get::response_schema::execute(executor, req, agent_arguments).await?;
137                Box::pin(crate::cli::command::StreamOnce::new(Ok(
138                    ResponseItem::GetResponseSchema(value),
139                )))
140            }
141            Request::Favorites(req) => {
142                let inner = favorites::execute(executor, req, agent_arguments).await?;
143                Box::pin(inner.map(|r| r.map(ResponseItem::Favorites)))
144            }
145            Request::Inventions(req) => {
146                let inner = inventions::execute(executor, req, agent_arguments).await?;
147                Box::pin(inner.map(|r| r.map(ResponseItem::Inventions)))
148            }
149            Request::Profiles(req) => {
150                let inner = profiles::execute(executor, req, agent_arguments).await?;
151                Box::pin(inner.map(|r| r.map(ResponseItem::Profiles)))
152            }
153        };
154    Ok(stream)
155}
156
157#[cfg(feature = "cli-executor")]
158pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
159    executor: &E,
160    request: Request,
161    jq: String,
162
163        agent_arguments: Option<&crate::cli::command::AgentArguments>,
164    ) -> Result<
165    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
166    E::Error,
167> {
168    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>> =
169        match request {
170            Request::Get(req) => {
171                let value = get::execute_jq(executor, req, jq, agent_arguments).await?;
172                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
173            }
174            Request::GetRequestSchema(req) => {
175                let value = get::request_schema::execute_jq(executor, req, jq, agent_arguments).await?;
176                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
177            }
178            Request::GetResponseSchema(req) => {
179                let value = get::response_schema::execute_jq(executor, req, jq, agent_arguments).await?;
180                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
181            }
182            Request::Favorites(req) => {
183                let inner = favorites::execute_jq(executor, req, jq, agent_arguments).await?;
184                Box::pin(inner)
185            }
186            Request::Inventions(req) => {
187                let inner = inventions::execute_jq(executor, req, jq, agent_arguments).await?;
188                Box::pin(inner)
189            }
190            Request::Profiles(req) => {
191                let inner = profiles::execute_jq(executor, req, jq, agent_arguments).await?;
192                Box::pin(inner)
193            }
194        };
195    Ok(stream)
196}