claude-mem-lite 4.0.4 → 5.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.
Files changed (64) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +18 -72
  4. package/README.zh-CN.md +24 -74
  5. package/adopt-content.mjs +0 -1
  6. package/cli/common.mjs +0 -17
  7. package/cli.mjs +19 -3
  8. package/commands/mem.md +3 -3
  9. package/commands/update.md +5 -12
  10. package/format-utils.mjs +0 -21
  11. package/haiku-client.mjs +36 -5
  12. package/hash-utils.mjs +21 -0
  13. package/hook-episode.mjs +7 -1
  14. package/hook-optimize.mjs +103 -50
  15. package/hook-shared.mjs +8 -2
  16. package/hook-update.mjs +56 -14
  17. package/hook.mjs +21 -29
  18. package/hooks/hooks.json +0 -10
  19. package/install.mjs +100 -678
  20. package/lib/atomic-write.mjs +16 -0
  21. package/lib/deferred-work.mjs +7 -3
  22. package/lib/doctor-drift.mjs +0 -1
  23. package/lib/frontmatter.mjs +1 -2
  24. package/lib/get-core.mjs +27 -1
  25. package/lib/hook-prune.mjs +133 -0
  26. package/lib/hook-stdin.mjs +1 -1
  27. package/lib/hook-telemetry.mjs +1 -1
  28. package/lib/install-shape.mjs +45 -4
  29. package/lib/maintain-core.mjs +9 -0
  30. package/lib/metrics.mjs +2 -2
  31. package/lib/proc-lock.mjs +111 -13
  32. package/lib/proxy-fetch.mjs +26 -2
  33. package/lib/scrub-record.mjs +8 -0
  34. package/lib/shard-gc.mjs +11 -7
  35. package/lib/tmp-fixture-sweep.mjs +9 -0
  36. package/mem-cli.mjs +46 -434
  37. package/nlp.mjs +1 -1
  38. package/npm-shrinkwrap.json +6 -2
  39. package/package.json +9 -16
  40. package/project-utils.mjs +43 -15
  41. package/schema.mjs +112 -50
  42. package/scripts/hook-launcher.mjs +9 -3
  43. package/scripts/prompt-search-utils.mjs +0 -30
  44. package/scripts/setup.sh +5 -0
  45. package/scripts/user-prompt-search.js +3 -125
  46. package/secret-scrub.mjs +32 -3
  47. package/server.mjs +19 -451
  48. package/source-files.mjs +12 -29
  49. package/synonyms.mjs +2 -1
  50. package/tool-schemas.mjs +23 -91
  51. package/utils.mjs +5 -13
  52. package/commands/tools.md +0 -67
  53. package/install-metadata.mjs +0 -2193
  54. package/lib/registry-core.mjs +0 -264
  55. package/registry/preinstalled.json +0 -2419
  56. package/registry-enricher.mjs +0 -124
  57. package/registry-github.mjs +0 -86
  58. package/registry-importer.mjs +0 -569
  59. package/registry-recommend.mjs +0 -503
  60. package/registry-retriever.mjs +0 -611
  61. package/registry-scanner.mjs +0 -261
  62. package/registry.mjs +0 -665
  63. package/resource-discovery.mjs +0 -199
  64. package/scripts/pre-skill-bridge.js +0 -146
@@ -1,261 +0,0 @@
1
- // claude-mem-lite: Resource scanner — discovers skills/agents from filesystem
2
- // Scans user local dirs and managed pre-installed dirs
3
-
4
- import { createHash } from 'crypto';
5
- import { existsSync, readdirSync, readFileSync, statSync } from 'fs';
6
- import { join } from 'path';
7
- import { homedir } from 'os';
8
- import { debugCatch } from './utils.mjs';
9
- import { DB_DIR } from './schema.mjs';
10
- import { discoverFlat, discoverPlugin, discoverPlugins } from './resource-discovery.mjs';
11
-
12
- /**
13
- * @typedef {object} ScannedResource
14
- * @property {string} name Resource name (directory name)
15
- * @property {'skill'|'agent'} type Resource type
16
- * @property {'preinstalled'|'user'} source Source origin
17
- * @property {string} localPath Absolute path to resource directory
18
- * @property {string} content Combined content of resource files
19
- * @property {string} fileHash SHA-256 hash of content
20
- * @property {string|null} repoUrl GitHub repo URL if preinstalled
21
- */
22
-
23
- // ─── Resource Location Convention ─────────────────────────────────────────────
24
- // Two resource locations, differentiated by naming convention:
25
- // managed/skills/{skill}/ → standalone skills, name: "{skill}"
26
- // managed/agents/{plugin}/skills/ → plugin-attached skills, name: "{plugin}/{skill}"
27
- // managed/agents/{plugin}/agents/ → plugin agents, name: "{plugin}/{agent}"
28
- // Both scanned by same scanner; deduplication by "type:name" key.
29
-
30
- // ─── Scan Sources ────────────────────────────────────────────────────────────
31
-
32
- /**
33
- * Build the list of directories to scan for skill and agent resources.
34
- * @param {string} dataDir Base data directory for managed resources
35
- * @returns {{path: string, type: string, source: string, layout: string}[]}
36
- */
37
- function getScanSources(dataDir) {
38
- const home = homedir();
39
- return [
40
- // User local resources (highest priority) — flat layout
41
- { path: join(home, '.claude', 'skills'), type: 'skill', source: 'user', layout: 'flat' },
42
- { path: join(home, '.claude', 'agents'), type: 'agent', source: 'user', layout: 'flat' },
43
- // Pre-installed managed resources
44
- { path: join(dataDir, 'managed', 'skills'), type: 'skill', source: 'preinstalled', layout: 'flat' },
45
- // Agent plugins contain both agents and skills in nested structure
46
- { path: join(dataDir, 'managed', 'agents'), type: null, source: 'preinstalled', layout: 'plugins' },
47
- ];
48
- }
49
-
50
- // ─── Content Reading ─────────────────────────────────────────────────────────
51
-
52
- /**
53
- * Read the primary markdown content file from a resource directory.
54
- * Searches skill.md, agent.md, README.md, then any .md or .yaml file.
55
- * @param {string} dirPath Absolute path to the resource directory
56
- * @returns {string} File content or empty string if not found
57
- */
58
- function readResourceContent(dirPath) {
59
- // Priority: skill.md / agent.md > README.md > first .md file
60
- const candidates = ['skill.md', 'agent.md', 'SKILL.md', 'AGENT.md', 'README.md'];
61
-
62
- for (const name of candidates) {
63
- const fp = join(dirPath, name);
64
- if (existsSync(fp)) {
65
- try {
66
- return readFileSync(fp, 'utf8');
67
- } catch {
68
- continue;
69
- }
70
- }
71
- }
72
-
73
- // Fallback: first .md file found
74
- try {
75
- const files = readdirSync(dirPath).filter((f) => f.endsWith('.md'));
76
- if (files.length > 0) {
77
- return readFileSync(join(dirPath, files[0]), 'utf8');
78
- }
79
- } catch {}
80
-
81
- // Last resort: look for .yaml/.yml files (some agents use YAML)
82
- try {
83
- const yamlFiles = readdirSync(dirPath).filter((f) => f.endsWith('.yaml') || f.endsWith('.yml'));
84
- if (yamlFiles.length > 0) {
85
- return readFileSync(join(dirPath, yamlFiles[0]), 'utf8');
86
- }
87
- } catch {}
88
-
89
- return '';
90
- }
91
-
92
- /**
93
- * Compute SHA-256 hash of content for change detection.
94
- * @param {string} content File content to hash
95
- * @returns {string|null} Hex-encoded SHA-256 hash, or null if content is empty
96
- */
97
- function computeHash(content) {
98
- if (!content) return null;
99
- return createHash('sha256').update(content).digest('hex');
100
- }
101
-
102
- // ─── Directory Scanning ──────────────────────────────────────────────────────
103
-
104
- /**
105
- * Scan a single directory for resources.
106
- * Each subdirectory (or .md file) is treated as one resource.
107
- * Delegates path discovery to discoverFlat(), then reads content via buildScannedResource().
108
- * @param {string} dirPath Directory to scan
109
- * @param {'skill'|'agent'} type Resource type
110
- * @param {'preinstalled'|'user'} source Source origin
111
- * @returns {ScannedResource[]} Array of discovered resources
112
- */
113
- export function scanDirectory(dirPath, type, source) {
114
- const discovered = discoverFlat(dirPath, type, { strict: false, includeFiles: true });
115
- const resources = [];
116
- for (const d of discovered) {
117
- const res = buildScannedResource(d, source);
118
- if (res) resources.push(res);
119
- }
120
- return resources;
121
- }
122
-
123
- // ─── Plugin Scanning ──────────────────────────────────────────────────────
124
-
125
- /**
126
- * Build a ScannedResource from a DiscoveredResource by reading content.
127
- * @param {{type: string, name: string, absPath: string}} discovered
128
- * @param {'preinstalled'|'user'} source
129
- * @returns {ScannedResource|null}
130
- */
131
- function buildScannedResource(discovered, source) {
132
- try {
133
- const stat = statSync(discovered.absPath);
134
- let content;
135
- if (stat.isDirectory()) {
136
- content = readResourceContent(discovered.absPath);
137
- } else {
138
- content = readFileSync(discovered.absPath, 'utf8');
139
- }
140
- if (!content || content.length < 10) return null;
141
- return {
142
- name: discovered.name,
143
- type: discovered.type,
144
- source,
145
- localPath: discovered.absPath,
146
- content,
147
- fileHash: computeHash(content),
148
- repoUrl: null,
149
- };
150
- } catch (e) {
151
- debugCatch(e, `buildScannedResource(${discovered.absPath})`);
152
- return null;
153
- }
154
- }
155
-
156
- /**
157
- * Scan a single agent plugin directory for nested agents and skills.
158
- * @param {string} pluginDir Absolute path to plugin directory
159
- * @param {string} pluginName Plugin directory name
160
- * @param {'preinstalled'|'user'} source Source origin
161
- * @returns {ScannedResource[]} Discovered agents and skills
162
- */
163
- export function scanPluginResources(pluginDir, pluginName, source) {
164
- const discovered = discoverPlugin(pluginDir, pluginName);
165
- const resources = [];
166
- for (const d of discovered) {
167
- const res = buildScannedResource(d, source);
168
- if (res) resources.push(res);
169
- }
170
- return resources;
171
- }
172
-
173
- /**
174
- * Scan a plugins directory (e.g. managed/agents/) for all plugin subdirs.
175
- * @param {string} dirPath Directory containing plugin directories
176
- * @param {'preinstalled'|'user'} source Source origin
177
- * @returns {ScannedResource[]} All discovered plugin resources
178
- */
179
- export function scanPluginsDirectory(dirPath, source) {
180
- const discovered = discoverPlugins(dirPath);
181
- const resources = [];
182
- for (const d of discovered) {
183
- const res = buildScannedResource(d, source);
184
- if (res) resources.push(res);
185
- }
186
- return resources;
187
- }
188
-
189
- // ─── Main Scan ───────────────────────────────────────────────────────────────
190
-
191
- /**
192
- * Scan all resource sources and return discovered resources.
193
- * Deduplicates by (type, name) — user resources take priority over preinstalled.
194
- * @param {object} [config] Configuration
195
- * @param {string} [config.dataDir] Data directory (defaults to DB_DIR)
196
- * @returns {ScannedResource[]} All discovered resources
197
- */
198
- export function scanAllResources(config = {}) {
199
- const dataDir = config.dataDir || DB_DIR;
200
- const sources = getScanSources(dataDir);
201
- const seen = new Map(); // key: "type:name" -> resource
202
-
203
- for (const src of sources) {
204
- const resources =
205
- src.layout === 'plugins'
206
- ? scanPluginsDirectory(src.path, src.source)
207
- : scanDirectory(src.path, src.type, src.source);
208
- for (const res of resources) {
209
- const key = `${res.type}:${res.name}`;
210
- // User resources override preinstalled (scanned first due to source order)
211
- if (!seen.has(key)) {
212
- seen.set(key, res);
213
- }
214
- }
215
- }
216
-
217
- return [...seen.values()];
218
- }
219
-
220
- /**
221
- * Compare scanned resources against DB state to find what needs indexing.
222
- * @param {Database} db Registry database
223
- * @param {ScannedResource[]} scanned Scanned resources
224
- * @returns {{toIndex: ScannedResource[], toDisable: object[]}} Resources needing action
225
- */
226
- export function diffResources(db, scanned) {
227
- const existing = new Map();
228
- const rows = db.prepare('SELECT id, type, name, file_hash, local_path, status FROM resources').all();
229
- for (const r of rows) existing.set(`${r.type}:${r.name}`, r);
230
-
231
- const toIndex = [];
232
- const scannedKeys = new Set();
233
-
234
- for (const res of scanned) {
235
- const key = `${res.type}:${res.name}`;
236
- scannedKeys.add(key);
237
- const ex = existing.get(key);
238
-
239
- if (!ex) {
240
- // New resource
241
- toIndex.push(res);
242
- } else if (ex.file_hash !== res.fileHash) {
243
- // Content changed
244
- toIndex.push(res);
245
- }
246
- // else: unchanged, skip
247
- }
248
-
249
- // Resources in DB but not on filesystem → disable
250
- // Only disable resources that have a local_path (filesystem-backed).
251
- // Resources without local_path were imported via metadata/registry and
252
- // cannot be validated by filesystem scan.
253
- const toDisable = [];
254
- for (const [key, row] of existing) {
255
- if (!scannedKeys.has(key) && row.status === 'active' && row.local_path) {
256
- toDisable.push(row);
257
- }
258
- }
259
-
260
- return { toIndex, toDisable };
261
- }