|
1 | | -import { NativeToolCallParser } from "../NativeToolCallParser" |
| 1 | +import { NativeToolCallParser, type ToolCallStreamEvent } from "../NativeToolCallParser" |
2 | 2 |
|
3 | 3 | describe("NativeToolCallParser", () => { |
4 | 4 | beforeEach(() => { |
@@ -343,4 +343,146 @@ describe("NativeToolCallParser", () => { |
343 | 343 | }) |
344 | 344 | }) |
345 | 345 | }) |
| 346 | + |
| 347 | + describe("processRawChunk streaming reassembly", () => { |
| 348 | + // Mirror the sequencing Task.ts performs: feed each raw chunk through |
| 349 | + // processRawChunk, drive startStreamingToolCall on tool_call_start, feed |
| 350 | + // tool_call_delta into processStreamingChunk, and finalize at the end. |
| 351 | + // Returns the ordered event types/ids plus the finalized tool uses by id. |
| 352 | + const drive = ( |
| 353 | + rawChunks: Array<{ index: number; id?: string; name?: string; arguments?: string }>, |
| 354 | + finishReason: string | null = "tool_calls", |
| 355 | + ) => { |
| 356 | + const events: ToolCallStreamEvent[] = [] |
| 357 | + |
| 358 | + const handleEvent = (event: ToolCallStreamEvent) => { |
| 359 | + events.push(event) |
| 360 | + if (event.type === "tool_call_start") { |
| 361 | + NativeToolCallParser.startStreamingToolCall(event.id, event.name) |
| 362 | + } else if (event.type === "tool_call_delta") { |
| 363 | + NativeToolCallParser.processStreamingChunk(event.id, event.delta) |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + for (const chunk of rawChunks) { |
| 368 | + for (const event of NativeToolCallParser.processRawChunk(chunk)) { |
| 369 | + handleEvent(event) |
| 370 | + } |
| 371 | + } |
| 372 | + |
| 373 | + // Task.ts emits ends via processFinishReason on finish_reason: "tool_calls". |
| 374 | + // Use clearRawChunkState (not finalizeRawChunks) for cleanup so we don't |
| 375 | + // double-count the end events both paths would produce. |
| 376 | + for (const event of NativeToolCallParser.processFinishReason(finishReason)) { |
| 377 | + handleEvent(event) |
| 378 | + } |
| 379 | + NativeToolCallParser.clearRawChunkState() |
| 380 | + |
| 381 | + const finalized = new Map<string, ReturnType<typeof NativeToolCallParser.finalizeStreamingToolCall>>() |
| 382 | + const startIds = events.filter((e) => e.type === "tool_call_start").map((e) => e.id) |
| 383 | + for (const id of startIds) { |
| 384 | + finalized.set(id, NativeToolCallParser.finalizeStreamingToolCall(id)) |
| 385 | + } |
| 386 | + |
| 387 | + return { events, finalized } |
| 388 | + } |
| 389 | + |
| 390 | + it("preserves leading argument bytes that arrive before the id", () => { |
| 391 | + // First chunk carries arguments but NO id; id+name arrive later, then more args. |
| 392 | + const fullArgs = JSON.stringify({ path: "src/leading.ts", mode: "slice" }) |
| 393 | + const firstHalf = fullArgs.slice(0, 10) |
| 394 | + const secondHalf = fullArgs.slice(10) |
| 395 | + |
| 396 | + const { events, finalized } = drive([ |
| 397 | + { index: 0, arguments: firstHalf }, |
| 398 | + { index: 0, id: "call_late_id", name: "read_file" }, |
| 399 | + { index: 0, arguments: secondHalf }, |
| 400 | + ]) |
| 401 | + |
| 402 | + // Exactly one start, in the right order, with the late id. |
| 403 | + const starts = events.filter((e) => e.type === "tool_call_start") |
| 404 | + expect(starts).toHaveLength(1) |
| 405 | + expect(starts[0].id).toBe("call_late_id") |
| 406 | + |
| 407 | + // The finalized arguments must contain the complete, uncorrupted payload. |
| 408 | + const result = finalized.get("call_late_id") |
| 409 | + expect(result).not.toBeNull() |
| 410 | + expect(result?.type).toBe("tool_use") |
| 411 | + if (result?.type === "tool_use") { |
| 412 | + const nativeArgs = result.nativeArgs as { path: string; mode?: string } |
| 413 | + expect(nativeArgs.path).toBe("src/leading.ts") |
| 414 | + expect(nativeArgs.mode).toBe("slice") |
| 415 | + } |
| 416 | + }) |
| 417 | + |
| 418 | + it("handles id and name arriving in separate chunks (issue #218)", () => { |
| 419 | + const fullArgs = JSON.stringify({ path: "src/split.ts" }) |
| 420 | + |
| 421 | + const { events, finalized } = drive([ |
| 422 | + { index: 0, id: "call_split" }, |
| 423 | + { index: 0, name: "read_file" }, |
| 424 | + { index: 0, arguments: fullArgs }, |
| 425 | + ]) |
| 426 | + |
| 427 | + const starts = events.filter((e) => e.type === "tool_call_start") |
| 428 | + expect(starts).toHaveLength(1) |
| 429 | + expect(starts[0].id).toBe("call_split") |
| 430 | + |
| 431 | + const result = finalized.get("call_split") |
| 432 | + expect(result?.type).toBe("tool_use") |
| 433 | + if (result?.type === "tool_use") { |
| 434 | + const nativeArgs = result.nativeArgs as { path: string } |
| 435 | + expect(nativeArgs.path).toBe("src/split.ts") |
| 436 | + } |
| 437 | + }) |
| 438 | + |
| 439 | + it("keeps two parallel tool calls on distinct indices isolated", () => { |
| 440 | + const argsA = JSON.stringify({ path: "src/a.ts" }) |
| 441 | + const argsB = JSON.stringify({ path: "src/b.ts" }) |
| 442 | + |
| 443 | + const { events, finalized } = drive([ |
| 444 | + { index: 0, arguments: argsA.slice(0, 8) }, |
| 445 | + { index: 1, arguments: argsB.slice(0, 8) }, |
| 446 | + { index: 0, id: "call_a", name: "read_file" }, |
| 447 | + { index: 1, id: "call_b", name: "read_file" }, |
| 448 | + { index: 0, arguments: argsA.slice(8) }, |
| 449 | + { index: 1, arguments: argsB.slice(8) }, |
| 450 | + ]) |
| 451 | + |
| 452 | + const starts = events.filter((e) => e.type === "tool_call_start") |
| 453 | + expect(starts).toHaveLength(2) |
| 454 | + |
| 455 | + const resultA = finalized.get("call_a") |
| 456 | + const resultB = finalized.get("call_b") |
| 457 | + if (resultA?.type === "tool_use") { |
| 458 | + expect((resultA.nativeArgs as { path: string }).path).toBe("src/a.ts") |
| 459 | + } |
| 460 | + if (resultB?.type === "tool_use") { |
| 461 | + expect((resultB.nativeArgs as { path: string }).path).toBe("src/b.ts") |
| 462 | + } |
| 463 | + }) |
| 464 | + |
| 465 | + it("emits the same event sequence for the single-chunk-with-id flow (regression guard)", () => { |
| 466 | + const fullArgs = JSON.stringify({ path: "src/single.ts" }) |
| 467 | + |
| 468 | + const { events, finalized } = drive([ |
| 469 | + { index: 0, id: "call_single", name: "read_file", arguments: fullArgs }, |
| 470 | + ]) |
| 471 | + |
| 472 | + expect(events.map((e) => e.type)).toEqual(["tool_call_start", "tool_call_delta", "tool_call_end"]) |
| 473 | + expect(events.every((e) => e.id === "call_single")).toBe(true) |
| 474 | + |
| 475 | + const result = finalized.get("call_single") |
| 476 | + if (result?.type === "tool_use") { |
| 477 | + expect((result.nativeArgs as { path: string }).path).toBe("src/single.ts") |
| 478 | + } |
| 479 | + }) |
| 480 | + |
| 481 | + it("does not emit a phantom tool_call_end for a tracker that never received an id", () => { |
| 482 | + const { events } = drive([{ index: 0, arguments: '{"path":"orphan.ts"}' }]) |
| 483 | + |
| 484 | + expect(events.filter((e) => e.type === "tool_call_start")).toHaveLength(0) |
| 485 | + expect(events.filter((e) => e.type === "tool_call_end")).toHaveLength(0) |
| 486 | + }) |
| 487 | + }) |
346 | 488 | }) |
0 commit comments