Commit 87566be
authored
fix: defer tool-input-start until real tool call ID is available (#103)
* fix: defer tool-input-start until real tool call ID is available
When streaming tool calls from Gemini-on-Vertex, the function name and
tool call ID may arrive in separate chunks. Previously, tool-input-start
was emitted with a placeholder ID (tool_0) as soon as the function name
was known, causing a mismatch with subsequent tool-input-delta events
that carried the real ID.
This fix:
- Makes ToolCallInProgress.id optional (undefined until real ID arrives)
- Gates tool-input-start emission on both toolName AND id being present
- Makes the ID immutable once set (first real value wins)
- Falls back to generateToolCallId() (UUID) in flush/finishReason paths
Aligns with the Vercel AI SDK StreamingToolCallTracker pattern where the
ID is set once and never mutated.
Closes #93
* test: assert tool-input-delta count and content in Vertex regression test
Strengthen the regression test to verify that tool-input-delta events
are actually emitted (not vacuously passing via empty loop) and that
concatenated deltas match the expected tool call input.
* fix: replay buffered arguments as tool-input-delta after deferred start
When arguments arrive on a chunk before the tool call ID, they are
buffered but not emitted as tool-input-delta. After tool-input-start is
finally emitted (once the ID arrives), replay any accumulated arguments
as a single tool-input-delta to ensure streaming consumers see all
intermediate argument data.
* test: add coverage for buffered-arguments replay path
Exercise the code path where arguments arrive before the tool call ID,
verifying that buffered arguments are replayed as tool-input-delta
immediately after the deferred tool-input-start is emitted.
* test: add coverage for fallback UUID generation when API provides no id
Verify that when the API never provides a tool call ID across any chunk,
the provider generates a valid UUID and uses it consistently across
tool-input-start and tool-call events.
* test: update existing test to reflect deferred tool-input-start behavior
The test 'should flush tool calls that never received input-start' now
exercises a different code path with the fix: tool-input-start IS emitted
during streaming (when toolName arrives on chunk 2 and id was already set
on chunk 1). Rename and strengthen assertions to match actual behavior:
verify tool-input-start, buffered args replay as deltas, and tool-call.
* fix: replay buffered arguments in flush and finishReason paths
For protocol completeness, emit buffered arguments as tool-input-delta
between tool-input-start and tool-input-end in finalization paths (flush
and finishReason handler), matching the behavior of the streaming path.
* refactor: extract emitToolInputStart helper and guard against empty string IDs
Address review comments:
- Extract repeated tool-input-start + buffered replay pattern into a
single helper to prevent drift between the 3 emission sites
- Guard against empty string IDs (consistent with existing toolName
guard that checks length > 0)
- Strengthen UUID fallback test to assert tool-input-delta emission1 parent d5145c1 commit 87566be
2 files changed
Lines changed: 248 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2401 | 2401 | | |
2402 | 2402 | | |
2403 | 2403 | | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
| 2409 | + | |
| 2410 | + | |
| 2411 | + | |
| 2412 | + | |
| 2413 | + | |
| 2414 | + | |
| 2415 | + | |
| 2416 | + | |
| 2417 | + | |
| 2418 | + | |
| 2419 | + | |
| 2420 | + | |
| 2421 | + | |
| 2422 | + | |
| 2423 | + | |
| 2424 | + | |
| 2425 | + | |
| 2426 | + | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
| 2455 | + | |
| 2456 | + | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
| 2467 | + | |
| 2468 | + | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
| 2472 | + | |
| 2473 | + | |
| 2474 | + | |
| 2475 | + | |
| 2476 | + | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
| 2489 | + | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
| 2518 | + | |
| 2519 | + | |
| 2520 | + | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
| 2530 | + | |
| 2531 | + | |
| 2532 | + | |
| 2533 | + | |
| 2534 | + | |
| 2535 | + | |
| 2536 | + | |
| 2537 | + | |
| 2538 | + | |
| 2539 | + | |
| 2540 | + | |
| 2541 | + | |
| 2542 | + | |
| 2543 | + | |
| 2544 | + | |
| 2545 | + | |
| 2546 | + | |
| 2547 | + | |
| 2548 | + | |
| 2549 | + | |
| 2550 | + | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | + | |
| 2556 | + | |
| 2557 | + | |
| 2558 | + | |
| 2559 | + | |
| 2560 | + | |
| 2561 | + | |
| 2562 | + | |
| 2563 | + | |
| 2564 | + | |
| 2565 | + | |
| 2566 | + | |
| 2567 | + | |
| 2568 | + | |
| 2569 | + | |
| 2570 | + | |
| 2571 | + | |
| 2572 | + | |
| 2573 | + | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
| 2578 | + | |
| 2579 | + | |
| 2580 | + | |
| 2581 | + | |
| 2582 | + | |
| 2583 | + | |
2404 | 2584 | | |
2405 | 2585 | | |
2406 | 2586 | | |
| |||
2727 | 2907 | | |
2728 | 2908 | | |
2729 | 2909 | | |
2730 | | - | |
| 2910 | + | |
2731 | 2911 | | |
2732 | 2912 | | |
2733 | 2913 | | |
| |||
2760 | 2940 | | |
2761 | 2941 | | |
2762 | 2942 | | |
| 2943 | + | |
| 2944 | + | |
| 2945 | + | |
| 2946 | + | |
| 2947 | + | |
| 2948 | + | |
| 2949 | + | |
| 2950 | + | |
2763 | 2951 | | |
| 2952 | + | |
| 2953 | + | |
| 2954 | + | |
| 2955 | + | |
| 2956 | + | |
| 2957 | + | |
| 2958 | + | |
| 2959 | + | |
| 2960 | + | |
2764 | 2961 | | |
2765 | 2962 | | |
2766 | 2963 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
335 | | - | |
| 335 | + | |
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
| |||
353 | 353 | | |
354 | 354 | | |
355 | 355 | | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
356 | 363 | | |
357 | 364 | | |
358 | 365 | | |
| |||
782 | 789 | | |
783 | 790 | | |
784 | 791 | | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
785 | 817 | | |
786 | 818 | | |
787 | 819 | | |
| |||
795 | 827 | | |
796 | 828 | | |
797 | 829 | | |
798 | | - | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
803 | | - | |
804 | | - | |
805 | | - | |
| 830 | + | |
| 831 | + | |
806 | 832 | | |
807 | 833 | | |
808 | 834 | | |
| |||
952 | 978 | | |
953 | 979 | | |
954 | 980 | | |
955 | | - | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
956 | 985 | | |
957 | 986 | | |
958 | 987 | | |
959 | 988 | | |
960 | 989 | | |
961 | 990 | | |
962 | 991 | | |
963 | | - | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
964 | 997 | | |
965 | 998 | | |
966 | 999 | | |
| |||
969 | 1002 | | |
970 | 1003 | | |
971 | 1004 | | |
972 | | - | |
973 | | - | |
974 | | - | |
975 | | - | |
976 | | - | |
977 | | - | |
978 | | - | |
| 1005 | + | |
| 1006 | + | |
979 | 1007 | | |
980 | 1008 | | |
981 | 1009 | | |
982 | 1010 | | |
983 | 1011 | | |
984 | 1012 | | |
985 | | - | |
| 1013 | + | |
986 | 1014 | | |
987 | 1015 | | |
988 | 1016 | | |
| |||
1003 | 1031 | | |
1004 | 1032 | | |
1005 | 1033 | | |
1006 | | - | |
1007 | | - | |
1008 | | - | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
1014 | 1037 | | |
1015 | 1038 | | |
1016 | 1039 | | |
| |||
0 commit comments