Elffuss BraidHub

kikocis/continue public

main · 13.7 KiBRawPermalink
1import { ToolCallDelta, ToolCallState } from "core";
2import { addToolCallDeltaToState } from "./toolCallState";
3
4describe("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 handle name streaming in full progressive chunks", () => {
61 // Test case where model streams the name progressively but includes full prefix each time
62 // e.g. "readFi" -> "readFil" -> "readFile"
63 const currentState: ToolCallState = {
64 status: "generating",
65 toolCall: {
66 id: "call123",
67 type: "function",
68 function: {
69 name: "readFi",
70 arguments: "{}",
71 },
72 },
73 toolCallId: "call123",
74 parsedArgs: {},
75 };
76
77 const delta: ToolCallDelta = {
78 function: {
79 name: "readFil",
80 },
81 };
82
83 const result = addToolCallDeltaToState(delta, currentState);
84 expect(result.toolCall.function.name).toBe("readFil");
85
86 // Continue the streaming
87 const nextDelta: ToolCallDelta = {
88 function: {
89 name: "readFile",
90 },
91 };
92
93 const finalResult = addToolCallDeltaToState(nextDelta, result);
94 expect(finalResult.toolCall.function.name).toBe("readFile");
95 });
96
97 it("should keep original name when receiving duplicate name chunks", () => {
98 // Test case where model streams the complete name multiple times
99 // e.g. "readFile" -> "readFile" -> "readFile"
100 const currentState: ToolCallState = {
101 status: "generating",
102 toolCall: {
103 id: "call123",
104 type: "function",
105 function: {
106 name: "readFile",
107 arguments: "{}",
108 },
109 },
110 toolCallId: "call123",
111 parsedArgs: {},
112 };
113
114 const delta: ToolCallDelta = {
115 function: {
116 name: "readFile",
117 },
118 };
119
120 const result = addToolCallDeltaToState(delta, currentState);
121 expect(result.toolCall.function.name).toBe("readFile");
122 });
123
124 it("should handle partial name streaming", () => {
125 // Test case where model streams the name in parts
126 // e.g. "read" -> "File"
127 const currentState: ToolCallState = {
128 status: "generating",
129 toolCall: {
130 id: "call123",
131 type: "function",
132 function: {
133 name: "read",
134 arguments: "{}",
135 },
136 },
137 toolCallId: "call123",
138 parsedArgs: {},
139 };
140
141 const delta: ToolCallDelta = {
142 function: {
143 name: "File",
144 },
145 };
146
147 const result = addToolCallDeltaToState(delta, currentState);
148 expect(result.toolCall.function.name).toBe("readFile");
149 });
150
151 it("should ignore new tool calls with different IDs", () => {
152 const currentState: ToolCallState = {
153 status: "generating",
154 toolCall: {
155 id: "call123",
156 type: "function",
157 function: {
158 name: "searchFiles",
159 arguments: '{"query":"test"}',
160 },
161 },
162 toolCallId: "call123",
163 parsedArgs: { query: "test" },
164 };
165
166 const delta: ToolCallDelta = {
167 id: "call456", // Different ID
168 type: "function",
169 function: {
170 name: "readFile",
171 arguments: '{"path":"file.txt"}',
172 },
173 };
174
175 const result = addToolCallDeltaToState(delta, currentState);
176
177 // Should keep the original state and ignore the new call
178 expect(result).toBe(currentState);
179 expect(result.toolCall.id).toBe("call123");
180 expect(result.toolCall.function.name).toBe("searchFiles");
181 });
182
183 it("should merge function argument deltas correctly", () => {
184 const currentState: ToolCallState = {
185 status: "generating",
186 toolCall: {
187 id: "call123",
188 type: "function",
189 function: {
190 name: "searchFiles",
191 arguments: '{"query":"java',
192 },
193 },
194 toolCallId: "call123",
195 parsedArgs: { query: "java" },
196 };
197
198 const delta: ToolCallDelta = {
199 function: {
200 arguments: 'script"}',
201 },
202 };
203
204 const result = addToolCallDeltaToState(delta, currentState);
205
206 expect(result.toolCall.function.arguments).toBe('{"query":"javascript"}');
207 expect(result.parsedArgs).toEqual({ query: "javascript" });
208 });
209
210 it("should handle empty deltas gracefully", () => {
211 const currentState: ToolCallState = {
212 status: "generating",
213 toolCall: {
214 id: "call123",
215 type: "function",
216 function: {
217 name: "searchFiles",
218 arguments: '{"query":"test"}',
219 },
220 },
221 toolCallId: "call123",
222 parsedArgs: { query: "test" },
223 };
224
225 const delta: ToolCallDelta = {};
226
227 const result = addToolCallDeltaToState(delta, currentState);
228
229 expect(result).toEqual(currentState);
230 });
231
232 it("should handle streaming complex JSON arguments", () => {
233 const initialState: ToolCallState = {
234 status: "generating",
235 toolCall: {
236 id: "call123",
237 type: "function",
238 function: {
239 name: "editFile",
240 arguments: '{"path":"',
241 },
242 },
243 toolCallId: "call123",
244 parsedArgs: { path: undefined },
245 };
246
247 // First chunk - add to path
248 const delta1: ToolCallDelta = {
249 function: {
250 arguments: 'src/main.js","changes":{"',
251 },
252 };
253
254 const state1 = addToolCallDeltaToState(delta1, initialState);
255 expect(state1.toolCall.function.arguments).toBe(
256 '{"path":"src/main.js","changes":{"',
257 );
258
259 // Second chunk - add property name
260 const delta2: ToolCallDelta = {
261 function: {
262 arguments: 'content":"cons',
263 },
264 };
265
266 const state2 = addToolCallDeltaToState(delta2, state1);
267 expect(state2.toolCall.function.arguments).toBe(
268 '{"path":"src/main.js","changes":{"content":"cons',
269 );
270 expect(state2.parsedArgs).toEqual({
271 path: "src/main.js",
272 changes: { content: "cons" },
273 });
274
275 // Third chunk - complete JSON
276 const delta3: ToolCallDelta = {
277 function: {
278 arguments: 't message = \\"Hello\\";"}}',
279 },
280 };
281
282 const state3 = addToolCallDeltaToState(delta3, state2);
283 expect(state3.toolCall.function.arguments).toBe(
284 '{"path":"src/main.js","changes":{"content":"const message = \\"Hello\\";"}}',
285 );
286 expect(state3.parsedArgs).toEqual({
287 path: "src/main.js",
288 changes: { content: 'const message = "Hello";' },
289 });
290 });
291
292 it("should handle tool call ids correctly", () => {
293 // Test when ID comes from current state
294 let currentState: ToolCallState = {
295 status: "generating",
296 toolCall: {
297 id: "existing-id",
298 type: "function",
299 function: {
300 name: "test",
301 arguments: "{}",
302 },
303 },
304 toolCallId: "existing-id",
305 parsedArgs: {},
306 };
307
308 let delta: ToolCallDelta = {
309 // No id in delta
310 function: {
311 name: "Function",
312 },
313 };
314
315 let result = addToolCallDeltaToState(delta, currentState);
316 expect(result.toolCallId).toBe("existing-id");
317
318 // Test when ID comes from delta
319 delta = {
320 id: "new-id",
321 function: {
322 name: "test",
323 },
324 };
325
326 result = addToolCallDeltaToState(delta, undefined);
327 expect(result.toolCallId).toBe("new-id");
328 });
329
330 it("should handle invalid JSON gracefully", () => {
331 const currentState: ToolCallState = {
332 status: "generating",
333 toolCall: {
334 id: "call123",
335 type: "function",
336 function: {
337 name: "searchFiles",
338 arguments: '{"query":',
339 },
340 },
341 toolCallId: "call123",
342 parsedArgs: { query: undefined },
343 };
344
345 const delta: ToolCallDelta = {
346 function: {
347 arguments: '"test"',
348 },
349 };
350
351 const result = addToolCallDeltaToState(delta, currentState);
352
353 expect(result.toolCall.function.arguments).toBe('{"query":"test"');
354 // Expect partial parsing still works
355 expect(result.parsedArgs).toEqual({ query: "test" });
356 });
357
358 it("should handle realistic weather API streaming pattern from OpenAI example", () => {
359 // https://platform.openai.com/docs/guides/function-calling?api-mode=chat#streaming
360 // Sequence of deltas as seen in the streaming example
361 const streamSequence: ToolCallDelta[] = [
362 {
363 id: "call_DdmO9pD3xa9XTPNJ32zg2hcA",
364 type: "function",
365 function: {
366 name: "get_weather",
367 arguments: "",
368 },
369 },
370 {
371 function: {
372 arguments: '{"',
373 },
374 },
375 {
376 function: {
377 arguments: "location",
378 },
379 },
380 {
381 function: {
382 arguments: '":"',
383 },
384 },
385 {
386 function: {
387 arguments: "Paris",
388 },
389 },
390 {
391 function: {
392 arguments: ",",
393 },
394 },
395 {
396 function: {
397 arguments: " France",
398 },
399 },
400 {
401 function: {
402 arguments: '"}',
403 },
404 },
405 ];
406
407 // Initialize with undefined state
408 let currentState: ToolCallState | undefined = undefined;
409
410 // Process each delta in sequence
411 for (const delta of streamSequence) {
412 currentState = addToolCallDeltaToState(delta, currentState);
413 }
414
415 // Check the final state
416 expect(currentState).toEqual({
417 status: "generating",
418 toolCall: {
419 id: "call_DdmO9pD3xa9XTPNJ32zg2hcA",
420 type: "function",
421 function: {
422 name: "get_weather",
423 arguments: '{"location":"Paris, France"}',
424 },
425 },
426 toolCallId: "call_DdmO9pD3xa9XTPNJ32zg2hcA",
427 parsedArgs: {
428 location: "Paris, France",
429 },
430 });
431
432 // Check the intermediate state after each delta to ensure incremental parsing works
433 currentState = undefined;
434
435 // First delta - initialize
436 currentState = addToolCallDeltaToState(streamSequence[0], currentState);
437 expect(currentState.toolCall.function.name).toBe("get_weather");
438 expect(currentState.toolCall.function.arguments).toBe("");
439 expect(currentState.parsedArgs).toEqual({});
440
441 // Second delta - start JSON
442 currentState = addToolCallDeltaToState(streamSequence[1], currentState);
443 expect(currentState.toolCall.function.arguments).toBe('{"');
444 expect(currentState.parsedArgs).toEqual({});
445
446 // Third delta - add key name
447 currentState = addToolCallDeltaToState(streamSequence[2], currentState);
448 expect(currentState.toolCall.function.arguments).toBe('{"location');
449 expect(currentState.parsedArgs).toEqual({ location: undefined });
450
451 // Fourth delta - add key-value separator
452 currentState = addToolCallDeltaToState(streamSequence[3], currentState);
453 expect(currentState.toolCall.function.arguments).toBe('{"location":"');
454 expect(currentState.parsedArgs).toEqual({ location: "" });
455
456 // Fifth delta - add first part of value
457 currentState = addToolCallDeltaToState(streamSequence[4], currentState);
458 expect(currentState.toolCall.function.arguments).toBe('{"location":"Paris');
459 expect(currentState.parsedArgs).toEqual({ location: "Paris" });
460
461 // Sixth delta - add comma
462 currentState = addToolCallDeltaToState(streamSequence[5], currentState);
463 expect(currentState.toolCall.function.arguments).toBe(
464 '{"location":"Paris,',
465 );
466 expect(currentState.parsedArgs).toEqual({ location: "Paris," });
467
468 // Seventh delta - add rest of value
469 currentState = addToolCallDeltaToState(streamSequence[6], currentState);
470 expect(currentState.toolCall.function.arguments).toBe(
471 '{"location":"Paris, France',
472 );
473 expect(currentState.parsedArgs).toEqual({ location: "Paris, France" });
474
475 // Eighth delta - close JSON
476 currentState = addToolCallDeltaToState(streamSequence[7], currentState);
477 expect(currentState.toolCall.function.arguments).toBe(
478 '{"location":"Paris, France"}',
479 );
480 expect(currentState.parsedArgs).toEqual({ location: "Paris, France" });
481 });
482
483 it("should handle when args are complete JSON and new deltas arrive", () => {
484 // When args are already a valid JSON object, new deltas should not modify it
485 const currentState: ToolCallState = {
486 status: "generating",
487 toolCall: {
488 id: "call123",
489 type: "function",
490 function: {
491 name: "searchFiles",
492 arguments: '{"query":"test","limit":10}',
493 },
494 },
495 toolCallId: "call123",
496 parsedArgs: { query: "test", limit: 10 },
497 };
498
499 const delta: ToolCallDelta = {
500 function: {
501 arguments: ',"sort":"asc"}',
502 },
503 };
504
505 // The complete JSON should not be modified
506 const result = addToolCallDeltaToState(delta, currentState);
507 expect(result.toolCall.function.arguments).toBe(
508 '{"query":"test","limit":10}',
509 );
510 expect(result.parsedArgs).toEqual({ query: "test", limit: 10 });
511 });
512});