Description
n8n-nodes-gateway-memory
This is an n8n community node that provides Memory Gateway – a powerful middleware for filtering and transforming chat memory content in AI workflows.
n8n is a fair-code licensed workflow automation platform.
Features
- ๐ Memory Middleware – Acts as a gateway between AI Agent and any Memory storage
- ๐งน Content Filtering – Filter data before saving and after retrieving from memory
- ๐ป Custom JavaScript Code – Write custom filter logic with full JavaScript support
- ๐ฏ Universal Compatibility – Works with Postgres, Redis, MongoDB, and any other Memory node
- โก Performance – Minimal overhead, processes data on-the-fly
- ๐ Data Control – Remove sensitive information, limit message length, clean technical logs
Installation
Follow the installation guide in the n8n community nodes documentation.
Manual Installation (Self-Hosted)
- Navigate to your n8n installation directory
- Install the package:
npm install n8n-nodes-gateway-memory
- Restart n8n
Local Development
- Clone this repository
- Mount to n8n custom nodes directory
- Restart n8n
docker-compose down
docker-compose up --build
Operations
Memory Gateway
The Memory Gateway node provides filtering capabilities for chat memory.
Connection Flow
AI Agent
โ (Memory slot)
Memory Gateway
โ (Internal Memory slot)
Postgres Chat Memory (or any other Memory node)
Parameters
| Parameter | Type | Description |
|---|---|---|
| Filter Before Save | Code Editor | JavaScript function to filter data BEFORE saving to memory |
| Filter After Retrieve | Code Editor | JavaScript function to filter data AFTER retrieving from memory |
Usage
Basic Setup
- Add Memory Gateway to your workflow
- Connect a Memory node (e.g., Postgres Chat Memory) to Memory Gateway's input
- Connect Memory Gateway to AI Agent's Memory slot
- Configure filters in Memory Gateway settings
Filter Before Save
This filter runs when AI Agent saves a message to memory.
Available variables:
input(string) – User's input messageoutput(string) – AI's response message
Must return:
- Object with
{ input: string, output: string }
Example – Remove tool execution logs:
function filterBeforeSave(input, output) {
// Remove [Used tools: ...] from output
if (output) {
output = output.replace(/\[Used tools:[\s\S]*?\]\s*/g, '');
}
// Limit message length
const MAX_LENGTH = 3000;
if (output && output.length > MAX_LENGTH) {
output = output.substring(0, MAX_LENGTH) + '... [truncated]';
}
return { input, output };
}
Filter After Retrieve
This filter runs when AI Agent loads messages from memory.
Available variables:
messages(Array) – Array of message objects from memory
Must return:
- Modified messages array
Example – Limit number of messages:
function filterAfterRetrieve(messages) {
// Keep only last 10 messages
if (messages && Array.isArray(messages) && messages.length > 10) {
messages = messages.slice(-10);
}
// Filter out system messages
messages = messages.filter(msg => msg.type !== 'system');
return messages;
}
Examples
Example 1: Clean AI Tool Logs
Remove technical information from AI responses before storing:
function filterBeforeSave(input, output) {
// Remove [Used tools: ...]
output = output.replace(/\[Used tools:[\s\S]*?\]\s*/g, '');
// Remove JSON metadata
output = output.replace(/\{[\s\S]*?"action"[\s\S]*?\}/g, '');
return { input, output };
}
Example 2: Privacy Filter
Remove sensitive information:
function filterBeforeSave(input, output) {
// Remove email addresses
input = input.replace(/[\w.-]+@[\w.-]+\.\w+/g, '[EMAIL]');
output = output.replace(/[\w.-]+@[\w.-]+\.\w+/g, '[EMAIL]');
// Remove phone numbers
input = input.replace(/\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g, '[PHONE]');
output = output.replace(/\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g, '[PHONE]');
return { input, output };
}
Example 3: Context Window Management
Keep only relevant messages:
function filterAfterRetrieve(messages) {
// Keep only last 5 messages
if (messages.length > 5) {
messages = messages.slice(-5);
}
// Remove messages older than 24 hours
const oneDayAgo = Date.now() - 24 * 60 * 60 * 1000;
messages = messages.filter(msg => {
return !msg.timestamp || msg.timestamp > oneDayAgo;
});
return messages;
}
Example 4: Content Summarization
Summarize long messages:
function filterBeforeSave(input, output) {
const MAX_LENGTH = 500;
if (output.length > MAX_LENGTH) {
// Keep first and last parts
const start = output.substring(0, 200);
const end = output.substring(output.length - 200);
output = start + '\n\n[... content summarized ...]\n\n' + end;
}
return { input, output };
}
Compatibility
Tested with:
- n8n version: 1.0.0+
- Node.js: 18.x, 20.x
Compatible Memory Nodes:
- โ Postgres Chat Memory
- โ Redis Chat Memory
- โ MongoDB Chat Memory
- โ Window Buffer Memory
- โ Buffer Memory
- โ All LangChain-compatible memory nodes
Resources
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
Author
vyunak
- GitHub: @vyunak
Support
If you find this node helpful, please give it a โญ๏ธ on GitHub!
Made with โค๏ธ for the n8n community