Skip to main content

objectiveai_sdk/cli/command/viewer/
mod.rs

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