@mastra/react 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-scorers-logs-20251208093427

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,6 +1,7 @@
1
1
  import { jsx, jsxs } from 'react/jsx-runtime';
2
2
  import { createContext, useContext, useRef, useState, Fragment, useLayoutEffect, useEffect } from 'react';
3
3
  import { MastraClient } from '@mastra/client-js';
4
+ import { v4 } from '@lukeed/uuid';
4
5
  import { ChevronDownIcon, CheckIcon, CopyIcon } from 'lucide-react';
5
6
  import { twMerge } from 'tailwind-merge';
6
7
  import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
@@ -255,17 +256,19 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
255
256
  if (!lastMessage || lastMessage.role !== "assistant") return result;
256
257
  const parts = [...lastMessage.parts];
257
258
  const toolPartIndex = parts.findIndex(
258
- (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
259
+ (part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
259
260
  );
260
261
  if (toolPartIndex !== -1) {
261
262
  const toolPart = parts[toolPartIndex];
262
- if (toolPart.type === "dynamic-tool") {
263
+ if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
264
+ const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
265
+ const toolCallId = toolPart.toolCallId;
263
266
  if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
264
267
  const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
265
268
  parts[toolPartIndex] = {
266
269
  type: "dynamic-tool",
267
- toolName: toolPart.toolName,
268
- toolCallId: toolPart.toolCallId,
270
+ toolName,
271
+ toolCallId,
269
272
  state: "output-error",
270
273
  input: toolPart.input,
271
274
  errorText: String(error),
@@ -284,8 +287,8 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
284
287
  }
285
288
  parts[toolPartIndex] = {
286
289
  type: "dynamic-tool",
287
- toolName: toolPart.toolName,
288
- toolCallId: toolPart.toolCallId,
290
+ toolName,
291
+ toolCallId,
289
292
  state: "output-available",
290
293
  input: toolPart.input,
291
294
  output,
@@ -307,11 +310,14 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
307
310
  if (!lastMessage || lastMessage.role !== "assistant") return result;
308
311
  const parts = [...lastMessage.parts];
309
312
  const toolPartIndex = parts.findIndex(
310
- (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
313
+ (part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
311
314
  );
312
315
  if (toolPartIndex !== -1) {
313
316
  const toolPart = parts[toolPartIndex];
314
- if (toolPart.type === "dynamic-tool") {
317
+ if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
318
+ const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : typeof toolPart.type === "string" && toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
319
+ const toolCallId = toolPart.toolCallId;
320
+ const input = toolPart.input;
315
321
  if (chunk.payload.output?.type?.startsWith("workflow-")) {
316
322
  const existingWorkflowState = toolPart.output || {};
317
323
  const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
@@ -319,7 +325,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
319
325
  chunk.payload.output
320
326
  );
321
327
  parts[toolPartIndex] = {
322
- ...toolPart,
328
+ type: "dynamic-tool",
329
+ toolName,
330
+ toolCallId,
331
+ state: "input-streaming",
332
+ input,
323
333
  output: updatedWorkflowState
324
334
  };
325
335
  } else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
@@ -328,7 +338,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
328
338
  const currentOutput = toolPart.output || [];
329
339
  const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
330
340
  parts[toolPartIndex] = {
331
- ...toolPart,
341
+ type: "dynamic-tool",
342
+ toolName,
343
+ toolCallId,
344
+ state: "input-streaming",
345
+ input,
332
346
  output: [...existingOutput, chunk.payload.output]
333
347
  };
334
348
  }
@@ -424,11 +438,10 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
424
438
  const lastMessage = result[result.length - 1];
425
439
  if (!lastMessage || lastMessage.role !== "assistant") return result;
426
440
  const parts = lastMessage.parts.map((part) => {
427
- if (part.type === "text" && part.state === "streaming") {
428
- return { ...part, state: "done" };
429
- }
430
- if (part.type === "reasoning" && part.state === "streaming") {
431
- return { ...part, state: "done" };
441
+ if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
442
+ if (part.type === "text" || part.type === "reasoning") {
443
+ return { ...part, state: "done" };
444
+ }
432
445
  }
433
446
  return part;
434
447
  });
@@ -652,6 +665,20 @@ const toAssistantUIMessage = (message) => {
652
665
  }
653
666
  return baseToolCall;
654
667
  }
668
+ const requireApprovalMetadata = extendedMessage.metadata?.requireApprovalMetadata;
669
+ const partToolCallId = "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : void 0;
670
+ const suspensionData = partToolCallId ? requireApprovalMetadata?.[partToolCallId] : void 0;
671
+ if (suspensionData) {
672
+ const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.startsWith("tool-") ? part.type.substring(5) : "";
673
+ return {
674
+ type: "tool-call",
675
+ toolCallId: partToolCallId,
676
+ toolName,
677
+ argsText: "input" in part ? JSON.stringify(part.input) : "{}",
678
+ args: "input" in part ? part.input : {},
679
+ metadata: extendedMessage.metadata
680
+ };
681
+ }
655
682
  return {
656
683
  type: "text",
657
684
  text: "",
@@ -693,7 +720,9 @@ const toAssistantUIMessage = (message) => {
693
720
 
694
721
  const resolveInitialMessages = (messages) => {
695
722
  return messages.map((message) => {
696
- const networkPart = message.parts.find((part) => part.type === "text" && part.text.includes('"isNetwork":true'));
723
+ const networkPart = message.parts.find(
724
+ (part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
725
+ );
697
726
  if (networkPart && networkPart.type === "text") {
698
727
  try {
699
728
  const json = JSON.parse(networkPart.text);
@@ -736,7 +765,6 @@ const resolveInitialMessages = (messages) => {
736
765
  childMessages,
737
766
  result: finalResult?.text || ""
738
767
  };
739
- console.log("json", json);
740
768
  const nextMessage = {
741
769
  role: "assistant",
742
770
  parts: [
@@ -764,6 +792,18 @@ const resolveInitialMessages = (messages) => {
764
792
  return message;
765
793
  }
766
794
  }
795
+ const extendedMessage = message;
796
+ const pendingToolApprovals = extendedMessage.metadata?.pendingToolApprovals;
797
+ if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
798
+ return {
799
+ ...message,
800
+ metadata: {
801
+ ...message.metadata,
802
+ mode: "stream",
803
+ requireApprovalMetadata: pendingToolApprovals
804
+ }
805
+ };
806
+ }
767
807
  return message;
768
808
  });
769
809
  };
@@ -1212,12 +1252,24 @@ const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
1212
1252
  };
1213
1253
  };
1214
1254
 
1215
- const useChat = ({ agentId, initializeMessages }) => {
1216
- const _currentRunId = useRef(void 0);
1255
+ const useChat = ({ agentId, resourceId, initializeMessages }) => {
1256
+ const extractRunIdFromMessages = (messages2) => {
1257
+ for (const message of messages2) {
1258
+ const pendingToolApprovals = message.metadata?.pendingToolApprovals;
1259
+ if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
1260
+ const suspensionData = Object.values(pendingToolApprovals)[0];
1261
+ if (suspensionData?.runId) {
1262
+ return suspensionData.runId;
1263
+ }
1264
+ }
1265
+ }
1266
+ return void 0;
1267
+ };
1268
+ const initialMessages = initializeMessages?.() || [];
1269
+ const initialRunId = extractRunIdFromMessages(initialMessages);
1270
+ const _currentRunId = useRef(initialRunId);
1217
1271
  const _onChunk = useRef(void 0);
1218
- const [messages, setMessages] = useState(
1219
- () => resolveInitialMessages(initializeMessages?.() || [])
1220
- );
1272
+ const [messages, setMessages] = useState(() => resolveInitialMessages(initialMessages));
1221
1273
  const [toolCallApprovals, setToolCallApprovals] = useState({});
1222
1274
  const baseClient = useMastraClient();
1223
1275
  const [isRunning, setIsRunning] = useState(false);
@@ -1227,7 +1279,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1227
1279
  threadId,
1228
1280
  modelSettings,
1229
1281
  signal,
1230
- onFinish
1282
+ onFinish,
1283
+ tracingOptions
1231
1284
  }) => {
1232
1285
  const {
1233
1286
  frequencyPenalty,
@@ -1249,7 +1302,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1249
1302
  const agent = clientWithAbort.getAgent(agentId);
1250
1303
  const response = await agent.generate({
1251
1304
  messages: coreUserMessages,
1252
- runId: agentId,
1305
+ runId: v4(),
1253
1306
  maxSteps,
1254
1307
  modelSettings: {
1255
1308
  frequencyPenalty,
@@ -1262,8 +1315,9 @@ const useChat = ({ agentId, initializeMessages }) => {
1262
1315
  },
1263
1316
  instructions,
1264
1317
  requestContext,
1265
- ...threadId ? { threadId, resourceId: agentId } : {},
1266
- providerOptions
1318
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1319
+ providerOptions,
1320
+ tracingOptions
1267
1321
  });
1268
1322
  setIsRunning(false);
1269
1323
  if (response && "uiMessages" in response.response && response.response.uiMessages) {
@@ -1277,7 +1331,15 @@ const useChat = ({ agentId, initializeMessages }) => {
1277
1331
  setMessages((prev) => [...prev, ...mastraUIMessages]);
1278
1332
  }
1279
1333
  };
1280
- const stream = async ({ coreUserMessages, requestContext, threadId, onChunk, modelSettings, signal }) => {
1334
+ const stream = async ({
1335
+ coreUserMessages,
1336
+ requestContext,
1337
+ threadId,
1338
+ onChunk,
1339
+ modelSettings,
1340
+ signal,
1341
+ tracingOptions
1342
+ }) => {
1281
1343
  const {
1282
1344
  frequencyPenalty,
1283
1345
  presencePenalty,
@@ -1297,7 +1359,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1297
1359
  abortSignal: signal
1298
1360
  });
1299
1361
  const agent = clientWithAbort.getAgent(agentId);
1300
- const runId = agentId;
1362
+ const runId = v4();
1301
1363
  const response = await agent.stream({
1302
1364
  messages: coreUserMessages,
1303
1365
  runId,
@@ -1313,9 +1375,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1313
1375
  },
1314
1376
  instructions,
1315
1377
  requestContext,
1316
- ...threadId ? { threadId, resourceId: agentId } : {},
1378
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1317
1379
  providerOptions,
1318
- requireToolApproval
1380
+ requireToolApproval,
1381
+ tracingOptions
1319
1382
  });
1320
1383
  _onChunk.current = onChunk;
1321
1384
  _currentRunId.current = runId;
@@ -1333,7 +1396,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1333
1396
  threadId,
1334
1397
  onNetworkChunk,
1335
1398
  modelSettings,
1336
- signal
1399
+ signal,
1400
+ tracingOptions
1337
1401
  }) => {
1338
1402
  const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
1339
1403
  setIsRunning(true);
@@ -1342,6 +1406,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1342
1406
  abortSignal: signal
1343
1407
  });
1344
1408
  const agent = clientWithAbort.getAgent(agentId);
1409
+ const runId = v4();
1345
1410
  const response = await agent.network({
1346
1411
  messages: coreUserMessages,
1347
1412
  maxSteps,
@@ -1354,9 +1419,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1354
1419
  topK,
1355
1420
  topP
1356
1421
  },
1357
- runId: agentId,
1422
+ runId,
1358
1423
  requestContext,
1359
- ...threadId ? { thread: threadId, resourceId: agentId } : {}
1424
+ ...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {},
1425
+ tracingOptions
1360
1426
  });
1361
1427
  const transformer = new AISdkNetworkTransformer();
1362
1428
  await response.processDataStream({