@mastra/ai-sdk 0.0.0-extract-tool-ui-inp-playground-ui-20251024041825 → 0.0.0-feat-improve-processors-20251205191721
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 +368 -3
- package/README.md +60 -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 +85 -3
- 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 +82 -0
- package/dist/convert-streams.d.ts.map +1 -0
- package/dist/helpers.d.ts +2 -3
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +564 -147
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +561 -148
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts +30 -2
- package/dist/network-route.d.ts.map +1 -1
- package/dist/to-ai-sdk-format.d.ts +15 -16
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +174 -10
- package/dist/transformers.d.ts.map +1 -1
- package/dist/ui.cjs +16 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.ts +2 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +13 -0
- package/dist/ui.js.map +1 -0
- package/dist/utils.d.ts +9 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/workflow-route.d.ts +38 -1
- package/dist/workflow-route.d.ts.map +1 -1
- package/package.json +21 -6
package/dist/index.js
CHANGED
|
@@ -8,6 +8,51 @@ 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
|
+
}
|
|
50
|
+
var isAgentExecutionDataChunkType = (chunk) => {
|
|
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-");
|
|
52
|
+
};
|
|
53
|
+
var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
54
|
+
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-");
|
|
55
|
+
};
|
|
11
56
|
|
|
12
57
|
// src/helpers.ts
|
|
13
58
|
function convertMastraChunkToAISDKv5({
|
|
@@ -118,6 +163,28 @@ function convertMastraChunkToAISDKv5({
|
|
|
118
163
|
toolName: chunk.payload.toolName,
|
|
119
164
|
input: chunk.payload.args
|
|
120
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
|
+
};
|
|
121
188
|
case "tool-call-input-streaming-start":
|
|
122
189
|
return {
|
|
123
190
|
type: "tool-input-start",
|
|
@@ -208,6 +275,13 @@ function convertMastraChunkToAISDKv5({
|
|
|
208
275
|
type: "object",
|
|
209
276
|
object: chunk.object
|
|
210
277
|
};
|
|
278
|
+
case "tripwire":
|
|
279
|
+
return {
|
|
280
|
+
type: "data-tripwire",
|
|
281
|
+
data: {
|
|
282
|
+
tripwireReason: chunk.payload.tripwireReason
|
|
283
|
+
}
|
|
284
|
+
};
|
|
211
285
|
default:
|
|
212
286
|
if (chunk.type && "payload" in chunk && chunk.payload) {
|
|
213
287
|
return {
|
|
@@ -263,6 +337,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
263
337
|
};
|
|
264
338
|
}
|
|
265
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
|
+
}
|
|
266
348
|
return;
|
|
267
349
|
}
|
|
268
350
|
case "reasoning-end": {
|
|
@@ -280,6 +362,25 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
280
362
|
};
|
|
281
363
|
}
|
|
282
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
|
+
}
|
|
283
384
|
return;
|
|
284
385
|
}
|
|
285
386
|
case "tool-input-start": {
|
|
@@ -337,6 +438,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
337
438
|
toolCallId: part.toolCallId,
|
|
338
439
|
payload: part.output
|
|
339
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;
|
|
340
449
|
}
|
|
341
450
|
return;
|
|
342
451
|
}
|
|
@@ -362,21 +471,23 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
362
471
|
return { type: "finish-step" };
|
|
363
472
|
}
|
|
364
473
|
case "start": {
|
|
365
|
-
{
|
|
474
|
+
if (sendStart) {
|
|
366
475
|
return {
|
|
367
476
|
type: "start",
|
|
368
477
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
369
478
|
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
370
479
|
};
|
|
371
480
|
}
|
|
481
|
+
return;
|
|
372
482
|
}
|
|
373
483
|
case "finish": {
|
|
374
|
-
{
|
|
484
|
+
if (sendFinish) {
|
|
375
485
|
return {
|
|
376
486
|
type: "finish",
|
|
377
487
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
378
488
|
};
|
|
379
489
|
}
|
|
490
|
+
return;
|
|
380
491
|
}
|
|
381
492
|
case "abort": {
|
|
382
493
|
return part;
|
|
@@ -403,7 +514,10 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
403
514
|
}
|
|
404
515
|
|
|
405
516
|
// src/transformers.ts
|
|
406
|
-
|
|
517
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
518
|
+
function WorkflowStreamToAISDKTransformer({
|
|
519
|
+
includeTextStreamParts
|
|
520
|
+
} = {}) {
|
|
407
521
|
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
408
522
|
return new TransformStream({
|
|
409
523
|
start(controller) {
|
|
@@ -417,7 +531,7 @@ function WorkflowStreamToAISDKTransformer() {
|
|
|
417
531
|
});
|
|
418
532
|
},
|
|
419
533
|
transform(chunk, controller) {
|
|
420
|
-
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
534
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
421
535
|
if (transformed) controller.enqueue(transformed);
|
|
422
536
|
}
|
|
423
537
|
});
|
|
@@ -441,20 +555,37 @@ function AgentNetworkToAISDKTransformer() {
|
|
|
441
555
|
}
|
|
442
556
|
});
|
|
443
557
|
}
|
|
444
|
-
function AgentStreamToAISDKTransformer(
|
|
558
|
+
function AgentStreamToAISDKTransformer({
|
|
559
|
+
lastMessageId,
|
|
560
|
+
sendStart,
|
|
561
|
+
sendFinish,
|
|
562
|
+
sendReasoning,
|
|
563
|
+
sendSources,
|
|
564
|
+
messageMetadata,
|
|
565
|
+
onError
|
|
566
|
+
}) {
|
|
445
567
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
568
|
+
let tripwireOccurred = false;
|
|
569
|
+
let finishEventSent = false;
|
|
446
570
|
return new TransformStream({
|
|
447
571
|
transform(chunk, controller) {
|
|
572
|
+
if (chunk.type === "tripwire") {
|
|
573
|
+
tripwireOccurred = true;
|
|
574
|
+
}
|
|
575
|
+
if (chunk.type === "finish") {
|
|
576
|
+
finishEventSent = true;
|
|
577
|
+
}
|
|
448
578
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
449
579
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
450
580
|
part,
|
|
451
|
-
sendReasoning
|
|
452
|
-
sendSources
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
581
|
+
sendReasoning,
|
|
582
|
+
sendSources,
|
|
583
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
584
|
+
sendStart,
|
|
585
|
+
sendFinish,
|
|
586
|
+
responseMessageId: lastMessageId,
|
|
587
|
+
onError(error) {
|
|
588
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
458
589
|
}
|
|
459
590
|
});
|
|
460
591
|
if (transformedChunk) {
|
|
@@ -474,6 +605,14 @@ function AgentStreamToAISDKTransformer() {
|
|
|
474
605
|
controller.enqueue(transformedChunk);
|
|
475
606
|
}
|
|
476
607
|
}
|
|
608
|
+
},
|
|
609
|
+
flush(controller) {
|
|
610
|
+
if (tripwireOccurred && !finishEventSent && sendFinish) {
|
|
611
|
+
controller.enqueue({
|
|
612
|
+
type: "finish",
|
|
613
|
+
finishReason: "other"
|
|
614
|
+
});
|
|
615
|
+
}
|
|
477
616
|
}
|
|
478
617
|
});
|
|
479
618
|
}
|
|
@@ -613,7 +752,7 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
613
752
|
}
|
|
614
753
|
return null;
|
|
615
754
|
}
|
|
616
|
-
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
755
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
617
756
|
switch (payload.type) {
|
|
618
757
|
case "workflow-start":
|
|
619
758
|
bufferedWorkflows.set(payload.runId, {
|
|
@@ -636,7 +775,9 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
636
775
|
name: payload.payload.id,
|
|
637
776
|
status: payload.payload.status,
|
|
638
777
|
input: payload.payload.payload ?? null,
|
|
639
|
-
output: null
|
|
778
|
+
output: null,
|
|
779
|
+
suspendPayload: null,
|
|
780
|
+
resumePayload: null
|
|
640
781
|
};
|
|
641
782
|
bufferedWorkflows.set(payload.runId, current);
|
|
642
783
|
return {
|
|
@@ -669,6 +810,27 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
669
810
|
}
|
|
670
811
|
};
|
|
671
812
|
}
|
|
813
|
+
case "workflow-step-suspended": {
|
|
814
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
815
|
+
if (!current) return null;
|
|
816
|
+
current.steps[payload.payload.id] = {
|
|
817
|
+
...current.steps[payload.payload.id],
|
|
818
|
+
status: payload.payload.status,
|
|
819
|
+
suspendPayload: payload.payload.suspendPayload ?? null,
|
|
820
|
+
resumePayload: payload.payload.resumePayload ?? null,
|
|
821
|
+
output: null
|
|
822
|
+
};
|
|
823
|
+
return {
|
|
824
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
825
|
+
id: payload.runId,
|
|
826
|
+
data: {
|
|
827
|
+
name: current.name,
|
|
828
|
+
status: "suspended",
|
|
829
|
+
steps: current.steps,
|
|
830
|
+
output: null
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
}
|
|
672
834
|
case "workflow-finish": {
|
|
673
835
|
const current = bufferedWorkflows.get(payload.runId);
|
|
674
836
|
if (!current) return null;
|
|
@@ -683,6 +845,29 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
683
845
|
}
|
|
684
846
|
};
|
|
685
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
|
+
}
|
|
686
871
|
default: {
|
|
687
872
|
if (isDataChunkType(payload)) {
|
|
688
873
|
if (!("data" in payload)) {
|
|
@@ -700,19 +885,39 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
|
700
885
|
function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
701
886
|
switch (payload.type) {
|
|
702
887
|
case "routing-agent-start": {
|
|
703
|
-
if (!bufferedNetworks.has(payload.
|
|
704
|
-
bufferedNetworks.set(payload.
|
|
705
|
-
name: payload.payload.
|
|
706
|
-
steps: []
|
|
888
|
+
if (!bufferedNetworks.has(payload.runId)) {
|
|
889
|
+
bufferedNetworks.set(payload.runId, {
|
|
890
|
+
name: payload.payload.networkId,
|
|
891
|
+
steps: [],
|
|
892
|
+
usage: null,
|
|
893
|
+
output: null
|
|
707
894
|
});
|
|
708
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
|
+
});
|
|
709
913
|
return {
|
|
710
914
|
type: isNested ? "data-tool-network" : "data-network",
|
|
711
|
-
id: payload.
|
|
915
|
+
id: payload.runId,
|
|
712
916
|
data: {
|
|
713
|
-
name: bufferedNetworks.get(payload.
|
|
917
|
+
name: bufferedNetworks.get(payload.runId).name,
|
|
714
918
|
status: "running",
|
|
715
|
-
|
|
919
|
+
usage: null,
|
|
920
|
+
steps: bufferedNetworks.get(payload.runId).steps,
|
|
716
921
|
output: null
|
|
717
922
|
}
|
|
718
923
|
};
|
|
@@ -735,150 +940,180 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
735
940
|
};
|
|
736
941
|
}
|
|
737
942
|
case "agent-execution-start": {
|
|
738
|
-
const current = bufferedNetworks.get(payload.
|
|
943
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
944
|
+
if (!current) return null;
|
|
739
945
|
current.steps.push({
|
|
946
|
+
id: payload.payload.runId,
|
|
740
947
|
name: payload.payload.agentId,
|
|
741
948
|
status: "running",
|
|
742
|
-
|
|
743
|
-
|
|
949
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
950
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
951
|
+
output: null,
|
|
952
|
+
task: null,
|
|
953
|
+
suspendPayload: null,
|
|
954
|
+
resumePayload: null,
|
|
955
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
744
956
|
});
|
|
745
|
-
bufferedNetworks.set(payload.
|
|
957
|
+
bufferedNetworks.set(payload.runId, current);
|
|
746
958
|
return {
|
|
747
959
|
type: isNested ? "data-tool-network" : "data-network",
|
|
748
|
-
id: payload.
|
|
960
|
+
id: payload.runId,
|
|
749
961
|
data: {
|
|
750
|
-
|
|
751
|
-
status: "running"
|
|
752
|
-
steps: current.steps,
|
|
753
|
-
output: null
|
|
962
|
+
...current,
|
|
963
|
+
status: "running"
|
|
754
964
|
}
|
|
755
965
|
};
|
|
756
966
|
}
|
|
757
967
|
case "workflow-execution-start": {
|
|
758
|
-
const current = bufferedNetworks.get(payload.
|
|
968
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
969
|
+
if (!current) return null;
|
|
759
970
|
current.steps.push({
|
|
760
|
-
|
|
971
|
+
id: payload.payload.runId,
|
|
972
|
+
name: payload.payload.workflowId,
|
|
761
973
|
status: "running",
|
|
762
|
-
|
|
763
|
-
|
|
974
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
975
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
976
|
+
output: null,
|
|
977
|
+
task: null,
|
|
978
|
+
suspendPayload: null,
|
|
979
|
+
resumePayload: null,
|
|
980
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
764
981
|
});
|
|
765
|
-
bufferedNetworks.set(payload.
|
|
982
|
+
bufferedNetworks.set(payload.runId, current);
|
|
766
983
|
return {
|
|
767
984
|
type: isNested ? "data-tool-network" : "data-network",
|
|
768
|
-
id: payload.
|
|
985
|
+
id: payload.runId,
|
|
769
986
|
data: {
|
|
770
|
-
|
|
771
|
-
status: "running"
|
|
772
|
-
steps: current.steps,
|
|
773
|
-
output: null
|
|
987
|
+
...current,
|
|
988
|
+
status: "running"
|
|
774
989
|
}
|
|
775
990
|
};
|
|
776
991
|
}
|
|
777
992
|
case "tool-execution-start": {
|
|
778
|
-
const current = bufferedNetworks.get(payload.
|
|
993
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
994
|
+
if (!current) return null;
|
|
779
995
|
current.steps.push({
|
|
996
|
+
id: payload.payload.args.toolCallId,
|
|
780
997
|
name: payload.payload.args?.toolName,
|
|
781
998
|
status: "running",
|
|
999
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
1000
|
+
task: {
|
|
1001
|
+
id: payload.payload.args?.toolName
|
|
1002
|
+
},
|
|
782
1003
|
input: payload.payload.args?.args || null,
|
|
783
|
-
output: null
|
|
1004
|
+
output: null,
|
|
1005
|
+
suspendPayload: null,
|
|
1006
|
+
resumePayload: null,
|
|
1007
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
784
1008
|
});
|
|
785
|
-
bufferedNetworks.set(payload.
|
|
1009
|
+
bufferedNetworks.set(payload.runId, current);
|
|
786
1010
|
return {
|
|
787
1011
|
type: isNested ? "data-tool-network" : "data-network",
|
|
788
|
-
id: payload.
|
|
1012
|
+
id: payload.runId,
|
|
789
1013
|
data: {
|
|
790
|
-
|
|
791
|
-
status: "running"
|
|
792
|
-
steps: current.steps,
|
|
793
|
-
output: null
|
|
1014
|
+
...current,
|
|
1015
|
+
status: "running"
|
|
794
1016
|
}
|
|
795
1017
|
};
|
|
796
1018
|
}
|
|
797
1019
|
case "agent-execution-end": {
|
|
798
1020
|
const current = bufferedNetworks.get(payload.runId);
|
|
799
1021
|
if (!current) return null;
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
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;
|
|
806
1029
|
return {
|
|
807
1030
|
type: isNested ? "data-tool-network" : "data-network",
|
|
808
1031
|
id: payload.runId,
|
|
809
1032
|
data: {
|
|
810
|
-
|
|
1033
|
+
...current,
|
|
1034
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
811
1035
|
status: "running",
|
|
812
|
-
|
|
813
|
-
output: payload.payload.result ?? null
|
|
1036
|
+
output: payload.payload.result ?? current.output
|
|
814
1037
|
}
|
|
815
1038
|
};
|
|
816
1039
|
}
|
|
817
1040
|
case "tool-execution-end": {
|
|
818
1041
|
const current = bufferedNetworks.get(payload.runId);
|
|
819
1042
|
if (!current) return null;
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
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;
|
|
826
1050
|
return {
|
|
827
1051
|
type: isNested ? "data-tool-network" : "data-network",
|
|
828
1052
|
id: payload.runId,
|
|
829
1053
|
data: {
|
|
830
|
-
|
|
1054
|
+
...current,
|
|
831
1055
|
status: "running",
|
|
832
|
-
|
|
833
|
-
output: payload.payload.result ?? null
|
|
1056
|
+
output: payload.payload.result ?? current.output
|
|
834
1057
|
}
|
|
835
1058
|
};
|
|
836
1059
|
}
|
|
837
1060
|
case "workflow-execution-end": {
|
|
838
1061
|
const current = bufferedNetworks.get(payload.runId);
|
|
839
1062
|
if (!current) return null;
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
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;
|
|
846
1070
|
return {
|
|
847
1071
|
type: isNested ? "data-tool-network" : "data-network",
|
|
848
1072
|
id: payload.runId,
|
|
849
1073
|
data: {
|
|
850
|
-
|
|
1074
|
+
...current,
|
|
1075
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
851
1076
|
status: "running",
|
|
852
|
-
|
|
853
|
-
output: payload.payload.result ?? null
|
|
1077
|
+
output: payload.payload.result ?? current.output
|
|
854
1078
|
}
|
|
855
1079
|
};
|
|
856
1080
|
}
|
|
857
1081
|
case "routing-agent-end": {
|
|
858
|
-
const current = bufferedNetworks.get(payload.
|
|
1082
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
859
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;
|
|
860
1097
|
return {
|
|
861
1098
|
type: isNested ? "data-tool-network" : "data-network",
|
|
862
|
-
id: payload.
|
|
1099
|
+
id: payload.runId,
|
|
863
1100
|
data: {
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
output: payload.payload?.result ?? null
|
|
1101
|
+
...current,
|
|
1102
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
1103
|
+
output: payload.payload?.result ?? current.output
|
|
868
1104
|
}
|
|
869
1105
|
};
|
|
870
1106
|
}
|
|
871
1107
|
case "network-execution-event-step-finish": {
|
|
872
|
-
const current = bufferedNetworks.get(payload.
|
|
1108
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
873
1109
|
if (!current) return null;
|
|
874
1110
|
return {
|
|
875
1111
|
type: isNested ? "data-tool-network" : "data-network",
|
|
876
|
-
id: payload.
|
|
1112
|
+
id: payload.runId,
|
|
877
1113
|
data: {
|
|
878
|
-
|
|
1114
|
+
...current,
|
|
879
1115
|
status: "finished",
|
|
880
|
-
|
|
881
|
-
output: payload.payload?.result ?? null
|
|
1116
|
+
output: payload.payload?.result ?? current.output
|
|
882
1117
|
}
|
|
883
1118
|
};
|
|
884
1119
|
}
|
|
@@ -889,14 +1124,47 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
889
1124
|
type: isNested ? "data-tool-network" : "data-network",
|
|
890
1125
|
id: payload.runId,
|
|
891
1126
|
data: {
|
|
892
|
-
|
|
1127
|
+
...current,
|
|
1128
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
893
1129
|
status: "finished",
|
|
894
|
-
|
|
895
|
-
output: payload.payload?.result ?? null
|
|
1130
|
+
output: payload.payload?.result ?? current.output
|
|
896
1131
|
}
|
|
897
1132
|
};
|
|
898
1133
|
}
|
|
899
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
|
+
}
|
|
900
1168
|
if (isDataChunkType(payload)) {
|
|
901
1169
|
if (!("data" in payload)) {
|
|
902
1170
|
throw new Error(
|
|
@@ -904,31 +1172,124 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
|
904
1172
|
${JSON.stringify(payload)}`
|
|
905
1173
|
);
|
|
906
1174
|
}
|
|
907
|
-
|
|
1175
|
+
const { type, data } = payload;
|
|
1176
|
+
return { type, data };
|
|
1177
|
+
}
|
|
1178
|
+
if (isAgentExecutionDataChunkType(payload)) {
|
|
1179
|
+
if (!("data" in payload.payload)) {
|
|
1180
|
+
throw new Error(
|
|
1181
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1182
|
+
${JSON.stringify(payload)}`
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1185
|
+
const { type, data } = payload.payload;
|
|
1186
|
+
return { type, data };
|
|
1187
|
+
}
|
|
1188
|
+
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1189
|
+
if (!("data" in payload.payload)) {
|
|
1190
|
+
throw new Error(
|
|
1191
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1192
|
+
${JSON.stringify(payload)}`
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
const { type, data } = payload.payload;
|
|
1196
|
+
return { type, data };
|
|
908
1197
|
}
|
|
909
1198
|
return null;
|
|
910
1199
|
}
|
|
911
1200
|
}
|
|
912
1201
|
}
|
|
913
1202
|
|
|
914
|
-
// src/
|
|
915
|
-
function
|
|
1203
|
+
// src/convert-streams.ts
|
|
1204
|
+
function toAISdkV5Stream(stream, options = {
|
|
1205
|
+
from: "agent",
|
|
1206
|
+
sendStart: true,
|
|
1207
|
+
sendFinish: true
|
|
1208
|
+
}) {
|
|
916
1209
|
const from = options?.from;
|
|
917
1210
|
if (from === "workflow") {
|
|
918
|
-
|
|
1211
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? true;
|
|
1212
|
+
return stream.pipeThrough(
|
|
1213
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1214
|
+
);
|
|
919
1215
|
}
|
|
920
1216
|
if (from === "network") {
|
|
921
1217
|
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
922
1218
|
}
|
|
923
1219
|
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
924
|
-
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
|
+
);
|
|
925
1231
|
}
|
|
926
1232
|
|
|
927
1233
|
// src/chat-route.ts
|
|
1234
|
+
async function handleChatStream({
|
|
1235
|
+
mastra,
|
|
1236
|
+
agentId,
|
|
1237
|
+
params,
|
|
1238
|
+
defaultOptions,
|
|
1239
|
+
sendStart = true,
|
|
1240
|
+
sendFinish = true,
|
|
1241
|
+
sendReasoning = false,
|
|
1242
|
+
sendSources = false
|
|
1243
|
+
}) {
|
|
1244
|
+
const { messages, resumeData, runId, requestContext, ...rest } = params;
|
|
1245
|
+
if (resumeData && !runId) {
|
|
1246
|
+
throw new Error("runId is required when resumeData is provided");
|
|
1247
|
+
}
|
|
1248
|
+
const agentObj = mastra.getAgentById(agentId);
|
|
1249
|
+
if (!agentObj) {
|
|
1250
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
1251
|
+
}
|
|
1252
|
+
if (!Array.isArray(messages)) {
|
|
1253
|
+
throw new Error("Messages must be an array of UIMessage objects");
|
|
1254
|
+
}
|
|
1255
|
+
const mergedOptions = {
|
|
1256
|
+
...defaultOptions,
|
|
1257
|
+
...rest,
|
|
1258
|
+
...runId && { runId },
|
|
1259
|
+
requestContext: requestContext || defaultOptions?.requestContext
|
|
1260
|
+
};
|
|
1261
|
+
const result = resumeData ? await agentObj.resumeStream(resumeData, mergedOptions) : await agentObj.stream(messages, mergedOptions);
|
|
1262
|
+
let lastMessageId;
|
|
1263
|
+
if (messages.length) {
|
|
1264
|
+
const lastMessage = messages[messages.length - 1];
|
|
1265
|
+
if (lastMessage?.role === "assistant") {
|
|
1266
|
+
lastMessageId = lastMessage.id;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return createUIMessageStream({
|
|
1270
|
+
originalMessages: messages,
|
|
1271
|
+
execute: async ({ writer }) => {
|
|
1272
|
+
for await (const part of toAISdkV5Stream(result, {
|
|
1273
|
+
from: "agent",
|
|
1274
|
+
lastMessageId,
|
|
1275
|
+
sendStart,
|
|
1276
|
+
sendFinish,
|
|
1277
|
+
sendReasoning,
|
|
1278
|
+
sendSources
|
|
1279
|
+
})) {
|
|
1280
|
+
writer.write(part);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
928
1285
|
function chatRoute({
|
|
929
1286
|
path = "/chat/:agentId",
|
|
930
1287
|
agent,
|
|
931
|
-
defaultOptions
|
|
1288
|
+
defaultOptions,
|
|
1289
|
+
sendStart = true,
|
|
1290
|
+
sendFinish = true,
|
|
1291
|
+
sendReasoning = false,
|
|
1292
|
+
sendSources = false
|
|
932
1293
|
}) {
|
|
933
1294
|
if (!agent && !path.includes("/:agentId")) {
|
|
934
1295
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -957,6 +1318,14 @@ function chatRoute({
|
|
|
957
1318
|
schema: {
|
|
958
1319
|
type: "object",
|
|
959
1320
|
properties: {
|
|
1321
|
+
resumeData: {
|
|
1322
|
+
type: "object",
|
|
1323
|
+
description: "Resume data for the agent"
|
|
1324
|
+
},
|
|
1325
|
+
runId: {
|
|
1326
|
+
type: "string",
|
|
1327
|
+
description: "The run ID required when resuming an agent execution"
|
|
1328
|
+
},
|
|
960
1329
|
messages: {
|
|
961
1330
|
type: "array",
|
|
962
1331
|
description: "Array of messages in the conversation",
|
|
@@ -1027,9 +1396,9 @@ function chatRoute({
|
|
|
1027
1396
|
}
|
|
1028
1397
|
},
|
|
1029
1398
|
handler: async (c) => {
|
|
1030
|
-
const
|
|
1399
|
+
const params = await c.req.json();
|
|
1031
1400
|
const mastra = c.get("mastra");
|
|
1032
|
-
const
|
|
1401
|
+
const contextRequestContext = c.get("requestContext");
|
|
1033
1402
|
let agentToUse = agent;
|
|
1034
1403
|
if (!agent) {
|
|
1035
1404
|
const agentId = c.req.param("agentId");
|
|
@@ -1040,28 +1409,24 @@ function chatRoute({
|
|
|
1040
1409
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1041
1410
|
);
|
|
1042
1411
|
}
|
|
1043
|
-
if (
|
|
1044
|
-
mastra.getLogger()?.warn(`"
|
|
1412
|
+
if (contextRequestContext && defaultOptions?.requestContext) {
|
|
1413
|
+
mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
|
|
1045
1414
|
}
|
|
1046
1415
|
if (!agentToUse) {
|
|
1047
1416
|
throw new Error("Agent ID is required");
|
|
1048
1417
|
}
|
|
1049
|
-
const
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
for await (const part of toAISdkFormat(result, { from: "agent" })) {
|
|
1062
|
-
writer.write(part);
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1418
|
+
const uiMessageStream = await handleChatStream({
|
|
1419
|
+
mastra,
|
|
1420
|
+
agentId: agentToUse,
|
|
1421
|
+
params: {
|
|
1422
|
+
...params,
|
|
1423
|
+
requestContext: contextRequestContext || params.requestContext
|
|
1424
|
+
},
|
|
1425
|
+
defaultOptions,
|
|
1426
|
+
sendStart,
|
|
1427
|
+
sendFinish,
|
|
1428
|
+
sendReasoning,
|
|
1429
|
+
sendSources
|
|
1065
1430
|
});
|
|
1066
1431
|
return createUIMessageStreamResponse({
|
|
1067
1432
|
stream: uiMessageStream
|
|
@@ -1069,9 +1434,31 @@ function chatRoute({
|
|
|
1069
1434
|
}
|
|
1070
1435
|
});
|
|
1071
1436
|
}
|
|
1437
|
+
async function handleWorkflowStream({
|
|
1438
|
+
mastra,
|
|
1439
|
+
workflowId,
|
|
1440
|
+
params,
|
|
1441
|
+
includeTextStreamParts = true
|
|
1442
|
+
}) {
|
|
1443
|
+
const { runId, resourceId, inputData, resumeData, requestContext, ...rest } = params;
|
|
1444
|
+
const workflowObj = mastra.getWorkflowById(workflowId);
|
|
1445
|
+
if (!workflowObj) {
|
|
1446
|
+
throw new Error(`Workflow ${workflowId} not found`);
|
|
1447
|
+
}
|
|
1448
|
+
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1449
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext }) : run.stream({ inputData, ...rest, requestContext });
|
|
1450
|
+
return createUIMessageStream({
|
|
1451
|
+
execute: async ({ writer }) => {
|
|
1452
|
+
for await (const part of toAISdkV5Stream(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1453
|
+
writer.write(part);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1072
1458
|
function workflowRoute({
|
|
1073
1459
|
path = "/api/workflows/:workflowId/stream",
|
|
1074
|
-
workflow
|
|
1460
|
+
workflow,
|
|
1461
|
+
includeTextStreamParts = true
|
|
1075
1462
|
}) {
|
|
1076
1463
|
if (!workflow && !path.includes("/:workflowId")) {
|
|
1077
1464
|
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
@@ -1098,9 +1485,13 @@ function workflowRoute({
|
|
|
1098
1485
|
schema: {
|
|
1099
1486
|
type: "object",
|
|
1100
1487
|
properties: {
|
|
1488
|
+
runId: { type: "string" },
|
|
1489
|
+
resourceId: { type: "string" },
|
|
1101
1490
|
inputData: { type: "object", additionalProperties: true },
|
|
1102
|
-
|
|
1103
|
-
|
|
1491
|
+
resumeData: { type: "object", additionalProperties: true },
|
|
1492
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1493
|
+
tracingOptions: { type: "object", additionalProperties: true },
|
|
1494
|
+
step: { type: "string" }
|
|
1104
1495
|
}
|
|
1105
1496
|
}
|
|
1106
1497
|
}
|
|
@@ -1118,8 +1509,9 @@ function workflowRoute({
|
|
|
1118
1509
|
}
|
|
1119
1510
|
},
|
|
1120
1511
|
handler: async (c) => {
|
|
1121
|
-
const
|
|
1512
|
+
const params = await c.req.json();
|
|
1122
1513
|
const mastra = c.get("mastra");
|
|
1514
|
+
const contextRequestContext = c.get("requestContext");
|
|
1123
1515
|
let workflowToUse = workflow;
|
|
1124
1516
|
if (!workflow) {
|
|
1125
1517
|
const workflowId = c.req.param("workflowId");
|
|
@@ -1133,23 +1525,47 @@ function workflowRoute({
|
|
|
1133
1525
|
if (!workflowToUse) {
|
|
1134
1526
|
throw new Error("Workflow ID is required");
|
|
1135
1527
|
}
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1528
|
+
if (contextRequestContext && params.requestContext) {
|
|
1529
|
+
mastra.getLogger()?.warn(
|
|
1530
|
+
`"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
|
|
1531
|
+
);
|
|
1139
1532
|
}
|
|
1140
|
-
const
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1533
|
+
const uiMessageStream = await handleWorkflowStream({
|
|
1534
|
+
mastra,
|
|
1535
|
+
workflowId: workflowToUse,
|
|
1536
|
+
params: {
|
|
1537
|
+
...params,
|
|
1538
|
+
requestContext: contextRequestContext || params.requestContext
|
|
1539
|
+
},
|
|
1540
|
+
includeTextStreamParts
|
|
1148
1541
|
});
|
|
1149
1542
|
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1150
1543
|
}
|
|
1151
1544
|
});
|
|
1152
1545
|
}
|
|
1546
|
+
async function handleNetworkStream({
|
|
1547
|
+
mastra,
|
|
1548
|
+
agentId,
|
|
1549
|
+
params,
|
|
1550
|
+
defaultOptions
|
|
1551
|
+
}) {
|
|
1552
|
+
const { messages, ...rest } = params;
|
|
1553
|
+
const agentObj = mastra.getAgentById(agentId);
|
|
1554
|
+
if (!agentObj) {
|
|
1555
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
1556
|
+
}
|
|
1557
|
+
const result = await agentObj.network(messages, {
|
|
1558
|
+
...defaultOptions,
|
|
1559
|
+
...rest
|
|
1560
|
+
});
|
|
1561
|
+
return createUIMessageStream({
|
|
1562
|
+
execute: async ({ writer }) => {
|
|
1563
|
+
for await (const part of toAISdkV5Stream(result, { from: "network" })) {
|
|
1564
|
+
writer.write(part);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1153
1569
|
function networkRoute({
|
|
1154
1570
|
path = "/network/:agentId",
|
|
1155
1571
|
agent,
|
|
@@ -1181,13 +1597,12 @@ function networkRoute({
|
|
|
1181
1597
|
type: "object",
|
|
1182
1598
|
properties: {
|
|
1183
1599
|
messages: { type: "array", items: { type: "object" } },
|
|
1184
|
-
|
|
1600
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1185
1601
|
runId: { type: "string" },
|
|
1186
1602
|
maxSteps: { type: "number" },
|
|
1187
1603
|
threadId: { type: "string" },
|
|
1188
1604
|
resourceId: { type: "string" },
|
|
1189
1605
|
modelSettings: { type: "object", additionalProperties: true },
|
|
1190
|
-
telemetry: { type: "object", additionalProperties: true },
|
|
1191
1606
|
tools: { type: "array", items: { type: "object" } }
|
|
1192
1607
|
},
|
|
1193
1608
|
required: ["messages"]
|
|
@@ -1211,7 +1626,7 @@ function networkRoute({
|
|
|
1211
1626
|
}
|
|
1212
1627
|
},
|
|
1213
1628
|
handler: async (c) => {
|
|
1214
|
-
const
|
|
1629
|
+
const params = await c.req.json();
|
|
1215
1630
|
const mastra = c.get("mastra");
|
|
1216
1631
|
let agentToUse = agent;
|
|
1217
1632
|
if (!agent) {
|
|
@@ -1226,26 +1641,24 @@ function networkRoute({
|
|
|
1226
1641
|
if (!agentToUse) {
|
|
1227
1642
|
throw new Error("Agent ID is required");
|
|
1228
1643
|
}
|
|
1229
|
-
const
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
...defaultOptions,
|
|
1235
|
-
...rest
|
|
1236
|
-
});
|
|
1237
|
-
const uiMessageStream = createUIMessageStream({
|
|
1238
|
-
execute: async ({ writer }) => {
|
|
1239
|
-
for await (const part of toAISdkFormat(result, { from: "network" })) {
|
|
1240
|
-
writer.write(part);
|
|
1241
|
-
}
|
|
1242
|
-
}
|
|
1644
|
+
const uiMessageStream = await handleNetworkStream({
|
|
1645
|
+
mastra,
|
|
1646
|
+
agentId: agentToUse,
|
|
1647
|
+
params,
|
|
1648
|
+
defaultOptions
|
|
1243
1649
|
});
|
|
1244
1650
|
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1245
1651
|
}
|
|
1246
1652
|
});
|
|
1247
1653
|
}
|
|
1248
1654
|
|
|
1249
|
-
|
|
1655
|
+
// src/to-ai-sdk-format.ts
|
|
1656
|
+
function toAISdkFormat() {
|
|
1657
|
+
throw new Error(
|
|
1658
|
+
'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.'
|
|
1659
|
+
);
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
export { chatRoute, handleChatStream, handleNetworkStream, handleWorkflowStream, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
|
|
1250
1663
|
//# sourceMappingURL=index.js.map
|
|
1251
1664
|
//# sourceMappingURL=index.js.map
|