Skip to main content

objectiveai_cli/command/
command.rs

1//! Root-level dispatch. Mirrors
2//! `objectiveai-sdk-rs/src/cli/command/command.rs` — same `match` shape,
3//! same fan-out, but each arm calls the *local* tier `execute` (which
4//! actually does the work) instead of routing through `CommandExecutor`.
5//!
6//! `run.rs` calls into [`execute`] with the SDK's root `Request` after
7//! parsing argv and running the SDK's `TryFrom<Command> for Request`.
8//! `jq` is *not* applied here — the bare-naked command tree ignores the
9//! `jq` field on incoming requests; `run.rs` is responsible for reading
10//! it off the request and applying it to the stream we return.
11
12use std::pin::Pin;
13
14use futures::{Stream, StreamExt};
15use objectiveai_sdk::cli::command::{Request, ResponseItem};
16
17use crate::context::Context;
18use crate::error::Error;
19
20type ItemStream =
21    Pin<Box<dyn Stream<Item = Result<ResponseItem, Error>> + Send>>;
22
23/// Shapes a unary `Result<T, E>` into a single-element stream so all
24/// root arms can share one return type.
25fn once<T: Send + 'static>(
26    item: Result<T, Error>,
27) -> Pin<Box<dyn Stream<Item = Result<T, Error>> + Send>> {
28    Box::pin(futures::stream::once(async move { item }))
29}
30
31pub async fn execute(ctx: &Context, request: Request) -> Result<ItemStream, Error> {
32    let stream: ItemStream = match request {
33        Request::Agents(req) => {
34            let inner = super::agents::execute(ctx, req).await?;
35            Box::pin(inner.map(|r| r.map(ResponseItem::Agents)))
36        }
37        Request::Api(req) => {
38            let inner = super::api::execute(ctx, req).await?;
39            Box::pin(inner.map(|r| r.map(ResponseItem::Api)))
40        }
41        Request::Daemon(req) => {
42            let inner = super::daemon::execute(ctx, req).await?;
43            Box::pin(inner.map(|r| r.map(ResponseItem::Daemon)))
44        }
45        Request::Db(req) => {
46            let inner = super::db::execute(ctx, req).await?;
47            Box::pin(inner.map(|r| r.map(ResponseItem::Db)))
48        }
49        Request::Functions(req) => {
50            let inner = super::functions::execute(ctx, req).await?;
51            Box::pin(inner.map(|r| r.map(ResponseItem::Functions)))
52        }
53        Request::KillAll(req) => {
54            let value = super::kill_all::execute(ctx, req).await?;
55            once(Ok(ResponseItem::KillAll(value)))
56        }
57        Request::KillAllRequestSchema(req) => {
58            let value = super::kill_all::request_schema::execute(ctx, req).await?;
59            once(Ok(ResponseItem::KillAllRequestSchema(value)))
60        }
61        Request::KillAllResponseSchema(req) => {
62            let value = super::kill_all::response_schema::execute(ctx, req).await?;
63            once(Ok(ResponseItem::KillAllResponseSchema(value)))
64        }
65        Request::Laboratories(req) => {
66            let inner = super::laboratories::execute(ctx, req).await?;
67            Box::pin(inner.map(|r| r.map(ResponseItem::Laboratories)))
68        }
69        Request::Mcp(req) => {
70            let inner = super::mcp::execute(ctx, req).await?;
71            Box::pin(inner.map(|r| r.map(ResponseItem::Mcp)))
72        }
73        Request::Plugins(req) => {
74            let inner = super::plugins::execute(ctx, req).await?;
75            Box::pin(inner.map(|r| r.map(ResponseItem::Plugins)))
76        }
77        Request::Python(req) => {
78            let value = super::python::execute(ctx, req).await?;
79            once(Ok(ResponseItem::Python(value)))
80        }
81        Request::PythonRequestSchema(req) => {
82            let value = super::python::request_schema::execute(ctx, req).await?;
83            once(Ok(ResponseItem::PythonRequestSchema(value)))
84        }
85        Request::PythonResponseSchema(req) => {
86            let value = super::python::response_schema::execute(ctx, req).await?;
87            once(Ok(ResponseItem::PythonResponseSchema(value)))
88        }
89        Request::Swarms(req) => {
90            let inner = super::swarms::execute(ctx, req).await?;
91            Box::pin(inner.map(|r| r.map(ResponseItem::Swarms)))
92        }
93        Request::Tools(req) => {
94            let inner = super::tools::execute(ctx, req).await?;
95            Box::pin(inner.map(|r| r.map(ResponseItem::Tools)))
96        }
97        Request::Update(req) => {
98            let inner = super::update::execute(ctx, req).await?;
99            Box::pin(inner.map(|r| r.map(ResponseItem::Update)))
100        }
101        Request::UpdateRequestSchema(req) => {
102            let value = super::update::request_schema::execute(ctx, req).await?;
103            once(Ok(ResponseItem::UpdateRequestSchema(value)))
104        }
105        Request::UpdateResponseSchema(req) => {
106            let value = super::update::response_schema::execute(ctx, req).await?;
107            once(Ok(ResponseItem::UpdateResponseSchema(value)))
108        }
109        Request::Viewer(req) => {
110            let inner = super::viewer::execute(ctx, req).await?;
111            Box::pin(inner.map(|r| r.map(ResponseItem::Viewer)))
112        }
113    };
114    Ok(stream)
115}