@mastra/server 1.23.0-alpha.7 → 1.23.0-alpha.8
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/_types/@internal_core/dist/index.d.ts +3 -1
- package/dist/{chunk-VMA5TY4H.cjs → chunk-2KF7EWOT.cjs} +314 -190
- package/dist/chunk-2KF7EWOT.cjs.map +1 -0
- package/dist/{chunk-VDHLJ6OD.cjs → chunk-3D2IBKF6.cjs} +4 -4
- package/dist/{chunk-VDHLJ6OD.cjs.map → chunk-3D2IBKF6.cjs.map} +1 -1
- package/dist/{chunk-B2OFZDJQ.cjs → chunk-4HHL4C6V.cjs} +13 -13
- package/dist/{chunk-B2OFZDJQ.cjs.map → chunk-4HHL4C6V.cjs.map} +1 -1
- package/dist/{chunk-QBXHK27Z.js → chunk-52JPWUPI.js} +3 -3
- package/dist/{chunk-QBXHK27Z.js.map → chunk-52JPWUPI.js.map} +1 -1
- package/dist/{chunk-VXBTV6LH.js → chunk-6MMZVEFC.js} +8 -3
- package/dist/chunk-6MMZVEFC.js.map +1 -0
- package/dist/{chunk-4EA2CJG4.js → chunk-A7BSSXDD.js} +3 -3
- package/dist/{chunk-4EA2CJG4.js.map → chunk-A7BSSXDD.js.map} +1 -1
- package/dist/{chunk-LRJ4LQBR.cjs → chunk-F4TKO2VD.cjs} +6 -6
- package/dist/{chunk-LRJ4LQBR.cjs.map → chunk-F4TKO2VD.cjs.map} +1 -1
- package/dist/{chunk-ZVSNY6VT.js → chunk-GMZFJZO4.js} +4 -3
- package/dist/chunk-GMZFJZO4.js.map +1 -0
- package/dist/{chunk-INKHQLVW.js → chunk-HKALCAKJ.js} +3 -3
- package/dist/{chunk-INKHQLVW.js.map → chunk-HKALCAKJ.js.map} +1 -1
- package/dist/{chunk-VOCNBP5Q.cjs → chunk-HUWHY7SC.cjs} +4 -3
- package/dist/chunk-HUWHY7SC.cjs.map +1 -0
- package/dist/{chunk-U6CWJTHH.cjs → chunk-I5FEAHJM.cjs} +29 -29
- package/dist/{chunk-U6CWJTHH.cjs.map → chunk-I5FEAHJM.cjs.map} +1 -1
- package/dist/{chunk-UVK6YXEL.cjs → chunk-IOGPYY6Z.cjs} +77 -75
- package/dist/chunk-IOGPYY6Z.cjs.map +1 -0
- package/dist/{chunk-5C535ZJO.cjs → chunk-KG5RK6PO.cjs} +4 -4
- package/dist/{chunk-5C535ZJO.cjs.map → chunk-KG5RK6PO.cjs.map} +1 -1
- package/dist/{chunk-AS6EW5BK.js → chunk-KYVNA2EN.js} +3 -3
- package/dist/{chunk-AS6EW5BK.js.map → chunk-KYVNA2EN.js.map} +1 -1
- package/dist/{chunk-FLGIOJA5.js → chunk-MO3ZKWYY.js} +80 -10
- package/dist/chunk-MO3ZKWYY.js.map +1 -0
- package/dist/{chunk-6Q6OH2XV.js → chunk-OOZYQF4U.js} +3 -2
- package/dist/chunk-OOZYQF4U.js.map +1 -0
- package/dist/{chunk-IFEMVRLI.cjs → chunk-OV2TAONK.cjs} +3 -2
- package/dist/chunk-OV2TAONK.cjs.map +1 -0
- package/dist/{chunk-DALTY2FC.js → chunk-P72YX3EY.js} +3 -3
- package/dist/{chunk-DALTY2FC.js.map → chunk-P72YX3EY.js.map} +1 -1
- package/dist/{chunk-SE5M6GC6.js → chunk-T4RZ3X4I.js} +4 -4
- package/dist/{chunk-SE5M6GC6.js.map → chunk-T4RZ3X4I.js.map} +1 -1
- package/dist/{chunk-XUIA3SIB.js → chunk-TBJHTT7B.js} +6 -4
- package/dist/chunk-TBJHTT7B.js.map +1 -0
- package/dist/{chunk-SZ2FSVP5.js → chunk-TX3IIDZN.js} +301 -177
- package/dist/chunk-TX3IIDZN.js.map +1 -0
- package/dist/{chunk-PNCR2M6Y.cjs → chunk-UHWF2UKO.cjs} +80 -10
- package/dist/chunk-UHWF2UKO.cjs.map +1 -0
- package/dist/{chunk-HHGZGERI.cjs → chunk-UI3SS2JO.cjs} +4 -4
- package/dist/{chunk-HHGZGERI.cjs.map → chunk-UI3SS2JO.cjs.map} +1 -1
- package/dist/{chunk-RNC7VEI6.cjs → chunk-WKBMLKBC.cjs} +8 -3
- package/dist/chunk-WKBMLKBC.cjs.map +1 -0
- package/dist/{chunk-SHWDJY3O.js → chunk-WZ6XPWVK.js} +3 -3
- package/dist/{chunk-SHWDJY3O.js.map → chunk-WZ6XPWVK.js.map} +1 -1
- package/dist/{chunk-SCEPRIRF.cjs → chunk-X2IFWP5E.cjs} +5 -5
- package/dist/{chunk-SCEPRIRF.cjs.map → chunk-X2IFWP5E.cjs.map} +1 -1
- package/dist/{chunk-DGS7O6Q7.js → chunk-XO5PXSFC.js} +3 -3
- package/dist/{chunk-DGS7O6Q7.js.map → chunk-XO5PXSFC.js.map} +1 -1
- package/dist/{chunk-LV4FV46S.cjs → chunk-ZWKZK4WO.cjs} +6 -6
- package/dist/{chunk-LV4FV46S.cjs.map → chunk-ZWKZK4WO.cjs.map} +1 -1
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/{observational-memory-JQ34KLFS-CNLSTXO3.cjs → observational-memory-OVRHDQRG-OCHV6RZ7.cjs} +26 -26
- package/dist/{observational-memory-JQ34KLFS-CNLSTXO3.cjs.map → observational-memory-OVRHDQRG-OCHV6RZ7.cjs.map} +1 -1
- package/dist/{observational-memory-JQ34KLFS-KLWGOGP4.js → observational-memory-OVRHDQRG-RC3PRA65.js} +3 -3
- package/dist/{observational-memory-JQ34KLFS-KLWGOGP4.js.map → observational-memory-OVRHDQRG-RC3PRA65.js.map} +1 -1
- package/dist/server/handlers/a2a.cjs +9 -9
- package/dist/server/handlers/a2a.js +1 -1
- package/dist/server/handlers/agent-builder.cjs +16 -16
- package/dist/server/handlers/agent-builder.js +1 -1
- package/dist/server/handlers/agents.cjs +35 -35
- package/dist/server/handlers/agents.d.ts.map +1 -1
- package/dist/server/handlers/agents.js +1 -1
- package/dist/server/handlers/conversations.cjs +5 -5
- package/dist/server/handlers/conversations.js +1 -1
- package/dist/server/handlers/memory.cjs +27 -27
- package/dist/server/handlers/memory.d.ts +4 -0
- package/dist/server/handlers/memory.d.ts.map +1 -1
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers/observability-new-endpoints.cjs +27 -27
- package/dist/server/handlers/observability-new-endpoints.js +1 -1
- package/dist/server/handlers/observability.cjs +33 -33
- package/dist/server/handlers/observability.js +2 -2
- package/dist/server/handlers/responses.cjs +4 -4
- package/dist/server/handlers/responses.js +1 -1
- package/dist/server/handlers/scores.cjs +7 -7
- package/dist/server/handlers/scores.js +1 -1
- package/dist/server/handlers/tools.cjs +6 -6
- package/dist/server/handlers/tools.js +1 -1
- package/dist/server/handlers/voice.cjs +8 -8
- package/dist/server/handlers/voice.js +1 -1
- package/dist/server/handlers.cjs +20 -20
- package/dist/server/handlers.js +10 -10
- package/dist/server/schemas/index.cjs +50 -50
- package/dist/server/schemas/index.js +2 -2
- package/dist/server/schemas/memory.d.ts +2 -0
- package/dist/server/schemas/memory.d.ts.map +1 -1
- package/dist/server/server-adapter/index.cjs +129 -129
- package/dist/server/server-adapter/index.js +11 -11
- package/dist/server/server-adapter/routes/memory.d.ts +4 -0
- package/dist/server/server-adapter/routes/memory.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/chunk-6Q6OH2XV.js.map +0 -1
- package/dist/chunk-FLGIOJA5.js.map +0 -1
- package/dist/chunk-IFEMVRLI.cjs.map +0 -1
- package/dist/chunk-PNCR2M6Y.cjs.map +0 -1
- package/dist/chunk-RNC7VEI6.cjs.map +0 -1
- package/dist/chunk-SZ2FSVP5.js.map +0 -1
- package/dist/chunk-UVK6YXEL.cjs.map +0 -1
- package/dist/chunk-VMA5TY4H.cjs.map +0 -1
- package/dist/chunk-VOCNBP5Q.cjs.map +0 -1
- package/dist/chunk-VXBTV6LH.js.map +0 -1
- package/dist/chunk-XUIA3SIB.js.map +0 -1
- package/dist/chunk-ZVSNY6VT.js.map +0 -1
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var chunk5N66PU3H_cjs = require('./chunk-5N66PU3H.cjs');
|
|
4
4
|
var chunkEXKS4QPI_cjs = require('./chunk-EXKS4QPI.cjs');
|
|
5
5
|
var chunkFPURK3UW_cjs = require('./chunk-FPURK3UW.cjs');
|
|
6
|
-
var
|
|
6
|
+
var chunkUHWF2UKO_cjs = require('./chunk-UHWF2UKO.cjs');
|
|
7
7
|
var chunkR2ODPDKE_cjs = require('./chunk-R2ODPDKE.cjs');
|
|
8
8
|
var chunkQKYHAQLX_cjs = require('./chunk-QKYHAQLX.cjs');
|
|
9
9
|
var chunkDOHUOYZS_cjs = require('./chunk-DOHUOYZS.cjs');
|
|
@@ -22,6 +22,7 @@ var zod = require('zod');
|
|
|
22
22
|
var z4 = require('zod/v4');
|
|
23
23
|
var features = require('@mastra/core/features');
|
|
24
24
|
var memory = require('@mastra/core/memory');
|
|
25
|
+
var observability = require('@mastra/core/observability');
|
|
25
26
|
var utils = require('@mastra/core/utils');
|
|
26
27
|
var tools = require('@mastra/core/tools');
|
|
27
28
|
var schema = require('@mastra/core/schema');
|
|
@@ -16392,8 +16393,8 @@ function formatTimestamp(date) {
|
|
|
16392
16393
|
return date.toISOString().replace("T", " ").replace(/\.\d{3}Z$/, "Z");
|
|
16393
16394
|
}
|
|
16394
16395
|
function truncateByTokens(text42, maxTokens, hint) {
|
|
16395
|
-
if (
|
|
16396
|
-
const truncated =
|
|
16396
|
+
if (chunkUHWF2UKO_cjs.estimateTokenCount(text42) <= maxTokens) return { text: text42, wasTruncated: false };
|
|
16397
|
+
const truncated = chunkUHWF2UKO_cjs.truncateStringByTokens(text42, maxTokens);
|
|
16397
16398
|
const suffix = hint ? ` [${hint} for more]` : "";
|
|
16398
16399
|
return { text: truncated + suffix, wasTruncated: true };
|
|
16399
16400
|
}
|
|
@@ -16426,11 +16427,11 @@ function formatMessageParts(msg, detail) {
|
|
|
16426
16427
|
} else if (partType === "tool-invocation") {
|
|
16427
16428
|
const inv = part.toolInvocation;
|
|
16428
16429
|
if (inv.state === "result") {
|
|
16429
|
-
const { value: resultValue } =
|
|
16430
|
+
const { value: resultValue } = chunkUHWF2UKO_cjs.resolveToolResultValue(
|
|
16430
16431
|
part,
|
|
16431
16432
|
inv.result
|
|
16432
16433
|
);
|
|
16433
|
-
const resultStr =
|
|
16434
|
+
const resultStr = chunkUHWF2UKO_cjs.formatToolResultForObserver(resultValue, { maxTokens: HIGH_DETAIL_TOOL_RESULT_TOKENS });
|
|
16434
16435
|
const fullText = `[Tool Result: ${inv.toolName}]
|
|
16435
16436
|
${resultStr}`;
|
|
16436
16437
|
parts.push(makePart(msg, i, "tool-result", fullText, detail));
|
|
@@ -16513,9 +16514,9 @@ function expandPriority(part) {
|
|
|
16513
16514
|
}
|
|
16514
16515
|
function renderFormattedParts(parts, timestamps, options) {
|
|
16515
16516
|
const text42 = buildRenderedText(parts, timestamps);
|
|
16516
|
-
let totalTokens =
|
|
16517
|
+
let totalTokens = chunkUHWF2UKO_cjs.estimateTokenCount(text42);
|
|
16517
16518
|
if (totalTokens > options.maxTokens) {
|
|
16518
|
-
const truncated =
|
|
16519
|
+
const truncated = chunkUHWF2UKO_cjs.truncateStringByTokens(text42, options.maxTokens);
|
|
16519
16520
|
return { text: truncated, truncated: true, tokenOffset: totalTokens - options.maxTokens };
|
|
16520
16521
|
}
|
|
16521
16522
|
const truncatedIndices = parts.map((p, i) => ({ part: p, index: i })).filter(({ part }) => part.text !== part.fullText).sort((a, b) => expandPriority(a.part) - expandPriority(b.part));
|
|
@@ -16526,8 +16527,8 @@ function renderFormattedParts(parts, timestamps, options) {
|
|
|
16526
16527
|
for (const { part, index } of truncatedIndices) {
|
|
16527
16528
|
if (remaining <= 0) break;
|
|
16528
16529
|
const maxTokens = expandLimit(part);
|
|
16529
|
-
const fullTokens =
|
|
16530
|
-
const currentTokens =
|
|
16530
|
+
const fullTokens = chunkUHWF2UKO_cjs.estimateTokenCount(part.fullText);
|
|
16531
|
+
const currentTokens = chunkUHWF2UKO_cjs.estimateTokenCount(part.text);
|
|
16531
16532
|
const targetTokens = Math.min(fullTokens, maxTokens);
|
|
16532
16533
|
const delta = targetTokens - currentTokens;
|
|
16533
16534
|
if (delta <= 0) continue;
|
|
@@ -16538,17 +16539,17 @@ function renderFormattedParts(parts, timestamps, options) {
|
|
|
16538
16539
|
const expandedLimit = Math.min(currentTokens + remaining, maxTokens);
|
|
16539
16540
|
const hint = `recall cursor="${part.messageId}" partIndex=${part.partIndex} detail="high"`;
|
|
16540
16541
|
const { text: expanded2 } = truncateByTokens(part.fullText, expandedLimit, hint);
|
|
16541
|
-
const expandedDelta =
|
|
16542
|
+
const expandedDelta = chunkUHWF2UKO_cjs.estimateTokenCount(expanded2) - currentTokens;
|
|
16542
16543
|
parts[index] = { ...part, text: expanded2 };
|
|
16543
16544
|
remaining -= expandedDelta;
|
|
16544
16545
|
}
|
|
16545
16546
|
}
|
|
16546
16547
|
const expanded = buildRenderedText(parts, timestamps);
|
|
16547
|
-
const expandedTokens =
|
|
16548
|
+
const expandedTokens = chunkUHWF2UKO_cjs.estimateTokenCount(expanded);
|
|
16548
16549
|
if (expandedTokens <= options.maxTokens) {
|
|
16549
16550
|
return { text: expanded, truncated: false, tokenOffset: 0 };
|
|
16550
16551
|
}
|
|
16551
|
-
const hardTruncated =
|
|
16552
|
+
const hardTruncated = chunkUHWF2UKO_cjs.truncateStringByTokens(expanded, options.maxTokens);
|
|
16552
16553
|
return { text: hardTruncated, truncated: true, tokenOffset: expandedTokens - options.maxTokens };
|
|
16553
16554
|
}
|
|
16554
16555
|
async function recallPart({
|
|
@@ -16599,7 +16600,7 @@ async function recallPart({
|
|
|
16599
16600
|
|
|
16600
16601
|
`;
|
|
16601
16602
|
const fallbackText = `${fallbackNote}${firstNextPart.text}`;
|
|
16602
|
-
const truncatedText2 =
|
|
16603
|
+
const truncatedText2 = chunkUHWF2UKO_cjs.truncateStringByTokens(fallbackText, maxTokens);
|
|
16603
16604
|
const wasTruncated2 = truncatedText2 !== fallbackText;
|
|
16604
16605
|
return {
|
|
16605
16606
|
text: truncatedText2,
|
|
@@ -16614,7 +16615,7 @@ async function recallPart({
|
|
|
16614
16615
|
}
|
|
16615
16616
|
throw new Error(`Part index ${partIndex} not found in message ${cursor}. Available indices: ${availableIndices}`);
|
|
16616
16617
|
}
|
|
16617
|
-
const truncatedText =
|
|
16618
|
+
const truncatedText = chunkUHWF2UKO_cjs.truncateStringByTokens(target.text, maxTokens);
|
|
16618
16619
|
const wasTruncated = truncatedText !== target.text;
|
|
16619
16620
|
return {
|
|
16620
16621
|
text: truncatedText,
|
|
@@ -17313,94 +17314,149 @@ var Memory = class extends memory.MastraMemory {
|
|
|
17313
17314
|
);
|
|
17314
17315
|
}
|
|
17315
17316
|
}
|
|
17317
|
+
createMemorySpan(operationType, observabilityContext, input, attributes) {
|
|
17318
|
+
const currentSpan = observabilityContext?.tracingContext?.currentSpan;
|
|
17319
|
+
if (!currentSpan) return void 0;
|
|
17320
|
+
return currentSpan.createChildSpan({
|
|
17321
|
+
type: observability.SpanType.MEMORY_OPERATION,
|
|
17322
|
+
name: `memory: ${operationType}`,
|
|
17323
|
+
entityType: observability.EntityType.MEMORY,
|
|
17324
|
+
entityName: "Memory",
|
|
17325
|
+
input,
|
|
17326
|
+
attributes: { operationType, ...attributes }
|
|
17327
|
+
});
|
|
17328
|
+
}
|
|
17316
17329
|
async recall(args) {
|
|
17317
|
-
const {
|
|
17318
|
-
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
17319
|
-
if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId, config);
|
|
17320
|
-
const perPage = perPageArg !== void 0 ? perPageArg : config.lastMessages;
|
|
17321
|
-
const historyDisabledByConfig = config.lastMessages === false && perPageArg === void 0;
|
|
17322
|
-
const shouldGetNewestAndReverse = !orderBy && perPage !== false;
|
|
17323
|
-
const effectiveOrderBy = shouldGetNewestAndReverse ? { field: "createdAt", direction: "DESC" } : orderBy;
|
|
17324
|
-
const vectorResults = [];
|
|
17325
|
-
this.logger.debug("Memory recall", {
|
|
17330
|
+
const {
|
|
17326
17331
|
threadId,
|
|
17327
|
-
|
|
17332
|
+
resourceId,
|
|
17333
|
+
perPage: perPageArg,
|
|
17328
17334
|
page,
|
|
17329
|
-
orderBy
|
|
17330
|
-
|
|
17331
|
-
|
|
17332
|
-
|
|
17333
|
-
|
|
17334
|
-
}
|
|
17335
|
-
const
|
|
17336
|
-
const
|
|
17337
|
-
const
|
|
17338
|
-
|
|
17339
|
-
|
|
17340
|
-
|
|
17341
|
-
|
|
17342
|
-
|
|
17343
|
-
|
|
17344
|
-
|
|
17345
|
-
|
|
17346
|
-
|
|
17347
|
-
|
|
17348
|
-
|
|
17349
|
-
|
|
17350
|
-
|
|
17351
|
-
|
|
17352
|
-
|
|
17353
|
-
|
|
17354
|
-
|
|
17355
|
-
|
|
17356
|
-
|
|
17357
|
-
|
|
17358
|
-
|
|
17359
|
-
|
|
17360
|
-
|
|
17361
|
-
|
|
17362
|
-
|
|
17363
|
-
|
|
17335
|
+
orderBy,
|
|
17336
|
+
threadConfig,
|
|
17337
|
+
vectorSearchString,
|
|
17338
|
+
includeSystemReminders,
|
|
17339
|
+
filter: filter32
|
|
17340
|
+
} = args;
|
|
17341
|
+
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
17342
|
+
const semanticRecallEnabled = Boolean(config.semanticRecall);
|
|
17343
|
+
const span = this.createMemorySpan(
|
|
17344
|
+
"recall",
|
|
17345
|
+
args.observabilityContext,
|
|
17346
|
+
{ threadId, resourceId, vectorSearchString },
|
|
17347
|
+
{
|
|
17348
|
+
semanticRecallEnabled,
|
|
17349
|
+
lastMessages: config.lastMessages
|
|
17350
|
+
}
|
|
17351
|
+
);
|
|
17352
|
+
try {
|
|
17353
|
+
if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId, config);
|
|
17354
|
+
const perPage = perPageArg !== void 0 ? perPageArg : config.lastMessages;
|
|
17355
|
+
const historyDisabledByConfig = config.lastMessages === false && perPageArg === void 0;
|
|
17356
|
+
const shouldGetNewestAndReverse = !orderBy && perPage !== false;
|
|
17357
|
+
const effectiveOrderBy = shouldGetNewestAndReverse ? { field: "createdAt", direction: "DESC" } : orderBy;
|
|
17358
|
+
const vectorResults = [];
|
|
17359
|
+
this.logger.debug("Memory recall", {
|
|
17360
|
+
threadId,
|
|
17361
|
+
perPage,
|
|
17362
|
+
page,
|
|
17363
|
+
orderBy: effectiveOrderBy,
|
|
17364
|
+
hasWorkingMemorySchema: Boolean(config.workingMemory?.schema),
|
|
17365
|
+
workingMemoryEnabled: config.workingMemory?.enabled,
|
|
17366
|
+
semanticRecallEnabled,
|
|
17367
|
+
historyDisabledByConfig
|
|
17368
|
+
});
|
|
17369
|
+
const defaultRange = DEFAULT_MESSAGE_RANGE;
|
|
17370
|
+
const defaultTopK = DEFAULT_TOP_K;
|
|
17371
|
+
const vectorConfig = typeof config?.semanticRecall === `boolean` ? {
|
|
17372
|
+
topK: defaultTopK,
|
|
17373
|
+
messageRange: defaultRange
|
|
17374
|
+
} : {
|
|
17375
|
+
topK: config?.semanticRecall?.topK ?? defaultTopK,
|
|
17376
|
+
messageRange: config?.semanticRecall?.messageRange ?? defaultRange
|
|
17377
|
+
};
|
|
17378
|
+
const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope !== `thread` || config.semanticRecall === true;
|
|
17379
|
+
if (resourceScope && !resourceId && config?.semanticRecall && vectorSearchString) {
|
|
17380
|
+
throw new Error(
|
|
17381
|
+
`Memory error: Resource-scoped semantic recall is enabled but no resourceId was provided. Either provide a resourceId or explicitly set semanticRecall.scope to 'thread'.`
|
|
17382
|
+
);
|
|
17383
|
+
}
|
|
17384
|
+
let usage;
|
|
17385
|
+
if (historyDisabledByConfig && (!config.semanticRecall || !vectorSearchString || !this.vector)) {
|
|
17386
|
+
const result = {
|
|
17387
|
+
messages: [],
|
|
17388
|
+
usage: void 0,
|
|
17389
|
+
total: 0,
|
|
17390
|
+
page: page ?? 0,
|
|
17391
|
+
perPage: 0,
|
|
17392
|
+
hasMore: false
|
|
17393
|
+
};
|
|
17394
|
+
span?.end({ output: { success: true }, attributes: { messageCount: 0 } });
|
|
17395
|
+
return result;
|
|
17396
|
+
}
|
|
17397
|
+
if (config?.semanticRecall && vectorSearchString && this.vector) {
|
|
17398
|
+
const result = await this.embedMessageContent(vectorSearchString);
|
|
17399
|
+
usage = result.usage;
|
|
17400
|
+
const { embeddings, dimension } = result;
|
|
17401
|
+
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
17402
|
+
await Promise.all(
|
|
17403
|
+
embeddings.map(async (embedding) => {
|
|
17404
|
+
if (typeof this.vector === `undefined`) {
|
|
17405
|
+
throw new Error(
|
|
17406
|
+
`Tried to query vector index ${indexName} but this Memory instance doesn't have an attached vector db.`
|
|
17407
|
+
);
|
|
17408
|
+
}
|
|
17409
|
+
vectorResults.push(
|
|
17410
|
+
...await this.vector.query({
|
|
17411
|
+
indexName,
|
|
17412
|
+
queryVector: embedding,
|
|
17413
|
+
topK: vectorConfig.topK,
|
|
17414
|
+
filter: resourceScope ? {
|
|
17415
|
+
resource_id: resourceId
|
|
17416
|
+
} : {
|
|
17417
|
+
thread_id: threadId
|
|
17418
|
+
}
|
|
17419
|
+
})
|
|
17364
17420
|
);
|
|
17365
|
-
}
|
|
17366
|
-
|
|
17367
|
-
|
|
17368
|
-
|
|
17369
|
-
|
|
17370
|
-
|
|
17371
|
-
|
|
17372
|
-
|
|
17373
|
-
|
|
17374
|
-
|
|
17375
|
-
|
|
17376
|
-
|
|
17377
|
-
|
|
17378
|
-
|
|
17379
|
-
|
|
17421
|
+
})
|
|
17422
|
+
);
|
|
17423
|
+
}
|
|
17424
|
+
const memoryStore = await this.getMemoryStore();
|
|
17425
|
+
const effectivePerPage = historyDisabledByConfig ? 0 : perPage;
|
|
17426
|
+
const paginatedResult = await memoryStore.listMessages({
|
|
17427
|
+
threadId,
|
|
17428
|
+
resourceId,
|
|
17429
|
+
perPage: effectivePerPage,
|
|
17430
|
+
page,
|
|
17431
|
+
orderBy: effectiveOrderBy,
|
|
17432
|
+
filter: filter32,
|
|
17433
|
+
...vectorResults?.length ? {
|
|
17434
|
+
include: vectorResults.map((r) => ({
|
|
17435
|
+
id: r.metadata?.message_id,
|
|
17436
|
+
threadId: r.metadata?.thread_id,
|
|
17437
|
+
withNextMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.after,
|
|
17438
|
+
withPreviousMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.before
|
|
17439
|
+
}))
|
|
17440
|
+
} : {}
|
|
17441
|
+
});
|
|
17442
|
+
const rawMessages = shouldGetNewestAndReverse ? paginatedResult.messages.reverse() : paginatedResult.messages;
|
|
17443
|
+
const list = new agent.MessageList({ threadId, resourceId }).add(rawMessages, "memory");
|
|
17444
|
+
const messages = memory.filterSystemReminderMessages(list.get.all.db(), includeSystemReminders);
|
|
17445
|
+
const { total, page: resultPage, perPage: resultPerPage, hasMore } = paginatedResult;
|
|
17446
|
+
const recallResult = { messages, usage, total, page: resultPage, perPage: resultPerPage, hasMore };
|
|
17447
|
+
span?.end({
|
|
17448
|
+
output: { success: true },
|
|
17449
|
+
attributes: {
|
|
17450
|
+
messageCount: messages.length,
|
|
17451
|
+
embeddingTokens: usage?.tokens,
|
|
17452
|
+
vectorResultCount: vectorResults.length
|
|
17453
|
+
}
|
|
17454
|
+
});
|
|
17455
|
+
return recallResult;
|
|
17456
|
+
} catch (error) {
|
|
17457
|
+
span?.error({ error, endSpan: true });
|
|
17458
|
+
throw error;
|
|
17380
17459
|
}
|
|
17381
|
-
const memoryStore = await this.getMemoryStore();
|
|
17382
|
-
const effectivePerPage = historyDisabledByConfig ? 0 : perPage;
|
|
17383
|
-
const paginatedResult = await memoryStore.listMessages({
|
|
17384
|
-
threadId,
|
|
17385
|
-
resourceId,
|
|
17386
|
-
perPage: effectivePerPage,
|
|
17387
|
-
page,
|
|
17388
|
-
orderBy: effectiveOrderBy,
|
|
17389
|
-
filter: filter32,
|
|
17390
|
-
...vectorResults?.length ? {
|
|
17391
|
-
include: vectorResults.map((r) => ({
|
|
17392
|
-
id: r.metadata?.message_id,
|
|
17393
|
-
threadId: r.metadata?.thread_id,
|
|
17394
|
-
withNextMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.after,
|
|
17395
|
-
withPreviousMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.before
|
|
17396
|
-
}))
|
|
17397
|
-
} : {}
|
|
17398
|
-
});
|
|
17399
|
-
const rawMessages = shouldGetNewestAndReverse ? paginatedResult.messages.reverse() : paginatedResult.messages;
|
|
17400
|
-
const list = new agent.MessageList({ threadId, resourceId }).add(rawMessages, "memory");
|
|
17401
|
-
const messages = list.get.all.db();
|
|
17402
|
-
const { total, page: resultPage, perPage: resultPerPage, hasMore } = paginatedResult;
|
|
17403
|
-
return { messages, usage, total, page: resultPage, perPage: resultPerPage, hasMore };
|
|
17404
17460
|
}
|
|
17405
17461
|
async getThreadById({ threadId }) {
|
|
17406
17462
|
const memoryStore = await this.getMemoryStore();
|
|
@@ -17510,45 +17566,60 @@ var Memory = class extends memory.MastraMemory {
|
|
|
17510
17566
|
threadId,
|
|
17511
17567
|
resourceId,
|
|
17512
17568
|
workingMemory,
|
|
17513
|
-
memoryConfig
|
|
17569
|
+
memoryConfig,
|
|
17570
|
+
observabilityContext
|
|
17514
17571
|
}) {
|
|
17515
17572
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
17516
17573
|
if (!config.workingMemory?.enabled) {
|
|
17517
17574
|
throw new Error("Working memory is not enabled for this memory instance");
|
|
17518
17575
|
}
|
|
17519
|
-
const
|
|
17520
|
-
|
|
17521
|
-
|
|
17522
|
-
|
|
17523
|
-
|
|
17524
|
-
|
|
17525
|
-
|
|
17526
|
-
|
|
17527
|
-
this.updateWorkingMemoryMutexes.set(mutexKey, mutex);
|
|
17528
|
-
const release = await mutex.acquire();
|
|
17576
|
+
const span = this.createMemorySpan(
|
|
17577
|
+
"update",
|
|
17578
|
+
observabilityContext,
|
|
17579
|
+
{ threadId, resourceId },
|
|
17580
|
+
{
|
|
17581
|
+
workingMemoryEnabled: true
|
|
17582
|
+
}
|
|
17583
|
+
);
|
|
17529
17584
|
try {
|
|
17530
|
-
const
|
|
17531
|
-
if (scope === "resource" && resourceId) {
|
|
17532
|
-
|
|
17533
|
-
resourceId
|
|
17534
|
-
|
|
17535
|
-
|
|
17536
|
-
}
|
|
17537
|
-
|
|
17538
|
-
|
|
17539
|
-
|
|
17540
|
-
|
|
17541
|
-
await
|
|
17542
|
-
|
|
17543
|
-
|
|
17544
|
-
|
|
17545
|
-
...thread.metadata,
|
|
17585
|
+
const scope = config.workingMemory.scope || "resource";
|
|
17586
|
+
if (scope === "resource" && !resourceId) {
|
|
17587
|
+
throw new Error(
|
|
17588
|
+
`Memory error: Resource-scoped working memory is enabled but no resourceId was provided. Either provide a resourceId or explicitly set workingMemory.scope to 'thread'.`
|
|
17589
|
+
);
|
|
17590
|
+
}
|
|
17591
|
+
const mutexKey = scope === "resource" ? `resource-${resourceId}` : `thread-${threadId}`;
|
|
17592
|
+
const mutex = this.updateWorkingMemoryMutexes.has(mutexKey) ? this.updateWorkingMemoryMutexes.get(mutexKey) : new Mutex();
|
|
17593
|
+
this.updateWorkingMemoryMutexes.set(mutexKey, mutex);
|
|
17594
|
+
const release = await mutex.acquire();
|
|
17595
|
+
try {
|
|
17596
|
+
const memoryStore = await this.getMemoryStore();
|
|
17597
|
+
if (scope === "resource" && resourceId) {
|
|
17598
|
+
await memoryStore.updateResource({
|
|
17599
|
+
resourceId,
|
|
17546
17600
|
workingMemory
|
|
17601
|
+
});
|
|
17602
|
+
} else {
|
|
17603
|
+
const thread = await this.getThreadById({ threadId });
|
|
17604
|
+
if (!thread) {
|
|
17605
|
+
throw new Error(`Thread ${threadId} not found`);
|
|
17547
17606
|
}
|
|
17548
|
-
|
|
17607
|
+
await memoryStore.updateThread({
|
|
17608
|
+
id: threadId,
|
|
17609
|
+
title: thread.title || "",
|
|
17610
|
+
metadata: {
|
|
17611
|
+
...thread.metadata,
|
|
17612
|
+
workingMemory
|
|
17613
|
+
}
|
|
17614
|
+
});
|
|
17615
|
+
}
|
|
17616
|
+
} finally {
|
|
17617
|
+
release();
|
|
17549
17618
|
}
|
|
17550
|
-
|
|
17551
|
-
|
|
17619
|
+
span?.end({ output: { success: true } });
|
|
17620
|
+
} catch (error) {
|
|
17621
|
+
span?.error({ error, endSpan: true });
|
|
17622
|
+
throw error;
|
|
17552
17623
|
}
|
|
17553
17624
|
}
|
|
17554
17625
|
updateWorkingMemoryMutexes = /* @__PURE__ */ new Map();
|
|
@@ -17674,7 +17745,7 @@ ${workingMemory}`;
|
|
|
17674
17745
|
}
|
|
17675
17746
|
return chunks;
|
|
17676
17747
|
}
|
|
17677
|
-
hasher =
|
|
17748
|
+
hasher = chunkUHWF2UKO_cjs.e();
|
|
17678
17749
|
// embedding is computationally expensive so cache content -> embeddings/chunks
|
|
17679
17750
|
embeddingCache = /* @__PURE__ */ new Map();
|
|
17680
17751
|
firstEmbed;
|
|
@@ -17726,67 +17797,85 @@ ${workingMemory}`;
|
|
|
17726
17797
|
}
|
|
17727
17798
|
async saveMessages({
|
|
17728
17799
|
messages,
|
|
17729
|
-
memoryConfig
|
|
17800
|
+
memoryConfig,
|
|
17801
|
+
observabilityContext
|
|
17730
17802
|
}) {
|
|
17731
|
-
const
|
|
17732
|
-
|
|
17733
|
-
}).filter((m) => Boolean(m));
|
|
17734
|
-
const config = this.getMergedThreadConfig(memoryConfig);
|
|
17735
|
-
const dbMessages = new agent.MessageList({
|
|
17736
|
-
generateMessageId: () => this.generateId()
|
|
17737
|
-
}).add(updatedMessages, "memory").get.all.db();
|
|
17738
|
-
const memoryStore = await this.getMemoryStore();
|
|
17739
|
-
const result = await memoryStore.saveMessages({
|
|
17740
|
-
messages: dbMessages
|
|
17803
|
+
const span = this.createMemorySpan("save", observabilityContext, void 0, {
|
|
17804
|
+
messageCount: messages.length
|
|
17741
17805
|
});
|
|
17742
|
-
|
|
17743
|
-
|
|
17744
|
-
|
|
17745
|
-
|
|
17746
|
-
|
|
17747
|
-
|
|
17748
|
-
|
|
17749
|
-
|
|
17750
|
-
|
|
17751
|
-
|
|
17752
|
-
|
|
17753
|
-
|
|
17806
|
+
try {
|
|
17807
|
+
const updatedMessages = messages.map((m) => {
|
|
17808
|
+
return this.updateMessageToHideWorkingMemoryV2(m);
|
|
17809
|
+
}).filter((m) => Boolean(m));
|
|
17810
|
+
const config = this.getMergedThreadConfig(memoryConfig);
|
|
17811
|
+
const dbMessages = new agent.MessageList({
|
|
17812
|
+
generateMessageId: () => this.generateId()
|
|
17813
|
+
}).add(updatedMessages, "memory").get.all.db();
|
|
17814
|
+
const memoryStore = await this.getMemoryStore();
|
|
17815
|
+
const result = await memoryStore.saveMessages({
|
|
17816
|
+
messages: dbMessages
|
|
17817
|
+
});
|
|
17818
|
+
let totalTokens = 0;
|
|
17819
|
+
if (this.vector && config.semanticRecall) {
|
|
17820
|
+
const embeddingData = [];
|
|
17821
|
+
let dimension;
|
|
17822
|
+
await Promise.all(
|
|
17823
|
+
updatedMessages.map(async (message) => {
|
|
17824
|
+
let textForEmbedding = null;
|
|
17825
|
+
if (message.content.content && typeof message.content.content === "string" && message.content.content.trim() !== "") {
|
|
17826
|
+
textForEmbedding = message.content.content;
|
|
17827
|
+
} else if (message.content.parts && message.content.parts.length > 0) {
|
|
17828
|
+
const joined = message.content.parts.filter((part) => part.type === "text").map((part) => part.text).join(" ").trim();
|
|
17829
|
+
if (joined) textForEmbedding = joined;
|
|
17830
|
+
}
|
|
17831
|
+
if (!textForEmbedding) return;
|
|
17832
|
+
const result2 = await this.embedMessageContent(textForEmbedding);
|
|
17833
|
+
dimension = result2.dimension;
|
|
17834
|
+
if (result2.usage?.tokens) {
|
|
17835
|
+
totalTokens += result2.usage.tokens;
|
|
17836
|
+
}
|
|
17837
|
+
embeddingData.push({
|
|
17838
|
+
embeddings: result2.embeddings,
|
|
17839
|
+
metadata: result2.chunks.map(() => ({
|
|
17840
|
+
message_id: message.id,
|
|
17841
|
+
thread_id: message.threadId,
|
|
17842
|
+
resource_id: message.resourceId
|
|
17843
|
+
}))
|
|
17844
|
+
});
|
|
17845
|
+
})
|
|
17846
|
+
);
|
|
17847
|
+
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
17848
|
+
if (typeof this.vector === `undefined`) {
|
|
17849
|
+
throw new Error(`Tried to upsert embeddings but this Memory instance doesn't have an attached vector db.`);
|
|
17754
17850
|
}
|
|
17755
|
-
|
|
17756
|
-
const
|
|
17757
|
-
|
|
17758
|
-
|
|
17759
|
-
|
|
17851
|
+
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
17852
|
+
const allVectors = [];
|
|
17853
|
+
const allMetadata = [];
|
|
17854
|
+
for (const data of embeddingData) {
|
|
17855
|
+
allVectors.push(...data.embeddings);
|
|
17856
|
+
allMetadata.push(...data.metadata);
|
|
17760
17857
|
}
|
|
17761
|
-
|
|
17762
|
-
|
|
17763
|
-
|
|
17764
|
-
|
|
17765
|
-
thread_id: message.threadId,
|
|
17766
|
-
resource_id: message.resourceId
|
|
17767
|
-
}))
|
|
17858
|
+
await this.vector.upsert({
|
|
17859
|
+
indexName,
|
|
17860
|
+
vectors: allVectors,
|
|
17861
|
+
metadata: allMetadata
|
|
17768
17862
|
});
|
|
17769
|
-
})
|
|
17770
|
-
);
|
|
17771
|
-
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
17772
|
-
if (typeof this.vector === `undefined`) {
|
|
17773
|
-
throw new Error(`Tried to upsert embeddings but this Memory instance doesn't have an attached vector db.`);
|
|
17774
17863
|
}
|
|
17775
|
-
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
17776
|
-
const allVectors = [];
|
|
17777
|
-
const allMetadata = [];
|
|
17778
|
-
for (const data of embeddingData) {
|
|
17779
|
-
allVectors.push(...data.embeddings);
|
|
17780
|
-
allMetadata.push(...data.metadata);
|
|
17781
|
-
}
|
|
17782
|
-
await this.vector.upsert({
|
|
17783
|
-
indexName,
|
|
17784
|
-
vectors: allVectors,
|
|
17785
|
-
metadata: allMetadata
|
|
17786
|
-
});
|
|
17787
17864
|
}
|
|
17865
|
+
const saveResult = { ...result, usage: totalTokens > 0 ? { tokens: totalTokens } : void 0 };
|
|
17866
|
+
span?.end({
|
|
17867
|
+
output: { success: true },
|
|
17868
|
+
attributes: {
|
|
17869
|
+
messageCount: dbMessages.length,
|
|
17870
|
+
embeddingTokens: saveResult.usage?.tokens,
|
|
17871
|
+
semanticRecallEnabled: Boolean(config.semanticRecall)
|
|
17872
|
+
}
|
|
17873
|
+
});
|
|
17874
|
+
return saveResult;
|
|
17875
|
+
} catch (error) {
|
|
17876
|
+
span?.error({ error, endSpan: true });
|
|
17877
|
+
throw error;
|
|
17788
17878
|
}
|
|
17789
|
-
return { ...result, usage: totalTokens > 0 ? { tokens: totalTokens } : void 0 };
|
|
17790
17879
|
}
|
|
17791
17880
|
updateMessageToHideWorkingMemoryV2(message) {
|
|
17792
17881
|
const newMessage = { ...message };
|
|
@@ -18058,7 +18147,7 @@ ${workingMemory}`;
|
|
|
18058
18147
|
"Observational memory requires @mastra/core support for request-response-id-rotation. Please bump @mastra/core to a newer version."
|
|
18059
18148
|
);
|
|
18060
18149
|
}
|
|
18061
|
-
const { ObservationalMemory: OMClass } = await import('./observational-memory-
|
|
18150
|
+
const { ObservationalMemory: OMClass } = await import('./observational-memory-OVRHDQRG-OCHV6RZ7.cjs');
|
|
18062
18151
|
const onIndexObservations = this.hasRetrievalSearch(omConfig.retrieval) ? async (observation) => {
|
|
18063
18152
|
await this.indexObservation(observation);
|
|
18064
18153
|
} : void 0;
|
|
@@ -18321,6 +18410,32 @@ Notes:
|
|
|
18321
18410
|
}))
|
|
18322
18411
|
});
|
|
18323
18412
|
}
|
|
18413
|
+
/**
|
|
18414
|
+
* Update per-record observational memory config overrides for a thread.
|
|
18415
|
+
* The provided config is deep-merged, so you only need to specify fields you want to change.
|
|
18416
|
+
*
|
|
18417
|
+
* @example
|
|
18418
|
+
* ```ts
|
|
18419
|
+
* await memory.updateObservationalMemoryConfig({
|
|
18420
|
+
* threadId: 'thread-1',
|
|
18421
|
+
* config: {
|
|
18422
|
+
* observation: { messageTokens: 2000 },
|
|
18423
|
+
* reflection: { observationTokens: 8000 },
|
|
18424
|
+
* },
|
|
18425
|
+
* });
|
|
18426
|
+
* ```
|
|
18427
|
+
*/
|
|
18428
|
+
async updateObservationalMemoryConfig({
|
|
18429
|
+
threadId,
|
|
18430
|
+
resourceId,
|
|
18431
|
+
config
|
|
18432
|
+
}) {
|
|
18433
|
+
const omEngine = await this.omEngine;
|
|
18434
|
+
if (!omEngine) {
|
|
18435
|
+
throw new Error("Observational memory is not enabled");
|
|
18436
|
+
}
|
|
18437
|
+
await omEngine.updateRecordConfig(threadId, resourceId, config);
|
|
18438
|
+
}
|
|
18324
18439
|
/**
|
|
18325
18440
|
* Index a list of messages directly (without querying storage).
|
|
18326
18441
|
* Used by observe-time indexing to vectorize newly-observed messages.
|
|
@@ -18494,7 +18609,7 @@ Notes:
|
|
|
18494
18609
|
* - Message objects with 'id' properties
|
|
18495
18610
|
* @returns Promise that resolves when all messages are deleted
|
|
18496
18611
|
*/
|
|
18497
|
-
async deleteMessages(input) {
|
|
18612
|
+
async deleteMessages(input, observabilityContext) {
|
|
18498
18613
|
let messageIds;
|
|
18499
18614
|
if (!Array.isArray(input)) {
|
|
18500
18615
|
throw new Error("Invalid input: must be an array of message IDs or message objects");
|
|
@@ -18515,10 +18630,19 @@ Notes:
|
|
|
18515
18630
|
if (invalidIds.length > 0) {
|
|
18516
18631
|
throw new Error("All message IDs must be non-empty strings");
|
|
18517
18632
|
}
|
|
18518
|
-
const
|
|
18519
|
-
|
|
18520
|
-
|
|
18521
|
-
|
|
18633
|
+
const span = this.createMemorySpan("delete", observabilityContext, void 0, {
|
|
18634
|
+
messageCount: messageIds.length
|
|
18635
|
+
});
|
|
18636
|
+
try {
|
|
18637
|
+
const memoryStore = await this.getMemoryStore();
|
|
18638
|
+
await memoryStore.deleteMessages(messageIds);
|
|
18639
|
+
if (this.vector) {
|
|
18640
|
+
void this.deleteMessageVectors(messageIds);
|
|
18641
|
+
}
|
|
18642
|
+
span?.end({ output: { success: true }, attributes: { messageCount: messageIds.length } });
|
|
18643
|
+
} catch (error) {
|
|
18644
|
+
span?.error({ error, endSpan: true });
|
|
18645
|
+
throw error;
|
|
18522
18646
|
}
|
|
18523
18647
|
}
|
|
18524
18648
|
/**
|
|
@@ -18946,7 +19070,7 @@ Notes:
|
|
|
18946
19070
|
if (!effectiveConfig) return null;
|
|
18947
19071
|
const engine = await this.omEngine;
|
|
18948
19072
|
if (!engine) return null;
|
|
18949
|
-
const { ObservationalMemoryProcessor } = await import('./observational-memory-
|
|
19073
|
+
const { ObservationalMemoryProcessor } = await import('./observational-memory-OVRHDQRG-OCHV6RZ7.cjs');
|
|
18950
19074
|
return new ObservationalMemoryProcessor(engine, this);
|
|
18951
19075
|
}
|
|
18952
19076
|
};
|
|
@@ -30166,5 +30290,5 @@ exports.START_ASYNC_AGENT_BUILDER_ACTION_ROUTE = START_ASYNC_AGENT_BUILDER_ACTIO
|
|
|
30166
30290
|
exports.STREAM_AGENT_BUILDER_ACTION_ROUTE = STREAM_AGENT_BUILDER_ACTION_ROUTE;
|
|
30167
30291
|
exports.STREAM_LEGACY_AGENT_BUILDER_ACTION_ROUTE = STREAM_LEGACY_AGENT_BUILDER_ACTION_ROUTE;
|
|
30168
30292
|
exports.agent_builder_exports = agent_builder_exports;
|
|
30169
|
-
//# sourceMappingURL=chunk-
|
|
30170
|
-
//# sourceMappingURL=chunk-
|
|
30293
|
+
//# sourceMappingURL=chunk-2KF7EWOT.cjs.map
|
|
30294
|
+
//# sourceMappingURL=chunk-2KF7EWOT.cjs.map
|