@mastra/ai-sdk 0.0.0-fix-issue-10434-concurrent-write-corruption-20251124213939 → 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 +216 -111
- 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 +184 -70
- 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 +185 -70
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.stream.d.ts","sourceRoot":"","sources":["../../../src/__tests__/__fixtures__/network.stream.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAu9FhC,CAAC"}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PartialSchemaOutput, OutputSchema, DataChunkType
|
|
1
|
+
import type { ChunkType, PartialSchemaOutput, OutputSchema, DataChunkType } from '@mastra/core/stream';
|
|
2
2
|
import type { InferUIMessageChunk, ObjectStreamPart, TextStreamPart, ToolSet, UIMessage } from 'ai';
|
|
3
3
|
export type OutputChunkType<OUTPUT extends OutputSchema = undefined> = TextStreamPart<ToolSet> | ObjectStreamPart<PartialSchemaOutput<OUTPUT>> | DataChunkType | undefined;
|
|
4
4
|
export type ToolAgentChunkType = {
|
package/dist/helpers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,mBAAmB,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEvG,OAAO,KAAK,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAGpG,MAAM,MAAM,eAAe,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAC/D,cAAc,CAAC,OAAO,CAAC,GACvB,gBAAgB,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,GAC7C,aAAa,G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B,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,GAAG,mBAAmB,CAAC,UAAU,CAAC,GAAG,kBAAkB,GAAG,qBAAqB,GAAG,oBAAoB,GAAG,SAAS,CAyOlH"}
|
package/dist/index.cjs
CHANGED
|
@@ -10,6 +10,31 @@ var stream = require('@mastra/core/stream');
|
|
|
10
10
|
var isDataChunkType = (chunk) => {
|
|
11
11
|
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
12
12
|
};
|
|
13
|
+
var isMastraTextStreamChunk = (chunk) => {
|
|
14
|
+
return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
|
|
15
|
+
"text-start",
|
|
16
|
+
"text-delta",
|
|
17
|
+
"text-end",
|
|
18
|
+
"reasoning-start",
|
|
19
|
+
"reasoning-delta",
|
|
20
|
+
"reasoning-end",
|
|
21
|
+
"file",
|
|
22
|
+
"source",
|
|
23
|
+
"tool-input-start",
|
|
24
|
+
"tool-input-delta",
|
|
25
|
+
"tool-call",
|
|
26
|
+
"tool-result",
|
|
27
|
+
"tool-error",
|
|
28
|
+
"error",
|
|
29
|
+
"start-step",
|
|
30
|
+
"finish-step",
|
|
31
|
+
"start",
|
|
32
|
+
"finish",
|
|
33
|
+
"abort",
|
|
34
|
+
"tool-input-end",
|
|
35
|
+
"raw"
|
|
36
|
+
].includes(chunk.type);
|
|
37
|
+
};
|
|
13
38
|
function safeParseErrorObject(obj) {
|
|
14
39
|
if (typeof obj !== "object" || obj === null) {
|
|
15
40
|
return String(obj);
|
|
@@ -491,7 +516,10 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
491
516
|
}
|
|
492
517
|
|
|
493
518
|
// src/transformers.ts
|
|
494
|
-
|
|
519
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
520
|
+
function WorkflowStreamToAISDKTransformer({
|
|
521
|
+
includeTextStreamParts
|
|
522
|
+
} = {}) {
|
|
495
523
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
496
524
|
return new TransformStream({
|
|
497
525
|
start(controller) {
|
|
@@ -505,7 +533,7 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
505
533
|
});
|
|
506
534
|
},
|
|
507
535
|
transform(chunk, controller) {
|
|
508
|
-
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
536
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
509
537
|
if (transformed) controller.enqueue(transformed);
|
|
510
538
|
}
|
|
511
539
|
});
|
|
@@ -726,7 +754,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
726
754
|
}
|
|
727
755
|
return null;
|
|
728
756
|
}
|
|
729
|
-
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
757
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
730
758
|
switch (payload.type) {
|
|
731
759
|
case "workflow-start":
|
|
732
760
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -821,6 +849,16 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
821
849
|
}
|
|
822
850
|
case "workflow-step-output": {
|
|
823
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
|
+
}
|
|
824
862
|
if (output && isDataChunkType(output)) {
|
|
825
863
|
if (!("data" in output)) {
|
|
826
864
|
throw new Error(
|
|
@@ -851,12 +889,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
851
889
|
case "routing-agent-start": {
|
|
852
890
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
853
891
|
bufferedNetworks.set(payload.runId, {
|
|
854
|
-
name: payload.payload.
|
|
892
|
+
name: payload.payload.networkId,
|
|
855
893
|
steps: [],
|
|
856
894
|
usage: null,
|
|
857
895
|
output: null
|
|
858
896
|
});
|
|
859
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
|
+
});
|
|
860
915
|
return {
|
|
861
916
|
type: isNested ? "data-tool-network" : "data-network",
|
|
862
917
|
id: payload.runId,
|
|
@@ -887,14 +942,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
887
942
|
};
|
|
888
943
|
}
|
|
889
944
|
case "agent-execution-start": {
|
|
890
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
945
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
946
|
+
if (!current) return null;
|
|
891
947
|
current.steps.push({
|
|
948
|
+
id: payload.payload.runId,
|
|
892
949
|
name: payload.payload.agentId,
|
|
893
950
|
status: "running",
|
|
894
|
-
|
|
951
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
952
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
895
953
|
output: null,
|
|
954
|
+
task: null,
|
|
896
955
|
suspendPayload: null,
|
|
897
|
-
resumePayload: null
|
|
956
|
+
resumePayload: null,
|
|
957
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
898
958
|
});
|
|
899
959
|
bufferedNetworks.set(payload.runId, current);
|
|
900
960
|
return {
|
|
@@ -907,14 +967,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
907
967
|
};
|
|
908
968
|
}
|
|
909
969
|
case "workflow-execution-start": {
|
|
910
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
970
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
971
|
+
if (!current) return null;
|
|
911
972
|
current.steps.push({
|
|
912
|
-
|
|
973
|
+
id: payload.payload.runId,
|
|
974
|
+
name: payload.payload.workflowId,
|
|
913
975
|
status: "running",
|
|
914
|
-
|
|
976
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
977
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
915
978
|
output: null,
|
|
979
|
+
task: null,
|
|
916
980
|
suspendPayload: null,
|
|
917
|
-
resumePayload: null
|
|
981
|
+
resumePayload: null,
|
|
982
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
918
983
|
});
|
|
919
984
|
bufferedNetworks.set(payload.runId, current);
|
|
920
985
|
return {
|
|
@@ -927,14 +992,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
927
992
|
};
|
|
928
993
|
}
|
|
929
994
|
case "tool-execution-start": {
|
|
930
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
995
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
996
|
+
if (!current) return null;
|
|
931
997
|
current.steps.push({
|
|
998
|
+
id: payload.payload.args.toolCallId,
|
|
932
999
|
name: payload.payload.args?.toolName,
|
|
933
1000
|
status: "running",
|
|
1001
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
1002
|
+
task: {
|
|
1003
|
+
id: payload.payload.args?.toolName
|
|
1004
|
+
},
|
|
934
1005
|
input: payload.payload.args?.args || null,
|
|
935
1006
|
output: null,
|
|
936
1007
|
suspendPayload: null,
|
|
937
|
-
resumePayload: null
|
|
1008
|
+
resumePayload: null,
|
|
1009
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
938
1010
|
});
|
|
939
1011
|
bufferedNetworks.set(payload.runId, current);
|
|
940
1012
|
return {
|
|
@@ -949,14 +1021,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
949
1021
|
case "agent-execution-end": {
|
|
950
1022
|
const current = bufferedNetworks.get(payload.runId);
|
|
951
1023
|
if (!current) return null;
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
});
|
|
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;
|
|
960
1031
|
return {
|
|
961
1032
|
type: isNested ? "data-tool-network" : "data-network",
|
|
962
1033
|
id: payload.runId,
|
|
@@ -971,14 +1042,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
971
1042
|
case "tool-execution-end": {
|
|
972
1043
|
const current = bufferedNetworks.get(payload.runId);
|
|
973
1044
|
if (!current) return null;
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
});
|
|
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;
|
|
982
1052
|
return {
|
|
983
1053
|
type: isNested ? "data-tool-network" : "data-network",
|
|
984
1054
|
id: payload.runId,
|
|
@@ -992,14 +1062,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
992
1062
|
case "workflow-execution-end": {
|
|
993
1063
|
const current = bufferedNetworks.get(payload.runId);
|
|
994
1064
|
if (!current) return null;
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
});
|
|
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;
|
|
1003
1072
|
return {
|
|
1004
1073
|
type: isNested ? "data-tool-network" : "data-network",
|
|
1005
1074
|
id: payload.runId,
|
|
@@ -1014,12 +1083,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1014
1083
|
case "routing-agent-end": {
|
|
1015
1084
|
const current = bufferedNetworks.get(payload.runId);
|
|
1016
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;
|
|
1017
1099
|
return {
|
|
1018
1100
|
type: isNested ? "data-tool-network" : "data-network",
|
|
1019
1101
|
id: payload.runId,
|
|
1020
1102
|
data: {
|
|
1021
1103
|
...current,
|
|
1022
|
-
status: "finished",
|
|
1023
1104
|
usage: payload.payload?.usage ?? current.usage,
|
|
1024
1105
|
output: payload.payload?.result ?? current.output
|
|
1025
1106
|
}
|
|
@@ -1053,6 +1134,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1053
1134
|
};
|
|
1054
1135
|
}
|
|
1055
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
|
+
}
|
|
1056
1170
|
if (isDataChunkType(payload)) {
|
|
1057
1171
|
if (!("data" in payload)) {
|
|
1058
1172
|
throw new Error(
|
|
@@ -1060,7 +1174,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1060
1174
|
${JSON.stringify(payload)}`
|
|
1061
1175
|
);
|
|
1062
1176
|
}
|
|
1063
|
-
|
|
1177
|
+
const { type, data } = payload;
|
|
1178
|
+
return { type, data };
|
|
1064
1179
|
}
|
|
1065
1180
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
1066
1181
|
if (!("data" in payload.payload)) {
|
|
@@ -1069,7 +1184,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1069
1184
|
${JSON.stringify(payload)}`
|
|
1070
1185
|
);
|
|
1071
1186
|
}
|
|
1072
|
-
|
|
1187
|
+
const { type, data } = payload.payload;
|
|
1188
|
+
return { type, data };
|
|
1073
1189
|
}
|
|
1074
1190
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1075
1191
|
if (!("data" in payload.payload)) {
|
|
@@ -1078,22 +1194,26 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1078
1194
|
${JSON.stringify(payload)}`
|
|
1079
1195
|
);
|
|
1080
1196
|
}
|
|
1081
|
-
|
|
1197
|
+
const { type, data } = payload.payload;
|
|
1198
|
+
return { type, data };
|
|
1082
1199
|
}
|
|
1083
1200
|
return null;
|
|
1084
1201
|
}
|
|
1085
1202
|
}
|
|
1086
1203
|
}
|
|
1087
1204
|
|
|
1088
|
-
// src/
|
|
1089
|
-
function
|
|
1205
|
+
// src/to-ai-sdk-format.ts
|
|
1206
|
+
function toAISdkFormat(stream, options = {
|
|
1090
1207
|
from: "agent",
|
|
1091
1208
|
sendStart: true,
|
|
1092
1209
|
sendFinish: true
|
|
1093
1210
|
}) {
|
|
1094
1211
|
const from = options?.from;
|
|
1095
1212
|
if (from === "workflow") {
|
|
1096
|
-
|
|
1213
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? false;
|
|
1214
|
+
return stream.pipeThrough(
|
|
1215
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1216
|
+
);
|
|
1097
1217
|
}
|
|
1098
1218
|
if (from === "network") {
|
|
1099
1219
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
@@ -1221,7 +1341,7 @@ function chatRoute({
|
|
|
1221
1341
|
handler: async (c) => {
|
|
1222
1342
|
const { messages, ...rest } = await c.req.json();
|
|
1223
1343
|
const mastra = c.get("mastra");
|
|
1224
|
-
const
|
|
1344
|
+
const runtimeContext = c.get("runtimeContext");
|
|
1225
1345
|
let agentToUse = agent;
|
|
1226
1346
|
if (!agent) {
|
|
1227
1347
|
const agentId = c.req.param("agentId");
|
|
@@ -1232,20 +1352,20 @@ function chatRoute({
|
|
|
1232
1352
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1233
1353
|
);
|
|
1234
1354
|
}
|
|
1235
|
-
if (
|
|
1236
|
-
mastra.getLogger()?.warn(`"
|
|
1355
|
+
if (runtimeContext && defaultOptions?.runtimeContext) {
|
|
1356
|
+
mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
|
|
1237
1357
|
}
|
|
1238
1358
|
if (!agentToUse) {
|
|
1239
1359
|
throw new Error("Agent ID is required");
|
|
1240
1360
|
}
|
|
1241
|
-
const agentObj = mastra.
|
|
1361
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1242
1362
|
if (!agentObj) {
|
|
1243
1363
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1244
1364
|
}
|
|
1245
1365
|
const result = await agentObj.stream(messages, {
|
|
1246
1366
|
...defaultOptions,
|
|
1247
1367
|
...rest,
|
|
1248
|
-
|
|
1368
|
+
runtimeContext: runtimeContext || defaultOptions?.runtimeContext
|
|
1249
1369
|
});
|
|
1250
1370
|
let lastMessageId;
|
|
1251
1371
|
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
@@ -1254,7 +1374,7 @@ function chatRoute({
|
|
|
1254
1374
|
const uiMessageStream = ai.createUIMessageStream({
|
|
1255
1375
|
originalMessages: messages,
|
|
1256
1376
|
execute: async ({ writer }) => {
|
|
1257
|
-
for await (const part of
|
|
1377
|
+
for await (const part of toAISdkFormat(result, {
|
|
1258
1378
|
from: "agent",
|
|
1259
1379
|
lastMessageId,
|
|
1260
1380
|
sendStart,
|
|
@@ -1274,7 +1394,8 @@ function chatRoute({
|
|
|
1274
1394
|
}
|
|
1275
1395
|
function workflowRoute({
|
|
1276
1396
|
path = "/api/workflows/:workflowId/stream",
|
|
1277
|
-
workflow
|
|
1397
|
+
workflow,
|
|
1398
|
+
includeTextStreamParts = false
|
|
1278
1399
|
}) {
|
|
1279
1400
|
if (!workflow && !path.includes("/:workflowId")) {
|
|
1280
1401
|
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
@@ -1305,7 +1426,7 @@ function workflowRoute({
|
|
|
1305
1426
|
resourceId: { type: "string" },
|
|
1306
1427
|
inputData: { type: "object", additionalProperties: true },
|
|
1307
1428
|
resumeData: { type: "object", additionalProperties: true },
|
|
1308
|
-
|
|
1429
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1309
1430
|
tracingOptions: { type: "object", additionalProperties: true },
|
|
1310
1431
|
step: { type: "string" }
|
|
1311
1432
|
}
|
|
@@ -1327,7 +1448,7 @@ function workflowRoute({
|
|
|
1327
1448
|
handler: async (c) => {
|
|
1328
1449
|
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1329
1450
|
const mastra = c.get("mastra");
|
|
1330
|
-
const
|
|
1451
|
+
const runtimeContext = c.get("runtimeContext");
|
|
1331
1452
|
let workflowToUse = workflow;
|
|
1332
1453
|
if (!workflow) {
|
|
1333
1454
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1341,20 +1462,20 @@ function workflowRoute({
|
|
|
1341
1462
|
if (!workflowToUse) {
|
|
1342
1463
|
throw new Error("Workflow ID is required");
|
|
1343
1464
|
}
|
|
1344
|
-
const workflowObj = mastra.
|
|
1465
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1345
1466
|
if (!workflowObj) {
|
|
1346
1467
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1347
1468
|
}
|
|
1348
|
-
if (
|
|
1469
|
+
if (runtimeContext && rest.runtimeContext) {
|
|
1349
1470
|
mastra.getLogger()?.warn(
|
|
1350
|
-
`"
|
|
1471
|
+
`"runtimeContext" from the request body will be ignored because "runtimeContext" is already set in the route options.`
|
|
1351
1472
|
);
|
|
1352
1473
|
}
|
|
1353
|
-
const run = await workflowObj.
|
|
1354
|
-
const stream = resumeData ? run.resumeStream({ resumeData, ...rest,
|
|
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 });
|
|
1355
1476
|
const uiMessageStream = ai.createUIMessageStream({
|
|
1356
1477
|
execute: async ({ writer }) => {
|
|
1357
|
-
for await (const part of
|
|
1478
|
+
for await (const part of toAISdkFormat(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1358
1479
|
writer.write(part);
|
|
1359
1480
|
}
|
|
1360
1481
|
}
|
|
@@ -1394,12 +1515,13 @@ function networkRoute({
|
|
|
1394
1515
|
type: "object",
|
|
1395
1516
|
properties: {
|
|
1396
1517
|
messages: { type: "array", items: { type: "object" } },
|
|
1397
|
-
|
|
1518
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1398
1519
|
runId: { type: "string" },
|
|
1399
1520
|
maxSteps: { type: "number" },
|
|
1400
1521
|
threadId: { type: "string" },
|
|
1401
1522
|
resourceId: { type: "string" },
|
|
1402
1523
|
modelSettings: { type: "object", additionalProperties: true },
|
|
1524
|
+
telemetry: { type: "object", additionalProperties: true },
|
|
1403
1525
|
tools: { type: "array", items: { type: "object" } }
|
|
1404
1526
|
},
|
|
1405
1527
|
required: ["messages"]
|
|
@@ -1438,7 +1560,7 @@ function networkRoute({
|
|
|
1438
1560
|
if (!agentToUse) {
|
|
1439
1561
|
throw new Error("Agent ID is required");
|
|
1440
1562
|
}
|
|
1441
|
-
const agentObj = mastra.
|
|
1563
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1442
1564
|
if (!agentObj) {
|
|
1443
1565
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1444
1566
|
}
|
|
@@ -1448,7 +1570,7 @@ function networkRoute({
|
|
|
1448
1570
|
});
|
|
1449
1571
|
const uiMessageStream = ai.createUIMessageStream({
|
|
1450
1572
|
execute: async ({ writer }) => {
|
|
1451
|
-
for await (const part of
|
|
1573
|
+
for await (const part of toAISdkFormat(result, { from: "network" })) {
|
|
1452
1574
|
writer.write(part);
|
|
1453
1575
|
}
|
|
1454
1576
|
}
|
|
@@ -1458,17 +1580,9 @@ function networkRoute({
|
|
|
1458
1580
|
});
|
|
1459
1581
|
}
|
|
1460
1582
|
|
|
1461
|
-
// src/to-ai-sdk-format.ts
|
|
1462
|
-
function toAISdkFormat() {
|
|
1463
|
-
throw new Error(
|
|
1464
|
-
'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.'
|
|
1465
|
-
);
|
|
1466
|
-
}
|
|
1467
|
-
|
|
1468
1583
|
exports.chatRoute = chatRoute;
|
|
1469
1584
|
exports.networkRoute = networkRoute;
|
|
1470
1585
|
exports.toAISdkFormat = toAISdkFormat;
|
|
1471
|
-
exports.toAISdkStream = toAISdkV5Stream;
|
|
1472
1586
|
exports.workflowRoute = workflowRoute;
|
|
1473
1587
|
//# sourceMappingURL=index.cjs.map
|
|
1474
1588
|
//# sourceMappingURL=index.cjs.map
|