feat: sync deployed v1.0.0-rc.38-custom.42
Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
This commit is contained in:
1 parent
aa6c3f50f4
commit
ceddfd3d6b
828 files changed
+122253
-13702
No files matched your search
+109
-19
@@ -1,11 +1,14 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/i18n"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
@@ -154,15 +157,15 @@ export const protocols = {openai_responses: {
|
||||
|
||||
func TestTokenModelLimitAllowsLegacyAliasAndModifierVariant(t *testing.T) {
|
||||
aliasOnly := map[string]bool{"claude-3-7-sonnet-thinking": true}
|
||||
assert.True(t, tokenModelLimitAllows(aliasOnly, "claude-3-7-sonnet-thinking"))
|
||||
assert.False(t, tokenModelLimitAllows(aliasOnly, "claude-3-7-sonnet"))
|
||||
assert.True(t, TokenModelLimitAllows(aliasOnly, "claude-3-7-sonnet-thinking"))
|
||||
assert.False(t, TokenModelLimitAllows(aliasOnly, "claude-3-7-sonnet"))
|
||||
|
||||
baseOnly := map[string]bool{"claude-3-7-sonnet": true}
|
||||
assert.True(t, tokenModelLimitAllows(baseOnly, "claude-3-7-sonnet@thinking:on"))
|
||||
assert.True(t, tokenModelLimitAllows(baseOnly, "claude-3-7-sonnet-thinking"))
|
||||
assert.True(t, TokenModelLimitAllows(baseOnly, "claude-3-7-sonnet@thinking:on"))
|
||||
assert.True(t, TokenModelLimitAllows(baseOnly, "claude-3-7-sonnet-thinking"))
|
||||
|
||||
wildcard := map[string]bool{"gemini-2.5-flash-thinking-*": true}
|
||||
assert.True(t, tokenModelLimitAllows(wildcard, "gemini-2.5-flash-thinking-8192"))
|
||||
assert.True(t, TokenModelLimitAllows(wildcard, "gemini-2.5-flash-thinking-8192"))
|
||||
}
|
||||
|
||||
func TestTokenModelLimitAllowsExemptAtNameByFullName(t *testing.T) {
|
||||
@@ -172,27 +175,90 @@ func TestTokenModelLimitAllowsExemptAtNameByFullName(t *testing.T) {
|
||||
settings.ThinkingModelBlacklist = append(original, "re:.*@sha256:.*")
|
||||
|
||||
fullOnly := map[string]bool{"opaque@sha256:deadbeef": true}
|
||||
assert.True(t, tokenModelLimitAllows(fullOnly, "opaque@sha256:deadbeef"))
|
||||
assert.True(t, TokenModelLimitAllows(fullOnly, "opaque@sha256:deadbeef"))
|
||||
|
||||
baseOnly := map[string]bool{"opaque": true}
|
||||
assert.False(t, tokenModelLimitAllows(baseOnly, "opaque@sha256:deadbeef"))
|
||||
assert.False(t, TokenModelLimitAllows(baseOnly, "opaque@sha256:deadbeef"))
|
||||
}
|
||||
|
||||
func TestNoAvailableChannelMessageNamesClaimingTaskPlugin(t *testing.T) {
|
||||
func TestDistributeHidesTaskPluginDetailsButLogsDiagnostics(t *testing.T) {
|
||||
require.NoError(t, i18n.Init())
|
||||
registry := jsplugin.NewRegistry()
|
||||
plugin, err := registry.Register(distributorTaskPluginSource("claimer", constant.ChannelTypeKling), jsplugin.Options{})
|
||||
require.NoError(t, err)
|
||||
previousCacheEnabled := common.MemoryCacheEnabled
|
||||
common.MemoryCacheEnabled = true
|
||||
t.Cleanup(func() { common.MemoryCacheEnabled = previousCacheEnabled })
|
||||
|
||||
pinned, _ := gin.CreateTestContext(nil)
|
||||
pinned.Request = httptest.NewRequest(http.MethodPost, "/v1/videos", nil)
|
||||
pinned.Request.Header.Set("Accept-Language", "en")
|
||||
pinned.Set(jsplugin.ContextKeyPinnedPlugin, jsplugin.PinnedPlugin{Generation: registry.Generation(), Plugin: plugin})
|
||||
message := noAvailableChannelMessage(pinned, "default", "kling-v1")
|
||||
assert.Contains(t, message, `"claimer"`)
|
||||
assert.Contains(t, message, "disable or override")
|
||||
assert.Contains(t, message, "kling-v1")
|
||||
const group = "private-plugin-error-test-group"
|
||||
for _, locale := range []struct{ language, message string }{
|
||||
{"en", "No available channel for model task-model under group " + group + ": the model is claimed by a task plugin, which has no enabled channel serving it (distributor)"},
|
||||
{"zh-CN", "分组 " + group + " 下模型 task-model 无可用渠道:该模型由任务插件认领,但当前没有启用的渠道可服务此模型(distributor)"},
|
||||
{"zh-TW", "分組 " + group + " 下模型 task-model 無可用管道:該模型由任務插件認領,但目前沒有啟用的管道可服務此模型(distributor)"},
|
||||
} {
|
||||
for _, providerCount := range []int{1, 2} {
|
||||
t.Run(fmt.Sprintf("%s/%d_providers", locale.language, providerCount), func(t *testing.T) {
|
||||
registry := jsplugin.NewRegistry()
|
||||
keys := []string{"private-provider-alpha", "private-provider-beta"}[:providerCount]
|
||||
for index, key := range keys {
|
||||
_, err := registry.Register(distributorEndpointPluginSource(key, constant.ChannelTypeKling+index), jsplugin.Options{})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
generation := registry.Generation()
|
||||
candidates := generation.LookupEndpointCandidates("POST", "/v1/responses", "task-model")
|
||||
require.Len(t, candidates, providerCount)
|
||||
|
||||
var logs bytes.Buffer
|
||||
common.LogWriterMu.Lock()
|
||||
previousWriter := gin.DefaultErrorWriter
|
||||
gin.DefaultErrorWriter = &logs
|
||||
common.LogWriterMu.Unlock()
|
||||
t.Cleanup(func() {
|
||||
common.LogWriterMu.Lock()
|
||||
gin.DefaultErrorWriter = previousWriter
|
||||
common.LogWriterMu.Unlock()
|
||||
})
|
||||
|
||||
router := gin.New()
|
||||
router.POST("/v1/responses", RequestId(), func(c *gin.Context) {
|
||||
common.SetContextKey(c, constant.ContextKeyUsingGroup, group)
|
||||
c.Set("resolved_task_model", "task-model")
|
||||
c.Set("expected_task_plugin_key", keys[0])
|
||||
c.Set(jsplugin.ContextKeyPinnedPlugin, jsplugin.PinnedPlugin{Generation: generation, Plugin: candidates[0].Plugin})
|
||||
if providerCount > 1 {
|
||||
c.Set(jsplugin.ContextKeyPinnedEndpoint, jsplugin.PinnedEndpoint{
|
||||
Generation: generation, Plugin: candidates[0].Plugin,
|
||||
Protocol: candidates[0].Protocol, Operation: candidates[0].Operation,
|
||||
Model: "task-model", Candidates: candidates,
|
||||
})
|
||||
}
|
||||
}, Distribute(), func(c *gin.Context) {
|
||||
t.Error("unavailable requests must stop before the relay handler")
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
request := httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
|
||||
request.Header.Set("Accept-Language", locale.language)
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, request)
|
||||
|
||||
require.Equal(t, http.StatusServiceUnavailable, recorder.Code)
|
||||
requestID := recorder.Header().Get(common.RequestIdKey)
|
||||
require.NotEmpty(t, requestID)
|
||||
assert.JSONEq(t, fmt.Sprintf(`{"error":{"message":%q,"type":"new_api_error","code":"model_not_found"}}`,
|
||||
locale.message+" (request id: "+requestID+")"), recorder.Body.String())
|
||||
assert.NotContains(t, recorder.Body.String(), "disable or override")
|
||||
for _, key := range keys {
|
||||
assert.NotContains(t, recorder.Body.String(), key)
|
||||
assert.Contains(t, logs.String(), key)
|
||||
}
|
||||
assert.Contains(t, logs.String(), requestID)
|
||||
assert.Contains(t, logs.String(), `group="`+group+`"`)
|
||||
assert.Contains(t, logs.String(), `model="task-model"`)
|
||||
assert.Contains(t, logs.String(), "reason=no_eligible_channel")
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoAvailableChannelMessageWithoutPlugin(t *testing.T) {
|
||||
require.NoError(t, i18n.Init())
|
||||
plain, _ := gin.CreateTestContext(nil)
|
||||
plain.Request = httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil)
|
||||
plain.Request.Header.Set("Accept-Language", "en")
|
||||
@@ -200,3 +266,27 @@ func TestNoAvailableChannelMessageNamesClaimingTaskPlugin(t *testing.T) {
|
||||
assert.NotContains(t, generic, "task plugin")
|
||||
assert.Contains(t, generic, "gpt-4o")
|
||||
}
|
||||
|
||||
func TestSharedEndpointRebindsToSelectedType61Plugin(t *testing.T) {
|
||||
registry := jsplugin.NewRegistry()
|
||||
for _, key := range []string{"alpha", "beta"} {
|
||||
source := strings.Replace(distributorEndpointPluginSource(key, 0), "channelTypes: [0],", "", 1)
|
||||
_, err := registry.Register(source, jsplugin.Options{})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
generation := registry.Generation()
|
||||
candidates := generation.LookupEndpointCandidates("POST", "/v1/responses", "task-model")
|
||||
require.Len(t, candidates, 2)
|
||||
c, _ := gin.CreateTestContext(nil)
|
||||
c.Set(jsplugin.ContextKeyPinnedPlugin, jsplugin.PinnedPlugin{Generation: generation, Plugin: candidates[0].Plugin})
|
||||
c.Set(jsplugin.ContextKeyPinnedEndpoint, jsplugin.PinnedEndpoint{Generation: generation, Plugin: candidates[0].Plugin, Protocol: candidates[0].Protocol, Operation: candidates[0].Operation, Model: "task-model", Candidates: candidates})
|
||||
c.Set("expected_task_plugin_key", "alpha")
|
||||
channel := &model.Channel{Id: 2, Type: constant.ChannelTypeTaskPlugin}
|
||||
channel.SetSetting(dto.ChannelSettings{TaskPluginKey: "unrelated"})
|
||||
assert.False(t, channelMatchesExpectedTaskPlugin(c, channel, "alpha"))
|
||||
channel.SetSetting(dto.ChannelSettings{TaskPluginKey: "beta"})
|
||||
require.Nil(t, SetupContextForSelectedChannel(c, channel, "task-model"))
|
||||
assert.Equal(t, "beta", c.GetString("task_plugin_key"))
|
||||
assert.Equal(t, "beta", c.GetString("expected_task_plugin_key"))
|
||||
assert.Equal(t, "beta", c.MustGet(jsplugin.ContextKeyPinnedEndpoint).(jsplugin.PinnedEndpoint).Plugin.Meta.Key)
|
||||
}
|
||||
Reference in new issue
Block a user