fix(composite): 跨族账号池中按模型分流与无 Anthropic 端点的多协议账号委派 OpenAI 网关链

#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 行为不变。
This commit is contained in:
shaw committed 2026-10-09 14:15:21 +08:00
1 parent 25af9b1dd2
commit 9b40c4f0cd
3 files changed
+82

No files matched your search

@@ -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 协议上被误委派。
@@ -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()) == ""
}
@@ -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))
}