Skip to content

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

fix(selection): retain continuation suffix whitespace

Emre Sokullu33bc16ae6933
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index 6d12f2948..e4e8e3236 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -132,8 +132,9 @@ function selectionSourceContract(sourceGrounding) {
: SELECTION_ONLY_SOURCE_CONTRACT;
}
-function wrapSelectedPageText(selectionText, instruction, sourceGrounding = SELECTION_ONLY_SOURCE_GROUNDING) {
- const text = String(selectionText || '').trim();
+function wrapSelectedPageText(selectionText, instruction, sourceGrounding = SELECTION_ONLY_SOURCE_GROUNDING, { trim = true } = {}) {
+ const raw = String(selectionText || '');
+ const text = trim ? raw.trim() : raw;
if (!text) return '';
const nonce = `ctx-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const safe = text.replace(/<\/?untrusted_page_content\b[^>]*>/gi, '[markup stripped]');
@@ -509,7 +510,7 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
trimmed = true;
}
}
- return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING);
+ return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING, { trim: false });
}
const CONTEXT_MENU_PENDING_PREFIX = 'contextMenuPrompt:';
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index 1d8200d78..b32b9eae2 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -132,8 +132,9 @@ function selectionSourceContract(sourceGrounding) {
: SELECTION_ONLY_SOURCE_CONTRACT;
}
-function wrapSelectedPageText(selectionText, instruction, sourceGrounding = SELECTION_ONLY_SOURCE_GROUNDING) {
- const text = String(selectionText || '').trim();
+function wrapSelectedPageText(selectionText, instruction, sourceGrounding = SELECTION_ONLY_SOURCE_GROUNDING, { trim = true } = {}) {
+ const raw = String(selectionText || '');
+ const text = trim ? raw.trim() : raw;
if (!text) return '';
const nonce = `ctx-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const safe = text.replace(/<\/?untrusted_page_content\b[^>]*>/gi, '[markup stripped]');
@@ -509,7 +510,7 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
trimmed = true;
}
}
- return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING);
+ return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING, { trim: false });
}
const CONTEXT_MENU_PENDING_PREFIX = 'contextMenuPrompt:';
diff --git a/test/run.js b/test/run.js
index ea4c67df4..9356342ff 100644
--- a/test/run.js
+++ b/test/run.js
@@ -52341,7 +52341,7 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Co
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`);
+ assert.match(block(spaced), /\n answer\n\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);