@alex900530/claude-persistent-memory 1.0.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.
- package/LICENSE +21 -0
- package/README.md +271 -0
- package/bin/setup.js +418 -0
- package/bin/uninstall.js +122 -0
- package/config.default.js +49 -0
- package/config.js +7 -0
- package/hooks/post-tool-memory-hook.js +151 -0
- package/hooks/pre-compact-hook.js +61 -0
- package/hooks/pre-tool-memory-hook.js +148 -0
- package/hooks/session-end-hook.js +159 -0
- package/hooks/user-prompt-hook.js +151 -0
- package/lib/compact-analyzer.js +319 -0
- package/lib/embedding-client.js +113 -0
- package/lib/llm-client.js +61 -0
- package/lib/memory-db.js +1310 -0
- package/lib/utils.js +92 -0
- package/package.json +44 -0
- package/services/embedding-server.js +108 -0
- package/services/llm-server.js +421 -0
- package/services/memory-mcp-server.js +252 -0
- package/tools/rebuild-vectors.js +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MIMI180306
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./assets/logo.png" alt="Claude Persistent Memory" width="120" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">Claude Persistent Memory</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<strong>Give Claude Code long-term memory that persists across sessions.</strong><br/>
|
|
9
|
+
Hybrid BM25 + vector semantic search · LLM-driven structuring · 4-channel retrieval (MCP + hooks)
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
<a href="https://github.com/MIMI180306/claude-persistent-memory/blob/main/LICENSE"><img src="https://img.shields.io/github/license/MIMI180306/claude-persistent-memory?style=flat-square&color=blue" alt="License"></a>
|
|
14
|
+
<a href="https://github.com/MIMI180306/claude-persistent-memory/stargazers"><img src="https://img.shields.io/github/stars/MIMI180306/claude-persistent-memory?style=flat-square&color=yellow" alt="Stars"></a>
|
|
15
|
+
<a href="https://github.com/MIMI180306/claude-persistent-memory/issues"><img src="https://img.shields.io/github/issues/MIMI180306/claude-persistent-memory?style=flat-square" alt="Issues"></a>
|
|
16
|
+
<a href="https://github.com/MIMI180306/claude-persistent-memory/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/MIMI180306/claude-persistent-memory/ci.yml?style=flat-square&label=CI" alt="CI"></a>
|
|
17
|
+
<img src="https://img.shields.io/badge/node-%3E%3D18-brightgreen?style=flat-square" alt="Node >= 18">
|
|
18
|
+
<img src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux-lightgrey?style=flat-square" alt="Platform">
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<p align="center">
|
|
22
|
+
<a href="#features">Features</a> •
|
|
23
|
+
<a href="#quick-start">Quick Start</a> •
|
|
24
|
+
<a href="#architecture">Architecture</a> •
|
|
25
|
+
<a href="#mcp-tools">MCP Tools</a> •
|
|
26
|
+
<a href="#configuration">Configuration</a> •
|
|
27
|
+
<a href="#contributing">Contributing</a>
|
|
28
|
+
</p>
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
<!-- TODO: Add a GIF demo here showing the memory system in action
|
|
33
|
+
<p align="center">
|
|
34
|
+
<img src="./assets/demo.gif" alt="Demo" width="700" />
|
|
35
|
+
</p>
|
|
36
|
+
-->
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
🧠 **Hybrid Search** — BM25 full-text (FTS5) + vector semantic similarity (sqlite-vec), combined ranking
|
|
41
|
+
|
|
42
|
+
📡 **4-Channel Retrieval** — Pull (MCP tools) + Push (auto-inject via hooks on user prompt, pre-tool, post-tool)
|
|
43
|
+
|
|
44
|
+
🏗️ **LLM Structuring** — Memories auto-structured into `<what>/<when>/<do>/<warn>` XML format
|
|
45
|
+
|
|
46
|
+
📦 **Automatic Clustering** — Similar memories grouped, mature clusters promoted to reusable skills
|
|
47
|
+
|
|
48
|
+
📊 **Confidence Scoring** — Memories gain/lose confidence through validation feedback and usage
|
|
49
|
+
|
|
50
|
+
⏳ **Time Decay** — Configurable half-lives per memory type (facts: 90d, context: 30d, skills: never)
|
|
51
|
+
|
|
52
|
+
🔒 **Local-First** — All data stored locally in SQLite. Your memories never leave your machine.
|
|
53
|
+
|
|
54
|
+
## Architecture
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
58
|
+
│ Claude Code Session │
|
|
59
|
+
├─────────────────────────────────────────────────────────────┤
|
|
60
|
+
│ │
|
|
61
|
+
│ Pull Channel (on demand) Push Channels (auto) │
|
|
62
|
+
│ ┌───────────────────┐ ┌──────────────────────────────┐ │
|
|
63
|
+
│ │ MCP Server │ │ UserPromptSubmit Hook │ │
|
|
64
|
+
│ │ memory_search │ │ PreToolUse Hook │ │
|
|
65
|
+
│ │ memory_save │ │ PostToolUse Hook │ │
|
|
66
|
+
│ │ memory_validate │ │ PreCompact Hook (analysis) │ │
|
|
67
|
+
│ │ memory_stats │ │ SessionEnd Hook (clustering) │ │
|
|
68
|
+
│ └────────┬──────────┘ └──────────────┬───────────────┘ │
|
|
69
|
+
│ │ │ │
|
|
70
|
+
│ └──────────┬───────────────────┘ │
|
|
71
|
+
│ ▼ │
|
|
72
|
+
│ ┌─────────────────────────────────────────────────────────┐│
|
|
73
|
+
│ │ SQLite + FTS5 + sqlite-vec ││
|
|
74
|
+
│ │ (memory.db) ││
|
|
75
|
+
│ └─────────────────────────────────────────────────────────┘│
|
|
76
|
+
│ ▲ │
|
|
77
|
+
│ ┌──────────┴───────────────────┐ │
|
|
78
|
+
│ │ │ │
|
|
79
|
+
│ ┌────────┴──────────┐ ┌──────────────┴───────────────┐ │
|
|
80
|
+
│ │ Embedding Server │ │ LLM Server │ │
|
|
81
|
+
│ │ TCP :23811 │ │ TCP :23812 │ │
|
|
82
|
+
│ │ bge-m3 (1024d) │ │ Azure OpenAI GPT-4.1 │ │
|
|
83
|
+
│ └───────────────────┘ └──────────────────────────────┘ │
|
|
84
|
+
│ │
|
|
85
|
+
└─────────────────────────────────────────────────────────────┘
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Quick Start
|
|
89
|
+
|
|
90
|
+
### One-command install
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
# Set Azure OpenAI credentials (optional — skip to configure later)
|
|
94
|
+
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com"
|
|
95
|
+
export AZURE_OPENAI_KEY="your-api-key"
|
|
96
|
+
|
|
97
|
+
# Install in any project
|
|
98
|
+
npm install @alex/claude-persistent-memory
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
That's it. The postinstall script automatically:
|
|
102
|
+
- Configures `.mcp.json` (MCP server)
|
|
103
|
+
- Configures `.claude/settings.json` (5 hooks)
|
|
104
|
+
- Generates `.claude-memory.config.js` (config)
|
|
105
|
+
- Registers background services (embedding + LLM)
|
|
106
|
+
- Updates `.gitignore`
|
|
107
|
+
|
|
108
|
+
Open Claude Code in the project directory — memory is ready.
|
|
109
|
+
|
|
110
|
+
### Configure later
|
|
111
|
+
|
|
112
|
+
If you skipped Azure credentials during install:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
npx claude-persistent-memory
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
This runs an interactive setup to configure credentials and start services.
|
|
119
|
+
|
|
120
|
+
### Manual install (from source)
|
|
121
|
+
|
|
122
|
+
<details>
|
|
123
|
+
<summary>Click to expand</summary>
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
git clone https://github.com/MIMI180306/claude-persistent-memory.git
|
|
127
|
+
cd claude-persistent-memory
|
|
128
|
+
npm install
|
|
129
|
+
cp config.default.js config.js
|
|
130
|
+
# Edit config.js with your Azure credentials
|
|
131
|
+
|
|
132
|
+
# Start services
|
|
133
|
+
npm run embedding-server # Terminal 1
|
|
134
|
+
npm run llm-server # Terminal 2
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Then manually configure `.mcp.json` and `.claude/settings.json` — see [Configuration](#configuration).
|
|
138
|
+
|
|
139
|
+
</details>
|
|
140
|
+
|
|
141
|
+
## MCP Tools
|
|
142
|
+
|
|
143
|
+
| Tool | Description |
|
|
144
|
+
|------|-------------|
|
|
145
|
+
| `memory_search` | Hybrid BM25 + vector search. Params: `query`, `limit?`, `type?`, `domain?` |
|
|
146
|
+
| `memory_save` | Save a new memory. Params: `content`, `type?`, `domain?`, `confidence?` |
|
|
147
|
+
| `memory_validate` | Feedback loop — helpful (+0.1) or unhelpful (-0.05). Params: `memory_id`, `is_valid` |
|
|
148
|
+
| `memory_stats` | System stats: total memories, type/domain distribution, cluster status |
|
|
149
|
+
|
|
150
|
+
## Hooks
|
|
151
|
+
|
|
152
|
+
| Hook | Event | Timeout | What it does |
|
|
153
|
+
|------|-------|---------|-------------|
|
|
154
|
+
| `user-prompt-hook.js` | UserPromptSubmit | 1500ms | Embeds user query → searches → injects top memories via stdout |
|
|
155
|
+
| `pre-tool-memory-hook.js` | PreToolUse | 300ms | Embeds tool context → searches → injects via `additionalContext` |
|
|
156
|
+
| `post-tool-memory-hook.js` | PostToolUse | 300ms | Embeds tool context + result → searches → injects via `additionalContext` |
|
|
157
|
+
| `pre-compact-hook.js` | PreCompact | async | Spawns LLM analysis of full transcript → extracts memories |
|
|
158
|
+
| `session-end-hook.js` | SessionEnd | async | Incremental transcript analysis + clustering + mature cluster merging |
|
|
159
|
+
|
|
160
|
+
## Memory Types
|
|
161
|
+
|
|
162
|
+
| Type | Half-life | Use case |
|
|
163
|
+
|------|-----------|----------|
|
|
164
|
+
| `fact` | 90 days | Stable facts about the codebase |
|
|
165
|
+
| `decision` | 90 days | Architectural decisions and rationale |
|
|
166
|
+
| `bug` | 60 days | Bug fixes and root causes |
|
|
167
|
+
| `pattern` | 90 days | Recurring code patterns |
|
|
168
|
+
| `context` | 30 days | Session-specific context |
|
|
169
|
+
| `preference` | 60 days | User workflow preferences |
|
|
170
|
+
| `skill` | never | Promoted from mature clusters |
|
|
171
|
+
|
|
172
|
+
## Memory Lifecycle
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
1. Save → memory_save or auto-extract from transcript
|
|
176
|
+
2. Structure → LLM converts to <what>/<when>/<do>/<warn> XML
|
|
177
|
+
3. Embed → bge-m3 generates 1024-dim vector
|
|
178
|
+
4. Search → BM25 + vector similarity, combined ranking
|
|
179
|
+
5. Validate → memory_validate adjusts confidence ±
|
|
180
|
+
6. Cluster → similar memories auto-grouped
|
|
181
|
+
7. Promote → mature clusters → skill memories
|
|
182
|
+
8. Decay → low-confidence memories fade over time
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Uninstall
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
npx claude-persistent-memory-uninstall
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Or manually: remove the `memory` entry from `.mcp.json`, remove memory hooks from `.claude/settings.json`, then `npm uninstall @alex/claude-persistent-memory`. The `.claude-memory/` data directory is preserved — delete it manually if no longer needed.
|
|
192
|
+
|
|
193
|
+
## Configuration
|
|
194
|
+
|
|
195
|
+
All settings in `config.default.js` (auto-loaded, override via `.claude-memory.config.js`):
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
module.exports = {
|
|
199
|
+
embeddingPort: 23811, // TCP port for embedding server
|
|
200
|
+
llmPort: 23812, // TCP port for LLM server
|
|
201
|
+
dataDir: './data', // memory.db lives here
|
|
202
|
+
azure: {
|
|
203
|
+
endpoint: process.env.AZURE_OPENAI_ENDPOINT || '',
|
|
204
|
+
apiKey: process.env.AZURE_OPENAI_KEY || '',
|
|
205
|
+
deployment: process.env.AZURE_OPENAI_DEPLOYMENT || 'gpt-4.1',
|
|
206
|
+
},
|
|
207
|
+
embedding: {
|
|
208
|
+
model: 'Xenova/bge-m3', // 1024 dimensions, 8192 token context
|
|
209
|
+
dimensions: 1024,
|
|
210
|
+
},
|
|
211
|
+
search: {
|
|
212
|
+
maxResults: 3, // top-K results per query
|
|
213
|
+
minSimilarity: 0.6, // vector similarity threshold
|
|
214
|
+
},
|
|
215
|
+
cluster: {
|
|
216
|
+
similarityThreshold: 0.70, // min similarity to join a cluster
|
|
217
|
+
maturityCount: 5, // memories needed for mature cluster
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Project Structure
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
claude-persistent-memory/
|
|
226
|
+
├── bin/ # CLI scripts
|
|
227
|
+
│ ├── setup.js # postinstall + interactive setup
|
|
228
|
+
│ └── uninstall.js # cleanup script
|
|
229
|
+
├── hooks/ # Claude Code lifecycle hooks
|
|
230
|
+
│ ├── user-prompt-hook.js # UserPromptSubmit → memory injection
|
|
231
|
+
│ ├── pre-tool-memory-hook.js # PreToolUse → memory injection
|
|
232
|
+
│ ├── post-tool-memory-hook.js # PostToolUse → memory injection
|
|
233
|
+
│ ├── pre-compact-hook.js # PreCompact → transcript analysis
|
|
234
|
+
│ └── session-end-hook.js # SessionEnd → clustering
|
|
235
|
+
├── lib/ # Core libraries
|
|
236
|
+
│ ├── memory-db.js # SQLite + FTS5 + sqlite-vec
|
|
237
|
+
│ ├── embedding-client.js # TCP client for embedding server
|
|
238
|
+
│ ├── llm-client.js # TCP client for LLM server
|
|
239
|
+
│ ├── compact-analyzer.js # Transcript → memory extraction
|
|
240
|
+
│ └── utils.js # Minimal utilities
|
|
241
|
+
├── services/ # Background services
|
|
242
|
+
│ ├── embedding-server.js # TCP embedding service (bge-m3)
|
|
243
|
+
│ ├── llm-server.js # TCP LLM proxy (Azure OpenAI)
|
|
244
|
+
│ └── memory-mcp-server.js # MCP server for Claude Code
|
|
245
|
+
├── tools/
|
|
246
|
+
│ └── rebuild-vectors.js # Rebuild all embeddings
|
|
247
|
+
├── config.default.js # Configuration template
|
|
248
|
+
├── CLAUDE.md # Claude Code project instructions
|
|
249
|
+
└── package.json
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Requirements
|
|
253
|
+
|
|
254
|
+
- Node.js >= 18
|
|
255
|
+
- macOS or Linux
|
|
256
|
+
- ~2GB RAM for embedding model (bge-m3)
|
|
257
|
+
- Azure OpenAI API access (for LLM structuring)
|
|
258
|
+
|
|
259
|
+
## Notes
|
|
260
|
+
|
|
261
|
+
- **LLM provider**: Currently supports Azure OpenAI only. For standard OpenAI or other providers, modify `services/llm-server.js`.
|
|
262
|
+
- **Ports**: Embedding and LLM servers default to TCP 23811 / 23812. Change in `config.js` if needed.
|
|
263
|
+
- **Data**: The `.claude-memory/` directory (containing `memory.db` and logs) is created automatically and gitignored.
|
|
264
|
+
|
|
265
|
+
## Contributing
|
|
266
|
+
|
|
267
|
+
Contributions are welcome! Please read the [Contributing Guide](CONTRIBUTING.md) before submitting a PR.
|
|
268
|
+
|
|
269
|
+
## License
|
|
270
|
+
|
|
271
|
+
[MIT](LICENSE) © MIMI180306
|