@mastra/ai-sdk 0.2.7 → 0.3.2-alpha.0
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 +132 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts +2329 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -0
- package/dist/chat-route.d.ts +52 -2
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/helpers.d.ts +2 -3
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +324 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +324 -62
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts.map +1 -1
- package/dist/to-ai-sdk-format.d.ts +66 -1
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +136 -10
- package/dist/transformers.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/workflow-route.d.ts +3 -1
- package/dist/workflow-route.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -8,6 +8,45 @@ import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core
|
|
|
8
8
|
var isDataChunkType = (chunk) => {
|
|
9
9
|
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
10
10
|
};
|
|
11
|
+
var isMastraTextStreamChunk = (chunk) => {
|
|
12
|
+
return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
|
|
13
|
+
"text-start",
|
|
14
|
+
"text-delta",
|
|
15
|
+
"text-end",
|
|
16
|
+
"reasoning-start",
|
|
17
|
+
"reasoning-delta",
|
|
18
|
+
"reasoning-end",
|
|
19
|
+
"file",
|
|
20
|
+
"source",
|
|
21
|
+
"tool-input-start",
|
|
22
|
+
"tool-input-delta",
|
|
23
|
+
"tool-call",
|
|
24
|
+
"tool-result",
|
|
25
|
+
"tool-error",
|
|
26
|
+
"error",
|
|
27
|
+
"start-step",
|
|
28
|
+
"finish-step",
|
|
29
|
+
"start",
|
|
30
|
+
"finish",
|
|
31
|
+
"abort",
|
|
32
|
+
"tool-input-end",
|
|
33
|
+
"raw"
|
|
34
|
+
].includes(chunk.type);
|
|
35
|
+
};
|
|
36
|
+
function safeParseErrorObject(obj) {
|
|
37
|
+
if (typeof obj !== "object" || obj === null) {
|
|
38
|
+
return String(obj);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const stringified = JSON.stringify(obj);
|
|
42
|
+
if (stringified === "{}") {
|
|
43
|
+
return String(obj);
|
|
44
|
+
}
|
|
45
|
+
return stringified;
|
|
46
|
+
} catch {
|
|
47
|
+
return String(obj);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
11
50
|
var isAgentExecutionDataChunkType = (chunk) => {
|
|
12
51
|
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-");
|
|
13
52
|
};
|
|
@@ -124,6 +163,28 @@ function convertMastraChunkToAISDKv5({
|
|
|
124
163
|
toolName: chunk.payload.toolName,
|
|
125
164
|
input: chunk.payload.args
|
|
126
165
|
};
|
|
166
|
+
case "tool-call-approval":
|
|
167
|
+
return {
|
|
168
|
+
type: "data-tool-call-approval",
|
|
169
|
+
id: chunk.payload.toolCallId,
|
|
170
|
+
data: {
|
|
171
|
+
runId: chunk.runId,
|
|
172
|
+
toolCallId: chunk.payload.toolCallId,
|
|
173
|
+
toolName: chunk.payload.toolName,
|
|
174
|
+
args: chunk.payload.args
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
case "tool-call-suspended":
|
|
178
|
+
return {
|
|
179
|
+
type: "data-tool-call-suspended",
|
|
180
|
+
id: chunk.payload.toolCallId,
|
|
181
|
+
data: {
|
|
182
|
+
runId: chunk.runId,
|
|
183
|
+
toolCallId: chunk.payload.toolCallId,
|
|
184
|
+
toolName: chunk.payload.toolName,
|
|
185
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
186
|
+
}
|
|
187
|
+
};
|
|
127
188
|
case "tool-call-input-streaming-start":
|
|
128
189
|
return {
|
|
129
190
|
type: "tool-input-start",
|
|
@@ -214,6 +275,13 @@ function convertMastraChunkToAISDKv5({
|
|
|
214
275
|
type: "object",
|
|
215
276
|
object: chunk.object
|
|
216
277
|
};
|
|
278
|
+
case "tripwire":
|
|
279
|
+
return {
|
|
280
|
+
type: "data-tripwire",
|
|
281
|
+
data: {
|
|
282
|
+
tripwireReason: chunk.payload.tripwireReason
|
|
283
|
+
}
|
|
284
|
+
};
|
|
217
285
|
default:
|
|
218
286
|
if (chunk.type && "payload" in chunk && chunk.payload) {
|
|
219
287
|
return {
|
|
@@ -269,6 +337,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
269
337
|
};
|
|
270
338
|
}
|
|
271
339
|
case "reasoning-delta": {
|
|
340
|
+
if (sendReasoning) {
|
|
341
|
+
return {
|
|
342
|
+
type: "reasoning-delta",
|
|
343
|
+
id: part.id,
|
|
344
|
+
delta: part.text,
|
|
345
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
272
348
|
return;
|
|
273
349
|
}
|
|
274
350
|
case "reasoning-end": {
|
|
@@ -286,6 +362,25 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
286
362
|
};
|
|
287
363
|
}
|
|
288
364
|
case "source": {
|
|
365
|
+
if (sendSources && part.sourceType === "url") {
|
|
366
|
+
return {
|
|
367
|
+
type: "source-url",
|
|
368
|
+
sourceId: part.id,
|
|
369
|
+
url: part.url,
|
|
370
|
+
title: part.title,
|
|
371
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
if (sendSources && part.sourceType === "document") {
|
|
375
|
+
return {
|
|
376
|
+
type: "source-document",
|
|
377
|
+
sourceId: part.id,
|
|
378
|
+
mediaType: part.mediaType,
|
|
379
|
+
title: part.title,
|
|
380
|
+
filename: part.filename,
|
|
381
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
289
384
|
return;
|
|
290
385
|
}
|
|
291
386
|
case "tool-input-start": {
|
|
@@ -376,21 +471,23 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
376
471
|
return { type: "finish-step" };
|
|
377
472
|
}
|
|
378
473
|
case "start": {
|
|
379
|
-
{
|
|
474
|
+
if (sendStart) {
|
|
380
475
|
return {
|
|
381
476
|
type: "start",
|
|
382
477
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
383
478
|
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
384
479
|
};
|
|
385
480
|
}
|
|
481
|
+
return;
|
|
386
482
|
}
|
|
387
483
|
case "finish": {
|
|
388
|
-
{
|
|
484
|
+
if (sendFinish) {
|
|
389
485
|
return {
|
|
390
486
|
type: "finish",
|
|
391
487
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
392
488
|
};
|
|
393
489
|
}
|
|
490
|
+
return;
|
|
394
491
|
}
|
|
395
492
|
case "abort": {
|
|
396
493
|
return part;
|
|
@@ -417,7 +514,10 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
417
514
|
}
|
|
418
515
|
|
|
419
516
|
// src/transformers.ts
|
|
420
|
-
|
|
517
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
518
|
+
function WorkflowStreamToAISDKTransformer({
|
|
519
|
+
includeTextStreamParts
|
|
520
|
+
} = {}) {
|
|
421
521
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
422
522
|
return new TransformStream({
|
|
423
523
|
start(controller) {
|
|
@@ -431,7 +531,7 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
431
531
|
});
|
|
432
532
|
},
|
|
433
533
|
transform(chunk, controller) {
|
|
434
|
-
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
534
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
435
535
|
if (transformed) controller.enqueue(transformed);
|
|
436
536
|
}
|
|
437
537
|
});
|
|
@@ -455,20 +555,37 @@ function AgentNetworkToAISDKTransformer() {
|
|
|
455
555
|
}
|
|
456
556
|
});
|
|
457
557
|
}
|
|
458
|
-
function AgentStreamToAISDKTransformer(
|
|
558
|
+
function AgentStreamToAISDKTransformer({
|
|
559
|
+
lastMessageId,
|
|
560
|
+
sendStart,
|
|
561
|
+
sendFinish,
|
|
562
|
+
sendReasoning,
|
|
563
|
+
sendSources,
|
|
564
|
+
messageMetadata,
|
|
565
|
+
onError
|
|
566
|
+
}) {
|
|
459
567
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
568
|
+
let tripwireOccurred = false;
|
|
569
|
+
let finishEventSent = false;
|
|
460
570
|
return new TransformStream({
|
|
461
571
|
transform(chunk, controller) {
|
|
572
|
+
if (chunk.type === "tripwire") {
|
|
573
|
+
tripwireOccurred = true;
|
|
574
|
+
}
|
|
575
|
+
if (chunk.type === "finish") {
|
|
576
|
+
finishEventSent = true;
|
|
577
|
+
}
|
|
462
578
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
463
579
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
464
580
|
part,
|
|
465
|
-
sendReasoning
|
|
466
|
-
sendSources
|
|
467
|
-
|
|
468
|
-
|
|
581
|
+
sendReasoning,
|
|
582
|
+
sendSources,
|
|
583
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
584
|
+
sendStart,
|
|
585
|
+
sendFinish,
|
|
469
586
|
responseMessageId: lastMessageId,
|
|
470
|
-
onError() {
|
|
471
|
-
return
|
|
587
|
+
onError(error) {
|
|
588
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
472
589
|
}
|
|
473
590
|
});
|
|
474
591
|
if (transformedChunk) {
|
|
@@ -488,6 +605,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
|
|
|
488
605
|
controller.enqueue(transformedChunk);
|
|
489
606
|
}
|
|
490
607
|
}
|
|
608
|
+
},
|
|
609
|
+
flush(controller) {
|
|
610
|
+
if (tripwireOccurred && !finishEventSent && sendFinish) {
|
|
611
|
+
controller.enqueue({
|
|
612
|
+
type: "finish",
|
|
613
|
+
finishReason: "other"
|
|
614
|
+
});
|
|
615
|
+
}
|
|
491
616
|
}
|
|
492
617
|
});
|
|
493
618
|
}
|
|
@@ -627,7 +752,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
627
752
|
}
|
|
628
753
|
return null;
|
|
629
754
|
}
|
|
630
|
-
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
755
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
631
756
|
switch (payload.type) {
|
|
632
757
|
case "workflow-start":
|
|
633
758
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -720,6 +845,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
720
845
|
}
|
|
721
846
|
};
|
|
722
847
|
}
|
|
848
|
+
case "workflow-step-output": {
|
|
849
|
+
const output = payload.payload.output;
|
|
850
|
+
if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
|
|
851
|
+
const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
|
|
852
|
+
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
853
|
+
part,
|
|
854
|
+
onError(error) {
|
|
855
|
+
return safeParseErrorObject(error);
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
return transformedChunk;
|
|
859
|
+
}
|
|
860
|
+
if (output && isDataChunkType(output)) {
|
|
861
|
+
if (!("data" in output)) {
|
|
862
|
+
throw new Error(
|
|
863
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
864
|
+
${JSON.stringify(output)}`
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
return output;
|
|
868
|
+
}
|
|
869
|
+
return null;
|
|
870
|
+
}
|
|
723
871
|
default: {
|
|
724
872
|
if (isDataChunkType(payload)) {
|
|
725
873
|
if (!("data" in payload)) {
|
|
@@ -739,12 +887,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
739
887
|
case "routing-agent-start": {
|
|
740
888
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
741
889
|
bufferedNetworks.set(payload.runId, {
|
|
742
|
-
name: payload.payload.
|
|
890
|
+
name: payload.payload.networkId,
|
|
743
891
|
steps: [],
|
|
744
892
|
usage: null,
|
|
745
893
|
output: null
|
|
746
894
|
});
|
|
747
895
|
}
|
|
896
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
897
|
+
current.steps.push({
|
|
898
|
+
id: payload.payload.runId,
|
|
899
|
+
name: payload.payload.agentId,
|
|
900
|
+
status: "running",
|
|
901
|
+
iteration: payload.payload.inputData.iteration,
|
|
902
|
+
input: {
|
|
903
|
+
task: payload.payload.inputData.task,
|
|
904
|
+
threadId: payload.payload.inputData.threadId,
|
|
905
|
+
threadResourceId: payload.payload.inputData.threadResourceId
|
|
906
|
+
},
|
|
907
|
+
output: "",
|
|
908
|
+
task: null,
|
|
909
|
+
suspendPayload: null,
|
|
910
|
+
resumePayload: null,
|
|
911
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
912
|
+
});
|
|
748
913
|
return {
|
|
749
914
|
type: isNested ? "data-tool-network" : "data-network",
|
|
750
915
|
id: payload.runId,
|
|
@@ -775,14 +940,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
775
940
|
};
|
|
776
941
|
}
|
|
777
942
|
case "agent-execution-start": {
|
|
778
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
943
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
944
|
+
if (!current) return null;
|
|
779
945
|
current.steps.push({
|
|
946
|
+
id: payload.payload.runId,
|
|
780
947
|
name: payload.payload.agentId,
|
|
781
948
|
status: "running",
|
|
782
|
-
|
|
949
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
950
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
783
951
|
output: null,
|
|
952
|
+
task: null,
|
|
784
953
|
suspendPayload: null,
|
|
785
|
-
resumePayload: null
|
|
954
|
+
resumePayload: null,
|
|
955
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
786
956
|
});
|
|
787
957
|
bufferedNetworks.set(payload.runId, current);
|
|
788
958
|
return {
|
|
@@ -795,14 +965,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
795
965
|
};
|
|
796
966
|
}
|
|
797
967
|
case "workflow-execution-start": {
|
|
798
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
968
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
969
|
+
if (!current) return null;
|
|
799
970
|
current.steps.push({
|
|
800
|
-
|
|
971
|
+
id: payload.payload.runId,
|
|
972
|
+
name: payload.payload.workflowId,
|
|
801
973
|
status: "running",
|
|
802
|
-
|
|
974
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
975
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
803
976
|
output: null,
|
|
977
|
+
task: null,
|
|
804
978
|
suspendPayload: null,
|
|
805
|
-
resumePayload: null
|
|
979
|
+
resumePayload: null,
|
|
980
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
806
981
|
});
|
|
807
982
|
bufferedNetworks.set(payload.runId, current);
|
|
808
983
|
return {
|
|
@@ -815,14 +990,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
815
990
|
};
|
|
816
991
|
}
|
|
817
992
|
case "tool-execution-start": {
|
|
818
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
993
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
994
|
+
if (!current) return null;
|
|
819
995
|
current.steps.push({
|
|
996
|
+
id: payload.payload.args.toolCallId,
|
|
820
997
|
name: payload.payload.args?.toolName,
|
|
821
998
|
status: "running",
|
|
999
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
1000
|
+
task: {
|
|
1001
|
+
id: payload.payload.args?.toolName
|
|
1002
|
+
},
|
|
822
1003
|
input: payload.payload.args?.args || null,
|
|
823
1004
|
output: null,
|
|
824
1005
|
suspendPayload: null,
|
|
825
|
-
resumePayload: null
|
|
1006
|
+
resumePayload: null,
|
|
1007
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
826
1008
|
});
|
|
827
1009
|
bufferedNetworks.set(payload.runId, current);
|
|
828
1010
|
return {
|
|
@@ -837,14 +1019,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
837
1019
|
case "agent-execution-end": {
|
|
838
1020
|
const current = bufferedNetworks.get(payload.runId);
|
|
839
1021
|
if (!current) return null;
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
});
|
|
1022
|
+
const stepId = payload.payload.runId;
|
|
1023
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1024
|
+
if (!step) {
|
|
1025
|
+
return null;
|
|
1026
|
+
}
|
|
1027
|
+
step.status = "success";
|
|
1028
|
+
step.output = payload.payload.result;
|
|
848
1029
|
return {
|
|
849
1030
|
type: isNested ? "data-tool-network" : "data-network",
|
|
850
1031
|
id: payload.runId,
|
|
@@ -859,14 +1040,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
859
1040
|
case "tool-execution-end": {
|
|
860
1041
|
const current = bufferedNetworks.get(payload.runId);
|
|
861
1042
|
if (!current) return null;
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
});
|
|
1043
|
+
const stepId = payload.payload.toolCallId;
|
|
1044
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1045
|
+
if (!step) {
|
|
1046
|
+
return null;
|
|
1047
|
+
}
|
|
1048
|
+
step.status = "success";
|
|
1049
|
+
step.output = payload.payload.result;
|
|
870
1050
|
return {
|
|
871
1051
|
type: isNested ? "data-tool-network" : "data-network",
|
|
872
1052
|
id: payload.runId,
|
|
@@ -880,14 +1060,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
880
1060
|
case "workflow-execution-end": {
|
|
881
1061
|
const current = bufferedNetworks.get(payload.runId);
|
|
882
1062
|
if (!current) return null;
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
});
|
|
1063
|
+
const stepId = payload.payload.runId;
|
|
1064
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1065
|
+
if (!step) {
|
|
1066
|
+
return null;
|
|
1067
|
+
}
|
|
1068
|
+
step.status = "success";
|
|
1069
|
+
step.output = payload.payload.result;
|
|
891
1070
|
return {
|
|
892
1071
|
type: isNested ? "data-tool-network" : "data-network",
|
|
893
1072
|
id: payload.runId,
|
|
@@ -902,12 +1081,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
902
1081
|
case "routing-agent-end": {
|
|
903
1082
|
const current = bufferedNetworks.get(payload.runId);
|
|
904
1083
|
if (!current) return null;
|
|
1084
|
+
const stepId = payload.payload.runId;
|
|
1085
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1086
|
+
if (!step) {
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
step.status = "success";
|
|
1090
|
+
step.task = {
|
|
1091
|
+
id: payload.payload.primitiveId,
|
|
1092
|
+
type: payload.payload.primitiveType,
|
|
1093
|
+
name: payload.payload.task,
|
|
1094
|
+
reason: payload.payload.selectionReason
|
|
1095
|
+
};
|
|
1096
|
+
step.output = payload.payload.result;
|
|
905
1097
|
return {
|
|
906
1098
|
type: isNested ? "data-tool-network" : "data-network",
|
|
907
1099
|
id: payload.runId,
|
|
908
1100
|
data: {
|
|
909
1101
|
...current,
|
|
910
|
-
status: "finished",
|
|
911
1102
|
usage: payload.payload?.usage ?? current.usage,
|
|
912
1103
|
output: payload.payload?.result ?? current.output
|
|
913
1104
|
}
|
|
@@ -941,6 +1132,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
941
1132
|
};
|
|
942
1133
|
}
|
|
943
1134
|
default: {
|
|
1135
|
+
if (payload.type.startsWith("agent-execution-event-")) {
|
|
1136
|
+
const stepId = payload.payload.runId;
|
|
1137
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1138
|
+
if (!current) return null;
|
|
1139
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1140
|
+
if (!step) {
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1143
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1144
|
+
const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1145
|
+
if (result) {
|
|
1146
|
+
const { request, response, ...data } = result.data;
|
|
1147
|
+
step.task = data;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
if (payload.type.startsWith("workflow-execution-event-")) {
|
|
1151
|
+
const stepId = payload.payload.runId;
|
|
1152
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1153
|
+
if (!current) return null;
|
|
1154
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1155
|
+
if (!step) {
|
|
1156
|
+
return null;
|
|
1157
|
+
}
|
|
1158
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1159
|
+
const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1160
|
+
if (result && "data" in result) {
|
|
1161
|
+
const data = result.data;
|
|
1162
|
+
step.task = data;
|
|
1163
|
+
if (data.name && step.task) {
|
|
1164
|
+
step.task.id = data.name;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
944
1168
|
if (isDataChunkType(payload)) {
|
|
945
1169
|
if (!("data" in payload)) {
|
|
946
1170
|
throw new Error(
|
|
@@ -948,7 +1172,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
948
1172
|
${JSON.stringify(payload)}`
|
|
949
1173
|
);
|
|
950
1174
|
}
|
|
951
|
-
|
|
1175
|
+
const { type, data } = payload;
|
|
1176
|
+
return { type, data };
|
|
952
1177
|
}
|
|
953
1178
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
954
1179
|
if (!("data" in payload.payload)) {
|
|
@@ -957,7 +1182,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
957
1182
|
${JSON.stringify(payload)}`
|
|
958
1183
|
);
|
|
959
1184
|
}
|
|
960
|
-
|
|
1185
|
+
const { type, data } = payload.payload;
|
|
1186
|
+
return { type, data };
|
|
961
1187
|
}
|
|
962
1188
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
963
1189
|
if (!("data" in payload.payload)) {
|
|
@@ -966,7 +1192,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
966
1192
|
${JSON.stringify(payload)}`
|
|
967
1193
|
);
|
|
968
1194
|
}
|
|
969
|
-
|
|
1195
|
+
const { type, data } = payload.payload;
|
|
1196
|
+
return { type, data };
|
|
970
1197
|
}
|
|
971
1198
|
return null;
|
|
972
1199
|
}
|
|
@@ -974,23 +1201,44 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
974
1201
|
}
|
|
975
1202
|
|
|
976
1203
|
// src/to-ai-sdk-format.ts
|
|
977
|
-
function toAISdkFormat(stream, options = {
|
|
1204
|
+
function toAISdkFormat(stream, options = {
|
|
1205
|
+
from: "agent",
|
|
1206
|
+
sendStart: true,
|
|
1207
|
+
sendFinish: true
|
|
1208
|
+
}) {
|
|
978
1209
|
const from = options?.from;
|
|
979
1210
|
if (from === "workflow") {
|
|
980
|
-
|
|
1211
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? false;
|
|
1212
|
+
return stream.pipeThrough(
|
|
1213
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1214
|
+
);
|
|
981
1215
|
}
|
|
982
1216
|
if (from === "network") {
|
|
983
1217
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
984
1218
|
}
|
|
985
1219
|
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
986
|
-
return agentReadable.pipeThrough(
|
|
1220
|
+
return agentReadable.pipeThrough(
|
|
1221
|
+
AgentStreamToAISDKTransformer({
|
|
1222
|
+
lastMessageId: options?.lastMessageId,
|
|
1223
|
+
sendStart: options?.sendStart,
|
|
1224
|
+
sendFinish: options?.sendFinish,
|
|
1225
|
+
sendReasoning: options?.sendReasoning,
|
|
1226
|
+
sendSources: options?.sendSources,
|
|
1227
|
+
messageMetadata: options?.messageMetadata,
|
|
1228
|
+
onError: options?.onError
|
|
1229
|
+
})
|
|
1230
|
+
);
|
|
987
1231
|
}
|
|
988
1232
|
|
|
989
1233
|
// src/chat-route.ts
|
|
990
1234
|
function chatRoute({
|
|
991
1235
|
path = "/chat/:agentId",
|
|
992
1236
|
agent,
|
|
993
|
-
defaultOptions
|
|
1237
|
+
defaultOptions,
|
|
1238
|
+
sendStart = true,
|
|
1239
|
+
sendFinish = true,
|
|
1240
|
+
sendReasoning = false,
|
|
1241
|
+
sendSources = false
|
|
994
1242
|
}) {
|
|
995
1243
|
if (!agent && !path.includes("/:agentId")) {
|
|
996
1244
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -1108,7 +1356,7 @@ function chatRoute({
|
|
|
1108
1356
|
if (!agentToUse) {
|
|
1109
1357
|
throw new Error("Agent ID is required");
|
|
1110
1358
|
}
|
|
1111
|
-
const agentObj = mastra.
|
|
1359
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1112
1360
|
if (!agentObj) {
|
|
1113
1361
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1114
1362
|
}
|
|
@@ -1124,7 +1372,14 @@ function chatRoute({
|
|
|
1124
1372
|
const uiMessageStream = createUIMessageStream({
|
|
1125
1373
|
originalMessages: messages,
|
|
1126
1374
|
execute: async ({ writer }) => {
|
|
1127
|
-
for await (const part of toAISdkFormat(result, {
|
|
1375
|
+
for await (const part of toAISdkFormat(result, {
|
|
1376
|
+
from: "agent",
|
|
1377
|
+
lastMessageId,
|
|
1378
|
+
sendStart,
|
|
1379
|
+
sendFinish,
|
|
1380
|
+
sendReasoning,
|
|
1381
|
+
sendSources
|
|
1382
|
+
})) {
|
|
1128
1383
|
writer.write(part);
|
|
1129
1384
|
}
|
|
1130
1385
|
}
|
|
@@ -1137,7 +1392,8 @@ function chatRoute({
|
|
|
1137
1392
|
}
|
|
1138
1393
|
function workflowRoute({
|
|
1139
1394
|
path = "/api/workflows/:workflowId/stream",
|
|
1140
|
-
workflow
|
|
1395
|
+
workflow,
|
|
1396
|
+
includeTextStreamParts = false
|
|
1141
1397
|
}) {
|
|
1142
1398
|
if (!workflow && !path.includes("/:workflowId")) {
|
|
1143
1399
|
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
@@ -1168,7 +1424,7 @@ function workflowRoute({
|
|
|
1168
1424
|
resourceId: { type: "string" },
|
|
1169
1425
|
inputData: { type: "object", additionalProperties: true },
|
|
1170
1426
|
resumeData: { type: "object", additionalProperties: true },
|
|
1171
|
-
|
|
1427
|
+
runtimeContext: { type: "object", additionalProperties: true },
|
|
1172
1428
|
tracingOptions: { type: "object", additionalProperties: true },
|
|
1173
1429
|
step: { type: "string" }
|
|
1174
1430
|
}
|
|
@@ -1190,6 +1446,7 @@ function workflowRoute({
|
|
|
1190
1446
|
handler: async (c) => {
|
|
1191
1447
|
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1192
1448
|
const mastra = c.get("mastra");
|
|
1449
|
+
const runtimeContext = c.get("runtimeContext");
|
|
1193
1450
|
let workflowToUse = workflow;
|
|
1194
1451
|
if (!workflow) {
|
|
1195
1452
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1203,15 +1460,20 @@ function workflowRoute({
|
|
|
1203
1460
|
if (!workflowToUse) {
|
|
1204
1461
|
throw new Error("Workflow ID is required");
|
|
1205
1462
|
}
|
|
1206
|
-
const workflowObj = mastra.
|
|
1463
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1207
1464
|
if (!workflowObj) {
|
|
1208
1465
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1209
1466
|
}
|
|
1467
|
+
if (runtimeContext && rest.runtimeContext) {
|
|
1468
|
+
mastra.getLogger()?.warn(
|
|
1469
|
+
`"runtimeContext" from the request body will be ignored because "runtimeContext" is already set in the route options.`
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1210
1472
|
const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
|
|
1211
|
-
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1473
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext }) : run.stream({ inputData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext });
|
|
1212
1474
|
const uiMessageStream = createUIMessageStream({
|
|
1213
1475
|
execute: async ({ writer }) => {
|
|
1214
|
-
for await (const part of toAISdkFormat(stream, { from: "workflow" })) {
|
|
1476
|
+
for await (const part of toAISdkFormat(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1215
1477
|
writer.write(part);
|
|
1216
1478
|
}
|
|
1217
1479
|
}
|
|
@@ -1296,7 +1558,7 @@ function networkRoute({
|
|
|
1296
1558
|
if (!agentToUse) {
|
|
1297
1559
|
throw new Error("Agent ID is required");
|
|
1298
1560
|
}
|
|
1299
|
-
const agentObj = mastra.
|
|
1561
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1300
1562
|
if (!agentObj) {
|
|
1301
1563
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1302
1564
|
}
|