@mastra/ai-sdk 1.6.2 → 1.6.3-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/CHANGELOG.md +9 -0
- package/dist/chat-route.d.ts +8 -16
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/index.cjs +110 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +110 -28
- package/dist/index.js.map +1 -1
- package/dist/transformers.d.ts.map +1 -1
- package/package.json +6 -4
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { TripWire, MessageList, aiV5ModelMessageToV2PromptMessage } from '@mastr
|
|
|
10
10
|
import { RequestContext } from '@mastra/core/di';
|
|
11
11
|
import { WorkingMemory, MessageHistory, SemanticRecall } from '@mastra/core/processors';
|
|
12
12
|
|
|
13
|
-
// ../../packages/_vendored/ai_v5/dist/chunk-
|
|
13
|
+
// ../../packages/_vendored/ai_v5/dist/chunk-G6L5YOWM.js
|
|
14
14
|
var marker = "vercel.ai.error";
|
|
15
15
|
var symbol = Symbol.for(marker);
|
|
16
16
|
var _a;
|
|
@@ -11589,28 +11589,95 @@ function toAISdkStream(stream, options = {
|
|
|
11589
11589
|
}
|
|
11590
11590
|
|
|
11591
11591
|
// src/chat-route.ts
|
|
11592
|
-
function
|
|
11593
|
-
const
|
|
11594
|
-
|
|
11595
|
-
const
|
|
11596
|
-
|
|
11597
|
-
const part
|
|
11598
|
-
|
|
11599
|
-
|
|
11600
|
-
|
|
11601
|
-
|
|
11602
|
-
|
|
11603
|
-
|
|
11604
|
-
|
|
11605
|
-
|
|
11606
|
-
|
|
11607
|
-
|
|
11608
|
-
|
|
11609
|
-
|
|
11610
|
-
|
|
11611
|
-
|
|
11592
|
+
function extractV6NativeApprovals(messages) {
|
|
11593
|
+
const byTarget = /* @__PURE__ */ new Map();
|
|
11594
|
+
const separator = APPROVAL_ID_SEPARATOR;
|
|
11595
|
+
for (const message of messages) {
|
|
11596
|
+
if (message.role !== "assistant") continue;
|
|
11597
|
+
for (const part of message.parts ?? []) {
|
|
11598
|
+
if (!isToolUIPart2(part) || part.state !== "approval-responded") continue;
|
|
11599
|
+
const lastSep = part.approval.id.lastIndexOf(separator);
|
|
11600
|
+
if (lastSep === -1) continue;
|
|
11601
|
+
const runId = part.approval.id.slice(0, lastSep);
|
|
11602
|
+
const toolCallId = part.approval.id.slice(lastSep + separator.length);
|
|
11603
|
+
if (!runId || !toolCallId) continue;
|
|
11604
|
+
if (toolCallId !== part.toolCallId) continue;
|
|
11605
|
+
byTarget.set(`${runId}${separator}${toolCallId}`, {
|
|
11606
|
+
resumeData: {
|
|
11607
|
+
approved: part.approval.approved,
|
|
11608
|
+
...part.approval.reason != null ? { reason: part.approval.reason } : {}
|
|
11609
|
+
},
|
|
11610
|
+
runId,
|
|
11611
|
+
toolCallId
|
|
11612
|
+
});
|
|
11613
|
+
}
|
|
11612
11614
|
}
|
|
11613
|
-
return
|
|
11615
|
+
return [...byTarget.values()];
|
|
11616
|
+
}
|
|
11617
|
+
function streamV6ApprovalResumes(args) {
|
|
11618
|
+
const { agent, approvals, baseOptions, structuredOutput, messages, lastMessageId } = args;
|
|
11619
|
+
const { sendStart, sendFinish, sendReasoning, sendSources, onError, messageMetadata } = args;
|
|
11620
|
+
return createUIMessageStream2({
|
|
11621
|
+
originalMessages: messages,
|
|
11622
|
+
onError,
|
|
11623
|
+
execute: async ({ writer }) => {
|
|
11624
|
+
let startWritten = false;
|
|
11625
|
+
let successfulLegs = 0;
|
|
11626
|
+
let firstResolvedTargetError;
|
|
11627
|
+
let finalFinish;
|
|
11628
|
+
for (const approval of approvals) {
|
|
11629
|
+
try {
|
|
11630
|
+
const result = await agent.resumeStream(approval.resumeData, {
|
|
11631
|
+
...baseOptions,
|
|
11632
|
+
runId: approval.runId,
|
|
11633
|
+
toolCallId: approval.toolCallId,
|
|
11634
|
+
...structuredOutput ? { structuredOutput } : {}
|
|
11635
|
+
});
|
|
11636
|
+
for await (const part of toAISdkStream(result, {
|
|
11637
|
+
from: "agent",
|
|
11638
|
+
version: "v6",
|
|
11639
|
+
lastMessageId,
|
|
11640
|
+
sendStart,
|
|
11641
|
+
sendFinish,
|
|
11642
|
+
sendReasoning,
|
|
11643
|
+
sendSources,
|
|
11644
|
+
onError,
|
|
11645
|
+
messageMetadata
|
|
11646
|
+
})) {
|
|
11647
|
+
if (part.type === "start") {
|
|
11648
|
+
if (startWritten) continue;
|
|
11649
|
+
startWritten = true;
|
|
11650
|
+
writer.write(part);
|
|
11651
|
+
continue;
|
|
11652
|
+
}
|
|
11653
|
+
if (!startWritten && sendStart) {
|
|
11654
|
+
writer.write({ type: "start", ...lastMessageId ? { messageId: lastMessageId } : {} });
|
|
11655
|
+
startWritten = true;
|
|
11656
|
+
}
|
|
11657
|
+
if (part.type === "finish") {
|
|
11658
|
+
finalFinish = part;
|
|
11659
|
+
continue;
|
|
11660
|
+
}
|
|
11661
|
+
writer.write(part);
|
|
11662
|
+
}
|
|
11663
|
+
successfulLegs++;
|
|
11664
|
+
} catch (error) {
|
|
11665
|
+
const id = error?.id;
|
|
11666
|
+
if (id !== "AGENT_RESUME_TOOL_CALL_NOT_SUSPENDED" && id !== "AGENT_RESUME_NO_SNAPSHOT_FOUND") {
|
|
11667
|
+
throw error;
|
|
11668
|
+
}
|
|
11669
|
+
firstResolvedTargetError ??= error;
|
|
11670
|
+
}
|
|
11671
|
+
}
|
|
11672
|
+
if (successfulLegs === 0 && firstResolvedTargetError) throw firstResolvedTargetError;
|
|
11673
|
+
if (!startWritten && sendStart) {
|
|
11674
|
+
writer.write({ type: "start", ...lastMessageId ? { messageId: lastMessageId } : {} });
|
|
11675
|
+
}
|
|
11676
|
+
if (sendFinish) {
|
|
11677
|
+
writer.write(finalFinish ?? { type: "finish" });
|
|
11678
|
+
}
|
|
11679
|
+
}
|
|
11680
|
+
});
|
|
11614
11681
|
}
|
|
11615
11682
|
async function handleChatStream({
|
|
11616
11683
|
mastra,
|
|
@@ -11648,9 +11715,6 @@ async function handleChatStream({
|
|
|
11648
11715
|
if (!Array.isArray(messages)) {
|
|
11649
11716
|
throw new Error("Messages must be an array of UIMessage objects");
|
|
11650
11717
|
}
|
|
11651
|
-
const nativeApproval = version === "v6" && !resumeData ? extractV6NativeApproval(messages) : null;
|
|
11652
|
-
const effectiveResumeData = nativeApproval?.resumeData ?? resumeData;
|
|
11653
|
-
const effectiveRunId = nativeApproval?.runId ?? runId;
|
|
11654
11718
|
let lastMessageId;
|
|
11655
11719
|
let messagesToSend = messages;
|
|
11656
11720
|
if (messages.length > 0) {
|
|
@@ -11672,12 +11736,30 @@ async function handleChatStream({
|
|
|
11672
11736
|
const baseOptions = {
|
|
11673
11737
|
...defaultOptionsRest,
|
|
11674
11738
|
...restOptions,
|
|
11675
|
-
...
|
|
11676
|
-
...nativeApproval?.toolCallId && { toolCallId: nativeApproval.toolCallId },
|
|
11739
|
+
...runId && { runId },
|
|
11677
11740
|
requestContext: requestContext || defaultOptions3?.requestContext,
|
|
11678
11741
|
...Object.keys(mergedProviderOptions).length > 0 && { providerOptions: mergedProviderOptions }
|
|
11679
11742
|
};
|
|
11680
|
-
|
|
11743
|
+
if (version === "v6" && !resumeData && trigger !== "regenerate-message" && messages.at(-1)?.role === "assistant") {
|
|
11744
|
+
const approvals = extractV6NativeApprovals(messages);
|
|
11745
|
+
if (approvals.length > 0) {
|
|
11746
|
+
return streamV6ApprovalResumes({
|
|
11747
|
+
agent: agentObj,
|
|
11748
|
+
approvals,
|
|
11749
|
+
baseOptions,
|
|
11750
|
+
structuredOutput,
|
|
11751
|
+
messages,
|
|
11752
|
+
lastMessageId,
|
|
11753
|
+
sendStart,
|
|
11754
|
+
sendFinish,
|
|
11755
|
+
sendReasoning,
|
|
11756
|
+
sendSources,
|
|
11757
|
+
onError,
|
|
11758
|
+
messageMetadata
|
|
11759
|
+
});
|
|
11760
|
+
}
|
|
11761
|
+
}
|
|
11762
|
+
const result = resumeData ? structuredOutput ? await agentObj.resumeStream(resumeData, { ...baseOptions, structuredOutput }) : await agentObj.resumeStream(resumeData, baseOptions) : structuredOutput ? await agentObj.stream(messagesToSend, { ...baseOptions, structuredOutput }) : await agentObj.stream(messagesToSend, baseOptions);
|
|
11681
11763
|
if (version === "v6") {
|
|
11682
11764
|
return createUIMessageStream2({
|
|
11683
11765
|
originalMessages: messages,
|