| 1 | import { ToolCallDelta, ToolCallState } from "core"; |
| 2 | import { addToolCallDeltaToState } from "./toolCallState"; |
| 3 | |
| 4 | describe("addToolCallDeltaToState", () => { |
| 5 | it("should initialize a new tool call state when current state is undefined", () => { |
| 6 | const delta: ToolCallDelta = { |
| 7 | id: "call123", |
| 8 | type: "function", |
| 9 | function: { |
| 10 | name: "searchFiles", |
| 11 | arguments: '{"query":', |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | const result = addToolCallDeltaToState(delta, undefined); |
| 16 | |
| 17 | expect(result).toEqual({ |
| 18 | status: "generating", |
| 19 | toolCall: { |
| 20 | id: "call123", |
| 21 | type: "function", |
| 22 | function: { |
| 23 | name: "searchFiles", |
| 24 | arguments: '{"query":', |
| 25 | }, |
| 26 | }, |
| 27 | toolCallId: "call123", |
| 28 | parsedArgs: { |
| 29 | query: undefined, |
| 30 | }, |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | it("should merge function name deltas correctly", () => { |
| 35 | const currentState: ToolCallState = { |
| 36 | status: "generating", |
| 37 | toolCall: { |
| 38 | id: "call123", |
| 39 | type: "function", |
| 40 | function: { |
| 41 | name: "search", |
| 42 | arguments: '{"query":', |
| 43 | }, |
| 44 | }, |
| 45 | toolCallId: "call123", |
| 46 | parsedArgs: { query: undefined }, |
| 47 | }; |
| 48 | |
| 49 | const delta: ToolCallDelta = { |
| 50 | function: { |
| 51 | name: "Files", |
| 52 | }, |
| 53 | }; |
| 54 | |
| 55 | const result = addToolCallDeltaToState(delta, currentState); |
| 56 | |
| 57 | expect(result.toolCall.function.name).toBe("searchFiles"); |
| 58 | }); |
| 59 | |
| 60 | it("should merge function argument deltas correctly", () => { |
| 61 | const currentState: ToolCallState = { |
| 62 | status: "generating", |
| 63 | toolCall: { |
| 64 | id: "call123", |
| 65 | type: "function", |
| 66 | function: { |
| 67 | name: "searchFiles", |
| 68 | arguments: '{"query":"java', |
| 69 | }, |
| 70 | }, |
| 71 | toolCallId: "call123", |
| 72 | parsedArgs: { query: "java" }, |
| 73 | }; |
| 74 | |
| 75 | const delta: ToolCallDelta = { |
| 76 | function: { |
| 77 | arguments: 'script"}', |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | const result = addToolCallDeltaToState(delta, currentState); |
| 82 | |
| 83 | expect(result.toolCall.function.arguments).toBe('{"query":"javascript"}'); |
| 84 | expect(result.parsedArgs).toEqual({ query: "javascript" }); |
| 85 | }); |
| 86 | |
| 87 | it("should handle empty deltas gracefully", () => { |
| 88 | const currentState: ToolCallState = { |
| 89 | status: "generating", |
| 90 | toolCall: { |
| 91 | id: "call123", |
| 92 | type: "function", |
| 93 | function: { |
| 94 | name: "searchFiles", |
| 95 | arguments: '{"query":"test"}', |
| 96 | }, |
| 97 | }, |
| 98 | toolCallId: "call123", |
| 99 | parsedArgs: { query: "test" }, |
| 100 | }; |
| 101 | |
| 102 | const delta: ToolCallDelta = {}; |
| 103 | |
| 104 | const result = addToolCallDeltaToState(delta, currentState); |
| 105 | |
| 106 | expect(result).toEqual(currentState); |
| 107 | }); |
| 108 | |
| 109 | it("should handle streaming complex JSON arguments", () => { |
| 110 | const initialState: ToolCallState = { |
| 111 | status: "generating", |
| 112 | toolCall: { |
| 113 | id: "call123", |
| 114 | type: "function", |
| 115 | function: { |
| 116 | name: "editFile", |
| 117 | arguments: '{"path":"', |
| 118 | }, |
| 119 | }, |
| 120 | toolCallId: "call123", |
| 121 | parsedArgs: { path: undefined }, |
| 122 | }; |
| 123 | |
| 124 | // First chunk - add to path |
| 125 | const delta1: ToolCallDelta = { |
| 126 | function: { |
| 127 | arguments: 'src/main.js","changes":{"', |
| 128 | }, |
| 129 | }; |
| 130 | |
| 131 | const state1 = addToolCallDeltaToState(delta1, initialState); |
| 132 | expect(state1.toolCall.function.arguments).toBe( |
| 133 | '{"path":"src/main.js","changes":{"', |
| 134 | ); |
| 135 | |
| 136 | // Second chunk - add property name |
| 137 | const delta2: ToolCallDelta = { |
| 138 | function: { |
| 139 | arguments: 'content":"cons', |
| 140 | }, |
| 141 | }; |
| 142 | |
| 143 | const state2 = addToolCallDeltaToState(delta2, state1); |
| 144 | expect(state2.toolCall.function.arguments).toBe( |
| 145 | '{"path":"src/main.js","changes":{"content":"cons', |
| 146 | ); |
| 147 | expect(state2.parsedArgs).toEqual({ |
| 148 | path: "src/main.js", |
| 149 | changes: { content: "cons" }, |
| 150 | }); |
| 151 | |
| 152 | // Third chunk - complete JSON |
| 153 | const delta3: ToolCallDelta = { |
| 154 | function: { |
| 155 | arguments: 't message = \\"Hello\\";"}}', |
| 156 | }, |
| 157 | }; |
| 158 | |
| 159 | const state3 = addToolCallDeltaToState(delta3, state2); |
| 160 | expect(state3.toolCall.function.arguments).toBe( |
| 161 | '{"path":"src/main.js","changes":{"content":"const message = \\"Hello\\";"}}', |
| 162 | ); |
| 163 | expect(state3.parsedArgs).toEqual({ |
| 164 | path: "src/main.js", |
| 165 | changes: { content: 'const message = "Hello";' }, |
| 166 | }); |
| 167 | }); |
| 168 | |
| 169 | it("should handle tool call ids correctly", () => { |
| 170 | // Test when ID comes from current state |
| 171 | let currentState: ToolCallState = { |
| 172 | status: "generating", |
| 173 | toolCall: { |
| 174 | id: "existing-id", |
| 175 | type: "function", |
| 176 | function: { |
| 177 | name: "test", |
| 178 | arguments: "{}", |
| 179 | }, |
| 180 | }, |
| 181 | toolCallId: "existing-id", |
| 182 | parsedArgs: {}, |
| 183 | }; |
| 184 | |
| 185 | let delta: ToolCallDelta = { |
| 186 | // No id in delta |
| 187 | function: { |
| 188 | name: "Function", |
| 189 | }, |
| 190 | }; |
| 191 | |
| 192 | let result = addToolCallDeltaToState(delta, currentState); |
| 193 | expect(result.toolCallId).toBe("existing-id"); |
| 194 | |
| 195 | // Test when ID comes from delta |
| 196 | delta = { |
| 197 | id: "new-id", |
| 198 | function: { |
| 199 | name: "test", |
| 200 | }, |
| 201 | }; |
| 202 | |
| 203 | result = addToolCallDeltaToState(delta, undefined); |
| 204 | expect(result.toolCallId).toBe("new-id"); |
| 205 | }); |
| 206 | |
| 207 | it("should handle invalid JSON gracefully", () => { |
| 208 | const currentState: ToolCallState = { |
| 209 | status: "generating", |
| 210 | toolCall: { |
| 211 | id: "call123", |
| 212 | type: "function", |
| 213 | function: { |
| 214 | name: "searchFiles", |
| 215 | arguments: '{"query":', |
| 216 | }, |
| 217 | }, |
| 218 | toolCallId: "call123", |
| 219 | parsedArgs: { query: undefined }, |
| 220 | }; |
| 221 | |
| 222 | const delta: ToolCallDelta = { |
| 223 | function: { |
| 224 | arguments: '"test"', |
| 225 | }, |
| 226 | }; |
| 227 | |
| 228 | const result = addToolCallDeltaToState(delta, currentState); |
| 229 | |
| 230 | expect(result.toolCall.function.arguments).toBe('{"query":"test"'); |
| 231 | // Expect partial parsing still works |
| 232 | expect(result.parsedArgs).toEqual({ query: "test" }); |
| 233 | }); |
| 234 | |
| 235 | it("should handle realistic weather API streaming pattern from OpenAI example", () => { |
| 236 | // https://platform.openai.com/docs/guides/function-calling?api-mode=chat#streaming |
| 237 | // Sequence of deltas as seen in the streaming example |
| 238 | const streamSequence: ToolCallDelta[] = [ |
| 239 | { |
| 240 | id: "call_DdmO9pD3xa9XTPNJ32zg2hcA", |
| 241 | type: "function", |
| 242 | function: { |
| 243 | name: "get_weather", |
| 244 | arguments: "", |
| 245 | }, |
| 246 | }, |
| 247 | { |
| 248 | function: { |
| 249 | arguments: '{"', |
| 250 | }, |
| 251 | }, |
| 252 | { |
| 253 | function: { |
| 254 | arguments: "location", |
| 255 | }, |
| 256 | }, |
| 257 | { |
| 258 | function: { |
| 259 | arguments: '":"', |
| 260 | }, |
| 261 | }, |
| 262 | { |
| 263 | function: { |
| 264 | arguments: "Paris", |
| 265 | }, |
| 266 | }, |
| 267 | { |
| 268 | function: { |
| 269 | arguments: ",", |
| 270 | }, |
| 271 | }, |
| 272 | { |
| 273 | function: { |
| 274 | arguments: " France", |
| 275 | }, |
| 276 | }, |
| 277 | { |
| 278 | function: { |
| 279 | arguments: '"}', |
| 280 | }, |
| 281 | }, |
| 282 | ]; |
| 283 | |
| 284 | // Initialize with undefined state |
| 285 | let currentState: ToolCallState | undefined = undefined; |
| 286 | |
| 287 | // Process each delta in sequence |
| 288 | for (const delta of streamSequence) { |
| 289 | currentState = addToolCallDeltaToState(delta, currentState); |
| 290 | } |
| 291 | |
| 292 | // Check the final state |
| 293 | expect(currentState).toEqual({ |
| 294 | status: "generating", |
| 295 | toolCall: { |
| 296 | id: "call_DdmO9pD3xa9XTPNJ32zg2hcA", |
| 297 | type: "function", |
| 298 | function: { |
| 299 | name: "get_weather", |
| 300 | arguments: '{"location":"Paris, France"}', |
| 301 | }, |
| 302 | }, |
| 303 | toolCallId: "call_DdmO9pD3xa9XTPNJ32zg2hcA", |
| 304 | parsedArgs: { |
| 305 | location: "Paris, France", |
| 306 | }, |
| 307 | }); |
| 308 | |
| 309 | // Check the intermediate state after each delta to ensure incremental parsing works |
| 310 | currentState = undefined; |
| 311 | |
| 312 | // First delta - initialize |
| 313 | currentState = addToolCallDeltaToState(streamSequence[0], currentState); |
| 314 | expect(currentState.toolCall.function.name).toBe("get_weather"); |
| 315 | expect(currentState.toolCall.function.arguments).toBe(""); |
| 316 | expect(currentState.parsedArgs).toEqual({}); |
| 317 | |
| 318 | // Second delta - start JSON |
| 319 | currentState = addToolCallDeltaToState(streamSequence[1], currentState); |
| 320 | expect(currentState.toolCall.function.arguments).toBe('{"'); |
| 321 | expect(currentState.parsedArgs).toEqual({}); |
| 322 | |
| 323 | // Third delta - add key name |
| 324 | currentState = addToolCallDeltaToState(streamSequence[2], currentState); |
| 325 | expect(currentState.toolCall.function.arguments).toBe('{"location'); |
| 326 | expect(currentState.parsedArgs).toEqual({ location: undefined }); |
| 327 | |
| 328 | // Fourth delta - add key-value separator |
| 329 | currentState = addToolCallDeltaToState(streamSequence[3], currentState); |
| 330 | expect(currentState.toolCall.function.arguments).toBe('{"location":"'); |
| 331 | expect(currentState.parsedArgs).toEqual({ location: "" }); |
| 332 | |
| 333 | // Fifth delta - add first part of value |
| 334 | currentState = addToolCallDeltaToState(streamSequence[4], currentState); |
| 335 | expect(currentState.toolCall.function.arguments).toBe('{"location":"Paris'); |
| 336 | expect(currentState.parsedArgs).toEqual({ location: "Paris" }); |
| 337 | |
| 338 | // Sixth delta - add comma |
| 339 | currentState = addToolCallDeltaToState(streamSequence[5], currentState); |
| 340 | expect(currentState.toolCall.function.arguments).toBe( |
| 341 | '{"location":"Paris,', |
| 342 | ); |
| 343 | expect(currentState.parsedArgs).toEqual({ location: "Paris," }); |
| 344 | |
| 345 | // Seventh delta - add rest of value |
| 346 | currentState = addToolCallDeltaToState(streamSequence[6], currentState); |
| 347 | expect(currentState.toolCall.function.arguments).toBe( |
| 348 | '{"location":"Paris, France', |
| 349 | ); |
| 350 | expect(currentState.parsedArgs).toEqual({ location: "Paris, France" }); |
| 351 | |
| 352 | // Eighth delta - close JSON |
| 353 | currentState = addToolCallDeltaToState(streamSequence[7], currentState); |
| 354 | expect(currentState.toolCall.function.arguments).toBe( |
| 355 | '{"location":"Paris, France"}', |
| 356 | ); |
| 357 | expect(currentState.parsedArgs).toEqual({ location: "Paris, France" }); |
| 358 | }); |
| 359 | }); |