@mastra/memory 1.0.1 → 1.1.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/CHANGELOG.md +60 -0
- package/dist/chunk-6TXUWFIU.js +3188 -0
- package/dist/chunk-6TXUWFIU.js.map +1 -0
- package/dist/chunk-FQJWVCDF.cjs +3205 -0
- package/dist/chunk-FQJWVCDF.cjs.map +1 -0
- package/dist/docs/README.md +1 -1
- package/dist/docs/SKILL.md +12 -1
- package/dist/docs/SOURCE_MAP.json +62 -2
- package/dist/docs/memory/02-storage.md +10 -0
- package/dist/index.cjs +108 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +62 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +108 -5
- package/dist/index.js.map +1 -1
- package/dist/observational-memory-3Q42SITP.cjs +52 -0
- package/dist/observational-memory-3Q42SITP.cjs.map +1 -0
- package/dist/observational-memory-VXLHOSDZ.js +3 -0
- package/dist/observational-memory-VXLHOSDZ.js.map +1 -0
- package/dist/processors/index.cjs +52 -0
- package/dist/processors/index.cjs.map +1 -0
- package/dist/processors/index.d.ts +2 -0
- package/dist/processors/index.d.ts.map +1 -0
- package/dist/processors/index.js +3 -0
- package/dist/processors/index.js.map +1 -0
- package/dist/processors/observational-memory/index.d.ts +18 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +579 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-agent.d.ts +117 -0
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts +46 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/token-counter.d.ts +30 -0
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -0
- package/dist/processors/observational-memory/types.d.ts +288 -0
- package/dist/processors/observational-memory/types.d.ts.map +1 -0
- package/package.json +15 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { embedMany } from './_types/@internal_ai-sdk-v4/dist/index.js';
|
|
2
2
|
import type { MastraDBMessage } from '@mastra/core/agent';
|
|
3
|
-
import type { MemoryConfig, SharedMemoryConfig, StorageThreadType, WorkingMemoryTemplate, MessageDeleteInput } from '@mastra/core/memory';
|
|
4
3
|
import { MastraMemory } from '@mastra/core/memory';
|
|
4
|
+
import type { MemoryConfig, SharedMemoryConfig, StorageThreadType, WorkingMemoryTemplate, MessageDeleteInput } from '@mastra/core/memory';
|
|
5
|
+
import type { InputProcessor, InputProcessorOrWorkflow, OutputProcessor, OutputProcessorOrWorkflow } from '@mastra/core/processors';
|
|
6
|
+
import type { RequestContext } from '@mastra/core/request-context';
|
|
5
7
|
import type { StorageListThreadsInput, StorageListThreadsOutput, StorageListMessagesInput, MemoryStorage, StorageCloneThreadInput, StorageCloneThreadOutput, ThreadCloneMetadata } from '@mastra/core/storage';
|
|
6
8
|
import type { ToolAction } from '@mastra/core/tools';
|
|
7
9
|
import { deepMergeWorkingMemory } from './tools/working-memory.js';
|
|
@@ -17,6 +19,35 @@ export declare class Memory extends MastraMemory {
|
|
|
17
19
|
* Gets the memory storage domain, throwing if not available.
|
|
18
20
|
*/
|
|
19
21
|
protected getMemoryStore(): Promise<MemoryStorage>;
|
|
22
|
+
listMessagesByResourceId(args: {
|
|
23
|
+
resourceId: string;
|
|
24
|
+
perPage?: number | false;
|
|
25
|
+
page?: number;
|
|
26
|
+
orderBy?: {
|
|
27
|
+
field?: 'createdAt';
|
|
28
|
+
direction?: 'ASC' | 'DESC';
|
|
29
|
+
};
|
|
30
|
+
filter?: {
|
|
31
|
+
dateRange?: {
|
|
32
|
+
start?: Date;
|
|
33
|
+
end?: Date;
|
|
34
|
+
startExclusive?: boolean;
|
|
35
|
+
endExclusive?: boolean;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
include?: Array<{
|
|
39
|
+
id: string;
|
|
40
|
+
threadId?: string;
|
|
41
|
+
withPreviousMessages?: number;
|
|
42
|
+
withNextMessages?: number;
|
|
43
|
+
}>;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
messages: MastraDBMessage[];
|
|
46
|
+
total: number;
|
|
47
|
+
page: number;
|
|
48
|
+
perPage: number | false;
|
|
49
|
+
hasMore: boolean;
|
|
50
|
+
}>;
|
|
20
51
|
protected validateThreadIsOwnedByResource(threadId: string, resourceId: string, config: MemoryConfig): Promise<void>;
|
|
21
52
|
recall(args: StorageListMessagesInput & {
|
|
22
53
|
threadConfig?: MemoryConfig;
|
|
@@ -24,6 +55,9 @@ export declare class Memory extends MastraMemory {
|
|
|
24
55
|
threadId: string;
|
|
25
56
|
}): Promise<{
|
|
26
57
|
messages: MastraDBMessage[];
|
|
58
|
+
usage?: {
|
|
59
|
+
tokens: number;
|
|
60
|
+
};
|
|
27
61
|
}>;
|
|
28
62
|
getThreadById({ threadId }: {
|
|
29
63
|
threadId: string;
|
|
@@ -68,6 +102,9 @@ export declare class Memory extends MastraMemory {
|
|
|
68
102
|
protected embedMessageContent(content: string): Promise<{
|
|
69
103
|
chunks: string[];
|
|
70
104
|
embeddings: Awaited<ReturnType<typeof embedMany>>["embeddings"];
|
|
105
|
+
usage?: {
|
|
106
|
+
tokens: number;
|
|
107
|
+
};
|
|
71
108
|
dimension: number | undefined;
|
|
72
109
|
}>;
|
|
73
110
|
saveMessages({ messages, memoryConfig, }: {
|
|
@@ -75,6 +112,9 @@ export declare class Memory extends MastraMemory {
|
|
|
75
112
|
memoryConfig?: MemoryConfig | undefined;
|
|
76
113
|
}): Promise<{
|
|
77
114
|
messages: MastraDBMessage[];
|
|
115
|
+
usage?: {
|
|
116
|
+
tokens: number;
|
|
117
|
+
};
|
|
78
118
|
}>;
|
|
79
119
|
protected updateMessageToHideWorkingMemoryV2(message: MastraDBMessage): MastraDBMessage | null;
|
|
80
120
|
protected parseWorkingMemory(text: string): string | null;
|
|
@@ -273,6 +313,27 @@ export declare class Memory extends MastraMemory {
|
|
|
273
313
|
* ```
|
|
274
314
|
*/
|
|
275
315
|
getCloneHistory(threadId: string): Promise<StorageThreadType[]>;
|
|
316
|
+
/**
|
|
317
|
+
* Get input processors for this memory instance.
|
|
318
|
+
* Extends the base implementation to add ObservationalMemory processor when configured.
|
|
319
|
+
*
|
|
320
|
+
* @param configuredProcessors - Processors already configured by the user (for deduplication)
|
|
321
|
+
* @param context - Request context for runtime configuration
|
|
322
|
+
* @returns Array of input processors configured for this memory instance
|
|
323
|
+
*/
|
|
324
|
+
getInputProcessors(configuredProcessors?: InputProcessorOrWorkflow[], context?: RequestContext): Promise<InputProcessor[]>;
|
|
325
|
+
/**
|
|
326
|
+
* Extends the base implementation to add ObservationalMemory as an output processor.
|
|
327
|
+
* OM needs processOutputResult to save messages at the end of the agent turn,
|
|
328
|
+
* even when the observation threshold was never reached during the loop.
|
|
329
|
+
*/
|
|
330
|
+
getOutputProcessors(configuredProcessors?: OutputProcessorOrWorkflow[], context?: RequestContext): Promise<OutputProcessor[]>;
|
|
331
|
+
/**
|
|
332
|
+
* Creates an ObservationalMemory processor instance if configured and not already present.
|
|
333
|
+
* A new instance is created per call — processorStates (e.g., sealedIds) are shared
|
|
334
|
+
* via the ProcessorRunner's state map keyed by processor ID, not by instance identity.
|
|
335
|
+
*/
|
|
336
|
+
private createOMProcessor;
|
|
276
337
|
}
|
|
277
338
|
export { SemanticRecall, WorkingMemory, MessageHistory } from '@mastra/core/processors';
|
|
278
339
|
export type { StorageCloneThreadInput, StorageCloneThreadOutput, ThreadCloneMetadata } from '@mastra/core/storage';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAKhD,OAAO,KAAK,EAAE,eAAe,EAAE,MA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|
|
7
7
|
import { MessageList } from '@mastra/core/agent';
|
|
8
|
+
import { coreFeatures } from '@mastra/core/features';
|
|
8
9
|
import { MastraMemory, removeWorkingMemoryTags, extractWorkingMemoryContent } from '@mastra/core/memory';
|
|
9
10
|
export { extractWorkingMemoryContent, extractWorkingMemoryTags, removeWorkingMemoryTags } from '@mastra/core/memory';
|
|
10
11
|
import { generateEmptyFromSchema } from '@mastra/core/utils';
|
|
@@ -14576,6 +14577,12 @@ var __experimental_updateWorkingMemoryToolVNext = (config) => {
|
|
|
14576
14577
|
}
|
|
14577
14578
|
});
|
|
14578
14579
|
};
|
|
14580
|
+
function normalizeObservationalMemoryConfig(config) {
|
|
14581
|
+
if (config === true) return {};
|
|
14582
|
+
if (config === false || config === void 0) return void 0;
|
|
14583
|
+
if (typeof config === "object" && config.enabled === false) return void 0;
|
|
14584
|
+
return config;
|
|
14585
|
+
}
|
|
14579
14586
|
var CHARS_PER_TOKEN = 4;
|
|
14580
14587
|
var DEFAULT_MESSAGE_RANGE = { before: 1, after: 1 };
|
|
14581
14588
|
var DEFAULT_TOP_K = 4;
|
|
@@ -14590,7 +14597,8 @@ var Memory = class extends MastraMemory {
|
|
|
14590
14597
|
// and someone bumps @mastra/memory without bumping @mastra/core the defaults wouldn't exist yet
|
|
14591
14598
|
enabled: false,
|
|
14592
14599
|
template: this.defaultWorkingMemoryTemplate
|
|
14593
|
-
}
|
|
14600
|
+
},
|
|
14601
|
+
observationalMemory: config.options?.observationalMemory
|
|
14594
14602
|
});
|
|
14595
14603
|
this.threadConfig = mergedConfig;
|
|
14596
14604
|
}
|
|
@@ -14604,6 +14612,10 @@ var Memory = class extends MastraMemory {
|
|
|
14604
14612
|
}
|
|
14605
14613
|
return store;
|
|
14606
14614
|
}
|
|
14615
|
+
async listMessagesByResourceId(args) {
|
|
14616
|
+
const memoryStore = await this.getMemoryStore();
|
|
14617
|
+
return memoryStore.listMessagesByResourceId(args);
|
|
14618
|
+
}
|
|
14607
14619
|
async validateThreadIsOwnedByResource(threadId, resourceId, config) {
|
|
14608
14620
|
const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope !== `thread` || config.semanticRecall === true;
|
|
14609
14621
|
const thread = await this.getThreadById({ threadId });
|
|
@@ -14648,8 +14660,11 @@ var Memory = class extends MastraMemory {
|
|
|
14648
14660
|
`Memory error: Resource-scoped semantic recall is enabled but no resourceId was provided. Either provide a resourceId or explicitly set semanticRecall.scope to 'thread'.`
|
|
14649
14661
|
);
|
|
14650
14662
|
}
|
|
14663
|
+
let usage;
|
|
14651
14664
|
if (config?.semanticRecall && vectorSearchString && this.vector) {
|
|
14652
|
-
const
|
|
14665
|
+
const result = await this.embedMessageContent(vectorSearchString);
|
|
14666
|
+
usage = result.usage;
|
|
14667
|
+
const { embeddings, dimension } = result;
|
|
14653
14668
|
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
14654
14669
|
await Promise.all(
|
|
14655
14670
|
embeddings.map(async (embedding) => {
|
|
@@ -14693,7 +14708,7 @@ var Memory = class extends MastraMemory {
|
|
|
14693
14708
|
const rawMessages = shouldGetNewestAndReverse ? paginatedResult.messages.reverse() : paginatedResult.messages;
|
|
14694
14709
|
const list = new MessageList({ threadId, resourceId }).add(rawMessages, "memory");
|
|
14695
14710
|
const messages = list.get.all.db();
|
|
14696
|
-
return { messages };
|
|
14711
|
+
return { messages, usage };
|
|
14697
14712
|
}
|
|
14698
14713
|
async getThreadById({ threadId }) {
|
|
14699
14714
|
const memoryStore = await this.getMemoryStore();
|
|
@@ -14940,10 +14955,11 @@ ${workingMemory}`;
|
|
|
14940
14955
|
...this.embedderOptions || {}
|
|
14941
14956
|
});
|
|
14942
14957
|
if (isFastEmbed && !this.firstEmbed) this.firstEmbed = promise;
|
|
14943
|
-
const { embeddings } = await promise;
|
|
14958
|
+
const { embeddings, usage } = await promise;
|
|
14944
14959
|
const result = {
|
|
14945
14960
|
embeddings,
|
|
14946
14961
|
chunks,
|
|
14962
|
+
usage,
|
|
14947
14963
|
dimension: embeddings[0]?.length
|
|
14948
14964
|
};
|
|
14949
14965
|
this.embeddingCache.set(key, result);
|
|
@@ -14964,6 +14980,7 @@ ${workingMemory}`;
|
|
|
14964
14980
|
const result = await memoryStore.saveMessages({
|
|
14965
14981
|
messages: dbMessages
|
|
14966
14982
|
});
|
|
14983
|
+
let totalTokens = 0;
|
|
14967
14984
|
if (this.vector && config.semanticRecall) {
|
|
14968
14985
|
const embeddingData = [];
|
|
14969
14986
|
let dimension;
|
|
@@ -14979,6 +14996,9 @@ ${workingMemory}`;
|
|
|
14979
14996
|
if (!textForEmbedding) return;
|
|
14980
14997
|
const result2 = await this.embedMessageContent(textForEmbedding);
|
|
14981
14998
|
dimension = result2.dimension;
|
|
14999
|
+
if (result2.usage?.tokens) {
|
|
15000
|
+
totalTokens += result2.usage.tokens;
|
|
15001
|
+
}
|
|
14982
15002
|
embeddingData.push({
|
|
14983
15003
|
embeddings: result2.embeddings,
|
|
14984
15004
|
metadata: result2.chunks.map(() => ({
|
|
@@ -15007,7 +15027,7 @@ ${workingMemory}`;
|
|
|
15007
15027
|
});
|
|
15008
15028
|
}
|
|
15009
15029
|
}
|
|
15010
|
-
return result;
|
|
15030
|
+
return { ...result, usage: totalTokens > 0 ? { tokens: totalTokens } : void 0 };
|
|
15011
15031
|
}
|
|
15012
15032
|
updateMessageToHideWorkingMemoryV2(message) {
|
|
15013
15033
|
const newMessage = { ...message };
|
|
@@ -15613,6 +15633,89 @@ Notes:
|
|
|
15613
15633
|
}
|
|
15614
15634
|
return history;
|
|
15615
15635
|
}
|
|
15636
|
+
/**
|
|
15637
|
+
* Get input processors for this memory instance.
|
|
15638
|
+
* Extends the base implementation to add ObservationalMemory processor when configured.
|
|
15639
|
+
*
|
|
15640
|
+
* @param configuredProcessors - Processors already configured by the user (for deduplication)
|
|
15641
|
+
* @param context - Request context for runtime configuration
|
|
15642
|
+
* @returns Array of input processors configured for this memory instance
|
|
15643
|
+
*/
|
|
15644
|
+
async getInputProcessors(configuredProcessors = [], context) {
|
|
15645
|
+
const processors = await super.getInputProcessors(configuredProcessors, context);
|
|
15646
|
+
const om = await this.createOMProcessor(configuredProcessors, context);
|
|
15647
|
+
if (om) {
|
|
15648
|
+
processors.push(om);
|
|
15649
|
+
}
|
|
15650
|
+
return processors;
|
|
15651
|
+
}
|
|
15652
|
+
/**
|
|
15653
|
+
* Extends the base implementation to add ObservationalMemory as an output processor.
|
|
15654
|
+
* OM needs processOutputResult to save messages at the end of the agent turn,
|
|
15655
|
+
* even when the observation threshold was never reached during the loop.
|
|
15656
|
+
*/
|
|
15657
|
+
async getOutputProcessors(configuredProcessors = [], context) {
|
|
15658
|
+
const processors = await super.getOutputProcessors(configuredProcessors, context);
|
|
15659
|
+
const om = await this.createOMProcessor(configuredProcessors, context);
|
|
15660
|
+
if (om) {
|
|
15661
|
+
processors.push(om);
|
|
15662
|
+
}
|
|
15663
|
+
return processors;
|
|
15664
|
+
}
|
|
15665
|
+
/**
|
|
15666
|
+
* Creates an ObservationalMemory processor instance if configured and not already present.
|
|
15667
|
+
* A new instance is created per call — processorStates (e.g., sealedIds) are shared
|
|
15668
|
+
* via the ProcessorRunner's state map keyed by processor ID, not by instance identity.
|
|
15669
|
+
*/
|
|
15670
|
+
async createOMProcessor(configuredProcessors = [], context) {
|
|
15671
|
+
const hasObservationalMemory = configuredProcessors.some(
|
|
15672
|
+
(p) => !("workflow" in p) && p.id === "observational-memory"
|
|
15673
|
+
);
|
|
15674
|
+
const memoryContext = context?.get("MastraMemory");
|
|
15675
|
+
const runtimeMemoryConfig = memoryContext?.memoryConfig;
|
|
15676
|
+
const effectiveConfig = runtimeMemoryConfig ? this.getMergedThreadConfig(runtimeMemoryConfig) : this.threadConfig;
|
|
15677
|
+
const omConfig = normalizeObservationalMemoryConfig(effectiveConfig.observationalMemory);
|
|
15678
|
+
if (!omConfig || hasObservationalMemory) {
|
|
15679
|
+
return null;
|
|
15680
|
+
}
|
|
15681
|
+
const coreSupportsOM = coreFeatures.has("observationalMemory");
|
|
15682
|
+
if (!coreSupportsOM) {
|
|
15683
|
+
throw new Error(
|
|
15684
|
+
"Observational memory is enabled but the installed version of @mastra/core does not support it. Please upgrade @mastra/core to a version that includes observational memory support."
|
|
15685
|
+
);
|
|
15686
|
+
}
|
|
15687
|
+
const memoryStore = await this.storage.getStore("memory");
|
|
15688
|
+
if (!memoryStore) {
|
|
15689
|
+
throw new Error(
|
|
15690
|
+
"Using Mastra Memory observational memory requires a storage adapter but no attached adapter was detected."
|
|
15691
|
+
);
|
|
15692
|
+
}
|
|
15693
|
+
if (!memoryStore.supportsObservationalMemory) {
|
|
15694
|
+
throw new Error(
|
|
15695
|
+
`Observational memory is enabled but the storage adapter (${memoryStore.constructor.name}) does not support it. If you're using @mastra/libsql, @mastra/pg, or @mastra/mongodb, upgrade to the latest version. Otherwise, use one of those adapters or disable observational memory.`
|
|
15696
|
+
);
|
|
15697
|
+
}
|
|
15698
|
+
const { ObservationalMemory } = await import('./observational-memory-VXLHOSDZ.js');
|
|
15699
|
+
return new ObservationalMemory({
|
|
15700
|
+
storage: memoryStore,
|
|
15701
|
+
scope: omConfig.scope,
|
|
15702
|
+
shareTokenBudget: omConfig.shareTokenBudget,
|
|
15703
|
+
model: omConfig.model,
|
|
15704
|
+
observation: omConfig.observation ? {
|
|
15705
|
+
model: omConfig.observation.model,
|
|
15706
|
+
messageTokens: omConfig.observation.messageTokens,
|
|
15707
|
+
modelSettings: omConfig.observation.modelSettings,
|
|
15708
|
+
maxTokensPerBatch: omConfig.observation.maxTokensPerBatch,
|
|
15709
|
+
providerOptions: omConfig.observation.providerOptions
|
|
15710
|
+
} : void 0,
|
|
15711
|
+
reflection: omConfig.reflection ? {
|
|
15712
|
+
model: omConfig.reflection.model,
|
|
15713
|
+
observationTokens: omConfig.reflection.observationTokens,
|
|
15714
|
+
modelSettings: omConfig.reflection.modelSettings,
|
|
15715
|
+
providerOptions: omConfig.reflection.providerOptions
|
|
15716
|
+
} : void 0
|
|
15717
|
+
});
|
|
15718
|
+
}
|
|
15616
15719
|
};
|
|
15617
15720
|
|
|
15618
15721
|
export { Memory, deepMergeWorkingMemory };
|