Skip to content

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

fix(selection): fit all card handoffs to context

Emre Sokullu0eb3655d99da
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 1d402d0d0..06db1830a 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1870,9 +1870,7 @@ async function queueSelectionShortcutPromptAfterProviders(msg, tab, sendResponse
msg.selectionText,
msg.priorAction,
msg.priorAnswer,
- // With the page as context the run manages its own context, so only a selection-only
- // follow-up is sized to the window.
- includePageContext ? {} : { contextWindow, question: msg.question, language: msg.language },
+ { contextWindow, question: msg.question, language: msg.language },
)
: msg.selectionText;
// A cut-off card answer resumes where it stopped instead of running the action again,
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index 299efba2f..0cc23bac5 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -499,8 +499,10 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
// the end, so only the end is sent, trimmed to what the window leaves. A window that cannot
// even take the selection keeps the whole text and is refused, with a reason, when it runs.
const room = priorAnswerRoom(contextWindow, wrapSelectedPageText(head, trusted, SELECTION_ONLY_SOURCE_GROUNDING));
- if (room > 0) {
- const tail = sliceWithinTokens(body, room, true);
+ if (room !== null && room >= 0) {
+ // A zero room comes from the conservative reply reserve. Keep the last
+ // token so the side panel can still resume a selection that itself fits.
+ const tail = sliceWithinTokens(body, Math.max(room, 1), true);
if (tail.length < body.length) {
body = tail;
trimmed = true;
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 768f1dbe8..0a80b9c37 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1622,9 +1622,7 @@ async function queueFirefoxSelectionShortcutPromptAfterProviders(msg, tab, sendR
msg.selectionText,
msg.priorAction,
msg.priorAnswer,
- // With the page as context the run manages its own context, so only a selection-only
- // follow-up is sized to the window.
- includePageContext ? {} : { contextWindow, question: msg.question, language: msg.language },
+ { contextWindow, question: msg.question, language: msg.language },
)
: msg.selectionText;
// A cut-off card answer resumes where it stopped instead of running the action again,
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index cfb874daf..a090c08d2 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -499,8 +499,10 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
// the end, so only the end is sent, trimmed to what the window leaves. A window that cannot
// even take the selection keeps the whole text and is refused, with a reason, when it runs.
const room = priorAnswerRoom(contextWindow, wrapSelectedPageText(head, trusted, SELECTION_ONLY_SOURCE_GROUNDING));
- if (room > 0) {
- const tail = sliceWithinTokens(body, room, true);
+ if (room !== null && room >= 0) {
+ // A zero room comes from the conservative reply reserve. Keep the last
+ // token so the side panel can still resume a selection that itself fits.
+ const tail = sliceWithinTokens(body, Math.max(room, 1), true);
if (tail.length < body.length) {
body = tail;
trimmed = true;
diff --git a/test/run.js b/test/run.js
index 0ba55977e..91659f672 100644
--- a/test/run.js
+++ b/test/run.js
@@ -52524,7 +52524,7 @@ test('inline card answers are one tool-less, bounded model call behind validated
assert.match(content, /function submitFollowUp\(\) \{\s*const value = cardParts\?\.followUp\.value\.trim\(\);\s*if \(!value \|\| cardState\?\.status !== 'ready'\) return;[\s\S]*?submitSelection\('custom', value, '', \{\s*priorAction: cardState\.kind,\s*priorAnswer: cardState\.text,/, `${prefix}: a follow-up is only sent once the card has an answer to continue from`);
assert.match(content, /cardParts\.send\.disabled = !\(cardParts\.followUp\.value\.trim\(\) && cardState\?\.status === 'ready'\);/, `${prefix}: the send button waits for an answer`);
assert.match(content, /cardParts\.followUp\.addEventListener\('input', syncFollowUpSend\);[\s\S]*?syncFollowUpSend\(\);\s*positionCard\(\);/, `${prefix}: the send button follows the card's state`);
- assert.match(background, /const contextWindow = providerManager\.providers\.size > 0 \? providerManager\.getActive\(\)\?\.contextWindow : undefined;\s*const selectionText = selectionAction === 'custom'\s*\? buildFollowUpSelectionText\(\s*msg\.selectionText,\s*msg\.priorAction,\s*msg\.priorAnswer,\s*(?:\/\/[^\n]*\n\s*)*includePageContext \? \{\} : \{ contextWindow, question: msg\.question, language: msg\.language \},?\s*\)\s*: msg\.selectionText;/, `${prefix}: the background adds the earlier answer inside the selection, for custom questions only`);
+ assert.match(background, /const contextWindow = providerManager\.providers\.size > 0 \? providerManager\.getActive\(\)\?\.contextWindow : undefined;\s*const selectionText = selectionAction === 'custom'\s*\? buildFollowUpSelectionText\(\s*msg\.selectionText,\s*msg\.priorAction,\s*msg\.priorAnswer,\s*\{ contextWindow, question: msg\.question, language: msg\.language \},?\s*\)\s*: msg\.selectionText;/, `${prefix}: the background trims each custom follow-up to the provider window`);
assert.match(background, /if \(msg\?\.type !== 'WB_SELECTION_SHORTCUT_INLINE'\) return;\s*answerInlineSelection\(msg\)\.then\(sendResponse\);\s*return true;/, `${prefix}: the inline listener should answer asynchronously`);
const start = background.indexOf('async function answerInlineSelection(msg) {');