@mastra/react 1.4.13-alpha.3 → 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
@@ -1,4 +1,4 @@
1
- import { $ as useEntity, A as TooltipContentClass, B as ToolApprovalContent, C as CodeBlock, D as IconButtonClass, E as IconButton, F as EntryTitleClass, G as ToolApprovalTitleClass, H as ToolApprovalHeader, I as ToolApproval, J as EntityContent, K as Entity, L as ToolApprovalActions, M as Entry, N as EntryClass, O as Tooltip, P as EntryTitle, Q as EntityTriggerVariantClasses, R as ToolApprovalActionsClass, S as AgentIcon, T as CodeCopyButton, U as ToolApprovalHeaderClass, V as ToolApprovalContentClass, W as ToolApprovalTitle, X as EntityTrigger, Y as EntityContentClass, Z as EntityTriggerClass, _ as MessageUsageValueClass, a as MessageClass, b as WorkflowIcon, c as MessageList, d as MessageStreamingClass, et as Icon, f as MessageUsage, g as MessageUsageValue, h as MessageUsageEntryClass, i as MessageActionsClass, j as TooltipTrigger, k as TooltipContent, l as MessageListClass, m as MessageUsageEntry, n as Message, o as MessageContent, p as MessageUsageClass, q as EntityCaret, r as MessageActions, s as MessageContentClass, t as MessageFactory, tt as IconSizes, u as MessageStreaming, v as MessageUsages, w as CodeBlockClass, x as ToolsIcon, y as MessageUsagesClass, z as ToolApprovalClass } from "./ui-Bd2cu1B9.js";
1
+ import { $ as useEntity, A as TooltipContentClass, B as ToolApprovalContent, C as CodeBlock, D as IconButtonClass, E as IconButton, F as EntryTitleClass, G as ToolApprovalTitleClass, H as ToolApprovalHeader, I as ToolApproval, J as EntityContent, K as Entity, L as ToolApprovalActions, M as Entry, N as EntryClass, O as Tooltip, P as EntryTitle, Q as EntityTriggerVariantClasses, R as ToolApprovalActionsClass, S as AgentIcon, T as CodeCopyButton, U as ToolApprovalHeaderClass, V as ToolApprovalContentClass, W as ToolApprovalTitle, X as EntityTrigger, Y as EntityContentClass, Z as EntityTriggerClass, _ as MessageUsageValueClass, a as MessageClass, b as WorkflowIcon, c as MessageList, d as MessageStreamingClass, et as Icon, f as MessageUsage, g as MessageUsageValue, h as MessageUsageEntryClass, i as MessageActionsClass, j as TooltipTrigger, k as TooltipContent, l as MessageListClass, m as MessageUsageEntry, n as Message, o as MessageContent, p as MessageUsageClass, q as EntityCaret, r as MessageActions, s as MessageContentClass, t as MessageFactory, tt as IconSizes, u as MessageStreaming, v as MessageUsages, w as CodeBlockClass, x as ToolsIcon, y as MessageUsagesClass, z as ToolApprovalClass } from "./ui-BQyoxYsH.js";
2
2
  import { MastraClient } from "@mastra/client-js";
3
3
  import { createContext, memo, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
4
4
  import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";
@@ -1908,7 +1908,7 @@ const dbFromServerUiMessages = (uiMessages, metadata) => uiMessages.map((uiMsg)
1908
1908
  }
1909
1909
  };
1910
1910
  });
1911
- const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals = false }) => {
1911
+ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContext: propsRequestContext, clientTools: hookClientTools, onSignalSent, onSignalEcho, onThreadSignalsUnsupported, enableThreadSignals = false, streamPath }) => {
1912
1912
  const threadSignalsDisabled = enableThreadSignals === false;
1913
1913
  const _currentRunId = useRef(void 0);
1914
1914
  const _onChunk = useRef(void 0);
@@ -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;
@@ -2084,7 +2102,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2084
2102
  (onChunk ?? _onChunk.current)?.(chunk);
2085
2103
  }, [onSignalEcho]);
2086
2104
  const ensureThreadSubscription = useCallback(async ({ threadId, resourceId }) => {
2087
- const subscriptionKey = `${agentId}:${resourceId ?? ""}:${threadId}`;
2105
+ const subscriptionKey = `${agentId}:${resourceId ?? ""}:${threadId}:${streamPath ?? ""}`;
2088
2106
  if (_threadSubscriptionKeyRef.current === subscriptionKey && _threadSubscriptionPromiseRef.current) {
2089
2107
  await _threadSubscriptionPromiseRef.current;
2090
2108
  return;
@@ -2103,7 +2121,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2103
2121
  const subscriptionAgent = new MastraClient({
2104
2122
  ...baseClient.options,
2105
2123
  abortSignal: subscriptionAbort.signal
2106
- }).getAgent(agentId);
2124
+ }).getAgent(agentId, void 0, { stream: streamPath });
2107
2125
  _threadSubscriptionPromiseRef.current = subscriptionAgent.subscribeToThread({
2108
2126
  resourceId,
2109
2127
  threadId
@@ -2141,7 +2159,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2141
2159
  baseClient,
2142
2160
  closeThreadSubscription,
2143
2161
  markThreadSignalsUnsupported,
2144
- processStreamChunk
2162
+ processStreamChunk,
2163
+ streamPath
2145
2164
  ]);
2146
2165
  useEffect(() => {
2147
2166
  _threadSignalsUnsupportedRef.current = false;
@@ -2171,7 +2190,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2171
2190
  threadId,
2172
2191
  threadSignalsDisabled
2173
2192
  ]);
2174
- const generate = async ({ coreUserMessages, model, requestContext, threadId, modelSettings, signal, onFinish, tracingOptions, clientTools }) => {
2193
+ const generate = async ({ coreUserMessages, model, requestContext, threadId, modelSettings, signal, onFinish, tracingOptions, clientTools, clientToolsResolver }) => {
2175
2194
  const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, instructions, system, providerOptions, maxSteps, requireToolApproval } = modelSettings || {};
2176
2195
  const resolvedRequestContext = requestContext ?? propsRequestContext;
2177
2196
  const resolvedClientTools = clientTools ?? hookClientTools;
@@ -2183,7 +2202,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2183
2202
  const agent = new MastraClient({
2184
2203
  ...baseClient.options,
2185
2204
  abortSignal: signal
2186
- }).getAgent(agentId);
2205
+ }).getAgent(agentId, void 0, { stream: streamPath });
2187
2206
  const runId = v4();
2188
2207
  _currentRunId.current = runId;
2189
2208
  const response = await agent.generate(coreUserMessages, {
@@ -2209,7 +2228,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2209
2228
  providerOptions,
2210
2229
  tracingOptions,
2211
2230
  requireToolApproval,
2212
- clientTools: resolvedClientTools
2231
+ clientTools: resolvedClientTools,
2232
+ clientToolsResolver
2213
2233
  });
2214
2234
  if (response.finishReason === "suspended" && response.suspendPayload) {
2215
2235
  const { toolCallId, toolName, args } = response.suspendPayload;
@@ -2234,7 +2254,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2234
2254
  setMessages((prev) => [...prev, ...dbMessages]);
2235
2255
  }
2236
2256
  };
2237
- const stream = async ({ coreUserMessages, model, requestContext, threadId, onChunk, modelSettings, signal, tracingOptions, clientTools, signalId, clientMessageId }) => {
2257
+ const stream = async ({ coreUserMessages, model, requestContext, threadId, onChunk, modelSettings, signal, tracingOptions, clientTools, clientToolsResolver, signalId, clientMessageId }) => {
2238
2258
  const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, instructions, system, providerOptions, maxSteps, requireToolApproval } = modelSettings || {};
2239
2259
  const resolvedRequestContext = requestContext ?? propsRequestContext;
2240
2260
  const resolvedClientTools = clientTools ?? hookClientTools;
@@ -2269,7 +2289,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2269
2289
  const agent = new MastraClient({
2270
2290
  ...baseClient.options,
2271
2291
  abortSignal: internalAbort.signal
2272
- }).getAgent(agentId);
2292
+ }).getAgent(agentId, void 0, { stream: streamPath });
2273
2293
  const streamWithLegacyRoute = async () => {
2274
2294
  const runId = v4();
2275
2295
  const response = await agent.stream(coreUserMessages, {
@@ -2296,7 +2316,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2296
2316
  providerOptions,
2297
2317
  requireToolApproval,
2298
2318
  tracingOptions,
2299
- clientTools: resolvedClientTools
2319
+ clientTools: resolvedClientTools,
2320
+ clientToolsResolver
2300
2321
  });
2301
2322
  _onChunk.current = onChunk;
2302
2323
  _currentRunId.current = runId;
@@ -2337,7 +2358,9 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2337
2358
  requestContext: requestContextRecord,
2338
2359
  providerOptions,
2339
2360
  requireToolApproval,
2340
- tracingOptions
2361
+ tracingOptions,
2362
+ clientTools: resolvedClientTools,
2363
+ clientToolsResolver
2341
2364
  };
2342
2365
  try {
2343
2366
  const result = await agent.sendMessage({
@@ -2350,7 +2373,8 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2350
2373
  ifIdle: { streamOptions: {
2351
2374
  ...signalContinuationOptions,
2352
2375
  requestContext: requestContextRecord,
2353
- clientTools: resolvedClientTools
2376
+ clientTools: resolvedClientTools,
2377
+ clientToolsResolver
2354
2378
  } }
2355
2379
  });
2356
2380
  const echoedSignalId = result.signal && typeof result.signal === "object" && "id" in result.signal && typeof result.signal.id === "string" ? result.signal.id : resolvedSignalId;
@@ -2395,8 +2419,9 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2395
2419
  const agent = new MastraClient({
2396
2420
  ...baseClient.options,
2397
2421
  abortSignal: signal
2398
- }).getAgent(agentId);
2422
+ }).getAgent(agentId, void 0, { stream: streamPath });
2399
2423
  const runId = v4();
2424
+ _currentRunId.current = runId;
2400
2425
  const response = await agent.network(coreUserMessages, {
2401
2426
  model,
2402
2427
  maxSteps,
@@ -2423,7 +2448,10 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2423
2448
  setMessages((prev) => accumulateNetworkChunk({
2424
2449
  chunk,
2425
2450
  conversation: prev,
2426
- metadata: { mode: "network" }
2451
+ metadata: {
2452
+ mode: "network",
2453
+ runId
2454
+ }
2427
2455
  }));
2428
2456
  onNetworkChunk?.(chunk);
2429
2457
  } });
@@ -2459,7 +2487,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2459
2487
  ...prev,
2460
2488
  [toolCallId]: { status: "approved" }
2461
2489
  }));
2462
- const agent = baseClient.getAgent(agentId);
2490
+ const agent = baseClient.getAgent(agentId, void 0, { stream: streamPath });
2463
2491
  if (_threadSubscriptionKeyRef.current && threadId) {
2464
2492
  try {
2465
2493
  await agent.sendToolApproval({
@@ -2520,7 +2548,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2520
2548
  ...prev,
2521
2549
  [toolCallId]: { status: "declined" }
2522
2550
  }));
2523
- const agent = baseClient.getAgent(agentId);
2551
+ const agent = baseClient.getAgent(agentId, void 0, { stream: streamPath });
2524
2552
  if (_threadSubscriptionKeyRef.current && threadId) {
2525
2553
  try {
2526
2554
  await agent.sendToolApproval({
@@ -2565,7 +2593,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2565
2593
  ...prev,
2566
2594
  [toolCallId]: { status: "approved" }
2567
2595
  }));
2568
- const response = await baseClient.getAgent(agentId).approveToolCallGenerate({
2596
+ const response = await baseClient.getAgent(agentId, void 0, { stream: streamPath }).approveToolCallGenerate({
2569
2597
  runId: currentRunId,
2570
2598
  toolCallId,
2571
2599
  ...continuation
@@ -2585,7 +2613,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2585
2613
  ...prev,
2586
2614
  [toolCallId]: { status: "declined" }
2587
2615
  }));
2588
- const response = await baseClient.getAgent(agentId).declineToolCallGenerate({
2616
+ const response = await baseClient.getAgent(agentId, void 0, { stream: streamPath }).declineToolCallGenerate({
2589
2617
  runId: currentRunId,
2590
2618
  toolCallId,
2591
2619
  ...continuation
@@ -2601,19 +2629,23 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2601
2629
  const networkRunId = runId || _networkRunId.current;
2602
2630
  const continuation = _activeContinuation.current;
2603
2631
  if (!networkRunId) return console.info("[approveNetworkToolCall] approveNetworkToolCall can only be called after a network stream has started");
2632
+ _currentRunId.current = networkRunId;
2604
2633
  setIsRunning(true);
2605
2634
  setNetworkToolCallApprovals((prev) => ({
2606
2635
  ...prev,
2607
2636
  [runId ? `${runId}-${toolName}` : toolName]: { status: "approved" }
2608
2637
  }));
2609
- await (await baseClient.getAgent(agentId).approveNetworkToolCall({
2638
+ await (await baseClient.getAgent(agentId, void 0, { stream: streamPath }).approveNetworkToolCall({
2610
2639
  runId: networkRunId,
2611
2640
  ...continuation
2612
2641
  })).processDataStream({ onChunk: async (chunk) => {
2613
2642
  setMessages((prev) => accumulateNetworkChunk({
2614
2643
  chunk,
2615
2644
  conversation: prev,
2616
- metadata: { mode: "network" }
2645
+ metadata: {
2646
+ mode: "network",
2647
+ runId: networkRunId
2648
+ }
2617
2649
  }));
2618
2650
  onNetworkChunk?.(chunk);
2619
2651
  } });
@@ -2625,19 +2657,23 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2625
2657
  const networkRunId = runId || _networkRunId.current;
2626
2658
  const continuation = _activeContinuation.current;
2627
2659
  if (!networkRunId) return console.info("[declineNetworkToolCall] declineNetworkToolCall can only be called after a network stream has started");
2660
+ _currentRunId.current = networkRunId;
2628
2661
  setIsRunning(true);
2629
2662
  setNetworkToolCallApprovals((prev) => ({
2630
2663
  ...prev,
2631
2664
  [runId ? `${runId}-${toolName}` : toolName]: { status: "declined" }
2632
2665
  }));
2633
- await (await baseClient.getAgent(agentId).declineNetworkToolCall({
2666
+ await (await baseClient.getAgent(agentId, void 0, { stream: streamPath }).declineNetworkToolCall({
2634
2667
  runId: networkRunId,
2635
2668
  ...continuation
2636
2669
  })).processDataStream({ onChunk: async (chunk) => {
2637
2670
  setMessages((prev) => accumulateNetworkChunk({
2638
2671
  chunk,
2639
2672
  conversation: prev,
2640
- metadata: { mode: "network" }
2673
+ metadata: {
2674
+ mode: "network",
2675
+ runId: networkRunId
2676
+ }
2641
2677
  }));
2642
2678
  onNetworkChunk?.(chunk);
2643
2679
  } });
@@ -2645,6 +2681,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2645
2681
  setIsRunning(false);
2646
2682
  };
2647
2683
  const sendMessage = async ({ mode = "stream", ...args }) => {
2684
+ if (!isRunning && !isAwaitingToolApproval) _currentRunId.current = void 0;
2648
2685
  const coreUserMessages = [{
2649
2686
  role: "user",
2650
2687
  content: [{
@@ -2698,6 +2735,7 @@ const useChat = ({ agentId, resourceId, threadId, initialMessages, requestContex
2698
2735
  setMessages,
2699
2736
  sendMessage,
2700
2737
  isRunning,
2738
+ activeRunId: isRunning || isAwaitingToolApproval ? _currentRunId.current : void 0,
2701
2739
  isAwaitingToolApproval,
2702
2740
  messages,
2703
2741
  tasks,