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 / hooks / userpromptsubmit.py
58 lines · 1.5 KBRaw
1#!/usr/bin/env python3
2"""UserPromptSubmit hook executor for hookify plugin.
3
4This script is called by Claude Code when user submits a prompt.
5It reads .claude/hookify.*.local.md files and evaluates rules.
6"""
7
8import os
9import sys
10import json
11
12# CRITICAL: Add plugin root to Python path for imports
13PLUGIN_ROOT = os.environ.get('CLAUDE_PLUGIN_ROOT')
14if PLUGIN_ROOT:
15 parent_dir = os.path.dirname(PLUGIN_ROOT)
16 if parent_dir not in sys.path:
17 sys.path.insert(0, parent_dir)
18 if PLUGIN_ROOT not in sys.path:
19 sys.path.insert(0, PLUGIN_ROOT)
20
21try:
22 from hookify.core.config_loader import load_rules
23 from hookify.core.rule_engine import RuleEngine
24except ImportError as e:
25 error_msg = {"systemMessage": f"Hookify import error: {e}"}
26 print(json.dumps(error_msg), file=sys.stdout)
27 sys.exit(0)
28
29
30def main():
31 """Main entry point for UserPromptSubmit hook."""
32 try:
33 # Read input from stdin
34 input_data = json.load(sys.stdin)
35
36 # Load user prompt rules
37 rules = load_rules(event='prompt')
38
39 # Evaluate rules
40 engine = RuleEngine()
41 result = engine.evaluate_rules(rules, input_data)
42
43 # Always output JSON (even if empty)
44 print(json.dumps(result), file=sys.stdout)
45
46 except Exception as e:
47 error_output = {
48 "systemMessage": f"Hookify error: {str(e)}"
49 }
50 print(json.dumps(error_output), file=sys.stdout)
51
52 finally:
53 # ALWAYS exit 0
54 sys.exit(0)
55
56
57if __name__ == '__main__':
58 main()