|
| 1 | +#!/bin/bash |
| 2 | +# Hook script for Claude Code Stop event |
| 3 | +# Sends a Warp notification when Claude completes a task |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | + |
| 7 | +# Read hook input from stdin |
| 8 | +INPUT=$(cat) |
| 9 | + |
| 10 | +# Extract transcript path from the hook input |
| 11 | +TRANSCRIPT_PATH=$(echo "$INPUT" | jq -r '.transcript_path // empty' 2>/dev/null) |
| 12 | + |
| 13 | +# Default message |
| 14 | +MSG="Task completed" |
| 15 | + |
| 16 | +# Try to extract prompt and response from the transcript (JSONL format) |
| 17 | +if [ -n "$TRANSCRIPT_PATH" ] && [ -f "$TRANSCRIPT_PATH" ]; then |
| 18 | + # Get the first user prompt |
| 19 | + PROMPT=$(jq -rs ' |
| 20 | + [.[] | select(.type == "user")] | first | .message.content // empty |
| 21 | + ' "$TRANSCRIPT_PATH" 2>/dev/null) |
| 22 | + |
| 23 | + # Get the last assistant response |
| 24 | + RESPONSE=$(jq -rs ' |
| 25 | + [.[] | select(.type == "assistant" and .message.content)] | last | |
| 26 | + [.message.content[] | select(.type == "text") | .text] | join(" ") |
| 27 | + ' "$TRANSCRIPT_PATH" 2>/dev/null) |
| 28 | + |
| 29 | + if [ -n "$PROMPT" ] && [ -n "$RESPONSE" ]; then |
| 30 | + # Truncate prompt to 50 chars |
| 31 | + if [ ${#PROMPT} -gt 50 ]; then |
| 32 | + PROMPT="${PROMPT:0:47}..." |
| 33 | + fi |
| 34 | + # Truncate response to 120 chars |
| 35 | + if [ ${#RESPONSE} -gt 120 ]; then |
| 36 | + RESPONSE="${RESPONSE:0:117}..." |
| 37 | + fi |
| 38 | + MSG="\"${PROMPT}\" → ${RESPONSE}" |
| 39 | + elif [ -n "$RESPONSE" ]; then |
| 40 | + # Fallback to just response if no prompt found |
| 41 | + if [ ${#RESPONSE} -gt 175 ]; then |
| 42 | + RESPONSE="${RESPONSE:0:172}..." |
| 43 | + fi |
| 44 | + MSG="$RESPONSE" |
| 45 | + fi |
| 46 | +fi |
| 47 | + |
| 48 | +"$SCRIPT_DIR/warp-notify.sh" "Claude Code" "$MSG" |
0 commit comments