Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/chrome/src/agent/tool-call-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,42 @@
* Parse common text tool-call formats into OpenAI-style tool call objects.
* Only names in allowedNames are accepted.
*/
function extractBalancedJsonObjects(text) {
const objects = [];
let start = -1;
let depth = 0;
let inString = false;
let escaped = false;

for (let i = 0; i < text.length; i++) {
const char = text[i];
if (start < 0) {
if (char === '{') {
start = i;
depth = 1;
}
continue;
}
if (inString) {
if (escaped) escaped = false;
else if (char === '\\') escaped = true;
else if (char === '"') inString = false;
continue;
}
if (char === '"') inString = true;
else if (char === '{') depth++;
else if (char === '}') {
depth--;
if (depth === 0) {
objects.push(text.slice(start, i + 1));
start = -1;
}
}
}

return objects;
}

export function parseToolCallsFromText(text, allowedNames) {
if (!text || text.length > 10000) return [];

Expand Down Expand Up @@ -78,12 +114,9 @@ export function parseToolCallsFromText(text, allowedNames) {
}

if (results.length === 0) {
const bareRe = /\{[^{}]*"name"\s*:\s*"(\w+)"[^{}]*\}/g;
let match;
while ((match = bareRe.exec(text)) !== null) {
if (!allowedNames.has(match[1])) continue;
for (const candidate of extractBalancedJsonObjects(text)) {
try {
const obj = JSON.parse(match[0]);
const obj = JSON.parse(candidate);
if (obj && obj.name && allowedNames.has(obj.name)) {
results.push(obj);
}
Expand Down
43 changes: 38 additions & 5 deletions src/firefox/src/agent/tool-call-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,42 @@
* Parse common text tool-call formats into OpenAI-style tool call objects.
* Only names in allowedNames are accepted.
*/
function extractBalancedJsonObjects(text) {
const objects = [];
let start = -1;
let depth = 0;
let inString = false;
let escaped = false;

for (let i = 0; i < text.length; i++) {
const char = text[i];
if (start < 0) {
if (char === '{') {
start = i;
depth = 1;
}
continue;
}
if (inString) {
if (escaped) escaped = false;
else if (char === '\\') escaped = true;
else if (char === '"') inString = false;
continue;
}
if (char === '"') inString = true;
else if (char === '{') depth++;
else if (char === '}') {
depth--;
if (depth === 0) {
objects.push(text.slice(start, i + 1));
start = -1;
}
}
}

return objects;
}

export function parseToolCallsFromText(text, allowedNames) {
if (!text || text.length > 10000) return [];

Expand Down Expand Up @@ -78,12 +114,9 @@ export function parseToolCallsFromText(text, allowedNames) {
}

if (results.length === 0) {
const bareRe = /\{[^{}]*"name"\s*:\s*"(\w+)"[^{}]*\}/g;
let match;
while ((match = bareRe.exec(text)) !== null) {
if (!allowedNames.has(match[1])) continue;
for (const candidate of extractBalancedJsonObjects(text)) {
try {
const obj = JSON.parse(match[0]);
const obj = JSON.parse(candidate);
if (obj && obj.name && allowedNames.has(obj.name)) {
results.push(obj);
}
Expand Down
23 changes: 23 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -49333,6 +49333,29 @@ test('text tool-call parser is production code with format and allowlist coverag
raw: '{"name":"read_page","arguments":"[]"}',
expected: [{ name: 'read_page', args: [] }],
},
{
label: 'bare JSON with nested arguments and braces in strings',
raw: [
'I will use the save button now.',
JSON.stringify({
name: 'click',
arguments: { text: 'Save "{draft}"', meta: { source: 'dialog' } },
}),
].join('\n'),
expected: [{ name: 'click', args: { text: 'Save "{draft}"', meta: { source: 'dialog' } } }],
},
{
label: 'multiple bare JSON calls preserve order',
raw: [
'{"name":"read_page","arguments":{"selector":{"role":"main"}}}',
'then',
'{"name":"click_ax","arguments":{"target":{"ref_id":"ref_7"}}}',
].join('\n'),
expected: [
{ name: 'read_page', args: { selector: { role: 'main' } } },
{ name: 'click_ax', args: { target: { ref_id: 'ref_7' } } },
],
},
{
label: 'XML typed parameters',
raw: [
Expand Down
Loading