Skip to content

Commit c461753

Browse files
committed
Add tab reuse control based on response completion state and track finished responses
1 parent 9daf563 commit c461753

3 files changed

Lines changed: 46 additions & 9 deletions

File tree

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface ChatQueueItem {
1515
const chat_queue: ChatQueueItem[] = []
1616
let is_processing = false
1717
let last_opened_tab_id: number | undefined
18+
let is_finished_responding = false
1819

1920
const 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

161177
export 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

‎apps/browser/src/content-scripts/send-prompt-content-script/utils/add-apply-response-button.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,21 @@ export function observe_for_responses(params: {
6868
footer_selector: string
6969
add_buttons: (footer: Element) => void
7070
}) {
71+
let has_sent_finished_responding = true
72+
7173
const observer = new MutationObserver(() => {
7274
if (params.is_generating()) {
75+
has_sent_finished_responding = false
7376
return
7477
}
7578

79+
if (!has_sent_finished_responding) {
80+
browser.runtime.sendMessage<Message>({
81+
action: 'finished-responding'
82+
})
83+
has_sent_finished_responding = true
84+
}
85+
7686
const all_footers = document.querySelectorAll(params.footer_selector)
7787
if (all_footers.length == 0) {
7888
return

‎apps/browser/src/types/messages.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ type ApplyChatResponseMessage = {
1010
url?: string
1111
}
1212

13-
export type Message = ChatInitializedMessage | ApplyChatResponseMessage
13+
type FinishedRespondingMessage = {
14+
action: 'finished-responding'
15+
}
16+
17+
export type Message =
18+
| ChatInitializedMessage
19+
| ApplyChatResponseMessage
20+
| FinishedRespondingMessage

0 commit comments

Comments
 (0)