diff --git a/.claude/tools/status_line.sh b/.claude/tools/status_line.sh index f893a24..44bc68a 100755 --- a/.claude/tools/status_line.sh +++ b/.claude/tools/status_line.sh @@ -5,12 +5,21 @@ input=$(cat) # Extract fields using jq MODEL=$(echo "$input" | jq -r '.model.display_name') DIR=$(echo "$input" | jq -r '.workspace.current_dir') - -# The "// 0" provides a fallback if the field is null -PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d. -f1) - V=$(echo "$input" | jq -r '.version') +# 计算上下文使用占比 +# JSON 中 used_percentage 是相对模型实际窗口(如 GLM 的 1M)计算的, +# 但 /context 命令显示的是相对 auto-compact 窗口(200k)的占比。 +# 因此需要用实际 token 数 / 200000 来获得正确的百分比。 +AUTO_COMPACT_WINDOW=200000 +PCT=$(echo "$input" | jq -r --argjson w "$AUTO_COMPACT_WINDOW" ' + (( + (.context_window.current_usage.input_tokens // 0) + + (.context_window.current_usage.cache_creation_input_tokens // 0) + + (.context_window.current_usage.cache_read_input_tokens // 0) + ) * 100 / $w) | round +') + BRANCH="" if command -v jj >/dev/null 2>&1 && jj repo root >/dev/null 2>&1; then BRANCH="🌿 $(jj log -r '@-' -T 'bookmarks' --no-graph 2>/dev/null)" @@ -25,5 +34,4 @@ elif git rev-parse --git-dir >/dev/null 2>&1; then REMOTE="🌐 $(git remote get-url origin 2>/dev/null)" fi -# echo "v$V | $MODEL | ${PCT}% context | 📁 ${DIR##*/} | $BRANCH $REMOTE" echo "版本:v$V | 模型:$MODEL | 上下文:${PCT}%" \ No newline at end of file