| 98a0fb9f Andy McCall ↶ |
1 |
|
| 258bb382 Andy McCall ↶ |
2 |
|
| 3 |
|
| 4 |
|
| 36537112 Andy McCall ↶ |
5 |
pub mod admin; |
| 09752034 Andy McCall ↶ |
6 |
pub mod agent; |
| 8d803d85 Andy McCall ↶ |
7 |
pub mod auto; |
| b044d909 Andy McCall ↶ |
8 |
pub mod brief; |
| eca5af92 Andy McCall ↶ |
9 |
pub mod connect; |
| 258bb382 Andy McCall ↶ |
10 |
pub mod format; |
| da84e98f Andy McCall ↶ |
11 |
pub mod human; |
| 09752034 Andy McCall ↶ |
12 |
pub mod identity; |
| 5d695d86 Andy McCall ↶ |
13 |
pub mod marks; |
| eb675dfa Andy McCall ↶ |
14 |
pub mod mcp; |
| 2fc88a7b Andy McCall ↶ |
15 |
pub mod project; |
| 571572f5 Andy McCall ↶ |
16 |
pub mod serve; |
| c7161ec1 Andy McCall ↶ |
17 |
pub mod setup; |
| 258bb382 Andy McCall ↶ |
18 |
pub mod ui; |
| 19 |
|
| 67bfa06a Andy McCall ↶ |
20 |
use anyhow::{bail, Context, Result}; |
| 258bb382 Andy McCall ↶ |
21 |
use clap::{Parser, Subcommand}; |
| 36537112 Andy McCall ↶ |
22 |
use std::path::PathBuf; |
| 258bb382 Andy McCall ↶ |
23 |
|
| 24 |
#[derive(Parser)] |
| 25 |
#[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")] |
| 26 |
pub struct Cli { |
| 27 |
|
| 28 |
#[arg(long, global = true)] |
| 29 |
pub plain: bool, |
| 36537112 Andy McCall ↶ |
30 |
|
| 31 |
#[arg(long, global = true, env = "SPECTRA_DATA")] |
| 32 |
pub data: Option<PathBuf>, |
| 258bb382 Andy McCall ↶ |
33 |
#[command(subcommand)] |
| 34 |
pub command: Command, |
| 35 |
} |
| 36 |
|
| 37 |
#[derive(Subcommand)] |
| 38 |
pub enum Command { |
| 39 |
|
| 40 |
Version { |
| 41 |
|
| 42 |
#[arg(long)] |
| 43 |
json: bool, |
| 44 |
}, |
| 09752034 Andy McCall ↶ |
45 |
|
| 46 |
|
| 47 |
Init { |
| 98a0fb9f Andy McCall ↶ |
48 |
|
| 09752034 Andy McCall ↶ |
49 |
#[arg(long)] |
| 50 |
owner_home: Option<PathBuf>, |
| 51 |
|
| 52 |
#[arg(long)] |
| 53 |
name: Option<String>, |
| 77df591d Andy McCall ↶ |
54 |
|
| 55 |
|
| 56 |
|
| 57 |
#[arg(long, value_name = "KEYID", conflicts_with = "owner_home")] |
| 58 |
owner: Option<String>, |
| 09752034 Andy McCall ↶ |
59 |
}, |
| eca5af92 Andy McCall ↶ |
60 |
|
| 61 |
|
| 62 |
Connect { |
| 63 |
|
| 64 |
url: String, |
| 98a0fb9f Andy McCall ↶ |
65 |
|
| 66 |
#[arg(long, value_name = "KEYID-PREFIX")] |
| 67 |
expect: Option<String>, |
| 68 |
|
| 69 |
#[arg(long, short = 'y')] |
| 70 |
yes: bool, |
| 71 |
|
| eca5af92 Andy McCall ↶ |
72 |
#[arg(long)] |
| 73 |
owner_home: Option<PathBuf>, |
| 74 |
}, |
| c7161ec1 Andy McCall ↶ |
75 |
|
| 76 |
Status { |
| 77 |
#[arg(long)] |
| 78 |
json: bool, |
| 79 |
}, |
| 80 |
|
| 81 |
Probe { |
| 82 |
|
| 83 |
url: String, |
| 84 |
#[arg(long)] |
| 85 |
json: bool, |
| 86 |
}, |
| 87 |
|
| 88 |
|
| 89 |
Bridge { |
| 90 |
#[arg(long, default_value_t = 0)] |
| 91 |
port: u16, |
| 92 |
#[arg(long)] |
| 93 |
owner_home: Option<PathBuf>, |
| 94 |
}, |
| b044d909 Andy McCall ↶ |
95 |
|
| 96 |
Brief { |
| 97 |
#[command(subcommand)] |
| 98 |
command: BriefCmd, |
| 99 |
}, |
| 09752034 Andy McCall ↶ |
100 |
|
| 101 |
Identity { |
| 102 |
#[command(subcommand)] |
| 103 |
command: IdentityCmd, |
| 104 |
}, |
| 571572f5 Andy McCall ↶ |
105 |
|
| 106 |
Serve { |
| b576d05c Andy McCall ↶ |
107 |
|
| 108 |
|
| 109 |
|
| 110 |
#[arg(long)] |
| 111 |
bind: Option<String>, |
| 112 |
|
| 113 |
|
| 114 |
#[arg(long)] |
| 115 |
tls: Option<String>, |
| 571572f5 Andy McCall ↶ |
116 |
|
| 117 |
#[arg(long)] |
| 118 |
insecure: bool, |
| 39f5c913 Andy McCall ↶ |
119 |
}, |
| eb675dfa Andy McCall ↶ |
120 |
|
| 121 |
Mcp { |
| 122 |
|
| 8d803d85 Andy McCall ↶ |
123 |
#[arg(long, required_unless_present = "auto")] |
| 124 |
agent: Option<String>, |
| 125 |
|
| 126 |
|
| 127 |
#[arg(long, conflicts_with = "agent")] |
| 128 |
auto: bool, |
| 129 |
|
| 130 |
#[arg(long, default_value = "claude-code")] |
| 131 |
kind: String, |
| eb675dfa Andy McCall ↶ |
132 |
|
| 133 |
#[arg(long)] |
| 134 |
home: Option<PathBuf>, |
| 135 |
}, |
| da84e98f Andy McCall ↶ |
136 |
|
| 137 |
Say { |
| 138 |
|
| 139 |
body: String, |
| 140 |
|
| 141 |
#[arg(short, long)] |
| 142 |
channel: Option<String>, |
| 143 |
|
| 144 |
#[arg(long)] |
| 145 |
reply_to: Option<String>, |
| 146 |
#[arg(long)] |
| 147 |
json: bool, |
| 148 |
#[arg(long)] |
| 149 |
owner_home: Option<PathBuf>, |
| 150 |
}, |
| 151 |
|
| 152 |
Inbox { |
| 153 |
#[arg(long)] |
| 154 |
follow: bool, |
| 155 |
#[arg(long)] |
| 156 |
json: bool, |
| 157 |
#[arg(long)] |
| 158 |
owner_home: Option<PathBuf>, |
| 159 |
}, |
| 160 |
|
| 161 |
Search { |
| 162 |
query: String, |
| 163 |
#[arg(long)] |
| 164 |
channel: Option<String>, |
| 165 |
#[arg(long)] |
| 166 |
author: Option<String>, |
| 167 |
|
| 168 |
#[arg(long)] |
| 169 |
since: Option<String>, |
| 170 |
#[arg(long, default_value_t = 20)] |
| 171 |
limit: u32, |
| 172 |
#[arg(long)] |
| 173 |
json: bool, |
| 174 |
}, |
| 175 |
|
| 176 |
Api { |
| 177 |
method: String, |
| 178 |
path: String, |
| 179 |
|
| 180 |
#[arg(short = 'd', long = "body")] |
| 181 |
body: Option<String>, |
| 182 |
#[arg(long)] |
| 183 |
owner_home: Option<PathBuf>, |
| 184 |
}, |
| 2fc88a7b Andy McCall ↶ |
185 |
|
| 186 |
Project { |
| 187 |
#[command(subcommand)] |
| 188 |
command: ProjectCmd, |
| 189 |
}, |
| 09752034 Andy McCall ↶ |
190 |
|
| 191 |
Agent { |
| 192 |
#[command(subcommand)] |
| 193 |
command: AgentCmd, |
| 194 |
}, |
| 36537112 Andy McCall ↶ |
195 |
|
| 196 |
Admin { |
| 197 |
#[command(subcommand)] |
| 198 |
command: Admin, |
| 09752034 Andy McCall ↶ |
199 |
}, |
| 200 |
} |
| 201 |
|
| 202 |
#[derive(Subcommand)] |
| 203 |
pub enum IdentityCmd { |
| 98a0fb9f Andy McCall ↶ |
204 |
|
| 205 |
Create { |
| 206 |
|
| 207 |
#[arg(long)] |
| 208 |
name: String, |
| 209 |
|
| 210 |
#[arg(long, default_value = "")] |
| 211 |
contact: String, |
| 212 |
|
| 213 |
#[arg(long)] |
| 214 |
home: Option<PathBuf>, |
| c7161ec1 Andy McCall ↶ |
215 |
#[arg(long)] |
| 216 |
json: bool, |
| 98a0fb9f Andy McCall ↶ |
217 |
}, |
| 09752034 Andy McCall ↶ |
218 |
|
| 219 |
Show { |
| 220 |
#[arg(long)] |
| 221 |
json: bool, |
| 222 |
}, |
| 98a0fb9f Andy McCall ↶ |
223 |
|
| 09752034 Andy McCall ↶ |
224 |
Refresh { |
| b044d909 Andy McCall ↶ |
225 |
#[arg(long)] |
| 226 |
owner_home: Option<PathBuf>, |
| 227 |
}, |
| 228 |
} |
| 229 |
|
| 230 |
#[derive(Subcommand)] |
| 231 |
pub enum BriefCmd { |
| 232 |
|
| 233 |
Show { |
| 234 |
#[arg(long)] |
| 235 |
project: Option<String>, |
| 236 |
#[arg(long)] |
| 237 |
json: bool, |
| 238 |
}, |
| 239 |
|
| 240 |
Set { |
| 241 |
#[arg(long)] |
| 242 |
project: Option<String>, |
| 243 |
|
| 244 |
text: Option<String>, |
| 245 |
|
| 246 |
#[arg(long, short = 'f')] |
| 247 |
file: Option<PathBuf>, |
| 09752034 Andy McCall ↶ |
248 |
#[arg(long)] |
| 249 |
owner_home: Option<PathBuf>, |
| 36537112 Andy McCall ↶ |
250 |
}, |
| b044d909 Andy McCall ↶ |
251 |
|
| 252 |
Edit { |
| 253 |
#[arg(long)] |
| 254 |
project: Option<String>, |
| 255 |
#[arg(long)] |
| 256 |
owner_home: Option<PathBuf>, |
| 257 |
}, |
| 36537112 Andy McCall ↶ |
258 |
} |
| 259 |
|
| 260 |
#[derive(Subcommand)] |
| 2fc88a7b Andy McCall ↶ |
261 |
pub enum ProjectCmd { |
| 98a0fb9f Andy McCall ↶ |
262 |
|
| 2fc88a7b Andy McCall ↶ |
263 |
Add { |
| 98a0fb9f Andy McCall ↶ |
264 |
|
| 265 |
target: String, |
| 266 |
|
| 2fc88a7b Andy McCall ↶ |
267 |
#[arg(long)] |
| 268 |
name: Option<String>, |
| 98a0fb9f Andy McCall ↶ |
269 |
|
| 270 |
#[arg(long)] |
| 271 |
path: Option<PathBuf>, |
| 272 |
|
| 273 |
#[arg(long, short = 'd', default_value = "")] |
| 274 |
description: String, |
| 275 |
|
| 276 |
#[arg(long)] |
| 277 |
url: Option<String>, |
| 278 |
#[arg(long)] |
| 279 |
owner_home: Option<PathBuf>, |
| 280 |
}, |
| 281 |
|
| 282 |
Link { |
| 283 |
project: String, |
| 284 |
#[arg(long)] |
| 285 |
depends_on: String, |
| 286 |
|
| 2fc88a7b Andy McCall ↶ |
287 |
#[arg(long)] |
| 98a0fb9f Andy McCall ↶ |
288 |
remove: bool, |
| 2fc88a7b Andy McCall ↶ |
289 |
#[arg(long)] |
| 290 |
owner_home: Option<PathBuf>, |
| 291 |
}, |
| 98a0fb9f Andy McCall ↶ |
292 |
|
| 2fc88a7b Andy McCall ↶ |
293 |
List { |
| 294 |
#[arg(long)] |
| 295 |
json: bool, |
| 296 |
}, |
| 297 |
} |
| 298 |
|
| 299 |
#[derive(Subcommand)] |
| 09752034 Andy McCall ↶ |
300 |
pub enum AgentCmd { |
| 98a0fb9f Andy McCall ↶ |
301 |
|
| 09752034 Andy McCall ↶ |
302 |
Add { |
| 303 |
|
| 304 |
name: String, |
| 305 |
|
| 306 |
#[arg(long)] |
| 307 |
kind: String, |
| 308 |
|
| 309 |
#[arg(long)] |
| 310 |
project: String, |
| 98a0fb9f Andy McCall ↶ |
311 |
|
| 09752034 Andy McCall ↶ |
312 |
#[arg(long)] |
| 313 |
home: PathBuf, |
| 314 |
|
| 315 |
#[arg(long)] |
| 316 |
uid: Option<u32>, |
| 317 |
|
| 318 |
#[arg(long, conflicts_with = "uid")] |
| 319 |
learn_uid: bool, |
| 98a0fb9f Andy McCall ↶ |
320 |
|
| 321 |
#[arg(long)] |
| 322 |
expires: Option<String>, |
| 323 |
|
| 324 |
#[arg(long, short = 'y')] |
| 325 |
yes: bool, |
| 326 |
|
| 327 |
#[arg(long)] |
| 328 |
owner_home: Option<PathBuf>, |
| 329 |
}, |
| 87f601cc Andy McCall ↶ |
330 |
|
| 331 |
Allow { |
| 332 |
name: String, |
| 333 |
|
| 334 |
#[arg(long)] |
| 335 |
channel: String, |
| 336 |
|
| 337 |
#[arg(long)] |
| 338 |
remove: bool, |
| 339 |
#[arg(long)] |
| 340 |
owner_home: Option<PathBuf>, |
| 341 |
}, |
| 98a0fb9f Andy McCall ↶ |
342 |
|
| 343 |
Renew { |
| 344 |
name: String, |
| 345 |
|
| 346 |
#[arg(long)] |
| 347 |
expires: String, |
| 09752034 Andy McCall ↶ |
348 |
#[arg(long)] |
| 349 |
owner_home: Option<PathBuf>, |
| 350 |
}, |
| 351 |
|
| 352 |
List { |
| 353 |
#[arg(long)] |
| 354 |
json: bool, |
| 355 |
}, |
| 356 |
|
| 357 |
Disable { |
| 358 |
name: String, |
| 359 |
#[arg(long)] |
| 360 |
owner_home: Option<PathBuf>, |
| 361 |
}, |
| 362 |
} |
| 363 |
|
| 364 |
#[derive(Subcommand)] |
| 36537112 Andy McCall ↶ |
365 |
pub enum Admin { |
| 366 |
|
| 367 |
Verify { |
| 368 |
|
| 369 |
#[arg(long)] |
| 370 |
json: bool, |
| 371 |
}, |
| 372 |
|
| 373 |
Check, |
| 374 |
|
| 375 |
Rebuild, |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
pub struct Ctx { |
| 380 |
pub data: PathBuf, |
| 381 |
} |
| 382 |
|
| 383 |
impl Ctx { |
| 384 |
|
| 385 |
pub fn db(&self) -> Result<PathBuf> { |
| 386 |
let db = self.data.join("spectra.db"); |
| 387 |
if !db.exists() { |
| 388 |
bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 389 |
} |
| 390 |
Ok(db) |
| 391 |
} |
| 09752034 Andy McCall ↶ |
392 |
|
| 393 |
|
| 394 |
|
| 395 |
pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 396 |
let db = self.db()?; |
| 397 |
let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 398 |
spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 571572f5 Andy McCall ↶ |
399 |
} |
| 400 |
|
| 401 |
|
| 402 |
pub fn socket(&self) -> PathBuf { |
| 403 |
self.data.join("spectra.sock") |
| eca5af92 Andy McCall ↶ |
404 |
} |
| 405 |
|
| 406 |
|
| 407 |
pub fn remote(&self) -> Result<Option<connect::ClientCfg>> { |
| 408 |
connect::load() |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
pub fn endpoint(&self) -> Result<spectra_mcp::client::Endpoint> { |
| 413 |
Ok(match self.remote()? { |
| 414 |
Some(c) => c.endpoint()?, |
| 415 |
None => spectra_mcp::client::Endpoint::Unix(self.socket()), |
| 416 |
}) |
| 571572f5 Andy McCall ↶ |
417 |
} |
| 418 |
|
| 67bfa06a Andy McCall ↶ |
419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
pub async fn owner_client(&self, owner_home: &std::path::Path) -> Result<Option<spectra_mcp::client::Client>> { |
| 424 |
use spectra_mcp::client::Client; |
| 425 |
if let Some(cfg) = self.remote()? { |
| 426 |
let ep = cfg.endpoint()?; |
| 427 |
let pair = remote_owner(&ep, owner_home).await?; |
| 428 |
return Ok(Some(Client::at(ep, Some(pair)))); |
| 429 |
} |
| 430 |
if !self.data.join("spectra.db").exists() || !Client::alive(&self.socket()).await { |
| 431 |
return Ok(None); |
| 432 |
} |
| 433 |
let ledger = spectra_core::ledger::Ledger::open_read_only(&self.db()?).await?; |
| 434 |
Ok(Some(Client::new(&self.socket(), Some(identity::recorded_owner(&ledger, owner_home).await?)))) |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 571572f5 Andy McCall ↶ |
440 |
pub async fn owner_writer(&self, owner_home: &std::path::Path) -> Result<Writer> { |
| 67bfa06a Andy McCall ↶ |
441 |
if let Some(cfg) = self.remote()? { |
| 442 |
let ep = cfg.endpoint()?; |
| 443 |
let pair = remote_owner(&ep, owner_home).await?; |
| 444 |
let recorded = pair.identity.clone(); |
| 445 |
return Ok(Writer::Remote { client: Box::new(spectra_mcp::client::Client::at(ep, Some(pair))), recorded }); |
| 446 |
} |
| 571572f5 Andy McCall ↶ |
447 |
let db = self.db()?; |
| 448 |
if spectra_mcp::client::Client::alive(&self.socket()).await { |
| 449 |
let ledger = spectra_core::ledger::Ledger::open_read_only(&db).await?; |
| 450 |
let transport = identity::recorded_owner(&ledger, owner_home).await?; |
| 451 |
return Ok(Writer::Daemon { ledger, client: Box::new(spectra_mcp::client::Client::new(&self.socket(), Some(transport))) }); |
| 452 |
} |
| 453 |
Ok(Writer::Direct(self.writer().await?)) |
| 67bfa06a Andy McCall ↶ |
454 |
} |
| 455 |
|
| 456 |
|
| 457 |
pub fn local_only(&self, what: &str) -> Result<()> { |
| 458 |
if self.remote()?.is_some() { |
| 459 |
bail!("{what} runs on the server, which holds the ledger (this machine uses {})", connect::path().display()); |
| 460 |
} |
| 461 |
Ok(()) |
| 09752034 Andy McCall ↶ |
462 |
} |
| 463 |
|
| 464 |
|
| 465 |
pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 466 |
let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 467 |
let t: toml::Table = text.parse().ok()?; |
| 468 |
t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 469 |
} |
| 571572f5 Andy McCall ↶ |
470 |
} |
| 471 |
|
| 472 |
|
| 473 |
pub enum Writer { |
| 474 |
Direct(spectra_core::ledger::Ledger), |
| 67bfa06a Andy McCall ↶ |
475 |
Daemon { |
| 476 |
ledger: spectra_core::ledger::Ledger, |
| 477 |
client: Box<spectra_mcp::client::Client>, |
| 478 |
}, |
| 479 |
|
| 480 |
Remote { |
| 481 |
client: Box<spectra_mcp::client::Client>, |
| 98a0fb9f Andy McCall ↶ |
482 |
recorded: spectra_core::identity::Identity, |
| 67bfa06a Andy McCall ↶ |
483 |
}, |
| 36537112 Andy McCall ↶ |
484 |
} |
| 485 |
|
| 571572f5 Andy McCall ↶ |
486 |
impl Writer { |
| 67bfa06a Andy McCall ↶ |
487 |
|
| 488 |
pub fn ledger(&self) -> Option<&spectra_core::ledger::Ledger> { |
| 571572f5 Andy McCall ↶ |
489 |
match self { |
| 67bfa06a Andy McCall ↶ |
490 |
Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => Some(l), |
| 491 |
Writer::Remote { .. } => None, |
| 571572f5 Andy McCall ↶ |
492 |
} |
| 493 |
} |
| 494 |
|
| 67bfa06a Andy McCall ↶ |
495 |
|
| 496 |
pub fn client(&self) -> Option<&spectra_mcp::client::Client> { |
| 497 |
match self { |
| 498 |
Writer::Daemon { client, .. } | Writer::Remote { client, .. } => Some(client), |
| 499 |
Writer::Direct(_) => None, |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
pub async fn refresh_event(&self, home: &std::path::Path) -> Result<Option<spectra_core::event::NewEvent>> { |
| 506 |
match self { |
| 507 |
Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => identity::refresh_event(l, home).await, |
| 508 |
Writer::Remote { recorded, .. } => identity::rotation(recorded, home), |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 571572f5 Andy McCall ↶ |
512 |
|
| 513 |
pub async fn append(&self, events: Vec<spectra_core::event::NewEvent>) -> Result<()> { |
| 514 |
match self { |
| 515 |
Writer::Direct(l) => { |
| 516 |
l.append(events).await?; |
| 517 |
l.checkpoint().await?; |
| 518 |
} |
| 67bfa06a Andy McCall ↶ |
519 |
Writer::Daemon { client, .. } | Writer::Remote { client, .. } => { |
| 571572f5 Andy McCall ↶ |
520 |
let wire: Vec<_> = events.iter().map(spectra_core::daemon::WireEvent::from_new).collect(); |
| 521 |
let r = client.send("POST", "/api/v1/ledger/events", &[], serde_json::to_vec(&wire)?).await?; |
| 522 |
if r.status != 200 { |
| 523 |
let why = r |
| 524 |
.json() |
| 525 |
.ok() |
| 526 |
.and_then(|v| v.get("title").and_then(|t| t.as_str()).map(String::from)) |
| 527 |
.unwrap_or_else(|| String::from_utf8_lossy(&r.body).into()); |
| 528 |
bail!("the daemon refused: {why}"); |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
Ok(()) |
| 533 |
} |
| 67bfa06a Andy McCall ↶ |
534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
async fn remote_owner(ep: &spectra_mcp::client::Endpoint, home: &std::path::Path) -> Result<spectra_core::keys::KeyPair> { |
| 539 |
let r = spectra_mcp::client::Client::at(ep.clone(), None).send("GET", "/api/v1/instance", &[], vec![]).await?; |
| 540 |
let info = r.json()?; |
| 541 |
let key = info.get("owner").and_then(|o| o.as_str()).context("the server has no owner yet: spectra connect claims it")?; |
| 542 |
let keys = home.join("keys"); |
| 543 |
spectra_core::keys::KeyPair::load(&keys.join(format!("{key}.secret")), &keys.join(format!("{key}.identity"))) |
| 544 |
.with_context(|| format!("the server's owner {} is not an identity in {}", &key[..12.min(key.len())], home.display())) |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
pub async fn call(c: &spectra_mcp::client::Client, method: &str, path: &str, body: Option<serde_json::Value>) -> Result<serde_json::Value> { |
| 549 |
let r = c.send(method, path, &[], body.map(|b| serde_json::to_vec(&b)).transpose()?.unwrap_or_default()).await?; |
| 550 |
let v: serde_json::Value = r.json().unwrap_or(serde_json::Value::Null); |
| 551 |
if r.status >= 400 { |
| 552 |
let why = v |
| 553 |
.get("detail") |
| 554 |
.or(v.get("title")) |
| 555 |
.and_then(serde_json::Value::as_str) |
| 556 |
.map(String::from) |
| 557 |
.unwrap_or_else(|| String::from_utf8_lossy(&r.body).into()); |
| 558 |
bail!("{why}"); |
| 559 |
} |
| 560 |
Ok(v) |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
pub fn encode(s: &str) -> String { |
| 565 |
s.bytes() |
| 566 |
.map(|b| match b { |
| 567 |
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => (b as char).to_string(), |
| 568 |
_ => format!("%{b:02X}"), |
| 569 |
}) |
| 570 |
.collect() |
| 571572f5 Andy McCall ↶ |
571 |
} |
| 572 |
|
| 573 |
fn init_logging() { |
| 574 |
let filter = tracing_subscriber::EnvFilter::try_from_env("SPECTRA_LOG").unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")); |
| 575 |
tracing_subscriber::fmt().with_env_filter(filter).with_writer(std::io::stderr).init(); |
| 576 |
} |
| 577 |
|
| 36537112 Andy McCall ↶ |
578 |
fn default_data() -> PathBuf { |
| 579 |
if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 580 |
return PathBuf::from(x).join("spectra"); |
| 581 |
} |
| 582 |
let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 583 |
PathBuf::from(home).join(".local").join("share").join("spectra") |
| 584 |
} |
| 585 |
|
| 586 |
fn runtime() -> Result<tokio::runtime::Runtime> { |
| 587 |
Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 258bb382 Andy McCall ↶ |
588 |
} |
| 589 |
|
| 590 |
pub fn run() -> Result<i32> { |
| 591 |
let cli = Cli::parse(); |
| 592 |
ui::init(cli.plain); |
| 36537112 Andy McCall ↶ |
593 |
let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 258bb382 Andy McCall ↶ |
594 |
match cli.command { |
| 595 |
Command::Version { json } => version(json), |
| c7161ec1 Andy McCall ↶ |
596 |
Command::Status { json } => runtime()?.block_on(setup::status(&ctx, json)), |
| 597 |
Command::Probe { url, json } => runtime()?.block_on(setup::probe(&url, json)), |
| 598 |
Command::Bridge { port, owner_home } => runtime()?.block_on(setup::bridge(&ctx, port, owner_home)), |
| b044d909 Andy McCall ↶ |
599 |
Command::Brief { command } => runtime()?.block_on(async { |
| 600 |
match command { |
| 601 |
BriefCmd::Show { project, json } => brief::show(&ctx, project, json).await, |
| 602 |
BriefCmd::Set { project, text, file, owner_home } => { |
| 603 |
brief::set(&ctx, project, brief::text_of(text, file)?, owner_home).await |
| 604 |
} |
| 605 |
BriefCmd::Edit { project, owner_home } => brief::edit(&ctx, project, owner_home).await, |
| 606 |
} |
| 607 |
}), |
| 98a0fb9f Andy McCall ↶ |
608 |
Command::Connect { url, owner_home, expect, yes } => runtime()?.block_on(connect::connect(&ctx, &url, owner_home, expect, yes)), |
| 77df591d Andy McCall ↶ |
609 |
Command::Init { owner_home, name, owner: Some(key) } => { |
| 610 |
let _ = owner_home; |
| 611 |
runtime()?.block_on(identity::init_for(&ctx, &key, name)) |
| 612 |
} |
| 613 |
Command::Init { owner_home, name, owner: None } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 09752034 Andy McCall ↶ |
614 |
Command::Identity { command } => runtime()?.block_on(async { |
| 615 |
match command { |
| c7161ec1 Andy McCall ↶ |
616 |
IdentityCmd::Create { name, contact, home, json } => identity::create(&name, &contact, home, json), |
| 09752034 Andy McCall ↶ |
617 |
IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 618 |
IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 2fc88a7b Andy McCall ↶ |
619 |
} |
| 620 |
}), |
| 862f0db2 Andy McCall ↶ |
621 |
Command::Serve { bind, tls, insecure } => { |
| 571572f5 Andy McCall ↶ |
622 |
init_logging(); |
| 862f0db2 Andy McCall ↶ |
623 |
runtime()?.block_on(serve::serve(&ctx, bind, tls, insecure)) |
| 571572f5 Andy McCall ↶ |
624 |
} |
| 8d803d85 Andy McCall ↶ |
625 |
Command::Mcp { agent, auto, kind, home } => runtime()?.block_on(async { |
| 626 |
match (agent, auto) { |
| 627 |
(Some(a), _) => mcp::run(&ctx, &a, home).await, |
| 628 |
(None, _) => match auto::resolve(&ctx, &kind).await? { |
| 629 |
Some(l) => mcp::run(&ctx, &l.name, Some(l.home)).await, |
| 630 |
None => auto::unconnected(&std::env::current_dir()?, &kind).await, |
| 631 |
}, |
| 01320eef Andy McCall ↶ |
632 |
} |
| 633 |
}), |
| da84e98f Andy McCall ↶ |
634 |
Command::Say { body, channel, reply_to, json, owner_home } => { |
| 635 |
runtime()?.block_on(human::say(&ctx, channel, reply_to, body, owner_home, json)) |
| 636 |
} |
| 637 |
Command::Inbox { follow, json, owner_home } => runtime()?.block_on(human::inbox(&ctx, follow, owner_home, json)), |
| 638 |
Command::Search { query, channel, author, since, limit, json } => { |
| 639 |
runtime()?.block_on(human::search(&ctx, &query, channel, author, since, limit, json)) |
| 640 |
} |
| 641 |
Command::Api { method, path, body, owner_home } => runtime()?.block_on(human::raw(&ctx, &method, &path, body, owner_home)), |
| 2fc88a7b Andy McCall ↶ |
642 |
Command::Project { command } => runtime()?.block_on(async { |
| 643 |
match command { |
| 98a0fb9f Andy McCall ↶ |
644 |
ProjectCmd::Add { target, name, path, description, url, owner_home } => { |
| 645 |
project::add(&ctx, project::Add { target, name, path, description, url }, owner_home).await |
| 646 |
} |
| 647 |
ProjectCmd::Link { project, depends_on, remove, owner_home } => { |
| 648 |
project::link(&ctx, &project, &depends_on, remove, owner_home).await |
| 649 |
} |
| 2fc88a7b Andy McCall ↶ |
650 |
ProjectCmd::List { json } => project::list(&ctx, json).await, |
| 09752034 Andy McCall ↶ |
651 |
} |
| 652 |
}), |
| 653 |
Command::Agent { command } => runtime()?.block_on(async { |
| 654 |
match command { |
| 98a0fb9f Andy McCall ↶ |
655 |
AgentCmd::Add { name, kind, project, home, uid, learn_uid, expires, yes, owner_home } => { |
| 656 |
agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, expires, yes, owner_home }).await |
| 09752034 Andy McCall ↶ |
657 |
} |
| 87f601cc Andy McCall ↶ |
658 |
AgentCmd::Allow { name, channel, remove, owner_home } => agent::allow(&ctx, &name, &channel, remove, owner_home).await, |
| 98a0fb9f Andy McCall ↶ |
659 |
AgentCmd::Renew { name, expires, owner_home } => agent::renew(&ctx, &name, &expires, owner_home).await, |
| 09752034 Andy McCall ↶ |
660 |
AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 661 |
AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 662 |
} |
| 663 |
}), |
| 36537112 Andy McCall ↶ |
664 |
Command::Admin { command } => runtime()?.block_on(async { |
| 665 |
match command { |
| 666 |
Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 667 |
Admin::Check => admin::check(&ctx).await, |
| 668 |
Admin::Rebuild => admin::rebuild(&ctx).await, |
| 669 |
} |
| 670 |
}), |
| 258bb382 Andy McCall ↶ |
671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
fn version(as_json: bool) -> Result<i32> { |
| 675 |
if as_json { |
| 676 |
println!( |
| 677 |
"{}", |
| 678 |
serde_json::json!({ |
| 679 |
"version": env!("CARGO_PKG_VERSION"), |
| 680 |
"build": env!("SPECTRA_BUILD_ID"), |
| 681 |
"mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 682 |
"ledger_format": spectra_core::LEDGER_FORMAT, |
| 683 |
}) |
| 684 |
); |
| 685 |
} else { |
| 686 |
println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 687 |
} |
| 688 |
Ok(0) |
| 689 |
} |