@mastra/ai-sdk 1.0.0-beta.0 → 1.0.0-beta.2

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
@@ -283,6 +283,14 @@ function convertFullStreamChunkToUIMessageStream({
283
283
  };
284
284
  }
285
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
+ }
286
294
  return;
287
295
  }
288
296
  case "reasoning-end": {
@@ -300,6 +308,25 @@ function convertFullStreamChunkToUIMessageStream({
300
308
  };
301
309
  }
302
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
+ }
303
330
  return;
304
331
  }
305
332
  case "tool-input-start": {
@@ -357,6 +384,14 @@ function convertFullStreamChunkToUIMessageStream({
357
384
  toolCallId: part.toolCallId,
358
385
  payload: part.output
359
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;
360
395
  }
361
396
  return;
362
397
  }
@@ -382,21 +417,23 @@ function convertFullStreamChunkToUIMessageStream({
382
417
  return { type: "finish-step" };
383
418
  }
384
419
  case "start": {
385
- {
420
+ if (sendStart) {
386
421
  return {
387
422
  type: "start",
388
423
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
389
424
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
390
425
  };
391
426
  }
427
+ return;
392
428
  }
393
429
  case "finish": {
394
- {
430
+ if (sendFinish) {
395
431
  return {
396
432
  type: "finish",
397
433
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
398
434
  };
399
435
  }
436
+ return;
400
437
  }
401
438
  case "abort": {
402
439
  return part;
@@ -461,17 +498,23 @@ function AgentNetworkToAISDKTransformer() {
461
498
  }
462
499
  });
463
500
  }
464
- function AgentStreamToAISDKTransformer(lastMessageId) {
501
+ function AgentStreamToAISDKTransformer({
502
+ lastMessageId,
503
+ sendStart,
504
+ sendFinish,
505
+ sendReasoning,
506
+ sendSources
507
+ }) {
465
508
  let bufferedSteps = /* @__PURE__ */ new Map();
466
509
  return new TransformStream({
467
510
  transform(chunk, controller) {
468
511
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
469
512
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
470
513
  part,
471
- sendReasoning: false,
472
- sendSources: false,
473
- sendStart: true,
474
- sendFinish: true,
514
+ sendReasoning,
515
+ sendSources,
516
+ sendStart,
517
+ sendFinish,
475
518
  responseMessageId: lastMessageId,
476
519
  onError(error) {
477
520
  return safeParseErrorObject(error);
@@ -980,7 +1023,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
980
1023
  }
981
1024
 
982
1025
  // src/convert-streams.ts
983
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1026
+ function toAISdkV5Stream(stream, options = {
1027
+ from: "agent",
1028
+ sendStart: true,
1029
+ sendFinish: true
1030
+ }) {
984
1031
  const from = options?.from;
985
1032
  if (from === "workflow") {
986
1033
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -989,14 +1036,26 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
989
1036
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
990
1037
  }
991
1038
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
992
- 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
+ );
993
1048
  }
994
1049
 
995
1050
  // src/chat-route.ts
996
1051
  function chatRoute({
997
1052
  path = "/chat/:agentId",
998
1053
  agent,
999
- defaultOptions
1054
+ defaultOptions,
1055
+ sendStart = true,
1056
+ sendFinish = true,
1057
+ sendReasoning = false,
1058
+ sendSources = false
1000
1059
  }) {
1001
1060
  if (!agent && !path.includes("/:agentId")) {
1002
1061
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1130,7 +1189,14 @@ function chatRoute({
1130
1189
  const uiMessageStream = createUIMessageStream({
1131
1190
  originalMessages: messages,
1132
1191
  execute: async ({ writer }) => {
1133
- 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
+ })) {
1134
1200
  writer.write(part);
1135
1201
  }
1136
1202
  }
@@ -1170,9 +1236,13 @@ function workflowRoute({
1170
1236
  schema: {
1171
1237
  type: "object",
1172
1238
  properties: {
1239
+ runId: { type: "string" },
1240
+ resourceId: { type: "string" },
1173
1241
  inputData: { type: "object", additionalProperties: true },
1242
+ resumeData: { type: "object", additionalProperties: true },
1174
1243
  requestContext: { type: "object", additionalProperties: true },
1175
- tracingOptions: { type: "object", additionalProperties: true }
1244
+ tracingOptions: { type: "object", additionalProperties: true },
1245
+ step: { type: "string" }
1176
1246
  }
1177
1247
  }
1178
1248
  }
@@ -1190,7 +1260,7 @@ function workflowRoute({
1190
1260
  }
1191
1261
  },
1192
1262
  handler: async (c) => {
1193
- const { inputData, resumeData, ...rest } = await c.req.json();
1263
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1194
1264
  const mastra = c.get("mastra");
1195
1265
  let workflowToUse = workflow;
1196
1266
  if (!workflow) {
@@ -1209,7 +1279,7 @@ function workflowRoute({
1209
1279
  if (!workflowObj) {
1210
1280
  throw new Error(`Workflow ${workflowToUse} not found`);
1211
1281
  }
1212
- const run = await workflowObj.createRun();
1282
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1213
1283
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1214
1284
  const uiMessageStream = createUIMessageStream({
1215
1285
  execute: async ({ writer }) => {