@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.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,6 +467,30 @@ 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;
@@ -635,13 +706,14 @@ const toAssistantUIMessage = (message) => {
635
706
  return baseToolCall;
636
707
  }
637
708
  if (part.type.startsWith("tool-") && part.state !== "input-available") {
638
- 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 : {};
639
711
  const baseToolCall = {
640
712
  type: "tool-call",
641
713
  toolCallId: "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : "",
642
- toolName,
643
- argsText: "input" in part ? JSON.stringify(part.input) : "{}",
644
- args: "input" in part ? part.input : {},
714
+ toolName: toolName2,
715
+ argsText: JSON.stringify(cleanInput ?? {}),
716
+ args: cleanInput ?? {},
645
717
  metadata: message.metadata
646
718
  };
647
719
  if ("output" in part) {
@@ -651,6 +723,31 @@ const toAssistantUIMessage = (message) => {
651
723
  }
652
724
  return baseToolCall;
653
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
+ }
654
751
  return {
655
752
  type: "text",
656
753
  text: "",
@@ -703,28 +800,34 @@ const resolveInitialMessages = (messages) => {
703
800
  const primitiveType = json.primitiveType || "";
704
801
  const primitiveId = json.primitiveId || "";
705
802
  const finalResult = json.finalResult;
706
- const toolCalls = finalResult?.toolCalls || [];
803
+ const messages2 = finalResult?.messages || [];
707
804
  const childMessages = [];
708
- for (const toolCall of toolCalls) {
709
- if (toolCall.type === "tool-call" && toolCall.payload) {
710
- const toolCallId = toolCall.payload.toolCallId;
711
- let toolResult;
712
- for (const message2 of finalResult?.messages || []) {
713
- for (const part of message2.content || []) {
714
- if (typeof part === "object" && part.type === "tool-result" && part.toolCallId === toolCallId) {
715
- toolResult = part;
716
- break;
717
- }
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
+ });
718
829
  }
719
830
  }
720
- const isWorkflow = Boolean(toolResult?.result?.result?.steps);
721
- childMessages.push({
722
- type: "tool",
723
- toolCallId: toolCall.payload.toolCallId,
724
- toolName: toolCall.payload.toolName,
725
- args: toolCall.payload.args,
726
- toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
727
- });
728
831
  }
729
832
  }
730
833
  if (finalResult && finalResult.text) {
@@ -737,7 +840,6 @@ const resolveInitialMessages = (messages) => {
737
840
  childMessages,
738
841
  result: finalResult?.text || ""
739
842
  };
740
- console.log("json", json);
741
843
  const nextMessage = {
742
844
  role: "assistant",
743
845
  parts: [
@@ -765,6 +867,29 @@ const resolveInitialMessages = (messages) => {
765
867
  return message;
766
868
  }
767
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
+ }
768
893
  return message;
769
894
  });
770
895
  };
@@ -1213,12 +1338,24 @@ const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
1213
1338
  };
1214
1339
  };
1215
1340
 
1216
- const useChat = ({ agentId, initializeMessages }) => {
1217
- 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);
1218
1357
  const _onChunk = useRef(void 0);
1219
- const [messages, setMessages] = useState(
1220
- () => resolveInitialMessages(initializeMessages?.() || [])
1221
- );
1358
+ const [messages, setMessages] = useState(() => resolveInitialMessages(initialMessages));
1222
1359
  const [toolCallApprovals, setToolCallApprovals] = useState({});
1223
1360
  const baseClient = useMastraClient();
1224
1361
  const [isRunning, setIsRunning] = useState(false);
@@ -1228,7 +1365,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1228
1365
  threadId,
1229
1366
  modelSettings,
1230
1367
  signal,
1231
- onFinish
1368
+ onFinish,
1369
+ tracingOptions
1232
1370
  }) => {
1233
1371
  const {
1234
1372
  frequencyPenalty,
@@ -1250,7 +1388,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1250
1388
  const agent = clientWithAbort.getAgent(agentId);
1251
1389
  const response = await agent.generate({
1252
1390
  messages: coreUserMessages,
1253
- runId: agentId,
1391
+ runId: v4(),
1254
1392
  maxSteps,
1255
1393
  modelSettings: {
1256
1394
  frequencyPenalty,
@@ -1263,8 +1401,9 @@ const useChat = ({ agentId, initializeMessages }) => {
1263
1401
  },
1264
1402
  instructions,
1265
1403
  requestContext,
1266
- ...threadId ? { threadId, resourceId: agentId } : {},
1267
- providerOptions
1404
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1405
+ providerOptions,
1406
+ tracingOptions
1268
1407
  });
1269
1408
  setIsRunning(false);
1270
1409
  if (response && "uiMessages" in response.response && response.response.uiMessages) {
@@ -1278,7 +1417,15 @@ const useChat = ({ agentId, initializeMessages }) => {
1278
1417
  setMessages((prev) => [...prev, ...mastraUIMessages]);
1279
1418
  }
1280
1419
  };
1281
- 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
+ }) => {
1282
1429
  const {
1283
1430
  frequencyPenalty,
1284
1431
  presencePenalty,
@@ -1298,7 +1445,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1298
1445
  abortSignal: signal
1299
1446
  });
1300
1447
  const agent = clientWithAbort.getAgent(agentId);
1301
- const runId = agentId;
1448
+ const runId = v4();
1302
1449
  const response = await agent.stream({
1303
1450
  messages: coreUserMessages,
1304
1451
  runId,
@@ -1314,9 +1461,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1314
1461
  },
1315
1462
  instructions,
1316
1463
  requestContext,
1317
- ...threadId ? { threadId, resourceId: agentId } : {},
1464
+ ...threadId ? { threadId, resourceId: resourceId || agentId } : {},
1318
1465
  providerOptions,
1319
- requireToolApproval
1466
+ requireToolApproval,
1467
+ tracingOptions
1320
1468
  });
1321
1469
  _onChunk.current = onChunk;
1322
1470
  _currentRunId.current = runId;
@@ -1334,7 +1482,8 @@ const useChat = ({ agentId, initializeMessages }) => {
1334
1482
  threadId,
1335
1483
  onNetworkChunk,
1336
1484
  modelSettings,
1337
- signal
1485
+ signal,
1486
+ tracingOptions
1338
1487
  }) => {
1339
1488
  const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
1340
1489
  setIsRunning(true);
@@ -1343,6 +1492,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1343
1492
  abortSignal: signal
1344
1493
  });
1345
1494
  const agent = clientWithAbort.getAgent(agentId);
1495
+ const runId = v4();
1346
1496
  const response = await agent.network({
1347
1497
  messages: coreUserMessages,
1348
1498
  maxSteps,
@@ -1355,9 +1505,10 @@ const useChat = ({ agentId, initializeMessages }) => {
1355
1505
  topK,
1356
1506
  topP
1357
1507
  },
1358
- runId: agentId,
1508
+ runId,
1359
1509
  requestContext,
1360
- ...threadId ? { thread: threadId, resourceId: agentId } : {}
1510
+ ...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {},
1511
+ tracingOptions
1361
1512
  });
1362
1513
  const transformer = new AISdkNetworkTransformer();
1363
1514
  await response.processDataStream({