@mastra/client-js 0.10.6-alpha.5 → 0.10.6-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
 
2
- > @mastra/client-js@0.10.6-alpha.5 build /home/runner/work/mastra/mastra/client-sdks/client-js
2
+ > @mastra/client-js@0.10.6-alpha.6 build /home/runner/work/mastra/mastra/client-sdks/client-js
3
3
  > tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -9,11 +9,11 @@
9
9
  CLI Cleaning output folder
10
10
  ESM Build start
11
11
  CJS Build start
12
- ESM dist/index.js 66.19 KB
13
- ESM ⚡️ Build success in 2323ms
14
- CJS dist/index.cjs 66.48 KB
15
- CJS ⚡️ Build success in 2324ms
12
+ ESM dist/index.js 66.44 KB
13
+ ESM ⚡️ Build success in 2290ms
14
+ CJS dist/index.cjs 66.73 KB
15
+ CJS ⚡️ Build success in 2290ms
16
16
  DTS Build start
17
- DTS ⚡️ Build success in 18285ms
17
+ DTS ⚡️ Build success in 17954ms
18
18
  DTS dist/index.d.ts 39.17 KB
19
19
  DTS dist/index.d.cts 39.17 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @mastra/client-js
2
2
 
3
+ ## 0.10.6-alpha.6
4
+
5
+ ### Patch Changes
6
+
7
+ - c0d41f6: Fix streaming for agent tool calls
8
+
3
9
  ## 0.10.6-alpha.5
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -722,74 +722,77 @@ var Agent = class extends BaseResource {
722
722
  }
723
723
  try {
724
724
  let toolCalls = [];
725
- let finishReasonToolCalls = false;
726
725
  let messages = [];
727
- let hasProcessedToolCalls = false;
728
- response.clone().body.pipeTo(writable);
729
- await this.processChatResponse({
730
- stream: response.clone().body,
726
+ const [streamForWritable, streamForProcessing] = response.body.tee();
727
+ streamForWritable.pipeTo(writable, {
728
+ preventClose: true
729
+ }).catch((error) => {
730
+ console.error("Error piping to writable stream:", error);
731
+ });
732
+ this.processChatResponse({
733
+ stream: streamForProcessing,
731
734
  update: ({ message }) => {
732
735
  messages.push(message);
733
736
  },
734
- onFinish: ({ finishReason, message }) => {
737
+ onFinish: async ({ finishReason, message }) => {
735
738
  if (finishReason === "tool-calls") {
736
- finishReasonToolCalls = true;
737
739
  const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
738
740
  if (toolCall) {
739
741
  toolCalls.push(toolCall);
740
742
  }
743
+ for (const toolCall2 of toolCalls) {
744
+ const clientTool = processedParams.clientTools?.[toolCall2.toolName];
745
+ if (clientTool && clientTool.execute) {
746
+ const result = await clientTool.execute(
747
+ {
748
+ context: toolCall2?.args,
749
+ runId: processedParams.runId,
750
+ resourceId: processedParams.resourceId,
751
+ threadId: processedParams.threadId,
752
+ runtimeContext: processedParams.runtimeContext
753
+ },
754
+ {
755
+ messages: response.messages,
756
+ toolCallId: toolCall2?.toolCallId
757
+ }
758
+ );
759
+ const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
760
+ const toolInvocationPart = lastMessage?.parts?.find(
761
+ (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
762
+ );
763
+ if (toolInvocationPart) {
764
+ toolInvocationPart.toolInvocation = {
765
+ ...toolInvocationPart.toolInvocation,
766
+ state: "result",
767
+ result
768
+ };
769
+ }
770
+ const toolInvocation = lastMessage?.toolInvocations?.find(
771
+ (toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
772
+ );
773
+ if (toolInvocation) {
774
+ toolInvocation.state = "result";
775
+ toolInvocation.result = result;
776
+ }
777
+ const originalMessages = processedParams.messages;
778
+ const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
779
+ this.processStreamResponse(
780
+ {
781
+ ...processedParams,
782
+ messages: [...messageArray, ...messages, lastMessage]
783
+ },
784
+ writable
785
+ );
786
+ }
787
+ }
788
+ } else {
789
+ setTimeout(() => {
790
+ writable.close();
791
+ }, 0);
741
792
  }
742
793
  },
743
794
  lastMessage: void 0
744
795
  });
745
- if (finishReasonToolCalls && !hasProcessedToolCalls) {
746
- hasProcessedToolCalls = true;
747
- for (const toolCall of toolCalls) {
748
- const clientTool = processedParams.clientTools?.[toolCall.toolName];
749
- if (clientTool && clientTool.execute) {
750
- const result = await clientTool.execute(
751
- {
752
- context: toolCall?.args,
753
- runId: processedParams.runId,
754
- resourceId: processedParams.resourceId,
755
- threadId: processedParams.threadId,
756
- runtimeContext: processedParams.runtimeContext
757
- },
758
- {
759
- messages: response.messages,
760
- toolCallId: toolCall?.toolCallId
761
- }
762
- );
763
- const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
764
- const toolInvocationPart = lastMessage?.parts?.find(
765
- (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall.toolCallId
766
- );
767
- if (toolInvocationPart) {
768
- toolInvocationPart.toolInvocation = {
769
- ...toolInvocationPart.toolInvocation,
770
- state: "result",
771
- result
772
- };
773
- }
774
- const toolInvocation = lastMessage?.toolInvocations?.find(
775
- (toolInvocation2) => toolInvocation2.toolCallId === toolCall.toolCallId
776
- );
777
- if (toolInvocation) {
778
- toolInvocation.state = "result";
779
- toolInvocation.result = result;
780
- }
781
- const originalMessages = processedParams.messages;
782
- const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
783
- this.processStreamResponse(
784
- {
785
- ...processedParams,
786
- messages: [...messageArray, ...messages, lastMessage]
787
- },
788
- writable
789
- );
790
- }
791
- }
792
- }
793
796
  } catch (error) {
794
797
  console.error("Error processing stream response:", error);
795
798
  }
package/dist/index.js CHANGED
@@ -716,74 +716,77 @@ var Agent = class extends BaseResource {
716
716
  }
717
717
  try {
718
718
  let toolCalls = [];
719
- let finishReasonToolCalls = false;
720
719
  let messages = [];
721
- let hasProcessedToolCalls = false;
722
- response.clone().body.pipeTo(writable);
723
- await this.processChatResponse({
724
- stream: response.clone().body,
720
+ const [streamForWritable, streamForProcessing] = response.body.tee();
721
+ streamForWritable.pipeTo(writable, {
722
+ preventClose: true
723
+ }).catch((error) => {
724
+ console.error("Error piping to writable stream:", error);
725
+ });
726
+ this.processChatResponse({
727
+ stream: streamForProcessing,
725
728
  update: ({ message }) => {
726
729
  messages.push(message);
727
730
  },
728
- onFinish: ({ finishReason, message }) => {
731
+ onFinish: async ({ finishReason, message }) => {
729
732
  if (finishReason === "tool-calls") {
730
- finishReasonToolCalls = true;
731
733
  const toolCall = [...message?.parts ?? []].reverse().find((part) => part.type === "tool-invocation")?.toolInvocation;
732
734
  if (toolCall) {
733
735
  toolCalls.push(toolCall);
734
736
  }
737
+ for (const toolCall2 of toolCalls) {
738
+ const clientTool = processedParams.clientTools?.[toolCall2.toolName];
739
+ if (clientTool && clientTool.execute) {
740
+ const result = await clientTool.execute(
741
+ {
742
+ context: toolCall2?.args,
743
+ runId: processedParams.runId,
744
+ resourceId: processedParams.resourceId,
745
+ threadId: processedParams.threadId,
746
+ runtimeContext: processedParams.runtimeContext
747
+ },
748
+ {
749
+ messages: response.messages,
750
+ toolCallId: toolCall2?.toolCallId
751
+ }
752
+ );
753
+ const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
754
+ const toolInvocationPart = lastMessage?.parts?.find(
755
+ (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
756
+ );
757
+ if (toolInvocationPart) {
758
+ toolInvocationPart.toolInvocation = {
759
+ ...toolInvocationPart.toolInvocation,
760
+ state: "result",
761
+ result
762
+ };
763
+ }
764
+ const toolInvocation = lastMessage?.toolInvocations?.find(
765
+ (toolInvocation2) => toolInvocation2.toolCallId === toolCall2.toolCallId
766
+ );
767
+ if (toolInvocation) {
768
+ toolInvocation.state = "result";
769
+ toolInvocation.result = result;
770
+ }
771
+ const originalMessages = processedParams.messages;
772
+ const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
773
+ this.processStreamResponse(
774
+ {
775
+ ...processedParams,
776
+ messages: [...messageArray, ...messages, lastMessage]
777
+ },
778
+ writable
779
+ );
780
+ }
781
+ }
782
+ } else {
783
+ setTimeout(() => {
784
+ writable.close();
785
+ }, 0);
735
786
  }
736
787
  },
737
788
  lastMessage: void 0
738
789
  });
739
- if (finishReasonToolCalls && !hasProcessedToolCalls) {
740
- hasProcessedToolCalls = true;
741
- for (const toolCall of toolCalls) {
742
- const clientTool = processedParams.clientTools?.[toolCall.toolName];
743
- if (clientTool && clientTool.execute) {
744
- const result = await clientTool.execute(
745
- {
746
- context: toolCall?.args,
747
- runId: processedParams.runId,
748
- resourceId: processedParams.resourceId,
749
- threadId: processedParams.threadId,
750
- runtimeContext: processedParams.runtimeContext
751
- },
752
- {
753
- messages: response.messages,
754
- toolCallId: toolCall?.toolCallId
755
- }
756
- );
757
- const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
758
- const toolInvocationPart = lastMessage?.parts?.find(
759
- (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall.toolCallId
760
- );
761
- if (toolInvocationPart) {
762
- toolInvocationPart.toolInvocation = {
763
- ...toolInvocationPart.toolInvocation,
764
- state: "result",
765
- result
766
- };
767
- }
768
- const toolInvocation = lastMessage?.toolInvocations?.find(
769
- (toolInvocation2) => toolInvocation2.toolCallId === toolCall.toolCallId
770
- );
771
- if (toolInvocation) {
772
- toolInvocation.state = "result";
773
- toolInvocation.result = result;
774
- }
775
- const originalMessages = processedParams.messages;
776
- const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
777
- this.processStreamResponse(
778
- {
779
- ...processedParams,
780
- messages: [...messageArray, ...messages, lastMessage]
781
- },
782
- writable
783
- );
784
- }
785
- }
786
- }
787
790
  } catch (error) {
788
791
  console.error("Error processing stream response:", error);
789
792
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/client-js",
3
- "version": "0.10.6-alpha.5",
3
+ "version": "0.10.6-alpha.6",
4
4
  "description": "The official TypeScript library for the Mastra Client API",
5
5
  "author": "",
6
6
  "type": "module",
@@ -604,87 +604,97 @@ export class Agent extends BaseResource {
604
604
  let messages: UIMessage[] = [];
605
605
  let hasProcessedToolCalls = false;
606
606
 
607
- response.clone().body!.pipeTo(writable);
607
+ // Use tee() to split the stream into two branches
608
+ const [streamForWritable, streamForProcessing] = response.body.tee();
609
+
610
+ // Pipe one branch to the writable stream
611
+ streamForWritable
612
+ .pipeTo(writable, {
613
+ preventClose: true,
614
+ })
615
+ .catch(error => {
616
+ console.error('Error piping to writable stream:', error);
617
+ });
608
618
 
609
- await this.processChatResponse({
610
- stream: response.clone().body!,
619
+ // Process the other branch for chat response handling
620
+ this.processChatResponse({
621
+ stream: streamForProcessing,
611
622
  update: ({ message }) => {
612
623
  messages.push(message);
613
624
  },
614
- onFinish: ({ finishReason, message }) => {
625
+ onFinish: async ({ finishReason, message }) => {
615
626
  if (finishReason === 'tool-calls') {
616
- finishReasonToolCalls = true;
617
627
  const toolCall = [...(message?.parts ?? [])]
618
628
  .reverse()
619
629
  .find(part => part.type === 'tool-invocation')?.toolInvocation;
620
630
  if (toolCall) {
621
631
  toolCalls.push(toolCall);
622
632
  }
633
+
634
+ // Handle tool calls if needed
635
+ for (const toolCall of toolCalls) {
636
+ const clientTool = processedParams.clientTools?.[toolCall.toolName] as Tool;
637
+ if (clientTool && clientTool.execute) {
638
+ const result = await clientTool.execute(
639
+ {
640
+ context: toolCall?.args,
641
+ runId: processedParams.runId,
642
+ resourceId: processedParams.resourceId,
643
+ threadId: processedParams.threadId,
644
+ runtimeContext: processedParams.runtimeContext as RuntimeContext,
645
+ },
646
+ {
647
+ messages: (response as unknown as { messages: CoreMessage[] }).messages,
648
+ toolCallId: toolCall?.toolCallId,
649
+ },
650
+ );
651
+
652
+ const lastMessage: UIMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
653
+
654
+ const toolInvocationPart = lastMessage?.parts?.find(
655
+ part => part.type === 'tool-invocation' && part.toolInvocation?.toolCallId === toolCall.toolCallId,
656
+ ) as ToolInvocationUIPart | undefined;
657
+
658
+ if (toolInvocationPart) {
659
+ toolInvocationPart.toolInvocation = {
660
+ ...toolInvocationPart.toolInvocation,
661
+ state: 'result',
662
+ result,
663
+ };
664
+ }
665
+
666
+ const toolInvocation = lastMessage?.toolInvocations?.find(
667
+ toolInvocation => toolInvocation.toolCallId === toolCall.toolCallId,
668
+ ) as ToolInvocation | undefined;
669
+
670
+ if (toolInvocation) {
671
+ toolInvocation.state = 'result';
672
+ // @ts-ignore
673
+ toolInvocation.result = result;
674
+ }
675
+
676
+ // Convert messages to the correct format for the recursive call
677
+ const originalMessages = processedParams.messages;
678
+ const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
679
+
680
+ // Recursively call stream with updated messages
681
+ this.processStreamResponse(
682
+ {
683
+ ...processedParams,
684
+ messages: [...messageArray, ...messages, lastMessage],
685
+ },
686
+ writable,
687
+ );
688
+ }
689
+ }
690
+ } else {
691
+ setTimeout(() => {
692
+ writable.close();
693
+ }, 0);
623
694
  }
624
695
  },
625
696
  lastMessage: undefined,
626
697
  });
627
-
628
- // Handle tool calls if needed
629
- if (finishReasonToolCalls && !hasProcessedToolCalls) {
630
- hasProcessedToolCalls = true;
631
-
632
- for (const toolCall of toolCalls) {
633
- const clientTool = processedParams.clientTools?.[toolCall.toolName] as Tool;
634
- if (clientTool && clientTool.execute) {
635
- const result = await clientTool.execute(
636
- {
637
- context: toolCall?.args,
638
- runId: processedParams.runId,
639
- resourceId: processedParams.resourceId,
640
- threadId: processedParams.threadId,
641
- runtimeContext: processedParams.runtimeContext as RuntimeContext,
642
- },
643
- {
644
- messages: (response as unknown as { messages: CoreMessage[] }).messages,
645
- toolCallId: toolCall?.toolCallId,
646
- },
647
- );
648
-
649
- const lastMessage: UIMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
650
-
651
- const toolInvocationPart = lastMessage?.parts?.find(
652
- part => part.type === 'tool-invocation' && part.toolInvocation?.toolCallId === toolCall.toolCallId,
653
- ) as ToolInvocationUIPart | undefined;
654
-
655
- if (toolInvocationPart) {
656
- toolInvocationPart.toolInvocation = {
657
- ...toolInvocationPart.toolInvocation,
658
- state: 'result',
659
- result,
660
- };
661
- }
662
-
663
- const toolInvocation = lastMessage?.toolInvocations?.find(
664
- toolInvocation => toolInvocation.toolCallId === toolCall.toolCallId,
665
- ) as ToolInvocation | undefined;
666
-
667
- if (toolInvocation) {
668
- toolInvocation.state = 'result';
669
- // @ts-ignore
670
- toolInvocation.result = result;
671
- }
672
-
673
- // Convert messages to the correct format for the recursive call
674
- const originalMessages = processedParams.messages;
675
- const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
676
-
677
- // Recursively call stream with updated messages
678
- this.processStreamResponse(
679
- {
680
- ...processedParams,
681
- messages: [...messageArray, ...messages, lastMessage],
682
- },
683
- writable,
684
- );
685
- }
686
- }
687
- }
688
698
  } catch (error) {
689
699
  console.error('Error processing stream response:', error);
690
700
  }