@mastra/memory 1.14.0-alpha.1 → 1.14.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 +101 -0
- package/dist/{chunk-FQGF36BE.js → chunk-GXDPND6K.js} +80 -10
- package/dist/chunk-GXDPND6K.js.map +1 -0
- package/dist/{chunk-X7E3WPF2.cjs → chunk-ZVRO2GUN.cjs} +80 -10
- package/dist/chunk-ZVRO2GUN.cjs.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +27 -27
- package/dist/index.cjs +307 -183
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +29 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +300 -176
- package/dist/index.js.map +1 -1
- package/dist/{observational-memory-22RZ4253.cjs → observational-memory-IRCDSDUB.cjs} +26 -26
- package/dist/{observational-memory-22RZ4253.cjs.map → observational-memory-IRCDSDUB.cjs.map} +1 -1
- package/dist/{observational-memory-JQ34KLFS.js → observational-memory-OVRHDQRG.js} +3 -3
- package/dist/{observational-memory-JQ34KLFS.js.map → observational-memory-OVRHDQRG.js.map} +1 -1
- package/dist/processors/index.cjs +24 -24
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +36 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/chunk-FQGF36BE.js.map +0 -1
- package/dist/chunk-X7E3WPF2.cjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { __commonJS, require_token_error, __toESM } from './chunk-SVPZMV27.js';
|
|
2
2
|
import { __commonJS as __commonJS$1, require_token_error as require_token_error$1, __toESM as __toESM$1 } from './chunk-RC6RZVYE.js';
|
|
3
|
-
import { truncateStringByTokens, resolveToolResultValue, formatToolResultForObserver } from './chunk-
|
|
4
|
-
export { ModelByInputTokens, getObservationsAsOf } from './chunk-
|
|
3
|
+
import { truncateStringByTokens, resolveToolResultValue, formatToolResultForObserver } from './chunk-GXDPND6K.js';
|
|
4
|
+
export { ModelByInputTokens, getObservationsAsOf } from './chunk-GXDPND6K.js';
|
|
5
5
|
import { ZodFirstPartyTypeKind } from 'zod/v3';
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
import * as z4 from 'zod/v4';
|
|
8
8
|
import { z as z$1 } from 'zod/v4';
|
|
9
9
|
import { MessageList } from '@mastra/core/agent';
|
|
10
10
|
import { coreFeatures } from '@mastra/core/features';
|
|
11
|
-
import { MastraMemory, removeWorkingMemoryTags, extractWorkingMemoryContent } from '@mastra/core/memory';
|
|
11
|
+
import { MastraMemory, filterSystemReminderMessages, removeWorkingMemoryTags, extractWorkingMemoryContent } from '@mastra/core/memory';
|
|
12
12
|
export { extractWorkingMemoryContent, extractWorkingMemoryTags, removeWorkingMemoryTags } from '@mastra/core/memory';
|
|
13
|
+
import { EntityType, SpanType } from '@mastra/core/observability';
|
|
13
14
|
import { generateEmptyFromSchema } from '@mastra/core/utils';
|
|
14
15
|
import { isStandardSchemaWithJSON, toStandardSchema, standardSchemaToJSONSchema } from '@mastra/schema-compat/schema';
|
|
15
16
|
import { Mutex } from 'async-mutex';
|
|
@@ -16599,94 +16600,149 @@ var Memory = class extends MastraMemory {
|
|
|
16599
16600
|
);
|
|
16600
16601
|
}
|
|
16601
16602
|
}
|
|
16603
|
+
createMemorySpan(operationType, observabilityContext, input, attributes) {
|
|
16604
|
+
const currentSpan = observabilityContext?.tracingContext?.currentSpan;
|
|
16605
|
+
if (!currentSpan) return void 0;
|
|
16606
|
+
return currentSpan.createChildSpan({
|
|
16607
|
+
type: SpanType.MEMORY_OPERATION,
|
|
16608
|
+
name: `memory: ${operationType}`,
|
|
16609
|
+
entityType: EntityType.MEMORY,
|
|
16610
|
+
entityName: "Memory",
|
|
16611
|
+
input,
|
|
16612
|
+
attributes: { operationType, ...attributes }
|
|
16613
|
+
});
|
|
16614
|
+
}
|
|
16602
16615
|
async recall(args) {
|
|
16603
|
-
const {
|
|
16604
|
-
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
16605
|
-
if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId, config);
|
|
16606
|
-
const perPage = perPageArg !== void 0 ? perPageArg : config.lastMessages;
|
|
16607
|
-
const historyDisabledByConfig = config.lastMessages === false && perPageArg === void 0;
|
|
16608
|
-
const shouldGetNewestAndReverse = !orderBy && perPage !== false;
|
|
16609
|
-
const effectiveOrderBy = shouldGetNewestAndReverse ? { field: "createdAt", direction: "DESC" } : orderBy;
|
|
16610
|
-
const vectorResults = [];
|
|
16611
|
-
this.logger.debug("Memory recall", {
|
|
16616
|
+
const {
|
|
16612
16617
|
threadId,
|
|
16613
|
-
|
|
16618
|
+
resourceId,
|
|
16619
|
+
perPage: perPageArg,
|
|
16614
16620
|
page,
|
|
16615
|
-
orderBy
|
|
16616
|
-
|
|
16617
|
-
|
|
16618
|
-
|
|
16619
|
-
|
|
16620
|
-
}
|
|
16621
|
-
const
|
|
16622
|
-
const
|
|
16623
|
-
const
|
|
16624
|
-
|
|
16625
|
-
|
|
16626
|
-
|
|
16627
|
-
|
|
16628
|
-
|
|
16629
|
-
|
|
16630
|
-
|
|
16631
|
-
|
|
16632
|
-
|
|
16633
|
-
|
|
16634
|
-
|
|
16635
|
-
|
|
16636
|
-
|
|
16637
|
-
|
|
16638
|
-
|
|
16639
|
-
|
|
16640
|
-
|
|
16641
|
-
|
|
16642
|
-
|
|
16643
|
-
|
|
16644
|
-
|
|
16645
|
-
|
|
16646
|
-
|
|
16647
|
-
|
|
16648
|
-
|
|
16649
|
-
|
|
16621
|
+
orderBy,
|
|
16622
|
+
threadConfig,
|
|
16623
|
+
vectorSearchString,
|
|
16624
|
+
includeSystemReminders,
|
|
16625
|
+
filter: filter3
|
|
16626
|
+
} = args;
|
|
16627
|
+
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
16628
|
+
const semanticRecallEnabled = Boolean(config.semanticRecall);
|
|
16629
|
+
const span = this.createMemorySpan(
|
|
16630
|
+
"recall",
|
|
16631
|
+
args.observabilityContext,
|
|
16632
|
+
{ threadId, resourceId, vectorSearchString },
|
|
16633
|
+
{
|
|
16634
|
+
semanticRecallEnabled,
|
|
16635
|
+
lastMessages: config.lastMessages
|
|
16636
|
+
}
|
|
16637
|
+
);
|
|
16638
|
+
try {
|
|
16639
|
+
if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId, config);
|
|
16640
|
+
const perPage = perPageArg !== void 0 ? perPageArg : config.lastMessages;
|
|
16641
|
+
const historyDisabledByConfig = config.lastMessages === false && perPageArg === void 0;
|
|
16642
|
+
const shouldGetNewestAndReverse = !orderBy && perPage !== false;
|
|
16643
|
+
const effectiveOrderBy = shouldGetNewestAndReverse ? { field: "createdAt", direction: "DESC" } : orderBy;
|
|
16644
|
+
const vectorResults = [];
|
|
16645
|
+
this.logger.debug("Memory recall", {
|
|
16646
|
+
threadId,
|
|
16647
|
+
perPage,
|
|
16648
|
+
page,
|
|
16649
|
+
orderBy: effectiveOrderBy,
|
|
16650
|
+
hasWorkingMemorySchema: Boolean(config.workingMemory?.schema),
|
|
16651
|
+
workingMemoryEnabled: config.workingMemory?.enabled,
|
|
16652
|
+
semanticRecallEnabled,
|
|
16653
|
+
historyDisabledByConfig
|
|
16654
|
+
});
|
|
16655
|
+
const defaultRange = DEFAULT_MESSAGE_RANGE;
|
|
16656
|
+
const defaultTopK = DEFAULT_TOP_K;
|
|
16657
|
+
const vectorConfig = typeof config?.semanticRecall === `boolean` ? {
|
|
16658
|
+
topK: defaultTopK,
|
|
16659
|
+
messageRange: defaultRange
|
|
16660
|
+
} : {
|
|
16661
|
+
topK: config?.semanticRecall?.topK ?? defaultTopK,
|
|
16662
|
+
messageRange: config?.semanticRecall?.messageRange ?? defaultRange
|
|
16663
|
+
};
|
|
16664
|
+
const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope !== `thread` || config.semanticRecall === true;
|
|
16665
|
+
if (resourceScope && !resourceId && config?.semanticRecall && vectorSearchString) {
|
|
16666
|
+
throw new Error(
|
|
16667
|
+
`Memory error: Resource-scoped semantic recall is enabled but no resourceId was provided. Either provide a resourceId or explicitly set semanticRecall.scope to 'thread'.`
|
|
16668
|
+
);
|
|
16669
|
+
}
|
|
16670
|
+
let usage;
|
|
16671
|
+
if (historyDisabledByConfig && (!config.semanticRecall || !vectorSearchString || !this.vector)) {
|
|
16672
|
+
const result = {
|
|
16673
|
+
messages: [],
|
|
16674
|
+
usage: void 0,
|
|
16675
|
+
total: 0,
|
|
16676
|
+
page: page ?? 0,
|
|
16677
|
+
perPage: 0,
|
|
16678
|
+
hasMore: false
|
|
16679
|
+
};
|
|
16680
|
+
span?.end({ output: { success: true }, attributes: { messageCount: 0 } });
|
|
16681
|
+
return result;
|
|
16682
|
+
}
|
|
16683
|
+
if (config?.semanticRecall && vectorSearchString && this.vector) {
|
|
16684
|
+
const result = await this.embedMessageContent(vectorSearchString);
|
|
16685
|
+
usage = result.usage;
|
|
16686
|
+
const { embeddings, dimension } = result;
|
|
16687
|
+
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
16688
|
+
await Promise.all(
|
|
16689
|
+
embeddings.map(async (embedding) => {
|
|
16690
|
+
if (typeof this.vector === `undefined`) {
|
|
16691
|
+
throw new Error(
|
|
16692
|
+
`Tried to query vector index ${indexName} but this Memory instance doesn't have an attached vector db.`
|
|
16693
|
+
);
|
|
16694
|
+
}
|
|
16695
|
+
vectorResults.push(
|
|
16696
|
+
...await this.vector.query({
|
|
16697
|
+
indexName,
|
|
16698
|
+
queryVector: embedding,
|
|
16699
|
+
topK: vectorConfig.topK,
|
|
16700
|
+
filter: resourceScope ? {
|
|
16701
|
+
resource_id: resourceId
|
|
16702
|
+
} : {
|
|
16703
|
+
thread_id: threadId
|
|
16704
|
+
}
|
|
16705
|
+
})
|
|
16650
16706
|
);
|
|
16651
|
-
}
|
|
16652
|
-
|
|
16653
|
-
|
|
16654
|
-
|
|
16655
|
-
|
|
16656
|
-
|
|
16657
|
-
|
|
16658
|
-
|
|
16659
|
-
|
|
16660
|
-
|
|
16661
|
-
|
|
16662
|
-
|
|
16663
|
-
|
|
16664
|
-
|
|
16665
|
-
|
|
16707
|
+
})
|
|
16708
|
+
);
|
|
16709
|
+
}
|
|
16710
|
+
const memoryStore = await this.getMemoryStore();
|
|
16711
|
+
const effectivePerPage = historyDisabledByConfig ? 0 : perPage;
|
|
16712
|
+
const paginatedResult = await memoryStore.listMessages({
|
|
16713
|
+
threadId,
|
|
16714
|
+
resourceId,
|
|
16715
|
+
perPage: effectivePerPage,
|
|
16716
|
+
page,
|
|
16717
|
+
orderBy: effectiveOrderBy,
|
|
16718
|
+
filter: filter3,
|
|
16719
|
+
...vectorResults?.length ? {
|
|
16720
|
+
include: vectorResults.map((r) => ({
|
|
16721
|
+
id: r.metadata?.message_id,
|
|
16722
|
+
threadId: r.metadata?.thread_id,
|
|
16723
|
+
withNextMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.after,
|
|
16724
|
+
withPreviousMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.before
|
|
16725
|
+
}))
|
|
16726
|
+
} : {}
|
|
16727
|
+
});
|
|
16728
|
+
const rawMessages = shouldGetNewestAndReverse ? paginatedResult.messages.reverse() : paginatedResult.messages;
|
|
16729
|
+
const list = new MessageList({ threadId, resourceId }).add(rawMessages, "memory");
|
|
16730
|
+
const messages = filterSystemReminderMessages(list.get.all.db(), includeSystemReminders);
|
|
16731
|
+
const { total, page: resultPage, perPage: resultPerPage, hasMore } = paginatedResult;
|
|
16732
|
+
const recallResult = { messages, usage, total, page: resultPage, perPage: resultPerPage, hasMore };
|
|
16733
|
+
span?.end({
|
|
16734
|
+
output: { success: true },
|
|
16735
|
+
attributes: {
|
|
16736
|
+
messageCount: messages.length,
|
|
16737
|
+
embeddingTokens: usage?.tokens,
|
|
16738
|
+
vectorResultCount: vectorResults.length
|
|
16739
|
+
}
|
|
16740
|
+
});
|
|
16741
|
+
return recallResult;
|
|
16742
|
+
} catch (error) {
|
|
16743
|
+
span?.error({ error, endSpan: true });
|
|
16744
|
+
throw error;
|
|
16666
16745
|
}
|
|
16667
|
-
const memoryStore = await this.getMemoryStore();
|
|
16668
|
-
const effectivePerPage = historyDisabledByConfig ? 0 : perPage;
|
|
16669
|
-
const paginatedResult = await memoryStore.listMessages({
|
|
16670
|
-
threadId,
|
|
16671
|
-
resourceId,
|
|
16672
|
-
perPage: effectivePerPage,
|
|
16673
|
-
page,
|
|
16674
|
-
orderBy: effectiveOrderBy,
|
|
16675
|
-
filter: filter3,
|
|
16676
|
-
...vectorResults?.length ? {
|
|
16677
|
-
include: vectorResults.map((r) => ({
|
|
16678
|
-
id: r.metadata?.message_id,
|
|
16679
|
-
threadId: r.metadata?.thread_id,
|
|
16680
|
-
withNextMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.after,
|
|
16681
|
-
withPreviousMessages: typeof vectorConfig.messageRange === "number" ? vectorConfig.messageRange : vectorConfig.messageRange.before
|
|
16682
|
-
}))
|
|
16683
|
-
} : {}
|
|
16684
|
-
});
|
|
16685
|
-
const rawMessages = shouldGetNewestAndReverse ? paginatedResult.messages.reverse() : paginatedResult.messages;
|
|
16686
|
-
const list = new MessageList({ threadId, resourceId }).add(rawMessages, "memory");
|
|
16687
|
-
const messages = list.get.all.db();
|
|
16688
|
-
const { total, page: resultPage, perPage: resultPerPage, hasMore } = paginatedResult;
|
|
16689
|
-
return { messages, usage, total, page: resultPage, perPage: resultPerPage, hasMore };
|
|
16690
16746
|
}
|
|
16691
16747
|
async getThreadById({ threadId }) {
|
|
16692
16748
|
const memoryStore = await this.getMemoryStore();
|
|
@@ -16796,45 +16852,60 @@ var Memory = class extends MastraMemory {
|
|
|
16796
16852
|
threadId,
|
|
16797
16853
|
resourceId,
|
|
16798
16854
|
workingMemory,
|
|
16799
|
-
memoryConfig
|
|
16855
|
+
memoryConfig,
|
|
16856
|
+
observabilityContext
|
|
16800
16857
|
}) {
|
|
16801
16858
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
16802
16859
|
if (!config.workingMemory?.enabled) {
|
|
16803
16860
|
throw new Error("Working memory is not enabled for this memory instance");
|
|
16804
16861
|
}
|
|
16805
|
-
const
|
|
16806
|
-
|
|
16807
|
-
|
|
16808
|
-
|
|
16809
|
-
|
|
16810
|
-
|
|
16811
|
-
|
|
16812
|
-
|
|
16813
|
-
this.updateWorkingMemoryMutexes.set(mutexKey, mutex);
|
|
16814
|
-
const release = await mutex.acquire();
|
|
16862
|
+
const span = this.createMemorySpan(
|
|
16863
|
+
"update",
|
|
16864
|
+
observabilityContext,
|
|
16865
|
+
{ threadId, resourceId },
|
|
16866
|
+
{
|
|
16867
|
+
workingMemoryEnabled: true
|
|
16868
|
+
}
|
|
16869
|
+
);
|
|
16815
16870
|
try {
|
|
16816
|
-
const
|
|
16817
|
-
if (scope === "resource" && resourceId) {
|
|
16818
|
-
|
|
16819
|
-
resourceId
|
|
16820
|
-
|
|
16821
|
-
|
|
16822
|
-
}
|
|
16823
|
-
|
|
16824
|
-
|
|
16825
|
-
|
|
16826
|
-
|
|
16827
|
-
await
|
|
16828
|
-
|
|
16829
|
-
|
|
16830
|
-
|
|
16831
|
-
...thread.metadata,
|
|
16871
|
+
const scope = config.workingMemory.scope || "resource";
|
|
16872
|
+
if (scope === "resource" && !resourceId) {
|
|
16873
|
+
throw new Error(
|
|
16874
|
+
`Memory error: Resource-scoped working memory is enabled but no resourceId was provided. Either provide a resourceId or explicitly set workingMemory.scope to 'thread'.`
|
|
16875
|
+
);
|
|
16876
|
+
}
|
|
16877
|
+
const mutexKey = scope === "resource" ? `resource-${resourceId}` : `thread-${threadId}`;
|
|
16878
|
+
const mutex = this.updateWorkingMemoryMutexes.has(mutexKey) ? this.updateWorkingMemoryMutexes.get(mutexKey) : new Mutex();
|
|
16879
|
+
this.updateWorkingMemoryMutexes.set(mutexKey, mutex);
|
|
16880
|
+
const release = await mutex.acquire();
|
|
16881
|
+
try {
|
|
16882
|
+
const memoryStore = await this.getMemoryStore();
|
|
16883
|
+
if (scope === "resource" && resourceId) {
|
|
16884
|
+
await memoryStore.updateResource({
|
|
16885
|
+
resourceId,
|
|
16832
16886
|
workingMemory
|
|
16887
|
+
});
|
|
16888
|
+
} else {
|
|
16889
|
+
const thread = await this.getThreadById({ threadId });
|
|
16890
|
+
if (!thread) {
|
|
16891
|
+
throw new Error(`Thread ${threadId} not found`);
|
|
16833
16892
|
}
|
|
16834
|
-
|
|
16893
|
+
await memoryStore.updateThread({
|
|
16894
|
+
id: threadId,
|
|
16895
|
+
title: thread.title || "",
|
|
16896
|
+
metadata: {
|
|
16897
|
+
...thread.metadata,
|
|
16898
|
+
workingMemory
|
|
16899
|
+
}
|
|
16900
|
+
});
|
|
16901
|
+
}
|
|
16902
|
+
} finally {
|
|
16903
|
+
release();
|
|
16835
16904
|
}
|
|
16836
|
-
|
|
16837
|
-
|
|
16905
|
+
span?.end({ output: { success: true } });
|
|
16906
|
+
} catch (error) {
|
|
16907
|
+
span?.error({ error, endSpan: true });
|
|
16908
|
+
throw error;
|
|
16838
16909
|
}
|
|
16839
16910
|
}
|
|
16840
16911
|
updateWorkingMemoryMutexes = /* @__PURE__ */ new Map();
|
|
@@ -17012,67 +17083,85 @@ ${workingMemory}`;
|
|
|
17012
17083
|
}
|
|
17013
17084
|
async saveMessages({
|
|
17014
17085
|
messages,
|
|
17015
|
-
memoryConfig
|
|
17086
|
+
memoryConfig,
|
|
17087
|
+
observabilityContext
|
|
17016
17088
|
}) {
|
|
17017
|
-
const
|
|
17018
|
-
|
|
17019
|
-
}).filter((m) => Boolean(m));
|
|
17020
|
-
const config = this.getMergedThreadConfig(memoryConfig);
|
|
17021
|
-
const dbMessages = new MessageList({
|
|
17022
|
-
generateMessageId: () => this.generateId()
|
|
17023
|
-
}).add(updatedMessages, "memory").get.all.db();
|
|
17024
|
-
const memoryStore = await this.getMemoryStore();
|
|
17025
|
-
const result = await memoryStore.saveMessages({
|
|
17026
|
-
messages: dbMessages
|
|
17089
|
+
const span = this.createMemorySpan("save", observabilityContext, void 0, {
|
|
17090
|
+
messageCount: messages.length
|
|
17027
17091
|
});
|
|
17028
|
-
|
|
17029
|
-
|
|
17030
|
-
|
|
17031
|
-
|
|
17032
|
-
|
|
17033
|
-
|
|
17034
|
-
|
|
17035
|
-
|
|
17036
|
-
|
|
17037
|
-
|
|
17038
|
-
|
|
17039
|
-
|
|
17092
|
+
try {
|
|
17093
|
+
const updatedMessages = messages.map((m) => {
|
|
17094
|
+
return this.updateMessageToHideWorkingMemoryV2(m);
|
|
17095
|
+
}).filter((m) => Boolean(m));
|
|
17096
|
+
const config = this.getMergedThreadConfig(memoryConfig);
|
|
17097
|
+
const dbMessages = new MessageList({
|
|
17098
|
+
generateMessageId: () => this.generateId()
|
|
17099
|
+
}).add(updatedMessages, "memory").get.all.db();
|
|
17100
|
+
const memoryStore = await this.getMemoryStore();
|
|
17101
|
+
const result = await memoryStore.saveMessages({
|
|
17102
|
+
messages: dbMessages
|
|
17103
|
+
});
|
|
17104
|
+
let totalTokens = 0;
|
|
17105
|
+
if (this.vector && config.semanticRecall) {
|
|
17106
|
+
const embeddingData = [];
|
|
17107
|
+
let dimension;
|
|
17108
|
+
await Promise.all(
|
|
17109
|
+
updatedMessages.map(async (message) => {
|
|
17110
|
+
let textForEmbedding = null;
|
|
17111
|
+
if (message.content.content && typeof message.content.content === "string" && message.content.content.trim() !== "") {
|
|
17112
|
+
textForEmbedding = message.content.content;
|
|
17113
|
+
} else if (message.content.parts && message.content.parts.length > 0) {
|
|
17114
|
+
const joined = message.content.parts.filter((part) => part.type === "text").map((part) => part.text).join(" ").trim();
|
|
17115
|
+
if (joined) textForEmbedding = joined;
|
|
17116
|
+
}
|
|
17117
|
+
if (!textForEmbedding) return;
|
|
17118
|
+
const result2 = await this.embedMessageContent(textForEmbedding);
|
|
17119
|
+
dimension = result2.dimension;
|
|
17120
|
+
if (result2.usage?.tokens) {
|
|
17121
|
+
totalTokens += result2.usage.tokens;
|
|
17122
|
+
}
|
|
17123
|
+
embeddingData.push({
|
|
17124
|
+
embeddings: result2.embeddings,
|
|
17125
|
+
metadata: result2.chunks.map(() => ({
|
|
17126
|
+
message_id: message.id,
|
|
17127
|
+
thread_id: message.threadId,
|
|
17128
|
+
resource_id: message.resourceId
|
|
17129
|
+
}))
|
|
17130
|
+
});
|
|
17131
|
+
})
|
|
17132
|
+
);
|
|
17133
|
+
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
17134
|
+
if (typeof this.vector === `undefined`) {
|
|
17135
|
+
throw new Error(`Tried to upsert embeddings but this Memory instance doesn't have an attached vector db.`);
|
|
17040
17136
|
}
|
|
17041
|
-
|
|
17042
|
-
const
|
|
17043
|
-
|
|
17044
|
-
|
|
17045
|
-
|
|
17137
|
+
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
17138
|
+
const allVectors = [];
|
|
17139
|
+
const allMetadata = [];
|
|
17140
|
+
for (const data of embeddingData) {
|
|
17141
|
+
allVectors.push(...data.embeddings);
|
|
17142
|
+
allMetadata.push(...data.metadata);
|
|
17046
17143
|
}
|
|
17047
|
-
|
|
17048
|
-
|
|
17049
|
-
|
|
17050
|
-
|
|
17051
|
-
thread_id: message.threadId,
|
|
17052
|
-
resource_id: message.resourceId
|
|
17053
|
-
}))
|
|
17144
|
+
await this.vector.upsert({
|
|
17145
|
+
indexName,
|
|
17146
|
+
vectors: allVectors,
|
|
17147
|
+
metadata: allMetadata
|
|
17054
17148
|
});
|
|
17055
|
-
})
|
|
17056
|
-
);
|
|
17057
|
-
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
17058
|
-
if (typeof this.vector === `undefined`) {
|
|
17059
|
-
throw new Error(`Tried to upsert embeddings but this Memory instance doesn't have an attached vector db.`);
|
|
17060
|
-
}
|
|
17061
|
-
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
17062
|
-
const allVectors = [];
|
|
17063
|
-
const allMetadata = [];
|
|
17064
|
-
for (const data of embeddingData) {
|
|
17065
|
-
allVectors.push(...data.embeddings);
|
|
17066
|
-
allMetadata.push(...data.metadata);
|
|
17067
17149
|
}
|
|
17068
|
-
await this.vector.upsert({
|
|
17069
|
-
indexName,
|
|
17070
|
-
vectors: allVectors,
|
|
17071
|
-
metadata: allMetadata
|
|
17072
|
-
});
|
|
17073
17150
|
}
|
|
17151
|
+
const saveResult = { ...result, usage: totalTokens > 0 ? { tokens: totalTokens } : void 0 };
|
|
17152
|
+
span?.end({
|
|
17153
|
+
output: { success: true },
|
|
17154
|
+
attributes: {
|
|
17155
|
+
messageCount: dbMessages.length,
|
|
17156
|
+
embeddingTokens: saveResult.usage?.tokens,
|
|
17157
|
+
semanticRecallEnabled: Boolean(config.semanticRecall)
|
|
17158
|
+
}
|
|
17159
|
+
});
|
|
17160
|
+
return saveResult;
|
|
17161
|
+
} catch (error) {
|
|
17162
|
+
span?.error({ error, endSpan: true });
|
|
17163
|
+
throw error;
|
|
17074
17164
|
}
|
|
17075
|
-
return { ...result, usage: totalTokens > 0 ? { tokens: totalTokens } : void 0 };
|
|
17076
17165
|
}
|
|
17077
17166
|
updateMessageToHideWorkingMemoryV2(message) {
|
|
17078
17167
|
const newMessage = { ...message };
|
|
@@ -17344,7 +17433,7 @@ ${workingMemory}`;
|
|
|
17344
17433
|
"Observational memory requires @mastra/core support for request-response-id-rotation. Please bump @mastra/core to a newer version."
|
|
17345
17434
|
);
|
|
17346
17435
|
}
|
|
17347
|
-
const { ObservationalMemory: OMClass } = await import('./observational-memory-
|
|
17436
|
+
const { ObservationalMemory: OMClass } = await import('./observational-memory-OVRHDQRG.js');
|
|
17348
17437
|
const onIndexObservations = this.hasRetrievalSearch(omConfig.retrieval) ? async (observation) => {
|
|
17349
17438
|
await this.indexObservation(observation);
|
|
17350
17439
|
} : void 0;
|
|
@@ -17607,6 +17696,32 @@ Notes:
|
|
|
17607
17696
|
}))
|
|
17608
17697
|
});
|
|
17609
17698
|
}
|
|
17699
|
+
/**
|
|
17700
|
+
* Update per-record observational memory config overrides for a thread.
|
|
17701
|
+
* The provided config is deep-merged, so you only need to specify fields you want to change.
|
|
17702
|
+
*
|
|
17703
|
+
* @example
|
|
17704
|
+
* ```ts
|
|
17705
|
+
* await memory.updateObservationalMemoryConfig({
|
|
17706
|
+
* threadId: 'thread-1',
|
|
17707
|
+
* config: {
|
|
17708
|
+
* observation: { messageTokens: 2000 },
|
|
17709
|
+
* reflection: { observationTokens: 8000 },
|
|
17710
|
+
* },
|
|
17711
|
+
* });
|
|
17712
|
+
* ```
|
|
17713
|
+
*/
|
|
17714
|
+
async updateObservationalMemoryConfig({
|
|
17715
|
+
threadId,
|
|
17716
|
+
resourceId,
|
|
17717
|
+
config
|
|
17718
|
+
}) {
|
|
17719
|
+
const omEngine = await this.omEngine;
|
|
17720
|
+
if (!omEngine) {
|
|
17721
|
+
throw new Error("Observational memory is not enabled");
|
|
17722
|
+
}
|
|
17723
|
+
await omEngine.updateRecordConfig(threadId, resourceId, config);
|
|
17724
|
+
}
|
|
17610
17725
|
/**
|
|
17611
17726
|
* Index a list of messages directly (without querying storage).
|
|
17612
17727
|
* Used by observe-time indexing to vectorize newly-observed messages.
|
|
@@ -17780,7 +17895,7 @@ Notes:
|
|
|
17780
17895
|
* - Message objects with 'id' properties
|
|
17781
17896
|
* @returns Promise that resolves when all messages are deleted
|
|
17782
17897
|
*/
|
|
17783
|
-
async deleteMessages(input) {
|
|
17898
|
+
async deleteMessages(input, observabilityContext) {
|
|
17784
17899
|
let messageIds;
|
|
17785
17900
|
if (!Array.isArray(input)) {
|
|
17786
17901
|
throw new Error("Invalid input: must be an array of message IDs or message objects");
|
|
@@ -17801,10 +17916,19 @@ Notes:
|
|
|
17801
17916
|
if (invalidIds.length > 0) {
|
|
17802
17917
|
throw new Error("All message IDs must be non-empty strings");
|
|
17803
17918
|
}
|
|
17804
|
-
const
|
|
17805
|
-
|
|
17806
|
-
|
|
17807
|
-
|
|
17919
|
+
const span = this.createMemorySpan("delete", observabilityContext, void 0, {
|
|
17920
|
+
messageCount: messageIds.length
|
|
17921
|
+
});
|
|
17922
|
+
try {
|
|
17923
|
+
const memoryStore = await this.getMemoryStore();
|
|
17924
|
+
await memoryStore.deleteMessages(messageIds);
|
|
17925
|
+
if (this.vector) {
|
|
17926
|
+
void this.deleteMessageVectors(messageIds);
|
|
17927
|
+
}
|
|
17928
|
+
span?.end({ output: { success: true }, attributes: { messageCount: messageIds.length } });
|
|
17929
|
+
} catch (error) {
|
|
17930
|
+
span?.error({ error, endSpan: true });
|
|
17931
|
+
throw error;
|
|
17808
17932
|
}
|
|
17809
17933
|
}
|
|
17810
17934
|
/**
|
|
@@ -18232,7 +18356,7 @@ Notes:
|
|
|
18232
18356
|
if (!effectiveConfig) return null;
|
|
18233
18357
|
const engine = await this.omEngine;
|
|
18234
18358
|
if (!engine) return null;
|
|
18235
|
-
const { ObservationalMemoryProcessor } = await import('./observational-memory-
|
|
18359
|
+
const { ObservationalMemoryProcessor } = await import('./observational-memory-OVRHDQRG.js');
|
|
18236
18360
|
return new ObservationalMemoryProcessor(engine, this);
|
|
18237
18361
|
}
|
|
18238
18362
|
};
|