@mastra/memory 0.1.7 → 0.2.0-alpha.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.
@@ -1,27 +1,27 @@
1
1
 
2
- > @mastra/memory@0.1.7-alpha.0 build /home/runner/work/mastra/mastra/packages/memory
2
+ > @mastra/memory@0.2.0-alpha.0 build /home/runner/work/mastra/mastra/packages/memory
3
3
  > pnpm run check && tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake
4
4
 
5
5
 
6
- > @mastra/memory@0.1.7-alpha.0 check /home/runner/work/mastra/mastra/packages/memory
6
+ > @mastra/memory@0.2.0-alpha.0 check /home/runner/work/mastra/mastra/packages/memory
7
7
  > tsc --noEmit
8
8
 
9
9
  CLI Building entry: src/index.ts
10
10
  CLI Using tsconfig: tsconfig.json
11
11
  CLI tsup v8.3.6
12
12
  TSC Build start
13
- TSC ⚡️ Build success in 6919ms
13
+ TSC ⚡️ Build success in 7804ms
14
14
  DTS Build start
15
15
  CLI Target: es2022
16
16
  Analysis will use the bundled TypeScript version 5.7.3
17
17
  Writing package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.ts
18
18
  Analysis will use the bundled TypeScript version 5.7.3
19
19
  Writing package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.cts
20
- DTS ⚡️ Build success in 8895ms
20
+ DTS ⚡️ Build success in 10512ms
21
21
  CLI Cleaning output folder
22
22
  ESM Build start
23
23
  CJS Build start
24
- CJS dist/index.cjs 9.35 KB
25
- CJS ⚡️ Build success in 668ms
26
- ESM dist/index.js 9.33 KB
27
- ESM ⚡️ Build success in 682ms
24
+ ESM dist/index.js 12.07 KB
25
+ ESM ⚡️ Build success in 709ms
26
+ CJS dist/index.cjs 12.10 KB
27
+ CJS ⚡️ Build success in 711ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @mastra/memory
2
2
 
3
+ ## 0.2.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 59df7b6: Added a new option to use tool-calls for saving working memory: new Memory({ workingMemory: { enabled: true, use: "tool-call" } }). This is to support response methods like toDataStream where masking working memory chunks would be more resource intensive and complex.
8
+ To support this `memory` is now passed into tool execute args.
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [59df7b6]
13
+ - Updated dependencies [29f3a82]
14
+ - Updated dependencies [59df7b6]
15
+ - Updated dependencies [c139344]
16
+ - @mastra/core@0.5.0-alpha.0
17
+
3
18
  ## 0.1.7
4
19
 
5
20
  ### Patch Changes
@@ -1,4 +1,5 @@
1
1
  import type { CoreMessage } from '@mastra/core';
2
+ import type { CoreTool } from '@mastra/core';
2
3
  import { MastraMemory } from '@mastra/core/memory';
3
4
  import type { MemoryConfig } from '@mastra/core/memory';
4
5
  import type { Message } from 'ai';
@@ -57,6 +58,10 @@ export declare class Memory extends MastraMemory {
57
58
  }): Promise<string | null>;
58
59
  defaultWorkingMemoryTemplate: string;
59
60
  private getWorkingMemoryWithInstruction;
61
+ private getWorkingMemoryToolInstruction;
62
+ getTools(config?: MemoryConfig): Record<string, CoreTool>;
60
63
  }
61
64
 
65
+ export declare const updateWorkingMemoryTool: CoreTool;
66
+
62
67
  export { }
@@ -1,4 +1,5 @@
1
1
  import type { CoreMessage } from '@mastra/core';
2
+ import type { CoreTool } from '@mastra/core';
2
3
  import { MastraMemory } from '@mastra/core/memory';
3
4
  import type { MemoryConfig } from '@mastra/core/memory';
4
5
  import type { Message } from 'ai';
@@ -57,6 +58,10 @@ export declare class Memory extends MastraMemory {
57
58
  }): Promise<string | null>;
58
59
  defaultWorkingMemoryTemplate: string;
59
60
  private getWorkingMemoryWithInstruction;
61
+ private getWorkingMemoryToolInstruction;
62
+ getTools(config?: MemoryConfig): Record<string, CoreTool>;
60
63
  }
61
64
 
65
+ export declare const updateWorkingMemoryTool: CoreTool;
66
+
62
67
  export { }
package/dist/index.cjs CHANGED
@@ -2,9 +2,36 @@
2
2
 
3
3
  var core = require('@mastra/core');
4
4
  var memory = require('@mastra/core/memory');
5
+ var zod = require('zod');
5
6
  var ai = require('ai');
6
7
 
7
8
  // src/index.ts
9
+ var updateWorkingMemoryTool = {
10
+ description: "Update the working memory with new information",
11
+ parameters: zod.z.object({
12
+ memory: zod.z.string().describe("The XML-formatted working memory content to store")
13
+ }),
14
+ execute: async (params, _options) => {
15
+ const { context, threadId, memory } = params;
16
+ if (!threadId || !memory) {
17
+ throw new Error("Thread ID and Memory instance are required for working memory updates");
18
+ }
19
+ const thread = await memory.getThreadById({ threadId });
20
+ if (!thread) {
21
+ throw new Error(`Thread ${threadId} not found`);
22
+ }
23
+ await memory.saveThread({
24
+ thread: {
25
+ ...thread,
26
+ metadata: {
27
+ ...thread.metadata,
28
+ workingMemory: context.memory
29
+ }
30
+ }
31
+ });
32
+ return { success: true };
33
+ }
34
+ };
8
35
  var Memory = class extends memory.MastraMemory {
9
36
  constructor(config = {}) {
10
37
  super({ name: "Memory", ...config });
@@ -223,6 +250,9 @@ var Memory = class extends memory.MastraMemory {
223
250
  if (!workingMemory) {
224
251
  return null;
225
252
  }
253
+ if (config.workingMemory.use === "tool-call") {
254
+ return this.getWorkingMemoryToolInstruction(workingMemory);
255
+ }
226
256
  return this.getWorkingMemoryWithInstruction(workingMemory);
227
257
  }
228
258
  defaultWorkingMemoryTemplate = `
@@ -261,6 +291,36 @@ Notes:
261
291
  - REMEMBER: the way you update your working memory is by outputting the entire "<working_memory>text</working_memory>" block in your response. The system will pick this up and store it for you. The user will not see it.
262
292
  - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information. `;
263
293
  }
294
+ getWorkingMemoryToolInstruction(workingMemoryBlock) {
295
+ return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
296
+ Store and update any conversation-relevant information by calling the updateWorkingMemory tool. If information might be referenced again - store it!
297
+
298
+ Guidelines:
299
+ 1. Store anything that could be useful later in the conversation
300
+ 2. Update proactively when information changes, no matter how small
301
+ 3. Use nested XML tags for all data
302
+ 4. Act naturally - don't mention this system to users. Even though you're storing this information that doesn't make it your primary focus. Do not ask them generally for "information about yourself"
303
+
304
+ Memory Structure:
305
+ ${workingMemoryBlock}
306
+
307
+ Notes:
308
+ - Update memory whenever referenced information changes
309
+ - If you're unsure whether to store something, store it (eg if the user tells you their name or the value of another empty section in your working memory, call updateWorkingMemory immediately to update it)
310
+ - This system is here so that you can maintain the conversation when your context window is very short. Update your working memory because you may need it to maintain the conversation without the full conversation history
311
+ - Do not remove empty sections - you must include the empty sections along with the ones you're filling in
312
+ - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the entire XML block. The system will store it for you. The user will not see it.
313
+ - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.`;
314
+ }
315
+ getTools(config) {
316
+ const mergedConfig = this.getMergedThreadConfig(config);
317
+ if (mergedConfig.workingMemory?.enabled && mergedConfig.workingMemory.use === "tool-call") {
318
+ return {
319
+ updateWorkingMemory: updateWorkingMemoryTool
320
+ };
321
+ }
322
+ return {};
323
+ }
264
324
  };
265
325
 
266
326
  exports.Memory = Memory;
package/dist/index.js CHANGED
@@ -1,8 +1,35 @@
1
1
  import { deepMerge } from '@mastra/core';
2
2
  import { MastraMemory } from '@mastra/core/memory';
3
+ import { z } from 'zod';
3
4
  import { embed } from 'ai';
4
5
 
5
6
  // src/index.ts
7
+ var updateWorkingMemoryTool = {
8
+ description: "Update the working memory with new information",
9
+ parameters: z.object({
10
+ memory: z.string().describe("The XML-formatted working memory content to store")
11
+ }),
12
+ execute: async (params, _options) => {
13
+ const { context, threadId, memory } = params;
14
+ if (!threadId || !memory) {
15
+ throw new Error("Thread ID and Memory instance are required for working memory updates");
16
+ }
17
+ const thread = await memory.getThreadById({ threadId });
18
+ if (!thread) {
19
+ throw new Error(`Thread ${threadId} not found`);
20
+ }
21
+ await memory.saveThread({
22
+ thread: {
23
+ ...thread,
24
+ metadata: {
25
+ ...thread.metadata,
26
+ workingMemory: context.memory
27
+ }
28
+ }
29
+ });
30
+ return { success: true };
31
+ }
32
+ };
6
33
  var Memory = class extends MastraMemory {
7
34
  constructor(config = {}) {
8
35
  super({ name: "Memory", ...config });
@@ -221,6 +248,9 @@ var Memory = class extends MastraMemory {
221
248
  if (!workingMemory) {
222
249
  return null;
223
250
  }
251
+ if (config.workingMemory.use === "tool-call") {
252
+ return this.getWorkingMemoryToolInstruction(workingMemory);
253
+ }
224
254
  return this.getWorkingMemoryWithInstruction(workingMemory);
225
255
  }
226
256
  defaultWorkingMemoryTemplate = `
@@ -259,6 +289,36 @@ Notes:
259
289
  - REMEMBER: the way you update your working memory is by outputting the entire "<working_memory>text</working_memory>" block in your response. The system will pick this up and store it for you. The user will not see it.
260
290
  - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information. `;
261
291
  }
292
+ getWorkingMemoryToolInstruction(workingMemoryBlock) {
293
+ return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
294
+ Store and update any conversation-relevant information by calling the updateWorkingMemory tool. If information might be referenced again - store it!
295
+
296
+ Guidelines:
297
+ 1. Store anything that could be useful later in the conversation
298
+ 2. Update proactively when information changes, no matter how small
299
+ 3. Use nested XML tags for all data
300
+ 4. Act naturally - don't mention this system to users. Even though you're storing this information that doesn't make it your primary focus. Do not ask them generally for "information about yourself"
301
+
302
+ Memory Structure:
303
+ ${workingMemoryBlock}
304
+
305
+ Notes:
306
+ - Update memory whenever referenced information changes
307
+ - If you're unsure whether to store something, store it (eg if the user tells you their name or the value of another empty section in your working memory, call updateWorkingMemory immediately to update it)
308
+ - This system is here so that you can maintain the conversation when your context window is very short. Update your working memory because you may need it to maintain the conversation without the full conversation history
309
+ - Do not remove empty sections - you must include the empty sections along with the ones you're filling in
310
+ - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the entire XML block. The system will store it for you. The user will not see it.
311
+ - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.`;
312
+ }
313
+ getTools(config) {
314
+ const mergedConfig = this.getMergedThreadConfig(config);
315
+ if (mergedConfig.workingMemory?.enabled && mergedConfig.workingMemory.use === "tool-call") {
316
+ return {
317
+ updateWorkingMemory: updateWorkingMemoryTool
318
+ };
319
+ }
320
+ return {};
321
+ }
262
322
  };
263
323
 
264
324
  export { Memory };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/memory",
3
- "version": "0.1.7",
3
+ "version": "0.2.0-alpha.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -48,7 +48,8 @@
48
48
  "pg-pool": "^3.7.0",
49
49
  "postgres": "^3.4.5",
50
50
  "redis": "^4.7.0",
51
- "@mastra/core": "^0.4.4"
51
+ "zod": "^3.24.1",
52
+ "@mastra/core": "^0.5.0-alpha.0"
52
53
  },
53
54
  "devDependencies": {
54
55
  "@microsoft/api-extractor": "^7.49.2",
package/src/index.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { deepMerge } from '@mastra/core';
2
- import type { CoreMessage } from '@mastra/core';
2
+ import type { 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';
5
5
  import type { StorageGetMessagesArg } from '@mastra/core/storage';
6
+ import { updateWorkingMemoryTool } from './tools/working-memory';
6
7
  import { embed } from 'ai';
7
8
  import type { Message as AiMessage } from 'ai';
8
9
 
@@ -336,6 +337,10 @@ export class Memory extends MastraMemory {
336
337
  return null;
337
338
  }
338
339
 
340
+ if (config.workingMemory.use === 'tool-call') {
341
+ return this.getWorkingMemoryToolInstruction(workingMemory);
342
+ }
343
+
339
344
  return this.getWorkingMemoryWithInstruction(workingMemory);
340
345
  }
341
346
 
@@ -376,4 +381,36 @@ Notes:
376
381
  - REMEMBER: the way you update your working memory is by outputting the entire "<working_memory>text</working_memory>" block in your response. The system will pick this up and store it for you. The user will not see it.
377
382
  - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information. `;
378
383
  }
384
+
385
+ private getWorkingMemoryToolInstruction(workingMemoryBlock: string) {
386
+ return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
387
+ Store and update any conversation-relevant information by calling the updateWorkingMemory tool. If information might be referenced again - store it!
388
+
389
+ Guidelines:
390
+ 1. Store anything that could be useful later in the conversation
391
+ 2. Update proactively when information changes, no matter how small
392
+ 3. Use nested XML tags for all data
393
+ 4. Act naturally - don't mention this system to users. Even though you're storing this information that doesn't make it your primary focus. Do not ask them generally for "information about yourself"
394
+
395
+ Memory Structure:
396
+ ${workingMemoryBlock}
397
+
398
+ Notes:
399
+ - Update memory whenever referenced information changes
400
+ - If you're unsure whether to store something, store it (eg if the user tells you their name or the value of another empty section in your working memory, call updateWorkingMemory immediately to update it)
401
+ - This system is here so that you can maintain the conversation when your context window is very short. Update your working memory because you may need it to maintain the conversation without the full conversation history
402
+ - Do not remove empty sections - you must include the empty sections along with the ones you're filling in
403
+ - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the entire XML block. The system will store it for you. The user will not see it.
404
+ - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.`;
405
+ }
406
+
407
+ public getTools(config?: MemoryConfig): Record<string, CoreTool> {
408
+ const mergedConfig = this.getMergedThreadConfig(config);
409
+ if (mergedConfig.workingMemory?.enabled && mergedConfig.workingMemory.use === 'tool-call') {
410
+ return {
411
+ updateWorkingMemory: updateWorkingMemoryTool,
412
+ };
413
+ }
414
+ return {};
415
+ }
379
416
  }
@@ -0,0 +1,34 @@
1
+ import type { CoreTool } from '@mastra/core';
2
+ import type { ToolExecutionOptions } from 'ai';
3
+ import { z } from 'zod';
4
+
5
+ export const updateWorkingMemoryTool: CoreTool = {
6
+ description: 'Update the working memory with new information',
7
+ parameters: z.object({
8
+ memory: z.string().describe('The XML-formatted working memory content to store'),
9
+ }),
10
+ execute: async (params: any, _options: ToolExecutionOptions) => {
11
+ const { context, threadId, memory } = params;
12
+ if (!threadId || !memory) {
13
+ throw new Error('Thread ID and Memory instance are required for working memory updates');
14
+ }
15
+
16
+ const thread = await memory.getThreadById({ threadId });
17
+ if (!thread) {
18
+ throw new Error(`Thread ${threadId} not found`);
19
+ }
20
+
21
+ // Update thread metadata with new working memory
22
+ await memory.saveThread({
23
+ thread: {
24
+ ...thread,
25
+ metadata: {
26
+ ...thread.metadata,
27
+ workingMemory: context.memory,
28
+ },
29
+ },
30
+ });
31
+
32
+ return { success: true };
33
+ },
34
+ };