Skip to main content

objectiveai_cli/command/agents/instances/
get.rs

1//! `agents instances get` — fetch per-agent aggregates for the EXACT
2//! resolved targets (not their children). An explicitly-named target
3//! always yields an item, zero-filled when it has no activity. Backed
4//! by `db::instances::get_exact`.
5
6use std::collections::BTreeMap;
7use std::pin::Pin;
8
9use futures::Stream;
10use objectiveai_sdk::cli::command::agents::instances::get::{Request, ResponseItem};
11
12use crate::context::Context;
13use crate::error::Error;
14
15type ItemStream = Pin<Box<dyn Stream<Item = Result<ResponseItem, Error>> + Send>>;
16
17pub async fn execute(ctx: &Context, request: Request) -> Result<ItemStream, Error> {
18    let default_parent = ctx.config.agent_instance_hierarchy.clone();
19
20    // Resolve each target to one exact AIH (GROUPED/ABSENT tags skip),
21    // dedup preserving first-seen order.
22    let mut aihs: Vec<String> = Vec::new();
23    for target in request.targets {
24        if let Some(aih) = super::resolve_target(ctx.db_client().await?, target, &default_parent).await? {
25            if !aihs.contains(&aih) {
26                aihs.push(aih);
27            }
28        }
29    }
30
31    // Fetch each agent exactly; BTreeMap sorts + dedups across targets
32    // that resolved to the same AIH.
33    let mut merged: BTreeMap<String, ResponseItem> = BTreeMap::new();
34    for aih in aihs {
35        let mut item = crate::db::instances::get_exact(ctx.db_client().await?, &aih).await?;
36        // The recorded definition source: agent_refs, with the
37        // legacy request-blob fallback. None when neither knows the
38        // agent.
39        item.agent = crate::db::logs::lookup_session(ctx.db_client().await?, &aih)
40            .await?
41            .map(|lookup| lookup.agent);
42        merged.insert(item.agent_instance_hierarchy.clone(), item);
43    }
44
45    let items: Vec<Result<ResponseItem, Error>> =
46        merged.into_values().map(Ok).collect();
47    Ok(Box::pin(futures::stream::iter(items)))
48}
49
50pub mod request_schema {
51    use objectiveai_sdk::cli::command::agents::instances::get as sdk;
52    use objectiveai_sdk::cli::command::agents::instances::get::request_schema::{Request, Response};
53
54    use crate::context::Context;
55    use crate::error::Error;
56
57    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
58        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Request)))
59    }
60}
61
62pub mod response_schema {
63    use objectiveai_sdk::cli::command::agents::instances::get as sdk;
64    use objectiveai_sdk::cli::command::agents::instances::get::response_schema::{Request, Response};
65
66    use crate::context::Context;
67    use crate::error::Error;
68
69    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
70        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::ResponseItem)))
71    }
72}