Skip to content

Commit efe9a70

Browse files
committed
Remove legacy chat initialization system and migrate to single-chat message handling
1 parent 6c1060e commit efe9a70

3 files changed

Lines changed: 29 additions & 139 deletions

File tree

‎packages/browser/src/background/message-handler.ts‎

Lines changed: 28 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
WebSocketMessage,
3-
InitializeChatsMessage,
43
InitializeChatMessage,
54
ApplyChatResponseMessage
65
} from '@shared/types/websocket-message'
@@ -11,9 +10,7 @@ import { GetTabDataResponse } from '@/types/responses'
1110
import { image_url_to_base64 } from '@/utils/image-url-to-base64'
1211

1312
interface ChatQueueItem {
14-
message: InitializeChatsMessage
15-
remaining_chats: number
16-
current_index: number
13+
message: InitializeChatMessage
1714
timeout_id?: number
1815
}
1916

@@ -24,9 +21,7 @@ let is_processing = false
2421
const CHAT_INITIALIZATION_TIMEOUT = 10000
2522

2623
export const handle_messages = (message: WebSocketMessage) => {
27-
if (message.action == 'initialize-chats') {
28-
handle_initialize_chats_message(message as InitializeChatsMessage)
29-
} else if (message.action == 'initialize-chat') {
24+
if (message.action == 'initialize-chat') {
3025
handle_initialize_chat_message(message as InitializeChatMessage)
3126
}
3227
}
@@ -68,65 +63,57 @@ const process_next_chat = async () => {
6863
current_queue_item.timeout_id = undefined
6964
}
7065

71-
if (
72-
current_queue_item.current_index >= current_queue_item.message.chats.length
73-
) {
74-
chat_queue.shift()
75-
76-
if (chat_queue.length > 0) {
77-
chat_queue[0].current_index = 0
78-
await process_next_chat()
79-
} else {
80-
is_processing = false
81-
}
82-
return
83-
}
84-
85-
const current_chat =
86-
current_queue_item.message.chats[current_queue_item.current_index]
66+
const current_chat_message = current_queue_item.message
8767

8868
const batch_id = await generate_alphanumeric_id('chat-init')
8969

9070
await browser.storage.local.set({
9171
[`chat-init:${batch_id}`]: {
92-
text: current_queue_item.message.text,
93-
current_chat: current_chat,
94-
client_id: current_queue_item.message.client_id,
95-
without_submission: current_queue_item.message.without_submission,
96-
raw_instructions: current_queue_item.message.raw_instructions,
97-
mode: current_queue_item.message.mode
72+
text: current_chat_message.text,
73+
current_chat: {
74+
url: current_chat_message.url,
75+
model: current_chat_message.model,
76+
temperature: current_chat_message.temperature,
77+
reasoning_effort: current_chat_message.reasoning_effort,
78+
thinking_budget: current_chat_message.thinking_budget,
79+
top_p: current_chat_message.top_p,
80+
system_instructions: current_chat_message.system_instructions,
81+
options: current_chat_message.options
82+
},
83+
client_id: current_chat_message.client_id,
84+
without_submission: current_chat_message.without_submission,
85+
raw_instructions: current_chat_message.raw_instructions,
86+
mode: current_chat_message.mode
9887
}
9988
})
10089

10190
// OpenRouter is a special case, in model handling via search params
102-
if (current_chat.url == 'https://openrouter.ai/chat') {
91+
if (current_chat_message.url == 'https://openrouter.ai/chat') {
10392
// https://openrouter.ai/chat?models=openrouter/quasar-alpha
10493
const search_params = new URLSearchParams()
105-
if (current_chat.model) {
106-
search_params.set('models', current_chat.model)
94+
if (current_chat_message.model) {
95+
search_params.set('models', current_chat_message.model)
10796
}
10897
const open_router_url = `${
109-
current_chat.url
98+
current_chat_message.url
11099
}?${search_params.toString()}#cwc-${batch_id}`
111100
browser.tabs.create({
112101
url: open_router_url,
113102
active: true
114103
})
115104
} else {
116105
browser.tabs.create({
117-
url: `${current_chat.url}#cwc-${batch_id}`,
106+
url: `${current_chat_message.url}#cwc-${batch_id}`,
118107
active: true
119108
})
120109
}
121110

122-
current_queue_item.current_index++
123-
124111
// Set a timeout to automatically proceed if no confirmation is received
125112
current_queue_item.timeout_id = setTimeout(() => {
126113
console.warn(
127-
`Chat initialization timeout for ${current_chat.url}. Moving to next chat.`
114+
`Chat initialization timeout for ${current_chat_message.url}. Moving to next chat.`
128115
)
129-
current_queue_item.remaining_chats--
116+
chat_queue.shift()
130117
process_next_chat()
131118
}, CHAT_INITIALIZATION_TIMEOUT) as unknown as number
132119
}
@@ -138,57 +125,19 @@ const start_processing = async () => {
138125
}
139126
}
140127

141-
// Will be deprecated
142-
const handle_initialize_chats_message = async (
143-
message: InitializeChatsMessage
144-
) => {
145-
if (message.chats && message.chats.length > 0) {
146-
chat_queue.push({
147-
message,
148-
remaining_chats: message.chats.length,
149-
current_index: 0
150-
})
151-
152-
await start_processing()
153-
}
154-
}
155-
156128
const handle_initialize_chat_message = async (
157129
message: InitializeChatMessage
158130
) => {
159-
const chat_config = {
160-
url: message.url,
161-
model: message.model,
162-
temperature: message.temperature,
163-
reasoning_effort: message.reasoning_effort,
164-
thinking_budget: message.thinking_budget,
165-
top_p: message.top_p,
166-
system_instructions: message.system_instructions,
167-
options: message.options
168-
}
169-
170-
const chats_message: InitializeChatsMessage = {
171-
action: 'initialize-chats',
172-
text: message.text,
173-
chats: [chat_config],
174-
client_id: message.client_id,
175-
without_submission: message.without_submission,
176-
raw_instructions: message.raw_instructions,
177-
edit_format: message.edit_format,
178-
mode: message.mode
179-
}
180-
181-
handle_initialize_chats_message(chats_message)
131+
chat_queue.push({ message })
132+
await start_processing()
182133
}
183134

184135
const handle_chat_initialized = async () => {
185136
if (chat_queue.length > 0) {
186137
if (chat_queue[0].timeout_id) {
187138
clearTimeout(chat_queue[0].timeout_id)
188-
chat_queue[0].timeout_id = undefined
189139
}
190-
191-
chat_queue[0].remaining_chats--
140+
chat_queue.shift()
192141
await process_next_chat()
193142
}
194143
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@ export type Chat = {
99
options?: string[]
1010
}
1111

12-
// Deprecated, used by browser clients older than 1.2.0
13-
export type InitializeChatsMessage = {
14-
action: 'initialize-chats'
15-
text: string
16-
chats: Chat[]
17-
client_id: number // Client ID to identify which editor sent this message
18-
without_submission?: boolean
19-
raw_instructions?: string
20-
edit_format?: string
21-
mode?: any
22-
}
23-
2412
export type InitializeChatMessage = {
2513
action: 'initialize-chat'
2614
text: string
@@ -69,7 +57,6 @@ export type ClientIdAssignmentMessage = {
6957
}
7058

7159
export type WebSocketMessage =
72-
| InitializeChatsMessage
7360
| InitializeChatMessage
7461
| UpdateSavedWebsitesMessage
7562
| BrowserConnectionStatusMessage

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -171,35 +171,7 @@ class WebSocketServer {
171171
this.current_browser_client &&
172172
this.current_browser_client.ws.readyState === WebSocket.OPEN
173173
) {
174-
const browser_version = this.current_browser_client.version
175-
const needs_legacy_format = this._is_version_lower_than(
176-
browser_version,
177-
'1.2.0'
178-
)
179-
180-
if (needs_legacy_format) {
181-
// Convert InitializeChatMessage to InitializeChatsMessage for older clients
182-
const legacy_message = {
183-
action: 'initialize-chats',
184-
text: msg_data.text,
185-
chats: [
186-
{
187-
url: msg_data.url,
188-
model: msg_data.model,
189-
temperature: msg_data.temperature,
190-
top_p: msg_data.top_p,
191-
thinking_budget: msg_data.thinking_budget,
192-
reasoning_effort: msg_data.reasoning_effort,
193-
system_instructions: msg_data.system_instructions,
194-
options: msg_data.options
195-
}
196-
],
197-
client_id: msg_data.client_id
198-
}
199-
this.current_browser_client.ws.send(JSON.stringify(legacy_message))
200-
} else {
201-
this.current_browser_client.ws.send(msg_string)
202-
}
174+
this.current_browser_client.ws.send(msg_string)
203175
}
204176
} else if (msg_data.action == 'update-saved-websites') {
205177
this.saved_websites = msg_data.websites
@@ -335,24 +307,6 @@ class WebSocketServer {
335307
}
336308
return false
337309
}
338-
339-
private _is_version_lower_than(
340-
version: string,
341-
target_version: string
342-
): boolean {
343-
if (version === 'unknown') return true // Assume older version if unknown
344-
345-
const parts1 = version.split('.').map(Number)
346-
const parts2 = target_version.split('.').map(Number)
347-
348-
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
349-
const p1 = parts1[i] || 0
350-
const p2 = parts2[i] || 0
351-
if (p1 < p2) return true
352-
if (p1 > p2) return false
353-
}
354-
return false
355-
}
356310
}
357311

358312
const server = new WebSocketServer()

0 commit comments

Comments
 (0)