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