Skip to main content

objectiveai_sdk/cli/command/agents/instances/
mod.rs

1//! `agents instances` — caller-side handles that didn't earn their
2//! own top-level home under `agents`. Two leaves today:
3//!
4//! - `get` — per-agent aggregates for the exact resolved targets.
5//! - `list` — the direct children of the resolved targets.
6//!
7//! Historically this tier also owned `spawn`, `message`, and `read`;
8//! those moved up to `agents spawn`, `agents message`, and
9//! `agents logs read` respectively.
10
11use crate::cli::command::CommandRequest;
12
13pub mod get;
14pub mod list;
15
16#[derive(clap::Subcommand)]
17pub enum Command {
18    /// Per-agent aggregates for the exact resolved targets.
19    Get(get::Command),
20    /// List the direct children of the resolved targets.
21    List(list::Command),
22}
23
24#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
25#[serde(untagged)]
26#[schemars(rename = "cli.command.agents.instances.Request")]
27pub enum Request {
28    #[schemars(title = "Get")]
29    Get(get::Request),
30    #[schemars(title = "GetRequestSchema")]
31    GetRequestSchema(get::request_schema::Request),
32    #[schemars(title = "GetResponseSchema")]
33    GetResponseSchema(get::response_schema::Request),
34    #[schemars(title = "List")]
35    List(list::Request),
36    #[schemars(title = "ListRequestSchema")]
37    ListRequestSchema(list::request_schema::Request),
38    #[schemars(title = "ListResponseSchema")]
39    ListResponseSchema(list::response_schema::Request),
40}
41
42// Exempt from json-schema coverage: tier aggregate (see the root
43// `ResponseItem` in command.rs - TS7056).
44#[objectiveai_sdk_macros::json_schema_ignore]
45#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
46#[schemars(rename = "cli.command.agents.instances.ResponseItem")]
47#[serde(untagged)]
48pub enum ResponseItem {
49    #[schemars(title = "Get")]
50    Get(get::ResponseItem),
51    #[schemars(title = "GetRequestSchema")]
52    GetRequestSchema(get::request_schema::Response),
53    #[schemars(title = "GetResponseSchema")]
54    GetResponseSchema(get::response_schema::Response),
55    #[schemars(title = "List")]
56    List(list::ResponseItem),
57    #[schemars(title = "ListRequestSchema")]
58    ListRequestSchema(list::request_schema::Response),
59    #[schemars(title = "ListResponseSchema")]
60    ListResponseSchema(list::response_schema::Response),
61}
62
63#[cfg(feature = "mcp")]
64impl crate::cli::command::CommandResponse for ResponseItem {
65    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
66        match self {
67            ResponseItem::Get(v) => v.into_mcp(),
68            ResponseItem::GetRequestSchema(v) => v.into_mcp(),
69            ResponseItem::GetResponseSchema(v) => v.into_mcp(),
70            ResponseItem::List(v) => v.into_mcp(),
71            ResponseItem::ListRequestSchema(v) => v.into_mcp(),
72            ResponseItem::ListResponseSchema(v) => v.into_mcp(),
73        }
74    }
75}
76
77impl TryFrom<Command> for Request {
78    type Error = crate::cli::command::FromArgsError;
79    fn try_from(command: Command) -> Result<Self, Self::Error> {
80        match command {
81            Command::Get(cmd) => match cmd.schema {
82                None => Ok(Request::Get(get::Request::try_from(cmd.args)?)),
83                Some(get::Schema::RequestSchema(args)) =>
84                    Ok(Request::GetRequestSchema(get::request_schema::Request::try_from(args)?)),
85                Some(get::Schema::ResponseSchema(args)) =>
86                    Ok(Request::GetResponseSchema(get::response_schema::Request::try_from(args)?)),
87            },
88            Command::List(cmd) => match cmd.schema {
89                None => Ok(Request::List(list::Request::try_from(cmd.args)?)),
90                Some(list::Schema::RequestSchema(args)) =>
91                    Ok(Request::ListRequestSchema(list::request_schema::Request::try_from(args)?)),
92                Some(list::Schema::ResponseSchema(args)) =>
93                    Ok(Request::ListResponseSchema(list::response_schema::Request::try_from(args)?)),
94            },
95        }
96    }
97}
98
99impl CommandRequest for Request {
100    fn into_command(&self) -> Vec<String> {
101        match self {
102            Request::Get(inner) => inner.into_command(),
103            Request::GetRequestSchema(inner) => inner.into_command(),
104            Request::GetResponseSchema(inner) => inner.into_command(),
105            Request::List(inner) => inner.into_command(),
106            Request::ListRequestSchema(inner) => inner.into_command(),
107            Request::ListResponseSchema(inner) => inner.into_command(),
108        }
109    }
110}
111
112#[cfg(feature = "cli-executor")]
113pub async fn execute<E: crate::cli::command::CommandExecutor>(
114    executor: &E,
115    request: Request,
116    agent_arguments: Option<&crate::cli::command::AgentArguments>,
117) -> Result<
118    std::pin::Pin<Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>>,
119    E::Error,
120> {
121    use futures::StreamExt;
122    let stream: std::pin::Pin<
123        Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>,
124    > = match request {
125        Request::Get(req) => {
126            let inner = get::execute(executor, req, agent_arguments).await?;
127            Box::pin(inner.map(|r| r.map(ResponseItem::Get)))
128        }
129        Request::GetRequestSchema(req) => {
130            let value = get::request_schema::execute(executor, req, agent_arguments).await?;
131            Box::pin(crate::cli::command::StreamOnce::new(Ok(
132                ResponseItem::GetRequestSchema(value),
133            )))
134        }
135        Request::GetResponseSchema(req) => {
136            let value = get::response_schema::execute(executor, req, agent_arguments).await?;
137            Box::pin(crate::cli::command::StreamOnce::new(Ok(
138                ResponseItem::GetResponseSchema(value),
139            )))
140        }
141        Request::List(req) => {
142            let inner = list::execute(executor, req, agent_arguments).await?;
143            Box::pin(inner.map(|r| r.map(ResponseItem::List)))
144        }
145        Request::ListRequestSchema(req) => {
146            let value = list::request_schema::execute(executor, req, agent_arguments).await?;
147            Box::pin(crate::cli::command::StreamOnce::new(Ok(
148                ResponseItem::ListRequestSchema(value),
149            )))
150        }
151        Request::ListResponseSchema(req) => {
152            let value = list::response_schema::execute(executor, req, agent_arguments).await?;
153            Box::pin(crate::cli::command::StreamOnce::new(Ok(
154                ResponseItem::ListResponseSchema(value),
155            )))
156        }
157    };
158    Ok(stream)
159}
160
161#[cfg(feature = "cli-executor")]
162pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
163    executor: &E,
164    request: Request,
165    jq: String,
166    agent_arguments: Option<&crate::cli::command::AgentArguments>,
167) -> Result<
168    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
169    E::Error,
170> {
171    let stream: std::pin::Pin<
172        Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>,
173    > = match request {
174        Request::Get(req) => {
175            let inner = get::execute_jq(executor, req, jq, agent_arguments).await?;
176            Box::pin(inner)
177        }
178        Request::GetRequestSchema(req) => {
179            let value =
180                get::request_schema::execute_jq(executor, req, jq, agent_arguments).await?;
181            Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
182        }
183        Request::GetResponseSchema(req) => {
184            let value =
185                get::response_schema::execute_jq(executor, req, jq, agent_arguments).await?;
186            Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
187        }
188        Request::List(req) => {
189            let inner = list::execute_jq(executor, req, jq, agent_arguments).await?;
190            Box::pin(inner)
191        }
192        Request::ListRequestSchema(req) => {
193            let value =
194                list::request_schema::execute_jq(executor, req, jq, agent_arguments).await?;
195            Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
196        }
197        Request::ListResponseSchema(req) => {
198            let value =
199                list::response_schema::execute_jq(executor, req, jq, agent_arguments).await?;
200            Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
201        }
202    };
203    Ok(stream)
204}