-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-context.sh
More file actions
executable file
·52 lines (43 loc) · 1.72 KB
/
check-context.sh
File metadata and controls
executable file
·52 lines (43 loc) · 1.72 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
#!/bin/bash
# Stop 훅: 컨텍스트 사용량이 85%를 초과하면 /half-clone을 제안합니다.
# 트리거되면 Claude가 중지하는 것을 차단하고 /half-clone을 실행하라고 알려줍니다.
# 이 명령은 후반부만 포함하는 새 대화를 만들어 거기서 계속할 수 있게 합니다.
# ~/.claude/settings.json에 추가하여 설치:
# {
# "hooks": {
# "Stop": [{
# "hooks": [{
# "type": "command",
# "command": "~/.claude/scripts/check-context.sh"
# }]
# }]
# }
# }
input=$(cat)
# 무한 루프 방지 - 이미 stop 훅에 의해 트리거된 경우 종료
stop_hook_active=$(echo "$input" | jq -r '.stop_hook_active // false')
if [[ "$stop_hook_active" == "true" ]]; then
exit 0
fi
transcript_path=$(echo "$input" | jq -r '.transcript_path // empty')
if [[ -z "$transcript_path" || ! -f "$transcript_path" ]]; then
exit 0
fi
max_context=200000
# 트랜스크립트에서 컨텍스트 사용량 계산 (context-bar.sh와 동일한 방법)
context_length=$(jq -s '
map(select(.message.usage and .isSidechain != true and .isApiErrorMessage != true)) |
last |
if . then
(.message.usage.input_tokens // 0) +
(.message.usage.cache_read_input_tokens // 0) +
(.message.usage.cache_creation_input_tokens // 0)
else 0 end
' < "$transcript_path")
if [[ -z "$context_length" || "$context_length" -eq 0 ]]; then
exit 0
fi
pct=$((context_length * 100 / max_context))
if [[ $pct -ge 85 ]]; then
echo "{\"decision\": \"block\", \"reason\": \"컨텍스트 사용량이 ${pct}%입니다. /half-clone을 실행하여 후반부만 포함된 새 대화를 만들고, 새 에이전트가 거기서 계속할 수 있게 해주세요.\"}"
fi