LifeVta commited on
Commit
d09fb08
·
verified ·
1 Parent(s): fa3d3c2

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +0 -57
app/main.py CHANGED
@@ -337,63 +337,6 @@ def create_app() -> FastAPI:
337
  reconcile_refresh_runtime()
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
-
358
- cf_url = f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}/ai/v1/chat/completions"
359
- cf_headers = {
360
- "Authorization": f"Bearer {CF_API_TOKEN}",
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:
370
- async with client.stream("POST", cf_url, headers=cf_headers, json=body_json, timeout=60.0) as resp:
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
393
-
394
- return await call_next(request)
395
- # ===========================================================================
396
-
397
  @app.exception_handler(AppError)
398
  async def _app_error_handler(request: Request, exc: AppError):
399
  return JSONResponse(exc.to_dict(), status_code=exc.status)
 
337
  reconcile_refresh_runtime()
338
  return await call_next(request)
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  @app.exception_handler(AppError)
341
  async def _app_error_handler(request: Request, exc: AppError):
342
  return JSONResponse(exc.to_dict(), status_code=exc.status)