|
| 1 | +// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage.spec.ts |
| 2 | + |
| 3 | +import { ToolUse } from "../../../shared/tools" |
| 4 | + |
| 5 | +// Mock the removeClosingTag function to test it in isolation |
| 6 | +// We'll extract the logic from presentAssistantMessage for testing |
| 7 | +function createRemoveClosingTagFunction(block: ToolUse) { |
| 8 | + return (tag: string, text?: string): string => { |
| 9 | + if (!text) { |
| 10 | + return "" |
| 11 | + } |
| 12 | + |
| 13 | + let cleanedText = text |
| 14 | + |
| 15 | + // For MCP tools, some models incorrectly add complete closing tags |
| 16 | + // like "</use_mcp_tool>" to the tool parameters. Remove these first. |
| 17 | + if (block.name === "use_mcp_tool" || block.name === "access_mcp_resource") { |
| 18 | + // Remove complete erroneous closing tags for MCP tools |
| 19 | + // This handles both single and multiple occurrences, and also handles cases |
| 20 | + // where partial tags might follow complete tags |
| 21 | + cleanedText = cleanedText.replace(/<\/use_mcp_tool>/g, "").trimEnd() |
| 22 | + cleanedText = cleanedText.replace(/<\/access_mcp_resource>/g, "").trimEnd() |
| 23 | + } |
| 24 | + |
| 25 | + // Handle partial closing tags during streaming (original logic) |
| 26 | + if (block.partial) { |
| 27 | + // This regex dynamically constructs a pattern to match the |
| 28 | + // closing tag: |
| 29 | + // - Optionally matches whitespace before the tag. |
| 30 | + // - Matches '<' or '</' optionally followed by any subset of |
| 31 | + // characters from the tag name. |
| 32 | + const tagRegex = new RegExp( |
| 33 | + `\\s?<\/?${tag |
| 34 | + .split("") |
| 35 | + .map((char) => `(?:${char})?`) |
| 36 | + .join("")}$`, |
| 37 | + "g", |
| 38 | + ) |
| 39 | + |
| 40 | + cleanedText = cleanedText.replace(tagRegex, "") |
| 41 | + } |
| 42 | + |
| 43 | + return cleanedText |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe("removeClosingTag function", () => { |
| 48 | + describe("MCP tool erroneous closing tag removal", () => { |
| 49 | + it("should remove complete </use_mcp_tool> closing tags from use_mcp_tool parameters", () => { |
| 50 | + const block: ToolUse = { |
| 51 | + type: "tool_use", |
| 52 | + name: "use_mcp_tool", |
| 53 | + params: { |
| 54 | + server_name: "test_server", |
| 55 | + tool_name: "test_tool", |
| 56 | + arguments: '{"param": "value"}</use_mcp_tool>', |
| 57 | + }, |
| 58 | + partial: false, |
| 59 | + } |
| 60 | + |
| 61 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 62 | + const result = removeClosingTag("arguments", block.params.arguments) |
| 63 | + |
| 64 | + expect(result).toBe('{"param": "value"}') |
| 65 | + }) |
| 66 | + |
| 67 | + it("should remove complete </use_mcp_tool> closing tags with whitespace", () => { |
| 68 | + const block: ToolUse = { |
| 69 | + type: "tool_use", |
| 70 | + name: "use_mcp_tool", |
| 71 | + params: { |
| 72 | + server_name: "test_server", |
| 73 | + tool_name: "test_tool", |
| 74 | + arguments: '{"param": "value"}</use_mcp_tool> ', |
| 75 | + }, |
| 76 | + partial: false, |
| 77 | + } |
| 78 | + |
| 79 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 80 | + const result = removeClosingTag("arguments", block.params.arguments) |
| 81 | + |
| 82 | + expect(result).toBe('{"param": "value"}') |
| 83 | + }) |
| 84 | + |
| 85 | + it("should remove complete </access_mcp_resource> closing tags from access_mcp_resource parameters", () => { |
| 86 | + const block: ToolUse = { |
| 87 | + type: "tool_use", |
| 88 | + name: "access_mcp_resource", |
| 89 | + params: { |
| 90 | + server_name: "test_server", |
| 91 | + uri: "file://test.txt</access_mcp_resource>", |
| 92 | + }, |
| 93 | + partial: false, |
| 94 | + } |
| 95 | + |
| 96 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 97 | + const result = removeClosingTag("uri", block.params.uri) |
| 98 | + |
| 99 | + expect(result).toBe("file://test.txt") |
| 100 | + }) |
| 101 | + |
| 102 | + it("should not remove closing tags from non-MCP tools", () => { |
| 103 | + const block: ToolUse = { |
| 104 | + type: "tool_use", |
| 105 | + name: "read_file", |
| 106 | + params: { |
| 107 | + path: "test.txt</use_mcp_tool>", |
| 108 | + }, |
| 109 | + partial: false, |
| 110 | + } |
| 111 | + |
| 112 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 113 | + const result = removeClosingTag("path", block.params.path) |
| 114 | + |
| 115 | + // Should not remove the closing tag since this is not an MCP tool |
| 116 | + expect(result).toBe("test.txt</use_mcp_tool>") |
| 117 | + }) |
| 118 | + |
| 119 | + it("should handle multiple erroneous closing tags", () => { |
| 120 | + const block: ToolUse = { |
| 121 | + type: "tool_use", |
| 122 | + name: "use_mcp_tool", |
| 123 | + params: { |
| 124 | + arguments: '{"param": "value"}</use_mcp_tool></use_mcp_tool>', |
| 125 | + }, |
| 126 | + partial: false, |
| 127 | + } |
| 128 | + |
| 129 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 130 | + const result = removeClosingTag("arguments", block.params.arguments) |
| 131 | + |
| 132 | + // Should remove both erroneous closing tags |
| 133 | + expect(result).toBe('{"param": "value"}') |
| 134 | + }) |
| 135 | + |
| 136 | + it("should not remove closing tags that are part of legitimate content", () => { |
| 137 | + const block: ToolUse = { |
| 138 | + type: "tool_use", |
| 139 | + name: "use_mcp_tool", |
| 140 | + params: { |
| 141 | + arguments: '{"html": "<div>content</div>", "tool": "use_mcp_tool"}', |
| 142 | + }, |
| 143 | + partial: false, |
| 144 | + } |
| 145 | + |
| 146 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 147 | + const result = removeClosingTag("arguments", block.params.arguments) |
| 148 | + |
| 149 | + // Should not remove legitimate HTML tags or tool names in content |
| 150 | + expect(result).toBe('{"html": "<div>content</div>", "tool": "use_mcp_tool"}') |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe("partial closing tag removal during streaming", () => { |
| 155 | + it("should remove partial closing tags when block is partial", () => { |
| 156 | + const block: ToolUse = { |
| 157 | + type: "tool_use", |
| 158 | + name: "read_file", |
| 159 | + params: { |
| 160 | + path: "test.txt</pa", |
| 161 | + }, |
| 162 | + partial: true, |
| 163 | + } |
| 164 | + |
| 165 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 166 | + const result = removeClosingTag("path", block.params.path) |
| 167 | + |
| 168 | + expect(result).toBe("test.txt") |
| 169 | + }) |
| 170 | + |
| 171 | + it("should remove partial opening tags when block is partial", () => { |
| 172 | + const block: ToolUse = { |
| 173 | + type: "tool_use", |
| 174 | + name: "read_file", |
| 175 | + params: { |
| 176 | + path: "test.txt<pa", |
| 177 | + }, |
| 178 | + partial: true, |
| 179 | + } |
| 180 | + |
| 181 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 182 | + const result = removeClosingTag("path", block.params.path) |
| 183 | + |
| 184 | + expect(result).toBe("test.txt") |
| 185 | + }) |
| 186 | + |
| 187 | + it("should not remove partial tags when block is not partial", () => { |
| 188 | + const block: ToolUse = { |
| 189 | + type: "tool_use", |
| 190 | + name: "read_file", |
| 191 | + params: { |
| 192 | + path: "test.txt</pa", |
| 193 | + }, |
| 194 | + partial: false, |
| 195 | + } |
| 196 | + |
| 197 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 198 | + const result = removeClosingTag("path", block.params.path) |
| 199 | + |
| 200 | + // Should not remove partial tags when block is complete |
| 201 | + expect(result).toBe("test.txt</pa") |
| 202 | + }) |
| 203 | + }) |
| 204 | + |
| 205 | + describe("combined scenarios", () => { |
| 206 | + it("should handle both erroneous closing tags and partial tags for MCP tools", () => { |
| 207 | + const block: ToolUse = { |
| 208 | + type: "tool_use", |
| 209 | + name: "use_mcp_tool", |
| 210 | + params: { |
| 211 | + arguments: '{"param": "value"}</use_mcp_tool></arg', |
| 212 | + }, |
| 213 | + partial: true, |
| 214 | + } |
| 215 | + |
| 216 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 217 | + const result = removeClosingTag("arguments", block.params.arguments) |
| 218 | + |
| 219 | + // Should remove both the erroneous closing tag and the partial tag |
| 220 | + expect(result).toBe('{"param": "value"}') |
| 221 | + }) |
| 222 | + |
| 223 | + it("should handle empty or undefined text", () => { |
| 224 | + const block: ToolUse = { |
| 225 | + type: "tool_use", |
| 226 | + name: "use_mcp_tool", |
| 227 | + params: {}, |
| 228 | + partial: false, |
| 229 | + } |
| 230 | + |
| 231 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 232 | + |
| 233 | + expect(removeClosingTag("arguments", undefined)).toBe("") |
| 234 | + expect(removeClosingTag("arguments", "")).toBe("") |
| 235 | + }) |
| 236 | + |
| 237 | + it("should handle text without any tags", () => { |
| 238 | + const block: ToolUse = { |
| 239 | + type: "tool_use", |
| 240 | + name: "use_mcp_tool", |
| 241 | + params: { |
| 242 | + arguments: '{"param": "value"}', |
| 243 | + }, |
| 244 | + partial: false, |
| 245 | + } |
| 246 | + |
| 247 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 248 | + const result = removeClosingTag("arguments", block.params.arguments) |
| 249 | + |
| 250 | + // Should return text unchanged when no erroneous tags are present |
| 251 | + expect(result).toBe('{"param": "value"}') |
| 252 | + }) |
| 253 | + }) |
| 254 | + |
| 255 | + describe("qwen2.5-72b-instruct specific scenarios", () => { |
| 256 | + it("should handle the exact issue reported with qwen2.5-72b-instruct model", () => { |
| 257 | + // This test simulates the exact scenario described in issue #5464 |
| 258 | + const block: ToolUse = { |
| 259 | + type: "tool_use", |
| 260 | + name: "use_mcp_tool", |
| 261 | + params: { |
| 262 | + server_name: "filesystem", |
| 263 | + tool_name: "read_file", |
| 264 | + arguments: '{"path": "/path/to/file.txt"}</use_mcp_tool>', |
| 265 | + }, |
| 266 | + partial: false, |
| 267 | + } |
| 268 | + |
| 269 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 270 | + const cleanedArguments = removeClosingTag("arguments", block.params.arguments) |
| 271 | + |
| 272 | + // The cleaned arguments should be valid JSON without the erroneous closing tag |
| 273 | + expect(cleanedArguments).toBe('{"path": "/path/to/file.txt"}') |
| 274 | + |
| 275 | + // Verify that the cleaned arguments can be parsed as valid JSON |
| 276 | + expect(() => JSON.parse(cleanedArguments)).not.toThrow() |
| 277 | + |
| 278 | + const parsedArgs = JSON.parse(cleanedArguments) |
| 279 | + expect(parsedArgs.path).toBe("/path/to/file.txt") |
| 280 | + }) |
| 281 | + |
| 282 | + it("should handle complex JSON arguments with erroneous closing tags", () => { |
| 283 | + const block: ToolUse = { |
| 284 | + type: "tool_use", |
| 285 | + name: "use_mcp_tool", |
| 286 | + params: { |
| 287 | + server_name: "database", |
| 288 | + tool_name: "query", |
| 289 | + arguments: '{"query": "SELECT * FROM users", "limit": 10, "offset": 0}</use_mcp_tool>', |
| 290 | + }, |
| 291 | + partial: false, |
| 292 | + } |
| 293 | + |
| 294 | + const removeClosingTag = createRemoveClosingTagFunction(block) |
| 295 | + const cleanedArguments = removeClosingTag("arguments", block.params.arguments) |
| 296 | + |
| 297 | + expect(cleanedArguments).toBe('{"query": "SELECT * FROM users", "limit": 10, "offset": 0}') |
| 298 | + |
| 299 | + // Verify valid JSON parsing |
| 300 | + const parsedArgs = JSON.parse(cleanedArguments) |
| 301 | + expect(parsedArgs.query).toBe("SELECT * FROM users") |
| 302 | + expect(parsedArgs.limit).toBe(10) |
| 303 | + expect(parsedArgs.offset).toBe(0) |
| 304 | + }) |
| 305 | + }) |
| 306 | +}) |
0 commit comments