插件改造:输出改为 OpenAI 协议,审核改走远程接口

response_guard.lua 重写:按 OpenAI 协议定位内容(choices[*].delta.content / message.content,支持多路径覆盖 tool_calls.arguments),判定改走远程 /chat/completions,拦截输出 400 content_policy_violation,审核不可用输出 503 safety_review_unavailable。

stream_redact.lua 改为 OpenAI SSE 全缓冲 PII 兜底,默认关闭;request_rewrite.lua 改为只重写最后一条 user 消息,默认关闭。

config.yaml 接口路径改为 /v1/chat/completions,停用缺模型文件的 guard_model 段与依赖 host.files 的 /upload 插件段,hook_timeout 调到 45s。

新增 tools/verify_plugins.py:13 个场景 57 项断言的契约校验。
This commit is contained in:
Автомата Калашникова committed 2026-11-27 17:25:18 +08:00
1 parent e1f944f16d
commit 9218bdf1e8
6 files changed
+1967 -1288

No files matched your search

+97 -97
View File
@@ -1,88 +1,96 @@
-- request_rewrite.lua
-- Request rewrite example following docs/plugins.md (Lua plugin interface v1).
-- ============================================================================
-- request_rewrite.lua —— OpenAI Chat Completions 请求改写(Lua 插件接口 v1)
--
-- Example config:
-- prefix: "[checked] "
-- 作用
-- 在最后一条 user 消息的正文前加上配置的前缀,用于打标/灰度/审计定位。
-- 只认 OpenAI 的 messages 结构,不再处理旧的单 message 字段。
--
-- Scope:
-- Intended for route-level use on chat-style JSON requests with a string
-- "message" field. Requests that are not applicable (invalid/non-object JSON,
-- or missing/non-string message) pass through unchanged.
-- 适用性
-- content 为字符串,或 content 为 [{type:"text",text:"..."}] 数组时改写;
-- 其它情况(非 JSON、无 user 消息、多模态纯图片等)原样放行。
--
-- Important:
-- This implementation decodes and re-encodes the complete JSON object after
-- changing message. Other fields' serialized representation (key order,
-- whitespace, escapes, numeric representation, etc.) may change. Do not use
-- it where downstream systems require byte-identical body/signature stability.
--
-- Contract:
-- init(ctx, config) must return nil.
-- on_request may return pass, replace, or block.
-- method/path are preserved; a new request table is returned for replace.
-- 注意
-- 改写会重新序列化整个 body(键序、空白、转义可能与原始字节不同),
-- 下游若要求签名稳定的字节级一致,请不要启用本插件。
-- ============================================================================
local plugin = {}
local settings = {
prefix = "",
}
local settings = { prefix = "" }
local MAX_PREFIX_BYTES = 1024
local function is_valid_utf8(value)
if type(value) ~= "string" then
if type(value) ~= "string" then return false end
local i, n = 1, #value
while i <= n do
local b1 = value:byte(i)
if b1 <= 0x7F then
i = i + 1
elseif b1 >= 0xC2 and b1 <= 0xDF then
local b2 = value:byte(i + 1)
if not b2 or b2 < 0x80 or b2 > 0xBF then return false end
i = i + 2
elseif b1 == 0xE0 then
local b2, b3 = value:byte(i + 1, i + 2)
if not b2 or not b3 or b2 < 0xA0 or b2 > 0xBF or b3 < 0x80 or b3 > 0xBF then
return false
end
i = i + 3
elseif (b1 >= 0xE1 and b1 <= 0xEC) or (b1 >= 0xEE and b1 <= 0xEF) then
local b2, b3 = value:byte(i + 1, i + 2)
if not b2 or not b3 or b2 < 0x80 or b2 > 0xBF or b3 < 0x80 or b3 > 0xBF then
return false
end
i = i + 3
elseif b1 == 0xED then
local b2, b3 = value:byte(i + 1, i + 2)
if not b2 or not b3 or b2 < 0x80 or b2 > 0x9F or b3 < 0x80 or b3 > 0xBF then
return false
end
i = i + 3
elseif b1 == 0xF0 then
local b2, b3, b4 = value:byte(i + 1, i + 3)
if not b2 or not b3 or not b4 or b2 < 0x90 or b2 > 0xBF
or b3 < 0x80 or b3 > 0xBF or b4 < 0x80 or b4 > 0xBF then
return false
end
i = i + 4
elseif b1 >= 0xF1 and b1 <= 0xF3 then
local b2, b3, b4 = value:byte(i + 1, i + 3)
if not b2 or not b3 or not b4 or b2 < 0x80 or b2 > 0xBF
or b3 < 0x80 or b3 > 0xBF or b4 < 0x80 or b4 > 0xBF then
return false
end
i = i + 4
elseif b1 == 0xF4 then
local b2, b3, b4 = value:byte(i + 1, i + 3)
if not b2 or not b3 or not b4 or b2 < 0x80 or b2 > 0x8F
or b3 < 0x80 or b3 > 0xBF or b4 < 0x80 or b4 > 0xBF then
return false
end
i = i + 4
else
return false
end
end
return true
end
local i = 1
local n = #value
while i <= n do
local b1 = value:byte(i)
if b1 <= 0x7F then
i = i + 1
elseif b1 >= 0xC2 and b1 <= 0xDF then
local b2 = value:byte(i + 1)
if not b2 or b2 < 0x80 or b2 > 0xBF then return false end
i = i + 2
elseif b1 == 0xE0 then
local b2, b3 = value:byte(i + 1, i + 2)
if not b2 or not b3 or b2 < 0xA0 or b2 > 0xBF
or b3 < 0x80 or b3 > 0xBF then return false end
i = i + 3
elseif (b1 >= 0xE1 and b1 <= 0xEC) or (b1 >= 0xEE and b1 <= 0xEF) then
local b2, b3 = value:byte(i + 1, i + 2)
if not b2 or not b3 or b2 < 0x80 or b2 > 0xBF
or b3 < 0x80 or b3 > 0xBF then return false end
i = i + 3
elseif b1 == 0xED then
local b2, b3 = value:byte(i + 1, i + 2)
if not b2 or not b3 or b2 < 0x80 or b2 > 0x9F
or b3 < 0x80 or b3 > 0xBF then return false end
i = i + 3
elseif b1 == 0xF0 then
local b2, b3, b4 = value:byte(i + 1, i + 3)
if not b2 or not b3 or not b4 or b2 < 0x90 or b2 > 0xBF
or b3 < 0x80 or b3 > 0xBF
or b4 < 0x80 or b4 > 0xBF then return false end
i = i + 4
elseif b1 >= 0xF1 and b1 <= 0xF3 then
local b2, b3, b4 = value:byte(i + 1, i + 3)
if not b2 or not b3 or not b4
or b2 < 0x80 or b2 > 0xBF
or b3 < 0x80 or b3 > 0xBF
or b4 < 0x80 or b4 > 0xBF then return false end
i = i + 4
elseif b1 == 0xF4 then
local b2, b3, b4 = value:byte(i + 1, i + 3)
if not b2 or not b3 or not b4 or b2 < 0x80 or b2 > 0x8F
or b3 < 0x80 or b3 > 0xBF
or b4 < 0x80 or b4 > 0xBF then return false end
i = i + 4
else
return false
end
end
-- 只改写最后一条 user 消息;返回是否命中
local function apply_prefix(message, prefix)
local content = message.content
if type(content) == "string" then
message.content = prefix .. content
return true
end
if type(content) ~= "table" then return false end
for _, part in ipairs(content) do
if type(part) == "table" and part.type == "text" and type(part.text) == "string" then
part.text = prefix .. part.text
return true
end
end
return false
end
function plugin.init(ctx, options)
@@ -108,10 +116,7 @@ function plugin.init(ctx, options)
end
end
-- Atomically replace local settings; don't retain the caller's config table.
settings = { prefix = prefix }
-- plugins.md requires init to return nil.
return nil
end
@@ -119,37 +124,32 @@ function plugin.on_request(ctx, request)
if type(request) ~= "table" or type(request.body) ~= "string" then
return { action = "block", reason = "invalid request object" }
end
if settings.prefix == "" then return { action = "pass" } end
-- This is a rewrite plugin, not a JSON validator or moderation plugin.
-- Non-applicable requests are passed through unchanged.
local decode_ok, body = pcall(function() return host.json.decode(request.body) end)
if not decode_ok or type(body) ~= "table" then
return { action = "pass" }
end
if type(body.message) ~= "string" then
return { action = "pass" }
end
local ok_decode, body = pcall(host.json.decode, request.body)
if not ok_decode or type(body) ~= "table" then return { action = "pass" } end
local new_message = settings.prefix .. body.message
if not is_valid_utf8(new_message) then
return { action = "block", reason = "rewritten message is not valid UTF-8" }
end
body.message = new_message
local messages = body.messages
if type(messages) ~= "table" then return { action = "pass" } end
-- The host codec re-serializes the complete object; see warning above.
local encode_ok, rewritten_body = pcall(function() return host.json.encode(body) end)
if not encode_ok or type(rewritten_body) ~= "string" then
local last_user = nil
for _, message in ipairs(messages) do
if type(message) == "table" and message.role == "user" then
last_user = message
end
end
if last_user == nil then return { action = "pass" } end
if not apply_prefix(last_user, settings.prefix) then return { action = "pass" } end
local ok_encode, rewritten_body = pcall(host.json.encode, body)
if not ok_encode or type(rewritten_body) ~= "string" then
return { action = "block", reason = "failed to encode rewritten body" }
end
-- Construct a new table so this plugin does not mutate the input request.
local rewritten = {}
for key, value in pairs(request) do
rewritten[key] = value
end
for key, value in pairs(request) do rewritten[key] = value end
rewritten.body = rewritten_body
return { action = "replace", value = rewritten }
end
return plugin