@mastra/ai-sdk 0.0.0-dynamic-model-router-20251010230835 → 0.0.0-export-agent-memory-from-local-studio-20251112153946
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 +193 -3
- package/README.md +65 -1
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/convert-messages.d.ts +10 -0
- package/dist/convert-messages.d.ts.map +1 -0
- package/dist/convert-streams.d.ts +18 -0
- package/dist/convert-streams.d.ts.map +1 -0
- package/dist/helpers.d.ts +2 -3
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +467 -95
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +465 -95
- 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 +15 -4
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +79 -32
- package/dist/transformers.d.ts.map +1 -1
- package/dist/ui.cjs +16 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.ts +2 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +13 -0
- package/dist/ui.js.map +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/workflow-route.d.ts +10 -0
- package/dist/workflow-route.d.ts.map +1 -0
- package/package.json +19 -6
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,33 @@ var ai = require('ai');
|
|
|
5
5
|
var stream = require('@mastra/core/stream');
|
|
6
6
|
|
|
7
7
|
// src/chat-route.ts
|
|
8
|
+
|
|
9
|
+
// src/utils.ts
|
|
10
|
+
var isDataChunkType = (chunk) => {
|
|
11
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
12
|
+
};
|
|
13
|
+
function safeParseErrorObject(obj) {
|
|
14
|
+
if (typeof obj !== "object" || obj === null) {
|
|
15
|
+
return String(obj);
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const stringified = JSON.stringify(obj);
|
|
19
|
+
if (stringified === "{}") {
|
|
20
|
+
return String(obj);
|
|
21
|
+
}
|
|
22
|
+
return stringified;
|
|
23
|
+
} catch {
|
|
24
|
+
return String(obj);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
var isAgentExecutionDataChunkType = (chunk) => {
|
|
28
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
29
|
+
};
|
|
30
|
+
var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
31
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/helpers.ts
|
|
8
35
|
function convertMastraChunkToAISDKv5({
|
|
9
36
|
chunk,
|
|
10
37
|
mode = "stream"
|
|
@@ -210,6 +237,9 @@ function convertMastraChunkToAISDKv5({
|
|
|
210
237
|
...chunk.payload || {}
|
|
211
238
|
};
|
|
212
239
|
}
|
|
240
|
+
if ("type" in chunk && chunk.type?.startsWith("data-")) {
|
|
241
|
+
return chunk;
|
|
242
|
+
}
|
|
213
243
|
return;
|
|
214
244
|
}
|
|
215
245
|
}
|
|
@@ -223,7 +253,7 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
223
253
|
sendFinish,
|
|
224
254
|
responseMessageId
|
|
225
255
|
}) {
|
|
226
|
-
const partType = part
|
|
256
|
+
const partType = part?.type;
|
|
227
257
|
switch (partType) {
|
|
228
258
|
case "text-start": {
|
|
229
259
|
return {
|
|
@@ -329,6 +359,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
329
359
|
toolCallId: part.toolCallId,
|
|
330
360
|
payload: part.output
|
|
331
361
|
};
|
|
362
|
+
} else if (isDataChunkType(part.output)) {
|
|
363
|
+
if (!("data" in part.output)) {
|
|
364
|
+
throw new Error(
|
|
365
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
366
|
+
${JSON.stringify(part)}`
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
return part.output;
|
|
332
370
|
}
|
|
333
371
|
return;
|
|
334
372
|
}
|
|
@@ -380,8 +418,16 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
380
418
|
return;
|
|
381
419
|
}
|
|
382
420
|
default: {
|
|
383
|
-
|
|
384
|
-
|
|
421
|
+
if (isDataChunkType(part)) {
|
|
422
|
+
if (!("data" in part)) {
|
|
423
|
+
throw new Error(
|
|
424
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
425
|
+
${JSON.stringify(part)}`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
return part;
|
|
429
|
+
}
|
|
430
|
+
return;
|
|
385
431
|
}
|
|
386
432
|
}
|
|
387
433
|
}
|
|
@@ -392,29 +438,40 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
392
438
|
return new TransformStream({
|
|
393
439
|
start(controller) {
|
|
394
440
|
controller.enqueue({
|
|
395
|
-
|
|
396
|
-
type: "start",
|
|
397
|
-
messageId: "1"
|
|
398
|
-
})
|
|
441
|
+
type: "start"
|
|
399
442
|
});
|
|
400
443
|
},
|
|
401
444
|
flush(controller) {
|
|
402
445
|
controller.enqueue({
|
|
403
|
-
|
|
404
|
-
type: "finish"
|
|
405
|
-
})
|
|
446
|
+
type: "finish"
|
|
406
447
|
});
|
|
448
|
+
},
|
|
449
|
+
transform(chunk, controller) {
|
|
450
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
451
|
+
if (transformed) controller.enqueue(transformed);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
function AgentNetworkToAISDKTransformer() {
|
|
456
|
+
const bufferedNetworks = /* @__PURE__ */ new Map();
|
|
457
|
+
return new TransformStream({
|
|
458
|
+
start(controller) {
|
|
459
|
+
controller.enqueue({
|
|
460
|
+
type: "start"
|
|
461
|
+
});
|
|
462
|
+
},
|
|
463
|
+
flush(controller) {
|
|
407
464
|
controller.enqueue({
|
|
408
|
-
|
|
465
|
+
type: "finish"
|
|
409
466
|
});
|
|
410
467
|
},
|
|
411
468
|
transform(chunk, controller) {
|
|
412
|
-
const transformed =
|
|
469
|
+
const transformed = transformNetwork(chunk, bufferedNetworks);
|
|
413
470
|
if (transformed) controller.enqueue(transformed);
|
|
414
471
|
}
|
|
415
472
|
});
|
|
416
473
|
}
|
|
417
|
-
function AgentStreamToAISDKTransformer() {
|
|
474
|
+
function AgentStreamToAISDKTransformer(lastMessageId) {
|
|
418
475
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
419
476
|
return new TransformStream({
|
|
420
477
|
transform(chunk, controller) {
|
|
@@ -425,9 +482,9 @@ function AgentStreamToAISDKTransformer() {
|
|
|
425
482
|
sendSources: false,
|
|
426
483
|
sendStart: true,
|
|
427
484
|
sendFinish: true,
|
|
428
|
-
responseMessageId:
|
|
429
|
-
onError() {
|
|
430
|
-
return
|
|
485
|
+
responseMessageId: lastMessageId,
|
|
486
|
+
onError(error) {
|
|
487
|
+
return safeParseErrorObject(error);
|
|
431
488
|
}
|
|
432
489
|
});
|
|
433
490
|
if (transformedChunk) {
|
|
@@ -437,11 +494,11 @@ function AgentStreamToAISDKTransformer() {
|
|
|
437
494
|
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
438
495
|
} else if (transformedChunk.type === "tool-workflow") {
|
|
439
496
|
const payload = transformedChunk.payload;
|
|
440
|
-
const workflowChunk = transformWorkflow(payload, bufferedSteps);
|
|
497
|
+
const workflowChunk = transformWorkflow(payload, bufferedSteps, true);
|
|
441
498
|
if (workflowChunk) controller.enqueue(workflowChunk);
|
|
442
499
|
} else if (transformedChunk.type === "tool-network") {
|
|
443
500
|
const payload = transformedChunk.payload;
|
|
444
|
-
const networkChunk = transformNetwork(payload, bufferedSteps);
|
|
501
|
+
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
445
502
|
if (networkChunk) controller.enqueue(networkChunk);
|
|
446
503
|
} else {
|
|
447
504
|
controller.enqueue(transformedChunk);
|
|
@@ -586,7 +643,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
586
643
|
}
|
|
587
644
|
return null;
|
|
588
645
|
}
|
|
589
|
-
function transformWorkflow(payload, bufferedWorkflows) {
|
|
646
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
590
647
|
switch (payload.type) {
|
|
591
648
|
case "workflow-start":
|
|
592
649
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -594,7 +651,7 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
594
651
|
steps: {}
|
|
595
652
|
});
|
|
596
653
|
return {
|
|
597
|
-
type: "data-workflow",
|
|
654
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
598
655
|
id: payload.runId,
|
|
599
656
|
data: {
|
|
600
657
|
name: bufferedWorkflows.get(payload.runId).name,
|
|
@@ -609,11 +666,13 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
609
666
|
name: payload.payload.id,
|
|
610
667
|
status: payload.payload.status,
|
|
611
668
|
input: payload.payload.payload ?? null,
|
|
612
|
-
output: null
|
|
669
|
+
output: null,
|
|
670
|
+
suspendPayload: null,
|
|
671
|
+
resumePayload: null
|
|
613
672
|
};
|
|
614
673
|
bufferedWorkflows.set(payload.runId, current);
|
|
615
674
|
return {
|
|
616
|
-
type: "data-workflow",
|
|
675
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
617
676
|
id: payload.runId,
|
|
618
677
|
data: {
|
|
619
678
|
name: current.name,
|
|
@@ -632,7 +691,7 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
632
691
|
output: payload.payload.output ?? null
|
|
633
692
|
};
|
|
634
693
|
return {
|
|
635
|
-
type: "data-workflow",
|
|
694
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
636
695
|
id: payload.runId,
|
|
637
696
|
data: {
|
|
638
697
|
name: current.name,
|
|
@@ -642,11 +701,32 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
642
701
|
}
|
|
643
702
|
};
|
|
644
703
|
}
|
|
704
|
+
case "workflow-step-suspended": {
|
|
705
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
706
|
+
if (!current) return null;
|
|
707
|
+
current.steps[payload.payload.id] = {
|
|
708
|
+
...current.steps[payload.payload.id],
|
|
709
|
+
status: payload.payload.status,
|
|
710
|
+
suspendPayload: payload.payload.suspendPayload ?? null,
|
|
711
|
+
resumePayload: payload.payload.resumePayload ?? null,
|
|
712
|
+
output: null
|
|
713
|
+
};
|
|
714
|
+
return {
|
|
715
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
716
|
+
id: payload.runId,
|
|
717
|
+
data: {
|
|
718
|
+
name: current.name,
|
|
719
|
+
status: "suspended",
|
|
720
|
+
steps: current.steps,
|
|
721
|
+
output: null
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
}
|
|
645
725
|
case "workflow-finish": {
|
|
646
726
|
const current = bufferedWorkflows.get(payload.runId);
|
|
647
727
|
if (!current) return null;
|
|
648
728
|
return {
|
|
649
|
-
type: "data-workflow",
|
|
729
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
650
730
|
id: payload.runId,
|
|
651
731
|
data: {
|
|
652
732
|
name: current.name,
|
|
@@ -656,142 +736,209 @@ function transformWorkflow(payload, bufferedWorkflows) {
|
|
|
656
736
|
}
|
|
657
737
|
};
|
|
658
738
|
}
|
|
659
|
-
default:
|
|
739
|
+
default: {
|
|
740
|
+
if (isDataChunkType(payload)) {
|
|
741
|
+
if (!("data" in payload)) {
|
|
742
|
+
throw new Error(
|
|
743
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
744
|
+
${JSON.stringify(payload)}`
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
return payload;
|
|
748
|
+
}
|
|
660
749
|
return null;
|
|
750
|
+
}
|
|
661
751
|
}
|
|
662
752
|
}
|
|
663
|
-
function transformNetwork(payload, bufferedNetworks) {
|
|
753
|
+
function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
664
754
|
switch (payload.type) {
|
|
665
755
|
case "routing-agent-start": {
|
|
666
|
-
bufferedNetworks.
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
756
|
+
if (!bufferedNetworks.has(payload.runId)) {
|
|
757
|
+
bufferedNetworks.set(payload.runId, {
|
|
758
|
+
name: payload.payload.agentId,
|
|
759
|
+
steps: [],
|
|
760
|
+
usage: null,
|
|
761
|
+
output: null
|
|
762
|
+
});
|
|
763
|
+
}
|
|
670
764
|
return {
|
|
671
|
-
type: "data-network",
|
|
672
|
-
id: payload.
|
|
765
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
766
|
+
id: payload.runId,
|
|
673
767
|
data: {
|
|
674
|
-
name: bufferedNetworks.get(payload.
|
|
768
|
+
name: bufferedNetworks.get(payload.runId).name,
|
|
675
769
|
status: "running",
|
|
676
|
-
|
|
770
|
+
usage: null,
|
|
771
|
+
steps: bufferedNetworks.get(payload.runId).steps,
|
|
677
772
|
output: null
|
|
678
773
|
}
|
|
679
774
|
};
|
|
680
775
|
}
|
|
776
|
+
case "routing-agent-text-start": {
|
|
777
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
778
|
+
if (!current) return null;
|
|
779
|
+
return {
|
|
780
|
+
type: "text-start",
|
|
781
|
+
id: payload.runId
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
case "routing-agent-text-delta": {
|
|
785
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
786
|
+
if (!current) return null;
|
|
787
|
+
return {
|
|
788
|
+
type: "text-delta",
|
|
789
|
+
id: payload.runId,
|
|
790
|
+
delta: payload.payload.text
|
|
791
|
+
};
|
|
792
|
+
}
|
|
681
793
|
case "agent-execution-start": {
|
|
682
|
-
const current = bufferedNetworks.get(payload.
|
|
794
|
+
const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
|
|
683
795
|
current.steps.push({
|
|
684
796
|
name: payload.payload.agentId,
|
|
685
797
|
status: "running",
|
|
686
798
|
input: payload.payload.args || null,
|
|
687
|
-
output: null
|
|
799
|
+
output: null,
|
|
800
|
+
suspendPayload: null,
|
|
801
|
+
resumePayload: null
|
|
688
802
|
});
|
|
689
|
-
bufferedNetworks.set(payload.
|
|
803
|
+
bufferedNetworks.set(payload.runId, current);
|
|
690
804
|
return {
|
|
691
|
-
type: "data-network",
|
|
692
|
-
id: payload.
|
|
805
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
806
|
+
id: payload.runId,
|
|
693
807
|
data: {
|
|
694
|
-
|
|
695
|
-
status: "running"
|
|
696
|
-
steps: current.steps,
|
|
697
|
-
output: null
|
|
808
|
+
...current,
|
|
809
|
+
status: "running"
|
|
698
810
|
}
|
|
699
811
|
};
|
|
700
812
|
}
|
|
701
813
|
case "workflow-execution-start": {
|
|
702
|
-
const current = bufferedNetworks.get(payload.
|
|
814
|
+
const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
|
|
703
815
|
current.steps.push({
|
|
704
816
|
name: payload.payload.name,
|
|
705
817
|
status: "running",
|
|
706
818
|
input: payload.payload.args || null,
|
|
707
|
-
output: null
|
|
819
|
+
output: null,
|
|
820
|
+
suspendPayload: null,
|
|
821
|
+
resumePayload: null
|
|
708
822
|
});
|
|
709
|
-
bufferedNetworks.set(payload.
|
|
823
|
+
bufferedNetworks.set(payload.runId, current);
|
|
710
824
|
return {
|
|
711
|
-
type: "data-network",
|
|
712
|
-
id: payload.
|
|
825
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
826
|
+
id: payload.runId,
|
|
713
827
|
data: {
|
|
714
|
-
|
|
715
|
-
status: "running"
|
|
716
|
-
steps: current.steps,
|
|
717
|
-
output: null
|
|
828
|
+
...current,
|
|
829
|
+
status: "running"
|
|
718
830
|
}
|
|
719
831
|
};
|
|
720
832
|
}
|
|
721
833
|
case "tool-execution-start": {
|
|
722
|
-
const current = bufferedNetworks.get(payload.
|
|
834
|
+
const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
|
|
723
835
|
current.steps.push({
|
|
724
836
|
name: payload.payload.args?.toolName,
|
|
725
837
|
status: "running",
|
|
726
838
|
input: payload.payload.args?.args || null,
|
|
727
|
-
output: null
|
|
839
|
+
output: null,
|
|
840
|
+
suspendPayload: null,
|
|
841
|
+
resumePayload: null
|
|
728
842
|
});
|
|
729
|
-
bufferedNetworks.set(payload.
|
|
843
|
+
bufferedNetworks.set(payload.runId, current);
|
|
730
844
|
return {
|
|
731
|
-
type: "data-network",
|
|
732
|
-
id: payload.
|
|
845
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
846
|
+
id: payload.runId,
|
|
733
847
|
data: {
|
|
734
|
-
|
|
848
|
+
...current,
|
|
849
|
+
status: "running"
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
case "agent-execution-end": {
|
|
854
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
855
|
+
if (!current) return null;
|
|
856
|
+
current.steps.push({
|
|
857
|
+
name: payload.payload.agentId,
|
|
858
|
+
status: "success",
|
|
859
|
+
input: null,
|
|
860
|
+
output: payload.payload.result,
|
|
861
|
+
suspendPayload: null,
|
|
862
|
+
resumePayload: null
|
|
863
|
+
});
|
|
864
|
+
return {
|
|
865
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
866
|
+
id: payload.runId,
|
|
867
|
+
data: {
|
|
868
|
+
...current,
|
|
869
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
735
870
|
status: "running",
|
|
736
|
-
|
|
737
|
-
output: null
|
|
871
|
+
output: payload.payload.result ?? current.output
|
|
738
872
|
}
|
|
739
873
|
};
|
|
740
874
|
}
|
|
741
|
-
case "agent-execution-end":
|
|
742
875
|
case "tool-execution-end": {
|
|
743
876
|
const current = bufferedNetworks.get(payload.runId);
|
|
744
877
|
if (!current) return null;
|
|
878
|
+
current.steps.push({
|
|
879
|
+
name: payload.payload.toolName,
|
|
880
|
+
status: "success",
|
|
881
|
+
input: null,
|
|
882
|
+
output: payload.payload.result,
|
|
883
|
+
suspendPayload: null,
|
|
884
|
+
resumePayload: null
|
|
885
|
+
});
|
|
745
886
|
return {
|
|
746
|
-
type: "data-network",
|
|
887
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
747
888
|
id: payload.runId,
|
|
748
889
|
data: {
|
|
749
|
-
|
|
890
|
+
...current,
|
|
750
891
|
status: "running",
|
|
751
|
-
|
|
752
|
-
output: null
|
|
892
|
+
output: payload.payload.result ?? current.output
|
|
753
893
|
}
|
|
754
894
|
};
|
|
755
895
|
}
|
|
756
896
|
case "workflow-execution-end": {
|
|
757
897
|
const current = bufferedNetworks.get(payload.runId);
|
|
758
898
|
if (!current) return null;
|
|
899
|
+
current.steps.push({
|
|
900
|
+
name: payload.payload.name,
|
|
901
|
+
status: "success",
|
|
902
|
+
input: null,
|
|
903
|
+
output: payload.payload.result,
|
|
904
|
+
suspendPayload: null,
|
|
905
|
+
resumePayload: null
|
|
906
|
+
});
|
|
759
907
|
return {
|
|
760
|
-
type: "data-network",
|
|
908
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
761
909
|
id: payload.runId,
|
|
762
910
|
data: {
|
|
763
|
-
|
|
911
|
+
...current,
|
|
912
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
764
913
|
status: "running",
|
|
765
|
-
|
|
766
|
-
output: null
|
|
914
|
+
output: payload.payload.result ?? current.output
|
|
767
915
|
}
|
|
768
916
|
};
|
|
769
917
|
}
|
|
770
918
|
case "routing-agent-end": {
|
|
771
|
-
const current = bufferedNetworks.get(payload.
|
|
919
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
772
920
|
if (!current) return null;
|
|
773
921
|
return {
|
|
774
|
-
type: "data-network",
|
|
775
|
-
id: payload.
|
|
922
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
923
|
+
id: payload.runId,
|
|
776
924
|
data: {
|
|
777
|
-
|
|
925
|
+
...current,
|
|
778
926
|
status: "finished",
|
|
779
|
-
|
|
780
|
-
output: payload.payload?.result ??
|
|
927
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
928
|
+
output: payload.payload?.result ?? current.output
|
|
781
929
|
}
|
|
782
930
|
};
|
|
783
931
|
}
|
|
784
932
|
case "network-execution-event-step-finish": {
|
|
785
|
-
const current = bufferedNetworks.get(payload.
|
|
933
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
786
934
|
if (!current) return null;
|
|
787
935
|
return {
|
|
788
|
-
type: "data-network",
|
|
789
|
-
id: payload.
|
|
936
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
937
|
+
id: payload.runId,
|
|
790
938
|
data: {
|
|
791
|
-
|
|
939
|
+
...current,
|
|
792
940
|
status: "finished",
|
|
793
|
-
|
|
794
|
-
output: payload.payload?.result ?? null
|
|
941
|
+
output: payload.payload?.result ?? current.output
|
|
795
942
|
}
|
|
796
943
|
};
|
|
797
944
|
}
|
|
@@ -799,24 +946,60 @@ function transformNetwork(payload, bufferedNetworks) {
|
|
|
799
946
|
const current = bufferedNetworks.get(payload.runId);
|
|
800
947
|
if (!current) return null;
|
|
801
948
|
return {
|
|
802
|
-
type: "data-network",
|
|
949
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
803
950
|
id: payload.runId,
|
|
804
951
|
data: {
|
|
805
|
-
|
|
952
|
+
...current,
|
|
953
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
806
954
|
status: "finished",
|
|
807
|
-
|
|
808
|
-
output: payload.payload?.result ?? null
|
|
955
|
+
output: payload.payload?.result ?? current.output
|
|
809
956
|
}
|
|
810
957
|
};
|
|
811
958
|
}
|
|
812
|
-
default:
|
|
959
|
+
default: {
|
|
960
|
+
if (isDataChunkType(payload)) {
|
|
961
|
+
if (!("data" in payload)) {
|
|
962
|
+
throw new Error(
|
|
963
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
964
|
+
${JSON.stringify(payload)}`
|
|
965
|
+
);
|
|
966
|
+
}
|
|
967
|
+
return payload;
|
|
968
|
+
}
|
|
969
|
+
if (isAgentExecutionDataChunkType(payload)) {
|
|
970
|
+
if (!("data" in payload.payload)) {
|
|
971
|
+
throw new Error(
|
|
972
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
973
|
+
${JSON.stringify(payload)}`
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
return payload.payload;
|
|
977
|
+
}
|
|
978
|
+
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
979
|
+
if (!("data" in payload.payload)) {
|
|
980
|
+
throw new Error(
|
|
981
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
982
|
+
${JSON.stringify(payload)}`
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
return payload.payload;
|
|
986
|
+
}
|
|
813
987
|
return null;
|
|
988
|
+
}
|
|
814
989
|
}
|
|
815
990
|
}
|
|
816
991
|
|
|
817
|
-
// src/
|
|
818
|
-
function
|
|
819
|
-
|
|
992
|
+
// src/convert-streams.ts
|
|
993
|
+
function toAISdkV5Stream(stream, options = { from: "agent" }) {
|
|
994
|
+
const from = options?.from;
|
|
995
|
+
if (from === "workflow") {
|
|
996
|
+
return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
|
|
997
|
+
}
|
|
998
|
+
if (from === "network") {
|
|
999
|
+
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
1000
|
+
}
|
|
1001
|
+
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
1002
|
+
return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
|
|
820
1003
|
}
|
|
821
1004
|
|
|
822
1005
|
// src/chat-route.ts
|
|
@@ -924,7 +1107,7 @@ function chatRoute({
|
|
|
924
1107
|
handler: async (c) => {
|
|
925
1108
|
const { messages, ...rest } = await c.req.json();
|
|
926
1109
|
const mastra = c.get("mastra");
|
|
927
|
-
const
|
|
1110
|
+
const requestContext = c.get("requestContext");
|
|
928
1111
|
let agentToUse = agent;
|
|
929
1112
|
if (!agent) {
|
|
930
1113
|
const agentId = c.req.param("agentId");
|
|
@@ -935,8 +1118,8 @@ function chatRoute({
|
|
|
935
1118
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
936
1119
|
);
|
|
937
1120
|
}
|
|
938
|
-
if (
|
|
939
|
-
mastra.getLogger()?.warn(`"
|
|
1121
|
+
if (requestContext && defaultOptions?.requestContext) {
|
|
1122
|
+
mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
|
|
940
1123
|
}
|
|
941
1124
|
if (!agentToUse) {
|
|
942
1125
|
throw new Error("Agent ID is required");
|
|
@@ -948,11 +1131,16 @@ function chatRoute({
|
|
|
948
1131
|
const result = await agentObj.stream(messages, {
|
|
949
1132
|
...defaultOptions,
|
|
950
1133
|
...rest,
|
|
951
|
-
|
|
1134
|
+
requestContext: requestContext || defaultOptions?.requestContext
|
|
952
1135
|
});
|
|
1136
|
+
let lastMessageId;
|
|
1137
|
+
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
1138
|
+
lastMessageId = messages[messages.length - 1].id;
|
|
1139
|
+
}
|
|
953
1140
|
const uiMessageStream = ai.createUIMessageStream({
|
|
1141
|
+
originalMessages: messages,
|
|
954
1142
|
execute: async ({ writer }) => {
|
|
955
|
-
for await (const part of
|
|
1143
|
+
for await (const part of toAISdkV5Stream(result, { from: "agent", lastMessageId })) {
|
|
956
1144
|
writer.write(part);
|
|
957
1145
|
}
|
|
958
1146
|
}
|
|
@@ -963,9 +1151,193 @@ function chatRoute({
|
|
|
963
1151
|
}
|
|
964
1152
|
});
|
|
965
1153
|
}
|
|
1154
|
+
function workflowRoute({
|
|
1155
|
+
path = "/api/workflows/:workflowId/stream",
|
|
1156
|
+
workflow
|
|
1157
|
+
}) {
|
|
1158
|
+
if (!workflow && !path.includes("/:workflowId")) {
|
|
1159
|
+
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
1160
|
+
}
|
|
1161
|
+
return server.registerApiRoute(path, {
|
|
1162
|
+
method: "POST",
|
|
1163
|
+
openapi: {
|
|
1164
|
+
summary: "Stream a workflow in AI SDK format",
|
|
1165
|
+
description: "Starts a workflow run and streams events as AI SDK UIMessage chunks",
|
|
1166
|
+
tags: ["ai-sdk"],
|
|
1167
|
+
parameters: [
|
|
1168
|
+
{
|
|
1169
|
+
name: "workflowId",
|
|
1170
|
+
in: "path",
|
|
1171
|
+
required: true,
|
|
1172
|
+
description: "The ID of the workflow to stream",
|
|
1173
|
+
schema: { type: "string" }
|
|
1174
|
+
}
|
|
1175
|
+
],
|
|
1176
|
+
requestBody: {
|
|
1177
|
+
required: true,
|
|
1178
|
+
content: {
|
|
1179
|
+
"application/json": {
|
|
1180
|
+
schema: {
|
|
1181
|
+
type: "object",
|
|
1182
|
+
properties: {
|
|
1183
|
+
inputData: { type: "object", additionalProperties: true },
|
|
1184
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1185
|
+
tracingOptions: { type: "object", additionalProperties: true }
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
},
|
|
1191
|
+
responses: {
|
|
1192
|
+
"200": {
|
|
1193
|
+
description: "Workflow UIMessage event stream",
|
|
1194
|
+
content: {
|
|
1195
|
+
"text/plain": {
|
|
1196
|
+
schema: { type: "string", description: "SSE stream" }
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
},
|
|
1202
|
+
handler: async (c) => {
|
|
1203
|
+
const { inputData, resumeData, ...rest } = await c.req.json();
|
|
1204
|
+
const mastra = c.get("mastra");
|
|
1205
|
+
let workflowToUse = workflow;
|
|
1206
|
+
if (!workflow) {
|
|
1207
|
+
const workflowId = c.req.param("workflowId");
|
|
1208
|
+
workflowToUse = workflowId;
|
|
1209
|
+
}
|
|
1210
|
+
if (c.req.param("workflowId") && workflow) {
|
|
1211
|
+
mastra.getLogger()?.warn(
|
|
1212
|
+
`Fixed workflow ID was set together with a workflowId path parameter. This can lead to unexpected behavior.`
|
|
1213
|
+
);
|
|
1214
|
+
}
|
|
1215
|
+
if (!workflowToUse) {
|
|
1216
|
+
throw new Error("Workflow ID is required");
|
|
1217
|
+
}
|
|
1218
|
+
const workflowObj = mastra.getWorkflow(workflowToUse);
|
|
1219
|
+
if (!workflowObj) {
|
|
1220
|
+
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1221
|
+
}
|
|
1222
|
+
const run = await workflowObj.createRun();
|
|
1223
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1224
|
+
const uiMessageStream = ai.createUIMessageStream({
|
|
1225
|
+
execute: async ({ writer }) => {
|
|
1226
|
+
for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
|
|
1227
|
+
writer.write(part);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
});
|
|
1231
|
+
return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1232
|
+
}
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
function networkRoute({
|
|
1236
|
+
path = "/network/:agentId",
|
|
1237
|
+
agent,
|
|
1238
|
+
defaultOptions
|
|
1239
|
+
}) {
|
|
1240
|
+
if (!agent && !path.includes("/:agentId")) {
|
|
1241
|
+
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
1242
|
+
}
|
|
1243
|
+
return server.registerApiRoute(path, {
|
|
1244
|
+
method: "POST",
|
|
1245
|
+
openapi: {
|
|
1246
|
+
summary: "Execute an agent network and stream AI SDK events",
|
|
1247
|
+
description: "Routes a request to an agent network and streams UIMessage chunks in AI SDK format",
|
|
1248
|
+
tags: ["ai-sdk"],
|
|
1249
|
+
parameters: [
|
|
1250
|
+
{
|
|
1251
|
+
name: "agentId",
|
|
1252
|
+
in: "path",
|
|
1253
|
+
required: true,
|
|
1254
|
+
description: "The ID of the routing agent to execute as a network",
|
|
1255
|
+
schema: { type: "string" }
|
|
1256
|
+
}
|
|
1257
|
+
],
|
|
1258
|
+
requestBody: {
|
|
1259
|
+
required: true,
|
|
1260
|
+
content: {
|
|
1261
|
+
"application/json": {
|
|
1262
|
+
schema: {
|
|
1263
|
+
type: "object",
|
|
1264
|
+
properties: {
|
|
1265
|
+
messages: { type: "array", items: { type: "object" } },
|
|
1266
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1267
|
+
runId: { type: "string" },
|
|
1268
|
+
maxSteps: { type: "number" },
|
|
1269
|
+
threadId: { type: "string" },
|
|
1270
|
+
resourceId: { type: "string" },
|
|
1271
|
+
modelSettings: { type: "object", additionalProperties: true },
|
|
1272
|
+
tools: { type: "array", items: { type: "object" } }
|
|
1273
|
+
},
|
|
1274
|
+
required: ["messages"]
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
},
|
|
1279
|
+
responses: {
|
|
1280
|
+
"200": {
|
|
1281
|
+
description: "Streaming AI SDK UIMessage event stream for the agent network",
|
|
1282
|
+
content: { "text/plain": { schema: { type: "string", description: "SSE stream" } } }
|
|
1283
|
+
},
|
|
1284
|
+
"404": {
|
|
1285
|
+
description: "Agent not found",
|
|
1286
|
+
content: {
|
|
1287
|
+
"application/json": {
|
|
1288
|
+
schema: { type: "object", properties: { error: { type: "string" } } }
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
},
|
|
1294
|
+
handler: async (c) => {
|
|
1295
|
+
const { messages, ...rest } = await c.req.json();
|
|
1296
|
+
const mastra = c.get("mastra");
|
|
1297
|
+
let agentToUse = agent;
|
|
1298
|
+
if (!agent) {
|
|
1299
|
+
const agentId = c.req.param("agentId");
|
|
1300
|
+
agentToUse = agentId;
|
|
1301
|
+
}
|
|
1302
|
+
if (c.req.param("agentId") && agent) {
|
|
1303
|
+
mastra.getLogger()?.warn(
|
|
1304
|
+
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
if (!agentToUse) {
|
|
1308
|
+
throw new Error("Agent ID is required");
|
|
1309
|
+
}
|
|
1310
|
+
const agentObj = mastra.getAgent(agentToUse);
|
|
1311
|
+
if (!agentObj) {
|
|
1312
|
+
throw new Error(`Agent ${agentToUse} not found`);
|
|
1313
|
+
}
|
|
1314
|
+
const result = await agentObj.network(messages, {
|
|
1315
|
+
...defaultOptions,
|
|
1316
|
+
...rest
|
|
1317
|
+
});
|
|
1318
|
+
const uiMessageStream = ai.createUIMessageStream({
|
|
1319
|
+
execute: async ({ writer }) => {
|
|
1320
|
+
for await (const part of toAISdkV5Stream(result, { from: "network" })) {
|
|
1321
|
+
writer.write(part);
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
});
|
|
1325
|
+
return ai.createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1326
|
+
}
|
|
1327
|
+
});
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
// src/to-ai-sdk-format.ts
|
|
1331
|
+
function toAISdkFormat() {
|
|
1332
|
+
throw new Error(
|
|
1333
|
+
'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.'
|
|
1334
|
+
);
|
|
1335
|
+
}
|
|
966
1336
|
|
|
967
|
-
exports.WorkflowStreamToAISDKTransformer = WorkflowStreamToAISDKTransformer;
|
|
968
1337
|
exports.chatRoute = chatRoute;
|
|
1338
|
+
exports.networkRoute = networkRoute;
|
|
969
1339
|
exports.toAISdkFormat = toAISdkFormat;
|
|
1340
|
+
exports.toAISdkStream = toAISdkV5Stream;
|
|
1341
|
+
exports.workflowRoute = workflowRoute;
|
|
970
1342
|
//# sourceMappingURL=index.cjs.map
|
|
971
1343
|
//# sourceMappingURL=index.cjs.map
|