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:
Calcium-Ion
2026-08-01 23:19:01 +08:00
committed by GitHub
parent bd585d78ef
commit 0ab0202060
57 changed files with 3922 additions and 210 deletions
+6
View File
@@ -120,6 +120,7 @@ func InitOptionMap() {
common.OptionMap["Chats"] = setting.Chats2JsonString()
common.OptionMap["AutoGroups"] = setting.AutoGroups2JsonString()
common.OptionMap["DefaultUseAutoGroup"] = strconv.FormatBool(setting.DefaultUseAutoGroup)
common.OptionMap["MaxTokenAutoGroups"] = strconv.Itoa(setting.GetMaxTokenAutoGroups())
common.OptionMap["PayMethods"] = operation_setting.PayMethods2JsonString()
common.OptionMap["GitHubClientId"] = ""
common.OptionMap["GitHubClientSecret"] = ""
@@ -208,6 +209,9 @@ func validateOptionValue(key string, value string) error {
if key == operation_setting.ToolPriceOptionKey {
return operation_setting.ValidateToolPricesJSON(value)
}
if key == "MaxTokenAutoGroups" {
return setting.ValidateMaxTokenAutoGroups(value)
}
return nil
}
@@ -413,6 +417,8 @@ func updateOptionMap(key string, value string) (err error) {
err = setting.UpdateChatsByJsonString(value)
case "AutoGroups":
err = setting.UpdateAutoGroupsByJsonString(value)
case "MaxTokenAutoGroups":
err = setting.UpdateMaxTokenAutoGroups(value)
case "CustomCallbackAddress":
operation_setting.CustomCallbackAddress = value
case "EpayId":
+17
View File
@@ -0,0 +1,17 @@
package model
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidateOptionValueRejectsInvalidMaxTokenAutoGroups(t *testing.T) {
for _, value := range []string{"", "0", "-1", "1.5", "invalid"} {
t.Run(value, func(t *testing.T) {
assert.Error(t, validateOptionValue("MaxTokenAutoGroups", value))
})
}
require.NoError(t, validateOptionValue("MaxTokenAutoGroups", "999999"))
}
+34 -11
View File
@@ -28,9 +28,34 @@ type Token struct {
UsedQuota int `json:"used_quota" gorm:"default:0"` // used quota
Group string `json:"group" gorm:"default:''"`
CrossGroupRetry bool `json:"cross_group_retry"` // 跨分组重试,仅auto分组有效
AutoGroups string `json:"-" gorm:"type:text"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (token *Token) GetAutoGroups() ([]string, error) {
if token.AutoGroups == "" {
return nil, nil
}
var groups []string
if err := common.UnmarshalJsonStr(token.AutoGroups, &groups); err != nil {
return nil, err
}
return groups, nil
}
func (token *Token) SetAutoGroups(groups []string) error {
if len(groups) == 0 {
token.AutoGroups = ""
return nil
}
data, err := common.Marshal(groups)
if err != nil {
return err
}
token.AutoGroups = string(data)
return nil
}
func (token *Token) Clean() {
token.Key = ""
}
@@ -291,18 +316,16 @@ func (token *Token) Insert() error {
// Update Make sure your token's fields is completed, because this will update non-zero values
func (token *Token) Update() (err error) {
defer func() {
if shouldUpdateRedis(true, err) {
gopool.Go(func() {
err := cacheSetToken(*token)
if err != nil {
common.SysLog("failed to update token cache: " + err.Error())
}
})
}
}()
err = DB.Model(token).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota",
"model_limits_enabled", "model_limits", "allow_ips", "group", "cross_group_retry").Updates(token).Error
"model_limits_enabled", "model_limits", "allow_ips", "group", "cross_group_retry", "auto_groups").Updates(token).Error
if shouldUpdateRedis(true, err) {
if cacheErr := cacheSetToken(*token); cacheErr != nil {
common.SysLog("failed to update token cache: " + cacheErr.Error())
if deleteErr := cacheDeleteToken(token.Key); deleteErr != nil {
common.SysLog("failed to invalidate token cache after update: " + deleteErr.Error())
}
}
}
return err
}
+57
View File
@@ -0,0 +1,57 @@
package model
import (
"testing"
"github.com/QuantumNous/new-api/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTokenAutoGroupsRoundTripThroughRedisHashCache(t *testing.T) {
useUserCacheMiniRedis(t)
token := Token{
Id: 42,
UserId: 7,
Key: "token-auto-groups-cache-key",
Name: "auto-cache",
Group: "auto",
AutoGroups: `["vip","default"]`,
}
require.NoError(t, cacheSetToken(token))
cached, err := cacheGetTokenByKey(token.Key)
require.NoError(t, err)
assert.Equal(t, token.AutoGroups, cached.AutoGroups)
groups, err := cached.GetAutoGroups()
require.NoError(t, err)
assert.Equal(t, []string{"vip", "default"}, groups)
}
func TestTokenUpdateSynchronouslyNarrowsPreheatedAutoGroupsCache(t *testing.T) {
truncateTables(t)
useUserCacheMiniRedis(t)
token := Token{
UserId: 7,
Key: "token-auto-groups-update-cache-key",
Name: "auto-cache-update",
Status: common.TokenStatusEnabled,
ExpiredTime: -1,
UnlimitedQuota: true,
Group: "auto",
CrossGroupRetry: true,
AutoGroups: `["default","vip"]`,
}
require.NoError(t, token.Insert())
require.NoError(t, cacheSetToken(token))
preheated, err := cacheGetTokenByKey(token.Key)
require.NoError(t, err)
assert.JSONEq(t, `["default","vip"]`, preheated.AutoGroups)
require.NoError(t, token.SetAutoGroups([]string{"vip"}))
require.NoError(t, token.Update())
immediate, err := cacheGetTokenByKey(token.Key)
require.NoError(t, err)
assert.JSONEq(t, `["vip"]`, immediate.AutoGroups)
}