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
Fix native thinking in Ask-to-Act handoff classifier
Barack Sokullu700946ed49c6
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 7e6a7682e..f89cca536 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -19975,12 +19975,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst costState = this.currentCostState.get(tabId) || null;const { tabUrl, tabTitle } = await this._getTabUrlTitle(tabId);const messages = buildAskModeHandoffMessages(userMessage, finalResponse, tabUrl, tabTitle);+ const chatOptions = this._plannerChatOptions(provider, false, true, 'ask_mode_handoff');+ if (['anthropic', 'anthropic-oauth', 'google-vertex-anthropic'].includes(provider?.name)) {+ // Native thinking cannot share this classifier's 24-token output budget.+ chatOptions.extraBody = {+ ...chatOptions.extraBody,+ thinking: { type: 'disabled' },+ };+ }const response = await this._withContentActionDeadline(signal => this._chatWithCostAllowance(provider,messages,{- ...this._plannerChatOptions(provider, false, true, 'ask_mode_handoff'),+ ...chatOptions,temperature: 0,maxTokens: 24,signal,diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 50d7c5988..a40c1e326 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -17759,12 +17759,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst costState = this.currentCostState.get(tabId) || null;const { tabUrl, tabTitle } = await this._getTabUrlTitle(tabId);const messages = buildAskModeHandoffMessages(userMessage, finalResponse, tabUrl, tabTitle);+ const chatOptions = this._plannerChatOptions(provider, false, true, 'ask_mode_handoff');+ if (['anthropic', 'anthropic-oauth', 'google-vertex-anthropic'].includes(provider?.name)) {+ // Native thinking cannot share this classifier's 24-token output budget.+ chatOptions.extraBody = {+ ...chatOptions.extraBody,+ thinking: { type: 'disabled' },+ };+ }const response = await this._withContentActionDeadline(signal => this._chatWithCostAllowance(provider,messages,{- ...this._plannerChatOptions(provider, false, true, 'ask_mode_handoff'),+ ...chatOptions,temperature: 0,maxTokens: 24,signal,diff --git a/test/run.js b/test/run.jsindex 04503b427..2006f397c 100644--- a/test/run.js+++ b/test/run.js@@ -604,10 +604,10 @@ const { buildPromptTraceProvenance: buildPromptTraceProvenanceFx } = await impor// anthropic.js imports cleanly under Node (its chrome.* touches are lazy); we// only exercise the pure _convertMessages transform here.-const { AnthropicProvider: AnthropicProviderCh } = await import(+const { AnthropicProvider: AnthropicProviderCh, AnthropicOAuthProvider: AnthropicOAuthProviderCh } = await import('file://' + path.join(ROOT, 'src/chrome/src/providers/anthropic.js').replace(/\\/g, '/'));-const { AnthropicProvider: AnthropicProviderFx } = await import(+const { AnthropicProvider: AnthropicProviderFx, AnthropicOAuthProvider: AnthropicOAuthProviderFx } = await import('file://' + path.join(ROOT, 'src/firefox/src/providers/anthropic.js').replace(/\\/g, '/'));@@ -12128,6 +12128,62 @@ test('Ask mode handoff classification is strict, silent, and mode guarded', asyn}});+test('Ask mode handoff disables native Anthropic thinking only for the classifier request', async () => {+ const originalFetch = globalThis.fetch;+ try {+ for (const [browserLabel, AgentClass, Provider, OAuthProvider, VertexProvider] of [+ ['chrome', AgentCh, AnthropicProviderCh, AnthropicOAuthProviderCh, VertexAnthropicProviderCh],+ ['firefox', AgentFx, AnthropicProviderFx, AnthropicOAuthProviderFx, VertexAnthropicProviderFx],+ ]) {+ for (const ProviderClass of [Provider, OAuthProvider, VertexProvider]) {+ for (const thinking of [+ { type: 'enabled', budget_tokens: 1024 },+ { type: 'adaptive' },+ ]) {+ const provider = new ProviderClass({+ model: 'claude-sonnet-4-6',+ apiKey: 'test-key',+ project: 'sample-project',+ location: 'us-east5',+ extraBody: { thinking },+ });+ if (provider instanceof OAuthProvider) provider._ensureFreshToken = async () => {};+ const label = `${browserLabel}/${provider.name}/${thinking.type}`;+ const requests = [];+ globalThis.fetch = async (_url, init) => {+ const body = JSON.parse(init.body);+ requests.push(body);+ if (body.thinking?.type === 'enabled' && body.thinking.budget_tokens >= body.max_tokens) {+ return new Response('thinking.budget_tokens must be less than max_tokens', { status: 400 });+ }+ return new Response(JSON.stringify({+ content: [{ type: 'text', text: '{"mode_handoff":"act"}' }],+ usage: { input_tokens: 12, output_tokens: 7 },+ }), { status: 200, headers: { 'Content-Type': 'application/json' } });+ };+ const agent = new AgentClass({ getActive: () => provider, getVisionProvider: async () => null });+ agent._getTabUrlTitle = async () => ({ tabUrl: 'https://example.com', tabTitle: 'Example' });+ const updates = [];+ await agent._maybeEmitAskModeHandoff(+ 53010, 'ask', 'Click the button', 'Switch to Act mode to click the button.',+ (type, data) => updates.push({ type, data }), {},+ );+ assert.equal(requests.length, 1, `${label}: classifier request missing`);+ assert.equal(requests[0].max_tokens, 24, `${label}: classifier budget changed`);+ assert.deepEqual(requests[0].thinking, { type: 'disabled' }, `${label}: classifier retained native thinking`);+ assert.deepEqual(updates, [{ type: 'ask_mode_handoff', data: { value: 'act' } }], `${label}: handoff event missing`);++ await provider.chat([{ role: 'user', content: 'Continue the task.' }], { maxTokens: 4096 });+ assert.deepEqual(requests[1].thinking, thinking, `${label}: classifier changed thinking for subsequent requests`);+ assert.deepEqual(provider.config.extraBody.thinking, thinking, `${label}: saved thinking configuration changed`);+ }+ }+ }+ } finally {+ globalThis.fetch = originalFetch;+ }+});+test('Ask mode handoff classification is detached from completion', () => {for (const [browserLabel, source] of [['chrome', fs.readFileSync(path.join(ROOT, 'src/chrome/src/agent/agent.js'), 'utf8')],