toon-memory 2.11.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  // src/cli/session-start.ts
2
- import { readFileSync as readFileSync2 } from "fs";
2
+ import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
3
+ import { join as join2 } from "path";
3
4
 
4
5
  // src/lib/sessions.ts
5
6
  import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, unlinkSync, statSync } from "fs";
@@ -155,6 +156,32 @@ function coordinationView(selfId) {
155
156
  }
156
157
 
157
158
  // src/cli/session-start.ts
159
+ var MEMORY_DIR = join2(process.cwd(), ".toon-memory", "memory");
160
+ var MEMORY_FILE = join2(MEMORY_DIR, "data.toon");
161
+ function systemPrimer() {
162
+ if (!existsSync2(MEMORY_FILE)) return "";
163
+ try {
164
+ const data = readFileSync2(MEMORY_FILE, "utf-8");
165
+ const lines = data.split("\n").filter((l) => l.startsWith(" ") && l.includes("|") && !l.startsWith(" summaries:"));
166
+ if (lines.length === 0) return "";
167
+ const entries = lines.map((l) => {
168
+ const p = l.trim().split("|");
169
+ if (p.length < 7) return null;
170
+ const accessed = p.length > 8 ? parseInt(p[8]) || 0 : 0;
171
+ const daysSince = (Date.now() - (/* @__PURE__ */ new Date(`${p[6]}T00:00:00`)).getTime()) / 864e5;
172
+ const recency = daysSince < 7 ? 3 : daysSince < 30 ? 2 : daysSince < 90 ? 1 : 0;
173
+ return { key: p[2], content: p[3], category: p[1], tags: p[5] || "", importance: accessed + recency };
174
+ }).filter(Boolean).sort((a, b) => b.importance - a.importance).slice(0, 5);
175
+ if (entries.length === 0) return "";
176
+ const lines2 = [" Top memories:"];
177
+ for (const e of entries) {
178
+ lines2.push(` [${e.category}] ${e.key}: ${e.content.slice(0, 100)}`);
179
+ }
180
+ return lines2.join("\n");
181
+ } catch {
182
+ return "";
183
+ }
184
+ }
158
185
  function main() {
159
186
  const agent = process.argv[2] || "agent";
160
187
  let payload = {};
@@ -195,6 +222,10 @@ function main() {
195
222
  } else {
196
223
  lines.push(" No file conflicts detected.");
197
224
  }
225
+ const primer = systemPrimer();
226
+ if (primer) {
227
+ lines.push(primer);
228
+ }
198
229
  lines.push(" Use memory_recall before reading files and memory_sessions to coordinate.");
199
230
  console.log(lines.join("\n"));
200
231
  process.exit(0);
package/dist/cli/setup.js CHANGED
@@ -165,7 +165,8 @@ function detectAgents() {
165
165
  agents.push({
166
166
  name: "claude",
167
167
  global: join3(HOME, ".claude", "settings.json"),
168
- local: join3(projectRoot, ".claude", "settings.json"),
168
+ local: join3(projectRoot, ".mcp.json"),
169
+ hookPath: join3(projectRoot, ".claude", "settings.json"),
169
170
  mcpKey: "mcpServers",
170
171
  format: "json",
171
172
  needsHooks: true,
@@ -202,8 +203,8 @@ function detectAgents() {
202
203
  agents.push({
203
204
  name: "continue",
204
205
  local: join3(projectRoot, ".continue", "config.json"),
205
- mcpKey: "mcpServers",
206
- format: "json",
206
+ mcpKey: "experimental.modelContextProtocolServers",
207
+ format: "continue",
207
208
  needsHooks: false,
208
209
  needsInstructions: false
209
210
  });
@@ -230,14 +231,14 @@ function detectAgents() {
230
231
  agents.push({
231
232
  name: "zed",
232
233
  global: join3(HOME, ".config", "zed", "settings.json"),
233
- mcpKey: "mcp_servers",
234
+ mcpKey: "context_servers",
234
235
  format: "jsonc",
235
236
  needsHooks: false,
236
237
  needsInstructions: false
237
238
  });
238
239
  agents.push({
239
240
  name: "antigravity",
240
- local: join3(projectRoot, ".gemini", "config", "mcp_config.json"),
241
+ local: join3(projectRoot, ".agents", "mcp_config.json"),
241
242
  mcpKey: "mcpServers",
242
243
  format: "json",
243
244
  needsHooks: true,
@@ -255,7 +256,7 @@ function detectAgents() {
255
256
  agents.push({
256
257
  name: "kilocode",
257
258
  global: join3(HOME, ".kilocode", "mcp_settings.json"),
258
- mcpKey: "mcpServers",
259
+ mcpKey: "mcp",
259
260
  format: "json",
260
261
  needsHooks: false,
261
262
  needsInstructions: true,
@@ -263,9 +264,9 @@ function detectAgents() {
263
264
  });
264
265
  agents.push({
265
266
  name: "openclaw",
266
- local: join3(projectRoot, ".openclaw.json"),
267
- mcpKey: "mcpServers",
268
- format: "json",
267
+ global: join3(HOME, ".openclaw", "openclaw.json"),
268
+ mcpKey: "mcp.servers",
269
+ format: "openclaw",
269
270
  needsHooks: false,
270
271
  needsInstructions: false
271
272
  });
@@ -389,6 +390,11 @@ function installJSONConfig(agent, scope) {
389
390
  type: "local",
390
391
  command: ["npx", "-y", "toon-memory", "mcp"]
391
392
  };
393
+ } else if (agent.name === "kilocode") {
394
+ config[mcpKey]["toon-memory"] = {
395
+ type: "local",
396
+ command: ["npx", "-y", "toon-memory", "mcp"]
397
+ };
392
398
  } else {
393
399
  config[mcpKey]["toon-memory"] = {
394
400
  command: "npx",
@@ -425,11 +431,69 @@ function installZedConfig(agent) {
425
431
  config = {};
426
432
  }
427
433
  }
428
- if (!config.mcp_servers) config.mcp_servers = {};
429
- config.mcp_servers["toon-memory"] = {
434
+ if (!config.context_servers) config.context_servers = {};
435
+ config.context_servers["toon-memory"] = {
436
+ command: "npx",
437
+ args: ["-y", "toon-memory", "mcp"],
438
+ source: "custom"
439
+ };
440
+ writeFileSync3(configPath, JSON.stringify(config, null, 2));
441
+ console.log(` MCP server added to ${configPath}`);
442
+ }
443
+ function installContinueConfig(agent) {
444
+ const configPath = agent.local;
445
+ if (!configPath) return;
446
+ const configDir = dirname3(configPath);
447
+ if (!existsSync5(configDir)) mkdirSync3(configDir, { recursive: true });
448
+ let config = {};
449
+ if (existsSync5(configPath)) {
450
+ try {
451
+ config = JSON.parse(readFileSync5(configPath, "utf-8"));
452
+ } catch {
453
+ config = {};
454
+ }
455
+ }
456
+ if (!config.experimental) config.experimental = {};
457
+ if (!config.experimental.modelContextProtocolServers) {
458
+ config.experimental.modelContextProtocolServers = [];
459
+ }
460
+ const servers = config.experimental.modelContextProtocolServers;
461
+ const existing = servers.findIndex(
462
+ (s) => s.name === "toon-memory"
463
+ );
464
+ const entry = {
465
+ name: "toon-memory",
430
466
  command: "npx",
431
467
  args: ["-y", "toon-memory", "mcp"]
432
468
  };
469
+ if (existing >= 0) {
470
+ servers[existing] = entry;
471
+ } else {
472
+ servers.push(entry);
473
+ }
474
+ writeFileSync3(configPath, JSON.stringify(config, null, 2));
475
+ console.log(` MCP server added to ${configPath}`);
476
+ }
477
+ function installOpenClawConfig(agent) {
478
+ const configPath = agent.global;
479
+ if (!configPath) return;
480
+ const configDir = dirname3(configPath);
481
+ if (!existsSync5(configDir)) mkdirSync3(configDir, { recursive: true });
482
+ let config = {};
483
+ if (existsSync5(configPath)) {
484
+ try {
485
+ config = JSON.parse(readFileSync5(configPath, "utf-8"));
486
+ } catch {
487
+ config = {};
488
+ }
489
+ }
490
+ if (!config.mcp) config.mcp = {};
491
+ if (!config.mcp.servers) config.mcp.servers = {};
492
+ config.mcp.servers["toon-memory"] = {
493
+ command: "npx",
494
+ args: ["-y", "toon-memory", "mcp"],
495
+ enabled: true
496
+ };
433
497
  writeFileSync3(configPath, JSON.stringify(config, null, 2));
434
498
  console.log(` MCP server added to ${configPath}`);
435
499
  }
@@ -442,6 +506,10 @@ function installMCPConfig(agent, scope) {
442
506
  installTOMLConfig(agent);
443
507
  } else if (agent.format === "jsonc") {
444
508
  installZedConfig(agent);
509
+ } else if (agent.format === "continue") {
510
+ installContinueConfig(agent);
511
+ } else if (agent.format === "openclaw") {
512
+ installOpenClawConfig(agent);
445
513
  } else {
446
514
  installJSONConfig(agent, scope);
447
515
  }
@@ -534,7 +602,7 @@ function installHooks(agent) {
534
602
  }
535
603
  }
536
604
  function registerCaptureHookJSON(agent, scriptPath) {
537
- const configPath = agent.local || agent.global;
605
+ const configPath = agent.hookPath || agent.local || agent.global;
538
606
  if (!configPath) return;
539
607
  const configDir = dirname5(configPath);
540
608
  if (!existsSync7(configDir)) mkdirSync5(configDir, { recursive: true });
@@ -588,7 +656,7 @@ command = "${hookPath}"
588
656
  console.log(` Hook registered in ${configPath}`);
589
657
  }
590
658
  function registerHookJSON(agent, hookPath) {
591
- const configPath = agent.format === "jsonc" ? agent.global : agent.local || agent.global;
659
+ const configPath = agent.hookPath || (agent.format === "jsonc" ? agent.global : agent.local || agent.global);
592
660
  if (!configPath) return;
593
661
  const configDir = dirname5(configPath);
594
662
  if (!existsSync7(configDir)) mkdirSync5(configDir, { recursive: true });
@@ -653,18 +721,37 @@ import { existsSync as existsSync8, mkdirSync as mkdirSync6, readFileSync as rea
653
721
  import { join as join6 } from "path";
654
722
  function opencodePluginContent() {
655
723
  return `import * as fs from "fs"
724
+ import * as path from "path"
656
725
 
657
726
  export const ToonMemory = async ({ $, directory, worktree }) => {
658
727
  const root = worktree || directory
659
728
  const INS = root + "/.opencode/instructions"
660
729
  const OUT = INS + "/memory-autoload.md"
730
+
661
731
  const dump = async () => {
662
732
  return await $\`npx -y toon-memory dump\`.cwd(root).text()
663
733
  }
734
+
735
+ const recall = async (query) => {
736
+ try {
737
+ const result = await $\`npx -y toon-memory mcp\`.cwd(root)
738
+ .stdin(JSON.stringify({
739
+ jsonrpc: "2.0",
740
+ id: 1,
741
+ method: "tools/call",
742
+ params: { name: "memory_recall", arguments: { query, mode: "flat", compact: true } }
743
+ }))
744
+ .text()
745
+ const parsed = JSON.parse(result)
746
+ return parsed?.result?.content?.[0]?.text || ""
747
+ } catch { return "" }
748
+ }
749
+
664
750
  const write = (text) => {
665
751
  fs.mkdirSync(INS, { recursive: true })
666
752
  fs.writeFileSync(OUT, text)
667
753
  }
754
+
668
755
  return {
669
756
  "session.created": async () => {
670
757
  try {
@@ -679,15 +766,39 @@ export const ToonMemory = async ({ $, directory, worktree }) => {
679
766
  } catch {}
680
767
  },
681
768
  "tool.execute.after": async (input) => {
682
- if (!process.env.TOON_MEMORY_CAPTURE) return
683
- const cap = directory + "/bin/cli/capture.js"
769
+ // Intelligent auto-loading: extract file path from tool result,
770
+ // recall only memory relevant to that file, inject as context.
684
771
  try {
685
- const payload = JSON.stringify({
686
- session_id: input?.session?.id ?? "",
687
- tool_name: input?.tool ?? "",
688
- tool_input: input?.args ?? {},
689
- })
690
- await $\`node \${cap} opencode\`.stdin(payload).quiet()
772
+ const toolName = input?.tool || ""
773
+ const args = input?.args || {}
774
+
775
+ // Extract file path from tool arguments or output
776
+ let filePath = ""
777
+ if (args.file) filePath = args.file
778
+ else if (args.path) filePath = args.path
779
+ else if (args.filePath) filePath = args.filePath
780
+
781
+ // Also try to extract from tool output text
782
+ if (!filePath && input?.output) {
783
+ const outputText = typeof input.output === "string" ? input.output : ""
784
+ const fileMatch = outputText.match(/(?:file|path|source):s*([\\w/.\\-]+)/i)
785
+ if (fileMatch) filePath = fileMatch[1]
786
+ }
787
+
788
+ if (!filePath) return
789
+
790
+ // Build recall query from file path components
791
+ const basename = path.basename(filePath, path.extname(filePath))
792
+ const dirParts = path.dirname(filePath).split(path.sep).filter(Boolean)
793
+ const query = [basename, ...dirParts.slice(-2)].join(" ")
794
+
795
+ const text = await recall(query)
796
+ if (!text || text.includes("(empty)") || text.includes("No results")) return
797
+
798
+ // Inject as context for the next agent turn
799
+ if (typeof input?.setContext === "function") {
800
+ input.setContext("toon-memory:" + filePath, text)
801
+ }
691
802
  } catch {}
692
803
  },
693
804
  }
@@ -863,7 +974,7 @@ function uninstall() {
863
974
  unlinkSync(ocPlugin);
864
975
  console.log(" Removed .opencode/plugins/toon-memory.ts");
865
976
  }
866
- const agyHooks = join7(projectRoot, ".gemini", "config", "hooks.json");
977
+ const agyHooks = join7(projectRoot, ".agents", "hooks.json");
867
978
  if (existsSync9(agyHooks)) {
868
979
  try {
869
980
  const agyCfg = JSON.parse(readFileSync9(agyHooks, "utf-8"));