File size: 63,013 Bytes
8dba93d 848c7d6 8dba93d 46fd52e 8dba93d 46fd52e 8dba93d c86119f 8dba93d 970375f 8dba93d 46fd52e 8dba93d 46fd52e 8dba93d c86119f 8dba93d c86119f 8dba93d c86119f 8dba93d 848c7d6 c86119f 8dba93d 848c7d6 8dba93d 46fd52e 8dba93d 46fd52e 8dba93d 46fd52e 8dba93d 46fd52e 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d 848c7d6 8dba93d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 | """
RyzGateway - BYOK OpenAI-Compatible AI Gateway
Single-file FastAPI app for HuggingFace Spaces deployment.
Model routing:
- provider/model β direct passthrough to that provider
- generic-name β routed pool (round-robin, skip rate-limited)
Rate limit handling:
- Auto-detects 429s and marks endpoints as cooled down
- Respects RPM / RPD / RPS limits configured per provider
- Smart selection skips exhausted endpoints
Features:
- Glassmorphism UI (highly optimized for mobile touch)
- Auto-fetches and imports models directly from provider's /v1/models
"""
import os
import re
import time
import uuid
import json
import asyncio
import logging
from pathlib import Path
from typing import Any, Dict, List, Optional, Literal
from contextlib import asynccontextmanager
from collections import defaultdict, deque
import httpx
from fastapi import FastAPI, HTTPException, Header, Request, Depends
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
from pydantic import BaseModel, Field
# ββ Logging ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
log = logging.getLogger("ryzgateway")
# ββ Persistence (JSON file, reloaded on each read) βββββββββββββββββββββββββββββ
DATA_FILE = Path(os.getenv("DATA_FILE", "/data/gateway.json"))
def _default_data() -> dict:
return {
"api_keys": {}, # key_id β {key, name, created_at}
"providers": {}, # provider_id β {name, base_url, api_key, rate_type, rate_limit, notes}
"pool_models": {}, # pool_id β {name, members: [{provider_id, model_name, weight}], strategy}
}
def load_data() -> dict:
try:
if DATA_FILE.exists():
return json.loads(DATA_FILE.read_text())
except Exception as e:
log.error(f"Failed to load data: {e}")
return _default_data()
def save_data(data: dict):
try:
DATA_FILE.parent.mkdir(parents=True, exist_ok=True)
DATA_FILE.write_text(json.dumps(data, indent=2))
except Exception as e:
log.error(f"Failed to save data: {e}")
# ββ In-memory rate limit state βββββββββββββββββββββββββββββββββββββββββββββββββ
# keyed by (provider_id, model_name)
class RateLimitState:
def __init__(self):
self._lock = asyncio.Lock()
self._cooldown: Dict[str, float] = {}
self._timestamps: Dict[str, deque] = defaultdict(lambda: deque(maxlen=10000))
self._rr_index: Dict[str, int] = defaultdict(int)
def key(self, provider_id: str, model_name: str) -> str:
return f"{provider_id}:{model_name}"
async def mark_rate_limited(self, provider_id: str, model_name: str, cooldown_secs: float = 60.0):
async with self._lock:
k = self.key(provider_id, model_name)
self._cooldown[k] = time.time() + cooldown_secs
log.warning(f"Rate limited: {k} β cooling for {cooldown_secs}s")
async def is_available(self, provider_id: str, model_name: str, provider_cfg: dict) -> bool:
async with self._lock:
k = self.key(provider_id, model_name)
now = time.time()
if self._cooldown.get(k, 0) > now:
return False
rate_type = provider_cfg.get("rate_type", "none")
rate_limit = provider_cfg.get("rate_limit", 0)
if rate_type == "none" or not rate_limit:
return True
window = {"rpm": 60, "rps": 1, "rpd": 86400}.get(rate_type, 60)
cutoff = now - window
dq = self._timestamps[k]
while dq and dq[0] < cutoff:
dq.popleft()
return len(dq) < rate_limit
async def record_request(self, provider_id: str, model_name: str):
async with self._lock:
k = self.key(provider_id, model_name)
self._timestamps[k].append(time.time())
def get_cooldown_remaining(self, provider_id: str, model_name: str) -> float:
k = self.key(provider_id, model_name)
remaining = self._cooldown.get(k, 0) - time.time()
return max(0.0, remaining)
async def next_rr(self, pool_id: str, count: int) -> int:
async with self._lock:
idx = self._rr_index[pool_id] % count
self._rr_index[pool_id] = (idx + 1) % count
return idx
rl_state = RateLimitState()
# ββ Auth βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ADMIN_KEY = os.getenv("ADMIN_KEY", "admin-changeme")
async def require_admin(authorization: Optional[str] = Header(None)):
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing Authorization header")
token = authorization[7:]
if token != ADMIN_KEY:
raise HTTPException(status_code=403, detail="Invalid admin key")
return token
async def require_api_key(authorization: Optional[str] = Header(None)):
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing Authorization header")
token = authorization[7:]
if token == ADMIN_KEY:
return token
data = load_data()
for k in data["api_keys"].values():
if k["key"] == token:
return token
raise HTTPException(status_code=403, detail="Invalid API key")
# ββ Routing logic ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def resolve_endpoint(model_str: str, data: dict):
if "/" in model_str:
parts = model_str.split("/", 1)
provider_id, model_name = parts[0], parts[1]
provider = data["providers"].get(provider_id)
if not provider:
raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
return provider, model_name, provider_id
pool = None
pool_id = None
for pid, p in data["pool_models"].items():
if p["name"] == model_str or pid == model_str:
pool = p
pool_id = pid
break
if not pool:
raise HTTPException(status_code=404, detail=f"Model or pool '{model_str}' not found.")
members = pool.get("members", [])
if not members:
raise HTTPException(status_code=503, detail=f"Pool '{model_str}' has no members")
strategy = pool.get("strategy", "round_robin")
if strategy == "round_robin":
start = await rl_state.next_rr(pool_id, len(members))
for i in range(len(members)):
m = members[(start + i) % len(members)]
provider = data["providers"].get(m["provider_id"])
if not provider:
continue
if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
return provider, m["model_name"], m["provider_id"]
elif strategy == "priority":
for m in members:
provider = data["providers"].get(m["provider_id"])
if not provider:
continue
if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
return provider, m["model_name"], m["provider_id"]
elif strategy == "weighted":
import random
available = []
for m in members:
provider = data["providers"].get(m["provider_id"])
if not provider:
continue
if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
available.append((m, provider))
if available:
weights = [m[0].get("weight", 1) for m in available]
chosen_m, chosen_p = random.choices(available, weights=weights, k=1)[0]
return chosen_p, chosen_m["model_name"], chosen_m["provider_id"]
raise HTTPException(status_code=429, detail=f"All endpoints in pool '{model_str}' are currently rate-limited.")
# ββ Proxy logic ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TIMEOUT = httpx.Timeout(320.0, connect=30.0, read=300.0, write=30.0)
def _extract_retry_after(response: httpx.Response) -> float:
ra = response.headers.get("retry-after", "")
try:
return float(ra)
except Exception:
return 60.0
async def proxy_request(provider: dict, model_name: str, provider_id: str, body: dict, stream: bool):
base_url = provider["base_url"].rstrip("/")
api_key = provider["api_key"]
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
body = {**body, "model": model_name}
url = f"{base_url}/chat/completions"
await rl_state.record_request(provider_id, model_name)
if stream:
async def stream_gen():
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
async with client.stream("POST", url, json=body, headers=headers) as resp:
if resp.status_code == 429:
cooldown = _extract_retry_after(resp)
await rl_state.mark_rate_limited(provider_id, model_name, cooldown)
yield f"data: {json.dumps({'error': 'rate_limited', 'provider': provider_id})}\n\n"
return
async for chunk in resp.aiter_bytes():
yield chunk
return StreamingResponse(stream_gen(), media_type="text/event-stream")
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
resp = await client.post(url, json=body, headers=headers)
if resp.status_code == 429:
cooldown = _extract_retry_after(resp)
await rl_state.mark_rate_limited(provider_id, model_name, cooldown)
raise HTTPException(status_code=429, detail=f"Upstream rate limit hit on {provider_id}/{model_name}. Retry after {cooldown}s.")
if resp.status_code >= 500:
raise HTTPException(status_code=502, detail=f"Upstream error from {provider_id}: {resp.status_code}")
if resp.status_code >= 400:
try:
detail = resp.json()
except Exception:
detail = resp.text
raise HTTPException(status_code=resp.status_code, detail=detail)
return JSONResponse(content=resp.json())
# ββ App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@asynccontextmanager
async def lifespan(app: FastAPI):
log.info("RyzGateway starting up")
if not DATA_FILE.exists():
save_data(_default_data())
log.info(f"Initialized empty data at {DATA_FILE}")
yield
log.info("RyzGateway shutting down")
app = FastAPI(title="RyzGateway", lifespan=lifespan)
# ββ OpenAI-compatible endpoints ββββββββββββββββββββββββββββββββββββββββββββββββ
class ChatMessage(BaseModel):
role: str
content: Any
class ChatRequest(BaseModel):
model: str
messages: List[ChatMessage]
stream: Optional[bool] = False
temperature: Optional[float] = None
max_tokens: Optional[int] = None
top_p: Optional[float] = None
model_config = {"extra": "allow"}
@app.post("/v1/chat/completions")
async def chat_completions(request: ChatRequest, _key: str = Depends(require_api_key)):
data = load_data()
provider, model_name, provider_id = await resolve_endpoint(request.model, data)
body = request.model_dump(exclude_none=True, exclude={"model"})
body.update(request.model_extra or {})
return await proxy_request(provider, model_name, provider_id, body, request.stream or False)
@app.get("/v1/models")
async def list_models(_key: str = Depends(require_api_key)):
data = load_data()
now = int(time.time())
models = []
for pid, p in data["providers"].items():
for m in p.get("known_models", []):
models.append({
"id": f"{pid}/{m}",
"object": "model",
"created": now,
"owned_by": pid,
})
for pool_id, pool in data["pool_models"].items():
models.append({
"id": pool["name"],
"object": "model",
"created": now,
"owned_by": "pool",
})
return {"object": "list", "data": models}
# ββ Admin API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/admin/api-keys")
async def get_api_keys(_=Depends(require_admin)):
data = load_data()
return list(data["api_keys"].values())
@app.post("/admin/api-keys")
async def create_api_key(body: dict, _=Depends(require_admin)):
data = load_data()
kid = str(uuid.uuid4())[:8]
key = "ryz-" + str(uuid.uuid4()).replace("-", "")
data["api_keys"][kid] = {
"id": kid,
"key": key,
"name": body.get("name", "Unnamed Key"),
"created_at": int(time.time()),
}
save_data(data)
return data["api_keys"][kid]
@app.delete("/admin/api-keys/{kid}")
async def delete_api_key(kid: str, _=Depends(require_admin)):
data = load_data()
if kid not in data["api_keys"]:
raise HTTPException(status_code=404, detail="Key not found")
del data["api_keys"][kid]
save_data(data)
return {"deleted": True}
# --- Providers ---
@app.get("/admin/providers")
async def get_providers(_=Depends(require_admin)):
data = load_data()
result = []
for pid, p in data["providers"].items():
entry = {**p, "id": pid}
raw = entry.get("api_key", "")
entry["api_key_masked"] = raw[:6] + "..." + raw[-4:] if len(raw) > 10 else "****"
entry.pop("api_key", None)
cooldowns = {}
for m in p.get("known_models", []):
cd = rl_state.get_cooldown_remaining(pid, m)
if cd > 0:
cooldowns[m] = round(cd, 1)
entry["active_cooldowns"] = cooldowns
result.append(entry)
return result
@app.post("/admin/providers")
async def create_provider(body: dict, _=Depends(require_admin)):
data = load_data()
pid = body.get("id") or re.sub(r"[^a-z0-9_-]", "-", body.get("name", "provider").lower())[:32]
if pid in data["providers"]:
raise HTTPException(status_code=409, detail=f"Provider ID '{pid}' already exists")
data["providers"][pid] = {
"name": body.get("name", pid),
"base_url": body.get("base_url", ""),
"api_key": body.get("api_key", ""),
"rate_type": body.get("rate_type", "none"),
"rate_limit": int(body.get("rate_limit", 0)),
"known_models": body.get("known_models", []),
"notes": body.get("notes", ""),
}
save_data(data)
return {"id": pid, **data["providers"][pid], "api_key": "[hidden]"}
@app.put("/admin/providers/{pid}")
async def update_provider(pid: str, body: dict, _=Depends(require_admin)):
data = load_data()
if pid not in data["providers"]:
raise HTTPException(status_code=404, detail="Provider not found")
p = data["providers"][pid]
for field in ["name", "base_url", "rate_type", "notes", "known_models"]:
if field in body:
p[field] = body[field]
if "rate_limit" in body:
p["rate_limit"] = int(body["rate_limit"])
if "api_key" in body and body["api_key"] and not body["api_key"].startswith("****"):
p["api_key"] = body["api_key"]
save_data(data)
return {"id": pid, **p, "api_key": "[hidden]"}
@app.delete("/admin/providers/{pid}")
async def delete_provider(pid: str, _=Depends(require_admin)):
data = load_data()
if pid not in data["providers"]:
raise HTTPException(status_code=404, detail="Provider not found")
del data["providers"][pid]
save_data(data)
return {"deleted": True}
@app.post("/admin/providers/{pid}/fetch-models")
async def fetch_provider_models(pid: str, _=Depends(require_admin)):
data = load_data()
if pid not in data["providers"]:
raise HTTPException(status_code=404, detail="Provider not found")
p = data["providers"][pid]
base_url = p["base_url"].rstrip("/")
api_key = p["api_key"]
if not base_url or not api_key:
raise HTTPException(status_code=400, detail="Provider base_url or api_key is missing.")
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
try:
async with httpx.AsyncClient(timeout=15.0) as client:
resp = await client.get(f"{base_url}/models", headers=headers)
if resp.status_code != 200:
raise HTTPException(status_code=502, detail=f"Upstream returned {resp.status_code}: {resp.text[:200]}")
resp_data = resp.json()
models = []
if isinstance(resp_data, dict) and "data" in resp_data:
for item in resp_data["data"]:
if "id" in item:
models.append(item["id"])
elif isinstance(resp_data, list):
for item in resp_data:
if isinstance(item, dict) and "id" in item:
models.append(item["id"])
elif isinstance(item, str):
models.append(item)
if not models:
raise HTTPException(status_code=404, detail="No models found in upstream /v1/models response.")
# Remove duplicates and sort
unique_models = sorted(list(set(models)))
p["known_models"] = unique_models
save_data(data)
return {"fetched_count": len(unique_models), "models": unique_models}
except httpx.RequestError as e:
raise HTTPException(status_code=502, detail=f"Network error fetching models: {str(e)}")
# --- Pool Models ---
@app.get("/admin/pools")
async def get_pools(_=Depends(require_admin)):
data = load_data()
result = []
for pool_id, pool in data["pool_models"].items():
entry = {**pool, "id": pool_id}
annotated = []
for m in pool.get("members", []):
provider = data["providers"].get(m["provider_id"], {})
available = await rl_state.is_available(m["provider_id"], m["model_name"], provider)
cooldown = rl_state.get_cooldown_remaining(m["provider_id"], m["model_name"])
annotated.append({**m, "available": available, "cooldown_remaining": round(cooldown, 1)})
entry["members"] = annotated
result.append(entry)
return result
@app.post("/admin/pools")
async def create_pool(body: dict, _=Depends(require_admin)):
data = load_data()
pool_id = body.get("id") or re.sub(r"[^a-z0-9_-]", "-", body.get("name", "pool").lower())[:32]
if pool_id in data["pool_models"]:
raise HTTPException(status_code=409, detail=f"Pool ID '{pool_id}' already exists")
data["pool_models"][pool_id] = {
"name": body.get("name", pool_id),
"strategy": body.get("strategy", "round_robin"),
"members": body.get("members", []),
}
save_data(data)
return {"id": pool_id, **data["pool_models"][pool_id]}
@app.put("/admin/pools/{pool_id}")
async def update_pool(pool_id: str, body: dict, _=Depends(require_admin)):
data = load_data()
if pool_id not in data["pool_models"]:
raise HTTPException(status_code=404, detail="Pool not found")
p = data["pool_models"][pool_id]
for field in ["name", "strategy", "members"]:
if field in body:
p[field] = body[field]
save_data(data)
return {"id": pool_id, **p}
@app.delete("/admin/pools/{pool_id}")
async def delete_pool(pool_id: str, _=Depends(require_admin)):
data = load_data()
if pool_id not in data["pool_models"]:
raise HTTPException(status_code=404, detail="Pool not found")
del data["pool_models"][pool_id]
save_data(data)
return {"deleted": True}
# --- Status ---
@app.get("/admin/status")
async def get_status(_=Depends(require_admin)):
data = load_data()
return {
"providers": len(data["providers"]),
"pools": len(data["pool_models"]),
"api_keys": len(data["api_keys"]),
}
# ββ Dashboard (Mobile-Optimized Glassmorphism UI) ββββββββββββββββββββββββββββββ
DASHBOARD_HTML = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>RyzGateway</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
:root {
--bg: #0a0a0f;
--surface-glass: rgba(20, 20, 30, 0.55);
--surface-glass-strong: rgba(30, 30, 45, 0.7);
--border-glass: rgba(255, 255, 255, 0.1);
--border-glass-strong: rgba(255, 255, 255, 0.2);
--text: #f0f0f5;
--muted: #8888a0;
--accent: #7c6af7;
--accent-dim: rgba(124,106,247,0.2);
--accent-hover: #9080ff;
--green: #4ade80;
--green-dim: rgba(74,222,128,0.15);
--red: #f87171;
--red-dim: rgba(248,113,113,0.15);
--amber: #fbbf24;
--amber-dim: rgba(251,191,36,0.15);
--radius: 10px;
--radius-lg: 16px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
html, body { height: 100%; overscroll-behavior: none; }
body {
background: var(--bg);
color: var(--text);
min-height: 100vh;
background-image:
radial-gradient(circle at 0% 0%, rgba(124, 106, 247, 0.15) 0%, transparent 40%),
radial-gradient(circle at 100% 100%, rgba(74, 222, 128, 0.1) 0%, transparent 40%);
background-attachment: fixed;
}
/* Layout */
.app { display: flex; min-height: 100vh; }
.sidebar {
width: 260px;
flex-shrink: 0;
background: var(--surface-glass);
backdrop-filter: blur(20px) saturate(150%);
-webkit-backdrop-filter: blur(20px) saturate(150%);
border-right: 1px solid var(--border-glass);
padding: 1.5rem 0;
display: flex;
flex-direction: column;
position: fixed;
top: 0; left: 0; height: 100vh;
z-index: 100;
transition: transform 0.3s ease;
}
.main {
flex: 1;
padding: 2rem;
max-width: 960px;
margin-left: 260px;
}
.mobile-header {
display: none;
position: sticky;
top: 0;
background: var(--surface-glass);
backdrop-filter: blur(20px) saturate(150%);
-webkit-backdrop-filter: blur(20px) saturate(150%);
border-bottom: 1px solid var(--border-glass);
padding: 1rem;
z-index: 90;
align-items: center;
justify-content: space-between;
}
.menu-btn {
background: rgba(255,255,255,0.1);
border: 1px solid var(--border-glass);
color: var(--text);
width: 40px; height: 40px;
border-radius: 10px;
display: flex; align-items: center; justify-content: center;
cursor: pointer;
}
/* Sidebar */
.logo { padding: 0 1.25rem 1.5rem; border-bottom: 1px solid var(--border-glass); margin-bottom: 0.75rem; }
.logo-text { font-size: 1.2rem; font-weight: 700; color: var(--text); letter-spacing: -0.02em; }
.logo-sub { font-size: 0.75rem; color: var(--muted); margin-top: 4px; }
.nav-item { display: flex; align-items: center; gap: 12px; padding: 0.75rem 1.25rem; color: var(--muted); cursor: pointer; font-size: 0.9rem; transition: all 0.2s; border-left: 3px solid transparent; }
.nav-item:hover { color: var(--text); background: rgba(255,255,255,0.05); }
.nav-item.active { color: var(--text); background: var(--accent-dim); border-left-color: var(--accent); }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-section { font-size: 0.7rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; padding: 1.25rem 1.25rem 0.5rem; }
/* Auth gate */
#auth-gate { position: fixed; inset: 0; background: rgba(0,0,0,0.8); backdrop-filter: blur(10px); display: flex; align-items: center; justify-content: center; z-index: 999; padding: 1rem; }
.auth-box { background: var(--surface-glass-strong); backdrop-filter: blur(30px) saturate(150%); -webkit-backdrop-filter: blur(30px) saturate(150%); border: 1px solid var(--border-glass-strong); border-radius: var(--radius-lg); padding: 2rem; width: 100%; max-width: 360px; }
.auth-box h2 { font-size: 1.25rem; margin-bottom: 0.5rem; }
.auth-box p { color: var(--muted); font-size: 0.85rem; margin-bottom: 1.5rem; }
/* Page header */
.page-header { margin-bottom: 1.75rem; }
.page-title { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }
.page-sub { color: var(--muted); font-size: 0.875rem; margin-top: 4px; }
/* Cards */
.card { background: var(--surface-glass); backdrop-filter: blur(15px); -webkit-backdrop-filter: blur(15px); border: 1px solid var(--border-glass); border-radius: var(--radius-lg); padding: 1.25rem; transition: border-color 0.2s; }
.card + .card { margin-top: 1rem; }
/* Stats row */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; margin-bottom: 1.5rem; }
.stat { background: var(--surface-glass); backdrop-filter: blur(15px); border: 1px solid var(--border-glass); border-radius: var(--radius); padding: 1rem; text-align: center; }
.stat-val { font-size: 1.5rem; font-weight: 700; color: var(--text); }
.stat-label { font-size: 0.7rem; color: var(--muted); margin-top: 4px; text-transform: uppercase; letter-spacing: 0.05em; }
/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th { text-align: left; color: var(--muted); font-weight: 500; font-size: 0.75rem; padding: 0 0.75rem 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; }
td { padding: 0.875rem 0.75rem; border-top: 1px solid var(--border-glass); vertical-align: middle; }
tr:hover td { background: rgba(255,255,255,0.03); }
/* Badges */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 99px; font-size: 0.7rem; font-weight: 600; }
.badge-green { background: var(--green-dim); color: var(--green); }
.badge-red { background: var(--red-dim); color: var(--red); }
.badge-amber { background: var(--amber-dim); color: var(--amber); }
.badge-purple { background: var(--accent-dim); color: var(--accent); }
.badge-gray { background: rgba(255,255,255,0.08); color: var(--muted); }
.dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 0.5rem 1rem; border-radius: var(--radius); border: 1px solid var(--border-glass-strong); background: rgba(255,255,255,0.05); color: var(--text); font-size: 0.85rem; cursor: pointer; transition: all 0.2s; white-space: nowrap; }
.btn:hover { background: rgba(255,255,255,0.1); }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { background: var(--red-dim); border-color: rgba(248,113,113,0.3); color: var(--red); }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.75rem; }
.btn-row { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 1rem; }
/* Forms */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-grid.full { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.span2 { grid-column: span 2; }
label { font-size: 0.8rem; color: var(--muted); font-weight: 500; }
input, select, textarea { background: rgba(0,0,0,0.2); border: 1px solid var(--border-glass-strong); border-radius: var(--radius); color: var(--text); padding: 0.65rem 0.85rem; font-size: 0.9rem; font-family: inherit; width: 100%; transition: border-color 0.2s; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
select option { background: #1a1a25; }
textarea { min-height: 70px; resize: vertical; }
/* Modal */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); backdrop-filter: blur(5px); display: flex; align-items: flex-end; justify-content: center; z-index: 500; padding: 0; }
.modal { background: var(--surface-glass-strong); backdrop-filter: blur(30px) saturate(150%); -webkit-backdrop-filter: blur(30px) saturate(150%); border: 1px solid var(--border-glass-strong); border-radius: 20px 20px 0 0; width: 100%; max-width: 600px; max-height: 90vh; overflow-y: auto; animation: slideUp 0.3s ease-out; }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal-header { padding: 1.5rem 1.5rem 1rem; border-bottom: 1px solid var(--border-glass); display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: inherit; z-index: 10; }
.modal-title { font-size: 1.1rem; font-weight: 600; }
.modal-body { padding: 1.5rem; }
.modal-footer { padding: 1rem 1.5rem; border-top: 1px solid var(--border-glass); display: flex; justify-content: flex-end; gap: 0.5rem; position: sticky; bottom: 0; background: inherit; }
.close-btn { background: rgba(255,255,255,0.05); border: 1px solid var(--border-glass); color: var(--muted); cursor: pointer; font-size: 1.2rem; line-height: 1; padding: 6px 10px; border-radius: 8px; }
.close-btn:hover { color: var(--text); background: rgba(255,255,255,0.1); }
/* Members list in pool */
.member-item { display: flex; align-items: center; gap: 8px; background: rgba(255,255,255,0.03); border: 1px solid var(--border-glass); border-radius: var(--radius); padding: 0.65rem 0.85rem; margin-bottom: 0.5rem; }
.member-item .member-label { flex: 1; font-size: 0.85rem; word-break: break-all; }
.member-badge { font-size: 0.75rem; color: var(--muted); }
/* Code / key display */
.key-display { font-family: 'SF Mono', Consolas, monospace; font-size: 0.8rem; background: rgba(0,0,0,0.3); border: 1px solid var(--border-glass); border-radius: var(--radius); padding: 0.65rem 0.85rem; word-break: break-all; color: var(--green); }
.code-block { font-family: 'SF Mono', Consolas, monospace; font-size: 0.8rem; background: rgba(0,0,0,0.3); border: 1px solid var(--border-glass); border-radius: var(--radius); padding: 1rem; overflow-x: auto; line-height: 1.6; }
/* Toast */
#toast { position: fixed; bottom: 2rem; left: 50%; transform: translateX(-50%); background: var(--surface-glass-strong); backdrop-filter: blur(20px); border: 1px solid var(--border-glass-strong); border-radius: 99px; padding: 0.75rem 1.5rem; font-size: 0.85rem; z-index: 9999; opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; max-width: 90%; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
/* Empty state */
.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); font-size: 0.9rem; }
/* Sections */
.section { display: none; }
.section.active { display: block; }
/* Callout */
.callout { background: var(--accent-dim); border: 1px solid rgba(124,106,247,0.3); border-radius: var(--radius); padding: 1rem; font-size: 0.85rem; color: var(--text); margin-bottom: 1.25rem; line-height: 1.6; }
.callout code { background: rgba(0,0,0,0.3); border-radius: 4px; padding: 2px 6px; font-family: monospace; font-size: 0.8rem; color: var(--accent); }
/* Mobile optimizations */
@media (max-width: 768px) {
.sidebar { transform: translateX(-100%); box-shadow: 5px 0 30px rgba(0,0,0,0.5); }
.sidebar.open { transform: translateX(0); }
.main { margin-left: 0; padding: 1rem; padding-bottom: 3rem; }
.mobile-header { display: flex; }
.form-grid { grid-template-columns: 1fr; }
.field.span2 { grid-column: span 1; }
.page-title { font-size: 1.25rem; }
.stats { gap: 0.5rem; }
.stat-val { font-size: 1.25rem; }
.card { padding: 1rem; }
th, td { padding: 0.65rem 0.5rem; font-size: 0.8rem; }
.hide-mobile { display: none; }
}
/* Prevent body scroll when menu open */
body.menu-open { overflow: hidden; }
.menu-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 99; }
body.menu-open .menu-overlay { display: block; }
</style>
</head>
<body>
<div id="auth-gate">
<div class="auth-box">
<h2>RyzGateway</h2>
<p>Enter your admin key to access the dashboard.</p>
<div class="field" style="margin-bottom:1rem">
<label>Admin Key</label>
<input type="password" id="login-key" placeholder="sk-admin-..." />
</div>
<button class="btn btn-primary" style="width:100%; justify-content:center;" onclick="doLogin()">Sign in</button>
<div id="login-err" style="color:var(--red);font-size:0.8rem;margin-top:0.75rem;display:none">Invalid key.</div>
</div>
</div>
<div class="app" id="app-shell" style="display:none">
<div class="menu-overlay" onclick="toggleMenu()"></div>
<aside class="sidebar" id="sidebar">
<div class="logo">
<div class="logo-text">RyzGateway</div>
<div class="logo-sub">BYOK AI Router</div>
</div>
<div class="nav-section">Overview</div>
<div class="nav-item active" onclick="nav('overview', this)">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
Dashboard
</div>
<div class="nav-section">Configure</div>
<div class="nav-item" onclick="nav('providers', this)">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/></svg>
Providers
</div>
<div class="nav-item" onclick="nav('pools', this)">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>
Model Pools
</div>
<div class="nav-item" onclick="nav('keys', this)">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="m21 2-2 2m-7.61 7.61a5.5 5.5 0 1 1-7.778 7.778 5.5 5.5 0 0 1 7.777-7.777zm0 0L15.5 7.5m0 0 3 3L22 7l-3-3m-3.5 3.5L19 4"/></svg>
API Keys
</div>
<div class="nav-section">Reference</div>
<div class="nav-item" onclick="nav('docs', this)">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
Usage Docs
</div>
</aside>
<main class="main">
<div class="mobile-header">
<button class="menu-btn" onclick="toggleMenu()">
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2"><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="18" x2="21" y2="18"/></svg>
</button>
<div style="font-weight:600">RyzGateway</div>
<div style="width:40px"></div>
</div>
<!-- OVERVIEW -->
<div class="section active" id="sec-overview">
<div class="page-header">
<div class="page-title">Dashboard</div>
<div class="page-sub">Gateway status and routing health</div>
</div>
<div class="stats" id="stats-row">
<div class="stat"><div class="stat-val" id="stat-providers">β</div><div class="stat-label">Providers</div></div>
<div class="stat"><div class="stat-val" id="stat-pools">β</div><div class="stat-label">Pools</div></div>
<div class="stat"><div class="stat-val" id="stat-keys">β</div><div class="stat-label">Keys</div></div>
</div>
<div class="card">
<div style="font-size:0.9rem;font-weight:600;margin-bottom:1rem">Provider health</div>
<div id="health-list"><div class="empty">No providers configured</div></div>
</div>
</div>
<!-- PROVIDERS -->
<div class="section" id="sec-providers">
<div class="page-header">
<div class="page-title">Providers</div>
<div class="page-sub">API endpoints with rate limit configuration</div>
</div>
<div class="btn-row">
<button class="btn btn-primary" onclick="openProviderModal()">+ Add provider</button>
<button class="btn" onclick="loadProviders()">βΊ Refresh</button>
</div>
<div class="callout">
<strong>Provider ID</strong> is auto-generated from the name and used in model routing. Use <code>provider-id/exact-model-name</code> to route directly. Click <strong>Fetch Models</strong> on a provider to auto-import available models.
</div>
<div class="card">
<div class="table-wrap">
<table>
<thead><tr>
<th>Provider</th><th class="hide-mobile">Base URL</th><th>Rate limit</th><th>Models</th><th>Status</th><th></th>
</tr></thead>
<tbody id="providers-tbody"><tr><td colspan="6"><div class="empty">Loading...</div></td></tr></tbody>
</table>
</div>
</div>
</div>
<!-- POOLS -->
<div class="section" id="sec-pools">
<div class="page-header">
<div class="page-title">Model Pools</div>
<div class="page-sub">Generic model names that route across multiple providers</div>
</div>
<div class="btn-row">
<button class="btn btn-primary" onclick="openPoolModal()">+ Add pool</button>
<button class="btn" onclick="loadPools()">βΊ Refresh</button>
</div>
<div class="callout">
Pool names are generic model names exposed in <code>/v1/models</code>. The gateway routes requests using your chosen strategy and automatically skips rate-limited endpoints.
</div>
<div id="pools-list"><div class="empty">Loading...</div></div>
</div>
<!-- API KEYS -->
<div class="section" id="sec-keys">
<div class="page-header">
<div class="page-title">API Keys</div>
<div class="page-sub">Keys issued to clients for /v1/ access</div>
</div>
<div class="btn-row">
<button class="btn btn-primary" onclick="openKeyModal()">+ Create key</button>
<button class="btn" onclick="loadKeys()">βΊ Refresh</button>
</div>
<div class="card">
<div class="table-wrap">
<table>
<thead><tr><th>Name</th><th>Key</th><th class="hide-mobile">Created</th><th></th></tr></thead>
<tbody id="keys-tbody"><tr><td colspan="4"><div class="empty">Loading...</div></td></tr></tbody>
</table>
</div>
</div>
</div>
<!-- DOCS -->
<div class="section" id="sec-docs">
<div class="page-header">
<div class="page-title">Usage docs</div>
<div class="page-sub">How to use RyzGateway in your apps</div>
</div>
<div class="card" style="margin-bottom:1rem">
<div style="font-size:0.9rem;font-weight:600;margin-bottom:0.75rem">Base URL</div>
<div class="key-display" id="base-url-display"></div>
<div style="font-size:0.8rem;color:var(--muted);margin-top:0.5rem">Point any OpenAI-compatible client at this URL.</div>
</div>
<div class="card" style="margin-bottom:1rem">
<div style="font-size:0.9rem;font-weight:600;margin-bottom:0.75rem">Model name formats</div>
<div style="font-size:0.85rem;line-height:1.8;color:var(--text)">
<div style="margin-bottom:0.75rem"><span style="font-family:monospace;background:rgba(0,0,0,0.3);padding:3px 8px;border-radius:6px;color:var(--accent)">provider-id/exact-model-name</span> β Direct passthrough.</div>
<div><span style="font-family:monospace;background:rgba(0,0,0,0.3);padding:3px 8px;border-radius:6px;color:var(--green)">pool-name</span> β Pool routing.</div>
</div>
</div>
<div class="card" style="margin-bottom:1rem">
<div style="font-size:0.9rem;font-weight:600;margin-bottom:0.75rem">Example: Python (openai SDK)</div>
<pre class="code-block"><code>import openai
client = openai.OpenAI(
base_url="<span id="doc-base-url"></span>",
api_key="YOUR_RYZGATEWAY_API_KEY",
)
# Direct provider routing
resp = client.chat.completions.create(
model="openrouter/google/gemini-flash-1.5",
messages=[{"role": "user", "content": "Hello!"}]
)
# Pool routing (auto-selects available endpoint)
resp = client.chat.completions.create(
model="fast-model",
messages=[{"role": "user", "content": "Hello!"}]
)</code></pre>
</div>
<div class="card">
<div style="font-size:0.9rem;font-weight:600;margin-bottom:0.75rem">Rate limit types</div>
<table style="font-size:0.85rem">
<thead><tr><th>Type</th><th>Meaning</th><th>Window</th></tr></thead>
<tbody>
<tr><td><span class="badge badge-purple">RPM</span></td><td>Requests per minute</td><td>60s</td></tr>
<tr><td><span class="badge badge-green">RPS</span></td><td>Requests per second</td><td>1s</td></tr>
<tr><td><span class="badge badge-amber">RPD</span></td><td>Requests per day</td><td>86400s</td></tr>
<tr><td><span class="badge badge-gray">None</span></td><td>No limit tracked</td><td>β</td></tr>
</tbody>
</table>
</div>
</div>
</main>
</div>
<!-- Modals -->
<div class="overlay" id="modal-overlay" style="display:none" onclick="if(event.target===this)closeModal()">
<div class="modal" id="modal-box">
<div class="modal-header">
<div class="modal-title" id="modal-title">Modal</div>
<button class="close-btn" onclick="closeModal()">β</button>
</div>
<div class="modal-body" id="modal-body"></div>
<div class="modal-footer" id="modal-footer"></div>
</div>
</div>
<div id="toast"></div>
<script>
let ADMIN_KEY = '';
const BASE = window.location.origin;
// ββ Auth ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function doLogin() {
const k = document.getElementById('login-key').value.trim();
const err = document.getElementById('login-err');
err.style.display = 'none';
try {
const r = await fetch('/admin/status', { headers: { Authorization: 'Bearer ' + k } });
if (!r.ok) { err.style.display = 'block'; return; }
ADMIN_KEY = k;
document.getElementById('auth-gate').style.display = 'none';
document.getElementById('app-shell').style.display = 'flex';
document.getElementById('base-url-display').textContent = BASE;
document.getElementById('doc-base-url').textContent = BASE + '/v1';
initApp();
} catch(e) { err.style.display = 'block'; }
}
document.getElementById('login-key').addEventListener('keydown', e => { if(e.key === 'Enter') doLogin(); });
async function api(method, path, body) {
const opts = { method, headers: { 'Authorization': 'Bearer ' + ADMIN_KEY, 'Content-Type': 'application/json' } };
if (body) opts.body = JSON.stringify(body);
const r = await fetch(path, opts);
if (!r.ok) {
const t = await r.text();
let msg = t;
try { const j = JSON.parse(t); msg = j.detail || t; } catch(e) {}
throw new Error(msg);
}
return r.json();
}
// ββ Navigation βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function toggleMenu() {
document.body.classList.toggle('menu-open');
document.getElementById('sidebar').classList.toggle('open');
}
function nav(id, el) {
document.querySelectorAll('.section').forEach(s => s.classList.remove('active'));
document.querySelectorAll('.nav-item').forEach(n => n.classList.remove('active'));
document.getElementById('sec-' + id).classList.add('active');
if(el) el.classList.add('active');
if (window.innerWidth <= 768) toggleMenu();
if (id === 'overview') loadOverview();
if (id === 'providers') loadProviders();
if (id === 'pools') loadPools();
if (id === 'keys') loadKeys();
}
function initApp() {
loadOverview();
loadProviders();
loadPools();
loadKeys();
}
// ββ Toast ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let toastTimer;
function toast(msg, type='ok') {
const el = document.getElementById('toast');
el.textContent = msg;
el.style.color = type === 'err' ? 'var(--red)' : 'var(--text)';
el.style.borderColor = type === 'err' ? 'var(--red-dim)' : 'var(--border-glass-strong)';
el.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => el.classList.remove('show'), 3500);
}
// ββ Modal ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function openModal(title, bodyHTML, footerHTML) {
document.getElementById('modal-title').textContent = title;
document.getElementById('modal-body').innerHTML = bodyHTML;
document.getElementById('modal-footer').innerHTML = footerHTML;
document.getElementById('modal-overlay').style.display = 'flex';
}
function closeModal() { document.getElementById('modal-overlay').style.display = 'none'; }
// ββ Overview βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function loadOverview() {
try {
const [status, providers] = await Promise.all([api('GET', '/admin/status'), api('GET', '/admin/providers')]);
document.getElementById('stat-providers').textContent = status.providers;
document.getElementById('stat-pools').textContent = status.pools;
document.getElementById('stat-keys').textContent = status.api_keys;
const el = document.getElementById('health-list');
if (!providers.length) { el.innerHTML = '<div class="empty">No providers configured</div>'; return; }
el.innerHTML = providers.map(p => {
const cds = Object.entries(p.active_cooldowns || {});
const status = cds.length
? `<span class="badge badge-amber"><span class="dot"></span>${cds.length} cooling</span>`
: `<span class="badge badge-green"><span class="dot"></span>OK</span>`;
const rl = p.rate_type !== 'none' ? `<span class="badge badge-gray">${p.rate_limit} ${p.rate_type.toUpperCase()}</span>` : '<span class="badge badge-gray">no limit</span>';
return `<div style="display:flex;align-items:center;gap:12px;padding:0.75rem 0;border-bottom:1px solid var(--border-glass)">
<div style="flex:1;font-size:0.9rem;font-weight:500">${p.name} <span style="color:var(--muted);font-weight:400;font-size:0.8rem">${p.id}</span></div>
<div>${rl}</div>
<div>${status}</div>
</div>`;
}).join('');
} catch(e) { console.error(e); }
}
// ββ Providers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function loadProviders() {
try {
const providers = await api('GET', '/admin/providers');
const tbody = document.getElementById('providers-tbody');
if (!providers.length) { tbody.innerHTML = '<tr><td colspan="6"><div class="empty">No providers yet</div></td></tr>'; return; }
tbody.innerHTML = providers.map(p => {
const rl = p.rate_type !== 'none' ? `${p.rate_limit} ${p.rate_type.toUpperCase()}` : 'β';
const cds = Object.entries(p.active_cooldowns || {});
const statusBadge = cds.length
? `<span class="badge badge-amber"><span class="dot"></span>${cds.length}</span>`
: `<span class="badge badge-green"><span class="dot"></span>OK</span>`;
const models = (p.known_models || []).length;
return `<tr>
<td><div style="font-weight:500">${p.name}</div><div style="font-size:0.75rem;color:var(--muted)">${p.id}</div></td>
<td class="hide-mobile" style="font-size:0.8rem;color:var(--muted);max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${p.base_url}</td>
<td style="font-size:0.8rem">${rl}</td>
<td style="font-size:0.8rem">${models}</td>
<td>${statusBadge}</td>
<td style="text-align:right; white-space:nowrap;">
<button class="btn btn-sm" onclick='fetchModels("${p.id}")' title="Auto-fetch models from provider">Fetch</button>
<button class="btn btn-sm" onclick='editProvider(${JSON.stringify(p)})'>Edit</button>
<button class="btn btn-sm btn-danger" onclick="deleteProvider('${p.id}','${p.name}')">Del</button>
</td>
</tr>`;
}).join('');
} catch(e) { console.error(e); }
}
async function fetchModels(pid) {
toast('Fetching models...');
try {
const res = await api('POST', `/admin/providers/${pid}/fetch-models`);
toast(`Successfully imported ${res.fetched_count} models!`);
loadProviders();
loadOverview();
} catch(e) {
toast('Fetch failed: ' + e.message, 'err');
}
}
function openProviderModal(existing) {
const p = existing || {};
const isEdit = !!p.id;
const body = `
<div class="form-grid">
<div class="field"><label>Provider name</label><input id="p-name" value="${p.name||''}" placeholder="OpenRouter"></div>
<div class="field"><label>Provider ID</label><input id="p-id" value="${p.id||''}" placeholder="auto-generated" ${isEdit?'disabled':''}></div>
<div class="field span2"><label>Base URL</label><input id="p-url" value="${p.base_url||''}" placeholder="https://openrouter.ai/api/v1"></div>
<div class="field span2"><label>API Key</label><input id="p-key" type="password" value="" placeholder="${isEdit?'Leave blank to keep current':'sk-...'}"></div>
<div class="field"><label>Rate limit type</label>
<select id="p-rtype">
<option value="none" ${p.rate_type==='none'||!p.rate_type?'selected':''}>None</option>
<option value="rpm" ${p.rate_type==='rpm'?'selected':''}>RPM (per minute)</option>
<option value="rps" ${p.rate_type==='rps'?'selected':''}>RPS (per second)</option>
<option value="rpd" ${p.rate_type==='rpd'?'selected':''}>RPD (per day)</option>
</select>
</div>
<div class="field"><label>Rate limit value</label><input id="p-rlimit" type="number" value="${p.rate_limit||0}" min="0"></div>
<div class="field span2"><label>Known models (comma-separated)</label><input id="p-models" value="${(p.known_models||[]).join(', ')}" placeholder="gpt-4o, gpt-4o-mini, ..."></div>
<div class="field span2"><label>Notes</label><textarea id="p-notes">${p.notes||''}</textarea></div>
</div>
<div class="callout" style="margin-top:1rem;font-size:0.8rem">
Tip: Save the provider first, then click "Fetch" on the providers list to automatically pull and populate the models list.
</div>`;
const footer = `
<button class="btn" onclick="closeModal()">Cancel</button>
<button class="btn btn-primary" onclick="saveProvider(${isEdit?`'${p.id}'`:'null'})">${isEdit?'Save changes':'Add provider'}</button>`;
openModal(isEdit ? 'Edit provider' : 'Add provider', body, footer);
}
function editProvider(p) { openProviderModal(p); }
async function saveProvider(existingId) {
const name = document.getElementById('p-name').value.trim();
const id = document.getElementById('p-id').value.trim() || undefined;
const base_url = document.getElementById('p-url').value.trim();
const api_key = document.getElementById('p-key').value.trim();
const rate_type = document.getElementById('p-rtype').value;
const rate_limit = parseInt(document.getElementById('p-rlimit').value) || 0;
const known_models = document.getElementById('p-models').value.split(',').map(s=>s.trim()).filter(Boolean);
const notes = document.getElementById('p-notes').value.trim();
if (!name || !base_url) { toast('Name and Base URL are required', 'err'); return; }
try {
if (existingId) {
await api('PUT', `/admin/providers/${existingId}`, { name, base_url, api_key, rate_type, rate_limit, known_models, notes });
toast('Provider updated');
} else {
await api('POST', '/admin/providers', { name, id, base_url, api_key, rate_type, rate_limit, known_models, notes });
toast('Provider added');
}
closeModal();
loadProviders();
loadOverview();
} catch(e) { toast('Error: ' + e.message, 'err'); }
}
async function deleteProvider(id, name) {
if (!confirm(`Delete provider "${name}"?`)) return;
try { await api('DELETE', `/admin/providers/${id}`); toast('Deleted'); loadProviders(); loadOverview(); }
catch(e) { toast('Error: ' + e.message, 'err'); }
}
// ββ Pools ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let providersCache = [];
async function loadPools() {
try {
const [pools, providers] = await Promise.all([api('GET', '/admin/pools'), api('GET', '/admin/providers')]);
providersCache = providers;
const el = document.getElementById('pools-list');
if (!pools.length) { el.innerHTML = '<div class="empty">No pools yet. Add a pool to create a generic routed model.</div>'; return; }
el.innerHTML = pools.map(pool => {
const stratBadge = {round_robin:'<span class="badge badge-purple">Round robin</span>', priority:'<span class="badge badge-amber">Priority</span>', weighted:'<span class="badge badge-green">Weighted</span>'}[pool.strategy] || '';
const members = (pool.members||[]).map(m => {
const avail = m.available;
const cd = m.cooldown_remaining > 0 ? ` (${m.cooldown_remaining}s)` : '';
const badge = avail ? '<span class="badge badge-green"><span class="dot"></span>OK</span>' : `<span class="badge badge-red"><span class="dot"></span>RL${cd}</span>`;
return `<div class="member-item">
<div class="member-label"><strong>${m.provider_id}</strong> / ${m.model_name}</div>
${m.weight !== undefined ? `<span class="member-badge">w:${m.weight}</span>` : ''}
${badge}
</div>`;
}).join('');
return `<div class="card" style="margin-bottom:1rem">
<div style="display:flex;align-items:center;gap:10px;margin-bottom:1rem;flex-wrap:wrap">
<div style="flex:1;min-width: 150px">
<div style="font-weight:600;font-size:1rem">${pool.name}</div>
<div style="font-size:0.8rem;color:var(--muted)">${pool.id}</div>
</div>
${stratBadge}
<button class="btn btn-sm" onclick='openPoolModal(${JSON.stringify(pool)})'>Edit</button>
<button class="btn btn-sm btn-danger" onclick="deletePool('${pool.id}','${pool.name}')">Del</button>
</div>
${members || '<div style="color:var(--muted);font-size:0.85rem">No members</div>'}
</div>`;
}).join('');
} catch(e) { console.error(e); }
}
let poolMembers = [];
function openPoolModal(existing) {
const p = existing || {};
const isEdit = !!p.id;
poolMembers = JSON.parse(JSON.stringify(p.members || []));
const provOpts = providersCache.map(pr =>
`<option value="${pr.id}">${pr.name} (${pr.id})</option>`
).join('');
const body = `
<div class="form-grid" style="margin-bottom:1rem">
<div class="field"><label>Pool name (used as model ID)</label><input id="pool-name" value="${p.name||''}" placeholder="fast-model"></div>
<div class="field"><label>Strategy</label>
<select id="pool-strat">
<option value="round_robin" ${p.strategy==='round_robin'||!p.strategy?'selected':''}>Round robin</option>
<option value="priority" ${p.strategy==='priority'?'selected':''}>Priority (top-down)</option>
<option value="weighted" ${p.strategy==='weighted'?'selected':''}>Weighted random</option>
</select>
</div>
</div>
<div style="font-size:0.8rem;color:var(--muted);font-weight:500;margin-bottom:0.5rem">MEMBERS</div>
<div id="pool-members-list"></div>
<div style="background:rgba(0,0,0,0.2);border:1px solid var(--border-glass);border-radius:var(--radius);padding:1rem;margin-top:0.5rem">
<div style="font-size:0.8rem;color:var(--muted);margin-bottom:0.75rem">Add member</div>
<div class="form-grid" style="grid-template-columns: 1fr 1.5fr auto; gap:8px;align-items:end">
<div class="field"><label>Provider</label><select id="new-m-prov">${provOpts||'<option>No providers</option>'}</select></div>
<div class="field"><label>Model name</label><input id="new-m-model" placeholder="google/gemini-flash-1.5"></div>
<div class="field"><label>Wgt</label><input id="new-m-weight" type="number" value="1" min="1" style="width:70px"></div>
</div>
<button class="btn btn-sm" style="margin-top:10px;width:100%;justify-content:center" onclick="addPoolMember()">+ Add Member</button>
</div>`;
const footer = `
<button class="btn" onclick="closeModal()">Cancel</button>
<button class="btn btn-primary" onclick="savePool(${isEdit?`'${p.id}'`:'null'})">${isEdit?'Save changes':'Create pool'}</button>`;
openModal(isEdit ? 'Edit pool' : 'Create model pool', body, footer);
renderPoolMembers();
}
function renderPoolMembers() {
const el = document.getElementById('pool-members-list');
if (!el) return;
if (!poolMembers.length) { el.innerHTML = '<div style="color:var(--muted);font-size:0.85rem;margin-bottom:0.5rem">No members yet</div>'; return; }
el.innerHTML = poolMembers.map((m, i) =>
`<div class="member-item">
<div class="member-label">${m.provider_id} / ${m.model_name}</div>
<span class="member-badge">w:${m.weight||1}</span>
<button class="btn btn-sm btn-danger" onclick="removePoolMember(${i})">β</button>
</div>`
).join('');
}
function addPoolMember() {
const prov = document.getElementById('new-m-prov').value;
const model = document.getElementById('new-m-model').value.trim();
const weight = parseInt(document.getElementById('new-m-weight').value) || 1;
if (!prov || !model) { toast('Provider and model name required', 'err'); return; }
poolMembers.push({ provider_id: prov, model_name: model, weight });
document.getElementById('new-m-model').value = '';
renderPoolMembers();
}
function removePoolMember(i) {
poolMembers.splice(i, 1);
renderPoolMembers();
}
async function savePool(existingId) {
const name = document.getElementById('pool-name').value.trim();
const strategy = document.getElementById('pool-strat').value;
if (!name) { toast('Pool name is required', 'err'); return; }
try {
const payload = { name, strategy, members: poolMembers };
if (existingId) {
await api('PUT', `/admin/pools/${existingId}`, payload);
toast('Pool updated');
} else {
await api('POST', '/admin/pools', payload);
toast('Pool created');
}
closeModal();
loadPools();
} catch(e) { toast('Error: ' + e.message, 'err'); }
}
async function deletePool(id, name) {
if (!confirm(`Delete pool "${name}"?`)) return;
try { await api('DELETE', `/admin/pools/${id}`); toast('Deleted'); loadPools(); }
catch(e) { toast('Error: ' + e.message, 'err'); }
}
// ββ API Keys ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function loadKeys() {
try {
const keys = await api('GET', '/admin/api-keys');
const tbody = document.getElementById('keys-tbody');
if (!keys.length) { tbody.innerHTML = '<tr><td colspan="4"><div class="empty">No keys yet</div></td></tr>'; return; }
tbody.innerHTML = keys.map(k =>
`<tr>
<td style="font-weight:500">${k.name}</td>
<td><div class="key-display">${k.key}</div></td>
<td class="hide-mobile" style="color:var(--muted);font-size:0.8rem">${new Date(k.created_at*1000).toLocaleDateString()}</td>
<td style="text-align:right"><button class="btn btn-sm btn-danger" onclick="deleteKey('${k.id}','${k.name}')">Delete</button></td>
</tr>`
).join('');
} catch(e) { console.error(e); }
}
function openKeyModal() {
openModal('Create API key',
`<div class="field"><label>Key name / description</label><input id="key-name" placeholder="My SillyTavern instance"></div>`,
`<button class="btn" onclick="closeModal()">Cancel</button><button class="btn btn-primary" onclick="createKey()">Create</button>`
);
}
async function createKey() {
const name = document.getElementById('key-name').value.trim();
if (!name) { toast('Name required', 'err'); return; }
try {
await api('POST', '/admin/api-keys', { name });
closeModal();
toast('Key created');
loadKeys();
} catch(e) { toast('Error: ' + e.message, 'err'); }
}
async function deleteKey(id, name) {
if (!confirm(`Delete key "${name}"?`)) return;
try { await api('DELETE', `/admin/api-keys/${id}`); toast('Deleted'); loadKeys(); }
catch(e) { toast('Error: ' + e.message, 'err'); }
}
</script>
</body>
</html>"""
@app.get("/", response_class=HTMLResponse)
async def dashboard():
return DASHBOARD_HTML
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False) |