|
1 | | -import { NativeToolCallParser } from "../NativeToolCallParser" |
| 1 | +import { NativeToolCallParser, type ToolCallStreamEvent } from "../NativeToolCallParser" |
2 | 2 |
|
3 | 3 | describe("NativeToolCallParser", () => { |
4 | 4 | describe("parseToolCall", () => { |
@@ -450,4 +450,146 @@ describe("NativeToolCallParser", () => { |
450 | 450 | }) |
451 | 451 | }) |
452 | 452 | }) |
| 453 | + |
| 454 | + describe("processRawChunk streaming reassembly", () => { |
| 455 | + // Mirror the sequencing Task.ts performs: feed each raw chunk through |
| 456 | + // processRawChunk, drive startStreamingToolCall on tool_call_start, feed |
| 457 | + // tool_call_delta into processStreamingChunk, and finalize at the end. |
| 458 | + // Returns the ordered event types/ids plus the finalized tool uses by id. |
| 459 | + const drive = ( |
| 460 | + rawChunks: Array<{ index: number; id?: string; name?: string; arguments?: string }>, |
| 461 | + finishReason: string | null = "tool_calls", |
| 462 | + ) => { |
| 463 | + const events: ToolCallStreamEvent[] = [] |
| 464 | + |
| 465 | + const handleEvent = (event: ToolCallStreamEvent) => { |
| 466 | + events.push(event) |
| 467 | + if (event.type === "tool_call_start") { |
| 468 | + NativeToolCallParser.startStreamingToolCall(event.id, event.name) |
| 469 | + } else if (event.type === "tool_call_delta") { |
| 470 | + NativeToolCallParser.processStreamingChunk(event.id, event.delta) |
| 471 | + } |
| 472 | + } |
| 473 | + |
| 474 | + for (const chunk of rawChunks) { |
| 475 | + for (const event of NativeToolCallParser.processRawChunk(chunk)) { |
| 476 | + handleEvent(event) |
| 477 | + } |
| 478 | + } |
| 479 | + |
| 480 | + // Task.ts emits ends via processFinishReason on finish_reason: "tool_calls". |
| 481 | + // Use clearRawChunkState (not finalizeRawChunks) for cleanup so we don't |
| 482 | + // double-count the end events both paths would produce. |
| 483 | + for (const event of NativeToolCallParser.processFinishReason(finishReason)) { |
| 484 | + handleEvent(event) |
| 485 | + } |
| 486 | + NativeToolCallParser.clearRawChunkState() |
| 487 | + |
| 488 | + const finalized = new Map<string, ReturnType<typeof NativeToolCallParser.finalizeStreamingToolCall>>() |
| 489 | + const startIds = events.filter((e) => e.type === "tool_call_start").map((e) => e.id) |
| 490 | + for (const id of startIds) { |
| 491 | + finalized.set(id, NativeToolCallParser.finalizeStreamingToolCall(id)) |
| 492 | + } |
| 493 | + |
| 494 | + return { events, finalized } |
| 495 | + } |
| 496 | + |
| 497 | + it("preserves leading argument bytes that arrive before the id", () => { |
| 498 | + // First chunk carries arguments but NO id; id+name arrive later, then more args. |
| 499 | + const fullArgs = JSON.stringify({ path: "src/leading.ts", mode: "slice" }) |
| 500 | + const firstHalf = fullArgs.slice(0, 10) |
| 501 | + const secondHalf = fullArgs.slice(10) |
| 502 | + |
| 503 | + const { events, finalized } = drive([ |
| 504 | + { index: 0, arguments: firstHalf }, |
| 505 | + { index: 0, id: "call_late_id", name: "read_file" }, |
| 506 | + { index: 0, arguments: secondHalf }, |
| 507 | + ]) |
| 508 | + |
| 509 | + // Exactly one start, in the right order, with the late id. |
| 510 | + const starts = events.filter((e) => e.type === "tool_call_start") |
| 511 | + expect(starts).toHaveLength(1) |
| 512 | + expect(starts[0].id).toBe("call_late_id") |
| 513 | + |
| 514 | + // The finalized arguments must contain the complete, uncorrupted payload. |
| 515 | + const result = finalized.get("call_late_id") |
| 516 | + expect(result).not.toBeNull() |
| 517 | + expect(result?.type).toBe("tool_use") |
| 518 | + if (result?.type === "tool_use") { |
| 519 | + const nativeArgs = result.nativeArgs as { path: string; mode?: string } |
| 520 | + expect(nativeArgs.path).toBe("src/leading.ts") |
| 521 | + expect(nativeArgs.mode).toBe("slice") |
| 522 | + } |
| 523 | + }) |
| 524 | + |
| 525 | + it("handles id and name arriving in separate chunks (issue #218)", () => { |
| 526 | + const fullArgs = JSON.stringify({ path: "src/split.ts" }) |
| 527 | + |
| 528 | + const { events, finalized } = drive([ |
| 529 | + { index: 0, id: "call_split" }, |
| 530 | + { index: 0, name: "read_file" }, |
| 531 | + { index: 0, arguments: fullArgs }, |
| 532 | + ]) |
| 533 | + |
| 534 | + const starts = events.filter((e) => e.type === "tool_call_start") |
| 535 | + expect(starts).toHaveLength(1) |
| 536 | + expect(starts[0].id).toBe("call_split") |
| 537 | + |
| 538 | + const result = finalized.get("call_split") |
| 539 | + expect(result?.type).toBe("tool_use") |
| 540 | + if (result?.type === "tool_use") { |
| 541 | + const nativeArgs = result.nativeArgs as { path: string } |
| 542 | + expect(nativeArgs.path).toBe("src/split.ts") |
| 543 | + } |
| 544 | + }) |
| 545 | + |
| 546 | + it("keeps two parallel tool calls on distinct indices isolated", () => { |
| 547 | + const argsA = JSON.stringify({ path: "src/a.ts" }) |
| 548 | + const argsB = JSON.stringify({ path: "src/b.ts" }) |
| 549 | + |
| 550 | + const { events, finalized } = drive([ |
| 551 | + { index: 0, arguments: argsA.slice(0, 8) }, |
| 552 | + { index: 1, arguments: argsB.slice(0, 8) }, |
| 553 | + { index: 0, id: "call_a", name: "read_file" }, |
| 554 | + { index: 1, id: "call_b", name: "read_file" }, |
| 555 | + { index: 0, arguments: argsA.slice(8) }, |
| 556 | + { index: 1, arguments: argsB.slice(8) }, |
| 557 | + ]) |
| 558 | + |
| 559 | + const starts = events.filter((e) => e.type === "tool_call_start") |
| 560 | + expect(starts).toHaveLength(2) |
| 561 | + |
| 562 | + const resultA = finalized.get("call_a") |
| 563 | + const resultB = finalized.get("call_b") |
| 564 | + if (resultA?.type === "tool_use") { |
| 565 | + expect((resultA.nativeArgs as { path: string }).path).toBe("src/a.ts") |
| 566 | + } |
| 567 | + if (resultB?.type === "tool_use") { |
| 568 | + expect((resultB.nativeArgs as { path: string }).path).toBe("src/b.ts") |
| 569 | + } |
| 570 | + }) |
| 571 | + |
| 572 | + it("emits the same event sequence for the single-chunk-with-id flow (regression guard)", () => { |
| 573 | + const fullArgs = JSON.stringify({ path: "src/single.ts" }) |
| 574 | + |
| 575 | + const { events, finalized } = drive([ |
| 576 | + { index: 0, id: "call_single", name: "read_file", arguments: fullArgs }, |
| 577 | + ]) |
| 578 | + |
| 579 | + expect(events.map((e) => e.type)).toEqual(["tool_call_start", "tool_call_delta", "tool_call_end"]) |
| 580 | + expect(events.every((e) => e.id === "call_single")).toBe(true) |
| 581 | + |
| 582 | + const result = finalized.get("call_single") |
| 583 | + if (result?.type === "tool_use") { |
| 584 | + expect((result.nativeArgs as { path: string }).path).toBe("src/single.ts") |
| 585 | + } |
| 586 | + }) |
| 587 | + |
| 588 | + it("does not emit a phantom tool_call_end for a tracker that never received an id", () => { |
| 589 | + const { events } = drive([{ index: 0, arguments: '{"path":"orphan.ts"}' }]) |
| 590 | + |
| 591 | + expect(events.filter((e) => e.type === "tool_call_start")).toHaveLength(0) |
| 592 | + expect(events.filter((e) => e.type === "tool_call_end")).toHaveLength(0) |
| 593 | + }) |
| 594 | + }) |
453 | 595 | }) |
0 commit comments