#!/bin/bash
input=$(cat)
IFS=$'\t' read -r cwd session_id model total size <<< $(echo "$input" | jq -r '[.cwd, .session_id, .model.display_name, (.context_window.current_usage.input_tokens + .context_window.current_usage.cache_creation_input_tokens + .context_window.current_usage.cache_read_input_tokens), .context_window.context_window_size] | @tsv')
percentage=$((total * 100 / size))
tokens=$((total / 1000))
# Output to stdout
echo "${tokens}k (${percentage}%) | ${model} | ${cwd}"
# Write percentage to file (preserve lines 2+ if file exists)
# works with cleanup-context.sh
# context_file="$cwd/.context-$session_id"
# if [[ -f "$context_file" ]]; then
# sed -i "1s/.*/$percentage/" "$context_file"
# else
# echo "$percentage" > "$context_file"
# fi