@mastra/ai-sdk 0.0.0-cloud-storage-adapter-20251106204059 → 0.0.0-cloud-604-map-nested-flow-details-to-side-panel-20251212192149

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
@@ -3,6 +3,9 @@
3
3
  var server = require('@mastra/core/server');
4
4
  var ai = require('ai');
5
5
  var stream = require('@mastra/core/stream');
6
+ var agent = require('@mastra/core/agent');
7
+ var di = require('@mastra/core/di');
8
+ var processors = require('@mastra/core/processors');
6
9
 
7
10
  // src/chat-route.ts
8
11
 
@@ -10,6 +13,35 @@ var stream = require('@mastra/core/stream');
10
13
  var isDataChunkType = (chunk) => {
11
14
  return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
12
15
  };
16
+ var isMastraTextStreamChunk = (chunk) => {
17
+ return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
18
+ "text-start",
19
+ "text-delta",
20
+ "text-end",
21
+ "reasoning-start",
22
+ "reasoning-delta",
23
+ "reasoning-end",
24
+ "file",
25
+ "source",
26
+ "tool-input-start",
27
+ "tool-input-delta",
28
+ "tool-call-approval",
29
+ "tool-call-suspended",
30
+ "tool-call",
31
+ "tool-result",
32
+ "tool-error",
33
+ "error",
34
+ "start-step",
35
+ "finish-step",
36
+ "start",
37
+ "finish",
38
+ "abort",
39
+ "tool-input-end",
40
+ "object",
41
+ "tripwire",
42
+ "raw"
43
+ ].includes(chunk.type);
44
+ };
13
45
  function safeParseErrorObject(obj) {
14
46
  if (typeof obj !== "object" || obj === null) {
15
47
  return String(obj);
@@ -32,6 +64,12 @@ var isWorkflowExecutionDataChunkType = (chunk) => {
32
64
  };
33
65
 
34
66
  // src/helpers.ts
67
+ function toAISDKFinishReason(reason) {
68
+ if (reason === "tripwire" || reason === "retry") {
69
+ return "other";
70
+ }
71
+ return reason;
72
+ }
35
73
  function convertMastraChunkToAISDKv5({
36
74
  chunk,
37
75
  mode = "stream"
@@ -56,7 +94,7 @@ function convertMastraChunkToAISDKv5({
56
94
  case "finish": {
57
95
  return {
58
96
  type: "finish",
59
- finishReason: chunk.payload.stepResult.reason,
97
+ finishReason: toAISDKFinishReason(chunk.payload.stepResult.reason),
60
98
  totalUsage: chunk.payload.output.usage
61
99
  };
62
100
  }
@@ -140,6 +178,28 @@ function convertMastraChunkToAISDKv5({
140
178
  toolName: chunk.payload.toolName,
141
179
  input: chunk.payload.args
142
180
  };
181
+ case "tool-call-approval":
182
+ return {
183
+ type: "data-tool-call-approval",
184
+ id: chunk.payload.toolCallId,
185
+ data: {
186
+ runId: chunk.runId,
187
+ toolCallId: chunk.payload.toolCallId,
188
+ toolName: chunk.payload.toolName,
189
+ args: chunk.payload.args
190
+ }
191
+ };
192
+ case "tool-call-suspended":
193
+ return {
194
+ type: "data-tool-call-suspended",
195
+ id: chunk.payload.toolCallId,
196
+ data: {
197
+ runId: chunk.runId,
198
+ toolCallId: chunk.payload.toolCallId,
199
+ toolName: chunk.payload.toolName,
200
+ suspendPayload: chunk.payload.suspendPayload
201
+ }
202
+ };
143
203
  case "tool-call-input-streaming-start":
144
204
  return {
145
205
  type: "tool-input-start",
@@ -173,7 +233,7 @@ function convertMastraChunkToAISDKv5({
173
233
  ...rest2
174
234
  },
175
235
  usage: chunk.payload.output.usage,
176
- finishReason: chunk.payload.stepResult.reason,
236
+ finishReason: toAISDKFinishReason(chunk.payload.stepResult.reason),
177
237
  providerMetadata
178
238
  };
179
239
  }
@@ -230,6 +290,16 @@ function convertMastraChunkToAISDKv5({
230
290
  type: "object",
231
291
  object: chunk.object
232
292
  };
293
+ case "tripwire":
294
+ return {
295
+ type: "data-tripwire",
296
+ data: {
297
+ reason: chunk.payload.reason,
298
+ retry: chunk.payload.retry,
299
+ metadata: chunk.payload.metadata,
300
+ processorId: chunk.payload.processorId
301
+ }
302
+ };
233
303
  default:
234
304
  if (chunk.type && "payload" in chunk && chunk.payload) {
235
305
  return {
@@ -285,6 +355,14 @@ function convertFullStreamChunkToUIMessageStream({
285
355
  };
286
356
  }
287
357
  case "reasoning-delta": {
358
+ if (sendReasoning) {
359
+ return {
360
+ type: "reasoning-delta",
361
+ id: part.id,
362
+ delta: part.text,
363
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
364
+ };
365
+ }
288
366
  return;
289
367
  }
290
368
  case "reasoning-end": {
@@ -302,6 +380,25 @@ function convertFullStreamChunkToUIMessageStream({
302
380
  };
303
381
  }
304
382
  case "source": {
383
+ if (sendSources && part.sourceType === "url") {
384
+ return {
385
+ type: "source-url",
386
+ sourceId: part.id,
387
+ url: part.url,
388
+ title: part.title,
389
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
390
+ };
391
+ }
392
+ if (sendSources && part.sourceType === "document") {
393
+ return {
394
+ type: "source-document",
395
+ sourceId: part.id,
396
+ mediaType: part.mediaType,
397
+ title: part.title,
398
+ filename: part.filename,
399
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
400
+ };
401
+ }
305
402
  return;
306
403
  }
307
404
  case "tool-input-start": {
@@ -359,6 +456,14 @@ function convertFullStreamChunkToUIMessageStream({
359
456
  toolCallId: part.toolCallId,
360
457
  payload: part.output
361
458
  };
459
+ } else if (isDataChunkType(part.output)) {
460
+ if (!("data" in part.output)) {
461
+ throw new Error(
462
+ `UI Messages require a data property when using data- prefixed chunks
463
+ ${JSON.stringify(part)}`
464
+ );
465
+ }
466
+ return part.output;
362
467
  }
363
468
  return;
364
469
  }
@@ -384,21 +489,23 @@ function convertFullStreamChunkToUIMessageStream({
384
489
  return { type: "finish-step" };
385
490
  }
386
491
  case "start": {
387
- {
492
+ if (sendStart) {
388
493
  return {
389
494
  type: "start",
390
495
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
391
496
  ...responseMessageId != null ? { messageId: responseMessageId } : {}
392
497
  };
393
498
  }
499
+ return;
394
500
  }
395
501
  case "finish": {
396
- {
502
+ if (sendFinish) {
397
503
  return {
398
504
  type: "finish",
399
505
  ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
400
506
  };
401
507
  }
508
+ return;
402
509
  }
403
510
  case "abort": {
404
511
  return part;
@@ -425,7 +532,10 @@ function convertFullStreamChunkToUIMessageStream({
425
532
  }
426
533
 
427
534
  // src/transformers.ts
428
- function WorkflowStreamToAISDKTransformer() {
535
+ var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
536
+ function WorkflowStreamToAISDKTransformer({
537
+ includeTextStreamParts
538
+ } = {}) {
429
539
  const bufferedWorkflows = /* @__PURE__ */ new Map();
430
540
  return new TransformStream({
431
541
  start(controller) {
@@ -439,7 +549,7 @@ function WorkflowStreamToAISDKTransformer() {
439
549
  });
440
550
  },
441
551
  transform(chunk, controller) {
442
- const transformed = transformWorkflow(chunk, bufferedWorkflows);
552
+ const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
443
553
  if (transformed) controller.enqueue(transformed);
444
554
  }
445
555
  });
@@ -463,20 +573,37 @@ function AgentNetworkToAISDKTransformer() {
463
573
  }
464
574
  });
465
575
  }
466
- function AgentStreamToAISDKTransformer(lastMessageId) {
576
+ function AgentStreamToAISDKTransformer({
577
+ lastMessageId,
578
+ sendStart,
579
+ sendFinish,
580
+ sendReasoning,
581
+ sendSources,
582
+ messageMetadata,
583
+ onError
584
+ }) {
467
585
  let bufferedSteps = /* @__PURE__ */ new Map();
586
+ let tripwireOccurred = false;
587
+ let finishEventSent = false;
468
588
  return new TransformStream({
469
589
  transform(chunk, controller) {
590
+ if (chunk.type === "tripwire") {
591
+ tripwireOccurred = true;
592
+ }
593
+ if (chunk.type === "finish") {
594
+ finishEventSent = true;
595
+ }
470
596
  const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
471
597
  const transformedChunk = convertFullStreamChunkToUIMessageStream({
472
598
  part,
473
- sendReasoning: false,
474
- sendSources: false,
475
- sendStart: true,
476
- sendFinish: true,
599
+ sendReasoning,
600
+ sendSources,
601
+ messageMetadataValue: messageMetadata?.({ part }),
602
+ sendStart,
603
+ sendFinish,
477
604
  responseMessageId: lastMessageId,
478
605
  onError(error) {
479
- return safeParseErrorObject(error);
606
+ return onError ? onError(error) : safeParseErrorObject(error);
480
607
  }
481
608
  });
482
609
  if (transformedChunk) {
@@ -496,6 +623,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
496
623
  controller.enqueue(transformedChunk);
497
624
  }
498
625
  }
626
+ },
627
+ flush(controller) {
628
+ if (tripwireOccurred && !finishEventSent && sendFinish) {
629
+ controller.enqueue({
630
+ type: "finish",
631
+ finishReason: "other"
632
+ });
633
+ }
499
634
  }
500
635
  });
501
636
  }
@@ -635,7 +770,7 @@ function transformAgent(payload, bufferedSteps) {
635
770
  }
636
771
  return null;
637
772
  }
638
- function transformWorkflow(payload, bufferedWorkflows, isNested) {
773
+ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
639
774
  switch (payload.type) {
640
775
  case "workflow-start":
641
776
  bufferedWorkflows.set(payload.runId, {
@@ -728,6 +863,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
728
863
  }
729
864
  };
730
865
  }
866
+ case "workflow-step-output": {
867
+ const output = payload.payload.output;
868
+ if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
869
+ const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
870
+ const transformedChunk = convertFullStreamChunkToUIMessageStream({
871
+ part,
872
+ onError(error) {
873
+ return safeParseErrorObject(error);
874
+ }
875
+ });
876
+ return transformedChunk;
877
+ }
878
+ if (output && isDataChunkType(output)) {
879
+ if (!("data" in output)) {
880
+ throw new Error(
881
+ `UI Messages require a data property when using data- prefixed chunks
882
+ ${JSON.stringify(output)}`
883
+ );
884
+ }
885
+ return output;
886
+ }
887
+ return null;
888
+ }
731
889
  default: {
732
890
  if (isDataChunkType(payload)) {
733
891
  if (!("data" in payload)) {
@@ -747,12 +905,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
747
905
  case "routing-agent-start": {
748
906
  if (!bufferedNetworks.has(payload.runId)) {
749
907
  bufferedNetworks.set(payload.runId, {
750
- name: payload.payload.agentId,
908
+ name: payload.payload.networkId,
751
909
  steps: [],
752
910
  usage: null,
753
911
  output: null
754
912
  });
755
913
  }
914
+ const current = bufferedNetworks.get(payload.runId);
915
+ current.steps.push({
916
+ id: payload.payload.runId,
917
+ name: payload.payload.agentId,
918
+ status: "running",
919
+ iteration: payload.payload.inputData.iteration,
920
+ input: {
921
+ task: payload.payload.inputData.task,
922
+ threadId: payload.payload.inputData.threadId,
923
+ threadResourceId: payload.payload.inputData.threadResourceId
924
+ },
925
+ output: "",
926
+ task: null,
927
+ suspendPayload: null,
928
+ resumePayload: null,
929
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
930
+ });
756
931
  return {
757
932
  type: isNested ? "data-tool-network" : "data-network",
758
933
  id: payload.runId,
@@ -783,14 +958,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
783
958
  };
784
959
  }
785
960
  case "agent-execution-start": {
786
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
961
+ const current = bufferedNetworks.get(payload.runId);
962
+ if (!current) return null;
787
963
  current.steps.push({
964
+ id: payload.payload.runId,
788
965
  name: payload.payload.agentId,
789
966
  status: "running",
790
- input: payload.payload.args || null,
967
+ iteration: payload.payload.args?.iteration ?? 0,
968
+ input: { prompt: payload.payload.args?.prompt ?? "" },
791
969
  output: null,
970
+ task: null,
792
971
  suspendPayload: null,
793
- resumePayload: null
972
+ resumePayload: null,
973
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
794
974
  });
795
975
  bufferedNetworks.set(payload.runId, current);
796
976
  return {
@@ -803,14 +983,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
803
983
  };
804
984
  }
805
985
  case "workflow-execution-start": {
806
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
986
+ const current = bufferedNetworks.get(payload.runId);
987
+ if (!current) return null;
807
988
  current.steps.push({
808
- name: payload.payload.name,
989
+ id: payload.payload.runId,
990
+ name: payload.payload.workflowId,
809
991
  status: "running",
810
- input: payload.payload.args || null,
992
+ iteration: payload.payload.args?.iteration ?? 0,
993
+ input: { prompt: payload.payload.args?.prompt ?? "" },
811
994
  output: null,
995
+ task: null,
812
996
  suspendPayload: null,
813
- resumePayload: null
997
+ resumePayload: null,
998
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
814
999
  });
815
1000
  bufferedNetworks.set(payload.runId, current);
816
1001
  return {
@@ -823,14 +1008,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
823
1008
  };
824
1009
  }
825
1010
  case "tool-execution-start": {
826
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
1011
+ const current = bufferedNetworks.get(payload.runId);
1012
+ if (!current) return null;
827
1013
  current.steps.push({
1014
+ id: payload.payload.args.toolCallId,
828
1015
  name: payload.payload.args?.toolName,
829
1016
  status: "running",
1017
+ iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
1018
+ task: {
1019
+ id: payload.payload.args?.toolName
1020
+ },
830
1021
  input: payload.payload.args?.args || null,
831
1022
  output: null,
832
1023
  suspendPayload: null,
833
- resumePayload: null
1024
+ resumePayload: null,
1025
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
834
1026
  });
835
1027
  bufferedNetworks.set(payload.runId, current);
836
1028
  return {
@@ -845,14 +1037,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
845
1037
  case "agent-execution-end": {
846
1038
  const current = bufferedNetworks.get(payload.runId);
847
1039
  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
- });
1040
+ const stepId = payload.payload.runId;
1041
+ const step = current.steps.find((step2) => step2.id === stepId);
1042
+ if (!step) {
1043
+ return null;
1044
+ }
1045
+ step.status = "success";
1046
+ step.output = payload.payload.result;
856
1047
  return {
857
1048
  type: isNested ? "data-tool-network" : "data-network",
858
1049
  id: payload.runId,
@@ -867,14 +1058,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
867
1058
  case "tool-execution-end": {
868
1059
  const current = bufferedNetworks.get(payload.runId);
869
1060
  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
- });
1061
+ const stepId = payload.payload.toolCallId;
1062
+ const step = current.steps.find((step2) => step2.id === stepId);
1063
+ if (!step) {
1064
+ return null;
1065
+ }
1066
+ step.status = "success";
1067
+ step.output = payload.payload.result;
878
1068
  return {
879
1069
  type: isNested ? "data-tool-network" : "data-network",
880
1070
  id: payload.runId,
@@ -888,14 +1078,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
888
1078
  case "workflow-execution-end": {
889
1079
  const current = bufferedNetworks.get(payload.runId);
890
1080
  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
- });
1081
+ const stepId = payload.payload.runId;
1082
+ const step = current.steps.find((step2) => step2.id === stepId);
1083
+ if (!step) {
1084
+ return null;
1085
+ }
1086
+ step.status = "success";
1087
+ step.output = payload.payload.result;
899
1088
  return {
900
1089
  type: isNested ? "data-tool-network" : "data-network",
901
1090
  id: payload.runId,
@@ -910,12 +1099,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
910
1099
  case "routing-agent-end": {
911
1100
  const current = bufferedNetworks.get(payload.runId);
912
1101
  if (!current) return null;
1102
+ const stepId = payload.payload.runId;
1103
+ const step = current.steps.find((step2) => step2.id === stepId);
1104
+ if (!step) {
1105
+ return null;
1106
+ }
1107
+ step.status = "success";
1108
+ step.task = {
1109
+ id: payload.payload.primitiveId,
1110
+ type: payload.payload.primitiveType,
1111
+ name: payload.payload.task,
1112
+ reason: payload.payload.selectionReason
1113
+ };
1114
+ step.output = payload.payload.result;
913
1115
  return {
914
1116
  type: isNested ? "data-tool-network" : "data-network",
915
1117
  id: payload.runId,
916
1118
  data: {
917
1119
  ...current,
918
- status: "finished",
919
1120
  usage: payload.payload?.usage ?? current.usage,
920
1121
  output: payload.payload?.result ?? current.output
921
1122
  }
@@ -949,32 +1150,86 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
949
1150
  };
950
1151
  }
951
1152
  default: {
952
- if (isDataChunkType(payload)) {
953
- if (!("data" in payload)) {
1153
+ if (isAgentExecutionDataChunkType(payload)) {
1154
+ if (!("data" in payload.payload)) {
954
1155
  throw new Error(
955
1156
  `UI Messages require a data property when using data- prefixed chunks
956
1157
  ${JSON.stringify(payload)}`
957
1158
  );
958
1159
  }
959
- return payload;
1160
+ const { type, data } = payload.payload;
1161
+ return { type, data };
960
1162
  }
961
- if (isAgentExecutionDataChunkType(payload)) {
1163
+ if (isWorkflowExecutionDataChunkType(payload)) {
962
1164
  if (!("data" in payload.payload)) {
963
1165
  throw new Error(
964
1166
  `UI Messages require a data property when using data- prefixed chunks
965
1167
  ${JSON.stringify(payload)}`
966
1168
  );
967
1169
  }
968
- return payload.payload;
1170
+ const { type, data } = payload.payload;
1171
+ return { type, data };
969
1172
  }
970
- if (isWorkflowExecutionDataChunkType(payload)) {
971
- if (!("data" in payload.payload)) {
1173
+ if (payload.type.startsWith("agent-execution-event-")) {
1174
+ const stepId = payload.payload.runId;
1175
+ const current = bufferedNetworks.get(payload.runId);
1176
+ if (!current) return null;
1177
+ const step = current.steps.find((step2) => step2.id === stepId);
1178
+ if (!step) {
1179
+ return null;
1180
+ }
1181
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1182
+ const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1183
+ if (result) {
1184
+ const { request, response, ...data } = result.data;
1185
+ step.task = data;
1186
+ }
1187
+ bufferedNetworks.set(payload.runId, current);
1188
+ return {
1189
+ type: isNested ? "data-tool-network" : "data-network",
1190
+ id: payload.runId,
1191
+ data: {
1192
+ ...current,
1193
+ status: "running"
1194
+ }
1195
+ };
1196
+ }
1197
+ if (payload.type.startsWith("workflow-execution-event-")) {
1198
+ const stepId = payload.payload.runId;
1199
+ const current = bufferedNetworks.get(payload.runId);
1200
+ if (!current) return null;
1201
+ const step = current.steps.find((step2) => step2.id === stepId);
1202
+ if (!step) {
1203
+ return null;
1204
+ }
1205
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1206
+ const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1207
+ if (result && "data" in result) {
1208
+ const data = result.data;
1209
+ step.task = data;
1210
+ if (data.name && step.task) {
1211
+ step.task.id = data.name;
1212
+ }
1213
+ }
1214
+ bufferedNetworks.set(payload.runId, current);
1215
+ return {
1216
+ type: isNested ? "data-tool-network" : "data-network",
1217
+ id: payload.runId,
1218
+ data: {
1219
+ ...current,
1220
+ status: "running"
1221
+ }
1222
+ };
1223
+ }
1224
+ if (isDataChunkType(payload)) {
1225
+ if (!("data" in payload)) {
972
1226
  throw new Error(
973
1227
  `UI Messages require a data property when using data- prefixed chunks
974
1228
  ${JSON.stringify(payload)}`
975
1229
  );
976
1230
  }
977
- return payload.payload;
1231
+ const { type, data } = payload;
1232
+ return { type, data };
978
1233
  }
979
1234
  return null;
980
1235
  }
@@ -982,23 +1237,95 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
982
1237
  }
983
1238
 
984
1239
  // src/convert-streams.ts
985
- function toAISdkV5Stream(stream, options = { from: "agent" }) {
1240
+ function toAISdkV5Stream(stream, options = {
1241
+ from: "agent",
1242
+ sendStart: true,
1243
+ sendFinish: true
1244
+ }) {
986
1245
  const from = options?.from;
987
1246
  if (from === "workflow") {
988
- return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
1247
+ const includeTextStreamParts = options?.includeTextStreamParts ?? true;
1248
+ return stream.pipeThrough(
1249
+ WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
1250
+ );
989
1251
  }
990
1252
  if (from === "network") {
991
1253
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
992
1254
  }
993
1255
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
994
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
1256
+ return agentReadable.pipeThrough(
1257
+ AgentStreamToAISDKTransformer({
1258
+ lastMessageId: options?.lastMessageId,
1259
+ sendStart: options?.sendStart,
1260
+ sendFinish: options?.sendFinish,
1261
+ sendReasoning: options?.sendReasoning,
1262
+ sendSources: options?.sendSources,
1263
+ messageMetadata: options?.messageMetadata,
1264
+ onError: options?.onError
1265
+ })
1266
+ );
995
1267
  }
996
1268
 
997
1269
  // src/chat-route.ts
1270
+ async function handleChatStream({
1271
+ mastra,
1272
+ agentId,
1273
+ params,
1274
+ defaultOptions,
1275
+ sendStart = true,
1276
+ sendFinish = true,
1277
+ sendReasoning = false,
1278
+ sendSources = false
1279
+ }) {
1280
+ const { messages, resumeData, runId, requestContext, ...rest } = params;
1281
+ if (resumeData && !runId) {
1282
+ throw new Error("runId is required when resumeData is provided");
1283
+ }
1284
+ const agentObj = mastra.getAgentById(agentId);
1285
+ if (!agentObj) {
1286
+ throw new Error(`Agent ${agentId} not found`);
1287
+ }
1288
+ if (!Array.isArray(messages)) {
1289
+ throw new Error("Messages must be an array of UIMessage objects");
1290
+ }
1291
+ const mergedOptions = {
1292
+ ...defaultOptions,
1293
+ ...rest,
1294
+ ...runId && { runId },
1295
+ requestContext: requestContext || defaultOptions?.requestContext
1296
+ };
1297
+ const result = resumeData ? await agentObj.resumeStream(resumeData, mergedOptions) : await agentObj.stream(messages, mergedOptions);
1298
+ let lastMessageId;
1299
+ if (messages.length) {
1300
+ const lastMessage = messages[messages.length - 1];
1301
+ if (lastMessage?.role === "assistant") {
1302
+ lastMessageId = lastMessage.id;
1303
+ }
1304
+ }
1305
+ return ai.createUIMessageStream({
1306
+ originalMessages: messages,
1307
+ execute: async ({ writer }) => {
1308
+ for await (const part of toAISdkV5Stream(result, {
1309
+ from: "agent",
1310
+ lastMessageId,
1311
+ sendStart,
1312
+ sendFinish,
1313
+ sendReasoning,
1314
+ sendSources
1315
+ })) {
1316
+ writer.write(part);
1317
+ }
1318
+ }
1319
+ });
1320
+ }
998
1321
  function chatRoute({
999
1322
  path = "/chat/:agentId",
1000
1323
  agent,
1001
- defaultOptions
1324
+ defaultOptions,
1325
+ sendStart = true,
1326
+ sendFinish = true,
1327
+ sendReasoning = false,
1328
+ sendSources = false
1002
1329
  }) {
1003
1330
  if (!agent && !path.includes("/:agentId")) {
1004
1331
  throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
@@ -1027,6 +1354,14 @@ function chatRoute({
1027
1354
  schema: {
1028
1355
  type: "object",
1029
1356
  properties: {
1357
+ resumeData: {
1358
+ type: "object",
1359
+ description: "Resume data for the agent"
1360
+ },
1361
+ runId: {
1362
+ type: "string",
1363
+ description: "The run ID required when resuming an agent execution"
1364
+ },
1030
1365
  messages: {
1031
1366
  type: "array",
1032
1367
  description: "Array of messages in the conversation",
@@ -1097,9 +1432,9 @@ function chatRoute({
1097
1432
  }
1098
1433
  },
1099
1434
  handler: async (c) => {
1100
- const { messages, ...rest } = await c.req.json();
1435
+ const params = await c.req.json();
1101
1436
  const mastra = c.get("mastra");
1102
- const requestContext = c.get("requestContext");
1437
+ const contextRequestContext = c.get("requestContext");
1103
1438
  let agentToUse = agent;
1104
1439
  if (!agent) {
1105
1440
  const agentId = c.req.param("agentId");
@@ -1110,32 +1445,24 @@ function chatRoute({
1110
1445
  `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
1111
1446
  );
1112
1447
  }
1113
- if (requestContext && defaultOptions?.requestContext) {
1448
+ if (contextRequestContext && defaultOptions?.requestContext) {
1114
1449
  mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
1115
1450
  }
1116
1451
  if (!agentToUse) {
1117
1452
  throw new Error("Agent ID is required");
1118
1453
  }
1119
- const agentObj = mastra.getAgent(agentToUse);
1120
- if (!agentObj) {
1121
- throw new Error(`Agent ${agentToUse} not found`);
1122
- }
1123
- const result = await agentObj.stream(messages, {
1124
- ...defaultOptions,
1125
- ...rest,
1126
- requestContext: requestContext || defaultOptions?.requestContext
1127
- });
1128
- let lastMessageId;
1129
- if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
1130
- lastMessageId = messages[messages.length - 1].id;
1131
- }
1132
- const uiMessageStream = ai.createUIMessageStream({
1133
- originalMessages: messages,
1134
- execute: async ({ writer }) => {
1135
- for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
1136
- writer.write(part);
1137
- }
1138
- }
1454
+ const uiMessageStream = await handleChatStream({
1455
+ mastra,
1456
+ agentId: agentToUse,
1457
+ params: {
1458
+ ...params,
1459
+ requestContext: contextRequestContext || params.requestContext
1460
+ },
1461
+ defaultOptions,
1462
+ sendStart,
1463
+ sendFinish,
1464
+ sendReasoning,
1465
+ sendSources
1139
1466
  });
1140
1467
  return ai.createUIMessageStreamResponse({
1141
1468
  stream: uiMessageStream
@@ -1143,9 +1470,31 @@ function chatRoute({
1143
1470
  }
1144
1471
  });
1145
1472
  }
1473
+ async function handleWorkflowStream({
1474
+ mastra,
1475
+ workflowId,
1476
+ params,
1477
+ includeTextStreamParts = true
1478
+ }) {
1479
+ const { runId, resourceId, inputData, resumeData, requestContext, ...rest } = params;
1480
+ const workflowObj = mastra.getWorkflowById(workflowId);
1481
+ if (!workflowObj) {
1482
+ throw new Error(`Workflow ${workflowId} not found`);
1483
+ }
1484
+ const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1485
+ const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext }) : run.stream({ inputData, ...rest, requestContext });
1486
+ return ai.createUIMessageStream({
1487
+ execute: async ({ writer }) => {
1488
+ for await (const part of toAISdkV5Stream(stream, { from: "workflow", includeTextStreamParts })) {
1489
+ writer.write(part);
1490
+ }
1491
+ }
1492
+ });
1493
+ }
1146
1494
  function workflowRoute({
1147
1495
  path = "/api/workflows/:workflowId/stream",
1148
- workflow
1496
+ workflow,
1497
+ includeTextStreamParts = true
1149
1498
  }) {
1150
1499
  if (!workflow && !path.includes("/:workflowId")) {
1151
1500
  throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
@@ -1172,9 +1521,13 @@ function workflowRoute({
1172
1521
  schema: {
1173
1522
  type: "object",
1174
1523
  properties: {
1524
+ runId: { type: "string" },
1525
+ resourceId: { type: "string" },
1175
1526
  inputData: { type: "object", additionalProperties: true },
1527
+ resumeData: { type: "object", additionalProperties: true },
1176
1528
  requestContext: { type: "object", additionalProperties: true },
1177
- tracingOptions: { type: "object", additionalProperties: true }
1529
+ tracingOptions: { type: "object", additionalProperties: true },
1530
+ step: { type: "string" }
1178
1531
  }
1179
1532
  }
1180
1533
  }
@@ -1192,8 +1545,9 @@ function workflowRoute({
1192
1545
  }
1193
1546
  },
1194
1547
  handler: async (c) => {
1195
- const { inputData, resumeData, ...rest } = await c.req.json();
1548
+ const params = await c.req.json();
1196
1549
  const mastra = c.get("mastra");
1550
+ const contextRequestContext = c.get("requestContext");
1197
1551
  let workflowToUse = workflow;
1198
1552
  if (!workflow) {
1199
1553
  const workflowId = c.req.param("workflowId");
@@ -1207,23 +1561,47 @@ function workflowRoute({
1207
1561
  if (!workflowToUse) {
1208
1562
  throw new Error("Workflow ID is required");
1209
1563
  }
1210
- const workflowObj = mastra.getWorkflow(workflowToUse);
1211
- if (!workflowObj) {
1212
- throw new Error(`Workflow ${workflowToUse} not found`);
1564
+ if (contextRequestContext && params.requestContext) {
1565
+ mastra.getLogger()?.warn(
1566
+ `"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
1567
+ );
1213
1568
  }
1214
- const run = await workflowObj.createRun();
1215
- const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1216
- const uiMessageStream = ai.createUIMessageStream({
1217
- execute: async ({ writer }) => {
1218
- for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
1219
- writer.write(part);
1220
- }
1221
- }
1569
+ const uiMessageStream = await handleWorkflowStream({
1570
+ mastra,
1571
+ workflowId: workflowToUse,
1572
+ params: {
1573
+ ...params,
1574
+ requestContext: contextRequestContext || params.requestContext
1575
+ },
1576
+ includeTextStreamParts
1222
1577
  });
1223
1578
  return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
1224
1579
  }
1225
1580
  });
1226
1581
  }
1582
+ async function handleNetworkStream({
1583
+ mastra,
1584
+ agentId,
1585
+ params,
1586
+ defaultOptions
1587
+ }) {
1588
+ const { messages, ...rest } = params;
1589
+ const agentObj = mastra.getAgentById(agentId);
1590
+ if (!agentObj) {
1591
+ throw new Error(`Agent ${agentId} not found`);
1592
+ }
1593
+ const result = await agentObj.network(messages, {
1594
+ ...defaultOptions,
1595
+ ...rest
1596
+ });
1597
+ return ai.createUIMessageStream({
1598
+ execute: async ({ writer }) => {
1599
+ for await (const part of toAISdkV5Stream(result, { from: "network" })) {
1600
+ writer.write(part);
1601
+ }
1602
+ }
1603
+ });
1604
+ }
1227
1605
  function networkRoute({
1228
1606
  path = "/network/:agentId",
1229
1607
  agent,
@@ -1284,7 +1662,7 @@ function networkRoute({
1284
1662
  }
1285
1663
  },
1286
1664
  handler: async (c) => {
1287
- const { messages, ...rest } = await c.req.json();
1665
+ const params = await c.req.json();
1288
1666
  const mastra = c.get("mastra");
1289
1667
  let agentToUse = agent;
1290
1668
  if (!agent) {
@@ -1299,25 +1677,465 @@ function networkRoute({
1299
1677
  if (!agentToUse) {
1300
1678
  throw new Error("Agent ID is required");
1301
1679
  }
1302
- const agentObj = mastra.getAgent(agentToUse);
1303
- if (!agentObj) {
1304
- throw new Error(`Agent ${agentToUse} not found`);
1680
+ const uiMessageStream = await handleNetworkStream({
1681
+ mastra,
1682
+ agentId: agentToUse,
1683
+ params,
1684
+ defaultOptions
1685
+ });
1686
+ return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
1687
+ }
1688
+ });
1689
+ }
1690
+ function withMastra(model, options = {}) {
1691
+ const { memory, inputProcessors = [], outputProcessors = [] } = options;
1692
+ const allInputProcessors = [...inputProcessors];
1693
+ const allOutputProcessors = [...outputProcessors];
1694
+ if (memory) {
1695
+ const { storage, lastMessages, semanticRecall, workingMemory } = memory;
1696
+ const isWorkingMemoryEnabled = typeof workingMemory === "object" && workingMemory.enabled !== false;
1697
+ if (isWorkingMemoryEnabled && typeof workingMemory === "object") {
1698
+ let template;
1699
+ if (workingMemory.template) {
1700
+ template = {
1701
+ format: "markdown",
1702
+ content: workingMemory.template
1703
+ };
1704
+ }
1705
+ const workingMemoryProcessor = new processors.WorkingMemory({
1706
+ storage,
1707
+ template,
1708
+ scope: workingMemory.scope,
1709
+ useVNext: "version" in workingMemory && workingMemory.version === "vnext"
1710
+ });
1711
+ allInputProcessors.push(workingMemoryProcessor);
1712
+ }
1713
+ if (lastMessages !== false && lastMessages !== void 0) {
1714
+ const messageHistory = new processors.MessageHistory({
1715
+ storage,
1716
+ lastMessages: typeof lastMessages === "number" ? lastMessages : void 0
1717
+ });
1718
+ allInputProcessors.push(messageHistory);
1719
+ allOutputProcessors.push(messageHistory);
1720
+ }
1721
+ if (semanticRecall) {
1722
+ const { vector, embedder, indexName, ...semanticConfig } = semanticRecall;
1723
+ const semanticRecallProcessor = new processors.SemanticRecall({
1724
+ storage,
1725
+ vector,
1726
+ embedder,
1727
+ indexName: indexName || "memory_messages",
1728
+ ...semanticConfig
1729
+ });
1730
+ allInputProcessors.push(semanticRecallProcessor);
1731
+ allOutputProcessors.push(semanticRecallProcessor);
1732
+ }
1733
+ }
1734
+ return ai.wrapLanguageModel({
1735
+ model,
1736
+ middleware: createProcessorMiddleware({
1737
+ inputProcessors: allInputProcessors,
1738
+ outputProcessors: allOutputProcessors,
1739
+ memory: memory ? {
1740
+ threadId: memory.threadId,
1741
+ resourceId: memory.resourceId
1742
+ } : void 0
1743
+ })
1744
+ });
1745
+ }
1746
+ function createProcessorMiddleware(options) {
1747
+ const { inputProcessors = [], outputProcessors = [], memory } = options;
1748
+ const requestContext = new di.RequestContext();
1749
+ if (memory) {
1750
+ requestContext.set("MastraMemory", {
1751
+ thread: memory.threadId ? { id: memory.threadId } : void 0,
1752
+ resourceId: memory.resourceId,
1753
+ memoryConfig: memory.config
1754
+ });
1755
+ }
1756
+ return {
1757
+ middlewareVersion: "v2",
1758
+ /**
1759
+ * Transform params runs input processors (processInput)
1760
+ */
1761
+ async transformParams({ params }) {
1762
+ const messageList = new agent.MessageList({
1763
+ threadId: memory?.threadId,
1764
+ resourceId: memory?.resourceId
1765
+ });
1766
+ for (const msg of params.prompt) {
1767
+ if (msg.role === "system") {
1768
+ messageList.addSystem(msg.content);
1769
+ } else {
1770
+ messageList.add(msg, "input");
1771
+ }
1772
+ }
1773
+ for (const processor of inputProcessors) {
1774
+ if (processor.processInput) {
1775
+ try {
1776
+ await processor.processInput({
1777
+ messages: messageList.get.input.db(),
1778
+ systemMessages: messageList.getAllSystemMessages(),
1779
+ messageList,
1780
+ requestContext,
1781
+ abort: (reason) => {
1782
+ throw new agent.TripWire(reason || "Aborted by processor");
1783
+ }
1784
+ });
1785
+ } catch (error) {
1786
+ if (error instanceof agent.TripWire) {
1787
+ return {
1788
+ ...params,
1789
+ providerOptions: {
1790
+ ...params.providerOptions,
1791
+ mastraProcessors: {
1792
+ tripwire: true,
1793
+ reason: error.message
1794
+ }
1795
+ }
1796
+ };
1797
+ }
1798
+ throw error;
1799
+ }
1800
+ }
1801
+ }
1802
+ const newPrompt = messageList.get.all.aiV5.prompt().map(agent.MessageList.aiV5ModelMessageToV2PromptMessage);
1803
+ return {
1804
+ ...params,
1805
+ prompt: newPrompt
1806
+ };
1807
+ },
1808
+ /**
1809
+ * Wrap generate for non-streaming output processing
1810
+ */
1811
+ async wrapGenerate({ doGenerate, params }) {
1812
+ const processorState = params.providerOptions?.mastraProcessors;
1813
+ if (processorState?.tripwire) {
1814
+ const reason = processorState.reason || "Blocked by processor";
1815
+ return {
1816
+ content: [{ type: "text", text: reason }],
1817
+ finishReason: "stop",
1818
+ usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
1819
+ warnings: [{ type: "other", message: `Tripwire: ${reason}` }]
1820
+ };
1305
1821
  }
1306
- const result = await agentObj.network(messages, {
1307
- ...defaultOptions,
1308
- ...rest
1822
+ const result = await doGenerate();
1823
+ if (!outputProcessors.length) return result;
1824
+ const messageList = new agent.MessageList({
1825
+ threadId: memory?.threadId,
1826
+ resourceId: memory?.resourceId
1309
1827
  });
1310
- const uiMessageStream = ai.createUIMessageStream({
1311
- execute: async ({ writer }) => {
1312
- for await (const part of toAISdkV5Stream(result, { from: "network" })) {
1313
- writer.write(part);
1828
+ for (const msg of params.prompt) {
1829
+ if (msg.role === "system") {
1830
+ messageList.addSystem(msg.content);
1831
+ } else {
1832
+ messageList.add(msg, "input");
1833
+ }
1834
+ }
1835
+ const textContent = result.content.filter((c) => c.type === "text").map((c) => c.text).join("");
1836
+ const responseMessage = {
1837
+ id: crypto.randomUUID(),
1838
+ role: "assistant",
1839
+ content: {
1840
+ format: 2,
1841
+ parts: [{ type: "text", text: textContent }]
1842
+ },
1843
+ createdAt: /* @__PURE__ */ new Date(),
1844
+ ...memory?.threadId && { threadId: memory.threadId },
1845
+ ...memory?.resourceId && { resourceId: memory.resourceId }
1846
+ };
1847
+ messageList.add(responseMessage, "response");
1848
+ for (const processor of outputProcessors) {
1849
+ if (processor.processOutputResult) {
1850
+ try {
1851
+ await processor.processOutputResult({
1852
+ messages: messageList.get.all.db(),
1853
+ messageList,
1854
+ requestContext,
1855
+ abort: (reason) => {
1856
+ throw new agent.TripWire(reason || "Aborted by processor");
1857
+ }
1858
+ });
1859
+ } catch (error) {
1860
+ if (error instanceof agent.TripWire) {
1861
+ return {
1862
+ content: [{ type: "text", text: error.message }],
1863
+ finishReason: "stop",
1864
+ usage: result.usage,
1865
+ warnings: [{ type: "other", message: `Output blocked: ${error.message}` }]
1866
+ };
1867
+ }
1868
+ throw error;
1314
1869
  }
1315
1870
  }
1871
+ }
1872
+ const processedText = messageList.get.response.db().map((m) => extractTextFromMastraMessage(m)).join("");
1873
+ return {
1874
+ ...result,
1875
+ content: [{ type: "text", text: processedText }]
1876
+ };
1877
+ },
1878
+ /**
1879
+ * Wrap stream for streaming output processing
1880
+ */
1881
+ async wrapStream({ doStream, params }) {
1882
+ const processorState = params.providerOptions?.mastraProcessors;
1883
+ if (processorState?.tripwire) {
1884
+ const reason = processorState.reason || "Blocked by processor";
1885
+ return {
1886
+ stream: createBlockedStream(reason)
1887
+ };
1888
+ }
1889
+ const { stream: stream$1, ...rest } = await doStream();
1890
+ if (!outputProcessors.length) return { stream: stream$1, ...rest };
1891
+ const processorStates = /* @__PURE__ */ new Map();
1892
+ const runId = crypto.randomUUID();
1893
+ const transformedStream = stream$1.pipeThrough(
1894
+ new TransformStream({
1895
+ async transform(chunk, controller) {
1896
+ let mastraChunk = stream.convertFullStreamChunkToMastra(
1897
+ chunk,
1898
+ { runId }
1899
+ );
1900
+ if (!mastraChunk) {
1901
+ controller.enqueue(chunk);
1902
+ return;
1903
+ }
1904
+ for (const processor of outputProcessors) {
1905
+ if (processor.processOutputStream && mastraChunk) {
1906
+ let state = processorStates.get(processor.id);
1907
+ if (!state) {
1908
+ state = { streamParts: [], customState: {} };
1909
+ processorStates.set(processor.id, state);
1910
+ }
1911
+ state.streamParts.push(mastraChunk);
1912
+ try {
1913
+ const result = await processor.processOutputStream({
1914
+ part: mastraChunk,
1915
+ streamParts: state.streamParts,
1916
+ state: state.customState,
1917
+ requestContext,
1918
+ abort: (reason) => {
1919
+ throw new agent.TripWire(reason || "Aborted by processor");
1920
+ }
1921
+ });
1922
+ if (result === null || result === void 0) {
1923
+ mastraChunk = void 0;
1924
+ } else {
1925
+ mastraChunk = result;
1926
+ }
1927
+ } catch (error) {
1928
+ if (error instanceof agent.TripWire) {
1929
+ controller.enqueue({
1930
+ type: "error",
1931
+ error: new Error(error.message)
1932
+ });
1933
+ controller.terminate();
1934
+ return;
1935
+ }
1936
+ throw error;
1937
+ }
1938
+ }
1939
+ }
1940
+ if (mastraChunk) {
1941
+ const aiChunk = convertMastraChunkToAISDKStreamPart(mastraChunk);
1942
+ if (aiChunk) {
1943
+ controller.enqueue(aiChunk);
1944
+ }
1945
+ }
1946
+ }
1947
+ })
1948
+ );
1949
+ return { stream: transformedStream, ...rest };
1950
+ }
1951
+ };
1952
+ }
1953
+ function createBlockedStream(reason) {
1954
+ return new ReadableStream({
1955
+ start(controller) {
1956
+ const id = crypto.randomUUID();
1957
+ controller.enqueue({
1958
+ type: "text-start",
1959
+ id
1316
1960
  });
1317
- return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
1961
+ controller.enqueue({
1962
+ type: "text-delta",
1963
+ id,
1964
+ delta: reason
1965
+ });
1966
+ controller.enqueue({
1967
+ type: "text-end",
1968
+ id
1969
+ });
1970
+ controller.enqueue({
1971
+ type: "finish",
1972
+ finishReason: "stop",
1973
+ usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
1974
+ });
1975
+ controller.close();
1318
1976
  }
1319
1977
  });
1320
1978
  }
1979
+ function extractTextFromMastraMessage(msg) {
1980
+ const content = msg.content;
1981
+ if (typeof content === "string") {
1982
+ return content;
1983
+ }
1984
+ if (content?.parts) {
1985
+ return content.parts.filter((p) => p.type === "text" && "text" in p).map((p) => p.text).join("");
1986
+ }
1987
+ return "";
1988
+ }
1989
+ function convertMastraChunkToAISDKStreamPart(chunk) {
1990
+ switch (chunk.type) {
1991
+ // Text streaming
1992
+ case "text-start":
1993
+ return {
1994
+ type: "text-start",
1995
+ id: chunk.payload.id || crypto.randomUUID(),
1996
+ providerMetadata: chunk.payload.providerMetadata
1997
+ };
1998
+ case "text-delta":
1999
+ return {
2000
+ type: "text-delta",
2001
+ id: chunk.payload.id || crypto.randomUUID(),
2002
+ delta: chunk.payload.text,
2003
+ providerMetadata: chunk.payload.providerMetadata
2004
+ };
2005
+ case "text-end":
2006
+ return {
2007
+ type: "text-end",
2008
+ id: chunk.payload.id || crypto.randomUUID(),
2009
+ providerMetadata: chunk.payload.providerMetadata
2010
+ };
2011
+ // Reasoning streaming
2012
+ case "reasoning-start":
2013
+ return {
2014
+ type: "reasoning-start",
2015
+ id: chunk.payload.id || crypto.randomUUID(),
2016
+ providerMetadata: chunk.payload.providerMetadata
2017
+ };
2018
+ case "reasoning-delta":
2019
+ return {
2020
+ type: "reasoning-delta",
2021
+ id: chunk.payload.id || crypto.randomUUID(),
2022
+ delta: chunk.payload.text,
2023
+ providerMetadata: chunk.payload.providerMetadata
2024
+ };
2025
+ case "reasoning-end":
2026
+ return {
2027
+ type: "reasoning-end",
2028
+ id: chunk.payload.id || crypto.randomUUID(),
2029
+ providerMetadata: chunk.payload.providerMetadata
2030
+ };
2031
+ // Tool call (complete)
2032
+ case "tool-call":
2033
+ return {
2034
+ type: "tool-call",
2035
+ toolCallId: chunk.payload.toolCallId,
2036
+ toolName: chunk.payload.toolName,
2037
+ input: JSON.stringify(chunk.payload.args),
2038
+ providerExecuted: chunk.payload.providerExecuted,
2039
+ providerMetadata: chunk.payload.providerMetadata
2040
+ };
2041
+ // Tool call input streaming
2042
+ case "tool-call-input-streaming-start":
2043
+ return {
2044
+ type: "tool-input-start",
2045
+ id: chunk.payload.toolCallId,
2046
+ toolName: chunk.payload.toolName,
2047
+ providerExecuted: chunk.payload.providerExecuted,
2048
+ providerMetadata: chunk.payload.providerMetadata
2049
+ };
2050
+ case "tool-call-delta":
2051
+ return {
2052
+ type: "tool-input-delta",
2053
+ id: chunk.payload.toolCallId,
2054
+ delta: chunk.payload.argsTextDelta,
2055
+ providerMetadata: chunk.payload.providerMetadata
2056
+ };
2057
+ case "tool-call-input-streaming-end":
2058
+ return {
2059
+ type: "tool-input-end",
2060
+ id: chunk.payload.toolCallId,
2061
+ providerMetadata: chunk.payload.providerMetadata
2062
+ };
2063
+ // Tool result
2064
+ case "tool-result":
2065
+ return {
2066
+ type: "tool-result",
2067
+ toolCallId: chunk.payload.toolCallId,
2068
+ toolName: chunk.payload.toolName,
2069
+ result: { type: "json", value: chunk.payload.result },
2070
+ isError: chunk.payload.isError,
2071
+ providerExecuted: chunk.payload.providerExecuted,
2072
+ providerMetadata: chunk.payload.providerMetadata
2073
+ };
2074
+ // Source (citations)
2075
+ case "source":
2076
+ if (chunk.payload.sourceType === "url") {
2077
+ return {
2078
+ type: "source",
2079
+ sourceType: "url",
2080
+ id: chunk.payload.id,
2081
+ url: chunk.payload.url,
2082
+ title: chunk.payload.title,
2083
+ providerMetadata: chunk.payload.providerMetadata
2084
+ };
2085
+ } else {
2086
+ return {
2087
+ type: "source",
2088
+ sourceType: "document",
2089
+ id: chunk.payload.id,
2090
+ mediaType: chunk.payload.mimeType,
2091
+ title: chunk.payload.title,
2092
+ filename: chunk.payload.filename,
2093
+ providerMetadata: chunk.payload.providerMetadata
2094
+ };
2095
+ }
2096
+ // File output
2097
+ case "file":
2098
+ return {
2099
+ type: "file",
2100
+ data: chunk.payload.data || chunk.payload.base64,
2101
+ mediaType: chunk.payload.mimeType
2102
+ };
2103
+ // Response metadata
2104
+ case "response-metadata":
2105
+ return {
2106
+ type: "response-metadata",
2107
+ ...chunk.payload
2108
+ };
2109
+ // Raw provider data
2110
+ case "raw":
2111
+ return {
2112
+ type: "raw",
2113
+ rawValue: chunk.payload
2114
+ };
2115
+ // Finish
2116
+ case "finish": {
2117
+ const usage = chunk.payload.output?.usage;
2118
+ return {
2119
+ type: "finish",
2120
+ finishReason: toAISDKFinishReason(chunk.payload.stepResult?.reason || "stop"),
2121
+ usage: usage ? {
2122
+ inputTokens: usage.inputTokens || 0,
2123
+ outputTokens: usage.outputTokens || 0,
2124
+ totalTokens: usage.totalTokens || 0
2125
+ } : { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
2126
+ providerMetadata: chunk.payload.metadata?.providerMetadata
2127
+ };
2128
+ }
2129
+ // Error
2130
+ case "error":
2131
+ return {
2132
+ type: "error",
2133
+ error: chunk.payload.error || chunk.payload
2134
+ };
2135
+ default:
2136
+ return null;
2137
+ }
2138
+ }
1321
2139
 
1322
2140
  // src/to-ai-sdk-format.ts
1323
2141
  function toAISdkFormat() {
@@ -1327,9 +2145,13 @@ function toAISdkFormat() {
1327
2145
  }
1328
2146
 
1329
2147
  exports.chatRoute = chatRoute;
2148
+ exports.handleChatStream = handleChatStream;
2149
+ exports.handleNetworkStream = handleNetworkStream;
2150
+ exports.handleWorkflowStream = handleWorkflowStream;
1330
2151
  exports.networkRoute = networkRoute;
1331
2152
  exports.toAISdkFormat = toAISdkFormat;
1332
2153
  exports.toAISdkStream = toAISdkV5Stream;
2154
+ exports.withMastra = withMastra;
1333
2155
  exports.workflowRoute = workflowRoute;
1334
2156
  //# sourceMappingURL=index.cjs.map
1335
2157
  //# sourceMappingURL=index.cjs.map