mirror of
https://github.com/anthropics/claude-code.git
synced 2026-04-26 15:45:26 +00:00
feat: Add Notification hook handler for formatting idle notifications
Format raw JSON IPC messages from workers/teammates into user-friendly
display instead of showing raw JSON to users.
Changes:
- Add notification.py hook handler to hookify plugin that formats
idle_notification, status_update, and progress_update messages
- Update hookify hooks.json to include Notification event handler
- Add Pattern 11 documentation for formatting teammate idle notifications
- Add format-idle-notification.sh example script
Raw JSON input like:
{"type":"idle_notification","from":"worker-1","timestamp":"..."}
Now displays as:
⏺ worker-1
⎿ Status is idle
Slack thread: https://anthropic.slack.com/archives/C07VBSHV7EV/p1765785226571409?thread_ts=1765776251.343939&cid=C07VBSHV7EV
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#!/bin/bash
|
||||
# Example: Format teammate idle notification
|
||||
#
|
||||
# This script demonstrates how to format raw JSON idle notifications
|
||||
# into user-friendly display format.
|
||||
#
|
||||
# Usage: echo '{"type":"idle_notification","from":"worker-1","timestamp":"..."}' | ./format-idle-notification.sh
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Read JSON from stdin
|
||||
input=$(cat)
|
||||
|
||||
# Parse notification type
|
||||
notification_type=$(echo "$input" | jq -r '.type // empty' 2>/dev/null || echo "")
|
||||
|
||||
if [[ "$notification_type" == "idle_notification" ]]; then
|
||||
# Extract fields
|
||||
worker_name=$(echo "$input" | jq -r '.from // "worker"')
|
||||
timestamp=$(echo "$input" | jq -r '.timestamp // empty')
|
||||
|
||||
# Format timestamp if present
|
||||
time_str=""
|
||||
if [[ -n "$timestamp" ]]; then
|
||||
# Try to format the timestamp
|
||||
time_str=$(date -d "$timestamp" '+%H:%M:%S' 2>/dev/null || echo "")
|
||||
fi
|
||||
|
||||
# Output formatted notification using recommended format:
|
||||
# ⏺ worker-1
|
||||
# ⎿ Status is idle
|
||||
echo "⏺ $worker_name"
|
||||
if [[ -n "$time_str" ]]; then
|
||||
echo " ⎿ Status is idle ($time_str)"
|
||||
else
|
||||
echo " ⎿ Status is idle"
|
||||
fi
|
||||
|
||||
# Output JSON for hook system
|
||||
if [[ -n "$time_str" ]]; then
|
||||
jq -n --arg msg "⏺ $worker_name\n ⎿ Status is idle ($time_str)" \
|
||||
'{"systemMessage": $msg}'
|
||||
else
|
||||
jq -n --arg msg "⏺ $worker_name\n ⎿ Status is idle" \
|
||||
'{"systemMessage": $msg}'
|
||||
fi
|
||||
else
|
||||
# Not an idle notification, pass through
|
||||
echo "$input"
|
||||
fi
|
||||
@@ -344,3 +344,69 @@ fi
|
||||
- Per-project settings
|
||||
- Team-specific rules
|
||||
- Dynamic validation criteria
|
||||
|
||||
## Pattern 11: Format Teammate Idle Notifications
|
||||
|
||||
Format raw JSON IPC messages from workers/teammates into user-friendly display:
|
||||
|
||||
```json
|
||||
{
|
||||
"Notification": [
|
||||
{
|
||||
"matcher": "*",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/notification.py",
|
||||
"timeout": 10
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
**Example script (format-idle-notification.py):**
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import json
|
||||
|
||||
def format_idle_notification(data):
|
||||
"""Format idle notification for display."""
|
||||
worker_name = data.get('from', 'worker')
|
||||
# Output format:
|
||||
# ⏺ worker-1
|
||||
# ⎿ Status is idle
|
||||
return f"⏺ {worker_name}\n ⎿ Status is idle"
|
||||
|
||||
def main():
|
||||
input_data = json.load(sys.stdin)
|
||||
|
||||
# Check for idle notification
|
||||
if input_data.get('type') == 'idle_notification':
|
||||
formatted = format_idle_notification(input_data)
|
||||
print(json.dumps({"systemMessage": formatted}))
|
||||
else:
|
||||
print(json.dumps({}))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
**Input (raw JSON IPC message):**
|
||||
```json
|
||||
{"type": "idle_notification", "from": "worker-1", "timestamp": "2025-12-15T05:22:40.320Z"}
|
||||
```
|
||||
|
||||
**Output (formatted for display):**
|
||||
```
|
||||
⏺ worker-1
|
||||
⎿ Status is idle
|
||||
```
|
||||
|
||||
**Use for:**
|
||||
- Formatting teammate/worker status messages
|
||||
- Converting internal IPC messages to user-friendly display
|
||||
- Multi-agent swarm coordination UI
|
||||
- Any notification that shouldn't show raw JSON to users
|
||||
|
||||
Reference in New Issue
Block a user