请求侧改为直接拦截,宿主统一渲染 OpenAI 错误体
response_guard.lua:判定通过直接 pass,原始请求一字不改转发上游;不通过直接 block。删除 stub 替换逻辑,风险请求不再让上游白跑一次生成。
middleware/errors.py:PolicyBlocked 携带 code。middleware/runtime.py:block 的 reason 经白名单映射成 code,插件文本不进响应。middleware/proxy.py:PolicyBlocked 输出 OpenAI 错误体(400 content_policy_violation / 503 safety_review_unavailable),替换原来的 403 {error: PolicyBlocked}。
tools/verify_plugins.py:断言同步到新流程。
This commit is contained in:
1 parent
9218bdf1e8
commit
af7b2e28f3
5 files changed
+89
-96
No files matched your search
+34
-2
@@ -28,6 +28,37 @@ async def bounded_body(chunks, maximum):
|
||||
return bytes(data)
|
||||
|
||||
|
||||
# OpenAI-shaped rejection bodies. The set is closed: an unknown code falls back to the
|
||||
# content-policy message, so no plugin-supplied text can reach the client.
|
||||
POLICY_ERRORS = {
|
||||
"content_policy_violation": (
|
||||
400,
|
||||
"Your request was rejected by the content safety policy.",
|
||||
"invalid_request_error",
|
||||
),
|
||||
"safety_review_unavailable": (
|
||||
503,
|
||||
"Content safety review is temporarily unavailable. Please retry later.",
|
||||
"server_error",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def policy_error(code):
|
||||
status_code, message, kind = POLICY_ERRORS.get(code, POLICY_ERRORS["content_policy_violation"])
|
||||
return (
|
||||
{
|
||||
"error": {
|
||||
"message": message,
|
||||
"type": kind,
|
||||
"param": None,
|
||||
"code": code if code in POLICY_ERRORS else "content_policy_violation",
|
||||
}
|
||||
},
|
||||
status_code,
|
||||
)
|
||||
|
||||
|
||||
async def disconnected(receive):
|
||||
while True:
|
||||
message = await receive()
|
||||
@@ -341,8 +372,9 @@ def create_app(settings: Settings | str = "config.yaml") -> FastAPI:
|
||||
)
|
||||
except Overloaded:
|
||||
return JSONResponse({"error": "Overloaded"}, status_code=503)
|
||||
except PolicyBlocked:
|
||||
return JSONResponse({"error": "PolicyBlocked"}, status_code=403)
|
||||
except PolicyBlocked as exc:
|
||||
payload, status_code = policy_error(exc.code)
|
||||
return JSONResponse(payload, status_code=status_code)
|
||||
except InvalidMultipart:
|
||||
return JSONResponse({"error": "InvalidMultipart"}, status_code=400)
|
||||
except (MiddlewareError, aiohttp.ClientError, TimeoutError, UnicodeError, ValueError):
|
||||
|
||||
Reference in new issue
Block a user