Skip to content

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

fix(selection): hydrate providers before handoff

Emre Sokullue695200ca595
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 7d0767f19..1d402d0d0 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1853,7 +1853,7 @@ chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
// Selection-shortcut clicks originate in a content script. Keep this listener
// synchronous until sidePanel.open() so Chrome preserves the originating user
// gesture; prompt recovery storage can finish afterward.
-function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = false } = {}) {
+async function queueSelectionShortcutPromptAfterProviders(msg, tab, sendResponse, { panelOpened = false } = {}) {
const selectionAction = normalizeSelectionAction(msg.action);
const includePageContext = selectionAction === 'custom' && msg.includePageContext === true;
const sourceGrounding = includePageContext
@@ -1862,8 +1862,8 @@ function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = fa
? SELECTION_CONTEXT_SOURCE_GROUNDING
: SELECTION_ONLY_SOURCE_GROUNDING;
// A follow-up typed in a page card carries that card's answer as context.
- // The model's context window is known only once the providers have loaded; until then the
- // run's own check decides.
+ // The provider metadata was loaded before this prompt was built, so its context
+ // window can trim the handoff before the side-panel run starts.
const contextWindow = providerManager.providers.size > 0 ? providerManager.getActive()?.contextWindow : undefined;
const selectionText = selectionAction === 'custom'
? buildFollowUpSelectionText(
@@ -1925,6 +1925,23 @@ function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = fa
return true;
}
+function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = false } = {}) {
+ if (!tab?.id) {
+ sendResponse({ ok: false, queued: false, requiresManualOpen: false, error: 'Invalid selection shortcut request.' });
+ return false;
+ }
+ // This must stay in the message handler's synchronous turn, but prompt construction
+ // waits below for provider context-window metadata so handoffs are trimmed correctly.
+ openSidePanelForContextMenu(tab, { open: !panelOpened });
+ (async () => {
+ if (providerManager.providers.size === 0) await providerManager.load();
+ return queueSelectionShortcutPromptAfterProviders(msg, tab, sendResponse, { panelOpened: true });
+ })().catch((error) => {
+ sendResponse({ ok: false, queued: false, requiresManualOpen: false, error: error?.message || String(error) });
+ });
+ return true;
+}
+
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg?.type !== 'WB_SELECTION_SHORTCUT_SUBMIT') return;
return queueSelectionShortcutPrompt(msg, sender?.tab, sendResponse);
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 3645206ad..768f1dbe8 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1605,7 +1605,7 @@ browser.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
// sidebarAction.open() gesture. Persist and notify the existing sidebar when
// it is open; otherwise startup recovery will consume the prompt after the
// user opens WebBrain manually.
-function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {
+async function queueFirefoxSelectionShortcutPromptAfterProviders(msg, tab, sendResponse) {
const selectionAction = normalizeSelectionAction(msg.action);
const includePageContext = selectionAction === 'custom' && msg.includePageContext === true;
const sourceGrounding = includePageContext
@@ -1614,8 +1614,8 @@ function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {
? SELECTION_CONTEXT_SOURCE_GROUNDING
: SELECTION_ONLY_SOURCE_GROUNDING;
// A follow-up typed in a page card carries that card's answer as context.
- // The model's context window is known only once the providers have loaded; until then the
- // run's own check decides.
+ // The provider metadata was loaded before this prompt was built, so its context
+ // window can trim the handoff before the side-panel run starts.
const contextWindow = providerManager.providers.size > 0 ? providerManager.getActive()?.contextWindow : undefined;
const selectionText = selectionAction === 'custom'
? buildFollowUpSelectionText(
@@ -1676,6 +1676,20 @@ function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {
return true;
}
+function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {
+ if (!tab?.id) {
+ sendResponse({ ok: false, queued: false, requiresManualOpen: true, error: 'Invalid selection shortcut request.' });
+ return false;
+ }
+ (async () => {
+ if (providerManager.providers.size === 0) await providerManager.load();
+ return queueFirefoxSelectionShortcutPromptAfterProviders(msg, tab, sendResponse);
+ })().catch((error) => {
+ sendResponse({ ok: false, queued: false, requiresManualOpen: true, error: error?.message || String(error) });
+ });
+ return true;
+}
+
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg?.type !== 'WB_SELECTION_SHORTCUT_SUBMIT') return;
return queueFirefoxSelectionShortcutPrompt(msg, sender?.tab, sendResponse);