Skip to main content

objectiveai_sdk/cli/command/daemon/
mod.rs

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