-
-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathposttooluse.mjs
More file actions
executable file
·56 lines (46 loc) · 1.77 KB
/
posttooluse.mjs
File metadata and controls
executable file
·56 lines (46 loc) · 1.77 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
#!/usr/bin/env node
import "./suppress-stderr.mjs";
import "./ensure-deps.mjs";
/**
* PostToolUse hook for context-mode session continuity.
*
* Captures session events from tool calls (13 categories) and stores
* them in the per-project SessionDB for later resume snapshot building.
*
* Must be fast (<20ms). No network, no LLM, just SQLite writes.
*/
import { readStdin, getSessionId, getSessionDBPath } from "./session-helpers.mjs";
import { createSessionLoaders } from "./session-loaders.mjs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
// Resolve absolute path for imports — relative dynamic imports can fail
// when Claude Code invokes hooks from a different working directory.
const HOOK_DIR = dirname(fileURLToPath(import.meta.url));
const { loadSessionDB, loadExtract } = createSessionLoaders(HOOK_DIR);
try {
const raw = await readStdin();
const input = JSON.parse(raw);
const { extractEvents } = await loadExtract();
const { SessionDB } = await loadSessionDB();
const dbPath = getSessionDBPath();
const db = new SessionDB({ dbPath });
const sessionId = getSessionId(input);
// Ensure session meta exists
db.ensureSession(sessionId, process.env.CLAUDE_PROJECT_DIR || process.cwd());
// Extract and store events
const events = extractEvents({
tool_name: input.tool_name,
tool_input: input.tool_input ?? {},
tool_response: typeof input.tool_response === "string"
? input.tool_response
: JSON.stringify(input.tool_response ?? ""),
tool_output: input.tool_output,
});
for (const event of events) {
db.insertEvent(sessionId, event, "PostToolUse");
}
db.close();
} catch {
// PostToolUse must never block the session — silent fallback
}
// PostToolUse hooks don't need hookSpecificOutput