toon-memory 2.5.1 → 2.7.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 +124 -3
- package/dist/cli/setup.js +463 -425
- package/install.sh +8 -1
- package/mcp/server.js +2058 -1084
- package/package.json +5 -3
- package/skills/toon-memory.md +7 -2
- package/src/cli/agents.ts +184 -0
- package/src/cli/capture-toggle.ts +29 -0
- package/src/cli/commands.ts +532 -0
- package/src/cli/config-writer.ts +133 -0
- package/src/cli/constants.ts +56 -0
- package/src/cli/hooks.ts +202 -0
- package/src/cli/instructions.ts +35 -0
- package/src/cli/memory.ts +104 -0
- package/src/cli/opencode-plugin.ts +84 -0
- package/src/cli/setup.ts +5 -1596
- package/src/cli/types.ts +29 -0
- package/src/cli/watch.ts +169 -0
- package/src/lib/code-search.ts +192 -0
- package/src/lib/context.ts +658 -0
- package/src/lib/git.ts +263 -0
- package/src/lib/project-scan.ts +224 -0
- package/src/mcp/archive.ts +129 -0
- package/src/mcp/config.ts +88 -0
- package/src/mcp/consolidation.ts +70 -0
- package/src/mcp/crypto.ts +41 -0
- package/src/mcp/entries.ts +110 -0
- package/src/mcp/memory-io.ts +53 -0
- package/src/mcp/observations.ts +16 -0
- package/src/mcp/resources.ts +57 -0
- package/src/mcp/scoring.ts +77 -0
- package/src/mcp/server.ts +7 -1482
- package/src/mcp/tools.ts +826 -0
package/dist/cli/setup.js
CHANGED
|
@@ -1,19 +1,73 @@
|
|
|
1
|
-
// src/cli/
|
|
2
|
-
import { existsSync
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/cli/capture-toggle.ts
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
3
|
+
|
|
4
|
+
// src/cli/constants.ts
|
|
5
|
+
import { dirname, join } from "path";
|
|
6
|
+
var projectRoot = process.cwd();
|
|
7
|
+
var HOME = process.env.HOME || process.env.USERPROFILE || "~";
|
|
8
|
+
var MEMORY_DIR = join(projectRoot, ".toon-memory", "memory");
|
|
9
|
+
var scriptDir = dirname(process.argv[1] || process.cwd());
|
|
10
|
+
var CAPTURE_JS = join(scriptDir, "capture.js");
|
|
11
|
+
var SESSION_START_JS = join(scriptDir, "session-start.js");
|
|
12
|
+
var CAPTURE_CONFIG = join(MEMORY_DIR, "config.json");
|
|
13
|
+
var ANTIGRAVITY_HOOK_NAME = "toon-memory";
|
|
14
|
+
var INSTRUCTION_CONTENT = `# toon-memory
|
|
15
|
+
|
|
16
|
+
Persistent memory for this project. Use it to avoid re-investigating things.
|
|
17
|
+
|
|
18
|
+
## At the START of every session
|
|
19
|
+
1. Run memory_stats to see what's in memory.
|
|
20
|
+
2. If the user asks something that might be in memory, run memory_recall BEFORE reading files.
|
|
21
|
+
|
|
22
|
+
## When making decisions
|
|
23
|
+
- Before implementing a non-trivial change: memory_remember(category='decision')
|
|
24
|
+
- When you resolve a complex bug: memory_remember(category='bug')
|
|
25
|
+
- When you observe a code pattern: memory_remember(category='pattern')
|
|
26
|
+
|
|
27
|
+
## At the END of every session
|
|
28
|
+
- Save important decisions, bugs resolved, and patterns observed.
|
|
29
|
+
`;
|
|
30
|
+
var OPENCODE_PASSIVE_INSTRUCTION = `# toon-memory
|
|
31
|
+
|
|
32
|
+
La memoria se auto-inyecta v\xEDa .opencode/instructions/memory-autoload.md; usa memory_recall solo como fallback.
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
// src/cli/capture-toggle.ts
|
|
36
|
+
function captureToggle(enable) {
|
|
37
|
+
if (!existsSync(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
|
|
38
|
+
let config = {};
|
|
39
|
+
if (existsSync(CAPTURE_CONFIG)) {
|
|
40
|
+
try {
|
|
41
|
+
config = JSON.parse(readFileSync(CAPTURE_CONFIG, "utf-8"));
|
|
42
|
+
} catch {
|
|
43
|
+
config = {};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
config.capture = enable;
|
|
47
|
+
writeFileSync(CAPTURE_CONFIG, JSON.stringify(config, null, 2));
|
|
48
|
+
if (enable) {
|
|
49
|
+
console.log("\n\u{1F534} Captura de actividad HABILITADA.");
|
|
50
|
+
console.log("Los hooks grabar\xE1n observaciones en .toon-memory/memory/observations.toon.");
|
|
51
|
+
console.log("Revisa con `memory_captured` y promu\xE9velas con `memory_remember`.\n");
|
|
52
|
+
} else {
|
|
53
|
+
console.log("\n\u26AA Captura de actividad DESHABILITADA.\n");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/cli/commands.ts
|
|
58
|
+
import { existsSync as existsSync9, mkdirSync as mkdirSync7, readFileSync as readFileSync9, writeFileSync as writeFileSync7, unlinkSync, rmSync } from "fs";
|
|
59
|
+
import { basename, join as join7 } from "path";
|
|
5
60
|
import { checkbox, select, confirm } from "@inquirer/prompts";
|
|
6
|
-
import { gzipSync } from "zlib";
|
|
7
61
|
|
|
8
62
|
// src/lib/vocab.ts
|
|
9
|
-
import { existsSync, readFileSync } from "fs";
|
|
10
|
-
import { join } from "path";
|
|
63
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
64
|
+
import { join as join2 } from "path";
|
|
11
65
|
function extractProjectDeps(root) {
|
|
12
66
|
const vocab = {};
|
|
13
|
-
const pkgPath =
|
|
14
|
-
if (
|
|
67
|
+
const pkgPath = join2(root, "package.json");
|
|
68
|
+
if (existsSync2(pkgPath)) {
|
|
15
69
|
try {
|
|
16
|
-
const pkg = JSON.parse(
|
|
70
|
+
const pkg = JSON.parse(readFileSync2(pkgPath, "utf-8"));
|
|
17
71
|
const deps = { ...pkg.dependencies || {}, ...pkg.devDependencies || {} };
|
|
18
72
|
for (const name of Object.keys(deps)) {
|
|
19
73
|
const tag = name.replace(/^@[^/]+\//, "").toLowerCase();
|
|
@@ -22,10 +76,10 @@ function extractProjectDeps(root) {
|
|
|
22
76
|
} catch {
|
|
23
77
|
}
|
|
24
78
|
}
|
|
25
|
-
const cargoPath =
|
|
26
|
-
if (
|
|
79
|
+
const cargoPath = join2(root, "Cargo.toml");
|
|
80
|
+
if (existsSync2(cargoPath)) {
|
|
27
81
|
try {
|
|
28
|
-
const txt =
|
|
82
|
+
const txt = readFileSync2(cargoPath, "utf-8");
|
|
29
83
|
const block = txt.match(/\[dependencies\][\s\S]*?(?:\n\[|$)/)?.[0] || "";
|
|
30
84
|
for (const line of block.split("\n")) {
|
|
31
85
|
const m = line.match(/^\s*([A-Za-z0-9_-]+)\s*=/);
|
|
@@ -38,10 +92,10 @@ function extractProjectDeps(root) {
|
|
|
38
92
|
}
|
|
39
93
|
}
|
|
40
94
|
for (const file of ["requirements.txt", "pyproject.toml"]) {
|
|
41
|
-
const p =
|
|
42
|
-
if (!
|
|
95
|
+
const p = join2(root, file);
|
|
96
|
+
if (!existsSync2(p)) continue;
|
|
43
97
|
try {
|
|
44
|
-
const txt =
|
|
98
|
+
const txt = readFileSync2(p, "utf-8");
|
|
45
99
|
for (const raw of txt.split("\n")) {
|
|
46
100
|
const name = raw.trim().split(/[<>=!~ \[\]]/)[0].toLowerCase();
|
|
47
101
|
if (!name || name.startsWith("#") || name.includes("[")) continue;
|
|
@@ -50,10 +104,10 @@ function extractProjectDeps(root) {
|
|
|
50
104
|
} catch {
|
|
51
105
|
}
|
|
52
106
|
}
|
|
53
|
-
const goPath =
|
|
54
|
-
if (
|
|
107
|
+
const goPath = join2(root, "go.mod");
|
|
108
|
+
if (existsSync2(goPath)) {
|
|
55
109
|
try {
|
|
56
|
-
const txt =
|
|
110
|
+
const txt = readFileSync2(goPath, "utf-8");
|
|
57
111
|
for (const raw of txt.split("\n")) {
|
|
58
112
|
const m = raw.match(/^\s*([A-Za-z0-9_./-]+)\s+v[0-9]/);
|
|
59
113
|
if (m) {
|
|
@@ -68,84 +122,41 @@ function extractProjectDeps(root) {
|
|
|
68
122
|
return vocab;
|
|
69
123
|
}
|
|
70
124
|
|
|
71
|
-
// src/cli/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
var sourceDir = join2(__dirname, "..", "..", "src");
|
|
125
|
+
// src/cli/agents.ts
|
|
126
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
127
|
+
import { dirname as dirname2, join as join3 } from "path";
|
|
75
128
|
function resolvePackageJson() {
|
|
76
|
-
let dir =
|
|
129
|
+
let dir = dirname2(process.argv[1] || ".");
|
|
77
130
|
while (true) {
|
|
78
|
-
const candidate =
|
|
79
|
-
if (
|
|
131
|
+
const candidate = join3(dir, "package.json");
|
|
132
|
+
if (existsSync3(candidate)) {
|
|
80
133
|
try {
|
|
81
|
-
const p = JSON.parse(
|
|
134
|
+
const p = JSON.parse(readFileSync3(candidate, "utf-8"));
|
|
82
135
|
if (p && p.name === "toon-memory") return candidate;
|
|
83
136
|
} catch {
|
|
84
137
|
}
|
|
85
138
|
}
|
|
86
|
-
const parent =
|
|
139
|
+
const parent = dirname2(dir);
|
|
87
140
|
if (parent === dir) return null;
|
|
88
141
|
dir = parent;
|
|
89
142
|
}
|
|
90
143
|
}
|
|
91
|
-
var HOME = process.env.HOME || process.env.USERPROFILE || "~";
|
|
92
|
-
var MEMORY_DIR = join2(projectRoot, ".toon-memory", "memory");
|
|
93
|
-
var CAPTURE_JS = join2(__dirname, "capture.js");
|
|
94
|
-
var SESSION_START_JS = join2(__dirname, "session-start.js");
|
|
95
|
-
function sessionStartHookContent(agentName) {
|
|
96
|
-
return `#!/bin/bash
|
|
97
|
-
node "${SESSION_START_JS}" ${agentName}
|
|
98
|
-
exit 0
|
|
99
|
-
`;
|
|
100
|
-
}
|
|
101
|
-
var CAPTURE_CONFIG = join2(MEMORY_DIR, "config.json");
|
|
102
|
-
function captureHookContent(agentName) {
|
|
103
|
-
return `#!/bin/bash
|
|
104
|
-
CFG="${CAPTURE_CONFIG}"
|
|
105
|
-
if [ -z "$TOON_MEMORY_CAPTURE" ]; then
|
|
106
|
-
if [ ! -f "$CFG" ]; then exit 0; fi
|
|
107
|
-
grep -q '"capture"[[:space:]]*:[[:space:]]*true' "$CFG" 2>/dev/null || exit 0
|
|
108
|
-
fi
|
|
109
|
-
node "${CAPTURE_JS}" ${agentName}
|
|
110
|
-
exit 0
|
|
111
|
-
`;
|
|
112
|
-
}
|
|
113
|
-
var INSTRUCTION_CONTENT = `# toon-memory
|
|
114
|
-
|
|
115
|
-
Persistent memory for this project. Use it to avoid re-investigating things.
|
|
116
|
-
|
|
117
|
-
## At the START of every session
|
|
118
|
-
1. Run memory_stats to see what's in memory.
|
|
119
|
-
2. If the user asks something that might be in memory, run memory_recall BEFORE reading files.
|
|
120
|
-
|
|
121
|
-
## When making decisions
|
|
122
|
-
- Before implementing a non-trivial change: memory_remember(category='decision')
|
|
123
|
-
- When you resolve a complex bug: memory_remember(category='bug')
|
|
124
|
-
- When you observe a code pattern: memory_remember(category='pattern')
|
|
125
|
-
|
|
126
|
-
## At the END of every session
|
|
127
|
-
- Save important decisions, bugs resolved, and patterns observed.
|
|
128
|
-
`;
|
|
129
|
-
var OPENCODE_PASSIVE_INSTRUCTION = `# toon-memory
|
|
130
|
-
|
|
131
|
-
La memoria se auto-inyecta v\xEDa .opencode/instructions/memory-autoload.md; usa memory_recall solo como fallback.
|
|
132
|
-
`;
|
|
133
144
|
function detectAgents() {
|
|
134
145
|
const agents = [];
|
|
135
146
|
agents.push({
|
|
136
147
|
name: "opencode",
|
|
137
|
-
global:
|
|
138
|
-
local:
|
|
148
|
+
global: join3(HOME, ".config", "opencode", "opencode.json"),
|
|
149
|
+
local: join3(projectRoot, ".opencode", "opencode.json"),
|
|
139
150
|
mcpKey: "mcp",
|
|
140
151
|
format: "json",
|
|
141
152
|
needsHooks: false,
|
|
142
153
|
needsPlugin: true,
|
|
143
154
|
needsInstructions: true,
|
|
144
|
-
instructionFile:
|
|
155
|
+
instructionFile: join3(projectRoot, "AGENTS.md")
|
|
145
156
|
});
|
|
146
157
|
agents.push({
|
|
147
158
|
name: "vscode/copilot",
|
|
148
|
-
local:
|
|
159
|
+
local: join3(projectRoot, ".vscode", "mcp.json"),
|
|
149
160
|
mcpKey: "servers",
|
|
150
161
|
format: "json",
|
|
151
162
|
needsHooks: false,
|
|
@@ -153,18 +164,18 @@ function detectAgents() {
|
|
|
153
164
|
});
|
|
154
165
|
agents.push({
|
|
155
166
|
name: "claude",
|
|
156
|
-
global:
|
|
157
|
-
local:
|
|
167
|
+
global: join3(HOME, ".claude", "settings.json"),
|
|
168
|
+
local: join3(projectRoot, ".claude", "settings.json"),
|
|
158
169
|
mcpKey: "mcpServers",
|
|
159
170
|
format: "json",
|
|
160
171
|
needsHooks: true,
|
|
161
172
|
needsInstructions: true,
|
|
162
|
-
instructionFile:
|
|
173
|
+
instructionFile: join3(projectRoot, ".claude", "AGENTS.md"),
|
|
163
174
|
captureJson: ["PostToolUse", "Stop"]
|
|
164
175
|
});
|
|
165
176
|
agents.push({
|
|
166
177
|
name: "cursor",
|
|
167
|
-
local:
|
|
178
|
+
local: join3(projectRoot, ".cursor", "mcp.json"),
|
|
168
179
|
mcpKey: "mcpServers",
|
|
169
180
|
format: "json",
|
|
170
181
|
needsHooks: false,
|
|
@@ -172,7 +183,7 @@ function detectAgents() {
|
|
|
172
183
|
});
|
|
173
184
|
agents.push({
|
|
174
185
|
name: "windsurf",
|
|
175
|
-
global:
|
|
186
|
+
global: join3(HOME, ".codeium", "windsurf", "mcp_config.json"),
|
|
176
187
|
mcpKey: "mcpServers",
|
|
177
188
|
format: "json",
|
|
178
189
|
needsHooks: false,
|
|
@@ -180,7 +191,7 @@ function detectAgents() {
|
|
|
180
191
|
});
|
|
181
192
|
agents.push({
|
|
182
193
|
name: "cline",
|
|
183
|
-
local:
|
|
194
|
+
local: join3(projectRoot, ".cline", "mcp.json"),
|
|
184
195
|
mcpKey: "mcpServers",
|
|
185
196
|
format: "json",
|
|
186
197
|
needsHooks: false,
|
|
@@ -188,7 +199,7 @@ function detectAgents() {
|
|
|
188
199
|
});
|
|
189
200
|
agents.push({
|
|
190
201
|
name: "continue",
|
|
191
|
-
local:
|
|
202
|
+
local: join3(projectRoot, ".continue", "config.json"),
|
|
192
203
|
mcpKey: "mcpServers",
|
|
193
204
|
format: "json",
|
|
194
205
|
needsHooks: false,
|
|
@@ -196,26 +207,26 @@ function detectAgents() {
|
|
|
196
207
|
});
|
|
197
208
|
agents.push({
|
|
198
209
|
name: "codex",
|
|
199
|
-
local:
|
|
210
|
+
local: join3(projectRoot, ".codex", "config.toml"),
|
|
200
211
|
mcpKey: "mcpServers",
|
|
201
212
|
format: "toml",
|
|
202
213
|
needsHooks: true,
|
|
203
214
|
needsInstructions: true,
|
|
204
|
-
instructionFile:
|
|
215
|
+
instructionFile: join3(projectRoot, ".codex", "AGENTS.md"),
|
|
205
216
|
captureToml: ["PostToolUse", "Stop"]
|
|
206
217
|
});
|
|
207
218
|
agents.push({
|
|
208
219
|
name: "gemini",
|
|
209
|
-
local:
|
|
220
|
+
local: join3(projectRoot, ".gemini", "settings.json"),
|
|
210
221
|
mcpKey: "mcpServers",
|
|
211
222
|
format: "json",
|
|
212
223
|
needsHooks: true,
|
|
213
224
|
needsInstructions: true,
|
|
214
|
-
instructionFile:
|
|
225
|
+
instructionFile: join3(projectRoot, ".gemini", "GEMINI.md")
|
|
215
226
|
});
|
|
216
227
|
agents.push({
|
|
217
228
|
name: "zed",
|
|
218
|
-
global:
|
|
229
|
+
global: join3(HOME, ".config", "zed", "settings.json"),
|
|
219
230
|
mcpKey: "mcp_servers",
|
|
220
231
|
format: "jsonc",
|
|
221
232
|
needsHooks: false,
|
|
@@ -223,12 +234,12 @@ function detectAgents() {
|
|
|
223
234
|
});
|
|
224
235
|
agents.push({
|
|
225
236
|
name: "antigravity",
|
|
226
|
-
local:
|
|
237
|
+
local: join3(projectRoot, ".gemini", "config", "mcp_config.json"),
|
|
227
238
|
mcpKey: "mcpServers",
|
|
228
239
|
format: "json",
|
|
229
240
|
needsHooks: true,
|
|
230
241
|
needsInstructions: true,
|
|
231
|
-
instructionFile:
|
|
242
|
+
instructionFile: join3(projectRoot, "antigravity-cli", "AGENTS.md")
|
|
232
243
|
});
|
|
233
244
|
agents.push({
|
|
234
245
|
name: "aider",
|
|
@@ -236,20 +247,20 @@ function detectAgents() {
|
|
|
236
247
|
format: "none",
|
|
237
248
|
needsHooks: false,
|
|
238
249
|
needsInstructions: true,
|
|
239
|
-
instructionFile:
|
|
250
|
+
instructionFile: join3(projectRoot, "CONVENTIONS.md")
|
|
240
251
|
});
|
|
241
252
|
agents.push({
|
|
242
253
|
name: "kilocode",
|
|
243
|
-
global:
|
|
254
|
+
global: join3(HOME, ".kilocode", "mcp_settings.json"),
|
|
244
255
|
mcpKey: "mcpServers",
|
|
245
256
|
format: "json",
|
|
246
257
|
needsHooks: false,
|
|
247
258
|
needsInstructions: true,
|
|
248
|
-
instructionFile:
|
|
259
|
+
instructionFile: join3(HOME, ".kilocode", "rules", "toon-memory.md")
|
|
249
260
|
});
|
|
250
261
|
agents.push({
|
|
251
262
|
name: "openclaw",
|
|
252
|
-
local:
|
|
263
|
+
local: join3(projectRoot, ".openclaw.json"),
|
|
253
264
|
mcpKey: "mcpServers",
|
|
254
265
|
format: "json",
|
|
255
266
|
needsHooks: false,
|
|
@@ -257,7 +268,7 @@ function detectAgents() {
|
|
|
257
268
|
});
|
|
258
269
|
agents.push({
|
|
259
270
|
name: "kiro",
|
|
260
|
-
local:
|
|
271
|
+
local: join3(projectRoot, ".kiro", "settings", "mcp.json"),
|
|
261
272
|
mcpKey: "mcpServers",
|
|
262
273
|
format: "json",
|
|
263
274
|
needsHooks: false,
|
|
@@ -265,44 +276,104 @@ function detectAgents() {
|
|
|
265
276
|
});
|
|
266
277
|
return agents;
|
|
267
278
|
}
|
|
279
|
+
|
|
280
|
+
// src/cli/memory.ts
|
|
281
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync4, writeFileSync as writeFileSync2 } from "fs";
|
|
282
|
+
import { join as join4 } from "path";
|
|
268
283
|
function installMemoryDir() {
|
|
269
|
-
const memoryFile =
|
|
270
|
-
if (!
|
|
271
|
-
if (!
|
|
272
|
-
|
|
284
|
+
const memoryFile = join4(MEMORY_DIR, "data.toon");
|
|
285
|
+
if (!existsSync4(MEMORY_DIR)) mkdirSync2(MEMORY_DIR, { recursive: true });
|
|
286
|
+
if (!existsSync4(memoryFile)) {
|
|
287
|
+
writeFileSync2(memoryFile, "version: 1\n[0|]\n");
|
|
273
288
|
console.log(" Created .toon-memory/memory/data.toon");
|
|
274
289
|
}
|
|
275
290
|
}
|
|
276
291
|
function ensureGitignore() {
|
|
277
|
-
const gitignorePath =
|
|
292
|
+
const gitignorePath = join4(projectRoot, ".gitignore");
|
|
278
293
|
const entries = [".toon-memory/memory/", ".opencode/instructions/memory-autoload.md"];
|
|
279
|
-
if (!
|
|
280
|
-
|
|
294
|
+
if (!existsSync4(gitignorePath)) {
|
|
295
|
+
writeFileSync2(gitignorePath, `${entries.join("\n")}
|
|
281
296
|
`);
|
|
282
297
|
console.log(" Created .gitignore with memory exclusion");
|
|
283
298
|
return;
|
|
284
299
|
}
|
|
285
|
-
const content =
|
|
300
|
+
const content = readFileSync4(gitignorePath, "utf-8");
|
|
286
301
|
const missing = entries.filter((e) => !content.includes(e));
|
|
287
302
|
if (missing.length > 0) {
|
|
288
|
-
|
|
303
|
+
writeFileSync2(gitignorePath, `${content.trim()}
|
|
289
304
|
${missing.join("\n")}
|
|
290
305
|
`);
|
|
291
306
|
console.log(` Added to .gitignore: ${missing.join(", ")}`);
|
|
292
307
|
}
|
|
293
308
|
}
|
|
309
|
+
function dumpMemoryMarkdown() {
|
|
310
|
+
const memoryFile = join4(MEMORY_DIR, "data.toon");
|
|
311
|
+
if (!existsSync4(memoryFile)) {
|
|
312
|
+
return "# toon-memory\n(empty)\n";
|
|
313
|
+
}
|
|
314
|
+
const data = readFileSync4(memoryFile, "utf-8");
|
|
315
|
+
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
|
|
316
|
+
const entries = lines.map((line) => {
|
|
317
|
+
const parts = line.trim().split("|");
|
|
318
|
+
return {
|
|
319
|
+
id: parts[0] ?? "",
|
|
320
|
+
category: parts[1] ?? "",
|
|
321
|
+
key: parts[2] ?? "",
|
|
322
|
+
content: parts[3] ?? "",
|
|
323
|
+
file: parts[4] ?? "",
|
|
324
|
+
tags: parts[5] ? parts[5].split(";").filter(Boolean) : [],
|
|
325
|
+
date: parts[6] ?? ""
|
|
326
|
+
};
|
|
327
|
+
});
|
|
328
|
+
const allLines = data.split("\n");
|
|
329
|
+
const summaryIdx = allLines.findIndex((l) => l.trim().startsWith("summaries:"));
|
|
330
|
+
const summaries = [];
|
|
331
|
+
if (summaryIdx !== -1) {
|
|
332
|
+
for (const sl of allLines.slice(summaryIdx + 1)) {
|
|
333
|
+
if (!sl.includes(":") || !sl.startsWith(" ")) continue;
|
|
334
|
+
const idx = sl.indexOf(":");
|
|
335
|
+
const file = sl.slice(0, idx).trim();
|
|
336
|
+
const summary = sl.slice(idx + 1).trim();
|
|
337
|
+
if (file && summary) summaries.push({ file, summary });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (entries.length === 0 && summaries.length === 0) {
|
|
341
|
+
return "# toon-memory\n(empty)\n";
|
|
342
|
+
}
|
|
343
|
+
const out = ["# toon-memory (auto-loaded)", ""];
|
|
344
|
+
for (const e of entries) {
|
|
345
|
+
out.push(`## ${e.category}: ${e.key}`);
|
|
346
|
+
out.push(e.content);
|
|
347
|
+
if (e.file) out.push(`- file: ${e.file}`);
|
|
348
|
+
if (e.tags.length) out.push(`- tags: ${e.tags.join(", ")}`);
|
|
349
|
+
if (e.date) out.push(`- date: ${e.date}`);
|
|
350
|
+
out.push("");
|
|
351
|
+
}
|
|
352
|
+
if (summaries.length > 0) {
|
|
353
|
+
out.push("## file summaries");
|
|
354
|
+
for (const s of summaries) {
|
|
355
|
+
out.push(`- ${s.file}: ${s.summary}`);
|
|
356
|
+
}
|
|
357
|
+
out.push("");
|
|
358
|
+
}
|
|
359
|
+
return out.join("\n").trimEnd() + "\n";
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// src/cli/config-writer.ts
|
|
363
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync3, readFileSync as readFileSync5, writeFileSync as writeFileSync3 } from "fs";
|
|
364
|
+
import { dirname as dirname3 } from "path";
|
|
294
365
|
function installJSONConfig(agent, scope) {
|
|
295
366
|
const configPath = scope === "global" ? agent.global : agent.local;
|
|
296
367
|
if (!configPath) {
|
|
297
368
|
console.log(` No ${scope} config path for ${agent.name}`);
|
|
298
369
|
return;
|
|
299
370
|
}
|
|
300
|
-
const configDir =
|
|
301
|
-
if (!
|
|
371
|
+
const configDir = dirname3(configPath);
|
|
372
|
+
if (!existsSync5(configDir)) mkdirSync3(configDir, { recursive: true });
|
|
302
373
|
let config = {};
|
|
303
|
-
if (
|
|
374
|
+
if (existsSync5(configPath)) {
|
|
304
375
|
try {
|
|
305
|
-
config = JSON.parse(
|
|
376
|
+
config = JSON.parse(readFileSync5(configPath, "utf-8"));
|
|
306
377
|
} catch {
|
|
307
378
|
config = {};
|
|
308
379
|
}
|
|
@@ -321,30 +392,30 @@ function installJSONConfig(agent, scope) {
|
|
|
321
392
|
args: ["-y", "toon-memory", "mcp"]
|
|
322
393
|
};
|
|
323
394
|
}
|
|
324
|
-
|
|
395
|
+
writeFileSync3(configPath, JSON.stringify(config, null, 2));
|
|
325
396
|
console.log(` MCP server added to ${configPath}`);
|
|
326
397
|
}
|
|
327
398
|
function installTOMLConfig(agent) {
|
|
328
399
|
const configPath = agent.local;
|
|
329
400
|
if (!configPath) return;
|
|
330
|
-
const configDir =
|
|
331
|
-
if (!
|
|
401
|
+
const configDir = dirname3(configPath);
|
|
402
|
+
if (!existsSync5(configDir)) mkdirSync3(configDir, { recursive: true });
|
|
332
403
|
const toml = `[mcpServers.toon-memory]
|
|
333
404
|
command = "npx"
|
|
334
405
|
args = ["-y", "toon-memory", "mcp"]
|
|
335
406
|
`;
|
|
336
|
-
|
|
407
|
+
writeFileSync3(configPath, toml);
|
|
337
408
|
console.log(` MCP server added to ${configPath}`);
|
|
338
409
|
}
|
|
339
410
|
function installZedConfig(agent) {
|
|
340
411
|
const configPath = agent.global;
|
|
341
412
|
if (!configPath) return;
|
|
342
|
-
const configDir =
|
|
343
|
-
if (!
|
|
413
|
+
const configDir = dirname3(configPath);
|
|
414
|
+
if (!existsSync5(configDir)) mkdirSync3(configDir, { recursive: true });
|
|
344
415
|
let config = {};
|
|
345
|
-
if (
|
|
416
|
+
if (existsSync5(configPath)) {
|
|
346
417
|
try {
|
|
347
|
-
const raw =
|
|
418
|
+
const raw = readFileSync5(configPath, "utf-8");
|
|
348
419
|
const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
349
420
|
config = JSON.parse(stripped);
|
|
350
421
|
} catch {
|
|
@@ -356,7 +427,7 @@ function installZedConfig(agent) {
|
|
|
356
427
|
command: "npx",
|
|
357
428
|
args: ["-y", "toon-memory", "mcp"]
|
|
358
429
|
};
|
|
359
|
-
|
|
430
|
+
writeFileSync3(configPath, JSON.stringify(config, null, 2));
|
|
360
431
|
console.log(` MCP server added to ${configPath}`);
|
|
361
432
|
}
|
|
362
433
|
function installMCPConfig(agent, scope) {
|
|
@@ -372,33 +443,71 @@ function installMCPConfig(agent, scope) {
|
|
|
372
443
|
installJSONConfig(agent, scope);
|
|
373
444
|
}
|
|
374
445
|
}
|
|
446
|
+
function updateMemoryConfig(patch) {
|
|
447
|
+
if (!existsSync5(MEMORY_DIR)) mkdirSync3(MEMORY_DIR, { recursive: true });
|
|
448
|
+
let cfg = {};
|
|
449
|
+
if (existsSync5(CAPTURE_CONFIG)) {
|
|
450
|
+
try {
|
|
451
|
+
cfg = JSON.parse(readFileSync5(CAPTURE_CONFIG, "utf-8"));
|
|
452
|
+
} catch {
|
|
453
|
+
cfg = {};
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
Object.assign(cfg, patch);
|
|
457
|
+
writeFileSync3(CAPTURE_CONFIG, JSON.stringify(cfg, null, 2));
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/cli/instructions.ts
|
|
461
|
+
import { existsSync as existsSync6, mkdirSync as mkdirSync4, readFileSync as readFileSync6, writeFileSync as writeFileSync4 } from "fs";
|
|
462
|
+
import { dirname as dirname4 } from "path";
|
|
375
463
|
function installInstructions(agent) {
|
|
376
464
|
if (!agent.needsInstructions || !agent.instructionFile) return;
|
|
377
465
|
const filePath = agent.instructionFile;
|
|
378
|
-
const fileDir =
|
|
379
|
-
if (!
|
|
466
|
+
const fileDir = dirname4(filePath);
|
|
467
|
+
if (!existsSync6(fileDir)) mkdirSync4(fileDir, { recursive: true });
|
|
380
468
|
const content = agent.name === "opencode" ? OPENCODE_PASSIVE_INSTRUCTION : INSTRUCTION_CONTENT;
|
|
381
|
-
if (
|
|
382
|
-
const existing =
|
|
469
|
+
if (existsSync6(filePath)) {
|
|
470
|
+
const existing = readFileSync6(filePath, "utf-8");
|
|
383
471
|
if (existing.includes("toon-memory") || existing.includes("memory_recall")) {
|
|
384
472
|
console.log(` Instructions already present in ${filePath}`);
|
|
385
473
|
return;
|
|
386
474
|
}
|
|
387
|
-
|
|
475
|
+
writeFileSync4(filePath, `${existing.trim()}
|
|
388
476
|
|
|
389
477
|
${content}`);
|
|
390
478
|
console.log(` Appended toon-memory instructions to ${filePath}`);
|
|
391
479
|
} else {
|
|
392
|
-
|
|
480
|
+
writeFileSync4(filePath, content);
|
|
393
481
|
console.log(` Created ${filePath}`);
|
|
394
482
|
}
|
|
395
483
|
}
|
|
484
|
+
|
|
485
|
+
// src/cli/hooks.ts
|
|
486
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync5, readFileSync as readFileSync7, writeFileSync as writeFileSync5, chmodSync } from "fs";
|
|
487
|
+
import { dirname as dirname5, join as join5 } from "path";
|
|
488
|
+
function sessionStartHookContent(agentName) {
|
|
489
|
+
return `#!/bin/bash
|
|
490
|
+
node "${SESSION_START_JS}" ${agentName}
|
|
491
|
+
exit 0
|
|
492
|
+
`;
|
|
493
|
+
}
|
|
494
|
+
function captureHookContent(agentName) {
|
|
495
|
+
return `#!/bin/bash
|
|
496
|
+
CFG="${CAPTURE_CONFIG}"
|
|
497
|
+
if [ -z "$TOON_MEMORY_CAPTURE" ]; then
|
|
498
|
+
if [ ! -f "$CFG" ]; then exit 0; fi
|
|
499
|
+
grep -q '"capture"[[:space:]]*:[[:space:]]*true' "$CFG" 2>/dev/null || exit 0
|
|
500
|
+
fi
|
|
501
|
+
node "${CAPTURE_JS}" ${agentName}
|
|
502
|
+
exit 0
|
|
503
|
+
`;
|
|
504
|
+
}
|
|
396
505
|
function installHooks(agent) {
|
|
397
506
|
if (!agent.needsHooks) return;
|
|
398
|
-
const hookDir =
|
|
399
|
-
if (!
|
|
400
|
-
const hookPath =
|
|
401
|
-
|
|
507
|
+
const hookDir = join5(projectRoot, ".toon-memory", "hooks");
|
|
508
|
+
if (!existsSync7(hookDir)) mkdirSync5(hookDir, { recursive: true });
|
|
509
|
+
const hookPath = join5(hookDir, `session-start-${agent.name}.sh`);
|
|
510
|
+
writeFileSync5(hookPath, sessionStartHookContent(agent.name));
|
|
402
511
|
chmodSync(hookPath, 493);
|
|
403
512
|
console.log(` Hook script created at ${hookPath}`);
|
|
404
513
|
if (agent.format === "toml" && agent.local) {
|
|
@@ -407,15 +516,15 @@ function installHooks(agent) {
|
|
|
407
516
|
registerHookJSON(agent, hookPath);
|
|
408
517
|
}
|
|
409
518
|
if (agent.captureJson && agent.format === "json") {
|
|
410
|
-
const capPath =
|
|
411
|
-
|
|
519
|
+
const capPath = join5(hookDir, `capture-${agent.name}.sh`);
|
|
520
|
+
writeFileSync5(capPath, captureHookContent(agent.name));
|
|
412
521
|
chmodSync(capPath, 493);
|
|
413
522
|
console.log(` Capture hook created at ${capPath}`);
|
|
414
523
|
registerCaptureHookJSON(agent, capPath);
|
|
415
524
|
}
|
|
416
525
|
if (agent.captureToml && agent.format === "toml" && agent.local) {
|
|
417
|
-
const capPath =
|
|
418
|
-
|
|
526
|
+
const capPath = join5(hookDir, `capture-${agent.name}.sh`);
|
|
527
|
+
writeFileSync5(capPath, captureHookContent(agent.name));
|
|
419
528
|
chmodSync(capPath, 493);
|
|
420
529
|
console.log(` Capture hook created at ${capPath}`);
|
|
421
530
|
registerCaptureHookTOML(agent, capPath);
|
|
@@ -424,12 +533,12 @@ function installHooks(agent) {
|
|
|
424
533
|
function registerCaptureHookJSON(agent, scriptPath) {
|
|
425
534
|
const configPath = agent.local || agent.global;
|
|
426
535
|
if (!configPath) return;
|
|
427
|
-
const configDir =
|
|
428
|
-
if (!
|
|
536
|
+
const configDir = dirname5(configPath);
|
|
537
|
+
if (!existsSync7(configDir)) mkdirSync5(configDir, { recursive: true });
|
|
429
538
|
let config = {};
|
|
430
|
-
if (
|
|
539
|
+
if (existsSync7(configPath)) {
|
|
431
540
|
try {
|
|
432
|
-
config = JSON.parse(
|
|
541
|
+
config = JSON.parse(readFileSync7(configPath, "utf-8"));
|
|
433
542
|
} catch {
|
|
434
543
|
config = {};
|
|
435
544
|
}
|
|
@@ -441,13 +550,13 @@ function registerCaptureHookJSON(agent, scriptPath) {
|
|
|
441
550
|
config.hooks[event].push({ command: scriptPath });
|
|
442
551
|
}
|
|
443
552
|
}
|
|
444
|
-
|
|
553
|
+
writeFileSync5(configPath, JSON.stringify(config, null, 2));
|
|
445
554
|
console.log(` Capture hooks registered in ${configPath}`);
|
|
446
555
|
}
|
|
447
556
|
function registerCaptureHookTOML(agent, scriptPath) {
|
|
448
557
|
const configPath = agent.local;
|
|
449
|
-
if (!configPath || !
|
|
450
|
-
let content =
|
|
558
|
+
if (!configPath || !existsSync7(configPath)) return;
|
|
559
|
+
let content = readFileSync7(configPath, "utf-8");
|
|
451
560
|
for (const event of agent.captureToml || []) {
|
|
452
561
|
if (content.includes(`event = "${event}"`)) continue;
|
|
453
562
|
content += `
|
|
@@ -456,13 +565,13 @@ event = "${event}"
|
|
|
456
565
|
command = "${scriptPath}"
|
|
457
566
|
`;
|
|
458
567
|
}
|
|
459
|
-
|
|
568
|
+
writeFileSync5(configPath, content);
|
|
460
569
|
console.log(` Capture hooks registered in ${configPath}`);
|
|
461
570
|
}
|
|
462
571
|
function registerHookTOML(agent, hookPath) {
|
|
463
572
|
const configPath = agent.local;
|
|
464
|
-
if (!configPath || !
|
|
465
|
-
let content =
|
|
573
|
+
if (!configPath || !existsSync7(configPath)) return;
|
|
574
|
+
let content = readFileSync7(configPath, "utf-8");
|
|
466
575
|
if (content.includes('event = "SessionStart"')) {
|
|
467
576
|
console.log(` Hook already registered in ${configPath}`);
|
|
468
577
|
return;
|
|
@@ -472,9 +581,73 @@ function registerHookTOML(agent, hookPath) {
|
|
|
472
581
|
event = "SessionStart"
|
|
473
582
|
command = "${hookPath}"
|
|
474
583
|
`;
|
|
475
|
-
|
|
584
|
+
writeFileSync5(configPath, content);
|
|
476
585
|
console.log(` Hook registered in ${configPath}`);
|
|
477
586
|
}
|
|
587
|
+
function registerHookJSON(agent, hookPath) {
|
|
588
|
+
const configPath = agent.format === "jsonc" ? agent.global : agent.local || agent.global;
|
|
589
|
+
if (!configPath) return;
|
|
590
|
+
const configDir = dirname5(configPath);
|
|
591
|
+
if (!existsSync7(configDir)) mkdirSync5(configDir, { recursive: true });
|
|
592
|
+
let config = {};
|
|
593
|
+
if (existsSync7(configPath)) {
|
|
594
|
+
try {
|
|
595
|
+
if (agent.format === "jsonc") {
|
|
596
|
+
const raw = readFileSync7(configPath, "utf-8");
|
|
597
|
+
const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
598
|
+
config = JSON.parse(stripped);
|
|
599
|
+
} else {
|
|
600
|
+
config = JSON.parse(readFileSync7(configPath, "utf-8"));
|
|
601
|
+
}
|
|
602
|
+
} catch {
|
|
603
|
+
config = {};
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
if (agent.name === "claude" || agent.name === "gemini") {
|
|
607
|
+
if (!config.hooks) config.hooks = {};
|
|
608
|
+
if (!config.hooks.SessionStart) config.hooks.SessionStart = [];
|
|
609
|
+
if (!config.hooks.SessionStart.some((h) => h.command === hookPath)) {
|
|
610
|
+
config.hooks.SessionStart.push({ command: hookPath });
|
|
611
|
+
writeFileSync5(configPath, JSON.stringify(config, null, 2));
|
|
612
|
+
console.log(` Hook registered in ${configPath}`);
|
|
613
|
+
}
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function registerAntigravityHooks(agent) {
|
|
618
|
+
const hookDir = join5(projectRoot, ".toon-memory", "hooks");
|
|
619
|
+
if (!existsSync7(hookDir)) mkdirSync5(hookDir, { recursive: true });
|
|
620
|
+
const sessionPath = join5(hookDir, "session-start-antigravity.sh");
|
|
621
|
+
writeFileSync5(sessionPath, sessionStartHookContent("antigravity"));
|
|
622
|
+
chmodSync(sessionPath, 493);
|
|
623
|
+
console.log(` Hook script created at ${sessionPath}`);
|
|
624
|
+
const capPath = join5(hookDir, "capture-antigravity.sh");
|
|
625
|
+
writeFileSync5(capPath, captureHookContent("antigravity"));
|
|
626
|
+
chmodSync(capPath, 493);
|
|
627
|
+
console.log(` Capture hook created at ${capPath}`);
|
|
628
|
+
const base = agent.local ? dirname5(agent.local) : projectRoot;
|
|
629
|
+
const hooksFile = join5(base, "hooks.json");
|
|
630
|
+
mkdirSync5(base, { recursive: true });
|
|
631
|
+
let cfg = {};
|
|
632
|
+
if (existsSync7(hooksFile)) {
|
|
633
|
+
try {
|
|
634
|
+
cfg = JSON.parse(readFileSync7(hooksFile, "utf-8"));
|
|
635
|
+
} catch {
|
|
636
|
+
cfg = {};
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
cfg[ANTIGRAVITY_HOOK_NAME] = {
|
|
640
|
+
PreInvocation: [{ type: "command", command: sessionPath }],
|
|
641
|
+
PostToolUse: [{ matcher: "*", hooks: [{ type: "command", command: capPath }] }],
|
|
642
|
+
Stop: [{ type: "command", command: capPath }]
|
|
643
|
+
};
|
|
644
|
+
writeFileSync5(hooksFile, JSON.stringify(cfg, null, 2));
|
|
645
|
+
console.log(` Antigravity hooks registered in ${hooksFile}`);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// src/cli/opencode-plugin.ts
|
|
649
|
+
import { existsSync as existsSync8, mkdirSync as mkdirSync6, readFileSync as readFileSync8, writeFileSync as writeFileSync6 } from "fs";
|
|
650
|
+
import { join as join6 } from "path";
|
|
478
651
|
function opencodePluginContent() {
|
|
479
652
|
return `import * as fs from "fs"
|
|
480
653
|
|
|
@@ -519,111 +692,33 @@ export const ToonMemory = async ({ $, directory, worktree }) => {
|
|
|
519
692
|
`;
|
|
520
693
|
}
|
|
521
694
|
function installOpenCodePlugin(agent) {
|
|
522
|
-
const pluginsDir =
|
|
523
|
-
if (!
|
|
524
|
-
const dest =
|
|
525
|
-
|
|
695
|
+
const pluginsDir = join6(projectRoot, ".opencode", "plugins");
|
|
696
|
+
if (!existsSync8(pluginsDir)) mkdirSync6(pluginsDir, { recursive: true });
|
|
697
|
+
const dest = join6(pluginsDir, "toon-memory.ts");
|
|
698
|
+
writeFileSync6(dest, opencodePluginContent());
|
|
526
699
|
console.log(` Plugin created at ${dest}`);
|
|
527
|
-
const instrDir =
|
|
528
|
-
if (!
|
|
529
|
-
const instrFile =
|
|
530
|
-
|
|
700
|
+
const instrDir = join6(projectRoot, ".opencode", "instructions");
|
|
701
|
+
if (!existsSync8(instrDir)) mkdirSync6(instrDir, { recursive: true });
|
|
702
|
+
const instrFile = join6(instrDir, "memory-autoload.md");
|
|
703
|
+
writeFileSync6(instrFile, dumpMemoryMarkdown());
|
|
531
704
|
console.log(` Seeded ${instrFile}`);
|
|
532
705
|
const configPath = agent.local || agent.global;
|
|
533
|
-
if (configPath &&
|
|
706
|
+
if (configPath && existsSync8(configPath)) {
|
|
534
707
|
try {
|
|
535
|
-
const config = JSON.parse(
|
|
708
|
+
const config = JSON.parse(readFileSync8(configPath, "utf-8"));
|
|
536
709
|
if (config.hooks) {
|
|
537
710
|
delete config.hooks;
|
|
538
|
-
|
|
711
|
+
writeFileSync6(configPath, JSON.stringify(config, null, 2));
|
|
539
712
|
console.log(` Removed stale "hooks" key from ${configPath}`);
|
|
540
713
|
}
|
|
541
714
|
} catch {
|
|
542
715
|
}
|
|
543
716
|
}
|
|
544
717
|
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
const sessionPath = join2(hookDir, "session-start-antigravity.sh");
|
|
550
|
-
writeFileSync(sessionPath, sessionStartHookContent("antigravity"));
|
|
551
|
-
chmodSync(sessionPath, 493);
|
|
552
|
-
console.log(` Hook script created at ${sessionPath}`);
|
|
553
|
-
const capPath = join2(hookDir, "capture-antigravity.sh");
|
|
554
|
-
writeFileSync(capPath, captureHookContent("antigravity"));
|
|
555
|
-
chmodSync(capPath, 493);
|
|
556
|
-
console.log(` Capture hook created at ${capPath}`);
|
|
557
|
-
const base = agent.local ? dirname(agent.local) : projectRoot;
|
|
558
|
-
const hooksFile = join2(base, "hooks.json");
|
|
559
|
-
mkdirSync(base, { recursive: true });
|
|
560
|
-
let cfg = {};
|
|
561
|
-
if (existsSync2(hooksFile)) {
|
|
562
|
-
try {
|
|
563
|
-
cfg = JSON.parse(readFileSync2(hooksFile, "utf-8"));
|
|
564
|
-
} catch {
|
|
565
|
-
cfg = {};
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
cfg[ANTIGRAVITY_HOOK_NAME] = {
|
|
569
|
-
PreInvocation: [{ type: "command", command: sessionPath }],
|
|
570
|
-
PostToolUse: [{ matcher: "*", hooks: [{ type: "command", command: capPath }] }],
|
|
571
|
-
Stop: [{ type: "command", command: capPath }]
|
|
572
|
-
};
|
|
573
|
-
writeFileSync(hooksFile, JSON.stringify(cfg, null, 2));
|
|
574
|
-
console.log(` Antigravity hooks registered in ${hooksFile}`);
|
|
575
|
-
}
|
|
576
|
-
function registerHookJSON(agent, hookPath) {
|
|
577
|
-
const configPath = agent.format === "jsonc" ? agent.global : agent.local || agent.global;
|
|
578
|
-
if (!configPath) return;
|
|
579
|
-
const configDir = dirname(configPath);
|
|
580
|
-
if (!existsSync2(configDir)) mkdirSync(configDir, { recursive: true });
|
|
581
|
-
let config = {};
|
|
582
|
-
if (existsSync2(configPath)) {
|
|
583
|
-
try {
|
|
584
|
-
if (agent.format === "jsonc") {
|
|
585
|
-
const raw = readFileSync2(configPath, "utf-8");
|
|
586
|
-
const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
587
|
-
config = JSON.parse(stripped);
|
|
588
|
-
} else {
|
|
589
|
-
config = JSON.parse(readFileSync2(configPath, "utf-8"));
|
|
590
|
-
}
|
|
591
|
-
} catch {
|
|
592
|
-
config = {};
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
if (agent.name === "claude" || agent.name === "gemini") {
|
|
596
|
-
if (!config.hooks) config.hooks = {};
|
|
597
|
-
if (!config.hooks.SessionStart) config.hooks.SessionStart = [];
|
|
598
|
-
if (!config.hooks.SessionStart.some((h) => h.command === hookPath)) {
|
|
599
|
-
config.hooks.SessionStart.push({ command: hookPath });
|
|
600
|
-
writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
601
|
-
console.log(` Hook registered in ${configPath}`);
|
|
602
|
-
}
|
|
603
|
-
return;
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
function installForAgent(agent, scope) {
|
|
607
|
-
console.log(`${agent.name}:`);
|
|
608
|
-
installMCPConfig(agent, scope);
|
|
609
|
-
installInstructions(agent);
|
|
610
|
-
if (agent.needsPlugin) installOpenCodePlugin(agent);
|
|
611
|
-
else if (agent.name === "antigravity") registerAntigravityHooks(agent);
|
|
612
|
-
else installHooks(agent);
|
|
613
|
-
}
|
|
614
|
-
function updateMemoryConfig(patch) {
|
|
615
|
-
if (!existsSync2(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
|
|
616
|
-
let cfg = {};
|
|
617
|
-
if (existsSync2(CAPTURE_CONFIG)) {
|
|
618
|
-
try {
|
|
619
|
-
cfg = JSON.parse(readFileSync2(CAPTURE_CONFIG, "utf-8"));
|
|
620
|
-
} catch {
|
|
621
|
-
cfg = {};
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
Object.assign(cfg, patch);
|
|
625
|
-
writeFileSync(CAPTURE_CONFIG, JSON.stringify(cfg, null, 2));
|
|
626
|
-
}
|
|
718
|
+
|
|
719
|
+
// src/cli/commands.ts
|
|
720
|
+
var MEMORY_DIR2 = join7(projectRoot, ".toon-memory", "memory");
|
|
721
|
+
var CAPTURE_CONFIG2 = join7(MEMORY_DIR2, "config.json");
|
|
627
722
|
async function promptAgentSelection(agents) {
|
|
628
723
|
return checkbox({
|
|
629
724
|
message: "\xBFQu\xE9 agentes quieres configurar?",
|
|
@@ -634,6 +729,14 @@ async function promptAgentSelection(agents) {
|
|
|
634
729
|
}))
|
|
635
730
|
});
|
|
636
731
|
}
|
|
732
|
+
function installForAgent(agent, scope) {
|
|
733
|
+
console.log(`${agent.name}:`);
|
|
734
|
+
installMCPConfig(agent, scope);
|
|
735
|
+
installInstructions(agent);
|
|
736
|
+
if (agent.needsPlugin) installOpenCodePlugin(agent);
|
|
737
|
+
else if (agent.name === "antigravity") registerAntigravityHooks(agent);
|
|
738
|
+
else installHooks(agent);
|
|
739
|
+
}
|
|
637
740
|
async function init(scope, agentFilter) {
|
|
638
741
|
console.log("\n\u{1F9E0} toon-memory init\n");
|
|
639
742
|
installMemoryDir();
|
|
@@ -692,40 +795,40 @@ function uninstall() {
|
|
|
692
795
|
if (agent.format === "json" || agent.format === "jsonc") {
|
|
693
796
|
const configs = [agent.global, agent.local].filter(Boolean);
|
|
694
797
|
for (const configPath of configs) {
|
|
695
|
-
if (!
|
|
798
|
+
if (!existsSync9(configPath)) continue;
|
|
696
799
|
try {
|
|
697
800
|
let config;
|
|
698
801
|
if (agent.format === "jsonc") {
|
|
699
|
-
const raw =
|
|
802
|
+
const raw = readFileSync9(configPath, "utf-8");
|
|
700
803
|
const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
701
804
|
config = JSON.parse(stripped);
|
|
702
805
|
} else {
|
|
703
|
-
config = JSON.parse(
|
|
806
|
+
config = JSON.parse(readFileSync9(configPath, "utf-8"));
|
|
704
807
|
}
|
|
705
808
|
const mcpKey = agent.mcpKey || "mcpServers";
|
|
706
809
|
if (config[mcpKey]?.["toon-memory"]) {
|
|
707
810
|
delete config[mcpKey]["toon-memory"];
|
|
708
|
-
|
|
811
|
+
writeFileSync7(configPath, JSON.stringify(config, null, 2));
|
|
709
812
|
console.log(` Removed MCP from ${agent.name} (${configPath})`);
|
|
710
813
|
}
|
|
711
814
|
} catch {
|
|
712
815
|
}
|
|
713
816
|
}
|
|
714
817
|
}
|
|
715
|
-
if (agent.format === "toml" && agent.local &&
|
|
818
|
+
if (agent.format === "toml" && agent.local && existsSync9(agent.local)) {
|
|
716
819
|
try {
|
|
717
|
-
let content =
|
|
820
|
+
let content = readFileSync9(agent.local, "utf-8");
|
|
718
821
|
content = content.replace(/\[mcpServers\.toon-memory\][\s\S]*?(?=\n\[|$)/, "").trim() + "\n";
|
|
719
|
-
|
|
822
|
+
writeFileSync7(agent.local, content);
|
|
720
823
|
console.log(` Removed MCP from ${agent.name} (${agent.local})`);
|
|
721
824
|
} catch {
|
|
722
825
|
}
|
|
723
826
|
}
|
|
724
827
|
}
|
|
725
828
|
for (const agent of agents) {
|
|
726
|
-
if (agent.instructionFile &&
|
|
829
|
+
if (agent.instructionFile && existsSync9(agent.instructionFile)) {
|
|
727
830
|
try {
|
|
728
|
-
const content =
|
|
831
|
+
const content = readFileSync9(agent.instructionFile, "utf-8");
|
|
729
832
|
if (content.includes(INSTRUCTION_CONTENT.trim()) && content.length < INSTRUCTION_CONTENT.length + 100) {
|
|
730
833
|
unlinkSync(agent.instructionFile);
|
|
731
834
|
console.log(` Removed ${agent.instructionFile}`);
|
|
@@ -734,32 +837,32 @@ function uninstall() {
|
|
|
734
837
|
}
|
|
735
838
|
}
|
|
736
839
|
}
|
|
737
|
-
const hookDir =
|
|
738
|
-
if (
|
|
840
|
+
const hookDir = join7(projectRoot, ".toon-memory", "hooks");
|
|
841
|
+
if (existsSync9(hookDir)) {
|
|
739
842
|
rmSync(hookDir, { recursive: true, force: true });
|
|
740
843
|
console.log(" Removed .toon-memory/hooks/");
|
|
741
844
|
}
|
|
742
|
-
const toolsFile =
|
|
743
|
-
if (
|
|
845
|
+
const toolsFile = join7(projectRoot, ".opencode", "tools", "memory.ts");
|
|
846
|
+
if (existsSync9(toolsFile)) {
|
|
744
847
|
unlinkSync(toolsFile);
|
|
745
848
|
console.log(" Removed .opencode/tools/memory.ts");
|
|
746
849
|
}
|
|
747
|
-
const ocPlugin =
|
|
748
|
-
if (
|
|
850
|
+
const ocPlugin = join7(projectRoot, ".opencode", "plugins", "toon-memory.ts");
|
|
851
|
+
if (existsSync9(ocPlugin)) {
|
|
749
852
|
unlinkSync(ocPlugin);
|
|
750
853
|
console.log(" Removed .opencode/plugins/toon-memory.ts");
|
|
751
854
|
}
|
|
752
|
-
const agyHooks =
|
|
753
|
-
if (
|
|
855
|
+
const agyHooks = join7(projectRoot, ".gemini", "config", "hooks.json");
|
|
856
|
+
if (existsSync9(agyHooks)) {
|
|
754
857
|
try {
|
|
755
|
-
const agyCfg = JSON.parse(
|
|
858
|
+
const agyCfg = JSON.parse(readFileSync9(agyHooks, "utf-8"));
|
|
756
859
|
if (agyCfg[ANTIGRAVITY_HOOK_NAME]) {
|
|
757
860
|
delete agyCfg[ANTIGRAVITY_HOOK_NAME];
|
|
758
861
|
if (Object.keys(agyCfg).length === 0) {
|
|
759
862
|
unlinkSync(agyHooks);
|
|
760
863
|
console.log(" Removed .gemini/config/hooks.json");
|
|
761
864
|
} else {
|
|
762
|
-
|
|
865
|
+
writeFileSync7(agyHooks, JSON.stringify(agyCfg, null, 2));
|
|
763
866
|
console.log(" Removed toon-memory hook from .gemini/config/hooks.json");
|
|
764
867
|
}
|
|
765
868
|
}
|
|
@@ -776,14 +879,14 @@ function status() {
|
|
|
776
879
|
let version = "unknown";
|
|
777
880
|
if (pkgPath) {
|
|
778
881
|
try {
|
|
779
|
-
version = JSON.parse(
|
|
882
|
+
version = JSON.parse(readFileSync9(pkgPath, "utf-8")).version;
|
|
780
883
|
} catch {
|
|
781
884
|
}
|
|
782
885
|
}
|
|
783
886
|
console.log(`Version: ${version}`);
|
|
784
|
-
const memoryFile =
|
|
785
|
-
if (
|
|
786
|
-
const data =
|
|
887
|
+
const memoryFile = join7(MEMORY_DIR2, "data.toon");
|
|
888
|
+
if (existsSync9(memoryFile)) {
|
|
889
|
+
const data = readFileSync9(memoryFile, "utf-8");
|
|
787
890
|
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
|
|
788
891
|
console.log(`Memory: ${lines.length} entries`);
|
|
789
892
|
} else {
|
|
@@ -791,9 +894,9 @@ function status() {
|
|
|
791
894
|
}
|
|
792
895
|
let capture = false;
|
|
793
896
|
if (process.env.TOON_MEMORY_CAPTURE) capture = true;
|
|
794
|
-
else if (
|
|
897
|
+
else if (existsSync9(CAPTURE_CONFIG2)) {
|
|
795
898
|
try {
|
|
796
|
-
const c = JSON.parse(
|
|
899
|
+
const c = JSON.parse(readFileSync9(CAPTURE_CONFIG2, "utf-8"));
|
|
797
900
|
if (c.capture) capture = true;
|
|
798
901
|
} catch {
|
|
799
902
|
}
|
|
@@ -806,15 +909,15 @@ function status() {
|
|
|
806
909
|
if (agent.format === "json" || agent.format === "jsonc") {
|
|
807
910
|
const configs = [agent.global, agent.local].filter(Boolean);
|
|
808
911
|
for (const configPath of configs) {
|
|
809
|
-
if (!
|
|
912
|
+
if (!existsSync9(configPath)) continue;
|
|
810
913
|
try {
|
|
811
914
|
let config;
|
|
812
915
|
if (agent.format === "jsonc") {
|
|
813
|
-
const raw =
|
|
916
|
+
const raw = readFileSync9(configPath, "utf-8");
|
|
814
917
|
const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
815
918
|
config = JSON.parse(stripped);
|
|
816
919
|
} else {
|
|
817
|
-
config = JSON.parse(
|
|
920
|
+
config = JSON.parse(readFileSync9(configPath, "utf-8"));
|
|
818
921
|
}
|
|
819
922
|
if (config[agent.mcpKey]?.["toon-memory"]) {
|
|
820
923
|
configured = true;
|
|
@@ -823,15 +926,15 @@ function status() {
|
|
|
823
926
|
}
|
|
824
927
|
}
|
|
825
928
|
}
|
|
826
|
-
if (agent.format === "toml" && agent.local &&
|
|
827
|
-
const content =
|
|
929
|
+
if (agent.format === "toml" && agent.local && existsSync9(agent.local)) {
|
|
930
|
+
const content = readFileSync9(agent.local, "utf-8");
|
|
828
931
|
if (content.includes("toon-memory")) configured = true;
|
|
829
932
|
}
|
|
830
|
-
const hasInstructions = agent.instructionFile ?
|
|
831
|
-
const hookPath =
|
|
832
|
-
let hasHooks =
|
|
933
|
+
const hasInstructions = agent.instructionFile ? existsSync9(agent.instructionFile) : false;
|
|
934
|
+
const hookPath = join7(projectRoot, ".toon-memory", "hooks", `session-start-${agent.name}.sh`);
|
|
935
|
+
let hasHooks = existsSync9(hookPath);
|
|
833
936
|
if (agent.needsPlugin) {
|
|
834
|
-
hasHooks =
|
|
937
|
+
hasHooks = existsSync9(join7(projectRoot, ".opencode", "plugins", "toon-memory.ts"));
|
|
835
938
|
}
|
|
836
939
|
if (agent.format === "none") {
|
|
837
940
|
console.log(` ${hasInstructions ? "\u2705" : "\u274C"} ${agent.name} (instructions only)`);
|
|
@@ -848,7 +951,7 @@ function upgrade() {
|
|
|
848
951
|
console.log("\n\u{1F9E0} toon-memory upgrade\n");
|
|
849
952
|
try {
|
|
850
953
|
const pkgPath = resolvePackageJson();
|
|
851
|
-
const currentVersion = pkgPath ? JSON.parse(
|
|
954
|
+
const currentVersion = pkgPath ? JSON.parse(readFileSync9(pkgPath, "utf-8")).version : "unknown";
|
|
852
955
|
console.log(`Current version: ${currentVersion}`);
|
|
853
956
|
console.log("\nTo upgrade, run:");
|
|
854
957
|
console.log(" npm install -g toon-memory@latest");
|
|
@@ -861,12 +964,12 @@ function upgrade() {
|
|
|
861
964
|
}
|
|
862
965
|
function stats() {
|
|
863
966
|
console.log("\n\u{1F9E0} toon-memory stats\n");
|
|
864
|
-
const memoryFile =
|
|
865
|
-
if (!
|
|
967
|
+
const memoryFile = join7(MEMORY_DIR2, "data.toon");
|
|
968
|
+
if (!existsSync9(memoryFile)) {
|
|
866
969
|
console.log("Memory not initialized. Run 'npx toon-memory init' first.\n");
|
|
867
970
|
return;
|
|
868
971
|
}
|
|
869
|
-
const data =
|
|
972
|
+
const data = readFileSync9(memoryFile, "utf-8");
|
|
870
973
|
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
|
|
871
974
|
const categories = {};
|
|
872
975
|
let latestDate = "";
|
|
@@ -892,12 +995,12 @@ function stats() {
|
|
|
892
995
|
}
|
|
893
996
|
function exportMemory() {
|
|
894
997
|
console.log("\n\u{1F9E0} toon-memory export\n");
|
|
895
|
-
const memoryFile =
|
|
896
|
-
if (!
|
|
998
|
+
const memoryFile = join7(MEMORY_DIR2, "data.toon");
|
|
999
|
+
if (!existsSync9(memoryFile)) {
|
|
897
1000
|
console.log("Memory not initialized. Run 'npx toon-memory init' first.\n");
|
|
898
1001
|
return;
|
|
899
1002
|
}
|
|
900
|
-
const data =
|
|
1003
|
+
const data = readFileSync9(memoryFile, "utf-8");
|
|
901
1004
|
const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
|
|
902
1005
|
const entries = lines.map((line) => {
|
|
903
1006
|
const parts = line.trim().split("|");
|
|
@@ -917,8 +1020,8 @@ function exportMemory() {
|
|
|
917
1020
|
entries,
|
|
918
1021
|
summaries: {}
|
|
919
1022
|
};
|
|
920
|
-
const outputPath =
|
|
921
|
-
|
|
1023
|
+
const outputPath = join7(projectRoot, "toon-memory-export.json");
|
|
1024
|
+
writeFileSync7(outputPath, JSON.stringify(exportData, null, 2));
|
|
922
1025
|
console.log(`Exported ${entries.length} entries to:`);
|
|
923
1026
|
console.log(` ${outputPath}
|
|
924
1027
|
`);
|
|
@@ -930,15 +1033,15 @@ function importMemory() {
|
|
|
930
1033
|
console.log("Usage: npx toon-memory import <file.json>\n");
|
|
931
1034
|
return;
|
|
932
1035
|
}
|
|
933
|
-
const importPath = importFile.startsWith("/") ? importFile :
|
|
934
|
-
if (!
|
|
1036
|
+
const importPath = importFile.startsWith("/") ? importFile : join7(projectRoot, importFile);
|
|
1037
|
+
if (!existsSync9(importPath)) {
|
|
935
1038
|
console.log(`File not found: ${importPath}
|
|
936
1039
|
`);
|
|
937
1040
|
return;
|
|
938
1041
|
}
|
|
939
1042
|
let importData;
|
|
940
1043
|
try {
|
|
941
|
-
importData = JSON.parse(
|
|
1044
|
+
importData = JSON.parse(readFileSync9(importPath, "utf-8"));
|
|
942
1045
|
} catch {
|
|
943
1046
|
console.log("Invalid JSON file\n");
|
|
944
1047
|
return;
|
|
@@ -947,11 +1050,11 @@ function importMemory() {
|
|
|
947
1050
|
console.log("Invalid format: missing 'entries' array\n");
|
|
948
1051
|
return;
|
|
949
1052
|
}
|
|
950
|
-
const memoryFile =
|
|
951
|
-
if (!
|
|
1053
|
+
const memoryFile = join7(MEMORY_DIR2, "data.toon");
|
|
1054
|
+
if (!existsSync9(MEMORY_DIR2)) mkdirSync7(MEMORY_DIR2, { recursive: true });
|
|
952
1055
|
let existingKeys = [];
|
|
953
|
-
if (
|
|
954
|
-
const existing =
|
|
1056
|
+
if (existsSync9(memoryFile)) {
|
|
1057
|
+
const existing = readFileSync9(memoryFile, "utf-8");
|
|
955
1058
|
existingKeys = existing.split("\n").filter((l) => l.startsWith(" ") && l.includes("|")).map((l) => l.trim().split("|")[2]);
|
|
956
1059
|
}
|
|
957
1060
|
const newEntries = importData.entries.filter((e) => !existingKeys.includes(e.key));
|
|
@@ -963,16 +1066,16 @@ function importMemory() {
|
|
|
963
1066
|
const tags = Array.isArray(e.tags) ? e.tags.join(";") : e.tags || "";
|
|
964
1067
|
return ` ${e.id}|${e.category}|${e.key}|${e.content}|${e.file}|${tags}|${e.date}`;
|
|
965
1068
|
}).join("\n");
|
|
966
|
-
if (
|
|
967
|
-
const existing =
|
|
1069
|
+
if (existsSync9(memoryFile)) {
|
|
1070
|
+
const existing = readFileSync9(memoryFile, "utf-8");
|
|
968
1071
|
const existingCount = existing.split("\n").filter((l) => l.startsWith(" ") && l.includes("|")).length;
|
|
969
1072
|
const updated = existing.replace(
|
|
970
1073
|
/entries\[\d+\|]/,
|
|
971
1074
|
`entries[${existingCount + newEntries.length}|]`
|
|
972
1075
|
) + "\n" + newLines;
|
|
973
|
-
|
|
1076
|
+
writeFileSync7(memoryFile, updated);
|
|
974
1077
|
} else {
|
|
975
|
-
|
|
1078
|
+
writeFileSync7(memoryFile, `version: 1
|
|
976
1079
|
entries[${newEntries.length}|]{id|category|key|content|file|tags|date}:
|
|
977
1080
|
${newLines}
|
|
978
1081
|
`);
|
|
@@ -981,61 +1084,55 @@ ${newLines}
|
|
|
981
1084
|
console.log(`Skipped ${importData.entries.length - newEntries.length} duplicates
|
|
982
1085
|
`);
|
|
983
1086
|
}
|
|
984
|
-
function
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1087
|
+
function dumpMemory() {
|
|
1088
|
+
console.log(dumpMemoryMarkdown());
|
|
1089
|
+
}
|
|
1090
|
+
async function interactiveInstall() {
|
|
1091
|
+
if (!process.stdin.isTTY) {
|
|
1092
|
+
console.log("\n\u{1F9E0} toon-memory \u2014 la instalaci\xF3n interactiva requiere una terminal.");
|
|
1093
|
+
console.log("Ejecuta 'toon-memory init [--agent <nombre>]' para instalaci\xF3n no interactiva.\n");
|
|
1094
|
+
return;
|
|
988
1095
|
}
|
|
989
|
-
const
|
|
990
|
-
const
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
return
|
|
994
|
-
id: parts[0] ?? "",
|
|
995
|
-
category: parts[1] ?? "",
|
|
996
|
-
key: parts[2] ?? "",
|
|
997
|
-
content: parts[3] ?? "",
|
|
998
|
-
file: parts[4] ?? "",
|
|
999
|
-
tags: parts[5] ? parts[5].split(";").filter(Boolean) : [],
|
|
1000
|
-
date: parts[6] ?? ""
|
|
1001
|
-
};
|
|
1002
|
-
});
|
|
1003
|
-
const allLines = data.split("\n");
|
|
1004
|
-
const summaryIdx = allLines.findIndex((l) => l.trim().startsWith("summaries:"));
|
|
1005
|
-
const summaries = [];
|
|
1006
|
-
if (summaryIdx !== -1) {
|
|
1007
|
-
for (const sl of allLines.slice(summaryIdx + 1)) {
|
|
1008
|
-
if (!sl.includes(":") || !sl.startsWith(" ")) continue;
|
|
1009
|
-
const idx = sl.indexOf(":");
|
|
1010
|
-
const file = sl.slice(0, idx).trim();
|
|
1011
|
-
const summary = sl.slice(idx + 1).trim();
|
|
1012
|
-
if (file && summary) summaries.push({ file, summary });
|
|
1013
|
-
}
|
|
1096
|
+
const agents = detectAgents();
|
|
1097
|
+
const selected = await promptAgentSelection(agents);
|
|
1098
|
+
if (selected.length === 0) {
|
|
1099
|
+
console.log("\nNo se seleccionaron agentes. Nada instalado.\n");
|
|
1100
|
+
return;
|
|
1014
1101
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
1102
|
+
const scope = await select({
|
|
1103
|
+
message: "Alcance:",
|
|
1104
|
+
choices: [
|
|
1105
|
+
{ name: "Local (proyecto)", value: "local" },
|
|
1106
|
+
{ name: "Global (~home)", value: "global" }
|
|
1107
|
+
]
|
|
1108
|
+
});
|
|
1109
|
+
const ok = await confirm({ message: "\xBFProceder?", default: true });
|
|
1110
|
+
if (!ok) {
|
|
1111
|
+
console.log("\nInstalaci\xF3n cancelada.\n");
|
|
1112
|
+
return;
|
|
1017
1113
|
}
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1114
|
+
console.log(`
|
|
1115
|
+
Instalando (${scope}) \u2014 ${selected.map((a) => a.name).join(", ")}...
|
|
1116
|
+
`);
|
|
1117
|
+
installMemoryDir();
|
|
1118
|
+
const deps = extractProjectDeps(projectRoot);
|
|
1119
|
+
if (Object.keys(deps).length > 0) {
|
|
1120
|
+
updateMemoryConfig({ vocab: deps });
|
|
1121
|
+
console.log(` Detected ${Object.keys(deps).length} dependencies \u2192 auto-tag vocabulary written to config.json`);
|
|
1026
1122
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
out.push(`- ${s.file}: ${s.summary}`);
|
|
1031
|
-
}
|
|
1032
|
-
out.push("");
|
|
1123
|
+
for (const agent of selected) {
|
|
1124
|
+
installForAgent(agent, scope);
|
|
1125
|
+
console.log("");
|
|
1033
1126
|
}
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
console.log(dumpMemoryMarkdown());
|
|
1127
|
+
ensureGitignore();
|
|
1128
|
+
console.log("Done! Restart your agent to use memory tools.");
|
|
1129
|
+
console.log("Run 'npx toon-memory uninstall' to remove.\n");
|
|
1038
1130
|
}
|
|
1131
|
+
|
|
1132
|
+
// src/cli/watch.ts
|
|
1133
|
+
import { existsSync as existsSync10, mkdirSync as mkdirSync8, readFileSync as readFileSync10, writeFileSync as writeFileSync8, readdirSync, statSync, unlinkSync as unlinkSync2 } from "fs";
|
|
1134
|
+
import { dirname as dirname7, join as join8 } from "path";
|
|
1135
|
+
import { gzipSync } from "zlib";
|
|
1039
1136
|
function parseWatchOptions(args2) {
|
|
1040
1137
|
const opts = {
|
|
1041
1138
|
interval: 5,
|
|
@@ -1050,7 +1147,7 @@ function parseWatchOptions(args2) {
|
|
|
1050
1147
|
opts.compress = true;
|
|
1051
1148
|
} else if (arg === "--log" || arg === "-l") {
|
|
1052
1149
|
opts.logFile = true;
|
|
1053
|
-
opts.logPath = args2[++i] ||
|
|
1150
|
+
opts.logPath = args2[++i] || join8(MEMORY_DIR, "watch.log");
|
|
1054
1151
|
} else if (arg === "--max-backups" || arg === "-m") {
|
|
1055
1152
|
opts.maxBackups = parseInt(args2[++i]) || 10;
|
|
1056
1153
|
} else if (!arg.startsWith("-")) {
|
|
@@ -1064,11 +1161,11 @@ function writeWatchLog(logPath, message) {
|
|
|
1064
1161
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
1065
1162
|
const logLine = `[${timestamp}] ${message}
|
|
1066
1163
|
`;
|
|
1067
|
-
|
|
1164
|
+
writeFileSync8(logPath, logLine, { flag: "a" });
|
|
1068
1165
|
}
|
|
1069
1166
|
function getBackupFiles(backupDir) {
|
|
1070
|
-
if (!
|
|
1071
|
-
return readdirSync(backupDir).filter((f) => f.startsWith("backup-") && (f.endsWith(".toon") || f.endsWith(".gz"))).map((f) =>
|
|
1167
|
+
if (!existsSync10(backupDir)) return [];
|
|
1168
|
+
return readdirSync(backupDir).filter((f) => f.startsWith("backup-") && (f.endsWith(".toon") || f.endsWith(".gz"))).map((f) => join8(backupDir, f)).sort((a, b) => statSync(a).mtimeMs - statSync(b).mtimeMs);
|
|
1072
1169
|
}
|
|
1073
1170
|
function pruneBackups(backupDir, maxBackups) {
|
|
1074
1171
|
if (maxBackups <= 0) return 0;
|
|
@@ -1076,26 +1173,35 @@ function pruneBackups(backupDir, maxBackups) {
|
|
|
1076
1173
|
const excess = files.length - maxBackups;
|
|
1077
1174
|
if (excess <= 0) return 0;
|
|
1078
1175
|
for (let i = 0; i < excess; i++) {
|
|
1079
|
-
|
|
1176
|
+
unlinkSync2(files[i]);
|
|
1080
1177
|
}
|
|
1081
1178
|
return excess;
|
|
1082
1179
|
}
|
|
1083
1180
|
function compressData(data) {
|
|
1084
1181
|
return gzipSync(Buffer.from(data, "utf-8"));
|
|
1085
1182
|
}
|
|
1086
|
-
function
|
|
1183
|
+
function hashContent(content) {
|
|
1184
|
+
let hash = 0;
|
|
1185
|
+
for (let i = 0; i < content.length; i++) {
|
|
1186
|
+
const char = content.charCodeAt(i);
|
|
1187
|
+
hash = (hash << 5) - hash + char;
|
|
1188
|
+
hash = hash & hash;
|
|
1189
|
+
}
|
|
1190
|
+
return hash.toString(36);
|
|
1191
|
+
}
|
|
1192
|
+
function watch(args2) {
|
|
1087
1193
|
console.log("\n\u{1F9E0} toon-memory watch\n");
|
|
1088
|
-
const memoryFile =
|
|
1089
|
-
const backupDir =
|
|
1090
|
-
if (!
|
|
1194
|
+
const memoryFile = join8(MEMORY_DIR, "data.toon");
|
|
1195
|
+
const backupDir = join8(MEMORY_DIR, "backups");
|
|
1196
|
+
if (!existsSync10(memoryFile)) {
|
|
1091
1197
|
console.log("Memory not initialized. Run 'npx toon-memory init' first.\n");
|
|
1092
1198
|
return;
|
|
1093
1199
|
}
|
|
1094
|
-
if (!
|
|
1095
|
-
const opts = parseWatchOptions(
|
|
1200
|
+
if (!existsSync10(backupDir)) mkdirSync8(backupDir, { recursive: true });
|
|
1201
|
+
const opts = parseWatchOptions(args2);
|
|
1096
1202
|
if (opts.logFile) {
|
|
1097
|
-
const logDir =
|
|
1098
|
-
if (!
|
|
1203
|
+
const logDir = dirname7(opts.logPath);
|
|
1204
|
+
if (!existsSync10(logDir)) mkdirSync8(logDir, { recursive: true });
|
|
1099
1205
|
}
|
|
1100
1206
|
console.log(`Watching memory file every ${opts.interval} minutes...`);
|
|
1101
1207
|
console.log(`Max backups: ${opts.maxBackups === 0 ? "unlimited" : opts.maxBackups}`);
|
|
@@ -1103,22 +1209,22 @@ function watch() {
|
|
|
1103
1209
|
console.log(`Logging: ${opts.logFile ? `enabled (${opts.logPath})` : "disabled"}`);
|
|
1104
1210
|
console.log(`Press Ctrl+C to stop
|
|
1105
1211
|
`);
|
|
1106
|
-
let lastContent =
|
|
1212
|
+
let lastContent = readFileSync10(memoryFile, "utf-8");
|
|
1107
1213
|
let lastHash = hashContent(lastContent);
|
|
1108
1214
|
let backupCount = 0;
|
|
1109
1215
|
if (opts.logFile) writeWatchLog(opts.logPath, "Watch started");
|
|
1110
1216
|
const backup = () => {
|
|
1111
1217
|
try {
|
|
1112
|
-
const currentContent =
|
|
1218
|
+
const currentContent = readFileSync10(memoryFile, "utf-8");
|
|
1113
1219
|
const currentHash = hashContent(currentContent);
|
|
1114
1220
|
if (currentHash !== lastHash) {
|
|
1115
1221
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1116
1222
|
const ext = opts.compress ? ".toon.gz" : ".toon";
|
|
1117
|
-
const backupFile =
|
|
1223
|
+
const backupFile = join8(backupDir, `backup-${timestamp}${ext}`);
|
|
1118
1224
|
if (opts.compress) {
|
|
1119
|
-
|
|
1225
|
+
writeFileSync8(backupFile, compressData(currentContent));
|
|
1120
1226
|
} else {
|
|
1121
|
-
|
|
1227
|
+
writeFileSync8(backupFile, currentContent);
|
|
1122
1228
|
}
|
|
1123
1229
|
backupCount++;
|
|
1124
1230
|
console.log(`\u{1F4E6} Backup #${backupCount} created: ${timestamp}`);
|
|
@@ -1137,15 +1243,6 @@ function watch() {
|
|
|
1137
1243
|
if (opts.logFile) writeWatchLog(opts.logPath, msg);
|
|
1138
1244
|
}
|
|
1139
1245
|
};
|
|
1140
|
-
function hashContent(content) {
|
|
1141
|
-
let hash = 0;
|
|
1142
|
-
for (let i = 0; i < content.length; i++) {
|
|
1143
|
-
const char = content.charCodeAt(i);
|
|
1144
|
-
hash = (hash << 5) - hash + char;
|
|
1145
|
-
hash = hash & hash;
|
|
1146
|
-
}
|
|
1147
|
-
return hash.toString(36);
|
|
1148
|
-
}
|
|
1149
1246
|
backup();
|
|
1150
1247
|
const interval = setInterval(backup, opts.interval * 60 * 1e3);
|
|
1151
1248
|
process.on("SIGINT", () => {
|
|
@@ -1161,26 +1258,8 @@ function watch() {
|
|
|
1161
1258
|
process.exit(0);
|
|
1162
1259
|
});
|
|
1163
1260
|
}
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
let config = {};
|
|
1167
|
-
if (existsSync2(CAPTURE_CONFIG)) {
|
|
1168
|
-
try {
|
|
1169
|
-
config = JSON.parse(readFileSync2(CAPTURE_CONFIG, "utf-8"));
|
|
1170
|
-
} catch {
|
|
1171
|
-
config = {};
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
config.capture = enable;
|
|
1175
|
-
writeFileSync(CAPTURE_CONFIG, JSON.stringify(config, null, 2));
|
|
1176
|
-
if (enable) {
|
|
1177
|
-
console.log("\n\u{1F534} Captura de actividad HABILITADA.");
|
|
1178
|
-
console.log("Los hooks grabar\xE1n observaciones en .toon-memory/memory/observations.toon.");
|
|
1179
|
-
console.log("Revisa con `memory_captured` y promu\xE9velas con `memory_remember`.\n");
|
|
1180
|
-
} else {
|
|
1181
|
-
console.log("\n\u26AA Captura de actividad DESHABILITADA.\n");
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1261
|
+
|
|
1262
|
+
// src/cli/setup.ts
|
|
1184
1263
|
var args = process.argv.slice(2);
|
|
1185
1264
|
if (args[0] === "capture") {
|
|
1186
1265
|
captureToggle(args[1] === "on");
|
|
@@ -1237,7 +1316,7 @@ if (args[0] === "import") {
|
|
|
1237
1316
|
process.exit(0);
|
|
1238
1317
|
}
|
|
1239
1318
|
if (args[0] === "watch") {
|
|
1240
|
-
watch();
|
|
1319
|
+
watch(args);
|
|
1241
1320
|
process.exit(0);
|
|
1242
1321
|
}
|
|
1243
1322
|
if (args.length === 0) {
|
|
@@ -1257,44 +1336,3 @@ Init: toon-memory init [--agent <nombre> --agent <nombre>...] [--scope local|glo
|
|
|
1257
1336
|
Opciones: -v/--version, -h/--help
|
|
1258
1337
|
Sin argumentos inicia el instalador interactivo.`);
|
|
1259
1338
|
}
|
|
1260
|
-
async function interactiveInstall() {
|
|
1261
|
-
if (!process.stdin.isTTY) {
|
|
1262
|
-
console.log("\n\u{1F9E0} toon-memory \u2014 la instalaci\xF3n interactiva requiere una terminal.");
|
|
1263
|
-
console.log("Ejecuta 'toon-memory init [--agent <nombre>]' para instalaci\xF3n no interactiva.\n");
|
|
1264
|
-
return;
|
|
1265
|
-
}
|
|
1266
|
-
const agents = detectAgents();
|
|
1267
|
-
const selected = await promptAgentSelection(agents);
|
|
1268
|
-
if (selected.length === 0) {
|
|
1269
|
-
console.log("\nNo se seleccionaron agentes. Nada instalado.\n");
|
|
1270
|
-
return;
|
|
1271
|
-
}
|
|
1272
|
-
const scope = await select({
|
|
1273
|
-
message: "Alcance:",
|
|
1274
|
-
choices: [
|
|
1275
|
-
{ name: "Local (proyecto)", value: "local" },
|
|
1276
|
-
{ name: "Global (~home)", value: "global" }
|
|
1277
|
-
]
|
|
1278
|
-
});
|
|
1279
|
-
const ok = await confirm({ message: "\xBFProceder?", default: true });
|
|
1280
|
-
if (!ok) {
|
|
1281
|
-
console.log("\nInstalaci\xF3n cancelada.\n");
|
|
1282
|
-
return;
|
|
1283
|
-
}
|
|
1284
|
-
console.log(`
|
|
1285
|
-
Instalando (${scope}) \u2014 ${selected.map((a) => a.name).join(", ")}...
|
|
1286
|
-
`);
|
|
1287
|
-
installMemoryDir();
|
|
1288
|
-
const deps = extractProjectDeps(projectRoot);
|
|
1289
|
-
if (Object.keys(deps).length > 0) {
|
|
1290
|
-
updateMemoryConfig({ vocab: deps });
|
|
1291
|
-
console.log(` Detected ${Object.keys(deps).length} dependencies \u2192 auto-tag vocabulary written to config.json`);
|
|
1292
|
-
}
|
|
1293
|
-
for (const agent of selected) {
|
|
1294
|
-
installForAgent(agent, scope);
|
|
1295
|
-
console.log("");
|
|
1296
|
-
}
|
|
1297
|
-
ensureGitignore();
|
|
1298
|
-
console.log("Done! Restart your agent to use memory tools.");
|
|
1299
|
-
console.log("Run 'npx toon-memory uninstall' to remove.\n");
|
|
1300
|
-
}
|