chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! WebSocket Handler for bidirectional real-time communication
//!
//! This module provides WebSocket-based communication for scenarios requiring
//! bidirectional messaging, such as live chat streaming, agent control, and
//! collaborative editing.
//!
//! Note: For simpler use cases, the SSE-based sync (in sync.rs) may be preferred
//! as it has better HTTP/2 compatibility and doesn't require connection upgrades.

use actix_web::{web, Error, HttpRequest, HttpResponse};
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::RwLock;
use std::time::{Duration, Instant};
use tokio::sync::broadcast;
use uuid::Uuid;

// =============================================================================
// WebSocket Configuration
// =============================================================================

const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
const CLIENT_TIMEOUT: Duration = Duration::from_secs(60);

// =============================================================================
// WebSocket Message Types
// =============================================================================

/// Messages sent from client to server
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WsClientMessage {
    /// Subscribe to a channel
    Subscribe { channel: String },
    /// Unsubscribe from a channel
    Unsubscribe { channel: String },

    /// Start streaming response for a session
    StreamStart { session_id: String, model: String },
    /// Cancel streaming for a session
    StreamCancel { session_id: String },
    /// Send input during streaming
    StreamInput { session_id: String, content: String },

    /// Send command to an agent
    AgentCommand {
        agent_id: String,
        command: String,
        params: Option<serde_json::Value>,
    },

    /// Request sync delta from version
    SyncRequest { from_version: u64 },

    /// Ping message for keepalive
    Ping { timestamp: i64 },
}

/// Messages sent from server to client
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WsServerMessage {
    /// Connection established
    Connected { client_id: String, version: u64 },
    /// Error occurred
    Error { code: String, message: String },

    /// Successfully subscribed to channel
    Subscribed { channel: String },
    /// Successfully unsubscribed from channel
    Unsubscribed { channel: String },

    /// Streaming token received
    StreamToken { session_id: String, token: String },
    /// Streaming completed
    StreamComplete {
        session_id: String,
        message_id: String,
    },
    /// Streaming error occurred
    StreamError { session_id: String, error: String },

    /// Agent event received
    AgentEvent {
        agent_id: String,
        event: String,
        data: Option<serde_json::Value>,
    },

    /// Sync event for real-time updates
    SyncEvent {
        entity_type: String,
        entity_id: String,
        operation: String,
        data: Option<serde_json::Value>,
        version: u64,
    },

    /// Pong response to ping
    Pong { timestamp: i64 },
}

// =============================================================================
// WebSocket State Management
// =============================================================================

/// Information about a connected client
#[derive(Debug, Clone)]
pub struct ClientInfo {
    pub id: String,
    pub connected_at: Instant,
    pub last_heartbeat: Instant,
    pub subscriptions: Vec<String>,
}

/// Global WebSocket state shared across connections
pub struct WebSocketState {
    /// Broadcast channel for server-wide messages
    pub broadcast_tx: broadcast::Sender<WsServerMessage>,
    /// Per-channel broadcast senders
    pub channel_senders: RwLock<HashMap<String, broadcast::Sender<WsServerMessage>>>,
    /// Connected clients info
    pub clients: RwLock<HashMap<String, ClientInfo>>,
    /// Current sync version
    pub version: std::sync::atomic::AtomicU64,
}

impl WebSocketState {
    pub fn new() -> Self {
        let (broadcast_tx, _) = broadcast::channel(1024);
        Self {
            broadcast_tx,
            channel_senders: RwLock::new(HashMap::new()),
            clients: RwLock::new(HashMap::new()),
            version: std::sync::atomic::AtomicU64::new(1),
        }
    }

    /// Get or create a channel sender
    pub fn get_channel_sender(&self, channel: &str) -> broadcast::Sender<WsServerMessage> {
        {
            let channels = self.channel_senders.read().unwrap();
            if let Some(sender) = channels.get(channel) {
                return sender.clone();
            }
        }

        let mut channels = self.channel_senders.write().unwrap();
        let entry = channels
            .entry(channel.to_string())
            .or_insert_with(|| broadcast::channel(256).0);
        entry.clone()
    }

    /// Broadcast to all clients
    pub fn broadcast(&self, msg: WsServerMessage) {
        let _ = self.broadcast_tx.send(msg);
    }

    /// Broadcast to a specific channel
    pub fn broadcast_to_channel(&self, channel: &str, msg: WsServerMessage) {
        let channels = self.channel_senders.read().unwrap();
        if let Some(sender) = channels.get(channel) {
            let _ = sender.send(msg);
        }
    }

    /// Increment version and return new value
    pub fn increment_version(&self) -> u64 {
        self.version
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            + 1
    }

    /// Get current version
    pub fn current_version(&self) -> u64 {
        self.version.load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Register a new client
    pub fn register_client(&self, id: &str) {
        let mut clients = self.clients.write().unwrap();
        clients.insert(
            id.to_string(),
            ClientInfo {
                id: id.to_string(),
                connected_at: Instant::now(),
                last_heartbeat: Instant::now(),
                subscriptions: Vec::new(),
            },
        );
    }

    /// Unregister a client
    pub fn unregister_client(&self, id: &str) {
        let mut clients = self.clients.write().unwrap();
        clients.remove(id);
    }

    /// Get client count
    pub fn client_count(&self) -> usize {
        self.clients.read().unwrap().len()
    }
}

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

// =============================================================================
// WebSocket Handler
// =============================================================================

/// Handle incoming WebSocket message
fn handle_client_message(
    client_id: &str,
    msg: WsClientMessage,
    state: &WebSocketState,
) -> Option<WsServerMessage> {
    match msg {
        WsClientMessage::Subscribe { channel } => {
            // Update client subscriptions
            if let Ok(mut clients) = state.clients.write() {
                if let Some(client) = clients.get_mut(client_id) {
                    if !client.subscriptions.contains(&channel) {
                        client.subscriptions.push(channel.clone());
                    }
                }
            }
            Some(WsServerMessage::Subscribed { channel })
        }

        WsClientMessage::Unsubscribe { channel } => {
            // Update client subscriptions
            if let Ok(mut clients) = state.clients.write() {
                if let Some(client) = clients.get_mut(client_id) {
                    client.subscriptions.retain(|c| c != &channel);
                }
            }
            Some(WsServerMessage::Unsubscribed { channel })
        }

        WsClientMessage::Ping { timestamp } => Some(WsServerMessage::Pong { timestamp }),

        WsClientMessage::StreamStart { session_id, model } => {
            log::info!(
                "Client {} requested stream start for {} with model {}",
                client_id,
                session_id,
                model
            );
            // TODO: Implement streaming start
            None
        }

        WsClientMessage::StreamCancel { session_id } => {
            log::info!(
                "Client {} requested stream cancel for {}",
                client_id,
                session_id
            );
            // TODO: Implement streaming cancel
            None
        }

        WsClientMessage::StreamInput {
            session_id,
            content,
        } => {
            log::info!(
                "Client {} sent input for {}: {} bytes",
                client_id,
                session_id,
                content.len()
            );
            // TODO: Implement streaming input
            None
        }

        WsClientMessage::AgentCommand {
            agent_id,
            command,
            params,
        } => {
            log::info!(
                "Client {} sent agent command {} to {}: {:?}",
                client_id,
                command,
                agent_id,
                params
            );
            // TODO: Implement agent commands
            None
        }

        WsClientMessage::SyncRequest { from_version } => {
            log::info!(
                "Client {} requested sync from version {}",
                client_id,
                from_version
            );
            // TODO: Implement sync delta response
            None
        }
    }
}

/// WebSocket endpoint handler using actix-ws
pub async fn ws_handler(
    req: HttpRequest,
    body: web::Payload,
    state: web::Data<WebSocketState>,
) -> Result<HttpResponse, Error> {
    // Perform WebSocket handshake
    let (response, mut session, mut msg_stream) = actix_ws::handle(&req, body)?;

    let client_id = Uuid::new_v4().to_string();
    let state_clone = state.clone();

    // Register client
    state.register_client(&client_id);

    // Send connected message
    let connected_msg = WsServerMessage::Connected {
        client_id: client_id.clone(),
        version: state.current_version(),
    };
    if let Ok(json) = serde_json::to_string(&connected_msg) {
        let _ = session.text(json).await;
    }

    log::info!("WebSocket client {} connected", client_id);

    // Subscribe to broadcast channel
    let mut broadcast_rx = state.broadcast_tx.subscribe();

    // Spawn handler task
    let client_id_clone = client_id.clone();
    actix_web::rt::spawn(async move {
        let mut heartbeat_interval = tokio::time::interval(HEARTBEAT_INTERVAL);
        let mut last_heartbeat = Instant::now();

        loop {
            tokio::select! {
                // Handle incoming messages
                Some(msg_result) = msg_stream.next() => {
                    match msg_result {
                        Ok(actix_ws::Message::Text(text)) => {
                            last_heartbeat = Instant::now();
                            if let Ok(client_msg) = serde_json::from_str::<WsClientMessage>(&text) {
                                if let Some(response) = handle_client_message(
                                    &client_id_clone,
                                    client_msg,
                                    &state_clone,
                                ) {
                                    if let Ok(json) = serde_json::to_string(&response) {
                                        let _ = session.text(json).await;
                                    }
                                }
                            } else {
                                let error_msg = WsServerMessage::Error {
                                    code: "invalid_message".to_string(),
                                    message: "Failed to parse message".to_string(),
                                };
                                if let Ok(json) = serde_json::to_string(&error_msg) {
                                    let _ = session.text(json).await;
                                }
                            }
                        }
                        Ok(actix_ws::Message::Ping(data)) => {
                            last_heartbeat = Instant::now();
                            let _ = session.pong(&data).await;
                        }
                        Ok(actix_ws::Message::Pong(_)) => {
                            last_heartbeat = Instant::now();
                        }
                        Ok(actix_ws::Message::Close(_)) => {
                            log::info!("WebSocket client {} requested close", client_id_clone);
                            break;
                        }
                        _ => {}
                    }
                }

                // Handle broadcast messages
                Ok(msg) = broadcast_rx.recv() => {
                    if let Ok(json) = serde_json::to_string(&msg) {
                        let _ = session.text(json).await;
                    }
                }

                // Heartbeat check
                _ = heartbeat_interval.tick() => {
                    if Instant::now().duration_since(last_heartbeat) > CLIENT_TIMEOUT {
                        log::warn!("WebSocket client {} timed out", client_id_clone);
                        break;
                    }
                    let _ = session.ping(b"").await;
                }
            }
        }

        // Cleanup
        state_clone.unregister_client(&client_id_clone);
        let _ = session.close(None).await;
        log::info!("WebSocket client {} disconnected", client_id_clone);
    });

    Ok(response)
}

/// Configure WebSocket routes
pub fn configure_websocket_routes(cfg: &mut web::ServiceConfig, state: web::Data<WebSocketState>) {
    cfg.app_data(state).route("/ws", web::get().to(ws_handler));
}

// =============================================================================
// Helper Functions for Broadcasting
// =============================================================================

/// Broadcast a sync event to all clients
pub fn broadcast_sync_event(
    state: &WebSocketState,
    entity_type: &str,
    entity_id: &str,
    operation: &str,
    data: Option<serde_json::Value>,
) {
    let version = state.increment_version();
    let msg = WsServerMessage::SyncEvent {
        entity_type: entity_type.to_string(),
        entity_id: entity_id.to_string(),
        operation: operation.to_string(),
        data,
        version,
    };
    state.broadcast(msg);
}

/// Broadcast a stream token to a specific session channel
pub fn broadcast_stream_token(state: &WebSocketState, session_id: &str, token: &str) {
    let msg = WsServerMessage::StreamToken {
        session_id: session_id.to_string(),
        token: token.to_string(),
    };
    state.broadcast_to_channel(&format!("session:{}", session_id), msg);
}

/// Broadcast stream completion
pub fn broadcast_stream_complete(state: &WebSocketState, session_id: &str, message_id: &str) {
    let msg = WsServerMessage::StreamComplete {
        session_id: session_id.to_string(),
        message_id: message_id.to_string(),
    };
    state.broadcast_to_channel(&format!("session:{}", session_id), msg);
}

/// Broadcast an agent event
pub fn broadcast_agent_event(
    state: &WebSocketState,
    agent_id: &str,
    event: &str,
    data: Option<serde_json::Value>,
) {
    let msg = WsServerMessage::AgentEvent {
        agent_id: agent_id.to_string(),
        event: event.to_string(),
        data,
    };
    state.broadcast_to_channel(&format!("agent:{}", agent_id), msg);
}