@mastra/ai-sdk 0.2.0-alpha.0 → 0.2.0-alpha.1
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 +15 -0
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/index.cjs +272 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +271 -38
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts +14 -0
- package/dist/network-route.d.ts.map +1 -0
- package/dist/to-ai-sdk-format.d.ts +12 -3
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +36 -15
- package/dist/transformers.d.ts.map +1 -1
- package/dist/workflow-route.d.ts +10 -0
- package/dist/workflow-route.d.ts.map +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @mastra/ai-sdk
|
|
2
2
|
|
|
3
|
+
## 0.2.0-alpha.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Update peer dependencies to match core package version bump (0.20.3) ([#8672](https://github.com/mastra-ai/mastra/pull/8672))
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- pass runtimeContext to agent stream options in chatRoute ([#8641](https://github.com/mastra-ai/mastra/pull/8641))
|
|
12
|
+
|
|
13
|
+
- ai-sdk workflow route, agent network route ([#8672](https://github.com/mastra-ai/mastra/pull/8672))
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [[`1ed9670`](https://github.com/mastra-ai/mastra/commit/1ed9670d3ca50cb60dc2e517738c5eef3968ed27), [`158381d`](https://github.com/mastra-ai/mastra/commit/158381d39335be934b81ef8a1947bccace492c25), [`fb703b9`](https://github.com/mastra-ai/mastra/commit/fb703b9634eeaff1a6eb2b5531ce0f9e8fb04727), [`37a2314`](https://github.com/mastra-ai/mastra/commit/37a23148e0e5a3b40d4f9f098b194671a8a49faf), [`05a9dee`](https://github.com/mastra-ai/mastra/commit/05a9dee3d355694d28847bfffb6289657fcf7dfa), [`e3c1077`](https://github.com/mastra-ai/mastra/commit/e3c107763aedd1643d3def5df450c235da9ff76c), [`1bccdb3`](https://github.com/mastra-ai/mastra/commit/1bccdb33eb90cbeba2dc5ece1c2561fb774b26b6), [`5ef944a`](https://github.com/mastra-ai/mastra/commit/5ef944a3721d93105675cac2b2311432ff8cc393), [`d6b186f`](https://github.com/mastra-ai/mastra/commit/d6b186fb08f1caf1b86f73d3a5ee88fb999ca3be), [`65493b3`](https://github.com/mastra-ai/mastra/commit/65493b31c36f6fdb78f9679f7e1ecf0c250aa5ee), [`a998b8f`](https://github.com/mastra-ai/mastra/commit/a998b8f858091c2ec47683e60766cf12d03001e4), [`8a37bdd`](https://github.com/mastra-ai/mastra/commit/8a37bddb6d8614a32c5b70303d583d80c620ea61)]:
|
|
16
|
+
- @mastra/core@0.21.0-alpha.1
|
|
17
|
+
|
|
3
18
|
## 0.2.0-alpha.0
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
package/dist/chat-route.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAIxD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,CAAC;AAEF,wBAAgB,SAAS,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAuJhE"}
|
package/dist/index.cjs
CHANGED
|
@@ -392,24 +392,35 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
392
392
|
return new TransformStream({
|
|
393
393
|
start(controller) {
|
|
394
394
|
controller.enqueue({
|
|
395
|
-
|
|
396
|
-
type: "start",
|
|
397
|
-
messageId: "1"
|
|
398
|
-
})
|
|
395
|
+
type: "start"
|
|
399
396
|
});
|
|
400
397
|
},
|
|
401
398
|
flush(controller) {
|
|
402
399
|
controller.enqueue({
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
400
|
+
type: "finish"
|
|
401
|
+
});
|
|
402
|
+
},
|
|
403
|
+
transform(chunk, controller) {
|
|
404
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
405
|
+
if (transformed) controller.enqueue(transformed);
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
function AgentNetworkToAISDKTransformer() {
|
|
410
|
+
const bufferedNetworks = /* @__PURE__ */ new Map();
|
|
411
|
+
return new TransformStream({
|
|
412
|
+
start(controller) {
|
|
413
|
+
controller.enqueue({
|
|
414
|
+
type: "start"
|
|
406
415
|
});
|
|
416
|
+
},
|
|
417
|
+
flush(controller) {
|
|
407
418
|
controller.enqueue({
|
|
408
|
-
|
|
419
|
+
type: "finish"
|
|
409
420
|
});
|
|
410
421
|
},
|
|
411
422
|
transform(chunk, controller) {
|
|
412
|
-
const transformed =
|
|
423
|
+
const transformed = transformNetwork(chunk, bufferedNetworks);
|
|
413
424
|
if (transformed) controller.enqueue(transformed);
|
|
414
425
|
}
|
|
415
426
|
});
|
|
@@ -437,11 +448,11 @@ function AgentStreamToAISDKTransformer() {
|
|
|
437
448
|
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
438
449
|
} else if (transformedChunk.type === "tool-workflow") {
|
|
439
450
|
const payload = transformedChunk.payload;
|
|
440
|
-
const workflowChunk = transformWorkflow(payload, bufferedSteps);
|
|
451
|
+
const workflowChunk = transformWorkflow(payload, bufferedSteps, true);
|
|
441
452
|
if (workflowChunk) controller.enqueue(workflowChunk);
|
|
442
453
|
} else if (transformedChunk.type === "tool-network") {
|
|
443
454
|
const payload = transformedChunk.payload;
|
|
444
|
-
const networkChunk = transformNetwork(payload, bufferedSteps);
|
|
455
|
+
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
445
456
|
if (networkChunk) controller.enqueue(networkChunk);
|
|
446
457
|
} else {
|
|
447
458
|
controller.enqueue(transformedChunk);
|
|
@@ -586,7 +597,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
586
597
|
}
|
|
587
598
|
return null;
|
|
588
599
|
}
|
|
589
|
-
function transformWorkflow(payload, bufferedWorkflows) {
|
|
600
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
590
601
|
switch (payload.type) {
|
|
591
602
|
case "workflow-start":
|
|
592
603
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -594,7 +605,7 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
594
605
|
steps: {}
|
|
595
606
|
});
|
|
596
607
|
return {
|
|
597
|
-
type: "data-workflow",
|
|
608
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
598
609
|
id: payload.runId,
|
|
599
610
|
data: {
|
|
600
611
|
name: bufferedWorkflows.get(payload.runId).name,
|
|
@@ -613,7 +624,7 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
613
624
|
};
|
|
614
625
|
bufferedWorkflows.set(payload.runId, current);
|
|
615
626
|
return {
|
|
616
|
-
type: "data-workflow",
|
|
627
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
617
628
|
id: payload.runId,
|
|
618
629
|
data: {
|
|
619
630
|
name: current.name,
|
|
@@ -632,7 +643,7 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
632
643
|
output: payload.payload.output ?? null
|
|
633
644
|
};
|
|
634
645
|
return {
|
|
635
|
-
type: "data-workflow",
|
|
646
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
636
647
|
id: payload.runId,
|
|
637
648
|
data: {
|
|
638
649
|
name: current.name,
|
|
@@ -646,7 +657,7 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
646
657
|
const current = bufferedWorkflows.get(payload.runId);
|
|
647
658
|
if (!current) return null;
|
|
648
659
|
return {
|
|
649
|
-
type: "data-workflow",
|
|
660
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
650
661
|
id: payload.runId,
|
|
651
662
|
data: {
|
|
652
663
|
name: current.name,
|
|
@@ -660,15 +671,17 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
660
671
|
return null;
|
|
661
672
|
}
|
|
662
673
|
}
|
|
663
|
-
function transformNetwork(payload, bufferedNetworks) {
|
|
674
|
+
function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
664
675
|
switch (payload.type) {
|
|
665
676
|
case "routing-agent-start": {
|
|
666
|
-
bufferedNetworks.
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
677
|
+
if (!bufferedNetworks.has(payload.payload.runId)) {
|
|
678
|
+
bufferedNetworks.set(payload.payload.runId, {
|
|
679
|
+
name: payload.payload.agentId,
|
|
680
|
+
steps: []
|
|
681
|
+
});
|
|
682
|
+
}
|
|
670
683
|
return {
|
|
671
|
-
type: "data-network",
|
|
684
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
672
685
|
id: payload.payload.runId,
|
|
673
686
|
data: {
|
|
674
687
|
name: bufferedNetworks.get(payload.payload.runId).name,
|
|
@@ -688,7 +701,7 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
688
701
|
});
|
|
689
702
|
bufferedNetworks.set(payload.payload.runId, current);
|
|
690
703
|
return {
|
|
691
|
-
type: "data-network",
|
|
704
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
692
705
|
id: payload.payload.runId,
|
|
693
706
|
data: {
|
|
694
707
|
name: current.name,
|
|
@@ -708,7 +721,7 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
708
721
|
});
|
|
709
722
|
bufferedNetworks.set(payload.payload.runId, current);
|
|
710
723
|
return {
|
|
711
|
-
type: "data-network",
|
|
724
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
712
725
|
id: payload.payload.runId,
|
|
713
726
|
data: {
|
|
714
727
|
name: current.name,
|
|
@@ -728,7 +741,7 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
728
741
|
});
|
|
729
742
|
bufferedNetworks.set(payload.payload.runId, current);
|
|
730
743
|
return {
|
|
731
|
-
type: "data-network",
|
|
744
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
732
745
|
id: payload.payload.runId,
|
|
733
746
|
data: {
|
|
734
747
|
name: current.name,
|
|
@@ -738,32 +751,63 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
738
751
|
}
|
|
739
752
|
};
|
|
740
753
|
}
|
|
741
|
-
case "agent-execution-end":
|
|
754
|
+
case "agent-execution-end": {
|
|
755
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
756
|
+
if (!current) return null;
|
|
757
|
+
current.steps.push({
|
|
758
|
+
name: payload.payload.agentId,
|
|
759
|
+
status: "success",
|
|
760
|
+
input: null,
|
|
761
|
+
output: payload.payload.result
|
|
762
|
+
});
|
|
763
|
+
return {
|
|
764
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
765
|
+
id: payload.runId,
|
|
766
|
+
data: {
|
|
767
|
+
name: current.name,
|
|
768
|
+
status: "running",
|
|
769
|
+
steps: current.steps,
|
|
770
|
+
output: payload.payload.result ?? null
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
}
|
|
742
774
|
case "tool-execution-end": {
|
|
743
775
|
const current = bufferedNetworks.get(payload.runId);
|
|
744
776
|
if (!current) return null;
|
|
777
|
+
current.steps.push({
|
|
778
|
+
name: payload.payload.toolName,
|
|
779
|
+
status: "success",
|
|
780
|
+
input: null,
|
|
781
|
+
output: payload.payload.result
|
|
782
|
+
});
|
|
745
783
|
return {
|
|
746
|
-
type: "data-network",
|
|
784
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
747
785
|
id: payload.runId,
|
|
748
786
|
data: {
|
|
749
787
|
name: current.name,
|
|
750
788
|
status: "running",
|
|
751
789
|
steps: current.steps,
|
|
752
|
-
output: null
|
|
790
|
+
output: payload.payload.result ?? null
|
|
753
791
|
}
|
|
754
792
|
};
|
|
755
793
|
}
|
|
756
794
|
case "workflow-execution-end": {
|
|
757
795
|
const current = bufferedNetworks.get(payload.runId);
|
|
758
796
|
if (!current) return null;
|
|
797
|
+
current.steps.push({
|
|
798
|
+
name: payload.payload.name,
|
|
799
|
+
status: "success",
|
|
800
|
+
input: null,
|
|
801
|
+
output: payload.payload.result
|
|
802
|
+
});
|
|
759
803
|
return {
|
|
760
|
-
type: "data-network",
|
|
804
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
761
805
|
id: payload.runId,
|
|
762
806
|
data: {
|
|
763
807
|
name: current.name,
|
|
764
808
|
status: "running",
|
|
765
809
|
steps: current.steps,
|
|
766
|
-
output: null
|
|
810
|
+
output: payload.payload.result ?? null
|
|
767
811
|
}
|
|
768
812
|
};
|
|
769
813
|
}
|
|
@@ -771,7 +815,7 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
771
815
|
const current = bufferedNetworks.get(payload.payload.runId);
|
|
772
816
|
if (!current) return null;
|
|
773
817
|
return {
|
|
774
|
-
type: "data-network",
|
|
818
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
775
819
|
id: payload.payload.runId,
|
|
776
820
|
data: {
|
|
777
821
|
name: current.name,
|
|
@@ -785,7 +829,7 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
785
829
|
const current = bufferedNetworks.get(payload.payload.runId);
|
|
786
830
|
if (!current) return null;
|
|
787
831
|
return {
|
|
788
|
-
type: "data-network",
|
|
832
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
789
833
|
id: payload.payload.runId,
|
|
790
834
|
data: {
|
|
791
835
|
name: current.name,
|
|
@@ -799,7 +843,7 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
799
843
|
const current = bufferedNetworks.get(payload.runId);
|
|
800
844
|
if (!current) return null;
|
|
801
845
|
return {
|
|
802
|
-
type: "data-network",
|
|
846
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
803
847
|
id: payload.runId,
|
|
804
848
|
data: {
|
|
805
849
|
name: current.name,
|
|
@@ -815,8 +859,16 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
815
859
|
}
|
|
816
860
|
|
|
817
861
|
// src/to-ai-sdk-format.ts
|
|
818
|
-
function toAISdkFormat(stream) {
|
|
819
|
-
|
|
862
|
+
function toAISdkFormat(stream, options = { from: "agent" }) {
|
|
863
|
+
const from = options?.from;
|
|
864
|
+
if (from === "workflow") {
|
|
865
|
+
return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
|
|
866
|
+
}
|
|
867
|
+
if (from === "network") {
|
|
868
|
+
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
869
|
+
}
|
|
870
|
+
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
871
|
+
return agentReadable.pipeThrough(AgentStreamToAISDKTransformer());
|
|
820
872
|
}
|
|
821
873
|
|
|
822
874
|
// src/chat-route.ts
|
|
@@ -924,6 +976,7 @@ function chatRoute({
|
|
|
924
976
|
handler: async (c) => {
|
|
925
977
|
const { messages, ...rest } = await c.req.json();
|
|
926
978
|
const mastra = c.get("mastra");
|
|
979
|
+
const runtimeContext = c.get("runtimeContext");
|
|
927
980
|
let agentToUse = agent;
|
|
928
981
|
if (!agent) {
|
|
929
982
|
const agentId = c.req.param("agentId");
|
|
@@ -934,6 +987,9 @@ function chatRoute({
|
|
|
934
987
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
935
988
|
);
|
|
936
989
|
}
|
|
990
|
+
if (runtimeContext && defaultOptions?.runtimeContext) {
|
|
991
|
+
mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
|
|
992
|
+
}
|
|
937
993
|
if (!agentToUse) {
|
|
938
994
|
throw new Error("Agent ID is required");
|
|
939
995
|
}
|
|
@@ -943,11 +999,12 @@ function chatRoute({
|
|
|
943
999
|
}
|
|
944
1000
|
const result = await agentObj.stream(messages, {
|
|
945
1001
|
...defaultOptions,
|
|
946
|
-
...rest
|
|
1002
|
+
...rest,
|
|
1003
|
+
runtimeContext: runtimeContext || defaultOptions?.runtimeContext
|
|
947
1004
|
});
|
|
948
1005
|
const uiMessageStream = ai.createUIMessageStream({
|
|
949
1006
|
execute: async ({ writer }) => {
|
|
950
|
-
for await (const part of toAISdkFormat(result)) {
|
|
1007
|
+
for await (const part of toAISdkFormat(result, { from: "agent" })) {
|
|
951
1008
|
writer.write(part);
|
|
952
1009
|
}
|
|
953
1010
|
}
|
|
@@ -958,9 +1015,186 @@ function chatRoute({
|
|
|
958
1015
|
}
|
|
959
1016
|
});
|
|
960
1017
|
}
|
|
1018
|
+
function workflowRoute({
|
|
1019
|
+
path = "/api/workflows/:workflowId/stream",
|
|
1020
|
+
workflow
|
|
1021
|
+
}) {
|
|
1022
|
+
if (!workflow && !path.includes("/:workflowId")) {
|
|
1023
|
+
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
1024
|
+
}
|
|
1025
|
+
return server.registerApiRoute(path, {
|
|
1026
|
+
method: "POST",
|
|
1027
|
+
openapi: {
|
|
1028
|
+
summary: "Stream a workflow in AI SDK format",
|
|
1029
|
+
description: "Starts a workflow run and streams events as AI SDK UIMessage chunks",
|
|
1030
|
+
tags: ["ai-sdk"],
|
|
1031
|
+
parameters: [
|
|
1032
|
+
{
|
|
1033
|
+
name: "workflowId",
|
|
1034
|
+
in: "path",
|
|
1035
|
+
required: true,
|
|
1036
|
+
description: "The ID of the workflow to stream",
|
|
1037
|
+
schema: { type: "string" }
|
|
1038
|
+
}
|
|
1039
|
+
],
|
|
1040
|
+
requestBody: {
|
|
1041
|
+
required: true,
|
|
1042
|
+
content: {
|
|
1043
|
+
"application/json": {
|
|
1044
|
+
schema: {
|
|
1045
|
+
type: "object",
|
|
1046
|
+
properties: {
|
|
1047
|
+
inputData: { type: "object", additionalProperties: true },
|
|
1048
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1049
|
+
tracingOptions: { type: "object", additionalProperties: true }
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
},
|
|
1055
|
+
responses: {
|
|
1056
|
+
"200": {
|
|
1057
|
+
description: "Workflow UIMessage event stream",
|
|
1058
|
+
content: {
|
|
1059
|
+
"text/plain": {
|
|
1060
|
+
schema: { type: "string", description: "SSE stream" }
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
},
|
|
1066
|
+
handler: async (c) => {
|
|
1067
|
+
const { inputData, ...rest } = await c.req.json();
|
|
1068
|
+
const mastra = c.get("mastra");
|
|
1069
|
+
let workflowToUse = workflow;
|
|
1070
|
+
if (!workflow) {
|
|
1071
|
+
const workflowId = c.req.param("workflowId");
|
|
1072
|
+
workflowToUse = workflowId;
|
|
1073
|
+
}
|
|
1074
|
+
if (c.req.param("workflowId") && workflow) {
|
|
1075
|
+
mastra.getLogger()?.warn(
|
|
1076
|
+
`Fixed workflow ID was set together with a workflowId path parameter. This can lead to unexpected behavior.`
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
if (!workflowToUse) {
|
|
1080
|
+
throw new Error("Workflow ID is required");
|
|
1081
|
+
}
|
|
1082
|
+
const workflowObj = mastra.getWorkflow(workflowToUse);
|
|
1083
|
+
if (!workflowObj) {
|
|
1084
|
+
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1085
|
+
}
|
|
1086
|
+
const run = await workflowObj.createRunAsync();
|
|
1087
|
+
const stream = run.streamVNext({ inputData, ...rest });
|
|
1088
|
+
const uiMessageStream = ai.createUIMessageStream({
|
|
1089
|
+
execute: async ({ writer }) => {
|
|
1090
|
+
for await (const part of toAISdkFormat(stream, { from: "workflow" })) {
|
|
1091
|
+
writer.write(part);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
});
|
|
1095
|
+
return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
function networkRoute({
|
|
1100
|
+
path = "/network/:agentId",
|
|
1101
|
+
agent,
|
|
1102
|
+
defaultOptions
|
|
1103
|
+
}) {
|
|
1104
|
+
if (!agent && !path.includes("/:agentId")) {
|
|
1105
|
+
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
1106
|
+
}
|
|
1107
|
+
return server.registerApiRoute(path, {
|
|
1108
|
+
method: "POST",
|
|
1109
|
+
openapi: {
|
|
1110
|
+
summary: "Execute an agent network and stream AI SDK events",
|
|
1111
|
+
description: "Routes a request to an agent network and streams UIMessage chunks in AI SDK format",
|
|
1112
|
+
tags: ["ai-sdk"],
|
|
1113
|
+
parameters: [
|
|
1114
|
+
{
|
|
1115
|
+
name: "agentId",
|
|
1116
|
+
in: "path",
|
|
1117
|
+
required: true,
|
|
1118
|
+
description: "The ID of the routing agent to execute as a network",
|
|
1119
|
+
schema: { type: "string" }
|
|
1120
|
+
}
|
|
1121
|
+
],
|
|
1122
|
+
requestBody: {
|
|
1123
|
+
required: true,
|
|
1124
|
+
content: {
|
|
1125
|
+
"application/json": {
|
|
1126
|
+
schema: {
|
|
1127
|
+
type: "object",
|
|
1128
|
+
properties: {
|
|
1129
|
+
messages: { type: "array", items: { type: "object" } },
|
|
1130
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1131
|
+
runId: { type: "string" },
|
|
1132
|
+
maxSteps: { type: "number" },
|
|
1133
|
+
threadId: { type: "string" },
|
|
1134
|
+
resourceId: { type: "string" },
|
|
1135
|
+
modelSettings: { type: "object", additionalProperties: true },
|
|
1136
|
+
telemetry: { type: "object", additionalProperties: true },
|
|
1137
|
+
tools: { type: "array", items: { type: "object" } }
|
|
1138
|
+
},
|
|
1139
|
+
required: ["messages"]
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
},
|
|
1144
|
+
responses: {
|
|
1145
|
+
"200": {
|
|
1146
|
+
description: "Streaming AI SDK UIMessage event stream for the agent network",
|
|
1147
|
+
content: { "text/plain": { schema: { type: "string", description: "SSE stream" } } }
|
|
1148
|
+
},
|
|
1149
|
+
"404": {
|
|
1150
|
+
description: "Agent not found",
|
|
1151
|
+
content: {
|
|
1152
|
+
"application/json": {
|
|
1153
|
+
schema: { type: "object", properties: { error: { type: "string" } } }
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
},
|
|
1159
|
+
handler: async (c) => {
|
|
1160
|
+
const { messages, ...rest } = await c.req.json();
|
|
1161
|
+
const mastra = c.get("mastra");
|
|
1162
|
+
let agentToUse = agent;
|
|
1163
|
+
if (!agent) {
|
|
1164
|
+
const agentId = c.req.param("agentId");
|
|
1165
|
+
agentToUse = agentId;
|
|
1166
|
+
}
|
|
1167
|
+
if (c.req.param("agentId") && agent) {
|
|
1168
|
+
mastra.getLogger()?.warn(
|
|
1169
|
+
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1170
|
+
);
|
|
1171
|
+
}
|
|
1172
|
+
if (!agentToUse) {
|
|
1173
|
+
throw new Error("Agent ID is required");
|
|
1174
|
+
}
|
|
1175
|
+
const agentObj = mastra.getAgent(agentToUse);
|
|
1176
|
+
if (!agentObj) {
|
|
1177
|
+
throw new Error(`Agent ${agentToUse} not found`);
|
|
1178
|
+
}
|
|
1179
|
+
const result = await agentObj.network(messages, {
|
|
1180
|
+
...defaultOptions,
|
|
1181
|
+
...rest
|
|
1182
|
+
});
|
|
1183
|
+
const uiMessageStream = ai.createUIMessageStream({
|
|
1184
|
+
execute: async ({ writer }) => {
|
|
1185
|
+
for await (const part of toAISdkFormat(result, { from: "network" })) {
|
|
1186
|
+
writer.write(part);
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
});
|
|
1190
|
+
return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1191
|
+
}
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
961
1194
|
|
|
962
|
-
exports.WorkflowStreamToAISDKTransformer = WorkflowStreamToAISDKTransformer;
|
|
963
1195
|
exports.chatRoute = chatRoute;
|
|
1196
|
+
exports.networkRoute = networkRoute;
|
|
964
1197
|
exports.toAISdkFormat = toAISdkFormat;
|
|
1198
|
+
exports.workflowRoute = workflowRoute;
|
|
965
1199
|
//# sourceMappingURL=index.cjs.map
|
|
966
1200
|
//# sourceMappingURL=index.cjs.map
|