@mastra/ai-sdk 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-safe-stringify-telemetry-20251205024938

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
@@ -8,6 +8,51 @@ import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core
8
8
  var isDataChunkType = (chunk) => {
9
9
  return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
10
10
  };
11
+ var isMastraTextStreamChunk = (chunk) => {
12
+ return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
13
+ "text-start",
14
+ "text-delta",
15
+ "text-end",
16
+ "reasoning-start",
17
+ "reasoning-delta",
18
+ "reasoning-end",
19
+ "file",
20
+ "source",
21
+ "tool-input-start",
22
+ "tool-input-delta",
23
+ "tool-call",
24
+ "tool-result",
25
+ "tool-error",
26
+ "error",
27
+ "start-step",
28
+ "finish-step",
29
+ "start",
30
+ "finish",
31
+ "abort",
32
+ "tool-input-end",
33
+ "raw"
34
+ ].includes(chunk.type);
35
+ };
36
+ function safeParseErrorObject(obj) {
37
+ if (typeof obj !== "object" || obj === null) {
38
+ return String(obj);
39
+ }
40
+ try {
41
+ const stringified = JSON.stringify(obj);
42
+ if (stringified === "{}") {
43
+ return String(obj);
44
+ }
45
+ return stringified;
46
+ } catch {
47
+ return String(obj);
48
+ }
49
+ }
50
+ var isAgentExecutionDataChunkType = (chunk) => {
51
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
52
+ };
53
+ var isWorkflowExecutionDataChunkType = (chunk) => {
54
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
55
+ };
11
56
 
12
57
  // src/helpers.ts
13
58
  function convertMastraChunkToAISDKv5({
@@ -118,6 +163,28 @@ function convertMastraChunkToAISDKv5({
118
163
  toolName: chunk.payload.toolName,
119
164
  input: chunk.payload.args
120
165
  };
166
+ case "tool-call-approval":
167
+ return {
168
+ type: "data-tool-call-approval",
169
+ id: chunk.payload.toolCallId,
170
+ data: {
171
+ runId: chunk.runId,
172
+ toolCallId: chunk.payload.toolCallId,
173
+ toolName: chunk.payload.toolName,
174
+ args: chunk.payload.args
175
+ }
176
+ };
177
+ case "tool-call-suspended":
178
+ return {
179
+ type: "data-tool-call-suspended",
180
+ id: chunk.payload.toolCallId,
181
+ data: {
182
+ runId: chunk.runId,
183
+ toolCallId: chunk.payload.toolCallId,
184
+ toolName: chunk.payload.toolName,
185
+ suspendPayload: chunk.payload.suspendPayload
186
+ }
187
+ };
121
188
  case "tool-call-input-streaming-start":
122
189
  return {
123
190
  type: "tool-input-start",
@@ -208,6 +275,13 @@ function convertMastraChunkToAISDKv5({
208
275
  type: "object",
209
276
  object: chunk.object
210
277
  };
278
+ case "tripwire":
279
+ return {
280
+ type: "data-tripwire",
281
+ data: {
282
+ tripwireReason: chunk.payload.tripwireReason
283
+ }
284
+ };
211
285
  default:
212
286
  if (chunk.type && "payload" in chunk && chunk.payload) {
213
287
  return {
@@ -263,6 +337,14 @@ function convertFullStreamChunkToUIMessageStream({
263
337
  };
264
338
  }
265
339
  case "reasoning-delta": {
340
+ if (sendReasoning) {
341
+ return {
342
+ type: "reasoning-delta",
343
+ id: part.id,
344
+ delta: part.text,
345
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
346
+ };
347
+ }
266
348
  return;
267
349
  }
268
350
  case "reasoning-end": {
@@ -280,6 +362,25 @@ function convertFullStreamChunkToUIMessageStream({
280
362
  };
281
363
  }
282
364
  case "source": {
365
+ if (sendSources && part.sourceType === "url") {
366
+ return {
367
+ type: "source-url",
368
+ sourceId: part.id,
369
+ url: part.url,
370
+ title: part.title,
371
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
372
+ };
373
+ }
374
+ if (sendSources && part.sourceType === "document") {
375
+ return {
376
+ type: "source-document",
377
+ sourceId: part.id,
378
+ mediaType: part.mediaType,
379
+ title: part.title,
380
+ filename: part.filename,
381
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
382
+ };
383
+ }
283
384
  return;
284
385
  }
285
386
  case "tool-input-start": {
@@ -337,6 +438,14 @@ function convertFullStreamChunkToUIMessageStream({
337
438
  toolCallId: part.toolCallId,
338
439
  payload: part.output
339
440
  };
441
+ } else if (isDataChunkType(part.output)) {
442
+ if (!("data" in part.output)) {
443
+ throw new Error(
444
+ `UI Messages require a data property when using data- prefixed chunks
445
+ ${JSON.stringify(part)}`
446
+ );
447
+ }
448
+ return part.output;
340
449
  }
341
450
  return;
342
451
  }
@@ -362,21 +471,23 @@ function convertFullStreamChunkToUIMessageStream({
362
471
  return { type: "finish-step" };
363
472
  }
364
473
  case "start": {
365
- {
474
+ if (sendStart) {
366
475
  return {
367
476
  type: "start",
368
477
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
369
478
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
370
479
  };
371
480
  }
481
+ return;
372
482
  }
373
483
  case "finish": {
374
- {
484
+ if (sendFinish) {
375
485
  return {
376
486
  type: "finish",
377
487
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
378
488
  };
379
489
  }
490
+ return;
380
491
  }
381
492
  case "abort": {
382
493
  return part;
@@ -403,7 +514,10 @@ function convertFullStreamChunkToUIMessageStream({
403
514
  }
404
515
 
405
516
  // src/transformers.ts
406
- function WorkflowStreamToAISDKTransformer() {
517
+ var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
518
+ function WorkflowStreamToAISDKTransformer({
519
+ includeTextStreamParts
520
+ } = {}) {
407
521
  const bufferedWorkflows = /* @__PURE__ */ new Map();
408
522
  return new TransformStream({
409
523
  start(controller) {
@@ -417,7 +531,7 @@ function WorkflowStreamToAISDKTransformer() {
417
531
  });
418
532
  },
419
533
  transform(chunk, controller) {
420
- const transformed = transformWorkflow(chunk, bufferedWorkflows);
534
+ const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
421
535
  if (transformed) controller.enqueue(transformed);
422
536
  }
423
537
  });
@@ -441,20 +555,37 @@ function AgentNetworkToAISDKTransformer() {
441
555
  }
442
556
  });
443
557
  }
444
- function AgentStreamToAISDKTransformer() {
558
+ function AgentStreamToAISDKTransformer({
559
+ lastMessageId,
560
+ sendStart,
561
+ sendFinish,
562
+ sendReasoning,
563
+ sendSources,
564
+ messageMetadata,
565
+ onError
566
+ }) {
445
567
  let bufferedSteps = /* @__PURE__ */ new Map();
568
+ let tripwireOccurred = false;
569
+ let finishEventSent = false;
446
570
  return new TransformStream({
447
571
  transform(chunk, controller) {
572
+ if (chunk.type === "tripwire") {
573
+ tripwireOccurred = true;
574
+ }
575
+ if (chunk.type === "finish") {
576
+ finishEventSent = true;
577
+ }
448
578
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
449
579
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
450
580
  part,
451
- sendReasoning: false,
452
- sendSources: false,
453
- sendStart: true,
454
- sendFinish: true,
455
- responseMessageId: chunk.runId,
456
- onError() {
457
- return "Error";
581
+ sendReasoning,
582
+ sendSources,
583
+ messageMetadataValue: messageMetadata?.({ part }),
584
+ sendStart,
585
+ sendFinish,
586
+ responseMessageId: lastMessageId,
587
+ onError(error) {
588
+ return onError ? onError(error) : safeParseErrorObject(error);
458
589
  }
459
590
  });
460
591
  if (transformedChunk) {
@@ -474,6 +605,14 @@ function AgentStreamToAISDKTransformer() {
474
605
  controller.enqueue(transformedChunk);
475
606
  }
476
607
  }
608
+ },
609
+ flush(controller) {
610
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
611
+ controller.enqueue({
612
+ type: "finish",
613
+ finishReason: "other"
614
+ });
615
+ }
477
616
  }
478
617
  });
479
618
  }
@@ -613,7 +752,7 @@ function transformAgent(payload, bufferedSteps) {
613
752
  }
614
753
  return null;
615
754
  }
616
- function transformWorkflow(payload, bufferedWorkflows, isNested) {
755
+ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
617
756
  switch (payload.type) {
618
757
  case "workflow-start":
619
758
  bufferedWorkflows.set(payload.runId, {
@@ -636,7 +775,9 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
636
775
  name: payload.payload.id,
637
776
  status: payload.payload.status,
638
777
  input: payload.payload.payload ?? null,
639
- output: null
778
+ output: null,
779
+ suspendPayload: null,
780
+ resumePayload: null
640
781
  };
641
782
  bufferedWorkflows.set(payload.runId, current);
642
783
  return {
@@ -669,6 +810,27 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
669
810
  }
670
811
  };
671
812
  }
813
+ case "workflow-step-suspended": {
814
+ const current = bufferedWorkflows.get(payload.runId);
815
+ if (!current) return null;
816
+ current.steps[payload.payload.id] = {
817
+ ...current.steps[payload.payload.id],
818
+ status: payload.payload.status,
819
+ suspendPayload: payload.payload.suspendPayload ?? null,
820
+ resumePayload: payload.payload.resumePayload ?? null,
821
+ output: null
822
+ };
823
+ return {
824
+ type: isNested ? "data-tool-workflow" : "data-workflow",
825
+ id: payload.runId,
826
+ data: {
827
+ name: current.name,
828
+ status: "suspended",
829
+ steps: current.steps,
830
+ output: null
831
+ }
832
+ };
833
+ }
672
834
  case "workflow-finish": {
673
835
  const current = bufferedWorkflows.get(payload.runId);
674
836
  if (!current) return null;
@@ -683,6 +845,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
683
845
  }
684
846
  };
685
847
  }
848
+ case "workflow-step-output": {
849
+ const output = payload.payload.output;
850
+ if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
851
+ const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
852
+ const transformedChunk = convertFullStreamChunkToUIMessageStream({
853
+ part,
854
+ onError(error) {
855
+ return safeParseErrorObject(error);
856
+ }
857
+ });
858
+ return transformedChunk;
859
+ }
860
+ if (output && isDataChunkType(output)) {
861
+ if (!("data" in output)) {
862
+ throw new Error(
863
+ `UI Messages require a data property when using data- prefixed chunks
864
+ ${JSON.stringify(output)}`
865
+ );
866
+ }
867
+ return output;
868
+ }
869
+ return null;
870
+ }
686
871
  default: {
687
872
  if (isDataChunkType(payload)) {
688
873
  if (!("data" in payload)) {
@@ -702,12 +887,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
702
887
  case "routing-agent-start": {
703
888
  if (!bufferedNetworks.has(payload.runId)) {
704
889
  bufferedNetworks.set(payload.runId, {
705
- name: payload.payload.agentId,
890
+ name: payload.payload.networkId,
706
891
  steps: [],
707
892
  usage: null,
708
893
  output: null
709
894
  });
710
895
  }
896
+ const current = bufferedNetworks.get(payload.runId);
897
+ current.steps.push({
898
+ id: payload.payload.runId,
899
+ name: payload.payload.agentId,
900
+ status: "running",
901
+ iteration: payload.payload.inputData.iteration,
902
+ input: {
903
+ task: payload.payload.inputData.task,
904
+ threadId: payload.payload.inputData.threadId,
905
+ threadResourceId: payload.payload.inputData.threadResourceId
906
+ },
907
+ output: "",
908
+ task: null,
909
+ suspendPayload: null,
910
+ resumePayload: null,
911
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
912
+ });
711
913
  return {
712
914
  type: isNested ? "data-tool-network" : "data-network",
713
915
  id: payload.runId,
@@ -738,12 +940,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
738
940
  };
739
941
  }
740
942
  case "agent-execution-start": {
741
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
943
+ const current = bufferedNetworks.get(payload.runId);
944
+ if (!current) return null;
742
945
  current.steps.push({
946
+ id: payload.payload.runId,
743
947
  name: payload.payload.agentId,
744
948
  status: "running",
745
- input: payload.payload.args || null,
746
- output: null
949
+ iteration: payload.payload.args?.iteration ?? 0,
950
+ input: { prompt: payload.payload.args?.prompt ?? "" },
951
+ output: null,
952
+ task: null,
953
+ suspendPayload: null,
954
+ resumePayload: null,
955
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
747
956
  });
748
957
  bufferedNetworks.set(payload.runId, current);
749
958
  return {
@@ -756,12 +965,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
756
965
  };
757
966
  }
758
967
  case "workflow-execution-start": {
759
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
968
+ const current = bufferedNetworks.get(payload.runId);
969
+ if (!current) return null;
760
970
  current.steps.push({
761
- name: payload.payload.name,
971
+ id: payload.payload.runId,
972
+ name: payload.payload.workflowId,
762
973
  status: "running",
763
- input: payload.payload.args || null,
764
- output: null
974
+ iteration: payload.payload.args?.iteration ?? 0,
975
+ input: { prompt: payload.payload.args?.prompt ?? "" },
976
+ output: null,
977
+ task: null,
978
+ suspendPayload: null,
979
+ resumePayload: null,
980
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
765
981
  });
766
982
  bufferedNetworks.set(payload.runId, current);
767
983
  return {
@@ -774,12 +990,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
774
990
  };
775
991
  }
776
992
  case "tool-execution-start": {
777
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
993
+ const current = bufferedNetworks.get(payload.runId);
994
+ if (!current) return null;
778
995
  current.steps.push({
996
+ id: payload.payload.args.toolCallId,
779
997
  name: payload.payload.args?.toolName,
780
998
  status: "running",
999
+ iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
1000
+ task: {
1001
+ id: payload.payload.args?.toolName
1002
+ },
781
1003
  input: payload.payload.args?.args || null,
782
- output: null
1004
+ output: null,
1005
+ suspendPayload: null,
1006
+ resumePayload: null,
1007
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
783
1008
  });
784
1009
  bufferedNetworks.set(payload.runId, current);
785
1010
  return {
@@ -794,12 +1019,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
794
1019
  case "agent-execution-end": {
795
1020
  const current = bufferedNetworks.get(payload.runId);
796
1021
  if (!current) return null;
797
- current.steps.push({
798
- name: payload.payload.agentId,
799
- status: "success",
800
- input: null,
801
- output: payload.payload.result
802
- });
1022
+ const stepId = payload.payload.runId;
1023
+ const step = current.steps.find((step2) => step2.id === stepId);
1024
+ if (!step) {
1025
+ return null;
1026
+ }
1027
+ step.status = "success";
1028
+ step.output = payload.payload.result;
803
1029
  return {
804
1030
  type: isNested ? "data-tool-network" : "data-network",
805
1031
  id: payload.runId,
@@ -814,12 +1040,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
814
1040
  case "tool-execution-end": {
815
1041
  const current = bufferedNetworks.get(payload.runId);
816
1042
  if (!current) return null;
817
- current.steps.push({
818
- name: payload.payload.toolName,
819
- status: "success",
820
- input: null,
821
- output: payload.payload.result
822
- });
1043
+ const stepId = payload.payload.toolCallId;
1044
+ const step = current.steps.find((step2) => step2.id === stepId);
1045
+ if (!step) {
1046
+ return null;
1047
+ }
1048
+ step.status = "success";
1049
+ step.output = payload.payload.result;
823
1050
  return {
824
1051
  type: isNested ? "data-tool-network" : "data-network",
825
1052
  id: payload.runId,
@@ -833,12 +1060,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
833
1060
  case "workflow-execution-end": {
834
1061
  const current = bufferedNetworks.get(payload.runId);
835
1062
  if (!current) return null;
836
- current.steps.push({
837
- name: payload.payload.name,
838
- status: "success",
839
- input: null,
840
- output: payload.payload.result
841
- });
1063
+ const stepId = payload.payload.runId;
1064
+ const step = current.steps.find((step2) => step2.id === stepId);
1065
+ if (!step) {
1066
+ return null;
1067
+ }
1068
+ step.status = "success";
1069
+ step.output = payload.payload.result;
842
1070
  return {
843
1071
  type: isNested ? "data-tool-network" : "data-network",
844
1072
  id: payload.runId,
@@ -853,12 +1081,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
853
1081
  case "routing-agent-end": {
854
1082
  const current = bufferedNetworks.get(payload.runId);
855
1083
  if (!current) return null;
1084
+ const stepId = payload.payload.runId;
1085
+ const step = current.steps.find((step2) => step2.id === stepId);
1086
+ if (!step) {
1087
+ return null;
1088
+ }
1089
+ step.status = "success";
1090
+ step.task = {
1091
+ id: payload.payload.primitiveId,
1092
+ type: payload.payload.primitiveType,
1093
+ name: payload.payload.task,
1094
+ reason: payload.payload.selectionReason
1095
+ };
1096
+ step.output = payload.payload.result;
856
1097
  return {
857
1098
  type: isNested ? "data-tool-network" : "data-network",
858
1099
  id: payload.runId,
859
1100
  data: {
860
1101
  ...current,
861
- status: "finished",
862
1102
  usage: payload.payload?.usage ?? current.usage,
863
1103
  output: payload.payload?.result ?? current.output
864
1104
  }
@@ -892,6 +1132,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
892
1132
  };
893
1133
  }
894
1134
  default: {
1135
+ if (payload.type.startsWith("agent-execution-event-")) {
1136
+ const stepId = payload.payload.runId;
1137
+ const current = bufferedNetworks.get(payload.runId);
1138
+ if (!current) return null;
1139
+ const step = current.steps.find((step2) => step2.id === stepId);
1140
+ if (!step) {
1141
+ return null;
1142
+ }
1143
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1144
+ const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1145
+ if (result) {
1146
+ const { request, response, ...data } = result.data;
1147
+ step.task = data;
1148
+ }
1149
+ }
1150
+ if (payload.type.startsWith("workflow-execution-event-")) {
1151
+ const stepId = payload.payload.runId;
1152
+ const current = bufferedNetworks.get(payload.runId);
1153
+ if (!current) return null;
1154
+ const step = current.steps.find((step2) => step2.id === stepId);
1155
+ if (!step) {
1156
+ return null;
1157
+ }
1158
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1159
+ const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1160
+ if (result && "data" in result) {
1161
+ const data = result.data;
1162
+ step.task = data;
1163
+ if (data.name && step.task) {
1164
+ step.task.id = data.name;
1165
+ }
1166
+ }
1167
+ }
895
1168
  if (isDataChunkType(payload)) {
896
1169
  if (!("data" in payload)) {
897
1170
  throw new Error(
@@ -899,7 +1172,28 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
899
1172
  ${JSON.stringify(payload)}`
900
1173
  );
901
1174
  }
902
- return payload;
1175
+ const { type, data } = payload;
1176
+ return { type, data };
1177
+ }
1178
+ if (isAgentExecutionDataChunkType(payload)) {
1179
+ if (!("data" in payload.payload)) {
1180
+ throw new Error(
1181
+ `UI Messages require a data property when using data- prefixed chunks
1182
+ ${JSON.stringify(payload)}`
1183
+ );
1184
+ }
1185
+ const { type, data } = payload.payload;
1186
+ return { type, data };
1187
+ }
1188
+ if (isWorkflowExecutionDataChunkType(payload)) {
1189
+ if (!("data" in payload.payload)) {
1190
+ throw new Error(
1191
+ `UI Messages require a data property when using data- prefixed chunks
1192
+ ${JSON.stringify(payload)}`
1193
+ );
1194
+ }
1195
+ const { type, data } = payload.payload;
1196
+ return { type, data };
903
1197
  }
904
1198
  return null;
905
1199
  }
@@ -907,23 +1201,44 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
907
1201
  }
908
1202
 
909
1203
  // src/to-ai-sdk-format.ts
910
- function toAISdkFormat(stream, options = { from: "agent" }) {
1204
+ function toAISdkFormat(stream, options = {
1205
+ from: "agent",
1206
+ sendStart: true,
1207
+ sendFinish: true
1208
+ }) {
911
1209
  const from = options?.from;
912
1210
  if (from === "workflow") {
913
- return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
1211
+ const includeTextStreamParts = options?.includeTextStreamParts ?? false;
1212
+ return stream.pipeThrough(
1213
+ WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
1214
+ );
914
1215
  }
915
1216
  if (from === "network") {
916
1217
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
917
1218
  }
918
1219
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
919
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer());
1220
+ return agentReadable.pipeThrough(
1221
+ AgentStreamToAISDKTransformer({
1222
+ lastMessageId: options?.lastMessageId,
1223
+ sendStart: options?.sendStart,
1224
+ sendFinish: options?.sendFinish,
1225
+ sendReasoning: options?.sendReasoning,
1226
+ sendSources: options?.sendSources,
1227
+ messageMetadata: options?.messageMetadata,
1228
+ onError: options?.onError
1229
+ })
1230
+ );
920
1231
  }
921
1232
 
922
1233
  // src/chat-route.ts
923
1234
  function chatRoute({
924
1235
  path = "/chat/:agentId",
925
1236
  agent,
926
- defaultOptions
1237
+ defaultOptions,
1238
+ sendStart = true,
1239
+ sendFinish = true,
1240
+ sendReasoning = false,
1241
+ sendSources = false
927
1242
  }) {
928
1243
  if (!agent && !path.includes("/:agentId")) {
929
1244
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1024,7 +1339,7 @@ function chatRoute({
1024
1339
  handler: async (c) => {
1025
1340
  const { messages, ...rest } = await c.req.json();
1026
1341
  const mastra = c.get("mastra");
1027
- const requestContext = c.get("requestContext");
1342
+ const runtimeContext = c.get("runtimeContext");
1028
1343
  let agentToUse = agent;
1029
1344
  if (!agent) {
1030
1345
  const agentId = c.req.param("agentId");
@@ -1035,25 +1350,36 @@ function chatRoute({
1035
1350
  `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
1036
1351
  );
1037
1352
  }
1038
- if (requestContext && defaultOptions?.requestContext) {
1039
- mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
1353
+ if (runtimeContext && defaultOptions?.runtimeContext) {
1354
+ mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
1040
1355
  }
1041
1356
  if (!agentToUse) {
1042
1357
  throw new Error("Agent ID is required");
1043
1358
  }
1044
- const agentObj = mastra.getAgent(agentToUse);
1359
+ const agentObj = mastra.getAgentById(agentToUse);
1045
1360
  if (!agentObj) {
1046
1361
  throw new Error(`Agent ${agentToUse} not found`);
1047
1362
  }
1048
1363
  const result = await agentObj.stream(messages, {
1049
1364
  ...defaultOptions,
1050
1365
  ...rest,
1051
- requestContext: requestContext || defaultOptions?.requestContext
1366
+ runtimeContext: runtimeContext || defaultOptions?.runtimeContext
1052
1367
  });
1368
+ let lastMessageId;
1369
+ if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
1370
+ lastMessageId = messages[messages.length - 1].id;
1371
+ }
1053
1372
  const uiMessageStream = createUIMessageStream({
1054
1373
  originalMessages: messages,
1055
1374
  execute: async ({ writer }) => {
1056
- for await (const part of toAISdkFormat(result, { from: "agent" })) {
1375
+ for await (const part of toAISdkFormat(result, {
1376
+ from: "agent",
1377
+ lastMessageId,
1378
+ sendStart,
1379
+ sendFinish,
1380
+ sendReasoning,
1381
+ sendSources
1382
+ })) {
1057
1383
  writer.write(part);
1058
1384
  }
1059
1385
  }
@@ -1066,7 +1392,8 @@ function chatRoute({
1066
1392
  }
1067
1393
  function workflowRoute({
1068
1394
  path = "/api/workflows/:workflowId/stream",
1069
- workflow
1395
+ workflow,
1396
+ includeTextStreamParts = false
1070
1397
  }) {
1071
1398
  if (!workflow && !path.includes("/:workflowId")) {
1072
1399
  throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
@@ -1093,9 +1420,13 @@ function workflowRoute({
1093
1420
  schema: {
1094
1421
  type: "object",
1095
1422
  properties: {
1423
+ runId: { type: "string" },
1424
+ resourceId: { type: "string" },
1096
1425
  inputData: { type: "object", additionalProperties: true },
1097
- requestContext: { type: "object", additionalProperties: true },
1098
- tracingOptions: { type: "object", additionalProperties: true }
1426
+ resumeData: { type: "object", additionalProperties: true },
1427
+ runtimeContext: { type: "object", additionalProperties: true },
1428
+ tracingOptions: { type: "object", additionalProperties: true },
1429
+ step: { type: "string" }
1099
1430
  }
1100
1431
  }
1101
1432
  }
@@ -1113,8 +1444,9 @@ function workflowRoute({
1113
1444
  }
1114
1445
  },
1115
1446
  handler: async (c) => {
1116
- const { inputData, ...rest } = await c.req.json();
1447
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1117
1448
  const mastra = c.get("mastra");
1449
+ const runtimeContext = c.get("runtimeContext");
1118
1450
  let workflowToUse = workflow;
1119
1451
  if (!workflow) {
1120
1452
  const workflowId = c.req.param("workflowId");
@@ -1128,15 +1460,20 @@ function workflowRoute({
1128
1460
  if (!workflowToUse) {
1129
1461
  throw new Error("Workflow ID is required");
1130
1462
  }
1131
- const workflowObj = mastra.getWorkflow(workflowToUse);
1463
+ const workflowObj = mastra.getWorkflowById(workflowToUse);
1132
1464
  if (!workflowObj) {
1133
1465
  throw new Error(`Workflow ${workflowToUse} not found`);
1134
1466
  }
1135
- const run = await workflowObj.createRunAsync();
1136
- const stream = run.streamVNext({ inputData, ...rest });
1467
+ if (runtimeContext && rest.runtimeContext) {
1468
+ mastra.getLogger()?.warn(
1469
+ `"runtimeContext" from the request body will be ignored because "runtimeContext" is already set in the route options.`
1470
+ );
1471
+ }
1472
+ const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
1473
+ const stream = resumeData ? run.resumeStream({ resumeData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext }) : run.stream({ inputData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext });
1137
1474
  const uiMessageStream = createUIMessageStream({
1138
1475
  execute: async ({ writer }) => {
1139
- for await (const part of toAISdkFormat(stream, { from: "workflow" })) {
1476
+ for await (const part of toAISdkFormat(stream, { from: "workflow", includeTextStreamParts })) {
1140
1477
  writer.write(part);
1141
1478
  }
1142
1479
  }
@@ -1176,12 +1513,13 @@ function networkRoute({
1176
1513
  type: "object",
1177
1514
  properties: {
1178
1515
  messages: { type: "array", items: { type: "object" } },
1179
- requestContext: { type: "object", additionalProperties: true },
1516
+ runtimeContext: { type: "object", additionalProperties: true },
1180
1517
  runId: { type: "string" },
1181
1518
  maxSteps: { type: "number" },
1182
1519
  threadId: { type: "string" },
1183
1520
  resourceId: { type: "string" },
1184
1521
  modelSettings: { type: "object", additionalProperties: true },
1522
+ telemetry: { type: "object", additionalProperties: true },
1185
1523
  tools: { type: "array", items: { type: "object" } }
1186
1524
  },
1187
1525
  required: ["messages"]
@@ -1220,7 +1558,7 @@ function networkRoute({
1220
1558
  if (!agentToUse) {
1221
1559
  throw new Error("Agent ID is required");
1222
1560
  }
1223
- const agentObj = mastra.getAgent(agentToUse);
1561
+ const agentObj = mastra.getAgentById(agentToUse);
1224
1562
  if (!agentObj) {
1225
1563
  throw new Error(`Agent ${agentToUse} not found`);
1226
1564
  }