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
|
//! WebSocket handler for file change subscriptions.
//!
//! Clients can subscribe to specific files and receive real-time notifications
//! when those files are updated by any source (user edits, LLM modifications, etc.).
use axum::{
extract::{ws::Message, ws::WebSocket, State, WebSocketUpgrade},
response::Response,
};
use futures::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use uuid::Uuid;
use crate::server::state::SharedState;
/// Client message for file subscription management.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum FileClientMessage {
/// Subscribe to updates for a specific file
Subscribe {
#[serde(rename = "fileId")]
file_id: Uuid,
},
/// Unsubscribe from updates for a specific file
Unsubscribe {
#[serde(rename = "fileId")]
file_id: Uuid,
},
}
/// Server message for file subscription WebSocket.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum FileServerMessage {
/// Subscription confirmed
Subscribed {
#[serde(rename = "fileId")]
file_id: Uuid,
},
/// Unsubscription confirmed
Unsubscribed {
#[serde(rename = "fileId")]
file_id: Uuid,
},
/// File was updated
FileUpdated {
#[serde(rename = "fileId")]
file_id: Uuid,
version: i32,
#[serde(rename = "updatedFields")]
updated_fields: Vec<String>,
#[serde(rename = "updatedBy")]
updated_by: String,
},
/// Error occurred
Error { code: String, message: String },
}
/// WebSocket upgrade handler for file subscriptions.
#[utoipa::path(
get,
path = "/api/v1/files/subscribe",
responses(
(status = 101, description = "WebSocket connection established"),
),
tag = "Files"
)]
pub async fn file_subscription_handler(
ws: WebSocketUpgrade,
State(state): State<SharedState>,
) -> Response {
ws.on_upgrade(|socket| handle_file_subscription(socket, state))
}
async fn handle_file_subscription(socket: WebSocket, state: SharedState) {
let (mut sender, mut receiver) = socket.split();
// Set of file IDs this client is subscribed to
let mut subscriptions: HashSet<Uuid> = HashSet::new();
// Subscribe to the broadcast channel
let mut broadcast_rx = state.file_updates.subscribe();
loop {
tokio::select! {
// Handle incoming WebSocket messages from client
msg = receiver.next() => {
match msg {
Some(Ok(Message::Text(text))) => {
match serde_json::from_str::<FileClientMessage>(&text) {
Ok(FileClientMessage::Subscribe { file_id }) => {
subscriptions.insert(file_id);
let response = FileServerMessage::Subscribed { file_id };
let json = serde_json::to_string(&response).unwrap();
if sender.send(Message::Text(json.into())).await.is_err() {
break;
}
tracing::debug!("Client subscribed to file {}", file_id);
}
Ok(FileClientMessage::Unsubscribe { file_id }) => {
subscriptions.remove(&file_id);
let response = FileServerMessage::Unsubscribed { file_id };
let json = serde_json::to_string(&response).unwrap();
if sender.send(Message::Text(json.into())).await.is_err() {
break;
}
tracing::debug!("Client unsubscribed from file {}", file_id);
}
Err(e) => {
let response = FileServerMessage::Error {
code: "PARSE_ERROR".into(),
message: e.to_string(),
};
let json = serde_json::to_string(&response).unwrap();
let _ = sender.send(Message::Text(json.into())).await;
}
}
}
Some(Ok(Message::Close(_))) | None => {
tracing::debug!("Client disconnected from file subscription");
break;
}
Some(Err(e)) => {
tracing::warn!("WebSocket error: {}", e);
break;
}
_ => {}
}
}
// Handle broadcast notifications
notification = broadcast_rx.recv() => {
match notification {
Ok(notification) => {
// Only forward if client is subscribed to this file
if subscriptions.contains(¬ification.file_id) {
let response = FileServerMessage::FileUpdated {
file_id: notification.file_id,
version: notification.version,
updated_fields: notification.updated_fields,
updated_by: notification.updated_by,
};
let json = serde_json::to_string(&response).unwrap();
if sender.send(Message::Text(json.into())).await.is_err() {
break;
}
}
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
// Client is too slow, skip some messages
tracing::warn!("File subscription client lagged, skipped {} messages", n);
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
// Channel closed, exit
break;
}
}
}
}
}
}
|