-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatcher_pre.py
More file actions
137 lines (116 loc) · 3.61 KB
/
dispatcher_pre.py
File metadata and controls
137 lines (116 loc) · 3.61 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
#!/usr/bin/env python3
"""Single dispatcher for all PreToolUse hooks (project + global).
Routes by tool_name so only relevant hooks run. Replaces ~15 separate python3 spawns.
"""
import importlib.util
import json
import sys
import io
from pathlib import Path
HOOKS_DIR = Path(__file__).parent
# tool_name → list of hook scripts
ROUTING = {
"Bash": [
"guard_safety.py", "auto_pre_publish.py", "unicode_grep_warn.py",
"vps_setup_guard.py",
],
"Edit": [
"guard_safety.py", "file_lock.py", "pre_edit_impact.py",
"skill_disable_not_delete.py", "memory_conflict_guard.py",
],
"Write": [
"guard_safety.py", "file_lock.py", "skill_disable_not_delete.py",
"memory_conflict_guard.py",
],
"Grep": [
"unicode_grep_warn.py", "auto_recall.py",
],
"Glob": [
"auto_recall.py",
],
"Agent": [
"agent_cascade_guard.py", "agent_count_guard.py",
"agent_simplicity_guard.py", "agent_tracker.py",
],
"Skill": [
"skill_enable_hook.py",
],
"mcp__plugin_telegram_telegram__reply": [
"tg_qr_document.py",
],
}
# Hooks that check tool_input content, not just tool_name — run on specific tools only
TOOL_INPUT_HOOKS = {
"api_key_lookup.py": ["Bash", "Grep", "Read"],
}
def load_and_run(script_name, event_data):
"""Import and run a hook's main(), capturing stdout."""
path = HOOKS_DIR / script_name
if not path.exists():
return None
spec = importlib.util.spec_from_file_location(script_name.replace(".py", ""), path)
if not spec or not spec.loader:
return None
mod = importlib.util.module_from_spec(spec)
old_stdin = sys.stdin
old_stdout = sys.stdout
sys.stdin = io.StringIO(json.dumps(event_data))
captured = io.StringIO()
sys.stdout = captured
try:
spec.loader.exec_module(mod)
if hasattr(mod, "main"):
mod.main()
except SystemExit:
pass
except Exception:
pass
finally:
sys.stdin = old_stdin
sys.stdout = old_stdout
output = captured.getvalue().strip()
if output:
try:
return json.loads(output)
except json.JSONDecodeError:
pass
return None
def main():
try:
event = json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
print("{}")
return
tool_name = event.get("tool_name", "")
# Collect hooks to run
hooks_to_run = []
if tool_name in ROUTING:
hooks_to_run.extend(ROUTING[tool_name])
# Add tool-input hooks that match this tool
for script, tools in TOOL_INPUT_HOOKS.items():
if tool_name in tools:
hooks_to_run.append(script)
if not hooks_to_run:
print("{}")
return
# Run hooks, merge results — stop on block/deny
merged = {}
for script in hooks_to_run:
result = load_and_run(script, event)
if result:
# If any hook blocks, return immediately
decision = result.get("decision", "")
if decision in ("block", "deny"):
print(json.dumps(result))
return
if "additionalContext" in result:
if "additionalContext" in merged:
merged["additionalContext"] += "\n" + result["additionalContext"]
else:
merged["additionalContext"] = result["additionalContext"]
for k, v in result.items():
if k != "additionalContext":
merged[k] = v
print(json.dumps(merged) if merged else "{}")
if __name__ == "__main__":
main()