mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-12 04:07:25 +00:00
feat(types): 统一格式落地为内核实际使用的类型 —— 私有字段泄漏归零
Phase 2 收尾(入向)。ChatRequest 从"客户端发什么就是什么"改为
UnifiedRequest(最大集合)的别名,内核真正用上了那个超集类型。
改掉的具体缺陷:
1. 私有字段泄漏归零(v1 的核心问题)
v1 的 disable_thinking 是内核私有开关,却被序列化进交给适配器的 body,
迫使 10 个适配器各自写 req.disable_thinking = nil 来清理。
现在它是中立的 thinking 字段({enabled, budget_tokens}),客户端与
适配器都认这个词:
- gateway/chat.go:thinkingFromClient() 把 disable_thinking 翻译成
thinking.enabled=false;冲突时中立字段优先(它能带预算,布尔不能)
- deepseek.lua:thinking.enabled=false → extra_body.thinking.type=disabled
- anthropic.lua:thinking.enabled=true + budget → 原生 {type,budget_tokens}
(enabled=false 时不传该字段——Anthropic 没有 type=disabled 这个写法)
- 其余 7 个适配器:把"清理内核私有字段"改成"清理中立字段"
2. Tools/ToolChoice 改 json.RawMessage
原为 []interface{}(OpenAI 原始形状 = 内核在假设某个协议)。
内核从不解释工具 schema——它原样交给适配器,所以原始形状不损失任何
内核侧统一性。ToolDef 结构化类型保留在包内供新适配器选用。
3. 删除 MarshalJSON 后门
v1 手工 splice ExtraBody 进 OpenAI 形状的 body;现在 ExtraBody 是
UnifiedRequest 上一个正正经经声明的字段。
渐进迁移:ChatRequest 保留为别名(不是新类型),49 处引用零改动编译,
语义变更不被机械改名淹没。
测试:新增 thinking_test.go(翻译链 4 例 + 泄漏回归 + tools 透传);
vm_test.go 两处迁移到中立语义。golden 字节级未漂移。
This commit is contained in:
@ -88,12 +88,15 @@ func TestTransformRequest(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
out, err := vm.Transform("openai", "transform_request", `{"model":"x","disable_thinking":true,"messages":[]}`)
|
||||
// v2: adapters strip the NEUTRAL field, because that is what the kernel now
|
||||
// hands them. (The v1 assertion checked disable_thinking, which was the
|
||||
// kernel's own private field — it no longer appears in the body at all.)
|
||||
out, err := vm.Transform("openai", "transform_request", `{"model":"x","thinking":{"enabled":true},"messages":[]}`)
|
||||
if err != nil {
|
||||
t.Fatalf("transform: %v", err)
|
||||
}
|
||||
if strings.Contains(out, "disable_thinking") {
|
||||
t.Fatalf("disable_thinking not stripped: %s", out)
|
||||
if strings.Contains(out, "thinking") {
|
||||
t.Fatalf("中立 thinking 字段未被剥除,会泄给上游: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,33 +151,56 @@ func TestDisableThinkingPassthrough(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
body := `{"model":"x","disable_thinking":true,"messages":[{"role":"user","content":"hi"}]}`
|
||||
// v2: the client speaks the NEUTRAL field. disable_thinking was the kernel's
|
||||
// private spelling and no longer reaches an adapter at all.
|
||||
body := `{"model":"x","thinking":{"enabled":false},"messages":[{"role":"user","content":"hi"}]}`
|
||||
out, err := vm.Transform("deepseek", "transform_request", body)
|
||||
if err != nil {
|
||||
t.Fatalf("deepseek transform: %v", err)
|
||||
}
|
||||
var req struct {
|
||||
ExtraBody map[string]interface{} `json:"extra_body"`
|
||||
Thinking map[string]interface{} `json:"thinking"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(out), &req); err != nil {
|
||||
t.Fatalf("unmarshal: %v (%s)", err, out)
|
||||
}
|
||||
thinking, ok := req.ExtraBody["thinking"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("deepseek should emit extra_body.thinking on disable_thinking: %s", out)
|
||||
t.Fatalf("deepseek 应把中立的 thinking.enabled=false 映射成 extra_body.thinking: %s", out)
|
||||
}
|
||||
if thinking["type"] != "disabled" {
|
||||
t.Fatalf("thinking.type = %v", thinking["type"])
|
||||
}
|
||||
// 关键回归:映射之后,中立字段本身必须已被剥掉,不能随 body 泄给上游。
|
||||
if req.Thinking != nil {
|
||||
t.Fatalf("中立 thinking 字段未被剥除,泄漏进了上游 body: %s", out)
|
||||
}
|
||||
|
||||
// anthropic: thinking is opt-in (Issue 3) — never emitted by default, and
|
||||
// disable_thinking is not a trigger either.
|
||||
// anthropic: thinking 是 opt-in —— enabled=false 时不传该字段
|
||||
// (Anthropic 没有 type=disabled 这个写法,传了会被拒)。
|
||||
out2, err := vm.Transform("anthropic", "transform_request", body)
|
||||
if err != nil {
|
||||
t.Fatalf("anthropic transform: %v", err)
|
||||
}
|
||||
if strings.Contains(out2, "thinking") {
|
||||
t.Fatalf("anthropic should drop thinking when disable_thinking: %s", out2)
|
||||
t.Fatalf("anthropic 在 thinking.enabled=false 时不应发出 thinking: %s", out2)
|
||||
}
|
||||
|
||||
// 新增覆盖:enabled=true + budget 应直译成 Anthropic 的原生形状
|
||||
outThinking, err := vm.Transform("anthropic", "transform_request",
|
||||
`{"model":"x","messages":[{"role":"user","content":"hi"}],"thinking":{"enabled":true,"budget_tokens":2048}}`)
|
||||
if err != nil {
|
||||
t.Fatalf("anthropic transform: %v", err)
|
||||
}
|
||||
var ar struct {
|
||||
Thinking map[string]interface{} `json:"thinking"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(outThinking), &ar); err != nil {
|
||||
t.Fatalf("unmarshal: %v (%s)", err, outThinking)
|
||||
}
|
||||
if ar.Thinking["type"] != "enabled" || ar.Thinking["budget_tokens"] != float64(2048) {
|
||||
t.Fatalf("anthropic 原生 thinking 未按中立语义直译: %s", outThinking)
|
||||
}
|
||||
out3, err := vm.Transform("anthropic", "transform_request", `{"model":"x","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user