leindex 1.6.6

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! WebSocket event broadcasting

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::broadcast;
use tracing::info;

/// Maximum WebSocket message size (1MB) to prevent DoS attacks
pub const MAX_WS_MESSAGE_SIZE: usize = 1_000_000;

/// Maximum WebSocket frame size (16KB) to prevent memory exhaustion
pub const MAX_WS_FRAME_SIZE: usize = 16_384;

/// Client-to-server WebSocket message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum WsClientMessage {
    /// Subscribe to project updates
    #[serde(rename = "subscribe")]
    Subscribe {
        /// Project ID to subscribe to
        project_id: String,
    },

    /// Unsubscribe from project updates
    #[serde(rename = "unsubscribe")]
    Unsubscribe {
        /// Project ID to unsubscribe from
        project_id: String,
    },

    /// Ping/heartbeat
    #[serde(rename = "ping")]
    Ping {
        /// Optional timestamp
        timestamp: Option<u64>,
    },
}

/// WebSocket event types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum WsEvent {
    /// Project added to registry
    #[serde(rename = "project_added")]
    ProjectAdded {
        /// Unique codebase identifier
        codebase_id: String,

        /// Display name of the project
        display_name: String,

        /// Base name of the project
        base_name: String,
    },

    /// Project metadata updated
    #[serde(rename = "project_updated")]
    ProjectUpdated {
        /// Unique codebase identifier
        codebase_id: String,

        /// New display name of the project
        display_name: String,
    },

    /// Project removed from registry
    #[serde(rename = "project_removed")]
    ProjectRemoved {
        /// Unique codebase identifier
        codebase_id: String,
    },

    /// Indexing progress update
    #[serde(rename = "indexing.progress")]
    IndexingProgress {
        /// Unique codebase identifier
        codebase_id: String,

        /// Current indexing phase
        phase: u32,

        /// Progress percentage (0-100)
        percent: u8,

        /// Currently processed file
        current_file: String,
    },

    /// Heartbeat/ping
    Heartbeat {
        /// Unix timestamp in milliseconds
        timestamp: u64,
    },
}

impl WsEvent {
    /// Get event type string for serialization
    pub fn event_type(&self) -> &'static str {
        match self {
            Self::ProjectAdded { .. } => "project_added",
            Self::ProjectUpdated { .. } => "project_updated",
            Self::ProjectRemoved { .. } => "project_removed",
            Self::IndexingProgress { .. } => "indexing.progress",
            Self::Heartbeat { .. } => "heartbeat",
        }
    }

    /// Convert to JSON string
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("Failed to serialize WebSocket event to JSON")
    }
}

impl std::fmt::Display for WsEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_json())
    }
}

/// Client connection state
#[derive(Debug, Clone)]
pub struct ConnectionState {
    /// Unique connection ID
    pub id: String,

    /// Subscribed project IDs (empty = all projects)
    pub subscriptions: Vec<String>,

    /// Client IP address
    pub ip_addr: Option<String>,
}

impl ConnectionState {
    /// Create new connection state
    pub fn new(id: String, ip_addr: Option<String>) -> Self {
        Self {
            id,
            subscriptions: Vec::new(),
            ip_addr,
        }
    }

    /// Check if connection is subscribed to a project
    pub fn is_subscribed_to(&self, project_id: &str) -> bool {
        // Empty subscriptions = all projects
        self.subscriptions.is_empty() || self.subscriptions.iter().any(|s| s == project_id)
    }

    /// Add subscription
    pub fn subscribe(&mut self, project_id: String) {
        if !self.subscriptions.contains(&project_id) {
            self.subscriptions.push(project_id);
        }
    }

    /// Remove subscription
    pub fn unsubscribe(&mut self, project_id: &str) {
        self.subscriptions.retain(|s| s != project_id);
    }
}

/// WebSocket connection manager
///
/// Tracks all active connections and broadcasts events
#[derive(Clone)]
pub struct WsManager {
    /// Active connections: connection_id -> state
    pub connections: Arc<tokio::sync::RwLock<HashMap<String, ConnectionState>>>,

    /// Broadcast channel for events
    pub broadcaster: broadcast::Sender<WsEvent>,
}

impl WsManager {
    /// Create new WebSocket manager
    pub fn new() -> Self {
        let (broadcaster, _) = broadcast::channel(1000);
        Self {
            connections: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            broadcaster,
        }
    }

    /// Register a new connection
    pub async fn register_connection(&self, conn_id: String, ip_addr: Option<String>) {
        let state = ConnectionState::new(conn_id.clone(), ip_addr);
        let mut connections = self.connections.write().await;
        connections.insert(conn_id.clone(), state);
        info!(
            "WebSocket connected: {} (active: {})",
            conn_id,
            connections.len()
        );
    }

    /// Unregister a connection
    pub async fn unregister_connection(&self, conn_id: &str) {
        let mut connections = self.connections.write().await;
        connections.remove(conn_id);
        info!(
            "WebSocket disconnected: {} (active: {})",
            conn_id,
            connections.len()
        );
    }

    /// Broadcast event to all connected clients
    pub async fn broadcast(&self, event: WsEvent) {
        let _ = self.broadcaster.send(event);
    }

    /// Broadcast event to specific project subscribers
    pub async fn broadcast_to_project(&self, _project_id: &str, event: WsEvent) {
        // For now, broadcast to all (filtering done per connection)
        self.broadcast(event).await;
    }

    /// Get number of active connections
    pub async fn connection_count(&self) -> usize {
        self.connections.read().await.len()
    }

    /// Get connection info by ID
    pub async fn get_connection(&self, id: &str) -> Option<ConnectionState> {
        self.connections.read().await.get(id).cloned()
    }

    /// Handle client message and update connection state
    pub async fn handle_client_message(&self, conn_id: &str, message: WsClientMessage) {
        match message {
            WsClientMessage::Subscribe { project_id } => {
                info!(
                    "Connection {} subscribing to project {}",
                    conn_id, project_id
                );
                let mut connections = self.connections.write().await;
                if let Some(state) = connections.get_mut(conn_id) {
                    state.subscribe(project_id);
                }
            }
            WsClientMessage::Unsubscribe { project_id } => {
                info!(
                    "Connection {} unsubscribing from project {}",
                    conn_id, project_id
                );
                let mut connections = self.connections.write().await;
                if let Some(state) = connections.get_mut(conn_id) {
                    state.unsubscribe(&project_id);
                }
            }
            WsClientMessage::Ping { timestamp } => {
                // Respond with pong (heartbeat)
                let pong_timestamp = timestamp.unwrap_or_else(|| {
                    std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_millis() as u64
                });
                let event = WsEvent::Heartbeat {
                    timestamp: pong_timestamp,
                };
                // Send heartbeat to all clients (broadcast)
                self.broadcast(event).await;
            }
        }
    }
}

impl Default for WsManager {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ws_event_project_added() {
        let event = WsEvent::ProjectAdded {
            codebase_id: "test_a1b2c3d4_0".to_string(),
            display_name: "Test".to_string(),
            base_name: "test".to_string(),
        };
        assert_eq!(event.event_type(), "project_added");

        let json = event.to_json();
        assert!(json.contains(r#""type":"project_added""#));
        assert!(json.contains("test_a1b2c3d4_0"));
    }

    #[test]
    fn test_ws_event_indexing_progress() {
        let event = WsEvent::IndexingProgress {
            codebase_id: "test_a1b2c3d4_0".to_string(),
            phase: 2,
            percent: 45,
            current_file: "src/lib.rs".to_string(),
        };
        assert_eq!(event.event_type(), "indexing.progress");

        let json = event.to_json();
        assert!(json.contains(r#""type":"indexing.progress""#));
        assert!(json.contains("45"));
    }

    #[test]
    fn test_ws_event_heartbeat() {
        let event = WsEvent::Heartbeat {
            timestamp: 1234567890,
        };
        assert_eq!(event.event_type(), "heartbeat");
    }

    #[test]
    fn test_connection_state_new() {
        let state = ConnectionState::new("conn_1".to_string(), Some("127.0.0.1:12345".to_string()));
        assert_eq!(state.id, "conn_1");
        assert_eq!(state.ip_addr, Some("127.0.0.1:12345".to_string()));
        assert!(state.subscriptions.is_empty());
    }

    #[test]
    fn test_connection_state_subscribe() {
        let mut state = ConnectionState::new("conn_1".to_string(), None);
        state.subscribe("proj_1".to_string());
        state.subscribe("proj_2".to_string());

        assert_eq!(state.subscriptions.len(), 2);
        assert!(state.is_subscribed_to("proj_1"));
        assert!(state.is_subscribed_to("proj_2"));
        assert!(!state.is_subscribed_to("proj_3"));
    }

    #[test]
    fn test_connection_state_unsubscribe() {
        let mut state = ConnectionState::new("conn_1".to_string(), None);
        state.subscribe("proj_1".to_string());
        state.subscribe("proj_2".to_string());

        state.unsubscribe("proj_1");
        assert_eq!(state.subscriptions.len(), 1);
        assert!(!state.is_subscribed_to("proj_1"));
        assert!(state.is_subscribed_to("proj_2"));
    }

    #[test]
    fn test_connection_state_empty_subscribes_to_all() {
        let state = ConnectionState::new("conn_1".to_string(), None);
        // Empty subscriptions means all projects
        assert!(state.is_subscribed_to("any_project"));
    }

    #[tokio::test]
    async fn test_ws_manager_new() {
        let manager = WsManager::new();
        let count = manager.connection_count().await;
        assert_eq!(count, 0);
    }

    #[tokio::test]
    async fn test_ws_manager_broadcast() {
        let manager = WsManager::new();
        let event = WsEvent::Heartbeat { timestamp: 123 };

        // Should not panic
        manager.broadcast(event).await;
    }

    #[tokio::test]
    async fn test_ws_manager_register_connection() {
        let manager = WsManager::new();
        manager
            .register_connection("conn_1".to_string(), Some("127.0.0.1".to_string()))
            .await;

        assert_eq!(manager.connection_count().await, 1);

        let conn = manager.get_connection("conn_1").await;
        assert!(conn.is_some());
        let conn = conn.expect("Connection should exist after registration");
        assert_eq!(conn.id, "conn_1");
    }

    #[tokio::test]
    async fn test_ws_manager_unregister_connection() {
        let manager = WsManager::new();
        manager
            .register_connection("conn_1".to_string(), None)
            .await;
        manager
            .register_connection("conn_2".to_string(), None)
            .await;

        assert_eq!(manager.connection_count().await, 2);

        manager.unregister_connection("conn_1").await;
        assert_eq!(manager.connection_count().await, 1);
    }
}