agent-knowledge 1.5.2 → 1.5.3
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/agent-desk-plugin.json +1 -1
- package/docs/SETUP.md +39 -10
- package/docs/hooks.md +116 -0
- package/package.json +1 -1
- package/scripts/hooks/precompact-distill.mjs +105 -0
- package/scripts/hooks/sessionend-distill.mjs +122 -0
- package/scripts/setup.js +175 -0
package/agent-desk-plugin.json
CHANGED
package/docs/SETUP.md
CHANGED
|
@@ -61,7 +61,17 @@ agent-knowledge works with any MCP client (stdio) or HTTP client (REST API). Pic
|
|
|
61
61
|
|
|
62
62
|
### Claude Code
|
|
63
63
|
|
|
64
|
-
####
|
|
64
|
+
#### Quick setup (recommended)
|
|
65
|
+
|
|
66
|
+
From the agent-knowledge clone:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
node scripts/setup.js
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
This detects Claude Code, registers the MCP server in `~/.claude.json`, wires all 4 hooks into `~/.claude/settings.json`, and grants `mcp__agent-knowledge__*` permission in one shot. Restart Claude Code afterwards to pick up the new config.
|
|
73
|
+
|
|
74
|
+
#### Register the MCP server (manual)
|
|
65
75
|
|
|
66
76
|
```bash
|
|
67
77
|
claude mcp add agent-knowledge -s user \
|
|
@@ -170,15 +180,18 @@ curl -X PUT http://localhost:3423/api/knowledge/notes/my-note \
|
|
|
170
180
|
|
|
171
181
|
## Hooks
|
|
172
182
|
|
|
173
|
-
|
|
183
|
+
agent-knowledge ships **4 hook scripts** that wire into Claude Code's lifecycle events. `scripts/setup.js` installs them automatically; the block below shows the manual equivalent. Support varies by client.
|
|
174
184
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
185
|
+
| Hook | Event | Purpose |
|
|
186
|
+
| ------------------------ | ------------ | ------------------------------------------------------------- |
|
|
187
|
+
| `session-start.js` | SessionStart | Announces the knowledge dashboard URL |
|
|
188
|
+
| `precompact-flush.mjs` | PreCompact | Rich session summary via the compiled agent-knowledge library |
|
|
189
|
+
| `precompact-distill.mjs` | PreCompact | Lightweight text snapshot of recent user prompts |
|
|
190
|
+
| `sessionend-distill.mjs` | SessionEnd | Final summary (turn counts, tool uses, first 20 prompts) |
|
|
178
191
|
|
|
179
|
-
|
|
192
|
+
See [`docs/hooks.md`](hooks.md) for the full hook reference including environment variables and test coverage.
|
|
180
193
|
|
|
181
|
-
|
|
194
|
+
### Claude Code Hooks
|
|
182
195
|
|
|
183
196
|
```json
|
|
184
197
|
{
|
|
@@ -194,13 +207,29 @@ Add to `~/.claude/settings.json`:
|
|
|
194
207
|
]
|
|
195
208
|
}
|
|
196
209
|
],
|
|
197
|
-
"
|
|
210
|
+
"PreCompact": [
|
|
198
211
|
{
|
|
199
212
|
"hooks": [
|
|
200
213
|
{
|
|
201
214
|
"type": "command",
|
|
202
|
-
"command": "node \"/path/to/agent-knowledge/scripts/hooks/
|
|
203
|
-
"timeout":
|
|
215
|
+
"command": "node \"/path/to/agent-knowledge/scripts/hooks/precompact-flush.mjs\"",
|
|
216
|
+
"timeout": 10
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"type": "command",
|
|
220
|
+
"command": "node \"/path/to/agent-knowledge/scripts/hooks/precompact-distill.mjs\"",
|
|
221
|
+
"timeout": 10
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
],
|
|
226
|
+
"SessionEnd": [
|
|
227
|
+
{
|
|
228
|
+
"hooks": [
|
|
229
|
+
{
|
|
230
|
+
"type": "command",
|
|
231
|
+
"command": "node \"/path/to/agent-knowledge/scripts/hooks/sessionend-distill.mjs\"",
|
|
232
|
+
"timeout": 10
|
|
204
233
|
}
|
|
205
234
|
]
|
|
206
235
|
}
|
package/docs/hooks.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Hooks (Claude Code)
|
|
2
|
+
|
|
3
|
+
agent-knowledge ships four hook scripts that integrate with Claude Code's
|
|
4
|
+
lifecycle events. They live under `scripts/hooks/` and are installed by
|
|
5
|
+
`scripts/setup.js` into `~/.claude/settings.json`.
|
|
6
|
+
|
|
7
|
+
All hooks fail open — any internal error is logged to stderr and the hook
|
|
8
|
+
returns an empty JSON object so the user is never blocked.
|
|
9
|
+
|
|
10
|
+
| Script | Event | Purpose |
|
|
11
|
+
| ------------------------ | ------------ | -------------------------------------------------------- |
|
|
12
|
+
| `session-start.js` | SessionStart | Announces the knowledge dashboard URL |
|
|
13
|
+
| `precompact-flush.mjs` | PreCompact | Rich session summary via the library |
|
|
14
|
+
| `precompact-distill.mjs` | PreCompact | Lightweight text snapshot of recent user prompts |
|
|
15
|
+
| `sessionend-distill.mjs` | SessionEnd | Final summary (turn counts, tool uses, first 20 prompts) |
|
|
16
|
+
|
|
17
|
+
## session-start.js
|
|
18
|
+
|
|
19
|
+
Prints the dashboard URL (`http://localhost:3423` by default, override via
|
|
20
|
+
`AGENT_KNOWLEDGE_PORT`) and injects it as session context.
|
|
21
|
+
|
|
22
|
+
## precompact-flush.mjs
|
|
23
|
+
|
|
24
|
+
Called right before the host compacts the transcript. Imports
|
|
25
|
+
`dist/sessions/summary.js` from the built agent-knowledge library, pulls a
|
|
26
|
+
rich session summary via `getSessionSummary(sessionId)`, and writes it to
|
|
27
|
+
`~/agent-knowledge/sessions/<project-slug>/precompact-<timestamp>-<id>.md`.
|
|
28
|
+
Requires `npm run build` to have succeeded — if `dist/` is missing, the hook
|
|
29
|
+
logs the reason to stderr and emits `{}`.
|
|
30
|
+
|
|
31
|
+
### Environment variables
|
|
32
|
+
|
|
33
|
+
| Variable | Default | Description |
|
|
34
|
+
| ---------------------- | ------------------- | -------------------------------- |
|
|
35
|
+
| `KNOWLEDGE_MEMORY_DIR` | `~/agent-knowledge` | Where rich summaries are written |
|
|
36
|
+
|
|
37
|
+
## precompact-distill.mjs
|
|
38
|
+
|
|
39
|
+
A simpler heuristic companion to `precompact-flush.mjs`. Reads the last
|
|
40
|
+
~200 KB of the transcript JSONL, extracts the last 10 user prompts, and
|
|
41
|
+
writes them to
|
|
42
|
+
`~/claude-memory/projects/precompact-<slug>-<session>.md`. This one does
|
|
43
|
+
not require the library to be built, so it still works when `dist/` is
|
|
44
|
+
missing. Running both is fine — they target different files.
|
|
45
|
+
|
|
46
|
+
### Environment variables
|
|
47
|
+
|
|
48
|
+
| Variable | Default | Description |
|
|
49
|
+
| ---------------------- | ----------------- | ------------------------------------------------- |
|
|
50
|
+
| `KNOWLEDGE_MEMORY_DIR` | `~/claude-memory` | Where the snapshot is written (`<dir>/projects/`) |
|
|
51
|
+
|
|
52
|
+
## sessionend-distill.mjs
|
|
53
|
+
|
|
54
|
+
On `SessionEnd`, walks the whole transcript JSONL, counts user / assistant
|
|
55
|
+
turns and `tool_use` blocks, collects the first 20 user prompts, and writes
|
|
56
|
+
a markdown summary to
|
|
57
|
+
`~/claude-memory/projects/session-<slug>-<session>.md`. Gives
|
|
58
|
+
agent-knowledge a breadcrumb for later session search/recall.
|
|
59
|
+
|
|
60
|
+
## Manual configuration
|
|
61
|
+
|
|
62
|
+
`scripts/setup.js` writes the hooks into `~/.claude/settings.json`
|
|
63
|
+
automatically. If you need to configure them by hand, add entries like:
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"hooks": {
|
|
68
|
+
"SessionStart": [
|
|
69
|
+
{
|
|
70
|
+
"hooks": [
|
|
71
|
+
{
|
|
72
|
+
"type": "command",
|
|
73
|
+
"command": "node \"/abs/path/to/agent-knowledge/scripts/hooks/session-start.js\"",
|
|
74
|
+
"timeout": 5
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
],
|
|
79
|
+
"PreCompact": [
|
|
80
|
+
{
|
|
81
|
+
"hooks": [
|
|
82
|
+
{
|
|
83
|
+
"type": "command",
|
|
84
|
+
"command": "node \"/abs/path/to/agent-knowledge/scripts/hooks/precompact-flush.mjs\"",
|
|
85
|
+
"timeout": 10
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"type": "command",
|
|
89
|
+
"command": "node \"/abs/path/to/agent-knowledge/scripts/hooks/precompact-distill.mjs\"",
|
|
90
|
+
"timeout": 10
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
],
|
|
95
|
+
"SessionEnd": [
|
|
96
|
+
{
|
|
97
|
+
"hooks": [
|
|
98
|
+
{
|
|
99
|
+
"type": "command",
|
|
100
|
+
"command": "node \"/abs/path/to/agent-knowledge/scripts/hooks/sessionend-distill.mjs\"",
|
|
101
|
+
"timeout": 10
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Replace `/abs/path/to/agent-knowledge` with your clone path.
|
|
111
|
+
|
|
112
|
+
## Testing the hooks
|
|
113
|
+
|
|
114
|
+
`tests/hooks/hooks.test.ts` covers all four scripts with shape-correct
|
|
115
|
+
fail-open assertions. Run `npm test -- tests/hooks/` to execute them in
|
|
116
|
+
isolation.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-knowledge",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "Cross-session memory and recall for AI agents — git-synced knowledge base, knowledge graph, confidence scoring, hybrid semantic+TF-IDF search, auto-distillation with secrets scrubbing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// agent-knowledge PreCompact distill hook
|
|
5
|
+
//
|
|
6
|
+
// Dumps the tail of the current session transcript into the knowledge base
|
|
7
|
+
// right before the host compacts the conversation, so cross-machine memory
|
|
8
|
+
// survives the summarization. This is a lightweight heuristic snapshot —
|
|
9
|
+
// the companion `precompact-flush.mjs` hook does a richer summary via the
|
|
10
|
+
// compiled agent-knowledge library when available. Running both is fine;
|
|
11
|
+
// they write to distinct files.
|
|
12
|
+
//
|
|
13
|
+
// Fail-open: any error logs to stderr and emits `{}` so it never blocks.
|
|
14
|
+
// =============================================================================
|
|
15
|
+
|
|
16
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
17
|
+
import { join } from 'path';
|
|
18
|
+
import { homedir } from 'os';
|
|
19
|
+
|
|
20
|
+
process.on('uncaughtException', (err) => {
|
|
21
|
+
process.stderr.write(`[precompact-distill] fatal: ${err.message}\n`);
|
|
22
|
+
console.log(JSON.stringify({}));
|
|
23
|
+
process.exit(0);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const MEMORY_DIR = process.env.KNOWLEDGE_MEMORY_DIR || join(homedir(), 'claude-memory');
|
|
27
|
+
|
|
28
|
+
async function main() {
|
|
29
|
+
let input = '';
|
|
30
|
+
for await (const chunk of process.stdin) input += chunk;
|
|
31
|
+
|
|
32
|
+
let hookData = {};
|
|
33
|
+
try {
|
|
34
|
+
hookData = JSON.parse(input);
|
|
35
|
+
} catch {
|
|
36
|
+
// ignore
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const transcriptPath = hookData?.transcript_path;
|
|
40
|
+
const sessionId = hookData?.session_id || 'unknown';
|
|
41
|
+
const cwd = hookData?.workspace?.current_dir || process.cwd();
|
|
42
|
+
|
|
43
|
+
if (!transcriptPath || !existsSync(transcriptPath)) {
|
|
44
|
+
console.log(JSON.stringify({}));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let recent = '';
|
|
49
|
+
try {
|
|
50
|
+
const buf = readFileSync(transcriptPath);
|
|
51
|
+
recent = buf.slice(Math.max(0, buf.length - 200_000)).toString('utf8');
|
|
52
|
+
} catch {
|
|
53
|
+
// ignore
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const lines = recent.split(/\r?\n/).filter(Boolean);
|
|
57
|
+
const userMsgs = [];
|
|
58
|
+
for (const line of lines) {
|
|
59
|
+
try {
|
|
60
|
+
const obj = JSON.parse(line);
|
|
61
|
+
if (obj?.type === 'user' && typeof obj.message?.content === 'string') {
|
|
62
|
+
userMsgs.push(obj.message.content.slice(0, 500));
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
// ignore non-JSON lines
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const summary = userMsgs.slice(-10).join('\n---\n');
|
|
69
|
+
|
|
70
|
+
const memDir = join(MEMORY_DIR, 'projects');
|
|
71
|
+
try {
|
|
72
|
+
mkdirSync(memDir, { recursive: true });
|
|
73
|
+
} catch {
|
|
74
|
+
// ignore
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const slug = cwd.replace(/[^a-zA-Z0-9]+/g, '-').slice(0, 64);
|
|
78
|
+
const file = join(memDir, `precompact-${slug}-${sessionId.slice(0, 8)}.md`);
|
|
79
|
+
const body = `---
|
|
80
|
+
type: precompact-snapshot
|
|
81
|
+
session: ${sessionId}
|
|
82
|
+
cwd: ${cwd}
|
|
83
|
+
created: ${new Date().toISOString()}
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# Pre-compact snapshot
|
|
87
|
+
|
|
88
|
+
Last ${userMsgs.length} user prompts (most recent ${Math.min(10, userMsgs.length)}):
|
|
89
|
+
|
|
90
|
+
${summary}
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
writeFileSync(file, body, 'utf8');
|
|
95
|
+
} catch {
|
|
96
|
+
// ignore — don't block on write failure
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
console.log(JSON.stringify({}));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
main().catch((err) => {
|
|
103
|
+
process.stderr.write(`[precompact-distill] ${err.message}\n`);
|
|
104
|
+
console.log(JSON.stringify({}));
|
|
105
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// agent-knowledge SessionEnd distill hook
|
|
5
|
+
//
|
|
6
|
+
// Dumps a final session summary (turn counts, tool uses, first 20 user
|
|
7
|
+
// prompts) into the knowledge base when the host ends the conversation.
|
|
8
|
+
// Lightweight heuristic snapshot — the richer distillation happens via
|
|
9
|
+
// the library on next agent-knowledge startup.
|
|
10
|
+
//
|
|
11
|
+
// Fail-open.
|
|
12
|
+
// =============================================================================
|
|
13
|
+
|
|
14
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
15
|
+
import { join } from 'path';
|
|
16
|
+
import { homedir } from 'os';
|
|
17
|
+
|
|
18
|
+
process.on('uncaughtException', (err) => {
|
|
19
|
+
process.stderr.write(`[sessionend-distill] fatal: ${err.message}\n`);
|
|
20
|
+
console.log(JSON.stringify({}));
|
|
21
|
+
process.exit(0);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const MEMORY_DIR = process.env.KNOWLEDGE_MEMORY_DIR || join(homedir(), 'claude-memory');
|
|
25
|
+
|
|
26
|
+
async function main() {
|
|
27
|
+
let input = '';
|
|
28
|
+
for await (const chunk of process.stdin) input += chunk;
|
|
29
|
+
|
|
30
|
+
let hookData = {};
|
|
31
|
+
try {
|
|
32
|
+
hookData = JSON.parse(input);
|
|
33
|
+
} catch {
|
|
34
|
+
// ignore
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const transcriptPath = hookData?.transcript_path;
|
|
38
|
+
const sessionId = hookData?.session_id || 'unknown';
|
|
39
|
+
const cwd = hookData?.workspace?.current_dir || process.cwd();
|
|
40
|
+
|
|
41
|
+
if (!transcriptPath || !existsSync(transcriptPath)) {
|
|
42
|
+
console.log(JSON.stringify({}));
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let raw = '';
|
|
47
|
+
try {
|
|
48
|
+
raw = readFileSync(transcriptPath, 'utf8');
|
|
49
|
+
} catch {
|
|
50
|
+
// ignore
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const lines = raw.split(/\r?\n/).filter(Boolean);
|
|
54
|
+
let userTurns = 0;
|
|
55
|
+
let assistantTurns = 0;
|
|
56
|
+
let toolUses = 0;
|
|
57
|
+
const userMsgs = [];
|
|
58
|
+
for (const line of lines) {
|
|
59
|
+
try {
|
|
60
|
+
const obj = JSON.parse(line);
|
|
61
|
+
if (obj?.type === 'user') {
|
|
62
|
+
userTurns++;
|
|
63
|
+
if (typeof obj.message?.content === 'string') {
|
|
64
|
+
userMsgs.push(obj.message.content.slice(0, 300));
|
|
65
|
+
}
|
|
66
|
+
} else if (obj?.type === 'assistant') {
|
|
67
|
+
assistantTurns++;
|
|
68
|
+
const content = obj.message?.content;
|
|
69
|
+
if (Array.isArray(content)) {
|
|
70
|
+
toolUses += content.filter((c) => c.type === 'tool_use').length;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// ignore non-JSON lines
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const memDir = join(MEMORY_DIR, 'projects');
|
|
79
|
+
try {
|
|
80
|
+
mkdirSync(memDir, { recursive: true });
|
|
81
|
+
} catch {
|
|
82
|
+
// ignore
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const slug = cwd.replace(/[^a-zA-Z0-9]+/g, '-').slice(0, 64);
|
|
86
|
+
const file = join(memDir, `session-${slug}-${sessionId.slice(0, 8)}.md`);
|
|
87
|
+
const body = `---
|
|
88
|
+
type: session-end
|
|
89
|
+
session: ${sessionId}
|
|
90
|
+
cwd: ${cwd}
|
|
91
|
+
ended: ${new Date().toISOString()}
|
|
92
|
+
turns: ${userTurns}u/${assistantTurns}a
|
|
93
|
+
tool_uses: ${toolUses}
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
# Session ${sessionId.slice(0, 8)}
|
|
97
|
+
|
|
98
|
+
**cwd**: ${cwd}
|
|
99
|
+
**Turns**: ${userTurns} user / ${assistantTurns} assistant
|
|
100
|
+
**Tool uses**: ${toolUses}
|
|
101
|
+
|
|
102
|
+
## User prompts (first 20)
|
|
103
|
+
|
|
104
|
+
${userMsgs
|
|
105
|
+
.slice(0, 20)
|
|
106
|
+
.map((m, i) => `${i + 1}. ${m}`)
|
|
107
|
+
.join('\n')}
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
writeFileSync(file, body, 'utf8');
|
|
112
|
+
} catch {
|
|
113
|
+
// ignore
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
console.log(JSON.stringify({}));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
main().catch((err) => {
|
|
120
|
+
process.stderr.write(`[sessionend-distill] ${err.message}\n`);
|
|
121
|
+
console.log(JSON.stringify({}));
|
|
122
|
+
});
|
package/scripts/setup.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// agent-knowledge setup script
|
|
5
|
+
//
|
|
6
|
+
// Configures an MCP-compatible AI agent to use agent-knowledge.
|
|
7
|
+
// Currently supports: Claude Code (auto-detected via ~/.claude.json)
|
|
8
|
+
//
|
|
9
|
+
// What it does:
|
|
10
|
+
// - Builds the project if dist/ is missing
|
|
11
|
+
// - Registers the MCP server in the agent's config
|
|
12
|
+
// - Adds lifecycle hooks for Claude Code (SessionStart banner, PreCompact
|
|
13
|
+
// flush + distill, SessionEnd distill)
|
|
14
|
+
// - Adds permission for mcp__agent-knowledge__* tools
|
|
15
|
+
//
|
|
16
|
+
// Usage: node scripts/setup.js [--agent claude|generic]
|
|
17
|
+
// =============================================================================
|
|
18
|
+
|
|
19
|
+
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
|
20
|
+
import { join, resolve, dirname } from 'path';
|
|
21
|
+
import { homedir } from 'os';
|
|
22
|
+
import { execSync } from 'child_process';
|
|
23
|
+
import { fileURLToPath } from 'url';
|
|
24
|
+
|
|
25
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
26
|
+
const PROJECT_DIR = resolve(join(__dirname, '..'));
|
|
27
|
+
const HOME = homedir();
|
|
28
|
+
const CLAUDE_JSON = join(HOME, '.claude.json');
|
|
29
|
+
const SETTINGS_JSON = join(HOME, '.claude', 'settings.json');
|
|
30
|
+
|
|
31
|
+
const AGENT_FLAG = process.argv.find((_a, i, arr) => arr[i - 1] === '--agent') ?? 'auto';
|
|
32
|
+
const IS_CLAUDE = AGENT_FLAG === 'claude' || (AGENT_FLAG === 'auto' && existsSync(CLAUDE_JSON));
|
|
33
|
+
|
|
34
|
+
console.log('agent-knowledge setup\n');
|
|
35
|
+
console.log(`Agent type: ${IS_CLAUDE ? 'Claude Code' : 'Generic (manual MCP config)'}`);
|
|
36
|
+
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Build if needed
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
if (!existsSync(join(PROJECT_DIR, 'dist', 'index.js'))) {
|
|
42
|
+
console.log('Building agent-knowledge...');
|
|
43
|
+
execSync('npm run build', { cwd: PROJECT_DIR, stdio: 'inherit' });
|
|
44
|
+
console.log('');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Register MCP server
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
const distPath = join(PROJECT_DIR, 'dist', 'index.js');
|
|
52
|
+
|
|
53
|
+
console.log('Registering MCP server...');
|
|
54
|
+
if (IS_CLAUDE && existsSync(CLAUDE_JSON)) {
|
|
55
|
+
const config = JSON.parse(readFileSync(CLAUDE_JSON, 'utf-8'));
|
|
56
|
+
if (!config.mcpServers) config.mcpServers = {};
|
|
57
|
+
|
|
58
|
+
config.mcpServers['agent-knowledge'] = {
|
|
59
|
+
type: 'stdio',
|
|
60
|
+
command: 'node',
|
|
61
|
+
args: [distPath],
|
|
62
|
+
env: {},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
writeFileSync(CLAUDE_JSON, JSON.stringify(config, null, 2));
|
|
66
|
+
console.log(` Added agent-knowledge MCP server → ${distPath}`);
|
|
67
|
+
} else {
|
|
68
|
+
console.log(' Add this to your MCP client config:');
|
|
69
|
+
console.log(' {');
|
|
70
|
+
console.log(' "mcpServers": {');
|
|
71
|
+
console.log(' "agent-knowledge": {');
|
|
72
|
+
console.log(' "command": "node",');
|
|
73
|
+
console.log(` "args": ["${distPath.replace(/\\/g, '/')}"]`);
|
|
74
|
+
console.log(' }');
|
|
75
|
+
console.log(' }');
|
|
76
|
+
console.log(' }');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
|
|
81
|
+
if (!IS_CLAUDE) {
|
|
82
|
+
console.log(`
|
|
83
|
+
Setup complete!
|
|
84
|
+
|
|
85
|
+
Start the dashboard: node dist/server.js
|
|
86
|
+
MCP server (stdio): node dist/index.js
|
|
87
|
+
Dashboard URL: http://localhost:3423
|
|
88
|
+
`);
|
|
89
|
+
process.exit(0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.log('Configuring Claude Code hooks...');
|
|
93
|
+
if (!existsSync(SETTINGS_JSON)) {
|
|
94
|
+
console.log(' Warning: settings.json not found. Configure hooks manually.');
|
|
95
|
+
process.exit(0);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const settings = JSON.parse(readFileSync(SETTINGS_JSON, 'utf-8'));
|
|
99
|
+
|
|
100
|
+
if (!settings.permissions) settings.permissions = {};
|
|
101
|
+
if (!settings.permissions.allow) settings.permissions.allow = [];
|
|
102
|
+
if (!settings.permissions.allow.includes('mcp__agent-knowledge__*')) {
|
|
103
|
+
settings.permissions.allow.push('mcp__agent-knowledge__*');
|
|
104
|
+
console.log(' Added mcp__agent-knowledge__* permission');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!settings.hooks) settings.hooks = {};
|
|
108
|
+
|
|
109
|
+
const hookDir = join(PROJECT_DIR, 'scripts', 'hooks');
|
|
110
|
+
|
|
111
|
+
function addUnmatchedHook(eventName, marker, command, timeout = 10) {
|
|
112
|
+
if (!settings.hooks[eventName]) settings.hooks[eventName] = [];
|
|
113
|
+
const groups = settings.hooks[eventName];
|
|
114
|
+
const existing = groups.find(
|
|
115
|
+
(g) => g.hooks && g.hooks.some((h) => h.command && h.command.includes(marker)),
|
|
116
|
+
);
|
|
117
|
+
if (existing) {
|
|
118
|
+
console.log(` ${eventName} (${marker}): already configured`);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (groups.length > 0 && groups[0].hooks && !groups[0].matcher) {
|
|
122
|
+
groups[0].hooks.push({ type: 'command', command, timeout });
|
|
123
|
+
} else {
|
|
124
|
+
groups.push({ hooks: [{ type: 'command', command, timeout }] });
|
|
125
|
+
}
|
|
126
|
+
console.log(` ${eventName}: added ${marker} hook`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// SessionStart: dashboard banner
|
|
130
|
+
addUnmatchedHook(
|
|
131
|
+
'SessionStart',
|
|
132
|
+
'session-start.js',
|
|
133
|
+
`node "${join(hookDir, 'session-start.js')}"`,
|
|
134
|
+
5,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
// PreCompact: rich summary (library) + simple text distill (heuristic)
|
|
138
|
+
addUnmatchedHook(
|
|
139
|
+
'PreCompact',
|
|
140
|
+
'precompact-flush.mjs',
|
|
141
|
+
`node "${join(hookDir, 'precompact-flush.mjs')}"`,
|
|
142
|
+
10,
|
|
143
|
+
);
|
|
144
|
+
addUnmatchedHook(
|
|
145
|
+
'PreCompact',
|
|
146
|
+
'precompact-distill.mjs',
|
|
147
|
+
`node "${join(hookDir, 'precompact-distill.mjs')}"`,
|
|
148
|
+
10,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// SessionEnd: simple text distill for cross-machine memory
|
|
152
|
+
addUnmatchedHook(
|
|
153
|
+
'SessionEnd',
|
|
154
|
+
'sessionend-distill.mjs',
|
|
155
|
+
`node "${join(hookDir, 'sessionend-distill.mjs')}"`,
|
|
156
|
+
10,
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
writeFileSync(SETTINGS_JSON, JSON.stringify(settings, null, 2));
|
|
160
|
+
console.log(' Saved settings.json');
|
|
161
|
+
|
|
162
|
+
console.log(`
|
|
163
|
+
Setup complete!
|
|
164
|
+
|
|
165
|
+
Restart Claude Code to load the new MCP server. Every session will now:
|
|
166
|
+
- Show the knowledge dashboard URL on start (SessionStart hook)
|
|
167
|
+
- Flush a rich session summary before compaction (precompact-flush)
|
|
168
|
+
- Snapshot recent user prompts before compaction (precompact-distill)
|
|
169
|
+
- Dump a final session summary on exit (sessionend-distill)
|
|
170
|
+
|
|
171
|
+
Dashboard: http://localhost:3423 (auto-starts on first MCP connection)
|
|
172
|
+
Knowledge base: ~/claude-memory/ (configurable via KNOWLEDGE_MEMORY_DIR)
|
|
173
|
+
|
|
174
|
+
See docs/hooks.md for details and docs/SETUP.md for manual configuration.
|
|
175
|
+
`);
|