@mastra/ai-sdk 1.0.0-beta.3 → 1.0.0-beta.4
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 +73 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts +2329 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -0
- package/dist/convert-streams.d.ts +15 -1
- package/dist/convert-streams.d.ts.map +1 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +171 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +171 -45
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts.map +1 -1
- package/dist/transformers.d.ts +12 -3
- package/dist/transformers.d.ts.map +1 -1
- package/dist/workflow-route.d.ts.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -138,6 +138,28 @@ function convertMastraChunkToAISDKv5({
|
|
|
138
138
|
toolName: chunk.payload.toolName,
|
|
139
139
|
input: chunk.payload.args
|
|
140
140
|
};
|
|
141
|
+
case "tool-call-approval":
|
|
142
|
+
return {
|
|
143
|
+
type: "data-tool-call-approval",
|
|
144
|
+
id: chunk.payload.toolCallId,
|
|
145
|
+
data: {
|
|
146
|
+
runId: chunk.runId,
|
|
147
|
+
toolCallId: chunk.payload.toolCallId,
|
|
148
|
+
toolName: chunk.payload.toolName,
|
|
149
|
+
args: chunk.payload.args
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
case "tool-call-suspended":
|
|
153
|
+
return {
|
|
154
|
+
type: "data-tool-call-suspended",
|
|
155
|
+
id: chunk.payload.toolCallId,
|
|
156
|
+
data: {
|
|
157
|
+
runId: chunk.runId,
|
|
158
|
+
toolCallId: chunk.payload.toolCallId,
|
|
159
|
+
toolName: chunk.payload.toolName,
|
|
160
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
161
|
+
}
|
|
162
|
+
};
|
|
141
163
|
case "tool-call-input-streaming-start":
|
|
142
164
|
return {
|
|
143
165
|
type: "tool-input-start",
|
|
@@ -467,6 +489,7 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
467
489
|
}
|
|
468
490
|
|
|
469
491
|
// src/transformers.ts
|
|
492
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
470
493
|
function WorkflowStreamToAISDKTransformer() {
|
|
471
494
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
472
495
|
return new TransformStream({
|
|
@@ -510,7 +533,9 @@ function AgentStreamToAISDKTransformer({
|
|
|
510
533
|
sendStart,
|
|
511
534
|
sendFinish,
|
|
512
535
|
sendReasoning,
|
|
513
|
-
sendSources
|
|
536
|
+
sendSources,
|
|
537
|
+
messageMetadata,
|
|
538
|
+
onError
|
|
514
539
|
}) {
|
|
515
540
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
516
541
|
let tripwireOccurred = false;
|
|
@@ -528,11 +553,12 @@ function AgentStreamToAISDKTransformer({
|
|
|
528
553
|
part,
|
|
529
554
|
sendReasoning,
|
|
530
555
|
sendSources,
|
|
556
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
531
557
|
sendStart,
|
|
532
558
|
sendFinish,
|
|
533
559
|
responseMessageId: lastMessageId,
|
|
534
560
|
onError(error) {
|
|
535
|
-
return safeParseErrorObject(error);
|
|
561
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
536
562
|
}
|
|
537
563
|
});
|
|
538
564
|
if (transformedChunk) {
|
|
@@ -792,6 +818,19 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
792
818
|
}
|
|
793
819
|
};
|
|
794
820
|
}
|
|
821
|
+
case "workflow-step-output": {
|
|
822
|
+
const output = payload.payload.output;
|
|
823
|
+
if (output && isDataChunkType(output)) {
|
|
824
|
+
if (!("data" in output)) {
|
|
825
|
+
throw new Error(
|
|
826
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
827
|
+
${JSON.stringify(output)}`
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
return output;
|
|
831
|
+
}
|
|
832
|
+
return null;
|
|
833
|
+
}
|
|
795
834
|
default: {
|
|
796
835
|
if (isDataChunkType(payload)) {
|
|
797
836
|
if (!("data" in payload)) {
|
|
@@ -811,12 +850,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
811
850
|
case "routing-agent-start": {
|
|
812
851
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
813
852
|
bufferedNetworks.set(payload.runId, {
|
|
814
|
-
name: payload.payload.
|
|
853
|
+
name: payload.payload.networkId,
|
|
815
854
|
steps: [],
|
|
816
855
|
usage: null,
|
|
817
856
|
output: null
|
|
818
857
|
});
|
|
819
858
|
}
|
|
859
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
860
|
+
current.steps.push({
|
|
861
|
+
id: payload.payload.runId,
|
|
862
|
+
name: payload.payload.agentId,
|
|
863
|
+
status: "running",
|
|
864
|
+
iteration: payload.payload.inputData.iteration,
|
|
865
|
+
input: {
|
|
866
|
+
task: payload.payload.inputData.task,
|
|
867
|
+
threadId: payload.payload.inputData.threadId,
|
|
868
|
+
threadResourceId: payload.payload.inputData.threadResourceId
|
|
869
|
+
},
|
|
870
|
+
output: "",
|
|
871
|
+
task: null,
|
|
872
|
+
suspendPayload: null,
|
|
873
|
+
resumePayload: null,
|
|
874
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
875
|
+
});
|
|
820
876
|
return {
|
|
821
877
|
type: isNested ? "data-tool-network" : "data-network",
|
|
822
878
|
id: payload.runId,
|
|
@@ -847,14 +903,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
847
903
|
};
|
|
848
904
|
}
|
|
849
905
|
case "agent-execution-start": {
|
|
850
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
906
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
907
|
+
if (!current) return null;
|
|
851
908
|
current.steps.push({
|
|
909
|
+
id: payload.payload.runId,
|
|
852
910
|
name: payload.payload.agentId,
|
|
853
911
|
status: "running",
|
|
854
|
-
|
|
912
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
913
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
855
914
|
output: null,
|
|
915
|
+
task: null,
|
|
856
916
|
suspendPayload: null,
|
|
857
|
-
resumePayload: null
|
|
917
|
+
resumePayload: null,
|
|
918
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
858
919
|
});
|
|
859
920
|
bufferedNetworks.set(payload.runId, current);
|
|
860
921
|
return {
|
|
@@ -867,14 +928,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
867
928
|
};
|
|
868
929
|
}
|
|
869
930
|
case "workflow-execution-start": {
|
|
870
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
931
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
932
|
+
if (!current) return null;
|
|
871
933
|
current.steps.push({
|
|
872
|
-
|
|
934
|
+
id: payload.payload.runId,
|
|
935
|
+
name: payload.payload.workflowId,
|
|
873
936
|
status: "running",
|
|
874
|
-
|
|
937
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
938
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
875
939
|
output: null,
|
|
940
|
+
task: null,
|
|
876
941
|
suspendPayload: null,
|
|
877
|
-
resumePayload: null
|
|
942
|
+
resumePayload: null,
|
|
943
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
878
944
|
});
|
|
879
945
|
bufferedNetworks.set(payload.runId, current);
|
|
880
946
|
return {
|
|
@@ -887,14 +953,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
887
953
|
};
|
|
888
954
|
}
|
|
889
955
|
case "tool-execution-start": {
|
|
890
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
956
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
957
|
+
if (!current) return null;
|
|
891
958
|
current.steps.push({
|
|
959
|
+
id: payload.payload.args.toolCallId,
|
|
892
960
|
name: payload.payload.args?.toolName,
|
|
893
961
|
status: "running",
|
|
962
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
963
|
+
task: {
|
|
964
|
+
id: payload.payload.args?.toolName
|
|
965
|
+
},
|
|
894
966
|
input: payload.payload.args?.args || null,
|
|
895
967
|
output: null,
|
|
896
968
|
suspendPayload: null,
|
|
897
|
-
resumePayload: null
|
|
969
|
+
resumePayload: null,
|
|
970
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
898
971
|
});
|
|
899
972
|
bufferedNetworks.set(payload.runId, current);
|
|
900
973
|
return {
|
|
@@ -909,14 +982,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
909
982
|
case "agent-execution-end": {
|
|
910
983
|
const current = bufferedNetworks.get(payload.runId);
|
|
911
984
|
if (!current) return null;
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
});
|
|
985
|
+
const stepId = payload.payload.runId;
|
|
986
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
987
|
+
if (!step) {
|
|
988
|
+
return null;
|
|
989
|
+
}
|
|
990
|
+
step.status = "success";
|
|
991
|
+
step.output = payload.payload.result;
|
|
920
992
|
return {
|
|
921
993
|
type: isNested ? "data-tool-network" : "data-network",
|
|
922
994
|
id: payload.runId,
|
|
@@ -931,14 +1003,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
931
1003
|
case "tool-execution-end": {
|
|
932
1004
|
const current = bufferedNetworks.get(payload.runId);
|
|
933
1005
|
if (!current) return null;
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
});
|
|
1006
|
+
const stepId = payload.payload.toolCallId;
|
|
1007
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1008
|
+
if (!step) {
|
|
1009
|
+
return null;
|
|
1010
|
+
}
|
|
1011
|
+
step.status = "success";
|
|
1012
|
+
step.output = payload.payload.result;
|
|
942
1013
|
return {
|
|
943
1014
|
type: isNested ? "data-tool-network" : "data-network",
|
|
944
1015
|
id: payload.runId,
|
|
@@ -952,14 +1023,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
952
1023
|
case "workflow-execution-end": {
|
|
953
1024
|
const current = bufferedNetworks.get(payload.runId);
|
|
954
1025
|
if (!current) return null;
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
});
|
|
1026
|
+
const stepId = payload.payload.runId;
|
|
1027
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1028
|
+
if (!step) {
|
|
1029
|
+
return null;
|
|
1030
|
+
}
|
|
1031
|
+
step.status = "success";
|
|
1032
|
+
step.output = payload.payload.result;
|
|
963
1033
|
return {
|
|
964
1034
|
type: isNested ? "data-tool-network" : "data-network",
|
|
965
1035
|
id: payload.runId,
|
|
@@ -974,12 +1044,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
974
1044
|
case "routing-agent-end": {
|
|
975
1045
|
const current = bufferedNetworks.get(payload.runId);
|
|
976
1046
|
if (!current) return null;
|
|
1047
|
+
const stepId = payload.payload.runId;
|
|
1048
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1049
|
+
if (!step) {
|
|
1050
|
+
return null;
|
|
1051
|
+
}
|
|
1052
|
+
step.status = "success";
|
|
1053
|
+
step.task = {
|
|
1054
|
+
id: payload.payload.primitiveId,
|
|
1055
|
+
type: payload.payload.primitiveType,
|
|
1056
|
+
name: payload.payload.task,
|
|
1057
|
+
reason: payload.payload.selectionReason
|
|
1058
|
+
};
|
|
1059
|
+
step.output = payload.payload.result;
|
|
977
1060
|
return {
|
|
978
1061
|
type: isNested ? "data-tool-network" : "data-network",
|
|
979
1062
|
id: payload.runId,
|
|
980
1063
|
data: {
|
|
981
1064
|
...current,
|
|
982
|
-
status: "finished",
|
|
983
1065
|
usage: payload.payload?.usage ?? current.usage,
|
|
984
1066
|
output: payload.payload?.result ?? current.output
|
|
985
1067
|
}
|
|
@@ -1013,6 +1095,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1013
1095
|
};
|
|
1014
1096
|
}
|
|
1015
1097
|
default: {
|
|
1098
|
+
if (payload.type.startsWith("agent-execution-event-")) {
|
|
1099
|
+
const stepId = payload.payload.runId;
|
|
1100
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1101
|
+
if (!current) return null;
|
|
1102
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1103
|
+
if (!step) {
|
|
1104
|
+
return null;
|
|
1105
|
+
}
|
|
1106
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1107
|
+
const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1108
|
+
if (result) {
|
|
1109
|
+
const { request, response, ...data } = result.data;
|
|
1110
|
+
step.task = data;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
if (payload.type.startsWith("workflow-execution-event-")) {
|
|
1114
|
+
const stepId = payload.payload.runId;
|
|
1115
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1116
|
+
if (!current) return null;
|
|
1117
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1118
|
+
if (!step) {
|
|
1119
|
+
return null;
|
|
1120
|
+
}
|
|
1121
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1122
|
+
const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1123
|
+
if (result) {
|
|
1124
|
+
const data = result.data;
|
|
1125
|
+
step.task = data;
|
|
1126
|
+
if (data.name && step.task) {
|
|
1127
|
+
step.task.id = data.name;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1016
1131
|
if (isDataChunkType(payload)) {
|
|
1017
1132
|
if (!("data" in payload)) {
|
|
1018
1133
|
throw new Error(
|
|
@@ -1020,7 +1135,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1020
1135
|
${JSON.stringify(payload)}`
|
|
1021
1136
|
);
|
|
1022
1137
|
}
|
|
1023
|
-
|
|
1138
|
+
const { type, data } = payload;
|
|
1139
|
+
return { type, data };
|
|
1024
1140
|
}
|
|
1025
1141
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
1026
1142
|
if (!("data" in payload.payload)) {
|
|
@@ -1029,7 +1145,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1029
1145
|
${JSON.stringify(payload)}`
|
|
1030
1146
|
);
|
|
1031
1147
|
}
|
|
1032
|
-
|
|
1148
|
+
const { type, data } = payload.payload;
|
|
1149
|
+
return { type, data };
|
|
1033
1150
|
}
|
|
1034
1151
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1035
1152
|
if (!("data" in payload.payload)) {
|
|
@@ -1038,7 +1155,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1038
1155
|
${JSON.stringify(payload)}`
|
|
1039
1156
|
);
|
|
1040
1157
|
}
|
|
1041
|
-
|
|
1158
|
+
const { type, data } = payload.payload;
|
|
1159
|
+
return { type, data };
|
|
1042
1160
|
}
|
|
1043
1161
|
return null;
|
|
1044
1162
|
}
|
|
@@ -1065,7 +1183,9 @@ function toAISdkV5Stream(stream, options = {
|
|
|
1065
1183
|
sendStart: options?.sendStart,
|
|
1066
1184
|
sendFinish: options?.sendFinish,
|
|
1067
1185
|
sendReasoning: options?.sendReasoning,
|
|
1068
|
-
sendSources: options?.sendSources
|
|
1186
|
+
sendSources: options?.sendSources,
|
|
1187
|
+
messageMetadata: options?.messageMetadata,
|
|
1188
|
+
onError: options?.onError
|
|
1069
1189
|
})
|
|
1070
1190
|
);
|
|
1071
1191
|
}
|
|
@@ -1196,7 +1316,7 @@ function chatRoute({
|
|
|
1196
1316
|
if (!agentToUse) {
|
|
1197
1317
|
throw new Error("Agent ID is required");
|
|
1198
1318
|
}
|
|
1199
|
-
const agentObj = mastra.
|
|
1319
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1200
1320
|
if (!agentObj) {
|
|
1201
1321
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1202
1322
|
}
|
|
@@ -1285,6 +1405,7 @@ function workflowRoute({
|
|
|
1285
1405
|
handler: async (c) => {
|
|
1286
1406
|
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1287
1407
|
const mastra = c.get("mastra");
|
|
1408
|
+
const requestContext = c.get("requestContext");
|
|
1288
1409
|
let workflowToUse = workflow;
|
|
1289
1410
|
if (!workflow) {
|
|
1290
1411
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1298,12 +1419,17 @@ function workflowRoute({
|
|
|
1298
1419
|
if (!workflowToUse) {
|
|
1299
1420
|
throw new Error("Workflow ID is required");
|
|
1300
1421
|
}
|
|
1301
|
-
const workflowObj = mastra.
|
|
1422
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1302
1423
|
if (!workflowObj) {
|
|
1303
1424
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1304
1425
|
}
|
|
1426
|
+
if (requestContext && rest.requestContext) {
|
|
1427
|
+
mastra.getLogger()?.warn(
|
|
1428
|
+
`"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1305
1431
|
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1306
|
-
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1432
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext: requestContext || rest.requestContext }) : run.stream({ inputData, ...rest, requestContext: requestContext || rest.requestContext });
|
|
1307
1433
|
const uiMessageStream = createUIMessageStream({
|
|
1308
1434
|
execute: async ({ writer }) => {
|
|
1309
1435
|
for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
|
|
@@ -1390,7 +1516,7 @@ function networkRoute({
|
|
|
1390
1516
|
if (!agentToUse) {
|
|
1391
1517
|
throw new Error("Agent ID is required");
|
|
1392
1518
|
}
|
|
1393
|
-
const agentObj = mastra.
|
|
1519
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1394
1520
|
if (!agentObj) {
|
|
1395
1521
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1396
1522
|
}
|