feat(advanced-custom): remove fallback option and enhance path matching for advanced custom routes
This commit is contained in:
+50
-1
@@ -7,6 +7,8 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"gorm.io/gorm"
|
||||
@@ -103,7 +105,7 @@ func getChannelQuery(group string, model string, retry int) (*gorm.DB, error) {
|
||||
return channelQuery, nil
|
||||
}
|
||||
|
||||
func GetChannel(group string, model string, retry int) (*Channel, error) {
|
||||
func GetChannel(group string, model string, retry int, requestPath string) (*Channel, error) {
|
||||
var abilities []Ability
|
||||
|
||||
var err error = nil
|
||||
@@ -119,6 +121,7 @@ func GetChannel(group string, model string, retry int) (*Channel, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
abilities = filterAbilitiesByRequestPath(abilities, requestPath)
|
||||
channel := Channel{}
|
||||
if len(abilities) > 0 {
|
||||
// Randomly choose one
|
||||
@@ -143,6 +146,52 @@ func GetChannel(group string, model string, retry int) (*Channel, error) {
|
||||
return &channel, err
|
||||
}
|
||||
|
||||
// filterAbilitiesByRequestPath restricts candidates by request path for the DB
|
||||
// (non-memory-cache) selection path. Only Advanced Custom (type 58) channels are
|
||||
// path-checked: kept only when one of their routes matches requestPath; all other
|
||||
// channel types always pass. When requestPath is empty, filtering is skipped.
|
||||
func filterAbilitiesByRequestPath(abilities []Ability, requestPath string) []Ability {
|
||||
if requestPath == "" || len(abilities) == 0 {
|
||||
return abilities
|
||||
}
|
||||
|
||||
channelIds := make([]int, 0, len(abilities))
|
||||
seen := make(map[int]struct{}, len(abilities))
|
||||
for _, ability := range abilities {
|
||||
if _, ok := seen[ability.ChannelId]; ok {
|
||||
continue
|
||||
}
|
||||
seen[ability.ChannelId] = struct{}{}
|
||||
channelIds = append(channelIds, ability.ChannelId)
|
||||
}
|
||||
|
||||
var channels []*Channel
|
||||
if err := DB.Where("id IN ?", channelIds).Find(&channels).Error; err != nil {
|
||||
// On error, fall back to unfiltered candidates to avoid blocking selection
|
||||
return abilities
|
||||
}
|
||||
|
||||
advancedConfigs := make(map[int]*dto.AdvancedCustomConfig)
|
||||
for _, channel := range channels {
|
||||
if channel.Type == constant.ChannelTypeAdvancedCustom {
|
||||
advancedConfigs[channel.Id] = channel.GetOtherSettings().AdvancedCustom
|
||||
}
|
||||
}
|
||||
|
||||
filtered := make([]Ability, 0, len(abilities))
|
||||
for _, ability := range abilities {
|
||||
config, isAdvancedCustom := advancedConfigs[ability.ChannelId]
|
||||
if !isAdvancedCustom {
|
||||
filtered = append(filtered, ability)
|
||||
continue
|
||||
}
|
||||
if config != nil && config.SupportsPath(requestPath) {
|
||||
filtered = append(filtered, ability)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func (channel *Channel) AddAbilities(tx *gorm.DB) error {
|
||||
models_ := strings.Split(channel.Models, ",")
|
||||
groups_ := strings.Split(channel.Group, ",")
|
||||
|
||||
Reference in New Issue
Block a user