From 968e702d99736c5351457122d6dc41c488d478e8 Mon Sep 17 00:00:00 2001 From: arno Date: Wed, 15 Apr 2026 11:35:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D(tools):=20=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A0=8F=E4=B8=8A=E4=B8=8B=E6=96=87=E5=8D=A0?= =?UTF-8?q?=E6=AF=94=E8=AE=A1=E7=AE=97=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用实际 token 数除以 auto-compact 窗口(200k)计算占比 - 修复 GLM 模型下 used_percentage 与 /context 显示不一致的问题 --- .claude/tools/status_line.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) 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