Skip to content

Commit b467343

Browse files
committed
Introduce a new initialize-chat WebSocket message type for single chat initialization, ensuring backward compatibility with older browser extension versions
1 parent d14b6d2 commit b467343

7 files changed

Lines changed: 111 additions & 34 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
WebSocketMessage,
33
InitializeChatsMessage,
4+
InitializeChatMessage,
45
ApplyChatResponseMessage
56
} from '@shared/types/websocket-message'
67
import browser from 'webextension-polyfill'
@@ -9,7 +10,6 @@ import { is_message } from '@/utils/is-message'
910
import { GetTabDataResponse } from '@/types/responses'
1011
import { image_url_to_base64 } from '@/utils/image-url-to-base64'
1112

12-
// Queue to manage multiple chat initialization
1313
interface ChatQueueItem {
1414
message: InitializeChatsMessage
1515
remaining_chats: number
@@ -26,6 +26,8 @@ const CHAT_INITIALIZATION_TIMEOUT = 5000
2626
export const handle_messages = (message: WebSocketMessage) => {
2727
if (message.action == 'initialize-chats') {
2828
handle_initialize_chats_message(message as InitializeChatsMessage)
29+
} else if (message.action == 'initialize-chat') {
30+
handle_initialize_chat_message(message as InitializeChatMessage)
2931
}
3032
}
3133

@@ -140,6 +142,7 @@ const start_processing = async () => {
140142
}
141143
}
142144

145+
// Will be deprecated
143146
const handle_initialize_chats_message = async (
144147
message: InitializeChatsMessage
145148
) => {
@@ -156,6 +159,28 @@ const handle_initialize_chats_message = async (
156159
}
157160
}
158161

162+
const handle_initialize_chat_message = async (
163+
message: InitializeChatMessage
164+
) => {
165+
const chat_config = {
166+
url: message.url,
167+
model: message.model,
168+
temperature: message.temperature,
169+
top_p: message.top_p,
170+
system_instructions: message.system_instructions,
171+
options: message.options
172+
}
173+
174+
const chats_message: InitializeChatsMessage = {
175+
action: 'initialize-chats',
176+
text: message.text,
177+
chats: [chat_config],
178+
client_id: message.client_id
179+
}
180+
181+
handle_initialize_chats_message(chats_message)
182+
}
183+
159184
const handle_chat_initialized = async () => {
160185
// Process the next chat in the queue if one exists
161186
if (chat_queue.length > 0) {

packages/browser/src/content-scripts/send-prompt-content-script/chatbots/huggingchat.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
import { Message } from '@/types/messages'
1010

1111
export const huggingchat: Chatbot = {
12+
wait_until_ready: async () => {
13+
await new Promise((resolve) => setTimeout(resolve, 500))
14+
},
1215
inject_apply_response_button: (client_id: number) => {
1316
const debounced_add_buttons = debounce((params: { footer: Element }) => {
1417
const apply_response_button_text = 'Apply response with CWC'

packages/browser/src/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"author": "Robert Piosik",
44
"name": "Code Web Chat Connector",
55
"short_name": "CWC",
6-
"description": "Initialize any web chat with your code",
7-
"version": "1.1.0",
6+
"description": "Initialize chatbots and apply responses.",
7+
"version": "1.2.0",
88
"homepage_url": "https://github.com/robertpiosik/CodeWebChat",
99
"icons": {
1010
"16": "icons/icon-16.png",

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@ export type Chat = {
77
options?: string[]
88
}
99

10+
// Deprecated, used by browser clients older than 1.2.0
1011
export type InitializeChatsMessage = {
1112
action: 'initialize-chats'
1213
text: string
1314
chats: Chat[]
1415
client_id: number // Client ID to identify which editor sent this message
1516
}
1617

18+
export type InitializeChatMessage = {
19+
action: 'initialize-chat'
20+
text: string
21+
url: string
22+
client_id: number // Client ID to identify which editor sent this message
23+
model?: string
24+
temperature?: number
25+
top_p?: number
26+
system_instructions?: string
27+
options?: string[]
28+
}
29+
1730
export type Website = {
1831
url: string
1932
title: string
@@ -43,6 +56,7 @@ export type ClientIdAssignmentMessage = {
4356

4457
export type WebSocketMessage =
4558
| InitializeChatsMessage
59+
| InitializeChatMessage
4660
| UpdateSavedWebsitesMessage
4761
| BrowserConnectionStatusMessage
4862
| ClientIdAssignmentMessage

packages/vscode/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "gemini-coder",
33
"displayName": "Code Web Chat (CWC)",
4-
"description": "Initialize any web chat with your code",
5-
"version": "1.53.0",
4+
"description": "Initialize chatbots and apply responses",
5+
"version": "1.54.0",
66
"scripts": {
77
"build": "npx vsce package --no-dependencies",
88
"vscode:prepublish": "npm run compile",

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as child_process from 'child_process'
44
import * as path from 'path'
55
import * as net from 'net'
66
import {
7-
InitializeChatsMessage,
7+
InitializeChatMessage,
88
UpdateSavedWebsitesMessage
99
} from '@shared/types/websocket-message'
1010
import { CHATBOTS } from '@shared/constants/chatbots'
@@ -296,19 +296,15 @@ export class WebSocketManager {
296296
url = chatbot.url
297297
}
298298

299-
const message: InitializeChatsMessage = {
300-
action: 'initialize-chats',
299+
const message: InitializeChatMessage = {
300+
action: 'initialize-chat',
301301
text: chat.text,
302-
chats: [
303-
{
304-
url,
305-
model: preset.model,
306-
temperature: preset.temperature,
307-
top_p: preset.top_p,
308-
system_instructions: preset.systemInstructions,
309-
options: preset.options
310-
}
311-
],
302+
url,
303+
model: preset.model,
304+
temperature: preset.temperature,
305+
top_p: preset.top_p,
306+
system_instructions: preset.systemInstructions,
307+
options: preset.options,
312308
client_id: this.client_id || 0 // 0 is a temporary fallback and should be removed few weeks from 28.03.25
313309
}
314310

@@ -342,19 +338,15 @@ export class WebSocketManager {
342338
url = chatbot.url
343339
}
344340

345-
const message: InitializeChatsMessage = {
346-
action: 'initialize-chats',
341+
const message: InitializeChatMessage = {
342+
action: 'initialize-chat',
347343
text: instruction,
348-
chats: [
349-
{
350-
url,
351-
model: preset.model,
352-
temperature: preset.temperature,
353-
top_p: preset.top_p,
354-
system_instructions: preset.system_instructions,
355-
options: preset.options
356-
}
357-
],
344+
url,
345+
model: preset.model,
346+
temperature: preset.temperature,
347+
top_p: preset.top_p,
348+
system_instructions: preset.system_instructions,
349+
options: preset.options,
358350
client_id: this.client_id || 0 // 0 is a temporary fallback and should be removed few weeks from 28.03.25
359351
}
360352

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

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,39 @@ class WebSocketServer {
178178
const msg_string = message.toString()
179179
const msg_data = JSON.parse(msg_string)
180180

181-
// Handle different message types
182-
if (msg_data.action == 'initialize-chats') {
181+
if (msg_data.action == 'initialize-chat') {
183182
if (
184183
this.current_browser_client &&
185184
this.current_browser_client.ws.readyState === WebSocket.OPEN
186185
) {
187-
// Forward the message with client ID to browser client
188-
this.current_browser_client.ws.send(msg_string)
186+
const browser_version = this.current_browser_client.version
187+
const needs_legacy_format = this._is_version_lower_than(
188+
browser_version,
189+
'1.2.0'
190+
)
191+
192+
if (needs_legacy_format) {
193+
// Convert InitializeChatMessage to InitializeChatsMessage for older clients
194+
const legacy_message = {
195+
action: 'initialize-chats',
196+
text: msg_data.text,
197+
chats: [
198+
{
199+
url: msg_data.url,
200+
model: msg_data.model,
201+
temperature: msg_data.temperature,
202+
top_p: msg_data.top_p,
203+
system_instructions: msg_data.system_instructions,
204+
options: msg_data.options
205+
}
206+
],
207+
client_id: msg_data.client_id
208+
}
209+
this.current_browser_client.ws.send(JSON.stringify(legacy_message))
210+
} else {
211+
// Forward the message as-is for newer browser clients
212+
this.current_browser_client.ws.send(msg_string)
213+
}
189214
}
190215
} else if (msg_data.action == 'update-saved-websites') {
191216
// Store the updated websites
@@ -335,6 +360,24 @@ class WebSocketServer {
335360
}
336361
return false // Versions are equal or v1 is not newer
337362
}
363+
364+
private _is_version_lower_than(
365+
version: string,
366+
target_version: string
367+
): boolean {
368+
if (version === 'unknown') return true // Assume older version if unknown
369+
370+
const parts1 = version.split('.').map(Number)
371+
const parts2 = target_version.split('.').map(Number)
372+
373+
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
374+
const p1 = parts1[i] || 0
375+
const p2 = parts2[i] || 0
376+
if (p1 < p2) return true
377+
if (p1 > p2) return false
378+
}
379+
return false // Versions are equal
380+
}
338381
}
339382

340383
const server = new WebSocketServer()

0 commit comments

Comments
 (0)