|
| 1 | +#!/bin/bash |
| 2 | +# Hook script for Claude Code StopFailure event |
| 3 | +# Sends a structured Warp notification when Claude's turn ends due to an API error |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +source "$SCRIPT_DIR/should-use-structured.sh" |
| 7 | + |
| 8 | +# Legacy Warp clients that don't support structured events don't get a |
| 9 | +# stop_failure notification — they have no UI to act on it. |
| 10 | +if ! should_use_structured; then |
| 11 | + exit 0 |
| 12 | +fi |
| 13 | + |
| 14 | +source "$SCRIPT_DIR/build-payload.sh" |
| 15 | + |
| 16 | +# Read hook input from stdin |
| 17 | +INPUT=$(cat) |
| 18 | + |
| 19 | +# Extract the error type (e.g. "rate_limit") and human-readable message. |
| 20 | +ERROR_TYPE=$(echo "$INPUT" | jq -r '.error // empty' 2>/dev/null) |
| 21 | +ERROR_MESSAGE=$(echo "$INPUT" | jq -r '.last_assistant_message // empty' 2>/dev/null) |
| 22 | + |
| 23 | +# Also try to extract the last user query from the transcript so Warp can |
| 24 | +# show it as the notification title, matching the Stop hook behaviour. |
| 25 | +TRANSCRIPT_PATH=$(echo "$INPUT" | jq -r '.transcript_path // empty' 2>/dev/null) |
| 26 | +QUERY="" |
| 27 | +if [ -n "$TRANSCRIPT_PATH" ] && [ -f "$TRANSCRIPT_PATH" ]; then |
| 28 | + QUERY=$(jq -rs ' |
| 29 | + [ |
| 30 | + .[] | select(.type == "user") | |
| 31 | + if .message.content | type == "string" then . |
| 32 | + elif [.message.content[] | select(.type == "text")] | length > 0 then . |
| 33 | + else empty |
| 34 | + end |
| 35 | + ] | last | |
| 36 | + if .message.content | type == "array" |
| 37 | + then [.message.content[] | select(.type == "text") | .text] | join(" ") |
| 38 | + else .message.content // empty |
| 39 | + end |
| 40 | + ' "$TRANSCRIPT_PATH" 2>/dev/null) |
| 41 | + |
| 42 | + if [ -n "$QUERY" ] && [ ${#QUERY} -gt 200 ]; then |
| 43 | + QUERY="${QUERY:0:197}..." |
| 44 | + fi |
| 45 | +fi |
| 46 | + |
| 47 | +BODY=$(build_payload "$INPUT" "stop_failure" \ |
| 48 | + --arg query "$QUERY" \ |
| 49 | + --arg response "$ERROR_MESSAGE" \ |
| 50 | + --arg error_type "$ERROR_TYPE" \ |
| 51 | + --arg transcript_path "$TRANSCRIPT_PATH") |
| 52 | + |
| 53 | +"$SCRIPT_DIR/warp-notify.sh" "warp://cli-agent" "$BODY" |
0 commit comments