feat: add sub2api compatibility
This commit is contained in:
+108
-1
@@ -129,6 +129,8 @@ func normalizePrivateSetting(setting model.PrivateSetting) model.PrivateSetting
|
||||
if setting.Channels[i].Protocol == "" {
|
||||
setting.Channels[i].Protocol = "openai"
|
||||
}
|
||||
setting.Channels[i].Compatibility = normalizeChannelCompatibility(setting.Channels[i].Compatibility)
|
||||
setting.Channels[i].RequestOptions = normalizeModelRequestOptions(setting.Channels[i].Compatibility, setting.Channels[i].RequestOptions)
|
||||
if setting.Channels[i].Models == nil {
|
||||
setting.Channels[i].Models = []string{}
|
||||
}
|
||||
@@ -202,12 +204,54 @@ func SelectModelChannel(modelName string) (model.ModelChannel, error) {
|
||||
func BuildModelChannelURL(channel model.ModelChannel, path string) string {
|
||||
baseURL := normalizeModelChannelBaseURL(channel.BaseURL)
|
||||
lowerBaseURL := strings.ToLower(baseURL)
|
||||
if !strings.HasSuffix(lowerBaseURL, "/v1") && !strings.HasSuffix(lowerBaseURL, "/api/v3") && !strings.HasSuffix(lowerBaseURL, "/api/plan/v3") {
|
||||
if !hasModelChannelVersionSuffix(lowerBaseURL) && !strings.HasSuffix(lowerBaseURL, "/api/v3") && !strings.HasSuffix(lowerBaseURL, "/api/plan/v3") {
|
||||
baseURL += "/v1"
|
||||
}
|
||||
return baseURL + path
|
||||
}
|
||||
|
||||
func hasModelChannelVersionSuffix(baseURL string) bool {
|
||||
pathValue := baseURL
|
||||
if parsed, err := url.Parse(baseURL); err == nil && parsed.Scheme != "" && parsed.Host != "" {
|
||||
pathValue = parsed.Path
|
||||
}
|
||||
pathValue = strings.TrimRight(pathValue, "/")
|
||||
if pathValue == "" {
|
||||
return false
|
||||
}
|
||||
segment := pathValue
|
||||
if slash := strings.LastIndex(pathValue, "/"); slash >= 0 {
|
||||
segment = pathValue[slash+1:]
|
||||
}
|
||||
segment = strings.ToLower(strings.TrimSpace(segment))
|
||||
if len(segment) < 2 || segment[0] != 'v' || !isASCIIDigit(segment[1]) {
|
||||
return false
|
||||
}
|
||||
i := 1
|
||||
for i < len(segment) && isASCIIDigit(segment[i]) {
|
||||
i++
|
||||
}
|
||||
if i == len(segment) {
|
||||
return true
|
||||
}
|
||||
if segment[i] == '.' {
|
||||
i++
|
||||
if i == len(segment) || !isASCIIDigit(segment[i]) {
|
||||
return false
|
||||
}
|
||||
for i < len(segment) && isASCIIDigit(segment[i]) {
|
||||
i++
|
||||
}
|
||||
return i == len(segment)
|
||||
}
|
||||
suffix := segment[i:]
|
||||
return strings.HasPrefix(suffix, "alpha") || strings.HasPrefix(suffix, "beta") || strings.HasPrefix(suffix, "preview")
|
||||
}
|
||||
|
||||
func isASCIIDigit(ch byte) bool {
|
||||
return ch >= '0' && ch <= '9'
|
||||
}
|
||||
|
||||
func normalizeModelChannelBaseURL(baseURL string) string {
|
||||
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
parsed, err := url.Parse(baseURL)
|
||||
@@ -299,6 +343,8 @@ func normalizeModelChannel(channel model.ModelChannel) model.ModelChannel {
|
||||
if channel.Protocol == "" {
|
||||
channel.Protocol = "openai"
|
||||
}
|
||||
channel.Compatibility = normalizeChannelCompatibility(channel.Compatibility)
|
||||
channel.RequestOptions = normalizeModelRequestOptions(channel.Compatibility, channel.RequestOptions)
|
||||
if channel.Models == nil {
|
||||
channel.Models = []string{}
|
||||
}
|
||||
@@ -308,6 +354,30 @@ func normalizeModelChannel(channel model.ModelChannel) model.ModelChannel {
|
||||
return channel
|
||||
}
|
||||
|
||||
func normalizeChannelCompatibility(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "sub2api":
|
||||
return "sub2api"
|
||||
default:
|
||||
return "openai"
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeModelRequestOptions(compatibility string, options model.ModelRequestOptions) model.ModelRequestOptions {
|
||||
format := strings.ToLower(strings.TrimSpace(options.ImageResponseFormat))
|
||||
switch format {
|
||||
case "url", "b64_json":
|
||||
options.ImageResponseFormat = format
|
||||
default:
|
||||
if normalizeChannelCompatibility(compatibility) == "sub2api" {
|
||||
options.ImageResponseFormat = "url"
|
||||
} else {
|
||||
options.ImageResponseFormat = "b64_json"
|
||||
}
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
func resolveAdminChannel(index *int, channel model.ModelChannel) (model.ModelChannel, error) {
|
||||
resolved := normalizeModelChannel(channel)
|
||||
if strings.TrimSpace(resolved.APIKey) == "" {
|
||||
@@ -380,6 +450,9 @@ func testAdminChannelModel(channel model.ModelChannel, modelName string) (string
|
||||
if strings.TrimSpace(modelName) == "" {
|
||||
return "", errors.New("缺少模型名称")
|
||||
}
|
||||
if isImageModelName(modelName) {
|
||||
return testAdminImageChannelModel(channel, modelName)
|
||||
}
|
||||
body, _ := json.Marshal(map[string]any{
|
||||
"model": modelName,
|
||||
"messages": []map[string]string{{
|
||||
@@ -416,6 +489,40 @@ func testAdminChannelModel(channel model.ModelChannel, modelName string) (string
|
||||
return "ok", nil
|
||||
}
|
||||
|
||||
func testAdminImageChannelModel(channel model.ModelChannel, modelName string) (string, error) {
|
||||
format := strings.ToLower(strings.TrimSpace(channel.RequestOptions.ImageResponseFormat))
|
||||
if format == "" {
|
||||
if strings.EqualFold(strings.TrimSpace(channel.Compatibility), "sub2api") {
|
||||
format = "url"
|
||||
} else {
|
||||
format = "b64_json"
|
||||
}
|
||||
}
|
||||
body, _ := json.Marshal(map[string]any{
|
||||
"model": modelName,
|
||||
"prompt": "test",
|
||||
"n": 1,
|
||||
"size": "1024x1024",
|
||||
"response_format": format,
|
||||
})
|
||||
request, err := http.NewRequest(http.MethodPost, BuildModelChannelURL(channel, "/images/generations"), strings.NewReader(string(body)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+channel.APIKey)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := adminModelHTTPClient.Do(request)
|
||||
if err != nil {
|
||||
return "", safeMessageError{message: "测试失败:上游接口无响应或网络不可达"}
|
||||
}
|
||||
defer response.Body.Close()
|
||||
responseBody, _ := io.ReadAll(response.Body)
|
||||
if response.StatusCode >= http.StatusBadRequest {
|
||||
return "", readAdminChannelError(responseBody, response.StatusCode, "测试失败")
|
||||
}
|
||||
return "ok", nil
|
||||
}
|
||||
|
||||
func testArkSeedanceChannelModel(channel model.ModelChannel, modelName string) (string, error) {
|
||||
if strings.TrimSpace(modelName) == "" {
|
||||
return "", errors.New("缺少模型名称")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
@@ -61,6 +62,56 @@ func TestBuildModelChannelURLNormalizesArkPlanTaskPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildModelChannelURLAcceptsVersionedSub2APIBaseURL(t *testing.T) {
|
||||
got := BuildModelChannelURL(model.ModelChannel{BaseURL: "https://sub2api.example.com/v1"}, "/images/generations")
|
||||
want := "https://sub2api.example.com/v1/images/generations"
|
||||
if got != want {
|
||||
t.Fatalf("BuildModelChannelURL = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePrivateSettingDefaultsSub2APIImageResponseFormatToURL(t *testing.T) {
|
||||
setting := normalizePrivateSetting(model.PrivateSetting{Channels: []model.ModelChannel{{Compatibility: "sub2api"}}})
|
||||
if got := setting.Channels[0].RequestOptions.ImageResponseFormat; got != "url" {
|
||||
t.Fatalf("image response format = %q, want url", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminTestChannelModelUsesImageGenerationProbeForImageModels(t *testing.T) {
|
||||
var gotPath string
|
||||
var gotPayload map[string]any
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
if err := json.NewDecoder(r.Body).Decode(&gotPayload); err != nil {
|
||||
t.Fatalf("Decode request body: %v", err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[{"url":"https://example.com/image.png"}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := testAdminChannelModel(model.ModelChannel{
|
||||
Compatibility: "sub2api",
|
||||
BaseURL: server.URL + "/v1",
|
||||
APIKey: "test-key",
|
||||
}, "gpt-image-2")
|
||||
if err != nil {
|
||||
t.Fatalf("testAdminChannelModel returned error: %v", err)
|
||||
}
|
||||
if result != "ok" {
|
||||
t.Fatalf("result = %q, want ok", result)
|
||||
}
|
||||
if gotPath != "/v1/images/generations" {
|
||||
t.Fatalf("path = %q, want /v1/images/generations", gotPath)
|
||||
}
|
||||
if gotPayload["model"] != "gpt-image-2" {
|
||||
t.Fatalf("model = %#v", gotPayload["model"])
|
||||
}
|
||||
if gotPayload["response_format"] != "url" {
|
||||
t.Fatalf("response_format = %#v, want url", gotPayload["response_format"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeSettingsPublishesEnabledChannelModelsAndRepairsDefaults(t *testing.T) {
|
||||
settings := normalizeSettings(model.Settings{
|
||||
Public: model.PublicSetting{
|
||||
|
||||
Reference in New Issue
Block a user