| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- ---
- title: "Hook Reference"
- sidebarTitle: "Hook Reference"
- description: "Complete API reference for all Cline hook types, JSON schemas, and field documentation"
- ---
- This reference provides complete technical documentation for all hook types, their JSON schemas, input/output formats, and communication protocols.
- ## Hook Types
- Cline provides multiple hook types that let you tap into different stages of the AI workflow. They're organized into categories based on their trigger points and use cases.
- <Note>
- The hook names below are the exact file names you need to create. For example, to use the TaskStart hook, create a file named `TaskStart` (no file extension) in your hooks directory.
- </Note>
- Each hook receives base fields in addition to its specific data: `clineVersion`, `hookName`, `timestamp`, `taskId`, `workspaceRoots`, `userId`.
- ### Tool Execution Hooks
- These hooks intercept and validate tool operations before and after they execute. Use them to enforce policies, track changes, and learn from operations.
- #### `PreToolUse`
- Triggered immediately before Cline uses any tool (see the [Cline Tools Reference Guide](/cline-tools) for all available tools). Use it to block invalid operations, validate parameters, and enforce project policies before changes happen.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "PreToolUse",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "preToolUse": {
- "toolName": "string",
- "parameters": {}
- }
- }
- ```
- **Example Usage:**
- ```bash
- #!/usr/bin/env bash
- input=$(cat)
- # Block creating .js files in TypeScript projects
- tool_name=$(echo "$input" | jq -r '.preToolUse.toolName')
- if [[ "$tool_name" == "write_to_file" ]]; then
- file_path=$(echo "$input" | jq -r '.preToolUse.parameters.path')
- if [[ "$file_path" == *.js ]] && [[ -f "tsconfig.json" ]]; then
- echo '{"cancel": true, "errorMessage": "JavaScript files not allowed in TypeScript project"}'
- exit 0
- fi
- fi
- echo '{"cancel": false}'
- ```
- #### `PostToolUse`
- Triggered immediately after Cline uses any tool (see the [Cline Tools Reference Guide](/cline-tools) for all available tools). Use it to learn from results, track performance metrics, and build project knowledge based on operations performed.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "PostToolUse",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "postToolUse": {
- "toolName": "string",
- "parameters": {},
- "result": "string",
- "success": boolean,
- "executionTimeMs": number
- }
- }
- ```
- **Example Usage:**
- ```bash
- #!/usr/bin/env bash
- input=$(cat)
- # Log slow operations for performance monitoring
- execution_time=$(echo "$input" | jq -r '.postToolUse.executionTimeMs')
- tool_name=$(echo "$input" | jq -r '.postToolUse.toolName')
- if (( execution_time > 5000 )); then
- context="PERFORMANCE: Slow operation detected - $tool_name took ${execution_time}ms"
- echo "{\"cancel\": false, \"contextModification\": \"$context\"}"
- else
- echo '{"cancel": false}'
- fi
- ```
- ### User Interaction Hooks
- These hooks monitor and enhance user communication with Cline. Use them to validate input, inject context, and track interaction patterns.
- #### `UserPromptSubmit`
- Triggered when the user enters text into the prompt box and presses enter to start a new task, continue a completed task, or resume a cancelled task. Use it to validate input, inject context based on the prompt, and track interaction patterns.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "UserPromptSubmit",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "userPromptSubmit": {
- "prompt": "string",
- "attachments": ["string"]
- }
- }
- ```
- **Example Usage:**
- ```bash
- #!/usr/bin/env bash
- input=$(cat)
- # Inject coding standards context for certain keywords
- prompt=$(echo "$input" | jq -r '.userPromptSubmit.prompt')
- context=""
- if echo "$prompt" | grep -qi "component\|react"; then
- context="CODING_STANDARDS: Follow React functional component patterns with proper TypeScript types"
- elif echo "$prompt" | grep -qi "api\|endpoint"; then
- context="CODING_STANDARDS: Use consistent REST API patterns with proper error handling"
- fi
- if [[ -n "$context" ]]; then
- jq -n --arg ctx "$context" '{"cancel": false, "contextModification": $ctx}'
- else
- echo '{"cancel": false}'
- fi
- ```
- ### Task Lifecycle Hooks
- These hooks monitor and respond to task state changes from start to finish. Use them to track progress, restore state, and trigger workflows.
- #### `TaskStart`
- Triggered once at the beginning of a new task. Use it to detect project type, initialize tracking, and inject initial context that shapes how Cline approaches the work.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "TaskStart",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "taskStart": {
- "taskMetadata": {
- "taskId": "string",
- "ulid": "string",
- "initialTask": "string"
- }
- }
- }
- ```
- **Example Usage:**
- ```bash
- #!/usr/bin/env bash
- input=$(cat)
- # Detect project type and inject relevant context
- context=""
- if [[ -f "package.json" ]]; then
- if grep -q "react" package.json; then
- context="PROJECT_TYPE: React application detected. Follow component-based architecture."
- elif grep -q "express" package.json; then
- context="PROJECT_TYPE: Express.js API detected. Follow RESTful patterns."
- else
- context="PROJECT_TYPE: Node.js project detected."
- fi
- elif [[ -f "requirements.txt" ]] || [[ -f "pyproject.toml" ]]; then
- context="PROJECT_TYPE: Python project detected. Follow PEP 8 standards."
- elif [[ -f "Cargo.toml" ]]; then
- context="PROJECT_TYPE: Rust project detected. Follow Rust conventions."
- fi
- if [[ -n "$context" ]]; then
- jq -n --arg ctx "$context" '{"cancel": false, "contextModification": $ctx}'
- else
- echo '{"cancel": false}'
- fi
- ```
- #### `TaskResume`
- Triggered when the user resumes a task that has been cancelled or aborted. Use it to restore state, refresh context, and log resumption for analytics or external system notifications.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "TaskResume",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "taskResume": {
- "taskMetadata": {
- "taskId": "string",
- "ulid": "string"
- },
- "previousState": {
- "lastMessageTs": "string",
- "messageCount": "string",
- "conversationHistoryDeleted": "string"
- }
- }
- }
- ```
- #### `TaskCancel`
- Triggered when the user cancels a task or aborts a hook execution. Use it to cleanup resources, log cancellation details, and notify external systems about interrupted work.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "TaskCancel",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "taskCancel": {
- "taskMetadata": {
- "taskId": "string",
- "ulid": "string"
- }
- }
- }
- ```
- #### `TaskComplete`
- Triggered when Cline finishes its work and successfully executes the `attempt_completion` tool to finalize the task output. Use it to track completion metrics, generate reports, log task outcomes, and trigger completion workflows.
- **Input Fields:**
- ```json
- {
- "clineVersion": "string",
- "hookName": "TaskComplete",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "taskComplete": {
- "taskMetadata": {
- "taskId": "string",
- "ulid": "string"
- }
- }
- }
- ```
- **Example Usage:**
- ```bash
- #!/usr/bin/env bash
- input=$(cat)
- # Extract task metadata
- task_id=$(echo "$input" | jq -r '.taskComplete.taskMetadata.taskId // "unknown"')
- ulid=$(echo "$input" | jq -r '.taskComplete.taskMetadata.ulid // "unknown"')
- # Log completion
- completion_log="$HOME/.cline_completions/$(date +%Y-%m-%d).log"
- mkdir -p "$(dirname "$completion_log")"
- echo "$(date -Iseconds): Task $task_id completed (ULID: $ulid)" >> "$completion_log"
- # Provide context about completion
- context="TASK_COMPLETED: Task $task_id finished successfully. Completion logged."
- jq -n --arg ctx "$context" '{"cancel": false, "contextModification": $ctx}'
- ```
- ### System Events Hooks
- These hooks monitor internal Cline operations and system-level events. Use them to track context usage, log system behavior, and analyze performance patterns.
- ## JSON Communication Protocol
- Hooks receive JSON via stdin and return JSON via stdout.
- ### Input Format
- All hooks receive a JSON object through stdin with this base structure:
- ```json
- {
- "clineVersion": "string",
- "hookName": "string",
- "timestamp": "string",
- "taskId": "string",
- "workspaceRoots": ["string"],
- "userId": "string",
- "[hookSpecificField]": {
- // Hook-specific data structure
- }
- }
- ```
- ### Output Format
- Your hook script must output a JSON response as the final stdout content:
- ```json
- {
- "cancel": false,
- "contextModification": "WORKSPACE_RULES: Use TypeScript",
- "errorMessage": "Error details if blocking"
- }
- ```
- **Field Descriptions:**
- - **`cancel`** (required): Boolean controlling whether execution continues
- - `true`: Block the current action
- - `false`: Allow the action to proceed
- - **`contextModification`** (optional): String that gets injected into the conversation
- - Affects future AI decisions, not the current one
- - Use clear prefixes like `WORKSPACE_RULES:`, `PERFORMANCE:`, `SECURITY:` for categorization
- - Maximum length: 50KB
- - **`errorMessage`** (optional): String shown to user when `cancel` is `true`
- - Only displayed when blocking an action
- - Should explain why the action was blocked
- ### Logging During Execution
- Your hook script can output logging or diagnostic information to stdout during execution, as long as the JSON response is the last thing written:
- ```bash
- #!/usr/bin/env bash
- echo "Processing hook..." # This is fine
- echo "Tool: $tool_name" # This is also fine
- # The JSON must be last:
- echo '{"cancel": false}'
- ```
- Cline will parse only the final JSON object from stdout.
- ### Error Handling
- Hook execution errors don't prevent task execution - only returning `"cancel": true` can halt a task. All other errors are treated as hook failures, not reasons to abort the task.
- **Hook Status Display:**
- - **Completed** (grey): Hook executed successfully, regardless of whether it returned `"cancel": false` or no JSON output
- - **Failed** (red): Hook exited with non-zero status, output invalid JSON, or timed out. The UI displays the error details (e.g., exit code number)
- - **Aborted** (red): Hook returned `"cancel": true`, halting the task. User must manually resume the task to continue
- **Important:** Even when a hook fails (non-zero exit, invalid JSON, timeout), Cline continues with the task. Only `"cancel": true` stops execution.
- ### Context Modification Timing
- Context injection affects future decisions, not current ones. When a hook runs:
- 1. The AI has already decided what to do
- 2. The hook can block or allow it
- 3. Any context gets added to the conversation
- 4. The next AI request sees that context
- This means:
- - **PreToolUse hooks**: Use for blocking bad actions + injecting context for next decision
- - **PostToolUse hooks**: Use for learning from completed actions
- ### Helpful Tip: String Escaping in JSON
- When your hook needs to include strings containing unescaped quote characters (`"`) in JSON output, use jq's `--arg` flag for proper escaping:
- ```bash
- #!/usr/bin/env bash
- # When $output contains unescaped quote characters (")...
- output='{"foo":"bar"}'
- # Use the --arg flag for automatic string escaping
- jq -n --arg ctx "$output" '{cancel: false, contextModification: $ctx}'
- # This will result in:
- # {
- # "cancel": false,
- # "contextModification": "{\"foo\":\"bar\"}"
- # }
- ```
- The `--arg` flag automatically escapes special characters, preventing JSON parsing errors when your context modification includes complex strings or nested JSON structures.
- ## Hook Execution Environment
- ### Execution Context
- Hooks are executable scripts that run with the same permissions as VS Code. They have unrestricted access to:
- - The entire filesystem (any file the user can access)
- - All environment variables
- - System commands and tools
- - Network resources
- Hooks can perform any operation the user could perform in a terminal, including reading and writing files outside the workspace, making network requests, and executing system commands.
- ### Security Considerations
- <Warning>
- Hooks run with the same permissions as VS Code. They can access all workspace files and environment variables. Review hooks from untrusted sources before enabling them.
- </Warning>
- ### Performance Guidelines
- Hooks have a 30 second timeout. As long as your hook completes within this time, it can perform any operations needed, including network calls or heavy computations.
- ### Hook Discovery
- Cline searches for hooks in this order:
- 1. Project-specific: `.clinerules/hooks/` in workspace root
- 2. User-global: `~/Documents/Cline/Hooks/`
- Project-specific hooks override global hooks with the same name.
|