@mastra/ai-sdk 0.0.0-main-test-05-11-2025-2-20251106053353 → 0.0.0-main-test-2-20251127210604
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 +310 -3
- 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/convert-messages.d.ts +1 -1
- package/dist/convert-messages.d.ts.map +1 -1
- package/dist/convert-streams.d.ts +65 -1
- package/dist/convert-streams.d.ts.map +1 -1
- package/dist/helpers.d.ts +1 -1
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +323 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +323 -63
- package/dist/index.js.map +1 -1
- package/dist/network-route.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 +2 -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 +8 -6
package/dist/index.js
CHANGED
|
@@ -8,6 +8,31 @@ 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
|
+
};
|
|
11
36
|
function safeParseErrorObject(obj) {
|
|
12
37
|
if (typeof obj !== "object" || obj === null) {
|
|
13
38
|
return String(obj);
|
|
@@ -138,6 +163,28 @@ function convertMastraChunkToAISDKv5({
|
|
|
138
163
|
toolName: chunk.payload.toolName,
|
|
139
164
|
input: chunk.payload.args
|
|
140
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
|
+
};
|
|
141
188
|
case "tool-call-input-streaming-start":
|
|
142
189
|
return {
|
|
143
190
|
type: "tool-input-start",
|
|
@@ -228,6 +275,13 @@ function convertMastraChunkToAISDKv5({
|
|
|
228
275
|
type: "object",
|
|
229
276
|
object: chunk.object
|
|
230
277
|
};
|
|
278
|
+
case "tripwire":
|
|
279
|
+
return {
|
|
280
|
+
type: "data-tripwire",
|
|
281
|
+
data: {
|
|
282
|
+
tripwireReason: chunk.payload.tripwireReason
|
|
283
|
+
}
|
|
284
|
+
};
|
|
231
285
|
default:
|
|
232
286
|
if (chunk.type && "payload" in chunk && chunk.payload) {
|
|
233
287
|
return {
|
|
@@ -283,6 +337,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
283
337
|
};
|
|
284
338
|
}
|
|
285
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
|
+
}
|
|
286
348
|
return;
|
|
287
349
|
}
|
|
288
350
|
case "reasoning-end": {
|
|
@@ -300,6 +362,25 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
300
362
|
};
|
|
301
363
|
}
|
|
302
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
|
+
}
|
|
303
384
|
return;
|
|
304
385
|
}
|
|
305
386
|
case "tool-input-start": {
|
|
@@ -357,6 +438,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
357
438
|
toolCallId: part.toolCallId,
|
|
358
439
|
payload: part.output
|
|
359
440
|
};
|
|
441
|
+
} else if (isDataChunkType(part.output)) {
|
|
442
|
+
if (!("data" in part.output)) {
|
|
443
|
+
throw new Error(
|
|
444
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
445
|
+
${JSON.stringify(part)}`
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return part.output;
|
|
360
449
|
}
|
|
361
450
|
return;
|
|
362
451
|
}
|
|
@@ -382,21 +471,23 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
382
471
|
return { type: "finish-step" };
|
|
383
472
|
}
|
|
384
473
|
case "start": {
|
|
385
|
-
{
|
|
474
|
+
if (sendStart) {
|
|
386
475
|
return {
|
|
387
476
|
type: "start",
|
|
388
477
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
389
478
|
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
390
479
|
};
|
|
391
480
|
}
|
|
481
|
+
return;
|
|
392
482
|
}
|
|
393
483
|
case "finish": {
|
|
394
|
-
{
|
|
484
|
+
if (sendFinish) {
|
|
395
485
|
return {
|
|
396
486
|
type: "finish",
|
|
397
487
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
398
488
|
};
|
|
399
489
|
}
|
|
490
|
+
return;
|
|
400
491
|
}
|
|
401
492
|
case "abort": {
|
|
402
493
|
return part;
|
|
@@ -423,7 +514,10 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
423
514
|
}
|
|
424
515
|
|
|
425
516
|
// src/transformers.ts
|
|
426
|
-
|
|
517
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
518
|
+
function WorkflowStreamToAISDKTransformer({
|
|
519
|
+
includeTextStreamParts
|
|
520
|
+
} = {}) {
|
|
427
521
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
428
522
|
return new TransformStream({
|
|
429
523
|
start(controller) {
|
|
@@ -437,7 +531,7 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
437
531
|
});
|
|
438
532
|
},
|
|
439
533
|
transform(chunk, controller) {
|
|
440
|
-
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
534
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
441
535
|
if (transformed) controller.enqueue(transformed);
|
|
442
536
|
}
|
|
443
537
|
});
|
|
@@ -461,20 +555,37 @@ function AgentNetworkToAISDKTransformer() {
|
|
|
461
555
|
}
|
|
462
556
|
});
|
|
463
557
|
}
|
|
464
|
-
function AgentStreamToAISDKTransformer(
|
|
558
|
+
function AgentStreamToAISDKTransformer({
|
|
559
|
+
lastMessageId,
|
|
560
|
+
sendStart,
|
|
561
|
+
sendFinish,
|
|
562
|
+
sendReasoning,
|
|
563
|
+
sendSources,
|
|
564
|
+
messageMetadata,
|
|
565
|
+
onError
|
|
566
|
+
}) {
|
|
465
567
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
568
|
+
let tripwireOccurred = false;
|
|
569
|
+
let finishEventSent = false;
|
|
466
570
|
return new TransformStream({
|
|
467
571
|
transform(chunk, controller) {
|
|
572
|
+
if (chunk.type === "tripwire") {
|
|
573
|
+
tripwireOccurred = true;
|
|
574
|
+
}
|
|
575
|
+
if (chunk.type === "finish") {
|
|
576
|
+
finishEventSent = true;
|
|
577
|
+
}
|
|
468
578
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
469
579
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
470
580
|
part,
|
|
471
|
-
sendReasoning
|
|
472
|
-
sendSources
|
|
473
|
-
|
|
474
|
-
|
|
581
|
+
sendReasoning,
|
|
582
|
+
sendSources,
|
|
583
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
584
|
+
sendStart,
|
|
585
|
+
sendFinish,
|
|
475
586
|
responseMessageId: lastMessageId,
|
|
476
587
|
onError(error) {
|
|
477
|
-
return safeParseErrorObject(error);
|
|
588
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
478
589
|
}
|
|
479
590
|
});
|
|
480
591
|
if (transformedChunk) {
|
|
@@ -494,6 +605,14 @@ function AgentStreamToAISDKTransformer(lastMessageId) {
|
|
|
494
605
|
controller.enqueue(transformedChunk);
|
|
495
606
|
}
|
|
496
607
|
}
|
|
608
|
+
},
|
|
609
|
+
flush(controller) {
|
|
610
|
+
if (tripwireOccurred && !finishEventSent && sendFinish) {
|
|
611
|
+
controller.enqueue({
|
|
612
|
+
type: "finish",
|
|
613
|
+
finishReason: "other"
|
|
614
|
+
});
|
|
615
|
+
}
|
|
497
616
|
}
|
|
498
617
|
});
|
|
499
618
|
}
|
|
@@ -633,7 +752,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
633
752
|
}
|
|
634
753
|
return null;
|
|
635
754
|
}
|
|
636
|
-
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
755
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
637
756
|
switch (payload.type) {
|
|
638
757
|
case "workflow-start":
|
|
639
758
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -726,6 +845,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
726
845
|
}
|
|
727
846
|
};
|
|
728
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
|
+
}
|
|
729
871
|
default: {
|
|
730
872
|
if (isDataChunkType(payload)) {
|
|
731
873
|
if (!("data" in payload)) {
|
|
@@ -745,12 +887,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
745
887
|
case "routing-agent-start": {
|
|
746
888
|
if (!bufferedNetworks.has(payload.runId)) {
|
|
747
889
|
bufferedNetworks.set(payload.runId, {
|
|
748
|
-
name: payload.payload.
|
|
890
|
+
name: payload.payload.networkId,
|
|
749
891
|
steps: [],
|
|
750
892
|
usage: null,
|
|
751
893
|
output: null
|
|
752
894
|
});
|
|
753
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
|
+
});
|
|
754
913
|
return {
|
|
755
914
|
type: isNested ? "data-tool-network" : "data-network",
|
|
756
915
|
id: payload.runId,
|
|
@@ -781,14 +940,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
781
940
|
};
|
|
782
941
|
}
|
|
783
942
|
case "agent-execution-start": {
|
|
784
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
943
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
944
|
+
if (!current) return null;
|
|
785
945
|
current.steps.push({
|
|
946
|
+
id: payload.payload.runId,
|
|
786
947
|
name: payload.payload.agentId,
|
|
787
948
|
status: "running",
|
|
788
|
-
|
|
949
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
950
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
789
951
|
output: null,
|
|
952
|
+
task: null,
|
|
790
953
|
suspendPayload: null,
|
|
791
|
-
resumePayload: null
|
|
954
|
+
resumePayload: null,
|
|
955
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
792
956
|
});
|
|
793
957
|
bufferedNetworks.set(payload.runId, current);
|
|
794
958
|
return {
|
|
@@ -801,14 +965,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
801
965
|
};
|
|
802
966
|
}
|
|
803
967
|
case "workflow-execution-start": {
|
|
804
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
968
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
969
|
+
if (!current) return null;
|
|
805
970
|
current.steps.push({
|
|
806
|
-
|
|
971
|
+
id: payload.payload.runId,
|
|
972
|
+
name: payload.payload.workflowId,
|
|
807
973
|
status: "running",
|
|
808
|
-
|
|
974
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
975
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
809
976
|
output: null,
|
|
977
|
+
task: null,
|
|
810
978
|
suspendPayload: null,
|
|
811
|
-
resumePayload: null
|
|
979
|
+
resumePayload: null,
|
|
980
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
812
981
|
});
|
|
813
982
|
bufferedNetworks.set(payload.runId, current);
|
|
814
983
|
return {
|
|
@@ -821,14 +990,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
821
990
|
};
|
|
822
991
|
}
|
|
823
992
|
case "tool-execution-start": {
|
|
824
|
-
const current = bufferedNetworks.get(payload.runId)
|
|
993
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
994
|
+
if (!current) return null;
|
|
825
995
|
current.steps.push({
|
|
996
|
+
id: payload.payload.args.toolCallId,
|
|
826
997
|
name: payload.payload.args?.toolName,
|
|
827
998
|
status: "running",
|
|
999
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
1000
|
+
task: {
|
|
1001
|
+
id: payload.payload.args?.toolName
|
|
1002
|
+
},
|
|
828
1003
|
input: payload.payload.args?.args || null,
|
|
829
1004
|
output: null,
|
|
830
1005
|
suspendPayload: null,
|
|
831
|
-
resumePayload: null
|
|
1006
|
+
resumePayload: null,
|
|
1007
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
832
1008
|
});
|
|
833
1009
|
bufferedNetworks.set(payload.runId, current);
|
|
834
1010
|
return {
|
|
@@ -843,14 +1019,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
843
1019
|
case "agent-execution-end": {
|
|
844
1020
|
const current = bufferedNetworks.get(payload.runId);
|
|
845
1021
|
if (!current) return null;
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
});
|
|
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;
|
|
854
1029
|
return {
|
|
855
1030
|
type: isNested ? "data-tool-network" : "data-network",
|
|
856
1031
|
id: payload.runId,
|
|
@@ -865,14 +1040,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
865
1040
|
case "tool-execution-end": {
|
|
866
1041
|
const current = bufferedNetworks.get(payload.runId);
|
|
867
1042
|
if (!current) return null;
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
});
|
|
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;
|
|
876
1050
|
return {
|
|
877
1051
|
type: isNested ? "data-tool-network" : "data-network",
|
|
878
1052
|
id: payload.runId,
|
|
@@ -886,14 +1060,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
886
1060
|
case "workflow-execution-end": {
|
|
887
1061
|
const current = bufferedNetworks.get(payload.runId);
|
|
888
1062
|
if (!current) return null;
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
});
|
|
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;
|
|
897
1070
|
return {
|
|
898
1071
|
type: isNested ? "data-tool-network" : "data-network",
|
|
899
1072
|
id: payload.runId,
|
|
@@ -908,12 +1081,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
908
1081
|
case "routing-agent-end": {
|
|
909
1082
|
const current = bufferedNetworks.get(payload.runId);
|
|
910
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;
|
|
911
1097
|
return {
|
|
912
1098
|
type: isNested ? "data-tool-network" : "data-network",
|
|
913
1099
|
id: payload.runId,
|
|
914
1100
|
data: {
|
|
915
1101
|
...current,
|
|
916
|
-
status: "finished",
|
|
917
1102
|
usage: payload.payload?.usage ?? current.usage,
|
|
918
1103
|
output: payload.payload?.result ?? current.output
|
|
919
1104
|
}
|
|
@@ -947,6 +1132,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
947
1132
|
};
|
|
948
1133
|
}
|
|
949
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
|
+
}
|
|
950
1168
|
if (isDataChunkType(payload)) {
|
|
951
1169
|
if (!("data" in payload)) {
|
|
952
1170
|
throw new Error(
|
|
@@ -954,7 +1172,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
954
1172
|
${JSON.stringify(payload)}`
|
|
955
1173
|
);
|
|
956
1174
|
}
|
|
957
|
-
|
|
1175
|
+
const { type, data } = payload;
|
|
1176
|
+
return { type, data };
|
|
958
1177
|
}
|
|
959
1178
|
if (isAgentExecutionDataChunkType(payload)) {
|
|
960
1179
|
if (!("data" in payload.payload)) {
|
|
@@ -963,7 +1182,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
963
1182
|
${JSON.stringify(payload)}`
|
|
964
1183
|
);
|
|
965
1184
|
}
|
|
966
|
-
|
|
1185
|
+
const { type, data } = payload.payload;
|
|
1186
|
+
return { type, data };
|
|
967
1187
|
}
|
|
968
1188
|
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
969
1189
|
if (!("data" in payload.payload)) {
|
|
@@ -972,7 +1192,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
972
1192
|
${JSON.stringify(payload)}`
|
|
973
1193
|
);
|
|
974
1194
|
}
|
|
975
|
-
|
|
1195
|
+
const { type, data } = payload.payload;
|
|
1196
|
+
return { type, data };
|
|
976
1197
|
}
|
|
977
1198
|
return null;
|
|
978
1199
|
}
|
|
@@ -980,23 +1201,44 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
980
1201
|
}
|
|
981
1202
|
|
|
982
1203
|
// src/convert-streams.ts
|
|
983
|
-
function toAISdkV5Stream(stream, options = {
|
|
1204
|
+
function toAISdkV5Stream(stream, options = {
|
|
1205
|
+
from: "agent",
|
|
1206
|
+
sendStart: true,
|
|
1207
|
+
sendFinish: true
|
|
1208
|
+
}) {
|
|
984
1209
|
const from = options?.from;
|
|
985
1210
|
if (from === "workflow") {
|
|
986
|
-
|
|
1211
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? true;
|
|
1212
|
+
return stream.pipeThrough(
|
|
1213
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1214
|
+
);
|
|
987
1215
|
}
|
|
988
1216
|
if (from === "network") {
|
|
989
1217
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
990
1218
|
}
|
|
991
1219
|
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
992
|
-
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
|
+
);
|
|
993
1231
|
}
|
|
994
1232
|
|
|
995
1233
|
// src/chat-route.ts
|
|
996
1234
|
function chatRoute({
|
|
997
1235
|
path = "/chat/:agentId",
|
|
998
1236
|
agent,
|
|
999
|
-
defaultOptions
|
|
1237
|
+
defaultOptions,
|
|
1238
|
+
sendStart = true,
|
|
1239
|
+
sendFinish = true,
|
|
1240
|
+
sendReasoning = false,
|
|
1241
|
+
sendSources = false
|
|
1000
1242
|
}) {
|
|
1001
1243
|
if (!agent && !path.includes("/:agentId")) {
|
|
1002
1244
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -1114,7 +1356,7 @@ function chatRoute({
|
|
|
1114
1356
|
if (!agentToUse) {
|
|
1115
1357
|
throw new Error("Agent ID is required");
|
|
1116
1358
|
}
|
|
1117
|
-
const agentObj = mastra.
|
|
1359
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1118
1360
|
if (!agentObj) {
|
|
1119
1361
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1120
1362
|
}
|
|
@@ -1130,7 +1372,14 @@ function chatRoute({
|
|
|
1130
1372
|
const uiMessageStream = createUIMessageStream({
|
|
1131
1373
|
originalMessages: messages,
|
|
1132
1374
|
execute: async ({ writer }) => {
|
|
1133
|
-
for await (const part of toAISdkV5Stream(result, {
|
|
1375
|
+
for await (const part of toAISdkV5Stream(result, {
|
|
1376
|
+
from: "agent",
|
|
1377
|
+
lastMessageId,
|
|
1378
|
+
sendStart,
|
|
1379
|
+
sendFinish,
|
|
1380
|
+
sendReasoning,
|
|
1381
|
+
sendSources
|
|
1382
|
+
})) {
|
|
1134
1383
|
writer.write(part);
|
|
1135
1384
|
}
|
|
1136
1385
|
}
|
|
@@ -1143,7 +1392,8 @@ function chatRoute({
|
|
|
1143
1392
|
}
|
|
1144
1393
|
function workflowRoute({
|
|
1145
1394
|
path = "/api/workflows/:workflowId/stream",
|
|
1146
|
-
workflow
|
|
1395
|
+
workflow,
|
|
1396
|
+
includeTextStreamParts = true
|
|
1147
1397
|
}) {
|
|
1148
1398
|
if (!workflow && !path.includes("/:workflowId")) {
|
|
1149
1399
|
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
@@ -1170,9 +1420,13 @@ function workflowRoute({
|
|
|
1170
1420
|
schema: {
|
|
1171
1421
|
type: "object",
|
|
1172
1422
|
properties: {
|
|
1423
|
+
runId: { type: "string" },
|
|
1424
|
+
resourceId: { type: "string" },
|
|
1173
1425
|
inputData: { type: "object", additionalProperties: true },
|
|
1426
|
+
resumeData: { type: "object", additionalProperties: true },
|
|
1174
1427
|
requestContext: { type: "object", additionalProperties: true },
|
|
1175
|
-
tracingOptions: { type: "object", additionalProperties: true }
|
|
1428
|
+
tracingOptions: { type: "object", additionalProperties: true },
|
|
1429
|
+
step: { type: "string" }
|
|
1176
1430
|
}
|
|
1177
1431
|
}
|
|
1178
1432
|
}
|
|
@@ -1190,8 +1444,9 @@ function workflowRoute({
|
|
|
1190
1444
|
}
|
|
1191
1445
|
},
|
|
1192
1446
|
handler: async (c) => {
|
|
1193
|
-
const { inputData, resumeData, ...rest } = await c.req.json();
|
|
1447
|
+
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1194
1448
|
const mastra = c.get("mastra");
|
|
1449
|
+
const requestContext = c.get("requestContext");
|
|
1195
1450
|
let workflowToUse = workflow;
|
|
1196
1451
|
if (!workflow) {
|
|
1197
1452
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1205,15 +1460,20 @@ function workflowRoute({
|
|
|
1205
1460
|
if (!workflowToUse) {
|
|
1206
1461
|
throw new Error("Workflow ID is required");
|
|
1207
1462
|
}
|
|
1208
|
-
const workflowObj = mastra.
|
|
1463
|
+
const workflowObj = mastra.getWorkflowById(workflowToUse);
|
|
1209
1464
|
if (!workflowObj) {
|
|
1210
1465
|
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1211
1466
|
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1467
|
+
if (requestContext && rest.requestContext) {
|
|
1468
|
+
mastra.getLogger()?.warn(
|
|
1469
|
+
`"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1473
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext: requestContext || rest.requestContext }) : run.stream({ inputData, ...rest, requestContext: requestContext || rest.requestContext });
|
|
1214
1474
|
const uiMessageStream = createUIMessageStream({
|
|
1215
1475
|
execute: async ({ writer }) => {
|
|
1216
|
-
for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
|
|
1476
|
+
for await (const part of toAISdkV5Stream(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1217
1477
|
writer.write(part);
|
|
1218
1478
|
}
|
|
1219
1479
|
}
|
|
@@ -1297,7 +1557,7 @@ function networkRoute({
|
|
|
1297
1557
|
if (!agentToUse) {
|
|
1298
1558
|
throw new Error("Agent ID is required");
|
|
1299
1559
|
}
|
|
1300
|
-
const agentObj = mastra.
|
|
1560
|
+
const agentObj = mastra.getAgentById(agentToUse);
|
|
1301
1561
|
if (!agentObj) {
|
|
1302
1562
|
throw new Error(`Agent ${agentToUse} not found`);
|
|
1303
1563
|
}
|