|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use clap::{Args, Subcommand}; |
| 3 | + |
| 4 | +use crate::cli::{self, GlobalArgs}; |
| 5 | +use crate::client::ApiClient; |
| 6 | +use crate::config::Config; |
| 7 | +use crate::output::{render_describe, render_list, resolve_format, TableView}; |
| 8 | + |
| 9 | +#[derive(Debug, Args)] |
| 10 | +pub struct SystemArgs { |
| 11 | + #[command(subcommand)] |
| 12 | + pub command: SystemCommand, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Subcommand)] |
| 16 | +pub enum SystemCommand { |
| 17 | + /// Health check (status, version, uptime) |
| 18 | + Status, |
| 19 | + /// Global statistics (documents, edges, top labels) |
| 20 | + Stats, |
| 21 | + /// Recent agent activities |
| 22 | + Activities { |
| 23 | + #[arg(long)] |
| 24 | + agent: Option<String>, |
| 25 | + #[arg(long)] |
| 26 | + task: Option<String>, |
| 27 | + #[arg(long, default_value_t = 50)] |
| 28 | + limit: u32, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +pub async fn execute(args: SystemArgs, globals: &GlobalArgs) -> Result<()> { |
| 33 | + let config_path = Config::default_path()?; |
| 34 | + let mut config = Config::load(&config_path)?; |
| 35 | + config.apply_profile_override(globals.profile.as_deref())?; |
| 36 | + let client = cli::client_from_globals(&config, globals).await?; |
| 37 | + |
| 38 | + match args.command { |
| 39 | + SystemCommand::Status => status(&client, globals).await, |
| 40 | + SystemCommand::Stats => stats(&client, globals).await, |
| 41 | + SystemCommand::Activities { agent, task, limit } => { |
| 42 | + activities(&client, agent.as_deref(), task.as_deref(), limit, globals).await |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async fn status(client: &ApiClient, globals: &GlobalArgs) -> Result<()> { |
| 48 | + let h = client.health().await.context("get health")?; |
| 49 | + let fmt = resolve_format(globals.format.unwrap_or_default()); |
| 50 | + println!("{}", render_describe(&h, fmt)?); |
| 51 | + Ok(()) |
| 52 | +} |
| 53 | + |
| 54 | +async fn stats(client: &ApiClient, globals: &GlobalArgs) -> Result<()> { |
| 55 | + let s = client.stats().await.context("get stats")?; |
| 56 | + let fmt = resolve_format(globals.format.unwrap_or_default()); |
| 57 | + println!("{}", render_describe(&s, fmt)?); |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +async fn activities( |
| 62 | + client: &ApiClient, |
| 63 | + agent_id: Option<&str>, |
| 64 | + task_id: Option<&str>, |
| 65 | + limit: u32, |
| 66 | + globals: &GlobalArgs, |
| 67 | +) -> Result<()> { |
| 68 | + let acts = client |
| 69 | + .list_activities(agent_id, task_id, Some(limit)) |
| 70 | + .await |
| 71 | + .context("list activities")?; |
| 72 | + let view = TableView { |
| 73 | + headers: vec!["WHEN", "AGENT", "TYPE", "STATUS", "TASK", "COST"], |
| 74 | + rows: acts |
| 75 | + .iter() |
| 76 | + .map(|a| { |
| 77 | + vec![ |
| 78 | + a.created_at.clone(), |
| 79 | + a.agent_id.clone(), |
| 80 | + a.activity_type.clone(), |
| 81 | + a.status.clone(), |
| 82 | + a.task_id |
| 83 | + .as_deref() |
| 84 | + .map(short_id) |
| 85 | + .unwrap_or_else(|| "-".into()), |
| 86 | + format!("{:.4}", a.cost_usd), |
| 87 | + ] |
| 88 | + }) |
| 89 | + .collect(), |
| 90 | + }; |
| 91 | + let fmt = resolve_format(globals.format.unwrap_or_default()); |
| 92 | + let out = render_list(&acts, view, fmt)?; |
| 93 | + println!("{out}"); |
| 94 | + Ok(()) |
| 95 | +} |
| 96 | + |
| 97 | +fn short_id(id: &str) -> String { |
| 98 | + if id.len() <= 8 { |
| 99 | + id.to_string() |
| 100 | + } else { |
| 101 | + format!("{}…", &id[..8]) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + use clap::Parser; |
| 109 | + |
| 110 | + #[derive(Parser)] |
| 111 | + struct Wrapper { |
| 112 | + #[command(subcommand)] |
| 113 | + cmd: SystemCommand, |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn parses_status() { |
| 118 | + let w = Wrapper::try_parse_from(["test", "status"]).unwrap(); |
| 119 | + assert!(matches!(w.cmd, SystemCommand::Status)); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn parses_activities_with_filters() { |
| 124 | + let w = |
| 125 | + Wrapper::try_parse_from(["test", "activities", "--agent", "coder", "--limit", "10"]) |
| 126 | + .unwrap(); |
| 127 | + match w.cmd { |
| 128 | + SystemCommand::Activities { agent, task, limit } => { |
| 129 | + assert_eq!(agent.as_deref(), Some("coder")); |
| 130 | + assert!(task.is_none()); |
| 131 | + assert_eq!(limit, 10); |
| 132 | + } |
| 133 | + _ => panic!("expected Activities"), |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments