| 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 |
pub fn db(&self) -> Result<PathBuf> { |
| 142 |
let db = self.data.join("spectra.db"); |
| 143 |
if !db.exists() { |
| 144 |
bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 145 |
} |
| 146 |
Ok(db) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 152 |
let db = self.db()?; |
| 153 |
let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 154 |
spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 159 |
let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 160 |
let t: toml::Table = text.parse().ok()?; |
| 161 |
t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
fn default_data() -> PathBuf { |
| 166 |
if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 167 |
return PathBuf::from(x).join("spectra"); |
| 168 |
} |
| 169 |
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 170 |
PathBuf::from(home).join(".local").join("share").join("spectra") |
| 171 |
} |
| 172 |
|
| 173 |
fn runtime() -> Result<tokio::runtime::Runtime> { |
| 174 |
Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 175 |
} |
| 176 |
|
| 177 |
pub fn run() -> Result<i32> { |
| 178 |
let cli = Cli::parse(); |
| 179 |
ui::init(cli.plain); |
| 180 |
let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 181 |
match cli.command { |
| 182 |
Command::Version { json } => version(json), |
| 183 |
Command::Init { owner_home, name } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 184 |
Command::Identity { command } => runtime()?.block_on(async { |
| 185 |
match command { |
| 186 |
IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 187 |
IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 188 |
} |
| 189 |
}), |
| 190 |
Command::Agent { command } => runtime()?.block_on(async { |
| 191 |
match command { |
| 192 |
AgentCmd::Add { name, kind, project, home, uid, learn_uid, wait, owner_home } => { |
| 193 |
agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, wait_secs: wait, owner_home }).await |
| 194 |
} |
| 195 |
AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 196 |
AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 197 |
} |
| 198 |
}), |
| 199 |
Command::Admin { command } => runtime()?.block_on(async { |
| 200 |
match command { |
| 201 |
Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 202 |
Admin::Check => admin::check(&ctx).await, |
| 203 |
Admin::Rebuild => admin::rebuild(&ctx).await, |
| 204 |
} |
| 205 |
}), |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
fn version(as_json: bool) -> Result<i32> { |
| 210 |
if as_json { |
| 211 |
println!( |
| 212 |
"{}", |
| 213 |
serde_json::json!({ |
| 214 |
"version": env!("CARGO_PKG_VERSION"), |
| 215 |
"build": env!("SPECTRA_BUILD_ID"), |
| 216 |
"mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 217 |
"ledger_format": spectra_core::LEDGER_FORMAT, |
| 218 |
}) |
| 219 |
); |
| 220 |
} else { |
| 221 |
println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 222 |
} |
| 223 |
Ok(0) |
| 224 |
} |