@mastra/react 1.2.5-alpha.9 → 1.2.6-alpha.0

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
@@ -68,6 +68,37 @@ var MastraReactProvider = ({
68
68
  );
69
69
  };
70
70
 
71
+ // src/agent/signal-data.ts
72
+ var uint8ArrayToBase64 = (bytes) => {
73
+ const chunkSize = 32768;
74
+ let binary = "";
75
+ for (let i = 0; i < bytes.length; i += chunkSize) {
76
+ binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
77
+ }
78
+ return btoa(binary);
79
+ };
80
+ function convertSignalDataToBase64String(content) {
81
+ if (typeof content === "string") {
82
+ return content;
83
+ }
84
+ const bytes = content instanceof ArrayBuffer ? new Uint8Array(content) : content;
85
+ return uint8ArrayToBase64(bytes);
86
+ }
87
+ function encodeFilePartDataForStorage(data, mimeType) {
88
+ if (typeof data === "string") {
89
+ return data;
90
+ }
91
+ if (data instanceof URL) {
92
+ return data.toString();
93
+ }
94
+ const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
95
+ const base64 = uint8ArrayToBase64(bytes);
96
+ if (base64.startsWith("data:")) {
97
+ return base64;
98
+ }
99
+ return `data:${mimeType};base64,${base64}`;
100
+ }
101
+
71
102
  // src/lib/mastra-db/formatCompletionFeedback.ts
72
103
  var formatBaseCompletionFeedback = (result, maxIterationReached, formatScorerHeading, incompleteMessage) => {
73
104
  const lines = [];
@@ -314,21 +345,37 @@ var signalContentsToUserMessages = (contents, metadata) => {
314
345
  }
315
346
  if (typedPart.type === "image") {
316
347
  const image = typedPart.image;
317
- return [
318
- {
319
- type: "file",
320
- mediaType: typeof typedPart.mediaType === "string" ? typedPart.mediaType : typeof typedPart.mimeType === "string" ? typedPart.mimeType : "image/*",
321
- url: typeof image === "string" ? image : image instanceof URL ? image.toString() : ""
322
- }
323
- ];
348
+ const mimeType = typeof typedPart.mediaType === "string" ? typedPart.mediaType : typeof typedPart.mimeType === "string" ? typedPart.mimeType : "image/*";
349
+ if (typeof image === "string" || image instanceof URL || image instanceof ArrayBuffer || image instanceof Uint8Array) {
350
+ return [
351
+ {
352
+ type: "file",
353
+ mimeType,
354
+ data: encodeFilePartDataForStorage(image, mimeType)
355
+ }
356
+ ];
357
+ }
358
+ return [{ type: "file", mimeType, data: "" }];
324
359
  }
325
360
  if (typedPart.type === "file") {
326
361
  const data = typedPart.data;
362
+ const mimeType = typeof typedPart.mediaType === "string" ? typedPart.mediaType : typeof typedPart.mimeType === "string" ? typedPart.mimeType : "application/octet-stream";
363
+ if (typeof data === "string" || data instanceof URL || data instanceof ArrayBuffer || data instanceof Uint8Array) {
364
+ return [
365
+ {
366
+ type: "file",
367
+ mimeType,
368
+ data: encodeFilePartDataForStorage(data, mimeType),
369
+ ...typeof typedPart.filename === "string" ? { filename: typedPart.filename } : {}
370
+ }
371
+ ];
372
+ }
373
+ const urlFallback = typeof typedPart.url === "string" ? typedPart.url : "";
327
374
  return [
328
375
  {
329
376
  type: "file",
330
- mediaType: typeof typedPart.mediaType === "string" ? typedPart.mediaType : typeof typedPart.mimeType === "string" ? typedPart.mimeType : "application/octet-stream",
331
- url: typeof data === "string" ? data : data instanceof URL ? data.toString() : "",
377
+ mimeType,
378
+ data: urlFallback,
332
379
  ...typeof typedPart.filename === "string" ? { filename: typedPart.filename } : {}
333
380
  }
334
381
  ];
@@ -353,6 +400,15 @@ var signalContentsToUserMessages = (contents, metadata) => {
353
400
  const parts = content.flatMap(toMessagePart);
354
401
  return parts.length ? [makeUserMessage(parts)] : [];
355
402
  };
403
+ var reconcilePendingUserEcho = (message, echoedContents, metadata, options) => {
404
+ const echoedMessages = signalContentsToUserMessages(echoedContents, metadata);
405
+ const echoedParts = echoedMessages[0]?.content.parts;
406
+ let reconciled = typeof options.signalId === "string" ? { ...message, id: options.signalId } : message;
407
+ if (echoedParts?.length) {
408
+ reconciled = { ...reconciled, content: { ...reconciled.content, parts: echoedParts } };
409
+ }
410
+ return options.keepClientMessageId ? clearPendingStatusKeepClientId(reconciled) : clearPendingStatus(reconciled);
411
+ };
356
412
  var makeToolInvocationPart = (invocation) => ({
357
413
  type: "tool-invocation",
358
414
  toolInvocation: invocation
@@ -373,14 +429,19 @@ var accumulateChunk = ({ chunk, conversation, metadata }) => {
373
429
  )) {
374
430
  return finishStreamingAssistantMessage(
375
431
  result.map(
376
- (message) => message.content.metadata?.status === "pending" && message.content.metadata[CLIENT_MESSAGE_ID_KEY] === echoedClientMessageId ? clearPendingStatusKeepClientId(typeof signalId === "string" ? { ...message, id: signalId } : message) : message
432
+ (message) => message.content.metadata?.status === "pending" && message.content.metadata[CLIENT_MESSAGE_ID_KEY] === echoedClientMessageId ? reconcilePendingUserEcho(message, chunk.data.contents, metadata, {
433
+ signalId: typeof signalId === "string" ? signalId : void 0,
434
+ keepClientMessageId: true
435
+ }) : message
377
436
  )
378
437
  );
379
438
  }
380
439
  if (typeof signalId === "string" && result.some((message) => message.id === signalId)) {
381
440
  return finishStreamingAssistantMessage(
382
441
  result.map(
383
- (message) => message.id === signalId && message.content.metadata?.status === "pending" ? clearPendingStatus(message) : message
442
+ (message) => message.id === signalId && message.content.metadata?.status === "pending" ? reconcilePendingUserEcho(message, chunk.data.contents, metadata, {
443
+ keepClientMessageId: false
444
+ }) : message
384
445
  )
385
446
  );
386
447
  }
@@ -987,17 +1048,17 @@ var accumulateChunk = ({ chunk, conversation, metadata }) => {
987
1048
  const lastMessage = result[result.length - 1];
988
1049
  if (!lastMessage || lastMessage.role !== "assistant") return result;
989
1050
  const parts = [...lastMessage.content.parts];
990
- let url;
1051
+ let data;
991
1052
  if (typeof chunk.payload.data === "string") {
992
- url = chunk.payload.base64 ? `data:${chunk.payload.mimeType};base64,${chunk.payload.data}` : `data:${chunk.payload.mimeType},${encodeURIComponent(chunk.payload.data)}`;
1053
+ data = chunk.payload.base64 ? `data:${chunk.payload.mimeType};base64,${chunk.payload.data}` : `data:${chunk.payload.mimeType},${encodeURIComponent(chunk.payload.data)}`;
993
1054
  } else {
994
- const base64 = btoa(String.fromCharCode(...chunk.payload.data));
995
- url = `data:${chunk.payload.mimeType};base64,${base64}`;
1055
+ const base64 = uint8ArrayToBase64(chunk.payload.data);
1056
+ data = `data:${chunk.payload.mimeType};base64,${base64}`;
996
1057
  }
997
1058
  parts.push({
998
1059
  type: "file",
999
- mediaType: chunk.payload.mimeType,
1000
- url,
1060
+ mimeType: chunk.payload.mimeType,
1061
+ data,
1001
1062
  providerMetadata: chunk.payload.providerMetadata
1002
1063
  });
1003
1064
  return replaceLast(result, withParts(lastMessage, parts));
@@ -1667,19 +1728,18 @@ var coreUserMessageToParts = (coreUserMessage) => typeof coreUserMessage.content
1667
1728
  return { type: "text", text: part.text };
1668
1729
  }
1669
1730
  case "image": {
1670
- const data = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
1731
+ const mimeType = part.mimeType ?? "image/*";
1671
1732
  return {
1672
1733
  type: "file",
1673
- mimeType: part.mimeType ?? "image/*",
1674
- data
1734
+ mimeType,
1735
+ data: encodeFilePartDataForStorage(part.image, mimeType)
1675
1736
  };
1676
1737
  }
1677
1738
  case "file": {
1678
- const data = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
1679
1739
  return {
1680
1740
  type: "file",
1681
1741
  mimeType: part.mimeType,
1682
- data,
1742
+ data: encodeFilePartDataForStorage(part.data, part.mimeType),
1683
1743
  ...part.filename !== void 0 ? { filename: part.filename } : {}
1684
1744
  };
1685
1745
  }
@@ -1807,19 +1867,6 @@ var extractRunIdFromMessages = (messages) => {
1807
1867
  return void 0;
1808
1868
  };
1809
1869
 
1810
- // src/agent/signal-data.ts
1811
- function convertSignalDataToBase64String(content) {
1812
- if (typeof content === "string") {
1813
- return content;
1814
- }
1815
- const bytes = content instanceof ArrayBuffer ? new Uint8Array(content) : content;
1816
- let binary = "";
1817
- for (const byte of bytes) {
1818
- binary += String.fromCharCode(byte);
1819
- }
1820
- return btoa(binary);
1821
- }
1822
-
1823
1870
  // src/agent/hooks.ts
1824
1871
  var extractPendingToolApprovalIdsFromMessages = (messages) => {
1825
1872
  const pendingToolApprovalIds = /* @__PURE__ */ new Set();