Skip to content

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

fix(selection): preserve handoff whitespace

Emre Sokulluc422e1b02fc3
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index d2517c0a1..6d12f2948 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -408,8 +408,8 @@ export function capInlineAnswer(text) {
export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer, { contextWindow, question = '', language = '' } = {}) {
const selection = String(selectionText || '');
const label = INLINE_FOLLOW_UP_LABELS[String(priorAction || '').trim()];
- const full = String(priorAnswer || '').trim();
- if (!label || !full) return selection;
+ const full = String(priorAnswer || '');
+ if (!label || !full.trim()) return selection;
// A longer answer than a card can produce is shortened, and says so.
let answer = full.length > INLINE_FOLLOW_UP_ANSWER_MAX_CHARS
? `${full.slice(0, INLINE_FOLLOW_UP_ANSWER_MAX_CHARS)}\n[The answer continues beyond this point.]`
@@ -484,8 +484,8 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
const actionId = String(action || '').trim();
const label = INLINE_FOLLOW_UP_LABELS[actionId];
const selection = String(selectionText || '').trim();
- const partial = String(partialAnswer || '').trim();
- if (!label || !selection || !partial) return '';
+ const partial = String(partialAnswer || '');
+ if (!label || !selection || !partial.trim()) return '';
const instruction = Object.prototype.hasOwnProperty.call(INLINE_CONTINUATION_INSTRUCTIONS, actionId)
? `${INLINE_CONTINUATION_INSTRUCTIONS[actionId]}${responseLanguageInstruction(language)}`
: selectionActionInstruction(actionId, '', language);
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index 4d7e50da4..1d8200d78 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -408,8 +408,8 @@ export function capInlineAnswer(text) {
export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer, { contextWindow, question = '', language = '' } = {}) {
const selection = String(selectionText || '');
const label = INLINE_FOLLOW_UP_LABELS[String(priorAction || '').trim()];
- const full = String(priorAnswer || '').trim();
- if (!label || !full) return selection;
+ const full = String(priorAnswer || '');
+ if (!label || !full.trim()) return selection;
// A longer answer than a card can produce is shortened, and says so.
let answer = full.length > INLINE_FOLLOW_UP_ANSWER_MAX_CHARS
? `${full.slice(0, INLINE_FOLLOW_UP_ANSWER_MAX_CHARS)}\n[The answer continues beyond this point.]`
@@ -484,8 +484,8 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
const actionId = String(action || '').trim();
const label = INLINE_FOLLOW_UP_LABELS[actionId];
const selection = String(selectionText || '').trim();
- const partial = String(partialAnswer || '').trim();
- if (!label || !selection || !partial) return '';
+ const partial = String(partialAnswer || '');
+ if (!label || !selection || !partial.trim()) return '';
const instruction = Object.prototype.hasOwnProperty.call(INLINE_CONTINUATION_INSTRUCTIONS, actionId)
? `${INLINE_CONTINUATION_INSTRUCTIONS[actionId]}${responseLanguageInstruction(language)}`
: selectionActionInstruction(actionId, '', language);
diff --git a/test/run.js b/test/run.js
index 91659f672..ea4c67df4 100644
--- a/test/run.js
+++ b/test/run.js
@@ -52340,6 +52340,8 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Co
const short = buildContinuation(selection, 'explain', 'A short unfinished answer.', 'en', { contextWindow: 8192 });
assert.match(block(short), /\n A short unfinished answer\.\n<\/untrusted_page_content>$|\nA short unfinished answer\.\n<\/untrusted_page_content>$/, `${label}: an answer that fits is not changed`);
assert.doesNotMatch(short, /…/, `${label}: and gets no ellipsis`);
+ const spaced = buildContinuation('Same.', 'proofread', ' answer\n', 'en', { contextWindow: 8192 });
+ assert.match(block(spaced), /\n answer\n<\/untrusted_page_content>$/, `${label}: a continuation preserves edge whitespace`);
// A selection that leaves no room at all is not made worse: the whole text goes and the run refuses it with a reason.
const huge = 'long words '.repeat(1200);
@@ -52419,6 +52421,7 @@ test('a card follow-up sends the model as much of the earlier answer as its wind
const short = buildFollowUp('A short selection.', 'explain', 'A short answer.', options(4096));
assert.ok(short.endsWith('\nA short answer.'), `${label}: an answer that fits is not changed`);
assert.doesNotMatch(short, /left out/, `${label}: and says nothing about leaving anything out`);
+ assert.match(buildFollowUp('Same.', 'proofread', ' answer\n', options(8192)), /\n answer\n$/, `${label}: a follow-up preserves edge whitespace`);
// The page-wide limit still applies underneath.
const capped = buildFollowUp(selection, 'explain', 'a'.repeat(70_000));
assert.match(capped, /\[The answer continues beyond this point\.\]$/, `${label}: an answer past the card limit keeps its own note`);