@mastra/ai-sdk 0.0.0-fix-ai-sdk-dependency-20251124104209 → 0.0.0-fix-backport-setserver-20251201144151
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/CHANGELOG.md +226 -104
- package/dist/__tests__/__fixtures__/network.stream.d.ts +2329 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -0
- package/dist/helpers.d.ts +1 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +200 -67
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +201 -67
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts.map +1 -1
- package/dist/to-ai-sdk-format.d.ts +78 -11
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +126 -8
- package/dist/transformers.d.ts.map +1 -1
- package/dist/utils.d.ts +2 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/workflow-route.d.ts +3 -1
- package/dist/workflow-route.d.ts.map +1 -1
- package/package.json +12 -20
- package/dist/convert-messages.d.ts +0 -10
- package/dist/convert-messages.d.ts.map +0 -1
- package/dist/convert-streams.d.ts +0 -80
- package/dist/convert-streams.d.ts.map +0 -1
- package/dist/ui.cjs +0 -16
- package/dist/ui.cjs.map +0 -1
- package/dist/ui.d.ts +0 -2
- package/dist/ui.d.ts.map +0 -1
- package/dist/ui.js +0 -13
- package/dist/ui.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -8,6 +8,31 @@ 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
|
+
};
|
|
11
36
|
function safeParseErrorObject(obj) {
|
|
12
37
|
if (typeof obj !== "object" || obj === null) {
|
|
13
38
|
return String(obj);
|
|
@@ -489,7 +514,10 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
489
514
|
}
|
|
490
515
|
|
|
491
516
|
// src/transformers.ts
|
|
492
|
-
|
|
517
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
518
|
+
function WorkflowStreamToAISDKTransformer({
|
|
519
|
+
includeTextStreamParts
|
|
520
|
+
} = {}) {
|
|
493
521
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
494
522
|
return new TransformStream({
|
|
495
523
|
start(controller) {
|
|
@@ -503,7 +531,7 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
503
531
|
});
|
|
504
532
|
},
|
|
505
533
|
transform(chunk, controller) {
|
|
506
|
-
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
534
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
507
535
|
if (transformed) controller.enqueue(transformed);
|
|
508
536
|
}
|
|
509
537
|
});
|
|
@@ -724,7 +752,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
724
752
|
}
|
|
725
753
|
return null;
|
|
726
754
|
}
|
|
727
|
-
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
755
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
728
756
|
switch (payload.type) {
|
|
729
757
|
case "workflow-start":
|
|
730
758
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -817,6 +845,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
817
845
|
}
|
|
818
846
|
};
|
|
819
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
|
+
}
|
|
820
871
|
default: {
|
|
821
872
|
if (isDataChunkType(payload)) {
|
|
822
873
|
if (!("data" in payload)) {
|
|
@@ -836,12 +887,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
836
887
|
case "routing-agent-start": {
|
|
837
888
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
838
889
|
bufferedNetworks.set(payload.runId, {
|
|
839
|
-
name: payload.payload.
|
|
890
|
+
name: payload.payload.networkId,
|
|
840
891
|
steps: [],
|
|
841
892
|
usage: null,
|
|
842
893
|
output: null
|
|
843
894
|
});
|
|
844
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
|
+
});
|
|
845
913
|
return {
|
|
846
914
|
type: isNested ? "data-tool-network" : "data-network",
|
|
847
915
|
id: payload.runId,
|
|
@@ -872,14 +940,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
872
940
|
};
|
|
873
941
|
}
|
|
874
942
|
case "agent-execution-start": {
|
|
875
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
943
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
944
|
+
if (!current) return null;
|
|
876
945
|
current.steps.push({
|
|
946
|
+
id: payload.payload.runId,
|
|
877
947
|
name: payload.payload.agentId,
|
|
878
948
|
status: "running",
|
|
879
|
-
|
|
949
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
950
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
880
951
|
output: null,
|
|
952
|
+
task: null,
|
|
881
953
|
suspendPayload: null,
|
|
882
|
-
resumePayload: null
|
|
954
|
+
resumePayload: null,
|
|
955
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
883
956
|
});
|
|
884
957
|
bufferedNetworks.set(payload.runId, current);
|
|
885
958
|
return {
|
|
@@ -892,14 +965,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
892
965
|
};
|
|
893
966
|
}
|
|
894
967
|
case "workflow-execution-start": {
|
|
895
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
968
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
969
|
+
if (!current) return null;
|
|
896
970
|
current.steps.push({
|
|
897
|
-
|
|
971
|
+
id: payload.payload.runId,
|
|
972
|
+
name: payload.payload.workflowId,
|
|
898
973
|
status: "running",
|
|
899
|
-
|
|
974
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
975
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
900
976
|
output: null,
|
|
977
|
+
task: null,
|
|
901
978
|
suspendPayload: null,
|
|
902
|
-
resumePayload: null
|
|
979
|
+
resumePayload: null,
|
|
980
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
903
981
|
});
|
|
904
982
|
bufferedNetworks.set(payload.runId, current);
|
|
905
983
|
return {
|
|
@@ -912,14 +990,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
912
990
|
};
|
|
913
991
|
}
|
|
914
992
|
case "tool-execution-start": {
|
|
915
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
993
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
994
|
+
if (!current) return null;
|
|
916
995
|
current.steps.push({
|
|
996
|
+
id: payload.payload.args.toolCallId,
|
|
917
997
|
name: payload.payload.args?.toolName,
|
|
918
998
|
status: "running",
|
|
999
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
1000
|
+
task: {
|
|
1001
|
+
id: payload.payload.args?.toolName
|
|
1002
|
+
},
|
|
919
1003
|
input: payload.payload.args?.args || null,
|
|
920
1004
|
output: null,
|
|
921
1005
|
suspendPayload: null,
|
|
922
|
-
resumePayload: null
|
|
1006
|
+
resumePayload: null,
|
|
1007
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
923
1008
|
});
|
|
924
1009
|
bufferedNetworks.set(payload.runId, current);
|
|
925
1010
|
return {
|
|
@@ -934,14 +1019,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
934
1019
|
case "agent-execution-end": {
|
|
935
1020
|
const current = bufferedNetworks.get(payload.runId);
|
|
936
1021
|
if (!current) return null;
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
});
|
|
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;
|
|
945
1029
|
return {
|
|
946
1030
|
type: isNested ? "data-tool-network" : "data-network",
|
|
947
1031
|
id: payload.runId,
|
|
@@ -956,14 +1040,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
956
1040
|
case "tool-execution-end": {
|
|
957
1041
|
const current = bufferedNetworks.get(payload.runId);
|
|
958
1042
|
if (!current) return null;
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
});
|
|
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;
|
|
967
1050
|
return {
|
|
968
1051
|
type: isNested ? "data-tool-network" : "data-network",
|
|
969
1052
|
id: payload.runId,
|
|
@@ -977,14 +1060,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
977
1060
|
case "workflow-execution-end": {
|
|
978
1061
|
const current = bufferedNetworks.get(payload.runId);
|
|
979
1062
|
if (!current) return null;
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
});
|
|
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;
|
|
988
1070
|
return {
|
|
989
1071
|
type: isNested ? "data-tool-network" : "data-network",
|
|
990
1072
|
id: payload.runId,
|
|
@@ -999,12 +1081,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
999
1081
|
case "routing-agent-end": {
|
|
1000
1082
|
const current = bufferedNetworks.get(payload.runId);
|
|
1001
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;
|
|
1002
1097
|
return {
|
|
1003
1098
|
type: isNested ? "data-tool-network" : "data-network",
|
|
1004
1099
|
id: payload.runId,
|
|
1005
1100
|
data: {
|
|
1006
1101
|
...current,
|
|
1007
|
-
status: "finished",
|
|
1008
1102
|
usage: payload.payload?.usage ?? current.usage,
|
|
1009
1103
|
output: payload.payload?.result ?? current.output
|
|
1010
1104
|
}
|
|
@@ -1038,6 +1132,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1038
1132
|
};
|
|
1039
1133
|
}
|
|
1040
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
|
+
}
|
|
1041
1168
|
if (isDataChunkType(payload)) {
|
|
1042
1169
|
if (!("data" in payload)) {
|
|
1043
1170
|
throw new Error(
|
|
@@ -1045,7 +1172,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1045
1172
|
${JSON.stringify(payload)}`
|
|
1046
1173
|
);
|
|
1047
1174
|
}
|
|
1048
|
-
|
|
1175
|
+
const { type, data } = payload;
|
|
1176
|
+
return { type, data };
|
|
1049
1177
|
}
|
|
1050
1178
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
1051
1179
|
if (!("data" in payload.payload)) {
|
|
@@ -1054,7 +1182,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1054
1182
|
${JSON.stringify(payload)}`
|
|
1055
1183
|
);
|
|
1056
1184
|
}
|
|
1057
|
-
|
|
1185
|
+
const { type, data } = payload.payload;
|
|
1186
|
+
return { type, data };
|
|
1058
1187
|
}
|
|
1059
1188
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1060
1189
|
if (!("data" in payload.payload)) {
|
|
@@ -1063,22 +1192,26 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1063
1192
|
${JSON.stringify(payload)}`
|
|
1064
1193
|
);
|
|
1065
1194
|
}
|
|
1066
|
-
|
|
1195
|
+
const { type, data } = payload.payload;
|
|
1196
|
+
return { type, data };
|
|
1067
1197
|
}
|
|
1068
1198
|
return null;
|
|
1069
1199
|
}
|
|
1070
1200
|
}
|
|
1071
1201
|
}
|
|
1072
1202
|
|
|
1073
|
-
// src/
|
|
1074
|
-
function
|
|
1203
|
+
// src/to-ai-sdk-format.ts
|
|
1204
|
+
function toAISdkFormat(stream, options = {
|
|
1075
1205
|
from: "agent",
|
|
1076
1206
|
sendStart: true,
|
|
1077
1207
|
sendFinish: true
|
|
1078
1208
|
}) {
|
|
1079
1209
|
const from = options?.from;
|
|
1080
1210
|
if (from === "workflow") {
|
|
1081
|
-
|
|
1211
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? false;
|
|
1212
|
+
return stream.pipeThrough(
|
|
1213
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1214
|
+
);
|
|
1082
1215
|
}
|
|
1083
1216
|
if (from === "network") {
|
|
1084
1217
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
@@ -1206,7 +1339,7 @@ function chatRoute({
|
|
|
1206
1339
|
handler: async (c) => {
|
|
1207
1340
|
const { messages, ...rest } = await c.req.json();
|
|
1208
1341
|
const mastra = c.get("mastra");
|
|
1209
|
-
const
|
|
1342
|
+
const runtimeContext = c.get("runtimeContext");
|
|
1210
1343
|
let agentToUse = agent;
|
|
1211
1344
|
if (!agent) {
|
|
1212
1345
|
const agentId = c.req.param("agentId");
|
|
@@ -1217,20 +1350,20 @@ function chatRoute({
|
|
|
1217
1350
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1218
1351
|
);
|
|
1219
1352
|
}
|
|
1220
|
-
if (
|
|
1221
|
-
mastra.getLogger()?.warn(`"
|
|
1353
|
+
if (runtimeContext && defaultOptions?.runtimeContext) {
|
|
1354
|
+
mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
|
|
1222
1355
|
}
|
|
1223
1356
|
if (!agentToUse) {
|
|
1224
1357
|
throw new Error("Agent ID is required");
|
|
1225
1358
|
}
|
|
1226
|
-
const agentObj = mastra.
|
|
1359
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1227
1360
|
if (!agentObj) {
|
|
1228
1361
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1229
1362
|
}
|
|
1230
1363
|
const result = await agentObj.stream(messages, {
|
|
1231
1364
|
...defaultOptions,
|
|
1232
1365
|
...rest,
|
|
1233
|
-
|
|
1366
|
+
runtimeContext: runtimeContext || defaultOptions?.runtimeContext
|
|
1234
1367
|
});
|
|
1235
1368
|
let lastMessageId;
|
|
1236
1369
|
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
@@ -1239,7 +1372,7 @@ function chatRoute({
|
|
|
1239
1372
|
const uiMessageStream = createUIMessageStream({
|
|
1240
1373
|
originalMessages: messages,
|
|
1241
1374
|
execute: async ({ writer }) => {
|
|
1242
|
-
for await (const part of
|
|
1375
|
+
for await (const part of toAISdkFormat(result, {
|
|
1243
1376
|
from: "agent",
|
|
1244
1377
|
lastMessageId,
|
|
1245
1378
|
sendStart,
|
|
@@ -1259,7 +1392,8 @@ function chatRoute({
|
|
|
1259
1392
|
}
|
|
1260
1393
|
function workflowRoute({
|
|
1261
1394
|
path = "/api/workflows/:workflowId/stream",
|
|
1262
|
-
workflow
|
|
1395
|
+
workflow,
|
|
1396
|
+
includeTextStreamParts = false
|
|
1263
1397
|
}) {
|
|
1264
1398
|
if (!workflow && !path.includes("/:workflowId")) {
|
|
1265
1399
|
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
@@ -1290,7 +1424,7 @@ function workflowRoute({
|
|
|
1290
1424
|
resourceId: { type: "string" },
|
|
1291
1425
|
inputData: { type: "object", additionalProperties: true },
|
|
1292
1426
|
resumeData: { type: "object", additionalProperties: true },
|
|
1293
|
-
|
|
1427
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1294
1428
|
tracingOptions: { type: "object", additionalProperties: true },
|
|
1295
1429
|
step: { type: "string" }
|
|
1296
1430
|
}
|
|
@@ -1312,6 +1446,7 @@ function workflowRoute({
|
|
|
1312
1446
|
handler: async (c) => {
|
|
1313
1447
|
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1314
1448
|
const mastra = c.get("mastra");
|
|
1449
|
+
const runtimeContext = c.get("runtimeContext");
|
|
1315
1450
|
let workflowToUse = workflow;
|
|
1316
1451
|
if (!workflow) {
|
|
1317
1452
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1325,15 +1460,20 @@ function workflowRoute({
|
|
|
1325
1460
|
if (!workflowToUse) {
|
|
1326
1461
|
throw new Error("Workflow ID is required");
|
|
1327
1462
|
}
|
|
1328
|
-
const workflowObj = mastra.
|
|
1463
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1329
1464
|
if (!workflowObj) {
|
|
1330
1465
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1331
1466
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
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 });
|
|
1334
1474
|
const uiMessageStream = createUIMessageStream({
|
|
1335
1475
|
execute: async ({ writer }) => {
|
|
1336
|
-
for await (const part of
|
|
1476
|
+
for await (const part of toAISdkFormat(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1337
1477
|
writer.write(part);
|
|
1338
1478
|
}
|
|
1339
1479
|
}
|
|
@@ -1373,12 +1513,13 @@ function networkRoute({
|
|
|
1373
1513
|
type: "object",
|
|
1374
1514
|
properties: {
|
|
1375
1515
|
messages: { type: "array", items: { type: "object" } },
|
|
1376
|
-
|
|
1516
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1377
1517
|
runId: { type: "string" },
|
|
1378
1518
|
maxSteps: { type: "number" },
|
|
1379
1519
|
threadId: { type: "string" },
|
|
1380
1520
|
resourceId: { type: "string" },
|
|
1381
1521
|
modelSettings: { type: "object", additionalProperties: true },
|
|
1522
|
+
telemetry: { type: "object", additionalProperties: true },
|
|
1382
1523
|
tools: { type: "array", items: { type: "object" } }
|
|
1383
1524
|
},
|
|
1384
1525
|
required: ["messages"]
|
|
@@ -1417,7 +1558,7 @@ function networkRoute({
|
|
|
1417
1558
|
if (!agentToUse) {
|
|
1418
1559
|
throw new Error("Agent ID is required");
|
|
1419
1560
|
}
|
|
1420
|
-
const agentObj = mastra.
|
|
1561
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1421
1562
|
if (!agentObj) {
|
|
1422
1563
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1423
1564
|
}
|
|
@@ -1427,7 +1568,7 @@ function networkRoute({
|
|
|
1427
1568
|
});
|
|
1428
1569
|
const uiMessageStream = createUIMessageStream({
|
|
1429
1570
|
execute: async ({ writer }) => {
|
|
1430
|
-
for await (const part of
|
|
1571
|
+
for await (const part of toAISdkFormat(result, { from: "network" })) {
|
|
1431
1572
|
writer.write(part);
|
|
1432
1573
|
}
|
|
1433
1574
|
}
|
|
@@ -1437,13 +1578,6 @@ function networkRoute({
|
|
|
1437
1578
|
});
|
|
1438
1579
|
}
|
|
1439
1580
|
|
|
1440
|
-
|
|
1441
|
-
function toAISdkFormat() {
|
|
1442
|
-
throw new Error(
|
|
1443
|
-
'toAISdkFormat() has been deprecated. Please use toAISdkStream() instead.\n\nMigration:\n import { toAISdkFormat } from "@mastra/ai-sdk";\n // Change to:\n import { toAISdkStream } from "@mastra/ai-sdk";\n\nThe function signature remains the same.'
|
|
1444
|
-
);
|
|
1445
|
-
}
|
|
1446
|
-
|
|
1447
|
-
export { chatRoute, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
|
|
1581
|
+
export { chatRoute, networkRoute, toAISdkFormat, workflowRoute };
|
|
1448
1582
|
//# sourceMappingURL=index.js.map
|
|
1449
1583
|
//# sourceMappingURL=index.js.map
|