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
Treat an OpenAI Responses output-limit error as a cut-off in the inline card
Claudec31e106bff11
diff --git a/docs/architecture.md b/docs/architecture.mdindex b05e27c59..521f6a01d 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -521,7 +521,9 @@ cost allowance (90 s timeout) and returns the text. The output budget is sized tselection (at least 2,000 tokens; Translate and Proofread grow to about two tokens percharacter, up to 8,000) and never exceeds the provider's own output ceiling. 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) the response+`incomplete_details`, or token usage at the budget when no reason is reported; OpenAI+Responses reports the limit as an error, which the background treats the same way and+answers with whatever text the provider returned) the responseis flagged `truncated`, and the card says so and offers **Continue in side panel** insteadof passing a cut-off translation off as complete. Each call carries a job id, and a cardthat is closed, retried or retargeted sends `WB_SELECTION_SHORTCUT_INLINE_CANCEL` so thediff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex 959e48be7..dc2bafa22 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -73,6 +73,7 @@ import {buildFullContextSelectionPrompt,buildInlineSelectionMessages,inlineAnswerWasCut,+ inlineErrorWasCut,inlineSelectionMaxTokens,buildSelectionPrompt,normalizeSelectionAction,@@ -1782,6 +1783,14 @@ async function answerInlineSelection(msg) {if (!text) return { ok: false, error: 'The model returned an empty answer.' };return inlineAnswerWasCut(result, maxTokens) ? { ok: true, text, truncated: true } : { ok: true, text };} catch (error) {+ 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 = String(error.partialContent || '').trim();+ return partial+ ? { ok: true, text: partial, truncated: true }+ : { ok: false, truncated: true, error: 'The answer was cut off before any text was produced.' };+ }return { ok: false, error: String(error?.message || error).slice(0, 300) };} finally {clearTimeout(timer);diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.jsindex 2111852e2..5fc33ccd1 100644--- a/src/chrome/src/content/selection-shortcut.js+++ b/src/chrome/src/content/selection-shortcut.js@@ -922,12 +922,14 @@parts.source.textContent = cardState.sourceLabel || str('autoDetect');parts.target.value = cardState.target;}- parts.result.hidden = status === 'idle';+ // A cut-off with no text has nothing to show or retry, so only the notice remains.+ const cutOffOnly = status === 'error' && cardState.truncated;+ parts.result.hidden = status === 'idle' || cutOffOnly;parts.result.setAttribute('aria-busy', status === 'loading' ? 'true' : 'false');parts.skeleton.hidden = status !== 'loading';parts.text.hidden = status !== 'ready';- parts.error.hidden = status !== 'error';- parts.cut.hidden = !(status === 'ready' && cardState.truncated);+ parts.error.hidden = status !== 'error' || cutOffOnly;+ parts.cut.hidden = !((status === 'ready' || cutOffOnly) && cardState.truncated);if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');if (status === 'error') {parts.errorText.textContent = str('inlineFailed');@@ -1033,10 +1035,12 @@if (requestId !== cardRequestId || !cardState) return;cardJobId = '';const answer = typeof response?.text === 'string' ? response.text.trim() : '';- if (!response?.ok || !answer) throw new Error(typeof response?.error === 'string' ? response.error : '');+ cardState.truncated = response?.truncated === true;+ if (!response?.ok || !answer) {+ throw new Error(typeof response?.error === 'string' && !cardState.truncated ? response.error : '');+ }cardState.status = 'ready';cardState.text = answer;- cardState.truncated = response.truncated === true;} catch (error) {if (requestId !== cardRequestId || !cardState) return;cardJobId = '';@@ -1060,7 +1064,7 @@// A cut-off answer is finished in the side panel, where the reply is not capped.function continueInPanel() {- if (!cardState || cardState.status !== 'ready') return;+ if (!cardState?.truncated) return;const { kind, target } = cardState;submitSelection(kind, '', kind === 'translate' ? target : interfaceLanguage);}diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.jsindex 4d4085123..199722d7e 100644--- a/src/chrome/src/context-menu-storage.js+++ b/src/chrome/src/context-menu-storage.js@@ -288,6 +288,13 @@ export function inlineSelectionMaxTokens(selectionText, action, providerCeiling)// that report none.const INLINE_CUT_REASONS = new Set(['length', 'max_tokens', 'max_output_tokens', 'max_completion_tokens']);+// Some providers (OpenAI Responses) report hitting the output limit as an error+// instead of a result. Such an error counts as a cut-off, not as a failure to retry.+export function inlineErrorWasCut(error) {+ const reason = String(error?.incompleteReason ?? '').trim().toLowerCase();+ return INLINE_CUT_REASONS.has(reason);+}+export function inlineAnswerWasCut(result, maxTokens) {const raw = result?.raw;const reason = [diff --git a/src/chrome/src/providers/openai.js b/src/chrome/src/providers/openai.jsindex 1099df8c3..b37cf89a9 100644--- a/src/chrome/src/providers/openai.js+++ b/src/chrome/src/providers/openai.js@@ -889,6 +889,9 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {error.isAskStreamTerminalError = !!stream && !error.isResponsesStreamFallbackSafe;error.incomplete = true;error.incompleteReason = reason;+ // What the model wrote before it stopped, for callers that can show a partial answer.+ const partial = Array.isArray(response?.output) ? this._responseText(response.output) : '';+ if (partial) error.partialContent = partial;return error;}diff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex a71f346ab..26accf549 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -70,6 +70,7 @@ import {buildFullContextSelectionPrompt,buildInlineSelectionMessages,inlineAnswerWasCut,+ inlineErrorWasCut,inlineSelectionMaxTokens,buildSelectionPrompt,normalizeSelectionAction,@@ -1545,6 +1546,14 @@ async function answerInlineSelection(msg) {if (!text) return { ok: false, error: 'The model returned an empty answer.' };return inlineAnswerWasCut(result, maxTokens) ? { ok: true, text, truncated: true } : { ok: true, text };} catch (error) {+ 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 = String(error.partialContent || '').trim();+ return partial+ ? { ok: true, text: partial, truncated: true }+ : { ok: false, truncated: true, error: 'The answer was cut off before any text was produced.' };+ }return { ok: false, error: String(error?.message || error).slice(0, 300) };} finally {clearTimeout(timer);diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.jsindex 2111852e2..5fc33ccd1 100644--- a/src/firefox/src/content/selection-shortcut.js+++ b/src/firefox/src/content/selection-shortcut.js@@ -922,12 +922,14 @@parts.source.textContent = cardState.sourceLabel || str('autoDetect');parts.target.value = cardState.target;}- parts.result.hidden = status === 'idle';+ // A cut-off with no text has nothing to show or retry, so only the notice remains.+ const cutOffOnly = status === 'error' && cardState.truncated;+ parts.result.hidden = status === 'idle' || cutOffOnly;parts.result.setAttribute('aria-busy', status === 'loading' ? 'true' : 'false');parts.skeleton.hidden = status !== 'loading';parts.text.hidden = status !== 'ready';- parts.error.hidden = status !== 'error';- parts.cut.hidden = !(status === 'ready' && cardState.truncated);+ parts.error.hidden = status !== 'error' || cutOffOnly;+ parts.cut.hidden = !((status === 'ready' || cutOffOnly) && cardState.truncated);if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');if (status === 'error') {parts.errorText.textContent = str('inlineFailed');@@ -1033,10 +1035,12 @@if (requestId !== cardRequestId || !cardState) return;cardJobId = '';const answer = typeof response?.text === 'string' ? response.text.trim() : '';- if (!response?.ok || !answer) throw new Error(typeof response?.error === 'string' ? response.error : '');+ cardState.truncated = response?.truncated === true;+ if (!response?.ok || !answer) {+ throw new Error(typeof response?.error === 'string' && !cardState.truncated ? response.error : '');+ }cardState.status = 'ready';cardState.text = answer;- cardState.truncated = response.truncated === true;} catch (error) {if (requestId !== cardRequestId || !cardState) return;cardJobId = '';@@ -1060,7 +1064,7 @@// A cut-off answer is finished in the side panel, where the reply is not capped.function continueInPanel() {- if (!cardState || cardState.status !== 'ready') return;+ if (!cardState?.truncated) return;const { kind, target } = cardState;submitSelection(kind, '', kind === 'translate' ? target : interfaceLanguage);}diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.jsindex 9b6d62b25..e6f3ce14a 100644--- a/src/firefox/src/context-menu-storage.js+++ b/src/firefox/src/context-menu-storage.js@@ -288,6 +288,13 @@ export function inlineSelectionMaxTokens(selectionText, action, providerCeiling)// that report none.const INLINE_CUT_REASONS = new Set(['length', 'max_tokens', 'max_output_tokens', 'max_completion_tokens']);+// Some providers (OpenAI Responses) report hitting the output limit as an error+// instead of a result. Such an error counts as a cut-off, not as a failure to retry.+export function inlineErrorWasCut(error) {+ const reason = String(error?.incompleteReason ?? '').trim().toLowerCase();+ return INLINE_CUT_REASONS.has(reason);+}+export function inlineAnswerWasCut(result, maxTokens) {const raw = result?.raw;const reason = [diff --git a/src/firefox/src/providers/openai.js b/src/firefox/src/providers/openai.jsindex 069729782..b1624df23 100644--- a/src/firefox/src/providers/openai.js+++ b/src/firefox/src/providers/openai.js@@ -906,6 +906,9 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {error.isAskStreamTerminalError = !!stream && !error.isResponsesStreamFallbackSafe;error.incomplete = true;error.incompleteReason = reason;+ // What the model wrote before it stopped, for callers that can show a partial answer.+ const partial = Array.isArray(response?.output) ? this._responseText(response.output) : '';+ if (partial) error.partialContent = partial;return error;}diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjsindex 1e44b8ee8..6629fdc31 100644--- a/test/fixtures/run.mjs+++ b/test/fixtures/run.mjs@@ -2272,6 +2272,39 @@ for (const [label, sourcePath, manualOpen] of [});}+ test(`${label}: an output limit with no text left offers only the side panel, not a retry`, async (page) => {+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });+ await page.evaluate(() => {+ window.__selectionInlineHandler = async () => ({+ ok: false,+ truncated: true,+ error: 'The answer was cut off before any text was produced.',+ });+ });+ await openInlineCard(page, 'proofread');+ await page.waitForFunction(() => window.__webbrainSelectionShortcut.getState().cardTruncated);+ let state = await page.evaluate(() => window.__webbrainSelectionShortcut.getState());+ if (state.cardStatus !== 'error' || state.retryRect || state.cardError || !state.continueRect || !state.cutNoticeText) {+ throw new Error(`a cut-off without text should show the notice and the side-panel button only: ${JSON.stringify(state)}`);+ }+ await clickRectCenter(page, state.continueRect);+ await page.waitForFunction(() => window.__selectionMessages.length === 1);+ const message = await page.evaluate(() => window.__selectionMessages[0]);+ if (message.action !== 'proofread' || !/Selected words near the viewport edge/.test(message.selectionText)) {+ throw new Error(`continuing should hand the same action to the side panel: ${JSON.stringify(message)}`);+ }++ // A plain failure is unchanged: the reason and a retry, no side-panel button.+ await page.evaluate(() => {+ window.__selectionInlineHandler = async () => ({ ok: false, error: 'No provider configured' });+ });+ await openInlineCard(page, 'explain');+ state = await waitForCardStatus(page, 'error');+ if (state.cardTruncated || state.continueRect || !state.retryRect || state.cardError !== 'No provider configured') {+ throw new Error(`an ordinary failure must keep its retry and reason: ${JSON.stringify(state)}`);+ }+ });+test(`${label}: a double click while the language bundle loads starts one card and one model call`, async (page) => {await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen, holdLocalization: true });const selected = await selectFixtureText(page);diff --git a/test/run.js b/test/run.jsindex fd5cfce19..2198b3f3e 100644--- a/test/run.js+++ b/test/run.js@@ -694,6 +694,7 @@ const {INLINE_SELECTION_ACTIONS: INLINE_SELECTION_ACTIONS_CH,INLINE_SELECTION_MAX_CHARS: INLINE_SELECTION_MAX_CHARS_CH,inlineAnswerWasCut: inlineAnswerWasCutCh,+ inlineErrorWasCut: inlineErrorWasCutCh,inlineSelectionMaxTokens: inlineSelectionMaxTokensCh,buildFollowUpSelectionText: buildFollowUpSelectionTextCh,buildInlineSelectionMessages: buildInlineSelectionMessagesCh,@@ -715,6 +716,7 @@ const {INLINE_SELECTION_ACTIONS: INLINE_SELECTION_ACTIONS_FX,INLINE_SELECTION_MAX_CHARS: INLINE_SELECTION_MAX_CHARS_FX,inlineAnswerWasCut: inlineAnswerWasCutFx,+ inlineErrorWasCut: inlineErrorWasCutFx,inlineSelectionMaxTokens: inlineSelectionMaxTokensFx,buildFollowUpSelectionText: buildFollowUpSelectionTextFx,buildInlineSelectionMessages: buildInlineSelectionMessagesFx,@@ -51757,6 +51759,7 @@ test('inline card answers are one tool-less, bounded model call behind validatedassert.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, /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`);// A card that is closed, retried or retargeted cancels the paid call it no longer needs.assert.match(body, /const jobId = typeof msg\.jobId === 'string' && INLINE_JOB_ID_PATTERN\.test\(msg\.jobId\) \? msg\.jobId : '';\s*if \(jobId\) inlineSelectionJobs\.set\(jobId, controller\);/, `${prefix}: only well-formed job ids are tracked`);@@ -51805,6 +51808,45 @@ test('inline card detects an answer that stopped at the output cap on every prov}});+test('inline card treats a Responses output-limit error as a cut-off with any partial text', () => {+ for (const [label, Provider, wasCutError] of [+ ['chrome', OpenAIProviderCh, inlineErrorWasCutCh],+ ['firefox', OpenAIProviderFx, inlineErrorWasCutFx],+ ]) {+ const provider = new Provider({+ category: 'cloud',+ providerName: 'openai',+ baseUrl: 'https://api.openai.com/v1',+ model: 'gpt-5.6-terra',+ });+ const thrown = (data) => {+ try { provider._parseResponsesData(data); } catch (error) { return error; }+ return null;+ };+ const partial = thrown({+ status: 'incomplete',+ incomplete_details: { reason: 'max_output_tokens' },+ output: [{ type: 'message', content: [{ type: 'output_text', text: 'Half of the ' }, { type: 'output_text', text: 'translation' }] }],+ });+ assert.equal(partial?.incompleteReason, 'max_output_tokens', `${label}: the provider still raises the output limit as an error for the agent`);+ assert.equal(partial?.partialContent, 'Half of the translation', `${label}: the error carries what the model wrote before it stopped`);+ assert.equal(wasCutError(partial), true, `${label}: the card treats that error as a cut-off`);++ const reasoningOnly = thrown({+ status: 'incomplete',+ incomplete_details: { reason: 'max_output_tokens' },+ output: [{ type: 'reasoning', summary: [] }],+ });+ assert.equal(wasCutError(reasoningOnly), true, `${label}: a limit spent on reasoning is still a cut-off`);+ assert.equal('partialContent' in reasoningOnly, false, `${label}: no text means no partial answer`);++ assert.equal(wasCutError(thrown({ status: 'incomplete', incomplete_details: { reason: 'content_filter' } })), false, `${label}: a content filter is a failure, not a cut-off`);+ assert.equal(wasCutError(provider._responsesIncompleteError({ incomplete_details: { reason: 'missing_response_completed' } }, { stream: true })), false, `${label}: a stream that never completed is a failure`);+ assert.equal(wasCutError(new Error('network error')), false, `${label}: ordinary errors stay errors`);+ assert.equal(wasCutError(null), false, `${label}: no error is not a cut-off`);+ }+});+test('inline card claims its request before waiting, cancels what it abandons and finishes cut-off answers in the panel', () => {for (const prefix of ['src/chrome', 'src/firefox']) {const content = fs.readFileSync(path.join(ROOT, prefix, 'src/content/selection-shortcut.js'), 'utf8');@@ -51829,9 +51871,9 @@ test('inline card claims its request before waiting, cancels what it abandons anassert.match(body('function cancelInline()', 'async function runInline()'), /type: INLINE_CANCEL_MESSAGE, jobId/, `${prefix}: cancellation names the job`);// A capped answer is never shown as complete.- assert.match(run, /cardState\.truncated = response\.truncated === true;/, `${prefix}: the card records the cut-off flag`);- assert.match(content, /parts\.cut\.hidden = !\(status === 'ready' && cardState\.truncated\);/, `${prefix}: the notice shows for a cut-off answer only`);- assert.match(body('function continueInPanel()', 'function resetCard()'), /submitSelection\(kind, '', kind === 'translate' \? target : interfaceLanguage\)/, `${prefix}: continuing hands the same action to the side panel`);+ assert.match(run, /cardState\.truncated = response\?\.truncated === true;\s*if \(!response\?\.ok \|\| !answer\) \{\s*throw new Error\(typeof response\?\.error === 'string' && !cardState\.truncated \? response\.error : ''\);/, `${prefix}: the card records the cut-off flag even when no text came back`);+ assert.match(content, /const cutOffOnly = status === 'error' && cardState\.truncated;[\s\S]*?parts\.error\.hidden = status !== 'error' \|\| cutOffOnly;\s*parts\.cut\.hidden = !\(\(status === 'ready' \|\| cutOffOnly\) && cardState\.truncated\);/, `${prefix}: the notice shows for a cut-off answer only, and a cut-off with no text drops the retry`);+ assert.match(body('function continueInPanel()', 'function resetCard()'), /if \(!cardState\?\.truncated\) return;[\s\S]*submitSelection\(kind, '', kind === 'translate' \? target : interfaceLanguage\)/, `${prefix}: continuing is only offered for a cut-off answer and hands the same action to the side panel`);}});