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