@mastra/ai-sdk 1.0.0-beta.1 → 1.0.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/CHANGELOG.md +110 -22
- package/dist/chat-route.d.ts +52 -2
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/helpers.d.ts +2 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +88 -47
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +89 -47
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts.map +1 -1
- package/dist/to-ai-sdk-format.d.ts +61 -11
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +7 -1
- package/dist/transformers.d.ts.map +1 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/workflow-route.d.ts.map +1 -1
- package/package.json +11 -17
- package/dist/convert-messages.d.ts +0 -10
- package/dist/convert-messages.d.ts.map +0 -1
- package/dist/convert-streams.d.ts +0 -18
- package/dist/convert-streams.d.ts.map +0 -1
- package/dist/ui.cjs +0 -16
- package/dist/ui.cjs.map +0 -1
- package/dist/ui.d.ts +0 -2
- package/dist/ui.d.ts.map +0 -1
- package/dist/ui.js +0 -13
- package/dist/ui.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -8,20 +8,6 @@ import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core
|
|
|
8
8
|
var isDataChunkType = (chunk) => {
|
|
9
9
|
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
10
10
|
};
|
|
11
|
-
function safeParseErrorObject(obj) {
|
|
12
|
-
if (typeof obj !== "object" || obj === null) {
|
|
13
|
-
return String(obj);
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const stringified = JSON.stringify(obj);
|
|
17
|
-
if (stringified === "{}") {
|
|
18
|
-
return String(obj);
|
|
19
|
-
}
|
|
20
|
-
return stringified;
|
|
21
|
-
} catch {
|
|
22
|
-
return String(obj);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
11
|
var isAgentExecutionDataChunkType = (chunk) => {
|
|
26
12
|
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
27
13
|
};
|
|
@@ -283,6 +269,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
283
269
|
};
|
|
284
270
|
}
|
|
285
271
|
case "reasoning-delta": {
|
|
272
|
+
if (sendReasoning) {
|
|
273
|
+
return {
|
|
274
|
+
type: "reasoning-delta",
|
|
275
|
+
id: part.id,
|
|
276
|
+
delta: part.text,
|
|
277
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
286
280
|
return;
|
|
287
281
|
}
|
|
288
282
|
case "reasoning-end": {
|
|
@@ -300,6 +294,25 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
300
294
|
};
|
|
301
295
|
}
|
|
302
296
|
case "source": {
|
|
297
|
+
if (sendSources && part.sourceType === "url") {
|
|
298
|
+
return {
|
|
299
|
+
type: "source-url",
|
|
300
|
+
sourceId: part.id,
|
|
301
|
+
url: part.url,
|
|
302
|
+
title: part.title,
|
|
303
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
if (sendSources && part.sourceType === "document") {
|
|
307
|
+
return {
|
|
308
|
+
type: "source-document",
|
|
309
|
+
sourceId: part.id,
|
|
310
|
+
mediaType: part.mediaType,
|
|
311
|
+
title: part.title,
|
|
312
|
+
filename: part.filename,
|
|
313
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
303
316
|
return;
|
|
304
317
|
}
|
|
305
318
|
case "tool-input-start": {
|
|
@@ -390,21 +403,23 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
390
403
|
return { type: "finish-step" };
|
|
391
404
|
}
|
|
392
405
|
case "start": {
|
|
393
|
-
{
|
|
406
|
+
if (sendStart) {
|
|
394
407
|
return {
|
|
395
408
|
type: "start",
|
|
396
409
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
397
410
|
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
398
411
|
};
|
|
399
412
|
}
|
|
413
|
+
return;
|
|
400
414
|
}
|
|
401
415
|
case "finish": {
|
|
402
|
-
{
|
|
416
|
+
if (sendFinish) {
|
|
403
417
|
return {
|
|
404
418
|
type: "finish",
|
|
405
419
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
406
420
|
};
|
|
407
421
|
}
|
|
422
|
+
return;
|
|
408
423
|
}
|
|
409
424
|
case "abort": {
|
|
410
425
|
return part;
|
|
@@ -469,20 +484,26 @@ function AgentNetworkToAISDKTransformer() {
|
|
|
469
484
|
}
|
|
470
485
|
});
|
|
471
486
|
}
|
|
472
|
-
function AgentStreamToAISDKTransformer(
|
|
487
|
+
function AgentStreamToAISDKTransformer({
|
|
488
|
+
lastMessageId,
|
|
489
|
+
sendStart,
|
|
490
|
+
sendFinish,
|
|
491
|
+
sendReasoning,
|
|
492
|
+
sendSources
|
|
493
|
+
}) {
|
|
473
494
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
474
495
|
return new TransformStream({
|
|
475
496
|
transform(chunk, controller) {
|
|
476
497
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
477
498
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
478
499
|
part,
|
|
479
|
-
sendReasoning
|
|
480
|
-
sendSources
|
|
481
|
-
sendStart
|
|
482
|
-
sendFinish
|
|
500
|
+
sendReasoning,
|
|
501
|
+
sendSources,
|
|
502
|
+
sendStart,
|
|
503
|
+
sendFinish,
|
|
483
504
|
responseMessageId: lastMessageId,
|
|
484
|
-
onError(
|
|
485
|
-
return
|
|
505
|
+
onError() {
|
|
506
|
+
return "Error";
|
|
486
507
|
}
|
|
487
508
|
});
|
|
488
509
|
if (transformedChunk) {
|
|
@@ -987,8 +1008,12 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
987
1008
|
}
|
|
988
1009
|
}
|
|
989
1010
|
|
|
990
|
-
// src/
|
|
991
|
-
function
|
|
1011
|
+
// src/to-ai-sdk-format.ts
|
|
1012
|
+
function toAISdkFormat(stream, options = {
|
|
1013
|
+
from: "agent",
|
|
1014
|
+
sendStart: true,
|
|
1015
|
+
sendFinish: true
|
|
1016
|
+
}) {
|
|
992
1017
|
const from = options?.from;
|
|
993
1018
|
if (from === "workflow") {
|
|
994
1019
|
return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
|
|
@@ -997,14 +1022,26 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
|
|
|
997
1022
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
998
1023
|
}
|
|
999
1024
|
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
1000
|
-
return agentReadable.pipeThrough(
|
|
1025
|
+
return agentReadable.pipeThrough(
|
|
1026
|
+
AgentStreamToAISDKTransformer({
|
|
1027
|
+
lastMessageId: options?.lastMessageId,
|
|
1028
|
+
sendStart: options?.sendStart,
|
|
1029
|
+
sendFinish: options?.sendFinish,
|
|
1030
|
+
sendReasoning: options?.sendReasoning,
|
|
1031
|
+
sendSources: options?.sendSources
|
|
1032
|
+
})
|
|
1033
|
+
);
|
|
1001
1034
|
}
|
|
1002
1035
|
|
|
1003
1036
|
// src/chat-route.ts
|
|
1004
1037
|
function chatRoute({
|
|
1005
1038
|
path = "/chat/:agentId",
|
|
1006
1039
|
agent,
|
|
1007
|
-
defaultOptions
|
|
1040
|
+
defaultOptions,
|
|
1041
|
+
sendStart = true,
|
|
1042
|
+
sendFinish = true,
|
|
1043
|
+
sendReasoning = false,
|
|
1044
|
+
sendSources = false
|
|
1008
1045
|
}) {
|
|
1009
1046
|
if (!agent && !path.includes("/:agentId")) {
|
|
1010
1047
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -1105,7 +1142,7 @@ function chatRoute({
|
|
|
1105
1142
|
handler: async (c) => {
|
|
1106
1143
|
const { messages, ...rest } = await c.req.json();
|
|
1107
1144
|
const mastra = c.get("mastra");
|
|
1108
|
-
const
|
|
1145
|
+
const runtimeContext = c.get("runtimeContext");
|
|
1109
1146
|
let agentToUse = agent;
|
|
1110
1147
|
if (!agent) {
|
|
1111
1148
|
const agentId = c.req.param("agentId");
|
|
@@ -1116,8 +1153,8 @@ function chatRoute({
|
|
|
1116
1153
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1117
1154
|
);
|
|
1118
1155
|
}
|
|
1119
|
-
if (
|
|
1120
|
-
mastra.getLogger()?.warn(`"
|
|
1156
|
+
if (runtimeContext && defaultOptions?.runtimeContext) {
|
|
1157
|
+
mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
|
|
1121
1158
|
}
|
|
1122
1159
|
if (!agentToUse) {
|
|
1123
1160
|
throw new Error("Agent ID is required");
|
|
@@ -1129,7 +1166,7 @@ function chatRoute({
|
|
|
1129
1166
|
const result = await agentObj.stream(messages, {
|
|
1130
1167
|
...defaultOptions,
|
|
1131
1168
|
...rest,
|
|
1132
|
-
|
|
1169
|
+
runtimeContext: runtimeContext || defaultOptions?.runtimeContext
|
|
1133
1170
|
});
|
|
1134
1171
|
let lastMessageId;
|
|
1135
1172
|
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
@@ -1138,7 +1175,14 @@ function chatRoute({
|
|
|
1138
1175
|
const uiMessageStream = createUIMessageStream({
|
|
1139
1176
|
originalMessages: messages,
|
|
1140
1177
|
execute: async ({ writer }) => {
|
|
1141
|
-
for await (const part of
|
|
1178
|
+
for await (const part of toAISdkFormat(result, {
|
|
1179
|
+
from: "agent",
|
|
1180
|
+
lastMessageId,
|
|
1181
|
+
sendStart,
|
|
1182
|
+
sendFinish,
|
|
1183
|
+
sendReasoning,
|
|
1184
|
+
sendSources
|
|
1185
|
+
})) {
|
|
1142
1186
|
writer.write(part);
|
|
1143
1187
|
}
|
|
1144
1188
|
}
|
|
@@ -1178,9 +1222,13 @@ function workflowRoute({
|
|
|
1178
1222
|
schema: {
|
|
1179
1223
|
type: "object",
|
|
1180
1224
|
properties: {
|
|
1225
|
+
runId: { type: "string" },
|
|
1226
|
+
resourceId: { type: "string" },
|
|
1181
1227
|
inputData: { type: "object", additionalProperties: true },
|
|
1182
|
-
|
|
1183
|
-
|
|
1228
|
+
resumeData: { type: "object", additionalProperties: true },
|
|
1229
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1230
|
+
tracingOptions: { type: "object", additionalProperties: true },
|
|
1231
|
+
step: { type: "string" }
|
|
1184
1232
|
}
|
|
1185
1233
|
}
|
|
1186
1234
|
}
|
|
@@ -1198,7 +1246,7 @@ function workflowRoute({
|
|
|
1198
1246
|
}
|
|
1199
1247
|
},
|
|
1200
1248
|
handler: async (c) => {
|
|
1201
|
-
const { inputData, resumeData, ...rest } = await c.req.json();
|
|
1249
|
+
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1202
1250
|
const mastra = c.get("mastra");
|
|
1203
1251
|
let workflowToUse = workflow;
|
|
1204
1252
|
if (!workflow) {
|
|
@@ -1217,11 +1265,11 @@ function workflowRoute({
|
|
|
1217
1265
|
if (!workflowObj) {
|
|
1218
1266
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1219
1267
|
}
|
|
1220
|
-
const run = await workflowObj.
|
|
1268
|
+
const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
|
|
1221
1269
|
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1222
1270
|
const uiMessageStream = createUIMessageStream({
|
|
1223
1271
|
execute: async ({ writer }) => {
|
|
1224
|
-
for await (const part of
|
|
1272
|
+
for await (const part of toAISdkFormat(stream, { from: "workflow" })) {
|
|
1225
1273
|
writer.write(part);
|
|
1226
1274
|
}
|
|
1227
1275
|
}
|
|
@@ -1261,12 +1309,13 @@ function networkRoute({
|
|
|
1261
1309
|
type: "object",
|
|
1262
1310
|
properties: {
|
|
1263
1311
|
messages: { type: "array", items: { type: "object" } },
|
|
1264
|
-
|
|
1312
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1265
1313
|
runId: { type: "string" },
|
|
1266
1314
|
maxSteps: { type: "number" },
|
|
1267
1315
|
threadId: { type: "string" },
|
|
1268
1316
|
resourceId: { type: "string" },
|
|
1269
1317
|
modelSettings: { type: "object", additionalProperties: true },
|
|
1318
|
+
telemetry: { type: "object", additionalProperties: true },
|
|
1270
1319
|
tools: { type: "array", items: { type: "object" } }
|
|
1271
1320
|
},
|
|
1272
1321
|
required: ["messages"]
|
|
@@ -1315,7 +1364,7 @@ function networkRoute({
|
|
|
1315
1364
|
});
|
|
1316
1365
|
const uiMessageStream = createUIMessageStream({
|
|
1317
1366
|
execute: async ({ writer }) => {
|
|
1318
|
-
for await (const part of
|
|
1367
|
+
for await (const part of toAISdkFormat(result, { from: "network" })) {
|
|
1319
1368
|
writer.write(part);
|
|
1320
1369
|
}
|
|
1321
1370
|
}
|
|
@@ -1325,13 +1374,6 @@ function networkRoute({
|
|
|
1325
1374
|
});
|
|
1326
1375
|
}
|
|
1327
1376
|
|
|
1328
|
-
|
|
1329
|
-
function toAISdkFormat() {
|
|
1330
|
-
throw new Error(
|
|
1331
|
-
'toAISdkFormat() has been deprecated. Please use toAISdkStream() instead.\n\nMigration:\n import { toAISdkFormat } from "@mastra/ai-sdk";\n // Change to:\n import { toAISdkStream } from "@mastra/ai-sdk";\n\nThe function signature remains the same.'
|
|
1332
|
-
);
|
|
1333
|
-
}
|
|
1334
|
-
|
|
1335
|
-
export { chatRoute, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
|
|
1377
|
+
export { chatRoute, networkRoute, toAISdkFormat, workflowRoute };
|
|
1336
1378
|
//# sourceMappingURL=index.js.map
|
|
1337
1379
|
//# sourceMappingURL=index.js.map
|