Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Fix /btw prompt forwarding and handoff recovery

Barack Sokullubee457f73668
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index 2c1c0f73a..765fcaf4c 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -4812,7 +4812,7 @@ async function init() {
const tabId = state?.tabId;
if (tabId != null && tabId === currentTabId) {
try {
- const html = await loadTabChat(tabId);
+ const html = await loadTabChat(tabId, { waitForHandoff: true });
if (html && html !== TAB_CHAT_LOAD_FAILED) {
messagesEl.innerHTML = html;
rebindRestoredMessageControls();
@@ -14696,6 +14696,12 @@ async function openBtwWindow(tabId, prompt = '') {
const win = await chrome.windows.get(existing.windowId);
if (win) {
await chrome.windows.update(existing.windowId, { focused: true });
+ if (prompt && Number(existing.tabId) === Number(tabId)) {
+ const target = win.tabs?.[0];
+ if (target?.id != null) {
+ await chrome.tabs.sendMessage(target.id, { action: 'btw_prompt', prompt }).catch(() => {});
+ }
+ }
return;
}
} catch {
@@ -14714,6 +14720,15 @@ async function openBtwWindow(tabId, prompt = '') {
await chrome.storage.session.set({ [BTW_WINDOW_KEY]: { windowId: win.id, tabId } }).catch(() => {});
}
+chrome.runtime.onMessage.addListener((msg) => {
+ if (!isBtwWindow || msg?.action !== 'btw_prompt') return;
+ const prompt = String(msg?.prompt || '');
+ if (!prompt || !inputEl) return;
+ inputEl.value = prompt;
+ inputEl.dispatchEvent(new Event('input', { bubbles: true }));
+ void sendMessage().catch(() => {});
+});
+
if (expandBtn) {
if (isStandaloneWindow) {
expandBtn.style.display = 'none';
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index fa86fd620..ab80a9b6f 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -4667,7 +4667,7 @@ async function init() {
const tabId = state?.tabId;
if (tabId != null && tabId === currentTabId) {
try {
- const html = await loadTabChat(tabId);
+ const html = await loadTabChat(tabId, { waitForHandoff: true });
if (html && html !== TAB_CHAT_LOAD_FAILED) {
messagesEl.innerHTML = html;
rebindRestoredMessageControls();
@@ -14194,6 +14194,12 @@ async function openBtwWindow(tabId, prompt = '') {
const win = await browser.windows.get(existing.windowId);
if (win) {
await browser.windows.update(win.id, { focused: true });
+ if (prompt && Number(existing.tabId) === Number(tabId)) {
+ const target = win.tabs?.[0];
+ if (target?.id != null) {
+ await browser.tabs.sendMessage(target.id, { action: 'btw_prompt', prompt }).catch(() => {});
+ }
+ }
return;
}
} catch {}
@@ -14209,6 +14215,15 @@ async function openBtwWindow(tabId, prompt = '') {
await browser.storage.session.set({ [BTW_WINDOW_KEY]: { windowId: win.id, tabId } });
}
+browser.runtime.onMessage.addListener((msg) => {
+ if (!isBtwWindow || msg?.action !== 'btw_prompt') return;
+ const prompt = String(msg?.prompt || '');
+ if (!prompt || !inputEl) return;
+ inputEl.value = prompt;
+ inputEl.dispatchEvent(new Event('input', { bubbles: true }));
+ void sendMessage().catch(() => {});
+});
+
if (expandBtn) {
if (isStandaloneWindow) {
expandBtn.style.display = 'none';
diff --git a/test/run.js b/test/run.js
index 8393b6b6d..73354a633 100644
--- a/test/run.js
+++ b/test/run.js
@@ -47793,6 +47793,9 @@ test('chrome sidepanel serializes tab-chat storage writes with clears and reads'
assert.match(loadBody, /return await enqueueTabChatOperation\(numericTabId, async \(queuedTabId\) => \{[\s\S]*?sendToBackground\('load_tab_chat', \{[\s\S]*?waitForHandoff,[\s\S]*?\}\);/, 'chrome: tab-chat restore should read through the shared background queue');
assert.match(loadBody, /catch \(e\) \{\s*if \(waitForHandoff\) return TAB_CHAT_LOAD_FAILED;\s*\}[\s\S]*?return null;/, 'chrome: coordinated load failures should remain distinct from successful empty restores');
assert.match(panel, /const html = await loadTabChat\(tabId, \{ waitForHandoff: true \}\);\s*if \(html === TAB_CHAT_LOAD_FAILED\) return false;[\s\S]*?messagesEl\.innerHTML = '';/, 'chrome: a failed visibility handoff must preserve the current transcript DOM');
+ assert.match(panel, /When the btw window closes[\s\S]*?loadTabChat\(tabId, \{ waitForHandoff: true \}\);/, 'chrome: closing a /btw window must reclaim the tab-chat handoff before reloading the main panel');
+ assert.match(panel, /async function openBtwWindow\(tabId, prompt = ''\) \{[\s\S]*?chrome\.tabs\.sendMessage\(target\.id, \{ action: 'btw_prompt', prompt \}\)\.catch\(\(\) => \{\}\);/, 'chrome: /btw should forward a prompt to an existing /btw window for the same tab');
+ assert.match(panel, /chrome\.runtime\.onMessage\.addListener\(\(msg\) => \{\s*if \(!isBtwWindow \|\| msg\?\.action !== 'btw_prompt'\) return;/, 'chrome: a /btw window should auto-send forwarded prompts');
assert.match(panel, /const payload = \{[\s\S]*?handoffOwnerId: tabChatHandoffOwnerId,[\s\S]*?handoffGeneration[\s\S]*?return enqueueTabChatOperation\(tabId, async \(numericTabId\) => \{[\s\S]*?sendToBackground\('persist_tab_chat', payload\);/, 'chrome: visible tab-chat persistence should carry its owner generation through the shared background queue');
assert.match(panel, /document\.visibilityState === 'hidden' && allowHidden[\s\S]*?sendToBackground\('persist_tab_chat', payload\);/, 'chrome: hidden handoff must bypass the document-local queue and enter the shared queue immediately');
const clearStart = panel.indexOf('function clearCachedTabChat(tabId) {');
@@ -47819,6 +47822,9 @@ test('firefox sidepanel serializes tab-chat storage writes with clears and reads
assert.match(loadBody, /return await enqueueTabChatOperation\(numericTabId, async \(queuedTabId\) => \{[\s\S]*?sendToBackground\('load_tab_chat', \{[\s\S]*?waitForHandoff,[\s\S]*?\}\);/, 'firefox: tab-chat restore should read through the shared background queue');
assert.match(loadBody, /catch \(e\) \{\s*if \(waitForHandoff\) return TAB_CHAT_LOAD_FAILED;\s*\}[\s\S]*?return null;/, 'firefox: coordinated load failures should remain distinct from successful empty restores');
assert.match(panel, /const html = await loadTabChat\(tabId, \{ waitForHandoff: true \}\);\s*if \(html === TAB_CHAT_LOAD_FAILED\) return false;[\s\S]*?messagesEl\.innerHTML = '';/, 'firefox: a failed visibility handoff must preserve the current transcript DOM');
+ assert.match(panel, /When the btw window closes[\s\S]*?loadTabChat\(tabId, \{ waitForHandoff: true \}\);/, 'firefox: closing a /btw window must reclaim the tab-chat handoff before reloading the main panel');
+ assert.match(panel, /async function openBtwWindow\(tabId, prompt = ''\) \{[\s\S]*?browser\.tabs\.sendMessage\(target\.id, \{ action: 'btw_prompt', prompt \}\)\.catch\(\(\) => \{\}\);/, 'firefox: /btw should forward a prompt to an existing /btw window for the same tab');
+ assert.match(panel, /browser\.runtime\.onMessage\.addListener\(\(msg\) => \{\s*if \(!isBtwWindow \|\| msg\?\.action !== 'btw_prompt'\) return;/, 'firefox: a /btw window should auto-send forwarded prompts');
assert.match(panel, /const payload = \{[\s\S]*?handoffOwnerId: tabChatHandoffOwnerId,[\s\S]*?handoffGeneration[\s\S]*?return enqueueTabChatOperation\(tabId, async \(numericTabId\) => \{[\s\S]*?sendToBackground\('persist_tab_chat', payload\);/, 'firefox: visible tab-chat persistence should carry its owner generation through the shared background queue');
assert.match(panel, /document\.visibilityState === 'hidden' && allowHidden[\s\S]*?sendToBackground\('persist_tab_chat', payload\);/, 'firefox: hidden handoff must bypass the document-local queue and enter the shared queue immediately');
const clearStart = panel.indexOf('function clearCachedTabChat(tabId) {');