@@ -15,6 +15,7 @@ interface ChatQueueItem {
1515const chat_queue : ChatQueueItem [ ] = [ ]
1616let is_processing = false
1717let last_opened_tab_id : number | undefined
18+ let is_finished_responding = false
1819
1920const CHAT_INITIALIZATION_TIMEOUT = 5000
2021
@@ -102,15 +103,28 @@ const process_next_chat = async () => {
102103 target_url = `${ current_chat_message . url } #cwc-${ batch_id } `
103104 }
104105
106+ const expects_apply_response_button =
107+ current_chat_message . prompt_type == 'edit-context' ||
108+ current_chat_message . prompt_type == 'code-at-cursor' ||
109+ current_chat_message . prompt_type == 'find-relevant-files'
110+
105111 let tab_reused = false
106- if ( last_opened_tab_id !== undefined && current_chat_message . reuse_last_tab ) {
112+ if (
113+ last_opened_tab_id !== undefined &&
114+ current_chat_message . reuse_last_tab &&
115+ ( ! expects_apply_response_button || is_finished_responding )
116+ ) {
107117 try {
108- await browser . tabs . get ( last_opened_tab_id )
109- await browser . tabs . update ( last_opened_tab_id , {
110- url : target_url ,
111- active : true
112- } )
113- tab_reused = true
118+ const tab = await browser . tabs . get ( last_opened_tab_id )
119+ if ( tab . active ) {
120+ await browser . tabs . update ( last_opened_tab_id , {
121+ url : target_url ,
122+ active : true
123+ } )
124+ tab_reused = true
125+ } else {
126+ last_opened_tab_id = undefined
127+ }
114128 } catch {
115129 last_opened_tab_id = undefined
116130 }
@@ -125,6 +139,8 @@ const process_next_chat = async () => {
125139 last_opened_tab_id = new_tab . id
126140 }
127141
142+ is_finished_responding = false
143+
128144 current_queue_item . timeout_id = setTimeout ( ( ) => {
129145 console . warn (
130146 `Chat initialization timeout for ${ current_chat_message . url } . Moving to next chat.`
@@ -160,7 +176,7 @@ const handle_chat_initialized = async () => {
160176
161177export const setup_message_listeners = ( ) => {
162178 browser . runtime . onMessage . addListener (
163- ( message : any , _ : any , __ : any ) : any => {
179+ ( message : any , sender : any , _ : any ) : any => {
164180 if ( is_message ( message ) ) {
165181 if ( message . action == 'chat-initialized' ) {
166182 handle_chat_initialized ( )
@@ -172,6 +188,10 @@ export const setup_message_listeners = () => {
172188 edit_format : message . edit_format ,
173189 url : message . url
174190 } as ApplyChatResponseMessage )
191+ } else if ( message . action == 'finished-responding' ) {
192+ if ( sender . tab ?. id && sender . tab . id == last_opened_tab_id ) {
193+ is_finished_responding = true
194+ }
175195 }
176196 }
177197 return false
0 commit comments