openclaw-mem 1.0.4 → 1.2.1

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.
Files changed (46) hide show
  1. package/HOOK.md +125 -0
  2. package/LICENSE +1 -1
  3. package/MCP.json +11 -0
  4. package/README.md +146 -168
  5. package/context-builder.js +703 -0
  6. package/database.js +520 -0
  7. package/debug-logger.js +280 -0
  8. package/extractor.js +211 -0
  9. package/gateway-llm.js +155 -0
  10. package/handler.js +1122 -0
  11. package/mcp-http-api.js +356 -0
  12. package/mcp-server.js +525 -0
  13. package/mem-get.sh +24 -0
  14. package/mem-search.sh +17 -0
  15. package/monitor.js +112 -0
  16. package/package.json +53 -29
  17. package/realtime-monitor.js +371 -0
  18. package/session-watcher.js +192 -0
  19. package/setup.js +114 -0
  20. package/sync-recent.js +63 -0
  21. package/README_CN.md +0 -201
  22. package/bin/openclaw-mem.js +0 -117
  23. package/docs/locales/README_AR.md +0 -35
  24. package/docs/locales/README_DE.md +0 -35
  25. package/docs/locales/README_ES.md +0 -35
  26. package/docs/locales/README_FR.md +0 -35
  27. package/docs/locales/README_HE.md +0 -35
  28. package/docs/locales/README_HI.md +0 -35
  29. package/docs/locales/README_ID.md +0 -35
  30. package/docs/locales/README_IT.md +0 -35
  31. package/docs/locales/README_JA.md +0 -57
  32. package/docs/locales/README_KO.md +0 -35
  33. package/docs/locales/README_NL.md +0 -35
  34. package/docs/locales/README_PL.md +0 -35
  35. package/docs/locales/README_PT.md +0 -35
  36. package/docs/locales/README_RU.md +0 -35
  37. package/docs/locales/README_TH.md +0 -35
  38. package/docs/locales/README_TR.md +0 -35
  39. package/docs/locales/README_UK.md +0 -35
  40. package/docs/locales/README_VI.md +0 -35
  41. package/docs/logo.svg +0 -32
  42. package/lib/context-builder.js +0 -415
  43. package/lib/database.js +0 -309
  44. package/lib/handler.js +0 -494
  45. package/scripts/commands.js +0 -141
  46. package/scripts/init.js +0 -248
package/HOOK.md ADDED
@@ -0,0 +1,125 @@
1
+ ---
2
+ name: openclaw-mem
3
+ description: "Persistent memory system - saves session context and injects history into new sessions"
4
+ homepage: https://github.com/openclaw-mem
5
+ metadata:
6
+ {
7
+ "openclaw":
8
+ {
9
+ "emoji": "🧠",
10
+ "events": ["command:new", "gateway:startup", "agent:bootstrap", "agent:response", "agent:stop", "tool:post", "user:prompt", "message"],
11
+ "requires": { "config": ["workspace.dir"] },
12
+ "install": [{ "id": "local", "kind": "local", "label": "Local installation" }],
13
+ },
14
+ }
15
+ ---
16
+
17
+ # OpenClaw-Mem: Persistent Memory System
18
+
19
+ A claude-mem inspired memory system for OpenClaw that automatically captures tool usage, generates summaries, and injects relevant context into new sessions.
20
+
21
+ ## Features
22
+
23
+ - 🧠 **Persistent Memory** - Context survives across sessions
24
+ - 📊 **Progressive Disclosure** - Shows index first, fetch details on demand
25
+ - 🔍 **Hybrid Search** - Full-text + semantic search
26
+ - 🤖 **AI Compression** - Automatic summarization of observations
27
+ - ⚡ **Token Efficient** - Only loads what's relevant
28
+ - 📡 **Real-time Capture** - Records messages as they happen
29
+
30
+ ## Events Captured
31
+
32
+ - `gateway:startup` - Initialize memory system
33
+ - `agent:bootstrap` - Inject historical context
34
+ - `agent:response` - Capture assistant responses in real-time
35
+ - `agent:stop` - Save session summary
36
+ - `command:new` - Save session content before reset
37
+ - `tool:post` - Capture tool usage
38
+ - `user:prompt` - Capture user messages
39
+ - `message` - Capture all messages
40
+
41
+ ## Configuration
42
+
43
+ ```json
44
+ {
45
+ "hooks": {
46
+ "internal": {
47
+ "entries": {
48
+ "openclaw-mem": {
49
+ "enabled": true,
50
+ "observationLimit": 50,
51
+ "fullDetailCount": 5,
52
+ "compressWithLLM": true
53
+ }
54
+ }
55
+ }
56
+ }
57
+ }
58
+ ```
59
+
60
+ ## Storage
61
+
62
+ Data is stored in SQLite at `~/.openclaw-mem/memory.db`:
63
+ - `sessions` - Session records
64
+ - `observations` - Tool call observations
65
+ - `summaries` - Session summaries
66
+
67
+ ## Real-time Monitoring
68
+
69
+ ```bash
70
+ node ~/.openclaw/hooks/openclaw-mem/monitor.js
71
+ ```
72
+
73
+ ## Usage
74
+
75
+ The memory system works automatically. To search manually:
76
+
77
+ ```bash
78
+ # Search memory
79
+ openclaw memory search "authentication"
80
+
81
+ # View status
82
+ openclaw memory status
83
+ ```
84
+
85
+ ## MCP Server (Model Context Protocol)
86
+
87
+ OpenClaw-Mem 提供 MCP Server,让 AI 可以按需查询记忆:
88
+
89
+ ### MCP 工具
90
+
91
+ | Tool | Description |
92
+ |------|-------------|
93
+ | `__IMPORTANT` | 显示 3 层工作流说明 |
94
+ | `search` | 搜索记忆索引 |
95
+ | `timeline` | 获取某条记录的上下文 |
96
+ | `get_observations` | 获取完整详情 |
97
+
98
+ ### 启动 MCP Server
99
+
100
+ ```bash
101
+ # stdio 模式(标准 MCP)
102
+ node ~/.openclaw/hooks/openclaw-mem/mcp-server.js
103
+
104
+ # HTTP API 模式(兼容模式)
105
+ node ~/.openclaw/hooks/openclaw-mem/mcp-http-api.js
106
+ ```
107
+
108
+ ### HTTP API 端点
109
+
110
+ ```bash
111
+ # 搜索
112
+ curl "http://127.0.0.1:18790/search?query=database&limit=10"
113
+
114
+ # Timeline
115
+ curl "http://127.0.0.1:18790/timeline?anchor=123"
116
+
117
+ # 获取详情
118
+ curl -X POST "http://127.0.0.1:18790/get_observations" -d '{"ids":[123,124]}'
119
+ ```
120
+
121
+ ## Disabling
122
+
123
+ ```bash
124
+ openclaw hooks disable openclaw-mem
125
+ ```
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 OpenClaw Contributors
3
+ Copyright (c) 2026 Aaron
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/MCP.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "mcpServers": {
3
+ "openclaw-mem-search": {
4
+ "type": "stdio",
5
+ "command": "node",
6
+ "args": ["mcp-server.js"],
7
+ "cwd": "${HOOK_ROOT}",
8
+ "description": "Memory search tools for OpenClaw-Mem"
9
+ }
10
+ }
11
+ }
package/README.md CHANGED
@@ -1,217 +1,195 @@
1
- <p align="center">
2
- <img src="docs/logo.svg" alt="OpenClaw-Mem Logo" width="300"/>
3
- </p>
4
-
5
- <h1 align="center">OpenClaw-Mem 🧠</h1>
6
-
7
- <p align="center">
8
- <strong>Give your OpenClaw AI agent persistent long-term memory</strong>
9
- </p>
10
-
11
- <p align="center">
12
- <a href="https://www.npmjs.com/package/openclaw-mem"><img src="https://img.shields.io/npm/v/openclaw-mem.svg" alt="npm version"/></a>
13
- <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"/></a>
14
- <a href="https://www.npmjs.com/package/openclaw-mem"><img src="https://img.shields.io/npm/dm/openclaw-mem.svg" alt="npm downloads"/></a>
15
- </p>
16
-
17
- <p align="center">
18
- <a href="README_CN.md">🇨🇳 中文</a>
19
- <a href="docs/locales/README_JA.md">🇯🇵 日本語</a> •
20
- <a href="docs/locales/README_KO.md">🇰🇷 한국어</a> •
21
- <a href="docs/locales/README_ES.md">🇪🇸 Español</a> •
22
- <a href="docs/locales/README_DE.md">🇩🇪 Deutsch</a> •
23
- <a href="docs/locales/README_FR.md">🇫🇷 Français</a> •
24
- <a href="docs/locales/README_PT.md">🇧🇷 Português</a>
25
- <br/>
26
- <a href="docs/locales/README_RU.md">🇷🇺 Русский</a> •
27
- <a href="docs/locales/README_AR.md">🇸🇦 العربية</a> •
28
- <a href="docs/locales/README_HE.md">🇮🇱 עברית</a> •
29
- <a href="docs/locales/README_TR.md">🇹🇷 Türkçe</a> •
30
- <a href="docs/locales/README_IT.md">🇮🇹 Italiano</a> •
31
- <a href="docs/locales/README_NL.md">🇳🇱 Nederlands</a> •
32
- <a href="docs/locales/README_PL.md">🇵🇱 Polski</a>
33
- <br/>
34
- <a href="docs/locales/README_UK.md">🇺🇦 Українська</a> •
35
- <a href="docs/locales/README_VI.md">🇻🇳 Tiếng Việt</a> •
36
- <a href="docs/locales/README_TH.md">🇹🇭 ไทย</a> •
37
- <a href="docs/locales/README_HI.md">🇮🇳 हिन्दी</a> •
38
- <a href="docs/locales/README_ID.md">🇮🇩 Indonesia</a>
39
- </p>
40
-
41
- <hr/>
42
-
43
- OpenClaw-Mem automatically captures your conversations and makes them searchable, allowing your AI assistant to remember what you've discussed across sessions.
44
-
45
- ## ✨ Features
46
-
47
- - **🔄 Automatic Memory Capture** - Conversations are saved automatically
48
- - **🔍 Full-Text Search** - Search through your entire conversation history
49
- - **📊 Progressive Disclosure** - Efficient token usage with layered context
50
- - **🎯 Topic Detection** - Automatically indexes discussions by topic
51
- - **💾 Local Storage** - All data stays on your machine (SQLite)
52
- - **⚡ Zero Config** - Works out of the box
53
-
54
- ## 🚀 Quick Start
1
+ # OpenClaw-Mem
2
+
3
+ A persistent memory system for [OpenClaw](https://github.com/openclaw) that automatically captures conversations, generates summaries, and injects relevant context into new sessions.
4
+
5
+ ## Features
6
+
7
+ - **Persistent Memory** - Context survives across sessions
8
+ - **Progressive Disclosure** - Shows index first, fetch details on demand
9
+ - **Hybrid Search** - Full-text + LIKE search with CJK support
10
+ - **AI Compression** - Automatic summarization of observations
11
+ - **Token Efficient** - Only loads what's relevant
12
+ - **Real-time Capture** - Records messages as they happen
13
+ - **MCP Compatible** - Model Context Protocol server included
14
+ - **HTTP API** - REST API for memory queries
15
+
16
+ ## Installation
17
+
18
+ ### As OpenClaw Hook
55
19
 
56
20
  ```bash
57
- # Install and setup (one command!)
58
- npx openclaw-mem init
21
+ # Clone to OpenClaw hooks directory
22
+ git clone https://github.com/wenyupapa-sys/openclaw-mem.git ~/.openclaw/hooks/openclaw-mem
23
+ cd ~/.openclaw/hooks/openclaw-mem
24
+ npm install
25
+ ```
59
26
 
60
- # Restart OpenClaw gateway
61
- openclaw gateway restart
27
+ ### As npm Package
28
+
29
+ ```bash
30
+ npm install openclaw-mem
62
31
  ```
63
32
 
64
- That's it! Start chatting and your conversations will be remembered.
33
+ ## Quick Start
65
34
 
66
- ## 📖 Usage
35
+ 1. **Install the hook** (see above)
67
36
 
68
- ### Recalling Memories in Chat
37
+ 2. **Run setup** - configure your DeepSeek API key (prompted automatically after install)
38
+ ```bash
39
+ # Or run manually later
40
+ npm run setup
41
+ # or
42
+ npx openclaw-mem-setup
43
+ ```
69
44
 
70
- Ask your AI assistant:
71
- - "What did we discuss before?"
72
- - "What were we working on last time?"
73
- - "Remind me about the authentication issue"
45
+ 3. **Restart OpenClaw** to load the hook
74
46
 
75
- ### CLI Commands
47
+ 4. **Start chatting** - conversations are automatically saved
76
48
 
77
- ```bash
78
- # Check memory status
79
- npx openclaw-mem status
49
+ 5. **Query memories** - ask "what did we discuss before?" and the AI will search the memory database
80
50
 
81
- # Search your memory
82
- npx openclaw-mem search "authentication"
83
- npx openclaw-mem search "AI memory"
51
+ ## Events Captured
84
52
 
85
- # Remove (keeps database)
86
- npx openclaw-mem uninstall
87
- ```
53
+ | Event | Description |
54
+ |-------|-------------|
55
+ | `gateway:startup` | Initialize memory system |
56
+ | `agent:bootstrap` | Inject historical context |
57
+ | `agent:response` | Capture assistant responses |
58
+ | `agent:stop` | Save session summary |
59
+ | `command:new` | Save session before reset |
60
+ | `tool:post` | Capture tool usage |
61
+ | `user:prompt` | Capture user messages |
88
62
 
89
- ## 🏗️ How It Works
63
+ ## API Reference
90
64
 
91
- ```
92
- +-------------+ +------------------+ +------------+
93
- | Telegram | --> | OpenClaw Gateway | --> | AI Agent |
94
- +-------------+ +------------------+ +------------+
95
- | |
96
- v v
97
- +--------------+ +------------+
98
- | openclaw-mem | | Read Tool |
99
- | hook | +------------+
100
- +--------------+ |
101
- | v
102
- +-------------------+----------------------+
103
- | |
104
- v v
105
- +---------------+ +------------------+
106
- | SQLite DB | | SESSION-MEMORY.md|
107
- | (persistent) | | (injected) |
108
- +---------------+ +------------------+
65
+ ### HTTP API (Port 18790)
66
+
67
+ ```bash
68
+ # Search memories
69
+ curl -s -X POST "http://127.0.0.1:18790/search" \
70
+ -H "Content-Type: application/json" \
71
+ -d '{"query":"keyword","limit":10}'
72
+
73
+ # Get observation details
74
+ curl -s -X POST "http://127.0.0.1:18790/get_observations" \
75
+ -H "Content-Type: application/json" \
76
+ -d '{"ids":[123,124]}'
77
+
78
+ # Get timeline context
79
+ curl -s -X POST "http://127.0.0.1:18790/timeline" \
80
+ -H "Content-Type: application/json" \
81
+ -d '{"anchor":123}'
82
+
83
+ # Health check
84
+ curl "http://127.0.0.1:18790/health"
109
85
  ```
110
86
 
111
- ### Event Flow
87
+ ### Shell Scripts
112
88
 
113
- 1. **`gateway:startup`** - Initializes the memory database
114
- 2. **`agent:bootstrap`** - Injects historical context into new sessions
115
- 3. **`command:new`** - Saves session summary when you start fresh
89
+ ```bash
90
+ # Search (handles CJK encoding automatically)
91
+ ~/.openclaw/hooks/openclaw-mem/mem-search.sh "关键词" 10
116
92
 
117
- ### Progressive Disclosure
93
+ # Get details
94
+ ~/.openclaw/hooks/openclaw-mem/mem-get.sh 123 124 125
95
+ ```
118
96
 
119
- To optimize token usage, memories are organized in layers:
97
+ ### MCP Server
120
98
 
121
- | Layer | Content | Tokens |
122
- |-------|---------|--------|
123
- | Index | Compact table of all observations | ~Low |
124
- | Topics | Key discussion summaries | ~Medium |
125
- | Details | Full content (on demand) | ~High |
99
+ ```bash
100
+ # Start MCP server (stdio mode)
101
+ node mcp-server.js
102
+ ```
126
103
 
127
- ## 📁 File Locations
104
+ MCP Tools:
105
+ - `search` - Search memory index
106
+ - `timeline` - Get context around an observation
107
+ - `get_observations` - Fetch full details
128
108
 
129
- | File | Location | Purpose |
130
- |------|----------|---------|
131
- | Database | `~/.openclaw-mem/memory.db` | Persistent storage |
132
- | Hook | `~/.openclaw/hooks/openclaw-mem/` | Event handlers |
133
- | Memory Context | `~/.openclaw/workspace/SESSION-MEMORY.md` | Injected context |
109
+ ## Configuration
134
110
 
135
- ## ⚙️ Configuration
111
+ ### Environment Variables
136
112
 
137
- ### Customize MEMORY.md
113
+ ```bash
114
+ # Required for AI summarization (optional but recommended)
115
+ export DEEPSEEK_API_KEY="your-deepseek-api-key"
138
116
 
139
- Add your preferences to `~/.openclaw/workspace/MEMORY.md`:
117
+ # Optional: Custom DeepSeek endpoint
118
+ export DEEPSEEK_BASE_URL="https://api.deepseek.com/v1"
140
119
 
141
- ```markdown
142
- ## User Preferences
143
- - Communication style: Technical and concise
144
- - Language: English
120
+ # Optional: Custom model
121
+ export DEEPSEEK_MODEL="deepseek-chat"
122
+ ```
145
123
 
146
- ## Long-Term Context
147
- - Working on: AI memory systems
148
- - Interests: Machine learning, distributed systems
124
+ Get your DeepSeek API key at: https://platform.deepseek.com/
149
125
 
150
- ## Ongoing Focus
151
- - Current project: Building a chat application
152
- ```
126
+ > **Note:** Without `DEEPSEEK_API_KEY`, the system will still work but won't generate AI summaries for sessions.
153
127
 
154
- ### Dynamic Topic Detection
128
+ ### OpenClaw Config
155
129
 
156
- The system automatically extracts topics from your actual conversations:
157
- - Concepts mentioned frequently are detected automatically
158
- - No hardcoded keywords - everything comes from your data
159
- - Topics evolve as your discussions change
130
+ Add to your OpenClaw config:
160
131
 
161
- ## 🔧 Troubleshooting
132
+ ```json
133
+ {
134
+ "hooks": {
135
+ "internal": {
136
+ "entries": {
137
+ "openclaw-mem": {
138
+ "enabled": true,
139
+ "observationLimit": 50,
140
+ "fullDetailCount": 5
141
+ }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ ```
162
147
 
163
- ### Memory not being recalled?
148
+ ## Storage
164
149
 
165
- 1. **Restart the gateway:**
166
- ```bash
167
- openclaw gateway restart
168
- ```
150
+ Data is stored in SQLite at `~/.openclaw-mem/memory.db`:
169
151
 
170
- 2. **Check hook is installed:**
171
- ```bash
172
- ls ~/.openclaw/hooks/openclaw-mem/
173
- ```
152
+ | Table | Description |
153
+ |-------|-------------|
154
+ | `sessions` | Session records |
155
+ | `observations` | Tool calls and messages |
156
+ | `summaries` | Session summaries |
157
+ | `user_prompts` | User inputs |
174
158
 
175
- 3. **Verify database has content:**
176
- ```bash
177
- npx openclaw-mem status
178
- ```
159
+ ## Development
179
160
 
180
- ### Search not finding results?
161
+ ```bash
162
+ # Run tests
163
+ npm test
181
164
 
182
- - Try simpler search terms
183
- - Check for typos
184
- - Use keywords from the actual conversation
165
+ # Start HTTP API server
166
+ npm run api
185
167
 
186
- ### Database issues?
168
+ # Start MCP server
169
+ npm run mcp
187
170
 
188
- ```bash
189
- # Reset database (⚠️ deletes all memories)
190
- rm ~/.openclaw-mem/memory.db
191
- openclaw gateway restart
171
+ # Monitor real-time activity
172
+ node debug-logger.js
192
173
  ```
193
174
 
194
- ## 🤝 Contributing
175
+ ## 3-Layer Retrieval Workflow
176
+
177
+ For efficient token usage, use progressive disclosure:
195
178
 
196
- Contributions are welcome! Please feel free to submit a Pull Request.
179
+ 1. **Search** Get index with IDs (~50-100 tokens/result)
180
+ 2. **Timeline** → Get context around interesting results
181
+ 3. **Get Observations** → Fetch full details ONLY for filtered IDs
197
182
 
198
- 1. Fork the repository
199
- 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
200
- 3. Commit your changes (`git commit -m 'Add amazing feature'`)
201
- 4. Push to the branch (`git push origin feature/amazing-feature`)
202
- 5. Open a Pull Request
183
+ This approach saves ~30% tokens compared to fetching everything.
203
184
 
204
- ## 📄 License
185
+ ## License
205
186
 
206
- MIT License - see [LICENSE](LICENSE) for details.
187
+ MIT
207
188
 
208
- ## 🙏 Acknowledgments
189
+ ## Contributing
209
190
 
210
- - Inspired by [claude-mem](https://github.com/anthropics/claude-mem)
211
- - Built for [OpenClaw](https://openclaw.ai)
191
+ Pull requests welcome! Please ensure tests pass before submitting.
212
192
 
213
- ---
193
+ ## Credits
214
194
 
215
- <p align="center">
216
- Made with ❤️ for the AI community
217
- </p>
195
+ Inspired by [claude-mem](https://github.com/anthropics/claude-code) plugin architecture.