|
| 1 | +// npx vitest src/core/assistant-message/kilocode/__tests__/AssistantMessageParser.spec.ts |
| 2 | + |
| 3 | +import { AssistantMessageParser } from "../../AssistantMessageParser" |
| 4 | +import { ToolUse } from "../../../../shared/tools" |
| 5 | + |
| 6 | +describe("AssistantMessageParser (streaming)", () => { |
| 7 | + let parser: AssistantMessageParser |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + parser = new AssistantMessageParser() |
| 11 | + }) |
| 12 | + describe("AssistantMessageParser (native tool calls)", () => { |
| 13 | + let parser: AssistantMessageParser |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + parser = new AssistantMessageParser() |
| 17 | + }) |
| 18 | + |
| 19 | + describe("dynamic MCP tool name handling", () => { |
| 20 | + it("should normalize dynamic MCP tool names to use_mcp_tool", () => { |
| 21 | + const toolCalls = [ |
| 22 | + { |
| 23 | + id: "call_123", |
| 24 | + type: "function" as const, |
| 25 | + function: { |
| 26 | + name: "use_mcp_tool___context7___get-library-docs", |
| 27 | + arguments: JSON.stringify({ |
| 28 | + toolInputProps: { |
| 29 | + context7CompatibleLibraryID: "/vercel/next.js", |
| 30 | + topic: "routing", |
| 31 | + }, |
| 32 | + }), |
| 33 | + }, |
| 34 | + }, |
| 35 | + ] |
| 36 | + |
| 37 | + parser.processNativeToolCalls(toolCalls) |
| 38 | + const blocks = parser.getContentBlocks() |
| 39 | + |
| 40 | + expect(blocks).toHaveLength(1) |
| 41 | + const toolUse = blocks[0] as ToolUse |
| 42 | + expect(toolUse.type).toBe("tool_use") |
| 43 | + expect(toolUse.name).toBe("use_mcp_tool") |
| 44 | + expect(toolUse.params.server_name).toBe("context7") |
| 45 | + expect(toolUse.params.tool_name).toBe("get-library-docs") |
| 46 | + // Verify arguments contains the toolInputProps as a JSON string |
| 47 | + expect(toolUse.params.arguments).toBeDefined() |
| 48 | + const parsedArgs = JSON.parse(toolUse.params.arguments!) |
| 49 | + expect(parsedArgs.context7CompatibleLibraryID).toBe("/vercel/next.js") |
| 50 | + expect(parsedArgs.topic).toBe("routing") |
| 51 | + expect(toolUse.partial).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + it("should handle dynamic MCP tool names with underscores in tool name", () => { |
| 55 | + const toolCalls = [ |
| 56 | + { |
| 57 | + id: "call_456", |
| 58 | + type: "function" as const, |
| 59 | + function: { |
| 60 | + name: "use_mcp_tool___myserver___get_user_data", |
| 61 | + arguments: JSON.stringify({ |
| 62 | + toolInputProps: { |
| 63 | + userId: "123", |
| 64 | + }, |
| 65 | + }), |
| 66 | + }, |
| 67 | + }, |
| 68 | + ] |
| 69 | + |
| 70 | + parser.processNativeToolCalls(toolCalls) |
| 71 | + const blocks = parser.getContentBlocks() |
| 72 | + |
| 73 | + expect(blocks).toHaveLength(1) |
| 74 | + const toolUse = blocks[0] as ToolUse |
| 75 | + expect(toolUse.name).toBe("use_mcp_tool") |
| 76 | + expect(toolUse.params.server_name).toBe("myserver") |
| 77 | + expect(toolUse.params.tool_name).toBe("get_user_data") |
| 78 | + // Verify arguments contains the toolInputProps as a JSON string |
| 79 | + const parsedArgs = JSON.parse(toolUse.params.arguments!) |
| 80 | + expect(parsedArgs.userId).toBe("123") |
| 81 | + }) |
| 82 | + |
| 83 | + it("should reject malformed dynamic MCP tool names (no triple underscore separator)", () => { |
| 84 | + const toolCalls = [ |
| 85 | + { |
| 86 | + id: "call_789", |
| 87 | + type: "function" as const, |
| 88 | + function: { |
| 89 | + name: "use_mcp_tool___notripleunderscoreseparator", |
| 90 | + arguments: JSON.stringify({}), |
| 91 | + }, |
| 92 | + }, |
| 93 | + ] |
| 94 | + |
| 95 | + // Mock console.warn to verify it's called |
| 96 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}) |
| 97 | + |
| 98 | + parser.processNativeToolCalls(toolCalls) |
| 99 | + const blocks = parser.getContentBlocks() |
| 100 | + |
| 101 | + expect(blocks).toHaveLength(0) |
| 102 | + expect(warnSpy).toHaveBeenCalledWith( |
| 103 | + "[AssistantMessageParser] Unknown tool name in native call:", |
| 104 | + "use_mcp_tool___notripleunderscoreseparator", |
| 105 | + ) |
| 106 | + |
| 107 | + warnSpy.mockRestore() |
| 108 | + }) |
| 109 | + |
| 110 | + it("should handle streaming dynamic MCP tool calls", () => { |
| 111 | + // First delta: function name |
| 112 | + const delta1 = [ |
| 113 | + { |
| 114 | + id: "call_stream", |
| 115 | + index: 0, |
| 116 | + type: "function" as const, |
| 117 | + function: { |
| 118 | + name: "use_mcp_tool___weather___get_forecast", |
| 119 | + arguments: "", |
| 120 | + }, |
| 121 | + }, |
| 122 | + ] |
| 123 | + |
| 124 | + // Second delta: partial arguments (name can be empty string during streaming) |
| 125 | + const delta2 = [ |
| 126 | + { |
| 127 | + index: 0, |
| 128 | + type: "function" as const, |
| 129 | + function: { |
| 130 | + name: "", |
| 131 | + arguments: '{"toolInputProps": {"city": "San', |
| 132 | + }, |
| 133 | + }, |
| 134 | + ] |
| 135 | + |
| 136 | + // Third delta: complete arguments |
| 137 | + const delta3 = [ |
| 138 | + { |
| 139 | + index: 0, |
| 140 | + type: "function" as const, |
| 141 | + function: { |
| 142 | + name: "", |
| 143 | + arguments: ' Francisco"}}', |
| 144 | + }, |
| 145 | + }, |
| 146 | + ] |
| 147 | + |
| 148 | + parser.processNativeToolCalls(delta1) |
| 149 | + let blocks = parser.getContentBlocks() |
| 150 | + expect(blocks).toHaveLength(0) // Not complete yet |
| 151 | + |
| 152 | + parser.processNativeToolCalls(delta2) |
| 153 | + blocks = parser.getContentBlocks() |
| 154 | + expect(blocks).toHaveLength(0) // Still not complete |
| 155 | + |
| 156 | + parser.processNativeToolCalls(delta3) |
| 157 | + blocks = parser.getContentBlocks() |
| 158 | + |
| 159 | + expect(blocks).toHaveLength(1) |
| 160 | + const toolUse = blocks[0] as ToolUse |
| 161 | + expect(toolUse.name).toBe("use_mcp_tool") |
| 162 | + expect(toolUse.params.server_name).toBe("weather") |
| 163 | + expect(toolUse.params.tool_name).toBe("get_forecast") |
| 164 | + // Verify arguments contains the toolInputProps as a JSON string |
| 165 | + const parsedArgs = JSON.parse(toolUse.params.arguments!) |
| 166 | + expect(parsedArgs.city).toBe("San Francisco") |
| 167 | + }) |
| 168 | + |
| 169 | + it("should preserve existing server_name and tool_name in params if present", () => { |
| 170 | + const toolCalls = [ |
| 171 | + { |
| 172 | + id: "call_preserve", |
| 173 | + type: "function" as const, |
| 174 | + function: { |
| 175 | + name: "use_mcp_tool___server1___tool1", |
| 176 | + arguments: JSON.stringify({ |
| 177 | + server_name: "custom_server", |
| 178 | + tool_name: "custom_tool", |
| 179 | + toolInputProps: { |
| 180 | + data: "test", |
| 181 | + }, |
| 182 | + }), |
| 183 | + }, |
| 184 | + }, |
| 185 | + ] |
| 186 | + |
| 187 | + parser.processNativeToolCalls(toolCalls) |
| 188 | + const blocks = parser.getContentBlocks() |
| 189 | + |
| 190 | + expect(blocks).toHaveLength(1) |
| 191 | + const toolUse = blocks[0] as ToolUse |
| 192 | + expect(toolUse.name).toBe("use_mcp_tool") |
| 193 | + // Should preserve the params from arguments, not override with extracted values |
| 194 | + expect(toolUse.params.server_name).toBe("custom_server") |
| 195 | + expect(toolUse.params.tool_name).toBe("custom_tool") |
| 196 | + // Verify arguments contains the toolInputProps as a JSON string |
| 197 | + const parsedArgs = JSON.parse(toolUse.params.arguments!) |
| 198 | + expect(parsedArgs.data).toBe("test") |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + describe("standard tool names", () => { |
| 203 | + it("should handle standard tool names without modification", () => { |
| 204 | + const toolCalls = [ |
| 205 | + { |
| 206 | + id: "call_standard", |
| 207 | + type: "function" as const, |
| 208 | + function: { |
| 209 | + name: "read_file", |
| 210 | + arguments: JSON.stringify({ |
| 211 | + path: "src/file.ts", |
| 212 | + }), |
| 213 | + }, |
| 214 | + }, |
| 215 | + ] |
| 216 | + |
| 217 | + parser.processNativeToolCalls(toolCalls) |
| 218 | + const blocks = parser.getContentBlocks() |
| 219 | + |
| 220 | + expect(blocks).toHaveLength(1) |
| 221 | + const toolUse = blocks[0] as ToolUse |
| 222 | + expect(toolUse.name).toBe("read_file") |
| 223 | + expect(toolUse.params.path).toBe("src/file.ts") |
| 224 | + }) |
| 225 | + }) |
| 226 | + }) |
| 227 | +}) |
0 commit comments