spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic ยท github.com/anthropics

claude-code / plugins / hookify / README.md
340 lines ยท 7.5 KBPreviewCodeRaw
1# Hookify Plugin
2
3Easily create custom hooks to prevent unwanted behaviors by analyzing conversation patterns or from explicit instructions.
4
5## Overview
6
7The hookify plugin makes it simple to create hooks without editing complex `hooks.json` files. Instead, you create lightweight markdown configuration files that define patterns to watch for and messages to show when those patterns match.
8
9**Key features:**
10- ๐ŸŽฏ Analyze conversations to find unwanted behaviors automatically
11- ๐Ÿ“ Simple markdown configuration files with YAML frontmatter
12- ๐Ÿ” Regex pattern matching for powerful rules
13- ๐Ÿš€ No coding required - just describe the behavior
14- ๐Ÿ”„ Easy enable/disable without restarting
15
16## Quick Start
17
18### 1. Create Your First Rule
19
20```bash
21/hookify Warn me when I use rm -rf commands
22```
23
24This analyzes your request and creates `.claude/hookify.warn-rm.local.md`.
25
26### 2. Test It Immediately
27
28**No restart needed!** Rules take effect on the very next tool use.
29
30Ask Claude to run a command that should trigger the rule:
31```
32Run rm -rf /tmp/test
33```
34
35You should see the warning message immediately!
36
37## Usage
38
39### Main Command: /hookify
40
41**With arguments:**
42```
43/hookify Don't use console.log in TypeScript files
44```
45Creates a rule from your explicit instructions.
46
47**Without arguments:**
48```
49/hookify
50```
51Analyzes recent conversation to find behaviors you've corrected or been frustrated by.
52
53### Helper Commands
54
55**List all rules:**
56```
57/hookify:list
58```
59
60**Configure rules interactively:**
61```
62/hookify:configure
63```
64Enable/disable existing rules through an interactive interface.
65
66**Get help:**
67```
68/hookify:help
69```
70
71## Rule Configuration Format
72
73### Simple Rule (Single Pattern)
74
75`.claude/hookify.dangerous-rm.local.md`:
76```markdown
77---
78name: block-dangerous-rm
79enabled: true
80event: bash
81pattern: rm\s+-rf
82action: block
83---
84
85โš ๏ธ **Dangerous rm command detected!**
86
87This command could delete important files. Please:
88- Verify the path is correct
89- Consider using a safer approach
90- Make sure you have backups
91```
92
93**Action field:**
94- `warn`: Shows warning but allows operation (default)
95- `block`: Prevents operation from executing (PreToolUse) or stops session (Stop events)
96
97### Advanced Rule (Multiple Conditions)
98
99`.claude/hookify.sensitive-files.local.md`:
100```markdown
101---
102name: warn-sensitive-files
103enabled: true
104event: file
105action: warn
106conditions:
107 - field: file_path
108 operator: regex_match
109 pattern: \.env$|credentials|secrets
110 - field: new_text
111 operator: contains
112 pattern: KEY
113---
114
115๐Ÿ” **Sensitive file edit detected!**
116
117Ensure credentials are not hardcoded and file is in .gitignore.
118```
119
120**All conditions must match** for the rule to trigger.
121
122## Event Types
123
124- **`bash`**: Triggers on Bash tool commands
125- **`file`**: Triggers on Edit, Write, MultiEdit tools
126- **`stop`**: Triggers when Claude wants to stop (for completion checks)
127- **`prompt`**: Triggers on user prompt submission
128- **`all`**: Triggers on all events
129
130## Pattern Syntax
131
132Use Python regex syntax:
133
134| Pattern | Matches | Example |
135|---------|---------|---------|
136| `rm\s+-rf` | rm -rf | rm -rf /tmp |
137| `console\.log\(` | console.log( | console.log("test") |
138| `(eval\|exec)\(` | eval( or exec( | eval("code") |
139| `\.env$` | files ending in .env | .env, .env.local |
140| `chmod\s+777` | chmod 777 | chmod 777 file.txt |
141
142**Tips:**
143- Use `\s` for whitespace
144- Escape special chars: `\.` for literal dot
145- Use `|` for OR: `(foo|bar)`
146- Use `.*` to match anything
147- Set `action: block` for dangerous operations
148- Set `action: warn` (or omit) for informational warnings
149
150## Examples
151
152### Example 1: Block Dangerous Commands
153
154```markdown
155---
156name: block-destructive-ops
157enabled: true
158event: bash
159pattern: rm\s+-rf|dd\s+if=|mkfs|format
160action: block
161---
162
163๐Ÿ›‘ **Destructive operation detected!**
164
165This command can cause data loss. Operation blocked for safety.
166Please verify the exact path and use a safer approach.
167```
168
169**This rule blocks the operation** - Claude will not be allowed to execute these commands.
170
171### Example 2: Warn About Debug Code
172
173```markdown
174---
175name: warn-debug-code
176enabled: true
177event: file
178pattern: console\.log\(|debugger;|print\(
179action: warn
180---
181
182๐Ÿ› **Debug code detected**
183
184Remember to remove debugging statements before committing.
185```
186
187**This rule warns but allows** - Claude sees the message but can still proceed.
188
189### Example 3: Require Tests Before Stopping
190
191```markdown
192---
193name: require-tests-run
194enabled: false
195event: stop
196action: block
197conditions:
198 - field: transcript
199 operator: not_contains
200 pattern: npm test|pytest|cargo test
201---
202
203**Tests not detected in transcript!**
204
205Before stopping, please run tests to verify your changes work correctly.
206```
207
208**This blocks Claude from stopping** if no test commands appear in the session transcript. Enable only when you want strict enforcement.
209
210## Advanced Usage
211
212### Multiple Conditions
213
214Check multiple fields simultaneously:
215
216```markdown
217---
218name: api-key-in-typescript
219enabled: true
220event: file
221conditions:
222 - field: file_path
223 operator: regex_match
224 pattern: \.tsx?$
225 - field: new_text
226 operator: regex_match
227 pattern: (API_KEY|SECRET|TOKEN)\s*=\s*["']
228---
229
230๐Ÿ” **Hardcoded credential in TypeScript!**
231
232Use environment variables instead of hardcoded values.
233```
234
235### Operators Reference
236
237- `regex_match`: Pattern must match (most common)
238- `contains`: String must contain pattern
239- `equals`: Exact string match
240- `not_contains`: String must NOT contain pattern
241- `starts_with`: String starts with pattern
242- `ends_with`: String ends with pattern
243
244### Field Reference
245
246**For bash events:**
247- `command`: The bash command string
248
249**For file events:**
250- `file_path`: Path to file being edited
251- `new_text`: New content being added (Edit, Write)
252- `old_text`: Old content being replaced (Edit only)
253- `content`: File content (Write only)
254
255**For prompt events:**
256- `user_prompt`: The user's submitted prompt text
257
258**For stop events:**
259- Use general matching on session state
260
261## Management
262
263### Enable/Disable Rules
264
265**Temporarily disable:**
266Edit the `.local.md` file and set `enabled: false`
267
268**Re-enable:**
269Set `enabled: true`
270
271**Or use interactive tool:**
272```
273/hookify:configure
274```
275
276### Delete Rules
277
278Simply delete the `.local.md` file:
279```bash
280rm .claude/hookify.my-rule.local.md
281```
282
283### View All Rules
284
285```
286/hookify:list
287```
288
289## Installation
290
291This plugin is part of the Claude Code Marketplace. It should be auto-discovered when the marketplace is installed.
292
293**Manual testing:**
294```bash
295cc --plugin-dir /path/to/hookify
296```
297
298## Requirements
299
300- Python 3.7+
301- No external dependencies (uses stdlib only)
302
303## Troubleshooting
304
305**Rule not triggering:**
3061. Check rule file exists in `.claude/` directory (in project root, not plugin directory)
3072. Verify `enabled: true` in frontmatter
3083. Test regex pattern separately
3094. Rules should work immediately - no restart needed
3105. Try `/hookify:list` to see if rule is loaded
311
312**Import errors:**
313- Ensure Python 3 is available: `python3 --version`
314- Check hookify plugin is installed
315
316**Pattern not matching:**
317- Test regex: `python3 -c "import re; print(re.search(r'pattern', 'text'))"`
318- Use unquoted patterns in YAML to avoid escaping issues
319- Start simple, then add complexity
320
321**Hook seems slow:**
322- Keep patterns simple (avoid complex regex)
323- Use specific event types (bash, file) instead of "all"
324- Limit number of active rules
325
326## Contributing
327
328Found a useful rule pattern? Consider sharing example files via PR!
329
330## Future Enhancements
331
332- Severity levels (error/warning/info distinctions)
333- Rule templates library
334- Interactive pattern builder
335- Hook testing utilities
336- JSON format support (in addition to markdown)
337
338## License
339
340MIT License