@totalreclaw/mcp-server 1.0.0 → 1.2.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 +92 -63
- package/dist/cli/setup.d.ts.map +1 -1
- package/dist/cli/setup.js +18 -2
- package/dist/cli/setup.js.map +1 -1
- package/dist/consolidation.d.ts +97 -0
- package/dist/consolidation.d.ts.map +1 -0
- package/dist/consolidation.js +183 -0
- package/dist/consolidation.js.map +1 -0
- package/dist/index.js +261 -51
- package/dist/index.js.map +1 -1
- package/dist/prompts.d.ts +5 -4
- package/dist/prompts.d.ts.map +1 -1
- package/dist/prompts.js +20 -5
- package/dist/prompts.js.map +1 -1
- package/dist/resources/memory-context.d.ts +1 -1
- package/dist/resources/memory-context.d.ts.map +1 -1
- package/dist/resources/memory-context.js +6 -11
- package/dist/resources/memory-context.js.map +1 -1
- package/dist/subgraph/embedding.d.ts +17 -18
- package/dist/subgraph/embedding.d.ts.map +1 -1
- package/dist/subgraph/embedding.js +25 -31
- package/dist/subgraph/embedding.js.map +1 -1
- package/dist/subgraph/lsh.d.ts +1 -1
- package/dist/subgraph/lsh.js +1 -1
- package/dist/subgraph/search.d.ts +3 -3
- package/dist/subgraph/search.d.ts.map +1 -1
- package/dist/subgraph/search.js +26 -11
- package/dist/subgraph/search.js.map +1 -1
- package/dist/subgraph/store.d.ts +11 -7
- package/dist/subgraph/store.d.ts.map +1 -1
- package/dist/subgraph/store.js +32 -17
- package/dist/subgraph/store.js.map +1 -1
- package/dist/tools/consolidate.d.ts +39 -0
- package/dist/tools/consolidate.d.ts.map +1 -0
- package/dist/tools/consolidate.js +167 -0
- package/dist/tools/consolidate.js.map +1 -0
- package/dist/tools/export.d.ts +1 -6
- package/dist/tools/export.d.ts.map +1 -1
- package/dist/tools/export.js +5 -15
- package/dist/tools/export.js.map +1 -1
- package/dist/tools/forget.d.ts +1 -6
- package/dist/tools/forget.d.ts.map +1 -1
- package/dist/tools/forget.js +2 -13
- package/dist/tools/forget.js.map +1 -1
- package/dist/tools/import-from.d.ts +94 -0
- package/dist/tools/import-from.d.ts.map +1 -0
- package/dist/tools/import-from.js +213 -0
- package/dist/tools/import-from.js.map +1 -0
- package/dist/tools/import.d.ts +1 -11
- package/dist/tools/import.d.ts.map +1 -1
- package/dist/tools/import.js +2 -17
- package/dist/tools/import.js.map +1 -1
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +7 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/recall.d.ts +1 -6
- package/dist/tools/recall.d.ts.map +1 -1
- package/dist/tools/recall.js +3 -11
- package/dist/tools/recall.js.map +1 -1
- package/dist/tools/remember.d.ts +4 -7
- package/dist/tools/remember.d.ts.map +1 -1
- package/dist/tools/remember.js +136 -17
- package/dist/tools/remember.js.map +1 -1
- package/dist/tools/status.d.ts.map +1 -1
- package/dist/tools/status.js +1 -0
- package/dist/tools/status.js.map +1 -1
- package/dist/tools/upgrade.d.ts.map +1 -1
- package/dist/tools/upgrade.js +1 -0
- package/dist/tools/upgrade.js.map +1 -1
- package/package.json +12 -5
package/README.md
CHANGED
|
@@ -1,18 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="../docs/assets/logo.png" alt="TotalReclaw" width="80" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">@totalreclaw/mcp-server</h1>
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
<p align="center">
|
|
8
|
+
<strong>Encrypted memory for Claude Desktop, Cursor, Windsurf, and any MCP-compatible agent</strong>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://totalreclaw.xyz">Website</a> ·
|
|
13
|
+
<a href="https://www.npmjs.com/package/@totalreclaw/mcp-server">npm</a> ·
|
|
14
|
+
<a href="../docs/guides/beta-tester-guide.md">Getting Started</a>
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<a href="https://www.npmjs.com/package/@totalreclaw/mcp-server"><img src="https://img.shields.io/npm/v/@totalreclaw/mcp-server?color=7B5CFF" alt="npm version"></a>
|
|
19
|
+
<a href="https://www.npmjs.com/package/@totalreclaw/mcp-server"><img src="https://img.shields.io/npm/dm/@totalreclaw/mcp-server" alt="npm downloads"></a>
|
|
20
|
+
<a href="../LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="License"></a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
End-to-end encrypted memory vault as an MCP server. Your memories are encrypted on your device before leaving -- no one can read them, not even us.
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
### 1. Run the setup wizard
|
|
6
30
|
|
|
7
31
|
```bash
|
|
8
|
-
|
|
32
|
+
npx @totalreclaw/mcp-server setup
|
|
9
33
|
```
|
|
10
34
|
|
|
11
|
-
|
|
35
|
+
The wizard generates your 12-word recovery phrase, registers you, and prints a config snippet for your MCP client.
|
|
12
36
|
|
|
13
|
-
|
|
37
|
+
> **Save your recovery phrase somewhere safe.** It's the only way to recover your encrypted memories.
|
|
14
38
|
|
|
15
|
-
|
|
39
|
+
> **Note:** The first run downloads a ~600MB embedding model for local inference. This is cached locally and only happens once.
|
|
40
|
+
|
|
41
|
+
### 2. Add to your MCP client
|
|
42
|
+
|
|
43
|
+
#### Claude Desktop
|
|
44
|
+
|
|
45
|
+
Add to `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS) or `%APPDATA%\Claude\claude_desktop_config.json` (Windows):
|
|
16
46
|
|
|
17
47
|
```json
|
|
18
48
|
{
|
|
@@ -21,93 +51,92 @@ Add to your `claude_desktop_config.json`:
|
|
|
21
51
|
"command": "npx",
|
|
22
52
|
"args": ["-y", "@totalreclaw/mcp-server"],
|
|
23
53
|
"env": {
|
|
24
|
-
"TOTALRECLAW_SERVER_URL": "
|
|
25
|
-
"TOTALRECLAW_MASTER_PASSWORD": "your
|
|
54
|
+
"TOTALRECLAW_SERVER_URL": "https://api.totalreclaw.xyz",
|
|
55
|
+
"TOTALRECLAW_MASTER_PASSWORD": "your twelve word recovery phrase goes here"
|
|
26
56
|
}
|
|
27
57
|
}
|
|
28
58
|
}
|
|
29
59
|
}
|
|
30
60
|
```
|
|
31
61
|
|
|
32
|
-
|
|
62
|
+
#### Cursor / Windsurf
|
|
33
63
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
### Environment Variables
|
|
37
|
-
|
|
38
|
-
| Variable | Description | Default |
|
|
39
|
-
|----------|-------------|---------|
|
|
40
|
-
| `TOTALRECLAW_SERVER_URL` | TotalReclaw server URL | `http://127.0.0.1:8080` |
|
|
41
|
-
| `TOTALRECLAW_MASTER_PASSWORD` | Master password for encryption | Required |
|
|
42
|
-
| `TOTALRECLAW_NAMESPACE` | Default namespace | `default` |
|
|
43
|
-
| `TOTALRECLAW_CREDENTIALS_PATH` | Path to store credentials | `/workspace/.totalreclaw/credentials.json` |
|
|
44
|
-
|
|
45
|
-
## Available Tools
|
|
46
|
-
|
|
47
|
-
### totalreclaw_remember
|
|
48
|
-
|
|
49
|
-
Store a fact in encrypted memory.
|
|
64
|
+
Add to your MCP settings (Settings > MCP Servers):
|
|
50
65
|
|
|
51
66
|
```json
|
|
52
67
|
{
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
"totalreclaw": {
|
|
69
|
+
"command": "npx",
|
|
70
|
+
"args": ["-y", "@totalreclaw/mcp-server"],
|
|
71
|
+
"env": {
|
|
72
|
+
"TOTALRECLAW_SERVER_URL": "https://api.totalreclaw.xyz",
|
|
73
|
+
"TOTALRECLAW_MASTER_PASSWORD": "your twelve word recovery phrase goes here"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
56
76
|
}
|
|
57
77
|
```
|
|
58
78
|
|
|
59
|
-
###
|
|
79
|
+
### 3. Verify
|
|
60
80
|
|
|
61
|
-
|
|
81
|
+
Ask your agent: *"Do you have access to TotalReclaw memory tools?"*
|
|
62
82
|
|
|
63
|
-
|
|
64
|
-
{
|
|
65
|
-
"query": "user preferences",
|
|
66
|
-
"k": 8,
|
|
67
|
-
"min_importance": 5
|
|
68
|
-
}
|
|
69
|
-
```
|
|
83
|
+
## How It Works
|
|
70
84
|
|
|
71
|
-
|
|
85
|
+
All encryption happens **client-side** inside the MCP server process on your machine:
|
|
72
86
|
|
|
73
|
-
|
|
87
|
+
1. Facts are encrypted with AES-256-GCM before leaving your device
|
|
88
|
+
2. Search uses blind indices (SHA-256 hashes) -- the server never sees your queries
|
|
89
|
+
3. Your recovery phrase derives all keys via Argon2id + HKDF
|
|
90
|
+
4. Encrypted facts are stored on-chain (Gnosis Chain) and indexed by The Graph
|
|
74
91
|
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
"fact_id": "uuid-of-fact"
|
|
78
|
-
}
|
|
79
|
-
```
|
|
92
|
+
The server only ever sees ciphertext and hashed tokens.
|
|
80
93
|
|
|
81
|
-
|
|
94
|
+
## Available Tools
|
|
82
95
|
|
|
83
|
-
|
|
96
|
+
| Tool | Description |
|
|
97
|
+
|------|-------------|
|
|
98
|
+
| `totalreclaw_remember` | Store a fact in encrypted memory |
|
|
99
|
+
| `totalreclaw_recall` | Search memories by natural language query |
|
|
100
|
+
| `totalreclaw_forget` | Delete a specific memory by ID |
|
|
101
|
+
| `totalreclaw_export` | Export all memories decrypted as Markdown or JSON |
|
|
102
|
+
| `totalreclaw_status` | Check billing status and quota usage |
|
|
103
|
+
| `totalreclaw_import` | Re-import previously exported memories |
|
|
104
|
+
| `totalreclaw_import_from` | Import from Mem0 or MCP Memory Server |
|
|
105
|
+
| `totalreclaw_consolidate` | Merge duplicate and related memories |
|
|
106
|
+
| `totalreclaw_upgrade` | Get a link to upgrade to Pro |
|
|
84
107
|
|
|
85
|
-
|
|
86
|
-
{
|
|
87
|
-
"format": "markdown"
|
|
88
|
-
}
|
|
89
|
-
```
|
|
108
|
+
## Environment Variables
|
|
90
109
|
|
|
91
|
-
|
|
110
|
+
| Variable | Description | Default |
|
|
111
|
+
|----------|-------------|---------|
|
|
112
|
+
| `TOTALRECLAW_SERVER_URL` | TotalReclaw server URL | `https://api.totalreclaw.xyz` |
|
|
113
|
+
| `TOTALRECLAW_MASTER_PASSWORD` | 12-word BIP-39 recovery phrase | Required |
|
|
114
|
+
| `TOTALRECLAW_SELF_HOSTED` | Use a self-hosted server instead of the managed service | `false` |
|
|
115
|
+
| `TOTALRECLAW_CHAIN_ID` | Chain ID (100=Gnosis mainnet, 84532=Base Sepolia staging) | `100` |
|
|
92
116
|
|
|
93
|
-
|
|
117
|
+
## Free Tier & Pricing
|
|
94
118
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
```
|
|
119
|
+
| Tier | Memories | Reads | Storage | Price |
|
|
120
|
+
|------|----------|-------|---------|-------|
|
|
121
|
+
| **Free** | 500/month | Unlimited | Testnet (trial) | $0 |
|
|
122
|
+
| **Pro** | Unlimited | Unlimited | Permanent on-chain (Gnosis) | $5/month |
|
|
123
|
+
|
|
124
|
+
Pay with card via Stripe. Counter resets monthly.
|
|
102
125
|
|
|
103
126
|
## Development
|
|
104
127
|
|
|
105
128
|
```bash
|
|
106
|
-
npm run build # Build
|
|
129
|
+
npm run build # Build
|
|
107
130
|
npm test # Run tests
|
|
108
|
-
npm run lint # Lint
|
|
131
|
+
npm run lint # Lint
|
|
109
132
|
```
|
|
110
133
|
|
|
134
|
+
## Learn More
|
|
135
|
+
|
|
136
|
+
- [Getting Started Guide](../docs/guides/beta-tester-guide.md)
|
|
137
|
+
- [totalreclaw.xyz](https://totalreclaw.xyz)
|
|
138
|
+
- [Main Repository](https://github.com/p-diogo/totalreclaw)
|
|
139
|
+
|
|
111
140
|
## License
|
|
112
141
|
|
|
113
142
|
MIT
|
package/dist/cli/setup.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/cli/setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AA0BH;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAWzF;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAG9D;AAgED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,eAAe,CAAC,WAAW,EAAE,gBAAgB,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAYtF;AAED,wBAAgB,eAAe,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,gBAAgB,CAInE;AAiCD,wBAAsB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/cli/setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AA0BH;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAWzF;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAG9D;AAgED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,eAAe,CAAC,WAAW,EAAE,gBAAgB,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAYtF;AAED,wBAAgB,eAAe,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,gBAAgB,CAInE;AAiCD,wBAAsB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAwH9C"}
|
package/dist/cli/setup.js
CHANGED
|
@@ -181,7 +181,7 @@ async function runSetup() {
|
|
|
181
181
|
const rl = createInterface();
|
|
182
182
|
console.log('');
|
|
183
183
|
console.log('=== TotalReclaw Setup ===');
|
|
184
|
-
console.log('
|
|
184
|
+
console.log('End-to-end encrypted memory for AI agents.');
|
|
185
185
|
console.log('');
|
|
186
186
|
try {
|
|
187
187
|
// ── Step 1: Mnemonic ──────────────────────────────────────────────────
|
|
@@ -249,7 +249,23 @@ async function runSetup() {
|
|
|
249
249
|
saveCredentials(credentials);
|
|
250
250
|
console.log(`\nCredentials saved to ${CREDENTIALS_PATH}`);
|
|
251
251
|
console.log('(Mnemonic and keys are NOT stored -- only userId, salt, and serverUrl.)');
|
|
252
|
-
// ── Step 6:
|
|
252
|
+
// ── Step 6: Pre-download Embedding Model ────────────────────────────
|
|
253
|
+
console.log('\nDownloading embedding model (one-time, ~600MB)...');
|
|
254
|
+
console.log('This enables local, private semantic search — no API calls needed.\n');
|
|
255
|
+
try {
|
|
256
|
+
const { pipeline } = await import('@huggingface/transformers');
|
|
257
|
+
await pipeline('feature-extraction', 'onnx-community/Qwen3-Embedding-0.6B-ONNX', {
|
|
258
|
+
// @ts-ignore - quantized option exists at runtime but not in type defs
|
|
259
|
+
quantized: true,
|
|
260
|
+
});
|
|
261
|
+
console.log('Embedding model downloaded and cached.');
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
265
|
+
console.warn(`\nWarning: Could not pre-download embedding model: ${message}`);
|
|
266
|
+
console.warn('The model will be downloaded automatically on first use.');
|
|
267
|
+
}
|
|
268
|
+
// ── Step 7: Print Config Snippet ──────────────────────────────────────
|
|
253
269
|
printConfigSnippet(serverUrl);
|
|
254
270
|
console.log('Setup complete! You can now use TotalReclaw with any MCP-compatible AI agent.');
|
|
255
271
|
console.log('');
|
package/dist/cli/setup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.js","sourceRoot":"","sources":["../../src/cli/setup.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCH,sCAWC;AAMD,gDAGC;AAsED,0CAYC;AAED,0CAIC;AAiCD,
|
|
1
|
+
{"version":3,"file":"setup.js","sourceRoot":"","sources":["../../src/cli/setup.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCH,sCAWC;AAMD,gDAGC;AAsED,0CAYC;AAED,0CAIC;AAiCD,4BAwHC;AAtSD,wDAA0C;AAC1C,4CAA8B;AAC9B,gDAAkC;AAClC,4CAA8B;AAE9B,wCAAsF;AACtF,kEAA6D;AAC7D,mDAA6C;AAC7C,mDAA+C;AAE/C,8EAA8E;AAC9E,2CAA2C;AAC3C,8EAA8E;AAE9E,MAAM,aAAa,GAAG,yBAAyB,CAAC;AAEhD,MAAM,kBAAkB,GAAG,6BAA6B,CAAC;AACzD,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,cAAc,CAAC,CAAC;AAChE,MAAM,gBAAgB,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAC;AAExE,8EAA8E;AAC9E,gEAAgE;AAChE,8EAA8E;AAE9E;;;;;;;;GAQG;AACH,SAAgB,aAAa,CAAC,QAAgB;IAC5C,MAAM,IAAI,GAAG,IAAA,0BAAkB,EAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAEjD,oDAAoD;IACpD,MAAM,IAAI,GAAG,IAAI,C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AAC,SAAS,CAAC,CAAC;QAE9B,OAAO,CAAC,GAAG,CAAC,+EAA+E,CAAC,CAAC;QAC7F,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAClB,CAAC;YAAS,CAAC;QACT,EAAE,CAAC,KAAK,EAAE,CAAC;IACb,CAAC;AACH,CAAC;AAED,8EAA8E;AAC9E,kBAAkB;AAClB,8EAA8E;AAE9E,6DAA6D;AAC7D,MAAM,WAAW,GACf,OAAO,OAAO,KAAK,WAAW;IAC9B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IACf,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC;QACrC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC;QACrC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC;AAE1C,IAAI,WAAW,EAAE,CAAC;IAChB,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QACzB,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;QACtC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TotalReclaw MCP Server - Memory Consolidation & Near-Duplicate Detection
|
|
3
|
+
*
|
|
4
|
+
* Provides cross-session / cross-vault deduplication of stored facts using
|
|
5
|
+
* cosine similarity on their embeddings. This module handles:
|
|
6
|
+
*
|
|
7
|
+
* 1. Store-time dedup — before writing a new fact, check whether a
|
|
8
|
+
* near-duplicate already exists in the vault (findNearDuplicate).
|
|
9
|
+
* 2. Supersede logic — when a near-duplicate is found, decide whether
|
|
10
|
+
* the new fact should replace or be skipped (shouldSupersede).
|
|
11
|
+
* 3. Bulk consolidation — cluster all facts in the vault and identify
|
|
12
|
+
* groups of near-duplicates for cleanup (clusterFacts).
|
|
13
|
+
*
|
|
14
|
+
* This module intentionally has minimal dependencies (only reranker for
|
|
15
|
+
* cosineSimilarity) so it can be tested without pulling in the full
|
|
16
|
+
* server dependency graph.
|
|
17
|
+
*
|
|
18
|
+
* Ported from skill/plugin/consolidation.ts — adapted for MCP server.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Get the cosine similarity threshold for store-time dedup.
|
|
22
|
+
*
|
|
23
|
+
* Configurable via TOTALRECLAW_STORE_DEDUP_THRESHOLD env var.
|
|
24
|
+
* Must be a number in [0, 1]. Falls back to 0.85 if invalid or unset.
|
|
25
|
+
*/
|
|
26
|
+
export declare function getStoreDedupThreshold(): number;
|
|
27
|
+
/**
|
|
28
|
+
* Get the cosine similarity threshold for bulk consolidation clustering.
|
|
29
|
+
*
|
|
30
|
+
* Configurable via TOTALRECLAW_CONSOLIDATION_THRESHOLD env var.
|
|
31
|
+
* Must be a number in [0, 1]. Falls back to 0.88 if invalid or unset.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getConsolidationThreshold(): number;
|
|
34
|
+
/** Maximum candidates to compare against during store-time dedup. */
|
|
35
|
+
export declare const STORE_DEDUP_MAX_CANDIDATES = 200;
|
|
36
|
+
/** A decrypted fact candidate from the vault, with metadata for ranking. */
|
|
37
|
+
export interface DecryptedCandidate {
|
|
38
|
+
id: string;
|
|
39
|
+
text: string;
|
|
40
|
+
embedding: number[] | null;
|
|
41
|
+
importance: number;
|
|
42
|
+
decayScore: number;
|
|
43
|
+
createdAt: number;
|
|
44
|
+
version: number;
|
|
45
|
+
}
|
|
46
|
+
/** A match result from near-duplicate detection. */
|
|
47
|
+
export interface NearDuplicateMatch {
|
|
48
|
+
existingFact: DecryptedCandidate;
|
|
49
|
+
similarity: number;
|
|
50
|
+
}
|
|
51
|
+
/** A cluster of near-duplicate facts for consolidation. */
|
|
52
|
+
export interface ConsolidationCluster {
|
|
53
|
+
representative: DecryptedCandidate;
|
|
54
|
+
duplicates: DecryptedCandidate[];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Find the best near-duplicate match for a new fact among existing candidates.
|
|
58
|
+
*
|
|
59
|
+
* Compares the new fact's embedding against all candidates using cosine
|
|
60
|
+
* similarity. Returns the candidate with the highest similarity above the
|
|
61
|
+
* threshold, or null if no match is found.
|
|
62
|
+
*
|
|
63
|
+
* Candidates without embeddings are skipped (fail-safe).
|
|
64
|
+
*
|
|
65
|
+
* @param newFactEmbedding - Embedding vector for the new fact
|
|
66
|
+
* @param candidates - Existing facts to compare against
|
|
67
|
+
* @param threshold - Cosine similarity threshold (e.g. 0.85)
|
|
68
|
+
* @returns - Best match above threshold, or null
|
|
69
|
+
*/
|
|
70
|
+
export declare function findNearDuplicate(newFactEmbedding: number[], candidates: DecryptedCandidate[], threshold: number): NearDuplicateMatch | null;
|
|
71
|
+
/**
|
|
72
|
+
* Decide whether a new fact should supersede an existing near-duplicate.
|
|
73
|
+
*
|
|
74
|
+
* - Higher importance wins.
|
|
75
|
+
* - Equal importance: new fact supersedes (newer is preferred).
|
|
76
|
+
*
|
|
77
|
+
* @param newImportance - Importance score of the new fact
|
|
78
|
+
* @param existingFact - The existing near-duplicate candidate
|
|
79
|
+
* @returns - 'supersede' if new fact should replace, 'skip' otherwise
|
|
80
|
+
*/
|
|
81
|
+
export declare function shouldSupersede(newImportance: number, existingFact: DecryptedCandidate): 'supersede' | 'skip';
|
|
82
|
+
/**
|
|
83
|
+
* Cluster facts by semantic similarity using greedy single-pass clustering.
|
|
84
|
+
*
|
|
85
|
+
* For each fact (in order), assigns it to the first existing cluster whose
|
|
86
|
+
* representative has cosine similarity >= threshold. If no cluster matches,
|
|
87
|
+
* a new cluster is started.
|
|
88
|
+
*
|
|
89
|
+
* Only returns clusters that have duplicates (i.e. more than one member).
|
|
90
|
+
* Facts without embeddings are not clustered.
|
|
91
|
+
*
|
|
92
|
+
* @param facts - All facts to cluster
|
|
93
|
+
* @param threshold - Cosine similarity threshold (e.g. 0.88)
|
|
94
|
+
* @returns - Clusters with duplicates (representative + duplicates)
|
|
95
|
+
*/
|
|
96
|
+
export declare function clusterFacts(facts: DecryptedCandidate[], threshold: number): ConsolidationCluster[];
|
|
97
|
+
//# sourceMappingURL=consolidation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consolidation.d.ts","sourceRoot":"","sources":["../src/consolidation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAQH;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,MAAM,CAO/C;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,IAAI,MAAM,CAOlD;AAED,qEAAqE;AACrE,eAAO,MAAM,0BAA0B,MAAM,CAAC;AAM9C,4EAA4E;AAC5E,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,oDAAoD;AACpD,MAAM,WAAW,kBAAkB;IACjC,YAAY,EAAE,kBAAkB,CAAC;IACjC,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,2DAA2D;AAC3D,MAAM,WAAW,oBAAoB;IACnC,cAAc,EAAE,kBAAkB,CAAC;IACnC,UAAU,EAAE,kBAAkB,EAAE,CAAC;CAClC;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAC/B,gBAAgB,EAAE,MAAM,EAAE,EAC1B,UAAU,EAAE,kBAAkB,EAAE,EAChC,SAAS,EAAE,MAAM,GAChB,kBAAkB,GAAG,IAAI,CAe3B;AAMD;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC7B,aAAa,EAAE,MAAM,EACrB,YAAY,EAAE,kBAAkB,GAC/B,WAAW,GAAG,MAAM,CAGtB;AA6BD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,kBAAkB,EAAE,EAC3B,SAAS,EAAE,MAAM,GAChB,oBAAoB,EAAE,CAoCxB"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TotalReclaw MCP Server - Memory Consolidation & Near-Duplicate Detection
|
|
4
|
+
*
|
|
5
|
+
* Provides cross-session / cross-vault deduplication of stored facts using
|
|
6
|
+
* cosine similarity on their embeddings. This module handles:
|
|
7
|
+
*
|
|
8
|
+
* 1. Store-time dedup — before writing a new fact, check whether a
|
|
9
|
+
* near-duplicate already exists in the vault (findNearDuplicate).
|
|
10
|
+
* 2. Supersede logic — when a near-duplicate is found, decide whether
|
|
11
|
+
* the new fact should replace or be skipped (shouldSupersede).
|
|
12
|
+
* 3. Bulk consolidation — cluster all facts in the vault and identify
|
|
13
|
+
* groups of near-duplicates for cleanup (clusterFacts).
|
|
14
|
+
*
|
|
15
|
+
* This module intentionally has minimal dependencies (only reranker for
|
|
16
|
+
* cosineSimilarity) so it can be tested without pulling in the full
|
|
17
|
+
* server dependency graph.
|
|
18
|
+
*
|
|
19
|
+
* Ported from skill/plugin/consolidation.ts — adapted for MCP server.
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.STORE_DEDUP_MAX_CANDIDATES = void 0;
|
|
23
|
+
exports.getStoreDedupThreshold = getStoreDedupThreshold;
|
|
24
|
+
exports.getConsolidationThreshold = getConsolidationThreshold;
|
|
25
|
+
exports.findNearDuplicate = findNearDuplicate;
|
|
26
|
+
exports.shouldSupersede = shouldSupersede;
|
|
27
|
+
exports.clusterFacts = clusterFacts;
|
|
28
|
+
const reranker_js_1 = require("./subgraph/reranker.js");
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Configuration
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
/**
|
|
33
|
+
* Get the cosine similarity threshold for store-time dedup.
|
|
34
|
+
*
|
|
35
|
+
* Configurable via TOTALRECLAW_STORE_DEDUP_THRESHOLD env var.
|
|
36
|
+
* Must be a number in [0, 1]. Falls back to 0.85 if invalid or unset.
|
|
37
|
+
*/
|
|
38
|
+
function getStoreDedupThreshold() {
|
|
39
|
+
const envVal = process.env.TOTALRECLAW_STORE_DEDUP_THRESHOLD;
|
|
40
|
+
if (envVal !== undefined) {
|
|
41
|
+
const parsed = parseFloat(envVal);
|
|
42
|
+
if (!isNaN(parsed) && parsed >= 0 && parsed <= 1)
|
|
43
|
+
return parsed;
|
|
44
|
+
}
|
|
45
|
+
return 0.85;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the cosine similarity threshold for bulk consolidation clustering.
|
|
49
|
+
*
|
|
50
|
+
* Configurable via TOTALRECLAW_CONSOLIDATION_THRESHOLD env var.
|
|
51
|
+
* Must be a number in [0, 1]. Falls back to 0.88 if invalid or unset.
|
|
52
|
+
*/
|
|
53
|
+
function getConsolidationThreshold() {
|
|
54
|
+
const envVal = process.env.TOTALRECLAW_CONSOLIDATION_THRESHOLD;
|
|
55
|
+
if (envVal !== undefined) {
|
|
56
|
+
const parsed = parseFloat(envVal);
|
|
57
|
+
if (!isNaN(parsed) && parsed >= 0 && parsed <= 1)
|
|
58
|
+
return parsed;
|
|
59
|
+
}
|
|
60
|
+
return 0.88;
|
|
61
|
+
}
|
|
62
|
+
/** Maximum candidates to compare against during store-time dedup. */
|
|
63
|
+
exports.STORE_DEDUP_MAX_CANDIDATES = 200;
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Store-time dedup
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
/**
|
|
68
|
+
* Find the best near-duplicate match for a new fact among existing candidates.
|
|
69
|
+
*
|
|
70
|
+
* Compares the new fact's embedding against all candidates using cosine
|
|
71
|
+
* similarity. Returns the candidate with the highest similarity above the
|
|
72
|
+
* threshold, or null if no match is found.
|
|
73
|
+
*
|
|
74
|
+
* Candidates without embeddings are skipped (fail-safe).
|
|
75
|
+
*
|
|
76
|
+
* @param newFactEmbedding - Embedding vector for the new fact
|
|
77
|
+
* @param candidates - Existing facts to compare against
|
|
78
|
+
* @param threshold - Cosine similarity threshold (e.g. 0.85)
|
|
79
|
+
* @returns - Best match above threshold, or null
|
|
80
|
+
*/
|
|
81
|
+
function findNearDuplicate(newFactEmbedding, candidates, threshold) {
|
|
82
|
+
let bestMatch = null;
|
|
83
|
+
for (const candidate of candidates) {
|
|
84
|
+
if (!candidate.embedding || candidate.embedding.length === 0)
|
|
85
|
+
continue;
|
|
86
|
+
const similarity = (0, reranker_js_1.cosineSimilarity)(newFactEmbedding, candidate.embedding);
|
|
87
|
+
if (similarity >= threshold) {
|
|
88
|
+
if (!bestMatch || similarity > bestMatch.similarity) {
|
|
89
|
+
bestMatch = { existingFact: candidate, similarity };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return bestMatch;
|
|
94
|
+
}
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Supersede logic
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
/**
|
|
99
|
+
* Decide whether a new fact should supersede an existing near-duplicate.
|
|
100
|
+
*
|
|
101
|
+
* - Higher importance wins.
|
|
102
|
+
* - Equal importance: new fact supersedes (newer is preferred).
|
|
103
|
+
*
|
|
104
|
+
* @param newImportance - Importance score of the new fact
|
|
105
|
+
* @param existingFact - The existing near-duplicate candidate
|
|
106
|
+
* @returns - 'supersede' if new fact should replace, 'skip' otherwise
|
|
107
|
+
*/
|
|
108
|
+
function shouldSupersede(newImportance, existingFact) {
|
|
109
|
+
if (newImportance >= existingFact.importance)
|
|
110
|
+
return 'supersede';
|
|
111
|
+
return 'skip';
|
|
112
|
+
}
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
// Bulk consolidation
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
/**
|
|
117
|
+
* Pick the best representative from a group of near-duplicate facts.
|
|
118
|
+
*
|
|
119
|
+
* Tiebreak order:
|
|
120
|
+
* 1. Highest decayScore
|
|
121
|
+
* 2. Most recent (highest createdAt)
|
|
122
|
+
* 3. Longest text
|
|
123
|
+
*/
|
|
124
|
+
function pickRepresentative(facts) {
|
|
125
|
+
let best = facts[0];
|
|
126
|
+
for (let i = 1; i < facts.length; i++) {
|
|
127
|
+
const f = facts[i];
|
|
128
|
+
if (f.decayScore > best.decayScore ||
|
|
129
|
+
(f.decayScore === best.decayScore && f.createdAt > best.createdAt) ||
|
|
130
|
+
(f.decayScore === best.decayScore && f.createdAt === best.createdAt && f.text.length > best.text.length)) {
|
|
131
|
+
best = f;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return best;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Cluster facts by semantic similarity using greedy single-pass clustering.
|
|
138
|
+
*
|
|
139
|
+
* For each fact (in order), assigns it to the first existing cluster whose
|
|
140
|
+
* representative has cosine similarity >= threshold. If no cluster matches,
|
|
141
|
+
* a new cluster is started.
|
|
142
|
+
*
|
|
143
|
+
* Only returns clusters that have duplicates (i.e. more than one member).
|
|
144
|
+
* Facts without embeddings are not clustered.
|
|
145
|
+
*
|
|
146
|
+
* @param facts - All facts to cluster
|
|
147
|
+
* @param threshold - Cosine similarity threshold (e.g. 0.88)
|
|
148
|
+
* @returns - Clusters with duplicates (representative + duplicates)
|
|
149
|
+
*/
|
|
150
|
+
function clusterFacts(facts, threshold) {
|
|
151
|
+
const clusters = [];
|
|
152
|
+
for (const fact of facts) {
|
|
153
|
+
if (!fact.embedding || fact.embedding.length === 0)
|
|
154
|
+
continue;
|
|
155
|
+
let assigned = false;
|
|
156
|
+
for (const cluster of clusters) {
|
|
157
|
+
// Compare against the first member's embedding (cluster seed)
|
|
158
|
+
const seed = cluster.members[0];
|
|
159
|
+
if (!seed.embedding)
|
|
160
|
+
continue;
|
|
161
|
+
const similarity = (0, reranker_js_1.cosineSimilarity)(fact.embedding, seed.embedding);
|
|
162
|
+
if (similarity >= threshold) {
|
|
163
|
+
cluster.members.push(fact);
|
|
164
|
+
assigned = true;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (!assigned) {
|
|
169
|
+
clusters.push({ members: [fact] });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Only return clusters with duplicates, pick representative for each
|
|
173
|
+
const result = [];
|
|
174
|
+
for (const cluster of clusters) {
|
|
175
|
+
if (cluster.members.length < 2)
|
|
176
|
+
continue;
|
|
177
|
+
const representative = pickRepresentative(cluster.members);
|
|
178
|
+
const duplicates = cluster.members.filter((m) => m !== representative);
|
|
179
|
+
result.push({ representative, duplicates });
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=consolidation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consolidation.js","sourceRoot":"","sources":["../src/consolidation.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAcH,wDAOC;AAQD,8DAOC;AAkDD,8CAmBC;AAgBD,0CAMC;AA2CD,oCAuCC;AA/MD,wDAA0D;AAE1D,8EAA8E;AAC9E,gBAAgB;AAChB,8EAA8E;AAE9E;;;;;GAKG;AACH,SAAgB,sBAAsB;IACpC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,iCAAiC,CAAC;IAC7D,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;QAClC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,IAAI,MAAM,IAAI,CA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