@mastra/react 0.0.6-alpha.0 → 0.0.6-alpha.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @mastra/react-hooks
2
2
 
3
+ ## 0.0.6-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies []:
8
+ - @mastra/client-js@0.16.0-alpha.2
9
+
10
+ ## 0.0.6-alpha.1
11
+
12
+ ### Patch Changes
13
+
14
+ - Improve the surface API of the react sdk ([#8715](https://github.com/mastra-ai/mastra/pull/8715))
15
+
16
+ - Move react and react-dom deps to peer and dev deps ([#8698](https://github.com/mastra-ai/mastra/pull/8698))
17
+
18
+ - Stream finalResult from network loop ([#8795](https://github.com/mastra-ai/mastra/pull/8795))
19
+
20
+ - Updated dependencies []:
21
+ - @mastra/client-js@0.16.0-alpha.1
22
+
3
23
  ## 0.0.6-alpha.0
4
24
 
5
25
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -554,6 +554,9 @@ const toAssistantUIMessage = (message) => {
554
554
  class AISdkNetworkTransformer {
555
555
  transform({ chunk, conversation, metadata }) {
556
556
  const newConversation = [...conversation];
557
+ if (chunk.type === "routing-agent-text-delta") {
558
+ return this.handleRoutingAgentConversation(chunk, newConversation);
559
+ }
557
560
  if (chunk.type.startsWith("agent-execution-")) {
558
561
  return this.handleAgentConversation(chunk, newConversation, metadata);
559
562
  }
@@ -564,22 +567,80 @@ class AISdkNetworkTransformer {
564
567
  return this.handleToolConversation(chunk, newConversation, metadata);
565
568
  }
566
569
  if (chunk.type === "network-execution-event-step-finish") {
567
- const newMessage = {
568
- id: `network-execution-event-step-finish-${chunk.runId}-${Date.now()}`,
569
- role: "assistant",
570
- parts: [
570
+ const lastMessage = newConversation[newConversation.length - 1];
571
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
572
+ const agentChunk = chunk.payload;
573
+ const parts = [...lastMessage.parts];
574
+ const textPartIndex = parts.findIndex((part) => part.type === "text");
575
+ if (textPartIndex === -1) {
576
+ parts.push({
577
+ type: "text",
578
+ text: agentChunk.result,
579
+ state: "done"
580
+ });
581
+ return [
582
+ ...newConversation.slice(0, -1),
571
583
  {
572
- type: "text",
573
- text: chunk.payload?.result || "",
574
- state: "done"
584
+ ...lastMessage,
585
+ parts
575
586
  }
576
- ],
577
- metadata
578
- };
579
- return [...newConversation, newMessage];
587
+ ];
588
+ }
589
+ const textPart = parts[textPartIndex];
590
+ if (textPart.type === "text") {
591
+ parts[textPartIndex] = {
592
+ ...textPart,
593
+ state: "done"
594
+ };
595
+ return [
596
+ ...newConversation.slice(0, -1),
597
+ {
598
+ ...lastMessage,
599
+ parts
600
+ }
601
+ ];
602
+ }
603
+ return newConversation;
580
604
  }
581
605
  return newConversation;
582
606
  }
607
+ handleRoutingAgentConversation = (chunk, newConversation) => {
608
+ const lastMessage = newConversation[newConversation.length - 1];
609
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
610
+ const agentChunk = chunk.payload;
611
+ const parts = [...lastMessage.parts];
612
+ const textPartIndex = parts.findIndex((part) => part.type === "text");
613
+ if (textPartIndex === -1) {
614
+ parts.push({
615
+ type: "text",
616
+ text: agentChunk.text,
617
+ state: "streaming"
618
+ });
619
+ return [
620
+ ...newConversation.slice(0, -1),
621
+ {
622
+ ...lastMessage,
623
+ parts
624
+ }
625
+ ];
626
+ }
627
+ const textPart = parts[textPartIndex];
628
+ if (textPart.type === "text") {
629
+ parts[textPartIndex] = {
630
+ ...textPart,
631
+ text: textPart.text + agentChunk.text,
632
+ state: "streaming"
633
+ };
634
+ return [
635
+ ...newConversation.slice(0, -1),
636
+ {
637
+ ...lastMessage,
638
+ parts
639
+ }
640
+ ];
641
+ }
642
+ return newConversation;
643
+ };
583
644
  handleAgentConversation = (chunk, newConversation, metadata) => {
584
645
  if (chunk.type === "agent-execution-start") {
585
646
  const primitiveId = chunk.payload?.args?.primitiveId;
@@ -1094,13 +1155,23 @@ const useChat = ({ agentId, initializeMessages }) => {
1094
1155
  });
1095
1156
  setIsRunning(false);
1096
1157
  };
1158
+ const sendMessage = async ({ mode = "stream", ...args }) => {
1159
+ const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
1160
+ const messages2 = args.coreUserMessages ? [nextMessage, ...args.coreUserMessages] : [nextMessage];
1161
+ setMessages((s) => [...s, { role: "user", parts: [{ type: "text", text: args.message }] }]);
1162
+ if (mode === "generate") {
1163
+ await generate({ ...args, coreUserMessages: messages2 });
1164
+ } else if (mode === "stream") {
1165
+ await stream({ ...args, coreUserMessages: messages2 });
1166
+ } else if (mode === "network") {
1167
+ await network({ ...args, coreUserMessages: messages2 });
1168
+ }
1169
+ };
1097
1170
  return {
1098
- network,
1099
- stream,
1100
- generate,
1171
+ setMessages,
1172
+ sendMessage,
1101
1173
  isRunning,
1102
1174
  messages,
1103
- setMessages,
1104
1175
  cancelRun: () => setIsRunning(false)
1105
1176
  };
1106
1177
  };