@mastra/ai-sdk 0.0.0-vnext-20251104230439 → 0.0.0-vnext-20251119160359

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
@@ -22,6 +22,12 @@ function safeParseErrorObject(obj) {
22
22
  return String(obj);
23
23
  }
24
24
  }
25
+ var isAgentExecutionDataChunkType = (chunk) => {
26
+ 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
+ };
28
+ var isWorkflowExecutionDataChunkType = (chunk) => {
29
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
30
+ };
25
31
 
26
32
  // src/helpers.ts
27
33
  function convertMastraChunkToAISDKv5({
@@ -277,6 +283,14 @@ function convertFullStreamChunkToUIMessageStream({
277
283
  };
278
284
  }
279
285
  case "reasoning-delta": {
286
+ if (sendReasoning) {
287
+ return {
288
+ type: "reasoning-delta",
289
+ id: part.id,
290
+ delta: part.text,
291
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
292
+ };
293
+ }
280
294
  return;
281
295
  }
282
296
  case "reasoning-end": {
@@ -294,6 +308,25 @@ function convertFullStreamChunkToUIMessageStream({
294
308
  };
295
309
  }
296
310
  case "source": {
311
+ if (sendSources && part.sourceType === "url") {
312
+ return {
313
+ type: "source-url",
314
+ sourceId: part.id,
315
+ url: part.url,
316
+ title: part.title,
317
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
318
+ };
319
+ }
320
+ if (sendSources && part.sourceType === "document") {
321
+ return {
322
+ type: "source-document",
323
+ sourceId: part.id,
324
+ mediaType: part.mediaType,
325
+ title: part.title,
326
+ filename: part.filename,
327
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
328
+ };
329
+ }
297
330
  return;
298
331
  }
299
332
  case "tool-input-start": {
@@ -351,6 +384,14 @@ function convertFullStreamChunkToUIMessageStream({
351
384
  toolCallId: part.toolCallId,
352
385
  payload: part.output
353
386
  };
387
+ } else if (isDataChunkType(part.output)) {
388
+ if (!("data" in part.output)) {
389
+ throw new Error(
390
+ `UI Messages require a data property when using data- prefixed chunks
391
+ ${JSON.stringify(part)}`
392
+ );
393
+ }
394
+ return part.output;
354
395
  }
355
396
  return;
356
397
  }
@@ -376,21 +417,23 @@ function convertFullStreamChunkToUIMessageStream({
376
417
  return { type: "finish-step" };
377
418
  }
378
419
  case "start": {
379
- {
420
+ if (sendStart) {
380
421
  return {
381
422
  type: "start",
382
423
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
383
424
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
384
425
  };
385
426
  }
427
+ return;
386
428
  }
387
429
  case "finish": {
388
- {
430
+ if (sendFinish) {
389
431
  return {
390
432
  type: "finish",
391
433
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
392
434
  };
393
435
  }
436
+ return;
394
437
  }
395
438
  case "abort": {
396
439
  return part;
@@ -455,17 +498,23 @@ function AgentNetworkToAISDKTransformer() {
455
498
  }
456
499
  });
457
500
  }
458
- function AgentStreamToAISDKTransformer(lastMessageId) {
501
+ function AgentStreamToAISDKTransformer({
502
+ lastMessageId,
503
+ sendStart,
504
+ sendFinish,
505
+ sendReasoning,
506
+ sendSources
507
+ }) {
459
508
  let bufferedSteps = /* @__PURE__ */ new Map();
460
509
  return new TransformStream({
461
510
  transform(chunk, controller) {
462
511
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
463
512
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
464
513
  part,
465
- sendReasoning: false,
466
- sendSources: false,
467
- sendStart: true,
468
- sendFinish: true,
514
+ sendReasoning,
515
+ sendSources,
516
+ sendStart,
517
+ sendFinish,
469
518
  responseMessageId: lastMessageId,
470
519
  onError(error) {
471
520
  return safeParseErrorObject(error);
@@ -950,13 +999,35 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
950
999
  }
951
1000
  return payload;
952
1001
  }
1002
+ if (isAgentExecutionDataChunkType(payload)) {
1003
+ if (!("data" in payload.payload)) {
1004
+ throw new Error(
1005
+ `UI Messages require a data property when using data- prefixed chunks
1006
+ ${JSON.stringify(payload)}`
1007
+ );
1008
+ }
1009
+ return payload.payload;
1010
+ }
1011
+ if (isWorkflowExecutionDataChunkType(payload)) {
1012
+ if (!("data" in payload.payload)) {
1013
+ throw new Error(
1014
+ `UI Messages require a data property when using data- prefixed chunks
1015
+ ${JSON.stringify(payload)}`
1016
+ );
1017
+ }
1018
+ return payload.payload;
1019
+ }
953
1020
  return null;
954
1021
  }
955
1022
  }
956
1023
  }
957
1024
 
958
1025
  // src/convert-streams.ts
959
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1026
+ function toAISdkV5Stream(stream, options = {
1027
+ from: "agent",
1028
+ sendStart: true,
1029
+ sendFinish: true
1030
+ }) {
960
1031
  const from = options?.from;
961
1032
  if (from === "workflow") {
962
1033
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -965,14 +1036,26 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
965
1036
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
966
1037
  }
967
1038
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
968
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1039
+ return agentReadable.pipeThrough(
1040
+ AgentStreamToAISDKTransformer({
1041
+ lastMessageId: options?.lastMessageId,
1042
+ sendStart: options?.sendStart,
1043
+ sendFinish: options?.sendFinish,
1044
+ sendReasoning: options?.sendReasoning,
1045
+ sendSources: options?.sendSources
1046
+ })
1047
+ );
969
1048
  }
970
1049
 
971
1050
  // src/chat-route.ts
972
1051
  function chatRoute({
973
1052
  path = "/chat/:agentId",
974
1053
  agent,
975
- defaultOptions
1054
+ defaultOptions,
1055
+ sendStart = true,
1056
+ sendFinish = true,
1057
+ sendReasoning = false,
1058
+ sendSources = false
976
1059
  }) {
977
1060
  if (!agent && !path.includes("/:agentId")) {
978
1061
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1106,7 +1189,14 @@ function chatRoute({
1106
1189
  const uiMessageStream = createUIMessageStream({
1107
1190
  originalMessages: messages,
1108
1191
  execute: async ({ writer }) => {
1109
- for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
1192
+ for await (const part of toAISdkV5Stream(result, {
1193
+ from: "agent",
1194
+ lastMessageId,
1195
+ sendStart,
1196
+ sendFinish,
1197
+ sendReasoning,
1198
+ sendSources
1199
+ })) {
1110
1200
  writer.write(part);
1111
1201
  }
1112
1202
  }
@@ -1146,9 +1236,13 @@ function workflowRoute({
1146
1236
  schema: {
1147
1237
  type: "object",
1148
1238
  properties: {
1239
+ runId: { type: "string" },
1240
+ resourceId: { type: "string" },
1149
1241
  inputData: { type: "object", additionalProperties: true },
1242
+ resumeData: { type: "object", additionalProperties: true },
1150
1243
  requestContext: { type: "object", additionalProperties: true },
1151
- tracingOptions: { type: "object", additionalProperties: true }
1244
+ tracingOptions: { type: "object", additionalProperties: true },
1245
+ step: { type: "string" }
1152
1246
  }
1153
1247
  }
1154
1248
  }
@@ -1166,7 +1260,7 @@ function workflowRoute({
1166
1260
  }
1167
1261
  },
1168
1262
  handler: async (c) => {
1169
- const { inputData, resumeData, ...rest } = await c.req.json();
1263
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1170
1264
  const mastra = c.get("mastra");
1171
1265
  let workflowToUse = workflow;
1172
1266
  if (!workflow) {
@@ -1185,7 +1279,7 @@ function workflowRoute({
1185
1279
  if (!workflowObj) {
1186
1280
  throw new Error(`Workflow ${workflowToUse} not found`);
1187
1281
  }
1188
- const run = await workflowObj.createRun();
1282
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1189
1283
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1190
1284
  const uiMessageStream = createUIMessageStream({
1191
1285
  execute: async ({ writer }) => {