feat: dashboard KDE Plasma 6 per utilizzo OpenAI Codex

Widget desktop 224x224 che mostra:
- residuo settimanale con barra e percentuale
- countdown esatto al reset (reset_at dall'API)
- piano (Plus/Free), crediti, stato limiti
- backend bash che chiama https://chatgpt.com/backend-api/wham/usage
- legge il token OAuth da ~/.pi/agent/auth.json (provider openai-codex)
This commit is contained in:
Matteo Benedetto
2026-08-20 18:48:51 +02:00
commit 3eee01f0f2
8 changed files with 555 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
# Backend del plasmoide OpenAI Codex Usage.
# Chiama l'endpoint (non documentato) GET https://chatgpt.com/backend-api/wham/usage
# con OAuth Bearer. La risposta contiene reset_at esatto → nessuna stima locale.
set -u
AUTH_JSON="${CODEX_AUTH_JSON:-$HOME/.pi/agent/auth.json}"
TOKEN=""
if [ -f "$AUTH_JSON" ]; then
TOKEN="$(jq -r '.["openai-codex"].access // empty' "$AUTH_JSON" 2>/dev/null)"
fi
[ -n "$TOKEN" ] || TOKEN="${CODEX_ACCESS_TOKEN:-}"
[ -n "$TOKEN" ] || TOKEN="${OPENAI_CODEX_ACCESS_TOKEN:-}"
if [ -z "$TOKEN" ]; then
printf '{"ok":false,"error":"no_token","detail":"nessun token Codex in %s"}\n' "$AUTH_JSON"
exit 0
fi
RESP="$(curl -sS --max-time 15 "https://chatgpt.com/backend-api/wham/usage" -H "Authorization: Bearer $TOKEN" 2>/dev/null)"
CURL_RC=$?
if [ "$CURL_RC" -ne 0 ] || [ -z "$RESP" ]; then
printf '{"ok":false,"error":"fetch_failed","detail":"curl exit %s"}\n' "$CURL_RC"
exit 0
fi
# Se il token è scaduto, l'API restituisce HTML di login invece di JSON.
if ! printf '%s' "$RESP" | jq -e '.rate_limit.primary_window.used_percent | type == "number"' >/dev/null 2>&1; then
printf '{"ok":false,"error":"token_expired","detail":"token Codex scaduto, riesegui /login in pi"}\n'
exit 0
fi
printf '%s' "$RESP" | jq -c '{
ok: true,
plan_type: .plan_type,
remaining: ((100 - .rate_limit.primary_window.used_percent) / 100),
used_percent: .rate_limit.primary_window.used_percent,
reset_at: .rate_limit.primary_window.reset_at,
limit_window: .rate_limit.primary_window.limit_window_seconds,
credits: { balance: .credits.balance, has_credits: .credits.has_credits },
spend_reached: .spend_control.reached,
limit_reached: .rate_limit.limit_reached
}' 2>/dev/null || printf '{"ok":false,"error":"bad_json","detail":"risposta non valida da wham/usage"}\n'
exit 0