Skip to content

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

fix(selection): retain notice on empty cutoffs

Emre Sokullu925f5ba831f9
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 72396467e..7d0767f19 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1823,7 +1823,12 @@ async function answerInlineSelection(msg) {
const notice = inlineCostNotice(error);
return partial
? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
- : { ok: false, truncated: true, error: 'The answer was cut off before any text was produced.' };
+ : {
+ ok: false,
+ truncated: true,
+ error: 'The answer was cut off before any text was produced.',
+ ...(notice ? { notice } : {}),
+ };
}
return { ok: false, error: String(error?.message || error).slice(0, 300) };
} finally {
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.js
index 9fb851980..9d64ac05c 100644
--- a/src/chrome/src/content/selection-shortcut.js
+++ b/src/chrome/src/content/selection-shortcut.js
@@ -931,8 +931,8 @@
parts.text.hidden = status !== 'ready';
parts.error.hidden = status !== 'error' || cutOffOnly;
parts.cut.hidden = !((status === 'ready' || cutOffOnly) && cardState.truncated);
- parts.notice.hidden = !(status === 'ready' && cardState.notice);
- parts.notice.textContent = status === 'ready' ? cardState.notice : '';
+ parts.notice.hidden = !((status === 'ready' || cutOffOnly) && cardState.notice);
+ parts.notice.textContent = status === 'ready' || cutOffOnly ? cardState.notice : '';
if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');
if (status === 'error') {
parts.errorText.textContent = str('inlineFailed');
@@ -1052,6 +1052,9 @@
cardJobId = '';
let answer = typeof response?.text === 'string' ? response.text.trim() : '';
cardState.truncated = response?.truncated === true;
+ // This also applies to a textless cut-off, where it warns that continuing
+ // in the panel would be blocked by the exhausted allowance.
+ cardState.notice = typeof response?.notice === 'string' ? response.notice.slice(0, 300) : '';
if (!response?.ok || !answer) {
throw new Error(typeof response?.error === 'string' && !cardState.truncated ? response.error : '');
}
@@ -1064,9 +1067,6 @@
}
cardState.status = 'ready';
cardState.text = answer;
- // Something the person should know about the answer they are looking at, such as
- // the cost allowance having been reached by this very request.
- cardState.notice = typeof response?.notice === 'string' ? response.notice.slice(0, 300) : '';
} catch (error) {
if (requestId !== cardRequestId || !cardState) return;
cardJobId = '';
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index c5583f2cf..3645206ad 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1574,7 +1574,12 @@ async function answerInlineSelection(msg) {
const notice = inlineCostNotice(error);
return partial
? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
- : { ok: false, truncated: true, error: 'The answer was cut off before any text was produced.' };
+ : {
+ ok: false,
+ truncated: true,
+ error: 'The answer was cut off before any text was produced.',
+ ...(notice ? { notice } : {}),
+ };
}
return { ok: false, error: String(error?.message || error).slice(0, 300) };
} finally {
diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.js
index 9fb851980..807063710 100644
--- a/src/firefox/src/content/selection-shortcut.js
+++ b/src/firefox/src/content/selection-shortcut.js
@@ -931,8 +931,8 @@
parts.text.hidden = status !== 'ready';
parts.error.hidden = status !== 'error' || cutOffOnly;
parts.cut.hidden = !((status === 'ready' || cutOffOnly) && cardState.truncated);
- parts.notice.hidden = !(status === 'ready' && cardState.notice);
- parts.notice.textContent = status === 'ready' ? cardState.notice : '';
+ parts.notice.hidden = !((status === 'ready' || cutOffOnly) && cardState.notice);
+ parts.notice.textContent = status === 'ready' || cutOffOnly ? cardState.notice : '';
if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');
if (status === 'error') {
parts.errorText.textContent = str('inlineFailed');
@@ -1052,6 +1052,9 @@
cardJobId = '';
let answer = typeof response?.text === 'string' ? response.text.trim() : '';
cardState.truncated = response?.truncated === true;
+ // This also applies to a textless cut-off, where it warns that continuing
+ // in the sidebar would be blocked by the exhausted allowance.
+ cardState.notice = typeof response?.notice === 'string' ? response.notice.slice(0, 300) : '';
if (!response?.ok || !answer) {
throw new Error(typeof response?.error === 'string' && !cardState.truncated ? response.error : '');
}
@@ -1064,9 +1067,6 @@
}
cardState.status = 'ready';
cardState.text = answer;
- // Something the person should know about the answer they are looking at, such as
- // the cost allowance having been reached by this very request.
- cardState.notice = typeof response?.notice === 'string' ? response.notice.slice(0, 300) : '';
} catch (error) {
if (requestId !== cardRequestId || !cardState) return;
cardJobId = '';
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index 584651b9d..3d73d1254 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2547,6 +2547,24 @@ for (const [label, sourcePath, manualOpen] of [
}
});
+ test(`${label}: a textless cut-off card preserves the cost allowance notice`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
+ const notice = 'Cloud cost allowance reached: total cloud/router usage is $5.00 against the $5.00 limit.';
+ await page.evaluate((text) => {
+ window.__selectionInlineHandler = async () => ({
+ ok: false,
+ truncated: true,
+ notice: text,
+ error: 'The answer was cut off before any text was produced.',
+ });
+ }, notice);
+ await openInlineCard(page, 'proofread');
+ const state = await waitForCardStatus(page, 'error');
+ if (state.cardNotice !== notice || !state.cardTruncated || !state.continueRect || state.cardError) {
+ throw new Error(`a textless cut-off should show its allowance notice: ${JSON.stringify(state)}`);
+ }
+ });
+
test(`${label}: an answer beyond the card limit is cut, flagged and carried whole to the side panel`, async (page) => {
await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
await page.evaluate(() => {