Update main.py
Browse files
main.py
CHANGED
|
@@ -36,6 +36,10 @@ REQUEST_TIMEOUT = 300
|
|
| 36 |
|
| 37 |
# βββ Headers βββ
|
| 38 |
HEADERS = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"accept": "*/*",
|
| 40 |
"accept-encoding": "gzip, deflate, br, zstd",
|
| 41 |
"accept-language": "en-US,en;q=0.9",
|
|
@@ -84,14 +88,10 @@ def random_id(): return _rand(21, string.digits)
|
|
| 84 |
def random_customer_id(): return "cus_" + _rand(12)
|
| 85 |
|
| 86 |
def build_payload(messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 87 |
-
unique_id = str(uuid.uuid4())
|
| 88 |
-
msg_id = _rand(8)
|
| 89 |
-
if messages and isinstance(messages[-1], dict):
|
| 90 |
-
msg_id = messages[-1].get("id", msg_id)
|
| 91 |
return {
|
| 92 |
"messages": messages,
|
| 93 |
"agentMode": {},
|
| 94 |
-
"id":
|
| 95 |
"previewToken": None,
|
| 96 |
"userId": None,
|
| 97 |
"codeModelMode": True,
|
|
@@ -110,7 +110,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,20 +126,20 @@ 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-
|
| 135 |
"isNewUser": False
|
| 136 |
},
|
| 137 |
"isPremium": True,
|
| 138 |
"subscriptionCache": {
|
| 139 |
"status": "PREMIUM",
|
| 140 |
-
"customerId":
|
| 141 |
-
"expiryTimestamp":
|
| 142 |
-
"lastChecked":
|
| 143 |
"isTrialSubscription": False
|
| 144 |
},
|
| 145 |
"beastMode": False,
|
|
@@ -185,9 +185,9 @@ async def get_blackbox_response(*, data, stream: bool, request_id: str) -> Async
|
|
| 185 |
yield chunk.decode("utf-8", "ignore")
|
| 186 |
else:
|
| 187 |
yield await resp.text()
|
| 188 |
-
except
|
| 189 |
-
logger.
|
| 190 |
-
raise HTTPException(status_code=502, detail="
|
| 191 |
|
| 192 |
@app.middleware("http")
|
| 193 |
async def add_request_id(request: Request, call_next):
|
|
|
|
| 36 |
|
| 37 |
# βββ Headers βββ
|
| 38 |
HEADERS = {
|
| 39 |
+
"authority": "www.blackbox.ai",
|
| 40 |
+
"method": "POST",
|
| 41 |
+
"path": "/api/chat",
|
| 42 |
+
"scheme": "https",
|
| 43 |
"accept": "*/*",
|
| 44 |
"accept-encoding": "gzip, deflate, br, zstd",
|
| 45 |
"accept-language": "en-US,en;q=0.9",
|
|
|
|
| 88 |
def random_customer_id(): return "cus_" + _rand(12)
|
| 89 |
|
| 90 |
def build_payload(messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return {
|
| 92 |
"messages": messages,
|
| 93 |
"agentMode": {},
|
| 94 |
+
"id": None,
|
| 95 |
"previewToken": None,
|
| 96 |
"userId": None,
|
| 97 |
"codeModelMode": True,
|
|
|
|
| 110 |
"isMemoryEnabled": False,
|
| 111 |
"mobileClient": False,
|
| 112 |
"userSelectedModel": None,
|
| 113 |
+
"validated": str(uuid.uuid4()),
|
| 114 |
"imageGenerationMode": False,
|
| 115 |
"webSearchModePrompt": False,
|
| 116 |
"deepSearchMode": True,
|
|
|
|
| 126 |
},
|
| 127 |
"session": {
|
| 128 |
"user": {
|
| 129 |
+
"name": "S.C gaming",
|
| 130 |
+
"email": "simarmanbir@gmail.com",
|
| 131 |
+
"image": "https://lh3.googleusercontent.com/a/ACg8ocI-ze5Qe42S-j8xaCL6X7KSVwfiOae4fONqpTxzt0d2_a2FIld1=s96-c",
|
| 132 |
+
"id": "100846841133312010974"
|
| 133 |
},
|
| 134 |
+
"expires": "2025-06-09T19:36:08.220Z",
|
| 135 |
"isNewUser": False
|
| 136 |
},
|
| 137 |
"isPremium": True,
|
| 138 |
"subscriptionCache": {
|
| 139 |
"status": "PREMIUM",
|
| 140 |
+
"customerId": "cus_Rtiok41PQ2oo1c",
|
| 141 |
+
"expiryTimestamp": 1749108685,
|
| 142 |
+
"lastChecked": 1746822333827,
|
| 143 |
"isTrialSubscription": False
|
| 144 |
},
|
| 145 |
"beastMode": False,
|
|
|
|
| 185 |
yield chunk.decode("utf-8", "ignore")
|
| 186 |
else:
|
| 187 |
yield await resp.text()
|
| 188 |
+
except Exception as e:
|
| 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):
|