@memrosetta/cli 0.5.0 → 0.5.2

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.
Files changed (57) hide show
  1. package/dist/chunk-47SU2YUJ.js +64 -0
  2. package/dist/chunk-4LNXT25H.js +891 -0
  3. package/dist/chunk-C4ANKSCI.js +151 -0
  4. package/dist/chunk-CEHRM6IW.js +151 -0
  5. package/dist/chunk-G2W4YK2T.js +56 -0
  6. package/dist/chunk-GGXC7TAJ.js +139 -0
  7. package/dist/chunk-GRNZVSAF.js +56 -0
  8. package/dist/chunk-GZINXXM4.js +139 -0
  9. package/dist/chunk-RZFCVYTK.js +71 -0
  10. package/dist/chunk-US6CEDMU.js +66 -0
  11. package/dist/chunk-VMGX5FCY.js +64 -0
  12. package/dist/chunk-WYHEAKPC.js +71 -0
  13. package/dist/clear-32Y3U2WR.js +39 -0
  14. package/dist/clear-AFEJPCDA.js +39 -0
  15. package/dist/compress-CL5D4VVJ.js +33 -0
  16. package/dist/compress-UUEO7WCU.js +33 -0
  17. package/dist/count-U2ML5ZON.js +24 -0
  18. package/dist/count-VVOGYSM7.js +24 -0
  19. package/dist/duplicates-CEJ7WSGW.js +149 -0
  20. package/dist/duplicates-IBUS7CJS.js +149 -0
  21. package/dist/enforce-T7AS4PVD.js +381 -0
  22. package/dist/enforce-TC5SDPEZ.js +381 -0
  23. package/dist/feedback-3PJTTEOD.js +51 -0
  24. package/dist/feedback-IB7BHIRP.js +51 -0
  25. package/dist/get-TQ2U7HCD.js +30 -0
  26. package/dist/get-WPZIHQKW.js +30 -0
  27. package/dist/hooks/enforce-codex.js +88 -0
  28. package/dist/hooks/on-prompt.js +3 -3
  29. package/dist/hooks/on-stop.js +3 -3
  30. package/dist/index.js +30 -20
  31. package/dist/ingest-37UXPVT5.js +97 -0
  32. package/dist/ingest-TPQRH34A.js +97 -0
  33. package/dist/init-6YQL3RCQ.js +210 -0
  34. package/dist/init-ISP73KEC.js +210 -0
  35. package/dist/init-LHXRCCLX.js +210 -0
  36. package/dist/invalidate-ER2TFFWK.js +40 -0
  37. package/dist/invalidate-PVHUGAJ6.js +40 -0
  38. package/dist/maintain-NICAXFK6.js +37 -0
  39. package/dist/maintain-Q553GBSF.js +37 -0
  40. package/dist/migrate-CZL3YNQK.js +255 -0
  41. package/dist/migrate-FI26FSBP.js +255 -0
  42. package/dist/relate-5TN2WEG3.js +57 -0
  43. package/dist/relate-KLBMYWB3.js +57 -0
  44. package/dist/reset-IPOAKTJM.js +132 -0
  45. package/dist/reset-P62B444X.js +132 -0
  46. package/dist/search-AYZBKRXF.js +48 -0
  47. package/dist/search-JQ3MLRKS.js +48 -0
  48. package/dist/status-FWHUUZ4R.js +184 -0
  49. package/dist/status-JF2V7ZBX.js +184 -0
  50. package/dist/status-UV66PWUD.js +184 -0
  51. package/dist/store-AAJCT3PX.js +101 -0
  52. package/dist/store-OVDS57U5.js +101 -0
  53. package/dist/sync-56KJTKE7.js +542 -0
  54. package/dist/sync-BCKBYRXY.js +542 -0
  55. package/dist/working-memory-CJARSGEK.js +53 -0
  56. package/dist/working-memory-Z3RUGSTQ.js +53 -0
  57. package/package.json +6 -5
@@ -0,0 +1,891 @@
1
+ // src/integrations/claude-code.ts
2
+ import { join as join2 } from "path";
3
+ import { homedir } from "os";
4
+ import { existsSync as existsSync2, readFileSync, writeFileSync } from "fs";
5
+
6
+ // src/integrations/resolve-command.ts
7
+ import { execSync } from "child_process";
8
+ import { createRequire } from "module";
9
+ import { join, dirname, resolve } from "path";
10
+ import { existsSync } from "fs";
11
+ import { fileURLToPath } from "url";
12
+ function resolveAbsolutePath(command) {
13
+ try {
14
+ const cmd = process.platform === "win32" ? `where ${command}` : `command -v ${command}`;
15
+ return execSync(cmd, { encoding: "utf-8" }).trim().split("\n")[0] || null;
16
+ } catch {
17
+ return null;
18
+ }
19
+ }
20
+ function isInPath(command) {
21
+ try {
22
+ const cmd = process.platform === "win32" ? `where ${command}` : `command -v ${command}`;
23
+ execSync(cmd, { stdio: "ignore" });
24
+ return true;
25
+ } catch {
26
+ return false;
27
+ }
28
+ }
29
+ function findUpwards(startDir, target, maxLevels = 6) {
30
+ let dir = startDir;
31
+ for (let i = 0; i < maxLevels; i++) {
32
+ const candidate = join(dir, target);
33
+ if (existsSync(candidate)) return candidate;
34
+ const parent = dirname(dir);
35
+ if (parent === dir) break;
36
+ dir = parent;
37
+ }
38
+ return null;
39
+ }
40
+ var __dirname = dirname(fileURLToPath(import.meta.url));
41
+ function resolveMcpCommand() {
42
+ if (isInPath("memrosetta-mcp")) {
43
+ const absPath = resolveAbsolutePath("memrosetta-mcp");
44
+ return { command: absPath ?? "memrosetta-mcp", args: [] };
45
+ }
46
+ try {
47
+ const require2 = createRequire(import.meta.url);
48
+ const mcpPkgPath = dirname(
49
+ require2.resolve("@memrosetta/mcp/package.json")
50
+ );
51
+ const entryPoint = join(mcpPkgPath, "dist", "index.js");
52
+ if (existsSync(entryPoint)) {
53
+ return { command: "node", args: [entryPoint] };
54
+ }
55
+ } catch {
56
+ }
57
+ const workspaceRoot = findUpwards(__dirname, "pnpm-workspace.yaml");
58
+ if (workspaceRoot) {
59
+ const rootDir = dirname(workspaceRoot);
60
+ const mcpEntry = join(rootDir, "adapters", "mcp", "dist", "index.js");
61
+ if (existsSync(mcpEntry)) {
62
+ return { command: "node", args: [mcpEntry] };
63
+ }
64
+ const mcpSrc = join(rootDir, "adapters", "mcp", "src", "index.ts");
65
+ if (existsSync(mcpSrc)) {
66
+ return { command: "npx", args: ["tsx", mcpSrc] };
67
+ }
68
+ }
69
+ return { command: "memrosetta-mcp", args: [] };
70
+ }
71
+ function resolveHookCommand(hookName) {
72
+ if (isInPath(hookName)) {
73
+ return resolveAbsolutePath(hookName) ?? hookName;
74
+ }
75
+ const hookFile = hookName === "memrosetta-on-stop" ? "hooks/on-stop.js" : hookName === "memrosetta-on-prompt" ? "hooks/on-prompt.js" : hookName === "memrosetta-enforce-claude-code" ? "hooks/enforce-claude-code.js" : "hooks/enforce-codex.js";
76
+ try {
77
+ const require2 = createRequire(import.meta.url);
78
+ const cliPkgPath = dirname(
79
+ require2.resolve("@memrosetta/cli/package.json")
80
+ );
81
+ const entryPoint = join(cliPkgPath, "dist", hookFile);
82
+ if (existsSync(entryPoint)) {
83
+ return `node "${entryPoint}"`;
84
+ }
85
+ } catch {
86
+ }
87
+ const distHook = resolve(__dirname, "..", "..", "dist", hookFile);
88
+ if (existsSync(distHook)) {
89
+ return `node "${distHook}"`;
90
+ }
91
+ const workspaceRoot = findUpwards(__dirname, "pnpm-workspace.yaml");
92
+ if (workspaceRoot) {
93
+ const rootDir = dirname(workspaceRoot);
94
+ const hookEntry = join(rootDir, "packages", "cli", "dist", hookFile);
95
+ if (existsSync(hookEntry)) {
96
+ return `node "${hookEntry}"`;
97
+ }
98
+ }
99
+ return hookName;
100
+ }
101
+
102
+ // src/integrations/claude-code.ts
103
+ var CLAUDE_DIR = join2(homedir(), ".claude");
104
+ var CLAUDE_SETTINGS_PATH = join2(CLAUDE_DIR, "settings.json");
105
+ var CLAUDE_MD_PATH = join2(CLAUDE_DIR, "CLAUDE.md");
106
+ function isMemrosettaHook(command) {
107
+ return command.includes("memrosetta") && (command.includes("enforce-claude-code") || command.includes("on-stop") || command.includes("on-prompt"));
108
+ }
109
+ function readClaudeSettings() {
110
+ if (!existsSync2(CLAUDE_SETTINGS_PATH)) return {};
111
+ try {
112
+ return JSON.parse(
113
+ readFileSync(CLAUDE_SETTINGS_PATH, "utf-8")
114
+ );
115
+ } catch {
116
+ return {};
117
+ }
118
+ }
119
+ function writeClaudeSettings(settings) {
120
+ if (!existsSync2(CLAUDE_DIR)) {
121
+ throw new Error(
122
+ "~/.claude directory does not exist. Is Claude Code installed?"
123
+ );
124
+ }
125
+ writeFileSync(
126
+ CLAUDE_SETTINGS_PATH,
127
+ JSON.stringify(settings, null, 2),
128
+ "utf-8"
129
+ );
130
+ }
131
+ function removeMemrosettaHooksFromSettings(settings) {
132
+ if (!settings.hooks) return settings;
133
+ const cleaned = {};
134
+ for (const [eventType, hookConfigs] of Object.entries(settings.hooks)) {
135
+ const filtered = hookConfigs.filter(
136
+ (hc) => !hc.hooks.some((h) => isMemrosettaHook(h.command))
137
+ );
138
+ cleaned[eventType] = filtered;
139
+ }
140
+ return { ...settings, hooks: cleaned };
141
+ }
142
+ function isClaudeCodeInstalled() {
143
+ return existsSync2(CLAUDE_DIR);
144
+ }
145
+ function isClaudeCodeConfigured() {
146
+ const settings = readClaudeSettings();
147
+ const stopHooks = settings.hooks?.["Stop"] || [];
148
+ return stopHooks.some(
149
+ (hc) => hc.hooks.some((h) => isMemrosettaHook(h.command))
150
+ );
151
+ }
152
+ function registerClaudeCodeHooks() {
153
+ if (!isClaudeCodeInstalled()) return false;
154
+ let settings = readClaudeSettings();
155
+ if (!settings.hooks) {
156
+ settings = { ...settings, hooks: {} };
157
+ }
158
+ settings = removeMemrosettaHooksFromSettings(settings);
159
+ const stopHookConfigs = settings.hooks["Stop"] || [];
160
+ settings.hooks["Stop"] = [
161
+ ...stopHookConfigs,
162
+ {
163
+ matcher: "*",
164
+ hooks: [
165
+ {
166
+ type: "command",
167
+ command: resolveHookCommand("memrosetta-enforce-claude-code"),
168
+ timeout: 30
169
+ }
170
+ ]
171
+ }
172
+ ];
173
+ writeClaudeSettings(settings);
174
+ return true;
175
+ }
176
+ function removeClaudeCodeHooks() {
177
+ if (!existsSync2(CLAUDE_SETTINGS_PATH)) return false;
178
+ const settings = readClaudeSettings();
179
+ if (!settings.hooks) return false;
180
+ const cleaned = removeMemrosettaHooksFromSettings(settings);
181
+ writeClaudeSettings(cleaned);
182
+ return true;
183
+ }
184
+ function updateClaudeMd() {
185
+ if (!isClaudeCodeInstalled()) return false;
186
+ const marker = "## MemRosetta (Long-term Memory)";
187
+ const existing = existsSync2(CLAUDE_MD_PATH) ? readFileSync(CLAUDE_MD_PATH, "utf-8") : "";
188
+ if (existing.includes(marker)) return false;
189
+ const memorySection = `
190
+
191
+ ${marker}
192
+
193
+ MCP server \`memory-service\` provides long-term memory across sessions.
194
+ userId defaults to the system username -- no need to specify it.
195
+
196
+ ### Search (mcp__memory-service__memrosetta_search)
197
+ When you need information not in the current context, search past memories.
198
+ No need to specify userId -- it defaults to the system username.
199
+
200
+ ### Store (mcp__memory-service__memrosetta_store)
201
+
202
+ **After EVERY response, run this checklist (zero extra cost):**
203
+ 1. Did I encounter a DECISION? (tech choice, approach selection) -> store as "decision"
204
+ 2. Did I learn a new FACT? (config, architecture, project info) -> store as "fact"
205
+ 3. Did the user state a PREFERENCE? (style, tool choice, pattern) -> store as "preference"
206
+ 4. Did we COMPLETE something? (deploy, migration, fix) -> store as "event"
207
+ 5. None of the above? -> skip, do not store.
208
+
209
+ Always include 2-3 keywords. Example:
210
+ content: "Decided to use OAuth2 with PKCE for auth"
211
+ type: "decision"
212
+ keywords: "auth, oauth2, pkce"
213
+
214
+ Do NOT store:
215
+ - Code itself (belongs in git)
216
+ - File operations ("Created file X", "Modified Y")
217
+ - Debugging steps and attempts
218
+ - Simple confirmations or acknowledgments
219
+ - Implementation details (HOW you did it -- only WHAT)
220
+
221
+ This checklist ensures nothing important is lost, including the last response before session ends.
222
+ No need to specify userId -- it defaults to the system username.
223
+
224
+ ### Feedback (mcp__memory-service__memrosetta_feedback)
225
+ After using a retrieved memory, report whether it was helpful:
226
+ - Memory was accurate and useful -> feedback(memoryId, helpful=true)
227
+ - Memory was outdated or wrong -> feedback(memoryId, helpful=false)
228
+ This improves future search ranking automatically.
229
+ `;
230
+ writeFileSync(CLAUDE_MD_PATH, existing + memorySection, "utf-8");
231
+ return true;
232
+ }
233
+ function removeClaudeMdSection() {
234
+ if (!existsSync2(CLAUDE_MD_PATH)) return false;
235
+ const content = readFileSync(CLAUDE_MD_PATH, "utf-8");
236
+ const marker = "## MemRosetta (Long-term Memory)";
237
+ const markerIdx = content.indexOf(marker);
238
+ if (markerIdx === -1) return false;
239
+ const afterMarker = content.slice(markerIdx + marker.length);
240
+ const nextHeadingMatch = afterMarker.match(/\n## (?!MemRosetta)/);
241
+ const endIdx = nextHeadingMatch ? markerIdx + marker.length + (nextHeadingMatch.index ?? afterMarker.length) : content.length;
242
+ const before = content.slice(0, markerIdx).replace(/\n+$/, "");
243
+ const after = content.slice(endIdx);
244
+ const updated = (before + after).trimEnd() + "\n";
245
+ writeFileSync(CLAUDE_MD_PATH, updated, "utf-8");
246
+ return true;
247
+ }
248
+
249
+ // src/integrations/mcp.ts
250
+ import { join as join3 } from "path";
251
+ import { homedir as homedir2 } from "os";
252
+ import { existsSync as existsSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
253
+ var MCP_CONFIG_PATH = join3(homedir2(), ".mcp.json");
254
+ var SERVER_NAME = "memory-service";
255
+ function readMcpConfig(path) {
256
+ if (!existsSync3(path)) return {};
257
+ try {
258
+ return JSON.parse(readFileSync2(path, "utf-8"));
259
+ } catch {
260
+ return {};
261
+ }
262
+ }
263
+ function writeMcpConfig(path, config) {
264
+ writeFileSync2(path, JSON.stringify(config, null, 2), "utf-8");
265
+ }
266
+ function mcpServerEntry() {
267
+ const { command, args } = resolveMcpCommand();
268
+ return { command, args };
269
+ }
270
+ function isGenericMCPConfigured() {
271
+ const config = readMcpConfig(MCP_CONFIG_PATH);
272
+ return !!config.mcpServers?.[SERVER_NAME];
273
+ }
274
+ function registerGenericMCP() {
275
+ const config = readMcpConfig(MCP_CONFIG_PATH);
276
+ const servers = config.mcpServers ?? {};
277
+ writeMcpConfig(MCP_CONFIG_PATH, {
278
+ ...config,
279
+ mcpServers: { ...servers, [SERVER_NAME]: mcpServerEntry() }
280
+ });
281
+ }
282
+ function removeGenericMCP() {
283
+ if (!existsSync3(MCP_CONFIG_PATH)) return false;
284
+ const config = readMcpConfig(MCP_CONFIG_PATH);
285
+ if (!config.mcpServers?.[SERVER_NAME]) return false;
286
+ const { [SERVER_NAME]: _, ...rest } = config.mcpServers;
287
+ writeMcpConfig(MCP_CONFIG_PATH, { ...config, mcpServers: rest });
288
+ return true;
289
+ }
290
+ function getGenericMCPPath() {
291
+ return MCP_CONFIG_PATH;
292
+ }
293
+
294
+ // src/integrations/cursor.ts
295
+ import { join as join4 } from "path";
296
+ import { homedir as homedir3 } from "os";
297
+ import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync3, mkdirSync } from "fs";
298
+ var SERVER_NAME2 = "memory-service";
299
+ var CURSOR_RULES_PATH_GETTER = () => join4(homedir3(), ".cursorrules");
300
+ var MEMROSETTA_CURSOR_RULES_MARKER = "## MemRosetta (Long-term Memory)";
301
+ var MEMROSETTA_CURSOR_RULES = `
302
+
303
+ ${MEMROSETTA_CURSOR_RULES_MARKER}
304
+
305
+ MCP server \`memory-service\` provides persistent memory across sessions.
306
+ userId defaults to the system username -- no need to specify it.
307
+
308
+ ### When to search (memrosetta_search)
309
+ When you need information not in the current context, search past memories.
310
+ No need to specify userId -- it defaults to the system username.
311
+
312
+ ### When to store (memrosetta_store)
313
+
314
+ **After EVERY response, run this checklist (zero extra cost):**
315
+ 1. Did I encounter a DECISION? (tech choice, approach selection) -> store as "decision"
316
+ 2. Did I learn a new FACT? (config, architecture, project info) -> store as "fact"
317
+ 3. Did the user state a PREFERENCE? (style, tool choice, pattern) -> store as "preference"
318
+ 4. Did we COMPLETE something? (deploy, migration, fix) -> store as "event"
319
+ 5. None of the above? -> skip, do not store.
320
+
321
+ Always include 2-3 keywords. Example:
322
+ content: "Decided to use OAuth2 with PKCE for auth"
323
+ type: "decision"
324
+ keywords: "auth, oauth2, pkce"
325
+
326
+ Do NOT store:
327
+ - Code itself (belongs in git)
328
+ - File operations ("Created file X", "Modified Y")
329
+ - Debugging steps and attempts
330
+ - Simple confirmations or acknowledgments
331
+ - Implementation details (HOW you did it -- only WHAT)
332
+
333
+ This checklist ensures nothing important is lost, including the last response before session ends.
334
+ No need to specify userId -- it defaults to the system username.
335
+
336
+ ### When to relate (memrosetta_relate)
337
+ When new information updates or contradicts existing memories, create a relation.
338
+
339
+ ### Working memory (memrosetta_working_memory)
340
+ Call this at the start of complex tasks to load relevant context.
341
+ No need to specify userId -- it defaults to the system username.
342
+
343
+ ### Feedback (memrosetta_feedback)
344
+ After using a retrieved memory, report whether it was helpful:
345
+ - Memory was accurate and useful -> feedback(memoryId, helpful=true)
346
+ - Memory was outdated or wrong -> feedback(memoryId, helpful=false)
347
+ This improves future search ranking automatically.
348
+ `;
349
+ function getCursorConfigDir() {
350
+ return join4(homedir3(), ".cursor");
351
+ }
352
+ function getCursorMcpPath() {
353
+ return join4(getCursorConfigDir(), "mcp.json");
354
+ }
355
+ function readCursorConfig(path) {
356
+ if (!existsSync4(path)) return {};
357
+ try {
358
+ return JSON.parse(readFileSync3(path, "utf-8"));
359
+ } catch {
360
+ return {};
361
+ }
362
+ }
363
+ function writeCursorConfig(path, config) {
364
+ const dir = getCursorConfigDir();
365
+ if (!existsSync4(dir)) {
366
+ mkdirSync(dir, { recursive: true });
367
+ }
368
+ writeFileSync3(path, JSON.stringify(config, null, 2), "utf-8");
369
+ }
370
+ function mcpServerEntry2() {
371
+ const { command, args } = resolveMcpCommand();
372
+ return { command, args };
373
+ }
374
+ function isCursorConfigured() {
375
+ const path = getCursorMcpPath();
376
+ const config = readCursorConfig(path);
377
+ return !!config.mcpServers?.[SERVER_NAME2];
378
+ }
379
+ function registerCursorMCP() {
380
+ const path = getCursorMcpPath();
381
+ const config = readCursorConfig(path);
382
+ const servers = config.mcpServers ?? {};
383
+ writeCursorConfig(path, {
384
+ ...config,
385
+ mcpServers: { ...servers, [SERVER_NAME2]: mcpServerEntry2() }
386
+ });
387
+ return updateCursorRules();
388
+ }
389
+ function removeCursorMCP() {
390
+ const path = getCursorMcpPath();
391
+ if (!existsSync4(path)) return false;
392
+ const config = readCursorConfig(path);
393
+ if (!config.mcpServers?.[SERVER_NAME2]) return false;
394
+ const { [SERVER_NAME2]: _, ...rest } = config.mcpServers;
395
+ writeCursorConfig(path, { ...config, mcpServers: rest });
396
+ removeCursorRulesSection();
397
+ return true;
398
+ }
399
+ function getCursorMcpConfigPath() {
400
+ return getCursorMcpPath();
401
+ }
402
+ function getCursorRulesPath() {
403
+ return CURSOR_RULES_PATH_GETTER();
404
+ }
405
+ function updateCursorRules() {
406
+ const rulesPath = CURSOR_RULES_PATH_GETTER();
407
+ const existing = existsSync4(rulesPath) ? readFileSync3(rulesPath, "utf-8") : "";
408
+ if (existing.includes(MEMROSETTA_CURSOR_RULES_MARKER)) return false;
409
+ writeFileSync3(rulesPath, existing + MEMROSETTA_CURSOR_RULES, "utf-8");
410
+ return true;
411
+ }
412
+ function removeCursorRulesSection() {
413
+ const rulesPath = CURSOR_RULES_PATH_GETTER();
414
+ if (!existsSync4(rulesPath)) return false;
415
+ const content = readFileSync3(rulesPath, "utf-8");
416
+ const markerIdx = content.indexOf(MEMROSETTA_CURSOR_RULES_MARKER);
417
+ if (markerIdx === -1) return false;
418
+ const afterMarker = content.slice(
419
+ markerIdx + MEMROSETTA_CURSOR_RULES_MARKER.length
420
+ );
421
+ const nextHeadingMatch = afterMarker.match(/\n## (?!MemRosetta)/);
422
+ const endIdx = nextHeadingMatch ? markerIdx + MEMROSETTA_CURSOR_RULES_MARKER.length + (nextHeadingMatch.index ?? afterMarker.length) : content.length;
423
+ const before = content.slice(0, markerIdx).replace(/\n+$/, "");
424
+ const after = content.slice(endIdx);
425
+ const updated = (before + after).trimEnd() + "\n";
426
+ writeFileSync3(rulesPath, updated, "utf-8");
427
+ return true;
428
+ }
429
+
430
+ // src/integrations/codex.ts
431
+ import { join as join5 } from "path";
432
+ import { homedir as homedir4 } from "os";
433
+ import { existsSync as existsSync5, readFileSync as readFileSync4, writeFileSync as writeFileSync4, mkdirSync as mkdirSync2 } from "fs";
434
+ var SERVER_NAME3 = "memory-service";
435
+ var LEGACY_SERVER_NAMES = ["memrosetta"];
436
+ var CODEX_CONFIG_PATH_GETTER = () => join5(homedir4(), ".codex", "config.toml");
437
+ var CODEX_HOOKS_PATH_GETTER = () => join5(homedir4(), ".codex", "hooks.json");
438
+ var AGENTS_MD_MARKER = "## MemRosetta (Long-term Memory)";
439
+ var CODEX_HOOKS_FEATURE_MARKER = "[features]";
440
+ var MEMROSETTA_AGENTS_MD = `
441
+
442
+ ${AGENTS_MD_MARKER}
443
+
444
+ MCP server \`memory-service\` provides persistent memory across sessions.
445
+ userId defaults to the system username -- no need to specify it.
446
+
447
+ ### When to search (mcp__memory-service__memrosetta_search)
448
+ When you need information not in the current context, search past memories.
449
+
450
+ ### When to store (mcp__memory-service__memrosetta_store)
451
+
452
+ **After EVERY response, run this checklist:**
453
+ 1. Did I encounter a DECISION? (tech choice, approach selection) -> store as "decision"
454
+ 2. Did I learn a new FACT? (config, architecture, project info) -> store as "fact"
455
+ 3. Did the user state a PREFERENCE? (style, tool choice, pattern) -> store as "preference"
456
+ 4. Did we COMPLETE something? (deploy, migration, fix) -> store as "event"
457
+ 5. None of the above? -> skip, do not store.
458
+
459
+ Always include 2-3 keywords. Example:
460
+ content: "Decided to use OAuth2 with PKCE for auth"
461
+ type: "decision"
462
+ keywords: "auth, oauth2, pkce"
463
+
464
+ Do NOT store:
465
+ - Code itself (belongs in git)
466
+ - File operations ("Created file X", "Modified Y")
467
+ - Debugging steps and attempts
468
+ - Simple confirmations or acknowledgments
469
+
470
+ ### When to relate (mcp__memory-service__memrosetta_relate)
471
+ When new information updates or contradicts existing memories, create a relation.
472
+
473
+ ### Working memory (mcp__memory-service__memrosetta_working_memory)
474
+ Call this at the start of complex tasks to load relevant context.
475
+ `;
476
+ function getCodexConfigDir() {
477
+ return join5(homedir4(), ".codex");
478
+ }
479
+ function getCodexConfigPath() {
480
+ return CODEX_CONFIG_PATH_GETTER();
481
+ }
482
+ function readCodexConfig(path) {
483
+ if (!existsSync5(path)) return "";
484
+ try {
485
+ return readFileSync4(path, "utf-8");
486
+ } catch {
487
+ return "";
488
+ }
489
+ }
490
+ function tomlLiteral(s) {
491
+ if (!s.includes("'")) {
492
+ return `'${s}'`;
493
+ }
494
+ const escaped = s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
495
+ return `"${escaped}"`;
496
+ }
497
+ function buildMcpServerToml() {
498
+ const { command, args } = resolveMcpCommand();
499
+ const argsLine = args.length > 0 ? `
500
+ args = [${args.map(tomlLiteral).join(", ")}]` : "";
501
+ return `
502
+ [mcp_servers.${SERVER_NAME3}]
503
+ command = ${tomlLiteral(command)}${argsLine}
504
+ `;
505
+ }
506
+ function hasMcpServer(content) {
507
+ return content.includes(`[mcp_servers.${SERVER_NAME3}]`);
508
+ }
509
+ function stripSection(content, name) {
510
+ const marker = `[mcp_servers.${name}]`;
511
+ const idx = content.indexOf(marker);
512
+ if (idx === -1) return content;
513
+ const afterMarker = content.slice(idx + marker.length);
514
+ const nextSectionMatch = afterMarker.match(/\n\[/);
515
+ const endIdx = nextSectionMatch ? idx + marker.length + (nextSectionMatch.index ?? afterMarker.length) : content.length;
516
+ const trimmedHead = content.slice(0, idx).trimEnd();
517
+ const tail = content.slice(endIdx);
518
+ return `${trimmedHead}
519
+ ${tail}`.trim() + "\n";
520
+ }
521
+ function removeMcpServerSection(content) {
522
+ let next = stripSection(content, SERVER_NAME3);
523
+ for (const legacy of LEGACY_SERVER_NAMES) {
524
+ next = stripSection(next, legacy);
525
+ }
526
+ return next;
527
+ }
528
+ function isCodexInstalled() {
529
+ return existsSync5(getCodexConfigDir());
530
+ }
531
+ function isCodexConfigured() {
532
+ const content = readCodexConfig(getCodexConfigPath());
533
+ return hasMcpServer(content);
534
+ }
535
+ function registerCodexMCP() {
536
+ const configPath = getCodexConfigPath();
537
+ const dir = getCodexConfigDir();
538
+ if (!existsSync5(dir)) {
539
+ mkdirSync2(dir, { recursive: true });
540
+ }
541
+ let content = readCodexConfig(configPath);
542
+ content = removeMcpServerSection(content);
543
+ writeFileSync4(configPath, content + buildMcpServerToml(), "utf-8");
544
+ return updateAgentsMd();
545
+ }
546
+ function removeCodexMCP() {
547
+ const configPath = getCodexConfigPath();
548
+ if (!existsSync5(configPath)) return false;
549
+ const original = readCodexConfig(configPath);
550
+ const cleaned = removeMcpServerSection(original);
551
+ if (cleaned === original) {
552
+ removeAgentsMdSection();
553
+ return false;
554
+ }
555
+ writeFileSync4(configPath, cleaned, "utf-8");
556
+ removeAgentsMdSection();
557
+ return true;
558
+ }
559
+ function getCodexConfigFilePath() {
560
+ return getCodexConfigPath();
561
+ }
562
+ function getAgentsMdPath() {
563
+ return join5(process.cwd(), "AGENTS.md");
564
+ }
565
+ function updateAgentsMd() {
566
+ const agentsPath = getAgentsMdPath();
567
+ const existing = existsSync5(agentsPath) ? readFileSync4(agentsPath, "utf-8") : "";
568
+ if (existing.includes(AGENTS_MD_MARKER)) return false;
569
+ writeFileSync4(agentsPath, existing + MEMROSETTA_AGENTS_MD, "utf-8");
570
+ return true;
571
+ }
572
+ function isMemrosettaCodexHook(command) {
573
+ return command.includes("memrosetta") && (command.includes("enforce-codex") || command.includes("enforce-claude-code") || command.includes("on-stop"));
574
+ }
575
+ function readCodexHooksFile() {
576
+ const path = CODEX_HOOKS_PATH_GETTER();
577
+ if (!existsSync5(path)) return {};
578
+ try {
579
+ const raw = readFileSync4(path, "utf-8");
580
+ if (!raw.trim()) return {};
581
+ return JSON.parse(raw);
582
+ } catch {
583
+ return {};
584
+ }
585
+ }
586
+ function writeCodexHooksFile(contents) {
587
+ const dir = getCodexConfigDir();
588
+ if (!existsSync5(dir)) {
589
+ mkdirSync2(dir, { recursive: true });
590
+ }
591
+ writeFileSync4(
592
+ CODEX_HOOKS_PATH_GETTER(),
593
+ JSON.stringify(contents, null, 2) + "\n",
594
+ "utf-8"
595
+ );
596
+ }
597
+ function stripMemrosettaHooks(file) {
598
+ if (!file.hooks) return file;
599
+ const cleaned = {};
600
+ for (const [event, configs] of Object.entries(file.hooks)) {
601
+ const filtered = configs.map((cfg) => ({
602
+ ...cfg,
603
+ hooks: cfg.hooks.filter((h) => !isMemrosettaCodexHook(h.command))
604
+ })).filter((cfg) => cfg.hooks.length > 0);
605
+ if (filtered.length > 0) {
606
+ cleaned[event] = filtered;
607
+ }
608
+ }
609
+ return { ...file, hooks: cleaned };
610
+ }
611
+ function ensureHooksFeatureFlag(content) {
612
+ if (/^\s*codex_hooks\s*=\s*true/m.test(content)) return content;
613
+ if (content.includes(CODEX_HOOKS_FEATURE_MARKER)) {
614
+ if (/^\s*codex_hooks\s*=\s*false/m.test(content)) {
615
+ return content.replace(
616
+ /^\s*codex_hooks\s*=\s*false\s*$/m,
617
+ "codex_hooks = true"
618
+ );
619
+ }
620
+ return content.replace(
621
+ CODEX_HOOKS_FEATURE_MARKER,
622
+ `${CODEX_HOOKS_FEATURE_MARKER}
623
+ codex_hooks = true`
624
+ );
625
+ }
626
+ const trimmed = content.replace(/\n+$/, "");
627
+ return `${trimmed}
628
+
629
+ [features]
630
+ codex_hooks = true
631
+ `;
632
+ }
633
+ function stripHooksFeatureFlag(content) {
634
+ if (!/^\s*codex_hooks\s*=\s*true/m.test(content)) return content;
635
+ return content.replace(/^\s*codex_hooks\s*=\s*true\s*\n?/m, "");
636
+ }
637
+ function isCodexHooksConfigured() {
638
+ const file = readCodexHooksFile();
639
+ const stopHooks = file.hooks?.Stop ?? [];
640
+ return stopHooks.some(
641
+ (cfg) => cfg.hooks.some((h) => isMemrosettaCodexHook(h.command))
642
+ );
643
+ }
644
+ function getCodexHooksPath() {
645
+ return CODEX_HOOKS_PATH_GETTER();
646
+ }
647
+ function registerCodexHooks() {
648
+ if (process.platform === "win32") return false;
649
+ if (!isCodexInstalled()) return false;
650
+ const file = stripMemrosettaHooks(readCodexHooksFile());
651
+ const newHooks = { ...file.hooks ?? {} };
652
+ const existingStop = newHooks.Stop ?? [];
653
+ newHooks.Stop = [
654
+ ...existingStop,
655
+ {
656
+ matcher: "*",
657
+ hooks: [
658
+ {
659
+ type: "command",
660
+ command: resolveHookCommand("memrosetta-enforce-codex"),
661
+ timeout: 30,
662
+ statusMessage: "memrosetta: enforcing memory capture"
663
+ }
664
+ ]
665
+ }
666
+ ];
667
+ writeCodexHooksFile({ ...file, hooks: newHooks });
668
+ const configPath = getCodexConfigPath();
669
+ const configContent = readCodexConfig(configPath);
670
+ const updated = ensureHooksFeatureFlag(configContent);
671
+ if (updated !== configContent) {
672
+ writeFileSync4(configPath, updated, "utf-8");
673
+ }
674
+ return true;
675
+ }
676
+ function removeCodexHooks() {
677
+ const hadHook = isCodexHooksConfigured();
678
+ const file = readCodexHooksFile();
679
+ const cleaned = stripMemrosettaHooks(file);
680
+ const anyOtherHooks = Object.values(cleaned.hooks ?? {}).some(
681
+ (cfgs) => cfgs.length > 0
682
+ );
683
+ if (hadHook) {
684
+ if (anyOtherHooks) {
685
+ writeCodexHooksFile(cleaned);
686
+ } else {
687
+ const hooksPath = CODEX_HOOKS_PATH_GETTER();
688
+ if (existsSync5(hooksPath)) {
689
+ writeCodexHooksFile({});
690
+ }
691
+ }
692
+ }
693
+ if (!anyOtherHooks) {
694
+ const configPath = getCodexConfigPath();
695
+ if (existsSync5(configPath)) {
696
+ const configContent = readCodexConfig(configPath);
697
+ const stripped = stripHooksFeatureFlag(configContent);
698
+ if (stripped !== configContent) {
699
+ writeFileSync4(configPath, stripped, "utf-8");
700
+ }
701
+ }
702
+ }
703
+ return hadHook;
704
+ }
705
+ function removeAgentsMdSection() {
706
+ const agentsPath = getAgentsMdPath();
707
+ if (!existsSync5(agentsPath)) return false;
708
+ const content = readFileSync4(agentsPath, "utf-8");
709
+ const markerIdx = content.indexOf(AGENTS_MD_MARKER);
710
+ if (markerIdx === -1) return false;
711
+ const afterMarker = content.slice(markerIdx + AGENTS_MD_MARKER.length);
712
+ const nextHeadingMatch = afterMarker.match(/\n## (?!MemRosetta)/);
713
+ const endIdx = nextHeadingMatch ? markerIdx + AGENTS_MD_MARKER.length + (nextHeadingMatch.index ?? afterMarker.length) : content.length;
714
+ const before = content.slice(0, markerIdx).replace(/\n+$/, "");
715
+ const after = content.slice(endIdx);
716
+ const updated = (before + after).trimEnd() + "\n";
717
+ writeFileSync4(agentsPath, updated, "utf-8");
718
+ return true;
719
+ }
720
+
721
+ // src/integrations/gemini.ts
722
+ import { join as join6 } from "path";
723
+ import { homedir as homedir5 } from "os";
724
+ import { existsSync as existsSync6, readFileSync as readFileSync5, writeFileSync as writeFileSync5, mkdirSync as mkdirSync3 } from "fs";
725
+ var SERVER_NAME4 = "memory-service";
726
+ var GEMINI_CONFIG_PATH_GETTER = () => join6(homedir5(), ".gemini", "settings.json");
727
+ var GEMINI_MD_MARKER = "## MemRosetta (Long-term Memory)";
728
+ var MEMROSETTA_GEMINI_MD = `
729
+
730
+ ${GEMINI_MD_MARKER}
731
+
732
+ MCP server \`memory-service\` provides persistent memory across sessions.
733
+ userId defaults to the system username -- no need to specify it.
734
+
735
+ ### When to search (memrosetta_search)
736
+ When you need information not in the current context, search past memories.
737
+ No need to specify userId -- it defaults to the system username.
738
+
739
+ ### When to store (memrosetta_store)
740
+
741
+ **After EVERY response, run this checklist (zero extra cost):**
742
+ 1. Did I encounter a DECISION? (tech choice, approach selection) -> store as "decision"
743
+ 2. Did I learn a new FACT? (config, architecture, project info) -> store as "fact"
744
+ 3. Did the user state a PREFERENCE? (style, tool choice, pattern) -> store as "preference"
745
+ 4. Did we COMPLETE something? (deploy, migration, fix) -> store as "event"
746
+ 5. None of the above? -> skip, do not store.
747
+
748
+ Always include 2-3 keywords. Example:
749
+ content: "Decided to use OAuth2 with PKCE for auth"
750
+ type: "decision"
751
+ keywords: "auth, oauth2, pkce"
752
+
753
+ Do NOT store:
754
+ - Code itself (belongs in git)
755
+ - File operations ("Created file X", "Modified Y")
756
+ - Debugging steps and attempts
757
+ - Simple confirmations or acknowledgments
758
+ - Implementation details (HOW you did it -- only WHAT)
759
+
760
+ This checklist ensures nothing important is lost, including the last response before session ends.
761
+ No need to specify userId -- it defaults to the system username.
762
+
763
+ ### When to relate (memrosetta_relate)
764
+ When new information updates or contradicts existing memories, create a relation.
765
+
766
+ ### Working memory (memrosetta_working_memory)
767
+ Call this at the start of complex tasks to load relevant context.
768
+ No need to specify userId -- it defaults to the system username.
769
+
770
+ ### Feedback (memrosetta_feedback)
771
+ After using a retrieved memory, report whether it was helpful:
772
+ - Memory was accurate and useful -> feedback(memoryId, helpful=true)
773
+ - Memory was outdated or wrong -> feedback(memoryId, helpful=false)
774
+ This improves future search ranking automatically.
775
+ `;
776
+ function getGeminiConfigDir() {
777
+ return join6(homedir5(), ".gemini");
778
+ }
779
+ function getGeminiSettingsPath() {
780
+ return GEMINI_CONFIG_PATH_GETTER();
781
+ }
782
+ function readGeminiSettings(path) {
783
+ if (!existsSync6(path)) return {};
784
+ const raw = readFileSync5(path, "utf-8");
785
+ try {
786
+ return JSON.parse(raw);
787
+ } catch {
788
+ throw new Error(
789
+ `Failed to parse ${path}. Fix or delete the file before running init.`
790
+ );
791
+ }
792
+ }
793
+ function writeGeminiSettings(path, settings) {
794
+ const dir = getGeminiConfigDir();
795
+ if (!existsSync6(dir)) {
796
+ mkdirSync3(dir, { recursive: true });
797
+ }
798
+ writeFileSync5(path, JSON.stringify(settings, null, 2), "utf-8");
799
+ }
800
+ function mcpServerEntry3() {
801
+ const { command, args } = resolveMcpCommand();
802
+ return { command, args };
803
+ }
804
+ function isGeminiConfigured() {
805
+ const path = getGeminiSettingsPath();
806
+ const settings = readGeminiSettings(path);
807
+ return !!settings.mcpServers?.[SERVER_NAME4];
808
+ }
809
+ function registerGeminiMCP() {
810
+ const path = getGeminiSettingsPath();
811
+ const settings = readGeminiSettings(path);
812
+ const servers = settings.mcpServers ?? {};
813
+ writeGeminiSettings(path, {
814
+ ...settings,
815
+ mcpServers: { ...servers, [SERVER_NAME4]: mcpServerEntry3() }
816
+ });
817
+ return updateGeminiMd();
818
+ }
819
+ function removeGeminiMCP() {
820
+ const path = getGeminiSettingsPath();
821
+ if (!existsSync6(path)) return false;
822
+ const settings = readGeminiSettings(path);
823
+ if (!settings.mcpServers?.[SERVER_NAME4]) return false;
824
+ const { [SERVER_NAME4]: _, ...rest } = settings.mcpServers;
825
+ writeGeminiSettings(path, { ...settings, mcpServers: rest });
826
+ return true;
827
+ }
828
+ function getGeminiSettingsFilePath() {
829
+ return getGeminiSettingsPath();
830
+ }
831
+ function getGeminiMdPath() {
832
+ return join6(process.cwd(), "GEMINI.md");
833
+ }
834
+ function updateGeminiMd() {
835
+ const geminiMdPath = getGeminiMdPath();
836
+ const existing = existsSync6(geminiMdPath) ? readFileSync5(geminiMdPath, "utf-8") : "";
837
+ if (existing.includes(GEMINI_MD_MARKER)) return false;
838
+ writeFileSync5(geminiMdPath, existing + MEMROSETTA_GEMINI_MD, "utf-8");
839
+ return true;
840
+ }
841
+ function removeGeminiMdSection() {
842
+ const geminiMdPath = getGeminiMdPath();
843
+ if (!existsSync6(geminiMdPath)) return false;
844
+ const content = readFileSync5(geminiMdPath, "utf-8");
845
+ const markerIdx = content.indexOf(GEMINI_MD_MARKER);
846
+ if (markerIdx === -1) return false;
847
+ const afterMarker = content.slice(
848
+ markerIdx + GEMINI_MD_MARKER.length
849
+ );
850
+ const nextHeadingMatch = afterMarker.match(/\n## (?!MemRosetta)/);
851
+ const endIdx = nextHeadingMatch ? markerIdx + GEMINI_MD_MARKER.length + (nextHeadingMatch.index ?? afterMarker.length) : content.length;
852
+ const before = content.slice(0, markerIdx).replace(/\n+$/, "");
853
+ const after = content.slice(endIdx);
854
+ const updated = (before + after).trimEnd() + "\n";
855
+ writeFileSync5(geminiMdPath, updated, "utf-8");
856
+ return true;
857
+ }
858
+
859
+ export {
860
+ isClaudeCodeInstalled,
861
+ isClaudeCodeConfigured,
862
+ registerClaudeCodeHooks,
863
+ removeClaudeCodeHooks,
864
+ updateClaudeMd,
865
+ removeClaudeMdSection,
866
+ isGenericMCPConfigured,
867
+ registerGenericMCP,
868
+ removeGenericMCP,
869
+ getGenericMCPPath,
870
+ isCursorConfigured,
871
+ registerCursorMCP,
872
+ removeCursorMCP,
873
+ getCursorMcpConfigPath,
874
+ getCursorRulesPath,
875
+ removeCursorRulesSection,
876
+ isCodexConfigured,
877
+ registerCodexMCP,
878
+ removeCodexMCP,
879
+ getCodexConfigFilePath,
880
+ getAgentsMdPath,
881
+ getCodexHooksPath,
882
+ registerCodexHooks,
883
+ removeCodexHooks,
884
+ removeAgentsMdSection,
885
+ isGeminiConfigured,
886
+ registerGeminiMCP,
887
+ removeGeminiMCP,
888
+ getGeminiSettingsFilePath,
889
+ getGeminiMdPath,
890
+ removeGeminiMdSection
891
+ };