@@ -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
340383const server = new WebSocketServer ( )
0 commit comments