|
Download hooks/README.md from ISLAM-PO/context-engineering-toolkit: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/ISLAM-PO/context-engineering-toolkit/resolve/main/hooks/README.md
- Command line
-
hf download hf://ISLAM-PO/context-engineering-toolkit/hooks/README.md
-
curl -L -o README.md https://huggingface.co/ISLAM-PO/context-engineering-toolkit/resolve/main/hooks/README.md
2.72 kB
| # Hooks Examples (Unit 5) | |
| This directory demonstrates hook patterns across Claude Code, Codex, OpenCode, and Pi. | |
| ## Hook Lifecycle | |
| ``` | |
| User prompt | |
| β | |
| ββ[UserPromptSubmit hook]ββΊ log / inject context | |
| βΌ | |
| Model reasoning | |
| β | |
| ββ[PreToolUse hook]ββΊ allow, deny, rewrite args | |
| βΌ | |
| Tool executes | |
| β | |
| ββ[PostToolUse hook]ββΊ log, analyze, post-process | |
| βΌ | |
| Model continues | |
| β | |
| ββ[Stop / SessionEnd hooks]ββΊ persist, notify, tear down | |
| ``` | |
| ## Claude Code β `.claude/settings.json` | |
| ```json | |
| { | |
| "hooks": { | |
| "PreToolUse": [ | |
| { | |
| "matcher": "Bash", | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "python .claude/hooks/guard_bash.py" | |
| } | |
| ] | |
| } | |
| ], | |
| "PostToolUse": [ | |
| { | |
| "matcher": "Edit|Write", | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "python .claude/hooks/run_linter.py" | |
| } | |
| ] | |
| } | |
| ], | |
| "Stop": [ | |
| { | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "python .claude/hooks/notify_done.py" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| } | |
| ``` | |
| ## OpenCode β `.opencode/plugins/activity-logger.ts` | |
| ```typescript | |
| export default { | |
| "tool.execute.before": async (input, output) => { | |
| console.log(`[HOOK] Before tool: ${input.tool}`); | |
| // Block dangerous commands | |
| if (input.tool === "bash" && input.args?.command?.includes("rm -rf")) { | |
| throw new Error("Blocked: dangerous command"); | |
| } | |
| }, | |
| "tool.execute.after": async (input, output) => { | |
| console.log(`[HOOK] After tool: ${input.tool} -> ${output.result?.slice(0, 100)}`); | |
| }, | |
| "event": async ({ event }) => { | |
| if (event.type === "session.idle") { | |
| console.log("[HOOK] Session ended"); | |
| } | |
| } | |
| }; | |
| ``` | |
| ## Guardrail Example β `.claude/hooks/guard_bash.py` | |
| ```python | |
| import json, sys | |
| payload = json.load(sys.stdin) | |
| command = payload.get("tool_input", {}).get("command", "") | |
| BLOCKED = ["rm -rf /", "rm -rf ~", "~/.ssh/", "chmod 777"] | |
| for pattern in BLOCKED: | |
| if pattern in command: | |
| print(json.dumps({"decision": "deny", "reason": f"Blocked: {pattern}"})) | |
| sys.exit(0) | |
| print(json.dumps({"decision": "allow"})) | |
| ``` | |
| ## Three Patterns | |
| 1. **Observability** β Log every tool call, prompt, and stop event to a dashboard. | |
| 2. **Guardrails** β Inspect commands before execution; deny unsafe actions. | |
| 3. **Automation** β Run linters, formatters, tests automatically after file edits. | |
| ## Agent Activity Dashboard | |
| A Gradio app that receives hook events over HTTP and visualizes tool calls, | |
| prompts, and sessions in real time. See `hooks/dashboard.py`. | |