@mastra/memory 0.3.4 → 0.4.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/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +35 -0
- package/dist/index.cjs +20 -11
- package/dist/index.js +21 -12
- package/package.json +6 -3
- package/src/index.ts +27 -14
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/memory@0.
|
|
2
|
+
> @mastra/memory@0.4.0-alpha.1 build /home/runner/work/mastra/mastra/packages/memory
|
|
3
3
|
> pnpm run check && tsup src/index.ts src/processors/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
> @mastra/memory@0.
|
|
6
|
+
> @mastra/memory@0.4.0-alpha.1 check /home/runner/work/mastra/mastra/packages/memory
|
|
7
7
|
> tsc --noEmit
|
|
8
8
|
|
|
9
9
|
[34mCLI[39m Building entry: src/index.ts, src/processors/index.ts
|
|
10
10
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
11
11
|
[34mCLI[39m tsup v8.4.0
|
|
12
12
|
[34mTSC[39m Build start
|
|
13
|
-
[32mTSC[39m ⚡️ Build success in
|
|
13
|
+
[32mTSC[39m ⚡️ Build success in 9760ms
|
|
14
14
|
[34mDTS[39m Build start
|
|
15
15
|
[34mCLI[39m Target: es2022
|
|
16
16
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
17
17
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.ts[39m
|
|
18
18
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
19
19
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.cts[39m
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 11637ms
|
|
21
21
|
[34mCLI[39m Cleaning output folder
|
|
22
22
|
[34mESM[39m Build start
|
|
23
23
|
[34mCJS[39m Build start
|
|
24
|
-
[
|
|
25
|
-
[32mESM[39m [1mdist/processors/index.js [22m[32m5.38 KB[39m
|
|
26
|
-
[32mESM[39m ⚡️ Build success in 1009ms
|
|
27
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m18.85 KB[39m
|
|
24
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m19.00 KB[39m
|
|
28
25
|
[32mCJS[39m [1mdist/processors/index.cjs [22m[32m5.59 KB[39m
|
|
29
|
-
[32mCJS[39m ⚡️ Build success in
|
|
26
|
+
[32mCJS[39m ⚡️ Build success in 995ms
|
|
27
|
+
[32mESM[39m [1mdist/index.js [22m[32m18.81 KB[39m
|
|
28
|
+
[32mESM[39m [1mdist/processors/index.js [22m[32m5.38 KB[39m
|
|
29
|
+
[32mESM[39m ⚡️ Build success in 995ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# @mastra/memory
|
|
2
2
|
|
|
3
|
+
## 0.4.0-alpha.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 83da932: Move @mastra/core to peerdeps
|
|
8
|
+
- 0dcb9f0: Memory breaking changes: storage, vector, and embedder are now required. Working memory text streaming has been removed, only tool calling is supported for working memory updates now. Default settings have changed (lastMessages: 40->10, semanticRecall: true->false, threads.generateTitle: true->false)
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- Updated dependencies [b3a3d63]
|
|
13
|
+
- Updated dependencies [344f453]
|
|
14
|
+
- Updated dependencies [0a3ae6d]
|
|
15
|
+
- Updated dependencies [95911be]
|
|
16
|
+
- Updated dependencies [5eb5a99]
|
|
17
|
+
- Updated dependencies [7e632c5]
|
|
18
|
+
- Updated dependencies [1e9fbfa]
|
|
19
|
+
- Updated dependencies [b2ae5aa]
|
|
20
|
+
- Updated dependencies [a7292b0]
|
|
21
|
+
- Updated dependencies [0dcb9f0]
|
|
22
|
+
- @mastra/core@0.10.0-alpha.1
|
|
23
|
+
|
|
24
|
+
## 0.3.5-alpha.0
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- 3cd7aee: [MASTRA-3439] Working Memory tool call fix: Updated saveMessages to filter out workingmemory content from messages, rather than skip message completely
|
|
29
|
+
- Updated dependencies [f53a6ac]
|
|
30
|
+
- Updated dependencies [eabdcd9]
|
|
31
|
+
- Updated dependencies [90be034]
|
|
32
|
+
- Updated dependencies [99f050a]
|
|
33
|
+
- Updated dependencies [d0ee3c6]
|
|
34
|
+
- Updated dependencies [23f258c]
|
|
35
|
+
- Updated dependencies [2672a05]
|
|
36
|
+
- @mastra/core@0.9.5-alpha.0
|
|
37
|
+
|
|
3
38
|
## 0.3.4
|
|
4
39
|
|
|
5
40
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -82,6 +82,8 @@ function reorderToolCallsAndResults(messages) {
|
|
|
82
82
|
|
|
83
83
|
// src/index.ts
|
|
84
84
|
var CHARS_PER_TOKEN = 4;
|
|
85
|
+
var DEFAULT_MESSAGE_RANGE = { before: 2, after: 2 };
|
|
86
|
+
var DEFAULT_TOP_K = 2;
|
|
85
87
|
var Memory = class extends memory.MastraMemory {
|
|
86
88
|
constructor(config = {}) {
|
|
87
89
|
super({ name: "Memory", ...config });
|
|
@@ -97,6 +99,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
97
99
|
this.threadConfig = mergedConfig;
|
|
98
100
|
}
|
|
99
101
|
async validateThreadIsOwnedByResource(threadId, resourceId) {
|
|
102
|
+
await this.storage.init();
|
|
100
103
|
const thread = await this.storage.getThreadById({ threadId });
|
|
101
104
|
if (!thread) {
|
|
102
105
|
throw new Error(`No thread found with id ${threadId}`);
|
|
@@ -121,8 +124,8 @@ var Memory = class extends memory.MastraMemory {
|
|
|
121
124
|
threadConfig
|
|
122
125
|
});
|
|
123
126
|
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
124
|
-
const defaultRange =
|
|
125
|
-
const defaultTopK =
|
|
127
|
+
const defaultRange = DEFAULT_MESSAGE_RANGE;
|
|
128
|
+
const defaultTopK = DEFAULT_TOP_K;
|
|
126
129
|
const vectorConfig = typeof config?.semanticRecall === `boolean` ? {
|
|
127
130
|
topK: defaultTopK,
|
|
128
131
|
messageRange: defaultRange
|
|
@@ -153,6 +156,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
153
156
|
})
|
|
154
157
|
);
|
|
155
158
|
}
|
|
159
|
+
await this.storage.init();
|
|
156
160
|
const rawMessages = await this.storage.getMessages({
|
|
157
161
|
threadId,
|
|
158
162
|
selectBy: {
|
|
@@ -204,15 +208,18 @@ var Memory = class extends memory.MastraMemory {
|
|
|
204
208
|
};
|
|
205
209
|
}
|
|
206
210
|
async getThreadById({ threadId }) {
|
|
211
|
+
await this.storage.init();
|
|
207
212
|
return this.storage.getThreadById({ threadId });
|
|
208
213
|
}
|
|
209
214
|
async getThreadsByResourceId({ resourceId }) {
|
|
215
|
+
await this.storage.init();
|
|
210
216
|
return this.storage.getThreadsByResourceId({ resourceId });
|
|
211
217
|
}
|
|
212
218
|
async saveThread({
|
|
213
219
|
thread,
|
|
214
220
|
memoryConfig
|
|
215
221
|
}) {
|
|
222
|
+
await this.storage.init();
|
|
216
223
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
217
224
|
if (config.workingMemory?.enabled && !thread?.metadata?.workingMemory) {
|
|
218
225
|
return this.storage.saveThread({
|
|
@@ -230,6 +237,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
230
237
|
title,
|
|
231
238
|
metadata
|
|
232
239
|
}) {
|
|
240
|
+
await this.storage.init();
|
|
233
241
|
return this.storage.updateThread({
|
|
234
242
|
id,
|
|
235
243
|
title,
|
|
@@ -237,6 +245,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
237
245
|
});
|
|
238
246
|
}
|
|
239
247
|
async deleteThread(threadId) {
|
|
248
|
+
await this.storage.init();
|
|
240
249
|
await this.storage.deleteThread({ threadId });
|
|
241
250
|
}
|
|
242
251
|
chunkText(text, tokenSize = 4096) {
|
|
@@ -293,6 +302,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
293
302
|
messages,
|
|
294
303
|
memoryConfig
|
|
295
304
|
}) {
|
|
305
|
+
await this.storage.init();
|
|
296
306
|
await this.saveWorkingMemory(messages);
|
|
297
307
|
const updatedMessages = this.updateMessagesToHideWorkingMemory(messages);
|
|
298
308
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
@@ -342,13 +352,13 @@ var Memory = class extends memory.MastraMemory {
|
|
|
342
352
|
content: message.content.replace(workingMemoryRegex, ``).trim()
|
|
343
353
|
});
|
|
344
354
|
} else if (Array.isArray(message?.content)) {
|
|
345
|
-
const
|
|
346
|
-
(content) => (content.type ===
|
|
355
|
+
const filteredContent = message.content.filter(
|
|
356
|
+
(content) => !((content.type === "tool-call" || content.type === "tool-result") && content.toolName === "updateWorkingMemory")
|
|
347
357
|
);
|
|
348
|
-
if (
|
|
358
|
+
if (filteredContent.length === 0) {
|
|
349
359
|
continue;
|
|
350
360
|
}
|
|
351
|
-
const newContent =
|
|
361
|
+
const newContent = filteredContent.map((content) => {
|
|
352
362
|
if (content.type === "text") {
|
|
353
363
|
return {
|
|
354
364
|
...content,
|
|
@@ -376,6 +386,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
376
386
|
}
|
|
377
387
|
async getWorkingMemory({ threadId }) {
|
|
378
388
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
389
|
+
await this.storage.init();
|
|
379
390
|
const thread = await this.storage.getThreadById({ threadId });
|
|
380
391
|
if (!thread) return this.threadConfig?.workingMemory?.template || this.defaultWorkingMemoryTemplate;
|
|
381
392
|
const memory = thread.metadata?.workingMemory || this.threadConfig.workingMemory.template || this.defaultWorkingMemoryTemplate;
|
|
@@ -395,6 +406,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
395
406
|
if (!newMemory) {
|
|
396
407
|
return;
|
|
397
408
|
}
|
|
409
|
+
await this.storage.init();
|
|
398
410
|
const thread = await this.storage.getThreadById({ threadId });
|
|
399
411
|
if (!thread) return;
|
|
400
412
|
await this.storage.updateThread({
|
|
@@ -418,10 +430,7 @@ var Memory = class extends memory.MastraMemory {
|
|
|
418
430
|
if (!workingMemory) {
|
|
419
431
|
return null;
|
|
420
432
|
}
|
|
421
|
-
|
|
422
|
-
return this.getWorkingMemoryToolInstruction(workingMemory);
|
|
423
|
-
}
|
|
424
|
-
return this.getWorkingMemoryWithInstruction(workingMemory);
|
|
433
|
+
return this.getWorkingMemoryToolInstruction(workingMemory);
|
|
425
434
|
}
|
|
426
435
|
defaultWorkingMemoryTemplate = `
|
|
427
436
|
# User Information
|
|
@@ -482,7 +491,7 @@ Notes:
|
|
|
482
491
|
}
|
|
483
492
|
getTools(config) {
|
|
484
493
|
const mergedConfig = this.getMergedThreadConfig(config);
|
|
485
|
-
if (mergedConfig.workingMemory?.enabled
|
|
494
|
+
if (mergedConfig.workingMemory?.enabled) {
|
|
486
495
|
return {
|
|
487
496
|
updateWorkingMemory: updateWorkingMemoryTool
|
|
488
497
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { deepMerge } from '@mastra/core';
|
|
2
2
|
import { MastraMemory } from '@mastra/core/memory';
|
|
3
3
|
import { embedMany } from 'ai';
|
|
4
4
|
import xxhash from 'xxhash-wasm';
|
|
@@ -76,6 +76,8 @@ function reorderToolCallsAndResults(messages) {
|
|
|
76
76
|
|
|
77
77
|
// src/index.ts
|
|
78
78
|
var CHARS_PER_TOKEN = 4;
|
|
79
|
+
var DEFAULT_MESSAGE_RANGE = { before: 2, after: 2 };
|
|
80
|
+
var DEFAULT_TOP_K = 2;
|
|
79
81
|
var Memory = class extends MastraMemory {
|
|
80
82
|
constructor(config = {}) {
|
|
81
83
|
super({ name: "Memory", ...config });
|
|
@@ -91,6 +93,7 @@ var Memory = class extends MastraMemory {
|
|
|
91
93
|
this.threadConfig = mergedConfig;
|
|
92
94
|
}
|
|
93
95
|
async validateThreadIsOwnedByResource(threadId, resourceId) {
|
|
96
|
+
await this.storage.init();
|
|
94
97
|
const thread = await this.storage.getThreadById({ threadId });
|
|
95
98
|
if (!thread) {
|
|
96
99
|
throw new Error(`No thread found with id ${threadId}`);
|
|
@@ -115,8 +118,8 @@ var Memory = class extends MastraMemory {
|
|
|
115
118
|
threadConfig
|
|
116
119
|
});
|
|
117
120
|
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
118
|
-
const defaultRange =
|
|
119
|
-
const defaultTopK =
|
|
121
|
+
const defaultRange = DEFAULT_MESSAGE_RANGE;
|
|
122
|
+
const defaultTopK = DEFAULT_TOP_K;
|
|
120
123
|
const vectorConfig = typeof config?.semanticRecall === `boolean` ? {
|
|
121
124
|
topK: defaultTopK,
|
|
122
125
|
messageRange: defaultRange
|
|
@@ -147,6 +150,7 @@ var Memory = class extends MastraMemory {
|
|
|
147
150
|
})
|
|
148
151
|
);
|
|
149
152
|
}
|
|
153
|
+
await this.storage.init();
|
|
150
154
|
const rawMessages = await this.storage.getMessages({
|
|
151
155
|
threadId,
|
|
152
156
|
selectBy: {
|
|
@@ -198,15 +202,18 @@ var Memory = class extends MastraMemory {
|
|
|
198
202
|
};
|
|
199
203
|
}
|
|
200
204
|
async getThreadById({ threadId }) {
|
|
205
|
+
await this.storage.init();
|
|
201
206
|
return this.storage.getThreadById({ threadId });
|
|
202
207
|
}
|
|
203
208
|
async getThreadsByResourceId({ resourceId }) {
|
|
209
|
+
await this.storage.init();
|
|
204
210
|
return this.storage.getThreadsByResourceId({ resourceId });
|
|
205
211
|
}
|
|
206
212
|
async saveThread({
|
|
207
213
|
thread,
|
|
208
214
|
memoryConfig
|
|
209
215
|
}) {
|
|
216
|
+
await this.storage.init();
|
|
210
217
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
211
218
|
if (config.workingMemory?.enabled && !thread?.metadata?.workingMemory) {
|
|
212
219
|
return this.storage.saveThread({
|
|
@@ -224,6 +231,7 @@ var Memory = class extends MastraMemory {
|
|
|
224
231
|
title,
|
|
225
232
|
metadata
|
|
226
233
|
}) {
|
|
234
|
+
await this.storage.init();
|
|
227
235
|
return this.storage.updateThread({
|
|
228
236
|
id,
|
|
229
237
|
title,
|
|
@@ -231,6 +239,7 @@ var Memory = class extends MastraMemory {
|
|
|
231
239
|
});
|
|
232
240
|
}
|
|
233
241
|
async deleteThread(threadId) {
|
|
242
|
+
await this.storage.init();
|
|
234
243
|
await this.storage.deleteThread({ threadId });
|
|
235
244
|
}
|
|
236
245
|
chunkText(text, tokenSize = 4096) {
|
|
@@ -287,6 +296,7 @@ var Memory = class extends MastraMemory {
|
|
|
287
296
|
messages,
|
|
288
297
|
memoryConfig
|
|
289
298
|
}) {
|
|
299
|
+
await this.storage.init();
|
|
290
300
|
await this.saveWorkingMemory(messages);
|
|
291
301
|
const updatedMessages = this.updateMessagesToHideWorkingMemory(messages);
|
|
292
302
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
@@ -336,13 +346,13 @@ var Memory = class extends MastraMemory {
|
|
|
336
346
|
content: message.content.replace(workingMemoryRegex, ``).trim()
|
|
337
347
|
});
|
|
338
348
|
} else if (Array.isArray(message?.content)) {
|
|
339
|
-
const
|
|
340
|
-
(content) => (content.type ===
|
|
349
|
+
const filteredContent = message.content.filter(
|
|
350
|
+
(content) => !((content.type === "tool-call" || content.type === "tool-result") && content.toolName === "updateWorkingMemory")
|
|
341
351
|
);
|
|
342
|
-
if (
|
|
352
|
+
if (filteredContent.length === 0) {
|
|
343
353
|
continue;
|
|
344
354
|
}
|
|
345
|
-
const newContent =
|
|
355
|
+
const newContent = filteredContent.map((content) => {
|
|
346
356
|
if (content.type === "text") {
|
|
347
357
|
return {
|
|
348
358
|
...content,
|
|
@@ -370,6 +380,7 @@ var Memory = class extends MastraMemory {
|
|
|
370
380
|
}
|
|
371
381
|
async getWorkingMemory({ threadId }) {
|
|
372
382
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
383
|
+
await this.storage.init();
|
|
373
384
|
const thread = await this.storage.getThreadById({ threadId });
|
|
374
385
|
if (!thread) return this.threadConfig?.workingMemory?.template || this.defaultWorkingMemoryTemplate;
|
|
375
386
|
const memory = thread.metadata?.workingMemory || this.threadConfig.workingMemory.template || this.defaultWorkingMemoryTemplate;
|
|
@@ -389,6 +400,7 @@ var Memory = class extends MastraMemory {
|
|
|
389
400
|
if (!newMemory) {
|
|
390
401
|
return;
|
|
391
402
|
}
|
|
403
|
+
await this.storage.init();
|
|
392
404
|
const thread = await this.storage.getThreadById({ threadId });
|
|
393
405
|
if (!thread) return;
|
|
394
406
|
await this.storage.updateThread({
|
|
@@ -412,10 +424,7 @@ var Memory = class extends MastraMemory {
|
|
|
412
424
|
if (!workingMemory) {
|
|
413
425
|
return null;
|
|
414
426
|
}
|
|
415
|
-
|
|
416
|
-
return this.getWorkingMemoryToolInstruction(workingMemory);
|
|
417
|
-
}
|
|
418
|
-
return this.getWorkingMemoryWithInstruction(workingMemory);
|
|
427
|
+
return this.getWorkingMemoryToolInstruction(workingMemory);
|
|
419
428
|
}
|
|
420
429
|
defaultWorkingMemoryTemplate = `
|
|
421
430
|
# User Information
|
|
@@ -476,7 +485,7 @@ Notes:
|
|
|
476
485
|
}
|
|
477
486
|
getTools(config) {
|
|
478
487
|
const mergedConfig = this.getMergedThreadConfig(config);
|
|
479
|
-
if (mergedConfig.workingMemory?.enabled
|
|
488
|
+
if (mergedConfig.workingMemory?.enabled) {
|
|
480
489
|
return {
|
|
481
490
|
updateWorkingMemory: updateWorkingMemoryTool
|
|
482
491
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/memory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -40,8 +40,7 @@
|
|
|
40
40
|
"postgres": "^3.4.5",
|
|
41
41
|
"redis": "^4.7.0",
|
|
42
42
|
"xxhash-wasm": "^1.1.0",
|
|
43
|
-
"zod": "^3.24.3"
|
|
44
|
-
"@mastra/core": "^0.9.4"
|
|
43
|
+
"zod": "^3.24.3"
|
|
45
44
|
},
|
|
46
45
|
"devDependencies": {
|
|
47
46
|
"@ai-sdk/openai": "^1.3.3",
|
|
@@ -53,8 +52,12 @@
|
|
|
53
52
|
"typescript": "^5.8.2",
|
|
54
53
|
"typescript-eslint": "^8.26.1",
|
|
55
54
|
"vitest": "^3.1.2",
|
|
55
|
+
"@mastra/core": "0.10.0-alpha.1",
|
|
56
56
|
"@internal/lint": "0.0.5"
|
|
57
57
|
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@mastra/core": "^0.9.4"
|
|
60
|
+
},
|
|
58
61
|
"scripts": {
|
|
59
62
|
"check": "tsc --noEmit",
|
|
60
63
|
"build": "pnpm run check && tsup src/index.ts src/processors/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { deepMerge
|
|
1
|
+
import { deepMerge } from '@mastra/core';
|
|
2
2
|
import type { AiMessageType, CoreMessage, CoreTool } from '@mastra/core';
|
|
3
3
|
import { MastraMemory } from '@mastra/core/memory';
|
|
4
4
|
import type { MessageType, MemoryConfig, SharedMemoryConfig, StorageThreadType } from '@mastra/core/memory';
|
|
@@ -13,6 +13,9 @@ import { reorderToolCallsAndResults } from './utils';
|
|
|
13
13
|
// Average characters per token based on OpenAI's tokenization
|
|
14
14
|
const CHARS_PER_TOKEN = 4;
|
|
15
15
|
|
|
16
|
+
const DEFAULT_MESSAGE_RANGE = { before: 2, after: 2 } as const;
|
|
17
|
+
const DEFAULT_TOP_K = 2;
|
|
18
|
+
|
|
16
19
|
/**
|
|
17
20
|
* Concrete implementation of MastraMemory that adds support for thread configuration
|
|
18
21
|
* and message injection.
|
|
@@ -34,6 +37,7 @@ export class Memory extends MastraMemory {
|
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
private async validateThreadIsOwnedByResource(threadId: string, resourceId: string) {
|
|
40
|
+
await this.storage.init();
|
|
37
41
|
const thread = await this.storage.getThreadById({ threadId });
|
|
38
42
|
if (!thread) {
|
|
39
43
|
throw new Error(`No thread found with id ${threadId}`);
|
|
@@ -70,8 +74,8 @@ export class Memory extends MastraMemory {
|
|
|
70
74
|
|
|
71
75
|
const config = this.getMergedThreadConfig(threadConfig || {});
|
|
72
76
|
|
|
73
|
-
const defaultRange =
|
|
74
|
-
const defaultTopK =
|
|
77
|
+
const defaultRange = DEFAULT_MESSAGE_RANGE;
|
|
78
|
+
const defaultTopK = DEFAULT_TOP_K;
|
|
75
79
|
|
|
76
80
|
const vectorConfig =
|
|
77
81
|
typeof config?.semanticRecall === `boolean`
|
|
@@ -110,6 +114,7 @@ export class Memory extends MastraMemory {
|
|
|
110
114
|
);
|
|
111
115
|
}
|
|
112
116
|
|
|
117
|
+
await this.storage.init();
|
|
113
118
|
// Get raw messages from storage
|
|
114
119
|
const rawMessages = await this.storage.getMessages({
|
|
115
120
|
threadId,
|
|
@@ -190,10 +195,12 @@ export class Memory extends MastraMemory {
|
|
|
190
195
|
}
|
|
191
196
|
|
|
192
197
|
async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
|
|
198
|
+
await this.storage.init();
|
|
193
199
|
return this.storage.getThreadById({ threadId });
|
|
194
200
|
}
|
|
195
201
|
|
|
196
202
|
async getThreadsByResourceId({ resourceId }: { resourceId: string }): Promise<StorageThreadType[]> {
|
|
203
|
+
await this.storage.init();
|
|
197
204
|
return this.storage.getThreadsByResourceId({ resourceId });
|
|
198
205
|
}
|
|
199
206
|
|
|
@@ -204,6 +211,7 @@ export class Memory extends MastraMemory {
|
|
|
204
211
|
thread: StorageThreadType;
|
|
205
212
|
memoryConfig?: MemoryConfig;
|
|
206
213
|
}): Promise<StorageThreadType> {
|
|
214
|
+
await this.storage.init();
|
|
207
215
|
const config = this.getMergedThreadConfig(memoryConfig || {});
|
|
208
216
|
|
|
209
217
|
if (config.workingMemory?.enabled && !thread?.metadata?.workingMemory) {
|
|
@@ -229,6 +237,7 @@ export class Memory extends MastraMemory {
|
|
|
229
237
|
title: string;
|
|
230
238
|
metadata: Record<string, unknown>;
|
|
231
239
|
}): Promise<StorageThreadType> {
|
|
240
|
+
await this.storage.init();
|
|
232
241
|
return this.storage.updateThread({
|
|
233
242
|
id,
|
|
234
243
|
title,
|
|
@@ -237,6 +246,7 @@ export class Memory extends MastraMemory {
|
|
|
237
246
|
}
|
|
238
247
|
|
|
239
248
|
async deleteThread(threadId: string): Promise<void> {
|
|
249
|
+
await this.storage.init();
|
|
240
250
|
await this.storage.deleteThread({ threadId });
|
|
241
251
|
}
|
|
242
252
|
|
|
@@ -324,6 +334,7 @@ export class Memory extends MastraMemory {
|
|
|
324
334
|
messages: MessageType[];
|
|
325
335
|
memoryConfig?: MemoryConfig;
|
|
326
336
|
}): Promise<MessageType[]> {
|
|
337
|
+
await this.storage.init();
|
|
327
338
|
// First save working memory from any messages
|
|
328
339
|
await this.saveWorkingMemory(messages);
|
|
329
340
|
|
|
@@ -394,15 +405,19 @@ export class Memory extends MastraMemory {
|
|
|
394
405
|
content: message.content.replace(workingMemoryRegex, ``).trim(),
|
|
395
406
|
});
|
|
396
407
|
} else if (Array.isArray(message?.content)) {
|
|
397
|
-
|
|
408
|
+
// Filter out updateWorkingMemory tool-call/result content items
|
|
409
|
+
const filteredContent = message.content.filter(
|
|
398
410
|
content =>
|
|
399
|
-
(
|
|
400
|
-
|
|
411
|
+
!(
|
|
412
|
+
(content.type === 'tool-call' || content.type === 'tool-result') &&
|
|
413
|
+
content.toolName === 'updateWorkingMemory'
|
|
414
|
+
),
|
|
401
415
|
);
|
|
402
|
-
if (
|
|
416
|
+
if (filteredContent.length === 0) {
|
|
417
|
+
// If nothing left, skip this message
|
|
403
418
|
continue;
|
|
404
419
|
}
|
|
405
|
-
const newContent =
|
|
420
|
+
const newContent = filteredContent.map(content => {
|
|
406
421
|
if (content.type === 'text') {
|
|
407
422
|
return {
|
|
408
423
|
...content,
|
|
@@ -437,6 +452,7 @@ export class Memory extends MastraMemory {
|
|
|
437
452
|
public async getWorkingMemory({ threadId }: { threadId: string }): Promise<string | null> {
|
|
438
453
|
if (!this.threadConfig.workingMemory?.enabled) return null;
|
|
439
454
|
|
|
455
|
+
await this.storage.init();
|
|
440
456
|
// Get thread from storage
|
|
441
457
|
const thread = await this.storage.getThreadById({ threadId });
|
|
442
458
|
if (!thread) return this.threadConfig?.workingMemory?.template || this.defaultWorkingMemoryTemplate;
|
|
@@ -476,6 +492,7 @@ export class Memory extends MastraMemory {
|
|
|
476
492
|
return;
|
|
477
493
|
}
|
|
478
494
|
|
|
495
|
+
await this.storage.init();
|
|
479
496
|
const thread = await this.storage.getThreadById({ threadId });
|
|
480
497
|
if (!thread) return;
|
|
481
498
|
|
|
@@ -507,11 +524,7 @@ export class Memory extends MastraMemory {
|
|
|
507
524
|
return null;
|
|
508
525
|
}
|
|
509
526
|
|
|
510
|
-
|
|
511
|
-
return this.getWorkingMemoryToolInstruction(workingMemory);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
return this.getWorkingMemoryWithInstruction(workingMemory);
|
|
527
|
+
return this.getWorkingMemoryToolInstruction(workingMemory);
|
|
515
528
|
}
|
|
516
529
|
|
|
517
530
|
public defaultWorkingMemoryTemplate = `
|
|
@@ -576,7 +589,7 @@ Notes:
|
|
|
576
589
|
|
|
577
590
|
public getTools(config?: MemoryConfig): Record<string, CoreTool> {
|
|
578
591
|
const mergedConfig = this.getMergedThreadConfig(config);
|
|
579
|
-
if (mergedConfig.workingMemory?.enabled
|
|
592
|
+
if (mergedConfig.workingMemory?.enabled) {
|
|
580
593
|
return {
|
|
581
594
|
updateWorkingMemory: updateWorkingMemoryTool,
|
|
582
595
|
};
|