@mastra/react 0.0.0-refactor-agent-information-for-recomposable-ui-20251112151814 → 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.cjs CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const jsxRuntime = require('react/jsx-runtime');
6
6
  const react = require('react');
7
7
  const clientJs = require('@mastra/client-js');
8
+ const uuid = require('@lukeed/uuid');
8
9
  const lucideReact = require('lucide-react');
9
10
  const tailwindMerge = require('tailwind-merge');
10
11
  const hastUtilToJsxRuntime = require('hast-util-to-jsx-runtime');
@@ -50,7 +51,7 @@ const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
50
51
  return {
51
52
  ...prev,
52
53
  status: chunk.payload.workflowStatus,
53
- ...finalStatus === "success" && lastStep?.status === "success" ? { result: lastStep?.output } : finalStatus === "failed" && lastStep?.status === "failed" ? { error: lastStep?.error } : {}
54
+ ...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 } : {}
54
55
  };
55
56
  }
56
57
  const { stepCallId, stepName, ...newPayload } = chunk.payload ?? {};
@@ -102,6 +103,34 @@ const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
102
103
  };
103
104
  const toUIMessage = ({ chunk, conversation, metadata }) => {
104
105
  const result = [...conversation];
106
+ if (chunk.type.startsWith("data-")) {
107
+ const lastMessage = result[result.length - 1];
108
+ if (!lastMessage || lastMessage.role !== "assistant") {
109
+ const newMessage = {
110
+ id: `data-${chunk.runId}-${Date.now()}`,
111
+ role: "assistant",
112
+ parts: [
113
+ {
114
+ type: chunk.type,
115
+ data: "data" in chunk ? chunk.data : void 0
116
+ }
117
+ ],
118
+ metadata
119
+ };
120
+ return [...result, newMessage];
121
+ }
122
+ const updatedMessage = {
123
+ ...lastMessage,
124
+ parts: [
125
+ ...lastMessage.parts,
126
+ {
127
+ type: chunk.type,
128
+ data: "data" in chunk ? chunk.data : void 0
129
+ }
130
+ ]
131
+ };
132
+ return [...result.slice(0, -1), updatedMessage];
133
+ }
105
134
  switch (chunk.type) {
106
135
  case "tripwire": {
107
136
  const newMessage = {
@@ -110,19 +139,24 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
110
139
  parts: [
111
140
  {
112
141
  type: "text",
113
- text: chunk.payload.tripwireReason
142
+ text: chunk.payload.reason
114
143
  }
115
144
  ],
116
145
  metadata: {
117
146
  ...metadata,
118
- status: "warning"
147
+ status: "tripwire",
148
+ tripwire: {
149
+ retry: chunk.payload.retry,
150
+ tripwirePayload: chunk.payload.metadata,
151
+ processorId: chunk.payload.processorId
152
+ }
119
153
  }
120
154
  };
121
155
  return [...result, newMessage];
122
156
  }
123
157
  case "start": {
124
158
  const newMessage = {
125
- id: `start-${chunk.runId + Date.now()}`,
159
+ id: typeof chunk.payload.messageId === "string" ? chunk.payload.messageId : `start-${chunk.runId + Date.now()}`,
126
160
  role: "assistant",
127
161
  parts: [],
128
162
  metadata
@@ -259,17 +293,19 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
259
293
  if (!lastMessage || lastMessage.role !== "assistant") return result;
260
294
  const parts = [...lastMessage.parts];
261
295
  const toolPartIndex = parts.findIndex(
262
- (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
296
+ (part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
263
297
  );
264
298
  if (toolPartIndex !== -1) {
265
299
  const toolPart = parts[toolPartIndex];
266
- if (toolPart.type === "dynamic-tool") {
300
+ if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
301
+ const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
302
+ const toolCallId = toolPart.toolCallId;
267
303
  if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
268
304
  const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
269
305
  parts[toolPartIndex] = {
270
306
  type: "dynamic-tool",
271
- toolName: toolPart.toolName,
272
- toolCallId: toolPart.toolCallId,
307
+ toolName,
308
+ toolCallId,
273
309
  state: "output-error",
274
310
  input: toolPart.input,
275
311
  errorText: String(error),
@@ -288,8 +324,8 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
288
324
  }
289
325
  parts[toolPartIndex] = {
290
326
  type: "dynamic-tool",
291
- toolName: toolPart.toolName,
292
- toolCallId: toolPart.toolCallId,
327
+ toolName,
328
+ toolCallId,
293
329
  state: "output-available",
294
330
  input: toolPart.input,
295
331
  output,
@@ -311,11 +347,14 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
311
347
  if (!lastMessage || lastMessage.role !== "assistant") return result;
312
348
  const parts = [...lastMessage.parts];
313
349
  const toolPartIndex = parts.findIndex(
314
- (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
350
+ (part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
315
351
  );
316
352
  if (toolPartIndex !== -1) {
317
353
  const toolPart = parts[toolPartIndex];
318
- if (toolPart.type === "dynamic-tool") {
354
+ if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
355
+ const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : typeof toolPart.type === "string" && toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
356
+ const toolCallId = toolPart.toolCallId;
357
+ const input = toolPart.input;
319
358
  if (chunk.payload.output?.type?.startsWith("workflow-")) {
320
359
  const existingWorkflowState = toolPart.output || {};
321
360
  const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
@@ -323,7 +362,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
323
362
  chunk.payload.output
324
363
  );
325
364
  parts[toolPartIndex] = {
326
- ...toolPart,
365
+ type: "dynamic-tool",
366
+ toolName,
367
+ toolCallId,
368
+ state: "input-streaming",
369
+ input,
327
370
  output: updatedWorkflowState
328
371
  };
329
372
  } else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
@@ -332,7 +375,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
332
375
  const currentOutput = toolPart.output || [];
333
376
  const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
334
377
  parts[toolPartIndex] = {
335
- ...toolPart,
378
+ type: "dynamic-tool",
379
+ toolName,
380
+ toolCallId,
381
+ state: "input-streaming",
382
+ input,
336
383
  output: [...existingOutput, chunk.payload.output]
337
384
  };
338
385
  }
@@ -414,7 +461,7 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
414
461
  mode: "stream",
415
462
  requireApprovalMetadata: {
416
463
  ...lastRequireApprovalMetadata,
417
- [chunk.payload.toolCallId]: {
464
+ [chunk.payload.toolName]: {
418
465
  toolCallId: chunk.payload.toolCallId,
419
466
  toolName: chunk.payload.toolName,
420
467
  args: chunk.payload.args
@@ -424,6 +471,30 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
424
471
  }
425
472
  ];
426
473
  }
474
+ case "tool-call-suspended": {
475
+ const lastMessage = result[result.length - 1];
476
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
477
+ const lastSuspendedTools = lastMessage.metadata?.mode === "stream" ? lastMessage.metadata?.suspendedTools : {};
478
+ return [
479
+ ...result.slice(0, -1),
480
+ {
481
+ ...lastMessage,
482
+ metadata: {
483
+ ...lastMessage.metadata,
484
+ mode: "stream",
485
+ suspendedTools: {
486
+ ...lastSuspendedTools,
487
+ [chunk.payload.toolName]: {
488
+ toolCallId: chunk.payload.toolCallId,
489
+ toolName: chunk.payload.toolName,
490
+ args: chunk.payload.args,
491
+ suspendPayload: chunk.payload.suspendPayload
492
+ }
493
+ }
494
+ }
495
+ }
496
+ ];
497
+ }
427
498
  case "finish": {
428
499
  const lastMessage = result[result.length - 1];
429
500
  if (!lastMessage || lastMessage.role !== "assistant") return result;
@@ -639,13 +710,14 @@ const toAssistantUIMessage = (message) => {
639
710
  return baseToolCall;
640
711
  }
641
712
  if (part.type.startsWith("tool-") && part.state !== "input-available") {
642
- const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.substring(5);
713
+ const toolName2 = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.substring(5);
714
+ const { suspendedToolRunId, ...cleanInput } = "input" in part ? part.input : {};
643
715
  const baseToolCall = {
644
716
  type: "tool-call",
645
717
  toolCallId: "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : "",
646
- toolName,
647
- argsText: "input" in part ? JSON.stringify(part.input) : "{}",
648
- args: "input" in part ? part.input : {},
718
+ toolName: toolName2,
719
+ argsText: JSON.stringify(cleanInput ?? {}),
720
+ args: cleanInput ?? {},
649
721
  metadata: message.metadata
650
722
  };
651
723
  if ("output" in part) {
@@ -655,6 +727,31 @@ const toAssistantUIMessage = (message) => {
655
727
  }
656
728
  return baseToolCall;
657
729
  }
730
+ const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.startsWith("tool-") ? part.type.substring(5) : "";
731
+ const requireApprovalMetadata = extendedMessage.metadata?.requireApprovalMetadata;
732
+ const suspendedTools = extendedMessage.metadata?.suspendedTools;
733
+ const partToolCallId = "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : void 0;
734
+ const suspensionData = toolName ? requireApprovalMetadata?.[toolName] ?? suspendedTools?.[toolName] : void 0;
735
+ if (suspensionData) {
736
+ const { suspendedToolRunId, ...cleanInput } = "input" in part ? part.input : {};
737
+ return {
738
+ type: "tool-call",
739
+ toolCallId: partToolCallId,
740
+ toolName,
741
+ argsText: JSON.stringify(cleanInput ?? {}),
742
+ args: cleanInput,
743
+ metadata: extendedMessage.metadata
744
+ };
745
+ }
746
+ if (part.type.startsWith("data-")) {
747
+ return {
748
+ type: "data",
749
+ name: part.type.substring(5),
750
+ // Extract name from 'data-{name}'
751
+ data: part.data,
752
+ metadata: message.metadata
753
+ };
754
+ }
658
755
  return {
659
756
  type: "text",
660
757
  text: "",
@@ -707,28 +804,34 @@ const resolveInitialMessages = (messages) => {
707
804
  const primitiveType = json.primitiveType || "";
708
805
  const primitiveId = json.primitiveId || "";
709
806
  const finalResult = json.finalResult;
710
- const toolCalls = finalResult?.toolCalls || [];
807
+ const messages2 = finalResult?.messages || [];
711
808
  const childMessages = [];
712
- for (const toolCall of toolCalls) {
713
- if (toolCall.type === "tool-call" && toolCall.payload) {
714
- const toolCallId = toolCall.payload.toolCallId;
715
- let toolResult;
716
- for (const message2 of finalResult?.messages || []) {
717
- for (const part of message2.content || []) {
718
- if (typeof part === "object" && part.type === "tool-result" && part.toolCallId === toolCallId) {
719
- toolResult = part;
720
- break;
721
- }
809
+ const toolResultMap = /* @__PURE__ */ new Map();
810
+ for (const msg of messages2) {
811
+ if (Array.isArray(msg.content)) {
812
+ for (const part of msg.content) {
813
+ if (typeof part === "object" && part.type === "tool-result") {
814
+ toolResultMap.set(part.toolCallId, part);
815
+ }
816
+ }
817
+ }
818
+ }
819
+ for (const msg of messages2) {
820
+ if (msg.type === "tool-call" && Array.isArray(msg.content)) {
821
+ for (const part of msg.content) {
822
+ if (typeof part === "object" && part.type === "tool-call") {
823
+ const toolCallContent = part;
824
+ const toolResult = toolResultMap.get(toolCallContent.toolCallId);
825
+ const isWorkflow = Boolean(toolResult?.result?.result?.steps);
826
+ childMessages.push({
827
+ type: "tool",
828
+ toolCallId: toolCallContent.toolCallId,
829
+ toolName: toolCallContent.toolName,
830
+ args: toolCallContent.args,
831
+ toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
832
+ });
722
833
  }
723
834
  }
724
- const isWorkflow = Boolean(toolResult?.result?.result?.steps);
725
- childMessages.push({
726
- type: "tool",
727
- toolCallId: toolCall.payload.toolCallId,
728
- toolName: toolCall.payload.toolName,
729
- args: toolCall.payload.args,
730
- toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
731
- });
732
835
  }
733
836
  }
734
837
  if (finalResult && finalResult.text) {
@@ -741,7 +844,6 @@ const resolveInitialMessages = (messages) => {
741
844
  childMessages,
742
845
  result: finalResult?.text || ""
743
846
  };
744
- console.log("json", json);
745
847
  const nextMessage = {
746
848
  role: "assistant",
747
849
  parts: [
@@ -769,6 +871,29 @@ const resolveInitialMessages = (messages) => {
769
871
  return message;
770
872
  }
771
873
  }
874
+ const extendedMessage = message;
875
+ const pendingToolApprovals = extendedMessage.metadata?.pendingToolApprovals;
876
+ if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
877
+ return {
878
+ ...message,
879
+ metadata: {
880
+ ...message.metadata,
881
+ mode: "stream",
882
+ requireApprovalMetadata: pendingToolApprovals
883
+ }
884
+ };
885
+ }
886
+ const suspendedTools = extendedMessage.metadata?.suspendedTools;
887
+ if (suspendedTools && typeof suspendedTools === "object") {
888
+ return {
889
+ ...message,
890
+ metadata: {
891
+ ...message.metadata,
892
+ mode: "stream",
893
+ suspendedTools
894
+ }
895
+ };
896
+ }
772
897
  return message;
773
898
  });
774
899
  };
@@ -1217,12 +1342,24 @@ const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
1217
1342
  };
1218
1343
  };
1219
1344
 
1220
- const useChat = ({ agentId, initializeMessages }) => {
1221
- const _currentRunId = react.useRef(void 0);
1345
+ const useChat = ({ agentId, resourceId, initializeMessages }) => {
1346
+ const extractRunIdFromMessages = (messages2) => {
1347
+ for (const message of messages2) {
1348
+ const pendingToolApprovals = message.metadata?.pendingToolApprovals;
1349
+ if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
1350
+ const suspensionData = Object.values(pendingToolApprovals)[0];
1351
+ if (suspensionData?.runId) {
1352
+ return suspensionData.runId;
1353
+ }
1354
+ }
1355
+ }
1356
+ return void 0;
1357
+ };
1358
+ const initialMessages = initializeMessages?.() || [];
1359
+ const initialRunId = extractRunIdFromMessages(initialMessages);
1360
+ const _currentRunId = react.useRef(initialRunId);
1222
1361
  const _onChunk = react.useRef(void 0);
1223
- const [messages, setMessages] = react.useState(
1224
- () => resolveInitialMessages(initializeMessages?.() || [])
1225
- );
1362
+ const [messages, setMessages] = react.useState(() => resolveInitialMessages(initialMessages));
1226
1363
  const [toolCallApprovals, setToolCallApprovals] = react.useState({});
1227
1364
  const baseClient = useMastraClient();
1228
1365
  const [isRunning, setIsRunning] = react.useState(false);
@@ -1232,7 +1369,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1232
1369
  threadId,
1233
1370
  modelSettings,
1234
1371
  signal,
1235
- onFinish
1372
+ onFinish,
1373
+ tracingOptions
1236
1374
  }) => {
1237
1375
  const {
1238
1376
  frequencyPenalty,
@@ -1254,7 +1392,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1254
1392
  const agent = clientWithAbort.getAgent(agentId);
1255
1393
  const response = await agent.generate({
1256
1394
  messages: coreUserMessages,
1257
- runId: agentId,
1395
+ runId: uuid.v4(),
1258
1396
  maxSteps,
1259
1397
  modelSettings: {
1260
1398
  frequencyPenalty,
@@ -1267,8 +1405,9 @@ const useChat = ({ agentId, initializeMessages }) => {
1267
1405
  },
1268
1406
  instructions,
1269
1407
  requestContext,
1270
- ...threadId ? { threadId, resourceId: agentId } : {},
1271
- providerOptions
1408
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1409
+ providerOptions,
1410
+ tracingOptions
1272
1411
  });
1273
1412
  setIsRunning(false);
1274
1413
  if (response && "uiMessages" in response.response && response.response.uiMessages) {
@@ -1282,7 +1421,15 @@ const useChat = ({ agentId, initializeMessages }) => {
1282
1421
  setMessages((prev) => [...prev, ...mastraUIMessages]);
1283
1422
  }
1284
1423
  };
1285
- const stream = async ({ coreUserMessages, requestContext, threadId, onChunk, modelSettings, signal }) => {
1424
+ const stream = async ({
1425
+ coreUserMessages,
1426
+ requestContext,
1427
+ threadId,
1428
+ onChunk,
1429
+ modelSettings,
1430
+ signal,
1431
+ tracingOptions
1432
+ }) => {
1286
1433
  const {
1287
1434
  frequencyPenalty,
1288
1435
  presencePenalty,
@@ -1302,7 +1449,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1302
1449
  abortSignal: signal
1303
1450
  });
1304
1451
  const agent = clientWithAbort.getAgent(agentId);
1305
- const runId = agentId;
1452
+ const runId = uuid.v4();
1306
1453
  const response = await agent.stream({
1307
1454
  messages: coreUserMessages,
1308
1455
  runId,
@@ -1318,9 +1465,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1318
1465
  },
1319
1466
  instructions,
1320
1467
  requestContext,
1321
- ...threadId ? { threadId, resourceId: agentId } : {},
1468
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1322
1469
  providerOptions,
1323
- requireToolApproval
1470
+ requireToolApproval,
1471
+ tracingOptions
1324
1472
  });
1325
1473
  _onChunk.current = onChunk;
1326
1474
  _currentRunId.current = runId;
@@ -1338,7 +1486,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1338
1486
  threadId,
1339
1487
  onNetworkChunk,
1340
1488
  modelSettings,
1341
- signal
1489
+ signal,
1490
+ tracingOptions
1342
1491
  }) => {
1343
1492
  const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
1344
1493
  setIsRunning(true);
@@ -1347,6 +1496,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1347
1496
  abortSignal: signal
1348
1497
  });
1349
1498
  const agent = clientWithAbort.getAgent(agentId);
1499
+ const runId = uuid.v4();
1350
1500
  const response = await agent.network({
1351
1501
  messages: coreUserMessages,
1352
1502
  maxSteps,
@@ -1359,9 +1509,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1359
1509
  topK,
1360
1510
  topP
1361
1511
  },
1362
- runId: agentId,
1512
+ runId,
1363
1513
  requestContext,
1364
- ...threadId ? { thread: threadId, resourceId: agentId } : {}
1514
+ ...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {},
1515
+ tracingOptions
1365
1516
  });
1366
1517
  const transformer = new AISdkNetworkTransformer();
1367
1518
  await response.processDataStream({