| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
pub mod admin; |
| 6 |
pub mod agent; |
| 7 |
pub mod connect; |
| 8 |
pub mod format; |
| 9 |
pub mod human; |
| 10 |
pub mod identity; |
| 11 |
pub mod json; |
| 12 |
pub mod marks; |
| 13 |
pub mod mcp; |
| 14 |
pub mod open; |
| 15 |
pub mod project; |
| 16 |
pub mod serve; |
| 17 |
pub mod ui; |
| 18 |
|
| 19 |
use anyhow::{bail, Result}; |
| 20 |
use clap::{Parser, Subcommand}; |
| 21 |
use std::path::PathBuf; |
| 22 |
|
| 23 |
#[derive(Parser)] |
| 24 |
#[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")] |
| 25 |
pub struct Cli { |
| 26 |
|
| 27 |
#[arg(long, global = true)] |
| 28 |
pub plain: bool, |
| 29 |
|
| 30 |
#[arg(long, global = true, env = "SPECTRA_DATA")] |
| 31 |
pub data: Option<PathBuf>, |
| 32 |
#[command(subcommand)] |
| 33 |
pub command: Command, |
| 34 |
} |
| 35 |
|
| 36 |
#[derive(Subcommand)] |
| 37 |
pub enum Command { |
| 38 |
|
| 39 |
Version { |
| 40 |
|
| 41 |
#[arg(long)] |
| 42 |
json: bool, |
| 43 |
}, |
| 44 |
|
| 45 |
|
| 46 |
Init { |
| 47 |
|
| 48 |
#[arg(long)] |
| 49 |
owner_home: Option<PathBuf>, |
| 50 |
|
| 51 |
#[arg(long)] |
| 52 |
name: Option<String>, |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[arg(long, value_name = "KEYID", conflicts_with = "owner_home")] |
| 57 |
owner: Option<String>, |
| 58 |
}, |
| 59 |
|
| 60 |
|
| 61 |
Connect { |
| 62 |
|
| 63 |
url: String, |
| 64 |
|
| 65 |
#[arg(long)] |
| 66 |
owner_home: Option<PathBuf>, |
| 67 |
}, |
| 68 |
|
| 69 |
Identity { |
| 70 |
#[command(subcommand)] |
| 71 |
command: IdentityCmd, |
| 72 |
}, |
| 73 |
|
| 74 |
Serve { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#[arg(long)] |
| 79 |
bind: Option<String>, |
| 80 |
|
| 81 |
|
| 82 |
#[arg(long)] |
| 83 |
tls: Option<String>, |
| 84 |
|
| 85 |
#[arg(long)] |
| 86 |
insecure: bool, |
| 87 |
|
| 88 |
#[arg(long)] |
| 89 |
open: bool, |
| 90 |
}, |
| 91 |
|
| 92 |
Open { |
| 93 |
#[arg(long)] |
| 94 |
owner_home: Option<PathBuf>, |
| 95 |
|
| 96 |
#[arg(long)] |
| 97 |
no_browser: bool, |
| 98 |
}, |
| 99 |
|
| 100 |
Mcp { |
| 101 |
|
| 102 |
#[arg(long)] |
| 103 |
agent: String, |
| 104 |
|
| 105 |
#[arg(long)] |
| 106 |
home: Option<PathBuf>, |
| 107 |
}, |
| 108 |
|
| 109 |
Say { |
| 110 |
|
| 111 |
body: String, |
| 112 |
|
| 113 |
#[arg(short, long)] |
| 114 |
channel: Option<String>, |
| 115 |
|
| 116 |
#[arg(long)] |
| 117 |
reply_to: Option<String>, |
| 118 |
#[arg(long)] |
| 119 |
json: bool, |
| 120 |
#[arg(long)] |
| 121 |
owner_home: Option<PathBuf>, |
| 122 |
}, |
| 123 |
|
| 124 |
Inbox { |
| 125 |
#[arg(long)] |
| 126 |
follow: bool, |
| 127 |
#[arg(long)] |
| 128 |
json: bool, |
| 129 |
#[arg(long)] |
| 130 |
owner_home: Option<PathBuf>, |
| 131 |
}, |
| 132 |
|
| 133 |
Search { |
| 134 |
query: String, |
| 135 |
#[arg(long)] |
| 136 |
channel: Option<String>, |
| 137 |
#[arg(long)] |
| 138 |
author: Option<String>, |
| 139 |
|
| 140 |
#[arg(long)] |
| 141 |
since: Option<String>, |
| 142 |
#[arg(long, default_value_t = 20)] |
| 143 |
limit: u32, |
| 144 |
#[arg(long)] |
| 145 |
json: bool, |
| 146 |
}, |
| 147 |
|
| 148 |
Api { |
| 149 |
method: String, |
| 150 |
path: String, |
| 151 |
|
| 152 |
#[arg(short = 'd', long = "body")] |
| 153 |
body: Option<String>, |
| 154 |
#[arg(long)] |
| 155 |
owner_home: Option<PathBuf>, |
| 156 |
}, |
| 157 |
|
| 158 |
Project { |
| 159 |
#[command(subcommand)] |
| 160 |
command: ProjectCmd, |
| 161 |
}, |
| 162 |
|
| 163 |
Agent { |
| 164 |
#[command(subcommand)] |
| 165 |
command: AgentCmd, |
| 166 |
}, |
| 167 |
|
| 168 |
Admin { |
| 169 |
#[command(subcommand)] |
| 170 |
command: Admin, |
| 171 |
}, |
| 172 |
} |
| 173 |
|
| 174 |
#[derive(Subcommand)] |
| 175 |
pub enum IdentityCmd { |
| 176 |
|
| 177 |
Show { |
| 178 |
#[arg(long)] |
| 179 |
json: bool, |
| 180 |
}, |
| 181 |
|
| 182 |
Refresh { |
| 183 |
#[arg(long)] |
| 184 |
owner_home: Option<PathBuf>, |
| 185 |
}, |
| 186 |
} |
| 187 |
|
| 188 |
#[derive(Subcommand)] |
| 189 |
pub enum ProjectCmd { |
| 190 |
|
| 191 |
Add { |
| 192 |
path: PathBuf, |
| 193 |
|
| 194 |
#[arg(long)] |
| 195 |
name: Option<String>, |
| 196 |
|
| 197 |
#[arg(long)] |
| 198 |
remote: Option<String>, |
| 199 |
#[arg(long)] |
| 200 |
owner_home: Option<PathBuf>, |
| 201 |
}, |
| 202 |
|
| 203 |
List { |
| 204 |
#[arg(long)] |
| 205 |
json: bool, |
| 206 |
}, |
| 207 |
} |
| 208 |
|
| 209 |
#[derive(Subcommand)] |
| 210 |
pub enum AgentCmd { |
| 211 |
|
| 212 |
Add { |
| 213 |
|
| 214 |
name: String, |
| 215 |
|
| 216 |
#[arg(long)] |
| 217 |
kind: String, |
| 218 |
|
| 219 |
#[arg(long)] |
| 220 |
project: String, |
| 221 |
|
| 222 |
#[arg(long)] |
| 223 |
home: PathBuf, |
| 224 |
|
| 225 |
#[arg(long)] |
| 226 |
uid: Option<u32>, |
| 227 |
|
| 228 |
#[arg(long, conflicts_with = "uid")] |
| 229 |
learn_uid: bool, |
| 230 |
|
| 231 |
#[arg(long, default_value_t = 600)] |
| 232 |
wait: u64, |
| 233 |
|
| 234 |
#[arg(long)] |
| 235 |
owner_home: Option<PathBuf>, |
| 236 |
}, |
| 237 |
|
| 238 |
List { |
| 239 |
#[arg(long)] |
| 240 |
json: bool, |
| 241 |
}, |
| 242 |
|
| 243 |
Disable { |
| 244 |
name: String, |
| 245 |
#[arg(long)] |
| 246 |
owner_home: Option<PathBuf>, |
| 247 |
}, |
| 248 |
} |
| 249 |
|
| 250 |
#[derive(Subcommand)] |
| 251 |
pub enum Admin { |
| 252 |
|
| 253 |
Verify { |
| 254 |
|
| 255 |
#[arg(long)] |
| 256 |
json: bool, |
| 257 |
}, |
| 258 |
|
| 259 |
Check, |
| 260 |
|
| 261 |
Rebuild, |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
pub struct Ctx { |
| 266 |
pub data: PathBuf, |
| 267 |
} |
| 268 |
|
| 269 |
impl Ctx { |
| 270 |
|
| 271 |
pub fn db(&self) -> Result<PathBuf> { |
| 272 |
let db = self.data.join("spectra.db"); |
| 273 |
if !db.exists() { |
| 274 |
bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 275 |
} |
| 276 |
Ok(db) |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 282 |
let db = self.db()?; |
| 283 |
let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 284 |
spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
pub fn socket(&self) -> PathBuf { |
| 289 |
self.data.join("spectra.sock") |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
pub fn remote(&self) -> Result<Option<connect::ClientCfg>> { |
| 294 |
connect::load() |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
pub fn endpoint(&self) -> Result<spectra_mcp::client::Endpoint> { |
| 299 |
Ok(match self.remote()? { |
| 300 |
Some(c) => c.endpoint()?, |
| 301 |
None => spectra_mcp::client::Endpoint::Unix(self.socket()), |
| 302 |
}) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
pub async fn owner_writer(&self, owner_home: &std::path::Path) -> Result<Writer> { |
| 308 |
let db = self.db()?; |
| 309 |
if spectra_mcp::client::Client::alive(&self.socket()).await { |
| 310 |
let ledger = spectra_core::ledger::Ledger::open_read_only(&db).await?; |
| 311 |
let transport = identity::recorded_owner(&ledger, owner_home).await?; |
| 312 |
return Ok(Writer::Daemon { ledger, client: Box::new(spectra_mcp::client::Client::new(&self.socket(), Some(transport))) }); |
| 313 |
} |
| 314 |
Ok(Writer::Direct(self.writer().await?)) |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 319 |
let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 320 |
let t: toml::Table = text.parse().ok()?; |
| 321 |
t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
pub enum Writer { |
| 327 |
Direct(spectra_core::ledger::Ledger), |
| 328 |
Daemon { ledger: spectra_core::ledger::Ledger, client: Box<spectra_mcp::client::Client> }, |
| 329 |
} |
| 330 |
|
| 331 |
impl Writer { |
| 332 |
|
| 333 |
pub fn ledger(&self) -> &spectra_core::ledger::Ledger { |
| 334 |
match self { |
| 335 |
Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => l, |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
pub async fn append(&self, events: Vec<spectra_core::event::NewEvent>) -> Result<()> { |
| 341 |
match self { |
| 342 |
Writer::Direct(l) => { |
| 343 |
l.append(events).await?; |
| 344 |
l.checkpoint().await?; |
| 345 |
} |
| 346 |
Writer::Daemon { client, .. } => { |
| 347 |
let wire: Vec<_> = events.iter().map(spectra_core::daemon::WireEvent::from_new).collect(); |
| 348 |
let r = client.send("POST", "/api/v1/ledger/events", &[], serde_json::to_vec(&wire)?).await?; |
| 349 |
if r.status != 200 { |
| 350 |
let why = r |
| 351 |
.json() |
| 352 |
.ok() |
| 353 |
.and_then(|v| v.get("title").and_then(|t| t.as_str()).map(String::from)) |
| 354 |
.unwrap_or_else(|| String::from_utf8_lossy(&r.body).into()); |
| 355 |
bail!("the daemon refused: {why}"); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
Ok(()) |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
fn init_logging() { |
| 364 |
let filter = tracing_subscriber::EnvFilter::try_from_env("SPECTRA_LOG").unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")); |
| 365 |
tracing_subscriber::fmt().with_env_filter(filter).with_writer(std::io::stderr).init(); |
| 366 |
} |
| 367 |
|
| 368 |
fn default_data() -> PathBuf { |
| 369 |
if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 370 |
return PathBuf::from(x).join("spectra"); |
| 371 |
} |
| 372 |
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 373 |
PathBuf::from(home).join(".local").join("share").join("spectra") |
| 374 |
} |
| 375 |
|
| 376 |
fn runtime() -> Result<tokio::runtime::Runtime> { |
| 377 |
Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 378 |
} |
| 379 |
|
| 380 |
pub fn run() -> Result<i32> { |
| 381 |
let cli = Cli::parse(); |
| 382 |
ui::init(cli.plain); |
| 383 |
let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 384 |
match cli.command { |
| 385 |
Command::Version { json } => version(json), |
| 386 |
Command::Connect { url, owner_home } => runtime()?.block_on(connect::connect(&ctx, &url, owner_home)), |
| 387 |
Command::Init { owner_home, name, owner: Some(key) } => { |
| 388 |
let _ = owner_home; |
| 389 |
runtime()?.block_on(identity::init_for(&ctx, &key, name)) |
| 390 |
} |
| 391 |
Command::Init { owner_home, name, owner: None } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 392 |
Command::Identity { command } => runtime()?.block_on(async { |
| 393 |
match command { |
| 394 |
IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 395 |
IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 396 |
} |
| 397 |
}), |
| 398 |
Command::Serve { bind, tls, insecure, open } => { |
| 399 |
init_logging(); |
| 400 |
runtime()?.block_on(serve::serve(&ctx, bind, tls, insecure, open)) |
| 401 |
} |
| 402 |
Command::Open { owner_home, no_browser } => runtime()?.block_on(open::open(&ctx, owner_home, !no_browser)), |
| 403 |
Command::Mcp { agent, home } => runtime()?.block_on(mcp::run(&ctx, &agent, home)), |
| 404 |
Command::Say { body, channel, reply_to, json, owner_home } => { |
| 405 |
runtime()?.block_on(human::say(&ctx, channel, reply_to, body, owner_home, json)) |
| 406 |
} |
| 407 |
Command::Inbox { follow, json, owner_home } => runtime()?.block_on(human::inbox(&ctx, follow, owner_home, json)), |
| 408 |
Command::Search { query, channel, author, since, limit, json } => { |
| 409 |
runtime()?.block_on(human::search(&ctx, &query, channel, author, since, limit, json)) |
| 410 |
} |
| 411 |
Command::Api { method, path, body, owner_home } => runtime()?.block_on(human::raw(&ctx, &method, &path, body, owner_home)), |
| 412 |
Command::Project { command } => runtime()?.block_on(async { |
| 413 |
match command { |
| 414 |
ProjectCmd::Add { path, name, remote, owner_home } => project::add(&ctx, &path, name, remote, owner_home).await, |
| 415 |
ProjectCmd::List { json } => project::list(&ctx, json).await, |
| 416 |
} |
| 417 |
}), |
| 418 |
Command::Agent { command } => runtime()?.block_on(async { |
| 419 |
match command { |
| 420 |
AgentCmd::Add { name, kind, project, home, uid, learn_uid, wait, owner_home } => { |
| 421 |
agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, wait_secs: wait, owner_home }).await |
| 422 |
} |
| 423 |
AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 424 |
AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 425 |
} |
| 426 |
}), |
| 427 |
Command::Admin { command } => runtime()?.block_on(async { |
| 428 |
match command { |
| 429 |
Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 430 |
Admin::Check => admin::check(&ctx).await, |
| 431 |
Admin::Rebuild => admin::rebuild(&ctx).await, |
| 432 |
} |
| 433 |
}), |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
fn version(as_json: bool) -> Result<i32> { |
| 438 |
if as_json { |
| 439 |
println!( |
| 440 |
"{}", |
| 441 |
serde_json::json!({ |
| 442 |
"version": env!("CARGO_PKG_VERSION"), |
| 443 |
"build": env!("SPECTRA_BUILD_ID"), |
| 444 |
"mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 445 |
"ledger_format": spectra_core::LEDGER_FORMAT, |
| 446 |
}) |
| 447 |
); |
| 448 |
} else { |
| 449 |
println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 450 |
} |
| 451 |
Ok(0) |
| 452 |
} |