codex-cli-usage 0.1.9__py3-none-any.whl → 0.1.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,8 @@ Usage:
16
16
 
17
17
  import argparse
18
18
  import json
19
+ import math
20
+ import os
19
21
  import queue
20
22
  import shutil
21
23
  import signal
@@ -66,8 +68,15 @@ def _find_codex_path(filename: str) -> Path:
66
68
  return native
67
69
 
68
70
 
71
+ def _find_codex_auth_path() -> Path:
72
+ codex_home = os.environ.get("CODEX_HOME")
73
+ if codex_home:
74
+ return Path(codex_home) / "auth.json"
75
+ return _find_codex_path("auth.json")
76
+
77
+
69
78
  CODEX_DIR = Path.home() / ".codex"
70
- AUTH_FILE = _find_codex_path("auth.json")
79
+ AUTH_FILE = _find_codex_auth_path()
71
80
  USAGE_FILE = _find_codex_path("usage-limits.json")
72
81
  DAEMON_INTERVAL = 300 # 5 minutes
73
82
  USAGE_URL = "https://chatgpt.com/backend-api/codex/usage"
@@ -323,7 +332,7 @@ def _fetch_usage_direct() -> dict:
323
332
  """
324
333
  auth = get_auth()
325
334
  if not auth:
326
- raise RuntimeError("No auth at ~/.codex/auth.json — run `codex` first")
335
+ raise RuntimeError(f"No auth at {AUTH_FILE}. Run `codex` first")
327
336
 
328
337
  tokens = auth.get("tokens", {})
329
338
  access_token = tokens.get("access_token")
@@ -381,13 +390,36 @@ def classify_window(seconds: int | float | None, kind: str) -> tuple[str, str]:
381
390
 
382
391
 
383
392
  def _build_window(kind: str, window: dict) -> dict:
393
+ used_percent = window.get("used_percent")
384
394
  seconds = window.get("limit_window_seconds")
385
- key, label = classify_window(seconds, kind)
386
395
  reset_at = window.get("reset_at")
396
+ for value in (used_percent, seconds, reset_at):
397
+ if value is not None and (
398
+ not isinstance(value, int | float)
399
+ or isinstance(value, bool)
400
+ or (isinstance(value, float) and not math.isfinite(value))
401
+ ):
402
+ raise UsageServiceError("Invalid rate limit response")
403
+ if seconds is not None and seconds <= 0:
404
+ raise UsageServiceError("Invalid rate limit response")
405
+
406
+ try:
407
+ key, label = classify_window(seconds, kind)
408
+ except OverflowError:
409
+ raise UsageServiceError("Invalid rate limit response") from None
410
+ try:
411
+ resets_at = (
412
+ datetime.fromtimestamp(reset_at, tz=timezone.utc).isoformat()
413
+ if reset_at is not None
414
+ else None
415
+ )
416
+ except (OSError, OverflowError, ValueError):
417
+ raise UsageServiceError("Invalid rate limit response") from None
418
+
387
419
  return {
388
420
  "kind": kind,
389
- "pct": window.get("used_percent"),
390
- "resets_at": datetime.fromtimestamp(reset_at, tz=timezone.utc).isoformat() if reset_at else None,
421
+ "pct": used_percent,
422
+ "resets_at": resets_at,
391
423
  "window_secs": seconds,
392
424
  "key": key,
393
425
  "label": label,
@@ -442,11 +474,13 @@ def build_usage_json(api_data: dict) -> dict:
442
474
  # Code review limits
443
475
  cr = api_data.get("code_review_rate_limit") or {}
444
476
  cr_primary = cr.get("primary_window")
445
- if cr_primary and cr_primary.get("used_percent", 0) > 0:
446
- result["code_review"] = {
447
- "pct": cr_primary["used_percent"],
448
- "resets_at": datetime.fromtimestamp(cr_primary["reset_at"], tz=timezone.utc).isoformat() if cr_primary.get("reset_at") else None,
449
- }
477
+ if isinstance(cr_primary, dict):
478
+ window = _build_window("code_review", cr_primary)
479
+ if window["pct"] is not None and window["pct"] > 0:
480
+ result["code_review"] = {
481
+ "pct": window["pct"],
482
+ "resets_at": window["resets_at"],
483
+ }
450
484
 
451
485
  # Credits
452
486
  credits = api_data.get("credits") or {}
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: codex-cli-usage
3
- Version: 0.1.9
3
+ Version: 0.1.10
4
4
  Summary: Codex CLI usage monitor — fetches rate limits through the Codex app-server
5
5
  Project-URL: Homepage, https://github.com/wakamex/codex-cli-usage
6
6
  Project-URL: Source, https://github.com/wakamex/codex-cli-usage
@@ -100,7 +100,10 @@ window actually classifies as 5-hour or weekly. Readers should prefer
100
100
 
101
101
  ### Authentication
102
102
 
103
- The OAuth tokens live at `~/.codex/auth.json`, written by the Codex CLI on login.
103
+ The OAuth tokens live at `~/.codex/auth.json` by default and are written by the
104
+ Codex CLI on login. When `CODEX_HOME` is set, the direct compatibility fallback
105
+ reads `$CODEX_HOME/auth.json`. The preferred app-server path leaves credential
106
+ resolution to Codex.
104
107
 
105
108
  Codex owns automatic token refresh and credential persistence. The compatibility
106
109
  fallback reads `auth.json` but never modifies credentials.
@@ -0,0 +1,6 @@
1
+ codex_cli_usage/__init__.py,sha256=I24UubGF-NXScacl-hRXOM1Is7lXmHtExIjHJ8vZP2k,23935
2
+ codex_cli_usage/__main__.py,sha256=V_ll4j4TO5TIww8Uh9mQZwAGMMvb66-9JSXuTkYbr3A,59
3
+ codex_cli_usage-0.1.10.dist-info/METADATA,sha256=1wY8qw8fG0MIigjAnT1F5ywYE27IoUQS-bratXXbbC8,3634
4
+ codex_cli_usage-0.1.10.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
5
+ codex_cli_usage-0.1.10.dist-info/entry_points.txt,sha256=P4fiVVq76DXzfc_gXLICI7J4-xwdBqeZoQjlUxq2d4g,57
6
+ codex_cli_usage-0.1.10.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.31.0
2
+ Generator: hatchling 1.32.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,6 +0,0 @@
1
- codex_cli_usage/__init__.py,sha256=FPHWaTVqb2-rVXovkA_iE-iqABvZIpApKDFk3sNxGog,22939
2
- codex_cli_usage/__main__.py,sha256=V_ll4j4TO5TIww8Uh9mQZwAGMMvb66-9JSXuTkYbr3A,59
3
- codex_cli_usage-0.1.9.dist-info/METADATA,sha256=eO2PlghUxjqpG2IuP1khkCCQKH6EiKPDDrSoj7p8eZI,3455
4
- codex_cli_usage-0.1.9.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
5
- codex_cli_usage-0.1.9.dist-info/entry_points.txt,sha256=P4fiVVq76DXzfc_gXLICI7J4-xwdBqeZoQjlUxq2d4g,57
6
- codex_cli_usage-0.1.9.dist-info/RECORD,,