@mastra/react 0.0.0-safe-stringify-telemetry-20251205024938 → 0.0.0-scorers-logs-20251208101616
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/CHANGELOG.md +63 -141
- package/dist/index.cjs +102 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +102 -29
- package/dist/index.js.map +1 -1
- package/dist/react.css +1 -1
- package/dist/src/agent/hooks.d.ts +4 -2
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.d.ts +10 -0
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.test.d.ts +1 -0
- package/package.json +10 -14
package/dist/index.js
CHANGED
|
@@ -438,11 +438,10 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
438
438
|
const lastMessage = result[result.length - 1];
|
|
439
439
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
440
440
|
const parts = lastMessage.parts.map((part) => {
|
|
441
|
-
if (part
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
return { ...part, state: "done" };
|
|
441
|
+
if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
|
|
442
|
+
if (part.type === "text" || part.type === "reasoning") {
|
|
443
|
+
return { ...part, state: "done" };
|
|
444
|
+
}
|
|
446
445
|
}
|
|
447
446
|
return part;
|
|
448
447
|
});
|
|
@@ -614,13 +613,23 @@ const toAssistantUIMessage = (message) => {
|
|
|
614
613
|
};
|
|
615
614
|
}
|
|
616
615
|
if (part.type === "file") {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
616
|
+
const type = part.mediaType.includes("image/") ? "image" : "file";
|
|
617
|
+
if (type === "file") {
|
|
618
|
+
return {
|
|
619
|
+
type,
|
|
620
|
+
mimeType: part.mediaType,
|
|
621
|
+
data: part.url,
|
|
622
|
+
// Use URL as data source
|
|
623
|
+
metadata: message.metadata
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
if (type === "image") {
|
|
627
|
+
return {
|
|
628
|
+
type,
|
|
629
|
+
image: part.url,
|
|
630
|
+
metadata: message.metadata
|
|
631
|
+
};
|
|
632
|
+
}
|
|
624
633
|
}
|
|
625
634
|
if (part.type === "dynamic-tool") {
|
|
626
635
|
const baseToolCall = {
|
|
@@ -711,7 +720,9 @@ const toAssistantUIMessage = (message) => {
|
|
|
711
720
|
|
|
712
721
|
const resolveInitialMessages = (messages) => {
|
|
713
722
|
return messages.map((message) => {
|
|
714
|
-
const networkPart = message.parts.find(
|
|
723
|
+
const networkPart = message.parts.find(
|
|
724
|
+
(part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
|
|
725
|
+
);
|
|
715
726
|
if (networkPart && networkPart.type === "text") {
|
|
716
727
|
try {
|
|
717
728
|
const json = JSON.parse(networkPart.text);
|
|
@@ -1196,6 +1207,51 @@ class AISdkNetworkTransformer {
|
|
|
1196
1207
|
};
|
|
1197
1208
|
}
|
|
1198
1209
|
|
|
1210
|
+
const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
|
|
1211
|
+
const id = `user-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
1212
|
+
const parts = typeof coreUserMessage.content === "string" ? [
|
|
1213
|
+
{
|
|
1214
|
+
type: "text",
|
|
1215
|
+
text: coreUserMessage.content
|
|
1216
|
+
}
|
|
1217
|
+
] : coreUserMessage.content.map((part) => {
|
|
1218
|
+
switch (part.type) {
|
|
1219
|
+
case "text": {
|
|
1220
|
+
return {
|
|
1221
|
+
type: "text",
|
|
1222
|
+
text: part.text
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
case "image": {
|
|
1226
|
+
const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
|
|
1227
|
+
return {
|
|
1228
|
+
type: "file",
|
|
1229
|
+
mediaType: part.mimeType ?? "image/*",
|
|
1230
|
+
url
|
|
1231
|
+
};
|
|
1232
|
+
}
|
|
1233
|
+
case "file": {
|
|
1234
|
+
const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
|
|
1235
|
+
return {
|
|
1236
|
+
type: "file",
|
|
1237
|
+
mediaType: part.mimeType,
|
|
1238
|
+
url,
|
|
1239
|
+
...part.filename !== void 0 ? { filename: part.filename } : {}
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
default: {
|
|
1243
|
+
const exhaustiveCheck = part;
|
|
1244
|
+
throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
});
|
|
1248
|
+
return {
|
|
1249
|
+
id,
|
|
1250
|
+
role: "user",
|
|
1251
|
+
parts
|
|
1252
|
+
};
|
|
1253
|
+
};
|
|
1254
|
+
|
|
1199
1255
|
const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
1200
1256
|
const extractRunIdFromMessages = (messages2) => {
|
|
1201
1257
|
for (const message of messages2) {
|
|
@@ -1219,11 +1275,12 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1219
1275
|
const [isRunning, setIsRunning] = useState(false);
|
|
1220
1276
|
const generate = async ({
|
|
1221
1277
|
coreUserMessages,
|
|
1222
|
-
|
|
1278
|
+
requestContext,
|
|
1223
1279
|
threadId,
|
|
1224
1280
|
modelSettings,
|
|
1225
1281
|
signal,
|
|
1226
|
-
onFinish
|
|
1282
|
+
onFinish,
|
|
1283
|
+
tracingOptions
|
|
1227
1284
|
}) => {
|
|
1228
1285
|
const {
|
|
1229
1286
|
frequencyPenalty,
|
|
@@ -1257,9 +1314,10 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1257
1314
|
topP
|
|
1258
1315
|
},
|
|
1259
1316
|
instructions,
|
|
1260
|
-
|
|
1317
|
+
requestContext,
|
|
1261
1318
|
...threadId ? { threadId, resourceId: resourceId || agentId } : {},
|
|
1262
|
-
providerOptions
|
|
1319
|
+
providerOptions,
|
|
1320
|
+
tracingOptions
|
|
1263
1321
|
});
|
|
1264
1322
|
setIsRunning(false);
|
|
1265
1323
|
if (response && "uiMessages" in response.response && response.response.uiMessages) {
|
|
@@ -1273,7 +1331,15 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1273
1331
|
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1274
1332
|
}
|
|
1275
1333
|
};
|
|
1276
|
-
const stream = async ({
|
|
1334
|
+
const stream = async ({
|
|
1335
|
+
coreUserMessages,
|
|
1336
|
+
requestContext,
|
|
1337
|
+
threadId,
|
|
1338
|
+
onChunk,
|
|
1339
|
+
modelSettings,
|
|
1340
|
+
signal,
|
|
1341
|
+
tracingOptions
|
|
1342
|
+
}) => {
|
|
1277
1343
|
const {
|
|
1278
1344
|
frequencyPenalty,
|
|
1279
1345
|
presencePenalty,
|
|
@@ -1308,10 +1374,11 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1308
1374
|
topP
|
|
1309
1375
|
},
|
|
1310
1376
|
instructions,
|
|
1311
|
-
|
|
1377
|
+
requestContext,
|
|
1312
1378
|
...threadId ? { threadId, resourceId: resourceId || agentId } : {},
|
|
1313
1379
|
providerOptions,
|
|
1314
|
-
requireToolApproval
|
|
1380
|
+
requireToolApproval,
|
|
1381
|
+
tracingOptions
|
|
1315
1382
|
});
|
|
1316
1383
|
_onChunk.current = onChunk;
|
|
1317
1384
|
_currentRunId.current = runId;
|
|
@@ -1325,11 +1392,12 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1325
1392
|
};
|
|
1326
1393
|
const network = async ({
|
|
1327
1394
|
coreUserMessages,
|
|
1328
|
-
|
|
1395
|
+
requestContext,
|
|
1329
1396
|
threadId,
|
|
1330
1397
|
onNetworkChunk,
|
|
1331
1398
|
modelSettings,
|
|
1332
|
-
signal
|
|
1399
|
+
signal,
|
|
1400
|
+
tracingOptions
|
|
1333
1401
|
}) => {
|
|
1334
1402
|
const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
|
|
1335
1403
|
setIsRunning(true);
|
|
@@ -1352,8 +1420,9 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1352
1420
|
topP
|
|
1353
1421
|
},
|
|
1354
1422
|
runId,
|
|
1355
|
-
|
|
1356
|
-
...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {}
|
|
1423
|
+
requestContext,
|
|
1424
|
+
...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {},
|
|
1425
|
+
tracingOptions
|
|
1357
1426
|
});
|
|
1358
1427
|
const transformer = new AISdkNetworkTransformer();
|
|
1359
1428
|
await response.processDataStream({
|
|
@@ -1405,14 +1474,18 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
1405
1474
|
};
|
|
1406
1475
|
const sendMessage = async ({ mode = "stream", ...args }) => {
|
|
1407
1476
|
const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
|
|
1408
|
-
const
|
|
1409
|
-
|
|
1477
|
+
const coreUserMessages = [nextMessage];
|
|
1478
|
+
if (args.coreUserMessages) {
|
|
1479
|
+
coreUserMessages.push(...args.coreUserMessages);
|
|
1480
|
+
}
|
|
1481
|
+
const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
|
|
1482
|
+
setMessages((s) => [...s, ...uiMessages]);
|
|
1410
1483
|
if (mode === "generate") {
|
|
1411
|
-
await generate({ ...args, coreUserMessages
|
|
1484
|
+
await generate({ ...args, coreUserMessages });
|
|
1412
1485
|
} else if (mode === "stream") {
|
|
1413
|
-
await stream({ ...args, coreUserMessages
|
|
1486
|
+
await stream({ ...args, coreUserMessages });
|
|
1414
1487
|
} else if (mode === "network") {
|
|
1415
|
-
await network({ ...args, coreUserMessages
|
|
1488
|
+
await network({ ...args, coreUserMessages });
|
|
1416
1489
|
}
|
|
1417
1490
|
};
|
|
1418
1491
|
return {
|