fix(composite): generic 链 Anthropic 协议端点按版本感知拼接,修复 /v1/v1/messages

跨族账号池中 ollama_cloud / 按模型分流平台账号经 GatewayService 转发时,
anthropicUpstreamTargetURL 朴素拼接,base 带 /v1 即拼出 /v1/v1/messages
(count_tokens 同),而同一账号在 OpenAI 族原生 Anthropic 链上版本感知拼接、
工作正常。抽出 anthropicProtocolEndpointURL 供两条链共用同一规则。
This commit is contained in:
shaw committed 2026-10-10 11:01:20 +08:00
1 parent ed8d6fb297
commit a0155fee26
3 files changed
+63 -7

No files matched your search

@@ -0,0 +1,49 @@
//go:build unit
package service
import (
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
)
// generic 链(跨族账号池走 GatewayService.Forward / CountTokens)与 OpenAI 族原生
// Anthropic 链同一拼接规则:ollama / 按模型分流平台的 Anthropic 协议 base 带 /v1 时
// 版本感知拼接,不得拼出 /v1/v1/messages;其余供应商保持朴素拼接。
func TestAnthropicUpstreamTargetURL_VersionAwareForOllamaCloud(t *testing.T) {
svc := &GatewayService{cfg: &config.Config{Security: config.SecurityConfig{URLAllowlist: config.URLAllowlistConfig{Enabled: false}}}}
ollama := &Account{
ID: 1, Platform: PlatformOllamaCloud, Type: AccountTypeAPIKey,
Credentials: map[string]any{
"api_key": "ollama-key",
"api_protocol": APIProtocolAnthropic,
"base_url": "https://ollama.com/v1",
},
}
got, err := svc.anthropicUpstreamTargetURL(ollama, "/v1/messages")
require.NoError(t, err)
require.Equal(t, "https://ollama.com/v1/messages", got)
got, err = svc.anthropicUpstreamTargetURL(ollama, "/v1/messages/count_tokens")
require.NoError(t, err)
require.Equal(t, "https://ollama.com/v1/messages/count_tokens", got)
ollama.Credentials["base_url"] = "https://ollama.com"
got, err = svc.anthropicUpstreamTargetURL(ollama, "/v1/messages")
require.NoError(t, err)
require.Equal(t, "https://ollama.com/v1/messages", got)
kimi := &Account{
ID: 2, Platform: PlatformKimi, Type: AccountTypeAPIKey,
Credentials: map[string]any{
"api_key": "kimi-key",
"api_protocol": APIProtocolAnthropic,
"base_url": "https://api.moonshot.cn/anthropic",
},
}
got, err = svc.anthropicUpstreamTargetURL(kimi, "/v1/messages")
require.NoError(t, err)
require.Equal(t, "https://api.moonshot.cn/anthropic/v1/messages", got)
}
@@ -1017,8 +1017,9 @@ func anthropicUpstreamBaseForAuth(account *Account) string {
// anthropicUpstreamTargetURL 组装 Anthropic 上游端点(path 以 "/" 开头,如
// "/v1/messages"、"/v1/messages/count_tokens")。CN/多协议账号(
// GetAnthropicProtocolBaseURL 非空)按 {base}{path} 朴素拼接、不附加 ?beta=true
// (第三方端点不接受该参数,与 OpenAI 族 nativeAnthropicTargetURL 对齐);
// GetAnthropicProtocolBaseURL 非空)经 anthropicProtocolEndpointURL 拼接(与 OpenAI 族
// nativeAnthropicTargetURL 同一规则,ollama / 按模型分流平台版本感知)、不附加
// ?beta=true(第三方端点不接受该参数);
// classic anthropic 账号保持 {base}{path}?beta=true 现状。返回空串表示无自定义
// base(走官方默认 claudeAPIURL / claudeAPICountTokensURL),由调用方保持默认。
func (s *GatewayService) anthropicUpstreamTargetURL(account *Account, path string) (string, error) {
@@ -1028,7 +1029,7 @@ func (s *GatewayService) anthropicUpstreamTargetURL(account *Account, path strin
if err != nil {
return "", err
}
return strings.TrimRight(validatedURL, "/") + path, nil
return anthropicProtocolEndpointURL(account, validatedURL, path), nil
}
baseURL := account.GetBaseURL()
if baseURL == "" {
@@ -146,12 +146,18 @@ func (s *OpenAIGatewayService) nativeAnthropicTargetURL(account *Account) (strin
// 连接测试共用。按模型分流的聚合平台(OpenCode、Command Code 等)的基址可能沿用带 /v1
// 的 Chat Completions 基址,用版本感知拼接避免 /v1/v1/messages;其余供应商朴素拼接。
func nativeAnthropicMessagesURL(account *Account, validatedBaseURL string) string {
// 版本感知拼接:base 已带 /v1 时不再追加,避免拼出 /v1/v1/messages(按模型分流平台的
// Chat Completions base 带 /v1;Ollama Cloud 的 anthropic 默认 base 不带,自定义可能带)。
return anthropicProtocolEndpointURL(account, validatedBaseURL, "/v1/messages")
}
// anthropicProtocolEndpointURL 由已校验的 Anthropic 协议基址拼出端点(endpoint 以 "/v1"
// 开头),OpenAI 族原生 Anthropic 链与 generic 链共用。版本感知拼接:base 已带 /v1 时
// 不再追加,避免拼出 /v1/v1/messages(按模型分流平台的 Chat Completions base 带 /v1;
// Ollama Cloud 的 anthropic 默认 base 不带,自定义可能带);其余供应商朴素拼接。
func anthropicProtocolEndpointURL(account *Account, validatedBaseURL, endpoint string) string {
if account.routesByModel() || account.IsOllamaCloud() {
return buildOpenAIEndpointURL(validatedBaseURL, "/v1/messages")
return buildOpenAIEndpointURL(validatedBaseURL, endpoint)
}
return strings.TrimRight(validatedBaseURL, "/") + "/v1/messages"
return strings.TrimRight(validatedBaseURL, "/") + endpoint
}
func resolveMappedUpstreamModel(account *Account, body []byte, defaultMappedModel string) string {