hooks
npx machina-cli add skill parcadei/Continuous-Claude-v3/hooks --openclawFiles (1)
SKILL.md
1.3 KB
Hook Development Rules
When working with files in .claude/hooks/:
Pattern
Shell wrapper (.sh) → TypeScript (.ts) via npx tsx
Shell Wrapper Template
#!/bin/bash
set -e
cd "$CLAUDE_PROJECT_DIR/.claude/hooks"
cat | npx tsx <handler>.ts
TypeScript Handler Pattern
interface HookInput {
// Event-specific fields
}
async function main() {
const input: HookInput = JSON.parse(await readStdin());
// Process input
const output = {
result: 'continue', // or 'block'
message: 'Optional system reminder'
};
console.log(JSON.stringify(output));
}
Hook Events
- PreToolUse - Before tool execution (can block)
- PostToolUse - After tool execution
- UserPromptSubmit - Before processing user prompt
- PreCompact - Before context compaction
- SessionStart - On session start/resume/compact
- Stop - When agent finishes
Testing
Test hooks manually:
echo '{"type": "resume"}' | .claude/hooks/session-start-continuity.sh
Registration
Add hooks to .claude/settings.json:
{
"hooks": {
"EventName": [{
"matcher": ["pattern"], // Optional
"hooks": [{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/hook.sh"
}]
}]
}
}
Source
git clone https://github.com/parcadei/Continuous-Claude-v3/blob/main/.claude/skills/hooks/SKILL.mdView on GitHub Overview
Hooks extend Claude by intercepting events via .claude/hooks. A shell wrapper (.sh) calls a TypeScript handler (via npx tsx) to process input and emit an outcome. Hooks respond to events like PreToolUse, PostToolUse, and SessionStart to influence behavior.
How This Skill Works
Create a shell wrapper that pipes input into a TS handler using npx tsx. The TS handler defines a HookInput interface, reads stdin for event data, and outputs JSON with { result: 'continue'|'block', message: string }. Hooks are registered to specific events in .claude/settings.json.
When to Use It
- Prevent or alter tool usage right before execution (PreToolUse).
- Handle output or decisions after a tool runs (PostToolUse).
- Inspect and possibly modify user prompts before processing (UserPromptSubmit).
- Manage context size by acting before compaction (PreCompact).
- Initialize, resume, or wrap up a session (SessionStart / Stop).
Quick Start
- Step 1: Create a shell wrapper in .claude/hooks using the standard template that redirects input to npx tsx <handler>.ts.
- Step 2: Implement a TypeScript handler that reads input with readStdin(), defines HookInput, and outputs { result: 'continue'|'block', message } via console.log(JSON.stringify(output)).
- Step 3: Register the hook in .claude/settings.json under 'hooks' with the EventName and the command path to your hook.sh.
Best Practices
- Return deterministic JSON with 'continue' or 'block' and a clear 'message'.
- Keep the handler focused and idempotent; avoid side effects.
- Test hooks individually with the provided test pattern.
- Register hooks with precise matcher patterns in .claude/settings.json.
- Document the event name and expected input/output for future maintenance.
Example Use Cases
- PreToolUse: block a potentially dangerous tool call based on input.
- PostToolUse: log tool output and append a note for auditing.
- UserPromptSubmit: sanitize or augment the user prompt before processing.
- PreCompact: prune irrelevant tokens to reduce context before merging.
- SessionStart: initialize per-session state like counters or flags.
Frequently Asked Questions
Add this skill to your agents