Skip to main content

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

1//! `agents instances list` — enumerate agent instances under one or
2//! more targets, reporting per-agent aggregates (tags, queued count,
3//! spawn/active timestamps, total logged messages).
4
5use crate::cli::command::CommandRequest;
6
7/// Reuse the shared `--target` enum (`instance=L[,parent=P]`, `tag=T`,
8/// `me`) from `agents logs read all`. Each target resolves to an AIH
9/// whose direct children this command lists.
10pub use super::super::logs::read::all::Target;
11
12#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
13#[schemars(rename = "cli.command.agents.instances.list.Request")]
14pub struct Request {
15    pub path_type: Path,
16    pub targets: Vec<Target>,
17    pub jq: Option<String>,
18}
19
20#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
21#[schemars(rename = "cli.command.agents.instances.list.Path")]
22pub enum Path {
23    #[serde(rename = "agents/instances/list")]
24    AgentsInstancesList,
25}
26
27impl CommandRequest for Request {
28    fn into_command(&self) -> Vec<String> {
29        let mut argv = vec![
30            "agents".to_string(),
31            "instances".to_string(),
32            "list".to_string(),
33        ];
34        for target in &self.targets {
35            argv.push("--target".to_string());
36            argv.push(target.into_arg_string());
37        }
38        if let Some(jq) = &self.jq {
39            argv.push("--jq".to_string());
40            argv.push(jq.clone());
41        }
42        argv
43    }
44}
45
46/// One discovered agent instance under a target. Aggregated from the
47/// `logs.messages`, `message_queue`, and `tags` tiers.
48#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
49#[schemars(rename = "cli.command.agents.instances.list.ResponseItem")]
50pub struct ResponseItem {
51    /// Full hierarchy of this agent instance.
52    pub agent_instance_hierarchy: String,
53    /// Tag names currently bound to this AIH, newest-bound first.
54    pub tags: Vec<String>,
55    /// Active `message_queue` rows targeting this agent — counting
56    /// both direct-AIH rows and rows whose tag is bound to this AIH.
57    pub queued: u64,
58    /// Timestamp of the first `logs.messages` row for this agent.
59    /// `None` when the agent has no logs yet (queue-only).
60    #[serde(default, skip_serializing_if = "Option::is_none")]
61    #[schemars(extend("omitempty" = true))]
62    pub timestamp_spawned: Option<i64>,
63    /// Timestamp of the most recent `logs.messages` row for this
64    /// agent. `None` when the agent has no logs yet (queue-only).
65    #[serde(default, skip_serializing_if = "Option::is_none")]
66    #[schemars(extend("omitempty" = true))]
67    pub timestamp_active: Option<i64>,
68    /// Total `logs.messages` rows for this agent over all time.
69    pub logged: u64,
70}
71
72#[derive(clap::Args)]
73pub struct Args {
74    /// One or more `--target instance=L[,parent=P]` entries. Also
75    /// accepts `--target tag=T` and `--target me`. Lists the direct
76    /// children of each resolved target.
77    #[arg(long = "target", required = true)]
78    pub targets: Vec<String>,
79    /// jq filter applied to the JSON output.
80    #[arg(long)]
81    pub jq: Option<String>,
82}
83
84#[derive(clap::Args)]
85#[command(args_conflicts_with_subcommands = true)]
86pub struct Command {
87    #[command(flatten)]
88    pub args: Args,
89    #[command(subcommand)]
90    pub schema: Option<Schema>,
91}
92
93#[derive(clap::Subcommand)]
94pub enum Schema {
95    /// Emit the JSON Schema for this leaf's `Request` type and exit.
96    RequestSchema(request_schema::Args),
97    /// Emit the JSON Schema for this leaf's `Response` type and exit.
98    ResponseSchema(response_schema::Args),
99}
100
101impl TryFrom<Args> for Request {
102    type Error = crate::cli::command::FromArgsError;
103    fn try_from(args: Args) -> Result<Self, Self::Error> {
104        let targets = args
105            .targets
106            .iter()
107            .map(|s| {
108                s.parse::<Target>().map_err(|msg| {
109                    crate::cli::command::FromArgsError::path_parse("target", msg)
110                })
111            })
112            .collect::<Result<Vec<_>, _>>()?;
113        Ok(Self {
114            path_type: Path::AgentsInstancesList,
115            targets,
116            jq: args.jq,
117        })
118    }
119}
120
121#[cfg(feature = "cli-executor")]
122pub async fn execute<E: crate::cli::command::CommandExecutor>(
123    executor: &E,
124    mut request: Request,
125
126        agent_arguments: Option<&crate::cli::command::AgentArguments>,
127    ) -> Result<E::Stream<ResponseItem>, E::Error> {
128    request.jq = None;
129    executor.execute(request, agent_arguments).await
130}
131
132#[cfg(feature = "cli-executor")]
133pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
134    executor: &E,
135    mut request: Request,
136    jq: String,
137
138        agent_arguments: Option<&crate::cli::command::AgentArguments>,
139    ) -> Result<E::Stream<serde_json::Value>, E::Error> {
140    request.jq = Some(jq);
141    executor.execute(request, agent_arguments).await
142}
143
144#[cfg(feature = "mcp")]
145impl crate::cli::command::CommandResponse for ResponseItem {
146    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
147        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
148    }
149}
150
151pub mod request_schema;
152
153
154pub mod response_schema;