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)) +}