diff options
| author | Noah Zweben MacBook <noahz@anthropic.com> | 2025-11-20 11:47:24 -0800 |
|---|---|---|
| committer | Noah Zweben MacBook <noahz@anthropic.com> | 2025-11-20 11:47:24 -0800 |
| commit | 4ca561fb8532594e7a5faef945e85096fcec0616 (patch) | |
| tree | 0218e6f274b7fb9872d470da3b4dc1f6b28dd9e8 /plugins/hookify/hooks/userpromptsubmit.py | |
creating intital scaffolding for claude code pluginsnoahz/initial_scaffold
Diffstat (limited to 'plugins/hookify/hooks/userpromptsubmit.py')
| -rwxr-xr-x | plugins/hookify/hooks/userpromptsubmit.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/hookify/hooks/userpromptsubmit.py b/plugins/hookify/hooks/userpromptsubmit.py new file mode 100755 index 00000000..28ee51fd --- /dev/null +++ b/plugins/hookify/hooks/userpromptsubmit.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""UserPromptSubmit hook executor for hookify plugin. + +This script is called by Claude Code when user submits a prompt. +It reads .claude/hookify.*.local.md files and evaluates rules. +""" + +import os +import sys +import json + +# CRITICAL: Add plugin root to Python path for imports +PLUGIN_ROOT = os.environ.get('CLAUDE_PLUGIN_ROOT') +if PLUGIN_ROOT: + parent_dir = os.path.dirname(PLUGIN_ROOT) + if parent_dir not in sys.path: + sys.path.insert(0, parent_dir) + if PLUGIN_ROOT not in sys.path: + sys.path.insert(0, PLUGIN_ROOT) + +try: + from hookify.core.config_loader import load_rules + from hookify.core.rule_engine import RuleEngine +except ImportError as e: + error_msg = {"systemMessage": f"Hookify import error: {e}"} + print(json.dumps(error_msg), file=sys.stdout) + sys.exit(0) + + +def main(): + """Main entry point for UserPromptSubmit hook.""" + try: + # Read input from stdin + input_data = json.load(sys.stdin) + + # Load user prompt rules + rules = load_rules(event='prompt') + + # Evaluate rules + engine = RuleEngine() + result = engine.evaluate_rules(rules, input_data) + + # Always output JSON (even if empty) + print(json.dumps(result), file=sys.stdout) + + except Exception as e: + error_output = { + "systemMessage": f"Hookify error: {str(e)}" + } + print(json.dumps(error_output), file=sys.stdout) + + finally: + # ALWAYS exit 0 + sys.exit(0) + + +if __name__ == '__main__': + main() |
