toon-memory 3.3.0 → 3.4.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 +5 -1
- package/dist/cli/setup.js +8 -3
- package/mcp/server.js +250 -13
- package/package.json +7 -3
- package/src/cli/hooks.ts +4 -0
- package/src/cli/memory.ts +1 -1
- package/src/lib/graph.ts +29 -4
- package/src/lib/quality.ts +26 -6
- package/src/mcp/config.ts +1 -1
- package/src/mcp/tools.ts +233 -0
package/README.md
CHANGED
|
@@ -65,7 +65,7 @@ Read [How toon-memory Makes Your AI Agent Smarter](https://luiggival08.github.io
|
|
|
65
65
|
|
|
66
66
|
## Features
|
|
67
67
|
|
|
68
|
-
- **
|
|
68
|
+
- **34 MCP tools** — Full memory management via Model Context Protocol, including `memory_smart_recall` (unified recall), `memory_sessions` for multi-session coordination, `context_*` tools for one-call context generation (briefing, diff, focus, health audit, export), `memory_compress` (LLM-powered compression), `memory_compress_all` (batch compression), `memory_primer` (auto-injected context), `memory_merge_sessions` (cross-session merge), `memory_pin`/`memory_unpin` (pin important entries), `memory_search` (unified search with tag filters), `memory_tag` (batch tag operations), and `memory_export_gist`/`memory_import_gist` (GitHub Gist sync)
|
|
69
69
|
- **MCP Resources** — Read memory as context without tool invocations, including a System Primer (auto-generated knowledge map)
|
|
70
70
|
- **15 agents supported** — OpenCode, VS Code, Claude Code, Cursor, Windsurf, Cline, Continue, Codex CLI, Gemini CLI, Zed, Antigravity, Aider, KiloCode, OpenClaw, Kiro
|
|
71
71
|
- **Interactive installer** — Select which agents to configure from a menu
|
|
@@ -247,6 +247,10 @@ Add to `~/.codeium/windsurf/mcp_config.json`:
|
|
|
247
247
|
| `context_focus` | **Hyper-focused briefing**: only relevant memory + related source files + callers + test files for a query |
|
|
248
248
|
| `context_health` | **Memory health audit**: orphan links, duplicates, broken file refs, expired TTL, stale sessions, score 0–100 |
|
|
249
249
|
| `context_export` | **Export memory as markdown**: injectable context for system prompts (full or compact) |
|
|
250
|
+
| `memory_pin` | **Pin an entry**: pinned entries always appear at the top of recall results, even without a keyword match |
|
|
251
|
+
| `memory_unpin` | **Unpin an entry**: remove the pinned flag |
|
|
252
|
+
| `memory_search` | **Unified search with filters**: same as `memory_recall` plus `category`, `tags`, `from_date`, `to_date` filters. Tag filter uses AND logic — all specified tags must match |
|
|
253
|
+
| `memory_tag` | **Batch tag operations**: `add`, `remove`, or `set` tags on one or more entries by key or id |
|
|
250
254
|
|
|
251
255
|
### MCP Resources
|
|
252
256
|
|
package/dist/cli/setup.js
CHANGED
|
@@ -292,7 +292,7 @@ function installMemoryDir() {
|
|
|
292
292
|
const memoryFile = join4(MEMORY_DIR, "data.toon");
|
|
293
293
|
if (!existsSync4(MEMORY_DIR)) mkdirSync2(MEMORY_DIR, { recursive: true });
|
|
294
294
|
if (!existsSync4(memoryFile)) {
|
|
295
|
-
writeFileSync2(memoryFile, "version: 1\n[0|]
|
|
295
|
+
writeFileSync2(memoryFile, "version: 1\n[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence|lastAccessed|pinned}:\n");
|
|
296
296
|
console.log(" Created .toon-memory/memory/data.toon");
|
|
297
297
|
}
|
|
298
298
|
}
|
|
@@ -562,12 +562,16 @@ import { existsSync as existsSync7, mkdirSync as mkdirSync5, readFileSync as rea
|
|
|
562
562
|
import { dirname as dirname5, join as join5 } from "path";
|
|
563
563
|
function sessionStartHookContent(agentName) {
|
|
564
564
|
return `#!/bin/bash
|
|
565
|
+
# toon-memory session-start hook for ${agentName}
|
|
566
|
+
# Registers this session so memory_sessions and context_* tools work.
|
|
565
567
|
node "${SESSION_START_JS}" ${agentName}
|
|
566
568
|
exit 0
|
|
567
569
|
`;
|
|
568
570
|
}
|
|
569
571
|
function captureHookContent(agentName) {
|
|
570
572
|
return `#!/bin/bash
|
|
573
|
+
# toon-memory capture hook for ${agentName}
|
|
574
|
+
# Logs tool calls to observations.toon (opt-in via TOON_MEMORY_CAPTURE env or config).
|
|
571
575
|
CFG="${CAPTURE_CONFIG}"
|
|
572
576
|
if [ -z "$TOON_MEMORY_CAPTURE" ]; then
|
|
573
577
|
if [ ! -f "$CFG" ]; then exit 0; fi
|
|
@@ -1518,7 +1522,7 @@ function ensureMemoryDir() {
|
|
|
1518
1522
|
function ensureMemoryFile() {
|
|
1519
1523
|
ensureMemoryDir();
|
|
1520
1524
|
if (!existsSync12(MEMORY_FILE)) {
|
|
1521
|
-
writeFileSync10(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence}:\n");
|
|
1525
|
+
writeFileSync10(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence|lastAccessed|pinned}:\n");
|
|
1522
1526
|
}
|
|
1523
1527
|
}
|
|
1524
1528
|
|
|
@@ -1574,7 +1578,8 @@ function parseEntries(data) {
|
|
|
1574
1578
|
ttl: ttl || "",
|
|
1575
1579
|
accessed: accessedRaw ? parseInt(accessedRaw) || 0 : 0,
|
|
1576
1580
|
links: (linksRaw || "").split(/[\s;]+/).map((t) => t.trim()).filter(Boolean),
|
|
1577
|
-
lastAccessed: parts.length > 12 ? parts[12] || "" : ""
|
|
1581
|
+
lastAccessed: parts.length > 12 ? parts[12] || "" : "",
|
|
1582
|
+
pinned: parts.length > 13 && parts[13] === "1"
|
|
1578
1583
|
});
|
|
1579
1584
|
}
|
|
1580
1585
|
return out;
|
package/mcp/server.js
CHANGED
|
@@ -22175,7 +22175,8 @@ function parseEntries(data) {
|
|
|
22175
22175
|
ttl: ttl || "",
|
|
22176
22176
|
accessed: accessedRaw ? parseInt(accessedRaw) || 0 : 0,
|
|
22177
22177
|
links: (linksRaw || "").split(/[\s;]+/).map((t) => t.trim()).filter(Boolean),
|
|
22178
|
-
lastAccessed: parts.length > 12 ? parts[12] || "" : ""
|
|
22178
|
+
lastAccessed: parts.length > 12 ? parts[12] || "" : "",
|
|
22179
|
+
pinned: parts.length > 13 && parts[13] === "1"
|
|
22179
22180
|
});
|
|
22180
22181
|
}
|
|
22181
22182
|
return out;
|
|
@@ -22263,6 +22264,7 @@ function graphRecallDetailed(data, query, opts = {}) {
|
|
|
22263
22264
|
const { adjacency, byKey } = buildGraph(entries);
|
|
22264
22265
|
const hops = Math.max(1, Math.min(2, opts.hops ?? 1));
|
|
22265
22266
|
const category = opts.category || "";
|
|
22267
|
+
const tagsFilter = opts.tags ? opts.tags.split(";").map((t) => t.trim()).filter(Boolean) : [];
|
|
22266
22268
|
const from_date = opts.from_date || "";
|
|
22267
22269
|
const to_date = opts.to_date || "";
|
|
22268
22270
|
const qTokens = tokenize(query);
|
|
@@ -22272,6 +22274,7 @@ function graphRecallDetailed(data, query, opts = {}) {
|
|
|
22272
22274
|
const seedKeys = /* @__PURE__ */ new Set();
|
|
22273
22275
|
for (const e of entries) {
|
|
22274
22276
|
if (category && e.category !== category) continue;
|
|
22277
|
+
if (tagsFilter.length > 0 && !tagsFilter.every((t) => e.tags.includes(t))) continue;
|
|
22275
22278
|
if (from_date && e.date < from_date) continue;
|
|
22276
22279
|
if (to_date && e.date > to_date) continue;
|
|
22277
22280
|
if (e.ttl && isExpiredLocal(e.ttl)) continue;
|
|
@@ -22288,7 +22291,10 @@ function graphRecallDetailed(data, query, opts = {}) {
|
|
|
22288
22291
|
const scored = [];
|
|
22289
22292
|
if (seedKeys.size === 0) {
|
|
22290
22293
|
scored.push(
|
|
22291
|
-
...[...entries].sort((a, b2) =>
|
|
22294
|
+
...[...entries].sort((a, b2) => {
|
|
22295
|
+
if (a.pinned !== b2.pinned) return a.pinned ? -1 : 1;
|
|
22296
|
+
return importance(b2) - importance(a);
|
|
22297
|
+
}).slice(0, opts.limit ?? 6).map((e) => ({ e, s: importance(e) }))
|
|
22292
22298
|
);
|
|
22293
22299
|
} else {
|
|
22294
22300
|
const best = /* @__PURE__ */ new Map();
|
|
@@ -22311,11 +22317,23 @@ function graphRecallDetailed(data, query, opts = {}) {
|
|
|
22311
22317
|
let s2 = (bm25.get(k3) || 0) + W_CENT * cent.get(k3) + W_IMP * importance(e);
|
|
22312
22318
|
if (seedKeys.has(k3)) s2 += SEED_BONUS;
|
|
22313
22319
|
s2 *= decay;
|
|
22314
|
-
return { e, s: s2 };
|
|
22315
|
-
}).sort((a, b2) =>
|
|
22320
|
+
return { e, s: s2, pinned: e.pinned };
|
|
22321
|
+
}).sort((a, b2) => {
|
|
22322
|
+
if (a.pinned !== b2.pinned) return a.pinned ? -1 : 1;
|
|
22323
|
+
return b2.s - a.s;
|
|
22324
|
+
}).slice(0, opts.limit ?? 6)
|
|
22316
22325
|
);
|
|
22317
22326
|
}
|
|
22318
22327
|
selected = scored.map((x) => x.e);
|
|
22328
|
+
const pinnedKeys = new Set(entries.filter((e) => e.pinned).map((e) => e.key));
|
|
22329
|
+
const inResult = new Set(selected.map((e) => e.key));
|
|
22330
|
+
for (const pKey of pinnedKeys) {
|
|
22331
|
+
if (!inResult.has(pKey) && byKey.has(pKey)) {
|
|
22332
|
+
const e = byKey.get(pKey);
|
|
22333
|
+
selected.unshift(e);
|
|
22334
|
+
scored.unshift({ e, s: 999 });
|
|
22335
|
+
}
|
|
22336
|
+
}
|
|
22319
22337
|
const subAdj = /* @__PURE__ */ new Map();
|
|
22320
22338
|
for (const e of selected) {
|
|
22321
22339
|
subAdj.set(
|
|
@@ -22338,13 +22356,14 @@ function renderCompact(entries, opts = {}) {
|
|
|
22338
22356
|
if (!isSeed && e.content.length > snippetLen) {
|
|
22339
22357
|
body = e.content.slice(0, snippetLen).trimEnd() + "\u2026";
|
|
22340
22358
|
}
|
|
22359
|
+
const tag = e.pinned ? " \u{1F4CC}" : "";
|
|
22341
22360
|
const tags = e.tags.length ? ` \xB7 tags: ${e.tags.join(";")}` : "";
|
|
22342
22361
|
let edges = "";
|
|
22343
22362
|
if (opts.adjacency) {
|
|
22344
22363
|
const nb2 = (opts.adjacency.get(e.key) || []).map((k3) => index.get(k3)).filter((x) => typeof x === "number");
|
|
22345
22364
|
if (nb2.length) edges = ` \xB7 edges: ->${nb2.join(", ->")}`;
|
|
22346
22365
|
}
|
|
22347
|
-
return `[${n}] ${e.category}/${e.key}
|
|
22366
|
+
return `[${n}] ${e.category}/${e.key}${tag}
|
|
22348
22367
|
${body}${tags}${edges}`;
|
|
22349
22368
|
}).join("\n\n");
|
|
22350
22369
|
}
|
|
@@ -22481,10 +22500,24 @@ function generateSmartRecall(data, intent, opts = {}) {
|
|
|
22481
22500
|
if (fileDate && e.date && fileDate > e.date) drift = 0.3;
|
|
22482
22501
|
}
|
|
22483
22502
|
const combined = bm25Score + 0.3 * centScore + 0.3 * impScore + (matchesQuery ? 0.5 : 0) - drift;
|
|
22484
|
-
return { entry: e, score: combined, matchesQuery };
|
|
22485
|
-
}).filter((x) => x.score > 0 || x.matchesQuery).sort((a, b2) =>
|
|
22503
|
+
return { entry: e, score: combined, matchesQuery, pinned: e.pinned };
|
|
22504
|
+
}).filter((x) => x.score > 0 || x.matchesQuery).sort((a, b2) => {
|
|
22505
|
+
if (a.pinned !== b2.pinned) return a.pinned ? -1 : 1;
|
|
22506
|
+
return b2.score - a.score;
|
|
22507
|
+
}).slice(0, limit);
|
|
22508
|
+
const pinnedKeys = new Set(entries.filter((e) => e.pinned).map((e) => e.key));
|
|
22509
|
+
const inResult = new Set(scored.map((x) => x.entry.key));
|
|
22510
|
+
for (const pKey of pinnedKeys) {
|
|
22511
|
+
if (!inResult.has(pKey)) {
|
|
22512
|
+
const e = entries.find((en2) => en2.key === pKey);
|
|
22513
|
+
if (e) scored.unshift({ entry: e, score: 999, matchesQuery: false, pinned: true });
|
|
22514
|
+
}
|
|
22515
|
+
}
|
|
22486
22516
|
if (scored.length === 0) {
|
|
22487
|
-
const top = entries.filter((e) => !category || e.category === category).sort((a, b2) =>
|
|
22517
|
+
const top = entries.filter((e) => !category || e.category === category).sort((a, b2) => {
|
|
22518
|
+
if (a.pinned !== b2.pinned) return a.pinned ? -1 : 1;
|
|
22519
|
+
return importance(b2) - importance(a);
|
|
22520
|
+
}).slice(0, limit);
|
|
22488
22521
|
if (top.length === 0) return "No entries in memory.";
|
|
22489
22522
|
return renderCompact(top);
|
|
22490
22523
|
}
|
|
@@ -22505,7 +22538,10 @@ function generateSystemPrimer(data) {
|
|
|
22505
22538
|
`Entries: ${entries.length}`,
|
|
22506
22539
|
""
|
|
22507
22540
|
];
|
|
22508
|
-
const top = [...entries].filter((e) => !isPrivate(e)).sort((a, b2) =>
|
|
22541
|
+
const top = [...entries].filter((e) => !isPrivate(e)).sort((a, b2) => {
|
|
22542
|
+
if (a.pinned !== b2.pinned) return a.pinned ? -1 : 1;
|
|
22543
|
+
return importance(b2) - importance(a);
|
|
22544
|
+
}).slice(0, 5);
|
|
22509
22545
|
lines.push("Top memories:");
|
|
22510
22546
|
for (const e of top) {
|
|
22511
22547
|
const quality = qualityScore(
|
|
@@ -22517,8 +22553,9 @@ function generateSystemPrimer(data) {
|
|
|
22517
22553
|
e.lastAccessed
|
|
22518
22554
|
);
|
|
22519
22555
|
const conf = quality >= 0.5 ? "high" : quality >= 0.3 ? "medium" : "low";
|
|
22556
|
+
const pin = e.pinned ? " \u{1F4CC}" : "";
|
|
22520
22557
|
lines.push(
|
|
22521
|
-
` [${e.category}] ${e.key} \u2014 ${e.content.slice(0, 80)} (quality: ${conf})`
|
|
22558
|
+
` [${e.category}] ${e.key}${pin} \u2014 ${e.content.slice(0, 80)} (quality: ${conf})`
|
|
22522
22559
|
);
|
|
22523
22560
|
}
|
|
22524
22561
|
lines.push("");
|
|
@@ -32548,7 +32585,7 @@ function ensureMemoryDir() {
|
|
|
32548
32585
|
function ensureMemoryFile() {
|
|
32549
32586
|
ensureMemoryDir();
|
|
32550
32587
|
if (!existsSync2(MEMORY_FILE)) {
|
|
32551
|
-
writeFileSync2(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence}:\n");
|
|
32588
|
+
writeFileSync2(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence|lastAccessed|pinned}:\n");
|
|
32552
32589
|
}
|
|
32553
32590
|
}
|
|
32554
32591
|
|
|
@@ -35113,6 +35150,206 @@ ${mergeActions.join("\n")}`
|
|
|
35113
35150
|
};
|
|
35114
35151
|
}
|
|
35115
35152
|
);
|
|
35153
|
+
server2.registerTool(
|
|
35154
|
+
"memory_pin",
|
|
35155
|
+
{
|
|
35156
|
+
title: "Pin Entry",
|
|
35157
|
+
description: "Pin a memory entry so it always appears first in recalls. Useful for critical decisions, project rules, or frequently-needed context.",
|
|
35158
|
+
inputSchema: {
|
|
35159
|
+
key: external_exports.string().describe("Key or id of the entry to pin")
|
|
35160
|
+
}
|
|
35161
|
+
},
|
|
35162
|
+
async ({ key }) => {
|
|
35163
|
+
const data = readMemory();
|
|
35164
|
+
const lines = data.split("\n");
|
|
35165
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l));
|
|
35166
|
+
if (headerIdx === -1) {
|
|
35167
|
+
return { content: [{ type: "text", text: "No entries in memory" }] };
|
|
35168
|
+
}
|
|
35169
|
+
let found = false;
|
|
35170
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
35171
|
+
const line = lines[i];
|
|
35172
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue;
|
|
35173
|
+
if (line.startsWith(" summaries:")) break;
|
|
35174
|
+
const parts = line.trim().split("|");
|
|
35175
|
+
if (parts[0] === key || parts[2] === key) {
|
|
35176
|
+
while (parts.length < 14) parts.push("");
|
|
35177
|
+
parts[13] = "1";
|
|
35178
|
+
lines[i] = ` ${parts.join("|")}`;
|
|
35179
|
+
found = true;
|
|
35180
|
+
break;
|
|
35181
|
+
}
|
|
35182
|
+
}
|
|
35183
|
+
if (!found) {
|
|
35184
|
+
return { content: [{ type: "text", text: `"${key}" not found in memory.` }] };
|
|
35185
|
+
}
|
|
35186
|
+
writeMemory(lines.join("\n"));
|
|
35187
|
+
return { content: [{ type: "text", text: `\u{1F4CC} Pinned "${key}". It will now appear first in recalls.` }] };
|
|
35188
|
+
}
|
|
35189
|
+
);
|
|
35190
|
+
server2.registerTool(
|
|
35191
|
+
"memory_unpin",
|
|
35192
|
+
{
|
|
35193
|
+
title: "Unpin Entry",
|
|
35194
|
+
description: "Remove the pin from a memory entry so it returns to normal ranking.",
|
|
35195
|
+
inputSchema: {
|
|
35196
|
+
key: external_exports.string().describe("Key or id of the entry to unpin")
|
|
35197
|
+
}
|
|
35198
|
+
},
|
|
35199
|
+
async ({ key }) => {
|
|
35200
|
+
const data = readMemory();
|
|
35201
|
+
const lines = data.split("\n");
|
|
35202
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l));
|
|
35203
|
+
if (headerIdx === -1) {
|
|
35204
|
+
return { content: [{ type: "text", text: "No entries in memory" }] };
|
|
35205
|
+
}
|
|
35206
|
+
let found = false;
|
|
35207
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
35208
|
+
const line = lines[i];
|
|
35209
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue;
|
|
35210
|
+
if (line.startsWith(" summaries:")) break;
|
|
35211
|
+
const parts = line.trim().split("|");
|
|
35212
|
+
if (parts[0] === key || parts[2] === key) {
|
|
35213
|
+
if (parts.length > 13) {
|
|
35214
|
+
parts[13] = "0";
|
|
35215
|
+
lines[i] = ` ${parts.join("|")}`;
|
|
35216
|
+
}
|
|
35217
|
+
found = true;
|
|
35218
|
+
break;
|
|
35219
|
+
}
|
|
35220
|
+
}
|
|
35221
|
+
if (!found) {
|
|
35222
|
+
return { content: [{ type: "text", text: `"${key}" not found in memory.` }] };
|
|
35223
|
+
}
|
|
35224
|
+
writeMemory(lines.join("\n"));
|
|
35225
|
+
return { content: [{ type: "text", text: `\u{1F4CC} Unpinned "${key}". It will now use normal ranking.` }] };
|
|
35226
|
+
}
|
|
35227
|
+
);
|
|
35228
|
+
server2.registerTool(
|
|
35229
|
+
"memory_search",
|
|
35230
|
+
{
|
|
35231
|
+
title: "Unified Search",
|
|
35232
|
+
description: "Search memory with combined filters: text query, category, tags, and date range. Returns ranked results.",
|
|
35233
|
+
inputSchema: {
|
|
35234
|
+
query: external_exports.string().describe("Text to search for"),
|
|
35235
|
+
category: external_exports.string().optional().default("").describe("Filter by category (empty = all)"),
|
|
35236
|
+
tags: external_exports.string().optional().default("").describe("Semicolon-separated tags; entry must have ALL specified tags"),
|
|
35237
|
+
from_date: external_exports.string().optional().default("").describe("Start date filter (YYYY-MM-DD or relative like 7d)"),
|
|
35238
|
+
to_date: external_exports.string().optional().default("").describe("End date filter (YYYY-MM-DD)"),
|
|
35239
|
+
limit: external_exports.number().optional().default(6).describe("Maximum entries to return"),
|
|
35240
|
+
mode: external_exports.enum(["flat", "graph"]).optional().default("flat").describe("'flat' = keyword search (default). 'graph' = graph-based recall."),
|
|
35241
|
+
hops: external_exports.number().optional().default(1).describe("Graph depth in 'graph' mode (1 or 2). Default 1."),
|
|
35242
|
+
compact: external_exports.boolean().optional().default(false).describe("Token-efficient output")
|
|
35243
|
+
}
|
|
35244
|
+
},
|
|
35245
|
+
async ({ query, category, tags, from_date, to_date, limit, mode, hops, compact }) => {
|
|
35246
|
+
const data = readMemory();
|
|
35247
|
+
const resolvedFrom = from_date ? parseRelativeDate(from_date) : "";
|
|
35248
|
+
const detail = graphRecallDetailed(data, query, {
|
|
35249
|
+
category: category || void 0,
|
|
35250
|
+
tags: tags || void 0,
|
|
35251
|
+
from_date: resolvedFrom || void 0,
|
|
35252
|
+
to_date: to_date || void 0,
|
|
35253
|
+
hops,
|
|
35254
|
+
limit
|
|
35255
|
+
});
|
|
35256
|
+
if (detail.entries.length === 0) {
|
|
35257
|
+
return { content: [{ type: "text", text: `No results found for "${query}"` }] };
|
|
35258
|
+
}
|
|
35259
|
+
bumpAccessed(detail.entries.map((e) => e.id));
|
|
35260
|
+
if (compact) {
|
|
35261
|
+
const formatted2 = renderCompact(detail.entries, {
|
|
35262
|
+
adjacency: detail.adjacency,
|
|
35263
|
+
seeds: detail.seeds,
|
|
35264
|
+
snippetLen: 90
|
|
35265
|
+
});
|
|
35266
|
+
return { content: [{ type: "text", text: `\u{1F50D} Search: "${query}"
|
|
35267
|
+
|
|
35268
|
+
${formatted2}` }] };
|
|
35269
|
+
}
|
|
35270
|
+
const formatted = detail.entries.map((r2) => {
|
|
35271
|
+
const links = r2.links.length ? `
|
|
35272
|
+
links: ${r2.links.join(", ")}` : "";
|
|
35273
|
+
const pin = r2.pinned ? " \u{1F4CC}" : "";
|
|
35274
|
+
return `[${r2.category}] ${r2.key}${pin} (${r2.id})
|
|
35275
|
+
${r2.content}
|
|
35276
|
+
File: ${r2.file} | Tags: ${r2.tags.join(";")} | Date: ${r2.date}${links}`;
|
|
35277
|
+
}).join("\n\n");
|
|
35278
|
+
let filterInfo = "";
|
|
35279
|
+
if (category) filterInfo += ` category: ${category}`;
|
|
35280
|
+
if (tags) filterInfo += ` tags: ${tags}`;
|
|
35281
|
+
if (from_date) filterInfo += ` from: ${from_date}`;
|
|
35282
|
+
if (to_date) filterInfo += ` to: ${to_date}`;
|
|
35283
|
+
return {
|
|
35284
|
+
content: [{ type: "text", text: `\u{1F50D} Search results${filterInfo}:
|
|
35285
|
+
|
|
35286
|
+
${formatted}` }]
|
|
35287
|
+
};
|
|
35288
|
+
}
|
|
35289
|
+
);
|
|
35290
|
+
server2.registerTool(
|
|
35291
|
+
"memory_tag",
|
|
35292
|
+
{
|
|
35293
|
+
title: "Batch Tag Operations",
|
|
35294
|
+
description: "Batch add, remove, or set tags on one or more memory entries by key or id.",
|
|
35295
|
+
inputSchema: {
|
|
35296
|
+
action: external_exports.enum(["add", "remove", "set"]).describe("'add': merge tags (union). 'remove': remove specified tags. 'set': replace all tags."),
|
|
35297
|
+
tags: external_exports.string().describe("Semicolon-separated tags to apply"),
|
|
35298
|
+
ids: external_exports.string().describe("Comma or space-separated entry keys or IDs to modify")
|
|
35299
|
+
}
|
|
35300
|
+
},
|
|
35301
|
+
async ({ action, tags, ids }) => {
|
|
35302
|
+
const data = readMemory();
|
|
35303
|
+
const lines = data.split("\n");
|
|
35304
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l));
|
|
35305
|
+
if (headerIdx === -1) {
|
|
35306
|
+
return { content: [{ type: "text", text: "No entries in memory." }] };
|
|
35307
|
+
}
|
|
35308
|
+
const targetIds = ids.split(/[\s,]+/).filter(Boolean);
|
|
35309
|
+
const targetSet = new Set(targetIds);
|
|
35310
|
+
const tagsToApply = tags.split(";").map((t) => t.trim()).filter(Boolean);
|
|
35311
|
+
if (tagsToApply.length === 0) {
|
|
35312
|
+
return { content: [{ type: "text", text: "No tags provided." }] };
|
|
35313
|
+
}
|
|
35314
|
+
let modified = 0;
|
|
35315
|
+
const modifiedKeys = [];
|
|
35316
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
35317
|
+
const line = lines[i];
|
|
35318
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue;
|
|
35319
|
+
if (line.startsWith(" summaries:")) break;
|
|
35320
|
+
const parts = line.trim().split("|");
|
|
35321
|
+
const entryId = parts[0];
|
|
35322
|
+
const entryKey = parts[2];
|
|
35323
|
+
if (!targetSet.has(entryId) && !targetSet.has(entryKey)) continue;
|
|
35324
|
+
const currentTags = (parts[5] || "").split(";").map((t) => t.trim()).filter(Boolean);
|
|
35325
|
+
let newTags;
|
|
35326
|
+
if (action === "add") {
|
|
35327
|
+
newTags = [.../* @__PURE__ */ new Set([...currentTags, ...tagsToApply])];
|
|
35328
|
+
} else if (action === "remove") {
|
|
35329
|
+
newTags = currentTags.filter((t) => !tagsToApply.includes(t));
|
|
35330
|
+
} else {
|
|
35331
|
+
newTags = [...tagsToApply];
|
|
35332
|
+
}
|
|
35333
|
+
parts[5] = newTags.join(";");
|
|
35334
|
+
lines[i] = ` ${parts.join("|")}`;
|
|
35335
|
+
modified++;
|
|
35336
|
+
modifiedKeys.push(entryKey);
|
|
35337
|
+
}
|
|
35338
|
+
if (modified === 0) {
|
|
35339
|
+
return { content: [{ type: "text", text: `No entries found matching: ${targetIds.join(", ")}` }] };
|
|
35340
|
+
}
|
|
35341
|
+
writeMemory(lines.join("\n"));
|
|
35342
|
+
const actionLabel = action === "add" ? "Added" : action === "remove" ? "Removed" : "Set";
|
|
35343
|
+
return {
|
|
35344
|
+
content: [{
|
|
35345
|
+
type: "text",
|
|
35346
|
+
text: `\u{1F3F7}\uFE0F ${actionLabel} tags on ${modified} entries:
|
|
35347
|
+
Tags: ${tagsToApply.join(";")}
|
|
35348
|
+
Entries: ${modifiedKeys.join(", ")}`
|
|
35349
|
+
}]
|
|
35350
|
+
};
|
|
35351
|
+
}
|
|
35352
|
+
);
|
|
35116
35353
|
K3(
|
|
35117
35354
|
server2,
|
|
35118
35355
|
"memory_visualize",
|
|
@@ -46776,7 +47013,7 @@ function registerResources(server2) {
|
|
|
46776
47013
|
|
|
46777
47014
|
// src/mcp/server.ts
|
|
46778
47015
|
var server = new McpServer(
|
|
46779
|
-
{ name: "toon-memory", version: "3.
|
|
47016
|
+
{ name: "toon-memory", version: "3.4.0" },
|
|
46780
47017
|
{ capabilities: { tools: { listChanged: true }, resources: { listChanged: true }, experimental: { "io.modelcontextprotocol/ui": {} } } }
|
|
46781
47018
|
);
|
|
46782
47019
|
var s = server.server;
|
|
@@ -46788,7 +47025,7 @@ s.setRequestHandler(InitializeRequestSchema, async (request) => {
|
|
|
46788
47025
|
return {
|
|
46789
47026
|
protocolVersion,
|
|
46790
47027
|
capabilities: s.getCapabilities(),
|
|
46791
|
-
serverInfo: { name: "toon-memory", version: "3.
|
|
47028
|
+
serverInfo: { name: "toon-memory", version: "3.4.0" },
|
|
46792
47029
|
...s._instructions && { instructions: s._instructions }
|
|
46793
47030
|
};
|
|
46794
47031
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toon-memory",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"description": "MCP server giving AI coding agents offline, token-efficient persistent memory —
|
|
3
|
+
"version": "3.4.0",
|
|
4
|
+
"description": "MCP server giving AI coding agents offline, token-efficient persistent memory — 34 tools, 80% fewer tool calls, recall decisions, patterns, and bugs across sessions. No LLM, no server; works with 15+ agents.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"toon-memory": "./bin/toon-memory.js"
|
|
@@ -72,7 +72,11 @@
|
|
|
72
72
|
"toon",
|
|
73
73
|
"mcp",
|
|
74
74
|
"llm",
|
|
75
|
-
"token-efficiency"
|
|
75
|
+
"token-efficiency",
|
|
76
|
+
"pin",
|
|
77
|
+
"search",
|
|
78
|
+
"tags",
|
|
79
|
+
"graph"
|
|
76
80
|
],
|
|
77
81
|
"author": "luiggival",
|
|
78
82
|
"license": "MIT",
|
package/src/cli/hooks.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { projectRoot, CAPTURE_JS, SESSION_START_JS, CAPTURE_CONFIG, ANTIGRAVITY_
|
|
|
6
6
|
/** Hook script content for SessionStart reminder */
|
|
7
7
|
export function sessionStartHookContent(agentName: string): string {
|
|
8
8
|
return `#!/bin/bash
|
|
9
|
+
# toon-memory session-start hook for ${agentName}
|
|
10
|
+
# Registers this session so memory_sessions and context_* tools work.
|
|
9
11
|
node "${SESSION_START_JS}" ${agentName}
|
|
10
12
|
exit 0
|
|
11
13
|
`
|
|
@@ -17,6 +19,8 @@ exit 0
|
|
|
17
19
|
*/
|
|
18
20
|
export function captureHookContent(agentName: string): string {
|
|
19
21
|
return `#!/bin/bash
|
|
22
|
+
# toon-memory capture hook for ${agentName}
|
|
23
|
+
# Logs tool calls to observations.toon (opt-in via TOON_MEMORY_CAPTURE env or config).
|
|
20
24
|
CFG="${CAPTURE_CONFIG}"
|
|
21
25
|
if [ -z "$TOON_MEMORY_CAPTURE" ]; then
|
|
22
26
|
if [ ! -f "$CFG" ]; then exit 0; fi
|
package/src/cli/memory.ts
CHANGED
|
@@ -12,7 +12,7 @@ export function installMemoryDir(): void {
|
|
|
12
12
|
if (!existsSync(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true })
|
|
13
13
|
|
|
14
14
|
if (!existsSync(memoryFile)) {
|
|
15
|
-
writeFileSync(memoryFile, "version: 1\n[0|]
|
|
15
|
+
writeFileSync(memoryFile, "version: 1\n[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence|lastAccessed|pinned}:\n")
|
|
16
16
|
console.log(" Created .toon-memory/memory/data.toon")
|
|
17
17
|
}
|
|
18
18
|
}
|
package/src/lib/graph.ts
CHANGED
|
@@ -32,6 +32,8 @@ export interface GraphEntry {
|
|
|
32
32
|
links: string[]
|
|
33
33
|
/** ISO timestamp of last recall/access. Empty if never accessed. */
|
|
34
34
|
lastAccessed: string
|
|
35
|
+
/** Whether the entry is pinned (always appears first in recall). */
|
|
36
|
+
pinned: boolean
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
export interface MemoryGraph {
|
|
@@ -78,6 +80,7 @@ export function parseEntries(data: string): GraphEntry[] {
|
|
|
78
80
|
.map((t) => t.trim())
|
|
79
81
|
.filter(Boolean),
|
|
80
82
|
lastAccessed: parts.length > 12 ? parts[12] || "" : "",
|
|
83
|
+
pinned: parts.length > 13 && parts[13] === "1",
|
|
81
84
|
})
|
|
82
85
|
}
|
|
83
86
|
return out
|
|
@@ -132,6 +135,8 @@ export function buildGraph(entries: GraphEntry[]): MemoryGraph {
|
|
|
132
135
|
|
|
133
136
|
export interface GraphRecallOpts {
|
|
134
137
|
category?: string
|
|
138
|
+
/** Semicolon-separated; entry must have ALL specified tags to match. */
|
|
139
|
+
tags?: string
|
|
135
140
|
from_date?: string
|
|
136
141
|
to_date?: string
|
|
137
142
|
/** Graph traversal depth from matched seeds (1 or 2). Default 1. */
|
|
@@ -225,6 +230,7 @@ export function graphRecallDetailed(
|
|
|
225
230
|
const { adjacency, byKey } = buildGraph(entries)
|
|
226
231
|
const hops = Math.max(1, Math.min(2, opts.hops ?? 1))
|
|
227
232
|
const category = opts.category || ""
|
|
233
|
+
const tagsFilter = opts.tags ? opts.tags.split(";").map((t) => t.trim()).filter(Boolean) : []
|
|
228
234
|
const from_date = opts.from_date || ""
|
|
229
235
|
const to_date = opts.to_date || ""
|
|
230
236
|
|
|
@@ -236,6 +242,7 @@ export function graphRecallDetailed(
|
|
|
236
242
|
const seedKeys = new Set<string>()
|
|
237
243
|
for (const e of entries) {
|
|
238
244
|
if (category && e.category !== category) continue
|
|
245
|
+
if (tagsFilter.length > 0 && !tagsFilter.every((t) => e.tags.includes(t))) continue
|
|
239
246
|
if (from_date && e.date < from_date) continue
|
|
240
247
|
if (to_date && e.date > to_date) continue
|
|
241
248
|
if (e.ttl && isExpiredLocal(e.ttl)) continue
|
|
@@ -254,7 +261,10 @@ export function graphRecallDetailed(
|
|
|
254
261
|
if (seedKeys.size === 0) {
|
|
255
262
|
scored.push(
|
|
256
263
|
...[...entries]
|
|
257
|
-
.sort((a, b) =>
|
|
264
|
+
.sort((a, b) => {
|
|
265
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
|
|
266
|
+
return importance(b) - importance(a)
|
|
267
|
+
})
|
|
258
268
|
.slice(0, opts.limit ?? 6)
|
|
259
269
|
.map((e) => ({ e, s: importance(e) }))
|
|
260
270
|
)
|
|
@@ -282,15 +292,29 @@ export function graphRecallDetailed(
|
|
|
282
292
|
let s = (bm25.get(k) || 0) + W_CENT * cent.get(k)! + W_IMP * importance(e)
|
|
283
293
|
if (seedKeys.has(k)) s += SEED_BONUS
|
|
284
294
|
s *= decay
|
|
285
|
-
return { e, s }
|
|
295
|
+
return { e, s, pinned: e.pinned }
|
|
296
|
+
})
|
|
297
|
+
.sort((a, b) => {
|
|
298
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
|
|
299
|
+
return b.s - a.s
|
|
286
300
|
})
|
|
287
|
-
.sort((a, b) => b.s - a.s)
|
|
288
301
|
.slice(0, opts.limit ?? 6)
|
|
289
302
|
)
|
|
290
303
|
}
|
|
291
304
|
|
|
292
305
|
selected = scored.map((x) => x.e)
|
|
293
306
|
|
|
307
|
+
// Inject pinned entries that aren't already in the result set, always at top.
|
|
308
|
+
const pinnedKeys = new Set(entries.filter((e) => e.pinned).map((e) => e.key))
|
|
309
|
+
const inResult = new Set(selected.map((e) => e.key))
|
|
310
|
+
for (const pKey of pinnedKeys) {
|
|
311
|
+
if (!inResult.has(pKey) && byKey.has(pKey)) {
|
|
312
|
+
const e = byKey.get(pKey)!
|
|
313
|
+
selected.unshift(e)
|
|
314
|
+
scored.unshift({ e, s: 999 })
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
294
318
|
// Restrict adjacency to the selected entries for compact edge rendering.
|
|
295
319
|
const subAdj = new Map<string, string[]>()
|
|
296
320
|
for (const e of selected) {
|
|
@@ -348,6 +372,7 @@ export function renderCompact(entries: GraphEntry[], opts: RenderCompactOpts = {
|
|
|
348
372
|
if (!isSeed && e.content.length > snippetLen) {
|
|
349
373
|
body = e.content.slice(0, snippetLen).trimEnd() + "…"
|
|
350
374
|
}
|
|
375
|
+
const tag = e.pinned ? " 📌" : ""
|
|
351
376
|
const tags = e.tags.length ? ` · tags: ${e.tags.join(";")}` : ""
|
|
352
377
|
let edges = ""
|
|
353
378
|
if (opts.adjacency) {
|
|
@@ -356,7 +381,7 @@ export function renderCompact(entries: GraphEntry[], opts: RenderCompactOpts = {
|
|
|
356
381
|
.filter((x): x is number => typeof x === "number")
|
|
357
382
|
if (nb.length) edges = ` · edges: ->${nb.join(", ->")}`
|
|
358
383
|
}
|
|
359
|
-
return `[${n}] ${e.category}/${e.key}\n ${body}${tags}${edges}`
|
|
384
|
+
return `[${n}] ${e.category}/${e.key}${tag}\n ${body}${tags}${edges}`
|
|
360
385
|
})
|
|
361
386
|
.join("\n\n")
|
|
362
387
|
}
|
package/src/lib/quality.ts
CHANGED
|
@@ -192,16 +192,32 @@ export function generateSmartRecall(
|
|
|
192
192
|
}
|
|
193
193
|
const combined =
|
|
194
194
|
bm25Score + 0.3 * centScore + 0.3 * impScore + (matchesQuery ? 0.5 : 0) - drift
|
|
195
|
-
return { entry: e, score: combined, matchesQuery }
|
|
195
|
+
return { entry: e, score: combined, matchesQuery, pinned: e.pinned }
|
|
196
196
|
})
|
|
197
197
|
.filter((x) => x.score > 0 || x.matchesQuery)
|
|
198
|
-
.sort((a, b) =>
|
|
198
|
+
.sort((a, b) => {
|
|
199
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
|
|
200
|
+
return b.score - a.score
|
|
201
|
+
})
|
|
199
202
|
.slice(0, limit)
|
|
200
203
|
|
|
204
|
+
// Inject pinned entries not already in the result set.
|
|
205
|
+
const pinnedKeys = new Set(entries.filter((e) => e.pinned).map((e) => e.key))
|
|
206
|
+
const inResult = new Set(scored.map((x) => x.entry.key))
|
|
207
|
+
for (const pKey of pinnedKeys) {
|
|
208
|
+
if (!inResult.has(pKey)) {
|
|
209
|
+
const e = entries.find((en) => en.key === pKey)
|
|
210
|
+
if (e) scored.unshift({ entry: e, score: 999, matchesQuery: false, pinned: true })
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
201
214
|
if (scored.length === 0) {
|
|
202
215
|
const top = entries
|
|
203
216
|
.filter((e) => !category || e.category === category)
|
|
204
|
-
.sort((a, b) =>
|
|
217
|
+
.sort((a, b) => {
|
|
218
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
|
|
219
|
+
return importance(b) - importance(a)
|
|
220
|
+
})
|
|
205
221
|
.slice(0, limit)
|
|
206
222
|
if (top.length === 0) return "No entries in memory."
|
|
207
223
|
return renderCompact(top)
|
|
@@ -232,10 +248,13 @@ export function generateSystemPrimer(data: string): string {
|
|
|
232
248
|
"",
|
|
233
249
|
]
|
|
234
250
|
|
|
235
|
-
//
|
|
251
|
+
// Pinned entries first, then top by importance
|
|
236
252
|
const top = [...entries]
|
|
237
253
|
.filter((e) => !isPrivate(e))
|
|
238
|
-
.sort((a, b) =>
|
|
254
|
+
.sort((a, b) => {
|
|
255
|
+
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
|
|
256
|
+
return importance(b) - importance(a)
|
|
257
|
+
})
|
|
239
258
|
.slice(0, 5)
|
|
240
259
|
lines.push("Top memories:")
|
|
241
260
|
for (const e of top) {
|
|
@@ -248,8 +267,9 @@ export function generateSystemPrimer(data: string): string {
|
|
|
248
267
|
e.lastAccessed
|
|
249
268
|
)
|
|
250
269
|
const conf = quality >= 0.5 ? "high" : quality >= 0.3 ? "medium" : "low"
|
|
270
|
+
const pin = e.pinned ? " 📌" : ""
|
|
251
271
|
lines.push(
|
|
252
|
-
` [${e.category}] ${e.key} — ${e.content.slice(0, 80)} (quality: ${conf})`
|
|
272
|
+
` [${e.category}] ${e.key}${pin} — ${e.content.slice(0, 80)} (quality: ${conf})`
|
|
253
273
|
)
|
|
254
274
|
}
|
|
255
275
|
lines.push("")
|
package/src/mcp/config.ts
CHANGED
|
@@ -98,6 +98,6 @@ export function ensureMemoryDir(): void {
|
|
|
98
98
|
export function ensureMemoryFile(): void {
|
|
99
99
|
ensureMemoryDir()
|
|
100
100
|
if (!existsSync(MEMORY_FILE)) {
|
|
101
|
-
writeFileSync(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence}:\n")
|
|
101
|
+
writeFileSync(MEMORY_FILE, "version: 1\nentries[0|]{id|category|key|content|file|tags|date|ttl|accessed|links|quality|confidence|lastAccessed|pinned}:\n")
|
|
102
102
|
}
|
|
103
103
|
}
|
package/src/mcp/tools.ts
CHANGED
|
@@ -1551,6 +1551,239 @@ server.registerTool(
|
|
|
1551
1551
|
}
|
|
1552
1552
|
)
|
|
1553
1553
|
|
|
1554
|
+
// ── memory_pin ───────────────────────────────────────────────────────────────
|
|
1555
|
+
|
|
1556
|
+
server.registerTool(
|
|
1557
|
+
"memory_pin",
|
|
1558
|
+
{
|
|
1559
|
+
title: "Pin Entry",
|
|
1560
|
+
description: "Pin a memory entry so it always appears first in recalls. Useful for critical decisions, project rules, or frequently-needed context.",
|
|
1561
|
+
inputSchema: {
|
|
1562
|
+
key: z.string().describe("Key or id of the entry to pin"),
|
|
1563
|
+
},
|
|
1564
|
+
},
|
|
1565
|
+
async ({ key }) => {
|
|
1566
|
+
const data = readMemory()
|
|
1567
|
+
const lines = data.split("\n")
|
|
1568
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
1569
|
+
|
|
1570
|
+
if (headerIdx === -1) {
|
|
1571
|
+
return { content: [{ type: "text" as const, text: "No entries in memory" }] }
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
let found = false
|
|
1575
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
1576
|
+
const line = lines[i]
|
|
1577
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
1578
|
+
if (line.startsWith(" summaries:")) break
|
|
1579
|
+
const parts = line.trim().split("|")
|
|
1580
|
+
if (parts[0] === key || parts[2] === key) {
|
|
1581
|
+
// Set pinned field at position 13
|
|
1582
|
+
while (parts.length < 14) parts.push("")
|
|
1583
|
+
parts[13] = "1"
|
|
1584
|
+
lines[i] = ` ${parts.join("|")}`
|
|
1585
|
+
found = true
|
|
1586
|
+
break
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
if (!found) {
|
|
1591
|
+
return { content: [{ type: "text" as const, text: `"${key}" not found in memory.` }] }
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
writeMemory(lines.join("\n"))
|
|
1595
|
+
return { content: [{ type: "text" as const, text: `📌 Pinned "${key}". It will now appear first in recalls.` }] }
|
|
1596
|
+
}
|
|
1597
|
+
)
|
|
1598
|
+
|
|
1599
|
+
// ── memory_unpin ─────────────────────────────────────────────────────────────
|
|
1600
|
+
|
|
1601
|
+
server.registerTool(
|
|
1602
|
+
"memory_unpin",
|
|
1603
|
+
{
|
|
1604
|
+
title: "Unpin Entry",
|
|
1605
|
+
description: "Remove the pin from a memory entry so it returns to normal ranking.",
|
|
1606
|
+
inputSchema: {
|
|
1607
|
+
key: z.string().describe("Key or id of the entry to unpin"),
|
|
1608
|
+
},
|
|
1609
|
+
},
|
|
1610
|
+
async ({ key }) => {
|
|
1611
|
+
const data = readMemory()
|
|
1612
|
+
const lines = data.split("\n")
|
|
1613
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
1614
|
+
|
|
1615
|
+
if (headerIdx === -1) {
|
|
1616
|
+
return { content: [{ type: "text" as const, text: "No entries in memory" }] }
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
let found = false
|
|
1620
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
1621
|
+
const line = lines[i]
|
|
1622
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
1623
|
+
if (line.startsWith(" summaries:")) break
|
|
1624
|
+
const parts = line.trim().split("|")
|
|
1625
|
+
if (parts[0] === key || parts[2] === key) {
|
|
1626
|
+
if (parts.length > 13) {
|
|
1627
|
+
parts[13] = "0"
|
|
1628
|
+
lines[i] = ` ${parts.join("|")}`
|
|
1629
|
+
}
|
|
1630
|
+
found = true
|
|
1631
|
+
break
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
if (!found) {
|
|
1636
|
+
return { content: [{ type: "text" as const, text: `"${key}" not found in memory.` }] }
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
writeMemory(lines.join("\n"))
|
|
1640
|
+
return { content: [{ type: "text" as const, text: `📌 Unpinned "${key}". It will now use normal ranking.` }] }
|
|
1641
|
+
}
|
|
1642
|
+
)
|
|
1643
|
+
|
|
1644
|
+
// ── memory_search ────────────────────────────────────────────────────────────
|
|
1645
|
+
|
|
1646
|
+
server.registerTool(
|
|
1647
|
+
"memory_search",
|
|
1648
|
+
{
|
|
1649
|
+
title: "Unified Search",
|
|
1650
|
+
description: "Search memory with combined filters: text query, category, tags, and date range. Returns ranked results.",
|
|
1651
|
+
inputSchema: {
|
|
1652
|
+
query: z.string().describe("Text to search for"),
|
|
1653
|
+
category: z.string().optional().default("").describe("Filter by category (empty = all)"),
|
|
1654
|
+
tags: z.string().optional().default("").describe("Semicolon-separated tags; entry must have ALL specified tags"),
|
|
1655
|
+
from_date: z.string().optional().default("").describe("Start date filter (YYYY-MM-DD or relative like 7d)"),
|
|
1656
|
+
to_date: z.string().optional().default("").describe("End date filter (YYYY-MM-DD)"),
|
|
1657
|
+
limit: z.number().optional().default(6).describe("Maximum entries to return"),
|
|
1658
|
+
mode: z.enum(["flat", "graph"]).optional().default("flat").describe("'flat' = keyword search (default). 'graph' = graph-based recall."),
|
|
1659
|
+
hops: z.number().optional().default(1).describe("Graph depth in 'graph' mode (1 or 2). Default 1."),
|
|
1660
|
+
compact: z.boolean().optional().default(false).describe("Token-efficient output"),
|
|
1661
|
+
},
|
|
1662
|
+
},
|
|
1663
|
+
async ({ query, category, tags, from_date, to_date, limit, mode, hops, compact }) => {
|
|
1664
|
+
const data = readMemory()
|
|
1665
|
+
|
|
1666
|
+
const resolvedFrom = from_date ? parseRelativeDate(from_date) : ""
|
|
1667
|
+
const detail = graphRecallDetailed(data, query, {
|
|
1668
|
+
category: category || undefined,
|
|
1669
|
+
tags: tags || undefined,
|
|
1670
|
+
from_date: resolvedFrom || undefined,
|
|
1671
|
+
to_date: to_date || undefined,
|
|
1672
|
+
hops,
|
|
1673
|
+
limit,
|
|
1674
|
+
})
|
|
1675
|
+
|
|
1676
|
+
if (detail.entries.length === 0) {
|
|
1677
|
+
return { content: [{ type: "text" as const, text: `No results found for "${query}"` }] }
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
bumpAccessed(detail.entries.map((e) => e.id))
|
|
1681
|
+
|
|
1682
|
+
if (compact) {
|
|
1683
|
+
const formatted = renderCompact(detail.entries, {
|
|
1684
|
+
adjacency: detail.adjacency,
|
|
1685
|
+
seeds: detail.seeds,
|
|
1686
|
+
snippetLen: 90,
|
|
1687
|
+
})
|
|
1688
|
+
return { content: [{ type: "text" as const, text: `🔍 Search: "${query}"\n\n${formatted}` }] }
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
const formatted = detail.entries
|
|
1692
|
+
.map((r) => {
|
|
1693
|
+
const links = r.links.length ? `\n links: ${r.links.join(", ")}` : ""
|
|
1694
|
+
const pin = r.pinned ? " 📌" : ""
|
|
1695
|
+
return `[${r.category}] ${r.key}${pin} (${r.id})\n ${r.content}\n File: ${r.file} | Tags: ${r.tags.join(";")} | Date: ${r.date}${links}`
|
|
1696
|
+
})
|
|
1697
|
+
.join("\n\n")
|
|
1698
|
+
|
|
1699
|
+
let filterInfo = ""
|
|
1700
|
+
if (category) filterInfo += ` category: ${category}`
|
|
1701
|
+
if (tags) filterInfo += ` tags: ${tags}`
|
|
1702
|
+
if (from_date) filterInfo += ` from: ${from_date}`
|
|
1703
|
+
if (to_date) filterInfo += ` to: ${to_date}`
|
|
1704
|
+
|
|
1705
|
+
return {
|
|
1706
|
+
content: [{ type: "text" as const, text: `🔍 Search results${filterInfo}:\n\n${formatted}` }],
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
)
|
|
1710
|
+
|
|
1711
|
+
// ── memory_tag ───────────────────────────────────────────────────────────────
|
|
1712
|
+
|
|
1713
|
+
server.registerTool(
|
|
1714
|
+
"memory_tag",
|
|
1715
|
+
{
|
|
1716
|
+
title: "Batch Tag Operations",
|
|
1717
|
+
description: "Batch add, remove, or set tags on one or more memory entries by key or id.",
|
|
1718
|
+
inputSchema: {
|
|
1719
|
+
action: z.enum(["add", "remove", "set"]).describe("'add': merge tags (union). 'remove': remove specified tags. 'set': replace all tags."),
|
|
1720
|
+
tags: z.string().describe("Semicolon-separated tags to apply"),
|
|
1721
|
+
ids: z.string().describe("Comma or space-separated entry keys or IDs to modify"),
|
|
1722
|
+
},
|
|
1723
|
+
},
|
|
1724
|
+
async ({ action, tags, ids }) => {
|
|
1725
|
+
const data = readMemory()
|
|
1726
|
+
const lines = data.split("\n")
|
|
1727
|
+
const headerIdx = lines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l))
|
|
1728
|
+
|
|
1729
|
+
if (headerIdx === -1) {
|
|
1730
|
+
return { content: [{ type: "text" as const, text: "No entries in memory." }] }
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
const targetIds = ids.split(/[\s,]+/).filter(Boolean)
|
|
1734
|
+
const targetSet = new Set(targetIds)
|
|
1735
|
+
const tagsToApply = tags.split(";").map((t) => t.trim()).filter(Boolean)
|
|
1736
|
+
|
|
1737
|
+
if (tagsToApply.length === 0) {
|
|
1738
|
+
return { content: [{ type: "text" as const, text: "No tags provided." }] }
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
let modified = 0
|
|
1742
|
+
const modifiedKeys: string[] = []
|
|
1743
|
+
|
|
1744
|
+
for (let i = headerIdx + 1; i < lines.length; i++) {
|
|
1745
|
+
const line = lines[i]
|
|
1746
|
+
if (!line.startsWith(" ") || !line.includes("|")) continue
|
|
1747
|
+
if (line.startsWith(" summaries:")) break
|
|
1748
|
+
const parts = line.trim().split("|")
|
|
1749
|
+
const entryId = parts[0]
|
|
1750
|
+
const entryKey = parts[2]
|
|
1751
|
+
|
|
1752
|
+
if (!targetSet.has(entryId) && !targetSet.has(entryKey)) continue
|
|
1753
|
+
|
|
1754
|
+
const currentTags = (parts[5] || "").split(";").map((t) => t.trim()).filter(Boolean)
|
|
1755
|
+
let newTags: string[]
|
|
1756
|
+
|
|
1757
|
+
if (action === "add") {
|
|
1758
|
+
newTags = [...new Set([...currentTags, ...tagsToApply])]
|
|
1759
|
+
} else if (action === "remove") {
|
|
1760
|
+
newTags = currentTags.filter((t) => !tagsToApply.includes(t))
|
|
1761
|
+
} else {
|
|
1762
|
+
newTags = [...tagsToApply]
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
parts[5] = newTags.join(";")
|
|
1766
|
+
lines[i] = ` ${parts.join("|")}`
|
|
1767
|
+
modified++
|
|
1768
|
+
modifiedKeys.push(entryKey)
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
if (modified === 0) {
|
|
1772
|
+
return { content: [{ type: "text" as const, text: `No entries found matching: ${targetIds.join(", ")}` }] }
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
writeMemory(lines.join("\n"))
|
|
1776
|
+
|
|
1777
|
+
const actionLabel = action === "add" ? "Added" : action === "remove" ? "Removed" : "Set"
|
|
1778
|
+
return {
|
|
1779
|
+
content: [{
|
|
1780
|
+
type: "text" as const,
|
|
1781
|
+
text: `🏷️ ${actionLabel} tags on ${modified} entries:\n Tags: ${tagsToApply.join(";")}\n Entries: ${modifiedKeys.join(", ")}`,
|
|
1782
|
+
}],
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
)
|
|
1786
|
+
|
|
1554
1787
|
// ── memory_visualize (MCP Apps) ──────────────────────────────────────────────
|
|
1555
1788
|
|
|
1556
1789
|
registerAppTool(
|