fix: extend tool argument normalization to all tools and harden edge cases
- Extend STRING_ARGUMENT_TOOL_FIELDS to normalize Read, Write, Edit,
Glob, and Grep plain-string arguments (fixes "Invalid tool parameters"
errors reported by VennDev)
- Normalize streaming Bash args regardless of finish_reason, not only
when finish_reason is 'tool_calls'
- Broaden isLikelyStructuredObjectLiteral to catch malformed object-shaped
strings like {command:"pwd"} and {'command':'pwd'} (fixes CR2 from
Vasanthdev2004)
- Apply blank/object-literal guard to all tools, not just Bash
- Extract duplicated JSON repair suffix combinations into shared constant
- Add 32 isolated unit tests for toolArgumentNormalization
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1420,7 +1420,7 @@ test('does not normalize incomplete streamed Bash commands when finish_reason is
|
|||||||
.map(event => (event.delta as Record<string, unknown>).partial_json)
|
.map(event => (event.delta as Record<string, unknown>).partial_json)
|
||||||
.join('')
|
.join('')
|
||||||
|
|
||||||
expect(streamedInput).toBe('rg --fi')
|
expect(streamedInput).toBe('{"command":"rg --fi"}')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('does not repair truncated Bash objects that do not contain command', async () => {
|
test('does not repair truncated Bash objects that do not contain command', async () => {
|
||||||
|
|||||||
@@ -479,6 +479,10 @@ function convertChunkUsage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const JSON_REPAIR_SUFFIXES = [
|
||||||
|
'}', '"}', ']}', '"]}', '}}', '"}}', ']}}', '"]}}', '"]}]}', '}]}'
|
||||||
|
]
|
||||||
|
|
||||||
function repairPossiblyTruncatedObjectJson(raw: string): string | null {
|
function repairPossiblyTruncatedObjectJson(raw: string): string | null {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(raw)
|
const parsed = JSON.parse(raw)
|
||||||
@@ -486,19 +490,7 @@ function repairPossiblyTruncatedObjectJson(raw: string): string | null {
|
|||||||
? raw
|
? raw
|
||||||
: null
|
: null
|
||||||
} catch {
|
} catch {
|
||||||
const combinations = [
|
for (const combo of JSON_REPAIR_SUFFIXES) {
|
||||||
'}',
|
|
||||||
'"}',
|
|
||||||
']}',
|
|
||||||
'"]}',
|
|
||||||
'}}',
|
|
||||||
'"}}',
|
|
||||||
']}}',
|
|
||||||
'"]}}',
|
|
||||||
'"]}]}',
|
|
||||||
'}]}',
|
|
||||||
]
|
|
||||||
for (const combo of combinations) {
|
|
||||||
try {
|
try {
|
||||||
const repaired = raw + combo
|
const repaired = raw + combo
|
||||||
const parsed = JSON.parse(repaired)
|
const parsed = JSON.parse(repaired)
|
||||||
@@ -700,11 +692,10 @@ async function* openaiStreamToAnthropic(
|
|||||||
// Close active tool calls
|
// Close active tool calls
|
||||||
for (const [, tc] of activeToolCalls) {
|
for (const [, tc] of activeToolCalls) {
|
||||||
if (tc.normalizeAtStop) {
|
if (tc.normalizeAtStop) {
|
||||||
let partialJson = tc.jsonBuffer
|
|
||||||
if (choice.finish_reason === 'tool_calls') {
|
|
||||||
const repairedStructuredJson = repairPossiblyTruncatedObjectJson(
|
const repairedStructuredJson = repairPossiblyTruncatedObjectJson(
|
||||||
tc.jsonBuffer,
|
tc.jsonBuffer,
|
||||||
)
|
)
|
||||||
|
let partialJson: string
|
||||||
if (repairedStructuredJson) {
|
if (repairedStructuredJson) {
|
||||||
partialJson = repairedStructuredJson
|
partialJson = repairedStructuredJson
|
||||||
} else {
|
} else {
|
||||||
@@ -712,7 +703,6 @@ async function* openaiStreamToAnthropic(
|
|||||||
normalizeToolArguments(tc.name, tc.jsonBuffer),
|
normalizeToolArguments(tc.name, tc.jsonBuffer),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: 'content_block_delta',
|
type: 'content_block_delta',
|
||||||
@@ -732,10 +722,7 @@ async function* openaiStreamToAnthropic(
|
|||||||
JSON.parse(tc.jsonBuffer)
|
JSON.parse(tc.jsonBuffer)
|
||||||
} catch {
|
} catch {
|
||||||
const str = tc.jsonBuffer.trimEnd()
|
const str = tc.jsonBuffer.trimEnd()
|
||||||
const combinations = [
|
for (const combo of JSON_REPAIR_SUFFIXES) {
|
||||||
'}', '"}', ']}', '"]}', '}}', '"}}', ']}}', '"]}}', '"]}]}', '}]}'
|
|
||||||
]
|
|
||||||
for (const combo of combinations) {
|
|
||||||
try {
|
try {
|
||||||
JSON.parse(str + combo)
|
JSON.parse(str + combo)
|
||||||
suffixToAdd = combo
|
suffixToAdd = combo
|
||||||
|
|||||||
184
src/services/api/toolArgumentNormalization.test.ts
Normal file
184
src/services/api/toolArgumentNormalization.test.ts
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
import { normalizeToolArguments } from './toolArgumentNormalization'
|
||||||
|
|
||||||
|
describe('normalizeToolArguments', () => {
|
||||||
|
describe('Bash tool', () => {
|
||||||
|
test('wraps plain string into { command }', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', 'pwd')).toEqual({ command: 'pwd' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('wraps multi-word command', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', 'ls -la /tmp')).toEqual({
|
||||||
|
command: 'ls -la /tmp',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Bash', '{"command":"echo hi"}'),
|
||||||
|
).toEqual({ command: 'echo hi' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns { raw } for blank string', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '')).toEqual({ raw: '' })
|
||||||
|
expect(normalizeToolArguments('Bash', ' ')).toEqual({ raw: ' ' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns { raw } for JSON-encoded blank string', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '""')).toEqual({ raw: '' })
|
||||||
|
expect(normalizeToolArguments('Bash', '" "')).toEqual({ raw: ' ' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns { raw } for likely structured object literal that fails parse', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '{ "command": "pwd"')).toEqual({
|
||||||
|
raw: '{ "command": "pwd"',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['{command:"pwd"}'],
|
||||||
|
["{'command':'pwd'}"],
|
||||||
|
['{command: pwd}'],
|
||||||
|
])(
|
||||||
|
'returns { raw } for malformed object-shaped string %s (does not wrap into command)',
|
||||||
|
(input) => {
|
||||||
|
expect(normalizeToolArguments('Bash', input)).toEqual({ raw: input })
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['false', false],
|
||||||
|
['null', null],
|
||||||
|
['[]', [] as unknown[]],
|
||||||
|
['0', 0],
|
||||||
|
['true', true],
|
||||||
|
['123', 123],
|
||||||
|
])(
|
||||||
|
'preserves JSON literal %s as-is (does not wrap into command)',
|
||||||
|
(input, expected) => {
|
||||||
|
expect(normalizeToolArguments('Bash', input)).toEqual(expected)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
test('wraps JSON-encoded string into { command }', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '"pwd"')).toEqual({
|
||||||
|
command: 'pwd',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('undefined arguments', () => {
|
||||||
|
test('returns empty object for undefined', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', undefined)).toEqual({})
|
||||||
|
expect(normalizeToolArguments('UnknownTool', undefined)).toEqual({})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Read tool', () => {
|
||||||
|
test('wraps plain string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Read', '/home/user/file.txt')).toEqual({
|
||||||
|
file_path: '/home/user/file.txt',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('wraps JSON-encoded string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Read', '"/home/user/file.txt"')).toEqual({
|
||||||
|
file_path: '/home/user/file.txt',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Read', '{"file_path":"/tmp/f.txt","limit":10}'),
|
||||||
|
).toEqual({ file_path: '/tmp/f.txt', limit: 10 })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Write tool', () => {
|
||||||
|
test('wraps plain string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Write', '/tmp/out.txt')).toEqual({
|
||||||
|
file_path: '/tmp/out.txt',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments(
|
||||||
|
'Write',
|
||||||
|
'{"file_path":"/tmp/out.txt","content":"hello"}',
|
||||||
|
),
|
||||||
|
).toEqual({ file_path: '/tmp/out.txt', content: 'hello' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Edit tool', () => {
|
||||||
|
test('wraps plain string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Edit', '/tmp/edit.ts')).toEqual({
|
||||||
|
file_path: '/tmp/edit.ts',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments(
|
||||||
|
'Edit',
|
||||||
|
'{"file_path":"/tmp/f.ts","old_string":"a","new_string":"b"}',
|
||||||
|
),
|
||||||
|
).toEqual({ file_path: '/tmp/f.ts', old_string: 'a', new_string: 'b' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Glob tool', () => {
|
||||||
|
test('wraps plain string into { pattern }', () => {
|
||||||
|
expect(normalizeToolArguments('Glob', '**/*.ts')).toEqual({
|
||||||
|
pattern: '**/*.ts',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Glob', '{"pattern":"*.js","path":"/src"}'),
|
||||||
|
).toEqual({ pattern: '*.js', path: '/src' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Grep tool', () => {
|
||||||
|
test('wraps plain string into { pattern }', () => {
|
||||||
|
expect(normalizeToolArguments('Grep', 'TODO')).toEqual({
|
||||||
|
pattern: 'TODO',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Grep', '{"pattern":"fixme","path":"/src"}'),
|
||||||
|
).toEqual({ pattern: 'fixme', path: '/src' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('unknown tools', () => {
|
||||||
|
test('returns { raw } for plain string', () => {
|
||||||
|
expect(normalizeToolArguments('UnknownTool', 'some value')).toEqual({
|
||||||
|
raw: 'some value',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('UnknownTool', '{"key":"val"}'),
|
||||||
|
).toEqual({ key: 'val' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves JSON literals as-is', () => {
|
||||||
|
expect(normalizeToolArguments('UnknownTool', 'false')).toEqual(false)
|
||||||
|
expect(normalizeToolArguments('UnknownTool', 'null')).toEqual(null)
|
||||||
|
expect(normalizeToolArguments('UnknownTool', '[]')).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns JSON-encoded string as parsed string for unknown tools', () => {
|
||||||
|
expect(normalizeToolArguments('UnknownTool', '"hello"')).toEqual(
|
||||||
|
'hello',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
const STRING_ARGUMENT_TOOL_FIELDS: Record<string, string> = {
|
const STRING_ARGUMENT_TOOL_FIELDS: Record<string, string> = {
|
||||||
Bash: 'command',
|
Bash: 'command',
|
||||||
|
Read: 'file_path',
|
||||||
|
Write: 'file_path',
|
||||||
|
Edit: 'file_path',
|
||||||
|
Glob: 'pattern',
|
||||||
|
Grep: 'pattern',
|
||||||
}
|
}
|
||||||
|
|
||||||
function isBlankString(value: string): boolean {
|
function isBlankString(value: string): boolean {
|
||||||
@@ -7,7 +12,10 @@ function isBlankString(value: string): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isLikelyStructuredObjectLiteral(value: string): boolean {
|
function isLikelyStructuredObjectLiteral(value: string): boolean {
|
||||||
return /^\s*\{\s*"[^"\\]+"\s*:/.test(value)
|
// Match object-like patterns with key-value syntax:
|
||||||
|
// {"key":, {key:, {'key':, { "key" :, etc.
|
||||||
|
// But NOT bash compound commands like { pwd; } or { echo hi; }
|
||||||
|
return /^\s*\{\s*['"]?\w+['"]?\s*:/.test(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
@@ -52,11 +60,9 @@ export function normalizeToolArguments(
|
|||||||
}
|
}
|
||||||
return parsed
|
return parsed
|
||||||
} catch {
|
} catch {
|
||||||
if (toolName === 'Bash') {
|
|
||||||
if (isBlankString(rawArguments) || isLikelyStructuredObjectLiteral(rawArguments)) {
|
if (isBlankString(rawArguments) || isLikelyStructuredObjectLiteral(rawArguments)) {
|
||||||
return { raw: rawArguments }
|
return { raw: rawArguments }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
wrapPlainStringToolArguments(toolName, rawArguments) ?? { raw: rawArguments }
|
wrapPlainStringToolArguments(toolName, rawArguments) ?? { raw: rawArguments }
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user