-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_recall.py
More file actions
63 lines (51 loc) · 1.62 KB
/
auto_recall.py
File metadata and controls
63 lines (51 loc) · 1.62 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
#!/usr/bin/env python3
"""PreToolUse hook: auto-enrich Grep/Glob on memory/ with BM25 search results."""
import json
import sys
import os
def main():
event = json.load(sys.stdin)
tool = event.get("tool_name", "")
tool_input = event.get("tool_input", {})
# Only trigger on Grep/Glob targeting memory
if tool not in ("Grep", "Glob"):
return
path = tool_input.get("path", "")
pattern = tool_input.get("pattern", "")
if "memory" not in path and "memory" not in pattern:
return
# Extract search query from grep pattern or glob pattern
query = tool_input.get("pattern", "")
if not query:
return
# Clean regex artifacts for BM25 query
import re
query = re.sub(r'[\\.*+?^${}()|[\]]', ' ', query).strip()
if len(query) < 3:
return
# Run BM25 search
script = os.path.expanduser("~/.claude/scripts/memory_search.py")
if not os.path.exists(script):
return
import subprocess
try:
result = subprocess.run(
[sys.executable, script, query, "--limit", "5", "--json"],
capture_output=True, text=True, timeout=5
)
if result.returncode != 0:
return
hits = json.loads(result.stdout)
if not hits:
return
# Format compact results
lines = ["RECALL (auto-BM25):"]
for h in hits:
lines.append(f" [{h['score']:.2f}] {h['path']} — {h.get('description', '')[:60]}")
print(json.dumps({
"additionalContext": "\n".join(lines)
}))
except Exception:
return
if __name__ == "__main__":
main()