@mastra/react 1.5.0-alpha.4 → 1.5.0-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.
package/dist/index.js CHANGED
@@ -2045,11 +2045,29 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2045
2045
  const processStreamChunk = useCallback(async (chunk, onChunk) => {
2046
2046
  const isTerminal = chunk.type === "finish" || chunk.type === "abort" || chunk.type === "error";
2047
2047
  if (isTerminal && liveRunId.current && chunk.runId !== liveRunId.current) return;
2048
- setMessages((prev) => accumulateChunk({
2049
- chunk,
2050
- conversation: prev,
2051
- metadata: { mode: "stream" }
2052
- }));
2048
+ const runId = "runId" in chunk && typeof chunk.runId === "string" ? chunk.runId : void 0;
2049
+ setMessages((prev) => {
2050
+ const metadata = {
2051
+ mode: "stream",
2052
+ runId
2053
+ };
2054
+ const next = accumulateChunk({
2055
+ chunk,
2056
+ conversation: prev,
2057
+ metadata
2058
+ });
2059
+ if ((chunk.type === "start" || chunk.type === "step-start") && chunk.payload?.messageId && runId) return next.map((message) => message.id === chunk.payload.messageId && message.role === "assistant" ? {
2060
+ ...message,
2061
+ content: {
2062
+ ...message.content,
2063
+ metadata: {
2064
+ ...message.content.metadata,
2065
+ ...metadata
2066
+ }
2067
+ }
2068
+ } : message);
2069
+ return next;
2070
+ });
2053
2071
  const streamedTasks = extractTasksFromToolResultChunk(chunk) ?? extractTasksFromSignalChunk(chunk);
2054
2072
  if (streamedTasks !== void 0) {
2055
2073
  liveTasks.current = streamedTasks;
@@ -2403,6 +2421,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2403
2421
  abortSignal: signal
2404
2422
  }).getAgent(agentId, void 0, { stream: streamPath });
2405
2423
  const runId = v4();
2424
+ _currentRunId.current = runId;
2406
2425
  const response = await agent.network(coreUserMessages, {
2407
2426
  model,
2408
2427
  maxSteps,
@@ -2429,7 +2448,10 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2429
2448
  setMessages((prev) => accumulateNetworkChunk({
2430
2449
  chunk,
2431
2450
  conversation: prev,
2432
- metadata: { mode: "network" }
2451
+ metadata: {
2452
+ mode: "network",
2453
+ runId
2454
+ }
2433
2455
  }));
2434
2456
  onNetworkChunk?.(chunk);
2435
2457
  } });
@@ -2607,6 +2629,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2607
2629
  const networkRunId = runId || _networkRunId.current;
2608
2630
  const continuation = _activeContinuation.current;
2609
2631
  if (!networkRunId) return console.info("[approveNetworkToolCall] approveNetworkToolCall can only be called after a network stream has started");
2632
+ _currentRunId.current = networkRunId;
2610
2633
  setIsRunning(true);
2611
2634
  setNetworkToolCallApprovals((prev) => ({
2612
2635
  ...prev,
@@ -2619,7 +2642,10 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2619
2642
  setMessages((prev) => accumulateNetworkChunk({
2620
2643
  chunk,
2621
2644
  conversation: prev,
2622
- metadata: { mode: "network" }
2645
+ metadata: {
2646
+ mode: "network",
2647
+ runId: networkRunId
2648
+ }
2623
2649
  }));
2624
2650
  onNetworkChunk?.(chunk);
2625
2651
  } });
@@ -2631,6 +2657,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2631
2657
  const networkRunId = runId || _networkRunId.current;
2632
2658
  const continuation = _activeContinuation.current;
2633
2659
  if (!networkRunId) return console.info("[declineNetworkToolCall] declineNetworkToolCall can only be called after a network stream has started");
2660
+ _currentRunId.current = networkRunId;
2634
2661
  setIsRunning(true);
2635
2662
  setNetworkToolCallApprovals((prev) => ({
2636
2663
  ...prev,
@@ -2643,7 +2670,10 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2643
2670
  setMessages((prev) => accumulateNetworkChunk({
2644
2671
  chunk,
2645
2672
  conversation: prev,
2646
- metadata: { mode: "network" }
2673
+ metadata: {
2674
+ mode: "network",
2675
+ runId: networkRunId
2676
+ }
2647
2677
  }));
2648
2678
  onNetworkChunk?.(chunk);
2649
2679
  } });
@@ -2651,6 +2681,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2651
2681
  setIsRunning(false);
2652
2682
  };
2653
2683
  const sendMessage = async ({ mode = "stream", ...args }) => {
2684
+ if (!isRunning && !isAwaitingToolApproval) _currentRunId.current = void 0;
2654
2685
  const coreUserMessages = [{
2655
2686
  role: "user",
2656
2687
  content: [{
@@ -2704,6 +2735,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2704
2735
  setMessages,
2705
2736
  sendMessage,
2706
2737
  isRunning,
2738
+ activeRunId: isRunning || isAwaitingToolApproval ? _currentRunId.current : void 0,
2707
2739
  isAwaitingToolApproval,
2708
2740
  messages,
2709
2741
  tasks,