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