@mastra/ai-sdk 0.2.7-alpha.0 → 0.3.1

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
@@ -124,6 +124,28 @@ function convertMastraChunkToAISDKv5({
124
124
  toolName: chunk.payload.toolName,
125
125
  input: chunk.payload.args
126
126
  };
127
+ case "tool-call-approval":
128
+ return {
129
+ type: "data-tool-call-approval",
130
+ id: chunk.payload.toolCallId,
131
+ data: {
132
+ runId: chunk.runId,
133
+ toolCallId: chunk.payload.toolCallId,
134
+ toolName: chunk.payload.toolName,
135
+ args: chunk.payload.args
136
+ }
137
+ };
138
+ case "tool-call-suspended":
139
+ return {
140
+ type: "data-tool-call-suspended",
141
+ id: chunk.payload.toolCallId,
142
+ data: {
143
+ runId: chunk.runId,
144
+ toolCallId: chunk.payload.toolCallId,
145
+ toolName: chunk.payload.toolName,
146
+ suspendPayload: chunk.payload.suspendPayload
147
+ }
148
+ };
127
149
  case "tool-call-input-streaming-start":
128
150
  return {
129
151
  type: "tool-input-start",
@@ -214,6 +236,13 @@ function convertMastraChunkToAISDKv5({
214
236
  type: "object",
215
237
  object: chunk.object
216
238
  };
239
+ case "tripwire":
240
+ return {
241
+ type: "data-tripwire",
242
+ data: {
243
+ tripwireReason: chunk.payload.tripwireReason
244
+ }
245
+ };
217
246
  default:
218
247
  if (chunk.type && "payload" in chunk && chunk.payload) {
219
248
  return {
@@ -269,6 +298,14 @@ function convertFullStreamChunkToUIMessageStream({
269
298
  };
270
299
  }
271
300
  case "reasoning-delta": {
301
+ if (sendReasoning) {
302
+ return {
303
+ type: "reasoning-delta",
304
+ id: part.id,
305
+ delta: part.text,
306
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
307
+ };
308
+ }
272
309
  return;
273
310
  }
274
311
  case "reasoning-end": {
@@ -286,6 +323,25 @@ function convertFullStreamChunkToUIMessageStream({
286
323
  };
287
324
  }
288
325
  case "source": {
326
+ if (sendSources && part.sourceType === "url") {
327
+ return {
328
+ type: "source-url",
329
+ sourceId: part.id,
330
+ url: part.url,
331
+ title: part.title,
332
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
333
+ };
334
+ }
335
+ if (sendSources && part.sourceType === "document") {
336
+ return {
337
+ type: "source-document",
338
+ sourceId: part.id,
339
+ mediaType: part.mediaType,
340
+ title: part.title,
341
+ filename: part.filename,
342
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
343
+ };
344
+ }
289
345
  return;
290
346
  }
291
347
  case "tool-input-start": {
@@ -376,21 +432,23 @@ function convertFullStreamChunkToUIMessageStream({
376
432
  return { type: "finish-step" };
377
433
  }
378
434
  case "start": {
379
- {
435
+ if (sendStart) {
380
436
  return {
381
437
  type: "start",
382
438
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
383
439
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
384
440
  };
385
441
  }
442
+ return;
386
443
  }
387
444
  case "finish": {
388
- {
445
+ if (sendFinish) {
389
446
  return {
390
447
  type: "finish",
391
448
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
392
449
  };
393
450
  }
451
+ return;
394
452
  }
395
453
  case "abort": {
396
454
  return part;
@@ -455,17 +513,31 @@ function AgentNetworkToAISDKTransformer() {
455
513
  }
456
514
  });
457
515
  }
458
- function AgentStreamToAISDKTransformer(lastMessageId) {
516
+ function AgentStreamToAISDKTransformer({
517
+ lastMessageId,
518
+ sendStart,
519
+ sendFinish,
520
+ sendReasoning,
521
+ sendSources
522
+ }) {
459
523
  let bufferedSteps = /* @__PURE__ */ new Map();
524
+ let tripwireOccurred = false;
525
+ let finishEventSent = false;
460
526
  return new TransformStream({
461
527
  transform(chunk, controller) {
528
+ if (chunk.type === "tripwire") {
529
+ tripwireOccurred = true;
530
+ }
531
+ if (chunk.type === "finish") {
532
+ finishEventSent = true;
533
+ }
462
534
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
463
535
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
464
536
  part,
465
- sendReasoning: false,
466
- sendSources: false,
467
- sendStart: true,
468
- sendFinish: true,
537
+ sendReasoning,
538
+ sendSources,
539
+ sendStart,
540
+ sendFinish,
469
541
  responseMessageId: lastMessageId,
470
542
  onError() {
471
543
  return "Error";
@@ -488,6 +560,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
488
560
  controller.enqueue(transformedChunk);
489
561
  }
490
562
  }
563
+ },
564
+ flush(controller) {
565
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
566
+ controller.enqueue({
567
+ type: "finish",
568
+ finishReason: "other"
569
+ });
570
+ }
491
571
  }
492
572
  });
493
573
  }
@@ -974,7 +1054,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
974
1054
  }
975
1055
 
976
1056
  // src/to-ai-sdk-format.ts
977
- function toAISdkFormat(stream, options = { from: "agent" }) {
1057
+ function toAISdkFormat(stream, options = {
1058
+ from: "agent",
1059
+ sendStart: true,
1060
+ sendFinish: true
1061
+ }) {
978
1062
  const from = options?.from;
979
1063
  if (from === "workflow") {
980
1064
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -983,14 +1067,26 @@ function toAISdkFormat(stream, options = { from: "agent" }) {
983
1067
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
984
1068
  }
985
1069
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
986
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1070
+ return agentReadable.pipeThrough(
1071
+ AgentStreamToAISDKTransformer({
1072
+ lastMessageId: options?.lastMessageId,
1073
+ sendStart: options?.sendStart,
1074
+ sendFinish: options?.sendFinish,
1075
+ sendReasoning: options?.sendReasoning,
1076
+ sendSources: options?.sendSources
1077
+ })
1078
+ );
987
1079
  }
988
1080
 
989
1081
  // src/chat-route.ts
990
1082
  function chatRoute({
991
1083
  path = "/chat/:agentId",
992
1084
  agent,
993
- defaultOptions
1085
+ defaultOptions,
1086
+ sendStart = true,
1087
+ sendFinish = true,
1088
+ sendReasoning = false,
1089
+ sendSources = false
994
1090
  }) {
995
1091
  if (!agent && !path.includes("/:agentId")) {
996
1092
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1124,7 +1220,14 @@ function chatRoute({
1124
1220
  const uiMessageStream = createUIMessageStream({
1125
1221
  originalMessages: messages,
1126
1222
  execute: async ({ writer }) => {
1127
- for await (const part of toAISdkFormat(result, { from: "agent", lastMessageId })) {
1223
+ for await (const part of toAISdkFormat(result, {
1224
+ from: "agent",
1225
+ lastMessageId,
1226
+ sendStart,
1227
+ sendFinish,
1228
+ sendReasoning,
1229
+ sendSources
1230
+ })) {
1128
1231
  writer.write(part);
1129
1232
  }
1130
1233
  }
@@ -1168,7 +1271,7 @@ function workflowRoute({
1168
1271
  resourceId: { type: "string" },
1169
1272
  inputData: { type: "object", additionalProperties: true },
1170
1273
  resumeData: { type: "object", additionalProperties: true },
1171
- requestContext: { type: "object", additionalProperties: true },
1274
+ runtimeContext: { type: "object", additionalProperties: true },
1172
1275
  tracingOptions: { type: "object", additionalProperties: true },
1173
1276
  step: { type: "string" }
1174
1277
  }
@@ -1207,7 +1310,7 @@ function workflowRoute({
1207
1310
  if (!workflowObj) {
1208
1311
  throw new Error(`Workflow ${workflowToUse} not found`);
1209
1312
  }
1210
- const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1313
+ const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
1211
1314
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1212
1315
  const uiMessageStream = createUIMessageStream({
1213
1316
  execute: async ({ writer }) => {