@mastra/ai-sdk 1.0.0-beta.2 → 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 +102 -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 +1 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +194 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +194 -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 +5 -5
|
@@ -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,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U,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,eAAe,CAAC,EAAE,sBAAsB,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACvE,OAAO,CAAC,EAAE,sBAAsB,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC;CACxD,GACA,cAAc,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,CAAC"}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PartialSchemaOutput, OutputSchema, DataChunkType, ChunkType } from '@mastra/core/stream';
|
|
2
2
|
import type { InferUIMessageChunk, ObjectStreamPart, TextStreamPart, ToolSet, UIMessage } from 'ai';
|
|
3
|
-
export type OutputChunkType<OUTPUT extends OutputSchema = undefined> = TextStreamPart<ToolSet> | ObjectStreamPart<PartialSchemaOutput<OUTPUT>> | undefined;
|
|
3
|
+
export type OutputChunkType<OUTPUT extends OutputSchema = undefined> = TextStreamPart<ToolSet> | ObjectStreamPart<PartialSchemaOutput<OUTPUT>> | DataChunkType | undefined;
|
|
4
4
|
export type ToolAgentChunkType = {
|
|
5
5
|
type: 'tool-agent';
|
|
6
6
|
toolCallId: string;
|
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,SAAS,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,GAC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BAAkB,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",
|
|
@@ -230,6 +252,13 @@ function convertMastraChunkToAISDKv5({
|
|
|
230
252
|
type: "object",
|
|
231
253
|
object: chunk.object
|
|
232
254
|
};
|
|
255
|
+
case "tripwire":
|
|
256
|
+
return {
|
|
257
|
+
type: "data-tripwire",
|
|
258
|
+
data: {
|
|
259
|
+
tripwireReason: chunk.payload.tripwireReason
|
|
260
|
+
}
|
|
261
|
+
};
|
|
233
262
|
default:
|
|
234
263
|
if (chunk.type && "payload" in chunk && chunk.payload) {
|
|
235
264
|
return {
|
|
@@ -462,6 +491,7 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
462
491
|
}
|
|
463
492
|
|
|
464
493
|
// src/transformers.ts
|
|
494
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
465
495
|
function WorkflowStreamToAISDKTransformer() {
|
|
466
496
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
467
497
|
return new TransformStream({
|
|
@@ -505,21 +535,32 @@ function AgentStreamToAISDKTransformer({
|
|
|
505
535
|
sendStart,
|
|
506
536
|
sendFinish,
|
|
507
537
|
sendReasoning,
|
|
508
|
-
sendSources
|
|
538
|
+
sendSources,
|
|
539
|
+
messageMetadata,
|
|
540
|
+
onError
|
|
509
541
|
}) {
|
|
510
542
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
543
|
+
let tripwireOccurred = false;
|
|
544
|
+
let finishEventSent = false;
|
|
511
545
|
return new TransformStream({
|
|
512
546
|
transform(chunk, controller) {
|
|
547
|
+
if (chunk.type === "tripwire") {
|
|
548
|
+
tripwireOccurred = true;
|
|
549
|
+
}
|
|
550
|
+
if (chunk.type === "finish") {
|
|
551
|
+
finishEventSent = true;
|
|
552
|
+
}
|
|
513
553
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
514
554
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
515
555
|
part,
|
|
516
556
|
sendReasoning,
|
|
517
557
|
sendSources,
|
|
558
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
518
559
|
sendStart,
|
|
519
560
|
sendFinish,
|
|
520
561
|
responseMessageId: lastMessageId,
|
|
521
562
|
onError(error) {
|
|
522
|
-
return safeParseErrorObject(error);
|
|
563
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
523
564
|
}
|
|
524
565
|
});
|
|
525
566
|
if (transformedChunk) {
|
|
@@ -539,6 +580,14 @@ function AgentStreamToAISDKTransformer({
|
|
|
539
580
|
controller.enqueue(transformedChunk);
|
|
540
581
|
}
|
|
541
582
|
}
|
|
583
|
+
},
|
|
584
|
+
flush(controller) {
|
|
585
|
+
if (tripwireOccurred && !finishEventSent && sendFinish) {
|
|
586
|
+
controller.enqueue({
|
|
587
|
+
type: "finish",
|
|
588
|
+
finishReason: "other"
|
|
589
|
+
});
|
|
590
|
+
}
|
|
542
591
|
}
|
|
543
592
|
});
|
|
544
593
|
}
|
|
@@ -771,6 +820,19 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
771
820
|
}
|
|
772
821
|
};
|
|
773
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
|
+
}
|
|
774
836
|
default: {
|
|
775
837
|
if (isDataChunkType(payload)) {
|
|
776
838
|
if (!("data" in payload)) {
|
|
@@ -790,12 +852,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
790
852
|
case "routing-agent-start": {
|
|
791
853
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
792
854
|
bufferedNetworks.set(payload.runId, {
|
|
793
|
-
name: payload.payload.
|
|
855
|
+
name: payload.payload.networkId,
|
|
794
856
|
steps: [],
|
|
795
857
|
usage: null,
|
|
796
858
|
output: null
|
|
797
859
|
});
|
|
798
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
|
+
});
|
|
799
878
|
return {
|
|
800
879
|
type: isNested ? "data-tool-network" : "data-network",
|
|
801
880
|
id: payload.runId,
|
|
@@ -826,14 +905,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
826
905
|
};
|
|
827
906
|
}
|
|
828
907
|
case "agent-execution-start": {
|
|
829
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
908
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
909
|
+
if (!current) return null;
|
|
830
910
|
current.steps.push({
|
|
911
|
+
id: payload.payload.runId,
|
|
831
912
|
name: payload.payload.agentId,
|
|
832
913
|
status: "running",
|
|
833
|
-
|
|
914
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
915
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
834
916
|
output: null,
|
|
917
|
+
task: null,
|
|
835
918
|
suspendPayload: null,
|
|
836
|
-
resumePayload: null
|
|
919
|
+
resumePayload: null,
|
|
920
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
837
921
|
});
|
|
838
922
|
bufferedNetworks.set(payload.runId, current);
|
|
839
923
|
return {
|
|
@@ -846,14 +930,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
846
930
|
};
|
|
847
931
|
}
|
|
848
932
|
case "workflow-execution-start": {
|
|
849
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
933
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
934
|
+
if (!current) return null;
|
|
850
935
|
current.steps.push({
|
|
851
|
-
|
|
936
|
+
id: payload.payload.runId,
|
|
937
|
+
name: payload.payload.workflowId,
|
|
852
938
|
status: "running",
|
|
853
|
-
|
|
939
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
940
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
854
941
|
output: null,
|
|
942
|
+
task: null,
|
|
855
943
|
suspendPayload: null,
|
|
856
|
-
resumePayload: null
|
|
944
|
+
resumePayload: null,
|
|
945
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
857
946
|
});
|
|
858
947
|
bufferedNetworks.set(payload.runId, current);
|
|
859
948
|
return {
|
|
@@ -866,14 +955,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
866
955
|
};
|
|
867
956
|
}
|
|
868
957
|
case "tool-execution-start": {
|
|
869
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
958
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
959
|
+
if (!current) return null;
|
|
870
960
|
current.steps.push({
|
|
961
|
+
id: payload.payload.args.toolCallId,
|
|
871
962
|
name: payload.payload.args?.toolName,
|
|
872
963
|
status: "running",
|
|
964
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
965
|
+
task: {
|
|
966
|
+
id: payload.payload.args?.toolName
|
|
967
|
+
},
|
|
873
968
|
input: payload.payload.args?.args || null,
|
|
874
969
|
output: null,
|
|
875
970
|
suspendPayload: null,
|
|
876
|
-
resumePayload: null
|
|
971
|
+
resumePayload: null,
|
|
972
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
877
973
|
});
|
|
878
974
|
bufferedNetworks.set(payload.runId, current);
|
|
879
975
|
return {
|
|
@@ -888,14 +984,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
888
984
|
case "agent-execution-end": {
|
|
889
985
|
const current = bufferedNetworks.get(payload.runId);
|
|
890
986
|
if (!current) return null;
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
});
|
|
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;
|
|
899
994
|
return {
|
|
900
995
|
type: isNested ? "data-tool-network" : "data-network",
|
|
901
996
|
id: payload.runId,
|
|
@@ -910,14 +1005,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
910
1005
|
case "tool-execution-end": {
|
|
911
1006
|
const current = bufferedNetworks.get(payload.runId);
|
|
912
1007
|
if (!current) return null;
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
});
|
|
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;
|
|
921
1015
|
return {
|
|
922
1016
|
type: isNested ? "data-tool-network" : "data-network",
|
|
923
1017
|
id: payload.runId,
|
|
@@ -931,14 +1025,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
931
1025
|
case "workflow-execution-end": {
|
|
932
1026
|
const current = bufferedNetworks.get(payload.runId);
|
|
933
1027
|
if (!current) return null;
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
});
|
|
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;
|
|
942
1035
|
return {
|
|
943
1036
|
type: isNested ? "data-tool-network" : "data-network",
|
|
944
1037
|
id: payload.runId,
|
|
@@ -953,12 +1046,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
953
1046
|
case "routing-agent-end": {
|
|
954
1047
|
const current = bufferedNetworks.get(payload.runId);
|
|
955
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;
|
|
956
1062
|
return {
|
|
957
1063
|
type: isNested ? "data-tool-network" : "data-network",
|
|
958
1064
|
id: payload.runId,
|
|
959
1065
|
data: {
|
|
960
1066
|
...current,
|
|
961
|
-
status: "finished",
|
|
962
1067
|
usage: payload.payload?.usage ?? current.usage,
|
|
963
1068
|
output: payload.payload?.result ?? current.output
|
|
964
1069
|
}
|
|
@@ -992,6 +1097,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
992
1097
|
};
|
|
993
1098
|
}
|
|
994
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
|
+
}
|
|
995
1133
|
if (isDataChunkType(payload)) {
|
|
996
1134
|
if (!("data" in payload)) {
|
|
997
1135
|
throw new Error(
|
|
@@ -999,7 +1137,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
999
1137
|
${JSON.stringify(payload)}`
|
|
1000
1138
|
);
|
|
1001
1139
|
}
|
|
1002
|
-
|
|
1140
|
+
const { type, data } = payload;
|
|
1141
|
+
return { type, data };
|
|
1003
1142
|
}
|
|
1004
1143
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
1005
1144
|
if (!("data" in payload.payload)) {
|
|
@@ -1008,7 +1147,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1008
1147
|
${JSON.stringify(payload)}`
|
|
1009
1148
|
);
|
|
1010
1149
|
}
|
|
1011
|
-
|
|
1150
|
+
const { type, data } = payload.payload;
|
|
1151
|
+
return { type, data };
|
|
1012
1152
|
}
|
|
1013
1153
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1014
1154
|
if (!("data" in payload.payload)) {
|
|
@@ -1017,7 +1157,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
1017
1157
|
${JSON.stringify(payload)}`
|
|
1018
1158
|
);
|
|
1019
1159
|
}
|
|
1020
|
-
|
|
1160
|
+
const { type, data } = payload.payload;
|
|
1161
|
+
return { type, data };
|
|
1021
1162
|
}
|
|
1022
1163
|
return null;
|
|
1023
1164
|
}
|
|
@@ -1044,7 +1185,9 @@ function toAISdkV5Stream(stream, options = {
|
|
|
1044
1185
|
sendStart: options?.sendStart,
|
|
1045
1186
|
sendFinish: options?.sendFinish,
|
|
1046
1187
|
sendReasoning: options?.sendReasoning,
|
|
1047
|
-
sendSources: options?.sendSources
|
|
1188
|
+
sendSources: options?.sendSources,
|
|
1189
|
+
messageMetadata: options?.messageMetadata,
|
|
1190
|
+
onError: options?.onError
|
|
1048
1191
|
})
|
|
1049
1192
|
);
|
|
1050
1193
|
}
|
|
@@ -1175,7 +1318,7 @@ function chatRoute({
|
|
|
1175
1318
|
if (!agentToUse) {
|
|
1176
1319
|
throw new Error("Agent ID is required");
|
|
1177
1320
|
}
|
|
1178
|
-
const agentObj = mastra.
|
|
1321
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1179
1322
|
if (!agentObj) {
|
|
1180
1323
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1181
1324
|
}
|
|
@@ -1264,6 +1407,7 @@ function workflowRoute({
|
|
|
1264
1407
|
handler: async (c) => {
|
|
1265
1408
|
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1266
1409
|
const mastra = c.get("mastra");
|
|
1410
|
+
const requestContext = c.get("requestContext");
|
|
1267
1411
|
let workflowToUse = workflow;
|
|
1268
1412
|
if (!workflow) {
|
|
1269
1413
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1277,12 +1421,17 @@ function workflowRoute({
|
|
|
1277
1421
|
if (!workflowToUse) {
|
|
1278
1422
|
throw new Error("Workflow ID is required");
|
|
1279
1423
|
}
|
|
1280
|
-
const workflowObj = mastra.
|
|
1424
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1281
1425
|
if (!workflowObj) {
|
|
1282
1426
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1283
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
|
+
}
|
|
1284
1433
|
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1285
|
-
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 });
|
|
1286
1435
|
const uiMessageStream = ai.createUIMessageStream({
|
|
1287
1436
|
execute: async ({ writer }) => {
|
|
1288
1437
|
for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
|
|
@@ -1369,7 +1518,7 @@ function networkRoute({
|
|
|
1369
1518
|
if (!agentToUse) {
|
|
1370
1519
|
throw new Error("Agent ID is required");
|
|
1371
1520
|
}
|
|
1372
|
-
const agentObj = mastra.
|
|
1521
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1373
1522
|
if (!agentObj) {
|
|
1374
1523
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1375
1524
|
}
|