feat: Add PreTeleport and PostTeleport hook events

Add documentation and examples for new teleport hooks that allow users
to automate workflows when sessions transfer between web and CLI:

- PreTeleport: Runs before teleporting (stash work, sync state)
- PostTeleport: Runs after teleporting (pull changes, start dev servers)

This addresses the common workflow where users teleport from web to CLI
and need to run setup commands like `yarn dev:staging`.

Slack thread: https://anthropic.slack.com/archives/C096H3HP75G/p1765833639134099?thread_ts=1765833353.488839&cid=C096H3HP75G
This commit is contained in:
Claude
2025-12-15 21:25:55 +00:00
parent eb87245010
commit fee53699c3
4 changed files with 291 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
---
name: Hook Development
description: This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.
description: This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification, PreTeleport, PostTeleport). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.
version: 0.1.0
---
@@ -275,6 +275,108 @@ Execute before context compaction. Use to add critical information to preserve.
Execute when Claude sends notifications. Use to react to user notifications.
### PreTeleport
Execute before a session teleports (transfers from web to CLI or vice versa). Use to prepare the environment, sync state, or validate teleport conditions.
**Example:**
```json
{
"PreTeleport": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/pre-teleport.sh"
}
]
}
]
}
```
**Example script (pre-teleport.sh):**
```bash
#!/bin/bash
# Stash any uncommitted changes before teleporting
cd "$CLAUDE_PROJECT_DIR" || exit 0
if [ -d ".git" ] && [ -n "$(git status --porcelain)" ]; then
echo "📦 Stashing uncommitted changes before teleport..."
git stash push -m "pre-teleport-stash-$(date +%s)"
fi
```
**Input fields:**
- `source`: The origin of the teleport ("web" or "cli")
- `destination`: Where the session is going ("web" or "cli")
- `branch`: The git branch being teleported (if applicable)
**Use for:**
- Stashing uncommitted work before teleporting
- Syncing local state with remote
- Validating prerequisites before teleport
- Cleaning up temporary files
### PostTeleport
Execute after a session successfully teleports. Use to set up the environment in the new context, run dev servers, or restore state.
**Example:**
```json
{
"PostTeleport": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/post-teleport.sh"
}
]
}
]
}
```
**Example script (post-teleport.sh):**
```bash
#!/bin/bash
cd "$CLAUDE_PROJECT_DIR" || exit 0
# Pull latest changes from the teleported branch
if [ -d ".git" ]; then
echo "🔄 Pulling latest changes..."
git pull origin "$(git branch --show-current)" 2>/dev/null || true
fi
# Install dependencies if needed
if [ -f "package.json" ]; then
echo "📦 Installing dependencies..."
npm install --silent
fi
# Start dev server (example for common workflow)
if [ -f "package.json" ] && grep -q '"dev"' package.json; then
echo "🚀 Starting dev server..."
npm run dev &
fi
```
**Input fields:**
- `source`: Where the session came from ("web" or "cli")
- `destination`: The current context ("web" or "cli")
- `branch`: The git branch that was teleported
- `teleport_success`: Boolean indicating if teleport completed successfully
**Use for:**
- Pulling latest changes after teleporting
- Installing/updating dependencies
- Starting development servers
- Restoring stashed work
- Running project-specific setup scripts
## Hook Output Format
### Standard Output (All Hooks)
@@ -642,6 +744,8 @@ echo "$output" | jq .
| SessionEnd | Session ends | Cleanup, logging |
| PreCompact | Before compact | Preserve context |
| Notification | User notified | Logging, reactions |
| PreTeleport | Before teleport | Stash work, sync state |
| PostTeleport | After teleport | Setup env, start servers |
### Best Practices
@@ -679,6 +783,8 @@ Working examples in `examples/`:
- **`validate-write.sh`** - File write validation example
- **`validate-bash.sh`** - Bash command validation example
- **`load-context.sh`** - SessionStart context loading example
- **`pre-teleport.sh`** - PreTeleport state preservation example
- **`post-teleport.sh`** - PostTeleport environment setup example
### Utility Scripts