diff --git a/backend/internal/service/gateway_anthropic_upstream_target_url_test.go b/backend/internal/service/gateway_anthropic_upstream_target_url_test.go new file mode 100644 index 0000000000..743648997f --- /dev/null +++ b/backend/internal/service/gateway_anthropic_upstream_target_url_test.go @@ -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) +} diff --git a/backend/internal/service/gateway_upstream_request.go b/backend/internal/service/gateway_upstream_request.go index 49b6838531..49abe1ba95 100644 --- a/backend/internal/service/gateway_upstream_request.go +++ b/backend/internal/service/gateway_upstream_request.go @@ -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 == "" { diff --git a/backend/internal/service/openai_gateway_messages_anthropic_native.go b/backend/internal/service/openai_gateway_messages_anthropic_native.go index 5ee7d6743a..90816c92ea 100644 --- a/backend/internal/service/openai_gateway_messages_anthropic_native.go +++ b/backend/internal/service/openai_gateway_messages_anthropic_native.go @@ -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 {