Skip to content

Commit 0caea66

Browse files
committed
Standardize WebSocket message handling to consistently use 'apply-chat-response' and remove redundant comments
1 parent 5bc68de commit 0caea66

2 files changed

Lines changed: 8 additions & 37 deletions

File tree

packages/vscode/src/services/websocket-manager.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ export class WebSocketManager {
5252
const tester = net
5353
.createServer()
5454
.once('error', () => {
55-
// Port is in use
5655
resolve(true)
5756
})
5857
.once('listening', () => {
59-
// Port is free
6058
tester.close()
6159
resolve(false)
6260
})
@@ -121,13 +119,11 @@ export class WebSocketManager {
121119
}
122120

123121
private async _connect_as_client() {
124-
// Close existing connection if any
125122
if (this.client) {
126123
this.client.close()
127124
this.client = null
128125
}
129126

130-
// Reset client ID when reconnecting
131127
this.client_id = null
132128

133129
// Check if server is running, restart if not
@@ -143,23 +139,20 @@ export class WebSocketManager {
143139
})
144140
// If server fails to start, don't attempt to connect immediately
145141
if (this.should_reconnect) {
146-
// Only schedule reconnect if allowed
147142
this._schedule_reconnect()
148143
}
149-
return // Exit the function
144+
return
150145
}
151146
}
152147

153-
// Connect to the WebSocket server
154-
const wsUrl = `ws://localhost:${this.port}?token=${this.security_token}&vscode_extension_version=${this.current_extension_version}`
155-
this.client = new WebSocket.WebSocket(wsUrl)
148+
const ws_url = `ws://localhost:${this.port}?token=${this.security_token}&vscode_extension_version=${this.current_extension_version}`
149+
this.client = new WebSocket.WebSocket(ws_url)
156150

157151
this.client.on('open', () => {
158152
Logger.log({
159153
function_name: 'connect_to_server',
160154
message: 'Connected to WebSocket server'
161155
})
162-
// Start ping interval to keep connection alive
163156
})
164157

165158
this.client.on('message', async (data) => {
@@ -215,9 +208,7 @@ export class WebSocketManager {
215208
this.has_connected_browsers = false
216209
this._on_connection_status_change.fire(false)
217210

218-
// Schedule reconnect
219211
if (this.should_reconnect) {
220-
// Only schedule reconnect if allowed
221212
this._schedule_reconnect()
222213
}
223214
})
@@ -230,9 +221,7 @@ export class WebSocketManager {
230221
this.has_connected_browsers = false
231222
this._on_connection_status_change.fire(false)
232223

233-
// Schedule reconnect
234224
if (this.should_reconnect) {
235-
// Only schedule reconnect if allowed
236225
this._schedule_reconnect()
237226
}
238227
})

packages/vscode/src/services/websocket-server-process.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as http from 'http'
22
import * as process from 'process'
33
// eslint-disable-next-line @typescript-eslint/no-var-requires
4-
const WebSocket = require('ws') // ws works only with requrie
4+
const WebSocket = require('ws')
55

66
import { DEFAULT_PORT, SECURITY_TOKENS } from '@shared/constants/websocket'
77
import { Website } from '@shared/types/websocket-message'
@@ -70,7 +70,6 @@ class WebSocketServer {
7070
}
7171

7272
private _handle_connection(ws: any, request: any): void {
73-
// Verify security token
7473
const url = new URL(request.url || '', `http://localhost:${DEFAULT_PORT}`)
7574
const token = url.searchParams.get('token')
7675

@@ -79,7 +78,6 @@ class WebSocketServer {
7978
return
8079
}
8180

82-
// Track if this is a browser connection
8381
const is_browser_client = token == SECURITY_TOKENS.BROWSERS
8482

8583
if (is_browser_client) {
@@ -90,18 +88,13 @@ class WebSocketServer {
9088

9189
this.connections.add(ws)
9290

93-
// Handle messages from clients
9491
ws.on('message', (message: any) => this._handle_message(message))
95-
96-
// Handle client disconnection
9792
ws.on('close', () => this._handle_disconnection(ws, is_browser_client))
9893
}
9994

10095
private _handle_browser_connection(ws: WebSocket, url: URL): void {
101-
// Extract version from URL parameters
10296
const version = url.searchParams.get('version') || 'unknown'
10397

104-
// Check if there is already a connected browser client
10598
if (
10699
this.current_browser_client &&
107100
this.current_browser_client.ws.readyState == WebSocket.OPEN
@@ -110,9 +103,8 @@ class WebSocketServer {
110103
return
111104
}
112105

113-
// Store the new browser client
114106
this.current_browser_client = { ws, version }
115-
this._notify_vscode_clients() // Notify when a browser connects
107+
this._notify_vscode_clients()
116108
}
117109

118110
private _handle_vscode_connection(ws: WebSocket, url: URL): void {
@@ -140,26 +132,22 @@ class WebSocketServer {
140132
const client_id = this._generate_client_id()
141133
this.vscode_clients.set(client_id, { ws, client_id })
142134

143-
// Send the client ID to the VS Code client
144135
ws.send(
145136
JSON.stringify({
146137
action: 'client-id-assignment',
147138
client_id
148139
})
149140
)
150141

151-
// Send initial status to new VS Code client
152142
ws.send(
153143
JSON.stringify({
154144
action: 'browser-connection-status',
155145
has_connected_browsers: this.current_browser_client !== null
156146
})
157147
)
158148

159-
// Send saved websites to new VS Code client
160149
this._send_saved_websites_to_client(ws)
161150

162-
// Notify browser when a new VSCode client connects (if browser is connected)
163151
if (
164152
this.current_browser_client &&
165153
this.current_browser_client.ws.readyState === WebSocket.OPEN
@@ -209,15 +197,12 @@ class WebSocketServer {
209197
}
210198
this.current_browser_client.ws.send(JSON.stringify(legacy_message))
211199
} else {
212-
// Forward the message as-is for newer browser clients
213200
this.current_browser_client.ws.send(msg_string)
214201
}
215202
}
216203
} else if (msg_data.action == 'update-saved-websites') {
217-
// Store the updated websites
218204
this.saved_websites = msg_data.websites
219205

220-
// Forward to VS Code clients
221206
for (const client of this.vscode_clients.values()) {
222207
if (client.ws.readyState == WebSocket.OPEN) {
223208
client.ws.send(msg_string)
@@ -227,7 +212,6 @@ class WebSocketServer {
227212
msg_data.action == 'invoke-fast-replace' || // <-- remove few weeks after 19 Apr 2025
228213
msg_data.action == 'apply-chat-response'
229214
) {
230-
// Forward the message to the specific VS Code client based on client_id
231215
const target_client_id = msg_data.client_id
232216
const target_client = this.vscode_clients.get(target_client_id)
233217
if (target_client && target_client.ws.readyState == WebSocket.OPEN) {
@@ -252,9 +236,8 @@ class WebSocketServer {
252236
this.current_browser_client.ws === ws
253237
) {
254238
this.current_browser_client = null
255-
this._notify_vscode_clients() // Notify when the browser disconnects
239+
this._notify_vscode_clients()
256240
} else {
257-
// Find and remove the disconnected VS Code client
258241
let disconnected_client_id: number | null = null
259242
for (const [client_id, client] of this.vscode_clients.entries()) {
260243
if (client.ws === ws) {
@@ -264,7 +247,6 @@ class WebSocketServer {
264247
}
265248
}
266249

267-
// Notify browser when a VSCode client disconnects (if browser is connected)
268250
if (
269251
disconnected_client_id !== null &&
270252
this.current_browser_client &&
@@ -359,7 +341,7 @@ class WebSocketServer {
359341
if (p1 > p2) return true
360342
if (p1 < p2) return false
361343
}
362-
return false // Versions are equal or v1 is not newer
344+
return false
363345
}
364346

365347
private _is_version_lower_than(
@@ -377,7 +359,7 @@ class WebSocketServer {
377359
if (p1 < p2) return true
378360
if (p1 > p2) return false
379361
}
380-
return false // Versions are equal
362+
return false
381363
}
382364
}
383365

0 commit comments

Comments
 (0)