codex-lb 0.1.2__py3-none-any.whl → 0.1.4__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.
- app/db/session.py +27 -9
- app/dependencies.py +11 -10
- app/modules/proxy/service.py +36 -15
- app/modules/request_logs/repository.py +17 -3
- {codex_lb-0.1.2.dist-info → codex_lb-0.1.4.dist-info}/METADATA +3 -3
- {codex_lb-0.1.2.dist-info → codex_lb-0.1.4.dist-info}/RECORD +9 -9
- {codex_lb-0.1.2.dist-info → codex_lb-0.1.4.dist-info}/WHEEL +0 -0
- {codex_lb-0.1.2.dist-info → codex_lb-0.1.4.dist-info}/entry_points.txt +0 -0
- {codex_lb-0.1.2.dist-info → codex_lb-0.1.4.dist-info}/licenses/LICENSE +0 -0
app/db/session.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import asyncio
|
|
3
4
|
from pathlib import Path
|
|
4
5
|
from typing import AsyncIterator
|
|
5
6
|
|
|
@@ -23,16 +24,33 @@ def _ensure_sqlite_dir(url: str) -> None:
|
|
|
23
24
|
Path(path).expanduser().parent.mkdir(parents=True, exist_ok=True)
|
|
24
25
|
|
|
25
26
|
|
|
27
|
+
async def _safe_rollback(session: AsyncSession) -> None:
|
|
28
|
+
if not session.in_transaction():
|
|
29
|
+
return
|
|
30
|
+
try:
|
|
31
|
+
await asyncio.shield(session.rollback())
|
|
32
|
+
except Exception:
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
async def _safe_close(session: AsyncSession) -> None:
|
|
37
|
+
try:
|
|
38
|
+
await asyncio.shield(session.close())
|
|
39
|
+
except Exception:
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
|
|
26
43
|
async def get_session() -> AsyncIterator[AsyncSession]:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
44
|
+
session = SessionLocal()
|
|
45
|
+
try:
|
|
46
|
+
yield session
|
|
47
|
+
except BaseException:
|
|
48
|
+
await _safe_rollback(session)
|
|
49
|
+
raise
|
|
50
|
+
finally:
|
|
51
|
+
if session.in_transaction():
|
|
52
|
+
await _safe_rollback(session)
|
|
53
|
+
await _safe_close(session)
|
|
36
54
|
|
|
37
55
|
|
|
38
56
|
async def init_db() -> None:
|
app/dependencies.py
CHANGED
|
@@ -7,7 +7,7 @@ from dataclasses import dataclass
|
|
|
7
7
|
from fastapi import Depends
|
|
8
8
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
9
9
|
|
|
10
|
-
from app.db.session import SessionLocal, get_session
|
|
10
|
+
from app.db.session import SessionLocal, _safe_close, _safe_rollback, get_session
|
|
11
11
|
from app.modules.accounts.repository import AccountsRepository
|
|
12
12
|
from app.modules.accounts.service import AccountsService
|
|
13
13
|
from app.modules.oauth.service import OauthService
|
|
@@ -87,15 +87,16 @@ def get_usage_context(
|
|
|
87
87
|
|
|
88
88
|
@asynccontextmanager
|
|
89
89
|
async def _accounts_repo_context() -> AsyncIterator[AccountsRepository]:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
session = SessionLocal()
|
|
91
|
+
try:
|
|
92
|
+
yield AccountsRepository(session)
|
|
93
|
+
except BaseException:
|
|
94
|
+
await _safe_rollback(session)
|
|
95
|
+
raise
|
|
96
|
+
finally:
|
|
97
|
+
if session.in_transaction():
|
|
98
|
+
await _safe_rollback(session)
|
|
99
|
+
await _safe_close(session)
|
|
99
100
|
|
|
100
101
|
|
|
101
102
|
def get_oauth_context(
|
app/modules/proxy/service.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import logging
|
|
3
4
|
import time
|
|
4
5
|
from datetime import timedelta
|
|
5
6
|
from typing import AsyncIterator, Iterable, Mapping
|
|
@@ -36,6 +37,8 @@ from app.modules.proxy.usage_updater import UsageUpdater
|
|
|
36
37
|
from app.modules.request_logs.repository import RequestLogsRepository
|
|
37
38
|
from app.modules.usage.repository import UsageRepository
|
|
38
39
|
|
|
40
|
+
logger = logging.getLogger(__name__)
|
|
41
|
+
|
|
39
42
|
|
|
40
43
|
class ProxyService:
|
|
41
44
|
def __init__(
|
|
@@ -191,6 +194,7 @@ class ProxyService:
|
|
|
191
194
|
yield format_sse_event(event)
|
|
192
195
|
return
|
|
193
196
|
|
|
197
|
+
account_id_value = account.id
|
|
194
198
|
try:
|
|
195
199
|
account = await self._ensure_fresh(account)
|
|
196
200
|
async for line in self._stream_once(
|
|
@@ -243,7 +247,15 @@ class ProxyService:
|
|
|
243
247
|
await self._load_balancer.mark_permanent_failure(account, exc.code)
|
|
244
248
|
continue
|
|
245
249
|
except Exception:
|
|
246
|
-
|
|
250
|
+
try:
|
|
251
|
+
await self._load_balancer.record_error(account)
|
|
252
|
+
except Exception:
|
|
253
|
+
logger.warning(
|
|
254
|
+
"Failed to record proxy error account_id=%s request_id=%s",
|
|
255
|
+
account_id_value,
|
|
256
|
+
request_id,
|
|
257
|
+
exc_info=True,
|
|
258
|
+
)
|
|
247
259
|
if attempt == max_attempts - 1:
|
|
248
260
|
event = response_failed_event(
|
|
249
261
|
"upstream_error",
|
|
@@ -267,8 +279,9 @@ class ProxyService:
|
|
|
267
279
|
request_id: str,
|
|
268
280
|
allow_retry: bool,
|
|
269
281
|
) -> AsyncIterator[str]:
|
|
282
|
+
account_id_value = account.id
|
|
270
283
|
access_token = self._encryptor.decrypt(account.access_token_encrypted)
|
|
271
|
-
account_id = _header_account_id(
|
|
284
|
+
account_id = _header_account_id(account_id_value)
|
|
272
285
|
model = payload.model
|
|
273
286
|
start = time.monotonic()
|
|
274
287
|
status = "success"
|
|
@@ -341,19 +354,27 @@ class ProxyService:
|
|
|
341
354
|
reasoning_tokens = (
|
|
342
355
|
usage.output_tokens_details.reasoning_tokens if usage and usage.output_tokens_details else None
|
|
343
356
|
)
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
+
try:
|
|
358
|
+
await self._logs_repo.add_log(
|
|
359
|
+
account_id=account_id_value,
|
|
360
|
+
request_id=request_id,
|
|
361
|
+
model=model,
|
|
362
|
+
input_tokens=input_tokens,
|
|
363
|
+
output_tokens=output_tokens,
|
|
364
|
+
cached_input_tokens=cached_input_tokens,
|
|
365
|
+
reasoning_tokens=reasoning_tokens,
|
|
366
|
+
latency_ms=latency_ms,
|
|
367
|
+
status=status,
|
|
368
|
+
error_code=error_code,
|
|
369
|
+
error_message=error_message,
|
|
370
|
+
)
|
|
371
|
+
except Exception:
|
|
372
|
+
logger.warning(
|
|
373
|
+
"Failed to persist request log account_id=%s request_id=%s",
|
|
374
|
+
account_id_value,
|
|
375
|
+
request_id,
|
|
376
|
+
exc_info=True,
|
|
377
|
+
)
|
|
357
378
|
|
|
358
379
|
async def _refresh_usage(self, accounts: list[Account]) -> None:
|
|
359
380
|
latest_usage = await self._usage_repo.latest_by_account(window="primary")
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import asyncio
|
|
3
4
|
from datetime import datetime
|
|
4
5
|
|
|
5
6
|
from sqlalchemy import and_, select
|
|
@@ -49,9 +50,13 @@ class RequestLogsRepository:
|
|
|
49
50
|
requested_at=requested_at or utcnow(),
|
|
50
51
|
)
|
|
51
52
|
self._session.add(log)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
try:
|
|
54
|
+
await self._session.commit()
|
|
55
|
+
await self._session.refresh(log)
|
|
56
|
+
return log
|
|
57
|
+
except BaseException:
|
|
58
|
+
await _safe_rollback(self._session)
|
|
59
|
+
raise
|
|
55
60
|
|
|
56
61
|
async def list_recent(
|
|
57
62
|
self,
|
|
@@ -84,3 +89,12 @@ class RequestLogsRepository:
|
|
|
84
89
|
stmt = stmt.limit(limit)
|
|
85
90
|
result = await self._session.execute(stmt)
|
|
86
91
|
return list(result.scalars().all())
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def _safe_rollback(session: AsyncSession) -> None:
|
|
95
|
+
if not session.in_transaction():
|
|
96
|
+
return
|
|
97
|
+
try:
|
|
98
|
+
await asyncio.shield(session.rollback())
|
|
99
|
+
except Exception:
|
|
100
|
+
return
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: codex-lb
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Codex load balancer and proxy for ChatGPT accounts with usage dashboard
|
|
5
5
|
Author-email: Soju06 <qlskssk@gmail.com>
|
|
6
6
|
Maintainer-email: Soju06 <qlskssk@gmail.com>
|
|
@@ -56,7 +56,7 @@ Description-Content-Type: text/markdown
|
|
|
56
56
|
Load balancer for ChatGPT accounts. Pool multiple accounts, track usage, view everything in a dashboard.
|
|
57
57
|
|
|
58
58
|
<p align="center">
|
|
59
|
-
<img src="docs/screenshots/dashboard.jpeg" alt="Codex Load Balancer dashboard" width="100%">
|
|
59
|
+
<img src="https://raw.githubusercontent.com/Soju06/codex-lb/main/docs/screenshots/dashboard.jpeg" alt="Codex Load Balancer dashboard" width="100%">
|
|
60
60
|
</p>
|
|
61
61
|
|
|
62
62
|
## Quick Start
|
|
@@ -80,7 +80,7 @@ Open [localhost:2455](http://localhost:2455) → Add account → Done.
|
|
|
80
80
|
|
|
81
81
|
## Accounts view
|
|
82
82
|
|
|
83
|
-

|
|
83
|
+

|
|
84
84
|
|
|
85
85
|
## Codex CLI & Extension Setup
|
|
86
86
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
app/__init__.py,sha256=X_4Y93UbuflRBRZd65MtjsVxlZW8d9ZUyi0n83Q-Lrg,89
|
|
2
2
|
app/cli.py,sha256=gkIAkYOT9SbQjUDnVmwhVKZeKjL3YJCMrOjFINwBx54,544
|
|
3
|
-
app/dependencies.py,sha256=
|
|
3
|
+
app/dependencies.py,sha256=9VvBKrUctmuab793Yq2MsHmAh7upXt0viTeofdop8lA,3966
|
|
4
4
|
app/main.py,sha256=JMvTfBdS8wTDNqjQ5QSdqKjjhE0CkpfkbFW6fWNMpy0,4351
|
|
5
5
|
app/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
app/core/crypto.py,sha256=zUz2GVqXigzXB5zX2Diq2cR41Kl37bBqxJiZmWGiIcY,1145
|
|
@@ -35,7 +35,7 @@ app/core/utils/sse.py,sha256=ER3_7mm50BUC1CPIKoVOmQikz-vlEWKNSRDEVqzI_KA,387
|
|
|
35
35
|
app/core/utils/time.py,sha256=B6FfSe43Eq_puE6eourly1X3gajyihK2VOAwJ8M3wyI,497
|
|
36
36
|
app/db/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
37
37
|
app/db/models.py,sha256=JCknQCuzjHfgyuSzjHqBmeIE-0XisIAQGEhEWqmzabs,3841
|
|
38
|
-
app/db/session.py,sha256=
|
|
38
|
+
app/db/session.py,sha256=te9-diH008J-YUAnmE7-OCBUII4LB2jgLlV_ipgoej4,1584
|
|
39
39
|
app/modules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
40
40
|
app/modules/accounts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
41
41
|
app/modules/accounts/api.py,sha256=rSkYrg3_p_JE1kHZ4xXa5lBFTdT40v1oNAmlTDGOguY,2807
|
|
@@ -54,12 +54,12 @@ app/modules/proxy/api.py,sha256=BR_qNlNZg2Ft5GYQ-7AzlLlJFf6RVJmlzJzFr_KHUIc,2921
|
|
|
54
54
|
app/modules/proxy/auth_manager.py,sha256=sFcbStd-OK0g8S56z7XcSkFJ22kEirBU0fYdz2drS0o,2185
|
|
55
55
|
app/modules/proxy/load_balancer.py,sha256=k53RXn9v9fQPXEJ3zWbkhh_Jd82PK1tUef3ZvCdSzog,7441
|
|
56
56
|
app/modules/proxy/schemas.py,sha256=55pXtUCl2R_93kAPOJJ7Ji4Jn3qVu10vq2KSCCkNdp4,2748
|
|
57
|
-
app/modules/proxy/service.py,sha256=
|
|
57
|
+
app/modules/proxy/service.py,sha256=XBpL1OIZuzss8nvsYajC5KsaRdGztjci5RgnWORZ1ss,26816
|
|
58
58
|
app/modules/proxy/types.py,sha256=iqEyoO8vGr8N5oEzUSvVWCai7UZbJAU62IvO7JNS9qs,927
|
|
59
59
|
app/modules/proxy/usage_updater.py,sha256=t5wpn3SmPnTXceiQFF0tw-9V5oPPhP4C0anwbI7T5v0,5588
|
|
60
60
|
app/modules/request_logs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
61
61
|
app/modules/request_logs/api.py,sha256=6nV2Uv_hnK7WI3gNpKrgTx4MyUQIXk1QxKp40nij0Xo,1037
|
|
62
|
-
app/modules/request_logs/repository.py,sha256=
|
|
62
|
+
app/modules/request_logs/repository.py,sha256=SnwDwD84wgslmdIgYFGD8ReUYmIyhxT6MOjFAolLBC8,3333
|
|
63
63
|
app/modules/request_logs/schemas.py,sha256=GSCi4TEWMmQ-THsQl2irRrA_msU8jzsqKSBDFn7hiJU,592
|
|
64
64
|
app/modules/request_logs/service.py,sha256=SuJeeFJy0qhMe6cUgQ3-zPrENoPlcNnDmTvG7aYsLmE,2386
|
|
65
65
|
app/modules/shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -73,8 +73,8 @@ app/static/7.css,sha256=9EHW2Ouff2fRXgcQbYuCuglxTFgQZGNLLTXKTS6S5aI,80687
|
|
|
73
73
|
app/static/index.css,sha256=ct1FfBg0PY7c6KxSS6A7JM_WDwdeJqhQa4pXTgyJ19Q,8786
|
|
74
74
|
app/static/index.html,sha256=FmWQ0l40rTBsHIR9i0ttGTzG17ePASsO2VDv6Q48CNw,24477
|
|
75
75
|
app/static/index.js,sha256=L6Gie0iSAHyrPLvO_QtzEis4rxhVqNe2m-g4h3lB6GU,52419
|
|
76
|
-
codex_lb-0.1.
|
|
77
|
-
codex_lb-0.1.
|
|
78
|
-
codex_lb-0.1.
|
|
79
|
-
codex_lb-0.1.
|
|
80
|
-
codex_lb-0.1.
|
|
76
|
+
codex_lb-0.1.4.dist-info/METADATA,sha256=lxv3ILVmcYOqGy3z-7y6_V4izHDFGH_U_m6dwmfHAmo,3828
|
|
77
|
+
codex_lb-0.1.4.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
78
|
+
codex_lb-0.1.4.dist-info/entry_points.txt,sha256=SEa5T6Uz2Fhy574No6Y0XyGmYi3PXLrhu2xStJTqyI8,42
|
|
79
|
+
codex_lb-0.1.4.dist-info/licenses/LICENSE,sha256=cHPibxiL0TXwrUX_kNY6ym544EX1UCzKhxdaca5cFuk,1062
|
|
80
|
+
codex_lb-0.1.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|