refactor
This commit is contained in:
parent
7cef56de15
commit
ecb5f68e32
17 changed files with 760 additions and 1626 deletions
|
|
@ -7,14 +7,8 @@ from pathlib import Path
|
|||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
||||
|
||||
from gibby.account_ops import (
|
||||
PermanentAccountFailure,
|
||||
handle_failed_account,
|
||||
refresh_account_usage,
|
||||
window_used_percent,
|
||||
)
|
||||
from gibby.client import OpenAIClient
|
||||
from gibby.models import format_reset_in
|
||||
from gibby.client import OpenAIAPIError, OpenAIClient
|
||||
from gibby.models import format_reset_in, parse_usage_payload
|
||||
from gibby.settings import Settings
|
||||
from gibby.store import JsonStateStore
|
||||
|
||||
|
|
@ -33,29 +27,23 @@ async def run(data_dir: Path | None = None) -> None:
|
|||
client = OpenAIClient(settings)
|
||||
try:
|
||||
for account in list(state.accounts):
|
||||
previous_id = account.id
|
||||
try:
|
||||
usage = await refresh_account_usage(
|
||||
account,
|
||||
client,
|
||||
settings.exhausted_usage_threshold,
|
||||
)
|
||||
store.update_active_account_id(state, previous_id, account.id)
|
||||
payload = await client.fetch_usage_payload(account.access_token)
|
||||
account.usage = parse_usage_payload(payload)
|
||||
account.usage_checked_at = account.usage.checked_at
|
||||
print(
|
||||
f"token ready for {account.id}, "
|
||||
f"primary {window_used_percent(usage.primary_window)}% "
|
||||
f"reset in {format_reset_in(usage.primary_window.reset_at if usage.primary_window else None)}, "
|
||||
f"secondary {window_used_percent(usage.secondary_window)}% "
|
||||
f"reset in {format_reset_in(usage.secondary_window.reset_at if usage.secondary_window else None)}"
|
||||
f"token ready for {account.email}, "
|
||||
f"primary {account.usage.primary_window.used_percent if account.usage.primary_window else 0}% "
|
||||
f"reset in {format_reset_in(account.usage.primary_window.reset_at if account.usage and account.usage.primary_window else None)}, "
|
||||
f"secondary {account.usage.secondary_window.used_percent if account.usage and account.usage.secondary_window else 0}% "
|
||||
f"reset in {format_reset_in(account.usage.secondary_window.reset_at if account.usage and account.usage.secondary_window else None)}"
|
||||
)
|
||||
except PermanentAccountFailure as exc:
|
||||
account.last_error = str(exc)
|
||||
handle_failed_account(store, account)
|
||||
store.remove_account(state, account.id)
|
||||
print(f"{account.id}: removed={exc}")
|
||||
except Exception as exc:
|
||||
account.last_error = str(exc)
|
||||
print(f"{account.id}: error={exc}")
|
||||
except OpenAIAPIError as exc:
|
||||
if exc.permanent:
|
||||
store.move_to_failed(state, account.email)
|
||||
print(f"{account.email}: removed={exc}")
|
||||
else:
|
||||
print(f"{account.email}: error={exc}")
|
||||
store.save(state)
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue