diff --git a/README.md b/README.md index 57945fe..ce01729 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ SOCKS5 代理示例: ### Thinking 工具历史兼容 -通过 Anthropic Messages API 调用 DeepSeek、Kimi/Moonshot 或 MiMo 模型时,代理会在转发前规范化包含 `tool_use` 的 assistant 历史:保留有效 thinking 文本、为缺失或空的 thinking 补充兼容占位内容、将 `redacted_thinking` 转为普通 thinking,并移除这些兼容端点不接受的 `signature`。该处理仅作用于已知需要此兼容行为的模型或上游地址,普通 Claude/Anthropic 请求不会被修改。 +所有请求都会经过同一个上游请求准备流程,同协议转发和跨协议转换不再使用两套分支。通过 Chat Completions 或 Anthropic Messages API 调用 DeepSeek、Kimi/Moonshot 或 MiMo 模型时,代理会按上游的目标协议规范化 assistant 工具历史:Chat 补全缺失或空的 `reasoning_content`;Anthropic 保留有效 thinking 文本、为缺失或空的 thinking 补充兼容占位内容、将 `redacted_thinking` 转为普通 thinking,并移除这些兼容端点不接受的 `signature`。显式启用 reasoning/thinking 的别名模型也会启用该处理,普通非 reasoning 请求不会被修改。 ### `performance` diff --git a/convert.go b/convert.go index a1e1f9e..e666d0d 100644 --- a/convert.go +++ b/convert.go @@ -9,7 +9,7 @@ import ( ) const ( - anthropicThinkingPlaceholder = "tool call" + toolReasoningPlaceholder = "tool call" anthropicRedactedThinkingPlaceholder = "[redacted thinking]" ) @@ -94,12 +94,43 @@ func convertRequest(from, to Protocol, input map[string]any) (map[string]any, er return encodeBridgeRequest(to, request) } -// shouldNormalizeAnthropicToolThinkingHistory identifies providers whose -// Anthropic-compatible endpoints require a plain thinking block before an -// assistant tool_use is replayed. Model names cover the normal OpenCode route; -// the upstream URL also supports deployments that alias those models. -func shouldNormalizeAnthropicToolThinkingHistory(model, upstreamURL string) bool { - return isReasoningVendorIdentifier(model) || isReasoningVendorIdentifier(upstreamURL) +// prepareUpstreamRequest is the single request preparation path for both +// pass-through and transcoded requests. Same-protocol requests are cloned so +// provider-specific fields survive, while cross-protocol requests go through +// the bridge. Target-protocol normalization then repairs reasoning history in +// either case. +func prepareUpstreamRequest(from, to Protocol, input map[string]any, upstreamURL string) (map[string]any, error) { + output, err := convertRequest(from, to, input) + if err != nil { + return nil, err + } + normalizeToolReasoningHistory(to, stringAt(output, "model"), upstreamURL, output) + return output, nil +} + +// normalizeToolReasoningHistory applies only to endpoints that are known to +// require reasoning replay, or to requests that explicitly enable reasoning. +// Normalizing the target shape makes the behavior independent of the client +// protocol used to reach the gateway. +func normalizeToolReasoningHistory(protocol Protocol, model, upstreamURL string, input map[string]any) bool { + if !shouldNormalizeToolReasoningHistory(model, upstreamURL, input) { + return false + } + switch protocol { + case ProtocolChat: + return normalizeChatToolReasoningHistory(input) + case ProtocolAnthropic: + return normalizeAnthropicToolThinkingHistory(input) + default: + return false + } +} + +// shouldNormalizeToolReasoningHistory identifies providers whose compatible +// endpoints require thinking/reasoning to be replayed with assistant tool +// calls. Explicit reasoning settings also cover aliased model names. +func shouldNormalizeToolReasoningHistory(model, upstreamURL string, input map[string]any) bool { + return isReasoningVendorIdentifier(model) || isReasoningVendorIdentifier(upstreamURL) || requestEnablesReasoning(input) } func isReasoningVendorIdentifier(value string) bool { @@ -112,6 +143,64 @@ func isReasoningVendorIdentifier(value string) bool { return false } +func requestEnablesReasoning(input map[string]any) bool { + for _, key := range []string{"reasoning_effort", "reasoning", "thinking", "effort"} { + value, exists := input[key] + if !exists || value == nil { + continue + } + switch typed := value.(type) { + case string: + mode := strings.ToLower(strings.TrimSpace(typed)) + if mode != "" && mode != "none" && mode != "disabled" { + return true + } + case bool: + if typed { + return true + } + case map[string]any: + mode := strings.ToLower(strings.TrimSpace(firstString(stringAt(typed, "type"), stringAt(typed, "effort")))) + if mode == "none" || mode == "disabled" { + continue + } + return true + default: + return true + } + } + return false +} + +// normalizeChatToolReasoningHistory ensures every assistant tool-call turn +// carries reasoning_content. Some clients discard this non-standard field +// while retaining tool_calls, which otherwise makes the next thinking-mode +// request invalid. A legacy reasoning string is promoted when available. +func normalizeChatToolReasoningHistory(input map[string]any) bool { + messages, ok := input["messages"].([]any) + if !ok { + return false + } + + changed := false + for _, rawMessage := range messages { + message, ok := rawMessage.(map[string]any) + if !ok || stringAt(message, "role") != "assistant" || len(sliceAt(message, "tool_calls")) == 0 { + continue + } + if reasoning, ok := message["reasoning_content"].(string); ok && strings.TrimSpace(reasoning) != "" { + continue + } + reasoning, _ := message["reasoning"].(string) + if strings.TrimSpace(reasoning) == "" { + reasoning = toolReasoningPlaceholder + } + message["reasoning_content"] = reasoning + changed = true + } + return changed +} + // normalizeAnthropicToolThinkingHistory repairs only assistant turns that // contain tool_use. DeepSeek, Kimi/Moonshot and MiMo reject signed, redacted, // empty or missing thinking history on those turns even though Anthropic @@ -148,7 +237,7 @@ func normalizeAnthropicToolThinkingHistory(input map[string]any) bool { } thinking, ok := block["thinking"].(string) if !ok || strings.TrimSpace(thinking) == "" { - block["thinking"] = anthropicThinkingPlaceholder + block["thinking"] = toolReasoningPlaceholder changed = true } case "redacted_thinking": @@ -163,7 +252,7 @@ func normalizeAnthropicToolThinkingHistory(input map[string]any) bool { if !hasThinking { content = append([]any{map[string]any{ "type": "thinking", - "thinking": anthropicThinkingPlaceholder, + "thinking": toolReasoningPlaceholder, }}, content...) message["content"] = content changed = true diff --git a/gateway.go b/gateway.go index d75971b..7de5d8c 100644 --- a/gateway.go +++ b/gateway.go @@ -225,32 +225,19 @@ func (g *Gateway) handleInference(external Protocol) http.HandlerFunc { writeAPIError(w, external, http.StatusBadRequest, err.Error(), "invalid_request_error", "model") return } - encoded := body - upstreamPayload := payload - needsEncoding := false - if external == ProtocolAnthropic { - upstreamURL := g.cfg.Upstream.Zen - if route.Tier == TierGo { - upstreamURL = g.cfg.Upstream.Go - } - if shouldNormalizeAnthropicToolThinkingHistory(model, upstreamURL) { - needsEncoding = normalizeAnthropicToolThinkingHistory(upstreamPayload) - } + upstreamURL := g.cfg.Upstream.Zen + if route.Tier == TierGo { + upstreamURL = g.cfg.Upstream.Go } - if external != route.Protocol { - upstreamPayload, err = convertRequest(external, route.Protocol, upstreamPayload) - if err != nil { - writeAPIError(w, external, http.StatusBadRequest, err.Error(), "invalid_request_error", "") - return - } - needsEncoding = true + upstreamPayload, err := prepareUpstreamRequest(external, route.Protocol, payload, upstreamURL) + if err != nil { + writeAPIError(w, external, http.StatusBadRequest, err.Error(), "invalid_request_error", "") + return } - if needsEncoding { - encoded, err = json.Marshal(upstreamPayload) - if err != nil { - writeAPIError(w, external, http.StatusBadRequest, "request contains unsupported JSON values", "invalid_request_error", "") - return - } + encoded, err := json.Marshal(upstreamPayload) + if err != nil { + writeAPIError(w, external, http.StatusBadRequest, "request contains unsupported JSON values", "invalid_request_error", "") + return } ids := deriveRequestIDs(r, payload) stream := boolAt(payload, "stream")