File size: 2,544 Bytes
c3a3710 | 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 | #!/usr/bin/env python3
"""
Claude Code PreToolUse hook — MnemoCore context injection
==========================================================
On the FIRST tool call of a session, queries MnemoCore for recent context
and writes it to a temporary file that is referenced from CLAUDE.md.
This gives Claude Code automatic memory of previous sessions WITHOUT
requiring any explicit user commands.
Configure in ~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": ".*",
"hooks": [
{
"type": "command",
"command": "python /path/to/pre_session_inject.py"
}
]
}
]
}
}
The hook must exit 0 (allow) or 2 (block with message).
It never blocks — silently degrades if MnemoCore is offline.
"""
import json
import os
import subprocess
import sys
import tempfile
from pathlib import Path
BRIDGE = Path(__file__).resolve().parents[2] / "mnemo_bridge.py"
CONTEXT_DIR = Path(os.getenv("MNEMOCORE_CONTEXT_DIR", Path.home() / ".claude" / "mnemo_context"))
DONE_FILE = CONTEXT_DIR / ".session_injected"
def main() -> int:
try:
raw = sys.stdin.read()
data = json.loads(raw) if raw.strip() else {}
except json.JSONDecodeError:
return 0
session_id = data.get("session_id", "")
# Only inject once per session
done_marker = CONTEXT_DIR / f".injected_{session_id[:16]}"
if done_marker.exists():
return 0
CONTEXT_DIR.mkdir(parents=True, exist_ok=True)
# Query MnemoCore for context
try:
result = subprocess.run(
[sys.executable, str(BRIDGE), "context", "--top-k", "8"],
capture_output=True,
text=True,
timeout=5,
env={**os.environ},
)
context_md = result.stdout.strip()
except (subprocess.TimeoutExpired, FileNotFoundError):
context_md = ""
if context_md:
context_file = CONTEXT_DIR / "latest_context.md"
context_file.write_text(context_md, encoding="utf-8")
# Mark session as injected
done_marker.touch()
# Output context as additional system information if available
if context_md:
# Claude Code hooks can output JSON to inject content
output = {
"type": "system_reminder",
"content": context_md,
}
print(json.dumps(output))
return 0
if __name__ == "__main__":
sys.exit(main())
|