Last active
July 10, 2025 11:26
-
-
Save arathunku/66a1ce1e2e2bc0118245a935f4cc864f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env fish | |
# Script receives Claude hook and tries its best to send a notification with useful content | |
# based on tool/transcript. | |
# Add in ~/.claude/settings.json | |
# "hooks": { | |
# "Notification": [ | |
# { | |
# "hooks": [ | |
# { "type": "command", "command": "notify-claude" } | |
# ] | |
# } | |
# ], | |
# "Stop": [ | |
# { | |
# "hooks": [ | |
# { "type": "command", "command": "notify-claude" } | |
# ] | |
# } | |
# ], | |
# } | |
# TODO: | |
# - Check if screen is locked and send via nfty/pushover | |
function safe_trim -a text | |
echo "$text" | string trim -c '\n' | |
end | |
function get_transcript_details -a transcript_path | |
if not test -f "$transcript_path"; | |
return 1 | |
end | |
set msg (jq -s '.[-1].message.content[0]' < "$transcript_path" 2>/dev/null) | |
if test $status -ne 0 | |
return 1 | |
end | |
switch (echo "$msg" | jq -r '.type // ""') | |
case tool_use | |
set text (echo "$msg" | jq -r '(.input.command + "\n" + .input.description) // ""') | |
string sub -l 280 "$text" | |
case text | |
set text (echo "$msg" | jq -r '.text // ""') | |
string sub -l 280 "$text" | |
case '*' | |
echo "Unknown message type" >&2 | |
return 1 | |
end | |
end | |
set json (cat) | |
if test -z "$json" | |
echo "No JSON input provided." >&2 | |
notify-send -a "claude" -c "agent" "Claude: Error" "No JSON input provided." | |
exit 1 | |
end | |
if not echo "$json" | jq empty 2>/dev/null | |
echo "Invalid JSON input." >&2 | |
notify-send -a "claude" -c "agent" "Claude: Error" "Invalid JSON input." | |
exit 1 | |
end | |
set hook_event_name (echo "$json" | jq -r '.hook_event_name // ""') | |
set transcript (echo "$json" | jq -r '.transcript_path // ""') | |
set session_id (echo "$json" | jq -r '.session_id // ""') | |
set tool_name (echo "$json" | jq -r '.tool_name // ""') | |
set details "" | |
if test -n "$transcript" | |
set details (get_transcript_details "$transcript") | |
if test $status -ne 0 | |
set details "Error reading transcript" | |
end | |
end | |
set subtitle "" | |
if test -n "$TMUX" | |
set tmux_info (tmux display-message -p '#S:#W' 2>/dev/null) | |
if test $status -eq 0 | |
set subtitle " | $tmux_info" | |
end | |
end | |
set title "" | |
set message "" | |
switch "$hook_event_name" | |
case "Notification" | |
set title (echo "$json" | jq -r '.title // "Notification"') | |
set message "$(echo "$json" | jq -r '.message // ""')\n$details" | |
case "Stop" "SubagentStop" | |
set title "Stopped" | |
set message "$details" | |
case "PreToolUse" | |
set title "Tool: $tool_name" | |
set message "$details" | |
case "PostToolUse" | |
set title "Completed: $tool_name" | |
set message "$details" | |
case '*' | |
set title "Unknown Event" | |
set message "$hook_event_name" | |
end | |
set message (safe_trim "$message") | |
notify-send -t 0 -a "claude" -c "agent" "Claude: $title$subtitle" "$message" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment