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:
@@ -502,6 +502,16 @@ func SetupContextForToken(c *gin.Context, token *model.Token, parts ...string) e
|
||||
}
|
||||
common.SetContextKey(c, constant.ContextKeyTokenGroup, token.Group)
|
||||
common.SetContextKey(c, constant.ContextKeyTokenCrossGroupRetry, token.CrossGroupRetry)
|
||||
if token.AutoGroups != "" {
|
||||
autoGroups, err := token.GetAutoGroups()
|
||||
if err != nil {
|
||||
common.SysError(fmt.Sprintf("failed to parse auto groups for token %d: %v", token.Id, err))
|
||||
autoGroups = []string{}
|
||||
common.SetContextKey(c, constant.ContextKeyTokenAutoGroups, autoGroups)
|
||||
} else if len(autoGroups) > 0 {
|
||||
common.SetContextKey(c, constant.ContextKeyTokenAutoGroups, autoGroups)
|
||||
}
|
||||
}
|
||||
if len(parts) > 1 {
|
||||
if model.IsAdmin(token.UserId) {
|
||||
c.Set("specific_channel_id", parts[1])
|
||||
|
||||
@@ -109,7 +109,7 @@ func Distribute() func(c *gin.Context) {
|
||||
channelSupportsRequestPath(preferred, c.Request.URL.Path, modelRequest.Model) {
|
||||
if usingGroup == "auto" {
|
||||
userGroup := common.GetContextKeyString(c, constant.ContextKeyUserGroup)
|
||||
autoGroups := service.GetUserAutoGroup(userGroup)
|
||||
autoGroups := service.GetRequestAutoGroups(c, userGroup)
|
||||
for _, g := range autoGroups {
|
||||
if model.IsChannelEnabledForGroupModel(g, modelRequest.Model, preferred.Id) {
|
||||
selectGroup = g
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newTokenAutoGroupsContext() *gin.Context {
|
||||
gin.SetMode(gin.TestMode)
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestSetupContextForTokenPreservesCustomAutoGroupsOrder(t *testing.T) {
|
||||
ctx := newTokenAutoGroupsContext()
|
||||
token := &model.Token{Id: 1, UserId: 2, AutoGroups: `["vip","default"]`}
|
||||
|
||||
require.NoError(t, SetupContextForToken(ctx, token))
|
||||
value, ok := common.GetContextKey(ctx, constant.ContextKeyTokenAutoGroups)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, []string{"vip", "default"}, value)
|
||||
}
|
||||
|
||||
func TestSetupContextForTokenTreatsStoredEmptyArrayAsInheritance(t *testing.T) {
|
||||
ctx := newTokenAutoGroupsContext()
|
||||
token := &model.Token{Id: 1, UserId: 2, AutoGroups: `[]`}
|
||||
|
||||
require.NoError(t, SetupContextForToken(ctx, token))
|
||||
_, ok := common.GetContextKey(ctx, constant.ContextKeyTokenAutoGroups)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestSetupContextForTokenMalformedAutoGroupsFailsClosed(t *testing.T) {
|
||||
ctx := newTokenAutoGroupsContext()
|
||||
token := &model.Token{Id: 1, UserId: 2, AutoGroups: `not-json`}
|
||||
|
||||
require.NoError(t, SetupContextForToken(ctx, token))
|
||||
value, ok := common.GetContextKey(ctx, constant.ContextKeyTokenAutoGroups)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, []string{}, value)
|
||||
}
|
||||
Reference in New Issue
Block a user