Fix and enrich statusline context-token display
The brace-bounded grep truncated at the first nested "}" in the usage object, so the token segment silently rendered empty. Parse each transcript JSONL line as JSON instead, summing input + cache_read + cache_creation to match /context's window-occupancy figure. Show it as "X.Xk/<limit> tokens (Y%)" with the limit auto-sized to the model's context window (1m vs 200k), and color the segment red once context reaches 200k.
This commit is contained in:
@@ -18,28 +18,37 @@ if command -v git >/dev/null 2>&1; then
|
|||||||
[ "$branch" = "HEAD" ] && branch=$(git -C "${cwd/#\~/$HOME}" rev-parse --short HEAD 2>/dev/null)
|
[ "$branch" = "HEAD" ] && branch=$(git -C "${cwd/#\~/$HOME}" rev-parse --short HEAD 2>/dev/null)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 3. Model display name
|
# 3. Model display name and id (id used to size the context window below)
|
||||||
model=$(printf '%s' "$input" | jq -r '.model.display_name // empty' 2>/dev/null)
|
model=$(printf '%s' "$input" | jq -r '.model.display_name // empty' 2>/dev/null)
|
||||||
|
model_id=$(printf '%s' "$input" | jq -r '.model.id // .model.display_name // empty' 2>/dev/null)
|
||||||
|
|
||||||
# 4. Token count from transcript JSONL
|
# 4. Context-window occupancy from transcript JSONL.
|
||||||
|
# Matches /context: input + cache_read + cache_creation of the most recent
|
||||||
|
# request is exactly what currently occupies the context window. The usage
|
||||||
|
# object nests sub-objects, so parse each JSONL line as JSON rather than
|
||||||
|
# grepping (a brace-bounded grep truncates at the first nested "}").
|
||||||
ctx=""
|
ctx=""
|
||||||
transcript=$(printf '%s' "$input" | jq -r '.transcript_path // empty' 2>/dev/null)
|
transcript=$(printf '%s' "$input" | jq -r '.transcript_path // empty' 2>/dev/null)
|
||||||
if [ -n "$transcript" ] && [ -r "$transcript" ] && command -v jq >/dev/null 2>&1; then
|
if [ -n "$transcript" ] && [ -r "$transcript" ] && command -v jq >/dev/null 2>&1; then
|
||||||
raw=$(grep -o '"usage":{[^}]*}' "$transcript" 2>/dev/null | tail -1)
|
total=$(tail -n 300 "$transcript" 2>/dev/null | jq -rR '
|
||||||
if [ -n "$raw" ]; then
|
fromjson? | (.message.usage // .usage) // empty
|
||||||
total=$(printf '%s' "{$raw}" | jq -r '
|
| select(.input_tokens != null)
|
||||||
.usage |
|
| (.input_tokens + (.cache_read_input_tokens // 0) + (.cache_creation_input_tokens // 0))
|
||||||
((.input_tokens // 0)
|
' 2>/dev/null | tail -1)
|
||||||
+ (.cache_read_input_tokens // 0)
|
if [ -n "$total" ] && [ "$total" -gt 0 ] 2>/dev/null; then
|
||||||
+ (.cache_creation_input_tokens // 0)
|
# 1M-context models report a [1m]/"1M" marker in the id/name; else 200k.
|
||||||
+ (.output_tokens // 0))
|
case "$model_id" in
|
||||||
' 2>/dev/null)
|
*1m*|*1M*) limit=1000000; label="1m" ;;
|
||||||
if [ -n "$total" ] && [ "$total" -gt 0 ] 2>/dev/null; then
|
*) limit=200000; label="200k" ;;
|
||||||
if [ "$total" -ge 1000 ]; then
|
esac
|
||||||
ctx=$(awk "BEGIN { printf \"%.1fk ctx\", $total/1000 }")
|
ctx=$(awk -v t="$total" -v lim="$limit" -v lab="$label" 'BEGIN {
|
||||||
else
|
if (t >= 1000) ts = sprintf("%.1fk", t/1000); else ts = sprintf("%d", t);
|
||||||
ctx="${total} ctx"
|
printf "%s/%s tokens (%.0f%%)", ts, lab, t*100/lim
|
||||||
fi
|
}')
|
||||||
|
# Turn the token segment red once context reaches 200k; reset after so the
|
||||||
|
# rest of the line keeps its default styling.
|
||||||
|
if [ "$total" -ge 200000 ] 2>/dev/null; then
|
||||||
|
ctx=$(printf '\033[31m%s\033[0m' "$ctx")
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user