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