@mastra/ai-sdk 1.0.0-beta.1 → 1.0.0-beta.3

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
@@ -228,6 +228,13 @@ function convertMastraChunkToAISDKv5({
228
228
  type: "object",
229
229
  object: chunk.object
230
230
  };
231
+ case "tripwire":
232
+ return {
233
+ type: "data-tripwire",
234
+ data: {
235
+ tripwireReason: chunk.payload.tripwireReason
236
+ }
237
+ };
231
238
  default:
232
239
  if (chunk.type && "payload" in chunk && chunk.payload) {
233
240
  return {
@@ -283,6 +290,14 @@ function convertFullStreamChunkToUIMessageStream({
283
290
  };
284
291
  }
285
292
  case "reasoning-delta": {
293
+ if (sendReasoning) {
294
+ return {
295
+ type: "reasoning-delta",
296
+ id: part.id,
297
+ delta: part.text,
298
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
299
+ };
300
+ }
286
301
  return;
287
302
  }
288
303
  case "reasoning-end": {
@@ -300,6 +315,25 @@ function convertFullStreamChunkToUIMessageStream({
300
315
  };
301
316
  }
302
317
  case "source": {
318
+ if (sendSources && part.sourceType === "url") {
319
+ return {
320
+ type: "source-url",
321
+ sourceId: part.id,
322
+ url: part.url,
323
+ title: part.title,
324
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
325
+ };
326
+ }
327
+ if (sendSources && part.sourceType === "document") {
328
+ return {
329
+ type: "source-document",
330
+ sourceId: part.id,
331
+ mediaType: part.mediaType,
332
+ title: part.title,
333
+ filename: part.filename,
334
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
335
+ };
336
+ }
303
337
  return;
304
338
  }
305
339
  case "tool-input-start": {
@@ -390,21 +424,23 @@ function convertFullStreamChunkToUIMessageStream({
390
424
  return { type: "finish-step" };
391
425
  }
392
426
  case "start": {
393
- {
427
+ if (sendStart) {
394
428
  return {
395
429
  type: "start",
396
430
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
397
431
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
398
432
  };
399
433
  }
434
+ return;
400
435
  }
401
436
  case "finish": {
402
- {
437
+ if (sendFinish) {
403
438
  return {
404
439
  type: "finish",
405
440
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
406
441
  };
407
442
  }
443
+ return;
408
444
  }
409
445
  case "abort": {
410
446
  return part;
@@ -469,17 +505,31 @@ function AgentNetworkToAISDKTransformer() {
469
505
  }
470
506
  });
471
507
  }
472
- function AgentStreamToAISDKTransformer(lastMessageId) {
508
+ function AgentStreamToAISDKTransformer({
509
+ lastMessageId,
510
+ sendStart,
511
+ sendFinish,
512
+ sendReasoning,
513
+ sendSources
514
+ }) {
473
515
  let bufferedSteps = /* @__PURE__ */ new Map();
516
+ let tripwireOccurred = false;
517
+ let finishEventSent = false;
474
518
  return new TransformStream({
475
519
  transform(chunk, controller) {
520
+ if (chunk.type === "tripwire") {
521
+ tripwireOccurred = true;
522
+ }
523
+ if (chunk.type === "finish") {
524
+ finishEventSent = true;
525
+ }
476
526
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
477
527
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
478
528
  part,
479
- sendReasoning: false,
480
- sendSources: false,
481
- sendStart: true,
482
- sendFinish: true,
529
+ sendReasoning,
530
+ sendSources,
531
+ sendStart,
532
+ sendFinish,
483
533
  responseMessageId: lastMessageId,
484
534
  onError(error) {
485
535
  return safeParseErrorObject(error);
@@ -502,6 +552,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
502
552
  controller.enqueue(transformedChunk);
503
553
  }
504
554
  }
555
+ },
556
+ flush(controller) {
557
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
558
+ controller.enqueue({
559
+ type: "finish",
560
+ finishReason: "other"
561
+ });
562
+ }
505
563
  }
506
564
  });
507
565
  }
@@ -988,7 +1046,11 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
988
1046
  }
989
1047
 
990
1048
  // src/convert-streams.ts
991
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1049
+ function toAISdkV5Stream(stream, options = {
1050
+ from: "agent",
1051
+ sendStart: true,
1052
+ sendFinish: true
1053
+ }) {
992
1054
  const from = options?.from;
993
1055
  if (from === "workflow") {
994
1056
  return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
@@ -997,14 +1059,26 @@ function toAISdkV5Stream(stream, options = { from: "agent" }) {
997
1059
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
998
1060
  }
999
1061
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
1000
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1062
+ return agentReadable.pipeThrough(
1063
+ AgentStreamToAISDKTransformer({
1064
+ lastMessageId: options?.lastMessageId,
1065
+ sendStart: options?.sendStart,
1066
+ sendFinish: options?.sendFinish,
1067
+ sendReasoning: options?.sendReasoning,
1068
+ sendSources: options?.sendSources
1069
+ })
1070
+ );
1001
1071
  }
1002
1072
 
1003
1073
  // src/chat-route.ts
1004
1074
  function chatRoute({
1005
1075
  path = "/chat/:agentId",
1006
1076
  agent,
1007
- defaultOptions
1077
+ defaultOptions,
1078
+ sendStart = true,
1079
+ sendFinish = true,
1080
+ sendReasoning = false,
1081
+ sendSources = false
1008
1082
  }) {
1009
1083
  if (!agent && !path.includes("/:agentId")) {
1010
1084
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1138,7 +1212,14 @@ function chatRoute({
1138
1212
  const uiMessageStream = createUIMessageStream({
1139
1213
  originalMessages: messages,
1140
1214
  execute: async ({ writer }) => {
1141
- for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
1215
+ for await (const part of toAISdkV5Stream(result, {
1216
+ from: "agent",
1217
+ lastMessageId,
1218
+ sendStart,
1219
+ sendFinish,
1220
+ sendReasoning,
1221
+ sendSources
1222
+ })) {
1142
1223
  writer.write(part);
1143
1224
  }
1144
1225
  }
@@ -1178,9 +1259,13 @@ function workflowRoute({
1178
1259
  schema: {
1179
1260
  type: "object",
1180
1261
  properties: {
1262
+ runId: { type: "string" },
1263
+ resourceId: { type: "string" },
1181
1264
  inputData: { type: "object", additionalProperties: true },
1265
+ resumeData: { type: "object", additionalProperties: true },
1182
1266
  requestContext: { type: "object", additionalProperties: true },
1183
- tracingOptions: { type: "object", additionalProperties: true }
1267
+ tracingOptions: { type: "object", additionalProperties: true },
1268
+ step: { type: "string" }
1184
1269
  }
1185
1270
  }
1186
1271
  }
@@ -1198,7 +1283,7 @@ function workflowRoute({
1198
1283
  }
1199
1284
  },
1200
1285
  handler: async (c) => {
1201
- const { inputData, resumeData, ...rest } = await c.req.json();
1286
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1202
1287
  const mastra = c.get("mastra");
1203
1288
  let workflowToUse = workflow;
1204
1289
  if (!workflow) {
@@ -1217,7 +1302,7 @@ function workflowRoute({
1217
1302
  if (!workflowObj) {
1218
1303
  throw new Error(`Workflow ${workflowToUse} not found`);
1219
1304
  }
1220
- const run = await workflowObj.createRun();
1305
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1221
1306
  const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1222
1307
  const uiMessageStream = createUIMessageStream({
1223
1308
  execute: async ({ writer }) => {