@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.cjs CHANGED
@@ -10,6 +10,51 @@ 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
+ };
38
+ function safeParseErrorObject(obj) {
39
+ if (typeof obj !== "object" || obj === null) {
40
+ return String(obj);
41
+ }
42
+ try {
43
+ const stringified = JSON.stringify(obj);
44
+ if (stringified === "{}") {
45
+ return String(obj);
46
+ }
47
+ return stringified;
48
+ } catch {
49
+ return String(obj);
50
+ }
51
+ }
52
+ var isAgentExecutionDataChunkType = (chunk) => {
53
+ 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-");
54
+ };
55
+ var isWorkflowExecutionDataChunkType = (chunk) => {
56
+ 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-");
57
+ };
13
58
 
14
59
  // src/helpers.ts
15
60
  function convertMastraChunkToAISDKv5({
@@ -120,6 +165,28 @@ function convertMastraChunkToAISDKv5({
120
165
  toolName: chunk.payload.toolName,
121
166
  input: chunk.payload.args
122
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
+ };
123
190
  case "tool-call-input-streaming-start":
124
191
  return {
125
192
  type: "tool-input-start",
@@ -210,6 +277,13 @@ function convertMastraChunkToAISDKv5({
210
277
  type: "object",
211
278
  object: chunk.object
212
279
  };
280
+ case "tripwire":
281
+ return {
282
+ type: "data-tripwire",
283
+ data: {
284
+ tripwireReason: chunk.payload.tripwireReason
285
+ }
286
+ };
213
287
  default:
214
288
  if (chunk.type && "payload" in chunk && chunk.payload) {
215
289
  return {
@@ -265,6 +339,14 @@ function convertFullStreamChunkToUIMessageStream({
265
339
  };
266
340
  }
267
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
+ }
268
350
  return;
269
351
  }
270
352
  case "reasoning-end": {
@@ -282,6 +364,25 @@ function convertFullStreamChunkToUIMessageStream({
282
364
  };
283
365
  }
284
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
+ }
285
386
  return;
286
387
  }
287
388
  case "tool-input-start": {
@@ -339,6 +440,14 @@ function convertFullStreamChunkToUIMessageStream({
339
440
  toolCallId: part.toolCallId,
340
441
  payload: part.output
341
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;
342
451
  }
343
452
  return;
344
453
  }
@@ -364,21 +473,23 @@ function convertFullStreamChunkToUIMessageStream({
364
473
  return { type: "finish-step" };
365
474
  }
366
475
  case "start": {
367
- {
476
+ if (sendStart) {
368
477
  return {
369
478
  type: "start",
370
479
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
371
480
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
372
481
  };
373
482
  }
483
+ return;
374
484
  }
375
485
  case "finish": {
376
- {
486
+ if (sendFinish) {
377
487
  return {
378
488
  type: "finish",
379
489
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
380
490
  };
381
491
  }
492
+ return;
382
493
  }
383
494
  case "abort": {
384
495
  return part;
@@ -405,7 +516,10 @@ function convertFullStreamChunkToUIMessageStream({
405
516
  }
406
517
 
407
518
  // src/transformers.ts
408
- function WorkflowStreamToAISDKTransformer() {
519
+ var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
520
+ function WorkflowStreamToAISDKTransformer({
521
+ includeTextStreamParts
522
+ } = {}) {
409
523
  const bufferedWorkflows = /* @__PURE__ */ new Map();
410
524
  return new TransformStream({
411
525
  start(controller) {
@@ -419,7 +533,7 @@ function WorkflowStreamToAISDKTransformer() {
419
533
  });
420
534
  },
421
535
  transform(chunk, controller) {
422
- const transformed = transformWorkflow(chunk, bufferedWorkflows);
536
+ const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
423
537
  if (transformed) controller.enqueue(transformed);
424
538
  }
425
539
  });
@@ -443,20 +557,37 @@ function AgentNetworkToAISDKTransformer() {
443
557
  }
444
558
  });
445
559
  }
446
- function AgentStreamToAISDKTransformer() {
560
+ function AgentStreamToAISDKTransformer({
561
+ lastMessageId,
562
+ sendStart,
563
+ sendFinish,
564
+ sendReasoning,
565
+ sendSources,
566
+ messageMetadata,
567
+ onError
568
+ }) {
447
569
  let bufferedSteps = /* @__PURE__ */ new Map();
570
+ let tripwireOccurred = false;
571
+ let finishEventSent = false;
448
572
  return new TransformStream({
449
573
  transform(chunk, controller) {
574
+ if (chunk.type === "tripwire") {
575
+ tripwireOccurred = true;
576
+ }
577
+ if (chunk.type === "finish") {
578
+ finishEventSent = true;
579
+ }
450
580
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
451
581
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
452
582
  part,
453
- sendReasoning: false,
454
- sendSources: false,
455
- sendStart: true,
456
- sendFinish: true,
457
- responseMessageId: chunk.runId,
458
- onError() {
459
- return "Error";
583
+ sendReasoning,
584
+ sendSources,
585
+ messageMetadataValue: messageMetadata?.({ part }),
586
+ sendStart,
587
+ sendFinish,
588
+ responseMessageId: lastMessageId,
589
+ onError(error) {
590
+ return onError ? onError(error) : safeParseErrorObject(error);
460
591
  }
461
592
  });
462
593
  if (transformedChunk) {
@@ -476,6 +607,14 @@ function AgentStreamToAISDKTransformer() {
476
607
  controller.enqueue(transformedChunk);
477
608
  }
478
609
  }
610
+ },
611
+ flush(controller) {
612
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
613
+ controller.enqueue({
614
+ type: "finish",
615
+ finishReason: "other"
616
+ });
617
+ }
479
618
  }
480
619
  });
481
620
  }
@@ -615,7 +754,7 @@ function transformAgent(payload, bufferedSteps) {
615
754
  }
616
755
  return null;
617
756
  }
618
- function transformWorkflow(payload, bufferedWorkflows, isNested) {
757
+ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
619
758
  switch (payload.type) {
620
759
  case "workflow-start":
621
760
  bufferedWorkflows.set(payload.runId, {
@@ -638,7 +777,9 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
638
777
  name: payload.payload.id,
639
778
  status: payload.payload.status,
640
779
  input: payload.payload.payload ?? null,
641
- output: null
780
+ output: null,
781
+ suspendPayload: null,
782
+ resumePayload: null
642
783
  };
643
784
  bufferedWorkflows.set(payload.runId, current);
644
785
  return {
@@ -671,6 +812,27 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
671
812
  }
672
813
  };
673
814
  }
815
+ case "workflow-step-suspended": {
816
+ const current = bufferedWorkflows.get(payload.runId);
817
+ if (!current) return null;
818
+ current.steps[payload.payload.id] = {
819
+ ...current.steps[payload.payload.id],
820
+ status: payload.payload.status,
821
+ suspendPayload: payload.payload.suspendPayload ?? null,
822
+ resumePayload: payload.payload.resumePayload ?? null,
823
+ output: null
824
+ };
825
+ return {
826
+ type: isNested ? "data-tool-workflow" : "data-workflow",
827
+ id: payload.runId,
828
+ data: {
829
+ name: current.name,
830
+ status: "suspended",
831
+ steps: current.steps,
832
+ output: null
833
+ }
834
+ };
835
+ }
674
836
  case "workflow-finish": {
675
837
  const current = bufferedWorkflows.get(payload.runId);
676
838
  if (!current) return null;
@@ -685,6 +847,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
685
847
  }
686
848
  };
687
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
+ }
688
873
  default: {
689
874
  if (isDataChunkType(payload)) {
690
875
  if (!("data" in payload)) {
@@ -704,12 +889,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
704
889
  case "routing-agent-start": {
705
890
  if (!bufferedNetworks.has(payload.runId)) {
706
891
  bufferedNetworks.set(payload.runId, {
707
- name: payload.payload.agentId,
892
+ name: payload.payload.networkId,
708
893
  steps: [],
709
894
  usage: null,
710
895
  output: null
711
896
  });
712
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
+ });
713
915
  return {
714
916
  type: isNested ? "data-tool-network" : "data-network",
715
917
  id: payload.runId,
@@ -740,12 +942,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
740
942
  };
741
943
  }
742
944
  case "agent-execution-start": {
743
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
945
+ const current = bufferedNetworks.get(payload.runId);
946
+ if (!current) return null;
744
947
  current.steps.push({
948
+ id: payload.payload.runId,
745
949
  name: payload.payload.agentId,
746
950
  status: "running",
747
- input: payload.payload.args || null,
748
- output: null
951
+ iteration: payload.payload.args?.iteration ?? 0,
952
+ input: { prompt: payload.payload.args?.prompt ?? "" },
953
+ output: null,
954
+ task: null,
955
+ suspendPayload: null,
956
+ resumePayload: null,
957
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
749
958
  });
750
959
  bufferedNetworks.set(payload.runId, current);
751
960
  return {
@@ -758,12 +967,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
758
967
  };
759
968
  }
760
969
  case "workflow-execution-start": {
761
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
970
+ const current = bufferedNetworks.get(payload.runId);
971
+ if (!current) return null;
762
972
  current.steps.push({
763
- name: payload.payload.name,
973
+ id: payload.payload.runId,
974
+ name: payload.payload.workflowId,
764
975
  status: "running",
765
- input: payload.payload.args || null,
766
- output: null
976
+ iteration: payload.payload.args?.iteration ?? 0,
977
+ input: { prompt: payload.payload.args?.prompt ?? "" },
978
+ output: null,
979
+ task: null,
980
+ suspendPayload: null,
981
+ resumePayload: null,
982
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
767
983
  });
768
984
  bufferedNetworks.set(payload.runId, current);
769
985
  return {
@@ -776,12 +992,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
776
992
  };
777
993
  }
778
994
  case "tool-execution-start": {
779
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
995
+ const current = bufferedNetworks.get(payload.runId);
996
+ if (!current) return null;
780
997
  current.steps.push({
998
+ id: payload.payload.args.toolCallId,
781
999
  name: payload.payload.args?.toolName,
782
1000
  status: "running",
1001
+ iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
1002
+ task: {
1003
+ id: payload.payload.args?.toolName
1004
+ },
783
1005
  input: payload.payload.args?.args || null,
784
- output: null
1006
+ output: null,
1007
+ suspendPayload: null,
1008
+ resumePayload: null,
1009
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
785
1010
  });
786
1011
  bufferedNetworks.set(payload.runId, current);
787
1012
  return {
@@ -796,12 +1021,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
796
1021
  case "agent-execution-end": {
797
1022
  const current = bufferedNetworks.get(payload.runId);
798
1023
  if (!current) return null;
799
- current.steps.push({
800
- name: payload.payload.agentId,
801
- status: "success",
802
- input: null,
803
- output: payload.payload.result
804
- });
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;
805
1031
  return {
806
1032
  type: isNested ? "data-tool-network" : "data-network",
807
1033
  id: payload.runId,
@@ -816,12 +1042,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
816
1042
  case "tool-execution-end": {
817
1043
  const current = bufferedNetworks.get(payload.runId);
818
1044
  if (!current) return null;
819
- current.steps.push({
820
- name: payload.payload.toolName,
821
- status: "success",
822
- input: null,
823
- output: payload.payload.result
824
- });
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;
825
1052
  return {
826
1053
  type: isNested ? "data-tool-network" : "data-network",
827
1054
  id: payload.runId,
@@ -835,12 +1062,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
835
1062
  case "workflow-execution-end": {
836
1063
  const current = bufferedNetworks.get(payload.runId);
837
1064
  if (!current) return null;
838
- current.steps.push({
839
- name: payload.payload.name,
840
- status: "success",
841
- input: null,
842
- output: payload.payload.result
843
- });
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;
844
1072
  return {
845
1073
  type: isNested ? "data-tool-network" : "data-network",
846
1074
  id: payload.runId,
@@ -855,12 +1083,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
855
1083
  case "routing-agent-end": {
856
1084
  const current = bufferedNetworks.get(payload.runId);
857
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;
858
1099
  return {
859
1100
  type: isNested ? "data-tool-network" : "data-network",
860
1101
  id: payload.runId,
861
1102
  data: {
862
1103
  ...current,
863
- status: "finished",
864
1104
  usage: payload.payload?.usage ?? current.usage,
865
1105
  output: payload.payload?.result ?? current.output
866
1106
  }
@@ -894,6 +1134,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
894
1134
  };
895
1135
  }
896
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
+ }
897
1170
  if (isDataChunkType(payload)) {
898
1171
  if (!("data" in payload)) {
899
1172
  throw new Error(
@@ -901,7 +1174,28 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
901
1174
  ${JSON.stringify(payload)}`
902
1175
  );
903
1176
  }
904
- return payload;
1177
+ const { type, data } = payload;
1178
+ return { type, data };
1179
+ }
1180
+ if (isAgentExecutionDataChunkType(payload)) {
1181
+ if (!("data" in payload.payload)) {
1182
+ throw new Error(
1183
+ `UI Messages require a data property when using data- prefixed chunks
1184
+ ${JSON.stringify(payload)}`
1185
+ );
1186
+ }
1187
+ const { type, data } = payload.payload;
1188
+ return { type, data };
1189
+ }
1190
+ if (isWorkflowExecutionDataChunkType(payload)) {
1191
+ if (!("data" in payload.payload)) {
1192
+ throw new Error(
1193
+ `UI Messages require a data property when using data- prefixed chunks
1194
+ ${JSON.stringify(payload)}`
1195
+ );
1196
+ }
1197
+ const { type, data } = payload.payload;
1198
+ return { type, data };
905
1199
  }
906
1200
  return null;
907
1201
  }
@@ -909,23 +1203,44 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
909
1203
  }
910
1204
 
911
1205
  // src/to-ai-sdk-format.ts
912
- function toAISdkFormat(stream, options = { from: "agent" }) {
1206
+ function toAISdkFormat(stream, options = {
1207
+ from: "agent",
1208
+ sendStart: true,
1209
+ sendFinish: true
1210
+ }) {
913
1211
  const from = options?.from;
914
1212
  if (from === "workflow") {
915
- return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
1213
+ const includeTextStreamParts = options?.includeTextStreamParts ?? false;
1214
+ return stream.pipeThrough(
1215
+ WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
1216
+ );
916
1217
  }
917
1218
  if (from === "network") {
918
1219
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
919
1220
  }
920
1221
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
921
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer());
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
+ );
922
1233
  }
923
1234
 
924
1235
  // src/chat-route.ts
925
1236
  function chatRoute({
926
1237
  path = "/chat/:agentId",
927
1238
  agent,
928
- defaultOptions
1239
+ defaultOptions,
1240
+ sendStart = true,
1241
+ sendFinish = true,
1242
+ sendReasoning = false,
1243
+ sendSources = false
929
1244
  }) {
930
1245
  if (!agent && !path.includes("/:agentId")) {
931
1246
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1026,7 +1341,7 @@ function chatRoute({
1026
1341
  handler: async (c) => {
1027
1342
  const { messages, ...rest } = await c.req.json();
1028
1343
  const mastra = c.get("mastra");
1029
- const requestContext = c.get("requestContext");
1344
+ const runtimeContext = c.get("runtimeContext");
1030
1345
  let agentToUse = agent;
1031
1346
  if (!agent) {
1032
1347
  const agentId = c.req.param("agentId");
@@ -1037,25 +1352,36 @@ function chatRoute({
1037
1352
  `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
1038
1353
  );
1039
1354
  }
1040
- if (requestContext && defaultOptions?.requestContext) {
1041
- mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
1355
+ if (runtimeContext && defaultOptions?.runtimeContext) {
1356
+ mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
1042
1357
  }
1043
1358
  if (!agentToUse) {
1044
1359
  throw new Error("Agent ID is required");
1045
1360
  }
1046
- const agentObj = mastra.getAgent(agentToUse);
1361
+ const agentObj = mastra.getAgentById(agentToUse);
1047
1362
  if (!agentObj) {
1048
1363
  throw new Error(`Agent ${agentToUse} not found`);
1049
1364
  }
1050
1365
  const result = await agentObj.stream(messages, {
1051
1366
  ...defaultOptions,
1052
1367
  ...rest,
1053
- requestContext: requestContext || defaultOptions?.requestContext
1368
+ runtimeContext: runtimeContext || defaultOptions?.runtimeContext
1054
1369
  });
1370
+ let lastMessageId;
1371
+ if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
1372
+ lastMessageId = messages[messages.length - 1].id;
1373
+ }
1055
1374
  const uiMessageStream = ai.createUIMessageStream({
1056
1375
  originalMessages: messages,
1057
1376
  execute: async ({ writer }) => {
1058
- for await (const part of toAISdkFormat(result, { from: "agent" })) {
1377
+ for await (const part of toAISdkFormat(result, {
1378
+ from: "agent",
1379
+ lastMessageId,
1380
+ sendStart,
1381
+ sendFinish,
1382
+ sendReasoning,
1383
+ sendSources
1384
+ })) {
1059
1385
  writer.write(part);
1060
1386
  }
1061
1387
  }
@@ -1068,7 +1394,8 @@ function chatRoute({
1068
1394
  }
1069
1395
  function workflowRoute({
1070
1396
  path = "/api/workflows/:workflowId/stream",
1071
- workflow
1397
+ workflow,
1398
+ includeTextStreamParts = false
1072
1399
  }) {
1073
1400
  if (!workflow && !path.includes("/:workflowId")) {
1074
1401
  throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
@@ -1095,9 +1422,13 @@ function workflowRoute({
1095
1422
  schema: {
1096
1423
  type: "object",
1097
1424
  properties: {
1425
+ runId: { type: "string" },
1426
+ resourceId: { type: "string" },
1098
1427
  inputData: { type: "object", additionalProperties: true },
1099
- requestContext: { type: "object", additionalProperties: true },
1100
- tracingOptions: { type: "object", additionalProperties: true }
1428
+ resumeData: { type: "object", additionalProperties: true },
1429
+ runtimeContext: { type: "object", additionalProperties: true },
1430
+ tracingOptions: { type: "object", additionalProperties: true },
1431
+ step: { type: "string" }
1101
1432
  }
1102
1433
  }
1103
1434
  }
@@ -1115,8 +1446,9 @@ function workflowRoute({
1115
1446
  }
1116
1447
  },
1117
1448
  handler: async (c) => {
1118
- const { inputData, ...rest } = await c.req.json();
1449
+ const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1119
1450
  const mastra = c.get("mastra");
1451
+ const runtimeContext = c.get("runtimeContext");
1120
1452
  let workflowToUse = workflow;
1121
1453
  if (!workflow) {
1122
1454
  const workflowId = c.req.param("workflowId");
@@ -1130,15 +1462,20 @@ function workflowRoute({
1130
1462
  if (!workflowToUse) {
1131
1463
  throw new Error("Workflow ID is required");
1132
1464
  }
1133
- const workflowObj = mastra.getWorkflow(workflowToUse);
1465
+ const workflowObj = mastra.getWorkflowById(workflowToUse);
1134
1466
  if (!workflowObj) {
1135
1467
  throw new Error(`Workflow ${workflowToUse} not found`);
1136
1468
  }
1137
- const run = await workflowObj.createRunAsync();
1138
- const stream = run.streamVNext({ inputData, ...rest });
1469
+ if (runtimeContext && rest.runtimeContext) {
1470
+ mastra.getLogger()?.warn(
1471
+ `"runtimeContext" from the request body will be ignored because "runtimeContext" is already set in the route options.`
1472
+ );
1473
+ }
1474
+ const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
1475
+ const stream = resumeData ? run.resumeStream({ resumeData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext }) : run.stream({ inputData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext });
1139
1476
  const uiMessageStream = ai.createUIMessageStream({
1140
1477
  execute: async ({ writer }) => {
1141
- for await (const part of toAISdkFormat(stream, { from: "workflow" })) {
1478
+ for await (const part of toAISdkFormat(stream, { from: "workflow", includeTextStreamParts })) {
1142
1479
  writer.write(part);
1143
1480
  }
1144
1481
  }
@@ -1178,12 +1515,13 @@ function networkRoute({
1178
1515
  type: "object",
1179
1516
  properties: {
1180
1517
  messages: { type: "array", items: { type: "object" } },
1181
- requestContext: { type: "object", additionalProperties: true },
1518
+ runtimeContext: { type: "object", additionalProperties: true },
1182
1519
  runId: { type: "string" },
1183
1520
  maxSteps: { type: "number" },
1184
1521
  threadId: { type: "string" },
1185
1522
  resourceId: { type: "string" },
1186
1523
  modelSettings: { type: "object", additionalProperties: true },
1524
+ telemetry: { type: "object", additionalProperties: true },
1187
1525
  tools: { type: "array", items: { type: "object" } }
1188
1526
  },
1189
1527
  required: ["messages"]
@@ -1222,7 +1560,7 @@ function networkRoute({
1222
1560
  if (!agentToUse) {
1223
1561
  throw new Error("Agent ID is required");
1224
1562
  }
1225
- const agentObj = mastra.getAgent(agentToUse);
1563
+ const agentObj = mastra.getAgentById(agentToUse);
1226
1564
  if (!agentObj) {
1227
1565
  throw new Error(`Agent ${agentToUse} not found`);
1228
1566
  }