| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | pub mod admin; |
| 6 | | pub mod agent; |
| 7 | | pub mod format; |
| 8 | | pub mod identity; |
| 9 | | pub mod json; |
| 10 | | pub mod marks; |
| 11 | | pub mod ui; |
| 12 | |
|
| 13 | | use anyhow::{bail, Result}; |
| 14 | | use clap::{Parser, Subcommand}; |
| 15 | | use std::path::PathBuf; |
| 16 | |
|
| 17 | | #[derive(Parser)] |
| 18 | | #[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")] |
| 19 | | pub struct Cli { |
| 20 | | |
| 21 | | #[arg(long, global = true)] |
| 22 | | pub plain: bool, |
| 23 | | |
| 24 | | #[arg(long, global = true, env = "SPECTRA_DATA")] |
| 25 | | pub data: Option<PathBuf>, |
| 26 | | #[command(subcommand)] |
| 27 | | pub command: Command, |
| 28 | | } |
| 29 | |
|
| 30 | | #[derive(Subcommand)] |
| 31 | | pub enum Command { |
| 32 | | |
| 33 | | Version { |
| 34 | | |
| 35 | | #[arg(long)] |
| 36 | | json: bool, |
| 37 | | }, |
| 38 | | |
| 39 | | |
| 40 | | Init { |
| 41 | | |
| 42 | | #[arg(long)] |
| 43 | | owner_home: Option<PathBuf>, |
| 44 | | |
| 45 | | #[arg(long)] |
| 46 | | name: Option<String>, |
| 47 | | }, |
| 48 | | |
| 49 | | Identity { |
| 50 | | #[command(subcommand)] |
| 51 | | command: IdentityCmd, |
| 52 | | }, |
| 53 | | |
| 54 | | Agent { |
| 55 | | #[command(subcommand)] |
| 56 | | command: AgentCmd, |
| 57 | | }, |
| 58 | | |
| 59 | | Admin { |
| 60 | | #[command(subcommand)] |
| 61 | | command: Admin, |
| 62 | | }, |
| 63 | | } |
| 64 | |
|
| 65 | | #[derive(Subcommand)] |
| 66 | | pub enum IdentityCmd { |
| 67 | | |
| 68 | | Show { |
| 69 | | #[arg(long)] |
| 70 | | json: bool, |
| 71 | | }, |
| 72 | | |
| 73 | | Refresh { |
| 74 | | #[arg(long)] |
| 75 | | owner_home: Option<PathBuf>, |
| 76 | | }, |
| 77 | | } |
| 78 | |
|
| 79 | | #[derive(Subcommand)] |
| 80 | | pub enum AgentCmd { |
| 81 | | |
| 82 | | Add { |
| 83 | | |
| 84 | | name: String, |
| 85 | | |
| 86 | | #[arg(long)] |
| 87 | | kind: String, |
| 88 | | |
| 89 | | #[arg(long)] |
| 90 | | project: String, |
| 91 | | |
| 92 | | #[arg(long)] |
| 93 | | home: PathBuf, |
| 94 | | |
| 95 | | #[arg(long)] |
| 96 | | uid: Option<u32>, |
| 97 | | |
| 98 | | #[arg(long, conflicts_with = "uid")] |
| 99 | | learn_uid: bool, |
| 100 | | |
| 101 | | #[arg(long, default_value_t = 600)] |
| 102 | | wait: u64, |
| 103 | | |
| 104 | | #[arg(long)] |
| 105 | | owner_home: Option<PathBuf>, |
| 106 | | }, |
| 107 | | |
| 108 | | List { |
| 109 | | #[arg(long)] |
| 110 | | json: bool, |
| 111 | | }, |
| 112 | | |
| 113 | | Disable { |
| 114 | | name: String, |
| 115 | | #[arg(long)] |
| 116 | | owner_home: Option<PathBuf>, |
| 117 | | }, |
| 118 | | } |
| 119 | |
|
| 120 | | #[derive(Subcommand)] |
| 121 | | pub enum Admin { |
| 122 | | |
| 123 | | Verify { |
| 124 | | |
| 125 | | #[arg(long)] |
| 126 | | json: bool, |
| 127 | | }, |
| 128 | | |
| 129 | | Check, |
| 130 | | |
| 131 | | Rebuild, |
| 132 | | } |
| 133 | |
|
| 134 | |
|
| 135 | | pub struct Ctx { |
| 136 | | pub data: PathBuf, |
| 137 | | } |
| 138 | |
|
| 139 | | impl Ctx { |
| 140 | | |
| 141 | 18× | pub fn db(&self) -> Result<PathBuf> { |
| 142 | 18× | let db = self.data.join("spectra.db"); |
| 143 | 18× | if !db.exists() { |
| 144 | 1× | bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 145 | 17× | } |
| 146 | 17× | Ok(db) |
| 147 | 18× | } |
| 148 | |
|
| 149 | | |
| 150 | | |
| 151 | 6× | pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 152 | 6× | let db = self.db()?; |
| 153 | 6× | let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 154 | 6× | spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 155 | 6× | } |
| 156 | |
|
| 157 | | |
| 158 | 6× | pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 159 | 6× | let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 160 | 3× | let t: toml::Table = text.parse().ok()?; |
| 161 | 3× | t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 162 | 6× | } |
| 163 | | } |
| 164 | |
|
| 165 | 0× | fn default_data() -> PathBuf { |
| 166 | 0× | if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 167 | 0× | return PathBuf::from(x).join("spectra"); |
| 168 | 0× | } |
| 169 | 0× | let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 170 | 0× | PathBuf::from(home).join(".local").join("share").join("spectra") |
| 171 | 0× | } |
| 172 | |
|
| 173 | 21× | fn runtime() -> Result<tokio::runtime::Runtime> { |
| 174 | 21× | Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 175 | 21× | } |
| 176 | |
|
| 177 | 21× | pub fn run() -> Result<i32> { |
| 178 | 21× | let cli = Cli::parse(); |
| 179 | 21× | ui::init(cli.plain); |
| 180 | 21× | let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 181 | 21× | match cli.command { |
| 182 | 0× | Command::Version { json } => version(json), |
| 183 | 4× | Command::Init { owner_home, name } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 184 | 0× | Command::Identity { command } => runtime()?.block_on(async { |
| 185 | 0× | match command { |
| 186 | 0× | IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 187 | 0× | IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 188 | | } |
| 189 | 0× | }), |
| 190 | 7× | Command::Agent { command } => runtime()?.block_on(async { |
| 191 | 7× | match command { |
| 192 | 2× | AgentCmd::Add { name, kind, project, home, uid, learn_uid, wait, owner_home } => { |
| 193 | 2× | agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, wait_secs: wait, owner_home }).await |
| 194 | | } |
| 195 | 3× | AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 196 | 2× | AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 197 | | } |
| 198 | 7× | }), |
| 199 | 10× | Command::Admin { command } => runtime()?.block_on(async { |
| 200 | 10× | match command { |
| 201 | 7× | Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 202 | 2× | Admin::Check => admin::check(&ctx).await, |
| 203 | 1× | Admin::Rebuild => admin::rebuild(&ctx).await, |
| 204 | | } |
| 205 | 10× | }), |
| 206 | | } |
| 207 | 21× | } |
| 208 | |
|
| 209 | 0× | fn version(as_json: bool) -> Result<i32> { |
| 210 | 0× | if as_json { |
| 211 | 0× | println!( |
| 212 | 0× | "{}", |
| 213 | 0× | serde_json::json!({ |
| 214 | 0× | "version": env!("CARGO_PKG_VERSION"), |
| 215 | 0× | "build": env!("SPECTRA_BUILD_ID"), |
| 216 | 0× | "mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 217 | 0× | "ledger_format": spectra_core::LEDGER_FORMAT, |
| 218 | 0× | }) |
| 219 | 0× | ); |
| 220 | 0× | } else { |
| 221 | 0× | println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 222 | 0× | } |
| 223 | 0× | Ok(0) |
| 224 | 0× | } |