mindlore 0.6.9 → 0.7.1
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 +31 -4
- package/dist/scripts/bundle-hooks.d.ts +2 -0
- package/dist/scripts/bundle-hooks.d.ts.map +1 -0
- package/dist/scripts/bundle-hooks.js +68 -0
- package/dist/scripts/bundle-hooks.js.map +1 -0
- package/dist/scripts/cc-memory-bulk-sync.d.ts.map +1 -1
- package/dist/scripts/cc-memory-bulk-sync.js +2 -1
- package/dist/scripts/cc-memory-bulk-sync.js.map +1 -1
- package/dist/scripts/cc-session-sync.d.ts.map +1 -1
- package/dist/scripts/cc-session-sync.js +3 -2
- package/dist/scripts/cc-session-sync.js.map +1 -1
- package/dist/scripts/init.js +4 -4
- package/dist/scripts/init.js.map +1 -1
- package/dist/scripts/lib/constants.d.ts +1 -2
- package/dist/scripts/lib/constants.d.ts.map +1 -1
- package/dist/scripts/lib/constants.js +2 -22
- package/dist/scripts/lib/constants.js.map +1 -1
- package/dist/scripts/lib/err-msg.d.ts +2 -0
- package/dist/scripts/lib/err-msg.d.ts.map +1 -0
- package/dist/scripts/lib/err-msg.js +7 -0
- package/dist/scripts/lib/err-msg.js.map +1 -0
- package/dist/scripts/lib/mcp-namespace.d.ts +2 -0
- package/dist/scripts/lib/mcp-namespace.d.ts.map +1 -0
- package/dist/scripts/lib/mcp-namespace.js +21 -0
- package/dist/scripts/lib/mcp-namespace.js.map +1 -0
- package/dist/scripts/lib/mcp-telemetry.d.ts +11 -0
- package/dist/scripts/lib/mcp-telemetry.d.ts.map +1 -0
- package/dist/scripts/lib/mcp-telemetry.js +37 -0
- package/dist/scripts/lib/mcp-telemetry.js.map +1 -0
- package/dist/scripts/lib/mcp-tools.d.ts +10 -0
- package/dist/scripts/lib/mcp-tools.d.ts.map +1 -0
- package/dist/scripts/lib/mcp-tools.js +121 -0
- package/dist/scripts/lib/mcp-tools.js.map +1 -0
- package/dist/scripts/lib/rrf.d.ts.map +1 -1
- package/dist/scripts/lib/rrf.js +2 -1
- package/dist/scripts/lib/rrf.js.map +1 -1
- package/dist/scripts/lib/search-engine.d.ts +1 -0
- package/dist/scripts/lib/search-engine.d.ts.map +1 -1
- package/dist/scripts/lib/search-engine.js +9 -5
- package/dist/scripts/lib/search-engine.js.map +1 -1
- package/dist/scripts/lib/slugify.d.ts +2 -0
- package/dist/scripts/lib/slugify.d.ts.map +1 -0
- package/dist/scripts/lib/slugify.js +13 -0
- package/dist/scripts/lib/slugify.js.map +1 -0
- package/dist/scripts/lib/smart-snippet.d.ts +9 -0
- package/dist/scripts/lib/smart-snippet.d.ts.map +1 -0
- package/dist/scripts/lib/smart-snippet.js +47 -0
- package/dist/scripts/lib/smart-snippet.js.map +1 -0
- package/dist/scripts/lib/tool-adapters/brief-adapter.d.ts +15 -0
- package/dist/scripts/lib/tool-adapters/brief-adapter.d.ts.map +1 -0
- package/dist/scripts/lib/tool-adapters/brief-adapter.js +66 -0
- package/dist/scripts/lib/tool-adapters/brief-adapter.js.map +1 -0
- package/dist/scripts/lib/tool-adapters/decide-adapter.d.ts +31 -0
- package/dist/scripts/lib/tool-adapters/decide-adapter.d.ts.map +1 -0
- package/dist/scripts/lib/tool-adapters/decide-adapter.js +71 -0
- package/dist/scripts/lib/tool-adapters/decide-adapter.js.map +1 -0
- package/dist/scripts/lib/tool-adapters/ingest-adapter.d.ts +16 -0
- package/dist/scripts/lib/tool-adapters/ingest-adapter.d.ts.map +1 -0
- package/dist/scripts/lib/tool-adapters/ingest-adapter.js +58 -0
- package/dist/scripts/lib/tool-adapters/ingest-adapter.js.map +1 -0
- package/dist/scripts/lib/tool-adapters/recall-adapter.d.ts +20 -0
- package/dist/scripts/lib/tool-adapters/recall-adapter.d.ts.map +1 -0
- package/dist/scripts/lib/tool-adapters/recall-adapter.js +69 -0
- package/dist/scripts/lib/tool-adapters/recall-adapter.js.map +1 -0
- package/dist/scripts/lib/tool-adapters/search-adapter.d.ts +22 -0
- package/dist/scripts/lib/tool-adapters/search-adapter.d.ts.map +1 -0
- package/dist/scripts/lib/tool-adapters/search-adapter.js +32 -0
- package/dist/scripts/lib/tool-adapters/search-adapter.js.map +1 -0
- package/dist/scripts/lib/tool-adapters/stats-adapter.d.ts +15 -0
- package/dist/scripts/lib/tool-adapters/stats-adapter.d.ts.map +1 -0
- package/dist/scripts/lib/tool-adapters/stats-adapter.js +66 -0
- package/dist/scripts/lib/tool-adapters/stats-adapter.js.map +1 -0
- package/dist/scripts/maintain-cleanup.d.ts.map +1 -1
- package/dist/scripts/maintain-cleanup.js +3 -2
- package/dist/scripts/maintain-cleanup.js.map +1 -1
- package/dist/scripts/mcp-server.d.ts +3 -0
- package/dist/scripts/mcp-server.d.ts.map +1 -0
- package/dist/scripts/mcp-server.js +85 -0
- package/dist/scripts/mcp-server.js.map +1 -0
- package/dist/scripts/mindlore-doctor.d.ts.map +1 -1
- package/dist/scripts/mindlore-doctor.js +4 -6
- package/dist/scripts/mindlore-doctor.js.map +1 -1
- package/dist/scripts/mindlore-fts5-index.js +2 -2
- package/dist/scripts/mindlore-fts5-index.js.map +1 -1
- package/dist/scripts/mindlore-health-check.d.ts.map +1 -1
- package/dist/scripts/mindlore-health-check.js +2 -2
- package/dist/scripts/mindlore-health-check.js.map +1 -1
- package/dist/scripts/validate-manifest-cli.js +2 -2
- package/dist/scripts/validate-manifest-cli.js.map +1 -1
- package/dist/tests/err-msg.test.d.ts +2 -0
- package/dist/tests/err-msg.test.d.ts.map +1 -0
- package/dist/tests/err-msg.test.js +24 -0
- package/dist/tests/err-msg.test.js.map +1 -0
- package/dist/tests/hook-smoke.test.js +1 -1
- package/dist/tests/hook-smoke.test.js.map +1 -1
- package/dist/tests/manifest-v2.test.js +0 -7
- package/dist/tests/manifest-v2.test.js.map +1 -1
- package/dist/tests/mcp-server.test.d.ts +2 -0
- package/dist/tests/mcp-server.test.d.ts.map +1 -0
- package/dist/tests/mcp-server.test.js +118 -0
- package/dist/tests/mcp-server.test.js.map +1 -0
- package/dist/tests/mcp-tools.test.d.ts +2 -0
- package/dist/tests/mcp-tools.test.d.ts.map +1 -0
- package/dist/tests/mcp-tools.test.js +243 -0
- package/dist/tests/mcp-tools.test.js.map +1 -0
- package/dist/tests/search-hook.test.js +1 -1
- package/dist/tests/search-hook.test.js.map +1 -1
- package/dist/tests/smart-snippet.test.d.ts +2 -0
- package/dist/tests/smart-snippet.test.d.ts.map +1 -0
- package/dist/tests/smart-snippet.test.js +67 -0
- package/dist/tests/smart-snippet.test.js.map +1 -0
- package/hooks/cc-memory-bulk-sync.cjs +592 -0
- package/hooks/cc-session-sync.cjs +842 -0
- package/hooks/hooks.json +149 -0
- package/hooks/lib/mindlore-common.cjs +2 -2
- package/hooks/lib/secure-io.cjs +17 -0
- package/hooks/mindlore-cwd-changed.cjs +19 -34
- package/hooks/mindlore-decision-detector.cjs +40 -31
- package/hooks/mindlore-dont-repeat.cjs +57 -115
- package/hooks/mindlore-fts5-sync.cjs +15 -44
- package/hooks/mindlore-index.cjs +100 -101
- package/hooks/mindlore-model-router.cjs +20 -32
- package/hooks/mindlore-post-compact.cjs +26 -42
- package/hooks/mindlore-post-read.cjs +35 -60
- package/hooks/mindlore-pre-compact.cjs +55 -73
- package/hooks/mindlore-read-guard.cjs +28 -51
- package/hooks/mindlore-research-guard.cjs +63 -101
- package/hooks/mindlore-search.cjs +1142 -93
- package/hooks/mindlore-session-end.cjs +155 -276
- package/hooks/mindlore-session-focus.cjs +639 -110
- package/hooks/src/lib/constants.cjs +15 -0
- package/hooks/src/lib/mindlore-common.cjs +975 -0
- package/hooks/src/lib/mindlore-common.d.cts +72 -0
- package/hooks/src/lib/secure-io.cjs +17 -0
- package/hooks/src/lib/types.d.ts +58 -0
- package/hooks/src/mindlore-cwd-changed.cjs +57 -0
- package/hooks/src/mindlore-decision-detector.cjs +54 -0
- package/hooks/src/mindlore-dont-repeat.cjs +222 -0
- package/hooks/src/mindlore-fts5-sync.cjs +98 -0
- package/hooks/src/mindlore-index.cjs +230 -0
- package/hooks/src/mindlore-model-router.cjs +54 -0
- package/hooks/src/mindlore-post-compact.cjs +69 -0
- package/hooks/src/mindlore-post-read.cjs +106 -0
- package/hooks/src/mindlore-pre-compact.cjs +154 -0
- package/hooks/src/mindlore-read-guard.cjs +105 -0
- package/hooks/src/mindlore-research-guard.cjs +176 -0
- package/hooks/src/mindlore-search.cjs +200 -0
- package/hooks/src/mindlore-session-end.cjs +511 -0
- package/hooks/src/mindlore-session-focus.cjs +256 -0
- package/package.json +8 -3
- package/plugin.json +7 -1
- package/templates/config.json +1 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-index — FileChanged hook
|
|
6
|
+
*
|
|
7
|
+
* When a .md file in .mindlore/ changes, update its FTS5 entry.
|
|
8
|
+
* Reads changed file path from stdin (CC FileChanged event).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const { safeMkdir, safeWriteFile } = require('../dist/scripts/lib/secure-io.js');
|
|
14
|
+
const { DB_NAME, SKIP_FILES, sha256, openDatabase, parseFrontmatter, extractFtsMetadata, insertFtsRow, readHookStdin, getProjectName, resolveProject, globalDir, hookLog, withTelemetry, isInsideMindloreDir, extractMindloreBaseDir } = require('./lib/mindlore-common.cjs');
|
|
15
|
+
|
|
16
|
+
function invalidateSearchCache(db) {
|
|
17
|
+
try { db.exec('DELETE FROM search_cache'); } catch (_) { /* table may not exist */ }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function main() {
|
|
21
|
+
const filePath = readHookStdin(['path', 'file_path']);
|
|
22
|
+
if (!filePath) return;
|
|
23
|
+
|
|
24
|
+
// Only process .md files inside .mindlore/ (resolved path check prevents traversal)
|
|
25
|
+
if (!filePath.endsWith('.md')) return;
|
|
26
|
+
const resolvedFile = path.resolve(filePath);
|
|
27
|
+
if (!isInsideMindloreDir(resolvedFile)) {
|
|
28
|
+
// CC memory path (~/.claude/projects/*/memory/*.md) — index to global mindlore DB
|
|
29
|
+
const isCcMemory = resolvedFile.includes(path.sep + '.claude' + path.sep + 'projects' + path.sep)
|
|
30
|
+
&& resolvedFile.includes(path.sep + 'memory' + path.sep)
|
|
31
|
+
&& resolvedFile.endsWith('.md');
|
|
32
|
+
if (!isCcMemory) return;
|
|
33
|
+
|
|
34
|
+
// CC memory path — index to global mindlore DB
|
|
35
|
+
indexCcMemory(resolvedFile);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const fileName = path.basename(filePath);
|
|
40
|
+
|
|
41
|
+
const baseDir = extractMindloreBaseDir(resolvedFile);
|
|
42
|
+
if (!baseDir) return;
|
|
43
|
+
const dbPath = path.join(baseDir, DB_NAME);
|
|
44
|
+
|
|
45
|
+
if (!fs.existsSync(dbPath)) return;
|
|
46
|
+
|
|
47
|
+
// Catch-up scan: when INDEX.md or log.md triggers, index recently-modified files
|
|
48
|
+
if (['INDEX.md', 'log.md'].includes(fileName)) {
|
|
49
|
+
catchUpScan(baseDir, dbPath);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (SKIP_FILES.has(fileName)) return;
|
|
54
|
+
|
|
55
|
+
if (!fs.existsSync(filePath)) {
|
|
56
|
+
// File was deleted — remove from index
|
|
57
|
+
const db = openDatabase(dbPath);
|
|
58
|
+
if (!db) return;
|
|
59
|
+
try {
|
|
60
|
+
db.prepare('DELETE FROM mindlore_fts WHERE path = ?').run(filePath);
|
|
61
|
+
db.prepare('DELETE FROM file_hashes WHERE path = ?').run(filePath);
|
|
62
|
+
invalidateSearchCache(db);
|
|
63
|
+
} finally {
|
|
64
|
+
db.close();
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const content = fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
|
|
70
|
+
const hash = sha256(content);
|
|
71
|
+
|
|
72
|
+
const db = openDatabase(dbPath);
|
|
73
|
+
if (!db) return;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
// Check if content changed
|
|
77
|
+
const existing = db
|
|
78
|
+
.prepare('SELECT content_hash FROM file_hashes WHERE path = ?')
|
|
79
|
+
.get(filePath);
|
|
80
|
+
|
|
81
|
+
if (existing && existing.content_hash === hash) return; // Unchanged
|
|
82
|
+
|
|
83
|
+
// Parse frontmatter for rich FTS5 columns
|
|
84
|
+
const { meta, body } = parseFrontmatter(content);
|
|
85
|
+
const { slug, description, type, category, title, tags, quality, dateCaptured, project: ftsProject } = extractFtsMetadata(meta, body, filePath, baseDir);
|
|
86
|
+
|
|
87
|
+
// Update FTS5 + hash atomically
|
|
88
|
+
const updateIndex = db.transaction(() => {
|
|
89
|
+
db.prepare('DELETE FROM mindlore_fts WHERE path = ?').run(filePath);
|
|
90
|
+
insertFtsRow(db, { path: filePath, slug, description, type, category, title, content: body, tags, quality, dateCaptured, project: resolveProject(ftsProject, filePath, getProjectName()) });
|
|
91
|
+
db.prepare(
|
|
92
|
+
`INSERT INTO file_hashes (path, content_hash, last_indexed)
|
|
93
|
+
VALUES (?, ?, ?)
|
|
94
|
+
ON CONFLICT(path) DO UPDATE SET
|
|
95
|
+
content_hash = excluded.content_hash,
|
|
96
|
+
last_indexed = excluded.last_indexed`
|
|
97
|
+
).run(filePath, hash, new Date().toISOString());
|
|
98
|
+
});
|
|
99
|
+
updateIndex();
|
|
100
|
+
invalidateSearchCache(db);
|
|
101
|
+
} finally {
|
|
102
|
+
db.close();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function indexCcMemory(filePath) {
|
|
107
|
+
const CC_MEMORY_CATEGORY = 'cc-memory';
|
|
108
|
+
// CC memory constants live in TS (scripts/lib/constants.ts) — CJS hooks can't require TS directly
|
|
109
|
+
const globalBase = globalDir();
|
|
110
|
+
const dbPath = path.join(globalBase, DB_NAME);
|
|
111
|
+
|
|
112
|
+
const content = fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
|
|
113
|
+
if (!content.trim()) return;
|
|
114
|
+
|
|
115
|
+
// Privacy filter — redact secrets before DB write
|
|
116
|
+
let cleaned = content;
|
|
117
|
+
try {
|
|
118
|
+
const { redactSecrets } = require('../dist/scripts/lib/privacy-filter.js');
|
|
119
|
+
cleaned = redactSecrets(content);
|
|
120
|
+
} catch (_err) {
|
|
121
|
+
// privacy-filter not built — use raw content
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// SHA256 dedup
|
|
125
|
+
const hash = sha256(cleaned);
|
|
126
|
+
const db = openDatabase(dbPath);
|
|
127
|
+
if (!db) return;
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const existing = db.prepare('SELECT content_hash FROM file_hashes WHERE path = ?').get(filePath);
|
|
131
|
+
if (existing && existing.content_hash === hash) {
|
|
132
|
+
return; // unchanged — finally handles db.close()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const { meta, body } = parseFrontmatter(cleaned);
|
|
136
|
+
const memType = String(meta.type || 'unknown');
|
|
137
|
+
|
|
138
|
+
// Extract project scope from path: ~/.claude/projects/C--Users-X-proj/memory/
|
|
139
|
+
const projMatch = filePath.match(/projects[/\\]([^/\\]+)[/\\]memory/);
|
|
140
|
+
const projectScope = projMatch ? projMatch[1] : null;
|
|
141
|
+
|
|
142
|
+
const ftsData = extractFtsMetadata(meta, body, filePath, globalBase);
|
|
143
|
+
|
|
144
|
+
// Update FTS5 + hash atomically
|
|
145
|
+
const updateIndex = db.transaction(() => {
|
|
146
|
+
db.prepare('DELETE FROM mindlore_fts WHERE path = ?').run(filePath);
|
|
147
|
+
insertFtsRow(db, {
|
|
148
|
+
path: filePath,
|
|
149
|
+
...ftsData,
|
|
150
|
+
category: CC_MEMORY_CATEGORY,
|
|
151
|
+
type: memType,
|
|
152
|
+
project: projectScope,
|
|
153
|
+
});
|
|
154
|
+
db.prepare(
|
|
155
|
+
`INSERT INTO file_hashes (path, content_hash, last_indexed, source_type, project_scope)
|
|
156
|
+
VALUES (?, ?, ?, ?, ?)
|
|
157
|
+
ON CONFLICT(path) DO UPDATE SET
|
|
158
|
+
content_hash = excluded.content_hash,
|
|
159
|
+
last_indexed = excluded.last_indexed,
|
|
160
|
+
source_type = excluded.source_type,
|
|
161
|
+
project_scope = excluded.project_scope`
|
|
162
|
+
).run(filePath, hash, new Date().toISOString(), CC_MEMORY_CATEGORY, projectScope);
|
|
163
|
+
});
|
|
164
|
+
updateIndex();
|
|
165
|
+
|
|
166
|
+
// Copy to ~/.mindlore/memory/{project}/ for git-sync + obsidian
|
|
167
|
+
const memoryDir = path.join(globalBase, 'memory', projectScope || '_global');
|
|
168
|
+
safeMkdir(memoryDir);
|
|
169
|
+
const destPath = path.join(memoryDir, path.basename(filePath));
|
|
170
|
+
safeWriteFile(destPath, cleaned);
|
|
171
|
+
} finally {
|
|
172
|
+
db.close();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function catchUpScan(baseDir, dbPath) {
|
|
177
|
+
const CATCH_UP_DIRS = ['raw', 'sources', 'analyses', 'diary'];
|
|
178
|
+
const fiveMinAgo = Date.now() - 5 * 60 * 1000;
|
|
179
|
+
|
|
180
|
+
const db = openDatabase(dbPath);
|
|
181
|
+
if (!db) return;
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
let indexed = 0;
|
|
185
|
+
for (const dir of CATCH_UP_DIRS) {
|
|
186
|
+
const dirPath = path.join(baseDir, dir);
|
|
187
|
+
if (!fs.existsSync(dirPath)) continue;
|
|
188
|
+
|
|
189
|
+
const files = fs.readdirSync(dirPath).filter(f => f.endsWith('.md'));
|
|
190
|
+
for (const file of files) {
|
|
191
|
+
const filePath = path.join(dirPath, file);
|
|
192
|
+
const stat = fs.statSync(filePath);
|
|
193
|
+
if (stat.mtimeMs < fiveMinAgo) continue;
|
|
194
|
+
|
|
195
|
+
const content = fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
|
|
196
|
+
const hash = sha256(content);
|
|
197
|
+
|
|
198
|
+
const existing = db.prepare('SELECT content_hash FROM file_hashes WHERE path = ?').get(filePath);
|
|
199
|
+
if (existing && existing.content_hash === hash) continue;
|
|
200
|
+
|
|
201
|
+
const { meta, body } = parseFrontmatter(content);
|
|
202
|
+
const ftsData = extractFtsMetadata(meta, body, filePath, baseDir);
|
|
203
|
+
|
|
204
|
+
const update = db.transaction(() => {
|
|
205
|
+
db.prepare('DELETE FROM mindlore_fts WHERE path = ?').run(filePath);
|
|
206
|
+
insertFtsRow(db, { path: filePath, ...ftsData, project: resolveProject(ftsData.project, filePath, getProjectName()) });
|
|
207
|
+
db.prepare(
|
|
208
|
+
`INSERT INTO file_hashes (path, content_hash, last_indexed)
|
|
209
|
+
VALUES (?, ?, ?)
|
|
210
|
+
ON CONFLICT(path) DO UPDATE SET
|
|
211
|
+
content_hash = excluded.content_hash,
|
|
212
|
+
last_indexed = excluded.last_indexed`
|
|
213
|
+
).run(filePath, hash, new Date().toISOString());
|
|
214
|
+
});
|
|
215
|
+
update();
|
|
216
|
+
indexed++;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (indexed > 0) {
|
|
220
|
+
hookLog(`catch-up: ${indexed} file(s) indexed`);
|
|
221
|
+
}
|
|
222
|
+
} finally {
|
|
223
|
+
db.close();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
withTelemetry('mindlore-index', main).catch(err => {
|
|
228
|
+
hookLog('mindlore-index', 'error', err?.message ?? String(err));
|
|
229
|
+
process.exit(0);
|
|
230
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* mindlore-model-router — PreToolUse (Agent) hook
|
|
5
|
+
* Overrides model for [mindlore:SKILL] marked Agent spawns.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const { findMindloreDir, readConfig, DEFAULT_MODELS, hookLog, withTelemetrySync } = require('./lib/mindlore-common.cjs');
|
|
10
|
+
|
|
11
|
+
const SKILL_KEYS = Object.keys(DEFAULT_MODELS).filter((k) => k !== 'default');
|
|
12
|
+
const MARKER_REGEX = new RegExp(`\\[mindlore:(${SKILL_KEYS.join('|')})\\]`);
|
|
13
|
+
|
|
14
|
+
function main() {
|
|
15
|
+
const mindloreDir = findMindloreDir();
|
|
16
|
+
if (!mindloreDir) return;
|
|
17
|
+
|
|
18
|
+
let input;
|
|
19
|
+
try {
|
|
20
|
+
const raw = fs.readFileSync(0, 'utf8').trim();
|
|
21
|
+
if (!raw) return;
|
|
22
|
+
input = JSON.parse(raw);
|
|
23
|
+
} catch (_err) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const toolName = input.tool_name || '';
|
|
28
|
+
if (toolName !== 'Agent') return;
|
|
29
|
+
|
|
30
|
+
const toolInput = input.tool_input || {};
|
|
31
|
+
const prompt = toolInput.prompt || '';
|
|
32
|
+
const match = prompt.match(MARKER_REGEX);
|
|
33
|
+
if (!match) return;
|
|
34
|
+
|
|
35
|
+
const skill = match[1];
|
|
36
|
+
|
|
37
|
+
// Resolve model: config.json → config default → hardcoded
|
|
38
|
+
const config = readConfig(mindloreDir);
|
|
39
|
+
const models = (config && config.models) || {};
|
|
40
|
+
const model = models[skill] || models.default || DEFAULT_MODELS[skill] || DEFAULT_MODELS.default;
|
|
41
|
+
|
|
42
|
+
const updatedInput = { ...toolInput, model: model };
|
|
43
|
+
|
|
44
|
+
const output = {
|
|
45
|
+
hookSpecificOutput: {
|
|
46
|
+
hookEventName: 'PreToolUse',
|
|
47
|
+
updatedInput: updatedInput,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
process.stdout.write(JSON.stringify(output));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try { withTelemetrySync('mindlore-model-router', main); } catch (err) { hookLog('model-router', 'error', err?.message ?? String(err)); }
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-post-compact — PostCompact hook
|
|
6
|
+
*
|
|
7
|
+
* After context compaction, re-inject session context:
|
|
8
|
+
* 1. Read INDEX.md
|
|
9
|
+
* 2. Read latest delta
|
|
10
|
+
* 3. Inject via stdout (same as session-focus)
|
|
11
|
+
*
|
|
12
|
+
* This ensures the agent has knowledge context after compaction.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
const { findMindloreDir, getLatestDelta, readConfig, hookLog, withTelemetry } = require('./lib/mindlore-common.cjs');
|
|
18
|
+
|
|
19
|
+
function main() {
|
|
20
|
+
const baseDir = findMindloreDir();
|
|
21
|
+
if (!baseDir) return;
|
|
22
|
+
|
|
23
|
+
const output = [];
|
|
24
|
+
|
|
25
|
+
// Re-inject INDEX.md
|
|
26
|
+
const indexPath = path.join(baseDir, 'INDEX.md');
|
|
27
|
+
if (fs.existsSync(indexPath)) {
|
|
28
|
+
const content = fs.readFileSync(indexPath, 'utf8').trim();
|
|
29
|
+
output.push(`[Mindlore INDEX (post-compact)]\n${content}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const diaryDir = path.join(baseDir, 'diary');
|
|
33
|
+
|
|
34
|
+
const latestDelta = getLatestDelta(diaryDir);
|
|
35
|
+
if (latestDelta) {
|
|
36
|
+
const deltaContent = fs.readFileSync(latestDelta, 'utf8').trim();
|
|
37
|
+
const deltaName = path.basename(latestDelta);
|
|
38
|
+
output.push(`[Mindlore Delta (post-compact): ${deltaName}]\n${deltaContent}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const snapshots = fs.readdirSync(diaryDir)
|
|
43
|
+
.filter(f => f.startsWith('compaction-snapshot-'))
|
|
44
|
+
.sort();
|
|
45
|
+
if (snapshots.length > 0) {
|
|
46
|
+
const latestSnapshot = snapshots[snapshots.length - 1];
|
|
47
|
+
const snapshotContent = fs.readFileSync(
|
|
48
|
+
path.join(diaryDir, latestSnapshot), 'utf8'
|
|
49
|
+
).trim();
|
|
50
|
+
output.push(`[Mindlore Compaction Resume]\n${snapshotContent}`);
|
|
51
|
+
}
|
|
52
|
+
} catch (_err) { /* snapshot best-effort */ }
|
|
53
|
+
|
|
54
|
+
if (output.length > 0) {
|
|
55
|
+
const config = readConfig(baseDir);
|
|
56
|
+
const budgetConfig = config?.tokenBudget ?? {};
|
|
57
|
+
const maxInjectChars = (budgetConfig.sessionInject || 2000) * 4;
|
|
58
|
+
let joined = output.join('\n\n');
|
|
59
|
+
if (joined.length > maxInjectChars) {
|
|
60
|
+
joined = joined.slice(0, maxInjectChars) + '\n[...truncated by token budget]';
|
|
61
|
+
}
|
|
62
|
+
process.stdout.write(joined + '\n');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
withTelemetry('mindlore-post-compact', main).catch(err => {
|
|
67
|
+
hookLog('mindlore-post-compact', 'error', err?.message ?? String(err));
|
|
68
|
+
process.exit(0);
|
|
69
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-post-read — PostToolUse hook (matcher: "Read")
|
|
6
|
+
*
|
|
7
|
+
* After a file is read, estimate its token count
|
|
8
|
+
* and store in _session-reads.json for the read-guard to reference.
|
|
9
|
+
*
|
|
10
|
+
* Outputs token estimate via additionalContext JSON.
|
|
11
|
+
* Also stores token info in _session-reads.json for read-guard.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const { findMindloreDir, getProjectName, hookLog, withTelemetry } = require('./lib/mindlore-common.cjs');
|
|
17
|
+
|
|
18
|
+
const CODE_EXTS = new Set(['.ts', '.tsx', '.js', '.jsx', '.py', '.rs', '.go', '.java', '.c', '.cpp', '.h', '.css', '.scss', '.sql', '.sh', '.yaml', '.yml', '.json', '.toml', '.xml', '.cjs', '.mjs']);
|
|
19
|
+
const PROSE_EXTS = new Set(['.md', '.txt', '.rst', '.adoc']);
|
|
20
|
+
|
|
21
|
+
function estimateTokens(charCount, ext) {
|
|
22
|
+
const ratio = CODE_EXTS.has(ext) ? 3.5 : PROSE_EXTS.has(ext) ? 4.0 : 3.75;
|
|
23
|
+
return Math.ceil(charCount / ratio);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function main() {
|
|
27
|
+
const baseDir = findMindloreDir();
|
|
28
|
+
if (!baseDir) return;
|
|
29
|
+
|
|
30
|
+
let input = '';
|
|
31
|
+
const stdinTimeout = setTimeout(() => process.exit(0), 3000);
|
|
32
|
+
process.stdin.setEncoding('utf8');
|
|
33
|
+
process.stdin.on('error', () => process.exit(0));
|
|
34
|
+
process.stdin.on('data', chunk => input += chunk);
|
|
35
|
+
process.stdin.on('end', () => {
|
|
36
|
+
clearTimeout(stdinTimeout);
|
|
37
|
+
try {
|
|
38
|
+
const data = JSON.parse(input || '{}');
|
|
39
|
+
const toolInput = data.tool_input || {};
|
|
40
|
+
const toolOutput = data.tool_output || {};
|
|
41
|
+
|
|
42
|
+
const filePath = toolInput.file_path || toolInput.path || '';
|
|
43
|
+
if (!filePath) return process.exit(0);
|
|
44
|
+
|
|
45
|
+
// Skip .mindlore/ internals
|
|
46
|
+
const resolved = path.resolve(filePath);
|
|
47
|
+
if (resolved.startsWith(path.resolve(baseDir))) return process.exit(0);
|
|
48
|
+
|
|
49
|
+
// Get content length from tool output or read file
|
|
50
|
+
let charCount = 0;
|
|
51
|
+
if (toolOutput.content) {
|
|
52
|
+
charCount = typeof toolOutput.content === 'string'
|
|
53
|
+
? toolOutput.content.length
|
|
54
|
+
: JSON.stringify(toolOutput.content).length;
|
|
55
|
+
} else {
|
|
56
|
+
// Fallback: read file size
|
|
57
|
+
try {
|
|
58
|
+
const stat = fs.statSync(resolved);
|
|
59
|
+
charCount = stat.size;
|
|
60
|
+
} catch { return process.exit(0); }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (charCount === 0) return process.exit(0);
|
|
64
|
+
|
|
65
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
66
|
+
const tokens = estimateTokens(charCount, ext);
|
|
67
|
+
|
|
68
|
+
// Update _session-reads.json with token info
|
|
69
|
+
const diaryDir = path.join(baseDir, 'diary');
|
|
70
|
+
const readsPath = path.join(diaryDir, `_session-reads-${getProjectName()}.json`);
|
|
71
|
+
let reads = {};
|
|
72
|
+
if (fs.existsSync(readsPath)) {
|
|
73
|
+
try { reads = JSON.parse(fs.readFileSync(readsPath, 'utf8')); } catch { reads = {}; }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const normalizedPath = path.resolve(filePath);
|
|
77
|
+
const key = normalizedPath;
|
|
78
|
+
|
|
79
|
+
if (typeof reads[key] === 'number') {
|
|
80
|
+
// Upgrade from old format (just count) to new format (object)
|
|
81
|
+
reads[key] = { count: reads[key], tokens, chars: charCount };
|
|
82
|
+
} else if (reads[key] && typeof reads[key] === 'object') {
|
|
83
|
+
reads[key].tokens = tokens;
|
|
84
|
+
reads[key].chars = charCount;
|
|
85
|
+
} else {
|
|
86
|
+
reads[key] = { count: 1, tokens, chars: charCount };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fs.writeFileSync(readsPath, JSON.stringify(reads, null, 2), 'utf8');
|
|
90
|
+
|
|
91
|
+
// Output token estimate to Claude via additionalContext
|
|
92
|
+
const basename = path.basename(filePath);
|
|
93
|
+
process.stdout.write(JSON.stringify({
|
|
94
|
+
hookSpecificOutput: {
|
|
95
|
+
hookEventName: 'PostToolUse',
|
|
96
|
+
additionalContext: `[Mindlore: ${basename} — ~${tokens} token (${charCount} char). Edit etmeyeceksen ctx_execute_file kullan.]`
|
|
97
|
+
}
|
|
98
|
+
}));
|
|
99
|
+
} catch {
|
|
100
|
+
// Silent fail
|
|
101
|
+
}
|
|
102
|
+
process.exit(0);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
withTelemetry('mindlore-post-read', main).catch(err => { hookLog('post-read', 'error', err?.message ?? String(err)); });
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* mindlore-pre-compact — PreCompact hook
|
|
6
|
+
*
|
|
7
|
+
* Before context compaction:
|
|
8
|
+
* 1. Ensure FTS5 index is up to date
|
|
9
|
+
* 2. Write pre-compact episode to episodes/ and append log entry
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const { findMindloreDir, openDatabase, hookLog, withTelemetry, listSnapshots } = require('./lib/mindlore-common.cjs');
|
|
15
|
+
|
|
16
|
+
function collectRecentEpisodes(baseDir) {
|
|
17
|
+
try {
|
|
18
|
+
const dbPath = path.join(baseDir, 'mindlore.db');
|
|
19
|
+
const db = openDatabase(dbPath, { readonly: true });
|
|
20
|
+
if (!db) return [];
|
|
21
|
+
try {
|
|
22
|
+
const episodes = db.prepare(
|
|
23
|
+
"SELECT kind, summary FROM episodes WHERE created_at > datetime('now', '-4 hours') ORDER BY created_at DESC LIMIT 20"
|
|
24
|
+
).all();
|
|
25
|
+
if (episodes.length === 0) return [];
|
|
26
|
+
const grouped = {};
|
|
27
|
+
for (const ep of episodes) {
|
|
28
|
+
const kind = ep.kind || 'other';
|
|
29
|
+
if (!grouped[kind]) grouped[kind] = [];
|
|
30
|
+
grouped[kind].push(ep.summary);
|
|
31
|
+
}
|
|
32
|
+
const lines = ['## Session Episodes'];
|
|
33
|
+
for (const [kind, items] of Object.entries(grouped)) {
|
|
34
|
+
lines.push(`### ${kind}`);
|
|
35
|
+
for (const item of items) lines.push(`- ${item}`);
|
|
36
|
+
}
|
|
37
|
+
return lines;
|
|
38
|
+
} finally {
|
|
39
|
+
db.close();
|
|
40
|
+
}
|
|
41
|
+
} catch (_err) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function collectGitDiff() {
|
|
47
|
+
try {
|
|
48
|
+
const { execFileSync } = require('child_process');
|
|
49
|
+
let diffStat = '';
|
|
50
|
+
try {
|
|
51
|
+
diffStat = execFileSync('git', ['diff', '--stat', 'HEAD'], { encoding: 'utf8', timeout: 5000, stdio: ['pipe', 'pipe', 'pipe'] }).trim();
|
|
52
|
+
} catch { diffStat = ''; }
|
|
53
|
+
if (diffStat) return ['## Changed Files (uncommitted)', '```', diffStat, '```'];
|
|
54
|
+
return [];
|
|
55
|
+
} catch (_err) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getActivePlan() {
|
|
61
|
+
try {
|
|
62
|
+
const plansDir = path.join(process.cwd(), '.claude', 'plans');
|
|
63
|
+
if (!fs.existsSync(plansDir)) return [];
|
|
64
|
+
const plans = fs.readdirSync(plansDir).filter(f => f.endsWith('.md'));
|
|
65
|
+
if (plans.length === 0) return [];
|
|
66
|
+
const latestPlan = plans.sort().pop();
|
|
67
|
+
return [`## Active Plan: ${latestPlan}`];
|
|
68
|
+
} catch (_err) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function main() {
|
|
74
|
+
const baseDir = findMindloreDir();
|
|
75
|
+
if (!baseDir) return;
|
|
76
|
+
|
|
77
|
+
// Trigger FTS5 sync via the index script
|
|
78
|
+
const indexScript = path.join(__dirname, '..', 'scripts', 'mindlore-fts5-index.cjs');
|
|
79
|
+
if (fs.existsSync(indexScript)) {
|
|
80
|
+
try {
|
|
81
|
+
const { spawnSync } = require('child_process');
|
|
82
|
+
spawnSync('node', [indexScript, baseDir], {
|
|
83
|
+
timeout: 10000,
|
|
84
|
+
stdio: 'pipe',
|
|
85
|
+
windowsHide: true,
|
|
86
|
+
});
|
|
87
|
+
} catch (_err) {
|
|
88
|
+
// Non-fatal — index might fail if better-sqlite3 not available
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const now = new Date();
|
|
93
|
+
const iso = now.toISOString();
|
|
94
|
+
const ts = iso.replace(/[:.]/g, '-');
|
|
95
|
+
|
|
96
|
+
const episodesDir = path.join(baseDir, 'episodes');
|
|
97
|
+
try {
|
|
98
|
+
const episodePath = path.join(episodesDir, `pre-compact-${ts}.md`);
|
|
99
|
+
const content = [
|
|
100
|
+
'---',
|
|
101
|
+
'type: episode',
|
|
102
|
+
'subtype: pre-compact',
|
|
103
|
+
`date: ${iso.slice(0, 10)}`,
|
|
104
|
+
`project: ${path.basename(process.cwd())}`,
|
|
105
|
+
'---',
|
|
106
|
+
'',
|
|
107
|
+
`Pre-compact snapshot at ${iso}.`,
|
|
108
|
+
`Working directory: ${process.cwd()}`,
|
|
109
|
+
].join('\n');
|
|
110
|
+
fs.writeFileSync(episodePath, content, 'utf8');
|
|
111
|
+
} catch (_err) { /* episodes dir may not exist */ }
|
|
112
|
+
|
|
113
|
+
// Append log entry
|
|
114
|
+
const logPath = path.join(baseDir, 'log.md');
|
|
115
|
+
try {
|
|
116
|
+
const entry = `| ${iso.slice(0, 10)} | pre-compact | FTS5 flush before compaction |\n`;
|
|
117
|
+
fs.appendFileSync(logPath, entry, 'utf8');
|
|
118
|
+
} catch (_err) { /* log file may not exist */ }
|
|
119
|
+
|
|
120
|
+
// Build compaction snapshot (#17)
|
|
121
|
+
const diaryDir = path.join(baseDir, 'diary');
|
|
122
|
+
try {
|
|
123
|
+
const sections = [];
|
|
124
|
+
sections.push(...collectRecentEpisodes(baseDir));
|
|
125
|
+
sections.push(...collectGitDiff());
|
|
126
|
+
sections.push(...getActivePlan());
|
|
127
|
+
|
|
128
|
+
if (sections.length > 0) {
|
|
129
|
+
const snapshotContent = [
|
|
130
|
+
'---',
|
|
131
|
+
'type: compaction-snapshot',
|
|
132
|
+
`date: ${iso.slice(0, 10)}`,
|
|
133
|
+
`project: ${path.basename(process.cwd())}`,
|
|
134
|
+
'---',
|
|
135
|
+
'',
|
|
136
|
+
...sections,
|
|
137
|
+
].join('\n');
|
|
138
|
+
fs.writeFileSync(path.join(diaryDir, `compaction-snapshot-${ts}.md`), snapshotContent);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const snapshots = listSnapshots(diaryDir).filter(f => f.startsWith('compaction-'));
|
|
142
|
+
while (snapshots.length > 5) {
|
|
143
|
+
const oldest = snapshots.shift();
|
|
144
|
+
if (oldest) fs.unlinkSync(path.join(diaryDir, oldest));
|
|
145
|
+
}
|
|
146
|
+
} catch (_err) { /* snapshot is best-effort */ }
|
|
147
|
+
|
|
148
|
+
process.stdout.write('[Mindlore: pre-compact FTS5 flush complete]\n');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
withTelemetry('mindlore-pre-compact', main).catch(err => {
|
|
152
|
+
hookLog('mindlore-pre-compact', 'error', err?.message ?? String(err));
|
|
153
|
+
process.exit(0);
|
|
154
|
+
});
|