-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadvanced_hooks_example.rb
More file actions
192 lines (161 loc) · 5.68 KB
/
advanced_hooks_example.rb
File metadata and controls
192 lines (161 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'claude_agent_sdk'
require 'async'
require 'json'
# Example: Using typed hook inputs and outputs
# This demonstrates the new hook system with typed input/output classes:
# - PreToolUseHookInput, PostToolUseHookInput
# - PreToolUseHookSpecificOutput, PostToolUseHookSpecificOutput
# - UserPromptSubmitHookInput, UserPromptSubmitHookSpecificOutput
# - SyncHookJSONOutput for controlling hook behavior
puts "=== Advanced Hooks Example ==="
puts "Demonstrating typed hook inputs and outputs\n\n"
Async do
# Example 1: PreToolUse hook with typed output
puts "--- Example 1: PreToolUse Hook with Input Modification ---"
pre_tool_hook = lambda do |input, _tool_use_id, _context|
puts "PreToolUse hook triggered:"
puts " Tool: #{input.tool_name}"
puts " Session: #{input.session_id}"
# Example: Modify Bash commands to add safety prefix
if input.tool_name == 'Bash'
tool_input = input.tool_input || {}
original_command = tool_input[:command] || tool_input['command'] || ''
# Check for dangerous patterns
if original_command.match?(/rm\s+-rf|sudo\s+rm/)
# Create typed deny output
output = ClaudeAgentSDK::PreToolUseHookSpecificOutput.new(
permission_decision: 'deny',
permission_decision_reason: 'Destructive commands are not allowed'
)
return output.to_h
end
# Modify command to be safer (example: add echo prefix for demo)
if original_command.start_with?('echo')
# Allow echo commands as-is
output = ClaudeAgentSDK::PreToolUseHookSpecificOutput.new(
permission_decision: 'allow'
)
return output.to_h
end
end
# Default: allow
{}
end
# Example 2: PostToolUse hook with context addition
post_tool_hook = lambda do |input, _tool_use_id, _context|
puts "PostToolUse hook triggered:"
puts " Tool: #{input.tool_name}"
puts " Response length: #{input.tool_response&.to_s&.length || 0} chars"
# Add context after tool execution
output = ClaudeAgentSDK::PostToolUseHookSpecificOutput.new(
additional_context: "Tool '#{input.tool_name}' executed at #{Time.now}"
)
# Wrap in SyncHookJSONOutput for full control
sync_output = ClaudeAgentSDK::SyncHookJSONOutput.new(
continue: true,
suppress_output: false,
hook_specific_output: output
)
sync_output.to_h
end
# Example 3: UserPromptSubmit hook
prompt_hook = lambda do |input, _tool_use_id, _context|
puts "UserPromptSubmit hook triggered:"
prompt = input.prompt.to_s
puts " Prompt preview: #{prompt[0..50]}..."
# Add context to the prompt
output = ClaudeAgentSDK::UserPromptSubmitHookSpecificOutput.new(
additional_context: "User is working in: #{input.cwd}"
)
{ hookSpecificOutput: output.to_h }
end
# Configure hooks with timeout
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
allowed_tools: ['Bash'],
hooks: {
'PreToolUse' => [
ClaudeAgentSDK::HookMatcher.new(
matcher: 'Bash',
hooks: [pre_tool_hook],
timeout: 5 # 5 second timeout for hook execution
)
],
'PostToolUse' => [
ClaudeAgentSDK::HookMatcher.new(
matcher: 'Bash',
hooks: [post_tool_hook],
timeout: 5
)
],
'UserPromptSubmit' => [
ClaudeAgentSDK::HookMatcher.new(
hooks: [prompt_hook]
)
]
}
)
client = ClaudeAgentSDK::Client.new(options: options)
begin
puts "\nConnecting with advanced hooks..."
client.connect
puts "Connected!\n"
# Test 1: Safe command
puts "\n=== Test 1: Safe Echo Command ==="
client.query("Run the command: echo 'Hello from advanced hooks!'")
client.receive_response do |msg|
case msg
when ClaudeAgentSDK::AssistantMessage
msg.content.each do |block|
case block
when ClaudeAgentSDK::TextBlock
puts "Claude: #{block.text}"
when ClaudeAgentSDK::ToolUseBlock
puts "Tool: #{block.name}"
end
end
when ClaudeAgentSDK::ResultMessage
puts "\nCompleted in #{msg.num_turns} turns"
end
end
puts "\n" + "-" * 40
# Test 2: Blocked command
puts "\n=== Test 2: Blocked Dangerous Command ==="
client.query("Run the command: rm -rf /tmp/test")
client.receive_response do |msg|
case msg
when ClaudeAgentSDK::AssistantMessage
msg.content.each do |block|
puts "Claude: #{block.text}" if block.is_a?(ClaudeAgentSDK::TextBlock)
end
when ClaudeAgentSDK::SystemMessage
puts "System: #{msg.subtype}"
puts " #{msg.data[:message]}" if msg.data[:message]
when ClaudeAgentSDK::ResultMessage
puts "\nCompleted"
end
end
ensure
puts "\nDisconnecting..."
client.disconnect
puts "Done!"
end
end.wait
puts "\n" + "=" * 50 + "\n"
# Example 4: Using HookContext with signal
puts "\n--- Example 4: Hook with Context Signal ---"
puts "HookContext provides access to abort signals for long-running hooks\n"
hook_with_context = lambda do |input, tool_use_id, context|
# HookContext provides a cooperative cancellation signal.
# In a real scenario, you might check context.signal periodically
# during long-running operations to allow graceful cancellation
puts "Hook context signal available: #{!context.signal.nil?}"
# Return allow decision
ClaudeAgentSDK::PreToolUseHookSpecificOutput.new(
permission_decision: 'allow'
).to_h
end
puts "Hook with context signal defined (for demonstration)"
puts "In production, use context.signal to support cooperative cancellation"