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 项断言的契约校验。
156 lines
5.1 KiB
Lua
156 lines
5.1 KiB
Lua
-- ============================================================================
|
|
-- request_rewrite.lua —— OpenAI Chat Completions 请求改写(Lua 插件接口 v1)
|
|
--
|
|
-- 作用
|
|
-- 在最后一条 user 消息的正文前加上配置的前缀,用于打标/灰度/审计定位。
|
|
-- 只认 OpenAI 的 messages 结构,不再处理旧的单 message 字段。
|
|
--
|
|
-- 适用性
|
|
-- content 为字符串,或 content 为 [{type:"text",text:"..."}] 数组时改写;
|
|
-- 其它情况(非 JSON、无 user 消息、多模态纯图片等)原样放行。
|
|
--
|
|
-- 注意
|
|
-- 改写会重新序列化整个 body(键序、空白、转义可能与原始字节不同),
|
|
-- 下游若要求签名稳定的字节级一致,请不要启用本插件。
|
|
-- ============================================================================
|
|
|
|
local plugin = {}
|
|
local settings = { prefix = "" }
|
|
|
|
local MAX_PREFIX_BYTES = 1024
|
|
|
|
local function is_valid_utf8(value)
|
|
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
|
|
|
|
-- 只改写最后一条 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)
|
|
if type(options) ~= "table" then
|
|
error("request_rewrite config must be an object")
|
|
end
|
|
local prefix = options.prefix
|
|
if prefix == nil then
|
|
prefix = ""
|
|
elseif type(prefix) ~= "string" then
|
|
error("request_rewrite config.prefix must be a string")
|
|
end
|
|
if #prefix > MAX_PREFIX_BYTES then
|
|
error("request_rewrite config.prefix exceeds 1024 bytes")
|
|
end
|
|
if not is_valid_utf8(prefix) then
|
|
error("request_rewrite config.prefix must be valid UTF-8")
|
|
end
|
|
for i = 1, #prefix do
|
|
local byte = prefix:byte(i)
|
|
if byte < 0x20 or byte == 0x7F then
|
|
error("request_rewrite config.prefix must not contain control characters")
|
|
end
|
|
end
|
|
|
|
settings = { prefix = prefix }
|
|
return nil
|
|
end
|
|
|
|
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
|
|
|
|
local ok_decode, body = pcall(host.json.decode, request.body)
|
|
if not ok_decode or type(body) ~= "table" then return { action = "pass" } end
|
|
|
|
local messages = body.messages
|
|
if type(messages) ~= "table" then return { action = "pass" } end
|
|
|
|
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
|
|
|
|
local rewritten = {}
|
|
for key, value in pairs(request) do rewritten[key] = value end
|
|
rewritten.body = rewritten_body
|
|
return { action = "replace", value = rewritten }
|
|
end
|
|
|
|
return plugin
|