Repository navigation
Expand file tree
/
Copy pathrtk-rewrite.sh
More file actions
executable file
Β·60 lines (52 loc) Β· 1.75 KB
/
Copy pathrtk-rewrite.sh
File metadata and controls
executable file
Β·60 lines (52 loc) Β· 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
# RTK hook for Trapeze
# This hook rewrites bash commands to use rtk for token savings.
#
# https://github.com/rtk-ai/rtk
#
# Requires: rtk >= 0.23.0, jq
#
# All rewrite logic lives in `rtk rewrite`, which is the single source of
# truth. To add or change rewrite rules, edit the Rust registry, not this
# file.
#
# Exit code protocol for `rtk rewrite`:
# 0 + stdout Rewrite found β allow with updated input
# 1 No RTK equivalent β pass through unchanged
# 2 Deny rule matched β pass through
set -euo pipefail
if ! command -v jq &>/dev/null; then
echo "[rtk] WARNING: jq is not installed. Install: https://jqlang.github.io/jq/download/" >&2
exit 0
fi
if ! command -v rtk &>/dev/null; then
echo "[rtk] WARNING: rtk is not installed. Install: https://github.com/rtk-ai/rtk#installation" >&2
exit 0
fi
# Version guard: rtk rewrite requires >= 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
CMD="${TRAPEZE_TOOL_INPUT_COMMAND:-}"
if [ -z "$CMD" ]; then
exit 0
fi
REWRITTEN=$(rtk rewrite "$CMD" 2>/dev/null) && EXIT_CODE=0 || EXIT_CODE=$?
case $EXIT_CODE in
0 | 3)
# Rewrite found. If identical, the command already uses rtk.
[ "$CMD" = "$REWRITTEN" ] && exit 0
jq -n --arg cmd "$REWRITTEN" \
'{decision: "allow", updated_input: ({command: $cmd} | tostring)}'
;;
*)
# No rewrite (1), deny (2), or unexpected β pass through.
exit 0
;;
esac