Feat/auto group (#6590)
* feat(token): support custom auto group order * feat(keys): enhance auto group presentation * fix(keys): rework Auto flow border and compact inherited order The Auto group highlight previously tinted the whole control surface with a gradient and animated only a 1px top sweep, which read as a background color rather than a flowing border. Replace it with a border-only effect: an aria-hidden, pointer-events-none overlay whose conic gradient is masked down to a thin ring hugging the rounded perimeter, so the highlight travels around all four edges and corners every 3.2s. The interior stays neutral with a restrained static primary border and glow; prefers-reduced-motion hides the moving layer while keeping the static emphasis. The inherited global Auto order also rendered as spacious two-line rows with circular sequence markers, wasting drawer space. Render it as a compact wrapping strip of one-line chips (index, name, ratio badge) with descriptions kept accessible via title and sr-only text, scrolling only past a much smaller max height. Custom add/remove/reorder editing, empty-array inheritance semantics, and the submit payload are unchanged. * fix(keys): preserve Auto inheritance and unify effects * refactor(keys): temporarily disable AutoGroupBadge in api-key-group-cell
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/logger"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/QuantumNous/new-api/setting"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -88,10 +87,10 @@ func CacheGetRandomSatisfiedChannel(param *RetryParam) (*model.Channel, string,
|
||||
userGroup := common.GetContextKeyString(param.Ctx, constant.ContextKeyUserGroup)
|
||||
|
||||
if param.TokenGroup == "auto" {
|
||||
if len(setting.GetAutoGroups()) == 0 {
|
||||
autoGroups := GetRequestAutoGroups(param.Ctx, userGroup)
|
||||
if len(autoGroups) == 0 {
|
||||
return nil, selectGroup, errors.New("auto groups is not enabled")
|
||||
}
|
||||
autoGroups := GetUserAutoGroup(userGroup)
|
||||
|
||||
// startGroupIndex: the group index to start searching from
|
||||
// startGroupIndex: 开始搜索的分组索引
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/QuantumNous/new-api/setting"
|
||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func setupChannelSelectAutoGroupsTest(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
originalDB := model.DB
|
||||
originalMemoryCacheEnabled := common.MemoryCacheEnabled
|
||||
originalRetryTimes := common.RetryTimes
|
||||
originalAutoGroups := setting.AutoGroups2JsonString()
|
||||
originalUsableGroups := setting.UserUsableGroups2JSONString()
|
||||
originalGroupRatios := ratio_setting.GroupRatio2JSONString()
|
||||
originalMaxTokenAutoGroups := setting.GetMaxTokenAutoGroups()
|
||||
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", strings.ReplaceAll(t.Name(), "/", "_"))
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&model.Channel{}, &model.Ability{}))
|
||||
model.DB = db
|
||||
common.MemoryCacheEnabled = true
|
||||
common.RetryTimes = 0
|
||||
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(`[]`))
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(`{"default":"Default","vip":"VIP"}`))
|
||||
require.NoError(t, ratio_setting.UpdateGroupRatioByJSONString(`{"default":1,"vip":2}`))
|
||||
require.NoError(t, setting.UpdateMaxTokenAutoGroups("2"))
|
||||
|
||||
t.Cleanup(func() {
|
||||
model.DB = originalDB
|
||||
common.MemoryCacheEnabled = originalMemoryCacheEnabled
|
||||
common.RetryTimes = originalRetryTimes
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(originalAutoGroups))
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(originalUsableGroups))
|
||||
require.NoError(t, ratio_setting.UpdateGroupRatioByJSONString(originalGroupRatios))
|
||||
require.NoError(t, setting.UpdateMaxTokenAutoGroups(fmt.Sprintf("%d", originalMaxTokenAutoGroups)))
|
||||
|
||||
if originalMemoryCacheEnabled && originalDB != nil &&
|
||||
originalDB.Migrator().HasTable(&model.Channel{}) && originalDB.Migrator().HasTable(&model.Ability{}) {
|
||||
model.InitChannelCache()
|
||||
}
|
||||
sqlDB, err := db.DB()
|
||||
if err == nil {
|
||||
require.NoError(t, sqlDB.Close())
|
||||
}
|
||||
})
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func createChannelSelectAutoGroupsChannel(t *testing.T, db *gorm.DB, id int, group, modelName string) {
|
||||
t.Helper()
|
||||
priority := int64(0)
|
||||
weight := uint(100)
|
||||
require.NoError(t, db.Create(&model.Channel{
|
||||
Id: id,
|
||||
Type: constant.ChannelTypeOpenAI,
|
||||
Key: fmt.Sprintf("key-%d", id),
|
||||
Status: common.ChannelStatusEnabled,
|
||||
Name: fmt.Sprintf("channel-%d", id),
|
||||
Weight: &weight,
|
||||
Models: modelName,
|
||||
Group: group,
|
||||
Priority: &priority,
|
||||
}).Error)
|
||||
require.NoError(t, db.Create(&model.Ability{
|
||||
Group: group,
|
||||
Model: modelName,
|
||||
ChannelId: id,
|
||||
Enabled: true,
|
||||
Priority: &priority,
|
||||
Weight: weight,
|
||||
}).Error)
|
||||
}
|
||||
|
||||
func TestCacheGetRandomSatisfiedChannelUsesTokenAutoGroupsWhenGlobalAutoIsEmpty(t *testing.T) {
|
||||
db := setupChannelSelectAutoGroupsTest(t)
|
||||
const modelName = "auto-groups-runtime-model"
|
||||
createChannelSelectAutoGroupsChannel(t, db, 2101, "vip", modelName)
|
||||
createChannelSelectAutoGroupsChannel(t, db, 2102, "default", modelName)
|
||||
model.InitChannelCache()
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
common.SetContextKey(ctx, constant.ContextKeyUserGroup, "default")
|
||||
common.SetContextKey(ctx, constant.ContextKeyTokenAutoGroups, []string{"vip", "default"})
|
||||
common.SetContextKey(ctx, constant.ContextKeyTokenCrossGroupRetry, true)
|
||||
|
||||
retry := 0
|
||||
param := &RetryParam{
|
||||
Ctx: ctx,
|
||||
TokenGroup: "auto",
|
||||
ModelName: modelName,
|
||||
RequestPath: "/v1/chat/completions",
|
||||
Retry: &retry,
|
||||
}
|
||||
|
||||
first, selectedGroup, err := CacheGetRandomSatisfiedChannel(param)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, first)
|
||||
assert.Equal(t, 2101, first.Id)
|
||||
assert.Equal(t, "vip", selectedGroup)
|
||||
assert.Equal(t, "vip", common.GetContextKeyString(ctx, constant.ContextKeyAutoGroup))
|
||||
assert.Empty(t, setting.GetAutoGroups(), "the selection must not depend on the global Auto list")
|
||||
|
||||
param.IncreaseRetry()
|
||||
second, selectedGroup, err := CacheGetRandomSatisfiedChannel(param)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, second)
|
||||
assert.Equal(t, 2102, second.Id)
|
||||
assert.Equal(t, "default", selectedGroup)
|
||||
assert.Equal(t, "default", common.GetContextKeyString(ctx, constant.ContextKeyAutoGroup))
|
||||
}
|
||||
+55
-3
@@ -3,9 +3,12 @@ package service
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/QuantumNous/new-api/setting"
|
||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetUserUsableGroups(userGroup string) map[string]string {
|
||||
@@ -42,18 +45,67 @@ func GroupInUserUsableGroups(userGroup, groupName string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsUserSelectableGroup(userGroup, groupName string) bool {
|
||||
if groupName == "" || groupName == "auto" {
|
||||
return false
|
||||
}
|
||||
return GroupInUserUsableGroups(userGroup, groupName) && ratio_setting.ContainsGroupRatio(groupName)
|
||||
}
|
||||
|
||||
// GetUserAutoGroup 根据用户分组获取自动分组设置
|
||||
func GetUserAutoGroup(userGroup string) []string {
|
||||
groups := GetUserUsableGroups(userGroup)
|
||||
autoGroups := make([]string, 0)
|
||||
seen := make(map[string]struct{})
|
||||
for _, group := range setting.GetAutoGroups() {
|
||||
if _, ok := groups[group]; ok {
|
||||
autoGroups = append(autoGroups, group)
|
||||
if !IsUserSelectableGroup(userGroup, group) {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[group]; ok {
|
||||
continue
|
||||
}
|
||||
seen[group] = struct{}{}
|
||||
autoGroups = append(autoGroups, group)
|
||||
}
|
||||
return autoGroups
|
||||
}
|
||||
|
||||
// FilterUserTokenAutoGroups applies current permissions before the current
|
||||
// per-token limit. It intentionally does not fall back to the global Auto list.
|
||||
func FilterUserTokenAutoGroups(userGroup string, groups []string) []string {
|
||||
maxCount := setting.GetMaxTokenAutoGroups()
|
||||
filtered := make([]string, 0, min(len(groups), maxCount))
|
||||
seen := make(map[string]struct{})
|
||||
for _, group := range groups {
|
||||
if !IsUserSelectableGroup(userGroup, group) {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[group]; ok {
|
||||
continue
|
||||
}
|
||||
seen[group] = struct{}{}
|
||||
filtered = append(filtered, group)
|
||||
if len(filtered) == maxCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// GetRequestAutoGroups resolves the ordered Auto groups for the current token.
|
||||
// The absence of the context value means that the token inherits the complete
|
||||
// global Auto list; a present (even empty) value is an explicit token snapshot.
|
||||
func GetRequestAutoGroups(c *gin.Context, userGroup string) []string {
|
||||
value, ok := common.GetContextKey(c, constant.ContextKeyTokenAutoGroups)
|
||||
if !ok {
|
||||
return GetUserAutoGroup(userGroup)
|
||||
}
|
||||
groups, ok := value.([]string)
|
||||
if !ok {
|
||||
return []string{}
|
||||
}
|
||||
return FilterUserTokenAutoGroups(userGroup, groups)
|
||||
}
|
||||
|
||||
// GetGroupsEnabledModels 按 groups 顺序获取各分组启用的模型并去重
|
||||
func GetGroupsEnabledModels(groups []string) []string {
|
||||
seen := make(map[string]struct{})
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/setting"
|
||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func configureRequestAutoGroupsTest(t *testing.T) {
|
||||
t.Helper()
|
||||
originalMax := setting.GetMaxTokenAutoGroups()
|
||||
originalAutoGroups := setting.AutoGroups2JsonString()
|
||||
originalUsableGroups := setting.UserUsableGroups2JSONString()
|
||||
originalRatios := ratio_setting.GroupRatio2JSONString()
|
||||
require.NoError(t, setting.UpdateMaxTokenAutoGroups("2"))
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(`["vip","default","svip"]`))
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(`{"default":"Default","vip":"VIP","svip":"SVIP"}`))
|
||||
require.NoError(t, ratio_setting.UpdateGroupRatioByJSONString(`{"default":1,"vip":1,"svip":1}`))
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, setting.UpdateMaxTokenAutoGroups(fmt.Sprintf("%d", originalMax)))
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(originalAutoGroups))
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(originalUsableGroups))
|
||||
require.NoError(t, ratio_setting.UpdateGroupRatioByJSONString(originalRatios))
|
||||
})
|
||||
}
|
||||
|
||||
func newRequestAutoGroupsContext() *gin.Context {
|
||||
gin.SetMode(gin.TestMode)
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestGetRequestAutoGroupsInheritedListIsNotLimited(t *testing.T) {
|
||||
configureRequestAutoGroupsTest(t)
|
||||
ctx := newRequestAutoGroupsContext()
|
||||
|
||||
groups := GetRequestAutoGroups(ctx, "default")
|
||||
|
||||
assert.Equal(t, []string{"vip", "default", "svip"}, groups)
|
||||
}
|
||||
|
||||
func TestGetRequestAutoGroupsFiltersBeforeApplyingCurrentLimit(t *testing.T) {
|
||||
configureRequestAutoGroupsTest(t)
|
||||
ctx := newRequestAutoGroupsContext()
|
||||
common.SetContextKey(ctx, constant.ContextKeyTokenAutoGroups, []string{"revoked", "vip", "default", "svip"})
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(`[]`))
|
||||
|
||||
groups := GetRequestAutoGroups(ctx, "default")
|
||||
|
||||
assert.Equal(t, []string{"vip", "default"}, groups)
|
||||
require.NoError(t, setting.UpdateMaxTokenAutoGroups("1"))
|
||||
assert.Equal(t, []string{"vip"}, GetRequestAutoGroups(ctx, "default"))
|
||||
}
|
||||
|
||||
func TestGetRequestAutoGroupsDoesNotFallBackAfterPermissionChange(t *testing.T) {
|
||||
configureRequestAutoGroupsTest(t)
|
||||
ctx := newRequestAutoGroupsContext()
|
||||
common.SetContextKey(ctx, constant.ContextKeyTokenAutoGroups, []string{"vip"})
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(`{"default":"Default"}`))
|
||||
|
||||
groups := GetRequestAutoGroups(ctx, "default")
|
||||
|
||||
assert.Empty(t, groups)
|
||||
}
|
||||
Reference in New Issue
Block a user