@mastra/ai-sdk 1.11.3 → 1.12.0-alpha.1
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/dist/_types/{@internal_ai-sdk-v5 → @internal_ai-sdk-v5@0.0.95}/dist/index.d.ts +2 -2
- package/dist/_types/{@internal_ai-v6 → @internal_ai-v6@0.0.95}/dist/index.d.ts +99 -18
- package/dist/_types/{@internal_ai-v7 → @internal_ai-v7@0.0.95}/dist/index.d.ts +297 -94
- package/dist/chat-route.d.ts +21 -5
- package/dist/chat-route.d.ts.map +1 -1
- package/dist/convert-streams.d.ts +3 -3
- package/dist/docs/SKILL.md +49 -0
- package/dist/docs/assets/SOURCE_MAP.json +77 -0
- package/dist/docs/references/docs-guides-streaming.md +341 -0
- package/dist/docs/references/docs-memory-message-history.md +424 -0
- package/dist/docs/references/docs-server-custom-api-routes.md +379 -0
- package/dist/docs/references/integrations-agentic-ui-ai-sdk-ui.md +1700 -0
- package/dist/docs/references/reference-ai-sdk-chat-route.md +139 -0
- package/dist/docs/references/reference-ai-sdk-handle-chat-stream.md +101 -0
- package/dist/docs/references/reference-ai-sdk-handle-network-stream.md +49 -0
- package/dist/docs/references/reference-ai-sdk-handle-workflow-stream.md +63 -0
- package/dist/docs/references/reference-ai-sdk-network-route.md +86 -0
- package/dist/docs/references/reference-ai-sdk-overview.md +144 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-messages.md +115 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-stream.md +286 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-v4-messages.md +83 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-v5-messages.md +80 -0
- package/dist/docs/references/reference-ai-sdk-with-mastra.md +67 -0
- package/dist/docs/references/reference-ai-sdk-with-sse-heartbeat.md +49 -0
- package/dist/docs/references/reference-ai-sdk-workflow-route.md +87 -0
- package/dist/docs/references/reference-ai-sdk-workflow-snapshot-to-stream.md +55 -0
- package/dist/docs/references/reference-client-js-agents.md +1112 -0
- package/dist/docs/references/reference-memory-recall.md +169 -0
- package/dist/docs/references/reference-streaming-agents-stream.md +496 -0
- package/dist/docs/references/reference-streaming-smoothStream.md +137 -0
- package/dist/helpers.d.ts +4 -3
- package/dist/helpers.d.ts.map +1 -1
- package/dist/index.cjs +306 -130
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +308 -132
- package/dist/index.js.map +1 -1
- package/dist/middleware.d.ts +2 -2
- package/dist/public-types.d.ts +3 -3
- package/dist/transformers.d.ts +28 -23
- package/dist/transformers.d.ts.map +1 -1
- package/package.json +19 -18
- /package/dist/_types/{@ai-sdk_provider → @ai-sdk_provider@2.0.5}/dist/index.d.ts +0 -0
- /package/dist/_types/{@ai-sdk_provider → @ai-sdk_provider@2.0.5}/package.json +0 -0
- /package/dist/_types/{@internal_ai-sdk-v5 → @internal_ai-sdk-v5@0.0.95}/package.json +0 -0
- /package/dist/_types/{@internal_ai-v6 → @internal_ai-v6@0.0.95}/package.json +0 -0
- /package/dist/_types/{@internal_ai-v7 → @internal_ai-v7@0.0.95}/package.json +0 -0
- /package/dist/_types/{@types_json-schema → @types_json-schema@7.0.15}/index.d.ts +0 -0
- /package/dist/_types/{@types_json-schema → @types_json-schema@7.0.15}/package.json +0 -0
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,EAC9B,mBAAmB,IAAI,qBAAqB,EAE5C,mBAAmB,EACnB,SAAS,IAAI,WAAW,EACzB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,aAAa,EAAE,SA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;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,CAgTZ"}
|
package/dist/index.cjs
CHANGED
|
@@ -24,14 +24,15 @@ let zod_v4 = require("zod/v4");
|
|
|
24
24
|
zod_v4 = __toESM(zod_v4, 1);
|
|
25
25
|
let zod_v3 = require("zod/v3");
|
|
26
26
|
let zod_v4_core = require("zod/v4/core");
|
|
27
|
+
let _mastra_core_memory = require("@mastra/core/memory");
|
|
28
|
+
let _mastra_core_request_context = require("@mastra/core/request-context");
|
|
27
29
|
let _mastra_core_server = require("@mastra/core/server");
|
|
28
30
|
let _mastra_core_stream = require("@mastra/core/stream");
|
|
29
31
|
let stream_web = require("stream/web");
|
|
30
|
-
let _mastra_core_request_context = require("@mastra/core/request-context");
|
|
31
32
|
let _mastra_core_agent = require("@mastra/core/agent");
|
|
32
33
|
let _mastra_core_di = require("@mastra/core/di");
|
|
33
34
|
let _mastra_core_processors = require("@mastra/core/processors");
|
|
34
|
-
// ../../packages/_vendored/ai_v5/dist/dist-
|
|
35
|
+
// ../../packages/_vendored/ai_v5/dist/dist-BYnbJEqC.js
|
|
35
36
|
var marker$1$4 = "vercel.ai.error";
|
|
36
37
|
var symbol$1$4 = Symbol.for(marker$1$4);
|
|
37
38
|
var _a$1$4;
|
|
@@ -1241,6 +1242,20 @@ function asSchema$2(schema) {
|
|
|
1241
1242
|
}) : isSchema$2(schema) ? schema : typeof schema === "function" ? schema() : zodSchema$2(schema);
|
|
1242
1243
|
}
|
|
1243
1244
|
var { btoa: btoa$3, atob: atob$6 } = globalThis;
|
|
1245
|
+
var typedArrayTag$2 = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
|
|
1246
|
+
var arrayBufferByteLength$2 = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
|
|
1247
|
+
function isUint8Array$2(value) {
|
|
1248
|
+
return ArrayBuffer.isView(value) && typedArrayTag$2.call(value) === "Uint8Array";
|
|
1249
|
+
}
|
|
1250
|
+
function isArrayBuffer$2(value) {
|
|
1251
|
+
if (typeof value !== "object" || value === null || !("byteLength" in value)) return false;
|
|
1252
|
+
try {
|
|
1253
|
+
arrayBufferByteLength$2.call(value);
|
|
1254
|
+
return true;
|
|
1255
|
+
} catch (e) {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1244
1259
|
// ../../packages/_vendored/ai_v5/dist/index.js
|
|
1245
1260
|
/** only globals that common to node and browsers are allowed */
|
|
1246
1261
|
var _globalThis = typeof globalThis === "object" ? globalThis : global;
|
|
@@ -2058,8 +2073,8 @@ var NoObjectGeneratedError$1 = class extends AISDKError$2 {
|
|
|
2058
2073
|
_a7$2 = symbol7$2;
|
|
2059
2074
|
var dataContentSchema$1 = zod_v4.z.union([
|
|
2060
2075
|
zod_v4.z.string(),
|
|
2061
|
-
zod_v4.z.
|
|
2062
|
-
zod_v4.z.
|
|
2076
|
+
zod_v4.z.custom(isUint8Array$2, { message: "Must be a Uint8Array" }),
|
|
2077
|
+
zod_v4.z.custom(isArrayBuffer$2, { message: "Must be an ArrayBuffer" }),
|
|
2063
2078
|
zod_v4.z.custom((value) => {
|
|
2064
2079
|
var _a17, _b;
|
|
2065
2080
|
return (_b = (_a17 = globalThis.Buffer) == null ? void 0 : _a17.isBuffer(value)) != null ? _b : false;
|
|
@@ -3266,7 +3281,7 @@ function createUIMessageStream$2({ execute, onError = () => "An error occurred."
|
|
|
3266
3281
|
getOutcome: () => outcome
|
|
3267
3282
|
});
|
|
3268
3283
|
}
|
|
3269
|
-
// ../../packages/_vendored/ai_v6/dist/dist-
|
|
3284
|
+
// ../../packages/_vendored/ai_v6/dist/dist-DwNW4Hp7.js
|
|
3270
3285
|
var marker$1$2 = "vercel.ai.error";
|
|
3271
3286
|
var symbol$1$2 = Symbol.for(marker$1$2);
|
|
3272
3287
|
var _a$1;
|
|
@@ -3397,6 +3412,20 @@ Error message: ${getErrorMessage$1(cause)}`,
|
|
|
3397
3412
|
};
|
|
3398
3413
|
TransformStream;
|
|
3399
3414
|
var { btoa: btoa$2, atob: atob$4 } = globalThis;
|
|
3415
|
+
var typedArrayTag$1 = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
|
|
3416
|
+
var arrayBufferByteLength$1 = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
|
|
3417
|
+
function isUint8Array$1(value) {
|
|
3418
|
+
return ArrayBuffer.isView(value) && typedArrayTag$1.call(value) === "Uint8Array";
|
|
3419
|
+
}
|
|
3420
|
+
function isArrayBuffer$1(value) {
|
|
3421
|
+
if (typeof value !== "object" || value === null || !("byteLength" in value)) return false;
|
|
3422
|
+
try {
|
|
3423
|
+
arrayBufferByteLength$1.call(value);
|
|
3424
|
+
return true;
|
|
3425
|
+
} catch (e) {
|
|
3426
|
+
return false;
|
|
3427
|
+
}
|
|
3428
|
+
}
|
|
3400
3429
|
var createIdGenerator$1 = ({ prefix, size = 16, alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", separator = "-" } = {}) => {
|
|
3401
3430
|
const generator = () => {
|
|
3402
3431
|
const alphabetLength = alphabet.length;
|
|
@@ -5351,8 +5380,8 @@ function mergeObjects$1(base, overrides) {
|
|
|
5351
5380
|
}
|
|
5352
5381
|
var dataContentSchema = zod_v4.z.union([
|
|
5353
5382
|
zod_v4.z.string(),
|
|
5354
|
-
zod_v4.z.
|
|
5355
|
-
zod_v4.z.
|
|
5383
|
+
zod_v4.z.custom(isUint8Array$1, { message: "Must be a Uint8Array" }),
|
|
5384
|
+
zod_v4.z.custom(isArrayBuffer$1, { message: "Must be an ArrayBuffer" }),
|
|
5356
5385
|
zod_v4.z.custom((value) => {
|
|
5357
5386
|
var _a23, _b;
|
|
5358
5387
|
return (_b = (_a23 = globalThis.Buffer) == null ? void 0 : _a23.isBuffer(value)) != null ? _b : false;
|
|
@@ -6943,7 +6972,7 @@ var doWrap = ({ model, middleware: { transformParams, wrapGenerate, wrapStream,
|
|
|
6943
6972
|
}
|
|
6944
6973
|
};
|
|
6945
6974
|
};
|
|
6946
|
-
// ../../packages/_vendored/ai_v7/dist/dist-
|
|
6975
|
+
// ../../packages/_vendored/ai_v7/dist/dist-CwN1TMYk.js
|
|
6947
6976
|
/**
|
|
6948
6977
|
* Symbol used for identifying AI SDK Error instances.
|
|
6949
6978
|
* Enables checking if an error is an instance of AISDKError across package versions.
|
|
@@ -7075,6 +7104,20 @@ var TypeValidationError = class TypeValidationError extends AISDKError {
|
|
|
7075
7104
|
TransformStream;
|
|
7076
7105
|
new TextDecoder();
|
|
7077
7106
|
const { btoa: btoa$1, atob: atob$2 } = globalThis;
|
|
7107
|
+
const typedArrayTag = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
|
|
7108
|
+
const arrayBufferByteLength = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
|
|
7109
|
+
function isUint8Array(value) {
|
|
7110
|
+
return ArrayBuffer.isView(value) && typedArrayTag.call(value) === "Uint8Array";
|
|
7111
|
+
}
|
|
7112
|
+
function isArrayBuffer(value) {
|
|
7113
|
+
if (typeof value !== "object" || value === null || !("byteLength" in value)) return false;
|
|
7114
|
+
try {
|
|
7115
|
+
arrayBufferByteLength.call(value);
|
|
7116
|
+
return true;
|
|
7117
|
+
} catch {
|
|
7118
|
+
return false;
|
|
7119
|
+
}
|
|
7120
|
+
}
|
|
7078
7121
|
/**
|
|
7079
7122
|
* Creates an ID generator.
|
|
7080
7123
|
* The total length of the ID is the sum of the prefix, separator, and random part length.
|
|
@@ -8423,9 +8466,7 @@ function fixJson(input) {
|
|
|
8423
8466
|
lastValidIndex = i;
|
|
8424
8467
|
stack.pop();
|
|
8425
8468
|
break;
|
|
8426
|
-
default:
|
|
8427
|
-
lastValidIndex = i;
|
|
8428
|
-
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
8469
|
+
default: processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
8429
8470
|
}
|
|
8430
8471
|
break;
|
|
8431
8472
|
case "INSIDE_ARRAY_AFTER_VALUE":
|
|
@@ -9304,12 +9345,13 @@ function processUIMessageStream({ stream, messageMetadataSchema, dataPartSchemas
|
|
|
9304
9345
|
onError?.(new Error(chunk.errorText));
|
|
9305
9346
|
break;
|
|
9306
9347
|
default: if (isDataUIMessageChunk(chunk)) {
|
|
9307
|
-
|
|
9348
|
+
const dataPartSchema = dataPartSchemas?.[chunk.type.slice(5)];
|
|
9349
|
+
if (dataPartSchema != null) {
|
|
9308
9350
|
const partIdx = state.message.parts.findIndex((p) => "id" in p && "data" in p && p.id === chunk.id && p.type === chunk.type);
|
|
9309
9351
|
const actualPartIdx = partIdx >= 0 ? partIdx : state.message.parts.length;
|
|
9310
9352
|
await validateTypes({
|
|
9311
9353
|
value: chunk.data,
|
|
9312
|
-
schema:
|
|
9354
|
+
schema: dataPartSchema,
|
|
9313
9355
|
context: {
|
|
9314
9356
|
field: `message.parts[${actualPartIdx}].data`,
|
|
9315
9357
|
entityName: chunk.type,
|
|
@@ -9595,8 +9637,8 @@ createIdGenerator({
|
|
|
9595
9637
|
});
|
|
9596
9638
|
const fileInlineDataSchema = z.union([
|
|
9597
9639
|
z.string(),
|
|
9598
|
-
z.
|
|
9599
|
-
z.
|
|
9640
|
+
z.custom(isUint8Array, { message: "Must be a Uint8Array" }),
|
|
9641
|
+
z.custom(isArrayBuffer, { message: "Must be an ArrayBuffer" }),
|
|
9600
9642
|
z.custom(isBuffer, { message: "Must be a Buffer" })
|
|
9601
9643
|
]);
|
|
9602
9644
|
const providerReferenceSchema = z.record(z.string(), z.string());
|
|
@@ -10019,7 +10061,7 @@ function convertBackgroundTaskChunkToDataChunk(chunk) {
|
|
|
10019
10061
|
}
|
|
10020
10062
|
};
|
|
10021
10063
|
}
|
|
10022
|
-
function convertMastraChunkToAISDKBase({ chunk, mode = "stream", normalizeWarnings, normalizeUsage, normalizeFinishReason, includeRawFinishReason = false }) {
|
|
10064
|
+
function convertMastraChunkToAISDKBase({ chunk, mode = "stream", normalizeWarnings, normalizeUsage, normalizeFinishReason, includeRawFinishReason = false, includeToolTitle = false }) {
|
|
10023
10065
|
const displayInputTransform = getTransformedToolPayload(chunk.metadata, "display", "input-available");
|
|
10024
10066
|
const displayInputDeltaTransform = getTransformedToolPayload(chunk.metadata, "display", "input-delta");
|
|
10025
10067
|
const displayOutputTransform = getTransformedToolPayload(chunk.metadata, "display", "output-available");
|
|
@@ -10106,6 +10148,7 @@ function convertMastraChunkToAISDKBase({ chunk, mode = "stream", normalizeWarnin
|
|
|
10106
10148
|
providerMetadata: chunk.payload.providerMetadata,
|
|
10107
10149
|
providerExecuted: chunk.payload.providerExecuted,
|
|
10108
10150
|
toolName: chunk.payload.toolName,
|
|
10151
|
+
...includeToolTitle && chunk.payload.title != null ? { title: chunk.payload.title } : {},
|
|
10109
10152
|
input: hasTransformedToolPayload(displayInputTransform) ? displayInputTransform.transformed : chunk.payload.args,
|
|
10110
10153
|
...chunk.payload.observability ? { observability: chunk.payload.observability } : {},
|
|
10111
10154
|
...chunk.payload.toolMetadata ? { toolMetadata: chunk.payload.toolMetadata } : {}
|
|
@@ -10173,6 +10216,7 @@ function convertMastraChunkToAISDKBase({ chunk, mode = "stream", normalizeWarnin
|
|
|
10173
10216
|
type: "tool-input-start",
|
|
10174
10217
|
id: chunk.payload.toolCallId,
|
|
10175
10218
|
toolName: chunk.payload.toolName,
|
|
10219
|
+
...includeToolTitle && chunk.payload.title != null ? { title: chunk.payload.title } : {},
|
|
10176
10220
|
dynamic: !!chunk.payload.dynamic,
|
|
10177
10221
|
providerMetadata: chunk.payload.providerMetadata,
|
|
10178
10222
|
providerExecuted: chunk.payload.providerExecuted,
|
|
@@ -10365,7 +10409,8 @@ function convertMastraChunkToAISDKv6({ chunk, mode = "stream" }) {
|
|
|
10365
10409
|
normalizeWarnings: normalizeV6Warnings,
|
|
10366
10410
|
normalizeUsage: normalizeV6Usage,
|
|
10367
10411
|
normalizeFinishReason: toAISDKFinishReasonV6,
|
|
10368
|
-
includeRawFinishReason: true
|
|
10412
|
+
includeRawFinishReason: true,
|
|
10413
|
+
includeToolTitle: true
|
|
10369
10414
|
});
|
|
10370
10415
|
}
|
|
10371
10416
|
function createToolOutputAncestryEntry(toolCallId, toolName) {
|
|
@@ -10476,10 +10521,12 @@ function convertFullStreamChunkToUIMessageStream({ part, messageMetadataValue, s
|
|
|
10476
10521
|
return;
|
|
10477
10522
|
case "tool-input-start": {
|
|
10478
10523
|
const toolMetadata = part.toolMetadata;
|
|
10524
|
+
const title = part.title;
|
|
10479
10525
|
return {
|
|
10480
10526
|
type: "tool-input-start",
|
|
10481
10527
|
toolCallId: part.id,
|
|
10482
10528
|
toolName: part.toolName,
|
|
10529
|
+
...title != null ? { title } : {},
|
|
10483
10530
|
...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
|
|
10484
10531
|
...part.dynamic != null ? { dynamic: part.dynamic } : {},
|
|
10485
10532
|
...toolMetadata != null ? { toolMetadata } : {}
|
|
@@ -10493,10 +10540,12 @@ function convertFullStreamChunkToUIMessageStream({ part, messageMetadataValue, s
|
|
|
10493
10540
|
case "tool-call": {
|
|
10494
10541
|
const observability = part.observability;
|
|
10495
10542
|
const toolMetadata = part.toolMetadata;
|
|
10543
|
+
const title = part.title;
|
|
10496
10544
|
return {
|
|
10497
10545
|
type: "tool-input-available",
|
|
10498
10546
|
toolCallId: part.toolCallId,
|
|
10499
10547
|
toolName: part.toolName,
|
|
10548
|
+
...title != null ? { title } : {},
|
|
10500
10549
|
input: part.input,
|
|
10501
10550
|
...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
|
|
10502
10551
|
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
|
|
@@ -11961,15 +12010,121 @@ const CREDENTIAL_HEADER_NAMES = /* @__PURE__ */ new Set([
|
|
|
11961
12010
|
"x-goog-api-key",
|
|
11962
12011
|
"cookie"
|
|
11963
12012
|
]);
|
|
11964
|
-
|
|
11965
|
-
|
|
11966
|
-
|
|
12013
|
+
/** Resolves one Editor agent version selector from the route query. */
|
|
12014
|
+
function resolveAgentVersionFromQuery(query, fallback) {
|
|
12015
|
+
const versionId = query("versionId");
|
|
12016
|
+
const status = query("status");
|
|
12017
|
+
if (versionId && status) throw new Error("Query parameters \"versionId\" and \"status\" are mutually exclusive");
|
|
12018
|
+
if (status && status !== "draft" && status !== "published") throw new Error("Query parameter \"status\" must be \"draft\" or \"published\"");
|
|
12019
|
+
return versionId ? { versionId } : status ? { status } : fallback;
|
|
12020
|
+
}
|
|
12021
|
+
/** Wraps a UI message stream in the response implementation for its AI SDK version. */
|
|
12022
|
+
function createChatUIMessageStreamResponse(stream, version) {
|
|
12023
|
+
if (version === "v7") return createUIMessageStreamResponse({ stream });
|
|
12024
|
+
if (version === "v6") return createUIMessageStreamResponse$1({ stream });
|
|
12025
|
+
return createUIMessageStreamResponse$2({ stream });
|
|
12026
|
+
}
|
|
12027
|
+
/** Resolves the configured agent, including Editor-stored overrides and explicit version selectors. */
|
|
12028
|
+
async function resolveChatAgent({ mastra, agentId, agentVersion, requestContext, defaultToPublished = true }) {
|
|
11967
12029
|
const baseAgent = mastra.getAgentById(agentId);
|
|
11968
12030
|
if (!baseAgent) throw new Error(`Agent ${agentId} not found`);
|
|
11969
|
-
let agentObj = baseAgent;
|
|
11970
12031
|
const editorAgent = mastra.getEditor?.()?.agent;
|
|
11971
|
-
if (editorAgent)
|
|
11972
|
-
|
|
12032
|
+
if (editorAgent) {
|
|
12033
|
+
if (!agentVersion && !defaultToPublished) return baseAgent;
|
|
12034
|
+
return editorAgent.applyStoredOverrides(baseAgent, agentVersion ?? { status: "published" }, requestContext);
|
|
12035
|
+
}
|
|
12036
|
+
if (agentVersion) return mastra.getAgentById(agentId, agentVersion);
|
|
12037
|
+
return baseAgent;
|
|
12038
|
+
}
|
|
12039
|
+
const MASTRA_AUTH_MODE_KEY = "mastra__authMode";
|
|
12040
|
+
/** Selects the FGA provider for the request's trusted authentication mode. */
|
|
12041
|
+
function resolveChatFGAProvider(mastra, requestContext) {
|
|
12042
|
+
const studioFga = mastra.getStudio()?.fga;
|
|
12043
|
+
const serverFga = mastra.getServer()?.fga;
|
|
12044
|
+
if (requestContext?.get(MASTRA_AUTH_MODE_KEY) === "studio" && studioFga) return studioFga;
|
|
12045
|
+
if (serverFga) return serverFga;
|
|
12046
|
+
return studioFga ? null : void 0;
|
|
12047
|
+
}
|
|
12048
|
+
/** Authorizes replay using the same resource ownership and thread FGA rules as run observation. */
|
|
12049
|
+
async function authorizeChatRunObservation({ mastra, agent, runRecord, requestContext }) {
|
|
12050
|
+
const resourceId = requestContext?.get(_mastra_core_request_context.MASTRA_RESOURCE_ID_KEY);
|
|
12051
|
+
const server = mastra.getServer();
|
|
12052
|
+
const fgaProvider = resolveChatFGAProvider(mastra, requestContext);
|
|
12053
|
+
if (fgaProvider === null) return false;
|
|
12054
|
+
if (typeof (server?.auth)?.mapUserToResourceId === "function") {
|
|
12055
|
+
if (resourceId) {
|
|
12056
|
+
if (runRecord.resourceId !== resourceId) return false;
|
|
12057
|
+
} else if (!fgaProvider || !runRecord.threadId) return false;
|
|
12058
|
+
} else if (runRecord.resourceId && runRecord.resourceId !== resourceId) return false;
|
|
12059
|
+
if (!runRecord.threadId) return true;
|
|
12060
|
+
requestContext?.set(_mastra_core_request_context.MASTRA_THREAD_ID_KEY, runRecord.threadId);
|
|
12061
|
+
const thread = await (await agent.getMemory({ requestContext }))?.getThreadById({ threadId: runRecord.threadId });
|
|
12062
|
+
if (resourceId && thread?.resourceId && thread.resourceId !== resourceId) return false;
|
|
12063
|
+
if (fgaProvider) {
|
|
12064
|
+
const user = requestContext?.get("user");
|
|
12065
|
+
if (!user || typeof user !== "object") return false;
|
|
12066
|
+
try {
|
|
12067
|
+
await _mastra_core_memory.MastraMemory.checkThreadFGA({
|
|
12068
|
+
mastra,
|
|
12069
|
+
user,
|
|
12070
|
+
threadId: runRecord.threadId,
|
|
12071
|
+
resourceId: thread?.resourceId ?? runRecord.resourceId ?? resourceId,
|
|
12072
|
+
requestContext,
|
|
12073
|
+
fgaProvider
|
|
12074
|
+
});
|
|
12075
|
+
} catch {
|
|
12076
|
+
return false;
|
|
12077
|
+
}
|
|
12078
|
+
}
|
|
12079
|
+
return true;
|
|
12080
|
+
}
|
|
12081
|
+
/** Converts a Mastra agent stream to the requested AI SDK UI message stream version. */
|
|
12082
|
+
function createChatUIMessageStream({ result, messages, version, lastMessageId, sendStart, sendFinish, sendReasoning, sendSources, includeSubAgentMetadata, experimentalTransform, onError, messageMetadata }) {
|
|
12083
|
+
if (version === "v6" || version === "v7") return (version === "v7" ? createUIMessageStream : createUIMessageStream$1)({
|
|
12084
|
+
originalMessages: messages,
|
|
12085
|
+
execute: async ({ writer }) => {
|
|
12086
|
+
for await (const part of toAISdkStream(result, {
|
|
12087
|
+
from: "agent",
|
|
12088
|
+
version: "v6",
|
|
12089
|
+
lastMessageId,
|
|
12090
|
+
sendStart,
|
|
12091
|
+
sendFinish,
|
|
12092
|
+
sendReasoning,
|
|
12093
|
+
sendSources,
|
|
12094
|
+
includeSubAgentMetadata,
|
|
12095
|
+
experimentalTransform,
|
|
12096
|
+
onError,
|
|
12097
|
+
messageMetadata
|
|
12098
|
+
})) writer.write(part);
|
|
12099
|
+
}
|
|
12100
|
+
});
|
|
12101
|
+
return createUIMessageStream$2({
|
|
12102
|
+
originalMessages: messages,
|
|
12103
|
+
execute: async ({ writer }) => {
|
|
12104
|
+
for await (const part of toAISdkStream(result, {
|
|
12105
|
+
from: "agent",
|
|
12106
|
+
lastMessageId,
|
|
12107
|
+
sendStart,
|
|
12108
|
+
sendFinish,
|
|
12109
|
+
sendReasoning,
|
|
12110
|
+
sendSources,
|
|
12111
|
+
includeSubAgentMetadata,
|
|
12112
|
+
experimentalTransform,
|
|
12113
|
+
onError,
|
|
12114
|
+
messageMetadata
|
|
12115
|
+
})) writer.write(part);
|
|
12116
|
+
}
|
|
12117
|
+
});
|
|
12118
|
+
}
|
|
12119
|
+
async function handleChatStream({ mastra, agentId, agentVersion, params, defaultOptions, experimentalTransform, version = "v5", sendStart = true, sendFinish = true, sendReasoning = false, sendSources = false, includeSubAgentMetadata = true, onError, messageMetadata }) {
|
|
12120
|
+
const { messages, resumeData, runId, requestContext, trigger, ...rest } = params;
|
|
12121
|
+
if (resumeData && !runId) throw new Error("runId is required when resumeData is provided");
|
|
12122
|
+
const agentObj = await resolveChatAgent({
|
|
12123
|
+
mastra,
|
|
12124
|
+
agentId,
|
|
12125
|
+
agentVersion,
|
|
12126
|
+
requestContext
|
|
12127
|
+
});
|
|
11973
12128
|
if (!Array.isArray(messages)) throw new Error("Messages must be an array of UIMessage objects");
|
|
11974
12129
|
let lastMessageId;
|
|
11975
12130
|
let messagesToSend = messages;
|
|
@@ -12019,101 +12174,31 @@ async function handleChatStream({ mastra, agentId, agentVersion, params, default
|
|
|
12019
12174
|
messageMetadata
|
|
12020
12175
|
});
|
|
12021
12176
|
}
|
|
12022
|
-
|
|
12023
|
-
|
|
12024
|
-
|
|
12025
|
-
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12029
|
-
|
|
12030
|
-
|
|
12031
|
-
|
|
12032
|
-
|
|
12033
|
-
|
|
12034
|
-
|
|
12035
|
-
|
|
12036
|
-
|
|
12037
|
-
|
|
12038
|
-
|
|
12039
|
-
|
|
12040
|
-
|
|
12041
|
-
experimentalTransform: effectiveExperimentalTransform,
|
|
12042
|
-
onError,
|
|
12043
|
-
messageMetadata
|
|
12044
|
-
})) writer.write(part);
|
|
12045
|
-
}
|
|
12046
|
-
});
|
|
12047
|
-
return createUIMessageStream$2({
|
|
12048
|
-
originalMessages: messages,
|
|
12049
|
-
execute: async ({ writer }) => {
|
|
12050
|
-
for await (const part of toAISdkStream(result, {
|
|
12051
|
-
from: "agent",
|
|
12052
|
-
lastMessageId,
|
|
12053
|
-
sendStart,
|
|
12054
|
-
sendFinish,
|
|
12055
|
-
sendReasoning,
|
|
12056
|
-
sendSources,
|
|
12057
|
-
includeSubAgentMetadata,
|
|
12058
|
-
experimentalTransform: effectiveExperimentalTransform,
|
|
12059
|
-
onError,
|
|
12060
|
-
messageMetadata
|
|
12061
|
-
})) writer.write(part);
|
|
12062
|
-
}
|
|
12177
|
+
return createChatUIMessageStream({
|
|
12178
|
+
result: resumeData ? structuredOutput ? await agentObj.resumeStream(resumeData, {
|
|
12179
|
+
...baseOptions,
|
|
12180
|
+
structuredOutput
|
|
12181
|
+
}) : await agentObj.resumeStream(resumeData, baseOptions) : structuredOutput ? await agentObj.stream(messagesToSend, {
|
|
12182
|
+
...baseOptions,
|
|
12183
|
+
structuredOutput
|
|
12184
|
+
}) : await agentObj.stream(messagesToSend, baseOptions),
|
|
12185
|
+
messages,
|
|
12186
|
+
version,
|
|
12187
|
+
lastMessageId,
|
|
12188
|
+
sendStart,
|
|
12189
|
+
sendFinish,
|
|
12190
|
+
sendReasoning,
|
|
12191
|
+
sendSources,
|
|
12192
|
+
includeSubAgentMetadata,
|
|
12193
|
+
experimentalTransform: effectiveExperimentalTransform,
|
|
12194
|
+
onError,
|
|
12195
|
+
messageMetadata
|
|
12063
12196
|
});
|
|
12064
12197
|
}
|
|
12065
|
-
|
|
12066
|
-
* Creates a chat route handler for streaming agent conversations using the AI SDK format.
|
|
12067
|
-
*
|
|
12068
|
-
* This function registers an HTTP POST endpoint that accepts messages, executes an agent, and streams the response back to the client in AI SDK-compatible format.
|
|
12069
|
-
*
|
|
12070
|
-
* @param {chatRouteOptions} options - Configuration options for the chat route
|
|
12071
|
-
* @param {string} [options.path='/chat/:agentId'] - The route path. Include `:agentId` for dynamic routing
|
|
12072
|
-
* @param {string} [options.agent] - Fixed agent ID when not using dynamic routing
|
|
12073
|
-
* @param {AgentExecutionOptions} [options.defaultOptions] - Default options passed to agent execution
|
|
12074
|
-
* @param {boolean} [options.sendStart=true] - Whether to send start events in the stream
|
|
12075
|
-
* @param {boolean} [options.sendFinish=true] - Whether to send finish events in the stream
|
|
12076
|
-
* @param {boolean} [options.sendReasoning=false] - Whether to include reasoning steps in the stream
|
|
12077
|
-
* @param {boolean} [options.sendSources=false] - Whether to include source citations in the stream
|
|
12078
|
-
* @param {boolean} [options.includeSubAgentMetadata=true] - Whether to emit `data-tool-agent` / `data-tool-agent-step` parts with sub-agent progress
|
|
12079
|
-
* @param {number} [options.heartbeatMs] - Target interval for periodic SSE comment heartbeats. Already-buffered source events and stream lifecycle signals take priority. Values up to 0 disable heartbeats. `NaN`, positive infinity, and values above 2,147,483,647 throw a `RangeError`.
|
|
12080
|
-
* @param {(error: unknown) => string} [options.onError] - Custom error serializer streamed to the client. When omitted, errors are passed through a default serializer that strips sensitive fields (e.g. `APICallError.requestBodyValues`, which holds the system prompt) before they reach the client.
|
|
12081
|
-
* @param {Function} [options.messageMetadata] - Maps stream parts to metadata attached to AI SDK start and finish message parts. Receives `{ part, traceId }`.
|
|
12082
|
-
*
|
|
12083
|
-
* @returns {ReturnType<typeof registerApiRoute>} A registered API route handler
|
|
12084
|
-
*
|
|
12085
|
-
* @throws {Error} When path doesn't include `:agentId` and no fixed agent is specified
|
|
12086
|
-
* @throws {RangeError} When `heartbeatMs` is `NaN`, positive infinity, or greater than 2,147,483,647
|
|
12087
|
-
* @throws {Error} When agent ID is missing at runtime
|
|
12088
|
-
* @throws {Error} When specified agent is not found in Mastra instance
|
|
12089
|
-
*
|
|
12090
|
-
* @example
|
|
12091
|
-
* // Dynamic agent routing
|
|
12092
|
-
* chatRoute({
|
|
12093
|
-
* path: '/chat/:agentId',
|
|
12094
|
-
* });
|
|
12095
|
-
*
|
|
12096
|
-
* @example
|
|
12097
|
-
* // Fixed agent with custom path
|
|
12098
|
-
* chatRoute({
|
|
12099
|
-
* path: '/api/support-chat',
|
|
12100
|
-
* agent: 'support-agent',
|
|
12101
|
-
* defaultOptions: {
|
|
12102
|
-
* maxSteps: 5,
|
|
12103
|
-
* },
|
|
12104
|
-
* });
|
|
12105
|
-
*
|
|
12106
|
-
* @remarks
|
|
12107
|
-
* - The route handler expects a JSON body with a `messages` array
|
|
12108
|
-
* - Messages should follow the format: `{ role: 'user' | 'assistant' | 'system', content: string }`
|
|
12109
|
-
* - The response is a Server-Sent Events (SSE) stream compatible with the selected AI SDK version
|
|
12110
|
-
* - If both `agent` and `:agentId` are present, a warning is logged and the fixed `agent` takes precedence
|
|
12111
|
-
* - Request context from the incoming request overrides `defaultOptions.requestContext` if both are present
|
|
12112
|
-
*/
|
|
12113
|
-
function chatRoute({ path = "/chat/:agentId", agent, defaultOptions, experimentalTransform, version = "v5", agentVersion, sendStart = true, sendFinish = true, sendReasoning = false, sendSources = false, includeSubAgentMetadata = true, heartbeatMs, onError, messageMetadata }) {
|
|
12198
|
+
function chatRoute({ path = "/chat/:agentId", agent, defaultOptions, experimentalTransform, version = "v5", agentVersion, resumable = false, sendStart = true, sendFinish = true, sendReasoning = false, sendSources = false, includeSubAgentMetadata = true, heartbeatMs, onError, messageMetadata }) {
|
|
12114
12199
|
if (!agent && !path.includes("/:agentId")) throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
12115
12200
|
assertValidHeartbeatMs(heartbeatMs);
|
|
12116
|
-
|
|
12201
|
+
const postRoute = (0, _mastra_core_server.registerApiRoute)(path, {
|
|
12117
12202
|
method: "POST",
|
|
12118
12203
|
openapi: {
|
|
12119
12204
|
summary: "Chat with an agent",
|
|
@@ -12216,22 +12301,29 @@ function chatRoute({ path = "/chat/:agentId", agent, defaultOptions, experimenta
|
|
|
12216
12301
|
let agentToUse = agent;
|
|
12217
12302
|
if (!agent) agentToUse = c.req.param("agentId");
|
|
12218
12303
|
if (c.req.param("agentId") && agent) mastra.getLogger()?.warn(`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`);
|
|
12219
|
-
const
|
|
12304
|
+
const configuredRequestContext = contextRequestContext || defaultOptions?.requestContext || params.requestContext;
|
|
12305
|
+
const effectiveRequestContext = resumable && params.id ? new _mastra_core_request_context.RequestContext(configuredRequestContext ? configuredRequestContext.entries() : void 0) : configuredRequestContext;
|
|
12306
|
+
if (resumable && params.id) effectiveRequestContext.set(_mastra_core_request_context.MASTRA_THREAD_ID_KEY, params.id);
|
|
12220
12307
|
if (contextRequestContext && defaultOptions?.requestContext || contextRequestContext && params.requestContext || defaultOptions?.requestContext && params.requestContext) mastra.getLogger()?.warn(`Multiple "requestContext" sources provided. Using priority: middleware > route options > body.`);
|
|
12221
12308
|
if (!agentToUse) throw new Error("Agent ID is required");
|
|
12222
|
-
const
|
|
12223
|
-
const
|
|
12224
|
-
|
|
12225
|
-
|
|
12226
|
-
|
|
12309
|
+
const effectiveAgentVersion = resolveAgentVersionFromQuery((name) => c.req.query(name), agentVersion);
|
|
12310
|
+
const resumableResourceId = (contextRequestContext || defaultOptions?.requestContext)?.get(_mastra_core_request_context.MASTRA_RESOURCE_ID_KEY);
|
|
12311
|
+
const bodyResourceId = typeof params.memory === "object" ? params.memory?.resource : void 0;
|
|
12312
|
+
const bodyRequestContextResourceId = params.requestContext?.get(_mastra_core_request_context.MASTRA_RESOURCE_ID_KEY);
|
|
12313
|
+
if (resumable && (bodyResourceId || bodyRequestContextResourceId) && !resumableResourceId) return c.json({ error: "Resumable chat resource IDs must be set by authentication middleware or route request context, not in the request body." }, 400);
|
|
12227
12314
|
const handlerOptions = {
|
|
12228
12315
|
mastra,
|
|
12229
12316
|
agentId: agentToUse,
|
|
12230
|
-
agentVersion:
|
|
12317
|
+
agentVersion: effectiveAgentVersion,
|
|
12231
12318
|
params: {
|
|
12232
12319
|
...params,
|
|
12320
|
+
...resumable && params.id ? { memory: {
|
|
12321
|
+
...typeof params.memory === "object" ? params.memory : {},
|
|
12322
|
+
...resumableResourceId ? { resource: resumableResourceId } : {},
|
|
12323
|
+
thread: params.id
|
|
12324
|
+
} } : {},
|
|
12233
12325
|
requestContext: effectiveRequestContext,
|
|
12234
|
-
abortSignal: c.req.raw.signal
|
|
12326
|
+
...!resumable && { abortSignal: c.req.raw.signal }
|
|
12235
12327
|
},
|
|
12236
12328
|
defaultOptions,
|
|
12237
12329
|
experimentalTransform,
|
|
@@ -12242,24 +12334,108 @@ function chatRoute({ path = "/chat/:agentId", agent, defaultOptions, experimenta
|
|
|
12242
12334
|
includeSubAgentMetadata,
|
|
12243
12335
|
onError
|
|
12244
12336
|
};
|
|
12245
|
-
|
|
12246
|
-
if (version === "v7") response = createUIMessageStreamResponse({ stream: await handleChatStream({
|
|
12337
|
+
return withSseHeartbeat(createChatUIMessageStreamResponse(version === "v7" ? await handleChatStream({
|
|
12247
12338
|
...handlerOptions,
|
|
12248
12339
|
version: "v7",
|
|
12249
12340
|
messageMetadata
|
|
12250
|
-
})
|
|
12251
|
-
else if (version === "v6") response = createUIMessageStreamResponse$1({ stream: await handleChatStream({
|
|
12341
|
+
}) : version === "v6" ? await handleChatStream({
|
|
12252
12342
|
...handlerOptions,
|
|
12253
12343
|
version: "v6",
|
|
12254
12344
|
messageMetadata
|
|
12255
|
-
})
|
|
12256
|
-
else response = createUIMessageStreamResponse$2({ stream: await handleChatStream({
|
|
12345
|
+
}) : await handleChatStream({
|
|
12257
12346
|
...handlerOptions,
|
|
12258
12347
|
messageMetadata
|
|
12259
|
-
})
|
|
12260
|
-
return withSseHeartbeat(response, heartbeatMs);
|
|
12348
|
+
}), version), heartbeatMs);
|
|
12261
12349
|
}
|
|
12262
12350
|
});
|
|
12351
|
+
if (!resumable) return postRoute;
|
|
12352
|
+
const reconnectPath = `${path.replace(/\/$/, "")}/:chatId/stream`;
|
|
12353
|
+
return [postRoute, (0, _mastra_core_server.registerApiRoute)(reconnectPath, {
|
|
12354
|
+
method: "GET",
|
|
12355
|
+
openapi: {
|
|
12356
|
+
summary: "Reconnect to an agent chat stream",
|
|
12357
|
+
description: "Replay and follow the active agent run for an AI SDK chat",
|
|
12358
|
+
tags: ["ai-sdk"],
|
|
12359
|
+
parameters: [
|
|
12360
|
+
...path.includes(":agentId") ? [{
|
|
12361
|
+
name: "agentId",
|
|
12362
|
+
in: "path",
|
|
12363
|
+
required: true,
|
|
12364
|
+
schema: { type: "string" }
|
|
12365
|
+
}] : [],
|
|
12366
|
+
{
|
|
12367
|
+
name: "chatId",
|
|
12368
|
+
in: "path",
|
|
12369
|
+
required: true,
|
|
12370
|
+
schema: { type: "string" }
|
|
12371
|
+
},
|
|
12372
|
+
{
|
|
12373
|
+
name: "versionId",
|
|
12374
|
+
in: "query",
|
|
12375
|
+
required: false,
|
|
12376
|
+
schema: { type: "string" }
|
|
12377
|
+
},
|
|
12378
|
+
{
|
|
12379
|
+
name: "status",
|
|
12380
|
+
in: "query",
|
|
12381
|
+
required: false,
|
|
12382
|
+
schema: {
|
|
12383
|
+
type: "string",
|
|
12384
|
+
enum: ["draft", "published"]
|
|
12385
|
+
}
|
|
12386
|
+
}
|
|
12387
|
+
],
|
|
12388
|
+
responses: {
|
|
12389
|
+
"200": {
|
|
12390
|
+
description: "Replayable streaming response from the agent run",
|
|
12391
|
+
content: { "text/plain": { schema: { type: "string" } } }
|
|
12392
|
+
},
|
|
12393
|
+
"204": { description: "The chat has no active resumable run" },
|
|
12394
|
+
"404": { description: "The active run does not belong to the current resource or thread" }
|
|
12395
|
+
}
|
|
12396
|
+
},
|
|
12397
|
+
handler: async (c) => {
|
|
12398
|
+
const mastra = c.get("mastra");
|
|
12399
|
+
const requestContext = c.get("requestContext") || (defaultOptions?.requestContext ? new _mastra_core_request_context.RequestContext(defaultOptions.requestContext.entries()) : void 0);
|
|
12400
|
+
const agentToUse = agent ?? c.req.param("agentId");
|
|
12401
|
+
const chatId = c.req.param("chatId");
|
|
12402
|
+
if (!agentToUse || !chatId) return new Response(null, { status: 204 });
|
|
12403
|
+
const agentObj = await resolveChatAgent({
|
|
12404
|
+
mastra,
|
|
12405
|
+
agentId: agentToUse,
|
|
12406
|
+
agentVersion: resolveAgentVersionFromQuery((name) => c.req.query(name), agentVersion),
|
|
12407
|
+
requestContext,
|
|
12408
|
+
defaultToPublished: false
|
|
12409
|
+
});
|
|
12410
|
+
const resourceId = requestContext?.get(_mastra_core_request_context.MASTRA_RESOURCE_ID_KEY);
|
|
12411
|
+
const runId = agentObj.getActiveThreadRunId({
|
|
12412
|
+
threadId: chatId,
|
|
12413
|
+
resourceId
|
|
12414
|
+
});
|
|
12415
|
+
if (!runId) return new Response(null, { status: 204 });
|
|
12416
|
+
const runRecord = await agentObj.__getRunRecord(runId);
|
|
12417
|
+
if (!runRecord || runRecord.status === "finished") return new Response(null, { status: 204 });
|
|
12418
|
+
if (!await authorizeChatRunObservation({
|
|
12419
|
+
mastra,
|
|
12420
|
+
agent: agentObj,
|
|
12421
|
+
runRecord,
|
|
12422
|
+
requestContext
|
|
12423
|
+
})) return new Response(null, { status: 404 });
|
|
12424
|
+
return withSseHeartbeat(createChatUIMessageStreamResponse(createChatUIMessageStream({
|
|
12425
|
+
result: (await agentObj.observe(runId)).output,
|
|
12426
|
+
messages: [],
|
|
12427
|
+
version,
|
|
12428
|
+
sendStart,
|
|
12429
|
+
sendFinish,
|
|
12430
|
+
sendReasoning,
|
|
12431
|
+
sendSources,
|
|
12432
|
+
includeSubAgentMetadata,
|
|
12433
|
+
experimentalTransform,
|
|
12434
|
+
onError,
|
|
12435
|
+
messageMetadata
|
|
12436
|
+
}), version), heartbeatMs);
|
|
12437
|
+
}
|
|
12438
|
+
})];
|
|
12263
12439
|
}
|
|
12264
12440
|
// src/workflow-route.ts
|
|
12265
12441
|
async function handleWorkflowStream({ mastra, workflowId, params, version = "v5", includeTextStreamParts = true, sendReasoning = false, sendSources = false }) {
|