|
| 1 | +#!/bin/sh |
| 2 | +# profile-switch.sh — account-aware ccstatusline launcher. |
| 3 | +# |
| 4 | +# Picks the right ccstatusline profile by your Claude account's subscriptionType, |
| 5 | +# then runs ccstatusline with it. Wire it into ~/.claude/settings.json: |
| 6 | +# "statusLine": { "type": "command", "command": "sh $HOME/.config/ccstatusline/profile-switch.sh", "padding": 0 } |
| 7 | +# |
| 8 | +# WHY this exists |
| 9 | +# ccstatusline's session-usage / weekly-usage / weekly-reset-timer widgets read |
| 10 | +# the five_hour / seven_day rate-limit buckets from api.anthropic.com/api/oauth/usage. |
| 11 | +# Enterprise/Team seats return those buckets as null, so those widgets render |
| 12 | +# "[Timeout]" (a pessimistic-lock label, not a real network timeout — the API |
| 13 | +# answers 200 in <0.5s). Enterprise exposes a monthly pay-as-you-go bucket |
| 14 | +# (extra_usage) instead. So we ship two profiles and auto-select. |
| 15 | +# |
| 16 | +# - settings.enterprise.json : 5h block reset timer + extra-usage-remaining |
| 17 | +# - settings.consumer.json : the classic 5h/7d usage % + reset widgets |
| 18 | +# - settings.json : ccstatusline default, the fallback |
| 19 | +# |
| 20 | +# ENTERPRISE SHIM (verified) |
| 21 | +# ccstatusline fetches usage ONCE per render and shares it across all Usage |
| 22 | +# widgets, UNIONing their required fields. reset-timer requires sessionResetAt; |
| 23 | +# Claude Code's statusline payload carries NO rate_limits, so on enterprise that |
| 24 | +# field is unsatisfiable and the shared object flips to {error}, which would make |
| 25 | +# extra-usage-remaining show "[Timeout]" too. We inject a synthetic |
| 26 | +# rate_limits.five_hour.resets_at (the 5h block reset, read from ccstatusline's |
| 27 | +# own block-cache) so sessionResetAt is satisfied LOCALLY, never enters the API |
| 28 | +# fetch, and the timer + monthly credit both render stably. |
| 29 | +# |
| 30 | +# Detection is cached for $TTL seconds. No credential value is ever printed; only |
| 31 | +# the subscriptionType field is read from the Keychain item. |
| 32 | +set -u |
| 33 | + |
| 34 | +CONFIG_DIR="$HOME/.config/ccstatusline" |
| 35 | +ENTERPRISE="$CONFIG_DIR/settings.enterprise.json" |
| 36 | +CONSUMER="$CONFIG_DIR/settings.consumer.json" |
| 37 | +CACHE="$CONFIG_DIR/.active-profile" |
| 38 | +TTL=300 |
| 39 | + |
| 40 | +CCSTATUSLINE="$(command -v ccstatusline 2>/dev/null || echo /opt/homebrew/bin/ccstatusline)" |
| 41 | +PYTHON="$(command -v python3 2>/dev/null || echo /usr/bin/python3)" |
| 42 | + |
| 43 | +# Read subscriptionType from the macOS Keychain (only that field; never the token). |
| 44 | +# On non-macOS, fall back to ~/.claude/.credentials.json. |
| 45 | +read_subscription() { |
| 46 | + creds="" |
| 47 | + if command -v security >/dev/null 2>&1; then |
| 48 | + creds=$(security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null) |
| 49 | + fi |
| 50 | + [ -z "$creds" ] && [ -f "$HOME/.claude/.credentials.json" ] && creds=$(cat "$HOME/.claude/.credentials.json" 2>/dev/null) |
| 51 | + printf '%s' "$creds" | "$PYTHON" -c 'import sys, json |
| 52 | +try: |
| 53 | + d = json.load(sys.stdin) or {} |
| 54 | + print(((d.get("claudeAiOauth") or {}).get("subscriptionType")) or "") |
| 55 | +except Exception: |
| 56 | + print("")' 2>/dev/null |
| 57 | +} |
| 58 | + |
| 59 | +pick_profile() { |
| 60 | + case "$(read_subscription)" in |
| 61 | + enterprise|team) [ -f "$ENTERPRISE" ] && printf '%s' "$ENTERPRISE" ;; |
| 62 | + *) [ -f "$CONSUMER" ] && printf '%s' "$CONSUMER" ;; |
| 63 | + esac |
| 64 | +} |
| 65 | + |
| 66 | +# Enterprise only: inject rate_limits.five_hour.resets_at (5h block reset, from |
| 67 | +# ccstatusline's block-cache) when the payload lacks rate_limits, so reset-timer's |
| 68 | +# required field is met locally and never poisons the shared usage fetch. |
| 69 | +enterprise_shim() { |
| 70 | + "$PYTHON" -c 'import sys, json, glob, os, time, datetime |
| 71 | +try: |
| 72 | + d = json.load(sys.stdin) |
| 73 | +except Exception: |
| 74 | + sys.stdout.write("{}"); sys.exit(0) |
| 75 | +if not d.get("rate_limits"): |
| 76 | + r = None |
| 77 | + bc = sorted(glob.glob(os.path.expanduser("~/.cache/ccstatusline/block-cache-*.json"))) |
| 78 | + if bc: |
| 79 | + try: |
| 80 | + s = json.load(open(bc[-1])).get("startTime") |
| 81 | + t = datetime.datetime.fromisoformat(s.replace("Z", "+00:00")) |
| 82 | + r = int(t.timestamp()) + 5 * 3600 |
| 83 | + except Exception: |
| 84 | + r = None |
| 85 | + if r is None: |
| 86 | + r = int(time.time()) + 5 * 3600 |
| 87 | + d["rate_limits"] = {"five_hour": {"resets_at": r, "used_percentage": 0}} |
| 88 | +json.dump(d, sys.stdout)' |
| 89 | +} |
| 90 | + |
| 91 | +cfg="" |
| 92 | +if [ -f "$CACHE" ]; then |
| 93 | + now=$(date +%s) |
| 94 | + mtime=$(stat -f %m "$CACHE" 2>/dev/null || stat -c %Y "$CACHE" 2>/dev/null || echo 0) |
| 95 | + if [ $(( now - mtime )) -lt "$TTL" ]; then |
| 96 | + cfg=$(cat "$CACHE" 2>/dev/null) |
| 97 | + fi |
| 98 | +fi |
| 99 | + |
| 100 | +if [ -z "$cfg" ] || [ ! -f "$cfg" ]; then |
| 101 | + cfg=$(pick_profile) |
| 102 | + [ -n "$cfg" ] && printf '%s' "$cfg" > "$CACHE" 2>/dev/null |
| 103 | +fi |
| 104 | + |
| 105 | +if [ "$cfg" = "$ENTERPRISE" ] && [ -f "$cfg" ]; then |
| 106 | + enterprise_shim | "$CCSTATUSLINE" --config "$cfg" |
| 107 | +elif [ -n "$cfg" ] && [ -f "$cfg" ]; then |
| 108 | + exec "$CCSTATUSLINE" --config "$cfg" |
| 109 | +else |
| 110 | + exec "$CCSTATUSLINE" |
| 111 | +fi |
0 commit comments