brain-memory 0.1.0-beta.2 → 0.1.0-beta.20
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 +270 -72
- package/assets/icon.png +0 -0
- package/assets/icon.svg +27 -0
- package/bin/brain.js +111 -0
- package/bin/cloud-sync.js +149 -0
- package/bin/install.js +217 -242
- package/bin/memorize.js +433 -0
- package/bin/pin.js +44 -0
- package/bin/recall.js +237 -0
- package/bin/reinforce.js +187 -0
- package/bin/session-start.js +292 -0
- package/bin/skill.js +67 -0
- package/bin/unpin.js +38 -0
- package/commands/brain/consolidate.md +7 -2
- package/commands/brain/explore.md +7 -2
- package/commands/brain/forget.md +7 -2
- package/commands/brain/init.md +8 -12
- package/commands/brain/memorize.md +76 -144
- package/commands/brain/pin.md +37 -0
- package/commands/brain/remember.md +48 -105
- package/commands/brain/review.md +7 -2
- package/commands/brain/skills.md +60 -0
- package/commands/brain/sleep.md +68 -27
- package/commands/brain/status.md +19 -5
- package/commands/brain/sunshine.md +18 -13
- package/commands/brain/sync.md +360 -0
- package/commands/brain/unpin.md +26 -0
- package/package.json +18 -8
- package/prompts/claude.md +115 -12
- package/prompts/gemini.md +115 -12
- package/prompts/openai.md +113 -10
- package/prompts/opencode.md +178 -0
- package/src/cloud-sync.js +524 -0
- 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 +205 -18
- package/src/installer.js +384 -0
- package/src/pinning.js +124 -0
- package/src/scorer.js +6 -2
- package/src/skills.js +208 -0
- package/src/tfidf.js +408 -0
- package/hooks/session-end.md +0 -48
- package/hooks/session-start.md +0 -48
package/src/crypto.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brain Memory — Crypto Utilities
|
|
3
|
+
*
|
|
4
|
+
* AES-256-GCM encryption for memory export/import and encrypted sync.
|
|
5
|
+
* Zero external dependencies — uses only Node.js built-in crypto.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const crypto = require('crypto');
|
|
9
|
+
|
|
10
|
+
const PBKDF2_ITERATIONS = 100000;
|
|
11
|
+
const PBKDF2_DIGEST = 'sha512';
|
|
12
|
+
const KEY_LENGTH = 32; // 256 bits
|
|
13
|
+
const IV_LENGTH = 12; // 96 bits for GCM
|
|
14
|
+
const SALT_LENGTH = 32;
|
|
15
|
+
const AUTH_TAG_LENGTH = 16;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Derive an AES-256 key from a passphrase using PBKDF2.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} passphrase - User-provided passphrase
|
|
21
|
+
* @param {Buffer} salt - 32-byte salt
|
|
22
|
+
* @returns {Buffer} 32-byte derived key
|
|
23
|
+
*/
|
|
24
|
+
function deriveKey(passphrase, salt) {
|
|
25
|
+
return crypto.pbkdf2Sync(passphrase, salt, PBKDF2_ITERATIONS, KEY_LENGTH, PBKDF2_DIGEST);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Encrypt a UTF-8 string with AES-256-GCM.
|
|
30
|
+
*
|
|
31
|
+
* Output format: salt (32) + iv (12) + authTag (16) + ciphertext
|
|
32
|
+
*
|
|
33
|
+
* @param {string} plaintext - UTF-8 string to encrypt
|
|
34
|
+
* @param {string} passphrase - Passphrase (key derived via PBKDF2)
|
|
35
|
+
* @returns {Buffer} Encrypted buffer
|
|
36
|
+
*/
|
|
37
|
+
function encrypt(plaintext, passphrase) {
|
|
38
|
+
const salt = crypto.randomBytes(SALT_LENGTH);
|
|
39
|
+
const key = deriveKey(passphrase, salt);
|
|
40
|
+
const iv = crypto.randomBytes(IV_LENGTH);
|
|
41
|
+
|
|
42
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
43
|
+
const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
|
|
44
|
+
const authTag = cipher.getAuthTag();
|
|
45
|
+
|
|
46
|
+
return Buffer.concat([salt, iv, authTag, encrypted]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Decrypt an AES-256-GCM encrypted buffer back to a UTF-8 string.
|
|
51
|
+
*
|
|
52
|
+
* @param {Buffer} encrypted - Buffer produced by encrypt()
|
|
53
|
+
* @param {string} passphrase - Same passphrase used for encryption
|
|
54
|
+
* @returns {string} Decrypted UTF-8 string
|
|
55
|
+
* @throws {Error} If passphrase is wrong or data is tampered
|
|
56
|
+
*/
|
|
57
|
+
function decrypt(encrypted, passphrase) {
|
|
58
|
+
if (encrypted.length < SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH) {
|
|
59
|
+
throw new Error('Invalid encrypted data: too short');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const salt = encrypted.subarray(0, SALT_LENGTH);
|
|
63
|
+
const iv = encrypted.subarray(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
|
|
64
|
+
const authTag = encrypted.subarray(SALT_LENGTH + IV_LENGTH, SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH);
|
|
65
|
+
const ciphertext = encrypted.subarray(SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH);
|
|
66
|
+
|
|
67
|
+
const key = deriveKey(passphrase, salt);
|
|
68
|
+
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
|
|
69
|
+
decipher.setAuthTag(authTag);
|
|
70
|
+
|
|
71
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Encrypt a Buffer with AES-256-GCM.
|
|
76
|
+
*
|
|
77
|
+
* Output format: salt (32) + iv (12) + authTag (16) + ciphertext
|
|
78
|
+
*
|
|
79
|
+
* @param {Buffer} buffer - Raw buffer to encrypt
|
|
80
|
+
* @param {string} passphrase - Passphrase
|
|
81
|
+
* @returns {Buffer} Encrypted buffer
|
|
82
|
+
*/
|
|
83
|
+
function encryptBuffer(buffer, passphrase) {
|
|
84
|
+
const salt = crypto.randomBytes(SALT_LENGTH);
|
|
85
|
+
const key = deriveKey(passphrase, salt);
|
|
86
|
+
const iv = crypto.randomBytes(IV_LENGTH);
|
|
87
|
+
|
|
88
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
89
|
+
const encrypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
|
|
90
|
+
const authTag = cipher.getAuthTag();
|
|
91
|
+
|
|
92
|
+
return Buffer.concat([salt, iv, authTag, encrypted]);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Decrypt a Buffer encrypted with encryptBuffer().
|
|
97
|
+
*
|
|
98
|
+
* @param {Buffer} encrypted - Buffer produced by encryptBuffer()
|
|
99
|
+
* @param {string} passphrase - Same passphrase used for encryption
|
|
100
|
+
* @returns {Buffer} Decrypted buffer
|
|
101
|
+
* @throws {Error} If passphrase is wrong or data is tampered
|
|
102
|
+
*/
|
|
103
|
+
function decryptBuffer(encrypted, passphrase) {
|
|
104
|
+
if (encrypted.length < SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH) {
|
|
105
|
+
throw new Error('Invalid encrypted data: too short');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const salt = encrypted.subarray(0, SALT_LENGTH);
|
|
109
|
+
const iv = encrypted.subarray(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
|
|
110
|
+
const authTag = encrypted.subarray(SALT_LENGTH + IV_LENGTH, SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH);
|
|
111
|
+
const ciphertext = encrypted.subarray(SALT_LENGTH + IV_LENGTH + AUTH_TAG_LENGTH);
|
|
112
|
+
|
|
113
|
+
const key = deriveKey(passphrase, salt);
|
|
114
|
+
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
|
|
115
|
+
decipher.setAuthTag(authTag);
|
|
116
|
+
|
|
117
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
deriveKey,
|
|
122
|
+
encrypt,
|
|
123
|
+
decrypt,
|
|
124
|
+
encryptBuffer,
|
|
125
|
+
decryptBuffer,
|
|
126
|
+
PBKDF2_ITERATIONS,
|
|
127
|
+
SALT_LENGTH,
|
|
128
|
+
IV_LENGTH,
|
|
129
|
+
AUTH_TAG_LENGTH,
|
|
130
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brain Memory — Export / Import
|
|
3
|
+
*
|
|
4
|
+
* Single-file encrypted export/import for portable memory transfers.
|
|
5
|
+
* Pack all ~/.brain/ files into one JSON blob, optionally AES-256-GCM encrypt,
|
|
6
|
+
* and write a single .brain-export file. Import reverses the process.
|
|
7
|
+
*
|
|
8
|
+
* Zero external dependencies — uses only Node.js built-in fs and crypto.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const { encrypt, decrypt } = require('./crypto');
|
|
14
|
+
|
|
15
|
+
// Directories inside ~/.brain/ to skip during export
|
|
16
|
+
const EXCLUDED = new Set(['.sync', '.DS_Store']);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Export the entire ~/.brain/ directory to a single file.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} brainDir - Absolute path to ~/.brain/
|
|
22
|
+
* @param {string} outputPath - Destination file path
|
|
23
|
+
* @param {string|null} [passphrase] - If provided, encrypt the export
|
|
24
|
+
* @returns {{ fileCount: number, outputPath: string }}
|
|
25
|
+
*/
|
|
26
|
+
function exportBrain(brainDir, outputPath, passphrase) {
|
|
27
|
+
if (!fs.existsSync(brainDir)) {
|
|
28
|
+
throw new Error('~/.brain/ directory not found. Run /brain:init first.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const files = {};
|
|
32
|
+
collectFiles(brainDir, brainDir, files);
|
|
33
|
+
|
|
34
|
+
const payload = JSON.stringify({ version: 1, exportedAt: new Date().toISOString(), files });
|
|
35
|
+
|
|
36
|
+
if (passphrase) {
|
|
37
|
+
const encrypted = encrypt(payload, passphrase);
|
|
38
|
+
fs.writeFileSync(outputPath, encrypted);
|
|
39
|
+
} else {
|
|
40
|
+
fs.writeFileSync(outputPath, payload, 'utf8');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { fileCount: Object.keys(files).length, outputPath };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Recursively collect all files from brainDir into a flat { relativePath: content } map.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} dir - Current directory to scan
|
|
50
|
+
* @param {string} brainDir - Root ~/.brain/ directory
|
|
51
|
+
* @param {Object} files - Accumulator object
|
|
52
|
+
*/
|
|
53
|
+
function collectFiles(dir, brainDir, files) {
|
|
54
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
const rel = path.relative(brainDir, path.join(dir, entry.name));
|
|
57
|
+
const topLevel = rel.split(path.sep)[0];
|
|
58
|
+
if (EXCLUDED.has(topLevel)) continue;
|
|
59
|
+
|
|
60
|
+
const fullPath = path.join(dir, entry.name);
|
|
61
|
+
if (entry.isDirectory()) {
|
|
62
|
+
collectFiles(fullPath, brainDir, files);
|
|
63
|
+
} else {
|
|
64
|
+
// Normalize path separators to forward slash for portability
|
|
65
|
+
const key = rel.split(path.sep).join('/');
|
|
66
|
+
files[key] = fs.readFileSync(fullPath, 'utf8');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Import memories from a .brain-export file into ~/.brain/.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} inputPath - Path to the export file
|
|
75
|
+
* @param {string} brainDir - Absolute path to ~/.brain/
|
|
76
|
+
* @param {string|null} [passphrase] - Decryption passphrase (if the export is encrypted)
|
|
77
|
+
* @param {Object} [options]
|
|
78
|
+
* @param {string} [options.mode='overwrite'] - 'overwrite' replaces all files; 'merge' only imports newer files
|
|
79
|
+
* @returns {{ fileCount: number, skipped: number }}
|
|
80
|
+
*/
|
|
81
|
+
function importBrain(inputPath, brainDir, passphrase, options = {}) {
|
|
82
|
+
const mode = options.mode || 'overwrite';
|
|
83
|
+
|
|
84
|
+
if (!fs.existsSync(inputPath)) {
|
|
85
|
+
throw new Error(`Export file not found: ${inputPath}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const raw = fs.readFileSync(inputPath);
|
|
89
|
+
let payload;
|
|
90
|
+
|
|
91
|
+
if (passphrase) {
|
|
92
|
+
try {
|
|
93
|
+
payload = JSON.parse(decrypt(raw, passphrase));
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (err.message.includes('Unsupported state') || err.message.includes('unable to authenticate') || err.message.includes('Invalid encrypted data')) {
|
|
96
|
+
throw new Error('Decryption failed — wrong passphrase or corrupted file.');
|
|
97
|
+
}
|
|
98
|
+
throw err;
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
try {
|
|
102
|
+
payload = JSON.parse(raw.toString('utf8'));
|
|
103
|
+
} catch {
|
|
104
|
+
throw new Error('Invalid export file. If the file is encrypted, provide a passphrase.');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!payload || !payload.files || typeof payload.files !== 'object') {
|
|
109
|
+
throw new Error('Invalid export format: missing files object.');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fs.mkdirSync(brainDir, { recursive: true });
|
|
113
|
+
|
|
114
|
+
let imported = 0;
|
|
115
|
+
let skipped = 0;
|
|
116
|
+
|
|
117
|
+
for (const [relPath, content] of Object.entries(payload.files)) {
|
|
118
|
+
// Convert forward-slash paths to OS-native
|
|
119
|
+
const nativePath = relPath.split('/').join(path.sep);
|
|
120
|
+
const destPath = path.join(brainDir, nativePath);
|
|
121
|
+
|
|
122
|
+
if (mode === 'merge' && fs.existsSync(destPath)) {
|
|
123
|
+
// In merge mode, only import if the export's file is newer
|
|
124
|
+
// We compare exportedAt vs local file mtime
|
|
125
|
+
const localMtime = fs.statSync(destPath).mtime;
|
|
126
|
+
const exportDate = new Date(payload.exportedAt);
|
|
127
|
+
if (exportDate <= localMtime) {
|
|
128
|
+
skipped++;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fs.mkdirSync(path.dirname(destPath), { recursive: true });
|
|
134
|
+
fs.writeFileSync(destPath, content, 'utf8');
|
|
135
|
+
imported++;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { fileCount: imported, skipped };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Preview what an import would do without writing any files.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} inputPath - Path to the export file
|
|
145
|
+
* @param {string} brainDir - Absolute path to ~/.brain/
|
|
146
|
+
* @param {string|null} [passphrase] - Decryption passphrase
|
|
147
|
+
* @returns {{ totalFiles: number, newFiles: string[], existingFiles: string[], exportedAt: string }}
|
|
148
|
+
*/
|
|
149
|
+
function previewImport(inputPath, brainDir, passphrase) {
|
|
150
|
+
const raw = fs.readFileSync(inputPath);
|
|
151
|
+
let payload;
|
|
152
|
+
|
|
153
|
+
if (passphrase) {
|
|
154
|
+
payload = JSON.parse(decrypt(raw, passphrase));
|
|
155
|
+
} else {
|
|
156
|
+
payload = JSON.parse(raw.toString('utf8'));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const files = Object.keys(payload.files);
|
|
160
|
+
const newFiles = [];
|
|
161
|
+
const existingFiles = [];
|
|
162
|
+
|
|
163
|
+
for (const relPath of files) {
|
|
164
|
+
const nativePath = relPath.split('/').join(path.sep);
|
|
165
|
+
const destPath = path.join(brainDir, nativePath);
|
|
166
|
+
if (fs.existsSync(destPath)) {
|
|
167
|
+
existingFiles.push(relPath);
|
|
168
|
+
} else {
|
|
169
|
+
newFiles.push(relPath);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
totalFiles: files.length,
|
|
175
|
+
newFiles,
|
|
176
|
+
existingFiles,
|
|
177
|
+
exportedAt: payload.exportedAt,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports = {
|
|
182
|
+
exportBrain,
|
|
183
|
+
importBrain,
|
|
184
|
+
previewImport,
|
|
185
|
+
};
|