@spike1292/claude-memory 0.0.0-stage → 0.14.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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +412 -2
  3. package/hooks/distill-session.mjs +74 -0
  4. package/hooks/graph-staleness-check.mjs +25 -0
  5. package/hooks/hooks.json +94 -0
  6. package/hooks/insights-surface.mjs +48 -0
  7. package/hooks/lib/distill-session.mjs +1560 -0
  8. package/hooks/lib/dupe-report.mjs +61 -0
  9. package/hooks/lib/env-shell.mjs +92 -0
  10. package/hooks/lib/graph-staleness-check.mjs +394 -0
  11. package/hooks/lib/hook-io.mjs +715 -0
  12. package/hooks/lib/insights-surface.mjs +165 -0
  13. package/hooks/lib/memory-link-lint.mjs +499 -0
  14. package/hooks/lib/memory-recall.mjs +219 -0
  15. package/hooks/lib/omp.mjs +103 -0
  16. package/hooks/lib/opencode.mjs +231 -0
  17. package/hooks/lib/paths.mjs +452 -0
  18. package/hooks/lib/semantic-index-refresh.mjs +155 -0
  19. package/hooks/lib/validate-note.mjs +218 -0
  20. package/hooks/lib/vault-env.sh +110 -0
  21. package/hooks/memory-link-lint.mjs +65 -0
  22. package/hooks/memory-recall.mjs +251 -0
  23. package/hooks/semantic-index-refresh.mjs +23 -0
  24. package/hooks/session-context.mjs +9 -0
  25. package/hooks/validate-note.mjs +52 -0
  26. package/hooks/vault-memory-sync.sh +154 -0
  27. package/omp/index.js +1 -0
  28. package/opencode/index.js +1 -0
  29. package/package.json +51 -4
  30. package/scripts/claude-memory.mjs +31 -0
  31. package/scripts/doctor-perf.mjs +121 -0
  32. package/scripts/doctor.sh +558 -0
  33. package/scripts/env.mjs +6 -0
  34. package/scripts/lib/claude-memory.mjs +168 -0
  35. package/scripts/lib/doctor-perf.mjs +434 -0
  36. package/scripts/lib/hook-stats.mjs +864 -0
  37. package/scripts/lib/lexical.mjs +85 -0
  38. package/scripts/lib/memory-adopt.mjs +69 -0
  39. package/scripts/lib/memory-audit-checks.mjs +344 -0
  40. package/scripts/lib/memory-eval.mjs +381 -0
  41. package/scripts/lib/memory-mark.mjs +135 -0
  42. package/scripts/lib/memory-mcp.mjs +142 -0
  43. package/scripts/lib/memory-merge.mjs +316 -0
  44. package/scripts/lib/memory-mine.mjs +108 -0
  45. package/scripts/lib/memory-promote.mjs +207 -0
  46. package/scripts/lib/memory-prune.mjs +84 -0
  47. package/scripts/lib/memory-semantic.mjs +999 -0
  48. package/scripts/lib/memory-setup.mjs +170 -0
  49. package/scripts/lib/memory-synth-vault.mjs +538 -0
  50. package/scripts/lib/model-default.mjs +7 -0
  51. package/scripts/lib/models.mjs +58 -0
  52. package/scripts/lib/prune-logs.mjs +130 -0
  53. package/scripts/lib/recall-stats.mjs +354 -0
  54. package/scripts/lib/share-modules.mjs +70 -0
  55. package/scripts/lib/slim-install.mjs +162 -0
  56. package/scripts/lib/vault-mcp.mjs +405 -0
  57. package/scripts/memory-adopt.mjs +160 -0
  58. package/scripts/memory-audit-checks.mjs +375 -0
  59. package/scripts/memory-eval.mjs +753 -0
  60. package/scripts/memory-mark.mjs +39 -0
  61. package/scripts/memory-mcp.mjs +35 -0
  62. package/scripts/memory-merge.mjs +55 -0
  63. package/scripts/memory-prune.mjs +44 -0
  64. package/scripts/memory-semantic.mjs +1192 -0
  65. package/scripts/memory-setup.mjs +33 -0
  66. package/scripts/memory-synth-vault.mjs +251 -0
  67. package/scripts/prune-logs.mjs +42 -0
  68. package/scripts/share-modules.mjs +79 -0
  69. package/scripts/slim-install.mjs +55 -0
  70. package/scripts/vault-mcp.mjs +50 -0
  71. package/stubs/onnxruntime-web/index.mjs +11 -0
  72. package/stubs/onnxruntime-web/package.json +12 -0
  73. package/stubs/sharp/index.mjs +11 -0
  74. package/stubs/sharp/package.json +12 -0
@@ -0,0 +1,170 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { readConfig } from '../../hooks/lib/paths.mjs';
4
+ import { MODELS } from './models.mjs';
5
+
6
+ export const USAGE = 'memory-setup.mjs [--vault <path>] [--model <name>] [--force]';
7
+ const STATE_DIRS = ['db', 'models', 'logs', 'run', 'eval'];
8
+
9
+ /**
10
+ * @param {string[]} argv
11
+ * @param {string} cwd
12
+ * @returns {{ vault?: string, model?: string, force: boolean } | { error: string }}
13
+ */
14
+ export function parseSetupArgs(argv, cwd) {
15
+ /** @type {{ vault?: string, model?: string, force: boolean }} */
16
+ const out = { force: false };
17
+ for (let i = 0; i < argv.length; i++) {
18
+ const a = argv[i];
19
+ if (a === '--force') out.force = true;
20
+ else if (a === '--vault' || a === '--model') {
21
+ const v = argv[i + 1];
22
+ if (!v || v.startsWith('-')) return { error: `${a} needs a value` };
23
+ i++;
24
+ if (a === '--vault') out.vault = path.resolve(cwd, v);
25
+ else if (!Object.hasOwn(MODELS, v))
26
+ return { error: `unknown model ${v} — one of: ${Object.keys(MODELS).join(', ')}` };
27
+ else out.model = v;
28
+ } else return { error: `unknown argument: ${a}` };
29
+ }
30
+ return out;
31
+ }
32
+
33
+ /**
34
+ * Add the wanted keys to an existing config. A key already set to another value is a conflict
35
+ * unless `force`.
36
+ *
37
+ * @param {Record<string, unknown>} existing
38
+ * @param {Record<string, string>} wanted
39
+ * @param {boolean} force
40
+ */
41
+ export function mergeConfig(existing, wanted, force) {
42
+ const config = { ...existing };
43
+ const changed = [];
44
+ const conflicts = [];
45
+ for (const [k, v] of Object.entries(wanted)) {
46
+ if (config[k] === v) continue;
47
+ if (config[k] !== undefined && !force) {
48
+ conflicts.push(`${k}=${JSON.stringify(config[k])}`);
49
+ continue;
50
+ }
51
+ config[k] = v;
52
+ changed.push(`${k}=${v}`);
53
+ }
54
+ return { config, changed, conflicts };
55
+ }
56
+
57
+ /**
58
+ * @typedef {{ code: number, out: string }} RunResult
59
+ * @typedef {{
60
+ * home: string,
61
+ * nodeVersion: string,
62
+ * pluginInstall: boolean,
63
+ * resolveVault: (settings: import('../../hooks/lib/paths.mjs').MemoryConfig) => string,
64
+ * run: (script: string, args: string[], model?: string) => RunResult,
65
+ * print: (line: string) => void,
66
+ * }} SetupDeps
67
+ */
68
+
69
+ /**
70
+ * Every mechanical step of /memory:install after `npm ci`. Stops at the first failure.
71
+ *
72
+ * @param {{ vault?: string, model?: string, force: boolean }} opts
73
+ * @param {SetupDeps} deps
74
+ * @returns {number} exit code
75
+ */
76
+ export function setup(opts, { home, nodeVersion, pluginInstall, resolveVault, run, print }) {
77
+ /** @type {(status: string, step: string, detail: string) => void} */
78
+ const line = (status, step, detail) => print(`${status.padEnd(4)} ${step.padEnd(6)} ${detail}`);
79
+ /** @type {(step: string, detail: string) => number} */
80
+ const fail = (step, detail) => (line('FAIL', step, detail), 1);
81
+ /** @param {string} out */
82
+ const last = (out) => out.trim().split('\n').pop() ?? '';
83
+
84
+ const [major, minor] = nodeVersion.split('.').map(Number);
85
+ if (major < 22 || (major === 22 && minor < 5))
86
+ return fail('node', `${nodeVersion} — need ≥ 22.5 for node:sqlite`);
87
+ line('ok', 'node', nodeVersion);
88
+
89
+ for (const d of STATE_DIRS) fs.mkdirSync(path.join(home, d), { recursive: true });
90
+ line('ok', 'state', `${home}/{${STATE_DIRS.join(',')}}`);
91
+
92
+ const file = path.join(home, 'config.json');
93
+ let existing;
94
+ try {
95
+ existing = readConfig(file);
96
+ } catch (e) {
97
+ if (/** @type {NodeJS.ErrnoException} */ (e).code !== 'ENOENT')
98
+ return fail('config', `${file}: ${/** @type {Error} */ (e).message}`);
99
+ }
100
+ /** @type {Record<string, string>} */
101
+ const wanted = {};
102
+ if (opts.vault) wanted.vault = opts.vault;
103
+ if (opts.model) wanted.model = opts.model;
104
+ const { config, changed, conflicts } = mergeConfig(existing ?? {}, wanted, opts.force);
105
+ if (conflicts.length)
106
+ return fail('config', `${file} already has ${conflicts.join(', ')} — pass --force to replace`);
107
+ const resolved = resolveVault(config);
108
+ const target = fs.statSync(resolved, { throwIfNoEntry: false });
109
+ if (target && !target.isDirectory()) return fail('vault', `${resolved} is not a directory`);
110
+ if (!target && resolved !== opts.vault)
111
+ return fail('vault', `${resolved} does not exist — pass --vault <path>`);
112
+ if (changed.length && fs.lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink())
113
+ return fail('config', `${file} is a symlink — edit its target directly`);
114
+
115
+ if (pluginInstall) {
116
+ const r = run('share-modules.mjs', []);
117
+ if (r.code) return fail('share', last(r.out) || `share-modules.mjs exited ${r.code}`);
118
+ line('ok', 'share', last(r.out));
119
+ } else line('skip', 'share', 'not a plugin cache, node_modules stays in place');
120
+
121
+ const slim = run('slim-install.mjs', []);
122
+ if (slim.code) return fail('slim', last(slim.out) || `slim-install.mjs exited ${slim.code}`);
123
+ line('ok', 'slim', last(slim.out) || 'complete');
124
+
125
+ const check = run('memory-semantic.mjs', ['--check-embedding'], opts.model);
126
+ if (check.code) return fail('model', last(check.out) || `--check-embedding exited ${check.code}`);
127
+ line('ok', 'model', check.out.trim().split('\n')[0] ?? '');
128
+
129
+ if (!target) fs.mkdirSync(resolved, { recursive: true });
130
+ line('ok', 'vault', resolved);
131
+
132
+ const index = run('memory-semantic.mjs', ['--index', '--vault', resolved], opts.model);
133
+ if (index.code) return fail('index', last(index.out) || `--index exited ${index.code}`);
134
+ line('ok', 'index', last(index.out));
135
+ if (changed.length) {
136
+ // Publish only after setup succeeds. The lock serializes config updates from concurrent setups.
137
+ const lock = `${file}.lock`;
138
+ let fd;
139
+ try {
140
+ fd = fs.openSync(lock, 'wx', 0o600);
141
+ const current = fs.lstatSync(file, { throwIfNoEntry: false });
142
+ if (
143
+ current?.isSymbolicLink() ||
144
+ Boolean(current) !== (existing !== undefined) ||
145
+ (current && JSON.stringify(readConfig(file)) !== JSON.stringify(existing))
146
+ )
147
+ return fail('config', `${file} changed during setup — retry`);
148
+ const temp = fs.mkdtempSync(path.join(home, '.config-'));
149
+ try {
150
+ const next = path.join(temp, 'config.json');
151
+ fs.writeFileSync(next, JSON.stringify(config, null, 2) + '\n', {
152
+ mode: current?.mode ?? 0o600,
153
+ });
154
+ if (current) fs.renameSync(next, file);
155
+ else fs.linkSync(next, file);
156
+ } finally {
157
+ fs.rmSync(temp, { recursive: true });
158
+ }
159
+ } catch (e) {
160
+ return fail('config', /** @type {Error} */ (e).message);
161
+ } finally {
162
+ if (fd !== undefined) {
163
+ fs.closeSync(fd);
164
+ fs.unlinkSync(lock);
165
+ }
166
+ }
167
+ }
168
+ line('ok', 'config', changed.length ? `wrote ${changed.join(', ')}` : 'unchanged');
169
+ return 0;
170
+ }