@mastra/ai-sdk 0.2.7-alpha.0 → 1.0.0-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/CHANGELOG.md +42 -0
- package/dist/chat-route.d.ts +52 -2
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/index.cjs +71 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -13
- package/dist/index.js.map +1 -1
- package/dist/to-ai-sdk-format.d.ts +48 -0
- 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/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -269,6 +269,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
269
269
|
};
|
|
270
270
|
}
|
|
271
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
|
+
}
|
|
272
280
|
return;
|
|
273
281
|
}
|
|
274
282
|
case "reasoning-end": {
|
|
@@ -286,6 +294,25 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
286
294
|
};
|
|
287
295
|
}
|
|
288
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
|
+
}
|
|
289
316
|
return;
|
|
290
317
|
}
|
|
291
318
|
case "tool-input-start": {
|
|
@@ -376,21 +403,23 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
376
403
|
return { type: "finish-step" };
|
|
377
404
|
}
|
|
378
405
|
case "start": {
|
|
379
|
-
{
|
|
406
|
+
if (sendStart) {
|
|
380
407
|
return {
|
|
381
408
|
type: "start",
|
|
382
409
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
383
410
|
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
384
411
|
};
|
|
385
412
|
}
|
|
413
|
+
return;
|
|
386
414
|
}
|
|
387
415
|
case "finish": {
|
|
388
|
-
{
|
|
416
|
+
if (sendFinish) {
|
|
389
417
|
return {
|
|
390
418
|
type: "finish",
|
|
391
419
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
392
420
|
};
|
|
393
421
|
}
|
|
422
|
+
return;
|
|
394
423
|
}
|
|
395
424
|
case "abort": {
|
|
396
425
|
return part;
|
|
@@ -455,17 +484,23 @@ function AgentNetworkToAISDKTransformer() {
|
|
|
455
484
|
}
|
|
456
485
|
});
|
|
457
486
|
}
|
|
458
|
-
function AgentStreamToAISDKTransformer(
|
|
487
|
+
function AgentStreamToAISDKTransformer({
|
|
488
|
+
lastMessageId,
|
|
489
|
+
sendStart,
|
|
490
|
+
sendFinish,
|
|
491
|
+
sendReasoning,
|
|
492
|
+
sendSources
|
|
493
|
+
}) {
|
|
459
494
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
460
495
|
return new TransformStream({
|
|
461
496
|
transform(chunk, controller) {
|
|
462
497
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
463
498
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
464
499
|
part,
|
|
465
|
-
sendReasoning
|
|
466
|
-
sendSources
|
|
467
|
-
sendStart
|
|
468
|
-
sendFinish
|
|
500
|
+
sendReasoning,
|
|
501
|
+
sendSources,
|
|
502
|
+
sendStart,
|
|
503
|
+
sendFinish,
|
|
469
504
|
responseMessageId: lastMessageId,
|
|
470
505
|
onError() {
|
|
471
506
|
return "Error";
|
|
@@ -974,7 +1009,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
974
1009
|
}
|
|
975
1010
|
|
|
976
1011
|
// src/to-ai-sdk-format.ts
|
|
977
|
-
function toAISdkFormat(stream, options = {
|
|
1012
|
+
function toAISdkFormat(stream, options = {
|
|
1013
|
+
from: "agent",
|
|
1014
|
+
sendStart: true,
|
|
1015
|
+
sendFinish: true
|
|
1016
|
+
}) {
|
|
978
1017
|
const from = options?.from;
|
|
979
1018
|
if (from === "workflow") {
|
|
980
1019
|
return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
|
|
@@ -983,14 +1022,26 @@ function toAISdkFormat(stream, options = { from: "agent" }) {
|
|
|
983
1022
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
984
1023
|
}
|
|
985
1024
|
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
986
|
-
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
|
+
);
|
|
987
1034
|
}
|
|
988
1035
|
|
|
989
1036
|
// src/chat-route.ts
|
|
990
1037
|
function chatRoute({
|
|
991
1038
|
path = "/chat/:agentId",
|
|
992
1039
|
agent,
|
|
993
|
-
defaultOptions
|
|
1040
|
+
defaultOptions,
|
|
1041
|
+
sendStart = true,
|
|
1042
|
+
sendFinish = true,
|
|
1043
|
+
sendReasoning = false,
|
|
1044
|
+
sendSources = false
|
|
994
1045
|
}) {
|
|
995
1046
|
if (!agent && !path.includes("/:agentId")) {
|
|
996
1047
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -1124,7 +1175,14 @@ function chatRoute({
|
|
|
1124
1175
|
const uiMessageStream = createUIMessageStream({
|
|
1125
1176
|
originalMessages: messages,
|
|
1126
1177
|
execute: async ({ writer }) => {
|
|
1127
|
-
for await (const part of toAISdkFormat(result, {
|
|
1178
|
+
for await (const part of toAISdkFormat(result, {
|
|
1179
|
+
from: "agent",
|
|
1180
|
+
lastMessageId,
|
|
1181
|
+
sendStart,
|
|
1182
|
+
sendFinish,
|
|
1183
|
+
sendReasoning,
|
|
1184
|
+
sendSources
|
|
1185
|
+
})) {
|
|
1128
1186
|
writer.write(part);
|
|
1129
1187
|
}
|
|
1130
1188
|
}
|
|
@@ -1168,7 +1226,7 @@ function workflowRoute({
|
|
|
1168
1226
|
resourceId: { type: "string" },
|
|
1169
1227
|
inputData: { type: "object", additionalProperties: true },
|
|
1170
1228
|
resumeData: { type: "object", additionalProperties: true },
|
|
1171
|
-
|
|
1229
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1172
1230
|
tracingOptions: { type: "object", additionalProperties: true },
|
|
1173
1231
|
step: { type: "string" }
|
|
1174
1232
|
}
|
|
@@ -1207,7 +1265,7 @@ function workflowRoute({
|
|
|
1207
1265
|
if (!workflowObj) {
|
|
1208
1266
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1209
1267
|
}
|
|
1210
|
-
const run = await workflowObj.
|
|
1268
|
+
const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
|
|
1211
1269
|
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1212
1270
|
const uiMessageStream = createUIMessageStream({
|
|
1213
1271
|
execute: async ({ writer }) => {
|