Files
new-api/model/channel_plugin_credential.go
Calcium-Ion 944464420b feat(plugin): 插件渠道可按渠道开关提供商插件的路由 (#8482)
同一个提供商插件绑到多个类型 61 渠道时,各渠道的路由完全相同,无法只在部分渠道上
开启某条转换路由。#8478 用 enabled_by 指向 channelConfig 里的 boolean 字段,已回退;
这里改为由渠道直接保存路由开关,插件不用为每条路由声明配置字段。

- meta.relay 的路由新增 default_enabled(默认 true),写 false 的路由新渠道默认不提供;
  管理路由(/v1/models、余额)始终开启,不能写 false。
- 渠道的 other_settings.plugin_routes 保存路由开关,键为 incoming_path,
  带 models 时追加 "|" 和逗号连接的模型名;保存时只保留与默认值不同的项,
  不认识的键丢弃。
- 选渠道、Responses WebSocket 判断、转发、提供商适配器绑定路由、渠道测试选端点、
  上游模型列表和定价端点都只看对该渠道开启的路由;同一路径的其他路由照常匹配。
- 插件选项接口返回 relayRoutes;渠道编辑「路由与映射」页签顶部列出插件路由,
  每条一个开关,补齐七种语言词条。
- pluginkit/v1.md、v1.d.ts、v1.schema.json 补充 default_enabled 与 plugin_routes。

Refs #8466
2026-11-27 23:25:50 +08:00

402 lines
13 KiB
Go

package model
import (
"context"
"errors"
"slices"
"sort"
"strings"
"sync"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/pkg/jsplugin"
"github.com/QuantumNous/new-api/relaykit/dto"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// Fixed reasons the host records when it disables a task plugin channel, or
// one of its keys, for a credential only a login can restore. A successful
// login re-enables what was disabled for one of them; a manual disable stays.
const (
ChannelStatusReasonPluginLoginRequired = "Plugin credential login required"
ChannelStatusReasonPluginNeedsLogin = "Plugin credential needs a new login"
)
// ErrChannelKeyChanged reports a channel key that changed while a locked
// update waited, which only a write outside UpdateChannelLocked can do.
var ErrChannelKeyChanged = errors.New("channel key changed during the credential update")
// SQLite serves one node, so process locks stand in for its row locks.
var (
sqliteChannelKeyLocks sync.Map // channel id -> *sync.Mutex
sqlitePluginLoginLock sync.Mutex
)
func sqliteChannelKeyLock(channelID int) *sync.Mutex {
lock, _ := sqliteChannelKeyLocks.LoadOrStore(channelID, &sync.Mutex{})
return lock.(*sync.Mutex)
}
// UpdateChannelLocked lets update change a channel's key while no other node
// or goroutine can: MySQL and PostgreSQL hold the channel row lock across
// update, and SQLite holds a process lock. update must not wait on the
// network, because every request of a multi-key channel waits on this lock.
// Every write of a channel's key goes through here or through
// UpdateTaskPluginChannelLocked, which takes the same locks; Channel.Update
// never writes it. So the key update reads is the stored one, never a copy a
// refresh has replaced since.
// update receives the stored channel and may change its Key, Status,
// OtherInfo and ChannelInfo; the changed ones are written together. The
// stored channel afterwards is returned, a new key is published to this
// node's channel cache, and a new status to the channel's abilities.
func UpdateChannelLocked(channelID int, update func(channel *Channel) error) (*Channel, error) {
var channel Channel
var keyChanged, statusChanged, written bool
// changes runs update on channel and returns the columns it changed.
changes := func() (map[string]any, error) {
key, status, otherInfo := channel.Key, channel.Status, channel.OtherInfo
info, err := common.Marshal(channel.ChannelInfo)
if err != nil {
return nil, err
}
if err := update(&channel); err != nil {
return nil, err
}
channel.Keys = nil
updates := map[string]any{}
if channel.Key != key {
updates["key"], keyChanged = channel.Key, true
}
if channel.Status != status {
updates["status"], statusChanged = channel.Status, true
}
if channel.OtherInfo != otherInfo {
updates["other_info"] = channel.OtherInfo
}
if next, err := common.Marshal(channel.ChannelInfo); err != nil {
return nil, err
} else if string(next) != string(info) {
updates["channel_info"] = channel.ChannelInfo
}
return updates, nil
}
if common.UsingMainDatabase(common.DatabaseTypeSQLite) {
lock := sqliteChannelKeyLock(channelID)
lock.Lock()
defer lock.Unlock()
if err := DB.Where("id = ?", channelID).First(&channel).Error; err != nil {
return nil, err
}
stored := channel.Key
updates, err := changes()
if err != nil {
return nil, err
}
if len(updates) > 0 {
result := DB.Model(&Channel{}).Where("id = ?", channelID).Where(map[string]any{"key": stored}).Updates(updates)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected != 1 {
return nil, ErrChannelKeyChanged
}
written = true
}
} else {
err := DB.Transaction(func(tx *gorm.DB) error {
if err := lockForUpdate(tx).Where("id = ?", channelID).First(&channel).Error; err != nil {
return err
}
updates, err := changes()
if err != nil || len(updates) == 0 {
return err
}
written = true
return tx.Model(&Channel{}).Where("id = ?", channelID).Updates(updates).Error
})
if err != nil {
return nil, err
}
}
if written {
defer BumpSyncTopic(SyncTopicChannels)
}
if keyChanged {
CacheUpdateChannelKey(channelID, channel.Key)
}
if statusChanged {
if err := UpdateAbilityStatus(channelID, channel.Status == common.ChannelStatusEnabled); err != nil {
return nil, err
}
}
return &channel, nil
}
// UpdateTaskPluginChannelLocked runs update on a task plugin channel while
// every other task plugin channel is locked too, so a check across channels,
// such as one account per plugin, cannot interleave with another login's
// write. update may change Key, Status, OtherInfo and ChannelInfo and must not
// wait on the network. The caller reloads the channel cache.
func UpdateTaskPluginChannelLocked(channelID int, update func(channel *Channel, others []*Channel) error) error {
// Key status and the polling cursor share channel_info; see UpdateChannelStatus.
pollingLock := GetChannelPollingLock(channelID)
pollingLock.Lock()
defer pollingLock.Unlock()
if common.UsingMainDatabase(common.DatabaseTypeSQLite) {
sqlitePluginLoginLock.Lock()
defer sqlitePluginLoginLock.Unlock()
keyLock := sqliteChannelKeyLock(channelID)
keyLock.Lock()
defer keyLock.Unlock()
}
var statusChanged bool
var enabled bool
err := DB.Transaction(func(tx *gorm.DB) error {
var channels []*Channel
if err := lockForUpdate(tx).Where("type = ?", constant.ChannelTypeTaskPlugin).Order("id").Find(&channels).Error; err != nil {
return err
}
var target *Channel
others := make([]*Channel, 0, len(channels))
for _, channel := range channels {
if channel.Id == channelID {
target = channel
continue
}
others = append(others, channel)
}
if target == nil {
return gorm.ErrRecordNotFound
}
status := target.Status
if err := update(target, others); err != nil {
return err
}
statusChanged, enabled = target.Status != status, target.Status == common.ChannelStatusEnabled
target.Keys = nil
updates := map[string]any{
"key": target.Key,
"status": target.Status,
"other_info": target.OtherInfo,
"channel_info": target.ChannelInfo,
}
return tx.Model(&Channel{}).Where("id = ?", channelID).Updates(updates).Error
})
if err != nil {
return err
}
defer BumpSyncTopic(SyncTopicChannels)
if statusChanged {
return UpdateAbilityStatus(channelID, enabled)
}
return nil
}
// CacheUpdateChannelKey publishes a new key for a cached channel. The cached
// channel is replaced by a copy, so a request holding the previous one keeps
// reading a consistent key.
func CacheUpdateChannelKey(id int, key string) {
if !common.MemoryCacheEnabled {
return
}
channelSyncLock.Lock()
defer channelSyncLock.Unlock()
cached, ok := channelsIDM[id]
if !ok {
return
}
updated := *cached
updated.Key = key
updated.Keys = nil
updated.Keys = updated.GetKeys()
channelsIDM[id] = &updated
}
// channelRelayConfig returns the relay routes of a task plugin channel's
// plugin that are on for the channel, nil for any other channel. It reads the
// settings without GetSetting or GetOtherSettings, which clear a stored
// setting when it is invalid, so a pricing read never writes the channel.
func channelRelayConfig(ch *Channel) *dto.AdvancedCustomConfig {
var setting dto.ChannelSettings
if ch.Type != constant.ChannelTypeTaskPlugin || ch.Setting == nil || common.UnmarshalJsonStr(*ch.Setting, &setting) != nil {
return nil
}
plugin, ok := jsplugin.DefaultRegistry.Get(setting.TaskPluginKey)
if !ok {
return nil
}
var otherSettings dto.ChannelOtherSettings
if ch.OtherSettings != "" {
_ = common.UnmarshalJsonStr(ch.OtherSettings, &otherSettings)
}
return plugin.Meta.ChannelRelayConfig(nil, otherSettings.PluginRoutes)
}
// UpdateScheduledChannelLocked persists a completed scheduled result. update
// only compares the prepared key and applies validated data to the current row;
// it must not run JavaScript or perform network I/O. The polling/key locks and
// database transaction therefore cover only the short persistence step.
func UpdateScheduledChannelLocked(ctx context.Context, channelID int, update func(channel *Channel) error) (*Channel, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
pollingLock := GetChannelPollingLock(channelID)
pollingLock.Lock()
defer pollingLock.Unlock()
if common.UsingMainDatabase(common.DatabaseTypeSQLite) {
keyLock := sqliteChannelKeyLock(channelID)
keyLock.Lock()
defer keyLock.Unlock()
}
var channel Channel
var keyChanged, statusChanged, infoChanged, otherChanged, written bool
// Failed SQL must not include a credential, including in debug mode.
db := DB.WithContext(ctx).Session(&gorm.Session{Logger: logger.Discard})
err := db.Transaction(func(tx *gorm.DB) error {
if err := lockForUpdate(tx).Where("id = ?", channelID).First(&channel).Error; err != nil {
return err
}
key, status, otherInfo := channel.Key, channel.Status, channel.OtherInfo
info, err := common.Marshal(channel.ChannelInfo)
if err != nil {
return err
}
if err := update(&channel); err != nil {
return err
}
channel.Keys = nil
updates := map[string]any{}
if channel.Key != key {
updates["key"], keyChanged = channel.Key, true
}
if channel.Status != status {
updates["status"], statusChanged = channel.Status, true
}
if channel.OtherInfo != otherInfo {
updates["other_info"], otherChanged = channel.OtherInfo, true
}
if next, err := common.Marshal(channel.ChannelInfo); err != nil {
return err
} else if string(next) != string(info) {
updates["channel_info"], infoChanged = channel.ChannelInfo, true
}
if len(updates) == 0 {
return nil
}
written = true
return tx.Model(&Channel{}).Where("id = ?", channelID).Updates(updates).Error
})
if err != nil {
return nil, err
}
if !written {
return &channel, nil
}
defer BumpSyncTopic(SyncTopicChannels)
if keyChanged {
CacheUpdateChannelKey(channelID, channel.Key)
}
if common.MemoryCacheEnabled && (statusChanged || infoChanged || otherChanged) {
channelSyncLock.Lock()
if cached, ok := channelsIDM[channelID]; ok {
updated := *cached
updated.Key, updated.Keys = channel.Key, nil
updated.ChannelInfo = channel.ChannelInfo
updated.ChannelInfo.MultiKeyPollingIndex = cached.ChannelInfo.MultiKeyPollingIndex
updated.OtherInfo = channel.OtherInfo
updated.Keys = updated.GetKeys()
channelsIDM[channelID] = &updated
}
channelSyncLock.Unlock()
CacheUpdateChannelStatus(channelID, channel.Status)
if channel.Status == common.ChannelStatusEnabled {
// The existing cache status path removes disabled channels. Restore
// this scheduled result to selection immediately when it enables one.
channelSyncLock.Lock()
if cached, ok := channelsIDM[channelID]; ok {
if group2model2channels == nil {
group2model2channels = make(map[string]map[string][]int)
}
for group := range strings.SplitSeq(cached.Group, ",") {
if group2model2channels[group] == nil {
group2model2channels[group] = make(map[string][]int)
}
for _, model := range cached.GetModels() {
ids := group2model2channels[group][model]
if slices.Contains(ids, channelID) {
continue
}
ids = append(ids, channelID)
sort.Slice(ids, func(i, j int) bool {
return channelsIDM[ids[i]].GetPriority() > channelsIDM[ids[j]].GetPriority()
})
group2model2channels[group][model] = ids
}
}
}
channelSyncLock.Unlock()
}
}
if statusChanged {
if err := UpdateAbilityStatus(channelID, channel.Status == common.ChannelStatusEnabled); err != nil {
return nil, err
}
}
return &channel, nil
}
// SetScheduledChannelKeyStatus applies an automatic status transition to the
// stored key. Call it only inside UpdateScheduledChannelLocked, after locating
// the key again; a manual disable can never be undone by a plugin.
func SetScheduledChannelKeyStatus(channel *Channel, keyIndex, status int, reason string) bool {
if channel.Status != common.ChannelStatusEnabled && channel.Status != common.ChannelStatusAutoDisabled {
return false
}
channel.Keys = nil
keys := []string{channel.Key}
if channel.ChannelInfo.IsMultiKey {
keys = channel.GetKeys()
}
if keyIndex < 0 || keyIndex >= len(keys) {
return false
}
current := channel.Status
if channel.ChannelInfo.IsMultiKey {
current = common.ChannelStatusEnabled
if stored, ok := channel.ChannelInfo.MultiKeyStatusList[keyIndex]; ok {
current = stored
}
} else if keyIndex != 0 {
return false
}
switch status {
case common.ChannelStatusEnabled:
if current != common.ChannelStatusAutoDisabled {
return false
}
case common.ChannelStatusAutoDisabled:
if current != common.ChannelStatusEnabled || !channel.GetAutoBan() {
return false
}
default:
return false
}
if channel.ChannelInfo.IsMultiKey {
handlerMultiKeyUpdate(channel, keys[keyIndex], status, reason)
if status == common.ChannelStatusEnabled {
delete(channel.ChannelInfo.MultiKeyDisabledReason, keyIndex)
delete(channel.ChannelInfo.MultiKeyDisabledTime, keyIndex)
}
} else {
channel.Status = status
info := channel.GetOtherInfo()
info["status_reason"] = reason
info["status_time"] = common.GetTimestamp()
channel.SetOtherInfo(info)
}
return true
}