toon-memory 3.0.0 → 3.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.de.md +7 -5
- package/README.es.md +7 -5
- package/README.fr.md +7 -5
- package/README.ja.md +7 -5
- package/README.ko.md +7 -5
- package/README.md +140 -9
- package/README.pt-br.md +7 -5
- package/README.zh.md +7 -5
- package/dist/cli/setup.js +1356 -28
- package/mcp/server.js +173 -2
- package/package.json +6 -2
- package/src/cli/agents.ts +10 -9
- package/src/cli/commands.ts +1 -1
- package/src/cli/config-writer.ts +90 -3
- package/src/cli/hooks.ts +2 -2
- package/src/cli/opencode-plugin.ts +58 -11
- package/src/cli/setup.ts +16 -7
- package/src/cli/types.ts +4 -2
- package/src/lib/checkpoint.ts +132 -0
- package/src/lib/graph.ts +16 -0
- package/src/lib/quality.ts +18 -1
- package/src/mcp/config.ts +5 -0
- package/src/mcp/prompts.ts +55 -0
- package/src/mcp/session-store.ts +276 -0
- package/src/mcp/tools.ts +177 -1
- package/src/viewer/data.ts +49 -0
- package/src/viewer/html.ts +880 -0
- package/src/viewer/index.ts +76 -0
- package/src/viewer/server.ts +1 -0
- package/src/viewer/types.ts +29 -0
package/mcp/server.js
CHANGED
|
@@ -226,6 +226,20 @@ function buildGraph(entries) {
|
|
|
226
226
|
const refs = e.content.match(/\[\[([\w-]+)\]\]/g) || [];
|
|
227
227
|
for (const r of refs) link(e.key, r.slice(2, -2));
|
|
228
228
|
}
|
|
229
|
+
if (entries.some((e) => e.tags.length > 0)) {
|
|
230
|
+
for (let i = 0; i < entries.length; i++) {
|
|
231
|
+
const ei = entries[i];
|
|
232
|
+
if (ei.tags.length === 0) continue;
|
|
233
|
+
const eTags = new Set(ei.tags);
|
|
234
|
+
for (let j = i + 1; j < entries.length; j++) {
|
|
235
|
+
const ej = entries[j];
|
|
236
|
+
if (ej.tags.length === 0) continue;
|
|
237
|
+
let shared = 0;
|
|
238
|
+
for (const t of ej.tags) if (eTags.has(t)) shared++;
|
|
239
|
+
if (shared >= 2) link(ei.key, ej.key);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
229
243
|
const adjacency = /* @__PURE__ */ new Map();
|
|
230
244
|
for (const [k, v] of adj) adjacency.set(k, [...v]);
|
|
231
245
|
return { adjacency, byKey };
|
|
@@ -418,7 +432,15 @@ function qualityScore(tags, links, content, date5, accessed = 0, lastAccessed =
|
|
|
418
432
|
else if (daysSinceAccess < 7) score += 0.07;
|
|
419
433
|
else if (daysSinceAccess < 30) score += 0.03;
|
|
420
434
|
}
|
|
421
|
-
|
|
435
|
+
if (date5) {
|
|
436
|
+
const daysSinceCreation = (Date.now() - (/* @__PURE__ */ new Date(`${date5}T00:00:00`)).getTime()) / 864e5;
|
|
437
|
+
const daysSinceLastAccess = lastAccessed ? (Date.now() - new Date(lastAccessed).getTime()) / 864e5 : daysSinceCreation;
|
|
438
|
+
if (daysSinceCreation > 30 && daysSinceLastAccess > 30) {
|
|
439
|
+
const staleness = Math.min(1, (daysSinceLastAccess - 30) / 90);
|
|
440
|
+
score -= staleness * 0.2;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return Math.max(0, Math.min(1, score));
|
|
422
444
|
}
|
|
423
445
|
function mergeEntries(existingLine, newLine) {
|
|
424
446
|
const ep = existingLine.trim().split("|");
|
|
@@ -31351,6 +31373,155 @@ To import later: memory_import_gist(gist_id: "${gist.id}")`
|
|
|
31351
31373
|
return { content: [{ type: "text", text: sections.join("\n") }] };
|
|
31352
31374
|
}
|
|
31353
31375
|
);
|
|
31376
|
+
server2.registerTool(
|
|
31377
|
+
"memory_merge_similar",
|
|
31378
|
+
{
|
|
31379
|
+
title: "Merge Similar Entries",
|
|
31380
|
+
description: "Find entries with overlapping content (>50% word similarity) and merge them. Deterministic, no LLM. Keeps the longer content and combines tags.",
|
|
31381
|
+
inputSchema: {
|
|
31382
|
+
dryRun: external_exports.boolean().optional().default(false).describe("If true, show what would be merged without making changes.")
|
|
31383
|
+
}
|
|
31384
|
+
},
|
|
31385
|
+
async ({ dryRun }) => {
|
|
31386
|
+
const data = readMemory();
|
|
31387
|
+
const entries = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:"));
|
|
31388
|
+
if (entries.length < 2) {
|
|
31389
|
+
return { content: [{ type: "text", text: "\u2705 Not enough entries to compress." }] };
|
|
31390
|
+
}
|
|
31391
|
+
const parsed = entries.map((line) => {
|
|
31392
|
+
const parts = line.trim().split("|");
|
|
31393
|
+
if (parts.length < 7) return null;
|
|
31394
|
+
const [id, cat, key, content, file2, tags, date5, ttl, accessedRaw] = parts;
|
|
31395
|
+
const words = new Set(normalize(`${key} ${content} ${tags}`).split(" ").filter(Boolean));
|
|
31396
|
+
return { id, cat, key, content, file: file2, tags, date: date5, ttl, accessed: parseInt(accessedRaw) || 0, words, line };
|
|
31397
|
+
}).filter(Boolean);
|
|
31398
|
+
const mergePairs = [];
|
|
31399
|
+
for (let i = 0; i < parsed.length; i++) {
|
|
31400
|
+
for (let j = i + 1; j < parsed.length; j++) {
|
|
31401
|
+
const a = parsed[i], b = parsed[j];
|
|
31402
|
+
const intersection2 = new Set([...a.words].filter((w) => b.words.has(w)));
|
|
31403
|
+
const union2 = /* @__PURE__ */ new Set([...a.words, ...b.words]);
|
|
31404
|
+
const similarity = union2.size > 0 ? intersection2.size / union2.size : 0;
|
|
31405
|
+
if (similarity > 0.5) {
|
|
31406
|
+
mergePairs.push({ a: i, b: j, similarity });
|
|
31407
|
+
}
|
|
31408
|
+
}
|
|
31409
|
+
}
|
|
31410
|
+
if (mergePairs.length === 0) {
|
|
31411
|
+
return { content: [{ type: "text", text: "\u2705 No similar entries found to merge." }] };
|
|
31412
|
+
}
|
|
31413
|
+
mergePairs.sort((x, y) => y.similarity - x.similarity);
|
|
31414
|
+
const merged = /* @__PURE__ */ new Set();
|
|
31415
|
+
const mergeActions = [];
|
|
31416
|
+
for (const pair of mergePairs) {
|
|
31417
|
+
if (merged.has(pair.a) || merged.has(pair.b)) continue;
|
|
31418
|
+
const a = parsed[pair.a], b = parsed[pair.b];
|
|
31419
|
+
const mergedTags = [.../* @__PURE__ */ new Set([...a.tags.split(";"), ...b.tags.split(";")])].filter(Boolean).join(";");
|
|
31420
|
+
const longer = a.content.length >= b.content.length ? a : b;
|
|
31421
|
+
const shorter = a.content.length < b.content.length ? a : b;
|
|
31422
|
+
const newContent = longer.content;
|
|
31423
|
+
const newDate = a.date > b.date ? a.date : b.date;
|
|
31424
|
+
const newAccessed = a.accessed + b.accessed;
|
|
31425
|
+
mergeActions.push(` [${longer.cat}] ${longer.key} \u2190 ${shorter.key} (similarity: ${(pair.similarity * 100).toFixed(0)}%)`);
|
|
31426
|
+
if (!dryRun) {
|
|
31427
|
+
const mergedLine = ` ${longer.id}|${longer.cat}|${longer.key}|${newContent}|${longer.file}|${mergedTags}|${newDate}|${longer.ttl}|${newAccessed}|${longer.line.split("|")[9] || ""}|${longer.line.split("|")[10] || ""}|${longer.line.split("|")[11] || ""}|${longer.line.split("|")[12] || ""}`;
|
|
31428
|
+
entries[entries.indexOf(longer.line)] = mergedLine;
|
|
31429
|
+
merged.add(pair.b);
|
|
31430
|
+
}
|
|
31431
|
+
}
|
|
31432
|
+
if (dryRun) {
|
|
31433
|
+
return {
|
|
31434
|
+
content: [{
|
|
31435
|
+
type: "text",
|
|
31436
|
+
text: `\u{1F50D} Dry run \u2014 would merge ${merged.size} entries:
|
|
31437
|
+
|
|
31438
|
+
${mergeActions.join("\n")}
|
|
31439
|
+
|
|
31440
|
+
Run with dryRun: false to apply.`
|
|
31441
|
+
}]
|
|
31442
|
+
};
|
|
31443
|
+
}
|
|
31444
|
+
const newLines = entries.filter((_, i) => !merged.has(i));
|
|
31445
|
+
const headerIdx = newLines.findIndex((l) => l.startsWith("entries[") || /^\[\d+\|]/.test(l));
|
|
31446
|
+
if (headerIdx !== -1) {
|
|
31447
|
+
const count = newLines.filter((l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:")).length;
|
|
31448
|
+
newLines[headerIdx] = newLines[headerIdx].replace(/\[\d+\|/, `[${count}|`);
|
|
31449
|
+
}
|
|
31450
|
+
writeMemory(newLines.join("\n"));
|
|
31451
|
+
return {
|
|
31452
|
+
content: [{
|
|
31453
|
+
type: "text",
|
|
31454
|
+
text: `\u2705 Merged ${merged.size} entries into ${entries.length - merged.size} entries.
|
|
31455
|
+
|
|
31456
|
+
${mergeActions.join("\n")}`
|
|
31457
|
+
}]
|
|
31458
|
+
};
|
|
31459
|
+
}
|
|
31460
|
+
);
|
|
31461
|
+
server2.registerTool(
|
|
31462
|
+
"memory_graph_path",
|
|
31463
|
+
{
|
|
31464
|
+
title: "Find Graph Path",
|
|
31465
|
+
description: "Find the shortest path between two memory entries in the knowledge graph. Shows how two concepts are connected through related entries.",
|
|
31466
|
+
inputSchema: {
|
|
31467
|
+
from: external_exports.string().describe("Starting entry key"),
|
|
31468
|
+
to: external_exports.string().describe("Target entry key")
|
|
31469
|
+
}
|
|
31470
|
+
},
|
|
31471
|
+
async ({ from, to }) => {
|
|
31472
|
+
const data = readMemory();
|
|
31473
|
+
const entries = parseEntries(data);
|
|
31474
|
+
const graph = buildGraph(entries);
|
|
31475
|
+
if (!graph.byKey.has(from)) {
|
|
31476
|
+
return { content: [{ type: "text", text: `\u274C Entry "${from}" not found in memory.` }] };
|
|
31477
|
+
}
|
|
31478
|
+
if (!graph.byKey.has(to)) {
|
|
31479
|
+
return { content: [{ type: "text", text: `\u274C Entry "${to}" not found in memory.` }] };
|
|
31480
|
+
}
|
|
31481
|
+
const visited = /* @__PURE__ */ new Set();
|
|
31482
|
+
const parent = /* @__PURE__ */ new Map();
|
|
31483
|
+
const queue = [from];
|
|
31484
|
+
visited.add(from);
|
|
31485
|
+
while (queue.length > 0) {
|
|
31486
|
+
const current2 = queue.shift();
|
|
31487
|
+
if (current2 === to) break;
|
|
31488
|
+
for (const neighbor of graph.adjacency.get(current2) || []) {
|
|
31489
|
+
if (!visited.has(neighbor)) {
|
|
31490
|
+
visited.add(neighbor);
|
|
31491
|
+
parent.set(neighbor, current2);
|
|
31492
|
+
queue.push(neighbor);
|
|
31493
|
+
}
|
|
31494
|
+
}
|
|
31495
|
+
}
|
|
31496
|
+
if (!visited.has(to)) {
|
|
31497
|
+
return {
|
|
31498
|
+
content: [{
|
|
31499
|
+
type: "text",
|
|
31500
|
+
text: `\u{1F517} No path found between "${from}" and "${to}". They may be in disconnected parts of the memory graph.`
|
|
31501
|
+
}]
|
|
31502
|
+
};
|
|
31503
|
+
}
|
|
31504
|
+
const path = [];
|
|
31505
|
+
let current = to;
|
|
31506
|
+
while (current !== null) {
|
|
31507
|
+
path.unshift(current);
|
|
31508
|
+
current = parent.get(current) || null;
|
|
31509
|
+
}
|
|
31510
|
+
const pathStr = path.map((key, i) => {
|
|
31511
|
+
const entry = graph.byKey.get(key);
|
|
31512
|
+
const arrow = i < path.length - 1 ? ` \u2192 ` : "";
|
|
31513
|
+
return `[${entry.category}] ${key}${arrow}`;
|
|
31514
|
+
}).join("\n ");
|
|
31515
|
+
return {
|
|
31516
|
+
content: [{
|
|
31517
|
+
type: "text",
|
|
31518
|
+
text: `\u{1F517} Path (${path.length} hops):
|
|
31519
|
+
|
|
31520
|
+
${pathStr}`
|
|
31521
|
+
}]
|
|
31522
|
+
};
|
|
31523
|
+
}
|
|
31524
|
+
);
|
|
31354
31525
|
}
|
|
31355
31526
|
function parseRelativeMs(s) {
|
|
31356
31527
|
const match = s.match(/^(\d+)(h|d|m)$/i);
|
|
@@ -31424,7 +31595,7 @@ function registerResources(server2) {
|
|
|
31424
31595
|
|
|
31425
31596
|
// src/mcp/server.ts
|
|
31426
31597
|
var server = new McpServer(
|
|
31427
|
-
{ name: "toon-memory", version: "3.
|
|
31598
|
+
{ name: "toon-memory", version: "3.2.0" },
|
|
31428
31599
|
{ capabilities: { tools: { listChanged: true }, resources: { listChanged: true } } }
|
|
31429
31600
|
);
|
|
31430
31601
|
registerResources(server);
|
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.2.0",
|
|
4
|
+
"description": "MCP server giving AI coding agents offline, token-efficient persistent memory — 29 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"
|
|
@@ -27,6 +27,9 @@
|
|
|
27
27
|
"bench:impact": "node scripts/benchmark-impact.mjs",
|
|
28
28
|
"bench:context": "node scripts/bench-context-tools.mjs",
|
|
29
29
|
"bench:full": "node scripts/bench-full-impact.mjs",
|
|
30
|
+
"bench:compress-all": "node scripts/bench-compress-all.mjs",
|
|
31
|
+
"bench:primer": "node scripts/bench-primer.mjs",
|
|
32
|
+
"capture:viewer": "node scripts/capture-viewer.mjs",
|
|
30
33
|
"inspect": "npm run build && npx @modelcontextprotocol/inspector node bin/toon-memory.js mcp",
|
|
31
34
|
"prepublishOnly": "npm run build && npm test"
|
|
32
35
|
},
|
|
@@ -40,6 +43,7 @@
|
|
|
40
43
|
"@types/node": "^26.1.1",
|
|
41
44
|
"esbuild": "^0.25.0",
|
|
42
45
|
"gpt-tokenizer": "^3.4.0",
|
|
46
|
+
"playwright": "^1.62.0",
|
|
43
47
|
"typescript": "^7.0.2",
|
|
44
48
|
"vitest": "^4.1.10"
|
|
45
49
|
},
|
package/src/cli/agents.ts
CHANGED
|
@@ -58,7 +58,8 @@ export function detectAgents(): Agent[] {
|
|
|
58
58
|
agents.push({
|
|
59
59
|
name: "claude",
|
|
60
60
|
global: join(HOME, ".claude", "settings.json"),
|
|
61
|
-
local: join(projectRoot, ".
|
|
61
|
+
local: join(projectRoot, ".mcp.json"),
|
|
62
|
+
hookPath: join(projectRoot, ".claude", "settings.json"),
|
|
62
63
|
mcpKey: "mcpServers",
|
|
63
64
|
format: "json",
|
|
64
65
|
needsHooks: true,
|
|
@@ -99,8 +100,8 @@ export function detectAgents(): Agent[] {
|
|
|
99
100
|
agents.push({
|
|
100
101
|
name: "continue",
|
|
101
102
|
local: join(projectRoot, ".continue", "config.json"),
|
|
102
|
-
mcpKey: "
|
|
103
|
-
format: "
|
|
103
|
+
mcpKey: "experimental.modelContextProtocolServers",
|
|
104
|
+
format: "continue",
|
|
104
105
|
needsHooks: false,
|
|
105
106
|
needsInstructions: false
|
|
106
107
|
})
|
|
@@ -130,7 +131,7 @@ export function detectAgents(): Agent[] {
|
|
|
130
131
|
agents.push({
|
|
131
132
|
name: "zed",
|
|
132
133
|
global: join(HOME, ".config", "zed", "settings.json"),
|
|
133
|
-
mcpKey: "
|
|
134
|
+
mcpKey: "context_servers",
|
|
134
135
|
format: "jsonc",
|
|
135
136
|
needsHooks: false,
|
|
136
137
|
needsInstructions: false
|
|
@@ -138,7 +139,7 @@ export function detectAgents(): Agent[] {
|
|
|
138
139
|
|
|
139
140
|
agents.push({
|
|
140
141
|
name: "antigravity",
|
|
141
|
-
local: join(projectRoot, ".
|
|
142
|
+
local: join(projectRoot, ".agents", "mcp_config.json"),
|
|
142
143
|
mcpKey: "mcpServers",
|
|
143
144
|
format: "json",
|
|
144
145
|
needsHooks: true,
|
|
@@ -158,7 +159,7 @@ export function detectAgents(): Agent[] {
|
|
|
158
159
|
agents.push({
|
|
159
160
|
name: "kilocode",
|
|
160
161
|
global: join(HOME, ".kilocode", "mcp_settings.json"),
|
|
161
|
-
mcpKey: "
|
|
162
|
+
mcpKey: "mcp",
|
|
162
163
|
format: "json",
|
|
163
164
|
needsHooks: false,
|
|
164
165
|
needsInstructions: true,
|
|
@@ -167,9 +168,9 @@ export function detectAgents(): Agent[] {
|
|
|
167
168
|
|
|
168
169
|
agents.push({
|
|
169
170
|
name: "openclaw",
|
|
170
|
-
|
|
171
|
-
mcpKey: "
|
|
172
|
-
format: "
|
|
171
|
+
global: join(HOME, ".openclaw", "openclaw.json"),
|
|
172
|
+
mcpKey: "mcp.servers",
|
|
173
|
+
format: "openclaw",
|
|
173
174
|
needsHooks: false,
|
|
174
175
|
needsInstructions: false
|
|
175
176
|
})
|
package/src/cli/commands.ts
CHANGED
|
@@ -187,7 +187,7 @@ export function uninstall(): void {
|
|
|
187
187
|
console.log(" Removed .opencode/plugins/toon-memory.ts")
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
const agyHooks = join(projectRoot, ".
|
|
190
|
+
const agyHooks = join(projectRoot, ".agents", "hooks.json")
|
|
191
191
|
if (existsSync(agyHooks)) {
|
|
192
192
|
try {
|
|
193
193
|
const agyCfg = JSON.parse(readFileSync(agyHooks, "utf-8"))
|
package/src/cli/config-writer.ts
CHANGED
|
@@ -35,6 +35,11 @@ export function installJSONConfig(agent: Agent, scope: string): void {
|
|
|
35
35
|
type: "local",
|
|
36
36
|
command: ["npx", "-y", "toon-memory", "mcp"]
|
|
37
37
|
}
|
|
38
|
+
} else if (agent.name === "kilocode") {
|
|
39
|
+
config[mcpKey]["toon-memory"] = {
|
|
40
|
+
type: "local",
|
|
41
|
+
command: ["npx", "-y", "toon-memory", "mcp"]
|
|
42
|
+
}
|
|
38
43
|
} else {
|
|
39
44
|
config[mcpKey]["toon-memory"] = {
|
|
40
45
|
command: "npx",
|
|
@@ -86,19 +91,97 @@ export function installZedConfig(agent: Agent): void {
|
|
|
86
91
|
}
|
|
87
92
|
}
|
|
88
93
|
|
|
89
|
-
if (!config.
|
|
94
|
+
if (!config.context_servers) config.context_servers = {}
|
|
95
|
+
|
|
96
|
+
config.context_servers["toon-memory"] = {
|
|
97
|
+
command: "npx",
|
|
98
|
+
args: ["-y", "toon-memory", "mcp"],
|
|
99
|
+
source: "custom"
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
writeFileSync(configPath, JSON.stringify(config, null, 2))
|
|
103
|
+
console.log(` MCP server added to ${configPath}`)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Install MCP server configuration for Continue (array-based format).
|
|
108
|
+
* Uses experimental.modelContextProtocolServers with name/command/args array.
|
|
109
|
+
*/
|
|
110
|
+
export function installContinueConfig(agent: Agent): void {
|
|
111
|
+
const configPath = agent.local
|
|
112
|
+
if (!configPath) return
|
|
113
|
+
|
|
114
|
+
const configDir = dirname(configPath)
|
|
115
|
+
if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true })
|
|
116
|
+
|
|
117
|
+
let config: Record<string, any> = {}
|
|
118
|
+
if (existsSync(configPath)) {
|
|
119
|
+
try {
|
|
120
|
+
config = JSON.parse(readFileSync(configPath, "utf-8"))
|
|
121
|
+
} catch {
|
|
122
|
+
config = {}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!config.experimental) config.experimental = {}
|
|
127
|
+
if (!config.experimental.modelContextProtocolServers) {
|
|
128
|
+
config.experimental.modelContextProtocolServers = []
|
|
129
|
+
}
|
|
90
130
|
|
|
91
|
-
|
|
131
|
+
const servers = config.experimental.modelContextProtocolServers
|
|
132
|
+
const existing = servers.findIndex(
|
|
133
|
+
(s: any) => s.name === "toon-memory"
|
|
134
|
+
)
|
|
135
|
+
const entry = {
|
|
136
|
+
name: "toon-memory",
|
|
92
137
|
command: "npx",
|
|
93
138
|
args: ["-y", "toon-memory", "mcp"]
|
|
94
139
|
}
|
|
95
140
|
|
|
141
|
+
if (existing >= 0) {
|
|
142
|
+
servers[existing] = entry
|
|
143
|
+
} else {
|
|
144
|
+
servers.push(entry)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
writeFileSync(configPath, JSON.stringify(config, null, 2))
|
|
148
|
+
console.log(` MCP server added to ${configPath}`)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Install MCP server configuration for OpenClaw (nested mcp.servers format).
|
|
153
|
+
*/
|
|
154
|
+
export function installOpenClawConfig(agent: Agent): void {
|
|
155
|
+
const configPath = agent.global
|
|
156
|
+
if (!configPath) return
|
|
157
|
+
|
|
158
|
+
const configDir = dirname(configPath)
|
|
159
|
+
if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true })
|
|
160
|
+
|
|
161
|
+
let config: Record<string, any> = {}
|
|
162
|
+
if (existsSync(configPath)) {
|
|
163
|
+
try {
|
|
164
|
+
config = JSON.parse(readFileSync(configPath, "utf-8"))
|
|
165
|
+
} catch {
|
|
166
|
+
config = {}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!config.mcp) config.mcp = {}
|
|
171
|
+
if (!config.mcp.servers) config.mcp.servers = {}
|
|
172
|
+
|
|
173
|
+
config.mcp.servers["toon-memory"] = {
|
|
174
|
+
command: "npx",
|
|
175
|
+
args: ["-y", "toon-memory", "mcp"],
|
|
176
|
+
enabled: true
|
|
177
|
+
}
|
|
178
|
+
|
|
96
179
|
writeFileSync(configPath, JSON.stringify(config, null, 2))
|
|
97
180
|
console.log(` MCP server added to ${configPath}`)
|
|
98
181
|
}
|
|
99
182
|
|
|
100
183
|
/**
|
|
101
|
-
* Install MCP
|
|
184
|
+
* Install MCP server configuration for an agent based on its format.
|
|
102
185
|
*/
|
|
103
186
|
export function installMCPConfig(agent: Agent, scope: string): void {
|
|
104
187
|
if (agent.format === "none") {
|
|
@@ -110,6 +193,10 @@ export function installMCPConfig(agent: Agent, scope: string): void {
|
|
|
110
193
|
installTOMLConfig(agent)
|
|
111
194
|
} else if (agent.format === "jsonc") {
|
|
112
195
|
installZedConfig(agent)
|
|
196
|
+
} else if (agent.format === "continue") {
|
|
197
|
+
installContinueConfig(agent)
|
|
198
|
+
} else if (agent.format === "openclaw") {
|
|
199
|
+
installOpenClawConfig(agent)
|
|
113
200
|
} else {
|
|
114
201
|
installJSONConfig(agent, scope)
|
|
115
202
|
}
|
package/src/cli/hooks.ts
CHANGED
|
@@ -66,7 +66,7 @@ export function installHooks(agent: Agent): void {
|
|
|
66
66
|
|
|
67
67
|
/** Register activity-capture hooks (PostToolUse/Stop) in a JSON agent config. */
|
|
68
68
|
export function registerCaptureHookJSON(agent: Agent, scriptPath: string): void {
|
|
69
|
-
const configPath = agent.local || agent.global
|
|
69
|
+
const configPath = agent.hookPath || agent.local || agent.global
|
|
70
70
|
if (!configPath) return
|
|
71
71
|
|
|
72
72
|
const configDir = dirname(configPath)
|
|
@@ -126,7 +126,7 @@ export function registerHookTOML(agent: Agent, hookPath: string): void {
|
|
|
126
126
|
|
|
127
127
|
/** Register SessionStart hook in JSON config (Claude Code / Gemini CLI). */
|
|
128
128
|
export function registerHookJSON(agent: Agent, hookPath: string): void {
|
|
129
|
-
const configPath = agent.format === "jsonc" ? agent.global : (agent.local || agent.global)
|
|
129
|
+
const configPath = agent.hookPath || (agent.format === "jsonc" ? agent.global : (agent.local || agent.global))
|
|
130
130
|
if (!configPath) return
|
|
131
131
|
|
|
132
132
|
const configDir = dirname(configPath)
|
|
@@ -5,24 +5,47 @@ import { projectRoot } from "./constants"
|
|
|
5
5
|
import { dumpMemoryMarkdown } from "./memory"
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* OpenCode plugin source with
|
|
9
|
-
* hooks via plugins (not a top-level `hooks` config key), so `init`
|
|
10
|
-
* file into `.opencode/plugins/`.
|
|
8
|
+
* OpenCode plugin source with INTELLIGENT memory injection. OpenCode 1.17+
|
|
9
|
+
* delivers hooks via plugins (not a top-level `hooks` config key), so `init`
|
|
10
|
+
* writes this file into `.opencode/plugins/`.
|
|
11
|
+
*
|
|
12
|
+
* Two injection modes:
|
|
13
|
+
* 1. Session start → full dump (seeded in instructions/)
|
|
14
|
+
* 2. Tool execution → targeted recall by file path (injected via output.context)
|
|
11
15
|
*/
|
|
12
16
|
export function opencodePluginContent(): string {
|
|
13
17
|
return `import * as fs from "fs"
|
|
18
|
+
import * as path from "path"
|
|
14
19
|
|
|
15
20
|
export const ToonMemory = async ({ $, directory, worktree }) => {
|
|
16
21
|
const root = worktree || directory
|
|
17
22
|
const INS = root + "/.opencode/instructions"
|
|
18
23
|
const OUT = INS + "/memory-autoload.md"
|
|
24
|
+
|
|
19
25
|
const dump = async () => {
|
|
20
26
|
return await $\`npx -y toon-memory dump\`.cwd(root).text()
|
|
21
27
|
}
|
|
28
|
+
|
|
29
|
+
const recall = async (query) => {
|
|
30
|
+
try {
|
|
31
|
+
const result = await $\`npx -y toon-memory mcp\`.cwd(root)
|
|
32
|
+
.stdin(JSON.stringify({
|
|
33
|
+
jsonrpc: "2.0",
|
|
34
|
+
id: 1,
|
|
35
|
+
method: "tools/call",
|
|
36
|
+
params: { name: "memory_recall", arguments: { query, mode: "flat", compact: true } }
|
|
37
|
+
}))
|
|
38
|
+
.text()
|
|
39
|
+
const parsed = JSON.parse(result)
|
|
40
|
+
return parsed?.result?.content?.[0]?.text || ""
|
|
41
|
+
} catch { return "" }
|
|
42
|
+
}
|
|
43
|
+
|
|
22
44
|
const write = (text) => {
|
|
23
45
|
fs.mkdirSync(INS, { recursive: true })
|
|
24
46
|
fs.writeFileSync(OUT, text)
|
|
25
47
|
}
|
|
48
|
+
|
|
26
49
|
return {
|
|
27
50
|
"session.created": async () => {
|
|
28
51
|
try {
|
|
@@ -37,15 +60,39 @@ export const ToonMemory = async ({ $, directory, worktree }) => {
|
|
|
37
60
|
} catch {}
|
|
38
61
|
},
|
|
39
62
|
"tool.execute.after": async (input) => {
|
|
40
|
-
|
|
41
|
-
|
|
63
|
+
// Intelligent auto-loading: extract file path from tool result,
|
|
64
|
+
// recall only memory relevant to that file, inject as context.
|
|
42
65
|
try {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
66
|
+
const toolName = input?.tool || ""
|
|
67
|
+
const args = input?.args || {}
|
|
68
|
+
|
|
69
|
+
// Extract file path from tool arguments or output
|
|
70
|
+
let filePath = ""
|
|
71
|
+
if (args.file) filePath = args.file
|
|
72
|
+
else if (args.path) filePath = args.path
|
|
73
|
+
else if (args.filePath) filePath = args.filePath
|
|
74
|
+
|
|
75
|
+
// Also try to extract from tool output text
|
|
76
|
+
if (!filePath && input?.output) {
|
|
77
|
+
const outputText = typeof input.output === "string" ? input.output : ""
|
|
78
|
+
const fileMatch = outputText.match(/(?:file|path|source):\s*([\\w/.\\-]+)/i)
|
|
79
|
+
if (fileMatch) filePath = fileMatch[1]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!filePath) return
|
|
83
|
+
|
|
84
|
+
// Build recall query from file path components
|
|
85
|
+
const basename = path.basename(filePath, path.extname(filePath))
|
|
86
|
+
const dirParts = path.dirname(filePath).split(path.sep).filter(Boolean)
|
|
87
|
+
const query = [basename, ...dirParts.slice(-2)].join(" ")
|
|
88
|
+
|
|
89
|
+
const text = await recall(query)
|
|
90
|
+
if (!text || text.includes("(empty)") || text.includes("No results")) return
|
|
91
|
+
|
|
92
|
+
// Inject as context for the next agent turn
|
|
93
|
+
if (typeof input?.setContext === "function") {
|
|
94
|
+
input.setContext("toon-memory:" + filePath, text)
|
|
95
|
+
}
|
|
49
96
|
} catch {}
|
|
50
97
|
},
|
|
51
98
|
}
|
package/src/cli/setup.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { captureToggle } from "./capture-toggle"
|
|
2
2
|
import { init, uninstall, status, upgrade, stats, exportMemory, importMemory, dumpMemory, interactiveInstall } from "./commands"
|
|
3
3
|
import { watch } from "./watch"
|
|
4
|
+
import { viewer, viewerExport } from "../viewer"
|
|
4
5
|
|
|
5
6
|
const args = process.argv.slice(2)
|
|
6
7
|
|
|
@@ -72,20 +73,28 @@ if (args[0] === "watch") {
|
|
|
72
73
|
process.exit(0)
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
if (args[0] === "viewer") {
|
|
77
|
+
const portIdx = args.indexOf("--port")
|
|
78
|
+
const port = portIdx !== -1 && args[portIdx + 1] ? args[portIdx + 1] : undefined
|
|
79
|
+
if (args.includes("--export")) {
|
|
80
|
+
viewerExport()
|
|
81
|
+
} else {
|
|
82
|
+
viewer(port)
|
|
83
|
+
}
|
|
84
|
+
} else if (args.length === 0) {
|
|
77
85
|
await interactiveInstall()
|
|
78
86
|
process.exit(0)
|
|
87
|
+
} else {
|
|
88
|
+
console.log(`Unknown command: '${args[0]}'\n`)
|
|
89
|
+
printUsage()
|
|
90
|
+
process.exit(1)
|
|
79
91
|
}
|
|
80
92
|
|
|
81
|
-
console.log(`Unknown command: '${args[0]}'\n`)
|
|
82
|
-
printUsage()
|
|
83
|
-
process.exit(1)
|
|
84
|
-
|
|
85
93
|
/** Minimal usage string for unknown commands. */
|
|
86
94
|
function printUsage(): void {
|
|
87
95
|
console.log(`Usage: toon-memory <command> [options]
|
|
88
|
-
Commands: init, status, stats, export, import, watch, uninstall, capture, upgrade, mcp
|
|
96
|
+
Commands: init, status, stats, export, import, watch, viewer, uninstall, capture, upgrade, mcp
|
|
97
|
+
viewer [--export] [--port <n>] — Start an HTTP server with the graph viewer (press 'r' to reload). --export saves static HTML.
|
|
89
98
|
Init: toon-memory init [--agent <name> --agent <name>...] [--scope local|global]
|
|
90
99
|
Without --agent: interactive agent and scope selector.
|
|
91
100
|
With --agent: non-interactive installation (default: scope local).
|
package/src/cli/types.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
/** Config format: "json" | "toml" | "jsonc" | "none" (instructions only) */
|
|
2
|
-
export type AgentFormat = "json" | "toml" | "jsonc" | "none"
|
|
1
|
+
/** Config format: "json" | "toml" | "jsonc" | "continue" | "openclaw" | "none" (instructions only) */
|
|
2
|
+
export type AgentFormat = "json" | "toml" | "jsonc" | "continue" | "openclaw" | "none"
|
|
3
3
|
|
|
4
4
|
/** Supported AI coding agent configuration */
|
|
5
5
|
export interface Agent {
|
|
6
6
|
name: string
|
|
7
7
|
global?: string
|
|
8
8
|
local?: string
|
|
9
|
+
/** Separate path for hook registration (when it differs from the MCP config path) */
|
|
10
|
+
hookPath?: string
|
|
9
11
|
mcpKey: string
|
|
10
12
|
format: AgentFormat
|
|
11
13
|
needsHooks: boolean
|