#!/bin/bash
# TraxAgent status line for Claude Code. Shows model, session cost and your limits, and sends the limit
# bars to TraxAgent about once a minute so your team can see who is near a 5-hour or weekly cap.
# Shows your team's spend cap too, when one is set on TraxAgent's Caps page.
# Sends only: the email you sign in to Claude Code with, your plan tier, this session's id and running cost, the two limit percentages and reset times, model id and Claude Code version.
export PATH="$HOME/.claude/bin:$PATH"   # jq installed without admin rights lives here
input=$(cat)
model=$(echo "$input" | jq -r '.model.display_name // "Claude"')
cost=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')
fh=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
sd=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
line="[$model] $(printf '$%.2f' "$cost")"
[ -n "$fh" ] && line="$line · 5h ${fh%.*}%"
[ -n "$sd" ] && line="$line · week ${sd%.*}%"
# your team's spend cap, as TraxAgent last reported it (written by the post below)
capf="${TMPDIR:-/tmp}/traxagent-cap-$(id -u).json"
if [ -s "$capf" ]; then
  capline=$(jq -r 'select(.cap != null) | .cap | (if .period == "day" then "today" elif .period == "week" then "this week" else "this month" end) + " $" + (.spent|floor|tostring) + " of $" + (.cap|floor|tostring) + " cap" + (if .projected_hit then " (hits " + ((.projected_hit + "T00:00:00Z") | try (fromdate | strftime("%b %-d")) catch .[5:10]) + " at this pace)" else "" end)' "$capf" 2>/dev/null)
  [ -n "$capline" ] && line="$line · $capline"
fi
echo "$line"
# what Claude Code handed us (limit fields only), kept on this machine to help troubleshooting
echo "$input" | jq -c '{at: (now|floor), rate_limits, model: .model.id, version}' > "${TMPDIR:-/tmp}/traxagent-last.json" 2>/dev/null
# post at most once a minute, in the background, never blocking the status line
if [ -n "$TRAXAGENT_CC_TOKEN" ] && [ -n "$fh$sd" ]; then
  stamp="${TMPDIR:-/tmp}/traxagent-limits-$(id -u)"
  now=$(date +%s)
  if [ ! -f "$stamp" ] || [ $((now - $(cat "$stamp" 2>/dev/null || echo 0))) -ge 60 ]; then
    echo "$now" > "$stamp"
    # the email Claude Code is signed in with (matches its usage reports), then Git's, then user@machine
    email=$(jq -r '.oauthAccount.emailAddress // empty' "$HOME/.claude.json" 2>/dev/null)
    [ -z "$email" ] && email=$(git config --get user.email 2>/dev/null)
    [ -z "$email" ] && email="$(id -un)@$(hostname -s 2>/dev/null || hostname)"
    plan=$(jq -r '.oauthAccount | (.userRateLimitTier // .organizationRateLimitTier // .seatTier // empty)' "$HOME/.claude.json" 2>/dev/null)
    payload=$(echo "$input" | jq -c --arg e "$email" --arg p "$plan" '{email: $e, plan: $p, rate_limits: (.rate_limits // {}), model: .model.id, version: .version,
      session_id: .session_id, session_cost: (.cost.total_cost_usd // null)}')
    (curl -s -m 5 -X POST "https://www.traxagent.io/cc/limits" -H "Authorization: Bearer $TRAXAGENT_CC_TOKEN" \
       -H "Content-Type: application/json" --data "$payload" -o "$capf.tmp" 2>/dev/null && mv "$capf.tmp" "$capf" &)
  fi
fi
