@mastra/ai-sdk 1.4.0 → 1.4.1-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 +11 -0
- package/dist/chat-route.d.ts +15 -0
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/helpers.d.ts +12 -5
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +133 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +133 -36
- package/dist/index.js.map +1 -1
- package/dist/transformers.d.ts +67 -41
- package/dist/transformers.d.ts.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @mastra/ai-sdk
|
|
2
2
|
|
|
3
|
+
## 1.4.1-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fixed tool call approvals in AI SDK v6: `handleChatStream` now automatically routes to `resumeStream` when the AI SDK v6 native approval flow is used on the client (no extra server-side wiring required). The v6 stream now emits native `tool-approval-request` parts so `useChat` can surface approval UI and call `addToolApprovalResponse()`, while also emitting the existing `data-tool-call-approval` chunk for backwards compatibility. ([#15345](https://github.com/mastra-ai/mastra/pull/15345))
|
|
8
|
+
|
|
9
|
+
- Fixed AI SDK v6 tool approval streams so requireApproval works with handleChatStream and AssistantChatTransport. ([#15345](https://github.com/mastra-ai/mastra/pull/15345))
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`d63ffdb`](https://github.com/mastra-ai/mastra/commit/d63ffdbb2c11e76fe5ea45faab44bc15460f010c)]:
|
|
12
|
+
- @mastra/core@1.25.1-alpha.0
|
|
13
|
+
|
|
3
14
|
## 1.4.0
|
|
4
15
|
|
|
5
16
|
### Minor Changes
|
package/dist/chat-route.d.ts
CHANGED
|
@@ -4,6 +4,21 @@ 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 an 'approval-responded' tool part in the
|
|
9
|
+
* last trailing assistant message only. When found, splits the composite
|
|
10
|
+
* approvalId ("${runId}::${toolCallId}") to recover the runId needed for
|
|
11
|
+
* resumeStream.
|
|
12
|
+
*
|
|
13
|
+
* Only the last trailing assistant message is inspected so that approval
|
|
14
|
+
* responses from earlier turns are never re-processed.
|
|
15
|
+
*
|
|
16
|
+
* Returns null when no approval response is present (normal chat turn).
|
|
17
|
+
*/
|
|
18
|
+
export declare function extractV6NativeApproval(messages: V6UIMessage[]): {
|
|
19
|
+
resumeData: Record<string, unknown>;
|
|
20
|
+
runId: string;
|
|
21
|
+
} | null;
|
|
7
22
|
export type ChatStreamHandlerParams<UI_MESSAGE extends SupportedUIMessage = SupportedUIMessage, OUTPUT = undefined> = AgentExecutionOptions<OUTPUT> & {
|
|
8
23
|
messages: UI_MESSAGE[];
|
|
9
24
|
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;
|
|
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,OA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1C,wBAAgB,gBAAgB,CAAC,UAAU,SAAS,WAAW,GAAG,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/F,OAAO,EAAE,0BAA0B,CAAC,UAAU,EAAE,MAAM,CAAC,GACtD,OAAO,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAAC,CAAC;AAwH1C,MAAM,MAAM,gBAAgB,CAAC,MAAM,GAAG,SAAS,IAAI;IACjD,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;CACvB,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;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,GACpB,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAkNhE"}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import type { InferUIMessageChunk, ObjectStreamPart, TextStreamPart, ToolSet, UIMessage, FinishReason } from './_types/@internal_ai-sdk-v5/dist/index.js';
|
|
2
|
-
import type { FinishReason as FinishReasonV6 } from './_types/@internal_ai-v6/dist/index.js';
|
|
2
|
+
import type { FinishReason as FinishReasonV6, InferUIMessageChunk as InferUIMessageChunkV6, ToolApprovalRequest, UIMessage as UIMessageV6 } from './_types/@internal_ai-v6/dist/index.js';
|
|
3
3
|
import type { DataChunkType, ChunkType, MastraFinishReason } from '@mastra/core/stream';
|
|
4
|
+
/**
|
|
5
|
+
* Separator used to encode both runId and toolCallId into a single approvalId string.
|
|
6
|
+
* Chosen because neither runId nor toolCallId can contain ":" in normal usage
|
|
7
|
+
* (UUIDs are hex + hyphens; provider tool call IDs are alphanumeric + underscores).
|
|
8
|
+
* The server splits on this separator to recover the runId for resumeStream.
|
|
9
|
+
*/
|
|
10
|
+
export declare const APPROVAL_ID_SEPARATOR = "::";
|
|
4
11
|
/**
|
|
5
12
|
* Maps Mastra's extended finish reasons to AI SDK-compatible values.
|
|
6
13
|
* 'tripwire' and 'retry' are Mastra-specific reasons for processor scenarios,
|
|
7
14
|
* which are mapped to 'other' for AI SDK compatibility.
|
|
8
15
|
*/
|
|
9
16
|
export declare function toAISDKFinishReason(reason: MastraFinishReason): FinishReason;
|
|
10
|
-
export type OutputChunkType<OUTPUT = undefined> = TextStreamPart<ToolSet> | ObjectStreamPart<Partial<OUTPUT>> | DataChunkType | undefined;
|
|
17
|
+
export type OutputChunkType<OUTPUT = undefined> = TextStreamPart<ToolSet> | ObjectStreamPart<Partial<OUTPUT>> | ToolApprovalRequest | DataChunkType | undefined;
|
|
11
18
|
export type ToolAgentChunkType = {
|
|
12
19
|
type: 'tool-agent';
|
|
13
20
|
toolCallId: string;
|
|
@@ -40,9 +47,9 @@ export declare function toAISDKFinishReasonV6(reason: MastraFinishReason): Finis
|
|
|
40
47
|
export declare function convertMastraChunkToAISDKv6<OUTPUT = undefined>({ chunk, mode, }: {
|
|
41
48
|
chunk: ChunkType<OUTPUT>;
|
|
42
49
|
mode?: 'generate' | 'stream';
|
|
43
|
-
}): OutputChunkType<OUTPUT
|
|
50
|
+
}): OutputChunkType<OUTPUT> | OutputChunkType<OUTPUT>[];
|
|
44
51
|
export declare function convertFullStreamChunkToUIMessageStream<UI_MESSAGE extends UIMessage>({ part, messageMetadataValue, sendReasoning, sendSources, onError, sendStart, sendFinish, responseMessageId, }: {
|
|
45
|
-
part: TextStreamPart<ToolSet> | DataChunkType | {
|
|
52
|
+
part: TextStreamPart<ToolSet> | DataChunkType | ToolApprovalRequest | {
|
|
46
53
|
type: 'tool-output';
|
|
47
54
|
toolCallId: string;
|
|
48
55
|
output: any;
|
|
@@ -54,6 +61,6 @@ export declare function convertFullStreamChunkToUIMessageStream<UI_MESSAGE exten
|
|
|
54
61
|
sendStart?: boolean;
|
|
55
62
|
sendFinish?: boolean;
|
|
56
63
|
responseMessageId?: string;
|
|
57
|
-
}): InferUIMessageChunk<UI_MESSAGE> | ToolAgentChunkType | ToolWorkflowChunkType | ToolNetworkChunkType | undefined;
|
|
64
|
+
}): InferUIMessageChunk<UI_MESSAGE> | InferUIMessageChunkV6<UIMessageV6> | ToolAgentChunkType | ToolWorkflowChunkType | ToolNetworkChunkType | undefined;
|
|
58
65
|
export {};
|
|
59
66
|
//# sourceMappingURL=helpers.d.ts.map
|
package/dist/helpers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EAEnB,gBAAgB,EAChB,cAAc,EACd,OAAO,EACP,SAAS,EACT,YAAY,EACb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAEV,YAAY,IAAI,cAAc,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EAEnB,gBAAgB,EAChB,cAAc,EACd,OAAO,EACP,SAAS,EACT,YAAY,EACb,MAAM,qBAAqB,CAAC;AAC7B,O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EAAE,OAAO,CAAC;IAC/B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;IACpC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,GACG,mBAAmB,CAAC,UAAU,CAAC,GAC/B,qBAAqB,CAAC,WAAW,CAAC,GAClC,kBAAkB,GAClB,qBAAqB,GACrB,oBAAoB,GACpB,SAAS,CA8PZ"}
|
package/dist/index.cjs
CHANGED
|
@@ -11616,6 +11616,7 @@ var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
|
11616
11616
|
};
|
|
11617
11617
|
|
|
11618
11618
|
// src/helpers.ts
|
|
11619
|
+
var APPROVAL_ID_SEPARATOR = "::";
|
|
11619
11620
|
function toAISDKFinishReason(reason) {
|
|
11620
11621
|
if (reason === "tripwire" || reason === "retry") {
|
|
11621
11622
|
return "other";
|
|
@@ -11952,6 +11953,27 @@ function convertMastraChunkToAISDKv6({
|
|
|
11952
11953
|
chunk,
|
|
11953
11954
|
mode = "stream"
|
|
11954
11955
|
}) {
|
|
11956
|
+
if (chunk.type === "tool-call-approval") {
|
|
11957
|
+
return [
|
|
11958
|
+
{
|
|
11959
|
+
type: "tool-approval-request",
|
|
11960
|
+
approvalId: `${chunk.runId}${APPROVAL_ID_SEPARATOR}${chunk.payload.toolCallId}`,
|
|
11961
|
+
toolCallId: chunk.payload.toolCallId
|
|
11962
|
+
},
|
|
11963
|
+
{
|
|
11964
|
+
type: "data-tool-call-approval",
|
|
11965
|
+
id: chunk.payload.toolCallId,
|
|
11966
|
+
data: {
|
|
11967
|
+
state: "data-tool-call-approval",
|
|
11968
|
+
runId: chunk.runId,
|
|
11969
|
+
toolCallId: chunk.payload.toolCallId,
|
|
11970
|
+
toolName: chunk.payload.toolName,
|
|
11971
|
+
args: chunk.payload.args,
|
|
11972
|
+
resumeSchema: chunk.payload.resumeSchema
|
|
11973
|
+
}
|
|
11974
|
+
}
|
|
11975
|
+
];
|
|
11976
|
+
}
|
|
11955
11977
|
return convertMastraChunkToAISDKBase({
|
|
11956
11978
|
chunk,
|
|
11957
11979
|
mode,
|
|
@@ -12082,6 +12104,13 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
12082
12104
|
...part.dynamic != null ? { dynamic: part.dynamic } : {}
|
|
12083
12105
|
};
|
|
12084
12106
|
}
|
|
12107
|
+
case "tool-approval-request": {
|
|
12108
|
+
return {
|
|
12109
|
+
type: "tool-approval-request",
|
|
12110
|
+
approvalId: part.approvalId,
|
|
12111
|
+
toolCallId: part.toolCallId
|
|
12112
|
+
};
|
|
12113
|
+
}
|
|
12085
12114
|
case "tool-result": {
|
|
12086
12115
|
return {
|
|
12087
12116
|
type: "tool-output-available",
|
|
@@ -12219,7 +12248,13 @@ function createWorkflowStreamToAISDKTransformer(convertMastraChunkToAISDK, {
|
|
|
12219
12248
|
},
|
|
12220
12249
|
convertMastraChunkToAISDK
|
|
12221
12250
|
);
|
|
12222
|
-
if (
|
|
12251
|
+
if (Array.isArray(transformed)) {
|
|
12252
|
+
for (const c of transformed) {
|
|
12253
|
+
if (c) controller.enqueue(c);
|
|
12254
|
+
}
|
|
12255
|
+
} else if (transformed) {
|
|
12256
|
+
controller.enqueue(transformed);
|
|
12257
|
+
}
|
|
12223
12258
|
}
|
|
12224
12259
|
});
|
|
12225
12260
|
}
|
|
@@ -12307,41 +12342,62 @@ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, {
|
|
|
12307
12342
|
});
|
|
12308
12343
|
}
|
|
12309
12344
|
const part = convertMastraChunkToAISDK({ chunk, mode: "stream" });
|
|
12310
|
-
const
|
|
12311
|
-
|
|
12312
|
-
|
|
12313
|
-
|
|
12314
|
-
|
|
12315
|
-
|
|
12316
|
-
|
|
12317
|
-
|
|
12318
|
-
|
|
12319
|
-
|
|
12345
|
+
const enqueueTransformedPart = (p) => {
|
|
12346
|
+
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
12347
|
+
part: p,
|
|
12348
|
+
sendReasoning,
|
|
12349
|
+
sendSources,
|
|
12350
|
+
messageMetadataValue: p ? messageMetadata?.({ part: p }) : void 0,
|
|
12351
|
+
sendStart,
|
|
12352
|
+
sendFinish,
|
|
12353
|
+
responseMessageId: lastMessageId,
|
|
12354
|
+
onError(error) {
|
|
12355
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
12356
|
+
}
|
|
12357
|
+
});
|
|
12358
|
+
if (transformedChunk) {
|
|
12359
|
+
if (transformedChunk.type === "tool-agent") {
|
|
12360
|
+
const payload = transformedChunk.payload;
|
|
12361
|
+
const agentTransformed = transformAgent(payload, bufferedSteps);
|
|
12362
|
+
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
12363
|
+
} else if (transformedChunk.type === "tool-workflow") {
|
|
12364
|
+
const payload = transformedChunk.payload;
|
|
12365
|
+
const workflowChunk = transformWorkflow(
|
|
12366
|
+
payload,
|
|
12367
|
+
bufferedSteps,
|
|
12368
|
+
true,
|
|
12369
|
+
void 0,
|
|
12370
|
+
void 0,
|
|
12371
|
+
convertMastraChunkToAISDK
|
|
12372
|
+
);
|
|
12373
|
+
if (Array.isArray(workflowChunk)) {
|
|
12374
|
+
for (const c of workflowChunk) {
|
|
12375
|
+
if (c) controller.enqueue(c);
|
|
12376
|
+
}
|
|
12377
|
+
} else if (workflowChunk) {
|
|
12378
|
+
controller.enqueue(workflowChunk);
|
|
12379
|
+
}
|
|
12380
|
+
} else if (transformedChunk.type === "tool-network") {
|
|
12381
|
+
const payload = transformedChunk.payload;
|
|
12382
|
+
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
12383
|
+
if (Array.isArray(networkChunk)) {
|
|
12384
|
+
for (const c of networkChunk) {
|
|
12385
|
+
if (c) controller.enqueue(c);
|
|
12386
|
+
}
|
|
12387
|
+
} else if (networkChunk) {
|
|
12388
|
+
controller.enqueue(networkChunk);
|
|
12389
|
+
}
|
|
12390
|
+
} else {
|
|
12391
|
+
controller.enqueue(transformedChunk);
|
|
12392
|
+
}
|
|
12320
12393
|
}
|
|
12321
|
-
}
|
|
12322
|
-
if (
|
|
12323
|
-
|
|
12324
|
-
|
|
12325
|
-
const agentTransformed = transformAgent(payload, bufferedSteps);
|
|
12326
|
-
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
12327
|
-
} else if (transformedChunk.type === "tool-workflow") {
|
|
12328
|
-
const payload = transformedChunk.payload;
|
|
12329
|
-
const workflowChunk = transformWorkflow(
|
|
12330
|
-
payload,
|
|
12331
|
-
bufferedSteps,
|
|
12332
|
-
true,
|
|
12333
|
-
void 0,
|
|
12334
|
-
void 0,
|
|
12335
|
-
convertMastraChunkToAISDK
|
|
12336
|
-
);
|
|
12337
|
-
if (workflowChunk) controller.enqueue(workflowChunk);
|
|
12338
|
-
} else if (transformedChunk.type === "tool-network") {
|
|
12339
|
-
const payload = transformedChunk.payload;
|
|
12340
|
-
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
12341
|
-
if (networkChunk) controller.enqueue(networkChunk);
|
|
12342
|
-
} else {
|
|
12343
|
-
controller.enqueue(transformedChunk);
|
|
12394
|
+
};
|
|
12395
|
+
if (Array.isArray(part)) {
|
|
12396
|
+
for (const p of part) {
|
|
12397
|
+
enqueueTransformedPart(p);
|
|
12344
12398
|
}
|
|
12399
|
+
} else {
|
|
12400
|
+
enqueueTransformedPart(part);
|
|
12345
12401
|
}
|
|
12346
12402
|
},
|
|
12347
12403
|
flush(controller) {
|
|
@@ -12684,6 +12740,18 @@ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStre
|
|
|
12684
12740
|
const output = payload.payload.output;
|
|
12685
12741
|
if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
|
|
12686
12742
|
const part = convertMastraChunkToAISDK({ chunk: output, mode: "stream" });
|
|
12743
|
+
if (Array.isArray(part)) {
|
|
12744
|
+
return part.map(
|
|
12745
|
+
(p) => convertFullStreamChunkToUIMessageStream({
|
|
12746
|
+
part: p,
|
|
12747
|
+
sendReasoning: streamOptions?.sendReasoning,
|
|
12748
|
+
sendSources: streamOptions?.sendSources,
|
|
12749
|
+
onError(error) {
|
|
12750
|
+
return safeParseErrorObject(error);
|
|
12751
|
+
}
|
|
12752
|
+
})
|
|
12753
|
+
).filter(Boolean);
|
|
12754
|
+
}
|
|
12687
12755
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
12688
12756
|
part,
|
|
12689
12757
|
sendReasoning: streamOptions?.sendReasoning,
|
|
@@ -13181,6 +13249,32 @@ function toAISdkStream(stream, options = {
|
|
|
13181
13249
|
}
|
|
13182
13250
|
|
|
13183
13251
|
// src/chat-route.ts
|
|
13252
|
+
function extractV6NativeApproval(messages) {
|
|
13253
|
+
let lastAssistantMsg;
|
|
13254
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
13255
|
+
const message = messages[i];
|
|
13256
|
+
if (message.role === "assistant") {
|
|
13257
|
+
lastAssistantMsg = message;
|
|
13258
|
+
break;
|
|
13259
|
+
}
|
|
13260
|
+
}
|
|
13261
|
+
if (!lastAssistantMsg) return null;
|
|
13262
|
+
for (const part of lastAssistantMsg.parts ?? []) {
|
|
13263
|
+
if (!isToolUIPart2(part) || part.state !== "approval-responded") continue;
|
|
13264
|
+
const lastSep = part.approval.id.lastIndexOf(APPROVAL_ID_SEPARATOR);
|
|
13265
|
+
if (lastSep === -1) continue;
|
|
13266
|
+
const runId = part.approval.id.slice(0, lastSep);
|
|
13267
|
+
if (!runId) continue;
|
|
13268
|
+
return {
|
|
13269
|
+
resumeData: {
|
|
13270
|
+
approved: part.approval.approved,
|
|
13271
|
+
...part.approval.reason != null ? { reason: part.approval.reason } : {}
|
|
13272
|
+
},
|
|
13273
|
+
runId
|
|
13274
|
+
};
|
|
13275
|
+
}
|
|
13276
|
+
return null;
|
|
13277
|
+
}
|
|
13184
13278
|
async function handleChatStream({
|
|
13185
13279
|
mastra,
|
|
13186
13280
|
agentId,
|
|
@@ -13206,6 +13300,9 @@ async function handleChatStream({
|
|
|
13206
13300
|
if (!Array.isArray(messages)) {
|
|
13207
13301
|
throw new Error("Messages must be an array of UIMessage objects");
|
|
13208
13302
|
}
|
|
13303
|
+
const nativeApproval = version === "v6" && !resumeData ? extractV6NativeApproval(messages) : null;
|
|
13304
|
+
const effectiveResumeData = nativeApproval?.resumeData ?? resumeData;
|
|
13305
|
+
const effectiveRunId = nativeApproval?.runId ?? runId;
|
|
13209
13306
|
let lastMessageId;
|
|
13210
13307
|
let messagesToSend = messages;
|
|
13211
13308
|
if (messages.length > 0) {
|
|
@@ -13227,11 +13324,11 @@ async function handleChatStream({
|
|
|
13227
13324
|
const baseOptions = {
|
|
13228
13325
|
...defaultOptionsRest,
|
|
13229
13326
|
...restOptions,
|
|
13230
|
-
...
|
|
13327
|
+
...effectiveRunId && { runId: effectiveRunId },
|
|
13231
13328
|
requestContext: requestContext || defaultOptions3?.requestContext,
|
|
13232
13329
|
...Object.keys(mergedProviderOptions).length > 0 && { providerOptions: mergedProviderOptions }
|
|
13233
13330
|
};
|
|
13234
|
-
const result =
|
|
13331
|
+
const result = effectiveResumeData ? structuredOutput ? await agentObj.resumeStream(effectiveResumeData, { ...baseOptions, structuredOutput }) : await agentObj.resumeStream(effectiveResumeData, baseOptions) : structuredOutput ? await agentObj.stream(messagesToSend, { ...baseOptions, structuredOutput }) : await agentObj.stream(messagesToSend, baseOptions);
|
|
13235
13332
|
if (version === "v6") {
|
|
13236
13333
|
return createUIMessageStream2({
|
|
13237
13334
|
originalMessages: messages,
|