@mastra/ai-sdk 1.0.0-beta.1 → 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": {
@@ -390,21 +417,23 @@ function convertFullStreamChunkToUIMessageStream({
390
417
  return { type: "finish-step" };
391
418
  }
392
419
  case "start": {
393
- {
420
+ if (sendStart) {
394
421
  return {
395
422
  type: "start",
396
423
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
397
424
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
398
425
  };
399
426
  }
427
+ return;
400
428
  }
401
429
  case "finish": {
402
- {
430
+ if (sendFinish) {
403
431
  return {
404
432
  type: "finish",
405
433
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
406
434
  };
407
435
  }
436
+ return;
408
437
  }
409
438
  case "abort": {
410
439
  return part;
@@ -469,17 +498,23 @@ function AgentNetworkToAISDKTransformer() {
469
498
  }
470
499
  });
471
500
  }
472
- function AgentStreamToAISDKTransformer(lastMessageId) {
501
+ function AgentStreamToAISDKTransformer({
502
+ lastMessageId,
503
+ sendStart,
504
+ sendFinish,
505
+ sendReasoning,
506
+ sendSources
507
+ }) {
473
508
  let bufferedSteps = /* @__PURE__ */ new Map();
474
509
  return new TransformStream({
475
510
  transform(chunk, controller) {
476
511
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
477
512
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
478
513
  part,
479
- sendReasoning: false,
480
- sendSources: false,
481
- sendStart: true,
482
- sendFinish: true,
514
+ sendReasoning,
515
+ sendSources,
516
+ sendStart,
517
+ sendFinish,
483
518
  responseMessageId: lastMessageId,
484
519
  onError(error) {
485
520
  return safeParseErrorObject(error);
@@ -988,7 +1023,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
988
1023
  }
989
1024
 
990
1025
  // src/convert-streams.ts
991
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1026
+ function toAISdkV5Stream(stream, options = {
1027
+ from: "agent",
1028
+ sendStart: true,
1029
+ sendFinish: true
1030
+ }) {
992
1031
  const from = options?.from;
993
1032
  if (from === "workflow") {
994
1033
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -997,14 +1036,26 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
997
1036
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
998
1037
  }
999
1038
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
1000
- 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
+ );
1001
1048
  }
1002
1049
 
1003
1050
  // src/chat-route.ts
1004
1051
  function chatRoute({
1005
1052
  path = "/chat/:agentId",
1006
1053
  agent,
1007
- defaultOptions
1054
+ defaultOptions,
1055
+ sendStart = true,
1056
+ sendFinish = true,
1057
+ sendReasoning = false,
1058
+ sendSources = false
1008
1059
  }) {
1009
1060
  if (!agent && !path.includes("/:agentId")) {
1010
1061
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1138,7 +1189,14 @@ function chatRoute({
1138
1189
  const uiMessageStream = createUIMessageStream({
1139
1190
  originalMessages: messages,
1140
1191
  execute: async ({ writer }) => {
1141
- 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
+ })) {
1142
1200
  writer.write(part);
1143
1201
  }
1144
1202
  }
@@ -1178,9 +1236,13 @@ function workflowRoute({
1178
1236
  schema: {
1179
1237
  type: "object",
1180
1238
  properties: {
1239
+ runId: { type: "string" },
1240
+ resourceId: { type: "string" },
1181
1241
  inputData: { type: "object", additionalProperties: true },
1242
+ resumeData: { type: "object", additionalProperties: true },
1182
1243
  requestContext: { type: "object", additionalProperties: true },
1183
- tracingOptions: { type: "object", additionalProperties: true }
1244
+ tracingOptions: { type: "object", additionalProperties: true },
1245
+ step: { type: "string" }
1184
1246
  }
1185
1247
  }
1186
1248
  }
@@ -1198,7 +1260,7 @@ function workflowRoute({
1198
1260
  }
1199
1261
  },
1200
1262
  handler: async (c) => {
1201
- const { inputData, resumeData, ...rest } = await c.req.json();
1263
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1202
1264
  const mastra = c.get("mastra");
1203
1265
  let workflowToUse = workflow;
1204
1266
  if (!workflow) {
@@ -1217,7 +1279,7 @@ function workflowRoute({
1217
1279
  if (!workflowObj) {
1218
1280
  throw new Error(`Workflow ${workflowToUse} not found`);
1219
1281
  }
1220
- const run = await workflowObj.createRun();
1282
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1221
1283
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1222
1284
  const uiMessageStream = createUIMessageStream({
1223
1285
  execute: async ({ writer }) => {