修复(tools): 修正状态栏上下文占比计算方式

- 使用实际 token 数除以 auto-compact 窗口(200k)计算占比
- 修复 GLM 模型下 used_percentage 与 /context 显示不一致的问题
This commit is contained in:
2026-04-15 11:35:56 +08:00
parent 1d4041c8b8
commit 968e702d99
+13 -5
View File
@@ -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}%"