@mastra/client-js 0.10.10-alpha.4 → 0.10.11-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +30 -0
- package/dist/index.cjs +246 -217
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +247 -218
- package/package.json +3 -3
- package/src/example.ts +45 -17
- package/src/resources/agent.ts +288 -252
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/client-js@0.10.
|
|
2
|
+
> @mastra/client-js@0.10.11-alpha.0 build /home/runner/work/mastra/mastra/client-sdks/client-js
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
[34mCLI[39m Cleaning output folder
|
|
10
10
|
[34mESM[39m Build start
|
|
11
11
|
[34mCJS[39m Build start
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
12
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m69.98 KB[39m
|
|
13
|
+
[32mCJS[39m ⚡️ Build success in 1857ms
|
|
14
|
+
[32mESM[39m [1mdist/index.js [22m[32m69.70 KB[39m
|
|
15
|
+
[32mESM[39m ⚡️ Build success in 1857ms
|
|
16
16
|
[34mDTS[39m Build start
|
|
17
|
-
[32mDTS[39m ⚡️ Build success in
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m40.
|
|
19
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[32m40.
|
|
17
|
+
[32mDTS[39m ⚡️ Build success in 15863ms
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m40.80 KB[39m
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m40.80 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @mastra/client-js
|
|
2
2
|
|
|
3
|
+
## 0.10.11-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9468be4: Fix error on agent generate while using antropic
|
|
8
|
+
- 44731a4: Client SDK - Expose processTextStream util for agent textstream with structured output
|
|
9
|
+
- Updated dependencies [b4a9811]
|
|
10
|
+
- @mastra/core@0.10.12-alpha.0
|
|
11
|
+
|
|
12
|
+
## 0.10.10
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- 18da791: Add abortSignal to client SDK options
|
|
17
|
+
- Updated dependencies [2873c7f]
|
|
18
|
+
- Updated dependencies [1c1c6a1]
|
|
19
|
+
- Updated dependencies [f8ce2cc]
|
|
20
|
+
- Updated dependencies [8c846b6]
|
|
21
|
+
- Updated dependencies [c7bbf1e]
|
|
22
|
+
- Updated dependencies [8722d53]
|
|
23
|
+
- Updated dependencies [565cc0c]
|
|
24
|
+
- Updated dependencies [b790fd1]
|
|
25
|
+
- Updated dependencies [132027f]
|
|
26
|
+
- Updated dependencies [0c85311]
|
|
27
|
+
- Updated dependencies [d7ed04d]
|
|
28
|
+
- Updated dependencies [cb16baf]
|
|
29
|
+
- Updated dependencies [f36e4f1]
|
|
30
|
+
- Updated dependencies [7f6e403]
|
|
31
|
+
- @mastra/core@0.10.11
|
|
32
|
+
|
|
3
33
|
## 0.10.10-alpha.4
|
|
4
34
|
|
|
5
35
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -382,7 +382,11 @@ var Agent = class extends BaseResource {
|
|
|
382
382
|
body: processedParams
|
|
383
383
|
});
|
|
384
384
|
if (response.finishReason === "tool-calls") {
|
|
385
|
-
|
|
385
|
+
const toolCalls = response.toolCalls;
|
|
386
|
+
if (!toolCalls || !Array.isArray(toolCalls)) {
|
|
387
|
+
return response;
|
|
388
|
+
}
|
|
389
|
+
for (const toolCall of toolCalls) {
|
|
386
390
|
const clientTool = params.clientTools?.[toolCall.toolName];
|
|
387
391
|
if (clientTool && clientTool.execute) {
|
|
388
392
|
const result = await clientTool.execute(
|
|
@@ -425,7 +429,8 @@ var Agent = class extends BaseResource {
|
|
|
425
429
|
onToolCall,
|
|
426
430
|
onFinish,
|
|
427
431
|
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
|
428
|
-
lastMessage
|
|
432
|
+
lastMessage,
|
|
433
|
+
streamProtocol
|
|
429
434
|
}) {
|
|
430
435
|
const replaceLastMessage = lastMessage?.role === "assistant";
|
|
431
436
|
let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
|
|
@@ -486,228 +491,213 @@ var Agent = class extends BaseResource {
|
|
|
486
491
|
replaceLastMessage
|
|
487
492
|
});
|
|
488
493
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
text: value
|
|
496
|
-
};
|
|
497
|
-
message.parts.push(currentTextPart);
|
|
498
|
-
} else {
|
|
499
|
-
currentTextPart.text += value;
|
|
494
|
+
if (streamProtocol === "text") {
|
|
495
|
+
await uiUtils.processTextStream({
|
|
496
|
+
stream,
|
|
497
|
+
onTextPart(value) {
|
|
498
|
+
message.content += value;
|
|
499
|
+
execUpdate();
|
|
500
500
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
if (
|
|
508
|
-
|
|
501
|
+
});
|
|
502
|
+
onFinish?.({ message, finishReason, usage });
|
|
503
|
+
} else {
|
|
504
|
+
await uiUtils.processDataStream({
|
|
505
|
+
stream,
|
|
506
|
+
onTextPart(value) {
|
|
507
|
+
if (currentTextPart == null) {
|
|
508
|
+
currentTextPart = {
|
|
509
|
+
type: "text",
|
|
510
|
+
text: value
|
|
511
|
+
};
|
|
512
|
+
message.parts.push(currentTextPart);
|
|
513
|
+
} else {
|
|
514
|
+
currentTextPart.text += value;
|
|
509
515
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
type: "
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
const invocation = {
|
|
573
|
-
state: "partial-call",
|
|
574
|
-
step,
|
|
575
|
-
toolCallId: value.toolCallId,
|
|
576
|
-
toolName: value.toolName,
|
|
577
|
-
args: void 0
|
|
578
|
-
};
|
|
579
|
-
message.toolInvocations.push(invocation);
|
|
580
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
581
|
-
execUpdate();
|
|
582
|
-
},
|
|
583
|
-
onToolCallDeltaPart(value) {
|
|
584
|
-
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
585
|
-
partialToolCall.text += value.argsTextDelta;
|
|
586
|
-
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
587
|
-
const invocation = {
|
|
588
|
-
state: "partial-call",
|
|
589
|
-
step: partialToolCall.step,
|
|
590
|
-
toolCallId: value.toolCallId,
|
|
591
|
-
toolName: partialToolCall.toolName,
|
|
592
|
-
args: partialArgs
|
|
593
|
-
};
|
|
594
|
-
message.toolInvocations[partialToolCall.index] = invocation;
|
|
595
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
596
|
-
execUpdate();
|
|
597
|
-
},
|
|
598
|
-
async onToolCallPart(value) {
|
|
599
|
-
const invocation = {
|
|
600
|
-
state: "call",
|
|
601
|
-
step,
|
|
602
|
-
...value
|
|
603
|
-
};
|
|
604
|
-
if (partialToolCalls[value.toolCallId] != null) {
|
|
605
|
-
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
606
|
-
} else {
|
|
516
|
+
message.content += value;
|
|
517
|
+
execUpdate();
|
|
518
|
+
},
|
|
519
|
+
onReasoningPart(value) {
|
|
520
|
+
if (currentReasoningTextDetail == null) {
|
|
521
|
+
currentReasoningTextDetail = { type: "text", text: value };
|
|
522
|
+
if (currentReasoningPart != null) {
|
|
523
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
currentReasoningTextDetail.text += value;
|
|
527
|
+
}
|
|
528
|
+
if (currentReasoningPart == null) {
|
|
529
|
+
currentReasoningPart = {
|
|
530
|
+
type: "reasoning",
|
|
531
|
+
reasoning: value,
|
|
532
|
+
details: [currentReasoningTextDetail]
|
|
533
|
+
};
|
|
534
|
+
message.parts.push(currentReasoningPart);
|
|
535
|
+
} else {
|
|
536
|
+
currentReasoningPart.reasoning += value;
|
|
537
|
+
}
|
|
538
|
+
message.reasoning = (message.reasoning ?? "") + value;
|
|
539
|
+
execUpdate();
|
|
540
|
+
},
|
|
541
|
+
onReasoningSignaturePart(value) {
|
|
542
|
+
if (currentReasoningTextDetail != null) {
|
|
543
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
onRedactedReasoningPart(value) {
|
|
547
|
+
if (currentReasoningPart == null) {
|
|
548
|
+
currentReasoningPart = {
|
|
549
|
+
type: "reasoning",
|
|
550
|
+
reasoning: "",
|
|
551
|
+
details: []
|
|
552
|
+
};
|
|
553
|
+
message.parts.push(currentReasoningPart);
|
|
554
|
+
}
|
|
555
|
+
currentReasoningPart.details.push({
|
|
556
|
+
type: "redacted",
|
|
557
|
+
data: value.data
|
|
558
|
+
});
|
|
559
|
+
currentReasoningTextDetail = void 0;
|
|
560
|
+
execUpdate();
|
|
561
|
+
},
|
|
562
|
+
onFilePart(value) {
|
|
563
|
+
message.parts.push({
|
|
564
|
+
type: "file",
|
|
565
|
+
mimeType: value.mimeType,
|
|
566
|
+
data: value.data
|
|
567
|
+
});
|
|
568
|
+
execUpdate();
|
|
569
|
+
},
|
|
570
|
+
onSourcePart(value) {
|
|
571
|
+
message.parts.push({
|
|
572
|
+
type: "source",
|
|
573
|
+
source: value
|
|
574
|
+
});
|
|
575
|
+
execUpdate();
|
|
576
|
+
},
|
|
577
|
+
onToolCallStreamingStartPart(value) {
|
|
607
578
|
if (message.toolInvocations == null) {
|
|
608
579
|
message.toolInvocations = [];
|
|
609
580
|
}
|
|
581
|
+
partialToolCalls[value.toolCallId] = {
|
|
582
|
+
text: "",
|
|
583
|
+
step,
|
|
584
|
+
toolName: value.toolName,
|
|
585
|
+
index: message.toolInvocations.length
|
|
586
|
+
};
|
|
587
|
+
const invocation = {
|
|
588
|
+
state: "partial-call",
|
|
589
|
+
step,
|
|
590
|
+
toolCallId: value.toolCallId,
|
|
591
|
+
toolName: value.toolName,
|
|
592
|
+
args: void 0
|
|
593
|
+
};
|
|
610
594
|
message.toolInvocations.push(invocation);
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
595
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
596
|
+
execUpdate();
|
|
597
|
+
},
|
|
598
|
+
onToolCallDeltaPart(value) {
|
|
599
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
600
|
+
partialToolCall.text += value.argsTextDelta;
|
|
601
|
+
const { value: partialArgs } = uiUtils.parsePartialJson(partialToolCall.text);
|
|
602
|
+
const invocation = {
|
|
603
|
+
state: "partial-call",
|
|
604
|
+
step: partialToolCall.step,
|
|
605
|
+
toolCallId: value.toolCallId,
|
|
606
|
+
toolName: partialToolCall.toolName,
|
|
607
|
+
args: partialArgs
|
|
608
|
+
};
|
|
609
|
+
message.toolInvocations[partialToolCall.index] = invocation;
|
|
610
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
611
|
+
execUpdate();
|
|
612
|
+
},
|
|
613
|
+
async onToolCallPart(value) {
|
|
614
|
+
const invocation = {
|
|
615
|
+
state: "call",
|
|
616
|
+
step,
|
|
617
|
+
...value
|
|
618
|
+
};
|
|
619
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
620
|
+
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
|
621
|
+
} else {
|
|
622
|
+
if (message.toolInvocations == null) {
|
|
623
|
+
message.toolInvocations = [];
|
|
624
|
+
}
|
|
625
|
+
message.toolInvocations.push(invocation);
|
|
626
626
|
}
|
|
627
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
628
|
+
execUpdate();
|
|
629
|
+
if (onToolCall) {
|
|
630
|
+
const result = await onToolCall({ toolCall: value });
|
|
631
|
+
if (result != null) {
|
|
632
|
+
const invocation2 = {
|
|
633
|
+
state: "result",
|
|
634
|
+
step,
|
|
635
|
+
...value,
|
|
636
|
+
result
|
|
637
|
+
};
|
|
638
|
+
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
|
639
|
+
updateToolInvocationPart(value.toolCallId, invocation2);
|
|
640
|
+
execUpdate();
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
onToolResultPart(value) {
|
|
645
|
+
const toolInvocations = message.toolInvocations;
|
|
646
|
+
if (toolInvocations == null) {
|
|
647
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
|
648
|
+
}
|
|
649
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
|
650
|
+
(invocation2) => invocation2.toolCallId === value.toolCallId
|
|
651
|
+
);
|
|
652
|
+
if (toolInvocationIndex === -1) {
|
|
653
|
+
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
654
|
+
}
|
|
655
|
+
const invocation = {
|
|
656
|
+
...toolInvocations[toolInvocationIndex],
|
|
657
|
+
state: "result",
|
|
658
|
+
...value
|
|
659
|
+
};
|
|
660
|
+
toolInvocations[toolInvocationIndex] = invocation;
|
|
661
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
662
|
+
execUpdate();
|
|
663
|
+
},
|
|
664
|
+
onDataPart(value) {
|
|
665
|
+
data.push(...value);
|
|
666
|
+
execUpdate();
|
|
667
|
+
},
|
|
668
|
+
onMessageAnnotationsPart(value) {
|
|
669
|
+
if (messageAnnotations == null) {
|
|
670
|
+
messageAnnotations = [...value];
|
|
671
|
+
} else {
|
|
672
|
+
messageAnnotations.push(...value);
|
|
673
|
+
}
|
|
674
|
+
execUpdate();
|
|
675
|
+
},
|
|
676
|
+
onFinishStepPart(value) {
|
|
677
|
+
step += 1;
|
|
678
|
+
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
679
|
+
currentReasoningPart = void 0;
|
|
680
|
+
currentReasoningTextDetail = void 0;
|
|
681
|
+
},
|
|
682
|
+
onStartStepPart(value) {
|
|
683
|
+
if (!replaceLastMessage) {
|
|
684
|
+
message.id = value.messageId;
|
|
685
|
+
}
|
|
686
|
+
message.parts.push({ type: "step-start" });
|
|
687
|
+
execUpdate();
|
|
688
|
+
},
|
|
689
|
+
onFinishMessagePart(value) {
|
|
690
|
+
finishReason = value.finishReason;
|
|
691
|
+
if (value.usage != null) {
|
|
692
|
+
usage = value.usage;
|
|
693
|
+
}
|
|
694
|
+
},
|
|
695
|
+
onErrorPart(error) {
|
|
696
|
+
throw new Error(error);
|
|
627
697
|
}
|
|
628
|
-
},
|
|
629
|
-
onToolResultPart(value) {
|
|
630
|
-
const toolInvocations = message.toolInvocations;
|
|
631
|
-
if (toolInvocations == null) {
|
|
632
|
-
throw new Error("tool_result must be preceded by a tool_call");
|
|
633
|
-
}
|
|
634
|
-
const toolInvocationIndex = toolInvocations.findIndex((invocation2) => invocation2.toolCallId === value.toolCallId);
|
|
635
|
-
if (toolInvocationIndex === -1) {
|
|
636
|
-
throw new Error("tool_result must be preceded by a tool_call with the same toolCallId");
|
|
637
|
-
}
|
|
638
|
-
const invocation = {
|
|
639
|
-
...toolInvocations[toolInvocationIndex],
|
|
640
|
-
state: "result",
|
|
641
|
-
...value
|
|
642
|
-
};
|
|
643
|
-
toolInvocations[toolInvocationIndex] = invocation;
|
|
644
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
|
645
|
-
execUpdate();
|
|
646
|
-
},
|
|
647
|
-
onDataPart(value) {
|
|
648
|
-
data.push(...value);
|
|
649
|
-
execUpdate();
|
|
650
|
-
},
|
|
651
|
-
onMessageAnnotationsPart(value) {
|
|
652
|
-
if (messageAnnotations == null) {
|
|
653
|
-
messageAnnotations = [...value];
|
|
654
|
-
} else {
|
|
655
|
-
messageAnnotations.push(...value);
|
|
656
|
-
}
|
|
657
|
-
execUpdate();
|
|
658
|
-
},
|
|
659
|
-
onFinishStepPart(value) {
|
|
660
|
-
step += 1;
|
|
661
|
-
currentTextPart = value.isContinued ? currentTextPart : void 0;
|
|
662
|
-
currentReasoningPart = void 0;
|
|
663
|
-
currentReasoningTextDetail = void 0;
|
|
664
|
-
},
|
|
665
|
-
onStartStepPart(value) {
|
|
666
|
-
if (!replaceLastMessage) {
|
|
667
|
-
message.id = value.messageId;
|
|
668
|
-
}
|
|
669
|
-
message.parts.push({ type: "step-start" });
|
|
670
|
-
execUpdate();
|
|
671
|
-
},
|
|
672
|
-
onFinishMessagePart(value) {
|
|
673
|
-
finishReason = value.finishReason;
|
|
674
|
-
if (value.usage != null) {
|
|
675
|
-
usage = value.usage;
|
|
676
|
-
}
|
|
677
|
-
},
|
|
678
|
-
onErrorPart(error) {
|
|
679
|
-
throw new Error(error);
|
|
680
|
-
}
|
|
681
|
-
});
|
|
682
|
-
onFinish?.({ message, finishReason, usage });
|
|
683
|
-
}
|
|
684
|
-
/**
|
|
685
|
-
* Streams a response from the agent
|
|
686
|
-
* @param params - Stream parameters including prompt
|
|
687
|
-
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
688
|
-
*/
|
|
689
|
-
async stream(params) {
|
|
690
|
-
const processedParams = {
|
|
691
|
-
...params,
|
|
692
|
-
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
693
|
-
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
694
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
695
|
-
clientTools: processClientTools(params.clientTools)
|
|
696
|
-
};
|
|
697
|
-
const { readable, writable } = new TransformStream();
|
|
698
|
-
const response = await this.processStreamResponse(processedParams, writable);
|
|
699
|
-
const streamResponse = new Response(readable, {
|
|
700
|
-
status: response.status,
|
|
701
|
-
statusText: response.statusText,
|
|
702
|
-
headers: response.headers
|
|
703
|
-
});
|
|
704
|
-
streamResponse.processDataStream = async (options = {}) => {
|
|
705
|
-
await uiUtils.processDataStream({
|
|
706
|
-
stream: streamResponse.body,
|
|
707
|
-
...options
|
|
708
698
|
});
|
|
709
|
-
|
|
710
|
-
|
|
699
|
+
onFinish?.({ message, finishReason, usage });
|
|
700
|
+
}
|
|
711
701
|
}
|
|
712
702
|
/**
|
|
713
703
|
* Processes the stream response and handles tool calls
|
|
@@ -722,6 +712,7 @@ var Agent = class extends BaseResource {
|
|
|
722
712
|
throw new Error("No response body");
|
|
723
713
|
}
|
|
724
714
|
try {
|
|
715
|
+
const streamProtocol = processedParams.output ? "text" : "data";
|
|
725
716
|
let toolCalls = [];
|
|
726
717
|
let messages = [];
|
|
727
718
|
const [streamForWritable, streamForProcessing] = response.body.tee();
|
|
@@ -801,17 +792,55 @@ var Agent = class extends BaseResource {
|
|
|
801
792
|
}
|
|
802
793
|
} else {
|
|
803
794
|
setTimeout(() => {
|
|
804
|
-
writable.
|
|
795
|
+
if (!writable.locked) {
|
|
796
|
+
writable.close();
|
|
797
|
+
}
|
|
805
798
|
}, 0);
|
|
806
799
|
}
|
|
807
800
|
},
|
|
808
|
-
lastMessage: void 0
|
|
801
|
+
lastMessage: void 0,
|
|
802
|
+
streamProtocol
|
|
809
803
|
});
|
|
810
804
|
} catch (error) {
|
|
811
805
|
console.error("Error processing stream response:", error);
|
|
812
806
|
}
|
|
813
807
|
return response;
|
|
814
808
|
}
|
|
809
|
+
/**
|
|
810
|
+
* Streams a response from the agent
|
|
811
|
+
* @param params - Stream parameters including prompt
|
|
812
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
813
|
+
*/
|
|
814
|
+
async stream(params) {
|
|
815
|
+
const processedParams = {
|
|
816
|
+
...params,
|
|
817
|
+
output: params.output ? zodToJsonSchema(params.output) : void 0,
|
|
818
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : void 0,
|
|
819
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
820
|
+
clientTools: processClientTools(params.clientTools)
|
|
821
|
+
};
|
|
822
|
+
const { readable, writable } = new TransformStream();
|
|
823
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
824
|
+
const streamResponse = new Response(readable, {
|
|
825
|
+
status: response.status,
|
|
826
|
+
statusText: response.statusText,
|
|
827
|
+
headers: response.headers
|
|
828
|
+
});
|
|
829
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
830
|
+
await uiUtils.processDataStream({
|
|
831
|
+
stream: streamResponse.body,
|
|
832
|
+
...options
|
|
833
|
+
});
|
|
834
|
+
};
|
|
835
|
+
streamResponse.processTextStream = async (options) => {
|
|
836
|
+
await uiUtils.processTextStream({
|
|
837
|
+
stream: streamResponse.body,
|
|
838
|
+
onTextPart: options?.onTextPart ?? (() => {
|
|
839
|
+
})
|
|
840
|
+
});
|
|
841
|
+
};
|
|
842
|
+
return streamResponse;
|
|
843
|
+
}
|
|
815
844
|
/**
|
|
816
845
|
* Gets details about a specific tool available to the agent
|
|
817
846
|
* @param toolId - ID of the tool to retrieve
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AbstractAgent } from '@ag-ui/client';
|
|
2
2
|
import { ServerInfo, MCPToolType, ServerDetailInfo } from '@mastra/core/mcp';
|
|
3
|
-
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
3
|
+
import { processDataStream, processTextStream } from '@ai-sdk/ui-utils';
|
|
4
4
|
import { CoreMessage, AiMessageType, StorageThreadType, MastraMessageV1, LegacyWorkflowRuns, WorkflowRuns, WorkflowRun, QueryResult, GenerateReturn } from '@mastra/core';
|
|
5
5
|
import { JSONSchema7 } from 'json-schema';
|
|
6
6
|
import { ZodSchema } from 'zod';
|
|
@@ -440,18 +440,19 @@ declare class Agent extends BaseResource {
|
|
|
440
440
|
experimental_output: T;
|
|
441
441
|
}): Promise<GenerateReturn<T>>;
|
|
442
442
|
private processChatResponse;
|
|
443
|
+
/**
|
|
444
|
+
* Processes the stream response and handles tool calls
|
|
445
|
+
*/
|
|
446
|
+
private processStreamResponse;
|
|
443
447
|
/**
|
|
444
448
|
* Streams a response from the agent
|
|
445
449
|
* @param params - Stream parameters including prompt
|
|
446
|
-
* @returns Promise containing the enhanced Response object with processDataStream
|
|
450
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
447
451
|
*/
|
|
448
452
|
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
|
|
449
453
|
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
454
|
+
processTextStream: (options?: Omit<Parameters<typeof processTextStream>[0], 'stream'>) => Promise<void>;
|
|
450
455
|
}>;
|
|
451
|
-
/**
|
|
452
|
-
* Processes the stream response and handles tool calls
|
|
453
|
-
*/
|
|
454
|
-
private processStreamResponse;
|
|
455
456
|
/**
|
|
456
457
|
* Gets details about a specific tool available to the agent
|
|
457
458
|
* @param toolId - ID of the tool to retrieve
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AbstractAgent } from '@ag-ui/client';
|
|
2
2
|
import { ServerInfo, MCPToolType, ServerDetailInfo } from '@mastra/core/mcp';
|
|
3
|
-
import { processDataStream } from '@ai-sdk/ui-utils';
|
|
3
|
+
import { processDataStream, processTextStream } from '@ai-sdk/ui-utils';
|
|
4
4
|
import { CoreMessage, AiMessageType, StorageThreadType, MastraMessageV1, LegacyWorkflowRuns, WorkflowRuns, WorkflowRun, QueryResult, GenerateReturn } from '@mastra/core';
|
|
5
5
|
import { JSONSchema7 } from 'json-schema';
|
|
6
6
|
import { ZodSchema } from 'zod';
|
|
@@ -440,18 +440,19 @@ declare class Agent extends BaseResource {
|
|
|
440
440
|
experimental_output: T;
|
|
441
441
|
}): Promise<GenerateReturn<T>>;
|
|
442
442
|
private processChatResponse;
|
|
443
|
+
/**
|
|
444
|
+
* Processes the stream response and handles tool calls
|
|
445
|
+
*/
|
|
446
|
+
private processStreamResponse;
|
|
443
447
|
/**
|
|
444
448
|
* Streams a response from the agent
|
|
445
449
|
* @param params - Stream parameters including prompt
|
|
446
|
-
* @returns Promise containing the enhanced Response object with processDataStream
|
|
450
|
+
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
447
451
|
*/
|
|
448
452
|
stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response & {
|
|
449
453
|
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
454
|
+
processTextStream: (options?: Omit<Parameters<typeof processTextStream>[0], 'stream'>) => Promise<void>;
|
|
450
455
|
}>;
|
|
451
|
-
/**
|
|
452
|
-
* Processes the stream response and handles tool calls
|
|
453
|
-
*/
|
|
454
|
-
private processStreamResponse;
|
|
455
456
|
/**
|
|
456
457
|
* Gets details about a specific tool available to the agent
|
|
457
458
|
* @param toolId - ID of the tool to retrieve
|