|
| 1 | +"""Helpers for reading Codex rate limits from session logs.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import time |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +_QUOTA_PREFIX = "Limits:" |
| 9 | + |
| 10 | + |
| 11 | +def rate_limits(): |
| 12 | + """Return the latest rate_limits dict from Codex session logs.""" |
| 13 | + root = Path(os.environ.get("CODEX_HOME", "~/.codex")).expanduser() |
| 14 | + sessions = root / "sessions" |
| 15 | + if not sessions.exists(): |
| 16 | + return None |
| 17 | + candidates = [] |
| 18 | + for dirpath, _dirnames, filenames in os.walk(sessions): |
| 19 | + for name in filenames: |
| 20 | + if not name.endswith(".jsonl"): |
| 21 | + continue |
| 22 | + path = os.path.join(dirpath, name) |
| 23 | + try: |
| 24 | + mtime = os.path.getmtime(path) |
| 25 | + except OSError: |
| 26 | + continue |
| 27 | + candidates.append((mtime, path)) |
| 28 | + if not candidates: |
| 29 | + return None |
| 30 | + for _mtime, path in sorted(candidates, reverse=True): |
| 31 | + found = _extract_rate_limits(path) |
| 32 | + if found is not None: |
| 33 | + return found |
| 34 | + return None |
| 35 | + |
| 36 | + |
| 37 | +def _extract_rate_limits(path): |
| 38 | + last = None |
| 39 | + try: |
| 40 | + with open(path, "r", encoding="utf-8", errors="replace") as handle: |
| 41 | + for line in handle: |
| 42 | + if '"rate_limits"' not in line: |
| 43 | + continue |
| 44 | + try: |
| 45 | + event = json.loads(line) |
| 46 | + except json.JSONDecodeError: |
| 47 | + continue |
| 48 | + payload = event.get("payload") or {} |
| 49 | + rate_data = payload.get("rate_limits") |
| 50 | + if isinstance(rate_data, dict): |
| 51 | + last = rate_data |
| 52 | + except OSError: |
| 53 | + return None |
| 54 | + return last |
| 55 | + |
| 56 | + |
| 57 | +def quota_line(): |
| 58 | + """Return a human-readable quota line.""" |
| 59 | + data = rate_limits() |
| 60 | + if not data: |
| 61 | + return f"{_QUOTA_PREFIX} unavailable" |
| 62 | + primary = data.get("primary") or {} |
| 63 | + secondary = data.get("secondary") or {} |
| 64 | + primary_left = _percent_left(primary.get("used_percent")) |
| 65 | + secondary_left = _percent_left(secondary.get("used_percent")) |
| 66 | + primary_reset = _format_reset(primary.get("resets_at")) |
| 67 | + secondary_reset = _format_reset(secondary.get("resets_at")) |
| 68 | + if primary_left is None or secondary_left is None: |
| 69 | + return f"{_QUOTA_PREFIX} unavailable" |
| 70 | + return ( |
| 71 | + f"{_QUOTA_PREFIX} {primary_left}% / {secondary_left}% left " |
| 72 | + f"(reset in {primary_reset} / {secondary_reset})" |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def _percent_left(used_percent): |
| 77 | + if not isinstance(used_percent, (int, float)): |
| 78 | + return None |
| 79 | + left = 100.0 - float(used_percent) |
| 80 | + if left < 0: |
| 81 | + left = 0.0 |
| 82 | + if left > 100: |
| 83 | + left = 100.0 |
| 84 | + return int(round(left)) |
| 85 | + |
| 86 | + |
| 87 | +def _format_reset(resets_at): |
| 88 | + if not isinstance(resets_at, (int, float)): |
| 89 | + return "unknown" |
| 90 | + remaining = float(resets_at) - time.time() |
| 91 | + if remaining < 0: |
| 92 | + remaining = 0 |
| 93 | + hours = remaining / 3600.0 |
| 94 | + if hours > 24: |
| 95 | + days = hours / 24.0 |
| 96 | + return f"{int(round(days))}d" |
| 97 | + return f"{int(round(hours))}h" |
0 commit comments