@@ -387,9 +387,6 @@ describe('text stream', () => {
387
387
server . urls [ '/api/chat' ] . response = {
388
388
type : 'controlled-stream' ,
389
389
controller,
390
- headers : {
391
- 'Content-Type' : 'text/event-stream' ,
392
- } ,
393
390
} ;
394
391
395
392
await userEvent . click ( screen . getByTestId ( 'do-append-text-stream' ) ) ;
@@ -735,7 +732,9 @@ describe('onToolCall', () => {
735
732
736
733
describe ( 'tool invocations' , ( ) => {
737
734
setupTestComponent ( ( ) => {
738
- const { messages, append, addToolResult } = useChat ( ) ;
735
+ const { messages, append, addToolResult } = useChat ( {
736
+ maxSteps : 5 ,
737
+ } ) ;
739
738
740
739
return (
741
740
< div >
@@ -852,15 +851,11 @@ describe('tool invocations', () => {
852
851
} ) ;
853
852
} ) ;
854
853
855
- it ( 'should display partial tool call and tool result (when there is no tool call streaming)' , async ( ) => {
854
+ it ( 'should display tool call and tool result (when there is no tool call streaming)' , async ( ) => {
856
855
const controller = new TestResponseController ( ) ;
857
-
858
856
server . urls [ '/api/chat' ] . response = {
859
857
type : 'controlled-stream' ,
860
858
controller,
861
- headers : {
862
- 'Content-Type' : 'text/event-stream' ,
863
- } ,
864
859
} ;
865
860
866
861
await userEvent . click ( screen . getByTestId ( 'do-append' ) ) ;
@@ -922,6 +917,73 @@ describe('tool invocations', () => {
922
917
) ;
923
918
} ) ;
924
919
} ) ;
920
+
921
+ it ( 'should delay tool result submission until the stream is finished' , async ( ) => {
922
+ const controller1 = new TestResponseController ( ) ;
923
+ const controller2 = new TestResponseController ( ) ;
924
+
925
+ server . urls [ '/api/chat' ] . response = [
926
+ { type : 'controlled-stream' , controller : controller1 } ,
927
+ { type : 'controlled-stream' , controller : controller2 } ,
928
+ ] ;
929
+
930
+ await userEvent . click ( screen . getByTestId ( 'do-append' ) ) ;
931
+
932
+ // start stream
933
+ controller1 . write (
934
+ formatDataStreamPart ( 'start_step' , {
935
+ messageId : '1234' ,
936
+ } ) ,
937
+ ) ;
938
+
939
+ // tool call
940
+ controller1 . write (
941
+ formatDataStreamPart ( 'tool_call' , {
942
+ toolCallId : 'tool-call-0' ,
943
+ toolName : 'test-tool' ,
944
+ args : { testArg : 'test-value' } ,
945
+ } ) ,
946
+ ) ;
947
+
948
+ await waitFor ( ( ) => {
949
+ expect ( screen . getByTestId ( 'message-1' ) ) . toHaveTextContent (
950
+ '{"state":"call","step":0,"toolCallId":"tool-call-0","toolName":"test-tool","args":{"testArg":"test-value"}}' ,
951
+ ) ;
952
+ } ) ;
953
+
954
+ // user submits the tool result
955
+ await userEvent . click ( screen . getByTestId ( 'add-result-0' ) ) ;
956
+
957
+ // UI should show the tool result
958
+ await waitFor ( ( ) => {
959
+ expect ( screen . getByTestId ( 'message-1' ) ) . toHaveTextContent (
960
+ '{"state":"result","step":0,"toolCallId":"tool-call-0","toolName":"test-tool","args":{"testArg":"test-value"},"result":"test-result"}' ,
961
+ ) ;
962
+ } ) ;
963
+
964
+ // should not have called the API yet
965
+ expect ( server . calls . length ) . toBe ( 1 ) ;
966
+
967
+ // finish stream
968
+ controller1 . write (
969
+ formatDataStreamPart ( 'finish_step' , {
970
+ isContinued : false ,
971
+ finishReason : 'tool-calls' ,
972
+ } ) ,
973
+ ) ;
974
+ controller1 . write (
975
+ formatDataStreamPart ( 'finish_message' , {
976
+ finishReason : 'tool-calls' ,
977
+ } ) ,
978
+ ) ;
979
+
980
+ await controller1 . close ( ) ;
981
+
982
+ // 2nd call should happen after the stream is finished
983
+ await waitFor ( ( ) => {
984
+ expect ( server . calls . length ) . toBe ( 2 ) ;
985
+ } ) ;
986
+ } ) ;
925
987
} ) ;
926
988
927
989
describe ( 'maxSteps' , ( ) => {
0 commit comments