| 1 | import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | ToolCallParseState, |
| 4 | getInitialTooLCallParseState, |
| 5 | handleToolCallBuffer, |
| 6 | } from "./parseSystemToolCall"; |
| 7 | |
| 8 | describe("getInitialTooLCallParseState", () => { |
| 9 | it("returns a properly initialized parse state", () => { |
| 10 | const state = getInitialTooLCallParseState(); |
| 11 | |
| 12 | expect(state).toEqual({ |
| 13 | isOnArgBeginLine: false, |
| 14 | currentArgName: undefined, |
| 15 | currentArgLines: [], |
| 16 | currentLineIndex: 0, |
| 17 | processedArgNames: new Set(), |
| 18 | lineChunks: [], |
| 19 | done: false, |
| 20 | toolCallId: expect.any(String), |
| 21 | }); |
| 22 | }); |
| 23 | }); |
| 24 | |
| 25 | describe("handleToolCallBuffer", () => { |
| 26 | let state: ToolCallParseState; |
| 27 | |
| 28 | beforeEach(() => { |
| 29 | state = getInitialTooLCallParseState(); |
| 30 | }); |
| 31 | |
| 32 | it("handles the ```tool\ntool_name the name", () => { |
| 33 | handleToolCallBuffer("```tool\ntool_name: my_name", state); |
| 34 | expect(state.currentLineIndex).toBe(1); |
| 35 | |
| 36 | const result = handleToolCallBuffer("\n", state); |
| 37 | expect(result).toEqual({ |
| 38 | type: "function", |
| 39 | function: { |
| 40 | name: "my_name", |
| 41 | arguments: "", |
| 42 | }, |
| 43 | id: expect.any(String), |
| 44 | }); |
| 45 | expect(state.currentLineIndex).toBe(2); |
| 46 | }); |
| 47 | |
| 48 | it("handles the tool name line", () => { |
| 49 | // Line 0 (```tool) is skipped internally |
| 50 | state.currentLineIndex = 1; |
| 51 | |
| 52 | const result = handleToolCallBuffer("TOOL_NAME: test_tool", state); |
| 53 | expect(result).toBeUndefined(); |
| 54 | |
| 55 | const newLineResult = handleToolCallBuffer("\n", state); |
| 56 | |
| 57 | expect(newLineResult).toEqual({ |
| 58 | type: "function", |
| 59 | function: { |
| 60 | name: "test_tool", |
| 61 | arguments: "", |
| 62 | }, |
| 63 | id: expect.any(String), |
| 64 | }); |
| 65 | expect(state.currentLineIndex).toBe(2); |
| 66 | }); |
| 67 | |
| 68 | it("handles case-insensitive tool name line", () => { |
| 69 | state.currentLineIndex = 1; |
| 70 | |
| 71 | const result = handleToolCallBuffer("tool_name: test_tool", state); |
| 72 | expect(result).toBeUndefined(); |
| 73 | |
| 74 | const newLineResult = handleToolCallBuffer("\n", state); |
| 75 | |
| 76 | expect(newLineResult).toEqual({ |
| 77 | type: "function", |
| 78 | function: { |
| 79 | name: "test_tool", |
| 80 | arguments: "", |
| 81 | }, |
| 82 | id: expect.any(String), |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | it("begins an argument correctly", () => { |
| 87 | state.currentLineIndex = 2; |
| 88 | |
| 89 | const result = handleToolCallBuffer("BEGIN_ARG: test_arg", state); |
| 90 | expect(result).toBeUndefined(); |
| 91 | expect(state.isOnArgBeginLine).toBe(true); |
| 92 | |
| 93 | const newLineResult = handleToolCallBuffer("\n", state); |
| 94 | |
| 95 | expect(newLineResult).toEqual({ |
| 96 | type: "function", |
| 97 | function: { |
| 98 | name: "", |
| 99 | arguments: '{"test_arg":', |
| 100 | }, |
| 101 | id: expect.any(String), |
| 102 | }); |
| 103 | expect(state.currentArgName).toBe("test_arg"); |
| 104 | expect(state.isOnArgBeginLine).toBe(false); |
| 105 | }); |
| 106 | |
| 107 | it("handles case-insensitive begin arg", () => { |
| 108 | state.currentLineIndex = 2; |
| 109 | |
| 110 | const result = handleToolCallBuffer("begin_arg: test_arg", state); |
| 111 | expect(result).toBeUndefined(); |
| 112 | expect(state.isOnArgBeginLine).toBe(true); |
| 113 | |
| 114 | const newLineResult = handleToolCallBuffer("\n", state); |
| 115 | |
| 116 | expect(newLineResult).toEqual({ |
| 117 | type: "function", |
| 118 | function: { |
| 119 | name: "", |
| 120 | arguments: '{"test_arg":', |
| 121 | }, |
| 122 | id: expect.any(String), |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | it("collects arg value lines", () => { |
| 127 | state.currentLineIndex = 3; |
| 128 | state.currentArgName = "test_arg"; |
| 129 | |
| 130 | handleToolCallBuffer("line 1", state); |
| 131 | const newLineResult1 = handleToolCallBuffer("\n", state); |
| 132 | expect(newLineResult1).toBeUndefined(); |
| 133 | |
| 134 | handleToolCallBuffer("line 2", state); |
| 135 | const newLineResult2 = handleToolCallBuffer("\n", state); |
| 136 | expect(newLineResult2).toBeUndefined(); |
| 137 | |
| 138 | expect(state.currentArgLines).toEqual(["line 1\n", "line 2\n"]); |
| 139 | }); |
| 140 | |
| 141 | it("ends an argument correctly with string value", () => { |
| 142 | state.currentLineIndex = 3; |
| 143 | state.currentArgName = "test_arg"; |
| 144 | state.currentArgLines = ["string value"]; |
| 145 | |
| 146 | const result = handleToolCallBuffer("END_ARG", state); |
| 147 | expect(result).toBeUndefined(); |
| 148 | |
| 149 | const newLineResult = handleToolCallBuffer("\n", state); |
| 150 | |
| 151 | expect(newLineResult).toEqual({ |
| 152 | type: "function", |
| 153 | function: { |
| 154 | name: "", |
| 155 | arguments: '"string value"', |
| 156 | }, |
| 157 | id: expect.any(String), |
| 158 | }); |
| 159 | expect(state.currentArgName).toBeUndefined(); |
| 160 | expect(state.processedArgNames.has("test_arg")).toBe(true); |
| 161 | }); |
| 162 | |
| 163 | it("handles case-insensitive end arg", () => { |
| 164 | state.currentLineIndex = 3; |
| 165 | state.currentArgName = "test_arg"; |
| 166 | state.currentArgLines = ["string value"]; |
| 167 | |
| 168 | const result = handleToolCallBuffer("end_arg", state); |
| 169 | expect(result).toBeUndefined(); |
| 170 | |
| 171 | const newLineResult = handleToolCallBuffer("\n", state); |
| 172 | |
| 173 | expect(newLineResult).toEqual({ |
| 174 | type: "function", |
| 175 | function: { |
| 176 | name: "", |
| 177 | arguments: '"string value"', |
| 178 | }, |
| 179 | id: expect.any(String), |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | it("attempts to parse JSON values", () => { |
| 184 | state.currentLineIndex = 3; |
| 185 | state.currentArgName = "test_arg"; |
| 186 | state.currentArgLines = ["123"]; |
| 187 | |
| 188 | handleToolCallBuffer("END_ARG", state); |
| 189 | const newLineResult = handleToolCallBuffer("\n", state); |
| 190 | |
| 191 | expect(newLineResult).toEqual({ |
| 192 | type: "function", |
| 193 | function: { |
| 194 | name: "", |
| 195 | arguments: "123", |
| 196 | }, |
| 197 | id: expect.any(String), |
| 198 | }); |
| 199 | }); |
| 200 | |
| 201 | it("handles multiple arguments", () => { |
| 202 | // Setup first arg |
| 203 | state.currentLineIndex = 2; |
| 204 | state.isOnArgBeginLine = true; |
| 205 | state.lineChunks[2] = ["BEGIN_ARG: first_arg"]; |
| 206 | |
| 207 | handleToolCallBuffer("\n", state); |
| 208 | handleToolCallBuffer("value1", state); |
| 209 | handleToolCallBuffer("\n", state); |
| 210 | handleToolCallBuffer("END_ARG", state); |
| 211 | handleToolCallBuffer("\n", state); |
| 212 | |
| 213 | expect(state.processedArgNames.has("first_arg")).toBe(true); |
| 214 | |
| 215 | // Setup second arg |
| 216 | state.isOnArgBeginLine = true; |
| 217 | handleToolCallBuffer("BEGIN_ARG: second_arg", state); |
| 218 | handleToolCallBuffer("\n", state); |
| 219 | handleToolCallBuffer("value2", state); |
| 220 | handleToolCallBuffer("\n", state); |
| 221 | handleToolCallBuffer("END_ARG", state); |
| 222 | |
| 223 | const newLineResult = handleToolCallBuffer("\n", state); |
| 224 | |
| 225 | expect(newLineResult).toEqual({ |
| 226 | type: "function", |
| 227 | function: { |
| 228 | name: "", |
| 229 | arguments: '"value2"', |
| 230 | }, |
| 231 | id: expect.any(String), |
| 232 | }); |
| 233 | expect(state.processedArgNames.has("second_arg")).toBe(true); |
| 234 | }); |
| 235 | |
| 236 | it("finalizes JSON object at end of tool call", () => { |
| 237 | state.currentLineIndex = 5; |
| 238 | state.processedArgNames.add("test_arg"); |
| 239 | |
| 240 | const result = handleToolCallBuffer("```", state); |
| 241 | |
| 242 | expect(result).toEqual({ |
| 243 | type: "function", |
| 244 | function: { |
| 245 | name: "", |
| 246 | arguments: "}", |
| 247 | }, |
| 248 | id: expect.any(String), |
| 249 | }); |
| 250 | expect(state.done).toBe(true); |
| 251 | }); |
| 252 | |
| 253 | it("finalizes on newline after all args processed", () => { |
| 254 | state.currentLineIndex = 5; |
| 255 | state.processedArgNames.add("test_arg"); |
| 256 | |
| 257 | const result = handleToolCallBuffer("\n", state); |
| 258 | |
| 259 | expect(result).toEqual({ |
| 260 | type: "function", |
| 261 | function: { |
| 262 | name: "", |
| 263 | arguments: "}", |
| 264 | }, |
| 265 | id: expect.any(String), |
| 266 | }); |
| 267 | expect(state.done).toBe(true); |
| 268 | }); |
| 269 | |
| 270 | it("handles JSON array args", () => { |
| 271 | state.currentLineIndex = 3; |
| 272 | state.currentArgName = "test_arg"; |
| 273 | state.currentArgLines = [ |
| 274 | "[\n", |
| 275 | '"------- SEARCH\n', |
| 276 | " subtract(number) {\n", |
| 277 | " return this;\n", |
| 278 | " }\n", |
| 279 | "=======\n", |
| 280 | " subtract(number) {\n", |
| 281 | " this -= number\n", |
| 282 | " return this;\n", |
| 283 | " }\n", |
| 284 | '+++++++ REPLACE"\n', |
| 285 | "]\n", |
| 286 | ]; |
| 287 | |
| 288 | handleToolCallBuffer("END_ARG", state); |
| 289 | const newLineResult = handleToolCallBuffer("\n", state); |
| 290 | |
| 291 | expect(newLineResult).toEqual({ |
| 292 | type: "function", |
| 293 | function: { |
| 294 | name: "", |
| 295 | arguments: |
| 296 | '["------- SEARCH\\n subtract(number) {\\n return this;\\n }\\n=======\\n subtract(number) {\\n this -= number\\n return this;\\n }\\n+++++++ REPLACE"]', |
| 297 | }, |
| 298 | id: expect.any(String), |
| 299 | }); |
| 300 | }); |
| 301 | }); |