th-memory-mcp 1.2.2 → 2.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/README.md +209 -187
- package/README.th.md +23 -7
- package/design.md +98 -308
- package/dist/core/consolidation-engine.js +87 -0
- package/dist/core/context-engine.js +50 -0
- package/dist/core/graph-engine.js +67 -0
- package/dist/core/lifecycle-engine.js +76 -0
- package/dist/core/retrieval-engine.js +40 -0
- package/dist/core/temporal-engine.js +73 -0
- package/dist/db/index.js +111 -0
- package/dist/db/migrations.js +160 -0
- package/dist/db/repositories/memories.js +52 -0
- package/dist/db.js +3 -0
- package/dist/index.js +12 -0
- package/dist/lib/embed.js +8 -5
- package/dist/memory/conflict-resolver.js +125 -0
- package/dist/memory/decay.js +30 -0
- package/dist/memory/deduplicator.js +51 -0
- package/dist/memory/scorer.js +44 -0
- package/dist/memory/source-weights.js +13 -0
- package/dist/memory/types.js +41 -0
- package/dist/retrieval/fts.js +22 -0
- package/dist/retrieval/fusion.js +11 -0
- package/dist/retrieval/scorer.js +19 -0
- package/dist/retrieval/vector.js +29 -0
- package/dist/tools/consolidate.js +63 -0
- package/dist/tools/context.js +55 -0
- package/dist/tools/export_memory.js +1 -1
- package/dist/tools/forget.js +1 -1
- package/dist/tools/history.js +1 -1
- package/dist/tools/lesson.js +1 -1
- package/dist/tools/memory_stats.js +1 -1
- package/dist/tools/profile.js +1 -1
- package/dist/tools/recall.js +1 -1
- package/dist/tools/recent_interactions.js +1 -1
- package/dist/tools/remember.js +1 -1
- package/package.json +46 -46
package/README.md
CHANGED
|
@@ -1,187 +1,209 @@
|
|
|
1
|
-
# th-memory-mcp
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/th-memory-mcp)
|
|
4
|
-
[](https://www.npmjs.com/package/th-memory-mcp)
|
|
5
|
-
[](LICENSE)
|
|
6
|
-
[](https://nodejs.org)
|
|
7
|
-
|
|
8
|
-
Long-term memory MCP server for OpenCode — stores preferences, lessons, and usage history in a single local SQLite file (100% local, no external API) so the AI can "remember and adapt" to the user through context-based learning.
|
|
9
|
-
|
|
10
|
-
**Status:**
|
|
11
|
-
|
|
12
|
-
## Requirements
|
|
13
|
-
|
|
14
|
-
- **Node.js >= 20** — the server uses Node-only APIs (the `better-sqlite3` native build and `import.meta.url` resolution) and the MCP SDK requires a modern runtime. CI tests on Node 20.x and 22.x.
|
|
15
|
-
- **npm** — to install dependencies and run the build/test scripts (`npm install`, `npm run build`, `npm test`).
|
|
16
|
-
- **OpenCode** — the host that loads this MCP server and the auto-capture plugin. Any build supporting MCP over stdio + plugins works; the plugin runs on OpenCode's bundled Bun runtime.
|
|
17
|
-
- **OS: Windows / macOS / Linux** — the server is cross-platform (Node). The auto-capture plugin runs wherever OpenCode's Bun runtime runs. Windows note: `MEMORY_DB_PATH` is easiest to set with `setx`; on macOS/Linux use `export` in your shell profile.
|
|
18
|
-
|
|
19
|
-
No external services, accounts, or API keys are required — everything lives in a single local SQLite file.
|
|
20
|
-
|
|
21
|
-
## Quick Start
|
|
22
|
-
|
|
23
|
-
**Fastest path:** after cloning, run `npm run quickstart` — it builds, wires `opencode.json`, deploys the plugin, and sets `MEMORY_DB_PATH` for you in one command. The steps below show exactly what it does (use them if you prefer manual control).
|
|
24
|
-
|
|
25
|
-
**Install via npm (alternative):** install the server globally with `npm install -g th-memory-mcp` (or run it on demand with `npx th-memory-mcp`), then point the `mcp` `command` in `opencode.json` to `th-memory-mcp` instead of the built `dist/index.js`. The auto-capture plugin still comes from this repo (copy `src/plugin/learning-capture.ts` as described in step 4 below).
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
# 1. Clone and build
|
|
29
|
-
git clone https://github.com/worakorn-prince/th-memory-mcp.git
|
|
30
|
-
cd th-memory-mcp
|
|
31
|
-
npm install
|
|
32
|
-
npm run build
|
|
33
|
-
|
|
34
|
-
# 2. Share one DB between the server and the plugin
|
|
35
|
-
# Windows (PowerShell):
|
|
36
|
-
setx MEMORY_DB_PATH "$PWD/data/memory.db"
|
|
37
|
-
# macOS / Linux (add to your shell profile, e.g. ~/.zshrc):
|
|
38
|
-
# export MEMORY_DB_PATH="$PWD/data/memory.db"
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
3. Merge this into your `~/.config/opencode/opencode.json` (replace `<REPO>` with the absolute clone path):
|
|
42
|
-
|
|
43
|
-
```json
|
|
44
|
-
{
|
|
45
|
-
"instructions": ["<REPO>/AGENTS.memory.example.md"],
|
|
46
|
-
"mcp": {
|
|
47
|
-
"memory": {
|
|
48
|
-
"type": "local",
|
|
49
|
-
"command": ["node", "<REPO>/dist/index.js"],
|
|
50
|
-
"enabled": true,
|
|
51
|
-
"environment": { "MEMORY_DB_PATH": "<REPO>/data/memory.db" }
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
4. (Optional) Auto-capture: copy `src/plugin/learning-capture.ts` → `~/.config/opencode/plugins/`
|
|
58
|
-
5. **Restart OpenCode**
|
|
59
|
-
6. Try it: *"Remember that I prefer pnpm"* → new session → *"What package manager do I prefer?"*
|
|
60
|
-
|
|
61
|
-
## Architecture
|
|
62
|
-
|
|
63
|
-
```
|
|
64
|
-
OpenCode ──┬─ Plugin learning-capture (Bun) ── auto-captures prompts/tool/error into DB
|
|
65
|
-
│ └─ injects profile back into context on compaction
|
|
66
|
-
|
|
67
|
-
▲
|
|
68
|
-
Global instructions (memory-protocol.md) teach the AI to use the tools
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
See [design.md](design.md) for full details.
|
|
72
|
-
|
|
73
|
-
## Why th-memory-mcp?
|
|
74
|
-
|
|
75
|
-
LLMs don't remember you between sessions — every new chat starts blank. th-memory-mcp gives your AI a private, local long-term memory:
|
|
76
|
-
|
|
77
|
-
- **Context-based learning, not fine-tuning** — it captures your preferences, corrections, and habits, then recalls them into context next time. Same mechanism as the memory features of leading AI products, without sending any data off your machine.
|
|
78
|
-
- **100% local & private** — a single SQLite file, no cloud, no external API. Secrets are filtered before anything is stored.
|
|
79
|
-
- **Low overhead** — each tool call is capped (latency < 10 ms, bounded output size) and the AI only queries memory when it's actually useful, so it never bloats your context.
|
|
80
|
-
- **Resilient** — every tool degrades gracefully; if the DB is unavailable the AI keeps working instead of crashing.
|
|
81
|
-
- **Open & extensible** — MIT licensed,
|
|
82
|
-
|
|
83
|
-
## Works with other harnesses
|
|
84
|
-
|
|
85
|
-
th-memory-mcp is a standard MCP server, so the 9 tools run anywhere MCP-over-stdio
|
|
86
|
-
is supported. Full **auto-capture** (background prompt/tool/error capture + profile
|
|
87
|
-
injection) needs a hook runtime — OpenCode has it built in; Claude Code gets it via
|
|
88
|
-
our hooks bridge; Codex and Cursor use the tools manually (no hook runtime yet).
|
|
89
|
-
|
|
90
|
-
| Feature | OpenCode | Claude Code | Qwen Code | Codex | Cursor |
|
|
91
|
-
|---|---|---|---|---|---|
|
|
92
|
-
|
|
|
93
|
-
| Auto-capture (background) | ✅ plugin | ✅ [hooks](CLAUDE_CODE_HOOKS.md) | ⚠️ adapter | ❌ manual | ❌ Rules |
|
|
94
|
-
| Profile injection | ✅ compaction | ✅ UserPromptSubmit | ❌ `get_profile` | ❌ `get_profile` | ❌ `get_profile` |
|
|
95
|
-
| Local semantic search | ✅ (
|
|
96
|
-
|
|
97
|
-
- **Claude Code:** see [CLAUDE_CODE_HOOKS.md](CLAUDE_CODE_HOOKS.md) — drop-in hooks replicate the OpenCode plugin (capture + profile injection on `UserPromptSubmit`/`PreCompact`, rule-based distill on `SessionEnd`).
|
|
98
|
-
- **Qwen Code:** see [QWEN_SETUP.md](QWEN_SETUP.md) — MCP works fully; hooks use the Gemini-CLI schema so auto-capture needs a small adapter.
|
|
99
|
-
- **Codex:** see [CODEX_SETUP.md](CODEX_SETUP.md)
|
|
100
|
-
- **Cursor:** see [CURSOR_SETUP.md](CURSOR_SETUP.md)
|
|
101
|
-
|
|
102
|
-
All harnesses share one SQLite file via `MEMORY_DB_PATH`, so memory captured
|
|
103
|
-
anywhere is readable everywhere.
|
|
104
|
-
|
|
105
|
-
## Highlights
|
|
106
|
-
|
|
107
|
-
- **Structured memory** — preferences with confidence scoring plus dedicated
|
|
108
|
-
`lesson` records (situation → mistake → correction) for capturing corrections,
|
|
109
|
-
not just flat facts.
|
|
110
|
-
- **
|
|
111
|
-
|
|
112
|
-
-
|
|
113
|
-
|
|
114
|
-
- **
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
| `
|
|
138
|
-
| `
|
|
139
|
-
| `
|
|
140
|
-
| `
|
|
141
|
-
| `
|
|
142
|
-
| `
|
|
143
|
-
| `
|
|
144
|
-
| `
|
|
145
|
-
| `
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
1
|
+
# th-memory-mcp
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/th-memory-mcp)
|
|
4
|
+
[](https://www.npmjs.com/package/th-memory-mcp)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
|
|
8
|
+
Long-term memory MCP server for OpenCode — stores preferences, lessons, and usage history in a single local SQLite file (100% local, no external API) so the AI can "remember and adapt" to the user through context-based learning.
|
|
9
|
+
|
|
10
|
+
**Status:** v2.0.0 — a temporal, conflict-aware, hybrid-retrieval memory engine. 11 MCP tools, 12 passing test suites. Non-destructive schema migration from v1 (all v1 data preserved). New in v2: lifecycle states, temporal validity, conflict/dedup resolution, hybrid FTS+vector retrieval (RRF), memory graph, `get_context` assembly, and periodic consolidation.
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- **Node.js >= 20** — the server uses Node-only APIs (the `better-sqlite3` native build and `import.meta.url` resolution) and the MCP SDK requires a modern runtime. CI tests on Node 20.x and 22.x.
|
|
15
|
+
- **npm** — to install dependencies and run the build/test scripts (`npm install`, `npm run build`, `npm test`).
|
|
16
|
+
- **OpenCode** — the host that loads this MCP server and the auto-capture plugin. Any build supporting MCP over stdio + plugins works; the plugin runs on OpenCode's bundled Bun runtime.
|
|
17
|
+
- **OS: Windows / macOS / Linux** — the server is cross-platform (Node). The auto-capture plugin runs wherever OpenCode's Bun runtime runs. Windows note: `MEMORY_DB_PATH` is easiest to set with `setx`; on macOS/Linux use `export` in your shell profile.
|
|
18
|
+
|
|
19
|
+
No external services, accounts, or API keys are required — everything lives in a single local SQLite file.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
**Fastest path:** after cloning, run `npm run quickstart` — it builds, wires `opencode.json`, deploys the plugin, and sets `MEMORY_DB_PATH` for you in one command. The steps below show exactly what it does (use them if you prefer manual control).
|
|
24
|
+
|
|
25
|
+
**Install via npm (alternative):** install the server globally with `npm install -g th-memory-mcp` (or run it on demand with `npx th-memory-mcp`), then point the `mcp` `command` in `opencode.json` to `th-memory-mcp` instead of the built `dist/index.js`. The auto-capture plugin still comes from this repo (copy `src/plugin/learning-capture.ts` as described in step 4 below).
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# 1. Clone and build
|
|
29
|
+
git clone https://github.com/worakorn-prince/th-memory-mcp.git
|
|
30
|
+
cd th-memory-mcp
|
|
31
|
+
npm install
|
|
32
|
+
npm run build
|
|
33
|
+
|
|
34
|
+
# 2. Share one DB between the server and the plugin
|
|
35
|
+
# Windows (PowerShell):
|
|
36
|
+
setx MEMORY_DB_PATH "$PWD/data/memory.db"
|
|
37
|
+
# macOS / Linux (add to your shell profile, e.g. ~/.zshrc):
|
|
38
|
+
# export MEMORY_DB_PATH="$PWD/data/memory.db"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
3. Merge this into your `~/.config/opencode/opencode.json` (replace `<REPO>` with the absolute clone path):
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"instructions": ["<REPO>/AGENTS.memory.example.md"],
|
|
46
|
+
"mcp": {
|
|
47
|
+
"memory": {
|
|
48
|
+
"type": "local",
|
|
49
|
+
"command": ["node", "<REPO>/dist/index.js"],
|
|
50
|
+
"enabled": true,
|
|
51
|
+
"environment": { "MEMORY_DB_PATH": "<REPO>/data/memory.db" }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
4. (Optional) Auto-capture: copy `src/plugin/learning-capture.ts` → `~/.config/opencode/plugins/`
|
|
58
|
+
5. **Restart OpenCode**
|
|
59
|
+
6. Try it: *"Remember that I prefer pnpm"* → new session → *"What package manager do I prefer?"*
|
|
60
|
+
|
|
61
|
+
## Architecture
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
OpenCode ──┬─ Plugin learning-capture (Bun) ── auto-captures prompts/tool/error into DB
|
|
65
|
+
│ └─ injects profile back into context on compaction
|
|
66
|
+
└─ MCP th-memory-mcp (Node.js stdio) ── 11 tools read/write the same SQLite DB
|
|
67
|
+
▲
|
|
68
|
+
Global instructions (memory-protocol.md) teach the AI to use the tools
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
See [design.md](design.md) for full details.
|
|
72
|
+
|
|
73
|
+
## Why th-memory-mcp?
|
|
74
|
+
|
|
75
|
+
LLMs don't remember you between sessions — every new chat starts blank. th-memory-mcp gives your AI a private, local long-term memory:
|
|
76
|
+
|
|
77
|
+
- **Context-based learning, not fine-tuning** — it captures your preferences, corrections, and habits, then recalls them into context next time. Same mechanism as the memory features of leading AI products, without sending any data off your machine.
|
|
78
|
+
- **100% local & private** — a single SQLite file, no cloud, no external API. Secrets are filtered before anything is stored.
|
|
79
|
+
- **Low overhead** — each tool call is capped (latency < 10 ms, bounded output size) and the AI only queries memory when it's actually useful, so it never bloats your context.
|
|
80
|
+
- **Resilient** — every tool degrades gracefully; if the DB is unavailable the AI keeps working instead of crashing.
|
|
81
|
+
- **Open & extensible** — MIT licensed, 11 documented tools, a rule-based distill, and an auto-capture plugin you can adapt.
|
|
82
|
+
|
|
83
|
+
## Works with other harnesses
|
|
84
|
+
|
|
85
|
+
th-memory-mcp is a standard MCP server, so the 9 tools run anywhere MCP-over-stdio
|
|
86
|
+
is supported. Full **auto-capture** (background prompt/tool/error capture + profile
|
|
87
|
+
injection) needs a hook runtime — OpenCode has it built in; Claude Code gets it via
|
|
88
|
+
our hooks bridge; Codex and Cursor use the tools manually (no hook runtime yet).
|
|
89
|
+
|
|
90
|
+
| Feature | OpenCode | Claude Code | Qwen Code | Codex | Cursor |
|
|
91
|
+
|---|---|---|---|---|---|
|
|
92
|
+
| 11 MCP tools | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
93
|
+
| Auto-capture (background) | ✅ plugin | ✅ [hooks](CLAUDE_CODE_HOOKS.md) | ⚠️ adapter | ❌ manual | ❌ Rules |
|
|
94
|
+
| Profile injection | ✅ compaction | ✅ UserPromptSubmit | ❌ `get_profile` | ❌ `get_profile` | ❌ `get_profile` |
|
|
95
|
+
| Local semantic search | ✅ (v2.0) | ✅ (v2.0) | ✅ (v2.0) | ✅ (v2.0) | ✅ (v2.0) |
|
|
96
|
+
|
|
97
|
+
- **Claude Code:** see [CLAUDE_CODE_HOOKS.md](CLAUDE_CODE_HOOKS.md) — drop-in hooks replicate the OpenCode plugin (capture + profile injection on `UserPromptSubmit`/`PreCompact`, rule-based distill on `SessionEnd`).
|
|
98
|
+
- **Qwen Code:** see [QWEN_SETUP.md](QWEN_SETUP.md) — MCP works fully; hooks use the Gemini-CLI schema so auto-capture needs a small adapter.
|
|
99
|
+
- **Codex:** see [CODEX_SETUP.md](CODEX_SETUP.md)
|
|
100
|
+
- **Cursor:** see [CURSOR_SETUP.md](CURSOR_SETUP.md)
|
|
101
|
+
|
|
102
|
+
All harnesses share one SQLite file via `MEMORY_DB_PATH`, so memory captured
|
|
103
|
+
anywhere is readable everywhere.
|
|
104
|
+
|
|
105
|
+
## Highlights
|
|
106
|
+
|
|
107
|
+
- **Structured memory** — preferences with confidence scoring plus dedicated
|
|
108
|
+
`lesson` records (situation → mistake → correction) for capturing corrections,
|
|
109
|
+
not just flat facts.
|
|
110
|
+
- **Lifecycle & temporal** — every memory has a lifecycle state
|
|
111
|
+
(active/stale/superseded/archived), confidence/importance/salience scoring,
|
|
112
|
+
per-type decay, and validity intervals so the AI can reason about
|
|
113
|
+
point-in-time truth and supersession chains.
|
|
114
|
+
- **Conflict-aware** — duplicate detection, contradiction detection, and
|
|
115
|
+
update/supersession resolution preserve both sides of ambiguous evidence
|
|
116
|
+
instead of silently overwriting.
|
|
117
|
+
- **Hybrid retrieval** — `get_context` blends FTS5 keyword search with a
|
|
118
|
+
dependency-free local vector embedding (RRF fusion + scoring), then assembles
|
|
119
|
+
a token-budgeted context with optional memory-graph expansion.
|
|
120
|
+
- **Consolidation** — periodic clustering of similar memories into derived
|
|
121
|
+
memories with full provenance (`derived_from` links).
|
|
122
|
+
- **First-class Thai / i18n** — Thai-aware tokenization in distill; the AI
|
|
123
|
+
accepts Thai and English interchangeably.
|
|
124
|
+
- **Private by default** — a single local SQLite file, no cloud, no API keys,
|
|
125
|
+
with secret lines (`api_key=`, `password:`, `token`) filtered before storage.
|
|
126
|
+
- **Cross-harness** — runs on OpenCode, Claude Code, Codex, and Cursor sharing
|
|
127
|
+
one DB; auto-capture + profile injection via OpenCode plugin or Claude hooks.
|
|
128
|
+
- **Lightweight & resilient** — Node + `better-sqlite3`, no extra native
|
|
129
|
+
extensions; every tool degrades gracefully so the AI keeps working if the DB
|
|
130
|
+
is unavailable.
|
|
131
|
+
|
|
132
|
+
## Scripts
|
|
133
|
+
|
|
134
|
+
| Command | Description |
|
|
135
|
+
|---------|-------------|
|
|
136
|
+
| `npm run build` | compile TypeScript → `dist/` |
|
|
137
|
+
| `npm start` | run the MCP server (stdio) from `dist/index.js` |
|
|
138
|
+
| `npm run distill` | rule-based distill: interactions → profile sections + prune old data (env `RETENTION_DAYS` default 30) |
|
|
139
|
+
| `npm test` | full suite: capture, distill, lifecycle, temporal, conflict, retrieval, graph, context, consolidation, benchmark, security, smoke |
|
|
140
|
+
| `node test/capture.test.mjs` | test capture-core (filter secrets, dedupe, truncate, insert SQL) |
|
|
141
|
+
| `node test/distill.test.mjs` | test distill-core (Thai tokenize, stats, profile sections, prune) |
|
|
142
|
+
| `node test/lifecycle.test.mjs` | test lifecycle engine (states, decay, supersession) |
|
|
143
|
+
| `node test/temporal.test.mjs` | test temporal model (validity, historical retrieval) |
|
|
144
|
+
| `node test/conflict.test.mjs` | test conflict & dedup resolution |
|
|
145
|
+
| `node test/retrieval.test.mjs` | test hybrid FTS+vector+RRF retrieval |
|
|
146
|
+
| `node test/graph.test.mjs` | test memory graph (entities, relations, traversal) |
|
|
147
|
+
| `node test/context.test.mjs` | test context assembly + token budgeting |
|
|
148
|
+
| `node test/consolidation.test.mjs` | test clustering + derived memories |
|
|
149
|
+
| `node test/benchmark.test.mjs` | latency benchmark over 300 memories |
|
|
150
|
+
| `node test/security.test.mjs` | injection / safety checks |
|
|
151
|
+
| `node test/smoke.mjs` | end-to-end smoke test over JSON-RPC (11 tools) |
|
|
152
|
+
|
|
153
|
+
## Tools (11)
|
|
154
|
+
|
|
155
|
+
| Tool | Description |
|
|
156
|
+
|------|-------------|
|
|
157
|
+
| `remember` | upsert preference (category+key) — re-saving the same key increases confidence by 0.1 (cap 1.0) |
|
|
158
|
+
| `recall` | search preferences + lessons (FTS5) + recent matching interactions. Use before starting a new task |
|
|
159
|
+
| `get_profile` | user profile overview: profile sections + top preferences + 5 most recent lessons |
|
|
160
|
+
| `save_lesson` | record a lesson learned from a correction (situation / mistake / correction) |
|
|
161
|
+
| `search_history` | search past user prompts by keyword (200-char snippets per row) |
|
|
162
|
+
| `forget` | delete one memory row (preference/lesson/interaction) by id (+type prevents cross-table id clash) |
|
|
163
|
+
| `memory_stats` | memory statistics: counts by kind, DB size, oldest/newest interaction, profile sections |
|
|
164
|
+
| `get_recent_interactions` | list recent raw interactions (filter by kind) — feedstock for Smart Distill |
|
|
165
|
+
| `export_memory` | export memory to JSON under `data/exports/` only (filename auto-sanitized) |
|
|
166
|
+
| `get_context` | assemble relevant memories for the current task via hybrid retrieval (+ optional graph expansion) with token budgeting |
|
|
167
|
+
| `consolidate` | cluster similar memories via embedding similarity; optionally create derived/consolidated memories linked via `derived_from` |
|
|
168
|
+
|
|
169
|
+
## Install with OpenCode
|
|
170
|
+
|
|
171
|
+
1. Merge the `mcp` section from [`opencode.example.json`](opencode.example.json) into your `opencode.json` (global or project-level)
|
|
172
|
+
- **Important:** set `MEMORY_DB_PATH` to the SAME database file for both the server and the plugin (the example uses `<ABSOLUTE_PATH>/th-memory-mcp/data/memory.db`), otherwise the auto-capture plugin writes to a different DB than the one the AI reads
|
|
173
|
+
- How to set it (pick one):
|
|
174
|
+
- define it in the mcp `environment` (see example) — covers the MCP server only
|
|
175
|
+
- **or** set it as a system/user-level environment variable (e.g. `setx MEMORY_DB_PATH "D:/path/to/memory.db"` on Windows) — covers both server and plugin, since the plugin runs in the same process as OpenCode
|
|
176
|
+
2. Attach the global memory rules — add to `opencode.json`:
|
|
177
|
+
```json
|
|
178
|
+
"instructions": ["C:/Users/<user>/.config/opencode/memory-protocol.md"]
|
|
179
|
+
```
|
|
180
|
+
(example rule content is in [`AGENTS.memory.example.md`](AGENTS.memory.example.md) — can be attached at project level instead)
|
|
181
|
+
3. (Optional) Deploy the auto-capture plugin: copy `src/plugin/learning-capture.ts` → `~/.config/opencode/plugins/learning-capture.ts`
|
|
182
|
+
4. **Restart OpenCode** (config loads at startup only)
|
|
183
|
+
5. Test: *"Remember that I prefer pnpm"* → open a new session and ask back
|
|
184
|
+
|
|
185
|
+
## Daily usage
|
|
186
|
+
|
|
187
|
+
The AI accepts both Thai and English interchangeably — you can switch languages at any time without warning.
|
|
188
|
+
|
|
189
|
+
| Example command | Tool / effect |
|
|
190
|
+
|-----------------|---------------|
|
|
191
|
+
| "Remember that..." | `remember` — save a preference |
|
|
192
|
+
| "Summarize memory" / "distill memory" | **Smart Distill** — AI reads `get_recent_interactions`, finds patterns, and saves insights itself |
|
|
193
|
+
| "How is my memory?" / "memory status" | `memory_stats` |
|
|
194
|
+
| "Export memory" / "backup memory" | `export_memory` |
|
|
195
|
+
| "Search history..." | `search_history` |
|
|
196
|
+
| "Forget..." | `forget` |
|
|
197
|
+
|
|
198
|
+
Long-term care: run `npm run distill` occasionally to summarize stats and prune interactions older than 30 days.
|
|
199
|
+
|
|
200
|
+
## data/ structure
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
data/
|
|
204
|
+
├── memory.db # SQLite (WAL mode) — main DB (+ .db-wal, .db-shm)
|
|
205
|
+
└── exports/ # JSON files from export_memory (writeable only in this dir)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
- DB path can be overridden via the `MEMORY_DB_PATH` env var
|
|
209
|
+
- everything in `data/` is git-ignored
|
package/README.th.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
MCP server ความจำระยะยาวสำหรับ OpenCode — เก็บ preferences, lessons, ประวัติการใช้งาน ลง SQLite ไฟล์เดียว (local 100%, ไม่มี external API) เพื่อให้ AI "จำและปรับตัว" กับผู้ใช้ผ่าน context-based learning
|
|
9
9
|
|
|
10
|
-
**สถานะ:**
|
|
10
|
+
**สถานะ:** v2.0.0 — engine ความจำแบบ temporal, conflict-aware, hybrid-retrieval 11 MCP tools, 12 ชุดเทสผ่าน อัปเกรด schema แบบ non-destructive จาก v1 (ข้อมูล v1 ทั้งหมดถูกเก็บรักษา) ฟีเจอร์ใหม่ใน v2: lifecycle states, temporal validity, การแก้ conflict/dedup, hybrid FTS+vector retrieval (RRF), memory graph, ประกอบ `get_context`, และ consolidation
|
|
11
11
|
|
|
12
12
|
> English: [README.md](README.md)
|
|
13
13
|
|
|
@@ -65,12 +65,12 @@ setx MEMORY_DB_PATH "$PWD/data/memory.db"
|
|
|
65
65
|
```
|
|
66
66
|
OpenCode ──┬─ Plugin learning-capture (Bun) ── จับ prompt/tool/error ลง DB อัตโนมัติ
|
|
67
67
|
│ └─ ฉีด profile กลับ context ตอน compaction
|
|
68
|
-
|
|
68
|
+
└─ MCP th-memory-mcp (Node.js stdio) ── tools 11 ตัว อ่าน/เขียน SQLite เดียวกัน
|
|
69
69
|
▲
|
|
70
70
|
Global instructions (memory-protocol.md) สอน AI ใช้ tools
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
รายละเอียดเต็มอยู่ใน [design.md](design.md)
|
|
73
|
+
รายละเอียดเต็มอยู่ใน [design.md](design.md) — คู่มืออัปเกรดจาก v1 ดูได้ที่ [MIGRATION_v2.md](MIGRATION_v2.md)
|
|
74
74
|
|
|
75
75
|
## ทำไมต้องใช้ th-memory-mcp?
|
|
76
76
|
|
|
@@ -80,7 +80,11 @@ LLM ไม่ได้จำคุณข้าม session — แชทใหม
|
|
|
80
80
|
- **local 100% และเป็นส่วนตัว** — ไฟล์ SQLite เดียว ไม่มีคลาวด์ ไม่มี external API มีการกรอง secret ก่อนบันทึกเสมอ
|
|
81
81
|
- **โอเวอร์เฮดต่ำ** — ทุก tool call มีเพดาน (latency < 10 ms, ขนาด output จำกัด) และ AI ค้นความจำเฉพาะตอนจำเป็น จึงไม่บวม context
|
|
82
82
|
- **ทนทาน** — ทุก tool ทำ graceful degradation ถ้า DB ไม่ได้เปิด AI ก็ทำงานต่อได้แทนที่จะพัง
|
|
83
|
-
-
|
|
83
|
+
- **Lifecycle & temporal** — ทุกความจำมี lifecycle state (active/stale/superseded/archived), คะแนน confidence/importance/salience, decay ต่อ type, และ validity intervals ให้ AI เหตุผลเรื่อง point-in-time truth และ supersession chains ได้
|
|
84
|
+
- **Conflict-aware** — ตรวจจับ duplicate / contradiction และแก้ด้วย update/supersession โดยเก็บทั้งสองฝ่ายของหลักฐานที่ขัดแย้งแทนการเขียนทับเงียบๆ
|
|
85
|
+
- **Hybrid retrieval** — `get_context` ผสาน FTS5 + local vector embedding (RRF fusion + scoring) แล้วประกอบ context แบบมี token budget พร้อมขยายผ่าน memory graph
|
|
86
|
+
- **Consolidation** — จัดคลัสเตอร์ความจำที่คล้ายกันเป็น derived memory พร้อม provenance (`derived_from` links)
|
|
87
|
+
- **เปิดกว้างและต่อยอดได้** — MIT license, 11 tools ที่อธิบายครบ, มี rule-based distill และ plugin auto-capture ที่คุณปรับแต่งได้
|
|
84
88
|
|
|
85
89
|
## Scripts
|
|
86
90
|
|
|
@@ -89,11 +93,21 @@ LLM ไม่ได้จำคุณข้าม session — แชทใหม
|
|
|
89
93
|
| `npm run build` | compile TypeScript → `dist/` |
|
|
90
94
|
| `npm start` | รัน MCP server (stdio) จาก `dist/index.js` |
|
|
91
95
|
| `npm run distill` | rule-based distill: interactions → profile sections + prune ข้อมูลเก่า (env `RETENTION_DAYS` default 30) |
|
|
92
|
-
| `npm test` |
|
|
96
|
+
| `npm test` | ชุดเทสครบ: capture, distill, lifecycle, temporal, conflict, retrieval, graph, context, consolidation, benchmark, security, smoke |
|
|
93
97
|
| `node test/capture.test.mjs` | ทดสอบ capture-core (filter secrets, dedupe, truncate, insert SQL) |
|
|
94
98
|
| `node test/distill.test.mjs` | ทดสอบ distill-core (tokenize ไทย, stats, profile sections, prune) |
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
| `node test/lifecycle.test.mjs` | ทดสอบ lifecycle engine (states, decay, supersession) |
|
|
100
|
+
| `node test/temporal.test.mjs` | ทดสอบ temporal model (validity, historical retrieval) |
|
|
101
|
+
| `node test/conflict.test.mjs` | ทดสอบ conflict & dedup resolution |
|
|
102
|
+
| `node test/retrieval.test.mjs` | ทดสอบ hybrid FTS+vector+RRF retrieval |
|
|
103
|
+
| `node test/graph.test.mjs` | ทดสอบ memory graph (entities, relations, traversal) |
|
|
104
|
+
| `node test/context.test.mjs` | ทดสอบ context assembly + token budgeting |
|
|
105
|
+
| `node test/consolidation.test.mjs` | ทดสอบ clustering + derived memories |
|
|
106
|
+
| `node test/benchmark.test.mjs` | เทสความเร็วบนความจำ 300 รายการ |
|
|
107
|
+
| `node test/security.test.mjs` | ตรวจการ injection / ความปลอดภัย |
|
|
108
|
+
| `node test/smoke.mjs` | smoke test end-to-end ผ่าน JSON-RPC (11 tools) |
|
|
109
|
+
|
|
110
|
+
## Tools (11)
|
|
97
111
|
|
|
98
112
|
| Tool | คำอธิบาย |
|
|
99
113
|
|------|----------|
|
|
@@ -106,6 +120,8 @@ LLM ไม่ได้จำคุณข้าม session — แชทใหม
|
|
|
106
120
|
| `memory_stats` | สถิติความจำ: counts แยก kind, ขนาด DB, oldest/newest interaction, profile sections |
|
|
107
121
|
| `get_recent_interactions` | ดึง interactions ล่าสุดแบบดิบ (กรองตาม kind ได้) — วัตถุดิบของ Smart Distill |
|
|
108
122
|
| `export_memory` | export ความจำเป็น JSON ลง `data/exports/` เท่านั้น (sanitize filename ให้เอง) |
|
|
123
|
+
| `get_context` | ประกอบความจำที่เกี่ยวข้องกับงานปัจจุบันผ่าน hybrid retrieval (+ ขยายผ่าน memory graph ได้) พร้อม token budgeting |
|
|
124
|
+
| `consolidate` | จัดคลัสเตอร์ความจำที่คล้ายกันด้วย embedding cosine และสร้าง derived/consolidated memory ที่ผูกด้วย `derived_from` ได้ |
|
|
109
125
|
|
|
110
126
|
## ติดตั้งกับ OpenCode
|
|
111
127
|
|