Update main.py
Browse files
main.py
CHANGED
|
@@ -9,7 +9,6 @@ from typing import AsyncGenerator, Dict, List, Any
|
|
| 9 |
|
| 10 |
import aiohttp
|
| 11 |
import brotli
|
| 12 |
-
import sys
|
| 13 |
import importlib
|
| 14 |
from fastapi import FastAPI, Request, HTTPException
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -110,7 +109,7 @@ def build_payload(messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
| 110 |
"isMemoryEnabled": False,
|
| 111 |
"mobileClient": False,
|
| 112 |
"userSelectedModel": None,
|
| 113 |
-
"validated":
|
| 114 |
"imageGenerationMode": False,
|
| 115 |
"webSearchModePrompt": False,
|
| 116 |
"deepSearchMode": True,
|
|
@@ -126,10 +125,10 @@ def build_payload(messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
| 126 |
},
|
| 127 |
"session": {
|
| 128 |
"user": {
|
| 129 |
-
"name":
|
| 130 |
-
"email":
|
| 131 |
-
"image": "https://lh3.googleusercontent.com/a/
|
| 132 |
-
"id":
|
| 133 |
},
|
| 134 |
"expires": "2025-06-09T19:36:08.220Z",
|
| 135 |
"isNewUser": False
|
|
@@ -137,7 +136,7 @@ def build_payload(messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
| 137 |
"isPremium": True,
|
| 138 |
"subscriptionCache": {
|
| 139 |
"status": "PREMIUM",
|
| 140 |
-
"customerId":
|
| 141 |
"expiryTimestamp": 1749108685,
|
| 142 |
"lastChecked": 1746822333827,
|
| 143 |
"isTrialSubscription": False
|
|
@@ -147,6 +146,7 @@ def build_payload(messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
| 147 |
"designerMode": False
|
| 148 |
}
|
| 149 |
|
|
|
|
| 150 |
def log_retry(retry_state):
|
| 151 |
rid = retry_state.kwargs.get("request_id", "unknown")
|
| 152 |
attempt = retry_state.attempt_number
|
|
@@ -189,6 +189,7 @@ async def get_blackbox_response(*, data, stream: bool, request_id: str) -> Async
|
|
| 189 |
logger.exception("[%s] Unexpected upstream error", request_id)
|
| 190 |
raise HTTPException(status_code=502, detail="Failed to contact upstream") from e
|
| 191 |
|
|
|
|
| 192 |
@app.middleware("http")
|
| 193 |
async def add_request_id(request: Request, call_next):
|
| 194 |
request.state.request_id = rid = str(uuid.uuid4())
|
|
@@ -198,6 +199,7 @@ async def add_request_id(request: Request, call_next):
|
|
| 198 |
logger.info("[%s] finished in %.2fs", rid, time.perf_counter() - start)
|
| 199 |
return resp
|
| 200 |
|
|
|
|
| 201 |
@app.get("/")
|
| 202 |
async def root():
|
| 203 |
return {"message": "API is running"}
|
|
@@ -247,8 +249,12 @@ async def chat_completions(request: Request):
|
|
| 247 |
"model": "DeepResearch",
|
| 248 |
"choices": [{"index": 0, "delta": {"content": chunk}}],
|
| 249 |
}
|
| 250 |
-
yield f"data: {json.dumps(msg)}
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
| 254 |
|
|
@@ -259,4 +265,4 @@ async def chat_completions(request: Request):
|
|
| 259 |
raise HTTPException(status_code=502, detail="Blackbox upstream failed")
|
| 260 |
except Exception as e:
|
| 261 |
logger.exception("[%s] error", rid)
|
| 262 |
-
raise HTTPException(status_code=500, detail="Internal proxy error")
|
|
|
|
| 9 |
|
| 10 |
import aiohttp
|
| 11 |
import brotli
|
|
|
|
| 12 |
import importlib
|
| 13 |
from fastapi import FastAPI, Request, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 109 |
"isMemoryEnabled": False,
|
| 110 |
"mobileClient": False,
|
| 111 |
"userSelectedModel": None,
|
| 112 |
+
"validated": "00f37b34-a166-4efb-bce5-1312d87f2f94",
|
| 113 |
"imageGenerationMode": False,
|
| 114 |
"webSearchModePrompt": False,
|
| 115 |
"deepSearchMode": True,
|
|
|
|
| 125 |
},
|
| 126 |
"session": {
|
| 127 |
"user": {
|
| 128 |
+
"name": _rand(10),
|
| 129 |
+
"email": random_email(),
|
| 130 |
+
"image": "https://lh3.googleusercontent.com/a/default",
|
| 131 |
+
"id": random_id()
|
| 132 |
},
|
| 133 |
"expires": "2025-06-09T19:36:08.220Z",
|
| 134 |
"isNewUser": False
|
|
|
|
| 136 |
"isPremium": True,
|
| 137 |
"subscriptionCache": {
|
| 138 |
"status": "PREMIUM",
|
| 139 |
+
"customerId": random_customer_id(),
|
| 140 |
"expiryTimestamp": 1749108685,
|
| 141 |
"lastChecked": 1746822333827,
|
| 142 |
"isTrialSubscription": False
|
|
|
|
| 146 |
"designerMode": False
|
| 147 |
}
|
| 148 |
|
| 149 |
+
# βββ Retry Logic βββ
|
| 150 |
def log_retry(retry_state):
|
| 151 |
rid = retry_state.kwargs.get("request_id", "unknown")
|
| 152 |
attempt = retry_state.attempt_number
|
|
|
|
| 189 |
logger.exception("[%s] Unexpected upstream error", request_id)
|
| 190 |
raise HTTPException(status_code=502, detail="Failed to contact upstream") from e
|
| 191 |
|
| 192 |
+
# βββ Middleware βββ
|
| 193 |
@app.middleware("http")
|
| 194 |
async def add_request_id(request: Request, call_next):
|
| 195 |
request.state.request_id = rid = str(uuid.uuid4())
|
|
|
|
| 199 |
logger.info("[%s] finished in %.2fs", rid, time.perf_counter() - start)
|
| 200 |
return resp
|
| 201 |
|
| 202 |
+
# βββ Routes βββ
|
| 203 |
@app.get("/")
|
| 204 |
async def root():
|
| 205 |
return {"message": "API is running"}
|
|
|
|
| 249 |
"model": "DeepResearch",
|
| 250 |
"choices": [{"index": 0, "delta": {"content": chunk}}],
|
| 251 |
}
|
| 252 |
+
yield f"data: {json.dumps(msg)}
|
| 253 |
+
|
| 254 |
+
"
|
| 255 |
+
yield "data: [DONE]
|
| 256 |
+
|
| 257 |
+
"
|
| 258 |
|
| 259 |
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
| 260 |
|
|
|
|
| 265 |
raise HTTPException(status_code=502, detail="Blackbox upstream failed")
|
| 266 |
except Exception as e:
|
| 267 |
logger.exception("[%s] error", rid)
|
| 268 |
+
raise HTTPException(status_code=500, detail="Internal proxy error")
|