Skip to main content

objectiveai_sdk/cli/command/channels/
mod.rs

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