pub mod accounts;
pub mod compact;
pub mod doctor;
pub mod limits;
pub mod models;
pub mod prices;
pub mod report;
pub mod setup;
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum};
use crate::core::period::Period;
use crate::index::query::{Dim, Filter};
use crate::ui::style::{ColorChoice, Painter};
#[derive(Parser, Debug)]
#[command(
name = "swarmy",
version,
about = "Real-time analytics dashboard for coding agents (Claude Code, Antigravity, Codex): live swarm, history, API-equivalent spend, weekly limits, login vault",
long_about = None,
after_help = "Run without a command for the live dashboard. Data never leaves this machine."
)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
#[arg(long, value_enum, global = true, default_value_t = ColorChoice::Auto)]
pub color: ColorChoice,
#[arg(long, global = true)]
pub mask: bool,
#[arg(long, global = true)]
pub no_boot: bool,
#[arg(long, global = true)]
pub eco: bool,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Report(ReportArgs),
Models(PeriodFilterArgs),
Limits {
#[arg(long, value_enum, default_value_t = Format::Table)]
format: Format,
#[arg(long, default_value_t = 8)]
top: usize,
},
Accounts(accounts::AccountsArgs),
Doctor,
Compact(compact::CompactArgs),
Setup(setup::SetupArgs),
Jump {
#[arg(long)]
list: bool,
},
Statusline {
#[arg(long)]
then: Option<String>,
},
Prices {
#[arg(long, value_enum, default_value_t = Format::Table)]
format: Format,
},
Demo {
#[arg(long, hide = true)]
record: Option<std::path::PathBuf>,
#[arg(long, hide = true, default_value_t = 100)]
frames: usize,
#[arg(long, hide = true, default_value = "140x40")]
size: String,
#[arg(long, hide = true, default_value = "")]
keys: String,
#[arg(long, hide = true)]
wall: bool,
#[arg(long, hide = true)]
codex_only: bool,
},
Completions {
#[arg(value_enum)]
shell: clap_complete::Shell,
},
Man,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Format {
Table,
Json,
Csv,
}
#[derive(Args, Debug, Clone)]
pub struct FilterArgs {
#[arg(long)]
pub account: Option<String>,
#[arg(long)]
pub seat: Option<String>,
#[arg(long, value_enum)]
pub harness: Option<crate::core::harness::Harness>,
#[arg(long)]
pub project: Option<String>,
#[arg(long)]
pub model: Option<String>,
#[arg(long, value_enum)]
pub checkout: Option<CheckoutArg>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum CheckoutArg {
Worktrees,
Main,
}
impl FilterArgs {
pub fn to_filter(
&self,
ctx: &crate::app::context::Ctx,
idx: &crate::index::Index,
) -> anyhow::Result<Filter> {
let seat = match &self.seat {
Some(s) => Some(ctx.resolve_seat(s)?.id.clone()),
None => None,
};
let account = match &self.account {
Some(a) => Some(ctx.resolve_account(idx, a)?),
None => None,
};
Ok(Filter {
account,
seat,
harness: self.harness,
project: self.project.clone(),
model: self.model.clone(),
in_worktree: self.checkout.map(|c| c == CheckoutArg::Worktrees),
})
}
}
#[derive(Args, Debug, Clone)]
pub struct PeriodFilterArgs {
#[arg(short, long, default_value = "week")]
pub period: Period,
#[command(flatten)]
pub filter: FilterArgs,
#[arg(long, value_enum, default_value_t = Format::Table)]
pub format: Format,
}
#[derive(Args, Debug, Clone)]
pub struct ReportArgs {
#[arg(short, long, value_enum, default_value_t = Dim::Project)]
pub by: Dim,
#[arg(short, long, default_value = "week")]
pub period: Period,
#[command(flatten)]
pub filter: FilterArgs,
#[arg(long, default_value_t = 15)]
pub top: usize,
#[arg(long, value_enum, default_value_t = Format::Table)]
pub format: Format,
}
pub fn run(cli: Cli) -> anyhow::Result<()> {
let painter = Painter::new(cli.color);
match cli.command {
Some(Command::Completions { shell }) => {
clap_complete::generate(shell, &mut Cli::command(), "swarmy", &mut std::io::stdout());
Ok(())
}
Some(Command::Jump { list }) => jump(list),
Some(Command::Statusline { then }) => {
let dirs = crate::config::Dirs::resolve()?;
let config = crate::config::Config::load(&dirs).unwrap_or_else(|e| {
crate::diag::log("config", &format!("{e:#}"));
crate::config::Config::default()
});
crate::statusline::run(&dirs, &config, then.as_deref())
}
Some(Command::Man) => {
clap_mangen::Man::new(Cli::command()).render(&mut std::io::stdout())?;
Ok(())
}
Some(Command::Demo {
record,
frames,
size,
keys,
wall,
codex_only,
}) => {
let dirs = crate::config::Dirs::resolve()?;
let load = if wall {
crate::demo::Load::Wall
} else if codex_only {
crate::demo::Load::CodexOnly
} else {
crate::demo::Load::Demo
};
let demo = crate::demo::start(&dirs.data.join("demo"), load)?;
let mut ctx = crate::app::context::Ctx::demo(painter, demo.seats.clone(), &demo.root)?;
ctx.mask = cli.mask;
{
let mut v = crate::vault::Vault::open(&ctx.dirs.vault_dir())?;
for s in ctx.seats.iter().filter(|s| s.harness.supports_vault()) {
v.edit(|tx| tx.capture(s, Some(&format!("alex-{}", s.label))))?;
}
if !codex_only {
let spare = crate::claude::credentials::Secret::new(serde_json::json!({
"accessToken": "demo-access-spare", "refreshToken": "demo-refresh-spare",
"refreshTokenExpiresAt": (chrono::Utc::now() + chrono::Duration::days(4)).timestamp_millis()
}));
let acct = crate::claude::config_json::Account {
account_uuid: "acc-demo-spare".into(),
email: Some("alex+spare@example.com".into()),
..Default::default()
};
v.edit(|tx| tx.put(&spare, Some(serde_json::json!({"accountUuid": "acc-demo-spare", "emailAddress": "alex+spare@example.com"})), Some(&acct), Some("alex-spare"), None))?;
let team = crate::claude::credentials::Secret::of(
crate::core::harness::Harness::Agy,
serde_json::json!({"token": {"access_token": "demo-agy-access-team", "refresh_token": "demo-agy-refresh-team",
"expiry": (chrono::Utc::now() + chrono::Duration::hours(1)).to_rfc3339()},
"auth_method": "consumer", "id_token_email": "alex.team@example.com"}),
);
let team_acct = crate::agy::account::account_of(team.expose());
v.edit(|tx| {
tx.put(&team, None, team_acct.as_ref(), Some("alex-g-team"), None)
})?;
}
if wall && let Some(lab) = ctx.seats.iter().find(|s| s.label == "lab") {
v.edit(|tx| tx.switch(lab, "alex-work"))?;
}
}
ctx.reload_seat_info()?;
match record {
Some(out) => {
let (w, h) = size
.split_once('x')
.and_then(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?)))
.unwrap_or((140, 40));
let keys: Vec<(usize, char)> = keys
.split(',')
.filter_map(|p| {
let (f, k) = p.split_once(':')?;
let c = match k {
"enter" => '\n',
"esc" => '\x1b',
k => k.chars().next()?,
};
Some((f.parse().ok()?, c))
})
.collect();
crate::tui::record(ctx, &out, w, h, frames, &keys)
}
None => crate::tui::run(ctx, cli.no_boot, cli.eco),
}
}
command => {
let ctx = crate::app::context::Ctx::load(painter, cli.mask)?;
match command {
Some(Command::Report(a)) => report::run(&ctx, &a),
Some(Command::Models(a)) => models::run(&ctx, &a),
Some(Command::Limits { format, top }) => limits::run(&ctx, format, top),
Some(Command::Accounts(a)) => accounts::run(&ctx, &a),
Some(Command::Doctor) => doctor::run(&ctx),
Some(Command::Compact(a)) => compact::run(&ctx, &a),
Some(Command::Prices { format }) => prices::run(&ctx, format),
None => {
use std::io::IsTerminal;
if std::io::stdout().is_terminal() {
crate::tui::run(ctx, cli.no_boot, cli.eco)
} else {
limits::run(&ctx, Format::Table, 8)
}
}
Some(Command::Setup(a)) => setup::run(&ctx, &a),
Some(
Command::Completions { .. }
| Command::Man
| Command::Demo { .. }
| Command::Statusline { .. }
| Command::Jump { .. },
) => {
unreachable!()
}
}
}
}
}
fn jump(list: bool) -> anyhow::Result<()> {
let dirs = crate::config::Dirs::resolve()?;
let config = crate::config::Config::load(&dirs)?;
let now = chrono::Utc::now();
use crate::core::pane::Pane;
let waiting: Vec<(String, String, Option<Pane>)> = match crate::fleet::read_fresh(
&crate::fleet::path(&dirs.data),
config.statusline.fresh_seconds,
now,
) {
Some(f) => f
.needs_you
.into_iter()
.map(|n| (n.name, n.what, n.pane))
.collect(),
None => {
let env_dir = crate::statusline::current_config_dir(&dirs.home);
let mut found: Vec<(String, String, Option<crate::core::pane::Pane>)> =
crate::claude::seats::discover(&crate::claude::seats::Candidates {
home: &dirs.home,
env_dirs: vec![env_dir],
configured: config.seats_of(&dirs.home, crate::core::harness::Harness::Claude),
})
.iter()
.flat_map(|s| crate::claude::registry::waiting_sessions(&s.sessions_dir()))
.map(|w| (w.label(), w.what.clone(), w.pane.clone()))
.collect();
let agy_seats =
crate::agy::discovery::discover(&crate::agy::discovery::AgyCandidates {
home: &dirs.home,
env_dirs: Vec::new(),
configured: config.seats_of(&dirs.home, crate::core::harness::Harness::Agy),
});
for seat in &agy_seats {
let agy_dir = &seat.dir;
let locked = crate::agy::live::locked_conversations_once(agy_dir);
if locked.is_empty() {
continue;
}
let mut cache = crate::agy::live::AgyLiveCache::default();
cache.resolve_procs(agy_dir, &locked);
let metas = crate::agy::summaries::read_all(agy_dir);
for cid in &locked {
if let Some(m) = metas.get(cid)
&& m.status == crate::agy::meta::RunStatus::WaitingForUser
{
let name = m
.title
.clone()
.unwrap_or_else(|| crate::core::ids::short(cid).to_string());
let pane = cache.proc_of(cid).and_then(|p| p.pane.clone());
found.push((name, "input needed".into(), pane));
}
}
}
found
}
};
if waiting.is_empty() {
println!("nothing waits for you");
return Ok(());
}
if list {
for (name, what, pane) in &waiting {
println!(
"▲ {name}: {what}{}",
pane.as_ref()
.map(|p| format!(" ({p})"))
.unwrap_or_default()
);
}
return Ok(());
}
let Some((name, what, Some(pane))) = waiting.iter().find(|w| w.2.is_some()).cloned() else {
anyhow::bail!(
"{} session(s) wait for you, none in a supported terminal (tmux, zellij, kitty, wezterm, iTerm2, Warp)",
waiting.len()
);
};
match crate::terminal::jump(&pane) {
crate::terminal::Jump::Done => {
println!("→ {name}: {what}");
Ok(())
}
crate::terminal::Jump::Unreachable(why) | crate::terminal::Jump::Failed(why) => {
anyhow::bail!(why)
}
}
}