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::Db(req) => {
42            let inner = super::db::execute(ctx, req).await?;
43            Box::pin(inner.map(|r| r.map(ResponseItem::Db)))
44        }
45        Request::Functions(req) => {
46            let inner = super::functions::execute(ctx, req).await?;
47            Box::pin(inner.map(|r| r.map(ResponseItem::Functions)))
48        }
49        Request::Mcp(req) => {
50            let inner = super::mcp::execute(ctx, req).await?;
51            Box::pin(inner.map(|r| r.map(ResponseItem::Mcp)))
52        }
53        Request::Plugins(req) => {
54            let inner = super::plugins::execute(ctx, req).await?;
55            Box::pin(inner.map(|r| r.map(ResponseItem::Plugins)))
56        }
57        Request::Swarms(req) => {
58            let inner = super::swarms::execute(ctx, req).await?;
59            Box::pin(inner.map(|r| r.map(ResponseItem::Swarms)))
60        }
61        Request::Tasks(req) => {
62            let inner = super::tasks::execute(ctx, req).await?;
63            Box::pin(inner.map(|r| r.map(ResponseItem::Tasks)))
64        }
65        Request::Tools(req) => {
66            let inner = super::tools::execute(ctx, req).await?;
67            Box::pin(inner.map(|r| r.map(ResponseItem::Tools)))
68        }
69        Request::Update(req) => {
70            let inner = super::update::execute(ctx, req).await?;
71            Box::pin(inner.map(|r| r.map(ResponseItem::Update)))
72        }
73        Request::UpdateRequestSchema(req) => {
74            let value = super::update::request_schema::execute(ctx, req).await?;
75            once(Ok(ResponseItem::UpdateRequestSchema(value)))
76        }
77        Request::UpdateResponseSchema(req) => {
78            let value = super::update::response_schema::execute(ctx, req).await?;
79            once(Ok(ResponseItem::UpdateResponseSchema(value)))
80        }
81        Request::Viewer(req) => {
82            let inner = super::viewer::execute(ctx, req).await?;
83            Box::pin(inner.map(|r| r.map(ResponseItem::Viewer)))
84        }
85    };
86    Ok(stream)
87}