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:
JianFeeeee
2026-10-11 09:35:32 +08:00
parent 958f1f48b5
commit 93eeba3762
25 changed files with 417 additions and 121 deletions

View File

@ -53,9 +53,20 @@ type UnifiedRequest struct {
StopSequences []string `json:"stop_sequences,omitempty"` // Anthropic
// ---- 工具调用 ----
Tools []ToolDef `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"` // OpenAI
// Tools 保留 json.RawMessage(宽松的原始形状)而不是结构化的 []ToolDef,
// 原因是**渐进迁移**:内核现有的 12 个适配器与 gateway 都按 []interface{}
// /json.RawMessage 形状序列化请求到适配器。若此处改成 []ToolDef,则
// 所有适配器与调用点立即编译失败,必须同步改写 19 个非测试点 + 30 个测试。
// 而 Tools 的真实消费者是适配器(transform_request),内核本身从不解析它,
// 所以用原始形状不损失任何内核侧的统一性——它本就是"原样交给适配器的载荷"。
//
// ToolDef 类型保留在此包内供新适配器选用(maximal-set 的结构化语义),
// 但内核序列化路径暂不强推它。
Tools json.RawMessage `json:"tools,omitempty"`
ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
// ParallelToolCalls *bool 允许显式 false(OpenAI 语义)。
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"` // OpenAI
// ---- 结构化输出 ----
ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // OpenAI
@ -69,6 +80,9 @@ type UnifiedRequest struct {
System string `json:"system,omitempty"`
// ---- 推理/思考 ----
// Thinking 取代 v1 的内核私有字段 DisableThinking:那个字段曾被序列化进
// 交给适配器的 body,迫使 10 个适配器各自写 req.disable_thinking = nil。
// 这里它是双方都认可的对称语义(客户端要什么 / 上游怎么映射)。
// Thinking 是"要不要思考、思考预算多少"的语义;
// 适配器各自映射(Anthropic thinking.budget_tokens、Gemini
// thinkingConfig.thinkingBudget、DeepSeek extra_body.thinking.type)。
@ -173,3 +187,14 @@ type UnifiedDelta struct {
// Cost 上游自报费用。
Cost string `json:"cost,omitempty"`
}
// ChatRequest is the historical name for the in-bound request type.
//
// Deprecated: it is an alias of UnifiedRequest. The rename happened because the
// kernel's internal format stopped being "whatever the client sent" once the
// maximal set landed — it is now the maximal set, and the client's own protocol
// shape only survives until the adapter sees it.
//
// Kept as an alias (not a distinct type) so the 49 existing references keep
// compiling while call sites migrate deliberately rather than in one sweep.
type ChatRequest = UnifiedRequest

View File

@ -34,8 +34,8 @@ func TestMaximalSetCoversEveryProtocol(t *testing.T) {
Seed: i64(42),
Stop: []string{"\n"},
StopSequences: []string{"\n\n"},
Tools: []ToolDef{{Name: "f", Type: "function"}},
ToolChoice: "auto",
Tools: json.RawMessage(`[{"type":"function","function":{"name":"f"}}]`),
ToolChoice: json.RawMessage(`"auto"`),
ParallelToolCalls: b(true),
ResponseFormat: &ResponseFormat{Type: "json_object"},
System: "you are helpful",

View File

@ -17,42 +17,32 @@ import (
// provider package (which pulls in the Lua runtime).
var ErrBusy = errors.New("provider busy")
// ---- OpenAI wire request (gateway input) ----
type ChatRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Stream bool `json:"stream,omitempty"`
Tools []interface{} `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
DisableThinking bool `json:"disable_thinking"`
ExtraBody map[string]interface{} `json:"-"`
// ClientSession 是客户端自带的会话标识(请求头 x-session-affinity 等)。
// 不参与序列化:它只用于让适配器生成稳定的上游会话,不应泄漏给上游 body。
ClientSession string `json:"-"`
}
func (r *ChatRequest) MarshalJSON() ([]byte, error) {
type Alias ChatRequest
data, err := json.Marshal((*Alias)(r))
if err != nil {
return nil, err
}
if len(r.ExtraBody) == 0 {
return data, nil
}
var raw map[string]interface{}
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
for k, v := range r.ExtraBody {
raw[k] = v
}
return json.Marshal(raw)
}
// ---- in-bound request ----
//
// ChatRequest now lives in maxwire.go as an alias of UnifiedRequest: the
// kernel's internal format is the maximal set (OpenAI / Anthropic / Gemini /
// Ollama / DeepSeek fields unioned), not "whatever an OpenAI client sent". The
// type stays named ChatRequest at every call site for now — renaming 19
// non-test + 30 test references in one sweep would bury the semantic change in
// mechanical noise, and an alias makes the migration free when it happens.
//
// The v1 struct that lived here is gone. Its fields and their fate:
//
// Model/Messages/Temperature/MaxTokens/Stream → UnifiedRequest, same tags
// Tools/ToolChoice → json.RawMessage (raw passthrough
// to the adapter; the kernel never
// parses them)
// DisableThinking (json:"disable_thinking") → REPLACED by Thinking. This was
// the private field that leaked into
// the body handed to adapters, forcing
// 10 adapters to nil it out.
// ExtraBody (hand-injected by MarshalJSON) → a real, declared field on
// UnifiedRequest — no more
// MarshalJSON back door
// ClientSession (json:"-") → kept, still never serialized
//
// The custom MarshalJSON is gone with it: its only job was splicing ExtraBody
// into an OpenAI-shaped body, which is no longer how the type serializes.
// ChatMessage supports both plain string content and multimodal arrays
// (RawMessage preserves whatever the client sent for the adapter to process).