Skip to content

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

Carry the whole card answer into a follow-up, not its first 6,000 characters

Claude23b01636cf6a
diff --git a/docs/architecture.md b/docs/architecture.md
index dd72be73b..56085bf1e 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -566,7 +566,7 @@ click and queues the verified prompt with `panelOpened`, because a panel that is
up has not reported in yet and a second `open()` would close it. The card's
answer travels inside the selection's untrusted boundary, labelled as an earlier answer that
may contain mistakes and
-capped at 6,000 characters, so model output derived from page text never becomes trusted
+capped at 32,000 characters (the most a card can produce; a longer one is shortened and says so), so model output derived from page text never becomes trusted
instruction. The selection and the answer go to the configured provider like any other
model call.
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index ec734ce5e..53e3eafad 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -359,7 +359,11 @@ const INLINE_FOLLOW_UP_LABELS = Object.freeze({
explain: 'explanation',
proofread: 'proofreading result',
});
-export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = 6000;
+// The most a card answer can hold: 8,000 output tokens at four characters each. Both a
+// follow-up and a continuation carry the whole answer up to this, because the side panel
+// shows it to the user and a question may refer to any part of it.
+const INLINE_CARD_ANSWER_MAX_CHARS = 32000;
+export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;
// A follow-up typed in a card continues in the side panel. The card's earlier
// answer is model output derived from untrusted page text, so it travels inside
@@ -367,8 +371,12 @@ export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = 6000;
export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer) {
const selection = String(selectionText || '');
const label = INLINE_FOLLOW_UP_LABELS[String(priorAction || '').trim()];
- const answer = String(priorAnswer || '').trim().slice(0, INLINE_FOLLOW_UP_ANSWER_MAX_CHARS);
- if (!label || !answer) return selection;
+ const full = String(priorAnswer || '').trim();
+ if (!label || !full) return selection;
+ // A longer answer than a card can produce is shortened, and says so.
+ const 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.]`
+ : full;
return `${selection}\n\n[Earlier ${label} of the text above, shown to the user in WebBrain's page card. It may contain mistakes.]\n${answer}`;
}
@@ -378,7 +386,7 @@ export function buildFollowUpSelectionText(selectionText, priorAction, priorAnsw
// text, so it travels inside the selection's untrusted block. The side panel shows that
// block to the user, so the whole unfinished text is kept (the cap is the most a card
// can produce, 8,000 tokens at four characters each) and only a longer one loses its start.
-export const INLINE_CONTINUE_PARTIAL_MAX_CHARS = 32000;
+export const INLINE_CONTINUE_PARTIAL_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;
const INLINE_CONTINUE_NOTE = ' An earlier attempt at this stopped partway; the text it produced is included after the selection. Continue it from exactly where it stops and give only the missing remainder, without repeating anything already written.';
export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '') {
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index ddff71922..101ba942c 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -359,7 +359,11 @@ const INLINE_FOLLOW_UP_LABELS = Object.freeze({
explain: 'explanation',
proofread: 'proofreading result',
});
-export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = 6000;
+// The most a card answer can hold: 8,000 output tokens at four characters each. Both a
+// follow-up and a continuation carry the whole answer up to this, because the side panel
+// shows it to the user and a question may refer to any part of it.
+const INLINE_CARD_ANSWER_MAX_CHARS = 32000;
+export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;
// A follow-up typed in a card continues in the side panel. The card's earlier
// answer is model output derived from untrusted page text, so it travels inside
@@ -367,8 +371,12 @@ export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = 6000;
export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer) {
const selection = String(selectionText || '');
const label = INLINE_FOLLOW_UP_LABELS[String(priorAction || '').trim()];
- const answer = String(priorAnswer || '').trim().slice(0, INLINE_FOLLOW_UP_ANSWER_MAX_CHARS);
- if (!label || !answer) return selection;
+ const full = String(priorAnswer || '').trim();
+ if (!label || !full) return selection;
+ // A longer answer than a card can produce is shortened, and says so.
+ const 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.]`
+ : full;
return `${selection}\n\n[Earlier ${label} of the text above, shown to the user in WebBrain's page card. It may contain mistakes.]\n${answer}`;
}
@@ -378,7 +386,7 @@ export function buildFollowUpSelectionText(selectionText, priorAction, priorAnsw
// text, so it travels inside the selection's untrusted block. The side panel shows that
// block to the user, so the whole unfinished text is kept (the cap is the most a card
// can produce, 8,000 tokens at four characters each) and only a longer one loses its start.
-export const INLINE_CONTINUE_PARTIAL_MAX_CHARS = 32000;
+export const INLINE_CONTINUE_PARTIAL_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;
const INLINE_CONTINUE_NOTE = ' An earlier attempt at this stopped partway; the text it produced is included after the selection. Continue it from exactly where it stops and give only the missing remainder, without repeating anything already written.';
export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '') {
diff --git a/test/run.js b/test/run.js
index c4b218d4f..c64d3069a 100644
--- a/test/run.js
+++ b/test/run.js
@@ -699,6 +699,7 @@ const {
buildContinuationSelectionPrompt: buildContinuationSelectionPromptCh,
INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_CH,
buildFollowUpSelectionText: buildFollowUpSelectionTextCh,
+ INLINE_FOLLOW_UP_ANSWER_MAX_CHARS: INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_CH,
buildInlineSelectionMessages: buildInlineSelectionMessagesCh,
SELECTION_CONTEXT_SOURCE_GROUNDING: SELECTION_CONTEXT_SOURCE_GROUNDING_CH,
SELECTION_ONLY_SOURCE_GROUNDING: SELECTION_ONLY_SOURCE_GROUNDING_CH,
@@ -723,6 +724,7 @@ const {
buildContinuationSelectionPrompt: buildContinuationSelectionPromptFx,
INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_FX,
buildFollowUpSelectionText: buildFollowUpSelectionTextFx,
+ INLINE_FOLLOW_UP_ANSWER_MAX_CHARS: INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_FX,
buildInlineSelectionMessages: buildInlineSelectionMessagesFx,
SELECTION_CONTEXT_SOURCE_GROUNDING: SELECTION_CONTEXT_SOURCE_GROUNDING_FX,
SELECTION_ONLY_SOURCE_GROUNDING: SELECTION_ONLY_SOURCE_GROUNDING_FX,
@@ -52237,7 +52239,14 @@ test('inline card prompts keep the selection and any earlier answer inside the u
assert.equal(follow.startsWith('selected words\n\n[Earlier explanation of the text above'), true, `${label}: the earlier answer follows the selection`);
assert.equal(buildFollowUp('sel', 'explain', ''), 'sel', `${label}: no answer means the selection is unchanged`);
assert.equal(buildFollowUp('sel', 'summarize', 'answer'), 'sel', `${label}: only card actions can supply an earlier answer`);
- assert.equal(buildFollowUp('sel', 'translate', 'x'.repeat(20000)).length < 'sel'.length + 6300, true, `${label}: the earlier answer is bounded`);
+ // The whole card answer reaches the side panel, up to the most a card can produce.
+ const wholeAnswer = `FIRST-${'y'.repeat(11_000)}-LAST`;
+ assert.equal(buildFollowUp('sel', 'translate', wholeAnswer).endsWith(`\n${wholeAnswer}`), true, `${label}: a long card answer is carried in full, end included`);
+ assert.equal(label === 'chrome' ? INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_CH : INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_FX, 32000, `${label}: the follow-up cap is the largest card answer`);
+ const oversized = buildFollowUp('sel', 'translate', 'x'.repeat(40000));
+ assert.equal(oversized.length < 'sel'.length + 32300 + 120, true, `${label}: an answer beyond that is bounded`);
+ assert.match(oversized, /\[The answer continues beyond this point\.\]$/, `${label}: and says it was shortened`);
+ assert.doesNotMatch(buildFollowUp('sel', 'translate', 'x'.repeat(32000)), /continues beyond/, `${label}: an answer at the cap is not marked`);
const prompt = buildSelectionPrompt(follow, 'custom', 'Why?', '', grounding);
const answerAt = prompt.indexOf('do evil');
assert.equal(answerAt > prompt.indexOf('<untrusted_page_content id="'), true, `${label}: the earlier answer must sit inside the untrusted block`);