@@ -467,56 +467,218 @@ describe("Complex AIMessageChunk concat", () => {
467467 } ) ;
468468
469469 it ( "concatenates tool call chunks without IDs" , ( ) => {
470- const chunks : ToolCallChunk [ ] = [
471- {
472- name : "get_current_time" ,
473- type : "tool_call_chunk" ,
474- index : 0 ,
475- // no `id` provided
476- } ,
470+ const chunks = [
471+ new AIMessageChunk ( {
472+ id : "chatcmpl-x" ,
473+ content : "" ,
474+ tool_call_chunks : [
475+ {
476+ name : "get_weather" ,
477+ args : "" ,
478+ id : "call_q6ZzjkLjKNYb4DizyMOaqpfW" ,
479+ index : 0 ,
480+ type : "tool_call_chunk" ,
481+ } ,
482+ ] ,
483+ } ) ,
484+ new AIMessageChunk ( {
485+ id : "chatcmpl-x" ,
486+ content : "" ,
487+ tool_call_chunks : [
488+ {
489+ args : '{"' ,
490+ index : 0 ,
491+ type : "tool_call_chunk" ,
492+ } ,
493+ ] ,
494+ } ) ,
495+ new AIMessageChunk ( {
496+ id : "chatcmpl-x" ,
497+ content : "" ,
498+ tool_call_chunks : [
499+ {
500+ args : "location" ,
501+ index : 0 ,
502+ type : "tool_call_chunk" ,
503+ } ,
504+ ] ,
505+ } ) ,
506+ new AIMessageChunk ( {
507+ id : "chatcmpl-x" ,
508+ content : "" ,
509+ tool_call_chunks : [
510+ {
511+ args : '":"' ,
512+ index : 0 ,
513+ type : "tool_call_chunk" ,
514+ } ,
515+ ] ,
516+ } ) ,
517+ new AIMessageChunk ( {
518+ id : "chatcmpl-x" ,
519+ content : "" ,
520+ tool_call_chunks : [
521+ {
522+ args : "San" ,
523+ index : 0 ,
524+ type : "tool_call_chunk" ,
525+ } ,
526+ ] ,
527+ } ) ,
528+ new AIMessageChunk ( {
529+ id : "chatcmpl-x" ,
530+ content : "" ,
531+ tool_call_chunks : [
532+ {
533+ args : " Francisco" ,
534+ index : 0 ,
535+ type : "tool_call_chunk" ,
536+ } ,
537+ ] ,
538+ } ) ,
539+ new AIMessageChunk ( {
540+ id : "chatcmpl-x" ,
541+ content : "" ,
542+ tool_call_chunks : [
543+ {
544+ args : '"}' ,
545+ index : 0 ,
546+ type : "tool_call_chunk" ,
547+ } ,
548+ ] ,
549+ } ) ,
477550 ] ;
478-
479- const result = new AIMessageChunk ( {
480- content : "" ,
481- tool_call_chunks : chunks ,
482- } ) ;
483-
484- expect ( result . tool_calls ?. length ) . toBe ( 1 ) ;
485- expect ( result . invalid_tool_calls ?. length ) . toBe ( 0 ) ;
486- expect ( result . tool_calls ) . toEqual ( [
551+ let finalChunk = new AIMessageChunk ( "" ) ;
552+ for ( const chunk of chunks ) {
553+ finalChunk = finalChunk . concat ( chunk ) ;
554+ }
555+ expect ( finalChunk . tool_calls ) . toHaveLength ( 1 ) ;
556+ expect ( finalChunk . tool_calls ) . toEqual ( [
487557 {
488- id : "fallback-0" , // Should get fallback ID
489- name : "get_current_time" ,
490- args : { } ,
491558 type : "tool_call" ,
559+ name : "get_weather" ,
560+ args : {
561+ location : "San Francisco" ,
562+ } ,
563+ id : "call_q6ZzjkLjKNYb4DizyMOaqpfW" ,
492564 } ,
493565 ] ) ;
494566 } ) ;
567+ } ) ;
495568
496- it ( "concatenates tool call chunks without IDs and no index" , ( ) => {
497- const chunks : ToolCallChunk [ ] = [
498- {
499- name : "get_current_time" ,
500- type : "tool_call_chunk" ,
501- // no `id` or `index` provided
502- } ,
503- ] ;
569+ describe ( "AIMessageChunk" , ( ) => {
570+ describe ( "constructor" , ( ) => {
571+ it ( "omits tool call chunks without IDs" , ( ) => {
572+ const chunks : ToolCallChunk [ ] = [
573+ {
574+ name : "get_current_time" ,
575+ type : "tool_call_chunk" ,
576+ index : 0 ,
577+ // no `id` provided
578+ } ,
579+ ] ;
504580
505- const result = new AIMessageChunk ( {
506- content : "" ,
507- tool_call_chunks : chunks ,
581+ const result = new AIMessageChunk ( {
582+ content : "" ,
583+ tool_call_chunks : chunks ,
584+ } ) ;
585+
586+ expect ( result . tool_calls ?. length ) . toBe ( 0 ) ;
587+ expect ( result . invalid_tool_calls ?. length ) . toBe ( 1 ) ;
588+ expect ( result . invalid_tool_calls ) . toEqual ( [
589+ {
590+ type : "invalid_tool_call" ,
591+ id : undefined ,
592+ name : "get_current_time" ,
593+ args : "{}" ,
594+ error : "Malformed args." ,
595+ } ,
596+ ] ) ;
508597 } ) ;
509598
510- expect ( result . tool_calls ?. length ) . toBe ( 1 ) ;
511- expect ( result . invalid_tool_calls ?. length ) . toBe ( 0 ) ;
512- expect ( result . tool_calls ) . toEqual ( [
513- {
514- id : "fallback-0" , // Should get fallback ID with index 0
515- name : "get_current_time" ,
516- args : { } ,
517- type : "tool_call" ,
518- } ,
519- ] ) ;
599+ it ( "omits tool call chunks without IDs and no index" , ( ) => {
600+ const chunks : ToolCallChunk [ ] = [
601+ {
602+ name : "get_current_time" ,
603+ type : "tool_call_chunk" ,
604+ // no `id` or `index` provided
605+ } ,
606+ ] ;
607+
608+ const result = new AIMessageChunk ( {
609+ content : "" ,
610+ tool_call_chunks : chunks ,
611+ } ) ;
612+
613+ expect ( result . tool_calls ?. length ) . toBe ( 0 ) ;
614+ expect ( result . invalid_tool_calls ?. length ) . toBe ( 1 ) ;
615+ expect ( result . invalid_tool_calls ) . toEqual ( [
616+ {
617+ type : "invalid_tool_call" ,
618+ id : undefined ,
619+ name : "get_current_time" ,
620+ args : "{}" ,
621+ error : "Malformed args." ,
622+ } ,
623+ ] ) ;
624+ } ) ;
625+
626+ it ( "can concatenate tool call chunks without IDs" , ( ) => {
627+ const chunk = new AIMessageChunk ( {
628+ id : "chatcmpl-x" ,
629+ content : "" ,
630+ tool_call_chunks : [
631+ {
632+ name : "get_weather" ,
633+ args : "" ,
634+ id : "call_q6ZzjkLjKNYb4DizyMOaqpfW" ,
635+ index : 0 ,
636+ type : "tool_call_chunk" ,
637+ } ,
638+ {
639+ args : '{"' ,
640+ index : 0 ,
641+ type : "tool_call_chunk" ,
642+ } ,
643+ {
644+ args : "location" ,
645+ index : 0 ,
646+ type : "tool_call_chunk" ,
647+ } ,
648+ {
649+ args : '":"' ,
650+ index : 0 ,
651+ type : "tool_call_chunk" ,
652+ } ,
653+ {
654+ args : "San" ,
655+ index : 0 ,
656+ type : "tool_call_chunk" ,
657+ } ,
658+ {
659+ args : " Francisco" ,
660+ index : 0 ,
661+ type : "tool_call_chunk" ,
662+ } ,
663+ {
664+ args : '"}' ,
665+ index : 0 ,
666+ type : "tool_call_chunk" ,
667+ } ,
668+ ] ,
669+ } ) ;
670+ expect ( chunk . tool_calls ) . toHaveLength ( 1 ) ;
671+ expect ( chunk . tool_calls ) . toEqual ( [
672+ {
673+ type : "tool_call" ,
674+ name : "get_weather" ,
675+ args : {
676+ location : "San Francisco" ,
677+ } ,
678+ id : "call_q6ZzjkLjKNYb4DizyMOaqpfW" ,
679+ } ,
680+ ] ) ;
681+ } ) ;
520682 } ) ;
521683} ) ;
522684
0 commit comments