toon-memory 2.2.0 → 2.3.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/dist/cli/setup.js CHANGED
@@ -1,23 +1,88 @@
1
1
  // src/cli/setup.ts
2
- import { existsSync, mkdirSync, readFileSync, writeFileSync, unlinkSync, readdirSync, statSync, chmodSync, rmSync } from "fs";
3
- import { basename, dirname, join } from "path";
2
+ import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, writeFileSync, unlinkSync, readdirSync, statSync, chmodSync, rmSync } from "fs";
3
+ import { basename, dirname, join as join2 } from "path";
4
4
  import { fileURLToPath } from "url";
5
5
  import { createInterface } from "readline";
6
6
  import { gzipSync } from "zlib";
7
+
8
+ // src/lib/vocab.ts
9
+ import { existsSync, readFileSync } from "fs";
10
+ import { join } from "path";
11
+ function extractProjectDeps(root) {
12
+ const vocab = {};
13
+ const pkgPath = join(root, "package.json");
14
+ if (existsSync(pkgPath)) {
15
+ try {
16
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
17
+ const deps = { ...pkg.dependencies || {}, ...pkg.devDependencies || {} };
18
+ for (const name of Object.keys(deps)) {
19
+ const tag = name.replace(/^@[^/]+\//, "").toLowerCase();
20
+ vocab[tag] = name.includes("/") ? [name.toLowerCase(), tag] : [name.toLowerCase()];
21
+ }
22
+ } catch {
23
+ }
24
+ }
25
+ const cargoPath = join(root, "Cargo.toml");
26
+ if (existsSync(cargoPath)) {
27
+ try {
28
+ const txt = readFileSync(cargoPath, "utf-8");
29
+ const block = txt.match(/\[dependencies\][\s\S]*?(?:\n\[|$)/)?.[0] || "";
30
+ for (const line of block.split("\n")) {
31
+ const m = line.match(/^\s*([A-Za-z0-9_-]+)\s*=/);
32
+ if (m) {
33
+ const tag = m[1].toLowerCase();
34
+ vocab[tag] = [tag];
35
+ }
36
+ }
37
+ } catch {
38
+ }
39
+ }
40
+ for (const file of ["requirements.txt", "pyproject.toml"]) {
41
+ const p = join(root, file);
42
+ if (!existsSync(p)) continue;
43
+ try {
44
+ const txt = readFileSync(p, "utf-8");
45
+ for (const raw of txt.split("\n")) {
46
+ const name = raw.trim().split(/[<>=!~ \[\]]/)[0].toLowerCase();
47
+ if (!name || name.startsWith("#") || name.includes("[")) continue;
48
+ vocab[name] = [name];
49
+ }
50
+ } catch {
51
+ }
52
+ }
53
+ const goPath = join(root, "go.mod");
54
+ if (existsSync(goPath)) {
55
+ try {
56
+ const txt = readFileSync(goPath, "utf-8");
57
+ for (const raw of txt.split("\n")) {
58
+ const m = raw.match(/^\s*([A-Za-z0-9_./-]+)\s+v[0-9]/);
59
+ if (m) {
60
+ const full = m[1];
61
+ const tag = full.split("/").pop().toLowerCase();
62
+ vocab[tag] = [tag, full.toLowerCase()];
63
+ }
64
+ }
65
+ } catch {
66
+ }
67
+ }
68
+ return vocab;
69
+ }
70
+
71
+ // src/cli/setup.ts
7
72
  var __dirname = dirname(fileURLToPath(import.meta.url));
8
73
  var projectRoot = process.cwd();
9
- var sourceDir = join(__dirname, "..", "..", "src");
74
+ var sourceDir = join2(__dirname, "..", "..", "src");
10
75
  var HOME = process.env.HOME || process.env.USERPROFILE || "~";
11
- var MEMORY_DIR = join(projectRoot, ".toon-memory", "memory");
12
- var CAPTURE_JS = join(__dirname, "capture.js");
13
- var SESSION_START_JS = join(__dirname, "session-start.js");
76
+ var MEMORY_DIR = join2(projectRoot, ".toon-memory", "memory");
77
+ var CAPTURE_JS = join2(__dirname, "capture.js");
78
+ var SESSION_START_JS = join2(__dirname, "session-start.js");
14
79
  function sessionStartHookContent(agentName) {
15
80
  return `#!/bin/bash
16
81
  node "${SESSION_START_JS}" ${agentName}
17
82
  exit 0
18
83
  `;
19
84
  }
20
- var CAPTURE_CONFIG = join(MEMORY_DIR, "config.json");
85
+ var CAPTURE_CONFIG = join2(MEMORY_DIR, "config.json");
21
86
  function captureHookContent(agentName) {
22
87
  return `#!/bin/bash
23
88
  CFG="${CAPTURE_CONFIG}"
@@ -49,18 +114,18 @@ function detectAgents() {
49
114
  const agents2 = [];
50
115
  agents2.push({
51
116
  name: "opencode",
52
- global: join(HOME, ".config", "opencode", "opencode.json"),
53
- local: join(projectRoot, ".opencode", "opencode.json"),
117
+ global: join2(HOME, ".config", "opencode", "opencode.json"),
118
+ local: join2(projectRoot, ".opencode", "opencode.json"),
54
119
  mcpKey: "mcp",
55
120
  format: "json",
56
121
  needsHooks: true,
57
122
  needsInstructions: true,
58
- instructionFile: join(projectRoot, "AGENTS.md"),
123
+ instructionFile: join2(projectRoot, "AGENTS.md"),
59
124
  captureJson: ["PostToolUse", "Stop"]
60
125
  });
61
126
  agents2.push({
62
127
  name: "vscode/copilot",
63
- local: join(projectRoot, ".vscode", "mcp.json"),
128
+ local: join2(projectRoot, ".vscode", "mcp.json"),
64
129
  mcpKey: "servers",
65
130
  format: "json",
66
131
  needsHooks: false,
@@ -68,18 +133,18 @@ function detectAgents() {
68
133
  });
69
134
  agents2.push({
70
135
  name: "claude",
71
- global: join(HOME, ".claude", "settings.json"),
72
- local: join(projectRoot, ".claude", "settings.json"),
136
+ global: join2(HOME, ".claude", "settings.json"),
137
+ local: join2(projectRoot, ".claude", "settings.json"),
73
138
  mcpKey: "mcpServers",
74
139
  format: "json",
75
140
  needsHooks: true,
76
141
  needsInstructions: true,
77
- instructionFile: join(projectRoot, ".claude", "AGENTS.md"),
142
+ instructionFile: join2(projectRoot, ".claude", "AGENTS.md"),
78
143
  captureJson: ["PostToolUse", "Stop"]
79
144
  });
80
145
  agents2.push({
81
146
  name: "cursor",
82
- local: join(projectRoot, ".cursor", "mcp.json"),
147
+ local: join2(projectRoot, ".cursor", "mcp.json"),
83
148
  mcpKey: "mcpServers",
84
149
  format: "json",
85
150
  needsHooks: false,
@@ -87,7 +152,7 @@ function detectAgents() {
87
152
  });
88
153
  agents2.push({
89
154
  name: "windsurf",
90
- global: join(HOME, ".codeium", "windsurf", "mcp_config.json"),
155
+ global: join2(HOME, ".codeium", "windsurf", "mcp_config.json"),
91
156
  mcpKey: "mcpServers",
92
157
  format: "json",
93
158
  needsHooks: false,
@@ -95,7 +160,7 @@ function detectAgents() {
95
160
  });
96
161
  agents2.push({
97
162
  name: "cline",
98
- local: join(projectRoot, ".cline", "mcp.json"),
163
+ local: join2(projectRoot, ".cline", "mcp.json"),
99
164
  mcpKey: "mcpServers",
100
165
  format: "json",
101
166
  needsHooks: false,
@@ -103,7 +168,7 @@ function detectAgents() {
103
168
  });
104
169
  agents2.push({
105
170
  name: "continue",
106
- local: join(projectRoot, ".continue", "config.json"),
171
+ local: join2(projectRoot, ".continue", "config.json"),
107
172
  mcpKey: "mcpServers",
108
173
  format: "json",
109
174
  needsHooks: false,
@@ -111,26 +176,26 @@ function detectAgents() {
111
176
  });
112
177
  agents2.push({
113
178
  name: "codex",
114
- local: join(projectRoot, ".codex", "config.toml"),
179
+ local: join2(projectRoot, ".codex", "config.toml"),
115
180
  mcpKey: "mcpServers",
116
181
  format: "toml",
117
182
  needsHooks: true,
118
183
  needsInstructions: true,
119
- instructionFile: join(projectRoot, ".codex", "AGENTS.md"),
184
+ instructionFile: join2(projectRoot, ".codex", "AGENTS.md"),
120
185
  captureToml: ["post_tool_use", "stop"]
121
186
  });
122
187
  agents2.push({
123
188
  name: "gemini",
124
- local: join(projectRoot, ".gemini", "settings.json"),
189
+ local: join2(projectRoot, ".gemini", "settings.json"),
125
190
  mcpKey: "mcpServers",
126
191
  format: "json",
127
192
  needsHooks: true,
128
193
  needsInstructions: true,
129
- instructionFile: join(projectRoot, ".gemini", "GEMINI.md")
194
+ instructionFile: join2(projectRoot, ".gemini", "GEMINI.md")
130
195
  });
131
196
  agents2.push({
132
197
  name: "zed",
133
- global: join(HOME, ".config", "zed", "settings.json"),
198
+ global: join2(HOME, ".config", "zed", "settings.json"),
134
199
  mcpKey: "mcp_servers",
135
200
  format: "jsonc",
136
201
  needsHooks: false,
@@ -138,12 +203,12 @@ function detectAgents() {
138
203
  });
139
204
  agents2.push({
140
205
  name: "antigravity",
141
- local: join(projectRoot, ".gemini", "config", "mcp_config.json"),
206
+ local: join2(projectRoot, ".gemini", "config", "mcp_config.json"),
142
207
  mcpKey: "mcpServers",
143
208
  format: "json",
144
209
  needsHooks: true,
145
210
  needsInstructions: true,
146
- instructionFile: join(projectRoot, "antigravity-cli", "AGENTS.md")
211
+ instructionFile: join2(projectRoot, "antigravity-cli", "AGENTS.md")
147
212
  });
148
213
  agents2.push({
149
214
  name: "aider",
@@ -151,20 +216,20 @@ function detectAgents() {
151
216
  format: "none",
152
217
  needsHooks: false,
153
218
  needsInstructions: true,
154
- instructionFile: join(projectRoot, "CONVENTIONS.md")
219
+ instructionFile: join2(projectRoot, "CONVENTIONS.md")
155
220
  });
156
221
  agents2.push({
157
222
  name: "kilocode",
158
- global: join(HOME, ".kilocode", "mcp_settings.json"),
223
+ global: join2(HOME, ".kilocode", "mcp_settings.json"),
159
224
  mcpKey: "mcpServers",
160
225
  format: "json",
161
226
  needsHooks: false,
162
227
  needsInstructions: true,
163
- instructionFile: join(HOME, ".kilocode", "rules", "toon-memory.md")
228
+ instructionFile: join2(HOME, ".kilocode", "rules", "toon-memory.md")
164
229
  });
165
230
  agents2.push({
166
231
  name: "openclaw",
167
- local: join(projectRoot, ".openclaw.json"),
232
+ local: join2(projectRoot, ".openclaw.json"),
168
233
  mcpKey: "mcpServers",
169
234
  format: "json",
170
235
  needsHooks: false,
@@ -172,7 +237,7 @@ function detectAgents() {
172
237
  });
173
238
  agents2.push({
174
239
  name: "kiro",
175
- local: join(projectRoot, ".kiro", "settings", "mcp.json"),
240
+ local: join2(projectRoot, ".kiro", "settings", "mcp.json"),
176
241
  mcpKey: "mcpServers",
177
242
  format: "json",
178
243
  needsHooks: false,
@@ -181,23 +246,23 @@ function detectAgents() {
181
246
  return agents2;
182
247
  }
183
248
  function installMemoryDir() {
184
- const memoryFile = join(MEMORY_DIR, "data.toon");
185
- if (!existsSync(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
186
- if (!existsSync(memoryFile)) {
249
+ const memoryFile = join2(MEMORY_DIR, "data.toon");
250
+ if (!existsSync2(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
251
+ if (!existsSync2(memoryFile)) {
187
252
  writeFileSync(memoryFile, "version: 1\n[0|]\n");
188
253
  console.log(" Created .toon-memory/memory/data.toon");
189
254
  }
190
255
  }
191
256
  function ensureGitignore() {
192
- const gitignorePath = join(projectRoot, ".gitignore");
257
+ const gitignorePath = join2(projectRoot, ".gitignore");
193
258
  const entry = ".toon-memory/memory/";
194
- if (!existsSync(gitignorePath)) {
259
+ if (!existsSync2(gitignorePath)) {
195
260
  writeFileSync(gitignorePath, `${entry}
196
261
  `);
197
262
  console.log(" Created .gitignore with memory exclusion");
198
263
  return;
199
264
  }
200
- const content = readFileSync(gitignorePath, "utf-8");
265
+ const content = readFileSync2(gitignorePath, "utf-8");
201
266
  if (!content.includes(entry)) {
202
267
  writeFileSync(gitignorePath, `${content.trim()}
203
268
  ${entry}
@@ -212,11 +277,11 @@ function installJSONConfig(agent, scope) {
212
277
  return;
213
278
  }
214
279
  const configDir = dirname(configPath);
215
- if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true });
280
+ if (!existsSync2(configDir)) mkdirSync(configDir, { recursive: true });
216
281
  let config = {};
217
- if (existsSync(configPath)) {
282
+ if (existsSync2(configPath)) {
218
283
  try {
219
- config = JSON.parse(readFileSync(configPath, "utf-8"));
284
+ config = JSON.parse(readFileSync2(configPath, "utf-8"));
220
285
  } catch {
221
286
  config = {};
222
287
  }
@@ -242,7 +307,7 @@ function installTOMLConfig(agent) {
242
307
  const configPath = agent.local;
243
308
  if (!configPath) return;
244
309
  const configDir = dirname(configPath);
245
- if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true });
310
+ if (!existsSync2(configDir)) mkdirSync(configDir, { recursive: true });
246
311
  const toml = `[mcpServers.toon-memory]
247
312
  command = "npx"
248
313
  args = ["-y", "toon-memory", "mcp"]
@@ -254,11 +319,11 @@ function installZedConfig(agent) {
254
319
  const configPath = agent.global;
255
320
  if (!configPath) return;
256
321
  const configDir = dirname(configPath);
257
- if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true });
322
+ if (!existsSync2(configDir)) mkdirSync(configDir, { recursive: true });
258
323
  let config = {};
259
- if (existsSync(configPath)) {
324
+ if (existsSync2(configPath)) {
260
325
  try {
261
- const raw = readFileSync(configPath, "utf-8");
326
+ const raw = readFileSync2(configPath, "utf-8");
262
327
  const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
263
328
  config = JSON.parse(stripped);
264
329
  } catch {
@@ -290,9 +355,9 @@ function installInstructions(agent) {
290
355
  if (!agent.needsInstructions || !agent.instructionFile) return;
291
356
  const filePath = agent.instructionFile;
292
357
  const fileDir = dirname(filePath);
293
- if (!existsSync(fileDir)) mkdirSync(fileDir, { recursive: true });
294
- if (existsSync(filePath)) {
295
- const existing = readFileSync(filePath, "utf-8");
358
+ if (!existsSync2(fileDir)) mkdirSync(fileDir, { recursive: true });
359
+ if (existsSync2(filePath)) {
360
+ const existing = readFileSync2(filePath, "utf-8");
296
361
  if (existing.includes("toon-memory") || existing.includes("memory_recall")) {
297
362
  console.log(` Instructions already present in ${filePath}`);
298
363
  return;
@@ -308,9 +373,9 @@ ${INSTRUCTION_CONTENT}`);
308
373
  }
309
374
  function installHooks(agent) {
310
375
  if (!agent.needsHooks) return;
311
- const hookDir = join(projectRoot, ".toon-memory", "hooks");
312
- if (!existsSync(hookDir)) mkdirSync(hookDir, { recursive: true });
313
- const hookPath = join(hookDir, `session-start-${agent.name}.sh`);
376
+ const hookDir = join2(projectRoot, ".toon-memory", "hooks");
377
+ if (!existsSync2(hookDir)) mkdirSync(hookDir, { recursive: true });
378
+ const hookPath = join2(hookDir, `session-start-${agent.name}.sh`);
314
379
  writeFileSync(hookPath, sessionStartHookContent(agent.name));
315
380
  chmodSync(hookPath, 493);
316
381
  console.log(` Hook script created at ${hookPath}`);
@@ -320,14 +385,14 @@ function installHooks(agent) {
320
385
  registerHookJSON(agent, hookPath);
321
386
  }
322
387
  if (agent.captureJson && agent.format === "json") {
323
- const capPath = join(hookDir, `capture-${agent.name}.sh`);
388
+ const capPath = join2(hookDir, `capture-${agent.name}.sh`);
324
389
  writeFileSync(capPath, captureHookContent(agent.name));
325
390
  chmodSync(capPath, 493);
326
391
  console.log(` Capture hook created at ${capPath}`);
327
392
  registerCaptureHookJSON(agent, capPath);
328
393
  }
329
394
  if (agent.captureToml && agent.format === "toml" && agent.local) {
330
- const capPath = join(hookDir, `capture-${agent.name}.sh`);
395
+ const capPath = join2(hookDir, `capture-${agent.name}.sh`);
331
396
  writeFileSync(capPath, captureHookContent(agent.name));
332
397
  chmodSync(capPath, 493);
333
398
  console.log(` Capture hook created at ${capPath}`);
@@ -338,11 +403,11 @@ function registerCaptureHookJSON(agent, scriptPath) {
338
403
  const configPath = agent.local || agent.global;
339
404
  if (!configPath) return;
340
405
  const configDir = dirname(configPath);
341
- if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true });
406
+ if (!existsSync2(configDir)) mkdirSync(configDir, { recursive: true });
342
407
  let config = {};
343
- if (existsSync(configPath)) {
408
+ if (existsSync2(configPath)) {
344
409
  try {
345
- config = JSON.parse(readFileSync(configPath, "utf-8"));
410
+ config = JSON.parse(readFileSync2(configPath, "utf-8"));
346
411
  } catch {
347
412
  config = {};
348
413
  }
@@ -359,8 +424,8 @@ function registerCaptureHookJSON(agent, scriptPath) {
359
424
  }
360
425
  function registerCaptureHookTOML(agent, scriptPath) {
361
426
  const configPath = agent.local;
362
- if (!configPath || !existsSync(configPath)) return;
363
- let content = readFileSync(configPath, "utf-8");
427
+ if (!configPath || !existsSync2(configPath)) return;
428
+ let content = readFileSync2(configPath, "utf-8");
364
429
  for (const event of agent.captureToml || []) {
365
430
  if (content.includes(`[hooks.${event}]`)) continue;
366
431
  content += `
@@ -373,8 +438,8 @@ command = "${scriptPath}"
373
438
  }
374
439
  function registerHookTOML(agent, hookPath) {
375
440
  const configPath = agent.local;
376
- if (!configPath || !existsSync(configPath)) return;
377
- let content = readFileSync(configPath, "utf-8");
441
+ if (!configPath || !existsSync2(configPath)) return;
442
+ let content = readFileSync2(configPath, "utf-8");
378
443
  if (content.includes("session_start")) {
379
444
  console.log(` Hook already registered in ${configPath}`);
380
445
  return;
@@ -390,16 +455,16 @@ function registerHookJSON(agent, hookPath) {
390
455
  const configPath = agent.format === "jsonc" ? agent.global : agent.local || agent.global;
391
456
  if (!configPath) return;
392
457
  const configDir = dirname(configPath);
393
- if (!existsSync(configDir)) mkdirSync(configDir, { recursive: true });
458
+ if (!existsSync2(configDir)) mkdirSync(configDir, { recursive: true });
394
459
  let config = {};
395
- if (existsSync(configPath)) {
460
+ if (existsSync2(configPath)) {
396
461
  try {
397
462
  if (agent.format === "jsonc") {
398
- const raw = readFileSync(configPath, "utf-8");
463
+ const raw = readFileSync2(configPath, "utf-8");
399
464
  const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
400
465
  config = JSON.parse(stripped);
401
466
  } else {
402
- config = JSON.parse(readFileSync(configPath, "utf-8"));
467
+ config = JSON.parse(readFileSync2(configPath, "utf-8"));
403
468
  }
404
469
  } catch {
405
470
  config = {};
@@ -428,9 +493,28 @@ function installForAgent(agent, scope) {
428
493
  installInstructions(agent);
429
494
  installHooks(agent);
430
495
  }
496
+ function updateMemoryConfig(patch) {
497
+ if (!existsSync2(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
498
+ let cfg = {};
499
+ if (existsSync2(CAPTURE_CONFIG)) {
500
+ try {
501
+ cfg = JSON.parse(readFileSync2(CAPTURE_CONFIG, "utf-8"));
502
+ } catch {
503
+ cfg = {};
504
+ }
505
+ }
506
+ Object.assign(cfg, patch);
507
+ writeFileSync(CAPTURE_CONFIG, JSON.stringify(cfg, null, 2));
508
+ }
431
509
  function init(scope = "local") {
432
510
  console.log("\n\u{1F9E0} toon-memory init\n");
433
511
  installMemoryDir();
512
+ const deps = extractProjectDeps(projectRoot);
513
+ const depCount = Object.keys(deps).length;
514
+ if (depCount > 0) {
515
+ updateMemoryConfig({ vocab: deps });
516
+ console.log(` Detected ${depCount} dependencies \u2192 auto-tag vocabulary written to config.json`);
517
+ }
434
518
  const agents2 = detectAgents();
435
519
  for (const agent of agents2) {
436
520
  installForAgent(agent, scope);
@@ -446,15 +530,15 @@ function uninstall() {
446
530
  if (agent.format === "json" || agent.format === "jsonc") {
447
531
  const configs = [agent.global, agent.local].filter(Boolean);
448
532
  for (const configPath of configs) {
449
- if (!existsSync(configPath)) continue;
533
+ if (!existsSync2(configPath)) continue;
450
534
  try {
451
535
  let config;
452
536
  if (agent.format === "jsonc") {
453
- const raw = readFileSync(configPath, "utf-8");
537
+ const raw = readFileSync2(configPath, "utf-8");
454
538
  const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
455
539
  config = JSON.parse(stripped);
456
540
  } else {
457
- config = JSON.parse(readFileSync(configPath, "utf-8"));
541
+ config = JSON.parse(readFileSync2(configPath, "utf-8"));
458
542
  }
459
543
  const mcpKey = agent.mcpKey || "mcpServers";
460
544
  if (config[mcpKey]?.["toon-memory"]) {
@@ -466,9 +550,9 @@ function uninstall() {
466
550
  }
467
551
  }
468
552
  }
469
- if (agent.format === "toml" && agent.local && existsSync(agent.local)) {
553
+ if (agent.format === "toml" && agent.local && existsSync2(agent.local)) {
470
554
  try {
471
- let content = readFileSync(agent.local, "utf-8");
555
+ let content = readFileSync2(agent.local, "utf-8");
472
556
  content = content.replace(/\[mcpServers\.toon-memory\][\s\S]*?(?=\n\[|$)/, "").trim() + "\n";
473
557
  writeFileSync(agent.local, content);
474
558
  console.log(` Removed MCP from ${agent.name} (${agent.local})`);
@@ -477,9 +561,9 @@ function uninstall() {
477
561
  }
478
562
  }
479
563
  for (const agent of agents2) {
480
- if (agent.instructionFile && existsSync(agent.instructionFile)) {
564
+ if (agent.instructionFile && existsSync2(agent.instructionFile)) {
481
565
  try {
482
- const content = readFileSync(agent.instructionFile, "utf-8");
566
+ const content = readFileSync2(agent.instructionFile, "utf-8");
483
567
  if (content.includes(INSTRUCTION_CONTENT.trim()) && content.length < INSTRUCTION_CONTENT.length + 100) {
484
568
  unlinkSync(agent.instructionFile);
485
569
  console.log(` Removed ${agent.instructionFile}`);
@@ -488,13 +572,13 @@ function uninstall() {
488
572
  }
489
573
  }
490
574
  }
491
- const hookDir = join(projectRoot, ".toon-memory", "hooks");
492
- if (existsSync(hookDir)) {
575
+ const hookDir = join2(projectRoot, ".toon-memory", "hooks");
576
+ if (existsSync2(hookDir)) {
493
577
  rmSync(hookDir, { recursive: true, force: true });
494
578
  console.log(" Removed .toon-memory/hooks/");
495
579
  }
496
- const toolsFile = join(projectRoot, ".opencode", "tools", "memory.ts");
497
- if (existsSync(toolsFile)) {
580
+ const toolsFile = join2(projectRoot, ".opencode", "tools", "memory.ts");
581
+ if (existsSync2(toolsFile)) {
498
582
  unlinkSync(toolsFile);
499
583
  console.log(" Removed .opencode/tools/memory.ts");
500
584
  }
@@ -503,14 +587,14 @@ function uninstall() {
503
587
  function status() {
504
588
  console.log("\n\u{1F9E0} toon-memory status\n");
505
589
  try {
506
- const pkg = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf-8"));
590
+ const pkg = JSON.parse(readFileSync2(join2(__dirname, "..", "package.json"), "utf-8"));
507
591
  console.log(`Version: ${pkg.version}`);
508
592
  } catch {
509
593
  console.log("Version: unknown");
510
594
  }
511
- const memoryFile = join(MEMORY_DIR, "data.toon");
512
- if (existsSync(memoryFile)) {
513
- const data = readFileSync(memoryFile, "utf-8");
595
+ const memoryFile = join2(MEMORY_DIR, "data.toon");
596
+ if (existsSync2(memoryFile)) {
597
+ const data = readFileSync2(memoryFile, "utf-8");
514
598
  const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
515
599
  console.log(`Memory: ${lines.length} entries`);
516
600
  } else {
@@ -518,9 +602,9 @@ function status() {
518
602
  }
519
603
  let capture = false;
520
604
  if (process.env.TOON_MEMORY_CAPTURE) capture = true;
521
- else if (existsSync(CAPTURE_CONFIG)) {
605
+ else if (existsSync2(CAPTURE_CONFIG)) {
522
606
  try {
523
- const c = JSON.parse(readFileSync(CAPTURE_CONFIG, "utf-8"));
607
+ const c = JSON.parse(readFileSync2(CAPTURE_CONFIG, "utf-8"));
524
608
  if (c.capture) capture = true;
525
609
  } catch {
526
610
  }
@@ -533,15 +617,15 @@ function status() {
533
617
  if (agent.format === "json" || agent.format === "jsonc") {
534
618
  const configs = [agent.global, agent.local].filter(Boolean);
535
619
  for (const configPath of configs) {
536
- if (!existsSync(configPath)) continue;
620
+ if (!existsSync2(configPath)) continue;
537
621
  try {
538
622
  let config;
539
623
  if (agent.format === "jsonc") {
540
- const raw = readFileSync(configPath, "utf-8");
624
+ const raw = readFileSync2(configPath, "utf-8");
541
625
  const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
542
626
  config = JSON.parse(stripped);
543
627
  } else {
544
- config = JSON.parse(readFileSync(configPath, "utf-8"));
628
+ config = JSON.parse(readFileSync2(configPath, "utf-8"));
545
629
  }
546
630
  if (config[agent.mcpKey]?.["toon-memory"]) {
547
631
  configured = true;
@@ -550,13 +634,13 @@ function status() {
550
634
  }
551
635
  }
552
636
  }
553
- if (agent.format === "toml" && agent.local && existsSync(agent.local)) {
554
- const content = readFileSync(agent.local, "utf-8");
637
+ if (agent.format === "toml" && agent.local && existsSync2(agent.local)) {
638
+ const content = readFileSync2(agent.local, "utf-8");
555
639
  if (content.includes("toon-memory")) configured = true;
556
640
  }
557
- const hasInstructions = agent.instructionFile ? existsSync(agent.instructionFile) : false;
558
- const hookPath = join(projectRoot, ".toon-memory", "hooks", `session-start-${agent.name}.sh`);
559
- const hasHooks = existsSync(hookPath);
641
+ const hasInstructions = agent.instructionFile ? existsSync2(agent.instructionFile) : false;
642
+ const hookPath = join2(projectRoot, ".toon-memory", "hooks", `session-start-${agent.name}.sh`);
643
+ const hasHooks = existsSync2(hookPath);
560
644
  if (agent.format === "none") {
561
645
  console.log(` ${hasInstructions ? "\u2705" : "\u274C"} ${agent.name} (instructions only)`);
562
646
  } else {
@@ -571,7 +655,7 @@ function status() {
571
655
  function upgrade() {
572
656
  console.log("\n\u{1F9E0} toon-memory upgrade\n");
573
657
  try {
574
- const pkg = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf-8"));
658
+ const pkg = JSON.parse(readFileSync2(join2(__dirname, "..", "package.json"), "utf-8"));
575
659
  const currentVersion = pkg.version;
576
660
  console.log(`Current version: ${currentVersion}`);
577
661
  console.log("\nTo upgrade, run:");
@@ -585,12 +669,12 @@ function upgrade() {
585
669
  }
586
670
  function stats() {
587
671
  console.log("\n\u{1F9E0} toon-memory stats\n");
588
- const memoryFile = join(MEMORY_DIR, "data.toon");
589
- if (!existsSync(memoryFile)) {
672
+ const memoryFile = join2(MEMORY_DIR, "data.toon");
673
+ if (!existsSync2(memoryFile)) {
590
674
  console.log("Memory not initialized. Run 'npx toon-memory init' first.\n");
591
675
  return;
592
676
  }
593
- const data = readFileSync(memoryFile, "utf-8");
677
+ const data = readFileSync2(memoryFile, "utf-8");
594
678
  const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
595
679
  const categories = {};
596
680
  let latestDate = "";
@@ -616,12 +700,12 @@ function stats() {
616
700
  }
617
701
  function exportMemory() {
618
702
  console.log("\n\u{1F9E0} toon-memory export\n");
619
- const memoryFile = join(MEMORY_DIR, "data.toon");
620
- if (!existsSync(memoryFile)) {
703
+ const memoryFile = join2(MEMORY_DIR, "data.toon");
704
+ if (!existsSync2(memoryFile)) {
621
705
  console.log("Memory not initialized. Run 'npx toon-memory init' first.\n");
622
706
  return;
623
707
  }
624
- const data = readFileSync(memoryFile, "utf-8");
708
+ const data = readFileSync2(memoryFile, "utf-8");
625
709
  const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|"));
626
710
  const entries = lines.map((line) => {
627
711
  const parts = line.trim().split("|");
@@ -641,7 +725,7 @@ function exportMemory() {
641
725
  entries,
642
726
  summaries: {}
643
727
  };
644
- const outputPath = join(projectRoot, "toon-memory-export.json");
728
+ const outputPath = join2(projectRoot, "toon-memory-export.json");
645
729
  writeFileSync(outputPath, JSON.stringify(exportData, null, 2));
646
730
  console.log(`Exported ${entries.length} entries to:`);
647
731
  console.log(` ${outputPath}
@@ -654,15 +738,15 @@ function importMemory() {
654
738
  console.log("Usage: npx toon-memory import <file.json>\n");
655
739
  return;
656
740
  }
657
- const importPath = importFile.startsWith("/") ? importFile : join(projectRoot, importFile);
658
- if (!existsSync(importPath)) {
741
+ const importPath = importFile.startsWith("/") ? importFile : join2(projectRoot, importFile);
742
+ if (!existsSync2(importPath)) {
659
743
  console.log(`File not found: ${importPath}
660
744
  `);
661
745
  return;
662
746
  }
663
747
  let importData;
664
748
  try {
665
- importData = JSON.parse(readFileSync(importPath, "utf-8"));
749
+ importData = JSON.parse(readFileSync2(importPath, "utf-8"));
666
750
  } catch {
667
751
  console.log("Invalid JSON file\n");
668
752
  return;
@@ -671,11 +755,11 @@ function importMemory() {
671
755
  console.log("Invalid format: missing 'entries' array\n");
672
756
  return;
673
757
  }
674
- const memoryFile = join(MEMORY_DIR, "data.toon");
675
- if (!existsSync(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
758
+ const memoryFile = join2(MEMORY_DIR, "data.toon");
759
+ if (!existsSync2(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
676
760
  let existingKeys = [];
677
- if (existsSync(memoryFile)) {
678
- const existing = readFileSync(memoryFile, "utf-8");
761
+ if (existsSync2(memoryFile)) {
762
+ const existing = readFileSync2(memoryFile, "utf-8");
679
763
  existingKeys = existing.split("\n").filter((l) => l.startsWith(" ") && l.includes("|")).map((l) => l.trim().split("|")[2]);
680
764
  }
681
765
  const newEntries = importData.entries.filter((e) => !existingKeys.includes(e.key));
@@ -687,8 +771,8 @@ function importMemory() {
687
771
  const tags = Array.isArray(e.tags) ? e.tags.join(";") : e.tags || "";
688
772
  return ` ${e.id}|${e.category}|${e.key}|${e.content}|${e.file}|${tags}|${e.date}`;
689
773
  }).join("\n");
690
- if (existsSync(memoryFile)) {
691
- const existing = readFileSync(memoryFile, "utf-8");
774
+ if (existsSync2(memoryFile)) {
775
+ const existing = readFileSync2(memoryFile, "utf-8");
692
776
  const existingCount = existing.split("\n").filter((l) => l.startsWith(" ") && l.includes("|")).length;
693
777
  const updated = existing.replace(
694
778
  /entries\[\d+\|]/,
@@ -719,7 +803,7 @@ function parseWatchOptions(args2) {
719
803
  opts.compress = true;
720
804
  } else if (arg === "--log" || arg === "-l") {
721
805
  opts.logFile = true;
722
- opts.logPath = args2[++i] || join(MEMORY_DIR, "watch.log");
806
+ opts.logPath = args2[++i] || join2(MEMORY_DIR, "watch.log");
723
807
  } else if (arg === "--max-backups" || arg === "-m") {
724
808
  opts.maxBackups = parseInt(args2[++i]) || 10;
725
809
  } else if (!arg.startsWith("-")) {
@@ -736,8 +820,8 @@ function writeWatchLog(logPath, message) {
736
820
  writeFileSync(logPath, logLine, { flag: "a" });
737
821
  }
738
822
  function getBackupFiles(backupDir) {
739
- if (!existsSync(backupDir)) return [];
740
- return readdirSync(backupDir).filter((f) => f.startsWith("backup-") && (f.endsWith(".toon") || f.endsWith(".gz"))).map((f) => join(backupDir, f)).sort((a, b) => statSync(a).mtimeMs - statSync(b).mtimeMs);
823
+ if (!existsSync2(backupDir)) return [];
824
+ return readdirSync(backupDir).filter((f) => f.startsWith("backup-") && (f.endsWith(".toon") || f.endsWith(".gz"))).map((f) => join2(backupDir, f)).sort((a, b) => statSync(a).mtimeMs - statSync(b).mtimeMs);
741
825
  }
742
826
  function pruneBackups(backupDir, maxBackups) {
743
827
  if (maxBackups <= 0) return 0;
@@ -754,17 +838,17 @@ function compressData(data) {
754
838
  }
755
839
  function watch() {
756
840
  console.log("\n\u{1F9E0} toon-memory watch\n");
757
- const memoryFile = join(MEMORY_DIR, "data.toon");
758
- const backupDir = join(MEMORY_DIR, "backups");
759
- if (!existsSync(memoryFile)) {
841
+ const memoryFile = join2(MEMORY_DIR, "data.toon");
842
+ const backupDir = join2(MEMORY_DIR, "backups");
843
+ if (!existsSync2(memoryFile)) {
760
844
  console.log("Memory not initialized. Run 'npx toon-memory init' first.\n");
761
845
  return;
762
846
  }
763
- if (!existsSync(backupDir)) mkdirSync(backupDir, { recursive: true });
847
+ if (!existsSync2(backupDir)) mkdirSync(backupDir, { recursive: true });
764
848
  const opts = parseWatchOptions(args);
765
849
  if (opts.logFile) {
766
850
  const logDir = dirname(opts.logPath);
767
- if (!existsSync(logDir)) mkdirSync(logDir, { recursive: true });
851
+ if (!existsSync2(logDir)) mkdirSync(logDir, { recursive: true });
768
852
  }
769
853
  console.log(`Watching memory file every ${opts.interval} minutes...`);
770
854
  console.log(`Max backups: ${opts.maxBackups === 0 ? "unlimited" : opts.maxBackups}`);
@@ -772,18 +856,18 @@ function watch() {
772
856
  console.log(`Logging: ${opts.logFile ? `enabled (${opts.logPath})` : "disabled"}`);
773
857
  console.log(`Press Ctrl+C to stop
774
858
  `);
775
- let lastContent = readFileSync(memoryFile, "utf-8");
859
+ let lastContent = readFileSync2(memoryFile, "utf-8");
776
860
  let lastHash = hashContent(lastContent);
777
861
  let backupCount = 0;
778
862
  if (opts.logFile) writeWatchLog(opts.logPath, "Watch started");
779
863
  const backup = () => {
780
864
  try {
781
- const currentContent = readFileSync(memoryFile, "utf-8");
865
+ const currentContent = readFileSync2(memoryFile, "utf-8");
782
866
  const currentHash = hashContent(currentContent);
783
867
  if (currentHash !== lastHash) {
784
868
  const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
785
869
  const ext = opts.compress ? ".toon.gz" : ".toon";
786
- const backupFile = join(backupDir, `backup-${timestamp}${ext}`);
870
+ const backupFile = join2(backupDir, `backup-${timestamp}${ext}`);
787
871
  if (opts.compress) {
788
872
  writeFileSync(backupFile, compressData(currentContent));
789
873
  } else {
@@ -831,11 +915,11 @@ function watch() {
831
915
  });
832
916
  }
833
917
  function captureToggle(enable) {
834
- if (!existsSync(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
918
+ if (!existsSync2(MEMORY_DIR)) mkdirSync(MEMORY_DIR, { recursive: true });
835
919
  let config = {};
836
- if (existsSync(CAPTURE_CONFIG)) {
920
+ if (existsSync2(CAPTURE_CONFIG)) {
837
921
  try {
838
- config = JSON.parse(readFileSync(CAPTURE_CONFIG, "utf-8"));
922
+ config = JSON.parse(readFileSync2(CAPTURE_CONFIG, "utf-8"));
839
923
  } catch {
840
924
  config = {};
841
925
  }
@@ -891,7 +975,7 @@ var agents = detectAgents();
891
975
  console.log("\n\u{1F9E0} toon-memory installer\n");
892
976
  console.log("Available agents:");
893
977
  agents.forEach((a, i) => {
894
- const hasConfig = a.format !== "none" && (a.local && existsSync(a.local) || a.global && existsSync(a.global));
978
+ const hasConfig = a.format !== "none" && (a.local && existsSync2(a.local) || a.global && existsSync2(a.global));
895
979
  const indicator = hasConfig ? "\u2713" : "\xB7";
896
980
  console.log(` ${indicator} ${i + 1}. ${a.name}`);
897
981
  });