|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { |
| 3 | + ToolCallManager, |
| 4 | + executeToolCalls, |
| 5 | +} from '../src/activities/chat/tools/tool-calls' |
| 6 | +import type { Tool, ToolCall } from '../src/types' |
| 7 | + |
| 8 | +/** |
| 9 | + * Drain an async generator and return its final return value. |
| 10 | + */ |
| 11 | +async function drainGenerator<TChunk, TResult>( |
| 12 | + gen: AsyncGenerator<TChunk, TResult, void>, |
| 13 | +): Promise<TResult> { |
| 14 | + while (true) { |
| 15 | + const next = await gen.next() |
| 16 | + if (next.done) return next.value |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('null tool input normalization', () => { |
| 21 | + describe('executeToolCalls', () => { |
| 22 | + it('should normalize "null" arguments to empty object', async () => { |
| 23 | + const receivedInput = vi.fn() |
| 24 | + |
| 25 | + const tool: Tool = { |
| 26 | + name: 'test_tool', |
| 27 | + description: 'test', |
| 28 | + execute: async (input: unknown) => { |
| 29 | + receivedInput(input) |
| 30 | + return { ok: true } |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + const toolCalls: Array<ToolCall> = [ |
| 35 | + { |
| 36 | + id: 'tc-1', |
| 37 | + type: 'function', |
| 38 | + function: { name: 'test_tool', arguments: 'null' }, |
| 39 | + }, |
| 40 | + ] |
| 41 | + |
| 42 | + const result = await drainGenerator(executeToolCalls(toolCalls, [tool])) |
| 43 | + expect(receivedInput).toHaveBeenCalledWith({}) |
| 44 | + expect(result.results).toHaveLength(1) |
| 45 | + expect(result.results[0]!.state).toBeUndefined() |
| 46 | + }) |
| 47 | + |
| 48 | + it('should normalize empty arguments to empty object', async () => { |
| 49 | + const receivedInput = vi.fn() |
| 50 | + |
| 51 | + const tool: Tool = { |
| 52 | + name: 'test_tool', |
| 53 | + description: 'test', |
| 54 | + execute: async (input: unknown) => { |
| 55 | + receivedInput(input) |
| 56 | + return { ok: true } |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + const toolCalls: Array<ToolCall> = [ |
| 61 | + { |
| 62 | + id: 'tc-1', |
| 63 | + type: 'function', |
| 64 | + function: { name: 'test_tool', arguments: '' }, |
| 65 | + }, |
| 66 | + ] |
| 67 | + |
| 68 | + await drainGenerator(executeToolCalls(toolCalls, [tool])) |
| 69 | + expect(receivedInput).toHaveBeenCalledWith({}) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should pass through valid object arguments unchanged', async () => { |
| 73 | + const receivedInput = vi.fn() |
| 74 | + |
| 75 | + const tool: Tool = { |
| 76 | + name: 'test_tool', |
| 77 | + description: 'test', |
| 78 | + execute: async (input: unknown) => { |
| 79 | + receivedInput(input) |
| 80 | + return { ok: true } |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + const toolCalls: Array<ToolCall> = [ |
| 85 | + { |
| 86 | + id: 'tc-1', |
| 87 | + type: 'function', |
| 88 | + function: { |
| 89 | + name: 'test_tool', |
| 90 | + arguments: '{"location":"NYC"}', |
| 91 | + }, |
| 92 | + }, |
| 93 | + ] |
| 94 | + |
| 95 | + await drainGenerator(executeToolCalls(toolCalls, [tool])) |
| 96 | + expect(receivedInput).toHaveBeenCalledWith({ location: 'NYC' }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('ToolCallManager.completeToolCall', () => { |
| 101 | + it('should normalize null input to empty object', () => { |
| 102 | + const manager = new ToolCallManager([]) |
| 103 | + |
| 104 | + // Register a tool call |
| 105 | + manager.addToolCallStartEvent({ |
| 106 | + type: 'TOOL_CALL_START', |
| 107 | + toolCallId: 'tc-1', |
| 108 | + toolName: 'test_tool', |
| 109 | + model: 'test', |
| 110 | + timestamp: Date.now(), |
| 111 | + index: 0, |
| 112 | + }) |
| 113 | + |
| 114 | + // Complete with null input (simulating Anthropic empty tool_use) |
| 115 | + manager.completeToolCall({ |
| 116 | + type: 'TOOL_CALL_END', |
| 117 | + toolCallId: 'tc-1', |
| 118 | + toolName: 'test_tool', |
| 119 | + model: 'test', |
| 120 | + timestamp: Date.now(), |
| 121 | + input: null as unknown, |
| 122 | + }) |
| 123 | + |
| 124 | + const toolCalls = manager.getToolCalls() |
| 125 | + expect(toolCalls).toHaveLength(1) |
| 126 | + // Should be "{}" not "null" |
| 127 | + expect(toolCalls[0]!.function.arguments).toBe('{}') |
| 128 | + }) |
| 129 | + |
| 130 | + it('should preserve valid object input', () => { |
| 131 | + const manager = new ToolCallManager([]) |
| 132 | + |
| 133 | + manager.addToolCallStartEvent({ |
| 134 | + type: 'TOOL_CALL_START', |
| 135 | + toolCallId: 'tc-1', |
| 136 | + toolName: 'test_tool', |
| 137 | + model: 'test', |
| 138 | + timestamp: Date.now(), |
| 139 | + index: 0, |
| 140 | + }) |
| 141 | + |
| 142 | + manager.completeToolCall({ |
| 143 | + type: 'TOOL_CALL_END', |
| 144 | + toolCallId: 'tc-1', |
| 145 | + toolName: 'test_tool', |
| 146 | + model: 'test', |
| 147 | + timestamp: Date.now(), |
| 148 | + input: { location: 'NYC' }, |
| 149 | + }) |
| 150 | + |
| 151 | + const toolCalls = manager.getToolCalls() |
| 152 | + expect(toolCalls[0]!.function.arguments).toBe('{"location":"NYC"}') |
| 153 | + }) |
| 154 | + }) |
| 155 | +}) |
0 commit comments