11import {
22 WebSocketMessage ,
3- InitializeChatsMessage ,
43 InitializeChatMessage ,
54 ApplyChatResponseMessage
65} from '@shared/types/websocket-message'
@@ -11,9 +10,7 @@ import { GetTabDataResponse } from '@/types/responses'
1110import { image_url_to_base64 } from '@/utils/image-url-to-base64'
1211
1312interface 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
2421const CHAT_INITIALIZATION_TIMEOUT = 10000
2522
2623export 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-
156128const 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
184135const 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}
0 commit comments