kammer/.claude/hooks/log-request.sh
2026-02-26 17:10:07 +01:00

54 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Claude Code — UserPromptSubmit hook
#
# Logs each new user prompt to .claude/request-log.jsonl (git-tracked).
# Runs async so it never blocks Claude's response.
#
# Input (stdin): JSON with fields: session_id, cwd, prompt, is_continuation
set -euo pipefail
INPUT=$(cat)
PROMPT=$(echo "$INPUT" | jq -r '.prompt // ""')
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // ""')
CWD=$(echo "$INPUT" | jq -r '.cwd // "$PWD"')
IS_CONTINUATION=$(echo "$INPUT" | jq -r '.is_continuation // false')
LOG_FILE="${CWD}/.claude/request-log.jsonl"
# Skip: continuation turns, trivially short prompts, no log file dir
if [ "$IS_CONTINUATION" = "true" ]; then
exit 0
fi
if [ "${#PROMPT}" -lt 5 ]; then
exit 0
fi
if [ ! -d "${CWD}/.claude" ]; then
exit 0
fi
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
REQ_ID="req_$(date +%Y%m%d_%H%M%S)_$(LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 5)"
BRANCH=$(git -C "$CWD" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
jq -n \
--arg id "$REQ_ID" \
--arg ts "$TIMESTAMP" \
--arg prompt "$PROMPT" \
--arg session "$SESSION_ID" \
--arg branch "$BRANCH" \
'{
id: $id,
timestamp: $ts,
prompt: $prompt,
session: $session,
branch: $branch,
pr_number: null,
pr_url: null,
status: "pending"
}' >> "$LOG_FILE"
exit 0