Skip to content
Apothem
Contoh

Hook minimal

Contoh hook Apothem minimal — memicu sebuah pemeriksaan pada setiap penulisan berkas.

Contoh ini menunjukkan hook Apothem terkecil yang mungkin: sebuah hook PreToolUse yang dipicu sebelum setiap pemanggilan tool Write dan mencatat jalur target.

Skrip hook

# src/apothem/hooks/log_writes.py
import json, sys

data = json.load(sys.stdin)
path = data.get("tool_input", {}).get("file_path", "unknown")
print(f"[apothem] Writing to: {path}", file=sys.stderr)
sys.exit(0)  # always exit 0; block with the deny envelope below

Registrasi di settings.json

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "python src/apothem/hooks/log_writes.py"
          }
        ]
      }
    ]
  }
}

Menguji hook

Lakukan penulisan berkas apa pun di Claude Code dan amati keluaran stderr di panel jejak hook.

Memblokir penulisan

Untuk memblokir penulisan, cetak amplop penolakan ke stdout dan tetap keluar dengan 0, sebagaimana hook Apothem sendiri melakukannya:

print(json.dumps({"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "blocked by log_writes"}}))
sys.exit(0)

On this page