brain-memory 0.1.0-beta.1 → 0.1.0-beta.10
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/README.md +140 -54
- package/assets/icon.png +0 -0
- package/bin/install.js +200 -230
- package/bin/recall.js +211 -0
- package/bin/reinforce.js +179 -0
- package/commands/brain/consolidate.md +2 -2
- package/commands/brain/explore.md +2 -2
- package/commands/brain/forget.md +2 -2
- package/commands/brain/init.md +4 -12
- package/commands/brain/memorize.md +22 -7
- package/commands/brain/remember.md +43 -105
- package/commands/brain/review.md +2 -2
- package/commands/brain/sleep.md +4 -23
- package/commands/brain/status.md +15 -5
- package/commands/brain/sunshine.md +13 -13
- package/commands/brain/sync.md +222 -0
- package/package.json +16 -5
- package/prompts/claude.md +75 -13
- package/prompts/gemini.md +75 -13
- package/prompts/openai.md +73 -11
- package/src/crypto.js +130 -0
- package/src/export-import.js +185 -0
- package/src/git-sync.js +444 -0
- package/src/index-manager.js +94 -18
- package/src/installer.js +361 -0
- package/src/tfidf.js +408 -0
- package/hooks/session-end.md +0 -48
- package/hooks/session-start.md +0 -48
package/src/index-manager.js
CHANGED
|
@@ -1,28 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Brain Memory — Index Manager
|
|
3
3
|
*
|
|
4
|
-
* Utilities for reading, updating, and maintaining the
|
|
4
|
+
* Utilities for reading, updating, and maintaining the ~/.brain/index.json
|
|
5
5
|
* file. Used by hooks and external integrations.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
const fs = require('fs');
|
|
9
9
|
const path = require('path');
|
|
10
|
+
const os = require('os');
|
|
11
|
+
const crypto = require('crypto');
|
|
10
12
|
|
|
11
|
-
const DEFAULT_BRAIN_DIR = '.brain';
|
|
13
|
+
const DEFAULT_BRAIN_DIR = path.join(os.homedir(), '.brain');
|
|
12
14
|
const INDEX_FILE = 'index.json';
|
|
13
15
|
const ASSOCIATIONS_FILE = 'associations.json';
|
|
14
16
|
const CONTEXTS_FILE = 'contexts.json';
|
|
15
17
|
const REVIEW_QUEUE_FILE = 'review-queue.json';
|
|
16
18
|
const ARCHIVE_INDEX_FILE = '_archived/index.json';
|
|
19
|
+
const SEARCH_INDEX_FILE = 'search-index.json';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Write data to a file atomically by writing to a temp file first,
|
|
23
|
+
* then renaming. Prevents corruption if the process crashes mid-write.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} filePath - Target file path
|
|
26
|
+
* @param {string} data - Data to write
|
|
27
|
+
*/
|
|
28
|
+
function atomicWriteSync(filePath, data) {
|
|
29
|
+
const tmpPath = filePath + '.tmp.' + crypto.randomBytes(4).toString('hex');
|
|
30
|
+
fs.writeFileSync(tmpPath, data);
|
|
31
|
+
fs.renameSync(tmpPath, filePath);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validate that a target path is within the brain directory.
|
|
36
|
+
* Prevents path traversal attacks (e.g. categoryPath = '../../etc/passwd').
|
|
37
|
+
*
|
|
38
|
+
* @param {string} targetPath - The resolved path to validate
|
|
39
|
+
* @param {string} brainDir - The brain directory root
|
|
40
|
+
* @throws {Error} if targetPath is outside brainDir
|
|
41
|
+
*/
|
|
42
|
+
function validateBrainPath(targetPath, brainDir) {
|
|
43
|
+
const resolved = path.resolve(targetPath);
|
|
44
|
+
const resolvedBrain = path.resolve(brainDir);
|
|
45
|
+
if (!resolved.startsWith(resolvedBrain + path.sep) && resolved !== resolvedBrain) {
|
|
46
|
+
throw new Error(`Path traversal detected: ${targetPath} is outside ${brainDir}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
17
49
|
|
|
18
50
|
/**
|
|
19
51
|
* Resolve the brain directory path.
|
|
20
52
|
*
|
|
21
|
-
* @param {string} [
|
|
22
|
-
* @returns {string} Absolute path to
|
|
53
|
+
* @param {string} [overrideBase] - Override base directory (for testing)
|
|
54
|
+
* @returns {string} Absolute path to ~/.brain/ (or overrideBase/.brain/ if provided)
|
|
23
55
|
*/
|
|
24
|
-
function getBrainDir(
|
|
25
|
-
return path.join(
|
|
56
|
+
function getBrainDir(overrideBase) {
|
|
57
|
+
if (overrideBase) return path.join(overrideBase, '.brain');
|
|
58
|
+
return DEFAULT_BRAIN_DIR;
|
|
26
59
|
}
|
|
27
60
|
|
|
28
61
|
/**
|
|
@@ -46,7 +79,7 @@ function readIndex(projectRoot) {
|
|
|
46
79
|
function writeIndex(index, projectRoot) {
|
|
47
80
|
const indexPath = path.join(getBrainDir(projectRoot), INDEX_FILE);
|
|
48
81
|
index.last_updated = new Date().toISOString();
|
|
49
|
-
|
|
82
|
+
atomicWriteSync(indexPath, JSON.stringify(index, null, 2) + '\n');
|
|
50
83
|
}
|
|
51
84
|
|
|
52
85
|
/**
|
|
@@ -106,27 +139,35 @@ function generateId() {
|
|
|
106
139
|
/**
|
|
107
140
|
* Read a _meta.json file from a brain subdirectory.
|
|
108
141
|
*
|
|
109
|
-
* @param {string} categoryPath - Relative path within
|
|
142
|
+
* @param {string} categoryPath - Relative path within ~/.brain/
|
|
110
143
|
* @param {string} [projectRoot] - Project root directory
|
|
111
144
|
* @returns {Object|null} Parsed meta or null
|
|
112
145
|
*/
|
|
113
146
|
function readMeta(categoryPath, projectRoot) {
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
147
|
+
const brainDir = getBrainDir(projectRoot);
|
|
148
|
+
const metaPath = path.join(brainDir, categoryPath, '_meta.json');
|
|
149
|
+
validateBrainPath(metaPath, brainDir);
|
|
150
|
+
try {
|
|
151
|
+
return JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
|
|
152
|
+
} catch (err) {
|
|
153
|
+
if (err.code === 'ENOENT') return null;
|
|
154
|
+
throw err;
|
|
155
|
+
}
|
|
117
156
|
}
|
|
118
157
|
|
|
119
158
|
/**
|
|
120
159
|
* Write a _meta.json file to a brain subdirectory.
|
|
121
160
|
*
|
|
122
|
-
* @param {string} categoryPath - Relative path within
|
|
161
|
+
* @param {string} categoryPath - Relative path within ~/.brain/
|
|
123
162
|
* @param {Object} meta - Meta object to write
|
|
124
163
|
* @param {string} [projectRoot] - Project root directory
|
|
125
164
|
*/
|
|
126
165
|
function writeMeta(categoryPath, meta, projectRoot) {
|
|
127
|
-
const
|
|
166
|
+
const brainDir = getBrainDir(projectRoot);
|
|
167
|
+
const metaPath = path.join(brainDir, categoryPath, '_meta.json');
|
|
168
|
+
validateBrainPath(metaPath, brainDir);
|
|
128
169
|
fs.mkdirSync(path.dirname(metaPath), { recursive: true });
|
|
129
|
-
|
|
170
|
+
atomicWriteSync(metaPath, JSON.stringify(meta, null, 2) + '\n');
|
|
130
171
|
}
|
|
131
172
|
|
|
132
173
|
/**
|
|
@@ -167,7 +208,7 @@ function readAssociations(projectRoot) {
|
|
|
167
208
|
*/
|
|
168
209
|
function writeAssociations(associations, projectRoot) {
|
|
169
210
|
const filePath = path.join(getBrainDir(projectRoot), ASSOCIATIONS_FILE);
|
|
170
|
-
|
|
211
|
+
atomicWriteSync(filePath, JSON.stringify(associations, null, 2) + '\n');
|
|
171
212
|
}
|
|
172
213
|
|
|
173
214
|
/**
|
|
@@ -273,7 +314,7 @@ function readContexts(projectRoot) {
|
|
|
273
314
|
*/
|
|
274
315
|
function writeContexts(contexts, projectRoot) {
|
|
275
316
|
const filePath = path.join(getBrainDir(projectRoot), CONTEXTS_FILE);
|
|
276
|
-
|
|
317
|
+
atomicWriteSync(filePath, JSON.stringify(contexts, null, 2) + '\n');
|
|
277
318
|
}
|
|
278
319
|
|
|
279
320
|
// --- Review Queue (Phase 4: Spaced Repetition) ---
|
|
@@ -298,7 +339,7 @@ function readReviewQueue(projectRoot) {
|
|
|
298
339
|
*/
|
|
299
340
|
function writeReviewQueue(queue, projectRoot) {
|
|
300
341
|
const filePath = path.join(getBrainDir(projectRoot), REVIEW_QUEUE_FILE);
|
|
301
|
-
|
|
342
|
+
atomicWriteSync(filePath, JSON.stringify(queue, null, 2) + '\n');
|
|
302
343
|
}
|
|
303
344
|
|
|
304
345
|
// --- Archive Index (Phase 4: Archive Search) ---
|
|
@@ -324,7 +365,7 @@ function readArchiveIndex(projectRoot) {
|
|
|
324
365
|
function writeArchiveIndex(archiveIndex, projectRoot) {
|
|
325
366
|
const filePath = path.join(getBrainDir(projectRoot), ARCHIVE_INDEX_FILE);
|
|
326
367
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
327
|
-
|
|
368
|
+
atomicWriteSync(filePath, JSON.stringify(archiveIndex, null, 2) + '\n');
|
|
328
369
|
}
|
|
329
370
|
|
|
330
371
|
/**
|
|
@@ -370,6 +411,35 @@ function removeFromReviewQueue(queue, memoryId) {
|
|
|
370
411
|
return queue;
|
|
371
412
|
}
|
|
372
413
|
|
|
414
|
+
// --- Search Index (TF-IDF) ---
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Read the TF-IDF search index.
|
|
418
|
+
*
|
|
419
|
+
* @param {string} [projectRoot] - Project root directory
|
|
420
|
+
* @returns {Object|null} Parsed search index or null
|
|
421
|
+
*/
|
|
422
|
+
function readSearchIndex(projectRoot) {
|
|
423
|
+
const filePath = path.join(getBrainDir(projectRoot), SEARCH_INDEX_FILE);
|
|
424
|
+
try {
|
|
425
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
426
|
+
} catch (err) {
|
|
427
|
+
if (err.code === 'ENOENT') return null;
|
|
428
|
+
throw err;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Write the TF-IDF search index to disk.
|
|
434
|
+
*
|
|
435
|
+
* @param {Object} searchIndex - The search index object
|
|
436
|
+
* @param {string} [projectRoot] - Project root directory
|
|
437
|
+
*/
|
|
438
|
+
function writeSearchIndex(searchIndex, projectRoot) {
|
|
439
|
+
const filePath = path.join(getBrainDir(projectRoot), SEARCH_INDEX_FILE);
|
|
440
|
+
atomicWriteSync(filePath, JSON.stringify(searchIndex, null, 2) + '\n');
|
|
441
|
+
}
|
|
442
|
+
|
|
373
443
|
module.exports = {
|
|
374
444
|
getBrainDir,
|
|
375
445
|
readIndex,
|
|
@@ -395,7 +465,13 @@ module.exports = {
|
|
|
395
465
|
writeReviewQueue,
|
|
396
466
|
readArchiveIndex,
|
|
397
467
|
writeArchiveIndex,
|
|
468
|
+
// Search Index (TF-IDF)
|
|
469
|
+
readSearchIndex,
|
|
470
|
+
writeSearchIndex,
|
|
398
471
|
// Deep erasure utilities
|
|
399
472
|
removeEdgesForMemory,
|
|
400
473
|
removeFromReviewQueue,
|
|
474
|
+
// Security utilities
|
|
475
|
+
atomicWriteSync,
|
|
476
|
+
validateBrainPath,
|
|
401
477
|
};
|
package/src/installer.js
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const os = require('os');
|
|
4
|
+
|
|
5
|
+
const PACKAGE_ROOT = path.resolve(__dirname, '..');
|
|
6
|
+
|
|
7
|
+
const RUNTIMES = {
|
|
8
|
+
claude: {
|
|
9
|
+
name: 'Claude Code',
|
|
10
|
+
globalDir: path.join(os.homedir(), '.claude'),
|
|
11
|
+
localDir: '.claude',
|
|
12
|
+
commandsSubdir: 'commands',
|
|
13
|
+
promptFile: 'CLAUDE.md',
|
|
14
|
+
promptSource: 'claude.md',
|
|
15
|
+
commandStyle: 'flat',
|
|
16
|
+
},
|
|
17
|
+
gemini: {
|
|
18
|
+
name: 'Gemini CLI',
|
|
19
|
+
globalDir: path.join(os.homedir(), '.gemini'),
|
|
20
|
+
localDir: '.gemini',
|
|
21
|
+
commandsSubdir: 'commands',
|
|
22
|
+
promptFile: 'GEMINI.md',
|
|
23
|
+
promptSource: 'gemini.md',
|
|
24
|
+
commandStyle: 'flat',
|
|
25
|
+
},
|
|
26
|
+
openai: {
|
|
27
|
+
name: 'OpenAI Codex CLI',
|
|
28
|
+
globalDir: path.join(os.homedir(), '.codex'),
|
|
29
|
+
localDir: '.codex',
|
|
30
|
+
commandsSubdir: 'skills',
|
|
31
|
+
promptFile: 'AGENTS.md',
|
|
32
|
+
promptSource: 'openai.md',
|
|
33
|
+
commandStyle: 'skills',
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const BRAIN_MARKER_START = '<!-- BRAIN-MEMORY-START -->';
|
|
38
|
+
const BRAIN_MARKER_END = '<!-- BRAIN-MEMORY-END -->';
|
|
39
|
+
|
|
40
|
+
function copyDir(src, dest, _depth = 0) {
|
|
41
|
+
if (_depth > 20) {
|
|
42
|
+
throw new Error(`copyDir: max depth (20) exceeded at ${src}`);
|
|
43
|
+
}
|
|
44
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
45
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
46
|
+
for (const entry of entries) {
|
|
47
|
+
if (entry.isSymbolicLink()) continue;
|
|
48
|
+
const srcPath = path.join(src, entry.name);
|
|
49
|
+
const destPath = path.join(dest, entry.name);
|
|
50
|
+
if (entry.isDirectory()) {
|
|
51
|
+
copyDir(srcPath, destPath, _depth + 1);
|
|
52
|
+
} else {
|
|
53
|
+
fs.copyFileSync(srcPath, destPath);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function installSkills(commandsSrc, skillsDest) {
|
|
59
|
+
const entries = fs.readdirSync(commandsSrc).filter((f) => f.endsWith('.md'));
|
|
60
|
+
for (const file of entries) {
|
|
61
|
+
const name = file.replace('.md', '');
|
|
62
|
+
const skillDir = path.join(skillsDest, `brain-${name}`);
|
|
63
|
+
fs.mkdirSync(skillDir, { recursive: true });
|
|
64
|
+
fs.copyFileSync(
|
|
65
|
+
path.join(commandsSrc, file),
|
|
66
|
+
path.join(skillDir, 'SKILL.md')
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function injectPrompt(targetDir, promptFile, promptSource) {
|
|
72
|
+
const promptContent = fs.readFileSync(
|
|
73
|
+
path.join(PACKAGE_ROOT, 'prompts', promptSource),
|
|
74
|
+
'utf-8'
|
|
75
|
+
);
|
|
76
|
+
const targetPath = path.join(targetDir, promptFile);
|
|
77
|
+
|
|
78
|
+
const wrappedContent = `\n${BRAIN_MARKER_START}\n${promptContent}\n${BRAIN_MARKER_END}\n`;
|
|
79
|
+
|
|
80
|
+
if (fs.existsSync(targetPath)) {
|
|
81
|
+
let existing = fs.readFileSync(targetPath, 'utf-8');
|
|
82
|
+
// Remove old brain section if present
|
|
83
|
+
const startIdx = existing.indexOf(BRAIN_MARKER_START);
|
|
84
|
+
const endIdx = existing.indexOf(BRAIN_MARKER_END);
|
|
85
|
+
if (startIdx !== -1 && endIdx !== -1) {
|
|
86
|
+
existing =
|
|
87
|
+
existing.substring(0, startIdx) +
|
|
88
|
+
existing.substring(endIdx + BRAIN_MARKER_END.length);
|
|
89
|
+
}
|
|
90
|
+
fs.writeFileSync(targetPath, existing.trimEnd() + '\n' + wrappedContent);
|
|
91
|
+
} else {
|
|
92
|
+
fs.writeFileSync(targetPath, wrappedContent.trim() + '\n');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function detectInstallations() {
|
|
97
|
+
const results = [];
|
|
98
|
+
for (const [runtime, config] of Object.entries(RUNTIMES)) {
|
|
99
|
+
for (const scope of ['global', 'local']) {
|
|
100
|
+
const targetDir = scope === 'global' ? config.globalDir : config.localDir;
|
|
101
|
+
const promptTarget = scope === 'global' ? config.globalDir : '.';
|
|
102
|
+
const promptPath = path.join(promptTarget, config.promptFile);
|
|
103
|
+
|
|
104
|
+
// Check for command files/dirs
|
|
105
|
+
let commandsFound = false;
|
|
106
|
+
if (config.commandStyle === 'skills') {
|
|
107
|
+
const skillsDir = path.join(targetDir, config.commandsSubdir);
|
|
108
|
+
commandsFound = fs.existsSync(path.join(skillsDir, 'brain-init', 'SKILL.md'));
|
|
109
|
+
} else {
|
|
110
|
+
const commandsDir = path.join(targetDir, config.commandsSubdir, 'brain');
|
|
111
|
+
commandsFound = fs.existsSync(commandsDir) &&
|
|
112
|
+
fs.readdirSync(commandsDir).some((f) => f.endsWith('.md'));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Check for prompt markers
|
|
116
|
+
let promptFound = false;
|
|
117
|
+
if (fs.existsSync(promptPath)) {
|
|
118
|
+
const content = fs.readFileSync(promptPath, 'utf-8');
|
|
119
|
+
promptFound = content.includes(BRAIN_MARKER_START) && content.includes(BRAIN_MARKER_END);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (commandsFound || promptFound) {
|
|
123
|
+
results.push({
|
|
124
|
+
runtime,
|
|
125
|
+
scope,
|
|
126
|
+
runtimeName: config.name,
|
|
127
|
+
commandsFound,
|
|
128
|
+
promptFound,
|
|
129
|
+
targetDir,
|
|
130
|
+
promptPath,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return results;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function removePromptSection(promptPath) {
|
|
139
|
+
if (!fs.existsSync(promptPath)) {
|
|
140
|
+
return { removed: false, reason: 'file-not-found' };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const content = fs.readFileSync(promptPath, 'utf-8');
|
|
144
|
+
const startIdx = content.indexOf(BRAIN_MARKER_START);
|
|
145
|
+
const endIdx = content.indexOf(BRAIN_MARKER_END);
|
|
146
|
+
|
|
147
|
+
if (startIdx === -1 || endIdx === -1) {
|
|
148
|
+
return { removed: false, reason: 'no-markers' };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const before = content.substring(0, startIdx);
|
|
152
|
+
const after = content.substring(endIdx + BRAIN_MARKER_END.length);
|
|
153
|
+
const remaining = (before + after).trim();
|
|
154
|
+
|
|
155
|
+
if (remaining.length === 0) {
|
|
156
|
+
fs.unlinkSync(promptPath);
|
|
157
|
+
return { removed: true, fileDeleted: true };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
fs.writeFileSync(promptPath, remaining + '\n');
|
|
161
|
+
return { removed: true, fileDeleted: false };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function removeCommands(targetDir, config) {
|
|
165
|
+
const removed = [];
|
|
166
|
+
|
|
167
|
+
if (config.commandStyle === 'skills') {
|
|
168
|
+
const skillsDir = path.join(targetDir, config.commandsSubdir);
|
|
169
|
+
if (fs.existsSync(skillsDir)) {
|
|
170
|
+
const entries = fs.readdirSync(skillsDir).filter((d) => d.startsWith('brain-'));
|
|
171
|
+
for (const entry of entries) {
|
|
172
|
+
const fullPath = path.join(skillsDir, entry);
|
|
173
|
+
fs.rmSync(fullPath, { recursive: true, force: true });
|
|
174
|
+
removed.push(fullPath);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
const commandsDir = path.join(targetDir, config.commandsSubdir, 'brain');
|
|
179
|
+
if (fs.existsSync(commandsDir)) {
|
|
180
|
+
removed.push(commandsDir);
|
|
181
|
+
fs.rmSync(commandsDir, { recursive: true, force: true });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return removed;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function uninstallForRuntime(runtime, scope) {
|
|
189
|
+
if (!Object.keys(RUNTIMES).includes(runtime)) {
|
|
190
|
+
throw new Error(`Unknown runtime: ${runtime}. Valid: ${Object.keys(RUNTIMES).join(', ')}`);
|
|
191
|
+
}
|
|
192
|
+
if (scope !== 'global' && scope !== 'local') {
|
|
193
|
+
throw new Error(`Invalid scope: ${scope}. Must be 'global' or 'local'.`);
|
|
194
|
+
}
|
|
195
|
+
const config = RUNTIMES[runtime];
|
|
196
|
+
const targetDir = scope === 'global' ? config.globalDir : config.localDir;
|
|
197
|
+
const promptTarget = scope === 'global' ? config.globalDir : '.';
|
|
198
|
+
const promptPath = path.join(promptTarget, config.promptFile);
|
|
199
|
+
|
|
200
|
+
const removedCommands = removeCommands(targetDir, config);
|
|
201
|
+
const promptResult = removePromptSection(promptPath);
|
|
202
|
+
|
|
203
|
+
return { removedCommands, promptResult };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function installForRuntime(runtime, scope) {
|
|
207
|
+
if (!Object.keys(RUNTIMES).includes(runtime)) {
|
|
208
|
+
throw new Error(`Unknown runtime: ${runtime}. Valid: ${Object.keys(RUNTIMES).join(', ')}`);
|
|
209
|
+
}
|
|
210
|
+
if (scope !== 'global' && scope !== 'local') {
|
|
211
|
+
throw new Error(`Invalid scope: ${scope}. Must be 'global' or 'local'.`);
|
|
212
|
+
}
|
|
213
|
+
const config = RUNTIMES[runtime];
|
|
214
|
+
const targetDir = scope === 'global' ? config.globalDir : config.localDir;
|
|
215
|
+
const commandsSrc = path.join(PACKAGE_ROOT, 'commands', 'brain');
|
|
216
|
+
|
|
217
|
+
if (config.commandStyle === 'skills') {
|
|
218
|
+
const skillsDest = path.join(targetDir, config.commandsSubdir);
|
|
219
|
+
installSkills(commandsSrc, skillsDest);
|
|
220
|
+
} else {
|
|
221
|
+
const commandsDest = path.join(targetDir, config.commandsSubdir, 'brain');
|
|
222
|
+
copyDir(commandsSrc, commandsDest);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const promptTarget = scope === 'global' ? config.globalDir : '.';
|
|
226
|
+
injectPrompt(promptTarget, config.promptFile, config.promptSource);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function initializeBrain(overrideBase) {
|
|
230
|
+
const brainDir = path.join(overrideBase || os.homedir(), '.brain');
|
|
231
|
+
|
|
232
|
+
if (fs.existsSync(brainDir)) {
|
|
233
|
+
return { alreadyExists: true };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const now = new Date().toISOString();
|
|
237
|
+
|
|
238
|
+
// Create directories
|
|
239
|
+
const categories = ['professional', 'personal', 'social', 'family', '_consolidated', '_archived'];
|
|
240
|
+
for (const cat of categories) {
|
|
241
|
+
fs.mkdirSync(path.join(brainDir, cat), { recursive: true });
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Create index.json
|
|
245
|
+
const index = {
|
|
246
|
+
version: 2,
|
|
247
|
+
created: now,
|
|
248
|
+
last_updated: now,
|
|
249
|
+
memory_count: 0,
|
|
250
|
+
memories: {},
|
|
251
|
+
config: {
|
|
252
|
+
max_depth: 6,
|
|
253
|
+
consolidation_threshold: 0.3,
|
|
254
|
+
decay_check_interval_days: 7,
|
|
255
|
+
strength_boost_on_recall: 0.05,
|
|
256
|
+
auto_consolidate: true,
|
|
257
|
+
propagation_window_days: 7,
|
|
258
|
+
association_config: {
|
|
259
|
+
co_retrieval_boost: 0.10,
|
|
260
|
+
link_decay_rate: 0.998,
|
|
261
|
+
link_prune_threshold: 0.05,
|
|
262
|
+
spreading_activation_depth: 2,
|
|
263
|
+
spreading_activation_decay: 0.5,
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
fs.writeFileSync(
|
|
268
|
+
path.join(brainDir, 'index.json'),
|
|
269
|
+
JSON.stringify(index, null, 2) + '\n'
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
// Create associations.json
|
|
273
|
+
fs.writeFileSync(
|
|
274
|
+
path.join(brainDir, 'associations.json'),
|
|
275
|
+
JSON.stringify({ version: 1, edges: {} }, null, 2) + '\n'
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
// Create contexts.json
|
|
279
|
+
fs.writeFileSync(
|
|
280
|
+
path.join(brainDir, 'contexts.json'),
|
|
281
|
+
JSON.stringify({ version: 1, sessions: [] }, null, 2) + '\n'
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// Create review-queue.json
|
|
285
|
+
fs.writeFileSync(
|
|
286
|
+
path.join(brainDir, 'review-queue.json'),
|
|
287
|
+
JSON.stringify({ version: 1, items: [] }, null, 2) + '\n'
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
// Create _archived/index.json
|
|
291
|
+
fs.writeFileSync(
|
|
292
|
+
path.join(brainDir, '_archived', 'index.json'),
|
|
293
|
+
JSON.stringify({ version: 1, archived_count: 0, memories: {} }, null, 2) + '\n'
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
// Create search-index.json (TF-IDF)
|
|
297
|
+
fs.writeFileSync(
|
|
298
|
+
path.join(brainDir, 'search-index.json'),
|
|
299
|
+
JSON.stringify({ version: 1, doc_count: 0, documents: {}, df: {} }, null, 2) + '\n'
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
// Load category descriptions from template
|
|
303
|
+
const templatePath = path.join(PACKAGE_ROOT, 'templates', 'default-categories.json');
|
|
304
|
+
let template = { top_categories: [] };
|
|
305
|
+
if (fs.existsSync(templatePath)) {
|
|
306
|
+
template = JSON.parse(fs.readFileSync(templatePath, 'utf-8'));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Create _meta.json for each category
|
|
310
|
+
const mainCategories = ['professional', 'personal', 'social', 'family'];
|
|
311
|
+
for (const cat of mainCategories) {
|
|
312
|
+
const templateCat = template.top_categories.find((c) => c.name === cat) || {};
|
|
313
|
+
const meta = {
|
|
314
|
+
category: cat,
|
|
315
|
+
description: templateCat.description || cat,
|
|
316
|
+
created: now,
|
|
317
|
+
memory_count: 0,
|
|
318
|
+
subcategories: [],
|
|
319
|
+
};
|
|
320
|
+
fs.writeFileSync(
|
|
321
|
+
path.join(brainDir, cat, '_meta.json'),
|
|
322
|
+
JSON.stringify(meta, null, 2) + '\n'
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Create _meta.json for special directories
|
|
327
|
+
for (const special of ['_consolidated', '_archived']) {
|
|
328
|
+
const meta = {
|
|
329
|
+
category: special,
|
|
330
|
+
description:
|
|
331
|
+
special === '_consolidated'
|
|
332
|
+
? 'Merged memories from consolidation operations'
|
|
333
|
+
: 'Archived memories preserved for recovery',
|
|
334
|
+
created: now,
|
|
335
|
+
memory_count: 0,
|
|
336
|
+
subcategories: [],
|
|
337
|
+
};
|
|
338
|
+
fs.writeFileSync(
|
|
339
|
+
path.join(brainDir, special, '_meta.json'),
|
|
340
|
+
JSON.stringify(meta, null, 2) + '\n'
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return { alreadyExists: false, brainDir };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
module.exports = {
|
|
348
|
+
RUNTIMES,
|
|
349
|
+
BRAIN_MARKER_START,
|
|
350
|
+
BRAIN_MARKER_END,
|
|
351
|
+
PACKAGE_ROOT,
|
|
352
|
+
copyDir,
|
|
353
|
+
installSkills,
|
|
354
|
+
injectPrompt,
|
|
355
|
+
installForRuntime,
|
|
356
|
+
initializeBrain,
|
|
357
|
+
detectInstallations,
|
|
358
|
+
removePromptSection,
|
|
359
|
+
removeCommands,
|
|
360
|
+
uninstallForRuntime,
|
|
361
|
+
};
|