@totalreclaw/mcp-server 1.0.0 → 1.1.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 +68 -63
- 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 +260 -50
- 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 +19 -4
- 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/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 +29 -16
- 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 +11 -4
package/README.md
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
# @totalreclaw/mcp-server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Encrypted memory for your AI agent — zero-knowledge E2EE, semantic search, and portable storage.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Works with Claude Desktop, Cursor, Windsurf, and any MCP-compatible client. Your memories are encrypted on your device before leaving — no one can read them, not even us.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
**[totalreclaw.xyz](https://totalreclaw.xyz)**
|
|
8
|
+
|
|
9
|
+
## Quick Start
|
|
10
|
+
|
|
11
|
+
### 1. Generate your recovery phrase
|
|
12
|
+
|
|
13
|
+
Your **recovery phrase** is a 12-word BIP-39 mnemonic that derives all encryption keys. TotalReclaw never sends it to the server — it stays on your machine.
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
**New user:** Generate a mnemonic using any BIP-39 tool, or let TotalReclaw generate one on first run.
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
**Returning user:** Use your existing 12-word phrase to restore your memories on a new device.
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
> **Save your recovery phrase somewhere safe.** It's the only way to recover your encrypted memories.
|
|
20
|
+
|
|
21
|
+
### 2. Add to your MCP client
|
|
22
|
+
|
|
23
|
+
#### Claude Desktop
|
|
24
|
+
|
|
25
|
+
Add to `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS) or `%APPDATA%\Claude\claude_desktop_config.json` (Windows):
|
|
16
26
|
|
|
17
27
|
```json
|
|
18
28
|
{
|
|
@@ -21,84 +31,79 @@ Add to your `claude_desktop_config.json`:
|
|
|
21
31
|
"command": "npx",
|
|
22
32
|
"args": ["-y", "@totalreclaw/mcp-server"],
|
|
23
33
|
"env": {
|
|
24
|
-
"TOTALRECLAW_SERVER_URL": "
|
|
25
|
-
"TOTALRECLAW_MASTER_PASSWORD": "your
|
|
34
|
+
"TOTALRECLAW_SERVER_URL": "https://api.totalreclaw.xyz",
|
|
35
|
+
"TOTALRECLAW_MASTER_PASSWORD": "your twelve word recovery phrase goes here replace these words"
|
|
26
36
|
}
|
|
27
37
|
}
|
|
28
38
|
}
|
|
29
39
|
}
|
|
30
40
|
```
|
|
31
41
|
|
|
32
|
-
|
|
42
|
+
#### Cursor / Windsurf
|
|
33
43
|
|
|
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.
|
|
44
|
+
Add to your MCP settings (Settings > MCP Servers):
|
|
50
45
|
|
|
51
46
|
```json
|
|
52
47
|
{
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
"totalreclaw": {
|
|
49
|
+
"command": "npx",
|
|
50
|
+
"args": ["-y", "@totalreclaw/mcp-server"],
|
|
51
|
+
"env": {
|
|
52
|
+
"TOTALRECLAW_SERVER_URL": "https://api.totalreclaw.xyz",
|
|
53
|
+
"TOTALRECLAW_MASTER_PASSWORD": "your twelve word recovery phrase goes here replace these words"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
56
|
}
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
###
|
|
60
|
-
|
|
61
|
-
Search memories semantically.
|
|
59
|
+
### 3. Verify
|
|
62
60
|
|
|
63
|
-
|
|
64
|
-
{
|
|
65
|
-
"query": "user preferences",
|
|
66
|
-
"k": 8,
|
|
67
|
-
"min_importance": 5
|
|
68
|
-
}
|
|
69
|
-
```
|
|
61
|
+
Ask your AI agent: *"Do you have access to TotalReclaw memory tools?"*
|
|
70
62
|
|
|
71
|
-
|
|
63
|
+
It should confirm access to `totalreclaw_remember`, `totalreclaw_recall`, `totalreclaw_forget`, and `totalreclaw_export`.
|
|
72
64
|
|
|
73
|
-
|
|
65
|
+
## How It Works
|
|
74
66
|
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
"fact_id": "uuid-of-fact"
|
|
78
|
-
}
|
|
79
|
-
```
|
|
67
|
+
All encryption happens **client-side** inside the MCP server process on your machine:
|
|
80
68
|
|
|
81
|
-
|
|
69
|
+
1. Facts are encrypted with AES-256-GCM before leaving your device
|
|
70
|
+
2. Search uses blind indices (SHA-256 hashes) — the server never sees your search terms
|
|
71
|
+
3. Your recovery phrase derives all keys via Argon2id + HKDF — the server never sees it
|
|
72
|
+
4. Encrypted facts are stored on-chain (Gnosis Chain) and indexed by The Graph
|
|
82
73
|
|
|
83
|
-
|
|
74
|
+
The server only ever sees ciphertext and hashed tokens.
|
|
84
75
|
|
|
85
|
-
|
|
86
|
-
{
|
|
87
|
-
"format": "markdown"
|
|
88
|
-
}
|
|
89
|
-
```
|
|
76
|
+
## Environment Variables
|
|
90
77
|
|
|
91
|
-
|
|
78
|
+
| Variable | Description | Default |
|
|
79
|
+
|----------|-------------|---------|
|
|
80
|
+
| `TOTALRECLAW_SERVER_URL` | TotalReclaw server URL | `https://api.totalreclaw.xyz` |
|
|
81
|
+
| `TOTALRECLAW_MASTER_PASSWORD` | 12-word BIP-39 recovery phrase | Required |
|
|
82
|
+
| `TOTALRECLAW_SELF_HOSTED` | Set to `true` to use a self-hosted server instead of the managed service. | `false` |
|
|
83
|
+
| `TOTALRECLAW_CHAIN_ID` | Chain ID (100=Gnosis mainnet, 10200=Chiado testnet). Managed service only. | `100` |
|
|
92
84
|
|
|
93
|
-
|
|
85
|
+
## Available Tools
|
|
94
86
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
| Tool | Description |
|
|
88
|
+
|------|-------------|
|
|
89
|
+
| `totalreclaw_remember` | Store a fact in encrypted memory |
|
|
90
|
+
| `totalreclaw_recall` | Search memories by natural language query |
|
|
91
|
+
| `totalreclaw_forget` | Delete a specific memory by ID |
|
|
92
|
+
| `totalreclaw_export` | Export all memories decrypted as Markdown or JSON |
|
|
93
|
+
| `totalreclaw_status` | Check billing status and quota usage |
|
|
94
|
+
| `totalreclaw_import` | Re-import previously exported memories (JSON/Markdown) |
|
|
95
|
+
| `totalreclaw_import_from` | Import from Mem0 or MCP Memory Server |
|
|
96
|
+
| `totalreclaw_consolidate` | Merge duplicate and related memories |
|
|
97
|
+
| `totalreclaw_upgrade` | Get a link to upgrade to Pro |
|
|
98
|
+
|
|
99
|
+
## Free Tier & Pricing
|
|
100
|
+
|
|
101
|
+
| Tier | Writes | Reads | Price |
|
|
102
|
+
|------|--------|-------|-------|
|
|
103
|
+
| **Free** | 250/month | Unlimited | $0 |
|
|
104
|
+
| **Pro** | 10,000/month | Unlimited | $2-5/month |
|
|
105
|
+
|
|
106
|
+
Pay with card (Stripe) or crypto (Coinbase Commerce). Counter resets monthly.
|
|
102
107
|
|
|
103
108
|
## Development
|
|
104
109
|
|
|
@@ -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,"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