@mastra/ai-sdk 0.0.0-main-test-05-11-2025-2-20251106053353 → 0.0.0-main-test-2-20251127210604

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.cjs CHANGED
@@ -10,6 +10,31 @@ var stream = require('@mastra/core/stream');
10
10
  var isDataChunkType = (chunk) => {
11
11
  return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
12
12
  };
13
+ var isMastraTextStreamChunk = (chunk) => {
14
+ return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
15
+ "text-start",
16
+ "text-delta",
17
+ "text-end",
18
+ "reasoning-start",
19
+ "reasoning-delta",
20
+ "reasoning-end",
21
+ "file",
22
+ "source",
23
+ "tool-input-start",
24
+ "tool-input-delta",
25
+ "tool-call",
26
+ "tool-result",
27
+ "tool-error",
28
+ "error",
29
+ "start-step",
30
+ "finish-step",
31
+ "start",
32
+ "finish",
33
+ "abort",
34
+ "tool-input-end",
35
+ "raw"
36
+ ].includes(chunk.type);
37
+ };
13
38
  function safeParseErrorObject(obj) {
14
39
  if (typeof obj !== "object" || obj === null) {
15
40
  return String(obj);
@@ -140,6 +165,28 @@ function convertMastraChunkToAISDKv5({
140
165
  toolName: chunk.payload.toolName,
141
166
  input: chunk.payload.args
142
167
  };
168
+ case "tool-call-approval":
169
+ return {
170
+ type: "data-tool-call-approval",
171
+ id: chunk.payload.toolCallId,
172
+ data: {
173
+ runId: chunk.runId,
174
+ toolCallId: chunk.payload.toolCallId,
175
+ toolName: chunk.payload.toolName,
176
+ args: chunk.payload.args
177
+ }
178
+ };
179
+ case "tool-call-suspended":
180
+ return {
181
+ type: "data-tool-call-suspended",
182
+ id: chunk.payload.toolCallId,
183
+ data: {
184
+ runId: chunk.runId,
185
+ toolCallId: chunk.payload.toolCallId,
186
+ toolName: chunk.payload.toolName,
187
+ suspendPayload: chunk.payload.suspendPayload
188
+ }
189
+ };
143
190
  case "tool-call-input-streaming-start":
144
191
  return {
145
192
  type: "tool-input-start",
@@ -230,6 +277,13 @@ function convertMastraChunkToAISDKv5({
230
277
  type: "object",
231
278
  object: chunk.object
232
279
  };
280
+ case "tripwire":
281
+ return {
282
+ type: "data-tripwire",
283
+ data: {
284
+ tripwireReason: chunk.payload.tripwireReason
285
+ }
286
+ };
233
287
  default:
234
288
  if (chunk.type && "payload" in chunk && chunk.payload) {
235
289
  return {
@@ -285,6 +339,14 @@ function convertFullStreamChunkToUIMessageStream({
285
339
  };
286
340
  }
287
341
  case "reasoning-delta": {
342
+ if (sendReasoning) {
343
+ return {
344
+ type: "reasoning-delta",
345
+ id: part.id,
346
+ delta: part.text,
347
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
348
+ };
349
+ }
288
350
  return;
289
351
  }
290
352
  case "reasoning-end": {
@@ -302,6 +364,25 @@ function convertFullStreamChunkToUIMessageStream({
302
364
  };
303
365
  }
304
366
  case "source": {
367
+ if (sendSources && part.sourceType === "url") {
368
+ return {
369
+ type: "source-url",
370
+ sourceId: part.id,
371
+ url: part.url,
372
+ title: part.title,
373
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
374
+ };
375
+ }
376
+ if (sendSources && part.sourceType === "document") {
377
+ return {
378
+ type: "source-document",
379
+ sourceId: part.id,
380
+ mediaType: part.mediaType,
381
+ title: part.title,
382
+ filename: part.filename,
383
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
384
+ };
385
+ }
305
386
  return;
306
387
  }
307
388
  case "tool-input-start": {
@@ -359,6 +440,14 @@ function convertFullStreamChunkToUIMessageStream({
359
440
  toolCallId: part.toolCallId,
360
441
  payload: part.output
361
442
  };
443
+ } else if (isDataChunkType(part.output)) {
444
+ if (!("data" in part.output)) {
445
+ throw new Error(
446
+ `UI Messages require a data property when using data- prefixed chunks
447
+ ${JSON.stringify(part)}`
448
+ );
449
+ }
450
+ return part.output;
362
451
  }
363
452
  return;
364
453
  }
@@ -384,21 +473,23 @@ function convertFullStreamChunkToUIMessageStream({
384
473
  return { type: "finish-step" };
385
474
  }
386
475
  case "start": {
387
- {
476
+ if (sendStart) {
388
477
  return {
389
478
  type: "start",
390
479
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
391
480
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
392
481
  };
393
482
  }
483
+ return;
394
484
  }
395
485
  case "finish": {
396
- {
486
+ if (sendFinish) {
397
487
  return {
398
488
  type: "finish",
399
489
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
400
490
  };
401
491
  }
492
+ return;
402
493
  }
403
494
  case "abort": {
404
495
  return part;
@@ -425,7 +516,10 @@ function convertFullStreamChunkToUIMessageStream({
425
516
  }
426
517
 
427
518
  // src/transformers.ts
428
- function WorkflowStreamToAISDKTransformer() {
519
+ var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
520
+ function WorkflowStreamToAISDKTransformer({
521
+ includeTextStreamParts
522
+ } = {}) {
429
523
  const bufferedWorkflows = /* @__PURE__ */ new Map();
430
524
  return new TransformStream({
431
525
  start(controller) {
@@ -439,7 +533,7 @@ function WorkflowStreamToAISDKTransformer() {
439
533
  });
440
534
  },
441
535
  transform(chunk, controller) {
442
- const transformed = transformWorkflow(chunk, bufferedWorkflows);
536
+ const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
443
537
  if (transformed) controller.enqueue(transformed);
444
538
  }
445
539
  });
@@ -463,20 +557,37 @@ function AgentNetworkToAISDKTransformer() {
463
557
  }
464
558
  });
465
559
  }
466
- function AgentStreamToAISDKTransformer(lastMessageId) {
560
+ function AgentStreamToAISDKTransformer({
561
+ lastMessageId,
562
+ sendStart,
563
+ sendFinish,
564
+ sendReasoning,
565
+ sendSources,
566
+ messageMetadata,
567
+ onError
568
+ }) {
467
569
  let bufferedSteps = /* @__PURE__ */ new Map();
570
+ let tripwireOccurred = false;
571
+ let finishEventSent = false;
468
572
  return new TransformStream({
469
573
  transform(chunk, controller) {
574
+ if (chunk.type === "tripwire") {
575
+ tripwireOccurred = true;
576
+ }
577
+ if (chunk.type === "finish") {
578
+ finishEventSent = true;
579
+ }
470
580
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
471
581
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
472
582
  part,
473
- sendReasoning: false,
474
- sendSources: false,
475
- sendStart: true,
476
- sendFinish: true,
583
+ sendReasoning,
584
+ sendSources,
585
+ messageMetadataValue: messageMetadata?.({ part }),
586
+ sendStart,
587
+ sendFinish,
477
588
  responseMessageId: lastMessageId,
478
589
  onError(error) {
479
- return safeParseErrorObject(error);
590
+ return onError ? onError(error) : safeParseErrorObject(error);
480
591
  }
481
592
  });
482
593
  if (transformedChunk) {
@@ -496,6 +607,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
496
607
  controller.enqueue(transformedChunk);
497
608
  }
498
609
  }
610
+ },
611
+ flush(controller) {
612
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
613
+ controller.enqueue({
614
+ type: "finish",
615
+ finishReason: "other"
616
+ });
617
+ }
499
618
  }
500
619
  });
501
620
  }
@@ -635,7 +754,7 @@ function transformAgent(payload, bufferedSteps) {
635
754
  }
636
755
  return null;
637
756
  }
638
- function transformWorkflow(payload, bufferedWorkflows, isNested) {
757
+ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
639
758
  switch (payload.type) {
640
759
  case "workflow-start":
641
760
  bufferedWorkflows.set(payload.runId, {
@@ -728,6 +847,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
728
847
  }
729
848
  };
730
849
  }
850
+ case "workflow-step-output": {
851
+ const output = payload.payload.output;
852
+ if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
853
+ const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
854
+ const transformedChunk = convertFullStreamChunkToUIMessageStream({
855
+ part,
856
+ onError(error) {
857
+ return safeParseErrorObject(error);
858
+ }
859
+ });
860
+ return transformedChunk;
861
+ }
862
+ if (output && isDataChunkType(output)) {
863
+ if (!("data" in output)) {
864
+ throw new Error(
865
+ `UI Messages require a data property when using data- prefixed chunks
866
+ ${JSON.stringify(output)}`
867
+ );
868
+ }
869
+ return output;
870
+ }
871
+ return null;
872
+ }
731
873
  default: {
732
874
  if (isDataChunkType(payload)) {
733
875
  if (!("data" in payload)) {
@@ -747,12 +889,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
747
889
  case "routing-agent-start": {
748
890
  if (!bufferedNetworks.has(payload.runId)) {
749
891
  bufferedNetworks.set(payload.runId, {
750
- name: payload.payload.agentId,
892
+ name: payload.payload.networkId,
751
893
  steps: [],
752
894
  usage: null,
753
895
  output: null
754
896
  });
755
897
  }
898
+ const current = bufferedNetworks.get(payload.runId);
899
+ current.steps.push({
900
+ id: payload.payload.runId,
901
+ name: payload.payload.agentId,
902
+ status: "running",
903
+ iteration: payload.payload.inputData.iteration,
904
+ input: {
905
+ task: payload.payload.inputData.task,
906
+ threadId: payload.payload.inputData.threadId,
907
+ threadResourceId: payload.payload.inputData.threadResourceId
908
+ },
909
+ output: "",
910
+ task: null,
911
+ suspendPayload: null,
912
+ resumePayload: null,
913
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
914
+ });
756
915
  return {
757
916
  type: isNested ? "data-tool-network" : "data-network",
758
917
  id: payload.runId,
@@ -783,14 +942,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
783
942
  };
784
943
  }
785
944
  case "agent-execution-start": {
786
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
945
+ const current = bufferedNetworks.get(payload.runId);
946
+ if (!current) return null;
787
947
  current.steps.push({
948
+ id: payload.payload.runId,
788
949
  name: payload.payload.agentId,
789
950
  status: "running",
790
- input: payload.payload.args || null,
951
+ iteration: payload.payload.args?.iteration ?? 0,
952
+ input: { prompt: payload.payload.args?.prompt ?? "" },
791
953
  output: null,
954
+ task: null,
792
955
  suspendPayload: null,
793
- resumePayload: null
956
+ resumePayload: null,
957
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
794
958
  });
795
959
  bufferedNetworks.set(payload.runId, current);
796
960
  return {
@@ -803,14 +967,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
803
967
  };
804
968
  }
805
969
  case "workflow-execution-start": {
806
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
970
+ const current = bufferedNetworks.get(payload.runId);
971
+ if (!current) return null;
807
972
  current.steps.push({
808
- name: payload.payload.name,
973
+ id: payload.payload.runId,
974
+ name: payload.payload.workflowId,
809
975
  status: "running",
810
- input: payload.payload.args || null,
976
+ iteration: payload.payload.args?.iteration ?? 0,
977
+ input: { prompt: payload.payload.args?.prompt ?? "" },
811
978
  output: null,
979
+ task: null,
812
980
  suspendPayload: null,
813
- resumePayload: null
981
+ resumePayload: null,
982
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
814
983
  });
815
984
  bufferedNetworks.set(payload.runId, current);
816
985
  return {
@@ -823,14 +992,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
823
992
  };
824
993
  }
825
994
  case "tool-execution-start": {
826
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
995
+ const current = bufferedNetworks.get(payload.runId);
996
+ if (!current) return null;
827
997
  current.steps.push({
998
+ id: payload.payload.args.toolCallId,
828
999
  name: payload.payload.args?.toolName,
829
1000
  status: "running",
1001
+ iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
1002
+ task: {
1003
+ id: payload.payload.args?.toolName
1004
+ },
830
1005
  input: payload.payload.args?.args || null,
831
1006
  output: null,
832
1007
  suspendPayload: null,
833
- resumePayload: null
1008
+ resumePayload: null,
1009
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
834
1010
  });
835
1011
  bufferedNetworks.set(payload.runId, current);
836
1012
  return {
@@ -845,14 +1021,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
845
1021
  case "agent-execution-end": {
846
1022
  const current = bufferedNetworks.get(payload.runId);
847
1023
  if (!current) return null;
848
- current.steps.push({
849
- name: payload.payload.agentId,
850
- status: "success",
851
- input: null,
852
- output: payload.payload.result,
853
- suspendPayload: null,
854
- resumePayload: null
855
- });
1024
+ const stepId = payload.payload.runId;
1025
+ const step = current.steps.find((step2) => step2.id === stepId);
1026
+ if (!step) {
1027
+ return null;
1028
+ }
1029
+ step.status = "success";
1030
+ step.output = payload.payload.result;
856
1031
  return {
857
1032
  type: isNested ? "data-tool-network" : "data-network",
858
1033
  id: payload.runId,
@@ -867,14 +1042,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
867
1042
  case "tool-execution-end": {
868
1043
  const current = bufferedNetworks.get(payload.runId);
869
1044
  if (!current) return null;
870
- current.steps.push({
871
- name: payload.payload.toolName,
872
- status: "success",
873
- input: null,
874
- output: payload.payload.result,
875
- suspendPayload: null,
876
- resumePayload: null
877
- });
1045
+ const stepId = payload.payload.toolCallId;
1046
+ const step = current.steps.find((step2) => step2.id === stepId);
1047
+ if (!step) {
1048
+ return null;
1049
+ }
1050
+ step.status = "success";
1051
+ step.output = payload.payload.result;
878
1052
  return {
879
1053
  type: isNested ? "data-tool-network" : "data-network",
880
1054
  id: payload.runId,
@@ -888,14 +1062,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
888
1062
  case "workflow-execution-end": {
889
1063
  const current = bufferedNetworks.get(payload.runId);
890
1064
  if (!current) return null;
891
- current.steps.push({
892
- name: payload.payload.name,
893
- status: "success",
894
- input: null,
895
- output: payload.payload.result,
896
- suspendPayload: null,
897
- resumePayload: null
898
- });
1065
+ const stepId = payload.payload.runId;
1066
+ const step = current.steps.find((step2) => step2.id === stepId);
1067
+ if (!step) {
1068
+ return null;
1069
+ }
1070
+ step.status = "success";
1071
+ step.output = payload.payload.result;
899
1072
  return {
900
1073
  type: isNested ? "data-tool-network" : "data-network",
901
1074
  id: payload.runId,
@@ -910,12 +1083,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
910
1083
  case "routing-agent-end": {
911
1084
  const current = bufferedNetworks.get(payload.runId);
912
1085
  if (!current) return null;
1086
+ const stepId = payload.payload.runId;
1087
+ const step = current.steps.find((step2) => step2.id === stepId);
1088
+ if (!step) {
1089
+ return null;
1090
+ }
1091
+ step.status = "success";
1092
+ step.task = {
1093
+ id: payload.payload.primitiveId,
1094
+ type: payload.payload.primitiveType,
1095
+ name: payload.payload.task,
1096
+ reason: payload.payload.selectionReason
1097
+ };
1098
+ step.output = payload.payload.result;
913
1099
  return {
914
1100
  type: isNested ? "data-tool-network" : "data-network",
915
1101
  id: payload.runId,
916
1102
  data: {
917
1103
  ...current,
918
- status: "finished",
919
1104
  usage: payload.payload?.usage ?? current.usage,
920
1105
  output: payload.payload?.result ?? current.output
921
1106
  }
@@ -949,6 +1134,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
949
1134
  };
950
1135
  }
951
1136
  default: {
1137
+ if (payload.type.startsWith("agent-execution-event-")) {
1138
+ const stepId = payload.payload.runId;
1139
+ const current = bufferedNetworks.get(payload.runId);
1140
+ if (!current) return null;
1141
+ const step = current.steps.find((step2) => step2.id === stepId);
1142
+ if (!step) {
1143
+ return null;
1144
+ }
1145
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1146
+ const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1147
+ if (result) {
1148
+ const { request, response, ...data } = result.data;
1149
+ step.task = data;
1150
+ }
1151
+ }
1152
+ if (payload.type.startsWith("workflow-execution-event-")) {
1153
+ const stepId = payload.payload.runId;
1154
+ const current = bufferedNetworks.get(payload.runId);
1155
+ if (!current) return null;
1156
+ const step = current.steps.find((step2) => step2.id === stepId);
1157
+ if (!step) {
1158
+ return null;
1159
+ }
1160
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1161
+ const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1162
+ if (result && "data" in result) {
1163
+ const data = result.data;
1164
+ step.task = data;
1165
+ if (data.name && step.task) {
1166
+ step.task.id = data.name;
1167
+ }
1168
+ }
1169
+ }
952
1170
  if (isDataChunkType(payload)) {
953
1171
  if (!("data" in payload)) {
954
1172
  throw new Error(
@@ -956,7 +1174,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
956
1174
  ${JSON.stringify(payload)}`
957
1175
  );
958
1176
  }
959
- return payload;
1177
+ const { type, data } = payload;
1178
+ return { type, data };
960
1179
  }
961
1180
  if (isAgentExecutionDataChunkType(payload)) {
962
1181
  if (!("data" in payload.payload)) {
@@ -965,7 +1184,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
965
1184
  ${JSON.stringify(payload)}`
966
1185
  );
967
1186
  }
968
- return payload.payload;
1187
+ const { type, data } = payload.payload;
1188
+ return { type, data };
969
1189
  }
970
1190
  if (isWorkflowExecutionDataChunkType(payload)) {
971
1191
  if (!("data" in payload.payload)) {
@@ -974,7 +1194,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
974
1194
  ${JSON.stringify(payload)}`
975
1195
  );
976
1196
  }
977
- return payload.payload;
1197
+ const { type, data } = payload.payload;
1198
+ return { type, data };
978
1199
  }
979
1200
  return null;
980
1201
  }
@@ -982,23 +1203,44 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
982
1203
  }
983
1204
 
984
1205
  // src/convert-streams.ts
985
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1206
+ function toAISdkV5Stream(stream, options = {
1207
+ from: "agent",
1208
+ sendStart: true,
1209
+ sendFinish: true
1210
+ }) {
986
1211
  const from = options?.from;
987
1212
  if (from === "workflow") {
988
- return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
1213
+ const includeTextStreamParts = options?.includeTextStreamParts ?? true;
1214
+ return stream.pipeThrough(
1215
+ WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
1216
+ );
989
1217
  }
990
1218
  if (from === "network") {
991
1219
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
992
1220
  }
993
1221
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
994
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1222
+ return agentReadable.pipeThrough(
1223
+ AgentStreamToAISDKTransformer({
1224
+ lastMessageId: options?.lastMessageId,
1225
+ sendStart: options?.sendStart,
1226
+ sendFinish: options?.sendFinish,
1227
+ sendReasoning: options?.sendReasoning,
1228
+ sendSources: options?.sendSources,
1229
+ messageMetadata: options?.messageMetadata,
1230
+ onError: options?.onError
1231
+ })
1232
+ );
995
1233
  }
996
1234
 
997
1235
  // src/chat-route.ts
998
1236
  function chatRoute({
999
1237
  path = "/chat/:agentId",
1000
1238
  agent,
1001
- defaultOptions
1239
+ defaultOptions,
1240
+ sendStart = true,
1241
+ sendFinish = true,
1242
+ sendReasoning = false,
1243
+ sendSources = false
1002
1244
  }) {
1003
1245
  if (!agent && !path.includes("/:agentId")) {
1004
1246
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1116,7 +1358,7 @@ function chatRoute({
1116
1358
  if (!agentToUse) {
1117
1359
  throw new Error("Agent ID is required");
1118
1360
  }
1119
- const agentObj = mastra.getAgent(agentToUse);
1361
+ const agentObj = mastra.getAgentById(agentToUse);
1120
1362
  if (!agentObj) {
1121
1363
  throw new Error(`Agent ${agentToUse} not found`);
1122
1364
  }
@@ -1132,7 +1374,14 @@ function chatRoute({
1132
1374
  const uiMessageStream = ai.createUIMessageStream({
1133
1375
  originalMessages: messages,
1134
1376
  execute: async ({ writer }) => {
1135
- for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
1377
+ for await (const part of toAISdkV5Stream(result, {
1378
+ from: "agent",
1379
+ lastMessageId,
1380
+ sendStart,
1381
+ sendFinish,
1382
+ sendReasoning,
1383
+ sendSources
1384
+ })) {
1136
1385
  writer.write(part);
1137
1386
  }
1138
1387
  }
@@ -1145,7 +1394,8 @@ function chatRoute({
1145
1394
  }
1146
1395
  function workflowRoute({
1147
1396
  path = "/api/workflows/:workflowId/stream",
1148
- workflow
1397
+ workflow,
1398
+ includeTextStreamParts = true
1149
1399
  }) {
1150
1400
  if (!workflow && !path.includes("/:workflowId")) {
1151
1401
  throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
@@ -1172,9 +1422,13 @@ function workflowRoute({
1172
1422
  schema: {
1173
1423
  type: "object",
1174
1424
  properties: {
1425
+ runId: { type: "string" },
1426
+ resourceId: { type: "string" },
1175
1427
  inputData: { type: "object", additionalProperties: true },
1428
+ resumeData: { type: "object", additionalProperties: true },
1176
1429
  requestContext: { type: "object", additionalProperties: true },
1177
- tracingOptions: { type: "object", additionalProperties: true }
1430
+ tracingOptions: { type: "object", additionalProperties: true },
1431
+ step: { type: "string" }
1178
1432
  }
1179
1433
  }
1180
1434
  }
@@ -1192,8 +1446,9 @@ function workflowRoute({
1192
1446
  }
1193
1447
  },
1194
1448
  handler: async (c) => {
1195
- const { inputData, resumeData, ...rest } = await c.req.json();
1449
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1196
1450
  const mastra = c.get("mastra");
1451
+ const requestContext = c.get("requestContext");
1197
1452
  let workflowToUse = workflow;
1198
1453
  if (!workflow) {
1199
1454
  const workflowId = c.req.param("workflowId");
@@ -1207,15 +1462,20 @@ function workflowRoute({
1207
1462
  if (!workflowToUse) {
1208
1463
  throw new Error("Workflow ID is required");
1209
1464
  }
1210
- const workflowObj = mastra.getWorkflow(workflowToUse);
1465
+ const workflowObj = mastra.getWorkflowById(workflowToUse);
1211
1466
  if (!workflowObj) {
1212
1467
  throw new Error(`Workflow ${workflowToUse} not found`);
1213
1468
  }
1214
- const run = await workflowObj.createRun();
1215
- const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1469
+ if (requestContext && rest.requestContext) {
1470
+ mastra.getLogger()?.warn(
1471
+ `"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
1472
+ );
1473
+ }
1474
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1475
+ const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext: requestContext || rest.requestContext }) : run.stream({ inputData, ...rest, requestContext: requestContext || rest.requestContext });
1216
1476
  const uiMessageStream = ai.createUIMessageStream({
1217
1477
  execute: async ({ writer }) => {
1218
- for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
1478
+ for await (const part of toAISdkV5Stream(stream, { from: "workflow", includeTextStreamParts })) {
1219
1479
  writer.write(part);
1220
1480
  }
1221
1481
  }
@@ -1299,7 +1559,7 @@ function networkRoute({
1299
1559
  if (!agentToUse) {
1300
1560
  throw new Error("Agent ID is required");
1301
1561
  }
1302
- const agentObj = mastra.getAgent(agentToUse);
1562
+ const agentObj = mastra.getAgentById(agentToUse);
1303
1563
  if (!agentObj) {
1304
1564
  throw new Error(`Agent ${agentToUse} not found`);
1305
1565
  }