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