syncable-ag-ui-server 0.2.1

Server-side AG-UI event producer for streaming to frontends - Syncable SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! WebSocket Transport for AG-UI Events
//!
//! This module provides WebSocket transport for streaming AG-UI events to frontend clients.
//! It integrates with axum to provide WebSocket endpoints as an alternative to SSE.
//!
//! # Architecture
//!
//! The WebSocket transport uses a channel-based design similar to SSE:
//! - [`WsSender`] - Used by agent code to send events into the WebSocket stream
//! - [`WsHandler`] - Handles the WebSocket connection and streams events
//!
//! # Example
//!
//! ```rust,ignore
//! use ag_ui_server::transport::ws;
//! use syncable_ag_ui_core::{Event, TextMessageStartEvent, MessageId};
//! use axum::extract::ws::WebSocketUpgrade;
//!
//! async fn ws_endpoint(upgrade: WebSocketUpgrade) -> impl IntoResponse {
//!     let (sender, handler) = ws::channel::<serde_json::Value>(32);
//!
//!     // Spawn task to send events
//!     tokio::spawn(async move {
//!         let event = Event::TextMessageStart(
//!             TextMessageStartEvent::new(MessageId::random())
//!         );
//!         sender.send(event).await.ok();
//!     });
//!
//!     handler.into_response(upgrade)
//! }
//! ```
//!
//! # SSE vs WebSocket
//!
//! Choose WebSocket when:
//! - You need bidirectional communication (future AG-UI extensions)
//! - You want lower latency for high-frequency updates
//! - You need to work around SSE connection limits in browsers
//!
//! Choose SSE when:
//! - You only need server-to-client streaming (current AG-UI)
//! - You want automatic reconnection (built into EventSource)
//! - You need HTTP/2 multiplexing benefits

use std::time::Duration;

use syncable_ag_ui_core::{AgentState, Event, JsonValue};
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::response::IntoResponse;
use futures::{SinkExt, StreamExt};
use tokio::sync::mpsc;
use tokio::time::interval;

use crate::error::ServerError;

/// Default ping interval for WebSocket keep-alive (30 seconds).
pub const DEFAULT_PING_INTERVAL: Duration = Duration::from_secs(30);

/// Error type for WebSocket send operations.
#[derive(Debug, Clone)]
pub struct SendError<T>(pub T);

impl<T> std::fmt::Display for SendError<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "WebSocket channel closed")
    }
}

impl<T: std::fmt::Debug> std::error::Error for SendError<T> {}

/// Configuration for WebSocket connections.
#[derive(Debug, Clone)]
pub struct WsConfig {
    /// Interval between ping messages for keep-alive.
    pub ping_interval: Duration,
    /// Whether to send ping messages.
    pub enable_ping: bool,
}

impl Default for WsConfig {
    fn default() -> Self {
        Self {
            ping_interval: DEFAULT_PING_INTERVAL,
            enable_ping: true,
        }
    }
}

impl WsConfig {
    /// Creates a new configuration with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the ping interval.
    pub fn ping_interval(mut self, interval: Duration) -> Self {
        self.ping_interval = interval;
        self
    }

    /// Disables ping messages.
    pub fn disable_ping(mut self) -> Self {
        self.enable_ping = false;
        self
    }
}

/// Sender side of a WebSocket channel.
///
/// Use this to send AG-UI events that will be streamed to connected clients.
/// Events are serialized to JSON and sent as WebSocket text messages.
#[derive(Debug, Clone)]
pub struct WsSender<StateT: AgentState = JsonValue> {
    sender: mpsc::Sender<Event<StateT>>,
}

impl<StateT: AgentState> WsSender<StateT> {
    /// Sends an event to the WebSocket stream.
    ///
    /// Returns an error if the receiver has been dropped (client disconnected).
    pub async fn send(&self, event: Event<StateT>) -> Result<(), SendError<Event<StateT>>> {
        self.sender.send(event).await.map_err(|e| SendError(e.0))
    }

    /// Sends multiple events to the WebSocket stream.
    ///
    /// Stops and returns an error on the first failed send.
    pub async fn send_many(
        &self,
        events: impl IntoIterator<Item = Event<StateT>>,
    ) -> Result<(), SendError<Event<StateT>>> {
        for event in events {
            self.send(event).await?;
        }
        Ok(())
    }

    /// Tries to send an event without waiting.
    ///
    /// Returns an error if the channel is full or closed.
    pub fn try_send(&self, event: Event<StateT>) -> Result<(), SendError<Event<StateT>>> {
        self.sender
            .try_send(event)
            .map_err(|e| SendError(e.into_inner()))
    }

    /// Checks if the receiver is still connected.
    pub fn is_closed(&self) -> bool {
        self.sender.is_closed()
    }
}

/// Handler side of a WebSocket channel.
///
/// This handles the WebSocket connection and streams events from the sender.
pub struct WsHandler<StateT: AgentState = JsonValue> {
    receiver: mpsc::Receiver<Event<StateT>>,
    config: WsConfig,
}

impl<StateT: AgentState> WsHandler<StateT> {
    /// Converts a WebSocket upgrade into an axum response.
    ///
    /// The response will upgrade to WebSocket and stream events as they are
    /// sent through the corresponding [`WsSender`].
    pub fn into_response(self, upgrade: WebSocketUpgrade) -> impl IntoResponse {
        upgrade.on_upgrade(move |socket| self.handle_socket(socket))
    }

    /// Handles the WebSocket connection.
    async fn handle_socket(self, socket: WebSocket) {
        let (mut ws_sender, mut ws_receiver) = socket.split();
        let mut event_receiver = self.receiver;

        // Create ping interval if enabled
        let mut ping_interval = if self.config.enable_ping {
            Some(interval(self.config.ping_interval))
        } else {
            None
        };

        loop {
            tokio::select! {
                // Handle incoming events to send
                event = event_receiver.recv() => {
                    match event {
                        Some(event) => {
                            // Serialize event to JSON
                            let json = match serde_json::to_string(&event) {
                                Ok(json) => json,
                                Err(e) => {
                                    eprintln!("WebSocket serialization error: {}", e);
                                    continue;
                                }
                            };

                            // Send as text message
                            if ws_sender.send(Message::Text(json.into())).await.is_err() {
                                // Client disconnected
                                break;
                            }
                        }
                        None => {
                            // Event channel closed, send close frame and exit
                            let _ = ws_sender.send(Message::Close(None)).await;
                            break;
                        }
                    }
                }

                // Handle ping interval
                _ = async {
                    if let Some(ref mut interval) = ping_interval {
                        interval.tick().await;
                    } else {
                        // Never completes if ping disabled
                        std::future::pending::<()>().await;
                    }
                } => {
                    if ws_sender.send(Message::Ping(vec![].into())).await.is_err() {
                        break;
                    }
                }

                // Handle incoming WebSocket messages (for close/pong)
                msg = ws_receiver.next() => {
                    match msg {
                        Some(Ok(Message::Pong(_))) => {
                            // Pong received, connection is alive
                        }
                        Some(Ok(Message::Close(_))) | None => {
                            // Client closed connection
                            break;
                        }
                        Some(Ok(_)) => {
                            // Ignore other message types (Text, Binary)
                            // AG-UI is unidirectional server->client
                        }
                        Some(Err(_)) => {
                            // WebSocket error
                            break;
                        }
                    }
                }
            }
        }
    }
}

/// Creates a new WebSocket channel pair with default configuration.
///
/// The `buffer` parameter controls how many events can be queued before
/// sends will block (or fail for `try_send`).
///
/// # Arguments
///
/// * `buffer` - The capacity of the internal channel buffer
///
/// # Returns
///
/// A tuple of (`WsSender`, `WsHandler`) that are connected.
///
/// # Example
///
/// ```rust,ignore
/// let (sender, handler) = ws::channel::<serde_json::Value>(32);
/// ```
pub fn channel<StateT: AgentState>(buffer: usize) -> (WsSender<StateT>, WsHandler<StateT>) {
    channel_with_config(buffer, WsConfig::default())
}

/// Creates a new WebSocket channel pair with custom configuration.
///
/// # Arguments
///
/// * `buffer` - The capacity of the internal channel buffer
/// * `config` - WebSocket configuration options
///
/// # Returns
///
/// A tuple of (`WsSender`, `WsHandler`) that are connected.
///
/// # Example
///
/// ```rust,ignore
/// let config = WsConfig::new()
///     .ping_interval(Duration::from_secs(15))
///     .disable_ping();
/// let (sender, handler) = ws::channel_with_config::<serde_json::Value>(32, config);
/// ```
pub fn channel_with_config<StateT: AgentState>(
    buffer: usize,
    config: WsConfig,
) -> (WsSender<StateT>, WsHandler<StateT>) {
    let (tx, rx) = mpsc::channel(buffer);
    (
        WsSender { sender: tx },
        WsHandler {
            receiver: rx,
            config,
        },
    )
}

/// Serializes an event to a WebSocket text message.
///
/// Returns the JSON string suitable for sending as a WebSocket text frame.
pub fn format_ws_message<StateT: AgentState>(event: &Event<StateT>) -> Result<String, ServerError> {
    serde_json::to_string(event).map_err(|e| ServerError::Serialization(e.to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use syncable_ag_ui_core::{MessageId, RunErrorEvent, TextMessageContentEvent, TextMessageStartEvent};

    #[tokio::test]
    async fn test_channel_creation() {
        let (sender, _handler) = channel::<JsonValue>(10);
        assert!(!sender.is_closed());
    }

    #[tokio::test]
    async fn test_channel_with_config() {
        let config = WsConfig::new()
            .ping_interval(Duration::from_secs(10))
            .disable_ping();

        let (sender, handler) = channel_with_config::<JsonValue>(10, config);
        assert!(!sender.is_closed());
        assert!(!handler.config.enable_ping);
        assert_eq!(handler.config.ping_interval, Duration::from_secs(10));
    }

    #[tokio::test]
    async fn test_send_event() {
        let (sender, mut handler) = channel::<JsonValue>(10);

        let event: Event = Event::TextMessageStart(TextMessageStartEvent::new(MessageId::random()));

        sender.send(event.clone()).await.unwrap();

        // Receive from the handler's receiver directly for testing
        let received = handler.receiver.recv().await.unwrap();
        assert_eq!(received.event_type(), event.event_type());
    }

    #[tokio::test]
    async fn test_send_many_events() {
        let (sender, mut handler) = channel::<JsonValue>(10);

        let events: Vec<Event> = vec![
            Event::TextMessageStart(TextMessageStartEvent::new(MessageId::random())),
            Event::TextMessageContent(TextMessageContentEvent::new_unchecked(
                MessageId::random(),
                "Hello",
            )),
            Event::RunError(RunErrorEvent::new("test error")),
        ];

        sender.send_many(events.clone()).await.unwrap();

        // Verify all events received
        for expected in &events {
            let received = handler.receiver.recv().await.unwrap();
            assert_eq!(received.event_type(), expected.event_type());
        }
    }

    #[tokio::test]
    async fn test_channel_close_detection() {
        let (sender, handler) = channel::<JsonValue>(10);

        // Drop the handler
        drop(handler);

        // Sender should detect closure
        assert!(sender.is_closed());

        // Send should fail
        let event: Event = Event::RunError(RunErrorEvent::new("test"));
        let result = sender.send(event).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_try_send() {
        let (sender, _handler) = channel::<JsonValue>(2);

        let event: Event = Event::RunError(RunErrorEvent::new("test"));

        // First two should succeed (buffer size is 2)
        assert!(sender.try_send(event.clone()).is_ok());
        assert!(sender.try_send(event.clone()).is_ok());

        // Third should fail (buffer full)
        assert!(sender.try_send(event).is_err());
    }

    #[test]
    fn test_format_ws_message() {
        let event: Event = Event::RunError(RunErrorEvent::new("test error"));
        let message = format_ws_message(&event).unwrap();

        assert!(message.contains("\"type\":\"RUN_ERROR\""));
        assert!(message.contains("\"message\":\"test error\""));
    }

    #[test]
    fn test_format_ws_message_complex() {
        let event: Event =
            Event::TextMessageStart(TextMessageStartEvent::new(MessageId::random()));
        let message = format_ws_message(&event).unwrap();

        assert!(message.contains("\"type\":\"TEXT_MESSAGE_START\""));
        assert!(message.contains("\"messageId\":"));
        assert!(message.contains("\"role\":\"assistant\""));
    }

    #[test]
    fn test_ws_config_default() {
        let config = WsConfig::default();
        assert!(config.enable_ping);
        assert_eq!(config.ping_interval, DEFAULT_PING_INTERVAL);
    }

    #[test]
    fn test_ws_config_builder() {
        let config = WsConfig::new()
            .ping_interval(Duration::from_secs(60))
            .disable_ping();

        assert!(!config.enable_ping);
        assert_eq!(config.ping_interval, Duration::from_secs(60));
    }

    #[test]
    fn test_send_error_display() {
        let error: SendError<i32> = SendError(42);
        assert_eq!(format!("{}", error), "WebSocket channel closed");
    }
}