OXYGENHUB
# 01320eefd505 spectra / crates / spectra-mcp / src / api.rs
20.0 KiB · Rust · last changed The command line from another machine: every command through the API (#23, completes #21) 1 day ago source blame history raw
1
//! The JSON API (spec §18): what spectra-vscode, scripts and SpectraHub
2
//! read and write. Every request is a signed request (spec §8.5); a read
3
//! may instead carry the owner's Radar session. Errors are RFC 7807
4
//! problems; lists page with `?after=`; reads carry an ETag.
5
6
use crate::http::{AppState, PeerInfo};
7
use axum::body::Bytes;
8
use axum::extract::connect_info::ConnectInfo;
9
use axum::extract::{Path, Query, State};
10
use axum::http::{header, HeaderMap, StatusCode};
11
use axum::response::sse::{Event as SseEvent, KeepAlive, Sse};
12
use axum::response::{IntoResponse, Response};
13
use axum::routing::{get, post};
14
use axum::Router;
15
use futures::StreamExt;
16
use serde::Deserialize;
17
use spectra_api as api;
18
use spectra_core::authn::Caller;
19
use spectra_core::conversation::{self as conv, Author, Post, Posted, Signer};
20
use spectra_core::event::{Auth, Method};
21
use spectra_core::ids::now_ms;
22
use spectra_core::principals::Principal;
23
use std::time::Duration;
24
25
pub fn router(state: AppState) -> Router {
26
    Router::new()
27
        .route("/api/v1/health", get(health))
28
        .route("/api/v1/version", get(version))
29
        .route("/api/v1/brief", get(brief))
30
        .route("/api/v1/agents", get(agents))
31
        .route("/api/v1/projects", get(projects))
32
        .route("/api/v1/channels", get(channels))
33
        .route("/api/v1/channels/{name}/messages", get(messages).post(post_message))
34
        .route("/api/v1/threads/{id}", get(thread))
35
        .route("/api/v1/inbox", get(inbox))
36
        .route("/api/v1/inbox/ack", post(ack))
37
        .route("/api/v1/search", get(search))
38
        .route("/api/v1/events", get(events))
39
        .route("/api/v1/admin/verify", get(admin_verify))
40
        .with_state(state)
41
}
42
43
/// An RFC 7807 problem.
44
pub fn problem(status: StatusCode, title: &str, detail: Option<String>) -> Response {
45
    let p = api::Problem { kind: "about:blank".into(), title: title.into(), status: status.as_u16(), detail };
46
    let mut r = (status, axum::Json(p)).into_response();
47
    r.headers_mut().insert(header::CONTENT_TYPE, "application/problem+json".parse().unwrap());
48
    r
49
}
50
51
fn bad(e: anyhow::Error) -> Response {
52
    problem(StatusCode::BAD_REQUEST, "the request cannot be done", Some(format!("{e:#}")))
53
}
54
55
/// JSON with an ETag; 304 when the client has it already.
56
fn json_etag<T: serde::Serialize>(headers: &HeaderMap, v: &T) -> Response {
57
    let body = serde_json::to_vec(v).unwrap_or_default();
58
    let tag = format!("\"{}\"", &blake3_hex(&body)[..32]);
59
    if headers.get(header::IF_NONE_MATCH).and_then(|h| h.to_str().ok()) == Some(tag.as_str()) {
60
        return StatusCode::NOT_MODIFIED.into_response();
61
    }
62
    ([(header::CONTENT_TYPE, "application/json".to_string()), (header::ETAG, tag)], body).into_response()
63
}
64
65
fn blake3_hex(b: &[u8]) -> String {
66
    hex::encode(*spectra_core::event::hash_body_plain(b))
67
}
68
69
/// Who is asking: a signed request, or for a read the owner's session.
70
async fn caller(
71
    st: &AppState,
72
    peer: PeerInfo,
73
    headers: &HeaderMap,
74
    method_path: &str,
75
    body: &[u8],
76
    read: bool,
77
) -> Result<Caller, Response> {
78
    if read && headers.get("spectra-key").is_none() {
79
        let cookie = headers.get(header::COOKIE).and_then(|c| c.to_str().ok()).unwrap_or("");
80
        let id = cookie.split(';').map(str::trim).find_map(|kv| kv.strip_prefix("spectra_session=")).unwrap_or("");
81
        if let Some(s) = st.sessions.get(id, now_ms()) {
82
            if !crate::http::origin_ok(headers, &st.origins) {
83
                return Err(problem(StatusCode::FORBIDDEN, "this origin may not read Spectra", None));
84
            }
85
            let key = oxy_core::hash::ObjectRef(s.key);
86
            let principal = st
87
                .daemon
88
                .principals
89
                .read()
90
                .await
91
                .principal(&key, now_ms())
92
                .map_err(|why| problem(StatusCode::UNAUTHORIZED, "not authenticated", Some(why)))?;
93
            return Ok(Caller {
94
                principal,
95
                auth: Auth { key: Some(s.key), method: Method::Session, assurance: 2, proof: Some(s.proof) },
96
                learn_uid: None,
97
            });
98
        }
99
    }
100
    let signed = headers.get("spectra-key").is_some();
101
    crate::http::authenticate_why(st, peer, headers, method_path, body).await.map_err(|(status, why)| {
102
        if status == StatusCode::FORBIDDEN {
103
            problem(status, "this origin may not call Spectra", None)
104
        } else if signed {
105
            // A signed request is told why its signature was not enough.
106
            problem(StatusCode::UNAUTHORIZED, "not authenticated", Some(why))
107
        } else {
108
            problem(
109
                StatusCode::UNAUTHORIZED,
110
                "not authenticated",
111
                Some("sign the request with your key (spectra's client does), or open Radar with `spectra open`".into()),
112
            )
113
        }
114
    })
115
}
116
117
fn path_of(uri: &axum::http::Uri) -> String {
118
    uri.path_and_query().map(|p| p.as_str().to_string()).unwrap_or_else(|| uri.path().to_string())
119
}
120
121
macro_rules! who {
122
    ($st:expr, $peer:expr, $headers:expr, $uri:expr, $method:literal, $body:expr, $read:expr) => {
123
        match caller(&$st, $peer, &$headers, &format!("{} {}", $method, path_of(&$uri)), $body, $read).await {
124
            Ok(c) => c,
125
            Err(r) => return r,
126
        }
127
    };
128
}
129
130
/// `GET /api/v1/admin/verify`: the owner asks the daemon to verify its
131
/// ledger (spec §8.6): what `spectra admin verify` prints, from afar.
132
async fn admin_verify(
133
    State(st): State<AppState>,
134
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
135
    headers: HeaderMap,
136
    uri: axum::http::Uri,
137
) -> Response {
138
    let c = who!(st, peer, headers, uri, "GET", b"", false);
139
    if !matches!(c.principal, Principal::Owner { .. }) {
140
        return problem(StatusCode::FORBIDDEN, "only the owner verifies the ledger", None);
141
    }
142
    let ledger = match spectra_core::ledger::Ledger::open_read_only(&st.daemon.data.join("spectra.db")).await {
143
        Ok(l) => l,
144
        Err(e) => return bad(e),
145
    };
146
    match ledger.verify(&mut spectra_core::principals::Principals::default()).await {
147
        Ok(v) => axum::Json(v).into_response(),
148
        Err(e) => bad(e),
149
    }
150
}
151
152
async fn health() -> Response {
153
    axum::Json(serde_json::json!({ "ok": true })).into_response()
154
}
155
156
async fn version() -> Response {
157
    axum::Json(api::Version {
158
        version: env!("CARGO_PKG_VERSION").into(),
159
        ledger_format: spectra_core::LEDGER_FORMAT,
160
        mcp_protocol_versions: crate::PROTOCOL_VERSIONS.iter().map(|v| v.to_string()).collect(),
161
    })
162
    .into_response()
163
}
164
165
async fn agent_list(st: &AppState) -> Vec<api::Agent> {
166
    #[allow(clippy::type_complexity)]
167
    let rows: Vec<(String, String, String, String, String, Option<String>, Option<i64>)> = sqlx::query_as(
168
        "SELECT a.name, a.owner_name, a.key, a.kind, a.project, p.status_text, a.disabled_ms FROM agents a LEFT JOIN presence p ON p.agent = a.name ORDER BY a.name",
169
    )
170
    .fetch_all(st.daemon.ledger.pool())
171
    .await
172
    .unwrap_or_default();
173
    let presence = st.daemon.presence.lock().await;
174
    let principals = st.daemon.principals.read().await;
175
    let now = now_ms();
176
    rows.into_iter()
177
        .map(|(name, owner, key, kind, project, status, disabled)| {
178
            let k = oxy_core::hash::ObjectRef::from_hex(&key);
179
            let state = match k.map(|k| principals.principal(&k, now)) {
180
                Some(Ok(_)) => "ok".to_string(),
181
                Some(Err(why)) => why,
182
                None => "a malformed key".into(),
183
            };
184
            let delegation_expires = k.and_then(|k| principals.owner().and_then(|o| o.delegate(&k)).and_then(|d| d.expires));
185
            api::Agent {
186
                display: format!("{name} · {owner}'s agent"),
187
                presence: if disabled.is_some() { "offline".into() } else { presence.state(&name, now).into() },
188
                disabled: disabled.is_some(),
189
                name,
190
                owner,
191
                key,
192
                kind,
193
                project,
194
                status,
195
                state,
196
                delegation_expires,
197
            }
198
        })
199
        .collect()
200
}
201
202
async fn agents(
203
    State(st): State<AppState>,
204
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
205
    headers: HeaderMap,
206
    uri: axum::http::Uri,
207
) -> Response {
208
    let _ = who!(st, peer, headers, uri, "GET", b"", true);
209
    json_etag(&headers, &agent_list(&st).await)
210
}
211
212
async fn projects(
213
    State(st): State<AppState>,
214
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
215
    headers: HeaderMap,
216
    uri: axum::http::Uri,
217
) -> Response {
218
    let _ = who!(st, peer, headers, uri, "GET", b"", true);
219
    let pool = st.daemon.ledger.pool();
220
    let rows: Vec<(String, String, Option<String>)> =
221
        sqlx::query_as("SELECT name, path, remote FROM projects ORDER BY name").fetch_all(pool).await.unwrap_or_default();
222
    let mut out = Vec::new();
223
    for (name, path, remote) in rows {
224
        let depends_on = sqlx::query_scalar("SELECT depends_on FROM project_relations WHERE project = ? ORDER BY 1")
225
            .bind(&name)
226
            .fetch_all(pool)
227
            .await
228
            .unwrap_or_default();
229
        let depended_on_by = sqlx::query_scalar("SELECT project FROM project_relations WHERE depends_on = ? ORDER BY 1")
230
            .bind(&name)
231
            .fetch_all(pool)
232
            .await
233
            .unwrap_or_default();
234
        let agents = sqlx::query_scalar("SELECT name FROM agents WHERE project = ? AND disabled_ms IS NULL ORDER BY name")
235
            .bind(&name)
236
            .fetch_all(pool)
237
            .await
238
            .unwrap_or_default();
239
        out.push(api::Project { name, path, remote, depends_on, depended_on_by, agents });
240
    }
241
    json_etag(&headers, &out)
242
}
243
244
async fn channels(
245
    State(st): State<AppState>,
246
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
247
    headers: HeaderMap,
248
    uri: axum::http::Uri,
249
) -> Response {
250
    let c = who!(st, peer, headers, uri, "GET", b"", true);
251
    let all = match conv::channels(&st.daemon.ledger).await {
252
        Ok(a) => a,
253
        Err(e) => return bad(e),
254
    };
255
    let mut out = Vec::new();
256
    for ch in all.into_iter().filter(|ch| conv::can_read(&c.principal, ch)) {
257
        let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM messages WHERE channel = ?")
258
            .bind(&ch.id)
259
            .fetch_one(st.daemon.ledger.pool())
260
            .await
261
            .unwrap_or(0);
262
        out.push(api::Channel { display: ch.display(), id: ch.id, name: ch.name, kind: ch.kind, purpose: ch.purpose, messages: n });
263
    }
264
    json_etag(&headers, &out)
265
}
266
267
async fn to_api(st: &AppState, m: conv::Message, ch: &str) -> api::Message {
268
    api::Message {
269
        author_display: conv::display_name(&st.daemon.ledger, &m.author).await,
270
        mentions: serde_json::from_str(&m.mentions).unwrap_or_default(),
271
        refs: serde_json::from_str(&m.refs).unwrap_or_default(),
272
        redacted: m.redacted.is_some(),
273
        id: m.id,
274
        channel: ch.to_string(),
275
        reply_to: m.reply_to,
276
        author: m.author,
277
        kind: m.kind,
278
        body: m.body,
279
        at_ms: m.at_ms,
280
        replies: m.replies,
281
    }
282
}
283
284
#[derive(Deserialize)]
285
struct PageQuery {
286
    after: Option<String>,
287
    limit: Option<u32>,
288
}
289
290
async fn messages(
291
    State(st): State<AppState>,
292
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
293
    headers: HeaderMap,
294
    uri: axum::http::Uri,
295
    Path(name): Path<String>,
296
    Query(q): Query<PageQuery>,
297
) -> Response {
298
    let c = who!(st, peer, headers, uri, "GET", b"", true);
299
    let limit = q.limit.unwrap_or(50).clamp(1, 500);
300
    match conv::read(&st.daemon.ledger, &c.principal, Some(&name), None, q.after.as_deref(), limit).await {
301
        Ok((ch, msgs)) => {
302
            let next = (msgs.len() as u32 == limit).then(|| msgs.last().map(|m| m.id.clone())).flatten();
303
            let mut items = Vec::new();
304
            for m in msgs {
305
                items.push(to_api(&st, m, &ch.display()).await);
306
            }
307
            json_etag(&headers, &api::Messages { channel: ch.display(), items, next })
308
        }
309
        Err(e) => problem(StatusCode::NOT_FOUND, "no such channel, or not yours to read", Some(format!("{e:#}"))),
310
    }
311
}
312
313
async fn thread(
314
    State(st): State<AppState>,
315
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
316
    headers: HeaderMap,
317
    uri: axum::http::Uri,
318
    Path(id): Path<String>,
319
    Query(q): Query<PageQuery>,
320
) -> Response {
321
    let c = who!(st, peer, headers, uri, "GET", b"", true);
322
    let limit = q.limit.unwrap_or(200).clamp(1, 500);
323
    match conv::read(&st.daemon.ledger, &c.principal, None, Some(&id), q.after.as_deref(), limit).await {
324
        Ok((ch, msgs)) => {
325
            let next = (msgs.len() as u32 == limit).then(|| msgs.last().map(|m| m.id.clone())).flatten();
326
            let mut items = Vec::new();
327
            for m in msgs {
328
                items.push(to_api(&st, m, &ch.display()).await);
329
            }
330
            json_etag(&headers, &api::Messages { channel: ch.display(), items, next })
331
        }
332
        Err(e) => problem(StatusCode::NOT_FOUND, "no such thread, or not yours to read", Some(format!("{e:#}"))),
333
    }
334
}
335
336
async fn post_message(
337
    State(st): State<AppState>,
338
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
339
    headers: HeaderMap,
340
    uri: axum::http::Uri,
341
    Path(name): Path<String>,
342
    body: Bytes,
343
) -> Response {
344
    let c = who!(st, peer, headers, uri, "POST", &body, false);
345
    let n: api::NewMessage = match serde_json::from_slice(&body) {
346
        Ok(n) => n,
347
        Err(e) => return problem(StatusCode::BAD_REQUEST, "not a message", Some(e.to_string())),
348
    };
349
    let author = Author { principal: c.principal.clone(), signer: Signer::Request(c.auth.clone()) };
350
    let post = Post {
351
        channel: if n.reply_to.is_some() { None } else { Some(name) },
352
        body: n.body,
353
        reply_to: n.reply_to,
354
        kind: n.kind,
355
        ..Default::default()
356
    };
357
    match conv::post(&st.daemon.ledger, &author, post, now_ms()).await {
358
        Ok(Posted::Message { id, channel }) => {
359
            (StatusCode::CREATED, axum::Json(api::Posted { id, channel, duplicate: false })).into_response()
360
        }
361
        Ok(Posted::Duplicate { id }) => axum::Json(api::Posted { id, channel: String::new(), duplicate: true }).into_response(),
362
        Err(e) => bad(e),
363
    }
364
}
365
366
async fn inbox_items(st: &AppState, actor: &str) -> Vec<api::InboxItem> {
367
    let mut out = Vec::new();
368
    for i in conv::inbox(&st.daemon.ledger, actor, None).await.unwrap_or_default() {
369
        let from_display = conv::display_name(&st.daemon.ledger, &i.from).await;
370
        out.push(api::InboxItem {
371
            id: i.id,
372
            kind: i.kind,
373
            at_ms: i.at_ms,
374
            from: i.from,
375
            from_display,
376
            channel: i.channel,
377
            summary: i.summary,
378
        });
379
    }
380
    out
381
}
382
383
async fn inbox(State(st): State<AppState>, ConnectInfo(peer): ConnectInfo<PeerInfo>, headers: HeaderMap, uri: axum::http::Uri) -> Response {
384
    let c = who!(st, peer, headers, uri, "GET", b"", true);
385
    json_etag(&headers, &inbox_items(&st, &c.principal.actor()).await)
386
}
387
388
async fn ack(
389
    State(st): State<AppState>,
390
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
391
    headers: HeaderMap,
392
    uri: axum::http::Uri,
393
    body: Bytes,
394
) -> Response {
395
    let c = who!(st, peer, headers, uri, "POST", &body, false);
396
    let a: api::Ack = match serde_json::from_slice(&body) {
397
        Ok(a) => a,
398
        Err(e) => return problem(StatusCode::BAD_REQUEST, "not an ack", Some(e.to_string())),
399
    };
400
    let author = Author { principal: c.principal, signer: Signer::Request(c.auth) };
401
    match conv::ack(&st.daemon.ledger, &author, &a.ids, a.reaction.as_deref()).await {
402
        Ok(n) => axum::Json(serde_json::json!({ "read": n })).into_response(),
403
        Err(e) => bad(e),
404
    }
405
}
406
407
async fn brief(State(st): State<AppState>, ConnectInfo(peer): ConnectInfo<PeerInfo>, headers: HeaderMap, uri: axum::http::Uri) -> Response {
408
    let c = who!(st, peer, headers, uri, "GET", b"", true);
409
    let project = match &c.principal {
410
        Principal::Agent { project, .. } => Some(project.clone()),
411
        _ => None,
412
    };
413
    let rules = spectra_core::rules::compile(&st.daemon.data).map(|r| r.version).unwrap_or_default();
414
    let inbox = inbox_items(&st, &c.principal.actor()).await;
415
    let b = api::Brief {
416
        you: c.principal.display(),
417
        actor: c.principal.actor(),
418
        project,
419
        rules_version: rules,
420
        unread: inbox.len() as i64,
421
        inbox,
422
        agents: agent_list(&st).await,
423
        head: st.daemon.ledger.head().await.0,
424
    };
425
    json_etag(&headers, &b)
426
}
427
428
#[derive(Deserialize)]
429
struct SearchQuery {
430
    q: String,
431
    channel: Option<String>,
432
    author: Option<String>,
433
    since: Option<String>,
434
    limit: Option<u32>,
435
}
436
437
async fn search(
438
    State(st): State<AppState>,
439
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
440
    headers: HeaderMap,
441
    uri: axum::http::Uri,
442
    Query(q): Query<SearchQuery>,
443
) -> Response {
444
    let c = who!(st, peer, headers, uri, "GET", b"", true);
445
    let since = q.since.as_deref().and_then(|s| crate::tools::parse_since(s, now_ms()));
446
    match conv::search(&st.daemon.ledger, &c.principal, &q.q, q.channel.as_deref(), q.author.as_deref(), since, q.limit.unwrap_or(20)).await
447
    {
448
        Ok(hits) => {
449
            let mut out: Vec<api::Hit> = Vec::new();
450
            for h in hits {
451
                let author_display = conv::display_name(&st.daemon.ledger, &h.author).await;
452
                out.push(api::Hit {
453
                    id: h.id,
454
                    channel: h.channel,
455
                    author: h.author,
456
                    at_ms: h.at_ms,
457
                    snippet: h.snippet,
458
                    thread: h.thread,
459
                    author_display,
460
                });
461
            }
462
            json_etag(&headers, &out)
463
        }
464
        Err(e) => bad(e),
465
    }
466
}
467
468
#[derive(Deserialize)]
469
struct EventsQuery {
470
    after: Option<u64>,
471
    kind: Option<String>,
472
    limit: Option<u32>,
473
    follow: Option<u8>,
474
    /// With `follow`, end the stream after this many events.
475
    max: Option<usize>,
476
}
477
478
fn summary(e: &spectra_core::event::Event) -> api::EventSummary {
479
    api::EventSummary {
480
        seq: e.seq,
481
        id: e.id.clone(),
482
        at_ms: e.at_ms,
483
        kind: e.kind.clone(),
484
        actor: e.actor.clone(),
485
        subject: e.subject.clone(),
486
        method: e.auth.method.name().into(),
487
        assurance: e.auth.assurance,
488
        hash: hex::encode(e.hash),
489
    }
490
}
491
492
/// The log as summaries; `follow=1` streams new ones as SSE instead.
493
async fn events(
494
    State(st): State<AppState>,
495
    ConnectInfo(peer): ConnectInfo<PeerInfo>,
496
    headers: HeaderMap,
497
    uri: axum::http::Uri,
498
    Query(q): Query<EventsQuery>,
499
) -> Response {
500
    let c = who!(st, peer, headers, uri, "GET", b"", true);
501
    // The log is the owner's to read; an agent reads what concerns it
502
    // through the conversation and its inbox.
503
    if !matches!(c.principal, Principal::Owner { .. }) {
504
        return problem(StatusCode::FORBIDDEN, "the event log is the owner's", None);
505
    }
506
    if q.follow == Some(1) {
507
        let kind = q.kind.clone();
508
        let rx = st.daemon.ledger.subscribe();
509
        let stream = tokio_stream::wrappers::BroadcastStream::new(rx)
510
            .filter_map(move |e| {
511
                let kind = kind.clone();
512
                async move {
513
                    let e = e.ok()?;
514
                    if kind.as_ref().is_some_and(|k| *k != e.kind) {
515
                        return None;
516
                    }
517
                    Some(Ok::<_, std::convert::Infallible>(
518
                        SseEvent::default().data(serde_json::to_string(&summary(&e)).unwrap_or_default()),
519
                    ))
520
                }
521
            })
522
            .take(q.max.unwrap_or(usize::MAX));
523
        return Sse::new(stream).keep_alive(KeepAlive::new().interval(Duration::from_secs(15))).into_response();
524
    }
525
    let limit = q.limit.unwrap_or(100).clamp(1, 1000);
526
    let from = q.after.map(|a| a + 1).unwrap_or(1);
527
    let evs = match &q.kind {
528
        Some(k) => st.daemon.ledger.events_of_kinds(from, &[k.as_str()], limit).await,
529
        None => st.daemon.ledger.events(from, limit).await,
530
    };
531
    match evs {
532
        Ok(evs) => {
533
            let next = (evs.len() as u32 == limit).then(|| evs.last().map(|e| e.seq)).flatten();
534
            json_etag(&headers, &api::Events { items: evs.iter().map(summary).collect(), next })
535
        }
536
        Err(e) => bad(e),
537
    }
538
}