请求侧改为直接拦截,宿主统一渲染 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:
Автомата Калашникова committed 2026-11-27 17:52:52 +08:00
1 parent 9218bdf1e8
commit af7b2e28f3
5 files changed
+89 -96

No files matched your search

+9 -1
View File
@@ -7,7 +7,15 @@ class PluginFailure(MiddlewareError):
class PolicyBlocked(MiddlewareError):
pass
"""A plugin rejected the request.
``code`` is one of a closed set of tokens (see runtime.BLOCK_CODES). Plugin-supplied
text must never reach the response body, so anything outside that set is normalised.
"""
def __init__(self, code: str = "content_policy_violation") -> None:
super().__init__(code)
self.code = code
class Overloaded(MiddlewareError):
+34 -2
View File
@@ -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):
+10 -1
View File
@@ -17,6 +17,15 @@ from .worker import json_bytes, worker_main
logger = logging.getLogger("middleware.audit")
ADAPTER_ID = "__protocol_adapter__"
# A plugin may only name a rejection from this closed set; anything else falls back to the
# default. This keeps plugin-supplied text out of the response body (see errors.PolicyBlocked).
BLOCK_CODES = frozenset({"content_policy_violation", "safety_review_unavailable"})
DEFAULT_BLOCK_CODE = "content_policy_violation"
def block_code(reason):
return reason if reason in BLOCK_CODES else DEFAULT_BLOCK_CODE
class Session:
def __init__(self, manager, request_id=None, plugins=None, attachments=None):
@@ -233,7 +242,7 @@ class Session:
raise PluginFailure()
action = result.get("action")
if action == "block":
raise PolicyBlocked()
raise PolicyBlocked(block_code(result.get("reason")))
if action == "pass":
if "value" in result or "events" in result:
raise PluginFailure()