-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude-wrapper.sh
More file actions
54 lines (44 loc) · 1.47 KB
/
claude-wrapper.sh
File metadata and controls
54 lines (44 loc) · 1.47 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
#!/usr/bin/env bash
# claude-wrapper.sh — Rate-limited Claude Code CLI wrapper
# Enforces max 5 invocations per rolling hour window.
set -euo pipefail
RATE_FILE="${HOME}/.openclaw/skills/claude-code/.rate-state.json"
MAX_CALLS=5
WINDOW_SECS=3600 # 1 hour
# Ensure rate state file exists
if [[ ! -f "$RATE_FILE" ]]; then
echo '{"calls":[]}' > "$RATE_FILE"
fi
NOW=$(date +%s)
CUTOFF=$((NOW - WINDOW_SECS))
# Read existing calls and filter to within window
CALLS=$(python3 -c "
import json, sys
with open('$RATE_FILE') as f:
data = json.load(f)
recent = [t for t in data.get('calls', []) if t > $CUTOFF]
print(json.dumps(recent))
")
COUNT=$(python3 -c "import json; print(len(json.loads('$CALLS')))")
if (( COUNT >= MAX_CALLS )); then
# Find when the oldest call in window expires
OLDEST=$(python3 -c "import json; calls=json.loads('$CALLS'); print(min(calls))")
RESET_AT=$((OLDEST + WINDOW_SECS))
WAIT_MINS=$(( (RESET_AT - NOW + 59) / 60 )) # round up
echo "⛔ Rate limit reached: ${COUNT}/${MAX_CALLS} calls in the last hour."
echo "⏳ Try again in ~${WAIT_MINS} minute(s)."
exit 1
fi
# Record this call
NEW_CALLS=$(python3 -c "
import json
calls = json.loads('$CALLS')
calls.append($NOW)
print(json.dumps({'calls': calls}))
")
echo "$NEW_CALLS" > "$RATE_FILE"
REMAINING=$((MAX_CALLS - COUNT - 1))
echo "✅ Claude Code invoked (${REMAINING}/${MAX_CALLS} calls remaining this hour)"
echo "---"
# Forward all arguments to claude
exec "${HOME}/.local/bin/claude" "$@"