mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(ollama): 出站整形的官方 host 判定容忍 base_url 末尾斜杠
CC/Responses 路径的 max_tokens clamp 与推理字段归一化直接把 base_url 交给 isOllamaCloudBaseURL(只认 path 为空或 /v1),"https://ollama.com/v1/" 这类 配置出站端点不变却漏掉 65535 钳制而被上游 400;Anthropic 侧早已先去尾斜杠。 出站整形统一经 isOllamaCloudOutboundBaseURL;用量窗口资格门禁仍严格比对。
This commit is contained in:
1 parent
502f8b827d
commit
6e5faccef6
6 files changed
+24
-9
No files matched your search
@@ -42,7 +42,7 @@ func (a *Account) GetAnthropicAPIKeyAuthScheme() string {
|
||||
// 与 '.../v1' 均合理),再复用既有严格 host helper isOllamaCloudBaseURL(精确
|
||||
// host、https、无 query/fragment、路径仅空或 /v1),不按 substring 匹配。
|
||||
func isOllamaCloudAnthropicAuthBaseURL(baseURL string) bool {
|
||||
return isOllamaCloudBaseURL(strings.TrimRight(strings.TrimSpace(baseURL), "/"))
|
||||
return isOllamaCloudOutboundBaseURL(baseURL)
|
||||
}
|
||||
|
||||
// setAnthropicAPIKeyAuthHeader 写入上游认证头。除 extra 覆写外,Ollama Cloud
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// clampOllamaCloudAnthropicMessagesMaxTokens 是 Anthropic Messages 出站
|
||||
// (/v1/messages,含 CC / Responses 桥接与 passthrough)的 max_tokens clamp 钩子,
|
||||
// 与 raw CC / Responses 路径共用 cap 配置与实现。判定与出站 URL 组装同源:调用方
|
||||
@@ -16,7 +12,7 @@ func clampOllamaCloudAnthropicMessagesMaxTokens(account *Account, baseURL string
|
||||
if account == nil || len(body) == 0 {
|
||||
return body
|
||||
}
|
||||
if !isOllamaCloudBaseURL(strings.TrimRight(strings.TrimSpace(baseURL), "/")) {
|
||||
if !isOllamaCloudOutboundBaseURL(baseURL) {
|
||||
return body
|
||||
}
|
||||
return clampOllamaCloudMaxTokens(account, body)
|
||||
|
||||
@@ -1119,6 +1119,14 @@ func IsOllamaCloudUsageAccount(account *Account) bool {
|
||||
return ollamaCloudUsageEligibilityReason(account) == ""
|
||||
}
|
||||
|
||||
// isOllamaCloudOutboundBaseURL 是出站请求整形(max_tokens clamp、推理字段归一化、
|
||||
// Bearer 认证)用的官方 host 判定:URL 拼装会规范化末尾 "/","https://ollama.com/v1/"
|
||||
// 与 "https://ollama.com/v1" 发往同一端点,判定必须一致。用量窗口资格门禁仍按
|
||||
// isOllamaCloudBaseURL 严格比对,不受影响。
|
||||
func isOllamaCloudOutboundBaseURL(raw string) bool {
|
||||
return isOllamaCloudBaseURL(strings.TrimRight(strings.TrimSpace(raw), "/"))
|
||||
}
|
||||
|
||||
func isOllamaCloudBaseURL(raw string) bool {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || strings.ContainsAny(raw, "?#") {
|
||||
|
||||
@@ -22,7 +22,7 @@ func isOllamaCloudDeepSeekUpstream(account *Account, upstreamModel string) bool
|
||||
if account == nil {
|
||||
return false
|
||||
}
|
||||
if !isOllamaCloudBaseURL(account.GetOpenAIBaseURL()) {
|
||||
if !isOllamaCloudOutboundBaseURL(account.GetOpenAIBaseURL()) {
|
||||
return false
|
||||
}
|
||||
return isDeepSeekModel(upstreamModel)
|
||||
|
||||
@@ -28,7 +28,7 @@ func clampOllamaCloudUpstreamMaxTokens(account *Account, body []byte) []byte {
|
||||
if account == nil || len(body) == 0 {
|
||||
return body
|
||||
}
|
||||
if !isOllamaCloudBaseURL(account.GetOpenAIBaseURL()) {
|
||||
if !isOllamaCloudOutboundBaseURL(account.GetOpenAIBaseURL()) {
|
||||
return body
|
||||
}
|
||||
return clampOllamaCloudMaxTokens(account, body)
|
||||
@@ -59,7 +59,7 @@ func ollamaCloudResponsesMaxOutputTokensClamp(account *Account, upstreamModel st
|
||||
if account == nil || account.Type != AccountTypeAPIKey {
|
||||
return 0, false, ""
|
||||
}
|
||||
if !isOllamaCloudBaseURL(ollamaCloudResponsesUpstreamBaseURL(account)) {
|
||||
if !isOllamaCloudOutboundBaseURL(ollamaCloudResponsesUpstreamBaseURL(account)) {
|
||||
return 0, false, ""
|
||||
}
|
||||
value := gjson.GetBytes(body, "max_output_tokens")
|
||||
|
||||
@@ -188,6 +188,17 @@ func ollamaUpstreamTestAccount(platform string, id int64) *Account {
|
||||
}
|
||||
}
|
||||
|
||||
// base_url 末尾 "/" 不改变出站端点(URL 拼装会规范化),clamp 判定必须与之一致:
|
||||
// "https://ollama.com/v1/" 与 "https://ollama.com/" 同样按 Ollama 上游钳制。
|
||||
func TestClampOllamaCloudUpstreamMaxTokensToleratesTrailingSlash(t *testing.T) {
|
||||
for _, baseURL := range []string{"https://ollama.com/v1/", "https://ollama.com/"} {
|
||||
account := ollamaUpstreamTestAccount(PlatformOllamaCloud, 303)
|
||||
account.Credentials["base_url"] = baseURL
|
||||
got := clampOllamaCloudUpstreamMaxTokens(account, []byte(`{"model":"gpt-oss:120b","max_tokens":100000}`))
|
||||
require.JSONEq(t, `{"model":"gpt-oss:120b","max_tokens":65535}`, string(got), "base_url=%s", baseURL)
|
||||
}
|
||||
}
|
||||
|
||||
// officialDeepSeekTestAccount 构造官方 DeepSeek 账号,extra 里带残留的 Ollama
|
||||
// usage 快照键:usage extra 不得把非 Ollama host 识别为 Ollama 上游。
|
||||
func officialDeepSeekTestAccount(id int64) *Account {
|
||||
|
||||
Reference in new issue
Block a user