|
| 1 | +#!/usr/bin/env bash |
| 2 | +# rtk-hook-version: 3 |
| 3 | +# RTK Claude Code hook — rewrites commands to use rtk for token savings. |
| 4 | +# Requires: rtk >= 0.23.0, jq |
| 5 | +# |
| 6 | +# This is a thin delegating hook: all rewrite logic lives in `rtk rewrite`, |
| 7 | +# which is the single source of truth (src/discover/registry.rs). |
| 8 | +# To add or change rewrite rules, edit the Rust registry — not this file. |
| 9 | +# |
| 10 | +# Exit code protocol for `rtk rewrite`: |
| 11 | +# 0 + stdout Rewrite found, no deny/ask rule matched → auto-allow |
| 12 | +# 1 No RTK equivalent → pass through unchanged |
| 13 | +# 2 Deny rule matched → pass through (Claude Code native deny handles it) |
| 14 | +# 3 + stdout Ask rule matched → rewrite but let Claude Code prompt the user |
| 15 | + |
| 16 | +if ! command -v jq &>/dev/null; then |
| 17 | + echo "[rtk] WARNING: jq is not installed. Hook cannot rewrite commands. Install jq: https://jqlang.github.io/jq/download/" >&2 |
| 18 | + exit 0 |
| 19 | +fi |
| 20 | + |
| 21 | +if ! command -v rtk &>/dev/null; then |
| 22 | + echo "[rtk] WARNING: rtk is not installed or not in PATH. Hook cannot rewrite commands. Install: https://github.com/rtk-ai/rtk#installation" >&2 |
| 23 | + exit 0 |
| 24 | +fi |
| 25 | + |
| 26 | +# Version guard: rtk rewrite was added in 0.23.0. |
| 27 | +# Older binaries: warn once and exit cleanly (no silent failure). |
| 28 | +RTK_VERSION=$(rtk --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1) |
| 29 | +if [ -n "$RTK_VERSION" ]; then |
| 30 | + MAJOR=$(echo "$RTK_VERSION" | cut -d. -f1) |
| 31 | + MINOR=$(echo "$RTK_VERSION" | cut -d. -f2) |
| 32 | + # Require >= 0.23.0 |
| 33 | + if [ "$MAJOR" -eq 0 ] && [ "$MINOR" -lt 23 ]; then |
| 34 | + echo "[rtk] WARNING: rtk $RTK_VERSION is too old (need >= 0.23.0). Upgrade: cargo install rtk" >&2 |
| 35 | + exit 0 |
| 36 | + fi |
| 37 | +fi |
| 38 | + |
| 39 | +INPUT=$(cat) |
| 40 | +CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty') |
| 41 | + |
| 42 | +if [ -z "$CMD" ]; then |
| 43 | + exit 0 |
| 44 | +fi |
| 45 | + |
| 46 | +# Delegate all rewrite + permission logic to the Rust binary. |
| 47 | +REWRITTEN=$(rtk rewrite "$CMD" 2>/dev/null) |
| 48 | +EXIT_CODE=$? |
| 49 | + |
| 50 | +case $EXIT_CODE in |
| 51 | + 0) |
| 52 | + # Rewrite found, no permission rules matched — safe to auto-allow. |
| 53 | + # If the output is identical, the command was already using RTK. |
| 54 | + [ "$CMD" = "$REWRITTEN" ] && exit 0 |
| 55 | + ;; |
| 56 | + 1) |
| 57 | + # No RTK equivalent — pass through unchanged. |
| 58 | + exit 0 |
| 59 | + ;; |
| 60 | + 2) |
| 61 | + # Deny rule matched — let Claude Code's native deny rule handle it. |
| 62 | + exit 0 |
| 63 | + ;; |
| 64 | + 3) |
| 65 | + # Ask rule matched — rewrite the command but do NOT auto-allow so that |
| 66 | + # Claude Code prompts the user for confirmation. |
| 67 | + ;; |
| 68 | + *) |
| 69 | + exit 0 |
| 70 | + ;; |
| 71 | +esac |
| 72 | + |
| 73 | +ORIGINAL_INPUT=$(echo "$INPUT" | jq -c '.tool_input') |
| 74 | +UPDATED_INPUT=$(echo "$ORIGINAL_INPUT" | jq --arg cmd "$REWRITTEN" '.command = $cmd') |
| 75 | + |
| 76 | +if [ "$EXIT_CODE" -eq 3 ]; then |
| 77 | + # Ask: rewrite the command, omit permissionDecision so Claude Code prompts. |
| 78 | + jq -n \ |
| 79 | + --argjson updated "$UPDATED_INPUT" \ |
| 80 | + '{ |
| 81 | + "hookSpecificOutput": { |
| 82 | + "hookEventName": "PreToolUse", |
| 83 | + "updatedInput": $updated |
| 84 | + } |
| 85 | + }' |
| 86 | +else |
| 87 | + # Allow: rewrite the command and auto-allow. |
| 88 | + jq -n \ |
| 89 | + --argjson updated "$UPDATED_INPUT" \ |
| 90 | + '{ |
| 91 | + "hookSpecificOutput": { |
| 92 | + "hookEventName": "PreToolUse", |
| 93 | + "permissionDecision": "allow", |
| 94 | + "permissionDecisionReason": "RTK auto-rewrite", |
| 95 | + "updatedInput": $updated |
| 96 | + } |
| 97 | + }' |
| 98 | +fi |
0 commit comments