Skip to main content

llm_test_bench_core/api/
websocket.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7//! WebSocket API implementation for real-time updates.
8
9use std::sync::Arc;
10use axum::{
11    extract::{
12        ws::{Message, WebSocket, WebSocketUpgrade},
13        State,
14    },
15    response::Response,
16};
17use futures::{sink::SinkExt, stream::StreamExt};
18use serde::{Deserialize, Serialize};
19use tokio::sync::broadcast;
20use tracing::{debug, error, info, warn};
21
22/// WebSocket message types
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(tag = "type", rename_all = "snake_case")]
25pub enum WsMessage {
26    /// Subscription request
27    Subscribe {
28        topics: Vec<String>,
29    },
30    /// Unsubscribe request
31    Unsubscribe {
32        topics: Vec<String>,
33    },
34    /// Ping message
35    Ping {
36        timestamp: i64,
37    },
38    /// Pong message
39    Pong {
40        timestamp: i64,
41    },
42    /// Event notification
43    Event {
44        topic: String,
45        data: serde_json::Value,
46        timestamp: i64,
47    },
48    /// Error message
49    Error {
50        code: String,
51        message: String,
52    },
53    /// Success acknowledgment
54    Ack {
55        request_id: Option<String>,
56        message: String,
57    },
58}
59
60/// WebSocket event topics
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
62pub enum WsTopic {
63    /// Benchmark progress updates
64    BenchmarkProgress,
65    /// Evaluation results
66    EvaluationResults,
67    /// System metrics
68    SystemMetrics,
69    /// Plugin events
70    PluginEvents,
71    /// Request logs
72    RequestLogs,
73}
74
75impl WsTopic {
76    pub fn as_str(&self) -> &'static str {
77        match self {
78            Self::BenchmarkProgress => "benchmark.progress",
79            Self::EvaluationResults => "evaluation.results",
80            Self::SystemMetrics => "system.metrics",
81            Self::PluginEvents => "plugin.events",
82            Self::RequestLogs => "request.logs",
83        }
84    }
85
86    pub fn from_str(s: &str) -> Option<Self> {
87        match s {
88            "benchmark.progress" => Some(Self::BenchmarkProgress),
89            "evaluation.results" => Some(Self::EvaluationResults),
90            "system.metrics" => Some(Self::SystemMetrics),
91            "plugin.events" => Some(Self::PluginEvents),
92            "request.logs" => Some(Self::RequestLogs),
93            _ => None,
94        }
95    }
96}
97
98/// WebSocket state
99#[derive(Clone)]
100pub struct WsState {
101    /// Event broadcaster
102    pub tx: broadcast::Sender<WsMessage>,
103}
104
105impl WsState {
106    /// Create new WebSocket state
107    pub fn new(capacity: usize) -> Self {
108        let (tx, _) = broadcast::channel(capacity);
109        Self { tx }
110    }
111
112    /// Broadcast an event to all subscribers
113    pub fn broadcast(&self, topic: WsTopic, data: serde_json::Value) {
114        let msg = WsMessage::Event {
115            topic: topic.as_str().to_string(),
116            data,
117            timestamp: chrono::Utc::now().timestamp_millis(),
118        };
119
120        // Ignore send errors (no receivers)
121        let _ = self.tx.send(msg);
122    }
123
124    /// Broadcast a generic message
125    pub fn broadcast_message(&self, msg: WsMessage) {
126        let _ = self.tx.send(msg);
127    }
128}
129
130/// WebSocket upgrade handler
131pub async fn ws_handler<S: Clone + Send + Sync + 'static>(
132    ws: WebSocketUpgrade,
133    State(state): State<Arc<WsState>>,
134) -> Response {
135    ws.on_upgrade(|socket| handle_socket(socket, state))
136}
137
138/// Handle WebSocket connection
139async fn handle_socket(socket: WebSocket, state: Arc<WsState>) {
140    info!("New WebSocket connection established");
141
142    let (mut sender, mut receiver) = socket.split();
143
144    // Subscribe to broadcast channel
145    let mut rx = state.tx.subscribe();
146
147    // Track subscribed topics for this connection (shared between tasks)
148    let subscribed_topics = Arc::new(parking_lot::RwLock::new(Vec::<String>::new()));
149    let subscribed_topics_send = subscribed_topics.clone();
150
151    // Send welcome message
152    let welcome = WsMessage::Ack {
153        request_id: None,
154        message: "Connected to LLM Test Bench API".to_string(),
155    };
156
157    if let Ok(msg) = serde_json::to_string(&welcome) {
158        if sender.send(Message::Text(msg)).await.is_err() {
159            error!("Failed to send welcome message");
160            return;
161        }
162    }
163
164    // Spawn a task to forward broadcast messages to this client
165    let mut send_task = tokio::spawn(async move {
166        while let Ok(msg) = rx.recv().await {
167            // Filter messages based on subscriptions
168            let should_send = match &msg {
169                WsMessage::Event { topic, .. } => {
170                    let topics = subscribed_topics_send.read();
171                    topics.is_empty() || topics.contains(topic)
172                }
173                _ => true, // Always send non-event messages
174            };
175
176            if !should_send {
177                continue;
178            }
179
180            let text = match serde_json::to_string(&msg) {
181                Ok(t) => t,
182                Err(e) => {
183                    error!("Failed to serialize message: {}", e);
184                    continue;
185                }
186            };
187
188            if sender.send(Message::Text(text)).await.is_err() {
189                warn!("Client disconnected");
190                break;
191            }
192        }
193    });
194
195    // Handle incoming messages from client
196    let mut recv_task = tokio::spawn(async move {
197        while let Some(Ok(msg)) = receiver.next().await {
198            match msg {
199                Message::Text(text) => {
200                    debug!("Received message: {}", text);
201
202                    // Parse and handle message
203                    match serde_json::from_str::<WsMessage>(&text) {
204                        Ok(ws_msg) => {
205                            match ws_msg {
206                                WsMessage::Subscribe { topics } => {
207                                    info!("Client subscribing to topics: {:?}", topics);
208                                    let mut subs = subscribed_topics.write();
209                                    for topic in &topics {
210                                        if !subs.contains(topic) {
211                                            subs.push(topic.clone());
212                                        }
213                                    }
214
215                                    // Send acknowledgment
216                                    let ack = WsMessage::Ack {
217                                        request_id: None,
218                                        message: format!("Subscribed to {} topics", topics.len()),
219                                    };
220
221                                    if let Ok(ack_msg) = serde_json::to_string(&ack) {
222                                        // Note: Can't send from this task, would need channel
223                                        debug!("Would send ack: {}", ack_msg);
224                                    }
225                                }
226                                WsMessage::Unsubscribe { topics } => {
227                                    info!("Client unsubscribing from topics: {:?}", topics);
228                                    subscribed_topics.write().retain(|t| !topics.contains(t));
229                                }
230                                WsMessage::Ping { timestamp } => {
231                                    debug!("Received ping at {}", timestamp);
232                                    // Pong is sent automatically by axum
233                                }
234                                _ => {
235                                    warn!("Received unexpected message type");
236                                }
237                            }
238                        }
239                        Err(e) => {
240                            error!("Failed to parse message: {}", e);
241                            let error_msg = WsMessage::Error {
242                                code: "INVALID_MESSAGE".to_string(),
243                                message: format!("Failed to parse message: {}", e),
244                            };
245
246                            if let Ok(err_text) = serde_json::to_string(&error_msg) {
247                                debug!("Would send error: {}", err_text);
248                            }
249                        }
250                    }
251                }
252                Message::Binary(_) => {
253                    warn!("Received unexpected binary message");
254                }
255                Message::Ping(_) => {
256                    debug!("Received ping");
257                }
258                Message::Pong(_) => {
259                    debug!("Received pong");
260                }
261                Message::Close(_) => {
262                    info!("Client closing connection");
263                    break;
264                }
265            }
266        }
267    });
268
269    // Wait for either task to finish
270    tokio::select! {
271        _ = &mut send_task => {
272            recv_task.abort();
273        }
274        _ = &mut recv_task => {
275            send_task.abort();
276        }
277    }
278
279    info!("WebSocket connection closed");
280}
281
282/// WebSocket router
283pub fn ws_router<S: Clone + Send + Sync + 'static>() -> axum::Router<Arc<WsState>> {
284    axum::Router::new()
285        .route("/ws", axum::routing::get(ws_handler::<S>))
286}
287
288#[cfg(test)]
289mod tests {
290    use super::*;
291
292    #[test]
293    fn test_ws_topic_conversion() {
294        assert_eq!(WsTopic::BenchmarkProgress.as_str(), "benchmark.progress");
295        assert_eq!(
296            WsTopic::from_str("benchmark.progress"),
297            Some(WsTopic::BenchmarkProgress)
298        );
299        assert_eq!(WsTopic::from_str("unknown"), None);
300    }
301
302    #[test]
303    fn test_ws_message_serialization() {
304        let msg = WsMessage::Subscribe {
305            topics: vec!["benchmark.progress".to_string()],
306        };
307
308        let json = serde_json::to_string(&msg).unwrap();
309        assert!(json.contains("subscribe"));
310        assert!(json.contains("benchmark.progress"));
311    }
312
313    #[test]
314    fn test_ws_state_creation() {
315        let state = WsState::new(100);
316
317        // Should be able to broadcast without panic
318        state.broadcast(
319            WsTopic::SystemMetrics,
320            serde_json::json!({"cpu": 50.0}),
321        );
322    }
323}