move memory hooks to ~/.claude/hooks for sandbox compatibility
This commit is contained in:
parent
f9c45b74de
commit
4695ac9121
6 changed files with 727 additions and 5 deletions
43
dot_claude/hooks/executable_pre-compact-backup.sh
Normal file
43
dot_claude/hooks/executable_pre-compact-backup.sh
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#!/bin/bash
|
||||
# PreCompact hook: Save key memories before compaction
|
||||
set -e
|
||||
|
||||
INPUT=$(cat)
|
||||
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // .sessionId // "unknown"')
|
||||
|
||||
MEMORY_HOME="${MEMORY_HOME:-$HOME/.claude/claude-memory}"
|
||||
MARKER_DIR="${MEMORY_HOME}/state/compaction-markers"
|
||||
MEMORY_DB="${MEMORY_HOME}/memory/memory.db"
|
||||
MARKER_FILE="${MARKER_DIR}/${SESSION_ID}.json"
|
||||
|
||||
mkdir -p "$MARKER_DIR"
|
||||
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
# Try API first, fall back to SQLite
|
||||
REMEMBERED_FACTS="[]"
|
||||
if [ -n "${MEMORY_API_KEY:-${CLAUDE_MEMORY_API_KEY:-}}" ]; then
|
||||
API_KEY="${MEMORY_API_KEY:-${CLAUDE_MEMORY_API_KEY:-}}"
|
||||
API_URL="${MEMORY_API_URL:-${CLAUDE_MEMORY_API_URL:-}}"
|
||||
if [ -n "$API_URL" ]; then
|
||||
REMEMBERED_FACTS=$(curl -sf -H "Authorization: Bearer ${API_KEY}" \
|
||||
"${API_URL}/api/memories?limit=20" 2>/dev/null | \
|
||||
jq '[.memories[] | {content, category, importance}]' 2>/dev/null || echo "[]")
|
||||
fi
|
||||
elif [ -f "$MEMORY_DB" ]; then
|
||||
REMEMBERED_FACTS=$(sqlite3 -json "$MEMORY_DB" \
|
||||
"SELECT content, category, importance FROM memories ORDER BY importance DESC, created_at DESC LIMIT 20" 2>/dev/null || echo "[]")
|
||||
fi
|
||||
|
||||
if ! echo "$REMEMBERED_FACTS" | jq empty 2>/dev/null; then
|
||||
REMEMBERED_FACTS="[]"
|
||||
fi
|
||||
|
||||
jq -n \
|
||||
--arg sid "$SESSION_ID" \
|
||||
--arg ts "$TIMESTAMP" \
|
||||
--argjson facts "$REMEMBERED_FACTS" \
|
||||
'{sessionId: $sid, compactedAt: $ts, rememberedFacts: $facts}' \
|
||||
> "$MARKER_FILE"
|
||||
|
||||
exit 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue