@mastra/react 0.0.15 → 0.1.0-beta.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
@@ -424,11 +424,10 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
424
424
  const lastMessage = result[result.length - 1];
425
425
  if (!lastMessage || lastMessage.role !== "assistant") return result;
426
426
  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" };
427
+ if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
428
+ if (part.type === "text" || part.type === "reasoning") {
429
+ return { ...part, state: "done" };
430
+ }
432
431
  }
433
432
  return part;
434
433
  });
@@ -600,13 +599,23 @@ const toAssistantUIMessage = (message) => {
600
599
  };
601
600
  }
602
601
  if (part.type === "file") {
603
- return {
604
- type: "file",
605
- mimeType: part.mediaType,
606
- data: part.url,
607
- // Use URL as data source
608
- metadata: message.metadata
609
- };
602
+ const type = part.mediaType.includes("image/") ? "image" : "file";
603
+ if (type === "file") {
604
+ return {
605
+ type,
606
+ mimeType: part.mediaType,
607
+ data: part.url,
608
+ // Use URL as data source
609
+ metadata: message.metadata
610
+ };
611
+ }
612
+ if (type === "image") {
613
+ return {
614
+ type,
615
+ image: part.url,
616
+ metadata: message.metadata
617
+ };
618
+ }
610
619
  }
611
620
  if (part.type === "dynamic-tool") {
612
621
  const baseToolCall = {
@@ -683,7 +692,9 @@ const toAssistantUIMessage = (message) => {
683
692
 
684
693
  const resolveInitialMessages = (messages) => {
685
694
  return messages.map((message) => {
686
- const networkPart = message.parts.find((part) => part.type === "text" && part.text.includes('"isNetwork":true'));
695
+ const networkPart = message.parts.find(
696
+ (part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
697
+ );
687
698
  if (networkPart && networkPart.type === "text") {
688
699
  try {
689
700
  const json = JSON.parse(networkPart.text);
@@ -1157,6 +1168,51 @@ class AISdkNetworkTransformer {
1157
1168
  };
1158
1169
  }
1159
1170
 
1171
+ const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
1172
+ const id = `user-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
1173
+ const parts = typeof coreUserMessage.content === "string" ? [
1174
+ {
1175
+ type: "text",
1176
+ text: coreUserMessage.content
1177
+ }
1178
+ ] : coreUserMessage.content.map((part) => {
1179
+ switch (part.type) {
1180
+ case "text": {
1181
+ return {
1182
+ type: "text",
1183
+ text: part.text
1184
+ };
1185
+ }
1186
+ case "image": {
1187
+ const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
1188
+ return {
1189
+ type: "file",
1190
+ mediaType: part.mimeType ?? "image/*",
1191
+ url
1192
+ };
1193
+ }
1194
+ case "file": {
1195
+ const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
1196
+ return {
1197
+ type: "file",
1198
+ mediaType: part.mimeType,
1199
+ url,
1200
+ ...part.filename !== void 0 ? { filename: part.filename } : {}
1201
+ };
1202
+ }
1203
+ default: {
1204
+ const exhaustiveCheck = part;
1205
+ throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
1206
+ }
1207
+ }
1208
+ });
1209
+ return {
1210
+ id,
1211
+ role: "user",
1212
+ parts
1213
+ };
1214
+ };
1215
+
1160
1216
  const useChat = ({ agentId, initializeMessages }) => {
1161
1217
  const _currentRunId = useRef(void 0);
1162
1218
  const _onChunk = useRef(void 0);
@@ -1168,7 +1224,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1168
1224
  const [isRunning, setIsRunning] = useState(false);
1169
1225
  const generate = async ({
1170
1226
  coreUserMessages,
1171
- runtimeContext,
1227
+ requestContext,
1172
1228
  threadId,
1173
1229
  modelSettings,
1174
1230
  signal,
@@ -1206,7 +1262,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1206
1262
  topP
1207
1263
  },
1208
1264
  instructions,
1209
- runtimeContext,
1265
+ requestContext,
1210
1266
  ...threadId ? { threadId, resourceId: agentId } : {},
1211
1267
  providerOptions
1212
1268
  });
@@ -1222,7 +1278,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1222
1278
  setMessages((prev) => [...prev, ...mastraUIMessages]);
1223
1279
  }
1224
1280
  };
1225
- const stream = async ({ coreUserMessages, runtimeContext, threadId, onChunk, modelSettings, signal }) => {
1281
+ const stream = async ({ coreUserMessages, requestContext, threadId, onChunk, modelSettings, signal }) => {
1226
1282
  const {
1227
1283
  frequencyPenalty,
1228
1284
  presencePenalty,
@@ -1257,7 +1313,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1257
1313
  topP
1258
1314
  },
1259
1315
  instructions,
1260
- runtimeContext,
1316
+ requestContext,
1261
1317
  ...threadId ? { threadId, resourceId: agentId } : {},
1262
1318
  providerOptions,
1263
1319
  requireToolApproval
@@ -1274,7 +1330,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1274
1330
  };
1275
1331
  const network = async ({
1276
1332
  coreUserMessages,
1277
- runtimeContext,
1333
+ requestContext,
1278
1334
  threadId,
1279
1335
  onNetworkChunk,
1280
1336
  modelSettings,
@@ -1300,7 +1356,7 @@ const useChat = ({ agentId, initializeMessages }) => {
1300
1356
  topP
1301
1357
  },
1302
1358
  runId: agentId,
1303
- runtimeContext,
1359
+ requestContext,
1304
1360
  ...threadId ? { thread: threadId, resourceId: agentId } : {}
1305
1361
  });
1306
1362
  const transformer = new AISdkNetworkTransformer();
@@ -1353,14 +1409,18 @@ const useChat = ({ agentId, initializeMessages }) => {
1353
1409
  };
1354
1410
  const sendMessage = async ({ mode = "stream", ...args }) => {
1355
1411
  const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
1356
- const messages2 = args.coreUserMessages ? [nextMessage, ...args.coreUserMessages] : [nextMessage];
1357
- setMessages((s) => [...s, { role: "user", parts: [{ type: "text", text: args.message }] }]);
1412
+ const coreUserMessages = [nextMessage];
1413
+ if (args.coreUserMessages) {
1414
+ coreUserMessages.push(...args.coreUserMessages);
1415
+ }
1416
+ const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
1417
+ setMessages((s) => [...s, ...uiMessages]);
1358
1418
  if (mode === "generate") {
1359
- await generate({ ...args, coreUserMessages: messages2 });
1419
+ await generate({ ...args, coreUserMessages });
1360
1420
  } else if (mode === "stream") {
1361
- await stream({ ...args, coreUserMessages: messages2 });
1421
+ await stream({ ...args, coreUserMessages });
1362
1422
  } else if (mode === "network") {
1363
- await network({ ...args, coreUserMessages: messages2 });
1423
+ await network({ ...args, coreUserMessages });
1364
1424
  }
1365
1425
  };
1366
1426
  return {