-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhook.js
More file actions
61 lines (52 loc) · 1.87 KB
/
hook.js
File metadata and controls
61 lines (52 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
// Claude Code Stop Hook — automatically saves the session when Claude Code exits.
// Register in ~/.claude/settings.json:
//
// "hooks": {
// "Stop": [{
// "matcher": "",
// "hooks": [{ "type": "command", "command": "node /absolute/path/to/adr-skills/hook.js" }]
// }]
// }
import 'dotenv/config';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { saveSession } from './db.js';
let raw = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', chunk => { raw += chunk; });
process.stdin.on('end', () => {
try {
const hook = JSON.parse(raw);
const { transcript_path, cwd } = hook;
if (!transcript_path || !fs.existsSync(transcript_path)) process.exit(0);
// Parse JSONL transcript — each line is a message object
const conversation = fs.readFileSync(transcript_path, 'utf8')
.split('\n')
.filter(Boolean)
.flatMap(line => {
try {
const msg = JSON.parse(line);
if (!msg.role || !msg.content) return [];
const text = Array.isArray(msg.content)
? msg.content.filter(b => b.type === 'text').map(b => b.text).join(' ')
: String(msg.content);
return [`[${msg.role}] ${text}`];
} catch { return []; }
})
.join('\n');
if (!conversation.trim()) process.exit(0);
const project = path.basename(cwd ?? process.cwd());
let git_commit = null;
try {
git_commit = execSync('git rev-parse HEAD', { cwd, stdio: ['pipe', 'pipe', 'ignore'] })
.toString().trim();
} catch { /* not a git repo */ }
const id = saveSession({ project, conversation, git_commit });
console.error(`[adr-skills] Session auto-saved (ID: ${id}, project: ${project})`);
} catch (err) {
console.error(`[adr-skills] Hook error: ${err.message}`);
}
process.exit(0);
});