处理器管道
LLM Fence 的核心请求处理通过可插拔管道实现。处理器配置存储在运行配置 processors 字段中,由管理端或 /api/admin/config/processors/* 更新。
三阶段
text
Client -> Auth -> PreProcessor -> Adapter -> Upstream
<- PostProcessor <-
Observer async| 阶段 | 用途 |
|---|---|
| PreProcessor | 请求转发前校验或改写 |
| PostProcessor | 响应返回前改写或补齐 |
| Observer | 请求完成后旁路记录或异步分析 |
当前处理器
| 名称 | 阶段 | 说明 |
|---|---|---|
client_guard | PreProcessor | 根据请求特征识别客户端类型,并校验 Key 的 allowed_clients |
model_guard | PreProcessor | 校验 Key 的 allowed_models |
ip_guard | PreProcessor | 校验 Key 的 allowed_ips |
format_converter | Pre/Post/Stream | OpenAI 与 Anthropic 格式互转 |
stream_usage_options | Stream | 处理流式用量相关选项 |
request_logger | Observer | 异步写入 request_logs |
audit | Observer | 旁路 AI 审计,写入 audit_logs |
配置
除 audit 外,普通处理器只接受 enabled 字段:
json
{
"client_guard": { "enabled": true },
"model_guard": { "enabled": true },
"ip_guard": { "enabled": true },
"request_logger": { "enabled": true },
"format_converter": { "enabled": true },
"stream_usage_options": { "enabled": true }
}如果给这些处理器添加额外字段,运行配置校验会拒绝提交。旧版 client_guard.detection 已被兼容性归一化移除,不再作为有效配置。
client_guard
识别的客户端类型:
codex/codex-lenientcc/cc-lenientauditunknown
Key 策略示例:
json
{
"allowed_clients": ["codex", "cc"]
}model_guard
Key 策略示例:
json
{
"allowed_models": ["gpt-4o", "deepseek-chat"]
}白名单为空或未配置时不限制模型。
ip_guard
IP 来源优先级:
Key-Client-IPX-Forwarded-For的第一个地址- 直连
remote_addr
Key 策略示例:
json
{
"allowed_ips": ["10.0.0.0/8", "192.168.1.100"]
}format_converter
Provider 使用 supported_formats 和 auto_convert 决定是否需要转换。
当客户端请求格式不在上游原生 supported_formats 中,且 auto_convert = true、format_converter.enabled = true 时,LLM Fence 会转换到 supported_formats[0] 对应的上游格式。
request_logger
异步记录:
- 本地
request_id和上游upstream_request_id - Key 名称和 Key ID
- 模型、上游模型、Provider、endpoint
- 流式标记、耗时、TTFT、客户端类型
- Token 用量和错误来源
audit
审计是独立配置较多的 Observer,见 AI 审计模块。