@@ -2,7 +2,7 @@ import { describe, it, expect } from "@jest/globals";
2
2
import { AIMessageChunk } from "../ai.js" ;
3
3
4
4
describe ( "AIMessageChunk" , ( ) => {
5
- it ( "should properly merge tool call chunks" , ( ) => {
5
+ it ( "should properly merge tool call chunks that have matching indices and ids " , ( ) => {
6
6
const chunk1 = new AIMessageChunk ( {
7
7
content : "" ,
8
8
tool_call_chunks : [
@@ -45,4 +45,71 @@ describe("AIMessageChunk", () => {
45
45
) ;
46
46
expect ( secondCall ?. id ) . toBe ( "5abf542e-87f3-4899-87c6-8f7d9cb6a28d" ) ;
47
47
} ) ;
48
+
49
+ it ( "should properly merge tool call chunks that have matching indices and at least one id is blank" , ( ) => {
50
+ const chunk1 = new AIMessageChunk ( {
51
+ content : "" ,
52
+ tool_call_chunks : [
53
+ {
54
+ name : "add_new_task" ,
55
+ type : "tool_call_chunk" ,
56
+ index : 0 ,
57
+ id : "9fb5c937-6944-4173-84be-ad1caee1cedd" ,
58
+ } ,
59
+ ] ,
60
+ } ) ;
61
+ const chunk2 = new AIMessageChunk ( {
62
+ content : "" ,
63
+ tool_call_chunks : [
64
+ {
65
+ args : '{"tasks":["buy tomatoes","help child with math"]}' ,
66
+ type : "tool_call_chunk" ,
67
+ index : 0 ,
68
+ } ,
69
+ ] ,
70
+ } ) ;
71
+
72
+ const merged = chunk1 . concat ( chunk2 ) ;
73
+ expect ( merged . tool_call_chunks ) . toHaveLength ( 1 ) ;
74
+
75
+ const firstCall = merged . tool_call_chunks ?. [ 0 ] ;
76
+ expect ( firstCall ?. name ) . toBe ( "add_new_task" ) ;
77
+ expect ( firstCall ?. args ) . toBe (
78
+ '{"tasks":["buy tomatoes","help child with math"]}'
79
+ ) ;
80
+ expect ( firstCall ?. id ) . toBe ( "9fb5c937-6944-4173-84be-ad1caee1cedd" ) ;
81
+ } ) ;
82
+
83
+ it ( "should properly merge tool call chunks that have matching indices no IDs at all" , ( ) => {
84
+ const chunk1 = new AIMessageChunk ( {
85
+ content : "" ,
86
+ tool_call_chunks : [
87
+ {
88
+ name : "add_new_task" ,
89
+ type : "tool_call_chunk" ,
90
+ index : 0 ,
91
+ } ,
92
+ ] ,
93
+ } ) ;
94
+ const chunk2 = new AIMessageChunk ( {
95
+ content : "" ,
96
+ tool_call_chunks : [
97
+ {
98
+ args : '{"tasks":["buy tomatoes","help child with math"]}' ,
99
+ type : "tool_call_chunk" ,
100
+ index : 0 ,
101
+ } ,
102
+ ] ,
103
+ } ) ;
104
+
105
+ const merged = chunk1 . concat ( chunk2 ) ;
106
+ expect ( merged . tool_call_chunks ) . toHaveLength ( 1 ) ;
107
+
108
+ const firstCall = merged . tool_call_chunks ?. [ 0 ] ;
109
+ expect ( firstCall ?. name ) . toBe ( "add_new_task" ) ;
110
+ expect ( firstCall ?. args ) . toBe (
111
+ '{"tasks":["buy tomatoes","help child with math"]}'
112
+ ) ;
113
+ expect ( firstCall ?. id ) . toBeUndefined ( ) ;
114
+ } ) ;
48
115
} ) ;
0 commit comments