guard: 所有出站请求体统一为 OpenAI Chat Completions 规范

占位请求(encode_stub)原先写死 max_tokens=1 + temperature=0,且客户端没给 model 时
直接不发 model——上游是 o 系列/gpt-5 会因 max_tokens、temperature 直接 400,
也因为缺 model 报错。改为只发规范字段:
  {model, messages:[{role:user,content:/ping/}], stream, max_tokens?}
- model:客户端原值;缺省用新增的 stub_model 兜底
- stream:显式布尔,不再是「有才发」
- max_tokens:新增 stub_max_tokens,默认 16,设 false 则不发送
判定请求(judge_body)本已是标准形态,未改动。
verify_plugins.py 新增 [17] 占位请求形态用例(12 项),共 97 项;mock 与真实后端均 97/97。
This commit is contained in:
Автомата Калашникова committed 2026-11-27 19:03:20 +08:00
1 parent 7b2f709b86
commit 5b4438a306
4 files changed
+100 -9

No files matched your search

+41 -7
View File
@@ -25,6 +25,15 @@
-- 3) 在响应侧用 replace 输出上面的 OpenAI 错误体。
-- 客户端看到的始终是 OpenAI 风格,且风险内容一次都没有离开网关。
--
-- 请求格式统一为 OpenAI Chat Completions
-- 本插件发出的每一个请求体都按 OpenAI 规范构造,不夹带自定义字段:
-- 判定请求(judge_body): {model, messages[system,user], stream:false,
-- temperature?, max_tokens?, response_format?,
-- reasoning_effort?}
-- 占位请求(encode_stub): {model, messages[user:"ping"], stream, max_tokens?}
-- 占位请求刻意不带 temperature / response_format / tools——这些与占位无关,
-- 且 o 系列、gpt-5 会直接 400,而上游一旦报错就会在我们的日志里制造噪声。
--
-- 放行路径
-- 流式按句读缓冲(可配 flush_chars)→ 审核 → 确定性脱敏 → 按原顺序放行;
-- 非流式整体审核 → 脱敏 → 放行。审核或脱敏任一步失败一律 fail-closed。
@@ -678,17 +687,25 @@ local function collect_user_text(decoded)
return table.concat(texts, "\n\n---\n\n")
end
-- 判定不通过时发往上游的最小请求:原文不出网关,也不浪费一次生成长文本。
-- 只需要一个 200 让宿主走正常响应通道,正文随即被 on_response / on_event 换掉;
-- 部分网关会把 max_tokens 抬到自己的下限(实测 1 会被抬到 128),不影响结果。
-- 拦截不通过时发往上游的占位请求。它必须是一个【完全符合 OpenAI Chat Completions
-- 规范的请求体】,否则上游会先用自己的 400 拒掉它,日志里凭空多一条与我们无关的错误。
-- 只带规范字段:
-- model : 客户端原样给的模型名(规范必填);客户端没给时用 stub_model 兜底
-- messages : 固定 ping,被拦截的原文不出网关
-- stream : 显式布尔,客户端流式就 true,否则 false
-- max_tokens: 可选生成长度上限,stub_max_tokens=false 时不发送
-- 不发送 temperature / response_format / tools 等与占位无关的字段。
local function encode_stub(state)
local body = {
messages = { { role = "user", content = "ping" } },
max_tokens = 1,
temperature = 0,
stream = state.stream == true,
}
if state.model then body.model = state.model end
if state.stream then body.stream = true end
local model = state.model
if model == nil or model == "" then model = config.stub_model end
if type(model) == "string" and model ~= "" then body.model = model end
if config.stub_max_tokens then
body.max_tokens = config.stub_max_tokens
end
return host.json.encode(body)
end
@@ -888,6 +905,21 @@ function plugin.init(ctx, options)
end
end
-- 占位请求兜底模型:客户端请求里没有 model 时用(规范要求 model 必填)
local stub_model = o.stub_model
if stub_model ~= nil and (type(stub_model) ~= "string" or stub_model == "") then
error("response_guard: config.stub_model must be a non-empty string or absent")
end
-- 占位请求的生成长度上限:默认 16;false 表示不发 max_tokens
-- (上游换成 gpt-5 / o 系列这类只认 max_completion_tokens 的服务时设 false)
local stub_max_tokens
if o.stub_max_tokens == false then
stub_max_tokens = nil
else
stub_max_tokens = positive_int(o.stub_max_tokens, 16, "stub_max_tokens")
end
config = {
judge_url = judge_url,
api_key = o.api_key,
@@ -898,6 +930,8 @@ function plugin.init(ctx, options)
judge_temperature = temperature,
judge_max_tokens = positive_int(o.judge_max_tokens, 512, "judge_max_tokens"),
judge_reasoning_effort = reasoning_effort,
stub_model = stub_model,
stub_max_tokens = stub_max_tokens,
judge_chunk_bytes = judge_chunk_bytes,
content_paths_stream = stream_paths,
content_paths_full = full_paths,