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
Support auto-only tool choice for OpenRouter Muse Spark
Barack Sokullu523d9f367125
diff --git a/package.json b/package.jsonindex acc70abeb..6c2474371 100644--- a/package.json+++ b/package.json@@ -10,7 +10,7 @@"test:captcha:bridge:ui": "node test/captcha-callback-bridge-ui.mjs","test:billing-notice": "node test/billing-payment-notice.mjs","test:provider-limits": "node test/provider-model-limits.mjs",- "test:openrouter-capabilities": "node --test test/openrouter-model-capabilities.mjs",+ "test:openrouter-capabilities": "node --test test/openrouter-model-capabilities.mjs test/muse-tool-choice.mjs","test:markdown": "node --test test/markdown-render.mjs","test:accessibility-tree-benchmark": "node --test test/llm/lib/accessibility-tree-formats.test.mjs && node test/llm/accessibility-tree-benchmark.mjs --no-exact-tokenizer --check","test:attachment-drop": "node test/attachment-drop.mjs",diff --git a/src/chrome/src/providers/openai.js b/src/chrome/src/providers/openai.jsindex ce09503af..276cba264 100644--- a/src/chrome/src/providers/openai.js+++ b/src/chrome/src/providers/openai.js@@ -11,6 +11,7 @@ import {shouldUseOpenAIResponsesApi,supportsOpenAIAskStreaming,applyOpenRouterRoutingVariant,+ openRouterMuseToolOptions,} from './provider-compatibility.js';import { normalizeRuntimeTraceConfig } from '../trace/runtime-config.js';import { canonicalizeOllamaBaseUrl } from './context-windows.js';@@ -578,6 +579,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {* compatibility presets, and safe extraBody merge.*/_buildChatCompletionsBody(messages, options = {}, stream = false) {+ options = openRouterMuseToolOptions({ ...this.config, baseUrl: this.baseUrl, model: this.model }, options);let body = {messages: this._chatMessages(messages, options),stream,@@ -702,6 +704,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {}_responsesBody(messages, options, stream) {+ options = openRouterMuseToolOptions({ ...this.config, baseUrl: this.baseUrl, model: this.model }, options);let body = {input: this._responsesInput(messages),stream,diff --git a/src/chrome/src/providers/provider-compatibility.js b/src/chrome/src/providers/provider-compatibility.jsindex cee1d9ab3..5b1be99a5 100644--- a/src/chrome/src/providers/provider-compatibility.js+++ b/src/chrome/src/providers/provider-compatibility.js@@ -74,6 +74,22 @@ export const RESERVED_EXTRA_BODY_KEYS = new Set([const UNSAFE_OBJECT_KEYS = new Set(['__proto__', 'prototype', 'constructor']);+/** Muse Spark on OpenRouter accepts only automatic tool selection. Custom+ * imported provider names still speak the same endpoint/model contract. */+export function openRouterMuseToolOptions(config = {}, options = {}) {+ let openRouter = false;+ try { openRouter = new URL(config.baseUrl || '').hostname.toLowerCase() === 'openrouter.ai'; } catch { /* not an OpenRouter endpoint */ }+ const model = String(config.model || '').trim().toLowerCase().replace(OPENROUTER_MODEL_VARIANT_SUFFIXES, '');+ if (!openRouter || model !== 'meta/muse-spark-1.3-contributor') return options;+ if (options.toolChoice === 'none') return { ...options, tools: [], toolChoice: undefined };+ const choice = options.toolChoice;+ const name = choice && typeof choice === 'object' ? choice.function?.name || choice.name : null;+ const tools = name ? (options.tools || []).filter(tool => tool?.function?.name === name || tool?.name === name) : options.tools;+ if (name && !tools.length) throw new Error(`Requested tool '${name}' is not available for Muse Spark.`);+ return { ...options, tools, toolChoice: 'auto' };+}++function clean(value) {return String(value || '').trim().toLowerCase();}diff --git a/src/firefox/src/providers/openai.js b/src/firefox/src/providers/openai.jsindex 77a563347..47d03dfe0 100644--- a/src/firefox/src/providers/openai.js+++ b/src/firefox/src/providers/openai.js@@ -11,6 +11,7 @@ import {shouldUseOpenAIResponsesApi,supportsOpenAIAskStreaming,applyOpenRouterRoutingVariant,+ openRouterMuseToolOptions,} from './provider-compatibility.js';import { normalizeRuntimeTraceConfig } from '../trace/runtime-config.js';import { RESEARCH_DATA_COLLECTION } from '../trace/research-consent.js';@@ -595,6 +596,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {* compatibility presets, and safe extraBody merge.*/_buildChatCompletionsBody(messages, options = {}, stream = false) {+ options = openRouterMuseToolOptions({ ...this.config, baseUrl: this.baseUrl, model: this.model }, options);let body = {messages: this._chatMessages(messages, options),stream,@@ -719,6 +721,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {}_responsesBody(messages, options, stream) {+ options = openRouterMuseToolOptions({ ...this.config, baseUrl: this.baseUrl, model: this.model }, options);let body = {input: this._responsesInput(messages),stream,diff --git a/src/firefox/src/providers/provider-compatibility.js b/src/firefox/src/providers/provider-compatibility.jsindex cee1d9ab3..5b1be99a5 100644--- a/src/firefox/src/providers/provider-compatibility.js+++ b/src/firefox/src/providers/provider-compatibility.js@@ -74,6 +74,22 @@ export const RESERVED_EXTRA_BODY_KEYS = new Set([const UNSAFE_OBJECT_KEYS = new Set(['__proto__', 'prototype', 'constructor']);+/** Muse Spark on OpenRouter accepts only automatic tool selection. Custom+ * imported provider names still speak the same endpoint/model contract. */+export function openRouterMuseToolOptions(config = {}, options = {}) {+ let openRouter = false;+ try { openRouter = new URL(config.baseUrl || '').hostname.toLowerCase() === 'openrouter.ai'; } catch { /* not an OpenRouter endpoint */ }+ const model = String(config.model || '').trim().toLowerCase().replace(OPENROUTER_MODEL_VARIANT_SUFFIXES, '');+ if (!openRouter || model !== 'meta/muse-spark-1.3-contributor') return options;+ if (options.toolChoice === 'none') return { ...options, tools: [], toolChoice: undefined };+ const choice = options.toolChoice;+ const name = choice && typeof choice === 'object' ? choice.function?.name || choice.name : null;+ const tools = name ? (options.tools || []).filter(tool => tool?.function?.name === name || tool?.name === name) : options.tools;+ if (name && !tools.length) throw new Error(`Requested tool '${name}' is not available for Muse Spark.`);+ return { ...options, tools, toolChoice: 'auto' };+}++function clean(value) {return String(value || '').trim().toLowerCase();}diff --git a/test/muse-tool-choice.mjs b/test/muse-tool-choice.mjsnew file mode 100644index 000000000..f57f3d67c--- /dev/null+++ b/test/muse-tool-choice.mjs@@ -0,0 +1,42 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';++for (const browser of ['chrome', 'firefox']) {+ const { OpenAICompatibleProvider } = await import(`../src/${browser}/src/providers/openai.js`);+ const config = { providerName: 'webbrain_me', baseUrl: 'https://openrouter.ai/api/v1', model: 'meta/muse-spark-1.3-contributor' };+ const tools = ['read_page', 'done'].map(name => ({ type: 'function', function: { name, parameters: { type: 'object', properties: {} } } }));+ test(`${browser}: Muse uses auto and preserves restricted tools in chat and streaming`, () => {+ const provider = new OpenAICompatibleProvider(config);+ for (const stream of [false, true]) {+ for (const choice of [undefined, 'auto', 'required']) {+ const body = provider._buildChatCompletionsBody([], { tools, toolChoice: choice }, stream);+ assert.equal(body.tool_choice, 'auto');+ assert.deepEqual(body.tools, tools);+ }+ const body = provider._buildChatCompletionsBody([], { tools, toolChoice: { type: 'function', function: { name: 'done' } } }, stream);+ assert.equal(body.tool_choice, 'auto');+ assert.deepEqual(body.tools, [tools[1]]);+ const none = provider._buildChatCompletionsBody([], { tools, toolChoice: 'none' }, stream);+ assert.equal(Object.hasOwn(none, 'tools'), false);+ assert.equal(Object.hasOwn(none, 'tool_choice'), false);+ }+ assert.throws(() => provider._buildChatCompletionsBody([], { tools, toolChoice: { function: { name: 'missing' } } }), /not available/);+ });+ test(`${browser}: other models/endpoints retain their tool choices`, () => {+ const named = { type: 'function', function: { name: 'done' } };+ for (const other of [{ ...config, model: 'other-model' }, { ...config, baseUrl: 'https://api.example.com/v1' }, { ...config, baseUrl: 'https://openrouter.ai.example.com/v1' }]) {+ const body = new OpenAICompatibleProvider(other)._buildChatCompletionsBody([], { tools, toolChoice: named });+ assert.deepEqual(body.tool_choice, named);+ assert.deepEqual(body.tools, tools);+ }+ const variant = new OpenAICompatibleProvider({ ...config, model: config.model + ':nitro' });+ assert.equal(variant._buildChatCompletionsBody([], { tools, toolChoice: 'required' }).tool_choice, 'auto');+ });+ test(`${browser}: Muse Responses builder applies the same tool restriction`, () => {+ const provider = new OpenAICompatibleProvider(config);+ const body = provider._buildResponsesBody([], { tools, toolChoice: { function: { name: 'done' } } });+ assert.equal(body.tool_choice, 'auto');+ assert.equal(body.tools.length, 1);+ assert.equal(body.tools[0].name, 'done');+ });+}