Skip to content

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

fix: reject bare MiniCPM function wrappers containing prose inside the body

Emre Sokullu6c23e8d9fb28
diff --git a/src/chrome/src/agent/tool-call-parser.js b/src/chrome/src/agent/tool-call-parser.js
index 42e687c4c..fb8282d11 100644
--- a/src/chrome/src/agent/tool-call-parser.js
+++ b/src/chrome/src/agent/tool-call-parser.js
@@ -485,13 +485,28 @@ export function parseToolCallsFromText(text, allowedNames) {
if (!allowedNames.has(toolName)) continue;
const body = minicpmMatch[3] || '';
const args = {};
+ const paramSpans = [];
const paramRe = /<(?:param|parameter)(?:\s*=\s*["']?([A-Za-z_]\w*)["']?|\s+name\s*=\s*["']?([A-Za-z_]\w*)["']?)\s*>\s*([\s\S]*?)\s*<\/(?:param|parameter)>/gi;
let paramMatch;
while ((paramMatch = paramRe.exec(body)) !== null) {
+ paramSpans.push({ start: paramMatch.index, end: paramMatch.index + paramMatch[0].length });
const key = paramMatch[1] || paramMatch[2];
if (!key) continue;
args[key] = parseXmlParamValue(paramMatch[3]);
}
+ // A dispatch discards the wrapper's prose outright (the caller sets
+ // result.content = null), so the body must be nothing but recognized
+ // parameters and whitespace. A warning inside the wrapper such as
+ // `<param ...>ref_7</param>Do not execute this example.` must reject the
+ // whole block rather than being excused by the remainder check below.
+ let bodyRemainder = '';
+ let bodyCursor = 0;
+ for (const span of paramSpans) {
+ if (span.start > bodyCursor) bodyRemainder += body.slice(bodyCursor, span.start);
+ bodyCursor = Math.max(bodyCursor, span.end);
+ }
+ bodyRemainder += body.slice(bodyCursor);
+ if (bodyRemainder.trim() !== '') continue;
minicpmCandidates.push({
start: minicpmMatch.index,
end: minicpmMatch.index + minicpmMatch[0].length,
diff --git a/src/firefox/src/agent/tool-call-parser.js b/src/firefox/src/agent/tool-call-parser.js
index 42e687c4c..fb8282d11 100644
--- a/src/firefox/src/agent/tool-call-parser.js
+++ b/src/firefox/src/agent/tool-call-parser.js
@@ -485,13 +485,28 @@ export function parseToolCallsFromText(text, allowedNames) {
if (!allowedNames.has(toolName)) continue;
const body = minicpmMatch[3] || '';
const args = {};
+ const paramSpans = [];
const paramRe = /<(?:param|parameter)(?:\s*=\s*["']?([A-Za-z_]\w*)["']?|\s+name\s*=\s*["']?([A-Za-z_]\w*)["']?)\s*>\s*([\s\S]*?)\s*<\/(?:param|parameter)>/gi;
let paramMatch;
while ((paramMatch = paramRe.exec(body)) !== null) {
+ paramSpans.push({ start: paramMatch.index, end: paramMatch.index + paramMatch[0].length });
const key = paramMatch[1] || paramMatch[2];
if (!key) continue;
args[key] = parseXmlParamValue(paramMatch[3]);
}
+ // A dispatch discards the wrapper's prose outright (the caller sets
+ // result.content = null), so the body must be nothing but recognized
+ // parameters and whitespace. A warning inside the wrapper such as
+ // `<param ...>ref_7</param>Do not execute this example.` must reject the
+ // whole block rather than being excused by the remainder check below.
+ let bodyRemainder = '';
+ let bodyCursor = 0;
+ for (const span of paramSpans) {
+ if (span.start > bodyCursor) bodyRemainder += body.slice(bodyCursor, span.start);
+ bodyCursor = Math.max(bodyCursor, span.end);
+ }
+ bodyRemainder += body.slice(bodyCursor);
+ if (bodyRemainder.trim() !== '') continue;
minicpmCandidates.push({
start: minicpmMatch.index,
end: minicpmMatch.index + minicpmMatch[0].length,
diff --git a/test/run.js b/test/run.js
index 07b13af98..311911447 100644
--- a/test/run.js
+++ b/test/run.js
@@ -100728,6 +100728,21 @@ test('text tool-call parser is production code with format and allowlist coverag
{ name: 'click_ax', args: { ref_id: 'ref_7' } },
],
},
+ {
+ label: 'MiniCPM5 bare function rejects prose after params',
+ raw: '<function name="click_ax"><param name="ref_id">ref_7</param>Do not execute this example.</function>',
+ expected: [],
+ },
+ {
+ label: 'MiniCPM5 bare function rejects prose before params',
+ raw: '<function name="click_ax">Do not execute.<param name="ref_id">ref_7</param></function>',
+ expected: [],
+ },
+ {
+ label: 'MiniCPM5 bare function rejects prose between params',
+ raw: '<function name="click_ax"><param name="ref_id">ref_7</param>Do not execute this example.<param name="force">true</param></function>',
+ expected: [],
+ },
];
for (const parser of [ToolCallParserCh, ToolCallParserFx]) {