Skip to main content

objectiveai_sdk/cli/command/agents/logs/token_usage/get/
mod.rs

1//! `agents logs token-usage get` — read an agent's current stored
2//! `total_tokens` snapshot (no waiting). `total_tokens` is null when no
3//! agent-completion usage has been recorded for the AIH yet.
4
5use crate::cli::command::CommandRequest;
6
7#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
8#[schemars(rename = "cli.command.agents.logs.token_usage.get.Request")]
9pub struct Request {
10    pub path_type: Path,
11    /// The full agent instance hierarchy to read.
12    pub agent_instance_hierarchy: String,
13    #[serde(flatten)]
14    pub base: crate::cli::command::RequestBase,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
18#[schemars(rename = "cli.command.agents.logs.token_usage.get.Path")]
19pub enum Path {
20    #[serde(rename = "agents/logs/token-usage/get")]
21    AgentsLogsTokenUsageGet,
22}
23
24impl CommandRequest for Request {
25    fn request_base(&self) -> &crate::cli::command::RequestBase {
26        &self.base
27    }
28
29    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
30        Some(&mut self.base)
31    }
32}
33
34/// The current stored snapshot. `total_tokens` is `None` when no
35/// agent-completion usage has been recorded for this AIH yet.
36#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
37#[schemars(rename = "cli.command.agents.logs.token_usage.get.Response")]
38pub struct Response {
39    pub agent_instance_hierarchy: String,
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    #[schemars(extend("omitempty" = true))]
42    pub total_tokens: Option<i64>,
43}
44
45#[derive(clap::Args)]
46#[command(group(clap::ArgGroup::new("aih_required").required(true).args(["agent_instance_hierarchy"])))]
47pub struct Args {
48    /// The full agent instance hierarchy to read.
49    #[arg(long = "agent-instance-hierarchy")]
50    pub agent_instance_hierarchy: Option<String>,
51    #[command(flatten)]
52    pub base: crate::cli::command::RequestBaseArgs,
53}
54
55#[derive(clap::Args)]
56#[command(args_conflicts_with_subcommands = true)]
57pub struct Command {
58    #[command(flatten)]
59    pub args: Args,
60    #[command(subcommand)]
61    pub schema: Option<Schema>,
62}
63
64#[derive(clap::Subcommand)]
65pub enum Schema {
66    /// Emit the JSON Schema for this leaf's `Request` type and exit.
67    RequestSchema(request_schema::Args),
68    /// Emit the JSON Schema for this leaf's `Response` type and exit.
69    ResponseSchema(response_schema::Args),
70}
71
72impl TryFrom<Args> for Request {
73    type Error = crate::cli::command::FromArgsError;
74    fn try_from(args: Args) -> Result<Self, Self::Error> {
75        Ok(Self {
76            path_type: Path::AgentsLogsTokenUsageGet,
77            agent_instance_hierarchy: args.agent_instance_hierarchy.ok_or_else(|| {
78                crate::cli::command::FromArgsError::path_parse(
79                    "agent-instance-hierarchy",
80                    "--agent-instance-hierarchy is required".to_string(),
81                )
82            })?,
83            base: args.base.into(),
84        })
85    }
86}
87
88#[cfg(feature = "mcp")]
89impl crate::cli::command::CommandResponse for Response {
90    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
91        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
92    }
93}
94
95#[cfg(feature = "cli-executor")]
96pub async fn execute<E: crate::cli::command::CommandExecutor>(
97    executor: &E,
98    mut request: Request,
99    agent_arguments: Option<&crate::cli::command::AgentArguments>,
100) -> Result<Response, E::Error> {
101    request.base.clear_transform();
102    executor.execute_one(request, agent_arguments).await
103}
104
105#[cfg(feature = "cli-executor")]
106pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
107    executor: &E,
108    mut request: Request,
109    transform: crate::cli::command::Transform,
110    agent_arguments: Option<&crate::cli::command::AgentArguments>,
111) -> Result<serde_json::Value, E::Error> {
112    request.base.set_transform(transform);
113    executor.execute_one(request, agent_arguments).await
114}
115
116pub mod request_schema;
117
118pub mod response_schema;
119
120/// One `/listen` broadcast run of `agents logs token_usage get`: the actual
121/// [`Request`], the producer's
122/// [`AgentArguments`](crate::cli::command::AgentArguments), and the
123/// unary response future. See [`crate::cli::websocket_listener`].
124#[cfg(feature = "cli-listener")]
125pub struct ListenerExecution {
126    pub request: Request,
127    pub agent_arguments: crate::cli::command::AgentArguments,
128    pub response: crate::cli::websocket_listener::UnaryResponse<Response>,
129}