File size: 1,356 Bytes
4d5727a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# agentmemory Claude Code hook
# Source this file or add it to your Claude Code hook configuration.
# Reads AGENTMEMORY_URL and AGENTMEMORY_SECRET from environment.
#
# Usage in .claude/settings.json hooks:
#   "PostToolUse": [{"matcher": "*", "hooks": [{"type": "command", "command": "bash /path/to/claude-code-hook.sh"}]}]

set -euo pipefail

AGENTMEMORY_URL="${AGENTMEMORY_URL:-http://127.0.0.1:3111}"
AGENTMEMORY_SECRET="${AGENTMEMORY_SECRET:-}"
AGENT_ID="${CLAUDE_AGENT_ID:-claude-code}"
FOLDER_PATH="${PWD}"
TIMESTAMP="$(date -u +%Y-%m-%dT%H:%M:%SZ)"

# Read observation text from stdin (Claude Code passes hook data via stdin)
HOOK_DATA="$(cat)"
TEXT="${HOOK_DATA:-Claude Code tool execution in ${FOLDER_PATH}}"

# Truncate text to 4000 chars
TEXT="${TEXT:0:4000}"

PAYLOAD=$(cat <<EOF
{
  "folderPath": "${FOLDER_PATH}",
  "agentId": "${AGENT_ID}",
  "text": $(echo "$TEXT" | python3 -c "import json,sys; print(json.dumps(sys.stdin.read()))"),
  "timestamp": "${TIMESTAMP}"
}
EOF
)

AUTH_HEADER=""
if [ -n "${AGENTMEMORY_SECRET}" ]; then
  AUTH_HEADER="-H \"Authorization: Bearer ${AGENTMEMORY_SECRET}\""
fi

curl -sf \
  -X POST \
  "${AGENTMEMORY_URL}/agentmemory/agent/observe" \
  -H "Content-Type: application/json" \
  ${AUTH_HEADER:+$AUTH_HEADER} \
  -d "${PAYLOAD}" \
  --max-time 5 \
  > /dev/null 2>&1 || true

exit 0