| 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 project; |
| 12 |
pub mod serve; |
| 13 |
pub mod ui; |
| 14 |
|
| 15 |
use anyhow::{bail, Result}; |
| 16 |
use clap::{Parser, Subcommand}; |
| 17 |
use std::path::PathBuf; |
| 18 |
|
| 19 |
#[derive(Parser)] |
| 20 |
#[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")] |
| 21 |
pub struct Cli { |
| 22 |
|
| 23 |
#[arg(long, global = true)] |
| 24 |
pub plain: bool, |
| 25 |
|
| 26 |
#[arg(long, global = true, env = "SPECTRA_DATA")] |
| 27 |
pub data: Option<PathBuf>, |
| 28 |
#[command(subcommand)] |
| 29 |
pub command: Command, |
| 30 |
} |
| 31 |
|
| 32 |
#[derive(Subcommand)] |
| 33 |
pub enum Command { |
| 34 |
|
| 35 |
Version { |
| 36 |
|
| 37 |
#[arg(long)] |
| 38 |
json: bool, |
| 39 |
}, |
| 40 |
|
| 41 |
|
| 42 |
Init { |
| 43 |
|
| 44 |
#[arg(long)] |
| 45 |
owner_home: Option<PathBuf>, |
| 46 |
|
| 47 |
#[arg(long)] |
| 48 |
name: Option<String>, |
| 49 |
}, |
| 50 |
|
| 51 |
Identity { |
| 52 |
#[command(subcommand)] |
| 53 |
command: IdentityCmd, |
| 54 |
}, |
| 55 |
|
| 56 |
Serve { |
| 57 |
|
| 58 |
#[arg(long, default_value = "127.0.0.1:7350")] |
| 59 |
bind: String, |
| 60 |
|
| 61 |
#[arg(long)] |
| 62 |
insecure: bool, |
| 63 |
}, |
| 64 |
|
| 65 |
Project { |
| 66 |
#[command(subcommand)] |
| 67 |
command: ProjectCmd, |
| 68 |
}, |
| 69 |
|
| 70 |
Agent { |
| 71 |
#[command(subcommand)] |
| 72 |
command: AgentCmd, |
| 73 |
}, |
| 74 |
|
| 75 |
Admin { |
| 76 |
#[command(subcommand)] |
| 77 |
command: Admin, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
#[derive(Subcommand)] |
| 82 |
pub enum IdentityCmd { |
| 83 |
|
| 84 |
Show { |
| 85 |
#[arg(long)] |
| 86 |
json: bool, |
| 87 |
}, |
| 88 |
|
| 89 |
Refresh { |
| 90 |
#[arg(long)] |
| 91 |
owner_home: Option<PathBuf>, |
| 92 |
}, |
| 93 |
} |
| 94 |
|
| 95 |
#[derive(Subcommand)] |
| 96 |
pub enum ProjectCmd { |
| 97 |
|
| 98 |
Add { |
| 99 |
path: PathBuf, |
| 100 |
|
| 101 |
#[arg(long)] |
| 102 |
name: Option<String>, |
| 103 |
|
| 104 |
#[arg(long)] |
| 105 |
remote: Option<String>, |
| 106 |
#[arg(long)] |
| 107 |
owner_home: Option<PathBuf>, |
| 108 |
}, |
| 109 |
|
| 110 |
List { |
| 111 |
#[arg(long)] |
| 112 |
json: bool, |
| 113 |
}, |
| 114 |
} |
| 115 |
|
| 116 |
#[derive(Subcommand)] |
| 117 |
pub enum AgentCmd { |
| 118 |
|
| 119 |
Add { |
| 120 |
|
| 121 |
name: String, |
| 122 |
|
| 123 |
#[arg(long)] |
| 124 |
kind: String, |
| 125 |
|
| 126 |
#[arg(long)] |
| 127 |
project: String, |
| 128 |
|
| 129 |
#[arg(long)] |
| 130 |
home: PathBuf, |
| 131 |
|
| 132 |
#[arg(long)] |
| 133 |
uid: Option<u32>, |
| 134 |
|
| 135 |
#[arg(long, conflicts_with = "uid")] |
| 136 |
learn_uid: bool, |
| 137 |
|
| 138 |
#[arg(long, default_value_t = 600)] |
| 139 |
wait: u64, |
| 140 |
|
| 141 |
#[arg(long)] |
| 142 |
owner_home: Option<PathBuf>, |
| 143 |
}, |
| 144 |
|
| 145 |
List { |
| 146 |
#[arg(long)] |
| 147 |
json: bool, |
| 148 |
}, |
| 149 |
|
| 150 |
Disable { |
| 151 |
name: String, |
| 152 |
#[arg(long)] |
| 153 |
owner_home: Option<PathBuf>, |
| 154 |
}, |
| 155 |
} |
| 156 |
|
| 157 |
#[derive(Subcommand)] |
| 158 |
pub enum Admin { |
| 159 |
|
| 160 |
Verify { |
| 161 |
|
| 162 |
#[arg(long)] |
| 163 |
json: bool, |
| 164 |
}, |
| 165 |
|
| 166 |
Check, |
| 167 |
|
| 168 |
Rebuild, |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
pub struct Ctx { |
| 173 |
pub data: PathBuf, |
| 174 |
} |
| 175 |
|
| 176 |
impl Ctx { |
| 177 |
|
| 178 |
pub fn db(&self) -> Result<PathBuf> { |
| 179 |
let db = self.data.join("spectra.db"); |
| 180 |
if !db.exists() { |
| 181 |
bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 182 |
} |
| 183 |
Ok(db) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 189 |
let db = self.db()?; |
| 190 |
let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 191 |
spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
pub fn socket(&self) -> PathBuf { |
| 196 |
self.data.join("spectra.sock") |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
pub async fn owner_writer(&self, owner_home: &std::path::Path) -> Result<Writer> { |
| 202 |
let db = self.db()?; |
| 203 |
if spectra_mcp::client::Client::alive(&self.socket()).await { |
| 204 |
let ledger = spectra_core::ledger::Ledger::open_read_only(&db).await?; |
| 205 |
let transport = identity::recorded_owner(&ledger, owner_home).await?; |
| 206 |
return Ok(Writer::Daemon { ledger, client: Box::new(spectra_mcp::client::Client::new(&self.socket(), Some(transport))) }); |
| 207 |
} |
| 208 |
Ok(Writer::Direct(self.writer().await?)) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 213 |
let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 214 |
let t: toml::Table = text.parse().ok()?; |
| 215 |
t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
pub enum Writer { |
| 221 |
Direct(spectra_core::ledger::Ledger), |
| 222 |
Daemon { ledger: spectra_core::ledger::Ledger, client: Box<spectra_mcp::client::Client> }, |
| 223 |
} |
| 224 |
|
| 225 |
impl Writer { |
| 226 |
|
| 227 |
pub fn ledger(&self) -> &spectra_core::ledger::Ledger { |
| 228 |
match self { |
| 229 |
Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => l, |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
pub async fn append(&self, events: Vec<spectra_core::event::NewEvent>) -> Result<()> { |
| 235 |
match self { |
| 236 |
Writer::Direct(l) => { |
| 237 |
l.append(events).await?; |
| 238 |
l.checkpoint().await?; |
| 239 |
} |
| 240 |
Writer::Daemon { client, .. } => { |
| 241 |
let wire: Vec<_> = events.iter().map(spectra_core::daemon::WireEvent::from_new).collect(); |
| 242 |
let r = client.send("POST", "/api/v1/ledger/events", &[], serde_json::to_vec(&wire)?).await?; |
| 243 |
if r.status != 200 { |
| 244 |
let why = r |
| 245 |
.json() |
| 246 |
.ok() |
| 247 |
.and_then(|v| v.get("title").and_then(|t| t.as_str()).map(String::from)) |
| 248 |
.unwrap_or_else(|| String::from_utf8_lossy(&r.body).into()); |
| 249 |
bail!("the daemon refused: {why}"); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
Ok(()) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
fn init_logging() { |
| 258 |
let filter = tracing_subscriber::EnvFilter::try_from_env("SPECTRA_LOG").unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")); |
| 259 |
tracing_subscriber::fmt().with_env_filter(filter).with_writer(std::io::stderr).init(); |
| 260 |
} |
| 261 |
|
| 262 |
fn default_data() -> PathBuf { |
| 263 |
if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 264 |
return PathBuf::from(x).join("spectra"); |
| 265 |
} |
| 266 |
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 267 |
PathBuf::from(home).join(".local").join("share").join("spectra") |
| 268 |
} |
| 269 |
|
| 270 |
fn runtime() -> Result<tokio::runtime::Runtime> { |
| 271 |
Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 272 |
} |
| 273 |
|
| 274 |
pub fn run() -> Result<i32> { |
| 275 |
let cli = Cli::parse(); |
| 276 |
ui::init(cli.plain); |
| 277 |
let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 278 |
match cli.command { |
| 279 |
Command::Version { json } => version(json), |
| 280 |
Command::Init { owner_home, name } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 281 |
Command::Identity { command } => runtime()?.block_on(async { |
| 282 |
match command { |
| 283 |
IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 284 |
IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 285 |
} |
| 286 |
}), |
| 287 |
Command::Serve { bind, insecure } => { |
| 288 |
init_logging(); |
| 289 |
runtime()?.block_on(serve::serve(&ctx, &bind, insecure)) |
| 290 |
} |
| 291 |
Command::Project { command } => runtime()?.block_on(async { |
| 292 |
match command { |
| 293 |
ProjectCmd::Add { path, name, remote, owner_home } => project::add(&ctx, &path, name, remote, owner_home).await, |
| 294 |
ProjectCmd::List { json } => project::list(&ctx, json).await, |
| 295 |
} |
| 296 |
}), |
| 297 |
Command::Agent { command } => runtime()?.block_on(async { |
| 298 |
match command { |
| 299 |
AgentCmd::Add { name, kind, project, home, uid, learn_uid, wait, owner_home } => { |
| 300 |
agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, wait_secs: wait, owner_home }).await |
| 301 |
} |
| 302 |
AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 303 |
AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 304 |
} |
| 305 |
}), |
| 306 |
Command::Admin { command } => runtime()?.block_on(async { |
| 307 |
match command { |
| 308 |
Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 309 |
Admin::Check => admin::check(&ctx).await, |
| 310 |
Admin::Rebuild => admin::rebuild(&ctx).await, |
| 311 |
} |
| 312 |
}), |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
fn version(as_json: bool) -> Result<i32> { |
| 317 |
if as_json { |
| 318 |
println!( |
| 319 |
"{}", |
| 320 |
serde_json::json!({ |
| 321 |
"version": env!("CARGO_PKG_VERSION"), |
| 322 |
"build": env!("SPECTRA_BUILD_ID"), |
| 323 |
"mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 324 |
"ledger_format": spectra_core::LEDGER_FORMAT, |
| 325 |
}) |
| 326 |
); |
| 327 |
} else { |
| 328 |
println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 329 |
} |
| 330 |
Ok(0) |
| 331 |
} |