Skip to content

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

fix(selection): preserve model whitespace

Emre Sokulluc0013a65be57
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 556fc3b32..8610cecd0 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1808,8 +1808,9 @@ async function answerInlineSelection(msg) {
{ maxTokens, temperature: 0, signal: controller.signal },
agent._newCostRunState(),
);
- const { text, cut } = capInlineAnswer(String(result?.content || '').trim());
- if (!text) {
+ const content = String(result?.content || '');
+ const { text, cut } = capInlineAnswer(content);
+ if (!content.trim()) {
const notice = inlineCostNotice(result);
return { ok: false, error: 'The model returned an empty answer.', ...(notice ? { notice } : {}) };
}
@@ -1822,9 +1823,10 @@ async function answerInlineSelection(msg) {
if (inlineErrorWasCut(error)) {
// The provider reported the output limit as an error. Show what it wrote, if
// anything, and let the card hand the rest to the side panel.
- const partial = capInlineAnswer(String(error.partialContent || '').trim()).text;
+ const partialContent = String(error.partialContent || '');
+ const partial = capInlineAnswer(partialContent).text;
const notice = inlineCostNotice(error);
- return partial
+ return partialContent.trim()
? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
: {
ok: false,
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 902158d3c..b701cb7b0 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1559,8 +1559,9 @@ async function answerInlineSelection(msg) {
{ maxTokens, temperature: 0, signal: controller.signal },
agent._newCostRunState(),
);
- const { text, cut } = capInlineAnswer(String(result?.content || '').trim());
- if (!text) {
+ const content = String(result?.content || '');
+ const { text, cut } = capInlineAnswer(content);
+ if (!content.trim()) {
const notice = inlineCostNotice(result);
return { ok: false, error: 'The model returned an empty answer.', ...(notice ? { notice } : {}) };
}
@@ -1573,9 +1574,10 @@ async function answerInlineSelection(msg) {
if (inlineErrorWasCut(error)) {
// The provider reported the output limit as an error. Show what it wrote, if
// anything, and let the card hand the rest to the side panel.
- const partial = capInlineAnswer(String(error.partialContent || '').trim()).text;
+ const partialContent = String(error.partialContent || '');
+ const partial = capInlineAnswer(partialContent).text;
const notice = inlineCostNotice(error);
- return partial
+ return partialContent.trim()
? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
: {
ok: false,
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index 7e3f66cad..a7191c8fe 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2218,6 +2218,19 @@ for (const [label, sourcePath, manualOpen] of [
}
});
+ test(`${label}: inline responses preserve edge whitespace`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
+ const answer = ' key: value\n';
+ await page.evaluate((text) => {
+ window.__selectionInlineHandler = async () => ({ ok: true, text });
+ }, answer);
+ await openInlineCard(page, 'translate');
+ const state = await waitForCardStatus(page, 'ready');
+ if (state.cardText !== answer) {
+ throw new Error(`an inline response should preserve edge whitespace: ${JSON.stringify(state.cardText)}`);
+ }
+ });
+
test(`${label}: a failed card answer offers a retry, and a follow-up waits for an answer`, async (page) => {
await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
await page.evaluate(() => {