Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Leave room for the prompt when sizing a card's output budget
Claude5fbf78d694f9
diff --git a/docs/architecture.md b/docs/architecture.mdindex bd4b45192..b000f70ec 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -520,7 +520,9 @@ selection prompt (the selection inside its untrusted boundary, `selection_only`behind a short fixed system message, then makes a single tool-less model call through thecost allowance (90 s timeout) and returns the text. The output budget is sized to theselection (at least 2,000 tokens; Translate and Proofread grow to about two tokens per-character, up to 8,000) and never exceeds the provider's own output ceiling. If the reply+character, up to 8,000) and never exceeds the provider's own output ceiling or the room its+context window leaves after the prompt (counted at half a token per character, with a floor of+256 tokens), so a small local window is not sent a request it would reject. If the replystill stopped at the cap (finish reason, Anthropic `stop_reason` or Responses`incomplete_details`, or token usage at the budget when no reason is reported; OpenAIResponses reports the limit as an error, which the background treats the same way anddiff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex 31951f075..326108226 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -1782,10 +1782,13 @@ async function answerInlineSelection(msg) {try {if (providerManager.providers.size === 0) await providerManager.load();const provider = providerManager.getActive();- // Sized to the selection and never above what the provider accepts; an answer- // that still hits the ceiling is flagged so the card can offer the side panel+ // Sized to the selection and never above what the provider accepts, prompt+ // included; an answer that still hits the ceiling is flagged so the card can offer the side panel// instead of presenting a cut-off translation as complete.- const maxTokens = inlineSelectionMaxTokens(msg.selectionText, msg.action, provider?.maxOutputTokens);+ const maxTokens = inlineSelectionMaxTokens(msg.selectionText, msg.action, provider?.maxOutputTokens, {+ contextWindow: provider?.contextWindow,+ messages,+ });const result = await agent._chatWithCostAllowance(provider,messages,diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.jsindex 257477a73..daeed4050 100644--- a/src/chrome/src/context-menu-storage.js+++ b/src/chrome/src/context-menu-storage.js@@ -278,14 +278,29 @@ export function buildInlineSelectionMessages(selectionText, action, language = 'export const INLINE_ANSWER_MIN_TOKENS = 2000;export const INLINE_ANSWER_MAX_TOKENS = 8000;const INLINE_FALLBACK_OUTPUT_CEILING = 4096;--export function inlineSelectionMaxTokens(selectionText, action, providerCeiling) {+// Room kept for the prompt in a small context window: backends that enforce+// input + max_tokens <= window reject a request whose output budget ignores it. The+// prompt is counted at half a token per character, which holds for CJK and Indic+// text and over-counts Latin text, so a tight window is more likely to cut an answer+// off (the card then offers the side panel) than to refuse it. Chat-template tokens+// are covered by the reserve, and the answer keeps a floor so it is never starved.+const INLINE_PROMPT_TOKENS_PER_CHAR = 0.5;+const INLINE_PROMPT_TOKEN_RESERVE = 128;+const INLINE_MIN_ANSWER_TOKENS = 256;++export function inlineSelectionMaxTokens(selectionText, action, providerCeiling, { contextWindow, messages } = {}) {const chars = String(selectionText || '').trim().length;const wanted = String(action || '').trim() === 'explain'? INLINE_ANSWER_MIN_TOKENS: Math.max(INLINE_ANSWER_MIN_TOKENS, Math.min(INLINE_ANSWER_MAX_TOKENS, chars * 2));const limit = Math.floor(Number(providerCeiling));- return Math.min(wanted, limit > 0 ? limit : INLINE_FALLBACK_OUTPUT_CEILING);+ const budget = Math.min(wanted, limit > 0 ? limit : INLINE_FALLBACK_OUTPUT_CEILING);+ const window = Math.floor(Number(contextWindow));+ if (!(window > 0)) return budget;+ const promptChars = (Array.isArray(messages) ? messages : [])+ .reduce((total, message) => total + (typeof message?.content === 'string' ? message.content.length : 0), 0);+ const promptTokens = Math.ceil(promptChars * INLINE_PROMPT_TOKENS_PER_CHAR) + INLINE_PROMPT_TOKEN_RESERVE;+ return Math.min(budget, Math.max(INLINE_MIN_ANSWER_TOKENS, window - promptTokens));}// Whether the model stopped because it ran out of output budget. Providers reportdiff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex 0c4aa8858..491c2585a 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -1533,10 +1533,13 @@ async function answerInlineSelection(msg) {try {if (providerManager.providers.size === 0) await providerManager.load();const provider = providerManager.getActive();- // Sized to the selection and never above what the provider accepts; an answer- // that still hits the ceiling is flagged so the card can offer the side panel+ // Sized to the selection and never above what the provider accepts, prompt+ // included; an answer that still hits the ceiling is flagged so the card can offer the side panel// instead of presenting a cut-off translation as complete.- const maxTokens = inlineSelectionMaxTokens(msg.selectionText, msg.action, provider?.maxOutputTokens);+ const maxTokens = inlineSelectionMaxTokens(msg.selectionText, msg.action, provider?.maxOutputTokens, {+ contextWindow: provider?.contextWindow,+ messages,+ });const result = await agent._chatWithCostAllowance(provider,messages,diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.jsindex 832c1b6b6..39d86d4cf 100644--- a/src/firefox/src/context-menu-storage.js+++ b/src/firefox/src/context-menu-storage.js@@ -278,14 +278,29 @@ export function buildInlineSelectionMessages(selectionText, action, language = 'export const INLINE_ANSWER_MIN_TOKENS = 2000;export const INLINE_ANSWER_MAX_TOKENS = 8000;const INLINE_FALLBACK_OUTPUT_CEILING = 4096;--export function inlineSelectionMaxTokens(selectionText, action, providerCeiling) {+// Room kept for the prompt in a small context window: backends that enforce+// input + max_tokens <= window reject a request whose output budget ignores it. The+// prompt is counted at half a token per character, which holds for CJK and Indic+// text and over-counts Latin text, so a tight window is more likely to cut an answer+// off (the card then offers the side panel) than to refuse it. Chat-template tokens+// are covered by the reserve, and the answer keeps a floor so it is never starved.+const INLINE_PROMPT_TOKENS_PER_CHAR = 0.5;+const INLINE_PROMPT_TOKEN_RESERVE = 128;+const INLINE_MIN_ANSWER_TOKENS = 256;++export function inlineSelectionMaxTokens(selectionText, action, providerCeiling, { contextWindow, messages } = {}) {const chars = String(selectionText || '').trim().length;const wanted = String(action || '').trim() === 'explain'? INLINE_ANSWER_MIN_TOKENS: Math.max(INLINE_ANSWER_MIN_TOKENS, Math.min(INLINE_ANSWER_MAX_TOKENS, chars * 2));const limit = Math.floor(Number(providerCeiling));- return Math.min(wanted, limit > 0 ? limit : INLINE_FALLBACK_OUTPUT_CEILING);+ const budget = Math.min(wanted, limit > 0 ? limit : INLINE_FALLBACK_OUTPUT_CEILING);+ const window = Math.floor(Number(contextWindow));+ if (!(window > 0)) return budget;+ const promptChars = (Array.isArray(messages) ? messages : [])+ .reduce((total, message) => total + (typeof message?.content === 'string' ? message.content.length : 0), 0);+ const promptTokens = Math.ceil(promptChars * INLINE_PROMPT_TOKENS_PER_CHAR) + INLINE_PROMPT_TOKEN_RESERVE;+ return Math.min(budget, Math.max(INLINE_MIN_ANSWER_TOKENS, window - promptTokens));}// Whether the model stopped because it ran out of output budget. Providers reportdiff --git a/test/run.js b/test/run.jsindex 38ae80edf..4af3f2610 100644--- a/test/run.js+++ b/test/run.js@@ -52275,7 +52275,7 @@ test('inline card answers are one tool-less, bounded model call behind validatedassert.match(body, /buildInlineSelectionMessages\(msg\.selectionText, msg\.action, msg\.language\)/, `${prefix}: the prompt comes from the validated builder`);assert.match(body, /if \(!messages\) return \{ ok: false/, `${prefix}: invalid requests are refused before any model call`);assert.match(body, /agent\._chatWithCostAllowance\(\s*provider,\s*messages,\s*\{ maxTokens, temperature: 0, signal: controller\.signal \},\s*agent\._newCostRunState\(\),/, `${prefix}: the call is bounded, abortable and goes through the cost allowance`);- assert.match(body, /const maxTokens = inlineSelectionMaxTokens\(msg\.selectionText, msg\.action, provider\?\.maxOutputTokens\);/, `${prefix}: the budget is sized to the selection and the provider's ceiling`);+ assert.match(body, /const maxTokens = inlineSelectionMaxTokens\(msg\.selectionText, msg\.action, provider\?\.maxOutputTokens, \{\s*contextWindow: provider\?\.contextWindow,\s*messages,\s*\}\);/, `${prefix}: the budget is sized to the selection, the provider's ceiling and the room its context window leaves after the prompt`);assert.match(body, /inlineAnswerWasCut\(result, maxTokens\) \? \{ ok: true, text, truncated: true \}/, `${prefix}: a capped answer is flagged instead of passing as complete`);assert.match(body, /catch \(error\) \{\s*if \(inlineErrorWasCut\(error\)\) \{[\s\S]*?\{ ok: true, text: partial, truncated: true \}[\s\S]*?\{ ok: false, truncated: true, error:/, `${prefix}: a provider that reports the output limit as an error still ends in the cut-off state`);@@ -52306,6 +52306,27 @@ test('inline card token budget follows the selection and never exceeds the proviassert.equal(budget('x'.repeat(1500), 'translate', undefined), 3000, `${label}: an unknown ceiling falls back to a safe default`);assert.equal(budget('x'.repeat(6000), 'translate', undefined), 4096, `${label}: the fallback ceiling stays conservative`);assert.equal(budget('x'.repeat(6000), 'translate', 'junk'), 4096, `${label}: a non-numeric ceiling is ignored`);++ // The prompt shares the context window with the answer.+ const prompt = (chars) => [{ role: 'system', content: 'x'.repeat(500) }, { role: 'user', content: 'x'.repeat(chars) }];+ const longText = 'x'.repeat(3000);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096, messages: prompt(3500) }), 4096 - Math.ceil(4000 * 0.5) - 128,+ `${label}: a 4k window leaves the answer only what the prompt does not use`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 131_072, messages: prompt(3500) }), 6000,+ `${label}: a large window changes nothing`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 16_384, messages: prompt(3500) }), 6000,+ `${label}: an ordinary local window changes nothing for a card-sized selection`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 2048, messages: prompt(3500) }), 256,+ `${label}: the answer keeps a floor when the prompt alone nearly fills the window`);+ assert.equal(budget('short', 'translate', 100, { contextWindow: 2048, messages: prompt(10) }), 100,+ `${label}: a ceiling lower than the floor still wins`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: undefined, messages: prompt(3500) }), 6000,+ `${label}: an unknown window is not guessed at`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 0 }), 6000, `${label}: a zero window is ignored`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096 }), 4096 - 128,+ `${label}: with no messages only the reserve is kept back`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096, messages: [{ role: 'user', content: ['not a string'] }] }), 4096 - 128,+ `${label}: non-text content is not counted`);}});