fix(statusline): keep last ctx value while a Z.ai request is running

Z.ai (ai-glm, ai-eltex) streams message_start with input_tokens=0 and
sends the real usage only in the final message_delta, so while a
request runs Claude Code passes used_percentage=0 and ctx: dropped to
0%. Keep the last non-zero value per session_id in
~/.cache/ai-setup/ctx_last_<id> and show it instead of the zero.
/clear starts a new session id, so it still shows 0 there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Виталий Никитенко
2026-10-07 12:46:18 +07:00
parent 17a943069a
commit e3a1c500fd

View File

@@ -268,6 +268,24 @@ else
fi fi
fi fi
# Z.ai (ai-glm, ai-eltex) отдаёт usage только в конце ответа, в начале потока
# input_tokens=0, поэтому пока идёт запрос, Claude Code присылает used_percentage=0.
# Держим последнее ненулевое значение этой сессии.
session_id=$(echo "$input" | jq -r '.session_id // empty')
if [ -n "$session_id" ]; then
ctx_cache="$HOME/.cache/ai-setup/ctx_last_${session_id}"
if [ -n "$ctx_pct" ] && [ "$(printf '%.0f' "$ctx_pct")" -gt 0 ]; then
if [ ! -f "$ctx_cache" ]; then
mkdir -p "$HOME/.cache/ai-setup"
# Раз за сессию чистим кэш старых сессий
find "$HOME/.cache/ai-setup" -name 'ctx_last_*' -mtime +7 -delete 2>/dev/null
fi
echo "$ctx_pct" > "$ctx_cache"
elif [ -f "$ctx_cache" ]; then
ctx_pct=$(cat "$ctx_cache")
fi
fi
# ctx:0% при старте новой сессии (нет данных от API) # ctx:0% при старте новой сессии (нет данных от API)
[ -z "$ctx_pct" ] && ctx_pct="0" [ -z "$ctx_pct" ] && ctx_pct="0"