@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
|
@@ -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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MastraModelOutput, OutputSchema, MastraAgentNetworkStream, WorkflowRunOutput } from '@mastra/core/stream';
|
|
2
2
|
import type { MastraWorkflowStream, Step, WorkflowResult } from '@mastra/core/workflows';
|
|
3
|
-
import type { InferUIMessageChunk, UIMessage } from 'ai';
|
|
3
|
+
import type { InferUIMessageChunk, UIMessage, UIMessageStreamOptions } from 'ai';
|
|
4
4
|
import type { ZodObject, ZodType } from 'zod';
|
|
5
5
|
/**
|
|
6
6
|
* Converts Mastra streams (workflow, agent network, or agent) to AI SDK v5 compatible streams.
|
|
@@ -23,6 +23,8 @@ import type { ZodObject, ZodType } from 'zod';
|
|
|
23
23
|
* @param {boolean} [options.sendFinish=true] - (Agent only) Whether to send finish events. Defaults to true
|
|
24
24
|
* @param {boolean} [options.sendReasoning] - (Agent only) Whether to include reasoning in the output
|
|
25
25
|
* @param {boolean} [options.sendSources] - (Agent only) Whether to include sources in the output
|
|
26
|
+
* @param {Function} [options.messageMetadata] - (Agent only) A function that receives the current stream part and returns metadata to attach to start and finish chunks
|
|
27
|
+
* @param {Function} [options.onError] - (Agent only) A function to handle errors during stream conversion. Receives the error and should return a string representation
|
|
26
28
|
*
|
|
27
29
|
* @returns {ReadableStream<InferUIMessageChunk<UIMessage>>} A ReadableStream compatible with AI SDK v5
|
|
28
30
|
*
|
|
@@ -45,6 +47,16 @@ import type { ZodObject, ZodType } from 'zod';
|
|
|
45
47
|
* sendReasoning: true,
|
|
46
48
|
* sendSources: true
|
|
47
49
|
* });
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // Convert an agent stream with messageMetadata
|
|
53
|
+
* const aiSDKStream = toAISdkV5Stream(agentStream, {
|
|
54
|
+
* from: 'agent',
|
|
55
|
+
* messageMetadata: ({ part }) => ({
|
|
56
|
+
* timestamp: Date.now(),
|
|
57
|
+
* partType: part.type
|
|
58
|
+
* })
|
|
59
|
+
* });
|
|
48
60
|
*/
|
|
49
61
|
export declare function toAISdkV5Stream<TOutput extends ZodType<any>, TInput extends ZodType<any>, TSteps extends Step<string, any, any, any, any, any>[], TState extends ZodObject<any>>(stream: MastraWorkflowStream<TState, TInput, TOutput, TSteps>, options: {
|
|
50
62
|
from: 'workflow';
|
|
@@ -62,5 +74,7 @@ export declare function toAISdkV5Stream<TOutput extends OutputSchema>(stream: Ma
|
|
|
62
74
|
sendFinish?: boolean;
|
|
63
75
|
sendReasoning?: boolean;
|
|
64
76
|
sendSources?: boolean;
|
|
77
|
+
messageMetadata?: UIMessageStreamOptions<UIMessage>['messageMetadata'];
|
|
78
|
+
onError?: UIMessageStreamOptions<UIMessage>['onError'];
|
|
65
79
|
}): ReadableStream<InferUIMessageChunk<UIMessage>>;
|
|
66
80
|
//# sourceMappingURL=convert-streams.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert-streams.d.ts","sourceRoot":"","sources":["../src/convert-streams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EAEjB,YAAY,EACZ,wBAAwB,EACxB,iBAAiB,EAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACzF,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"convert-streams.d.ts","sourceRoot":"","sources":["../src/convert-streams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EAEjB,YAAY,EACZ,wBAAwB,EACxB,iBAAiB,EAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACzF,OAAO,KAAK,EAAE,mBAAmB,EAAE,SAAS,EAAE,sBAAsB,EAAE,MAAM,IAAI,CAAC;AACjF,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAS9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,eAAe,CAC7B,OAAO,SAAS,OAAO,CAAC,GAAG,CAAC,EAC5B,MAAM,SAAS,OAAO,CAAC,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E,UAAU,GAAG,QAAQ,CAAC;CAC9B,GAAG,eAAe,CAAC,MAAM,CAAC,CAiP1B;AAED,wBAAgB,uCAAuC,CAAC,UAAU,SAAS,SAAS,EAAE,EACpF,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,WAAW,EACX,OAAO,EACP,SAAS,EACT,UAAU,EACV,iBAAiB,GAClB,EAAE;IAED,IAAI,EAAE,cAAc,CAAC,OAAO,CAAC,GAAG,aAAa,GAAG;QAAE,IAAI,EAAE,aAAa,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,GAAG,CAAA;KAAE,CAAC;IACzG,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;IACpC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,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
|
@@ -140,6 +140,28 @@ function convertMastraChunkToAISDKv5({
|
|
|
140
140
|
toolName: chunk.payload.toolName,
|
|
141
141
|
input: chunk.payload.args
|
|
142
142
|
};
|
|
143
|
+
case "tool-call-approval":
|
|
144
|
+
return {
|
|
145
|
+
type: "data-tool-call-approval",
|
|
146
|
+
id: chunk.payload.toolCallId,
|
|
147
|
+
data: {
|
|
148
|
+
runId: chunk.runId,
|
|
149
|
+
toolCallId: chunk.payload.toolCallId,
|
|
150
|
+
toolName: chunk.payload.toolName,
|
|
151
|
+
args: chunk.payload.args
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
case "tool-call-suspended":
|
|
155
|
+
return {
|
|
156
|
+
type: "data-tool-call-suspended",
|
|
157
|
+
id: chunk.payload.toolCallId,
|
|
158
|
+
data: {
|
|
159
|
+
runId: chunk.runId,
|
|
160
|
+
toolCallId: chunk.payload.toolCallId,
|
|
161
|
+
toolName: chunk.payload.toolName,
|
|
162
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
163
|
+
}
|
|
164
|
+
};
|
|
143
165
|
case "tool-call-input-streaming-start":
|
|
144
166
|
return {
|
|
145
167
|
type: "tool-input-start",
|
|
@@ -469,6 +491,7 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
469
491
|
}
|
|
470
492
|
|
|
471
493
|
// src/transformers.ts
|
|
494
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
472
495
|
function WorkflowStreamToAISDKTransformer() {
|
|
473
496
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
474
497
|
return new TransformStream({
|
|
@@ -512,7 +535,9 @@ function AgentStreamToAISDKTransformer({
|
|
|
512
535
|
sendStart,
|
|
513
536
|
sendFinish,
|
|
514
537
|
sendReasoning,
|
|
515
|
-
sendSources
|
|
538
|
+
sendSources,
|
|
539
|
+
messageMetadata,
|
|
540
|
+
onError
|
|
516
541
|
}) {
|
|
517
542
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
518
543
|
let tripwireOccurred = false;
|
|
@@ -530,11 +555,12 @@ function AgentStreamToAISDKTransformer({
|
|
|
530
555
|
part,
|
|
531
556
|
sendReasoning,
|
|
532
557
|
sendSources,
|
|
558
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
533
559
|
sendStart,
|
|
534
560
|
sendFinish,
|
|
535
561
|
responseMessageId: lastMessageId,
|
|
536
562
|
onError(error) {
|
|
537
|
-
return safeParseErrorObject(error);
|
|
563
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
538
564
|
}
|
|
539
565
|
});
|
|
540
566
|
if (transformedChunk) {
|
|
@@ -794,6 +820,19 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
794
820
|
}
|
|
795
821
|
};
|
|
796
822
|
}
|
|
823
|
+
case "workflow-step-output": {
|
|
824
|
+
const output = payload.payload.output;
|
|
825
|
+
if (output && isDataChunkType(output)) {
|
|
826
|
+
if (!("data" in output)) {
|
|
827
|
+
throw new Error(
|
|
828
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
829
|
+
${JSON.stringify(output)}`
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
return output;
|
|
833
|
+
}
|
|
834
|
+
return null;
|
|
835
|
+
}
|
|
797
836
|
default: {
|
|
798
837
|
if (isDataChunkType(payload)) {
|
|
799
838
|
if (!("data" in payload)) {
|
|
@@ -813,12 +852,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
813
852
|
case "routing-agent-start": {
|
|
814
853
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
815
854
|
bufferedNetworks.set(payload.runId, {
|
|
816
|
-
name: payload.payload.
|
|
855
|
+
name: payload.payload.networkId,
|
|
817
856
|
steps: [],
|
|
818
857
|
usage: null,
|
|
819
858
|
output: null
|
|
820
859
|
});
|
|
821
860
|
}
|
|
861
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
862
|
+
current.steps.push({
|
|
863
|
+
id: payload.payload.runId,
|
|
864
|
+
name: payload.payload.agentId,
|
|
865
|
+
status: "running",
|
|
866
|
+
iteration: payload.payload.inputData.iteration,
|
|
867
|
+
input: {
|
|
868
|
+
task: payload.payload.inputData.task,
|
|
869
|
+
threadId: payload.payload.inputData.threadId,
|
|
870
|
+
threadResourceId: payload.payload.inputData.threadResourceId
|
|
871
|
+
},
|
|
872
|
+
output: "",
|
|
873
|
+
task: null,
|
|
874
|
+
suspendPayload: null,
|
|
875
|
+
resumePayload: null,
|
|
876
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
877
|
+
});
|
|
822
878
|
return {
|
|
823
879
|
type: isNested ? "data-tool-network" : "data-network",
|
|
824
880
|
id: payload.runId,
|
|
@@ -849,14 +905,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
849
905
|
};
|
|
850
906
|
}
|
|
851
907
|
case "agent-execution-start": {
|
|
852
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
908
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
909
|
+
if (!current) return null;
|
|
853
910
|
current.steps.push({
|
|
911
|
+
id: payload.payload.runId,
|
|
854
912
|
name: payload.payload.agentId,
|
|
855
913
|
status: "running",
|
|
856
|
-
|
|
914
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
915
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
857
916
|
output: null,
|
|
917
|
+
task: null,
|
|
858
918
|
suspendPayload: null,
|
|
859
|
-
resumePayload: null
|
|
919
|
+
resumePayload: null,
|
|
920
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
860
921
|
});
|
|
861
922
|
bufferedNetworks.set(payload.runId, current);
|
|
862
923
|
return {
|
|
@@ -869,14 +930,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
869
930
|
};
|
|
870
931
|
}
|
|
871
932
|
case "workflow-execution-start": {
|
|
872
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
933
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
934
|
+
if (!current) return null;
|
|
873
935
|
current.steps.push({
|
|
874
|
-
|
|
936
|
+
id: payload.payload.runId,
|
|
937
|
+
name: payload.payload.workflowId,
|
|
875
938
|
status: "running",
|
|
876
|
-
|
|
939
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
940
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
877
941
|
output: null,
|
|
942
|
+
task: null,
|
|
878
943
|
suspendPayload: null,
|
|
879
|
-
resumePayload: null
|
|
944
|
+
resumePayload: null,
|
|
945
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
880
946
|
});
|
|
881
947
|
bufferedNetworks.set(payload.runId, current);
|
|
882
948
|
return {
|
|
@@ -889,14 +955,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
889
955
|
};
|
|
890
956
|
}
|
|
891
957
|
case "tool-execution-start": {
|
|
892
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
958
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
959
|
+
if (!current) return null;
|
|
893
960
|
current.steps.push({
|
|
961
|
+
id: payload.payload.args.toolCallId,
|
|
894
962
|
name: payload.payload.args?.toolName,
|
|
895
963
|
status: "running",
|
|
964
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
965
|
+
task: {
|
|
966
|
+
id: payload.payload.args?.toolName
|
|
967
|
+
},
|
|
896
968
|
input: payload.payload.args?.args || null,
|
|
897
969
|
output: null,
|
|
898
970
|
suspendPayload: null,
|
|
899
|
-
resumePayload: null
|
|
971
|
+
resumePayload: null,
|
|
972
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
900
973
|
});
|
|
901
974
|
bufferedNetworks.set(payload.runId, current);
|
|
902
975
|
return {
|
|
@@ -911,14 +984,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
911
984
|
case "agent-execution-end": {
|
|
912
985
|
const current = bufferedNetworks.get(payload.runId);
|
|
913
986
|
if (!current) return null;
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
});
|
|
987
|
+
const stepId = payload.payload.runId;
|
|
988
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
989
|
+
if (!step) {
|
|
990
|
+
return null;
|
|
991
|
+
}
|
|
992
|
+
step.status = "success";
|
|
993
|
+
step.output = payload.payload.result;
|
|
922
994
|
return {
|
|
923
995
|
type: isNested ? "data-tool-network" : "data-network",
|
|
924
996
|
id: payload.runId,
|
|
@@ -933,14 +1005,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
933
1005
|
case "tool-execution-end": {
|
|
934
1006
|
const current = bufferedNetworks.get(payload.runId);
|
|
935
1007
|
if (!current) return null;
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
});
|
|
1008
|
+
const stepId = payload.payload.toolCallId;
|
|
1009
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1010
|
+
if (!step) {
|
|
1011
|
+
return null;
|
|
1012
|
+
}
|
|
1013
|
+
step.status = "success";
|
|
1014
|
+
step.output = payload.payload.result;
|
|
944
1015
|
return {
|
|
945
1016
|
type: isNested ? "data-tool-network" : "data-network",
|
|
946
1017
|
id: payload.runId,
|
|
@@ -954,14 +1025,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
954
1025
|
case "workflow-execution-end": {
|
|
955
1026
|
const current = bufferedNetworks.get(payload.runId);
|
|
956
1027
|
if (!current) return null;
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
});
|
|
1028
|
+
const stepId = payload.payload.runId;
|
|
1029
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1030
|
+
if (!step) {
|
|
1031
|
+
return null;
|
|
1032
|
+
}
|
|
1033
|
+
step.status = "success";
|
|
1034
|
+
step.output = payload.payload.result;
|
|
965
1035
|
return {
|
|
966
1036
|
type: isNested ? "data-tool-network" : "data-network",
|
|
967
1037
|
id: payload.runId,
|
|
@@ -976,12 +1046,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
976
1046
|
case "routing-agent-end": {
|
|
977
1047
|
const current = bufferedNetworks.get(payload.runId);
|
|
978
1048
|
if (!current) return null;
|
|
1049
|
+
const stepId = payload.payload.runId;
|
|
1050
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1051
|
+
if (!step) {
|
|
1052
|
+
return null;
|
|
1053
|
+
}
|
|
1054
|
+
step.status = "success";
|
|
1055
|
+
step.task = {
|
|
1056
|
+
id: payload.payload.primitiveId,
|
|
1057
|
+
type: payload.payload.primitiveType,
|
|
1058
|
+
name: payload.payload.task,
|
|
1059
|
+
reason: payload.payload.selectionReason
|
|
1060
|
+
};
|
|
1061
|
+
step.output = payload.payload.result;
|
|
979
1062
|
return {
|
|
980
1063
|
type: isNested ? "data-tool-network" : "data-network",
|
|
981
1064
|
id: payload.runId,
|
|
982
1065
|
data: {
|
|
983
1066
|
...current,
|
|
984
|
-
status: "finished",
|
|
985
1067
|
usage: payload.payload?.usage ?? current.usage,
|
|
986
1068
|
output: payload.payload?.result ?? current.output
|
|
987
1069
|
}
|
|
@@ -1015,6 +1097,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1015
1097
|
};
|
|
1016
1098
|
}
|
|
1017
1099
|
default: {
|
|
1100
|
+
if (payload.type.startsWith("agent-execution-event-")) {
|
|
1101
|
+
const stepId = payload.payload.runId;
|
|
1102
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1103
|
+
if (!current) return null;
|
|
1104
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1105
|
+
if (!step) {
|
|
1106
|
+
return null;
|
|
1107
|
+
}
|
|
1108
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1109
|
+
const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1110
|
+
if (result) {
|
|
1111
|
+
const { request, response, ...data } = result.data;
|
|
1112
|
+
step.task = data;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
if (payload.type.startsWith("workflow-execution-event-")) {
|
|
1116
|
+
const stepId = payload.payload.runId;
|
|
1117
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1118
|
+
if (!current) return null;
|
|
1119
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1120
|
+
if (!step) {
|
|
1121
|
+
return null;
|
|
1122
|
+
}
|
|
1123
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1124
|
+
const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1125
|
+
if (result) {
|
|
1126
|
+
const data = result.data;
|
|
1127
|
+
step.task = data;
|
|
1128
|
+
if (data.name && step.task) {
|
|
1129
|
+
step.task.id = data.name;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1018
1133
|
if (isDataChunkType(payload)) {
|
|
1019
1134
|
if (!("data" in payload)) {
|
|
1020
1135
|
throw new Error(
|
|
@@ -1022,7 +1137,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1022
1137
|
${JSON.stringify(payload)}`
|
|
1023
1138
|
);
|
|
1024
1139
|
}
|
|
1025
|
-
|
|
1140
|
+
const { type, data } = payload;
|
|
1141
|
+
return { type, data };
|
|
1026
1142
|
}
|
|
1027
1143
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
1028
1144
|
if (!("data" in payload.payload)) {
|
|
@@ -1031,7 +1147,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1031
1147
|
${JSON.stringify(payload)}`
|
|
1032
1148
|
);
|
|
1033
1149
|
}
|
|
1034
|
-
|
|
1150
|
+
const { type, data } = payload.payload;
|
|
1151
|
+
return { type, data };
|
|
1035
1152
|
}
|
|
1036
1153
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1037
1154
|
if (!("data" in payload.payload)) {
|
|
@@ -1040,7 +1157,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1040
1157
|
${JSON.stringify(payload)}`
|
|
1041
1158
|
);
|
|
1042
1159
|
}
|
|
1043
|
-
|
|
1160
|
+
const { type, data } = payload.payload;
|
|
1161
|
+
return { type, data };
|
|
1044
1162
|
}
|
|
1045
1163
|
return null;
|
|
1046
1164
|
}
|
|
@@ -1067,7 +1185,9 @@ function toAISdkV5Stream(stream, options = {
|
|
|
1067
1185
|
sendStart: options?.sendStart,
|
|
1068
1186
|
sendFinish: options?.sendFinish,
|
|
1069
1187
|
sendReasoning: options?.sendReasoning,
|
|
1070
|
-
sendSources: options?.sendSources
|
|
1188
|
+
sendSources: options?.sendSources,
|
|
1189
|
+
messageMetadata: options?.messageMetadata,
|
|
1190
|
+
onError: options?.onError
|
|
1071
1191
|
})
|
|
1072
1192
|
);
|
|
1073
1193
|
}
|
|
@@ -1198,7 +1318,7 @@ function chatRoute({
|
|
|
1198
1318
|
if (!agentToUse) {
|
|
1199
1319
|
throw new Error("Agent ID is required");
|
|
1200
1320
|
}
|
|
1201
|
-
const agentObj = mastra.
|
|
1321
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1202
1322
|
if (!agentObj) {
|
|
1203
1323
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1204
1324
|
}
|
|
@@ -1287,6 +1407,7 @@ function workflowRoute({
|
|
|
1287
1407
|
handler: async (c) => {
|
|
1288
1408
|
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1289
1409
|
const mastra = c.get("mastra");
|
|
1410
|
+
const requestContext = c.get("requestContext");
|
|
1290
1411
|
let workflowToUse = workflow;
|
|
1291
1412
|
if (!workflow) {
|
|
1292
1413
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1300,12 +1421,17 @@ function workflowRoute({
|
|
|
1300
1421
|
if (!workflowToUse) {
|
|
1301
1422
|
throw new Error("Workflow ID is required");
|
|
1302
1423
|
}
|
|
1303
|
-
const workflowObj = mastra.
|
|
1424
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1304
1425
|
if (!workflowObj) {
|
|
1305
1426
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1306
1427
|
}
|
|
1428
|
+
if (requestContext && rest.requestContext) {
|
|
1429
|
+
mastra.getLogger()?.warn(
|
|
1430
|
+
`"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1307
1433
|
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1308
|
-
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1434
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext: requestContext || rest.requestContext }) : run.stream({ inputData, ...rest, requestContext: requestContext || rest.requestContext });
|
|
1309
1435
|
const uiMessageStream = ai.createUIMessageStream({
|
|
1310
1436
|
execute: async ({ writer }) => {
|
|
1311
1437
|
for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
|
|
@@ -1392,7 +1518,7 @@ function networkRoute({
|
|
|
1392
1518
|
if (!agentToUse) {
|
|
1393
1519
|
throw new Error("Agent ID is required");
|
|
1394
1520
|
}
|
|
1395
|
-
const agentObj = mastra.
|
|
1521
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1396
1522
|
if (!agentObj) {
|
|
1397
1523
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1398
1524
|
}
|