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

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/dist/index.js CHANGED
@@ -550,6 +550,9 @@ const toAssistantUIMessage = (message) => {
550
550
  class AISdkNetworkTransformer {
551
551
  transform({ chunk, conversation, metadata }) {
552
552
  const newConversation = [...conversation];
553
+ if (chunk.type === "routing-agent-text-delta") {
554
+ return this.handleRoutingAgentConversation(chunk, newConversation);
555
+ }
553
556
  if (chunk.type.startsWith("agent-execution-")) {
554
557
  return this.handleAgentConversation(chunk, newConversation, metadata);
555
558
  }
@@ -560,22 +563,80 @@ class AISdkNetworkTransformer {
560
563
  return this.handleToolConversation(chunk, newConversation, metadata);
561
564
  }
562
565
  if (chunk.type === "network-execution-event-step-finish") {
563
- const newMessage = {
564
- id: `network-execution-event-step-finish-${chunk.runId}-${Date.now()}`,
565
- role: "assistant",
566
- parts: [
566
+ const lastMessage = newConversation[newConversation.length - 1];
567
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
568
+ const agentChunk = chunk.payload;
569
+ const parts = [...lastMessage.parts];
570
+ const textPartIndex = parts.findIndex((part) => part.type === "text");
571
+ if (textPartIndex === -1) {
572
+ parts.push({
573
+ type: "text",
574
+ text: agentChunk.result,
575
+ state: "done"
576
+ });
577
+ return [
578
+ ...newConversation.slice(0, -1),
567
579
  {
568
- type: "text",
569
- text: chunk.payload?.result || "",
570
- state: "done"
580
+ ...lastMessage,
581
+ parts
571
582
  }
572
- ],
573
- metadata
574
- };
575
- return [...newConversation, newMessage];
583
+ ];
584
+ }
585
+ const textPart = parts[textPartIndex];
586
+ if (textPart.type === "text") {
587
+ parts[textPartIndex] = {
588
+ ...textPart,
589
+ state: "done"
590
+ };
591
+ return [
592
+ ...newConversation.slice(0, -1),
593
+ {
594
+ ...lastMessage,
595
+ parts
596
+ }
597
+ ];
598
+ }
599
+ return newConversation;
576
600
  }
577
601
  return newConversation;
578
602
  }
603
+ handleRoutingAgentConversation = (chunk, newConversation) => {
604
+ const lastMessage = newConversation[newConversation.length - 1];
605
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
606
+ const agentChunk = chunk.payload;
607
+ const parts = [...lastMessage.parts];
608
+ const textPartIndex = parts.findIndex((part) => part.type === "text");
609
+ if (textPartIndex === -1) {
610
+ parts.push({
611
+ type: "text",
612
+ text: agentChunk.text,
613
+ state: "streaming"
614
+ });
615
+ return [
616
+ ...newConversation.slice(0, -1),
617
+ {
618
+ ...lastMessage,
619
+ parts
620
+ }
621
+ ];
622
+ }
623
+ const textPart = parts[textPartIndex];
624
+ if (textPart.type === "text") {
625
+ parts[textPartIndex] = {
626
+ ...textPart,
627
+ text: textPart.text + agentChunk.text,
628
+ state: "streaming"
629
+ };
630
+ return [
631
+ ...newConversation.slice(0, -1),
632
+ {
633
+ ...lastMessage,
634
+ parts
635
+ }
636
+ ];
637
+ }
638
+ return newConversation;
639
+ };
579
640
  handleAgentConversation = (chunk, newConversation, metadata) => {
580
641
  if (chunk.type === "agent-execution-start") {
581
642
  const primitiveId = chunk.payload?.args?.primitiveId;
@@ -1090,13 +1151,23 @@ const useChat = ({ agentId, initializeMessages }) => {
1090
1151
  });
1091
1152
  setIsRunning(false);
1092
1153
  };
1154
+ const sendMessage = async ({ mode = "stream", ...args }) => {
1155
+ const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
1156
+ const messages2 = args.coreUserMessages ? [nextMessage, ...args.coreUserMessages] : [nextMessage];
1157
+ setMessages((s) => [...s, { role: "user", parts: [{ type: "text", text: args.message }] }]);
1158
+ if (mode === "generate") {
1159
+ await generate({ ...args, coreUserMessages: messages2 });
1160
+ } else if (mode === "stream") {
1161
+ await stream({ ...args, coreUserMessages: messages2 });
1162
+ } else if (mode === "network") {
1163
+ await network({ ...args, coreUserMessages: messages2 });
1164
+ }
1165
+ };
1093
1166
  return {
1094
- network,
1095
- stream,
1096
- generate,
1167
+ setMessages,
1168
+ sendMessage,
1097
1169
  isRunning,
1098
1170
  messages,
1099
- setMessages,
1100
1171
  cancelRun: () => setIsRunning(false)
1101
1172
  };
1102
1173
  };