Update app/main.py
Browse files- app/main.py +11 -11
app/main.py
CHANGED
|
@@ -338,23 +338,20 @@ def create_app() -> FastAPI:
|
|
| 338 |
return await call_next(request)
|
| 339 |
|
| 340 |
# ===========================================================================
|
| 341 |
-
# 🔥
|
| 342 |
# ===========================================================================
|
| 343 |
@app.middleware("http")
|
| 344 |
async def _glm_intercept_middleware(request: Request, call_next):
|
| 345 |
-
# 仅拦截发往聊天对齐接口的 POST 请求
|
| 346 |
if request.method == "POST" and "chat/completions" in request.url.path:
|
| 347 |
body_bytes = await request.body()
|
| 348 |
try:
|
| 349 |
import json
|
| 350 |
body_json = json.loads(body_bytes)
|
| 351 |
|
| 352 |
-
# 命中目标模型标记
|
| 353 |
if body_json.get("model") in ["glm-5.2", "@cf/zai-org/glm-5.2"]:
|
| 354 |
import httpx
|
| 355 |
from fastapi.responses import StreamingResponse
|
| 356 |
|
| 357 |
-
# 强行注入你的 Cloudflare 凭证
|
| 358 |
CF_ACCOUNT_ID = "1b18653b5658eab10682cb5396b9d446"
|
| 359 |
CF_API_TOKEN = "cfut_5xFTKKEOnH25XHPKpq9y2l7VtTgqibSGBFF93PxR1663887a"
|
| 360 |
|
|
@@ -364,11 +361,9 @@ def create_app() -> FastAPI:
|
|
| 364 |
"Content-Type": "application/json"
|
| 365 |
}
|
| 366 |
|
| 367 |
-
# 转换模型名称为 Cloudflare 官方认识的 ID
|
| 368 |
body_json["model"] = "@cf/zai-org/glm-5.2"
|
| 369 |
is_stream = body_json.get("stream", False)
|
| 370 |
|
| 371 |
-
# 1. 流式返回处理 (Stream)
|
| 372 |
if is_stream:
|
| 373 |
async def cloudflare_stream_generator():
|
| 374 |
async with httpx.AsyncClient() as client:
|
|
@@ -376,17 +371,22 @@ def create_app() -> FastAPI:
|
|
| 376 |
async for chunk in resp.aiter_bytes():
|
| 377 |
yield chunk
|
| 378 |
return StreamingResponse(cloudflare_stream_generator(), media_type="text/event-stream")
|
| 379 |
-
|
| 380 |
-
# 2. 非流式返回处理
|
| 381 |
else:
|
| 382 |
async with httpx.AsyncClient() as client:
|
| 383 |
resp = await client.post(cf_url, headers=cf_headers, json=body_json, timeout=60.0)
|
| 384 |
return JSONResponse(resp.json(), status_code=resp.status_code)
|
| 385 |
|
| 386 |
-
except Exception:
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
-
# 恢复被 await request.body() 消费掉的内存流,防止原版路由器(Grok逻辑)在二次读取时死锁卡住
|
| 390 |
async def re_receive():
|
| 391 |
return {"type": "http.request", "body": body_bytes, "more_body": False}
|
| 392 |
request._receive = re_receive
|
|
|
|
| 338 |
return await call_next(request)
|
| 339 |
|
| 340 |
# ===========================================================================
|
| 341 |
+
# 🔥 诊断版:GLM 5.2 全局路由拦截中间件 (拒绝盲目 pass,直接暴露真实异常)
|
| 342 |
# ===========================================================================
|
| 343 |
@app.middleware("http")
|
| 344 |
async def _glm_intercept_middleware(request: Request, call_next):
|
|
|
|
| 345 |
if request.method == "POST" and "chat/completions" in request.url.path:
|
| 346 |
body_bytes = await request.body()
|
| 347 |
try:
|
| 348 |
import json
|
| 349 |
body_json = json.loads(body_bytes)
|
| 350 |
|
|
|
|
| 351 |
if body_json.get("model") in ["glm-5.2", "@cf/zai-org/glm-5.2"]:
|
| 352 |
import httpx
|
| 353 |
from fastapi.responses import StreamingResponse
|
| 354 |
|
|
|
|
| 355 |
CF_ACCOUNT_ID = "1b18653b5658eab10682cb5396b9d446"
|
| 356 |
CF_API_TOKEN = "cfut_5xFTKKEOnH25XHPKpq9y2l7VtTgqibSGBFF93PxR1663887a"
|
| 357 |
|
|
|
|
| 361 |
"Content-Type": "application/json"
|
| 362 |
}
|
| 363 |
|
|
|
|
| 364 |
body_json["model"] = "@cf/zai-org/glm-5.2"
|
| 365 |
is_stream = body_json.get("stream", False)
|
| 366 |
|
|
|
|
| 367 |
if is_stream:
|
| 368 |
async def cloudflare_stream_generator():
|
| 369 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 371 |
async for chunk in resp.aiter_bytes():
|
| 372 |
yield chunk
|
| 373 |
return StreamingResponse(cloudflare_stream_generator(), media_type="text/event-stream")
|
|
|
|
|
|
|
| 374 |
else:
|
| 375 |
async with httpx.AsyncClient() as client:
|
| 376 |
resp = await client.post(cf_url, headers=cf_headers, json=body_json, timeout=60.0)
|
| 377 |
return JSONResponse(resp.json(), status_code=resp.status_code)
|
| 378 |
|
| 379 |
+
except Exception as e:
|
| 380 |
+
import traceback
|
| 381 |
+
error_trace = traceback.format_exc()
|
| 382 |
+
return JSONResponse({
|
| 383 |
+
"error": {
|
| 384 |
+
"message": f"GLM 拦截器内部崩溃: {str(e)}",
|
| 385 |
+
"type": "interceptor_exception",
|
| 386 |
+
"traceback": error_trace
|
| 387 |
+
}
|
| 388 |
+
}, status_code=500)
|
| 389 |
|
|
|
|
| 390 |
async def re_receive():
|
| 391 |
return {"type": "http.request", "body": body_bytes, "more_body": False}
|
| 392 |
request._receive = re_receive
|