mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-08-18 12:07:02 +08:00
refactor: remove endpoint tests and enhance URL handling
- Deleted outdated test files for OpenAI endpoint functionality. - Improved the OpenAIEndpointURL function to better handle custom paths and version segments. - Introduced a new helper function, stripEndpointVersionPrefix, to streamline endpoint processing.
This commit is contained in:
@@ -304,19 +304,24 @@ func OpenAIEndpointURL(baseURL string, endpoint string) string {
|
||||
if !strings.HasPrefix(normalizedEndpoint, "/") {
|
||||
normalizedEndpoint = "/" + normalizedEndpoint
|
||||
}
|
||||
// 规则0:自定义路径模式 → 直接用 baseURL 作为完整请求地址
|
||||
// 规则0:自定义路径模式
|
||||
// - baseURL 已含 endpoint 后缀(/chat/completions 或 /responses)→ 直接用 base
|
||||
// - 否则追加 /chat/completions(默认协议形态,覆盖 Z.AI /v4 等场景)
|
||||
if normalizedEndpoint == modelchannel.OpenAIEndpointCustom {
|
||||
return base
|
||||
if OpenAIEndpointFromBaseURL(base) != "" {
|
||||
return base
|
||||
}
|
||||
return base + "/chat/completions"
|
||||
}
|
||||
// 规则1:baseURL 已含 endpoint 后缀 → 直接用 base
|
||||
if OpenAIEndpointFromBaseURL(base) != "" {
|
||||
return base
|
||||
}
|
||||
// 规则2:通用版本段去重(/v1 /v2 /v3 /v4 ... 任意版本号)
|
||||
if version, ok := trailingVersionSegment(base); ok {
|
||||
prefix := "/" + version + "/"
|
||||
if strings.HasPrefix(normalizedEndpoint, prefix) {
|
||||
return base + strings.TrimPrefix(normalizedEndpoint, "/"+version)
|
||||
// 规则2:baseURL 以 /vN 结尾时,剥离 endpoint 的版本前缀(/v1/、/v2/ 等)
|
||||
// 这样 base=.../v4 + endpoint=/v1/chat/completions → .../v4/chat/completions
|
||||
if _, ok := trailingVersionSegment(base); ok {
|
||||
if rest, stripped := stripEndpointVersionPrefix(normalizedEndpoint); stripped {
|
||||
return base + rest
|
||||
}
|
||||
}
|
||||
// 规则3:兜底原样拼接
|
||||
@@ -342,6 +347,24 @@ func trailingVersionSegment(base string) (string, bool) {
|
||||
return seg, true
|
||||
}
|
||||
|
||||
// stripEndpointVersionPrefix 剥离 endpoint 路径开头的版本段前缀(/vN/),
|
||||
// 返回剩余路径和是否成功剥离。
|
||||
// /v1/chat/completions → ("/chat/completions", true)
|
||||
// /chat/completions → ("", false)
|
||||
func stripEndpointVersionPrefix(endpoint string) (string, bool) {
|
||||
if len(endpoint) < 4 || endpoint[0] != '/' || endpoint[1] != 'v' {
|
||||
return "", false
|
||||
}
|
||||
i := 2
|
||||
for i < len(endpoint) && endpoint[i] >= '0' && endpoint[i] <= '9' {
|
||||
i++
|
||||
}
|
||||
if i == 2 || i >= len(endpoint) || endpoint[i] != '/' {
|
||||
return "", false
|
||||
}
|
||||
return endpoint[i:], true
|
||||
}
|
||||
|
||||
func ResolveOpenAIEndpoint(baseURL string, endpoint string) string {
|
||||
if endpointFromURL := OpenAIEndpointFromBaseURL(baseURL); endpointFromURL != "" {
|
||||
return endpointFromURL
|
||||
|
||||
Reference in New Issue
Block a user