Skip to content

Commit 4088590

Browse files
committed
Migrate browser IDs from numbers to persistent UUIDs
- [Pasted text] ├── apps │ ├── browser │ │ ├── src │ │ │ ├── background │ │ │ │ ├── main.ts │ │ │ │ └── websocket.ts │ │ │ └── manifest.json │ │ └── webpack.config.ts │ └── editor │ ├── src │ │ ├── constants │ │ │ └── state-keys.ts │ │ ├── extension.ts │ │ └── services │ │ ├── websocket-manager.ts │ │ └── websocket-server-process.ts │ └── webpack.config.ts └── packages └── shared └── src ├── constants │ └── websocket.ts └── types └── websocket-message.ts
1 parent 61d2ce9 commit 4088590

5 files changed

Lines changed: 28 additions & 33 deletions

File tree

‎apps/browser/src/background/websocket.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,21 @@ export const connect_websocket = async (): Promise<void> => {
7171
const version = manifest.version
7272
const user_agent = navigator.userAgent
7373

74-
const storage = await browser.storage.local.get('browser-profile-name')
74+
const storage = await browser.storage.local.get([
75+
'browser-profile-name',
76+
'browser-id'
77+
])
7578
const profile_name = (
7679
storage['browser-profile-name'] as string | undefined
7780
)?.trim()
7881

79-
let ws_url = `ws://localhost:${DEFAULT_PORT}?client_type=${CLIENT_TYPE.BROWSER}&version=${version}&user_agent=${encodeURIComponent(user_agent)}`
82+
let browser_id = storage['browser-id'] as string | undefined
83+
if (!browser_id) {
84+
browser_id = crypto.randomUUID()
85+
await browser.storage.local.set({ 'browser-id': browser_id })
86+
}
87+
88+
let ws_url = `ws://localhost:${DEFAULT_PORT}?client_type=${CLIENT_TYPE.BROWSER}&version=${version}&user_agent=${encodeURIComponent(user_agent)}&browser_id=${encodeURIComponent(browser_id)}`
8089
if (profile_name) {
8190
ws_url += `&profile_name=${encodeURIComponent(profile_name)}`
8291
}

‎apps/browser/src/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "Autofill for Code Web Chat",
55
"short_name": "CWC",
66
"description": "Autofill prompts in chatbots",
7-
"version": "2026.1019.19",
7+
"version": "2026.1021.20",
88
"homepage_url": "https://github.com/robertpiosik/CodeWebChat",
99
"icons": {
1010
"16": "icons/icon-16.png",

‎apps/editor/src/services/websocket-manager.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export class WebSocketManager {
307307
}
308308
}
309309

310-
private async _select_browser(): Promise<number | undefined> {
310+
private async _select_browser(): Promise<string | undefined> {
311311
if (this.connected_browsers.length == 0) {
312312
return undefined
313313
}
@@ -321,10 +321,9 @@ export class WebSocketManager {
321321
return id
322322
}
323323

324-
const last_selected_browser_id =
325-
this.extension_context.workspaceState.get<number>(
326-
LAST_SELECTED_BROWSER_ID_STATE_KEY
327-
)
324+
const last_selected_browser_id = this.extension_context.workspaceState.get<
325+
string | number
326+
>(LAST_SELECTED_BROWSER_ID_STATE_KEY)
328327

329328
const items = this.connected_browsers
330329
.map((b) => ({
@@ -341,23 +340,23 @@ export class WebSocketManager {
341340
if (a._has_profile && b._has_profile) {
342341
return a.label.localeCompare(b.label)
343342
}
344-
return a.id - b.id
343+
return a.id.localeCompare(b.id)
345344
})
346345
.map((item) => ({
347346
label: item.label,
348347
description: item.description,
349348
id: item.id
350349
}))
351350

352-
return new Promise<number | undefined>((resolve) => {
351+
return new Promise<string | undefined>((resolve) => {
353352
const quick_pick = vscode.window.createQuickPick<
354-
vscode.QuickPickItem & { id: number }
353+
vscode.QuickPickItem & { id: string }
355354
>()
356355
quick_pick.items = items
357356

358-
if (last_selected_browser_id) {
357+
if (last_selected_browser_id !== undefined) {
359358
const active_item = items.find(
360-
(item) => item.id == last_selected_browser_id
359+
(item) => item.id === last_selected_browser_id
361360
)
362361
if (active_item) {
363362
quick_pick.activeItems = [active_item]

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dotenv.config({ path: path.join(__dirname, '..', '.env') })
1212
interface BrowserClient {
1313
ws: WebSocket
1414
version: string
15-
id: number
15+
id: string
1616
user_agent: string
1717
profile_name?: string
1818
is_alive: boolean
@@ -26,8 +26,7 @@ interface VSCodeClient {
2626

2727
class WebSocketServer {
2828
private vscode_clients: Map<string, VSCodeClient> = new Map()
29-
private browser_clients: Map<number, BrowserClient> = new Map()
30-
private browser_client_counter: number = 0
29+
private browser_clients: Map<string, BrowserClient> = new Map()
3130
private connections: Set<WebSocket> = new Set()
3231
private server: http.Server
3332
private wss: any
@@ -117,25 +116,15 @@ class WebSocketServer {
117116

118117
private _create_http_server(): http.Server {
119118
return http.createServer((req: any, res: any) => {
120-
res.setHeader('Access-Control-Allow-Origin', '*')
121-
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS')
122-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
123-
124-
if (req.method == 'OPTIONS') {
125-
res.writeHead(204)
126-
res.end()
127-
return
128-
}
129-
130119
const url = new URL(req.url || '', `http://localhost:${DEFAULT_PORT}`)
131120
if (url.pathname == '/health') {
132121
const challenge = url.searchParams.get('challenge')
133122
const response: any = { status: 'ok' }
134123

135124
if (challenge) {
136-
// eslint-disable-next-line @typescript-eslint/no-var-requires
137125
const {
138126
get_or_create_shared_token
127+
// eslint-disable-next-line @typescript-eslint/no-var-requires
139128
} = require('../utils/shared-token')
140129
const token = get_or_create_shared_token()
141130
const signature = crypto
@@ -192,9 +181,7 @@ class WebSocketServer {
192181
const version = url.searchParams.get('version') || 'unknown'
193182
const user_agent = url.searchParams.get('user_agent') || 'unknown'
194183
const profile_name = url.searchParams.get('profile_name') || undefined
195-
196-
this.browser_client_counter++
197-
const id = this.browser_client_counter
184+
const id = url.searchParams.get('browser_id') || crypto.randomUUID()
198185

199186
const client: BrowserClient = {
200187
ws,

‎packages/shared/src/types/websocket-message.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type Chat = {
77
}
88

99
export type ConnectedBrowser = {
10-
id: number
10+
id: string
1111
version: string
1212
user_agent: string
1313
profile_name?: string
@@ -17,9 +17,9 @@ export type InitializeChatMessage = {
1717
action: 'initialize-chat'
1818
text: string
1919
url: string
20-
client_id: string
20+
client_id: string
2121
model?: string
22-
target_browser_id?: number
22+
target_browser_id?: string
2323
reasoning_effort?: string
2424
system_instructions?: string
2525
options?: string[]

0 commit comments

Comments
 (0)