claude_hooks 0.2.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +92 -0
  3. data/README.md +215 -357
  4. data/claude_hooks.gemspec +2 -2
  5. data/docs/API/COMMON.md +83 -0
  6. data/docs/API/NOTIFICATION.md +32 -0
  7. data/docs/API/POST_TOOL_USE.md +45 -0
  8. data/docs/API/PRE_COMPACT.md +39 -0
  9. data/docs/API/PRE_TOOL_USE.md +110 -0
  10. data/docs/API/SESSION_END.md +100 -0
  11. data/docs/API/SESSION_START.md +40 -0
  12. data/docs/API/STOP.md +47 -0
  13. data/docs/API/SUBAGENT_STOP.md +47 -0
  14. data/docs/API/USER_PROMPT_SUBMIT.md +47 -0
  15. data/docs/OUTPUT_MIGRATION_GUIDE.md +228 -0
  16. data/example_dotclaude/hooks/entrypoints/session_end.rb +35 -0
  17. data/example_dotclaude/hooks/entrypoints/user_prompt_submit.rb +17 -12
  18. data/example_dotclaude/hooks/handlers/session_end/cleanup_handler.rb +55 -0
  19. data/example_dotclaude/hooks/handlers/session_end/log_session_stats.rb +64 -0
  20. data/example_dotclaude/hooks/handlers/user_prompt_submit/append_rules.rb +1 -1
  21. data/example_dotclaude/hooks/handlers/user_prompt_submit/log_user_prompt.rb +2 -2
  22. data/example_dotclaude/settings.json +11 -0
  23. data/lib/claude_hooks/base.rb +16 -24
  24. data/lib/claude_hooks/cli.rb +75 -1
  25. data/lib/claude_hooks/logger.rb +0 -1
  26. data/lib/claude_hooks/output/base.rb +152 -0
  27. data/lib/claude_hooks/output/notification.rb +22 -0
  28. data/lib/claude_hooks/output/post_tool_use.rb +68 -0
  29. data/lib/claude_hooks/output/pre_compact.rb +20 -0
  30. data/lib/claude_hooks/output/pre_tool_use.rb +97 -0
  31. data/lib/claude_hooks/output/session_end.rb +24 -0
  32. data/lib/claude_hooks/output/session_start.rb +49 -0
  33. data/lib/claude_hooks/output/stop.rb +76 -0
  34. data/lib/claude_hooks/output/subagent_stop.rb +14 -0
  35. data/lib/claude_hooks/output/user_prompt_submit.rb +71 -0
  36. data/lib/claude_hooks/post_tool_use.rb +6 -12
  37. data/lib/claude_hooks/pre_tool_use.rb +0 -37
  38. data/lib/claude_hooks/session_end.rb +43 -0
  39. data/lib/claude_hooks/session_start.rb +0 -23
  40. data/lib/claude_hooks/stop.rb +0 -25
  41. data/lib/claude_hooks/user_prompt_submit.rb +0 -26
  42. data/lib/claude_hooks/version.rb +1 -1
  43. data/lib/claude_hooks.rb +15 -0
  44. metadata +33 -8
  45. /data/{WHY.md → docs/WHY.md} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08b8b23bcfdeed5cf98e3477b6339f29488d6e547fd2fb10ef40947bbc3d45dc'
4
- data.tar.gz: 20d0f599bac9dcde566829dabba1d1f3f13756181ed4ea7bbe4cfa1c0a95838a
3
+ metadata.gz: bbd6349008b693bddb770b79ac5883a746c826a800ed7141f4de466bd88025e0
4
+ data.tar.gz: b7a724ed1655ecb7c4af3393619e5dd800b0b59a08721c6d4b7279c4d31a185d
5
5
  SHA512:
6
- metadata.gz: 7cd6ecea85c72ce39e77613f68c3dc9f689aca7611dd499482e20538bbb456e2c6b4c23bc791372173fd4b955e8d4211c3c8c29e0e97957812916f901327ce22
7
- data.tar.gz: 93b6032c1c29c11081419d0415dae8ac5cd2494fa558af1637c081ce11d256403c1b26c49740c9aa8294c385aa311b12da2b3e5a6949e88a4a5f892b68abcf54
6
+ metadata.gz: a1596119ed1ee346ed133a76a0991cc64f8be594905e96e6b056e6e5187be91ca43ffd4c28c3e822725b5ee4bdb0bdbc03aa0cea38c06a5813b94c3a381ff0c3
7
+ data.tar.gz: 6063e35f39ffabd8eaf78e9deccd009e56f9103c86f69dedb6a2ed66b97cc9a8606d0aafb6ee2467d6fa29536031f4d0d671ff20e466c672009d16e2c2a962ea
data/CHANGELOG.md CHANGED
@@ -5,6 +5,98 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.0.0] - 2025-08-27
9
+
10
+ > [!WARNING]
11
+ > These changes are not backward compatible (hence the version bump to 1.0.0), the API has changed too much.
12
+
13
+ ### Changed
14
+
15
+ #### Revamped documentation
16
+
17
+ - **New documentation structure**:
18
+ - **API Reference**: Comprehensive API reference for all hook types
19
+ - **Common Helpers**: Shared helpers for all hook types
20
+ - **Output Helpers**: Helpers for working with the output state
21
+ - **Hook Exit Codes**: Exit codes for each hook type
22
+ - **Hook Types**: Overview of all hook types and their purpose
23
+
24
+ #### Added support for SessionEnd
25
+ Handles the new `SessionEnd` hook type.
26
+
27
+ #### Handles new field for PostToolUse
28
+ `additionalContext` field is now available in the `hookSpecificOutput` field.
29
+
30
+ #### Output Objects System
31
+ New output handling with intelligent exit code management.
32
+
33
+ - **Automatic Exit Code Selection**: Output objects automatically choose the correct exit code (0, 1, 2) based on hook type and the state of the hook
34
+ - - **Enhanced Base Class**: All hook instances now automatically get an `@output` attribute with a `output` accessor. They can still access raw `output_data`
35
+ - **Helper Methods**: Access to hook-specific data via methods like `output.blocked?`, `output.allowed?`, `output.should_ask_permission?`
36
+ - **Automatic Stream Selection**: Output objects automatically route to `STDOUT` or `STDERR` based on exit code and hook type
37
+ - **One-Line Output Execution**: New `output_and_exit` method handles JSON serialization, stream selection, and exit code in one call
38
+ - **Object-Based Merging**: New `ClaudeHooks::Output::*.merge()` methods for cleaner multi-hook merging
39
+ - **Comprehensive Output Classes**: Full coverage for all 8 hook types:
40
+ - `ClaudeHooks::Output::UserPromptSubmit` - Provides `blocked?`, `reason`, `additional_context`
41
+ - `ClaudeHooks::Output::PreToolUse` - Provides `allowed?`, `denied?`, `should_ask_permission?`
42
+ - `ClaudeHooks::Output::PostToolUse` - Provides `blocked?`, `reason`
43
+ - `ClaudeHooks::Output::Stop` - Provides `should_continue?`, `continue_instructions` (inverted logic)
44
+ - `ClaudeHooks::Output::SubagentStop` - Inherits Stop behavior
45
+ - `ClaudeHooks::Output::Notification` - Basic output handling
46
+ - `ClaudeHooks::Output::SessionStart` - Provides `additional_context`
47
+ - `ClaudeHooks::Output::PreCompact` - Basic output handling
48
+
49
+ ##### Migration Guide
50
+
51
+ **Before (Old Pattern):**
52
+ ```ruby
53
+ #!/usr/bin/env ruby
54
+ require 'claude_hooks'
55
+
56
+ begin
57
+ input_data = JSON.parse(STDIN.read)
58
+ hook = MyHook.new(input_data)
59
+ result = hook.call
60
+
61
+ # Manual stream and exit code selection
62
+ if result['continue'] != false
63
+ if result.dig('hookSpecificOutput', 'permissionDecision') == 'deny'
64
+ STDERR.puts hook.stringify_output
65
+ exit 1
66
+ elsif result.dig('hookSpecificOutput', 'permissionDecision') == 'ask'
67
+ STDERR.puts hook.stringify_output
68
+ exit 2
69
+ else
70
+ puts hook.stringify_output
71
+ exit 0
72
+ end
73
+ else # Continue == false
74
+ STDERR.puts hook.stringify_output
75
+ exit 2
76
+ end
77
+ rescue StandardError => e
78
+ # Error handling...
79
+ end
80
+ ```
81
+
82
+ **After (New Pattern):**
83
+ ```ruby
84
+ #!/usr/bin/env ruby
85
+ require 'claude_hooks'
86
+
87
+ # Just 3 lines for most cases!
88
+ begin
89
+ input_data = JSON.parse(STDIN.read)
90
+ hook = MyHook.new(input_data)
91
+ hook.call
92
+
93
+ # Handles everything automatically!
94
+ hook.output_and_exit
95
+ rescue StandardError => e
96
+ # Error handling...
97
+ end
98
+ ```
99
+
8
100
  ## [0.2.1] - 2025-08-21
9
101
 
10
102
  ### Fixed