@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mastra/ai-sdk
|
|
2
2
|
|
|
3
|
+
## 1.6.3-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fixed the AI SDK v6 native approval flow in handleChatStream so approval responses are collected across all assistant messages instead of only the final message. Approving a tool call now resumes its exact run and tool call, multiple approval responses in one request resume sequentially in a single framed response stream, and already-resolved responses from history are safely skipped instead of executing again. ([#19528](https://github.com/mastra-ai/mastra/pull/19528))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`1426af2`](https://github.com/mastra-ai/mastra/commit/1426af24975879c000d13ac75673f630fcc970c1), [`975295d`](https://github.com/mastra-ai/mastra/commit/975295d418552f0d46a59edfef4c3ee555f9930a), [`85e4fb5`](https://github.com/mastra-ai/mastra/commit/85e4fb50087a81c74df3a762f53b56373db0b912), [`ef03c0c`](https://github.com/mastra-ai/mastra/commit/ef03c0cfc62367a458e4cc56462e2148b35681c5), [`4fb4d88`](https://github.com/mastra-ai/mastra/commit/4fb4d881bc107acee13890ad4d78661016c510ed), [`4eba27a`](https://github.com/mastra-ai/mastra/commit/4eba27adcf60f991df0e62f94b3e75b4e67f3b4b), [`c701be3`](https://github.com/mastra-ai/mastra/commit/c701be32d7d9aa94a66da8c6cc38dcac6856f464)]:
|
|
10
|
+
- @mastra/core@1.52.0-alpha.3
|
|
11
|
+
|
|
3
12
|
## 1.6.2
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/dist/chat-route.d.ts
CHANGED
|
@@ -4,25 +4,17 @@ import type { AgentExecutionOptions } from '@mastra/core/agent';
|
|
|
4
4
|
import type { Mastra } from '@mastra/core/mastra';
|
|
5
5
|
import { registerApiRoute } from '@mastra/core/server';
|
|
6
6
|
import type { SupportedUIMessage, V5UIMessage, V5UIMessageStream, V6UIMessage, V6UIMessageStream } from './public-types.js';
|
|
7
|
-
|
|
8
|
-
* Scans a v6 UIMessage array for the most recent 'approval-responded' tool
|
|
9
|
-
* part in the last trailing assistant message only. When found, splits the
|
|
10
|
-
* composite approvalId ("${runId}::${toolCallId}") to recover the runId and
|
|
11
|
-
* toolCallId needed for a targeted resumeStream.
|
|
12
|
-
*
|
|
13
|
-
* Only the last trailing assistant message is inspected so that approval
|
|
14
|
-
* responses from earlier turns are never re-processed. Within that message,
|
|
15
|
-
* parts are scanned in reverse so the decision the user just acted on wins
|
|
16
|
-
* over any earlier 'approval-responded' parts that have not yet transitioned
|
|
17
|
-
* to 'output-available'.
|
|
18
|
-
*
|
|
19
|
-
* Returns null when no approval response is present (normal chat turn).
|
|
20
|
-
*/
|
|
21
|
-
export declare function extractV6NativeApproval(messages: V6UIMessage[]): {
|
|
7
|
+
export interface V6NativeApprovalResponse {
|
|
22
8
|
resumeData: Record<string, unknown>;
|
|
23
9
|
runId: string;
|
|
24
10
|
toolCallId: string;
|
|
25
|
-
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Collects every approval response across all assistant messages in a v6
|
|
14
|
+
* request. Responses are deduplicated by their composite run/tool-call target,
|
|
15
|
+
* with the most recent part winning when history contains repeated copies.
|
|
16
|
+
*/
|
|
17
|
+
export declare function extractV6NativeApprovals(messages: V6UIMessage[]): V6NativeApprovalResponse[];
|
|
26
18
|
export type ChatStreamHandlerParams<UI_MESSAGE extends SupportedUIMessage = SupportedUIMessage, OUTPUT = undefined> = AgentExecutionOptions<OUTPUT> & {
|
|
27
19
|
messages: UI_MESSAGE[];
|
|
28
20
|
resumeData?: Record<string, any>;
|
package/dist/chat-route.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,IAAI,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAM9F,OAAO,KAAK,EAAE,sBAAsB,IAAI,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,KAAK,EAAE,qBAAqB,EAA6B,MAAM,oBAAoB,CAAC;AAC3F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGvD,OAAO,KAAK,EACV,kBAAkB,EAClB,WAAW,EACX,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AAExB
|
|
1
|
+
{"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,IAAI,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAM9F,OAAO,KAAK,EAAE,sBAAsB,IAAI,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,KAAK,EAAE,qBAAqB,EAA6B,MAAM,oBAAoB,CAAC;AAC3F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGvD,OAAO,KAAK,EACV,kBAAkB,EAClB,WAAW,EACX,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AAExB,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpC,KAAK,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D,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC/C,OAAO,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACtB,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,GAAG;IACA,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;CACtC,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,wBAAgB,SAAS,CAAC,MAAM,GAAG,SAAS,EAAE,EAC5C,IAAuB,EACvB,KAAK,EACL,cAAc,EACd,OAAc,EACd,YAAY,EACZ,SAAgB,EAChB,UAAiB,EACjB,aAAqB,EACrB,WAAmB,EACnB,OAAO,GACR,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAmNhE"}
|
package/dist/index.cjs
CHANGED
|
@@ -31,7 +31,7 @@ function _interopNamespace(e) {
|
|
|
31
31
|
|
|
32
32
|
var z4__namespace = /*#__PURE__*/_interopNamespace(z4);
|
|
33
33
|
|
|
34
|
-
// ../../packages/_vendored/ai_v5/dist/chunk-
|
|
34
|
+
// ../../packages/_vendored/ai_v5/dist/chunk-G6L5YOWM.js
|
|
35
35
|
var marker = "vercel.ai.error";
|
|
36
36
|
var symbol = Symbol.for(marker);
|
|
37
37
|
var _a;
|
|
@@ -11610,28 +11610,95 @@ function toAISdkStream(stream, options = {
|
|
|
11610
11610
|
}
|
|
11611
11611
|
|
|
11612
11612
|
// src/chat-route.ts
|
|
11613
|
-
function
|
|
11614
|
-
const
|
|
11615
|
-
|
|
11616
|
-
const
|
|
11617
|
-
|
|
11618
|
-
const part
|
|
11619
|
-
|
|
11620
|
-
|
|
11621
|
-
|
|
11622
|
-
|
|
11623
|
-
|
|
11624
|
-
|
|
11625
|
-
|
|
11626
|
-
|
|
11627
|
-
|
|
11628
|
-
|
|
11629
|
-
|
|
11630
|
-
|
|
11631
|
-
|
|
11632
|
-
|
|
11613
|
+
function extractV6NativeApprovals(messages) {
|
|
11614
|
+
const byTarget = /* @__PURE__ */ new Map();
|
|
11615
|
+
const separator = APPROVAL_ID_SEPARATOR;
|
|
11616
|
+
for (const message of messages) {
|
|
11617
|
+
if (message.role !== "assistant") continue;
|
|
11618
|
+
for (const part of message.parts ?? []) {
|
|
11619
|
+
if (!isToolUIPart2(part) || part.state !== "approval-responded") continue;
|
|
11620
|
+
const lastSep = part.approval.id.lastIndexOf(separator);
|
|
11621
|
+
if (lastSep === -1) continue;
|
|
11622
|
+
const runId = part.approval.id.slice(0, lastSep);
|
|
11623
|
+
const toolCallId = part.approval.id.slice(lastSep + separator.length);
|
|
11624
|
+
if (!runId || !toolCallId) continue;
|
|
11625
|
+
if (toolCallId !== part.toolCallId) continue;
|
|
11626
|
+
byTarget.set(`${runId}${separator}${toolCallId}`, {
|
|
11627
|
+
resumeData: {
|
|
11628
|
+
approved: part.approval.approved,
|
|
11629
|
+
...part.approval.reason != null ? { reason: part.approval.reason } : {}
|
|
11630
|
+
},
|
|
11631
|
+
runId,
|
|
11632
|
+
toolCallId
|
|
11633
|
+
});
|
|
11634
|
+
}
|
|
11633
11635
|
}
|
|
11634
|
-
return
|
|
11636
|
+
return [...byTarget.values()];
|
|
11637
|
+
}
|
|
11638
|
+
function streamV6ApprovalResumes(args) {
|
|
11639
|
+
const { agent, approvals, baseOptions, structuredOutput, messages, lastMessageId } = args;
|
|
11640
|
+
const { sendStart, sendFinish, sendReasoning, sendSources, onError, messageMetadata } = args;
|
|
11641
|
+
return createUIMessageStream2({
|
|
11642
|
+
originalMessages: messages,
|
|
11643
|
+
onError,
|
|
11644
|
+
execute: async ({ writer }) => {
|
|
11645
|
+
let startWritten = false;
|
|
11646
|
+
let successfulLegs = 0;
|
|
11647
|
+
let firstResolvedTargetError;
|
|
11648
|
+
let finalFinish;
|
|
11649
|
+
for (const approval of approvals) {
|
|
11650
|
+
try {
|
|
11651
|
+
const result = await agent.resumeStream(approval.resumeData, {
|
|
11652
|
+
...baseOptions,
|
|
11653
|
+
runId: approval.runId,
|
|
11654
|
+
toolCallId: approval.toolCallId,
|
|
11655
|
+
...structuredOutput ? { structuredOutput } : {}
|
|
11656
|
+
});
|
|
11657
|
+
for await (const part of toAISdkStream(result, {
|
|
11658
|
+
from: "agent",
|
|
11659
|
+
version: "v6",
|
|
11660
|
+
lastMessageId,
|
|
11661
|
+
sendStart,
|
|
11662
|
+
sendFinish,
|
|
11663
|
+
sendReasoning,
|
|
11664
|
+
sendSources,
|
|
11665
|
+
onError,
|
|
11666
|
+
messageMetadata
|
|
11667
|
+
})) {
|
|
11668
|
+
if (part.type === "start") {
|
|
11669
|
+
if (startWritten) continue;
|
|
11670
|
+
startWritten = true;
|
|
11671
|
+
writer.write(part);
|
|
11672
|
+
continue;
|
|
11673
|
+
}
|
|
11674
|
+
if (!startWritten && sendStart) {
|
|
11675
|
+
writer.write({ type: "start", ...lastMessageId ? { messageId: lastMessageId } : {} });
|
|
11676
|
+
startWritten = true;
|
|
11677
|
+
}
|
|
11678
|
+
if (part.type === "finish") {
|
|
11679
|
+
finalFinish = part;
|
|
11680
|
+
continue;
|
|
11681
|
+
}
|
|
11682
|
+
writer.write(part);
|
|
11683
|
+
}
|
|
11684
|
+
successfulLegs++;
|
|
11685
|
+
} catch (error) {
|
|
11686
|
+
const id = error?.id;
|
|
11687
|
+
if (id !== "AGENT_RESUME_TOOL_CALL_NOT_SUSPENDED" && id !== "AGENT_RESUME_NO_SNAPSHOT_FOUND") {
|
|
11688
|
+
throw error;
|
|
11689
|
+
}
|
|
11690
|
+
firstResolvedTargetError ??= error;
|
|
11691
|
+
}
|
|
11692
|
+
}
|
|
11693
|
+
if (successfulLegs === 0 && firstResolvedTargetError) throw firstResolvedTargetError;
|
|
11694
|
+
if (!startWritten && sendStart) {
|
|
11695
|
+
writer.write({ type: "start", ...lastMessageId ? { messageId: lastMessageId } : {} });
|
|
11696
|
+
}
|
|
11697
|
+
if (sendFinish) {
|
|
11698
|
+
writer.write(finalFinish ?? { type: "finish" });
|
|
11699
|
+
}
|
|
11700
|
+
}
|
|
11701
|
+
});
|
|
11635
11702
|
}
|
|
11636
11703
|
async function handleChatStream({
|
|
11637
11704
|
mastra,
|
|
@@ -11669,9 +11736,6 @@ async function handleChatStream({
|
|
|
11669
11736
|
if (!Array.isArray(messages)) {
|
|
11670
11737
|
throw new Error("Messages must be an array of UIMessage objects");
|
|
11671
11738
|
}
|
|
11672
|
-
const nativeApproval = version === "v6" && !resumeData ? extractV6NativeApproval(messages) : null;
|
|
11673
|
-
const effectiveResumeData = nativeApproval?.resumeData ?? resumeData;
|
|
11674
|
-
const effectiveRunId = nativeApproval?.runId ?? runId;
|
|
11675
11739
|
let lastMessageId;
|
|
11676
11740
|
let messagesToSend = messages;
|
|
11677
11741
|
if (messages.length > 0) {
|
|
@@ -11693,12 +11757,30 @@ async function handleChatStream({
|
|
|
11693
11757
|
const baseOptions = {
|
|
11694
11758
|
...defaultOptionsRest,
|
|
11695
11759
|
...restOptions,
|
|
11696
|
-
...
|
|
11697
|
-
...nativeApproval?.toolCallId && { toolCallId: nativeApproval.toolCallId },
|
|
11760
|
+
...runId && { runId },
|
|
11698
11761
|
requestContext: requestContext || defaultOptions3?.requestContext,
|
|
11699
11762
|
...Object.keys(mergedProviderOptions).length > 0 && { providerOptions: mergedProviderOptions }
|
|
11700
11763
|
};
|
|
11701
|
-
|
|
11764
|
+
if (version === "v6" && !resumeData && trigger !== "regenerate-message" && messages.at(-1)?.role === "assistant") {
|
|
11765
|
+
const approvals = extractV6NativeApprovals(messages);
|
|
11766
|
+
if (approvals.length > 0) {
|
|
11767
|
+
return streamV6ApprovalResumes({
|
|
11768
|
+
agent: agentObj,
|
|
11769
|
+
approvals,
|
|
11770
|
+
baseOptions,
|
|
11771
|
+
structuredOutput,
|
|
11772
|
+
messages,
|
|
11773
|
+
lastMessageId,
|
|
11774
|
+
sendStart,
|
|
11775
|
+
sendFinish,
|
|
11776
|
+
sendReasoning,
|
|
11777
|
+
sendSources,
|
|
11778
|
+
onError,
|
|
11779
|
+
messageMetadata
|
|
11780
|
+
});
|
|
11781
|
+
}
|
|
11782
|
+
}
|
|
11783
|
+
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);
|
|
11702
11784
|
if (version === "v6") {
|
|
11703
11785
|
return createUIMessageStream2({
|
|
11704
11786
|
originalMessages: messages,
|