chat-recall 0.7.30 → 0.7.31

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.
@@ -1,260 +0,0 @@
1
- #!/usr/bin/env bash
2
- # chat-recall save hook — auto-saves memory every 15 messages
3
- # Installed as a Claude Code hook in ~/.claude/hooks.json
4
- #
5
- # This hook fires every 15 messages during a session.
6
- # It extracts key facts (decisions, topics, tools) and saves them
7
- # so they're preserved even if the session ends unexpectedly.
8
- #
9
- # Setup (one-time):
10
- # 1. Copy this file to ~/.claude/chat-recall-hooks/
11
- # 2. Add to ~/.claude/hooks.json:
12
- # {
13
- # "hooks": {
14
- # "Stop": [{"matcher": "", "hooks": [{"type": "command", "command": "/home/YOU/.claude/chat-recall-hooks/chat_recall_save_hook.sh"}]}],
15
- # "PreCompact": [{"matcher": "", "hooks": [{"type": "command", "command": "/home/YOU/.claude/chat-recall-hooks/chat_recall_save_hook.sh --precompact"}]}]
16
- # }
17
- # }
18
-
19
- set -euo pipefail
20
-
21
- HOOKS_DIR="${HOME}/.claude/chat-recall-hooks"
22
- MEMORY_DIR="${HOME}/.claude/chat-recall-memory"
23
- STATE_FILE="${MEMORY_DIR}/last_save.json"
24
- SESSION_ID=""
25
-
26
- log() {
27
- echo "[chat-recall-save] $(date +%H:%M:%S) $*" >&2
28
- }
29
-
30
- init() {
31
- mkdir -p "${MEMORY_DIR}"
32
- mkdir -p "${HOOKS_DIR}"
33
- }
34
-
35
- # Detect current session from transcript
36
- detect_session() {
37
- local projects_dir="${HOME}/.claude/projects"
38
-
39
- # Find the most recently modified session file
40
- local latest_file=""
41
- local latest_time=0
42
-
43
- if [[ -d "$projects_dir" ]]; then
44
- for dir in "$projects_dir"/*/; do
45
- if [[ -d "$dir" ]]; then
46
- for file in "$dir"/*.jsonl; do
47
- if [[ -f "$file" ]]; then
48
- local mtime
49
- mtime=$(stat -c %Y "$file" 2>/dev/null || stat -f %m "$file" 2>/dev/null || echo 0)
50
- if [[ "$mtime" -gt "$latest_time" ]]; then
51
- latest_time="$mtime"
52
- latest_file="$file"
53
- fi
54
- fi
55
- done
56
- fi
57
- done
58
- fi
59
-
60
- if [[ -n "$latest_file" && -f "$latest_file" ]]; then
61
- SESSION_ID=$(basename "$latest_file" .jsonl)
62
- fi
63
- }
64
-
65
- # Extract key facts from a session file
66
- extract_facts() {
67
- local session_file="$1"
68
- local session_id="$2"
69
-
70
- if [[ ! -f "$session_file" ]]; then
71
- echo "{}"
72
- return
73
- fi
74
-
75
- # Count messages
76
- local msg_count
77
- msg_count=$(wc -l < "$session_file" 2>/dev/null || echo 0)
78
-
79
- # Extract unique tools used
80
- local tools
81
- tools=$(grep -o '"type":"tool_use"' "$session_file" 2>/dev/null | wc -l | tr -d ' ')
82
-
83
- # Extract topics from first user message
84
- local first_topic
85
- first_topic=$(grep -m1 '"type":"user"' "$session_file" 2>/dev/null | \
86
- sed 's/.*"text":"//' | sed 's/".*//' | head -c 200 | tr -d '\n' | tr -d '"')
87
-
88
- # Extract decisions (heuristic)
89
- local decisions=""
90
- decisions=$(grep -i "decided\|chose\|going with\|recommend\|prefer" "$session_file" 2>/dev/null | \
91
- head -3 | sed 's/.*"text":"//' | sed 's/".*//' | head -c 300 | tr -d '\n')
92
-
93
- # Get project path
94
- local project_path=""
95
- for dir in "${HOME}"/.claude/projects/*/; do
96
- if [[ -d "$dir" && -f "${dir}${session_id}.jsonl" ]]; then
97
- project_path=$(echo "$dir" | sed "s|${HOME}/.claude/projects/||" | sed 's|/||g' | sed 's|^|/|')
98
- project_path=$(echo "$project_path" | sed "s|-|/|g")
99
- break
100
- fi
101
- done
102
-
103
- # Output JSON
104
- cat <<EOF
105
- {
106
- "session_id": "$session_id",
107
- "project_path": "$project_path",
108
- "message_count": $msg_count,
109
- "tool_uses": $tools,
110
- "first_topic": "$(echo "$first_topic" | jq -Rs '.' 2>/dev/null || echo "\"$first_topic\"")",
111
- "decisions": "$(echo "$decisions" | jq -Rs '.' 2>/dev/null || echo "\"\"")",
112
- "saved_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
113
- }
114
- EOF
115
- }
116
-
117
- # Count messages in current session
118
- count_messages() {
119
- local session_file="$1"
120
- if [[ ! -f "$session_file" ]]; then
121
- echo 0
122
- return
123
- fi
124
- grep -c '"type":"user"\|\|"type":"assistant"' "$session_file" 2>/dev/null || echo 0
125
- }
126
-
127
- # Main save logic
128
- do_save() {
129
- local precompact="${1:-false}"
130
-
131
- detect_session
132
-
133
- if [[ -z "$SESSION_ID" ]]; then
134
- log "No active session found"
135
- return
136
- fi
137
-
138
- # Find the session file
139
- local session_file=""
140
- for dir in "${HOME}"/.claude/projects/*/; do
141
- if [[ -f "${dir}${SESSION_ID}.jsonl" ]]; then
142
- session_file="${dir}${SESSION_ID}.jsonl"
143
- break
144
- fi
145
- done
146
-
147
- if [[ -z "$session_file" || ! -f "$session_file" ]]; then
148
- log "Session file not found for $SESSION_ID"
149
- return
150
- fi
151
-
152
- # Extract and save facts
153
- local facts
154
- facts=$(extract_facts "$session_file" "$SESSION_ID")
155
-
156
- local output_file="${MEMORY_DIR}/sessions/${SESSION_ID}.json"
157
- mkdir -p "$(dirname "$output_file")"
158
-
159
- # Merge with existing data if present
160
- if [[ -f "$output_file" ]]; then
161
- # Simple merge: update fields but preserve accumulated data
162
- local existing_msg_count
163
- existing_msg_count=$(jq -r '.accumulated_messages // 0' "$output_file" 2>/dev/null || echo 0)
164
- local new_msg_count
165
- new_msg_count=$(echo "$facts" | jq -r '.message_count')
166
- local total_msgs=$((existing_msg_count + new_msg_count))
167
-
168
- # Update but preserve accumulated decisions
169
- local existing_decisions
170
- existing_decisions=$(jq -r '.accumulated_decisions // []' "$output_file" 2>/dev/null || echo '[]')
171
-
172
- echo "$facts" | jq --argjson decisions "$existing_decisions" --argjson total "$total_msgs" \
173
- '. + {accumulated_messages: $total, accumulated_decisions: $decisions}' > "$output_file"
174
- else
175
- echo "$facts" | jq '. + {accumulated_messages: .message_count, accumulated_decisions: []}' > "$output_file"
176
- fi
177
-
178
- # Track this save
179
- # `date -Iseconds` is GNU-only: BSD date (macOS) rejects -I and prints an
180
- # empty string, so this field was blank on every Mac. -u +format works on
181
- # both. saved_at_epoch is written alongside it because reading the ISO value
182
- # BACK needed `date -d`, which BSD also lacks — storing the number means
183
- # nothing has to parse a date at all.
184
- echo "{\"session_id\": \"$SESSION_ID\", \"saved_at\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"saved_at_epoch\": $(date +%s), \"precompact\": $precompact}" > "${STATE_FILE}"
185
-
186
- if [[ "$precompact" == "true" ]]; then
187
- log "[PRECOMPACT] Emergency save for $SESSION_ID"
188
- else
189
- log "Saved facts for $SESSION_ID ($(echo "$facts" | jq -r '.message_count') messages)"
190
- fi
191
- }
192
-
193
- # Check if we should save (throttle to avoid too frequent saves)
194
- should_save() {
195
- local session_file="$1"
196
-
197
- if [[ ! -f "${STATE_FILE}" ]]; then
198
- return 0
199
- fi
200
-
201
- # Read the epoch we stored. The old code kept only an ISO string and parsed
202
- # it with `date -d`, which does not exist on BSD date — so on macOS this
203
- # always resolved to 0, the throttle never engaged, and the hook re-scanned
204
- # the entire transcript on EVERY Stop event.
205
- local last_save_epoch
206
- last_save_epoch=$(jq -r '.saved_at_epoch // empty' "${STATE_FILE}" 2>/dev/null || echo "")
207
-
208
- if [[ -z "$last_save_epoch" ]]; then
209
- # A state file written before saved_at_epoch existed. Treat it as due
210
- # rather than trying to parse its timestamp portably.
211
- return 0
212
- fi
213
- local now_epoch
214
- now_epoch=$(date +%s)
215
-
216
- if (( now_epoch - last_save_epoch < 30 )); then
217
- return 1
218
- fi
219
-
220
- return 0
221
- }
222
-
223
- main() {
224
- init
225
-
226
- local precompact="false"
227
- if [[ "${1:-}" == "--precompact" ]]; then
228
- precompact="true"
229
- fi
230
-
231
- detect_session
232
-
233
- if [[ -z "$SESSION_ID" ]]; then
234
- log "No session detected, skipping"
235
- exit 0
236
- fi
237
-
238
- # Find session file
239
- local session_file=""
240
- for dir in "${HOME}"/.claude/projects/*/; do
241
- if [[ -f "${dir}${SESSION_ID}.jsonl" ]]; then
242
- session_file="${dir}${SESSION_ID}.jsonl"
243
- break
244
- fi
245
- done
246
-
247
- if [[ -z "$session_file" ]]; then
248
- log "Session file not found"
249
- exit 1
250
- fi
251
-
252
- if ! should_save "$session_file"; then
253
- log "Throttled (recent save)"
254
- exit 0
255
- fi
256
-
257
- do_save "$precompact"
258
- }
259
-
260
- main "$@"