agent-knowledge 1.3.27 → 1.4.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 (43) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +6 -1
  3. package/agent-desk-plugin.json +1 -1
  4. package/dist/knowledge/graph.d.ts +21 -4
  5. package/dist/knowledge/graph.d.ts.map +1 -1
  6. package/dist/knowledge/graph.js +71 -15
  7. package/dist/knowledge/graph.js.map +1 -1
  8. package/dist/knowledge/search.d.ts +13 -0
  9. package/dist/knowledge/search.d.ts.map +1 -1
  10. package/dist/knowledge/search.js +13 -3
  11. package/dist/knowledge/search.js.map +1 -1
  12. package/dist/search/boosts.d.ts +63 -0
  13. package/dist/search/boosts.d.ts.map +1 -0
  14. package/dist/search/boosts.js +214 -0
  15. package/dist/search/boosts.js.map +1 -0
  16. package/dist/server.d.ts.map +1 -1
  17. package/dist/server.js +38 -9
  18. package/dist/server.js.map +1 -1
  19. package/dist/sessions/indexer.d.ts +3 -0
  20. package/dist/sessions/indexer.d.ts.map +1 -1
  21. package/dist/sessions/indexer.js +9 -1
  22. package/dist/sessions/indexer.js.map +1 -1
  23. package/dist/sessions/search.d.ts.map +1 -1
  24. package/dist/sessions/search.js +9 -1
  25. package/dist/sessions/search.js.map +1 -1
  26. package/dist/tool-handlers.d.ts.map +1 -1
  27. package/dist/tool-handlers.js +34 -7
  28. package/dist/tool-handlers.js.map +1 -1
  29. package/dist/types.d.ts +8 -0
  30. package/dist/types.d.ts.map +1 -1
  31. package/dist/types.js +5 -0
  32. package/dist/types.js.map +1 -1
  33. package/dist/vectorstore/chunker.d.ts +13 -1
  34. package/dist/vectorstore/chunker.d.ts.map +1 -1
  35. package/dist/vectorstore/chunker.js +8 -1
  36. package/dist/vectorstore/chunker.js.map +1 -1
  37. package/dist/wakeup.d.ts +37 -0
  38. package/dist/wakeup.d.ts.map +1 -0
  39. package/dist/wakeup.js +132 -0
  40. package/dist/wakeup.js.map +1 -0
  41. package/docs/USER-MANUAL.md +31 -10
  42. package/package.json +3 -1
  43. package/scripts/hooks/precompact-flush.mjs +174 -0
@@ -0,0 +1,174 @@
1
+ #!/usr/bin/env node
2
+
3
+ // =============================================================================
4
+ // agent-knowledge PreCompact / Stop hook
5
+ //
6
+ // Flushes a session summary to ~/agent-knowledge/sessions/ before the host
7
+ // (Claude Code, Cursor, Codex CLI, etc.) compacts or ends the conversation,
8
+ // so the verbatim distillation has something to anchor against later.
9
+ //
10
+ // Modeled after mempalace's `mempal_precompact_hook.sh`. Fail-open: any error
11
+ // is logged to stderr and the hook still prints `{}` so it never blocks the host.
12
+ //
13
+ // Wire-up (host-specific — examples):
14
+ //
15
+ // Claude Code (~/.claude/settings.json):
16
+ // {
17
+ // "hooks": {
18
+ // "PreCompact": [{"matcher": "", "hooks": [
19
+ // {"type": "command",
20
+ // "command": "node \"$HOME/.claude/mcp-servers/agent-knowledge/scripts/hooks/precompact-flush.mjs\"",
21
+ // "timeout": 10}
22
+ // ]}],
23
+ // "Stop": [{"matcher": "", "hooks": [
24
+ // {"type": "command",
25
+ // "command": "node \"$HOME/.claude/mcp-servers/agent-knowledge/scripts/hooks/precompact-flush.mjs\"",
26
+ // "timeout": 10}
27
+ // ]}]
28
+ // }
29
+ // }
30
+ //
31
+ // Reads hook JSON from stdin (the host pipes it). The fields we look at:
32
+ // { session_id, hook_event_name, cwd? }
33
+ // =============================================================================
34
+
35
+ import { fileURLToPath } from 'url';
36
+ import { dirname, join, resolve } from 'path';
37
+ import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
38
+ import { homedir } from 'os';
39
+
40
+ const __filename = fileURLToPath(import.meta.url);
41
+ const __dirname = dirname(__filename);
42
+
43
+ function fail(msg) {
44
+ process.stderr.write(`[precompact-flush] ${msg}\n`);
45
+ process.stdout.write('{}\n');
46
+ process.exit(0);
47
+ }
48
+
49
+ function ok() {
50
+ process.stdout.write('{}\n');
51
+ process.exit(0);
52
+ }
53
+
54
+ async function readStdin() {
55
+ return new Promise((resolveP) => {
56
+ let data = '';
57
+ if (process.stdin.isTTY) return resolveP('');
58
+ process.stdin.setEncoding('utf-8');
59
+ process.stdin.on('data', (chunk) => (data += chunk));
60
+ process.stdin.on('end', () => resolveP(data));
61
+ // Don't hang forever if the host doesn't pipe anything
62
+ setTimeout(() => resolveP(data), 2000);
63
+ });
64
+ }
65
+
66
+ function slugify(input) {
67
+ return (input || 'session')
68
+ .toLowerCase()
69
+ .replace(/[^a-z0-9]+/g, '-')
70
+ .replace(/^-+|-+$/g, '')
71
+ .slice(0, 60);
72
+ }
73
+
74
+ function memoryDir() {
75
+ return process.env.KNOWLEDGE_MEMORY_DIR || join(homedir(), 'agent-knowledge');
76
+ }
77
+
78
+ async function main() {
79
+ const raw = await readStdin();
80
+ let payload = {};
81
+ if (raw) {
82
+ try {
83
+ payload = JSON.parse(raw);
84
+ } catch {
85
+ // Ignore — host may not have piped JSON
86
+ }
87
+ }
88
+
89
+ const sessionId = payload.session_id || process.env.CLAUDE_SESSION_ID || 'unknown';
90
+ const event = payload.hook_event_name || 'precompact';
91
+ const cwd = payload.cwd || process.cwd();
92
+ const projectSlug = slugify(cwd.split(/[\\/]/).filter(Boolean).pop() || 'project');
93
+
94
+ // Try to import the agent-knowledge summary helper directly (ESM dynamic import).
95
+ // The compiled output lives next to this script: ../../dist/sessions/summary.js
96
+ const distSummary = resolve(__dirname, '..', '..', 'dist', 'sessions', 'summary.js');
97
+ if (!existsSync(distSummary)) {
98
+ return fail(`dist not built: ${distSummary} (run \`npm run build\`)`);
99
+ }
100
+
101
+ let summaryFn;
102
+ try {
103
+ const mod = await import(`file://${distSummary.replace(/\\/g, '/')}`);
104
+ summaryFn = mod.getSessionSummary;
105
+ } catch (err) {
106
+ return fail(`import failed: ${err?.message || err}`);
107
+ }
108
+ if (typeof summaryFn !== 'function') {
109
+ return fail('getSessionSummary not exported from dist/sessions/summary.js');
110
+ }
111
+
112
+ let summary;
113
+ try {
114
+ summary = summaryFn(sessionId);
115
+ } catch (err) {
116
+ return fail(`getSessionSummary threw: ${err?.message || err}`);
117
+ }
118
+ if (!summary) {
119
+ return fail(`session ${sessionId} not found — nothing to flush`);
120
+ }
121
+
122
+ const outDir = join(memoryDir(), 'sessions', projectSlug);
123
+ try {
124
+ mkdirSync(outDir, { recursive: true });
125
+ } catch (err) {
126
+ return fail(`mkdir ${outDir}: ${err?.message || err}`);
127
+ }
128
+
129
+ const stamp = new Date().toISOString().replace(/[:.]/g, '-');
130
+ const outPath = join(outDir, `${event}-${stamp}-${sessionId.slice(0, 8)}.md`);
131
+
132
+ const body = [
133
+ '---',
134
+ `session_id: ${sessionId}`,
135
+ `event: ${event}`,
136
+ `flushed_at: ${new Date().toISOString()}`,
137
+ `cwd: ${cwd}`,
138
+ '---',
139
+ '',
140
+ `# Session ${sessionId}`,
141
+ '',
142
+ typeof summary === 'string'
143
+ ? summary
144
+ : '```json\n' + JSON.stringify(summary, null, 2) + '\n```',
145
+ '',
146
+ ].join('\n');
147
+
148
+ try {
149
+ writeFileSync(outPath, body, 'utf-8');
150
+ } catch (err) {
151
+ return fail(`write ${outPath}: ${err?.message || err}`);
152
+ }
153
+
154
+ process.stderr.write(`[precompact-flush] wrote ${outPath}\n`);
155
+
156
+ // Best-effort: append a one-line breadcrumb readable later by `knowledge` action=list
157
+ try {
158
+ const breadcrumb = join(memoryDir(), 'sessions', 'index.md');
159
+ const existing = existsSync(breadcrumb)
160
+ ? readFileSync(breadcrumb, 'utf-8')
161
+ : '# Session flushes\n';
162
+ writeFileSync(
163
+ breadcrumb,
164
+ existing + `- ${event} ${stamp} ${sessionId} → ${outPath}\n`,
165
+ 'utf-8',
166
+ );
167
+ } catch {
168
+ /* ignore */
169
+ }
170
+
171
+ ok();
172
+ }
173
+
174
+ main().catch((err) => fail(err?.message || String(err)));