aboutsummaryrefslogtreecommitdiffstats
path: root/hooks/dump.hook.sh
blob: 17ca4bb16b8d2f56cf3c731d9b5a4c9c35b9141d (plain)
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
#!/usr/bin/env bash
# Generic payload-dump hook: write the full JSON payload Claude Code sends on an
# event to its own `.json` file, so a consumer can read the raw record later.
# Reads the payload as JSON on stdin. One script serves every event it is
# registered against — both the target dir and the filename are derived from the
# payload, exactly as `nvim-notify.hook.sh` derives its signal.
#
# One file per fire (XDG state home):
#   $XDG_STATE_HOME/claude/<dir>/<id>.json
#   * <dir> is `hook_event_name` lowercased, with a trailing `failure` stripped
#     so PostToolUse and PostToolUseFailure share one `posttooluse` dir (the two
#     halves — command and result — of a Bash call's record).
#   * <id> is `tool_use_id` (`toolu_…`, unique per tool call and identical
#     across the Pre/PostToolUse payloads, so input and output dumps pair by
#     basename), else `session_id` with the session-scoped suffix `source`
#     (SessionStart: startup/resume/clear/compact) or `reason` (SessionEnd:
#     clear/logout/prompt_input_exit/other) appended to keep a session's fires
#     distinct. A repeated id overwrites its previous dump (latest wins).
# Chronological order comes from file mtime; the wall-clock time lives inside
# the JSON.
input=$(cat)

event=$(jq --raw-output '.hook_event_name // empty' <<<"$input" 2>/dev/null)
[ -n "$event" ] || exit 0
name="${event,,}"
name="${name%failure}"
dir="${XDG_STATE_HOME:-$HOME/.local/state}/claude/$name"
mkdir --parents "$dir" 2>/dev/null

id=$(jq --raw-output '.tool_use_id // empty' <<<"$input" 2>/dev/null)
if [ -z "$id" ]; then
	id=$(jq --raw-output '.session_id // empty' <<<"$input" 2>/dev/null)
	suffix=$(jq --raw-output '.source // .reason // empty' <<<"$input" 2>/dev/null)
	[ -n "$id" ] && [ -n "$suffix" ] && id="$id-$suffix"
fi
[ -n "$id" ] || id="unknown_$(date +'%F_%T')"

jq --sort-keys '.' <<<"$input" >"$dir/$id.json" 2>/dev/null ||
	printf '%s\n' "$input" >"$dir/$id.json"