@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/dist/index.js
CHANGED
|
@@ -11595,6 +11595,7 @@ var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
|
11595
11595
|
};
|
|
11596
11596
|
|
|
11597
11597
|
// src/helpers.ts
|
|
11598
|
+
var APPROVAL_ID_SEPARATOR = "::";
|
|
11598
11599
|
function toAISDKFinishReason(reason) {
|
|
11599
11600
|
if (reason === "tripwire" || reason === "retry") {
|
|
11600
11601
|
return "other";
|
|
@@ -11931,6 +11932,27 @@ function convertMastraChunkToAISDKv6({
|
|
|
11931
11932
|
chunk,
|
|
11932
11933
|
mode = "stream"
|
|
11933
11934
|
}) {
|
|
11935
|
+
if (chunk.type === "tool-call-approval") {
|
|
11936
|
+
return [
|
|
11937
|
+
{
|
|
11938
|
+
type: "tool-approval-request",
|
|
11939
|
+
approvalId: `${chunk.runId}${APPROVAL_ID_SEPARATOR}${chunk.payload.toolCallId}`,
|
|
11940
|
+
toolCallId: chunk.payload.toolCallId
|
|
11941
|
+
},
|
|
11942
|
+
{
|
|
11943
|
+
type: "data-tool-call-approval",
|
|
11944
|
+
id: chunk.payload.toolCallId,
|
|
11945
|
+
data: {
|
|
11946
|
+
state: "data-tool-call-approval",
|
|
11947
|
+
runId: chunk.runId,
|
|
11948
|
+
toolCallId: chunk.payload.toolCallId,
|
|
11949
|
+
toolName: chunk.payload.toolName,
|
|
11950
|
+
args: chunk.payload.args,
|
|
11951
|
+
resumeSchema: chunk.payload.resumeSchema
|
|
11952
|
+
}
|
|
11953
|
+
}
|
|
11954
|
+
];
|
|
11955
|
+
}
|
|
11934
11956
|
return convertMastraChunkToAISDKBase({
|
|
11935
11957
|
chunk,
|
|
11936
11958
|
mode,
|
|
@@ -12061,6 +12083,13 @@ function convertFullStreamChunkToUIMessageStream({
|
|
|
12061
12083
|
...part.dynamic != null ? { dynamic: part.dynamic } : {}
|
|
12062
12084
|
};
|
|
12063
12085
|
}
|
|
12086
|
+
case "tool-approval-request": {
|
|
12087
|
+
return {
|
|
12088
|
+
type: "tool-approval-request",
|
|
12089
|
+
approvalId: part.approvalId,
|
|
12090
|
+
toolCallId: part.toolCallId
|
|
12091
|
+
};
|
|
12092
|
+
}
|
|
12064
12093
|
case "tool-result": {
|
|
12065
12094
|
return {
|
|
12066
12095
|
type: "tool-output-available",
|
|
@@ -12198,7 +12227,13 @@ function createWorkflowStreamToAISDKTransformer(convertMastraChunkToAISDK, {
|
|
|
12198
12227
|
},
|
|
12199
12228
|
convertMastraChunkToAISDK
|
|
12200
12229
|
);
|
|
12201
|
-
if (
|
|
12230
|
+
if (Array.isArray(transformed)) {
|
|
12231
|
+
for (const c of transformed) {
|
|
12232
|
+
if (c) controller.enqueue(c);
|
|
12233
|
+
}
|
|
12234
|
+
} else if (transformed) {
|
|
12235
|
+
controller.enqueue(transformed);
|
|
12236
|
+
}
|
|
12202
12237
|
}
|
|
12203
12238
|
});
|
|
12204
12239
|
}
|
|
@@ -12286,41 +12321,62 @@ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, {
|
|
|
12286
12321
|
});
|
|
12287
12322
|
}
|
|
12288
12323
|
const part = convertMastraChunkToAISDK({ chunk, mode: "stream" });
|
|
12289
|
-
const
|
|
12290
|
-
|
|
12291
|
-
|
|
12292
|
-
|
|
12293
|
-
|
|
12294
|
-
|
|
12295
|
-
|
|
12296
|
-
|
|
12297
|
-
|
|
12298
|
-
|
|
12324
|
+
const enqueueTransformedPart = (p) => {
|
|
12325
|
+
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
12326
|
+
part: p,
|
|
12327
|
+
sendReasoning,
|
|
12328
|
+
sendSources,
|
|
12329
|
+
messageMetadataValue: p ? messageMetadata?.({ part: p }) : void 0,
|
|
12330
|
+
sendStart,
|
|
12331
|
+
sendFinish,
|
|
12332
|
+
responseMessageId: lastMessageId,
|
|
12333
|
+
onError(error) {
|
|
12334
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
12335
|
+
}
|
|
12336
|
+
});
|
|
12337
|
+
if (transformedChunk) {
|
|
12338
|
+
if (transformedChunk.type === "tool-agent") {
|
|
12339
|
+
const payload = transformedChunk.payload;
|
|
12340
|
+
const agentTransformed = transformAgent(payload, bufferedSteps);
|
|
12341
|
+
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
12342
|
+
} else if (transformedChunk.type === "tool-workflow") {
|
|
12343
|
+
const payload = transformedChunk.payload;
|
|
12344
|
+
const workflowChunk = transformWorkflow(
|
|
12345
|
+
payload,
|
|
12346
|
+
bufferedSteps,
|
|
12347
|
+
true,
|
|
12348
|
+
void 0,
|
|
12349
|
+
void 0,
|
|
12350
|
+
convertMastraChunkToAISDK
|
|
12351
|
+
);
|
|
12352
|
+
if (Array.isArray(workflowChunk)) {
|
|
12353
|
+
for (const c of workflowChunk) {
|
|
12354
|
+
if (c) controller.enqueue(c);
|
|
12355
|
+
}
|
|
12356
|
+
} else if (workflowChunk) {
|
|
12357
|
+
controller.enqueue(workflowChunk);
|
|
12358
|
+
}
|
|
12359
|
+
} else if (transformedChunk.type === "tool-network") {
|
|
12360
|
+
const payload = transformedChunk.payload;
|
|
12361
|
+
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
12362
|
+
if (Array.isArray(networkChunk)) {
|
|
12363
|
+
for (const c of networkChunk) {
|
|
12364
|
+
if (c) controller.enqueue(c);
|
|
12365
|
+
}
|
|
12366
|
+
} else if (networkChunk) {
|
|
12367
|
+
controller.enqueue(networkChunk);
|
|
12368
|
+
}
|
|
12369
|
+
} else {
|
|
12370
|
+
controller.enqueue(transformedChunk);
|
|
12371
|
+
}
|
|
12299
12372
|
}
|
|
12300
|
-
}
|
|
12301
|
-
if (
|
|
12302
|
-
|
|
12303
|
-
|
|
12304
|
-
const agentTransformed = transformAgent(payload, bufferedSteps);
|
|
12305
|
-
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
12306
|
-
} else if (transformedChunk.type === "tool-workflow") {
|
|
12307
|
-
const payload = transformedChunk.payload;
|
|
12308
|
-
const workflowChunk = transformWorkflow(
|
|
12309
|
-
payload,
|
|
12310
|
-
bufferedSteps,
|
|
12311
|
-
true,
|
|
12312
|
-
void 0,
|
|
12313
|
-
void 0,
|
|
12314
|
-
convertMastraChunkToAISDK
|
|
12315
|
-
);
|
|
12316
|
-
if (workflowChunk) controller.enqueue(workflowChunk);
|
|
12317
|
-
} else if (transformedChunk.type === "tool-network") {
|
|
12318
|
-
const payload = transformedChunk.payload;
|
|
12319
|
-
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
12320
|
-
if (networkChunk) controller.enqueue(networkChunk);
|
|
12321
|
-
} else {
|
|
12322
|
-
controller.enqueue(transformedChunk);
|
|
12373
|
+
};
|
|
12374
|
+
if (Array.isArray(part)) {
|
|
12375
|
+
for (const p of part) {
|
|
12376
|
+
enqueueTransformedPart(p);
|
|
12323
12377
|
}
|
|
12378
|
+
} else {
|
|
12379
|
+
enqueueTransformedPart(part);
|
|
12324
12380
|
}
|
|
12325
12381
|
},
|
|
12326
12382
|
flush(controller) {
|
|
@@ -12663,6 +12719,18 @@ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStre
|
|
|
12663
12719
|
const output = payload.payload.output;
|
|
12664
12720
|
if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
|
|
12665
12721
|
const part = convertMastraChunkToAISDK({ chunk: output, mode: "stream" });
|
|
12722
|
+
if (Array.isArray(part)) {
|
|
12723
|
+
return part.map(
|
|
12724
|
+
(p) => convertFullStreamChunkToUIMessageStream({
|
|
12725
|
+
part: p,
|
|
12726
|
+
sendReasoning: streamOptions?.sendReasoning,
|
|
12727
|
+
sendSources: streamOptions?.sendSources,
|
|
12728
|
+
onError(error) {
|
|
12729
|
+
return safeParseErrorObject(error);
|
|
12730
|
+
}
|
|
12731
|
+
})
|
|
12732
|
+
).filter(Boolean);
|
|
12733
|
+
}
|
|
12666
12734
|
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
12667
12735
|
part,
|
|
12668
12736
|
sendReasoning: streamOptions?.sendReasoning,
|
|
@@ -13160,6 +13228,32 @@ function toAISdkStream(stream, options = {
|
|
|
13160
13228
|
}
|
|
13161
13229
|
|
|
13162
13230
|
// src/chat-route.ts
|
|
13231
|
+
function extractV6NativeApproval(messages) {
|
|
13232
|
+
let lastAssistantMsg;
|
|
13233
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
13234
|
+
const message = messages[i];
|
|
13235
|
+
if (message.role === "assistant") {
|
|
13236
|
+
lastAssistantMsg = message;
|
|
13237
|
+
break;
|
|
13238
|
+
}
|
|
13239
|
+
}
|
|
13240
|
+
if (!lastAssistantMsg) return null;
|
|
13241
|
+
for (const part of lastAssistantMsg.parts ?? []) {
|
|
13242
|
+
if (!isToolUIPart2(part) || part.state !== "approval-responded") continue;
|
|
13243
|
+
const lastSep = part.approval.id.lastIndexOf(APPROVAL_ID_SEPARATOR);
|
|
13244
|
+
if (lastSep === -1) continue;
|
|
13245
|
+
const runId = part.approval.id.slice(0, lastSep);
|
|
13246
|
+
if (!runId) continue;
|
|
13247
|
+
return {
|
|
13248
|
+
resumeData: {
|
|
13249
|
+
approved: part.approval.approved,
|
|
13250
|
+
...part.approval.reason != null ? { reason: part.approval.reason } : {}
|
|
13251
|
+
},
|
|
13252
|
+
runId
|
|
13253
|
+
};
|
|
13254
|
+
}
|
|
13255
|
+
return null;
|
|
13256
|
+
}
|
|
13163
13257
|
async function handleChatStream({
|
|
13164
13258
|
mastra,
|
|
13165
13259
|
agentId,
|
|
@@ -13185,6 +13279,9 @@ async function handleChatStream({
|
|
|
13185
13279
|
if (!Array.isArray(messages)) {
|
|
13186
13280
|
throw new Error("Messages must be an array of UIMessage objects");
|
|
13187
13281
|
}
|
|
13282
|
+
const nativeApproval = version === "v6" && !resumeData ? extractV6NativeApproval(messages) : null;
|
|
13283
|
+
const effectiveResumeData = nativeApproval?.resumeData ?? resumeData;
|
|
13284
|
+
const effectiveRunId = nativeApproval?.runId ?? runId;
|
|
13188
13285
|
let lastMessageId;
|
|
13189
13286
|
let messagesToSend = messages;
|
|
13190
13287
|
if (messages.length > 0) {
|
|
@@ -13206,11 +13303,11 @@ async function handleChatStream({
|
|
|
13206
13303
|
const baseOptions = {
|
|
13207
13304
|
...defaultOptionsRest,
|
|
13208
13305
|
...restOptions,
|
|
13209
|
-
...
|
|
13306
|
+
...effectiveRunId && { runId: effectiveRunId },
|
|
13210
13307
|
requestContext: requestContext || defaultOptions3?.requestContext,
|
|
13211
13308
|
...Object.keys(mergedProviderOptions).length > 0 && { providerOptions: mergedProviderOptions }
|
|
13212
13309
|
};
|
|
13213
|
-
const result =
|
|
13310
|
+
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);
|
|
13214
13311
|
if (version === "v6") {
|
|
13215
13312
|
return createUIMessageStream2({
|
|
13216
13313
|
originalMessages: messages,
|