0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

feat(@mastra/core, @mastra/memory): Memory processors (#3304)

## TLDR
Adds memory processors to filter or transform messages before they're
sent to the LLM.
Includes two built-in processors: `TokenLimiter` (prevents context
window overflow) and `ToolCallFilter` (removes specific tool calls and
results from message history).

## Example usage
```ts
const memory = new Memory({
  processors: [
    // First filter out image generator calls 
    new ToolCallFilter({ exclude: ["imageGen"] }),
    // Then limit the total tokens
    new TokenLimiter(8000),
  ],
});
```

## Custom processors
This PR introduces a new `MemoryProcessor` abstract class that enables
message filtering and transformation. The processors run just before
messages are sent to the LLM, helping developers manage the context
window and implement custom memory modifications. This class can be
extended and added to memory the same way the built in ones can.

## What this unlocks for users
- Token management: Prevent context overflow by automatically limiting
the token count in message history
- Content filtering: Remove tool calls (especially ones with large
outputs like image/audio data)
- Message transformation: Create custom processors to modify messages
(strip sensitive data, remove reasoning parts, etc.) by extending
`MemoryProcessor`
- Flexible processing pipeline: Chain multiple processors together for
complex message transformations

## Future

Using this we could move semantic recall + working memory out to be
memory processors instead. This should make it easier for us to add
memory features without having a huge frankenstein memory config
`Memory(opts)`

---------

Co-authored-by: codesandbox-bot <codesandbot@codesandbox.io>
This commit is contained in:
Tyler Barnes 2025-03-31 12:27:43 -07:00 • committed by GitHub
commit a0967a068c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 2454 additions and 492 deletions

763
pnpm-lock.yaml generated