@mastra/memory 0.2.7-alpha.2 → 0.2.7-alpha.3

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,29 +1,29 @@
1
1
 
2
- > @mastra/memory@0.2.7-alpha.2 build /home/runner/work/mastra/mastra/packages/memory
2
+ > @mastra/memory@0.2.7-alpha.3 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.2.7-alpha.2 check /home/runner/work/mastra/mastra/packages/memory
6
+ > @mastra/memory@0.2.7-alpha.3 check /home/runner/work/mastra/mastra/packages/memory
7
7
  > tsc --noEmit
8
8
 
9
9
  CLI Building entry: src/index.ts, src/processors/index.ts
10
10
  CLI Using tsconfig: tsconfig.json
11
11
  CLI tsup v8.4.0
12
12
  TSC Build start
13
- TSC ⚡️ Build success in 11238ms
13
+ TSC ⚡️ Build success in 11095ms
14
14
  DTS Build start
15
15
  CLI Target: es2022
16
16
  Analysis will use the bundled TypeScript version 5.8.2
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.8.2
19
19
  Writing package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.cts
20
- DTS ⚡️ Build success in 5847ms
20
+ DTS ⚡️ Build success in 6446ms
21
21
  CLI Cleaning output folder
22
22
  ESM Build start
23
23
  CJS Build start
24
- ESM dist/index.js 13.60 KB
25
- ESM dist/processors/index.js 5.33 KB
26
- ESM ⚡️ Build success in 238ms
27
- CJS dist/index.cjs 13.62 KB
24
+ CJS dist/index.cjs 13.53 KB
28
25
  CJS dist/processors/index.cjs 5.54 KB
29
- CJS ⚡️ Build success in 238ms
26
+ CJS ⚡️ Build success in 411ms
27
+ ESM dist/processors/index.js 5.33 KB
28
+ ESM dist/index.js 13.50 KB
29
+ ESM ⚡️ Build success in 412ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @mastra/memory
2
2
 
3
+ ## 0.2.7-alpha.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 5ae0180: Removed prefixed doc references
8
+ - 3e72f94: Updated the internal working memory system to use MD for formatting instead of nested XML - this is more token efficient and makes it more obvious that it's unstructured text
9
+ - Updated dependencies [5ae0180]
10
+ - Updated dependencies [9bfa12b]
11
+ - Updated dependencies [515ebfb]
12
+ - Updated dependencies [88fa727]
13
+ - Updated dependencies [f37f535]
14
+ - Updated dependencies [4d67826]
15
+ - Updated dependencies [6330967]
16
+ - Updated dependencies [8393832]
17
+ - Updated dependencies [6330967]
18
+ - @mastra/core@0.8.0-alpha.3
19
+ - @mastra/rag@0.1.15-alpha.3
20
+
3
21
  ## 0.2.7-alpha.2
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Mastra Memory
2
2
 
3
- Memory management for Mastra agents. Visit [the docs](https://mastra.ai/docs/agents/01-agent-memory) for more information.
3
+ Memory management for Mastra agents. Visit [the docs](https://mastra.ai/docs/agents/agent-memory) for more information.
4
4
 
5
5
  ## License
6
6
 
package/dist/index.cjs CHANGED
@@ -10,7 +10,7 @@ var zod = require('zod');
10
10
  var updateWorkingMemoryTool = {
11
11
  description: "Update the working memory with new information",
12
12
  parameters: zod.z.object({
13
- memory: zod.z.string().describe("The XML-formatted working memory content to store")
13
+ memory: zod.z.string().describe("The Markdown-formatted working memory content to store")
14
14
  }),
15
15
  execute: async (params) => {
16
16
  const { context, threadId, memory } = params;
@@ -254,8 +254,7 @@ var Memory = class extends memory.MastraMemory {
254
254
  const thread = await this.storage.__getThreadById({ threadId });
255
255
  if (!thread) return this.threadConfig?.workingMemory?.template || this.defaultWorkingMemoryTemplate;
256
256
  const memory = thread.metadata?.workingMemory || this.threadConfig.workingMemory.template || this.defaultWorkingMemoryTemplate;
257
- return memory.split(`>
258
- `).map((c) => c.trim()).join(`>`);
257
+ return memory.trim();
259
258
  }
260
259
  async saveWorkingMemory(messages) {
261
260
  const latestMessage = messages[messages.length - 1];
@@ -300,17 +299,16 @@ var Memory = class extends memory.MastraMemory {
300
299
  return this.getWorkingMemoryWithInstruction(workingMemory);
301
300
  }
302
301
  defaultWorkingMemoryTemplate = `
303
- <user>
304
- <first_name></first_name>
305
- <last_name></last_name>
306
- <location></location>
307
- <occupation></occupation>
308
- <interests></interests>
309
- <goals></goals>
310
- <events></events>
311
- <facts></facts>
312
- <projects></projects>
313
- </user>
302
+ # User Information
303
+ - **First Name**:
304
+ - **Last Name**:
305
+ - **Location**:
306
+ - **Occupation**:
307
+ - **Interests**:
308
+ - **Goals**:
309
+ - **Events**:
310
+ - **Facts**:
311
+ - **Projects**:
314
312
  `;
315
313
  getWorkingMemoryWithInstruction(workingMemoryBlock) {
316
314
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
@@ -319,21 +317,21 @@ Store and update any conversation-relevant information by including "<working_me
319
317
  Guidelines:
320
318
  1. Store anything that could be useful later in the conversation
321
319
  2. Update proactively when information changes, no matter how small
322
- 3. Use nested tags for all data
320
+ 3. Use Markdown for all data
323
321
  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"
324
322
 
325
323
  Memory Structure:
326
324
  <working_memory>
327
- ${workingMemoryBlock}
325
+ ${workingMemoryBlock}
328
326
  </working_memory>
329
327
 
330
328
  Notes:
331
329
  - Update memory whenever referenced information changes
332
- - 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, output the <working_memory> block immediately to update it)
330
+ - If you're unsure whether to store something, store it (eg if the user tells you their name or other information, output the <working_memory> block immediately to update it)
333
331
  - 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
334
- - Do not remove empty sections - you must output the empty sections along with the ones you're filling in
335
332
  - 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.
336
- - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information. `;
333
+ - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information.
334
+ - IMPORTANT: Preserve the Markdown formatting structure above while updating the content.`;
337
335
  }
338
336
  getWorkingMemoryToolInstruction(workingMemoryBlock) {
339
337
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
@@ -342,7 +340,7 @@ Store and update any conversation-relevant information by calling the updateWork
342
340
  Guidelines:
343
341
  1. Store anything that could be useful later in the conversation
344
342
  2. Update proactively when information changes, no matter how small
345
- 3. Use nested XML tags for all data
343
+ 3. Use Markdown format for all data
346
344
  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"
347
345
 
348
346
  Memory Structure:
@@ -350,11 +348,12 @@ ${workingMemoryBlock}
350
348
 
351
349
  Notes:
352
350
  - Update memory whenever referenced information changes
353
- - 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)
351
+ - If you're unsure whether to store something, store it (eg if the user tells you information about themselves, call updateWorkingMemory immediately to update it)
354
352
  - 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
355
353
  - Do not remove empty sections - you must include the empty sections along with the ones you're filling in
356
- - 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.
357
- - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.`;
354
+ - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the entire Markdown content. The system will store it for you. The user will not see it.
355
+ - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.
356
+ - IMPORTANT: Preserve the Markdown formatting structure above while updating the content.`;
358
357
  }
359
358
  getTools(config) {
360
359
  const mergedConfig = this.getMergedThreadConfig(config);
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import { z } from 'zod';
8
8
  var updateWorkingMemoryTool = {
9
9
  description: "Update the working memory with new information",
10
10
  parameters: z.object({
11
- memory: z.string().describe("The XML-formatted working memory content to store")
11
+ memory: z.string().describe("The Markdown-formatted working memory content to store")
12
12
  }),
13
13
  execute: async (params) => {
14
14
  const { context, threadId, memory } = params;
@@ -252,8 +252,7 @@ var Memory = class extends MastraMemory {
252
252
  const thread = await this.storage.__getThreadById({ threadId });
253
253
  if (!thread) return this.threadConfig?.workingMemory?.template || this.defaultWorkingMemoryTemplate;
254
254
  const memory = thread.metadata?.workingMemory || this.threadConfig.workingMemory.template || this.defaultWorkingMemoryTemplate;
255
- return memory.split(`>
256
- `).map((c) => c.trim()).join(`>`);
255
+ return memory.trim();
257
256
  }
258
257
  async saveWorkingMemory(messages) {
259
258
  const latestMessage = messages[messages.length - 1];
@@ -298,17 +297,16 @@ var Memory = class extends MastraMemory {
298
297
  return this.getWorkingMemoryWithInstruction(workingMemory);
299
298
  }
300
299
  defaultWorkingMemoryTemplate = `
301
- <user>
302
- <first_name></first_name>
303
- <last_name></last_name>
304
- <location></location>
305
- <occupation></occupation>
306
- <interests></interests>
307
- <goals></goals>
308
- <events></events>
309
- <facts></facts>
310
- <projects></projects>
311
- </user>
300
+ # User Information
301
+ - **First Name**:
302
+ - **Last Name**:
303
+ - **Location**:
304
+ - **Occupation**:
305
+ - **Interests**:
306
+ - **Goals**:
307
+ - **Events**:
308
+ - **Facts**:
309
+ - **Projects**:
312
310
  `;
313
311
  getWorkingMemoryWithInstruction(workingMemoryBlock) {
314
312
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
@@ -317,21 +315,21 @@ Store and update any conversation-relevant information by including "<working_me
317
315
  Guidelines:
318
316
  1. Store anything that could be useful later in the conversation
319
317
  2. Update proactively when information changes, no matter how small
320
- 3. Use nested tags for all data
318
+ 3. Use Markdown for all data
321
319
  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"
322
320
 
323
321
  Memory Structure:
324
322
  <working_memory>
325
- ${workingMemoryBlock}
323
+ ${workingMemoryBlock}
326
324
  </working_memory>
327
325
 
328
326
  Notes:
329
327
  - Update memory whenever referenced information changes
330
- - 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, output the <working_memory> block immediately to update it)
328
+ - If you're unsure whether to store something, store it (eg if the user tells you their name or other information, output the <working_memory> block immediately to update it)
331
329
  - 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
332
- - Do not remove empty sections - you must output the empty sections along with the ones you're filling in
333
330
  - 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.
334
- - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information. `;
331
+ - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information.
332
+ - IMPORTANT: Preserve the Markdown formatting structure above while updating the content.`;
335
333
  }
336
334
  getWorkingMemoryToolInstruction(workingMemoryBlock) {
337
335
  return `WORKING_MEMORY_SYSTEM_INSTRUCTION:
@@ -340,7 +338,7 @@ Store and update any conversation-relevant information by calling the updateWork
340
338
  Guidelines:
341
339
  1. Store anything that could be useful later in the conversation
342
340
  2. Update proactively when information changes, no matter how small
343
- 3. Use nested XML tags for all data
341
+ 3. Use Markdown format for all data
344
342
  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"
345
343
 
346
344
  Memory Structure:
@@ -348,11 +346,12 @@ ${workingMemoryBlock}
348
346
 
349
347
  Notes:
350
348
  - Update memory whenever referenced information changes
351
- - 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)
349
+ - If you're unsure whether to store something, store it (eg if the user tells you information about themselves, call updateWorkingMemory immediately to update it)
352
350
  - 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
353
351
  - Do not remove empty sections - you must include the empty sections along with the ones you're filling in
354
- - 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.
355
- - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.`;
352
+ - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the entire Markdown content. The system will store it for you. The user will not see it.
353
+ - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.
354
+ - IMPORTANT: Preserve the Markdown formatting structure above while updating the content.`;
356
355
  }
357
356
  getTools(config) {
358
357
  const mergedConfig = this.getMergedThreadConfig(config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/memory",
3
- "version": "0.2.7-alpha.2",
3
+ "version": "0.2.7-alpha.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -40,8 +40,8 @@
40
40
  "postgres": "^3.4.5",
41
41
  "redis": "^4.7.0",
42
42
  "zod": "^3.24.2",
43
- "@mastra/core": "^0.8.0-alpha.2",
44
- "@mastra/rag": "^0.1.15-alpha.2"
43
+ "@mastra/core": "^0.8.0-alpha.3",
44
+ "@mastra/rag": "^0.1.15-alpha.3"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@ai-sdk/openai": "^1.3.3",
package/src/index.ts CHANGED
@@ -332,11 +332,7 @@ export class Memory extends MastraMemory {
332
332
  this.threadConfig.workingMemory.template ||
333
333
  this.defaultWorkingMemoryTemplate;
334
334
 
335
- // compress working memory because LLMs will generate faster without the spaces and line breaks
336
- return memory
337
- .split(`>\n`)
338
- .map(c => c.trim()) // remove extra whitespace
339
- .join(`>`); // and linebreaks
335
+ return memory.trim();
340
336
  }
341
337
 
342
338
  private async saveWorkingMemory(messages: MessageType[]) {
@@ -404,17 +400,16 @@ export class Memory extends MastraMemory {
404
400
  }
405
401
 
406
402
  public defaultWorkingMemoryTemplate = `
407
- <user>
408
- <first_name></first_name>
409
- <last_name></last_name>
410
- <location></location>
411
- <occupation></occupation>
412
- <interests></interests>
413
- <goals></goals>
414
- <events></events>
415
- <facts></facts>
416
- <projects></projects>
417
- </user>
403
+ # User Information
404
+ - **First Name**:
405
+ - **Last Name**:
406
+ - **Location**:
407
+ - **Occupation**:
408
+ - **Interests**:
409
+ - **Goals**:
410
+ - **Events**:
411
+ - **Facts**:
412
+ - **Projects**:
418
413
  `;
419
414
 
420
415
  private getWorkingMemoryWithInstruction(workingMemoryBlock: string) {
@@ -424,21 +419,21 @@ Store and update any conversation-relevant information by including "<working_me
424
419
  Guidelines:
425
420
  1. Store anything that could be useful later in the conversation
426
421
  2. Update proactively when information changes, no matter how small
427
- 3. Use nested tags for all data
422
+ 3. Use Markdown for all data
428
423
  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"
429
424
 
430
425
  Memory Structure:
431
426
  <working_memory>
432
- ${workingMemoryBlock}
427
+ ${workingMemoryBlock}
433
428
  </working_memory>
434
429
 
435
430
  Notes:
436
431
  - Update memory whenever referenced information changes
437
- - 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, output the <working_memory> block immediately to update it)
432
+ - If you're unsure whether to store something, store it (eg if the user tells you their name or other information, output the <working_memory> block immediately to update it)
438
433
  - 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
439
- - Do not remove empty sections - you must output the empty sections along with the ones you're filling in
440
434
  - 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.
441
- - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information. `;
435
+ - IMPORTANT: You MUST output the <working_memory> block in every response to a prompt where you received relevant information.
436
+ - IMPORTANT: Preserve the Markdown formatting structure above while updating the content.`;
442
437
  }
443
438
 
444
439
  private getWorkingMemoryToolInstruction(workingMemoryBlock: string) {
@@ -448,7 +443,7 @@ Store and update any conversation-relevant information by calling the updateWork
448
443
  Guidelines:
449
444
  1. Store anything that could be useful later in the conversation
450
445
  2. Update proactively when information changes, no matter how small
451
- 3. Use nested XML tags for all data
446
+ 3. Use Markdown format for all data
452
447
  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"
453
448
 
454
449
  Memory Structure:
@@ -456,11 +451,12 @@ ${workingMemoryBlock}
456
451
 
457
452
  Notes:
458
453
  - Update memory whenever referenced information changes
459
- - 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)
454
+ - If you're unsure whether to store something, store it (eg if the user tells you information about themselves, call updateWorkingMemory immediately to update it)
460
455
  - 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
461
456
  - Do not remove empty sections - you must include the empty sections along with the ones you're filling in
462
- - 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.
463
- - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.`;
457
+ - REMEMBER: the way you update your working memory is by calling the updateWorkingMemory tool with the entire Markdown content. The system will store it for you. The user will not see it.
458
+ - IMPORTANT: You MUST call updateWorkingMemory in every response to a prompt where you received relevant information.
459
+ - IMPORTANT: Preserve the Markdown formatting structure above while updating the content.`;
464
460
  }
465
461
 
466
462
  public getTools(config?: MemoryConfig): Record<string, CoreTool> {
@@ -3,11 +3,13 @@ import { createTool } from '@mastra/core';
3
3
  import type { CoreMessage, MessageType } from '@mastra/core';
4
4
  import { Agent } from '@mastra/core/agent';
5
5
  import cl100k_base from 'js-tiktoken/ranks/cl100k_base';
6
- import { describe, it, expect } from 'vitest';
6
+ import { describe, it, expect, vi } from 'vitest';
7
7
  import { z } from 'zod';
8
8
  import { generateConversationHistory } from '../../integration-tests/src/test-utils';
9
9
  import { TokenLimiter, ToolCallFilter } from './index';
10
10
 
11
+ vi.setConfig({ testTimeout: 20_000, hookTimeout: 20_000 });
12
+
11
13
  describe('TokenLimiter', () => {
12
14
  it('should limit messages to the specified token count', () => {
13
15
  // Create messages with predictable token counts (approximately 25 tokens each)
@@ -4,7 +4,7 @@ import { z } from 'zod';
4
4
  export const updateWorkingMemoryTool: CoreTool = {
5
5
  description: 'Update the working memory with new information',
6
6
  parameters: z.object({
7
- memory: z.string().describe('The XML-formatted working memory content to store'),
7
+ memory: z.string().describe('The Markdown-formatted working memory content to store'),
8
8
  }),
9
9
  execute: async (params: any) => {
10
10
  const { context, threadId, memory } = params;