Skip to content

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

fix: require bare MiniCPM function calls to stand alone before dispatch

Emre Sokullu65168cc8ee73
diff --git a/src/chrome/src/agent/tool-call-parser.js b/src/chrome/src/agent/tool-call-parser.js
index 42d680b97..4b41149d2 100644
--- a/src/chrome/src/agent/tool-call-parser.js
+++ b/src/chrome/src/agent/tool-call-parser.js
@@ -465,6 +465,13 @@ export function parseToolCallsFromText(text, allowedNames) {
while ((minicpmMatch = minicpmFunctionRe.exec(text)) !== null) {
// Skip functions already consumed inside a <tool_call> wrapper above.
if (xmlToolSpans.some(span => minicpmMatch.index >= span.start && minicpmMatch.index < span.end)) continue;
+ // A bare call replaces the model's prose outright, so only accept it on
+ // the same standalone-line boundary the JSON fallback requires. This
+ // keeps quoted/explanatory markup such as `Do not call <function ...>`
+ // from dispatching while still allowing genuine calls emitted on their
+ // own lines.
+ const matchEnd = minicpmMatch.index + minicpmMatch[0].length - 1;
+ if (!standsAloneOnLine(text, minicpmMatch.index, matchEnd)) continue;
const toolName = minicpmMatch[1] || minicpmMatch[2];
if (!allowedNames.has(toolName)) continue;
const body = minicpmMatch[3] || '';
diff --git a/src/firefox/src/agent/tool-call-parser.js b/src/firefox/src/agent/tool-call-parser.js
index 42d680b97..4b41149d2 100644
--- a/src/firefox/src/agent/tool-call-parser.js
+++ b/src/firefox/src/agent/tool-call-parser.js
@@ -465,6 +465,13 @@ export function parseToolCallsFromText(text, allowedNames) {
while ((minicpmMatch = minicpmFunctionRe.exec(text)) !== null) {
// Skip functions already consumed inside a <tool_call> wrapper above.
if (xmlToolSpans.some(span => minicpmMatch.index >= span.start && minicpmMatch.index < span.end)) continue;
+ // A bare call replaces the model's prose outright, so only accept it on
+ // the same standalone-line boundary the JSON fallback requires. This
+ // keeps quoted/explanatory markup such as `Do not call <function ...>`
+ // from dispatching while still allowing genuine calls emitted on their
+ // own lines.
+ const matchEnd = minicpmMatch.index + minicpmMatch[0].length - 1;
+ if (!standsAloneOnLine(text, minicpmMatch.index, matchEnd)) continue;
const toolName = minicpmMatch[1] || minicpmMatch[2];
if (!allowedNames.has(toolName)) continue;
const body = minicpmMatch[3] || '';
diff --git a/test/run.js b/test/run.js
index f8020d18d..3dd313dd6 100644
--- a/test/run.js
+++ b/test/run.js
@@ -100787,6 +100787,8 @@ test('text tool-call parser is production code with format and allowlist coverag
['inline array after prose', 'Options: [{"name":"click","arguments":{"text":"Yes"}},{"name":"navigate","arguments":{"url":"https://a.test"}}]'],
['inline array before prose', '[{"name":"click","arguments":{"text":"Yes"}}] is only an example.'],
['array on a labeled response line', 'Options:\n[{"name":"click","arguments":{"text":"Yes"}}]'],
+ ['bare function inline warning', 'Do not call <function name="click_ax"><param name="ref_id">ref_7</param></function> here.'],
+ ['bare function on a labeled line', 'Option A: <function name="click_ax"><param name="ref_id">ref_7</param></function>'],
]) {
assert.deepEqual(
parser.parseToolCallsFromText(narrated, allowed),