|
| 1 | +import { parseAssistantMessageV2 } from "../parseAssistantMessageV2" |
| 2 | +import { ToolUse } from "../../../shared/tools" |
| 3 | + |
| 4 | +describe("parseAssistantMessageV2 - Issue #7664", () => { |
| 5 | + describe("handling malformed XML structure from LLMs", () => { |
| 6 | + it("should handle the exact structure from juliettefournier-econ's example", () => { |
| 7 | + // This is the exact structure that was causing issues |
| 8 | + const message = `I'll read that file for you. |
| 9 | +
|
| 10 | +<read_file> |
| 11 | +<args> |
| 12 | +<file> |
| 13 | +<path>src/shared/infrastructure/supabase/factory.py</path> |
| 14 | +<line_range>10-20</line_range> |
| 15 | +</file> |
| 16 | +</args> |
| 17 | +</read_file>` |
| 18 | + |
| 19 | + const result = parseAssistantMessageV2(message) |
| 20 | + |
| 21 | + // Should have text and tool use |
| 22 | + expect(result).toHaveLength(2) |
| 23 | + expect(result[0].type).toBe("text") |
| 24 | + expect(result[1].type).toBe("tool_use") |
| 25 | + |
| 26 | + const toolUse = result[1] as ToolUse |
| 27 | + expect(toolUse.name).toBe("read_file") |
| 28 | + expect(toolUse.params.args).toBeDefined() |
| 29 | + expect(toolUse.params.args).toContain("<file>") |
| 30 | + expect(toolUse.params.args).toContain("<path>src/shared/infrastructure/supabase/factory.py</path>") |
| 31 | + expect(toolUse.params.args).toContain("<line_range>10-20</line_range>") |
| 32 | + }) |
| 33 | + |
| 34 | + it("should handle structure with no spaces between XML tags", () => { |
| 35 | + const message = `<read_file><args><file><path>test.py</path><line_range>1-10</line_range></file></args></read_file>` |
| 36 | + |
| 37 | + const result = parseAssistantMessageV2(message) |
| 38 | + |
| 39 | + expect(result).toHaveLength(1) |
| 40 | + const toolUse = result[0] as ToolUse |
| 41 | + expect(toolUse.type).toBe("tool_use") |
| 42 | + expect(toolUse.name).toBe("read_file") |
| 43 | + expect(toolUse.params.args).toBeDefined() |
| 44 | + expect(toolUse.params.args).toContain("<path>test.py</path>") |
| 45 | + expect(toolUse.params.args).toContain("<line_range>1-10</line_range>") |
| 46 | + }) |
| 47 | + |
| 48 | + it("should handle structure with mixed spacing and newlines", () => { |
| 49 | + const message = `<read_file> |
| 50 | + <args> |
| 51 | + <file><path> src/test.py </path> |
| 52 | + <line_range>1-10</line_range> |
| 53 | + </file> |
| 54 | + </args> |
| 55 | +</read_file>` |
| 56 | + |
| 57 | + const result = parseAssistantMessageV2(message) |
| 58 | + |
| 59 | + expect(result).toHaveLength(1) |
| 60 | + const toolUse = result[0] as ToolUse |
| 61 | + expect(toolUse.type).toBe("tool_use") |
| 62 | + expect(toolUse.name).toBe("read_file") |
| 63 | + expect(toolUse.params.args).toBeDefined() |
| 64 | + // The args should preserve the internal structure |
| 65 | + expect(toolUse.params.args).toContain("<file>") |
| 66 | + expect(toolUse.params.args).toContain("<path>") |
| 67 | + expect(toolUse.params.args).toContain("src/test.py") |
| 68 | + expect(toolUse.params.args).toContain("</path>") |
| 69 | + expect(toolUse.params.args).toContain("<line_range>1-10</line_range>") |
| 70 | + }) |
| 71 | + |
| 72 | + it("should handle empty path element", () => { |
| 73 | + const message = `<read_file> |
| 74 | +<args> |
| 75 | +<file> |
| 76 | +<path></path> |
| 77 | +<line_range>10-20</line_range> |
| 78 | +</file> |
| 79 | +</args> |
| 80 | +</read_file>` |
| 81 | + |
| 82 | + const result = parseAssistantMessageV2(message) |
| 83 | + |
| 84 | + expect(result).toHaveLength(1) |
| 85 | + const toolUse = result[0] as ToolUse |
| 86 | + expect(toolUse.type).toBe("tool_use") |
| 87 | + expect(toolUse.name).toBe("read_file") |
| 88 | + expect(toolUse.params.args).toBeDefined() |
| 89 | + expect(toolUse.params.args).toContain("<path></path>") |
| 90 | + }) |
| 91 | + |
| 92 | + it("should handle self-closing path element", () => { |
| 93 | + const message = `<read_file> |
| 94 | +<args> |
| 95 | +<file> |
| 96 | +<path/> |
| 97 | +<line_range>10-20</line_range> |
| 98 | +</file> |
| 99 | +</args> |
| 100 | +</read_file>` |
| 101 | + |
| 102 | + const result = parseAssistantMessageV2(message) |
| 103 | + |
| 104 | + expect(result).toHaveLength(1) |
| 105 | + const toolUse = result[0] as ToolUse |
| 106 | + expect(toolUse.type).toBe("tool_use") |
| 107 | + expect(toolUse.name).toBe("read_file") |
| 108 | + expect(toolUse.params.args).toBeDefined() |
| 109 | + expect(toolUse.params.args).toContain("<path/>") |
| 110 | + }) |
| 111 | + |
| 112 | + it("should handle multiple files with varying structures", () => { |
| 113 | + const message = `<read_file> |
| 114 | +<args> |
| 115 | +<file> |
| 116 | +<path> ./file1.ts </path> |
| 117 | +</file> |
| 118 | +<file> |
| 119 | +<path> |
| 120 | + ./file2.ts |
| 121 | +</path> |
| 122 | +<line_range>10-20</line_range> |
| 123 | +</file> |
| 124 | +</args> |
| 125 | +</read_file>` |
| 126 | + |
| 127 | + const result = parseAssistantMessageV2(message) |
| 128 | + |
| 129 | + expect(result).toHaveLength(1) |
| 130 | + const toolUse = result[0] as ToolUse |
| 131 | + expect(toolUse.type).toBe("tool_use") |
| 132 | + expect(toolUse.name).toBe("read_file") |
| 133 | + expect(toolUse.params.args).toBeDefined() |
| 134 | + // Check that both files are present |
| 135 | + expect(toolUse.params.args).toContain("./file1.ts") |
| 136 | + expect(toolUse.params.args).toContain("./file2.ts") |
| 137 | + expect(toolUse.params.args).toContain("<line_range>10-20</line_range>") |
| 138 | + }) |
| 139 | + |
| 140 | + it("should handle partial/incomplete tool use", () => { |
| 141 | + const message = `<read_file> |
| 142 | +<args> |
| 143 | +<file> |
| 144 | +<path>test.py</path>` |
| 145 | + // Message ends abruptly |
| 146 | + |
| 147 | + const result = parseAssistantMessageV2(message) |
| 148 | + |
| 149 | + expect(result).toHaveLength(1) |
| 150 | + const toolUse = result[0] as ToolUse |
| 151 | + expect(toolUse.type).toBe("tool_use") |
| 152 | + expect(toolUse.name).toBe("read_file") |
| 153 | + expect(toolUse.partial).toBe(true) |
| 154 | + expect(toolUse.params.args).toBeDefined() |
| 155 | + expect(toolUse.params.args).toContain("<path>test.py</path>") |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe("args parameter trimming behavior", () => { |
| 160 | + it("should trim args parameter content", () => { |
| 161 | + const message = `<read_file> |
| 162 | +<args> |
| 163 | + |
| 164 | + <file> |
| 165 | + <path>test.py</path> |
| 166 | + </file> |
| 167 | + |
| 168 | +</args> |
| 169 | +</read_file>` |
| 170 | + |
| 171 | + const result = parseAssistantMessageV2(message) |
| 172 | + |
| 173 | + expect(result).toHaveLength(1) |
| 174 | + const toolUse = result[0] as ToolUse |
| 175 | + expect(toolUse.params.args).toBeDefined() |
| 176 | + // args should be trimmed |
| 177 | + expect(toolUse.params.args).not.toMatch(/^\s+/) |
| 178 | + expect(toolUse.params.args).not.toMatch(/\s+$/) |
| 179 | + expect(toolUse.params.args).toMatch(/^<file>/) |
| 180 | + expect(toolUse.params.args).toMatch(/<\/file>$/) |
| 181 | + }) |
| 182 | + }) |
| 183 | +}) |
0 commit comments