Skip to main content

lago_api/sse/
format.rs

1use lago_core::EventEnvelope;
2
3/// A single frame to be sent over the SSE connection.
4pub struct SseFrame {
5    /// Optional event type (the `event:` field in SSE).
6    pub event: Option<String>,
7    /// The `data:` field in SSE (JSON-encoded string, typically).
8    pub data: String,
9    /// Optional event ID (the `id:` field in SSE). Used for reconnection.
10    pub id: Option<String>,
11}
12
13/// Trait for formatting `EventEnvelope`s into SSE frames for a given wire
14/// protocol (OpenAI, Anthropic, Vercel AI SDK, or native Lago format).
15pub trait SseFormat: Send + Sync {
16    /// Convert an event envelope into zero or more SSE frames.
17    ///
18    /// Returning an empty vec means the event is filtered out (not relevant
19    /// to this format).
20    fn format(&self, event: &EventEnvelope) -> Vec<SseFrame>;
21
22    /// An optional "done" frame to send when the stream terminates.
23    fn done_frame(&self) -> Option<SseFrame>;
24
25    /// Extra HTTP headers to include in the SSE response (e.g. Vercel's
26    /// `x-vercel-ai-data-stream` header).
27    fn extra_headers(&self) -> Vec<(String, String)>;
28
29    /// Human-readable name of this format (for logging).
30    fn name(&self) -> &str;
31}