@mastra/react 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-remove-ai-peer-dep-from-evals-20260105220639

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';
@@ -46,7 +47,7 @@ const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
46
47
  return {
47
48
  ...prev,
48
49
  status: chunk.payload.workflowStatus,
49
- ...finalStatus === "success" && lastStep?.status === "success" ? { result: lastStep?.output } : finalStatus === "failed" && lastStep?.status === "failed" ? { error: lastStep?.error } : {}
50
+ ...finalStatus === "success" && lastStep?.status === "success" ? { result: lastStep?.output } : finalStatus === "failed" && lastStep?.status === "failed" ? { error: lastStep?.error } : finalStatus === "tripwire" && chunk.payload.tripwire ? { tripwire: chunk.payload.tripwire } : {}
50
51
  };
51
52
  }
52
53
  const { stepCallId, stepName, ...newPayload } = chunk.payload ?? {};
@@ -98,6 +99,34 @@ const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
98
99
  };
99
100
  const toUIMessage = ({ chunk, conversation, metadata }) => {
100
101
  const result = [...conversation];
102
+ if (chunk.type.startsWith("data-")) {
103
+ const lastMessage = result[result.length - 1];
104
+ if (!lastMessage || lastMessage.role !== "assistant") {
105
+ const newMessage = {
106
+ id: `data-${chunk.runId}-${Date.now()}`,
107
+ role: "assistant",
108
+ parts: [
109
+ {
110
+ type: chunk.type,
111
+ data: "data" in chunk ? chunk.data : void 0
112
+ }
113
+ ],
114
+ metadata
115
+ };
116
+ return [...result, newMessage];
117
+ }
118
+ const updatedMessage = {
119
+ ...lastMessage,
120
+ parts: [
121
+ ...lastMessage.parts,
122
+ {
123
+ type: chunk.type,
124
+ data: "data" in chunk ? chunk.data : void 0
125
+ }
126
+ ]
127
+ };
128
+ return [...result.slice(0, -1), updatedMessage];
129
+ }
101
130
  switch (chunk.type) {
102
131
  case "tripwire": {
103
132
  const newMessage = {
@@ -106,19 +135,24 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
106
135
  parts: [
107
136
  {
108
137
  type: "text",
109
- text: chunk.payload.tripwireReason
138
+ text: chunk.payload.reason
110
139
  }
111
140
  ],
112
141
  metadata: {
113
142
  ...metadata,
114
- status: "warning"
143
+ status: "tripwire",
144
+ tripwire: {
145
+ retry: chunk.payload.retry,
146
+ tripwirePayload: chunk.payload.metadata,
147
+ processorId: chunk.payload.processorId
148
+ }
115
149
  }
116
150
  };
117
151
  return [...result, newMessage];
118
152
  }
119
153
  case "start": {
120
154
  const newMessage = {
121
- id: `start-${chunk.runId + Date.now()}`,
155
+ id: typeof chunk.payload.messageId === "string" ? chunk.payload.messageId : `start-${chunk.runId + Date.now()}`,
122
156
  role: "assistant",
123
157
  parts: [],
124
158
  metadata
@@ -255,17 +289,19 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
255
289
  if (!lastMessage || lastMessage.role !== "assistant") return result;
256
290
  const parts = [...lastMessage.parts];
257
291
  const toolPartIndex = parts.findIndex(
258
- (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
292
+ (part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
259
293
  );
260
294
  if (toolPartIndex !== -1) {
261
295
  const toolPart = parts[toolPartIndex];
262
- if (toolPart.type === "dynamic-tool") {
296
+ if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
297
+ const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
298
+ const toolCallId = toolPart.toolCallId;
263
299
  if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
264
300
  const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
265
301
  parts[toolPartIndex] = {
266
302
  type: "dynamic-tool",
267
- toolName: toolPart.toolName,
268
- toolCallId: toolPart.toolCallId,
303
+ toolName,
304
+ toolCallId,
269
305
  state: "output-error",
270
306
  input: toolPart.input,
271
307
  errorText: String(error),
@@ -284,8 +320,8 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
284
320
  }
285
321
  parts[toolPartIndex] = {
286
322
  type: "dynamic-tool",
287
- toolName: toolPart.toolName,
288
- toolCallId: toolPart.toolCallId,
323
+ toolName,
324
+ toolCallId,
289
325
  state: "output-available",
290
326
  input: toolPart.input,
291
327
  output,
@@ -307,11 +343,14 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
307
343
  if (!lastMessage || lastMessage.role !== "assistant") return result;
308
344
  const parts = [...lastMessage.parts];
309
345
  const toolPartIndex = parts.findIndex(
310
- (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
346
+ (part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
311
347
  );
312
348
  if (toolPartIndex !== -1) {
313
349
  const toolPart = parts[toolPartIndex];
314
- if (toolPart.type === "dynamic-tool") {
350
+ if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
351
+ const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : typeof toolPart.type === "string" && toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
352
+ const toolCallId = toolPart.toolCallId;
353
+ const input = toolPart.input;
315
354
  if (chunk.payload.output?.type?.startsWith("workflow-")) {
316
355
  const existingWorkflowState = toolPart.output || {};
317
356
  const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
@@ -319,7 +358,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
319
358
  chunk.payload.output
320
359
  );
321
360
  parts[toolPartIndex] = {
322
- ...toolPart,
361
+ type: "dynamic-tool",
362
+ toolName,
363
+ toolCallId,
364
+ state: "input-streaming",
365
+ input,
323
366
  output: updatedWorkflowState
324
367
  };
325
368
  } else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
@@ -328,7 +371,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
328
371
  const currentOutput = toolPart.output || [];
329
372
  const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
330
373
  parts[toolPartIndex] = {
331
- ...toolPart,
374
+ type: "dynamic-tool",
375
+ toolName,
376
+ toolCallId,
377
+ state: "input-streaming",
378
+ input,
332
379
  output: [...existingOutput, chunk.payload.output]
333
380
  };
334
381
  }
@@ -410,7 +457,7 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
410
457
  mode: "stream",
411
458
  requireApprovalMetadata: {
412
459
  ...lastRequireApprovalMetadata,
413
- [chunk.payload.toolCallId]: {
460
+ [chunk.payload.toolName]: {
414
461
  toolCallId: chunk.payload.toolCallId,
415
462
  toolName: chunk.payload.toolName,
416
463
  args: chunk.payload.args
@@ -420,15 +467,38 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
420
467
  }
421
468
  ];
422
469
  }
470
+ case "tool-call-suspended": {
471
+ const lastMessage = result[result.length - 1];
472
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
473
+ const lastSuspendedTools = lastMessage.metadata?.mode === "stream" ? lastMessage.metadata?.suspendedTools : {};
474
+ return [
475
+ ...result.slice(0, -1),
476
+ {
477
+ ...lastMessage,
478
+ metadata: {
479
+ ...lastMessage.metadata,
480
+ mode: "stream",
481
+ suspendedTools: {
482
+ ...lastSuspendedTools,
483
+ [chunk.payload.toolName]: {
484
+ toolCallId: chunk.payload.toolCallId,
485
+ toolName: chunk.payload.toolName,
486
+ args: chunk.payload.args,
487
+ suspendPayload: chunk.payload.suspendPayload
488
+ }
489
+ }
490
+ }
491
+ }
492
+ ];
493
+ }
423
494
  case "finish": {
424
495
  const lastMessage = result[result.length - 1];
425
496
  if (!lastMessage || lastMessage.role !== "assistant") return result;
426
497
  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" };
498
+ if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
499
+ if (part.type === "text" || part.type === "reasoning") {
500
+ return { ...part, state: "done" };
501
+ }
432
502
  }
433
503
  return part;
434
504
  });
@@ -636,13 +706,14 @@ const toAssistantUIMessage = (message) => {
636
706
  return baseToolCall;
637
707
  }
638
708
  if (part.type.startsWith("tool-") && part.state !== "input-available") {
639
- const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.substring(5);
709
+ const toolName2 = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.substring(5);
710
+ const { suspendedToolRunId, ...cleanInput } = "input" in part ? part.input : {};
640
711
  const baseToolCall = {
641
712
  type: "tool-call",
642
713
  toolCallId: "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : "",
643
- toolName,
644
- argsText: "input" in part ? JSON.stringify(part.input) : "{}",
645
- args: "input" in part ? part.input : {},
714
+ toolName: toolName2,
715
+ argsText: JSON.stringify(cleanInput ?? {}),
716
+ args: cleanInput ?? {},
646
717
  metadata: message.metadata
647
718
  };
648
719
  if ("output" in part) {
@@ -652,6 +723,31 @@ const toAssistantUIMessage = (message) => {
652
723
  }
653
724
  return baseToolCall;
654
725
  }
726
+ const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.startsWith("tool-") ? part.type.substring(5) : "";
727
+ const requireApprovalMetadata = extendedMessage.metadata?.requireApprovalMetadata;
728
+ const suspendedTools = extendedMessage.metadata?.suspendedTools;
729
+ const partToolCallId = "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : void 0;
730
+ const suspensionData = toolName ? requireApprovalMetadata?.[toolName] ?? suspendedTools?.[toolName] : void 0;
731
+ if (suspensionData) {
732
+ const { suspendedToolRunId, ...cleanInput } = "input" in part ? part.input : {};
733
+ return {
734
+ type: "tool-call",
735
+ toolCallId: partToolCallId,
736
+ toolName,
737
+ argsText: JSON.stringify(cleanInput ?? {}),
738
+ args: cleanInput,
739
+ metadata: extendedMessage.metadata
740
+ };
741
+ }
742
+ if (part.type.startsWith("data-")) {
743
+ return {
744
+ type: "data",
745
+ name: part.type.substring(5),
746
+ // Extract name from 'data-{name}'
747
+ data: part.data,
748
+ metadata: message.metadata
749
+ };
750
+ }
655
751
  return {
656
752
  type: "text",
657
753
  text: "",
@@ -693,7 +789,9 @@ const toAssistantUIMessage = (message) => {
693
789
 
694
790
  const resolveInitialMessages = (messages) => {
695
791
  return messages.map((message) => {
696
- const networkPart = message.parts.find((part) => part.type === "text" && part.text.includes('"isNetwork":true'));
792
+ const networkPart = message.parts.find(
793
+ (part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
794
+ );
697
795
  if (networkPart && networkPart.type === "text") {
698
796
  try {
699
797
  const json = JSON.parse(networkPart.text);
@@ -702,28 +800,34 @@ const resolveInitialMessages = (messages) => {
702
800
  const primitiveType = json.primitiveType || "";
703
801
  const primitiveId = json.primitiveId || "";
704
802
  const finalResult = json.finalResult;
705
- const toolCalls = finalResult?.toolCalls || [];
803
+ const messages2 = finalResult?.messages || [];
706
804
  const childMessages = [];
707
- for (const toolCall of toolCalls) {
708
- if (toolCall.type === "tool-call" && toolCall.payload) {
709
- const toolCallId = toolCall.payload.toolCallId;
710
- let toolResult;
711
- for (const message2 of finalResult?.messages || []) {
712
- for (const part of message2.content || []) {
713
- if (typeof part === "object" && part.type === "tool-result" && part.toolCallId === toolCallId) {
714
- toolResult = part;
715
- break;
716
- }
805
+ const toolResultMap = /* @__PURE__ */ new Map();
806
+ for (const msg of messages2) {
807
+ if (Array.isArray(msg.content)) {
808
+ for (const part of msg.content) {
809
+ if (typeof part === "object" && part.type === "tool-result") {
810
+ toolResultMap.set(part.toolCallId, part);
811
+ }
812
+ }
813
+ }
814
+ }
815
+ for (const msg of messages2) {
816
+ if (msg.type === "tool-call" && Array.isArray(msg.content)) {
817
+ for (const part of msg.content) {
818
+ if (typeof part === "object" && part.type === "tool-call") {
819
+ const toolCallContent = part;
820
+ const toolResult = toolResultMap.get(toolCallContent.toolCallId);
821
+ const isWorkflow = Boolean(toolResult?.result?.result?.steps);
822
+ childMessages.push({
823
+ type: "tool",
824
+ toolCallId: toolCallContent.toolCallId,
825
+ toolName: toolCallContent.toolName,
826
+ args: toolCallContent.args,
827
+ toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
828
+ });
717
829
  }
718
830
  }
719
- const isWorkflow = Boolean(toolResult?.result?.result?.steps);
720
- childMessages.push({
721
- type: "tool",
722
- toolCallId: toolCall.payload.toolCallId,
723
- toolName: toolCall.payload.toolName,
724
- args: toolCall.payload.args,
725
- toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
726
- });
727
831
  }
728
832
  }
729
833
  if (finalResult && finalResult.text) {
@@ -736,7 +840,6 @@ const resolveInitialMessages = (messages) => {
736
840
  childMessages,
737
841
  result: finalResult?.text || ""
738
842
  };
739
- console.log("json", json);
740
843
  const nextMessage = {
741
844
  role: "assistant",
742
845
  parts: [
@@ -764,6 +867,29 @@ const resolveInitialMessages = (messages) => {
764
867
  return message;
765
868
  }
766
869
  }
870
+ const extendedMessage = message;
871
+ const pendingToolApprovals = extendedMessage.metadata?.pendingToolApprovals;
872
+ if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
873
+ return {
874
+ ...message,
875
+ metadata: {
876
+ ...message.metadata,
877
+ mode: "stream",
878
+ requireApprovalMetadata: pendingToolApprovals
879
+ }
880
+ };
881
+ }
882
+ const suspendedTools = extendedMessage.metadata?.suspendedTools;
883
+ if (suspendedTools && typeof suspendedTools === "object") {
884
+ return {
885
+ ...message,
886
+ metadata: {
887
+ ...message.metadata,
888
+ mode: "stream",
889
+ suspendedTools
890
+ }
891
+ };
892
+ }
767
893
  return message;
768
894
  });
769
895
  };
@@ -1212,12 +1338,24 @@ const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
1212
1338
  };
1213
1339
  };
1214
1340
 
1215
- const useChat = ({ agentId, initializeMessages }) => {
1216
- const _currentRunId = useRef(void 0);
1341
+ const useChat = ({ agentId, resourceId, initializeMessages }) => {
1342
+ const extractRunIdFromMessages = (messages2) => {
1343
+ for (const message of messages2) {
1344
+ const pendingToolApprovals = message.metadata?.pendingToolApprovals;
1345
+ if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
1346
+ const suspensionData = Object.values(pendingToolApprovals)[0];
1347
+ if (suspensionData?.runId) {
1348
+ return suspensionData.runId;
1349
+ }
1350
+ }
1351
+ }
1352
+ return void 0;
1353
+ };
1354
+ const initialMessages = initializeMessages?.() || [];
1355
+ const initialRunId = extractRunIdFromMessages(initialMessages);
1356
+ const _currentRunId = useRef(initialRunId);
1217
1357
  const _onChunk = useRef(void 0);
1218
- const [messages, setMessages] = useState(
1219
- () => resolveInitialMessages(initializeMessages?.() || [])
1220
- );
1358
+ const [messages, setMessages] = useState(() => resolveInitialMessages(initialMessages));
1221
1359
  const [toolCallApprovals, setToolCallApprovals] = useState({});
1222
1360
  const baseClient = useMastraClient();
1223
1361
  const [isRunning, setIsRunning] = useState(false);
@@ -1227,7 +1365,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1227
1365
  threadId,
1228
1366
  modelSettings,
1229
1367
  signal,
1230
- onFinish
1368
+ onFinish,
1369
+ tracingOptions
1231
1370
  }) => {
1232
1371
  const {
1233
1372
  frequencyPenalty,
@@ -1249,7 +1388,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1249
1388
  const agent = clientWithAbort.getAgent(agentId);
1250
1389
  const response = await agent.generate({
1251
1390
  messages: coreUserMessages,
1252
- runId: agentId,
1391
+ runId: v4(),
1253
1392
  maxSteps,
1254
1393
  modelSettings: {
1255
1394
  frequencyPenalty,
@@ -1262,8 +1401,9 @@ const useChat = ({ agentId, initializeMessages }) => {
1262
1401
  },
1263
1402
  instructions,
1264
1403
  requestContext,
1265
- ...threadId ? { threadId, resourceId: agentId } : {},
1266
- providerOptions
1404
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1405
+ providerOptions,
1406
+ tracingOptions
1267
1407
  });
1268
1408
  setIsRunning(false);
1269
1409
  if (response && "uiMessages" in response.response && response.response.uiMessages) {
@@ -1277,7 +1417,15 @@ const useChat = ({ agentId, initializeMessages }) => {
1277
1417
  setMessages((prev) => [...prev, ...mastraUIMessages]);
1278
1418
  }
1279
1419
  };
1280
- const stream = async ({ coreUserMessages, requestContext, threadId, onChunk, modelSettings, signal }) => {
1420
+ const stream = async ({
1421
+ coreUserMessages,
1422
+ requestContext,
1423
+ threadId,
1424
+ onChunk,
1425
+ modelSettings,
1426
+ signal,
1427
+ tracingOptions
1428
+ }) => {
1281
1429
  const {
1282
1430
  frequencyPenalty,
1283
1431
  presencePenalty,
@@ -1297,7 +1445,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1297
1445
  abortSignal: signal
1298
1446
  });
1299
1447
  const agent = clientWithAbort.getAgent(agentId);
1300
- const runId = agentId;
1448
+ const runId = v4();
1301
1449
  const response = await agent.stream({
1302
1450
  messages: coreUserMessages,
1303
1451
  runId,
@@ -1313,9 +1461,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1313
1461
  },
1314
1462
  instructions,
1315
1463
  requestContext,
1316
- ...threadId ? { threadId, resourceId: agentId } : {},
1464
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1317
1465
  providerOptions,
1318
- requireToolApproval
1466
+ requireToolApproval,
1467
+ tracingOptions
1319
1468
  });
1320
1469
  _onChunk.current = onChunk;
1321
1470
  _currentRunId.current = runId;
@@ -1333,7 +1482,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1333
1482
  threadId,
1334
1483
  onNetworkChunk,
1335
1484
  modelSettings,
1336
- signal
1485
+ signal,
1486
+ tracingOptions
1337
1487
  }) => {
1338
1488
  const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
1339
1489
  setIsRunning(true);
@@ -1342,6 +1492,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1342
1492
  abortSignal: signal
1343
1493
  });
1344
1494
  const agent = clientWithAbort.getAgent(agentId);
1495
+ const runId = v4();
1345
1496
  const response = await agent.network({
1346
1497
  messages: coreUserMessages,
1347
1498
  maxSteps,
@@ -1354,9 +1505,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1354
1505
  topK,
1355
1506
  topP
1356
1507
  },
1357
- runId: agentId,
1508
+ runId,
1358
1509
  requestContext,
1359
- ...threadId ? { thread: threadId, resourceId: agentId } : {}
1510
+ ...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {},
1511
+ tracingOptions
1360
1512
  });
1361
1513
  const transformer = new AISdkNetworkTransformer();
1362
1514
  await response.processDataStream({