mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-11 22:49:57 +00:00
feat(task): replace built-in task adaptors with a sandboxed JS plugin system (#7076)
This commit is contained in:
+14
-36
@@ -12,7 +12,8 @@ import (
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/logger"
|
||||
"github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
kitdto "github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||
)
|
||||
|
||||
@@ -20,7 +21,7 @@ var group2model2channels map[string]map[string][]int // enabled channel
|
||||
var channelsIDM map[int]*Channel // all channels include disabled
|
||||
// channel2advancedCustomConfig caches parsed Advanced Custom (type 58) configs so
|
||||
// path-aware selection avoids re-parsing JSON per request. Refreshed on full sync.
|
||||
var channel2advancedCustomConfig map[int]*dto.AdvancedCustomConfig
|
||||
var channel2advancedCustomConfig map[int]*kitdto.AdvancedCustomConfig
|
||||
var channelSyncLock sync.RWMutex
|
||||
|
||||
func InitChannelCache() {
|
||||
@@ -29,7 +30,7 @@ func InitChannelCache() {
|
||||
return
|
||||
}
|
||||
newChannelId2channel := make(map[int]*Channel)
|
||||
newChannel2advancedCustomConfig := make(map[int]*dto.AdvancedCustomConfig)
|
||||
newChannel2advancedCustomConfig := make(map[int]*kitdto.AdvancedCustomConfig)
|
||||
var channels []*Channel
|
||||
DB.Find(&channels)
|
||||
for _, channel := range channels {
|
||||
@@ -111,22 +112,27 @@ func SyncChannelCache(frequency int) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetRandomSatisfiedChannel(group string, model string, retry int, requestPath string) (*Channel, error) {
|
||||
func GetRandomSatisfiedChannel(
|
||||
group string,
|
||||
model string,
|
||||
retry int,
|
||||
filters []dto.ChannelFilter,
|
||||
) (*Channel, error) {
|
||||
// if memory cache is disabled, get channel directly from database
|
||||
if !common.MemoryCacheEnabled {
|
||||
return GetChannel(group, model, retry, requestPath)
|
||||
return GetChannel(group, model, retry, filters)
|
||||
}
|
||||
|
||||
channelSyncLock.RLock()
|
||||
defer channelSyncLock.RUnlock()
|
||||
|
||||
// First, try to find channels with the exact model name.
|
||||
channels := filterChannelsByRequestPathAndModel(group2model2channels[group][model], requestPath, model)
|
||||
channels, _ := filterCandidateIDs(group2model2channels[group][model], model, filters)
|
||||
|
||||
// If no channels found, try to find channels with the normalized model name.
|
||||
if len(channels) == 0 {
|
||||
normalizedModel := ratio_setting.FormatMatchingModelName(model)
|
||||
channels = filterChannelsByRequestPathAndModel(group2model2channels[group][normalizedModel], requestPath, model)
|
||||
channels, _ = filterCandidateIDs(group2model2channels[group][normalizedModel], model, filters)
|
||||
}
|
||||
|
||||
if len(channels) == 0 {
|
||||
@@ -208,34 +214,6 @@ func GetRandomSatisfiedChannel(group string, model string, retry int, requestPat
|
||||
return nil, errors.New("channel not found")
|
||||
}
|
||||
|
||||
// filterChannelsByRequestPathAndModel restricts candidates by request path and
|
||||
// model. Only Advanced Custom (type 58) channels are path-checked: they are kept
|
||||
// only when one of their configured routes matches requestPath and model. All
|
||||
// other channel types always pass. When requestPath is empty, filtering is skipped.
|
||||
// Caller must hold channelSyncLock (read lock). The cached slice is never mutated.
|
||||
func filterChannelsByRequestPathAndModel(channels []int, requestPath string, model string) []int {
|
||||
if requestPath == "" || len(channels) == 0 {
|
||||
return channels
|
||||
}
|
||||
filtered := make([]int, 0, len(channels))
|
||||
for _, channelId := range channels {
|
||||
channel, ok := channelsIDM[channelId]
|
||||
if !ok {
|
||||
// keep it so the downstream consistency error is raised as before
|
||||
filtered = append(filtered, channelId)
|
||||
continue
|
||||
}
|
||||
if channel.Type != constant.ChannelTypeAdvancedCustom {
|
||||
filtered = append(filtered, channelId)
|
||||
continue
|
||||
}
|
||||
if config := channel2advancedCustomConfig[channelId]; config != nil && config.SupportsPathForModel(requestPath, model) {
|
||||
filtered = append(filtered, channelId)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func CacheGetChannel(id int) (*Channel, error) {
|
||||
if !common.MemoryCacheEnabled {
|
||||
return GetChannelById(id, true)
|
||||
@@ -311,7 +289,7 @@ func CacheUpdateChannel(channel *Channel) {
|
||||
}
|
||||
channelsIDM[channel.Id] = channel
|
||||
if channel2advancedCustomConfig == nil {
|
||||
channel2advancedCustomConfig = make(map[int]*dto.AdvancedCustomConfig)
|
||||
channel2advancedCustomConfig = make(map[int]*kitdto.AdvancedCustomConfig)
|
||||
}
|
||||
delete(channel2advancedCustomConfig, channel.Id)
|
||||
if channel.Type == constant.ChannelTypeAdvancedCustom {
|
||||
|
||||
Reference in New Issue
Block a user