diff --git a/src/services/api/openaiShim.test.ts b/src/services/api/openaiShim.test.ts index b47d84f6..e84ff27c 100644 --- a/src/services/api/openaiShim.test.ts +++ b/src/services/api/openaiShim.test.ts @@ -1420,7 +1420,7 @@ test('does not normalize incomplete streamed Bash commands when finish_reason is .map(event => (event.delta as Record).partial_json) .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 () => { diff --git a/src/services/api/openaiShim.ts b/src/services/api/openaiShim.ts index 8f8247b9..c781563c 100644 --- a/src/services/api/openaiShim.ts +++ b/src/services/api/openaiShim.ts @@ -479,6 +479,10 @@ function convertChunkUsage( } } +const JSON_REPAIR_SUFFIXES = [ + '}', '"}', ']}', '"]}', '}}', '"}}', ']}}', '"]}}', '"]}]}', '}]}' +] + function repairPossiblyTruncatedObjectJson(raw: string): string | null { try { const parsed = JSON.parse(raw) @@ -486,19 +490,7 @@ function repairPossiblyTruncatedObjectJson(raw: string): string | null { ? raw : null } catch { - const combinations = [ - '}', - '"}', - ']}', - '"]}', - '}}', - '"}}', - ']}}', - '"]}}', - '"]}]}', - '}]}', - ] - for (const combo of combinations) { + for (const combo of JSON_REPAIR_SUFFIXES) { try { const repaired = raw + combo const parsed = JSON.parse(repaired) @@ -700,18 +692,16 @@ async function* openaiStreamToAnthropic( // Close active tool calls for (const [, tc] of activeToolCalls) { if (tc.normalizeAtStop) { - let partialJson = tc.jsonBuffer - if (choice.finish_reason === 'tool_calls') { - const repairedStructuredJson = repairPossiblyTruncatedObjectJson( - tc.jsonBuffer, + const repairedStructuredJson = repairPossiblyTruncatedObjectJson( + tc.jsonBuffer, + ) + let partialJson: string + if (repairedStructuredJson) { + partialJson = repairedStructuredJson + } else { + partialJson = JSON.stringify( + normalizeToolArguments(tc.name, tc.jsonBuffer), ) - if (repairedStructuredJson) { - partialJson = repairedStructuredJson - } else { - partialJson = JSON.stringify( - normalizeToolArguments(tc.name, tc.jsonBuffer), - ) - } } yield { @@ -732,10 +722,7 @@ async function* openaiStreamToAnthropic( JSON.parse(tc.jsonBuffer) } catch { const str = tc.jsonBuffer.trimEnd() - const combinations = [ - '}', '"}', ']}', '"]}', '}}', '"}}', ']}}', '"]}}', '"]}]}', '}]}' - ] - for (const combo of combinations) { + for (const combo of JSON_REPAIR_SUFFIXES) { try { JSON.parse(str + combo) suffixToAdd = combo diff --git a/src/services/api/toolArgumentNormalization.test.ts b/src/services/api/toolArgumentNormalization.test.ts new file mode 100644 index 00000000..9f522b5e --- /dev/null +++ b/src/services/api/toolArgumentNormalization.test.ts @@ -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', + ) + }) + }) +}) diff --git a/src/services/api/toolArgumentNormalization.ts b/src/services/api/toolArgumentNormalization.ts index 82106569..ecfa3f48 100644 --- a/src/services/api/toolArgumentNormalization.ts +++ b/src/services/api/toolArgumentNormalization.ts @@ -1,5 +1,10 @@ const STRING_ARGUMENT_TOOL_FIELDS: Record = { Bash: 'command', + Read: 'file_path', + Write: 'file_path', + Edit: 'file_path', + Glob: 'pattern', + Grep: 'pattern', } function isBlankString(value: string): boolean { @@ -7,7 +12,10 @@ function isBlankString(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 { @@ -52,10 +60,8 @@ export function normalizeToolArguments( } return parsed } catch { - if (toolName === 'Bash') { - if (isBlankString(rawArguments) || isLikelyStructuredObjectLiteral(rawArguments)) { - return { raw: rawArguments } - } + if (isBlankString(rawArguments) || isLikelyStructuredObjectLiteral(rawArguments)) { + return { raw: rawArguments } } return ( wrapPlainStringToolArguments(toolName, rawArguments) ?? { raw: rawArguments }