From 9b40c4f0cd34cf0c65c33a60cf2e8a782128dc84 Mon Sep 17 00:00:00 2001 From: shaw Date: Fri, 9 Oct 2026 14:15:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(composite):=20=E8=B7=A8=E6=97=8F=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E6=B1=A0=E4=B8=AD=E6=8C=89=E6=A8=A1=E5=9E=8B=E5=88=86?= =?UTF-8?q?=E6=B5=81=E4=B8=8E=E6=97=A0=20Anthropic=20=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=E7=9A=84=E5=A4=9A=E5=8D=8F=E8=AE=AE=E8=B4=A6=E5=8F=B7=E5=A7=94?= =?UTF-8?q?=E6=B4=BE=20OpenAI=20=E7=BD=91=E5=85=B3=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #7618 新增的 Command Code(按模型分流)与 Cline(仅 Chat Completions)不满足池逻辑 「adaptive 多协议账号具备 Anthropic 原生端点」的前提:Cline 被 servable 门静默排除, Command Code 走 generic Anthropic 原生链绕过 protocol_rules(GPT 模型被发到 /messages)。 新增 Account.RequiresOpenAIProtocolChain:adaptive 且按模型分流或没有 Anthropic 原生 端点的多协议账号由 OpenAI 网关链(resolveUpstreamProtocol)转发;此类账号可入池但 不由 generic count_tokens 计数。国产厂商与 Ollama Cloud 行为不变。 --- .../handler/composite_pool_attempt_policy.go | 5 ++ .../service/composite_generic_pool.go | 21 +++++++ .../composite_pool_openai_chain_test.go | 56 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 backend/internal/service/composite_pool_openai_chain_test.go diff --git a/backend/internal/handler/composite_pool_attempt_policy.go b/backend/internal/handler/composite_pool_attempt_policy.go index 4fc612f43a..9c96fc4548 100644 --- a/backend/internal/handler/composite_pool_attempt_policy.go +++ b/backend/internal/handler/composite_pool_attempt_policy.go @@ -232,6 +232,11 @@ func compositePoolAccountDelegatesToOpenAI(account *service.Account) bool { case service.PlatformAnthropic, service.PlatformGemini, service.PlatformAntigravity: return false } + if account.RequiresOpenAIProtocolChain() { + // 按模型分流的聚合平台与无 Anthropic 原生端点的供应商:只有 OpenAI 网关链 + // 能按模型选协议或完成协议转换。 + return true + } if account.IsAnthropicProtocol() || account.IsAdaptiveAPIProtocol() { // 多协议账号走既有 generic Forward(servable 门已保证 Anthropic 协议 // base 非空);不应停留在 Chat Completions 协议上被误委派。 diff --git a/backend/internal/service/composite_generic_pool.go b/backend/internal/service/composite_generic_pool.go index 48adffb25b..9458d0ad40 100644 --- a/backend/internal/service/composite_generic_pool.go +++ b/backend/internal/service/composite_generic_pool.go @@ -60,6 +60,11 @@ func genericCompositePoolAccountServable(account *Account) bool { case PlatformAnthropic, PlatformGemini, PlatformAntigravity: return true } + if account.RequiresOpenAIProtocolChain() { + // 由 handler 层委派 OpenAI 网关链转发(按模型分流 / 协议转换),不要求 + // Anthropic 协议上游 base。 + return true + } if account.IsAnthropicProtocol() || account.IsAdaptiveAPIProtocol() { return strings.TrimSpace(account.GetAnthropicProtocolBaseURL()) != "" } @@ -78,5 +83,21 @@ func GenericCompositePoolCountableAccount(account *Account) bool { case PlatformAnthropic, PlatformGemini, PlatformAntigravity: return true } + if account.RequiresOpenAIProtocolChain() { + return false + } return account.IsAnthropicProtocol() || account.IsAdaptiveAPIProtocol() } + +// RequiresOpenAIProtocolChain 报告 adaptive 多协议账号在跨族账号池中是否只能由 +// OpenAI 网关链转发:按模型分流的聚合平台(OpenCode、Command Code)须按 +// protocol_rules / 模型目录逐模型选协议,只有该链(resolveUpstreamProtocol)实现了 +// 这一分流;没有 Anthropic 原生端点的供应商(如 Cline 只有 Chat Completions)须由 +// 该链做协议转换。其余 adaptive 账号(国产厂商、Ollama Cloud)具备 Anthropic 原生 +// 端点,仍走 generic 转发。 +func (a *Account) RequiresOpenAIProtocolChain() bool { + if a == nil || !a.IsMultiProtocolAPIKey() || !a.IsAdaptiveAPIProtocol() { + return false + } + return a.routesByModel() || strings.TrimSpace(a.GetAnthropicProtocolBaseURL()) == "" +} diff --git a/backend/internal/service/composite_pool_openai_chain_test.go b/backend/internal/service/composite_pool_openai_chain_test.go new file mode 100644 index 0000000000..c9a456d3bf --- /dev/null +++ b/backend/internal/service/composite_pool_openai_chain_test.go @@ -0,0 +1,56 @@ +//go:build unit + +package service + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// 跨族账号池中 adaptive 多协议账号的转发链选择:按模型分流的聚合平台与没有 +// Anthropic 原生端点的供应商只能由 OpenAI 网关链转发,其余具备 Anthropic 原生 +// 端点的供应商仍走 generic 转发。 +func TestRequiresOpenAIProtocolChain(t *testing.T) { + apiKey := func(platform string, credentials map[string]any) *Account { + creds := map[string]any{"api_key": "sk-test"} + for k, v := range credentials { + creds[k] = v + } + return &Account{ID: 1, Platform: platform, Type: AccountTypeAPIKey, Credentials: creds} + } + cases := []struct { + name string + account *Account + want bool + }{ + {"kimi adaptive has native anthropic", apiKey(PlatformKimi, map[string]any{"api_protocol": APIProtocolAdaptive}), false}, + {"ollama_cloud default adaptive has native anthropic", apiKey(PlatformOllamaCloud, nil), false}, + {"opencode_go routes by model", apiKey(PlatformOpenCodeGo, nil), true}, + {"command_code routes by model", apiKey(PlatformCommandCode, nil), true}, + {"cline adaptive has no anthropic endpoint", apiKey(PlatformCline, map[string]any{"api_protocol": APIProtocolAdaptive}), true}, + {"cline default chat completions is not adaptive", apiKey(PlatformCline, nil), false}, + {"kimi pinned anthropic", apiKey(PlatformKimi, map[string]any{"api_protocol": APIProtocolAnthropic}), false}, + {"openai is not multi-protocol", apiKey(PlatformOpenAI, nil), false}, + {"nil account", nil, false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + require.Equal(t, tc.want, tc.account.RequiresOpenAIProtocolChain()) + }) + } +} + +func TestGenericCompositePoolOpenAIChainAccounts(t *testing.T) { + cline := &Account{ID: 2, Platform: PlatformCline, Type: AccountTypeAPIKey, Credentials: map[string]any{"api_key": "sk", "api_protocol": APIProtocolAdaptive}} + commandCode := &Account{ID: 3, Platform: PlatformCommandCode, Type: AccountTypeAPIKey, Credentials: map[string]any{"api_key": "sk"}} + kimi := &Account{ID: 4, Platform: PlatformKimi, Type: AccountTypeAPIKey, Credentials: map[string]any{"api_key": "sk", "api_protocol": APIProtocolAdaptive}} + + // 委派链账号可入池(不要求 Anthropic 协议 base),但不能由 generic count_tokens 计数。 + for _, account := range []*Account{cline, commandCode} { + require.True(t, genericCompositePoolAccountServable(account), account.Platform) + require.False(t, GenericCompositePoolCountableAccount(account), account.Platform) + } + require.True(t, genericCompositePoolAccountServable(kimi)) + require.True(t, GenericCompositePoolCountableAccount(kimi)) +}