remove the agent's live terminal page and the endpoints only it used
This commit is contained in:
parent
802151820c
commit
6289b8d64b
1 changed files with 127 additions and 4053 deletions
|
|
@ -1,11 +1,10 @@
|
|||
//! Live SSE event stream + history endpoints.
|
||||
//! The live SSE event stream, which `hivectl watch` reads.
|
||||
|
||||
use std::convert::Infallible;
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::{Query, State};
|
||||
use axum::extract::State;
|
||||
use axum::response::sse::{Event, KeepAlive, Sse};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::Serialize;
|
||||
use tokio_stream::{Stream, StreamExt, wrappers::BroadcastStream};
|
||||
|
||||
use super::AppState;
|
||||
|
|
@ -18,15 +17,9 @@ use crate::term_msg::{ClassifyCtx, Level, TermMsg, classify, iso8601_utc};
|
|||
/// drop-category noise) never reaches the wire at all; see
|
||||
/// `crate::term_msg` for why.
|
||||
///
|
||||
/// `seq` is the live per-event dedup counter (`BusEvent::seq`) — `Some` on
|
||||
/// the SSE path, `None` on history replay (a stored row has no live seq).
|
||||
/// The client uses it to drop buffered live traffic it's about to see again
|
||||
/// in the initial history page, and that need didn't go away just because
|
||||
/// rows lost their `kind` tag — dropping it here would silently reintroduce
|
||||
/// duplicate rows across the live/history boundary. It is the one piece of
|
||||
/// transport plumbing left: the event's time now rides on each row's own
|
||||
/// `ts` (`crate::term_msg`), which is where a consumer with no envelope
|
||||
/// around it — the swarm queue's — can also read it.
|
||||
/// `seq` is the live per-event counter (`BusEvent::seq`). The event's time rides
|
||||
/// on each row's own `ts` (`crate::term_msg`), where a consumer with no
|
||||
/// envelope around it, such as the swarm queue's, can also read it.
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct TermEnvelope {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
|
@ -34,76 +27,6 @@ pub(super) struct TermEnvelope {
|
|||
msgs: Vec<TermMsg>,
|
||||
}
|
||||
|
||||
/// Response body for `GET /api/events/history`. `seq` is omitted from the
|
||||
/// wire entirely on a paginated (non-initial) load — matches the old
|
||||
/// `json!` shape, which only ever set the `"seq"` key when `Some`.
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct EventsHistoryBody {
|
||||
events: Vec<TermEnvelope>,
|
||||
min_id: Option<i64>,
|
||||
has_more: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
seq: Option<u64>,
|
||||
}
|
||||
|
||||
/// Query params for the paginated history endpoint.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(super) struct HistoryParams {
|
||||
/// Cursor: only return events with sqlite row id < `before`.
|
||||
/// Omit for the initial (most-recent) page.
|
||||
before: Option<i64>,
|
||||
/// Page size (default 100, capped at `HISTORY_CAPACITY`).
|
||||
limit: Option<usize>,
|
||||
}
|
||||
|
||||
pub(super) async fn events_history(
|
||||
State(state): State<AppState>,
|
||||
Query(params): Query<HistoryParams>,
|
||||
) -> Json<EventsHistoryBody> {
|
||||
use crate::events::HISTORY_CAPACITY;
|
||||
let limit = params.limit.unwrap_or(100).min(HISTORY_CAPACITY);
|
||||
let before = params.before;
|
||||
let is_initial = before.is_none();
|
||||
|
||||
// Capture seq *before* the read on initial loads so the SSE dedupe
|
||||
// window is "drop buffered events you've already seen in history",
|
||||
// never "lose an event that fired between the read and the seq."
|
||||
// On paginated loads (`before` is set) seq is not needed.
|
||||
let seq = if is_initial {
|
||||
Some(state.bus.current_seq())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let (events, min_id, has_more) = state.bus.history_page(before, limit);
|
||||
// Classify with the same function the live SSE path uses so history
|
||||
// replay and live tail deliver identical shapes. The DB stores raw
|
||||
// events; classification is applied at read time here (see
|
||||
// `crate::term_msg`). One `ClassifyCtx` for the whole page — tool_use→
|
||||
// name correlation (for markdown-vs-plain `recv` result bodies) only
|
||||
// works within a single page/connection, not across the live/history
|
||||
// boundary; see that module's doc for why that's an accepted
|
||||
// degradation.
|
||||
let mut ctx = ClassifyCtx::default();
|
||||
let events: Vec<TermEnvelope> = events
|
||||
.into_iter()
|
||||
.filter_map(|se| {
|
||||
let msgs = classify(&se.event, se.ts, &mut ctx);
|
||||
if msgs.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(TermEnvelope { seq: None, msgs })
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Json(EventsHistoryBody {
|
||||
events,
|
||||
min_id,
|
||||
has_more,
|
||||
seq,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn events_stream(
|
||||
State(state): State<AppState>,
|
||||
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue