mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-08-17 11:37:20 +08:00
feat: add support for custom OpenAI endpoint path
- Introduced a new endpoint option for OpenAI integrations, allowing users to specify a custom path. - Updated relevant components and validation logic to accommodate the new endpoint. - Enhanced documentation and error messages to reflect the addition of the custom endpoint option.
This commit is contained in:
@@ -13,6 +13,7 @@ const ChannelIDHexLength = 16
|
||||
const (
|
||||
OpenAIEndpointResponses = "/v1/responses"
|
||||
OpenAIEndpointChatCompletions = "/v1/chat/completions"
|
||||
OpenAIEndpointCustom = "/custom"
|
||||
)
|
||||
|
||||
func NormalizeBaseURL(raw string) (string, error) {
|
||||
@@ -39,20 +40,37 @@ func NormalizeBaseURL(raw string) (string, error) {
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
// NormalizeOpenAIEndpoint 归一化 OpenAI endpoint 路径。
|
||||
// 支持三个预设值:/v1/responses、/v1/chat/completions、/custom(自定义路径)。
|
||||
// 选 /custom 时,用户需在接口地址栏填写完整请求 URL。
|
||||
func NormalizeOpenAIEndpoint(providerType string, endpoint string) string {
|
||||
if strings.TrimSpace(strings.ToLower(providerType)) != "openai" {
|
||||
return ""
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(endpoint)) {
|
||||
case "", OpenAIEndpointResponses:
|
||||
normalized := strings.TrimSpace(endpoint)
|
||||
switch normalized {
|
||||
case "":
|
||||
return OpenAIEndpointResponses
|
||||
case OpenAIEndpointChatCompletions:
|
||||
return OpenAIEndpointChatCompletions
|
||||
case OpenAIEndpointResponses, OpenAIEndpointChatCompletions, OpenAIEndpointCustom:
|
||||
return normalized
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// OpenAIEndpointShape 根据 endpoint 路径末段推断协议形态。
|
||||
// 返回 "responses"(Responses API)或 "chat/completions"(Chat Completions API)。
|
||||
// 这样 /v1/chat/completions、/v4/chat/completions、/chat/completions 都走同一协议分支。
|
||||
func OpenAIEndpointShape(endpoint string) string {
|
||||
lower := strings.ToLower(strings.TrimSpace(endpoint))
|
||||
switch {
|
||||
case strings.HasSuffix(lower, "/responses"):
|
||||
return "responses"
|
||||
default:
|
||||
return "chat/completions"
|
||||
}
|
||||
}
|
||||
|
||||
func BuildLegacyChannelID(baseURL string, modelID string, apiKey string, name string) string {
|
||||
return buildChannelID([]string{
|
||||
strings.TrimSpace(baseURL),
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package modelchannel
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeOpenAIEndpoint(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
providerType string
|
||||
endpoint string
|
||||
want string
|
||||
}{
|
||||
// 非 openai 类型始终返回空
|
||||
{"anthropic_ignored", "anthropic", "/v1/responses", ""},
|
||||
{"empty_type_ignored", "", "/v1/responses", ""},
|
||||
|
||||
// 预设值
|
||||
{"empty_defaults_to_responses", "openai", "", OpenAIEndpointResponses},
|
||||
{"preset_responses", "openai", "/v1/responses", OpenAIEndpointResponses},
|
||||
{"preset_chat_completions", "openai", "/v1/chat/completions", OpenAIEndpointChatCompletions},
|
||||
{"preset_custom", "openai", "/custom", OpenAIEndpointCustom},
|
||||
|
||||
// 非法值:不再允许任意自定义路径,只接受三个预设值
|
||||
{"arbitrary_path_rejected", "openai", "/v4/chat/completions", ""},
|
||||
{"arbitrary_path_rejected_2", "openai", "/chat/completions", ""},
|
||||
{"missing_slash", "openai", "v1/responses", ""},
|
||||
{"slash_only", "openai", "/", ""},
|
||||
{"whitespace", "openai", " ", OpenAIEndpointResponses},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := NormalizeOpenAIEndpoint(tc.providerType, tc.endpoint)
|
||||
if got != tc.want {
|
||||
t.Fatalf("NormalizeOpenAIEndpoint(%q, %q) = %q, want %q", tc.providerType, tc.endpoint, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAIEndpointShape(t *testing.T) {
|
||||
cases := []struct {
|
||||
endpoint string
|
||||
want string
|
||||
}{
|
||||
// Responses 形态
|
||||
{"/v1/responses", "responses"},
|
||||
{"/v4/responses", "responses"},
|
||||
{"/responses", "responses"},
|
||||
|
||||
// Chat Completions 形态
|
||||
{"/v1/chat/completions", "chat/completions"},
|
||||
{"/v4/chat/completions", "chat/completions"},
|
||||
{"/chat/completions", "chat/completions"},
|
||||
|
||||
// /custom 不含已知后缀 → 兜底走 chat/completions
|
||||
{"/custom", "chat/completions"},
|
||||
|
||||
// 兜底默认
|
||||
{"", "chat/completions"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.endpoint, func(t *testing.T) {
|
||||
got := OpenAIEndpointShape(tc.endpoint)
|
||||
if got != tc.want {
|
||||
t.Fatalf("OpenAIEndpointShape(%q) = %q, want %q", tc.endpoint, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user