mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 08:06:50 +00:00
feat(platform): wire ollama_cloud into scheduling and routing
增量 2(⑥ 组 + ⑨ 组,严格同批): ⑥ adaptive 准入 / 协议路由 / base 守卫: - A9/A13: IsOllamaCloud 谓词 + Account.IsOllamaCloud(); IsMultiProtocolAPIKeyProvider 纳入 ollama_cloud(adaptive 总开关) - A11: GetAPIProtocol 缺省档扩为 opencode_go || ollama_cloud → adaptive - A7/A10 + C30/Q2-3: DefaultOllamaCloudBaseURL(https://ollama.com/v1) / DefaultOllamaCloudAnthropicBaseURL(https://ollama.com),并补 GetOpenAIBaseURL 与 defaultCNProtocolBaseURL 的平台分支 - C33/Q2-4 + S6 层 1: GetOpenAIBaseURL 平台守卫纳入 ollama_cloud; 多协议账号缺省端点不再回落 https://api.openai.com - C32: shouldForwardOpenAIResponsesViaRawChatCompletions 新增 ollama_cloud 分支(不复用 opencode 的恒 false) - S5: nativeAnthropicTargetURL 改用版本感知 buildOpenAIEndpointURL, 删除 opencode_go 特判(消灭 /v1/v1/messages) - S6 层 2: CC / Responses / passthrough 三处 OpenAI 官方域名兜底 对多协议账号改为显式报错(防凭据外泄) ⑨ count_tokens(必须与 C40 同批): - findings B1: openai_gateway_count_tokens.go 本地估算分支加入 IsOllamaCloud()(命中时不产生任何出站请求) - C40: routes/gateway.go 的 responses 兼容白名单与 count_tokens 分派白名单加入 service.PlatformOllamaCloud 新增单测 ollama_cloud_platform_routing_test.go: 默认协议/base、nativeAnthropicTargetURL 带与不带 /v1 双向、C32 分流、 count_tokens 本地估算零出站。
This commit is contained in:
1 parent
27b9e2357e
commit
ab67f3d444
9 files changed
+289
-18
No files matched your search
@@ -49,7 +49,7 @@ func RegisterGatewayRoutes(
|
||||
switch getGroupPlatform(c) {
|
||||
case service.PlatformOpenAI, service.PlatformGrok,
|
||||
service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek,
|
||||
service.PlatformMiniMax, service.PlatformOpenCodeGo:
|
||||
service.PlatformMiniMax, service.PlatformOpenCodeGo, service.PlatformOllamaCloud:
|
||||
// 国产 OpenAI 兼容供应商与 openai/grok 一样经 OpenAI 网关转发。
|
||||
return true
|
||||
default:
|
||||
@@ -58,7 +58,7 @@ func RegisterGatewayRoutes(
|
||||
}
|
||||
countTokensHandler := func(c *gin.Context) {
|
||||
switch getGroupPlatform(c) {
|
||||
case service.PlatformOpenAI, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo:
|
||||
case service.PlatformOpenAI, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo, service.PlatformOllamaCloud:
|
||||
h.OpenAIGateway.CountTokens(c)
|
||||
case service.PlatformGrok:
|
||||
h.OpenAIGateway.GrokCountTokens(c)
|
||||
|
||||
@@ -294,6 +294,11 @@ func (a *Account) IsCNProvider() bool {
|
||||
return a != nil && IsCNProvider(a.Platform)
|
||||
}
|
||||
|
||||
// IsOllamaCloud 报告账号平台是否为 Ollama Cloud。
|
||||
func (a *Account) IsOllamaCloud() bool {
|
||||
return a != nil && IsOllamaCloud(a.Platform)
|
||||
}
|
||||
|
||||
// IsOpenAICompatible 报告账号是否走 OpenAI 网关(OpenAI 协议族)。
|
||||
// openai/grok 原生走 OpenAI 网关;国产供应商同为 OpenAI Chat Completions
|
||||
// 兼容上游,也经 OpenAI 网关转发。OpenCode 同样经 OpenAI 网关按模型分流。
|
||||
@@ -1343,10 +1348,10 @@ func (a *Account) IsOpenAIApiKey() bool {
|
||||
}
|
||||
|
||||
// GetOpenAIBaseURL 解析 OpenAI 协议族账号的上游 base_url。
|
||||
// 适用 openai、国产 OpenAI 兼容供应商(kimi/zhipu/deepseek)与 OpenCode Go;
|
||||
// grok 走 GetGrokBaseURL,此处对 grok 返回 "" 以保持原有行为。
|
||||
// 适用 openai、国产 OpenAI 兼容供应商(kimi/zhipu/deepseek)、OpenCode Go 与
|
||||
// Ollama Cloud;grok 走 GetGrokBaseURL,此处对 grok 返回 "" 以保持原有行为。
|
||||
func (a *Account) GetOpenAIBaseURL() string {
|
||||
if !a.IsOpenAI() && !a.IsCNProvider() && !a.IsOpenCodeGo() {
|
||||
if !a.IsOpenAI() && !a.IsCNProvider() && !a.IsOpenCodeGo() && !a.IsOllamaCloud() {
|
||||
return ""
|
||||
}
|
||||
if a.IsMultiProtocolAPIKey() && a.IsAdaptiveAPIProtocol() {
|
||||
@@ -1379,7 +1384,14 @@ func (a *Account) GetOpenAIBaseURL() string {
|
||||
return DefaultMiniMaxBaseURL
|
||||
case PlatformOpenCodeGo:
|
||||
return a.openCodeDefaultChatBaseURL()
|
||||
case PlatformOllamaCloud:
|
||||
return DefaultOllamaCloudBaseURL
|
||||
default:
|
||||
if a.IsMultiProtocolAPIKey() {
|
||||
// 多协议网关(CN / OpenCode / Ollama Cloud)的缺省端点必须由上面的平台
|
||||
// 分支显式给出:回落官方域名会把第三方 key 明文发到 api.openai.com。
|
||||
return ""
|
||||
}
|
||||
return "https://api.openai.com"
|
||||
}
|
||||
}
|
||||
@@ -1402,10 +1414,11 @@ func (a *Account) IsCodingPlan() bool {
|
||||
return a.GetAccountMode() == AccountModeCoding
|
||||
}
|
||||
|
||||
// GetAPIProtocol 返回国产供应商账号的上游 API 协议。存储于
|
||||
// GetAPIProtocol 返回多协议 API Key 账号的上游 API 协议。存储于
|
||||
// credentials["api_protocol"];缺失或与平台不匹配时回退 chat_completions
|
||||
// (与既有行为完全一致)。responses 协议仅 deepseek / kimi / minimax 支持(官方原生
|
||||
// Responses 端点,适配 Codex);zhipu 无此端点。
|
||||
// (与既有行为完全一致),opencode_go / ollama_cloud 例外:缺省为 adaptive
|
||||
// (按入站协议选择供应商原生端点)。responses 协议仅 deepseek / kimi / minimax 支持
|
||||
// (官方原生 Responses 端点,适配 Codex);zhipu 无此端点。
|
||||
func (a *Account) GetAPIProtocol() string {
|
||||
if a == nil || !a.IsMultiProtocolAPIKey() {
|
||||
return APIProtocolChatCompletions
|
||||
@@ -1422,7 +1435,7 @@ func (a *Account) GetAPIProtocol() string {
|
||||
case APIProtocolChatCompletions:
|
||||
return APIProtocolChatCompletions
|
||||
}
|
||||
if a.IsOpenCodeGo() {
|
||||
if a.IsOpenCodeGo() || a.IsOllamaCloud() {
|
||||
return APIProtocolAdaptive
|
||||
}
|
||||
return APIProtocolChatCompletions
|
||||
@@ -1501,6 +1514,8 @@ func (a *Account) defaultCNProtocolBaseURL(protocol string) string {
|
||||
return DefaultMiniMaxAnthropicBaseURL
|
||||
case PlatformOpenCodeGo:
|
||||
return a.openCodeDefaultAnthropicBaseURL()
|
||||
case PlatformOllamaCloud:
|
||||
return DefaultOllamaCloudAnthropicBaseURL
|
||||
}
|
||||
case APIProtocolChatCompletions, APIProtocolResponses:
|
||||
switch a.Platform {
|
||||
@@ -1520,6 +1535,8 @@ func (a *Account) defaultCNProtocolBaseURL(protocol string) string {
|
||||
return DefaultMiniMaxBaseURL
|
||||
case PlatformOpenCodeGo:
|
||||
return a.openCodeDefaultChatBaseURL()
|
||||
case PlatformOllamaCloud:
|
||||
return DefaultOllamaCloudBaseURL
|
||||
}
|
||||
}
|
||||
return ""
|
||||
|
||||
@@ -87,6 +87,8 @@ const (
|
||||
DefaultOpenCodeGoBaseURL = "https://opencode.ai/zen/go/v1"
|
||||
// OpenCode Zen:按量付费网关,模型列表为 /zen/v1/models。
|
||||
DefaultOpenCodeZenBaseURL = "https://opencode.ai/zen/v1"
|
||||
// Ollama Cloud:Chat Completions / Responses 共用带 /v1 的基址。
|
||||
DefaultOllamaCloudBaseURL = "https://ollama.com/v1"
|
||||
)
|
||||
|
||||
// 国产供应商 Anthropic 协议端点的默认 base_url(上游路径为 {base}/v1/messages)。
|
||||
@@ -100,6 +102,8 @@ const (
|
||||
// OpenCode Go Anthropic 基址不含 /v1:nativeAnthropicTargetURL 会再拼 /v1/messages。
|
||||
DefaultOpenCodeGoAnthropicBaseURL = "https://opencode.ai/zen/go"
|
||||
DefaultOpenCodeZenAnthropicBaseURL = "https://opencode.ai/zen"
|
||||
// Ollama Cloud Anthropic 基址不含 /v1:nativeAnthropicTargetURL 会再拼 /v1/messages。
|
||||
DefaultOllamaCloudAnthropicBaseURL = "https://ollama.com"
|
||||
)
|
||||
|
||||
// IsCNProvider 报告 platform 是否为国产 OpenAI 兼容供应商(kimi/zhipu/deepseek/minimax)。
|
||||
@@ -117,10 +121,15 @@ func IsOpenCodeGo(platform string) bool {
|
||||
return platform == PlatformOpenCodeGo
|
||||
}
|
||||
|
||||
// IsOllamaCloud 报告 platform 是否为 Ollama Cloud 多协议 API Key 网关。
|
||||
func IsOllamaCloud(platform string) bool {
|
||||
return platform == PlatformOllamaCloud
|
||||
}
|
||||
|
||||
// IsMultiProtocolAPIKeyProvider 报告 platform 是否为多协议 API Key 网关
|
||||
// (国产供应商 + OpenCode):走 OpenAI 网关、支持 adaptive 协议分流。
|
||||
// (国产供应商 + OpenCode + Ollama Cloud):走 OpenAI 网关、支持 adaptive 协议分流。
|
||||
func IsMultiProtocolAPIKeyProvider(platform string) bool {
|
||||
return IsCNProvider(platform) || platform == PlatformOpenCodeGo
|
||||
return IsCNProvider(platform) || platform == PlatformOpenCodeGo || IsOllamaCloud(platform)
|
||||
}
|
||||
|
||||
// AllowedQuotaPlatforms 是允许设置 user × platform quota 的平台列表(单一权威来源)。
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// ollamaCloudRoutingTestService 返回仅配置宽松 URL 白名单的 service,用于只验证
|
||||
// 出站 URL 组装,不发起真实请求。
|
||||
func ollamaCloudRoutingTestService() *OpenAIGatewayService {
|
||||
return &OpenAIGatewayService{
|
||||
cfg: &config.Config{Security: config.SecurityConfig{URLAllowlist: config.URLAllowlistConfig{
|
||||
Enabled: false,
|
||||
AllowInsecureHTTP: true,
|
||||
}}},
|
||||
}
|
||||
}
|
||||
|
||||
// TestOllamaCloudDefaultProtocolAndBaseURLs 覆盖 A9/A11/A13 + C30/Q2-3 + C33/Q2-4:
|
||||
// ollama_cloud 属多协议网关、api_protocol 缺省为 adaptive,且 anthropic 默认 base
|
||||
// 不带 /v1 而 cc/responses 带 /v1;任何情况下都不得回落到 api.openai.com。
|
||||
func TestOllamaCloudDefaultProtocolAndBaseURLs(t *testing.T) {
|
||||
account := &Account{
|
||||
ID: 900,
|
||||
Name: "ollama-cloud-default",
|
||||
Platform: PlatformOllamaCloud,
|
||||
Type: AccountTypeAPIKey,
|
||||
Credentials: map[string]any{"api_key": "sk-test"},
|
||||
Extra: map[string]any{},
|
||||
}
|
||||
|
||||
require.True(t, account.IsOllamaCloud())
|
||||
require.True(t, IsOllamaCloud(PlatformOllamaCloud))
|
||||
require.True(t, IsMultiProtocolAPIKeyProvider(PlatformOllamaCloud))
|
||||
require.True(t, account.IsMultiProtocolAPIKey())
|
||||
|
||||
// A11:api_protocol 缺失时 ollama_cloud 缺省 adaptive(不是 CC)。
|
||||
require.Equal(t, APIProtocolAdaptive, account.GetAPIProtocol())
|
||||
require.True(t, account.IsAdaptiveAPIProtocol())
|
||||
|
||||
// C30/Q2-3:anthropic 默认 base 不带 /v1;cc/responses 带 /v1。
|
||||
require.Equal(t, "https://ollama.com", account.defaultCNProtocolBaseURL(APIProtocolAnthropic))
|
||||
require.Equal(t, "https://ollama.com/v1", account.defaultCNProtocolBaseURL(APIProtocolChatCompletions))
|
||||
require.Equal(t, "https://ollama.com/v1", account.defaultCNProtocolBaseURL(APIProtocolResponses))
|
||||
|
||||
// 同一结论经 adaptive 解析链(api_base_urls / base_url 均缺失)。
|
||||
require.Equal(t, "https://ollama.com", account.GetCNProtocolBaseURL(APIProtocolAnthropic))
|
||||
require.Equal(t, "https://ollama.com/v1", account.GetCNProtocolBaseURL(APIProtocolChatCompletions))
|
||||
require.Equal(t, "https://ollama.com/v1", account.GetCNProtocolBaseURL(APIProtocolResponses))
|
||||
require.Equal(t, "https://ollama.com", account.GetAnthropicProtocolBaseURL())
|
||||
|
||||
// C33/Q2-4 + S6 层 1:OpenAI 协议 base 守卫必须接受 ollama_cloud,
|
||||
// 且缺省端点绝不是官方域名。
|
||||
require.Equal(t, DefaultOllamaCloudBaseURL, account.GetOpenAIBaseURL())
|
||||
require.NotEqual(t, "https://api.openai.com", account.GetOpenAIBaseURL())
|
||||
|
||||
// adaptive 下 anthropic 段没有 credentials.base_url 回退,CC 段有。
|
||||
withBase := &Account{
|
||||
ID: 901,
|
||||
Name: "ollama-cloud-base-url",
|
||||
Platform: PlatformOllamaCloud,
|
||||
Type: AccountTypeAPIKey,
|
||||
Credentials: map[string]any{
|
||||
"api_key": "sk-test",
|
||||
"base_url": "https://proxy.example/v1",
|
||||
},
|
||||
Extra: map[string]any{},
|
||||
}
|
||||
require.Equal(t, "https://proxy.example/v1", withBase.GetOpenAIBaseURL())
|
||||
require.Equal(t, "https://proxy.example/v1", withBase.GetCNProtocolBaseURL(APIProtocolChatCompletions))
|
||||
require.Equal(t, "https://ollama.com", withBase.GetCNProtocolBaseURL(APIProtocolAnthropic))
|
||||
require.Equal(t, "https://ollama.com/v1", withBase.GetCNProtocolBaseURL(APIProtocolResponses))
|
||||
}
|
||||
|
||||
// TestNativeAnthropicTargetURLOllamaCloudVersionAware 覆盖 S5:nativeAnthropicTargetURL
|
||||
// 改用版本感知拼接后,base 带 /v1 与不带 /v1 都得到同一个 {base}/v1/messages。
|
||||
func TestNativeAnthropicTargetURLOllamaCloudVersionAware(t *testing.T) {
|
||||
svc := ollamaCloudRoutingTestService()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
apiProtocol string
|
||||
baseURL string
|
||||
anthropicBase string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "explicit anthropic base without version suffix",
|
||||
apiProtocol: APIProtocolAnthropic,
|
||||
baseURL: "https://ollama.com",
|
||||
want: "https://ollama.com/v1/messages",
|
||||
},
|
||||
{
|
||||
name: "explicit anthropic base with version suffix",
|
||||
apiProtocol: APIProtocolAnthropic,
|
||||
baseURL: "https://ollama.com/v1",
|
||||
want: "https://ollama.com/v1/messages",
|
||||
},
|
||||
{
|
||||
name: "explicit anthropic base trailing slash",
|
||||
apiProtocol: APIProtocolAnthropic,
|
||||
baseURL: "https://ollama.com/",
|
||||
want: "https://ollama.com/v1/messages",
|
||||
},
|
||||
{
|
||||
name: "adaptive api_base_urls anthropic with version suffix",
|
||||
apiProtocol: APIProtocolAdaptive,
|
||||
anthropicBase: "https://ollama.com/v1",
|
||||
want: "https://ollama.com/v1/messages",
|
||||
},
|
||||
{
|
||||
name: "adaptive api_base_urls anthropic without version suffix",
|
||||
apiProtocol: APIProtocolAdaptive,
|
||||
anthropicBase: "https://ollama.com",
|
||||
want: "https://ollama.com/v1/messages",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
credentials := map[string]any{
|
||||
"api_key": "sk-test",
|
||||
"api_protocol": tt.apiProtocol,
|
||||
}
|
||||
if tt.baseURL != "" {
|
||||
credentials["base_url"] = tt.baseURL
|
||||
}
|
||||
if tt.anthropicBase != "" {
|
||||
credentials["api_base_urls"] = map[string]any{APIProtocolAnthropic: tt.anthropicBase}
|
||||
}
|
||||
account := &Account{
|
||||
ID: 902,
|
||||
Name: "ollama-cloud-anthropic",
|
||||
Platform: PlatformOllamaCloud,
|
||||
Type: AccountTypeAPIKey,
|
||||
Credentials: credentials,
|
||||
Extra: map[string]any{},
|
||||
}
|
||||
|
||||
targetURL, err := svc.nativeAnthropicTargetURL(account)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.want, targetURL)
|
||||
})
|
||||
}
|
||||
|
||||
// 双向钉住 buildOpenAIEndpointURL 在 messages 场景的边界行为。
|
||||
require.Equal(t, "https://ollama.com/v1/messages", buildOpenAIEndpointURL("https://ollama.com", "/v1/messages"))
|
||||
require.Equal(t, "https://ollama.com/v1/messages", buildOpenAIEndpointURL("https://ollama.com/v1", "/v1/messages"))
|
||||
require.Equal(t, "https://opencode.ai/zen/go/v1/messages", buildOpenAIEndpointURL("https://opencode.ai/zen/go", "/v1/messages"))
|
||||
require.Equal(t, "https://opencode.ai/zen/go/v1/messages", buildOpenAIEndpointURL("https://opencode.ai/zen/go/v1", "/v1/messages"))
|
||||
}
|
||||
|
||||
// TestShouldForwardOpenAIResponsesViaRawChatCompletions_OllamaCloud 覆盖 C32:
|
||||
// ollama_cloud 不能被 opencode 的「恒 false」复用,显式 CC 必须走 raw CC。
|
||||
func TestShouldForwardOpenAIResponsesViaRawChatCompletions_OllamaCloud(t *testing.T) {
|
||||
account := func(protocol string) *Account {
|
||||
return &Account{
|
||||
ID: 903,
|
||||
Name: "ollama-cloud-cc",
|
||||
Platform: PlatformOllamaCloud,
|
||||
Type: AccountTypeAPIKey,
|
||||
Credentials: map[string]any{
|
||||
"api_key": "sk-test",
|
||||
"api_protocol": protocol,
|
||||
},
|
||||
Extra: map[string]any{},
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, shouldForwardOpenAIResponsesViaRawChatCompletions(account(APIProtocolChatCompletions)))
|
||||
require.False(t, shouldForwardOpenAIResponsesViaRawChatCompletions(account(APIProtocolAnthropic)))
|
||||
// adaptive / responses 的取值跟随 SupportsNativeCNResponses()(该谓词的 ollama
|
||||
// 分支属增量 3,本增量按现状断言结构一致)。
|
||||
require.Equal(t, !account(APIProtocolAdaptive).SupportsNativeCNResponses(),
|
||||
shouldForwardOpenAIResponsesViaRawChatCompletions(account(APIProtocolAdaptive)))
|
||||
}
|
||||
|
||||
// TestForwardCountTokensAsAnthropic_OllamaCloudEstimatesLocally 覆盖 ⑨ findings B1:
|
||||
// ollama_cloud 账号命中本地估算分支,返回 200 且不产生任何出站请求。
|
||||
func TestForwardCountTokensAsAnthropic_OllamaCloudEstimatesLocally(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
body := []byte(`{"model":"claude-sonnet-4-5","system":"You are helpful.","messages":[{"role":"user","content":"hello"}]}`)
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1/messages/count_tokens", bytes.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
upstream := &httpUpstreamRecorder{err: errors.New("local estimate must not hit upstream")}
|
||||
svc := ollamaCloudRoutingTestService()
|
||||
svc.httpUpstream = upstream
|
||||
|
||||
account := &Account{
|
||||
ID: 904,
|
||||
Name: "ollama-cloud-count-tokens",
|
||||
Platform: PlatformOllamaCloud,
|
||||
Type: AccountTypeAPIKey,
|
||||
Credentials: map[string]any{"api_key": "sk-test"},
|
||||
Extra: map[string]any{},
|
||||
}
|
||||
|
||||
err := svc.ForwardCountTokensAsAnthropic(context.Background(), c, account, body, "claude-sonnet-4-5")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Greater(t, gjson.Get(rec.Body.String(), "input_tokens").Int(), int64(0))
|
||||
require.Nil(t, upstream.lastReq)
|
||||
require.Empty(t, upstream.requests)
|
||||
}
|
||||
@@ -142,6 +142,11 @@ func (s *OpenAIGatewayService) failoverOpenAIUpstreamHTTPError(
|
||||
func (s *OpenAIGatewayService) openAIChatCompletionsTargetURL(account *Account) (string, error) {
|
||||
baseURL := account.GetOpenAIBaseURL()
|
||||
if baseURL == "" {
|
||||
if account.IsMultiProtocolAPIKey() {
|
||||
// 多协议网关缺 base 时必须显式失败:回落官方域名会把第三方 key
|
||||
// 明文发到 api.openai.com。
|
||||
return "", fmt.Errorf("account %d has no openai base url", account.ID)
|
||||
}
|
||||
baseURL = "https://api.openai.com"
|
||||
}
|
||||
validatedURL, err := s.validateUpstreamBaseURL(baseURL)
|
||||
|
||||
@@ -264,14 +264,17 @@ func (s *OpenAIGatewayService) ForwardCountTokensAsAnthropic(
|
||||
return fmt.Errorf("count_tokens: missing account")
|
||||
}
|
||||
|
||||
// 国产供应商与 OpenCode(全部协议,含 anthropic):一律本地估算,不发上游请求。
|
||||
// 国产供应商、OpenCode 与 Ollama Cloud(全部协议,含 anthropic):一律本地估算,
|
||||
// 不发上游请求。
|
||||
// 依据(2026-08 核实):三家的 Anthropic 兼容层均未提供
|
||||
// /v1/messages/count_tokens——DeepSeek 官方 anthropic_api 文档无此端点
|
||||
// (且注明 anthropic-version 头被忽略),聚合网关 OpenModel 明确标注
|
||||
// count_tokens 为 "Anthropic only",Kimi/智谱亦无任何文档承诺。转发上游
|
||||
// 只会常态 404,且错误还会流入账号处置逻辑误伤整账号调度;Claude Code
|
||||
// 高频调用此端点,本地 tiktoken 估算是与 Grok 一致的既有方案。
|
||||
if account.IsCNProvider() || account.IsOpenCodeGo() {
|
||||
// Ollama Cloud 同因:其 Anthropic 兼容层同样未提供该端点(上游只有
|
||||
// /v1/responses/input_tokens),转发同样只会得到 404。
|
||||
if account.IsCNProvider() || account.IsOpenCodeGo() || account.IsOllamaCloud() {
|
||||
estimated, err := estimateAnthropicCountTokensLocally(body)
|
||||
if err != nil {
|
||||
writeAnthropicCountTokensError(c, http.StatusBadRequest, "invalid_request_error", "Failed to parse request body")
|
||||
|
||||
@@ -1369,6 +1369,18 @@ func shouldForwardOpenAIResponsesViaRawChatCompletions(account *Account) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if account.IsOllamaCloud() {
|
||||
// Ollama Cloud 无 protocol_rules:显式 CC 锁 CC,adaptive / responses
|
||||
// 走平台原生 Responses 端点(探针 Extra 不得把协议决策带偏)。
|
||||
switch account.GetAPIProtocol() {
|
||||
case APIProtocolChatCompletions:
|
||||
return true
|
||||
case APIProtocolAdaptive, APIProtocolResponses:
|
||||
return !account.SupportsNativeCNResponses()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return !openai_compat.ShouldUseResponsesAPI(account.Extra)
|
||||
}
|
||||
|
||||
@@ -1392,6 +1404,11 @@ func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.
|
||||
baseURL = account.GetCNProtocolBaseURL(APIProtocolResponses)
|
||||
}
|
||||
if baseURL == "" {
|
||||
if account.IsMultiProtocolAPIKey() {
|
||||
// 多协议网关缺 base 时必须显式失败:回落官方域名会把第三方 key
|
||||
// 明文发到 api.openai.com。
|
||||
return nil, fmt.Errorf("account %d has no openai responses base url", account.ID)
|
||||
}
|
||||
targetURL = openaiPlatformAPIURL
|
||||
} else {
|
||||
validatedURL, err := s.validateUpstreamBaseURL(baseURL)
|
||||
|
||||
@@ -133,11 +133,10 @@ func (s *OpenAIGatewayService) nativeAnthropicTargetURL(account *Account) (strin
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid base_url: %w", err)
|
||||
}
|
||||
if account.IsOpenCodeGo() {
|
||||
// OpenCode Go 的 Chat Completions base 带 /v1;用版本感知拼接避免 /v1/v1/messages。
|
||||
return buildOpenAIEndpointURL(validatedURL, "/v1/messages"), nil
|
||||
}
|
||||
return strings.TrimRight(validatedURL, "/") + "/v1/messages", nil
|
||||
// 版本感知拼接:base 已带 /v1 时不再追加,避免拼出 /v1/v1/messages
|
||||
//(OpenCode Go 的 Chat Completions base 带 /v1,Ollama Cloud 的 anthropic
|
||||
// 默认 base 不带)。
|
||||
return buildOpenAIEndpointURL(validatedURL, "/v1/messages"), nil
|
||||
}
|
||||
|
||||
func resolveOpenCodeGoMappedModel(account *Account, body []byte, defaultMappedModel string) string {
|
||||
|
||||
@@ -601,6 +601,10 @@ func (s *OpenAIGatewayService) buildUpstreamRequestOpenAIPassthrough(
|
||||
return nil, err
|
||||
}
|
||||
targetURL = buildOpenAIResponsesURLForPlatform(account.Platform, validatedURL)
|
||||
} else if account.IsMultiProtocolAPIKey() {
|
||||
// 多协议网关缺 base 时必须显式失败:targetURL 初值即官方域名,
|
||||
// 回落它会把第三方 key 明文发到 api.openai.com。
|
||||
return nil, fmt.Errorf("account %d has no openai responses base url", account.ID)
|
||||
}
|
||||
}
|
||||
targetURL = appendOpenAIResponsesRequestPathSuffix(targetURL, openAIResponsesRequestPathSuffix(c))
|
||||
|
||||
Reference in new issue
Block a user