mirror of
https://github.com/rtk-ai/rtk.git
synced 2026-09-19 07:33:17 +08:00
c3917e4de2
Add `rtk init -g --agent cursor` to install RTK hooks for Cursor Agent.
Cursor's preToolUse hook supports command rewriting via updated_input,
functionally identical to Claude Code's PreToolUse. Works with both the
Cursor editor and cursor-cli (they share ~/.cursor/hooks.json).
Changes:
- New `--agent <name>` flag (claude|cursor) on `rtk init`, extensible
for future agents. Default is claude (backward compatible).
- Cursor hook script (hooks/cursor-rtk-rewrite.sh) outputs Cursor's
JSON format: {permission, updated_input} vs Claude's hookSpecificOutput.
- `rtk init --show` reports Cursor hook and hooks.json status.
- `rtk init -g --uninstall` removes Cursor artifacts.
- `rtk discover` notes that Cursor sessions are tracked via `rtk gain`
(Cursor transcripts lack structured tool_use/tool_result blocks).
- Unit tests for Cursor hooks.json patching, detection, and removal.
Made-with: Cursor
Signed-off-by: Patrick szymkowiak <patrick.szymkowiak@innovtech.eu>
Co-authored-by: Moisei <1199723+moisei@users.noreply.github.com>
55 lines
1.8 KiB
Bash
Executable File
55 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# rtk-hook-version: 1
|
|
# RTK Cursor Agent hook — rewrites shell commands to use rtk for token savings.
|
|
# Works with both Cursor editor and cursor-cli (they share ~/.cursor/hooks.json).
|
|
# Cursor preToolUse hook format: receives JSON on stdin, returns JSON on stdout.
|
|
# Requires: rtk >= 0.23.0, jq
|
|
#
|
|
# This is a thin delegating hook: all rewrite logic lives in `rtk rewrite`,
|
|
# which is the single source of truth (src/discover/registry.rs).
|
|
# To add or change rewrite rules, edit the Rust registry — not this file.
|
|
|
|
if ! command -v jq &>/dev/null; then
|
|
echo "[rtk] WARNING: jq is not installed. Hook cannot rewrite commands. Install jq: https://jqlang.github.io/jq/download/" >&2
|
|
exit 0
|
|
fi
|
|
|
|
if ! command -v rtk &>/dev/null; then
|
|
echo "[rtk] WARNING: rtk is not installed or not in PATH. Hook cannot rewrite commands. Install: https://github.com/rtk-ai/rtk#installation" >&2
|
|
exit 0
|
|
fi
|
|
|
|
# Version guard: rtk rewrite was added in 0.23.0.
|
|
RTK_VERSION=$(rtk --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
|
|
if [ -n "$RTK_VERSION" ]; then
|
|
MAJOR=$(echo "$RTK_VERSION" | cut -d. -f1)
|
|
MINOR=$(echo "$RTK_VERSION" | cut -d. -f2)
|
|
if [ "$MAJOR" -eq 0 ] && [ "$MINOR" -lt 23 ]; then
|
|
echo "[rtk] WARNING: rtk $RTK_VERSION is too old (need >= 0.23.0). Upgrade: cargo install rtk" >&2
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
INPUT=$(cat)
|
|
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
|
|
|
|
if [ -z "$CMD" ]; then
|
|
echo '{}'
|
|
exit 0
|
|
fi
|
|
|
|
# Delegate all rewrite logic to the Rust binary.
|
|
# rtk rewrite exits 1 when there's no rewrite — hook passes through silently.
|
|
REWRITTEN=$(rtk rewrite "$CMD" 2>/dev/null) || { echo '{}'; exit 0; }
|
|
|
|
# No change — nothing to do.
|
|
if [ "$CMD" = "$REWRITTEN" ]; then
|
|
echo '{}'
|
|
exit 0
|
|
fi
|
|
|
|
jq -n --arg cmd "$REWRITTEN" '{
|
|
"permission": "allow",
|
|
"updated_input": { "command": $cmd }
|
|
}'
|