mirror of
https://github.com/imjasonh/cnotes
synced 2026-07-18 23:01:31 +00:00
Scaffold all 7 hook events and eliminate type casting
- Add missing hook handlers: Stop, SubagentStop, PreCompact
- Replace all string type casting with proper JSON unmarshaling
- Change ToolUseRequest.Parameters from map[string]interface{} to json.RawMessage
- Update helper methods to use json.Unmarshal directly to typed structs
- Replace speech notifications with simple logging notifications
- Update documentation to reflect all hook events and remove speech references
- Add comprehensive CLAUDE.md with architecture overview and development guidance
All hooks now use type-safe JSON handling without .(string) assertions.
Complete coverage of Claude Code events: PreToolUse, PostToolUse,
UserPromptSubmit, Notification, Stop, SubagentStop, PreCompact.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
c61b579243
commit
45ef980233
10 changed files with 351 additions and 108 deletions
|
|
@ -2,78 +2,54 @@ package handlers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/imjasonh/hooks/internal/hooks"
|
||||
)
|
||||
|
||||
func init() {
|
||||
hooks.RegisterHook(hooks.EventNotification, "*", SpeakNotification)
|
||||
hooks.RegisterHook(hooks.EventNotification, "*", LogNotification)
|
||||
}
|
||||
|
||||
func SpeakNotification(ctx context.Context, input hooks.HookInput) (hooks.HookOutput, error) {
|
||||
// Only run on macOS
|
||||
if runtime.GOOS != "darwin" {
|
||||
return hooks.HookOutput{Decision: "approve"}, nil
|
||||
}
|
||||
|
||||
// Build the notification content for speech
|
||||
// LogNotification logs notification events for debugging and monitoring.
|
||||
// This hook is triggered when Claude needs permission to use a tool or when
|
||||
// prompt input has been idle for 60+ seconds.
|
||||
//
|
||||
// Common use cases:
|
||||
// - Debug notification flow
|
||||
// - Monitor permission requests
|
||||
// - Track user idle states
|
||||
// - Audit notification patterns
|
||||
func LogNotification(ctx context.Context, input hooks.HookInput) (hooks.HookOutput, error) {
|
||||
var message string
|
||||
var notificationType string
|
||||
|
||||
// Check if we have the direct message format (newer)
|
||||
if input.Message != "" {
|
||||
message = input.Message
|
||||
notificationType = "direct"
|
||||
} else if input.Notification.Permission != "" {
|
||||
// Permission request (older format)
|
||||
message = fmt.Sprintf("Requesting permission to %s", input.Notification.Message)
|
||||
message = input.Notification.Message
|
||||
notificationType = "permission"
|
||||
} else if input.Notification.Message != "" {
|
||||
// Regular notification (older format)
|
||||
message = input.Notification.Message
|
||||
notificationType = "message"
|
||||
} else {
|
||||
// No valid notification content
|
||||
slog.Debug("notification hook triggered with no message content", "session_id", input.SessionID)
|
||||
return hooks.HookOutput{Decision: "approve"}, nil
|
||||
}
|
||||
|
||||
// Speak the notification using macOS say command
|
||||
if _, err := exec.LookPath("say"); err == nil {
|
||||
// Build more informative spoken message
|
||||
var spokenMessage string
|
||||
if input.Notification.Permission != "" {
|
||||
spokenMessage = fmt.Sprintf("Claude is requesting permission for %s: %s", input.Notification.Tool, message)
|
||||
} else if input.Notification.Tool != "" {
|
||||
spokenMessage = fmt.Sprintf("Claude notification from %s: %s", input.Notification.Tool, message)
|
||||
} else {
|
||||
spokenMessage = fmt.Sprintf("Claude notification: %s", message)
|
||||
}
|
||||
|
||||
// Sanitize for speech
|
||||
spokenMessage = strings.ReplaceAll(spokenMessage, "\n", " ")
|
||||
spokenMessage = strings.ReplaceAll(spokenMessage, "\"", "'")
|
||||
|
||||
// Truncate if too long
|
||||
if len(spokenMessage) > 200 {
|
||||
spokenMessage = spokenMessage[:197] + "..."
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, "say", "-v", "Samantha", spokenMessage)
|
||||
if err := cmd.Start(); err != nil {
|
||||
slog.Debug("failed to start say command", "error", err)
|
||||
} else {
|
||||
// Don't wait for completion to avoid blocking
|
||||
go func() {
|
||||
if err := cmd.Wait(); err != nil {
|
||||
slog.Debug("say command failed", "error", err)
|
||||
}
|
||||
}()
|
||||
slog.Info("spoke notification",
|
||||
"tool", input.Notification.Tool,
|
||||
"permission", input.Notification.Permission != "")
|
||||
}
|
||||
}
|
||||
slog.Info("notification received",
|
||||
"type", notificationType,
|
||||
"message", message,
|
||||
"tool", input.Notification.Tool,
|
||||
"permission", input.Notification.Permission != "",
|
||||
"session_id", input.SessionID,
|
||||
"timestamp", time.Now().Unix())
|
||||
|
||||
return hooks.HookOutput{Decision: "approve"}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue