@mastra/ai-sdk 0.0.0-chore-core-swap-aiv5-ai-package-naming-20251009203931 → 0.0.0-client-js-listmessages-agentid-fix-20251119175531
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 +265 -5
- package/README.md +65 -1
- package/dist/chat-route.d.ts +52 -2
- 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 +66 -0
- package/dist/convert-streams.d.ts.map +1 -0
- package/dist/helpers.d.ts +13 -4
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +719 -96
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +717 -96
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts +14 -0
- package/dist/network-route.d.ts.map +1 -0
- package/dist/to-ai-sdk-format.d.ts +15 -32
- package/dist/to-ai-sdk-format.d.ts.map +1 -1
- package/dist/transformers.d.ts +139 -0
- package/dist/transformers.d.ts.map +1 -0
- package/dist/ui.cjs +16 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.ts +2 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +13 -0
- package/dist/ui.js.map +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/workflow-route.d.ts +10 -0
- package/dist/workflow-route.d.ts.map +1 -0
- package/package.json +22 -7
package/dist/index.js
CHANGED
|
@@ -3,6 +3,33 @@ import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
|
3
3
|
import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core/stream';
|
|
4
4
|
|
|
5
5
|
// src/chat-route.ts
|
|
6
|
+
|
|
7
|
+
// src/utils.ts
|
|
8
|
+
var isDataChunkType = (chunk) => {
|
|
9
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
10
|
+
};
|
|
11
|
+
function safeParseErrorObject(obj) {
|
|
12
|
+
if (typeof obj !== "object" || obj === null) {
|
|
13
|
+
return String(obj);
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const stringified = JSON.stringify(obj);
|
|
17
|
+
if (stringified === "{}") {
|
|
18
|
+
return String(obj);
|
|
19
|
+
}
|
|
20
|
+
return stringified;
|
|
21
|
+
} catch {
|
|
22
|
+
return String(obj);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
var isAgentExecutionDataChunkType = (chunk) => {
|
|
26
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
27
|
+
};
|
|
28
|
+
var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
29
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/helpers.ts
|
|
6
33
|
function convertMastraChunkToAISDKv5({
|
|
7
34
|
chunk,
|
|
8
35
|
mode = "stream"
|
|
@@ -208,6 +235,9 @@ function convertMastraChunkToAISDKv5({
|
|
|
208
235
|
...chunk.payload || {}
|
|
209
236
|
};
|
|
210
237
|
}
|
|
238
|
+
if ("type" in chunk && chunk.type?.startsWith("data-")) {
|
|
239
|
+
return chunk;
|
|
240
|
+
}
|
|
211
241
|
return;
|
|
212
242
|
}
|
|
213
243
|
}
|
|
@@ -221,7 +251,7 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
221
251
|
sendFinish,
|
|
222
252
|
responseMessageId
|
|
223
253
|
}) {
|
|
224
|
-
const partType = part
|
|
254
|
+
const partType = part?.type;
|
|
225
255
|
switch (partType) {
|
|
226
256
|
case "text-start": {
|
|
227
257
|
return {
|
|
@@ -253,6 +283,14 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
253
283
|
};
|
|
254
284
|
}
|
|
255
285
|
case "reasoning-delta": {
|
|
286
|
+
if (sendReasoning) {
|
|
287
|
+
return {
|
|
288
|
+
type: "reasoning-delta",
|
|
289
|
+
id: part.id,
|
|
290
|
+
delta: part.text,
|
|
291
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
256
294
|
return;
|
|
257
295
|
}
|
|
258
296
|
case "reasoning-end": {
|
|
@@ -270,6 +308,25 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
270
308
|
};
|
|
271
309
|
}
|
|
272
310
|
case "source": {
|
|
311
|
+
if (sendSources && part.sourceType === "url") {
|
|
312
|
+
return {
|
|
313
|
+
type: "source-url",
|
|
314
|
+
sourceId: part.id,
|
|
315
|
+
url: part.url,
|
|
316
|
+
title: part.title,
|
|
317
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
if (sendSources && part.sourceType === "document") {
|
|
321
|
+
return {
|
|
322
|
+
type: "source-document",
|
|
323
|
+
sourceId: part.id,
|
|
324
|
+
mediaType: part.mediaType,
|
|
325
|
+
title: part.title,
|
|
326
|
+
filename: part.filename,
|
|
327
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
273
330
|
return;
|
|
274
331
|
}
|
|
275
332
|
case "tool-input-start": {
|
|
@@ -315,6 +372,26 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
315
372
|
toolCallId: part.toolCallId,
|
|
316
373
|
payload: part.output
|
|
317
374
|
};
|
|
375
|
+
} else if (part.output.from === "WORKFLOW") {
|
|
376
|
+
return {
|
|
377
|
+
type: "tool-workflow",
|
|
378
|
+
toolCallId: part.toolCallId,
|
|
379
|
+
payload: part.output
|
|
380
|
+
};
|
|
381
|
+
} else if (part.output.from === "NETWORK") {
|
|
382
|
+
return {
|
|
383
|
+
type: "tool-network",
|
|
384
|
+
toolCallId: part.toolCallId,
|
|
385
|
+
payload: part.output
|
|
386
|
+
};
|
|
387
|
+
} else if (isDataChunkType(part.output)) {
|
|
388
|
+
if (!("data" in part.output)) {
|
|
389
|
+
throw new Error(
|
|
390
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
391
|
+
${JSON.stringify(part)}`
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
return part.output;
|
|
318
395
|
}
|
|
319
396
|
return;
|
|
320
397
|
}
|
|
@@ -340,21 +417,23 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
340
417
|
return { type: "finish-step" };
|
|
341
418
|
}
|
|
342
419
|
case "start": {
|
|
343
|
-
{
|
|
420
|
+
if (sendStart) {
|
|
344
421
|
return {
|
|
345
422
|
type: "start",
|
|
346
423
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
347
424
|
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
348
425
|
};
|
|
349
426
|
}
|
|
427
|
+
return;
|
|
350
428
|
}
|
|
351
429
|
case "finish": {
|
|
352
|
-
{
|
|
430
|
+
if (sendFinish) {
|
|
353
431
|
return {
|
|
354
432
|
type: "finish",
|
|
355
433
|
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
356
434
|
};
|
|
357
435
|
}
|
|
436
|
+
return;
|
|
358
437
|
}
|
|
359
438
|
case "abort": {
|
|
360
439
|
return part;
|
|
@@ -366,119 +445,79 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
366
445
|
return;
|
|
367
446
|
}
|
|
368
447
|
default: {
|
|
369
|
-
|
|
370
|
-
|
|
448
|
+
if (isDataChunkType(part)) {
|
|
449
|
+
if (!("data" in part)) {
|
|
450
|
+
throw new Error(
|
|
451
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
452
|
+
${JSON.stringify(part)}`
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
return part;
|
|
456
|
+
}
|
|
457
|
+
return;
|
|
371
458
|
}
|
|
372
459
|
}
|
|
373
460
|
}
|
|
374
461
|
|
|
375
|
-
// src/
|
|
462
|
+
// src/transformers.ts
|
|
376
463
|
function WorkflowStreamToAISDKTransformer() {
|
|
377
|
-
const
|
|
464
|
+
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
378
465
|
return new TransformStream({
|
|
379
466
|
start(controller) {
|
|
380
467
|
controller.enqueue({
|
|
381
|
-
|
|
382
|
-
type: "start",
|
|
383
|
-
messageId: "1"
|
|
384
|
-
})
|
|
468
|
+
type: "start"
|
|
385
469
|
});
|
|
386
470
|
},
|
|
387
471
|
flush(controller) {
|
|
388
472
|
controller.enqueue({
|
|
389
|
-
|
|
390
|
-
type: "finish"
|
|
391
|
-
})
|
|
473
|
+
type: "finish"
|
|
392
474
|
});
|
|
475
|
+
},
|
|
476
|
+
transform(chunk, controller) {
|
|
477
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows);
|
|
478
|
+
if (transformed) controller.enqueue(transformed);
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
function AgentNetworkToAISDKTransformer() {
|
|
483
|
+
const bufferedNetworks = /* @__PURE__ */ new Map();
|
|
484
|
+
return new TransformStream({
|
|
485
|
+
start(controller) {
|
|
486
|
+
controller.enqueue({
|
|
487
|
+
type: "start"
|
|
488
|
+
});
|
|
489
|
+
},
|
|
490
|
+
flush(controller) {
|
|
393
491
|
controller.enqueue({
|
|
394
|
-
|
|
492
|
+
type: "finish"
|
|
395
493
|
});
|
|
396
494
|
},
|
|
397
495
|
transform(chunk, controller) {
|
|
398
|
-
|
|
399
|
-
if (
|
|
400
|
-
workflowName = chunk.payload.workflowId;
|
|
401
|
-
controller.enqueue({
|
|
402
|
-
data: JSON.stringify({
|
|
403
|
-
type: "data-workflow",
|
|
404
|
-
id: chunk.runId,
|
|
405
|
-
data: {
|
|
406
|
-
name: workflowName,
|
|
407
|
-
status: "running",
|
|
408
|
-
steps: {},
|
|
409
|
-
output: null
|
|
410
|
-
}
|
|
411
|
-
})
|
|
412
|
-
});
|
|
413
|
-
} else if (chunk.type === "workflow-step-start") {
|
|
414
|
-
steps[chunk.payload.id] = {
|
|
415
|
-
// TODO swap with name
|
|
416
|
-
name: chunk.payload.id,
|
|
417
|
-
status: chunk.payload.status,
|
|
418
|
-
input: chunk.payload.payload ?? null,
|
|
419
|
-
output: null
|
|
420
|
-
};
|
|
421
|
-
controller.enqueue({
|
|
422
|
-
data: JSON.stringify({
|
|
423
|
-
type: "data-workflow",
|
|
424
|
-
id: chunk.runId,
|
|
425
|
-
data: {
|
|
426
|
-
name: workflowName,
|
|
427
|
-
status: "running",
|
|
428
|
-
steps,
|
|
429
|
-
output: null
|
|
430
|
-
}
|
|
431
|
-
})
|
|
432
|
-
});
|
|
433
|
-
} else if (chunk.type === "workflow-step-result") {
|
|
434
|
-
steps[chunk.payload.id] = {
|
|
435
|
-
...steps[chunk.payload.id],
|
|
436
|
-
status: chunk.payload.status,
|
|
437
|
-
output: chunk.payload.output ?? null
|
|
438
|
-
};
|
|
439
|
-
controller.enqueue({
|
|
440
|
-
data: JSON.stringify({
|
|
441
|
-
type: "data-workflow",
|
|
442
|
-
id: chunk.runId,
|
|
443
|
-
data: {
|
|
444
|
-
name: workflowName,
|
|
445
|
-
status: "running",
|
|
446
|
-
steps,
|
|
447
|
-
output: null
|
|
448
|
-
}
|
|
449
|
-
})
|
|
450
|
-
});
|
|
451
|
-
} else if (chunk.type === "workflow-finish") {
|
|
452
|
-
controller.enqueue({
|
|
453
|
-
data: JSON.stringify({
|
|
454
|
-
type: "data-workflow",
|
|
455
|
-
id: chunk.runId,
|
|
456
|
-
data: {
|
|
457
|
-
name: workflowName,
|
|
458
|
-
steps,
|
|
459
|
-
output: chunk.payload.output ?? null,
|
|
460
|
-
status: chunk.payload.workflowStatus
|
|
461
|
-
}
|
|
462
|
-
})
|
|
463
|
-
});
|
|
464
|
-
}
|
|
496
|
+
const transformed = transformNetwork(chunk, bufferedNetworks);
|
|
497
|
+
if (transformed) controller.enqueue(transformed);
|
|
465
498
|
}
|
|
466
499
|
});
|
|
467
500
|
}
|
|
468
|
-
function AgentStreamToAISDKTransformer(
|
|
501
|
+
function AgentStreamToAISDKTransformer({
|
|
502
|
+
lastMessageId,
|
|
503
|
+
sendStart,
|
|
504
|
+
sendFinish,
|
|
505
|
+
sendReasoning,
|
|
506
|
+
sendSources
|
|
507
|
+
}) {
|
|
469
508
|
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
470
509
|
return new TransformStream({
|
|
471
510
|
transform(chunk, controller) {
|
|
472
511
|
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
473
512
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
474
513
|
part,
|
|
475
|
-
sendReasoning
|
|
476
|
-
sendSources
|
|
477
|
-
sendStart
|
|
478
|
-
sendFinish
|
|
479
|
-
responseMessageId:
|
|
480
|
-
onError() {
|
|
481
|
-
return
|
|
514
|
+
sendReasoning,
|
|
515
|
+
sendSources,
|
|
516
|
+
sendStart,
|
|
517
|
+
sendFinish,
|
|
518
|
+
responseMessageId: lastMessageId,
|
|
519
|
+
onError(error) {
|
|
520
|
+
return safeParseErrorObject(error);
|
|
482
521
|
}
|
|
483
522
|
});
|
|
484
523
|
if (transformedChunk) {
|
|
@@ -486,6 +525,14 @@ function AgentStreamToAISDKTransformer() {
|
|
|
486
525
|
const payload = transformedChunk.payload;
|
|
487
526
|
const agentTransformed = transformAgent(payload, bufferedSteps);
|
|
488
527
|
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
528
|
+
} else if (transformedChunk.type === "tool-workflow") {
|
|
529
|
+
const payload = transformedChunk.payload;
|
|
530
|
+
const workflowChunk = transformWorkflow(payload, bufferedSteps, true);
|
|
531
|
+
if (workflowChunk) controller.enqueue(workflowChunk);
|
|
532
|
+
} else if (transformedChunk.type === "tool-network") {
|
|
533
|
+
const payload = transformedChunk.payload;
|
|
534
|
+
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
535
|
+
if (networkChunk) controller.enqueue(networkChunk);
|
|
489
536
|
} else {
|
|
490
537
|
controller.enqueue(transformedChunk);
|
|
491
538
|
}
|
|
@@ -629,15 +676,386 @@ function transformAgent(payload, bufferedSteps) {
|
|
|
629
676
|
}
|
|
630
677
|
return null;
|
|
631
678
|
}
|
|
632
|
-
function
|
|
633
|
-
|
|
679
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested) {
|
|
680
|
+
switch (payload.type) {
|
|
681
|
+
case "workflow-start":
|
|
682
|
+
bufferedWorkflows.set(payload.runId, {
|
|
683
|
+
name: payload.payload.workflowId,
|
|
684
|
+
steps: {}
|
|
685
|
+
});
|
|
686
|
+
return {
|
|
687
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
688
|
+
id: payload.runId,
|
|
689
|
+
data: {
|
|
690
|
+
name: bufferedWorkflows.get(payload.runId).name,
|
|
691
|
+
status: "running",
|
|
692
|
+
steps: bufferedWorkflows.get(payload.runId).steps,
|
|
693
|
+
output: null
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
case "workflow-step-start": {
|
|
697
|
+
const current = bufferedWorkflows.get(payload.runId) || { name: "", steps: {} };
|
|
698
|
+
current.steps[payload.payload.id] = {
|
|
699
|
+
name: payload.payload.id,
|
|
700
|
+
status: payload.payload.status,
|
|
701
|
+
input: payload.payload.payload ?? null,
|
|
702
|
+
output: null,
|
|
703
|
+
suspendPayload: null,
|
|
704
|
+
resumePayload: null
|
|
705
|
+
};
|
|
706
|
+
bufferedWorkflows.set(payload.runId, current);
|
|
707
|
+
return {
|
|
708
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
709
|
+
id: payload.runId,
|
|
710
|
+
data: {
|
|
711
|
+
name: current.name,
|
|
712
|
+
status: "running",
|
|
713
|
+
steps: current.steps,
|
|
714
|
+
output: null
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
case "workflow-step-result": {
|
|
719
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
720
|
+
if (!current) return null;
|
|
721
|
+
current.steps[payload.payload.id] = {
|
|
722
|
+
...current.steps[payload.payload.id],
|
|
723
|
+
status: payload.payload.status,
|
|
724
|
+
output: payload.payload.output ?? null
|
|
725
|
+
};
|
|
726
|
+
return {
|
|
727
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
728
|
+
id: payload.runId,
|
|
729
|
+
data: {
|
|
730
|
+
name: current.name,
|
|
731
|
+
status: "running",
|
|
732
|
+
steps: current.steps,
|
|
733
|
+
output: null
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
case "workflow-step-suspended": {
|
|
738
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
739
|
+
if (!current) return null;
|
|
740
|
+
current.steps[payload.payload.id] = {
|
|
741
|
+
...current.steps[payload.payload.id],
|
|
742
|
+
status: payload.payload.status,
|
|
743
|
+
suspendPayload: payload.payload.suspendPayload ?? null,
|
|
744
|
+
resumePayload: payload.payload.resumePayload ?? null,
|
|
745
|
+
output: null
|
|
746
|
+
};
|
|
747
|
+
return {
|
|
748
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
749
|
+
id: payload.runId,
|
|
750
|
+
data: {
|
|
751
|
+
name: current.name,
|
|
752
|
+
status: "suspended",
|
|
753
|
+
steps: current.steps,
|
|
754
|
+
output: null
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
case "workflow-finish": {
|
|
759
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
760
|
+
if (!current) return null;
|
|
761
|
+
return {
|
|
762
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
763
|
+
id: payload.runId,
|
|
764
|
+
data: {
|
|
765
|
+
name: current.name,
|
|
766
|
+
steps: current.steps,
|
|
767
|
+
output: payload.payload.output ?? null,
|
|
768
|
+
status: payload.payload.workflowStatus
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
default: {
|
|
773
|
+
if (isDataChunkType(payload)) {
|
|
774
|
+
if (!("data" in payload)) {
|
|
775
|
+
throw new Error(
|
|
776
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
777
|
+
${JSON.stringify(payload)}`
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
return payload;
|
|
781
|
+
}
|
|
782
|
+
return null;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
787
|
+
switch (payload.type) {
|
|
788
|
+
case "routing-agent-start": {
|
|
789
|
+
if (!bufferedNetworks.has(payload.runId)) {
|
|
790
|
+
bufferedNetworks.set(payload.runId, {
|
|
791
|
+
name: payload.payload.agentId,
|
|
792
|
+
steps: [],
|
|
793
|
+
usage: null,
|
|
794
|
+
output: null
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
return {
|
|
798
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
799
|
+
id: payload.runId,
|
|
800
|
+
data: {
|
|
801
|
+
name: bufferedNetworks.get(payload.runId).name,
|
|
802
|
+
status: "running",
|
|
803
|
+
usage: null,
|
|
804
|
+
steps: bufferedNetworks.get(payload.runId).steps,
|
|
805
|
+
output: null
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
case "routing-agent-text-start": {
|
|
810
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
811
|
+
if (!current) return null;
|
|
812
|
+
return {
|
|
813
|
+
type: "text-start",
|
|
814
|
+
id: payload.runId
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
case "routing-agent-text-delta": {
|
|
818
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
819
|
+
if (!current) return null;
|
|
820
|
+
return {
|
|
821
|
+
type: "text-delta",
|
|
822
|
+
id: payload.runId,
|
|
823
|
+
delta: payload.payload.text
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
case "agent-execution-start": {
|
|
827
|
+
const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
|
|
828
|
+
current.steps.push({
|
|
829
|
+
name: payload.payload.agentId,
|
|
830
|
+
status: "running",
|
|
831
|
+
input: payload.payload.args || null,
|
|
832
|
+
output: null,
|
|
833
|
+
suspendPayload: null,
|
|
834
|
+
resumePayload: null
|
|
835
|
+
});
|
|
836
|
+
bufferedNetworks.set(payload.runId, current);
|
|
837
|
+
return {
|
|
838
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
839
|
+
id: payload.runId,
|
|
840
|
+
data: {
|
|
841
|
+
...current,
|
|
842
|
+
status: "running"
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
case "workflow-execution-start": {
|
|
847
|
+
const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
|
|
848
|
+
current.steps.push({
|
|
849
|
+
name: payload.payload.name,
|
|
850
|
+
status: "running",
|
|
851
|
+
input: payload.payload.args || null,
|
|
852
|
+
output: null,
|
|
853
|
+
suspendPayload: null,
|
|
854
|
+
resumePayload: null
|
|
855
|
+
});
|
|
856
|
+
bufferedNetworks.set(payload.runId, current);
|
|
857
|
+
return {
|
|
858
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
859
|
+
id: payload.runId,
|
|
860
|
+
data: {
|
|
861
|
+
...current,
|
|
862
|
+
status: "running"
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
case "tool-execution-start": {
|
|
867
|
+
const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
|
|
868
|
+
current.steps.push({
|
|
869
|
+
name: payload.payload.args?.toolName,
|
|
870
|
+
status: "running",
|
|
871
|
+
input: payload.payload.args?.args || null,
|
|
872
|
+
output: null,
|
|
873
|
+
suspendPayload: null,
|
|
874
|
+
resumePayload: null
|
|
875
|
+
});
|
|
876
|
+
bufferedNetworks.set(payload.runId, current);
|
|
877
|
+
return {
|
|
878
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
879
|
+
id: payload.runId,
|
|
880
|
+
data: {
|
|
881
|
+
...current,
|
|
882
|
+
status: "running"
|
|
883
|
+
}
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
case "agent-execution-end": {
|
|
887
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
888
|
+
if (!current) return null;
|
|
889
|
+
current.steps.push({
|
|
890
|
+
name: payload.payload.agentId,
|
|
891
|
+
status: "success",
|
|
892
|
+
input: null,
|
|
893
|
+
output: payload.payload.result,
|
|
894
|
+
suspendPayload: null,
|
|
895
|
+
resumePayload: null
|
|
896
|
+
});
|
|
897
|
+
return {
|
|
898
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
899
|
+
id: payload.runId,
|
|
900
|
+
data: {
|
|
901
|
+
...current,
|
|
902
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
903
|
+
status: "running",
|
|
904
|
+
output: payload.payload.result ?? current.output
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
case "tool-execution-end": {
|
|
909
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
910
|
+
if (!current) return null;
|
|
911
|
+
current.steps.push({
|
|
912
|
+
name: payload.payload.toolName,
|
|
913
|
+
status: "success",
|
|
914
|
+
input: null,
|
|
915
|
+
output: payload.payload.result,
|
|
916
|
+
suspendPayload: null,
|
|
917
|
+
resumePayload: null
|
|
918
|
+
});
|
|
919
|
+
return {
|
|
920
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
921
|
+
id: payload.runId,
|
|
922
|
+
data: {
|
|
923
|
+
...current,
|
|
924
|
+
status: "running",
|
|
925
|
+
output: payload.payload.result ?? current.output
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
case "workflow-execution-end": {
|
|
930
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
931
|
+
if (!current) return null;
|
|
932
|
+
current.steps.push({
|
|
933
|
+
name: payload.payload.name,
|
|
934
|
+
status: "success",
|
|
935
|
+
input: null,
|
|
936
|
+
output: payload.payload.result,
|
|
937
|
+
suspendPayload: null,
|
|
938
|
+
resumePayload: null
|
|
939
|
+
});
|
|
940
|
+
return {
|
|
941
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
942
|
+
id: payload.runId,
|
|
943
|
+
data: {
|
|
944
|
+
...current,
|
|
945
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
946
|
+
status: "running",
|
|
947
|
+
output: payload.payload.result ?? current.output
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
case "routing-agent-end": {
|
|
952
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
953
|
+
if (!current) return null;
|
|
954
|
+
return {
|
|
955
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
956
|
+
id: payload.runId,
|
|
957
|
+
data: {
|
|
958
|
+
...current,
|
|
959
|
+
status: "finished",
|
|
960
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
961
|
+
output: payload.payload?.result ?? current.output
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
case "network-execution-event-step-finish": {
|
|
966
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
967
|
+
if (!current) return null;
|
|
968
|
+
return {
|
|
969
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
970
|
+
id: payload.runId,
|
|
971
|
+
data: {
|
|
972
|
+
...current,
|
|
973
|
+
status: "finished",
|
|
974
|
+
output: payload.payload?.result ?? current.output
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
case "network-execution-event-finish": {
|
|
979
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
980
|
+
if (!current) return null;
|
|
981
|
+
return {
|
|
982
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
983
|
+
id: payload.runId,
|
|
984
|
+
data: {
|
|
985
|
+
...current,
|
|
986
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
987
|
+
status: "finished",
|
|
988
|
+
output: payload.payload?.result ?? current.output
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
default: {
|
|
993
|
+
if (isDataChunkType(payload)) {
|
|
994
|
+
if (!("data" in payload)) {
|
|
995
|
+
throw new Error(
|
|
996
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
997
|
+
${JSON.stringify(payload)}`
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
return payload;
|
|
1001
|
+
}
|
|
1002
|
+
if (isAgentExecutionDataChunkType(payload)) {
|
|
1003
|
+
if (!("data" in payload.payload)) {
|
|
1004
|
+
throw new Error(
|
|
1005
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1006
|
+
${JSON.stringify(payload)}`
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
return payload.payload;
|
|
1010
|
+
}
|
|
1011
|
+
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1012
|
+
if (!("data" in payload.payload)) {
|
|
1013
|
+
throw new Error(
|
|
1014
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1015
|
+
${JSON.stringify(payload)}`
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
return payload.payload;
|
|
1019
|
+
}
|
|
1020
|
+
return null;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// src/convert-streams.ts
|
|
1026
|
+
function toAISdkV5Stream(stream, options = {
|
|
1027
|
+
from: "agent",
|
|
1028
|
+
sendStart: true,
|
|
1029
|
+
sendFinish: true
|
|
1030
|
+
}) {
|
|
1031
|
+
const from = options?.from;
|
|
1032
|
+
if (from === "workflow") {
|
|
1033
|
+
return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
|
|
1034
|
+
}
|
|
1035
|
+
if (from === "network") {
|
|
1036
|
+
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
1037
|
+
}
|
|
1038
|
+
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
1039
|
+
return agentReadable.pipeThrough(
|
|
1040
|
+
AgentStreamToAISDKTransformer({
|
|
1041
|
+
lastMessageId: options?.lastMessageId,
|
|
1042
|
+
sendStart: options?.sendStart,
|
|
1043
|
+
sendFinish: options?.sendFinish,
|
|
1044
|
+
sendReasoning: options?.sendReasoning,
|
|
1045
|
+
sendSources: options?.sendSources
|
|
1046
|
+
})
|
|
1047
|
+
);
|
|
634
1048
|
}
|
|
635
1049
|
|
|
636
1050
|
// src/chat-route.ts
|
|
637
1051
|
function chatRoute({
|
|
638
1052
|
path = "/chat/:agentId",
|
|
639
1053
|
agent,
|
|
640
|
-
defaultOptions
|
|
1054
|
+
defaultOptions,
|
|
1055
|
+
sendStart = true,
|
|
1056
|
+
sendFinish = true,
|
|
1057
|
+
sendReasoning = false,
|
|
1058
|
+
sendSources = false
|
|
641
1059
|
}) {
|
|
642
1060
|
if (!agent && !path.includes("/:agentId")) {
|
|
643
1061
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -738,6 +1156,7 @@ function chatRoute({
|
|
|
738
1156
|
handler: async (c) => {
|
|
739
1157
|
const { messages, ...rest } = await c.req.json();
|
|
740
1158
|
const mastra = c.get("mastra");
|
|
1159
|
+
const requestContext = c.get("requestContext");
|
|
741
1160
|
let agentToUse = agent;
|
|
742
1161
|
if (!agent) {
|
|
743
1162
|
const agentId = c.req.param("agentId");
|
|
@@ -748,6 +1167,9 @@ function chatRoute({
|
|
|
748
1167
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
749
1168
|
);
|
|
750
1169
|
}
|
|
1170
|
+
if (requestContext && defaultOptions?.requestContext) {
|
|
1171
|
+
mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
|
|
1172
|
+
}
|
|
751
1173
|
if (!agentToUse) {
|
|
752
1174
|
throw new Error("Agent ID is required");
|
|
753
1175
|
}
|
|
@@ -757,11 +1179,24 @@ function chatRoute({
|
|
|
757
1179
|
}
|
|
758
1180
|
const result = await agentObj.stream(messages, {
|
|
759
1181
|
...defaultOptions,
|
|
760
|
-
...rest
|
|
1182
|
+
...rest,
|
|
1183
|
+
requestContext: requestContext || defaultOptions?.requestContext
|
|
761
1184
|
});
|
|
1185
|
+
let lastMessageId;
|
|
1186
|
+
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
1187
|
+
lastMessageId = messages[messages.length - 1].id;
|
|
1188
|
+
}
|
|
762
1189
|
const uiMessageStream = createUIMessageStream({
|
|
1190
|
+
originalMessages: messages,
|
|
763
1191
|
execute: async ({ writer }) => {
|
|
764
|
-
for await (const part of
|
|
1192
|
+
for await (const part of toAISdkV5Stream(result, {
|
|
1193
|
+
from: "agent",
|
|
1194
|
+
lastMessageId,
|
|
1195
|
+
sendStart,
|
|
1196
|
+
sendFinish,
|
|
1197
|
+
sendReasoning,
|
|
1198
|
+
sendSources
|
|
1199
|
+
})) {
|
|
765
1200
|
writer.write(part);
|
|
766
1201
|
}
|
|
767
1202
|
}
|
|
@@ -772,7 +1207,193 @@ function chatRoute({
|
|
|
772
1207
|
}
|
|
773
1208
|
});
|
|
774
1209
|
}
|
|
1210
|
+
function workflowRoute({
|
|
1211
|
+
path = "/api/workflows/:workflowId/stream",
|
|
1212
|
+
workflow
|
|
1213
|
+
}) {
|
|
1214
|
+
if (!workflow && !path.includes("/:workflowId")) {
|
|
1215
|
+
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
1216
|
+
}
|
|
1217
|
+
return registerApiRoute(path, {
|
|
1218
|
+
method: "POST",
|
|
1219
|
+
openapi: {
|
|
1220
|
+
summary: "Stream a workflow in AI SDK format",
|
|
1221
|
+
description: "Starts a workflow run and streams events as AI SDK UIMessage chunks",
|
|
1222
|
+
tags: ["ai-sdk"],
|
|
1223
|
+
parameters: [
|
|
1224
|
+
{
|
|
1225
|
+
name: "workflowId",
|
|
1226
|
+
in: "path",
|
|
1227
|
+
required: true,
|
|
1228
|
+
description: "The ID of the workflow to stream",
|
|
1229
|
+
schema: { type: "string" }
|
|
1230
|
+
}
|
|
1231
|
+
],
|
|
1232
|
+
requestBody: {
|
|
1233
|
+
required: true,
|
|
1234
|
+
content: {
|
|
1235
|
+
"application/json": {
|
|
1236
|
+
schema: {
|
|
1237
|
+
type: "object",
|
|
1238
|
+
properties: {
|
|
1239
|
+
runId: { type: "string" },
|
|
1240
|
+
resourceId: { type: "string" },
|
|
1241
|
+
inputData: { type: "object", additionalProperties: true },
|
|
1242
|
+
resumeData: { type: "object", additionalProperties: true },
|
|
1243
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1244
|
+
tracingOptions: { type: "object", additionalProperties: true },
|
|
1245
|
+
step: { type: "string" }
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
},
|
|
1251
|
+
responses: {
|
|
1252
|
+
"200": {
|
|
1253
|
+
description: "Workflow UIMessage event stream",
|
|
1254
|
+
content: {
|
|
1255
|
+
"text/plain": {
|
|
1256
|
+
schema: { type: "string", description: "SSE stream" }
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
},
|
|
1262
|
+
handler: async (c) => {
|
|
1263
|
+
const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
|
|
1264
|
+
const mastra = c.get("mastra");
|
|
1265
|
+
let workflowToUse = workflow;
|
|
1266
|
+
if (!workflow) {
|
|
1267
|
+
const workflowId = c.req.param("workflowId");
|
|
1268
|
+
workflowToUse = workflowId;
|
|
1269
|
+
}
|
|
1270
|
+
if (c.req.param("workflowId") && workflow) {
|
|
1271
|
+
mastra.getLogger()?.warn(
|
|
1272
|
+
`Fixed workflow ID was set together with a workflowId path parameter. This can lead to unexpected behavior.`
|
|
1273
|
+
);
|
|
1274
|
+
}
|
|
1275
|
+
if (!workflowToUse) {
|
|
1276
|
+
throw new Error("Workflow ID is required");
|
|
1277
|
+
}
|
|
1278
|
+
const workflowObj = mastra.getWorkflow(workflowToUse);
|
|
1279
|
+
if (!workflowObj) {
|
|
1280
|
+
throw new Error(`Workflow ${workflowToUse} not found`);
|
|
1281
|
+
}
|
|
1282
|
+
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1283
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
|
|
1284
|
+
const uiMessageStream = createUIMessageStream({
|
|
1285
|
+
execute: async ({ writer }) => {
|
|
1286
|
+
for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
|
|
1287
|
+
writer.write(part);
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
});
|
|
1291
|
+
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1292
|
+
}
|
|
1293
|
+
});
|
|
1294
|
+
}
|
|
1295
|
+
function networkRoute({
|
|
1296
|
+
path = "/network/:agentId",
|
|
1297
|
+
agent,
|
|
1298
|
+
defaultOptions
|
|
1299
|
+
}) {
|
|
1300
|
+
if (!agent && !path.includes("/:agentId")) {
|
|
1301
|
+
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
1302
|
+
}
|
|
1303
|
+
return registerApiRoute(path, {
|
|
1304
|
+
method: "POST",
|
|
1305
|
+
openapi: {
|
|
1306
|
+
summary: "Execute an agent network and stream AI SDK events",
|
|
1307
|
+
description: "Routes a request to an agent network and streams UIMessage chunks in AI SDK format",
|
|
1308
|
+
tags: ["ai-sdk"],
|
|
1309
|
+
parameters: [
|
|
1310
|
+
{
|
|
1311
|
+
name: "agentId",
|
|
1312
|
+
in: "path",
|
|
1313
|
+
required: true,
|
|
1314
|
+
description: "The ID of the routing agent to execute as a network",
|
|
1315
|
+
schema: { type: "string" }
|
|
1316
|
+
}
|
|
1317
|
+
],
|
|
1318
|
+
requestBody: {
|
|
1319
|
+
required: true,
|
|
1320
|
+
content: {
|
|
1321
|
+
"application/json": {
|
|
1322
|
+
schema: {
|
|
1323
|
+
type: "object",
|
|
1324
|
+
properties: {
|
|
1325
|
+
messages: { type: "array", items: { type: "object" } },
|
|
1326
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1327
|
+
runId: { type: "string" },
|
|
1328
|
+
maxSteps: { type: "number" },
|
|
1329
|
+
threadId: { type: "string" },
|
|
1330
|
+
resourceId: { type: "string" },
|
|
1331
|
+
modelSettings: { type: "object", additionalProperties: true },
|
|
1332
|
+
tools: { type: "array", items: { type: "object" } }
|
|
1333
|
+
},
|
|
1334
|
+
required: ["messages"]
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
},
|
|
1339
|
+
responses: {
|
|
1340
|
+
"200": {
|
|
1341
|
+
description: "Streaming AI SDK UIMessage event stream for the agent network",
|
|
1342
|
+
content: { "text/plain": { schema: { type: "string", description: "SSE stream" } } }
|
|
1343
|
+
},
|
|
1344
|
+
"404": {
|
|
1345
|
+
description: "Agent not found",
|
|
1346
|
+
content: {
|
|
1347
|
+
"application/json": {
|
|
1348
|
+
schema: { type: "object", properties: { error: { type: "string" } } }
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
},
|
|
1354
|
+
handler: async (c) => {
|
|
1355
|
+
const { messages, ...rest } = await c.req.json();
|
|
1356
|
+
const mastra = c.get("mastra");
|
|
1357
|
+
let agentToUse = agent;
|
|
1358
|
+
if (!agent) {
|
|
1359
|
+
const agentId = c.req.param("agentId");
|
|
1360
|
+
agentToUse = agentId;
|
|
1361
|
+
}
|
|
1362
|
+
if (c.req.param("agentId") && agent) {
|
|
1363
|
+
mastra.getLogger()?.warn(
|
|
1364
|
+
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1365
|
+
);
|
|
1366
|
+
}
|
|
1367
|
+
if (!agentToUse) {
|
|
1368
|
+
throw new Error("Agent ID is required");
|
|
1369
|
+
}
|
|
1370
|
+
const agentObj = mastra.getAgent(agentToUse);
|
|
1371
|
+
if (!agentObj) {
|
|
1372
|
+
throw new Error(`Agent ${agentToUse} not found`);
|
|
1373
|
+
}
|
|
1374
|
+
const result = await agentObj.network(messages, {
|
|
1375
|
+
...defaultOptions,
|
|
1376
|
+
...rest
|
|
1377
|
+
});
|
|
1378
|
+
const uiMessageStream = createUIMessageStream({
|
|
1379
|
+
execute: async ({ writer }) => {
|
|
1380
|
+
for await (const part of toAISdkV5Stream(result, { from: "network" })) {
|
|
1381
|
+
writer.write(part);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
});
|
|
1385
|
+
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1386
|
+
}
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// src/to-ai-sdk-format.ts
|
|
1391
|
+
function toAISdkFormat() {
|
|
1392
|
+
throw new Error(
|
|
1393
|
+
'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.'
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
775
1396
|
|
|
776
|
-
export {
|
|
1397
|
+
export { chatRoute, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
|
|
777
1398
|
//# sourceMappingURL=index.js.map
|
|
778
1399
|
//# sourceMappingURL=index.js.map
|