Files
new-api/model/channel_constraint.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

165 lines
5.6 KiB
Go

package model
import (
"slices"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/pkg/jsplugin"
)
var filterEvalOrder = []dto.ChannelFilterKind{
dto.FilterRequestPath,
dto.FilterTaskPluginEndpoint,
dto.FilterTaskPluginIdentity,
dto.FilterResponsesWebSocket,
}
// ChannelSatisfiesFilters reports whether ch passes every filter.
// On false, it returns the first violated filter in filterEvalOrder for error
// attribution.
func ChannelSatisfiesFilters(ch *Channel, modelName string, filters []dto.ChannelFilter) (bool, dto.ChannelFilterKind) {
if ch == nil {
return false, ""
}
for _, kind := range filterEvalOrder {
for _, filter := range filters {
if filter.Kind != kind {
continue
}
if !channelMatchesFilter(ch, modelName, filter) {
return false, kind
}
}
}
return true, ""
}
// filterCandidateIDs applies filters to a cached candidate id list.
// Caller must hold channelSyncLock (read lock). The input slice is never mutated.
// A missing id in channelsIDM is kept for request_path (downstream consistency
// error) and dropped for the other filters.
func filterCandidateIDs(ids []int, modelName string, filters []dto.ChannelFilter) (kept []int, emptiedBy dto.ChannelFilterKind) {
if len(ids) == 0 {
return ids, ""
}
kept = ids
for _, kind := range filterEvalOrder {
kindFilters := filtersByKind(filters, kind)
if len(kindFilters) == 0 {
continue
}
next := make([]int, 0, len(kept))
for _, id := range kept {
channel, exists := channelsIDM[id]
if candidatePassesKindFilters(channel, exists, modelName, kind, kindFilters) {
next = append(next, id)
}
}
if len(kept) > 0 && len(next) == 0 {
return next, kind
}
kept = next
}
return kept, ""
}
func filtersByKind(filters []dto.ChannelFilter, kind dto.ChannelFilterKind) []dto.ChannelFilter {
var matched []dto.ChannelFilter
for _, filter := range filters {
if filter.Kind == kind {
matched = append(matched, filter)
}
}
return matched
}
func candidatePassesKindFilters(ch *Channel, exists bool, modelName string, kind dto.ChannelFilterKind, filters []dto.ChannelFilter) bool {
if kind == dto.FilterRequestPath && !exists {
return true
}
if !exists || ch == nil {
return false
}
for _, filter := range filters {
if !channelMatchesFilter(ch, modelName, filter) {
return false
}
}
return true
}
func channelMatchesFilter(ch *Channel, modelName string, filter dto.ChannelFilter) bool {
switch filter.Kind {
case dto.FilterRequestPath:
if filter.RequestPath == "" {
return true
}
if !constant.IsAdvancedCustomChannel(ch.Type) {
return true
}
config := ch.GetOtherSettings().AdvancedCustom
return config != nil && config.SupportsPathForModel(filter.RequestPath, modelName)
case dto.FilterTaskPluginIdentity:
if filter.TaskPluginKey == "" {
return ch.Type != constant.ChannelTypeTaskPlugin || providerChannelServes(ch, filter.RequestPath, modelName)
}
if ch.Type == constant.ChannelTypeTaskPlugin || ch.Type == constant.ChannelTypeNewAPI {
// A New API channel serves every plugin it is extended with; the
// pinned plugin or any shared-model candidate may execute there.
setting := ch.GetSetting()
return setting.BindsTaskPlugin(filter.TaskPluginKey) || slices.ContainsFunc(filter.TaskPluginKeys, setting.BindsTaskPlugin)
}
return slices.Contains(filter.TaskPluginChannelTypes, ch.Type)
case dto.FilterTaskPluginEndpoint:
pluginChannel := slices.Contains(filter.TaskPluginChannelTypes, ch.Type)
if ch.Type == constant.ChannelTypeTaskPlugin || ch.Type == constant.ChannelTypeNewAPI {
pluginChannel = slices.ContainsFunc(filter.TaskPluginKeys, ch.GetSetting().BindsTaskPlugin)
}
relayChannel := ch.Type != constant.ChannelTypeTaskPlugin || providerChannelServes(ch, filter.RequestPath, modelName)
if filter.TaskPluginRelayOnly {
return relayChannel && !pluginChannel
}
// Ordinary channels and matching plugin channels compete using the same
// group, priority, weight, affinity, and pin rules. Unrelated type-61
// plugins cannot execute this endpoint unless their relay routes do.
return relayChannel || pluginChannel
case dto.FilterResponsesWebSocket:
if !ch.GetSetting().ResponsesWebSocketEnabled {
return false
}
switch ch.Type {
case constant.ChannelTypeOpenAI, constant.ChannelTypeAzure, constant.ChannelTypeCodex, constant.ChannelTypeSub2API, constant.ChannelTypeNewAPI:
return true
case constant.ChannelTypeAdvancedCustom:
// The session forwards native Responses events without protocol
// conversion, so only a converter-free /v1/responses route qualifies.
route, ok := ch.GetOtherSettings().AdvancedCustom.MatchPathForModel("/v1/responses", modelName)
return ok && route.IsNative()
case constant.ChannelTypeTaskPlugin:
// The same rule on the relay routes of the channel's plugin.
plugin, ok := jsplugin.DefaultRegistry.Get(ch.GetSetting().TaskPluginKey)
return ok && plugin.Meta.ChannelServesResponsesWebSocket(ch.GetOtherSettings().PluginRoutes, modelName)
default:
return false
}
default:
return true
}
}
// providerChannelServes reports a task plugin channel whose plugin relays
// requestPath for modelName through its relay routes, as an Advanced Custom
// channel with those routes would.
func providerChannelServes(ch *Channel, requestPath, modelName string) bool {
if ch.Type != constant.ChannelTypeTaskPlugin || requestPath == "" {
return false
}
plugin, ok := jsplugin.DefaultRegistry.Get(ch.GetSetting().TaskPluginKey)
if !ok || !plugin.Meta.HasProviderPart() {
return false
}
_, ok = plugin.Meta.ChannelRelayRoute(ch.GetOtherSettings().PluginRoutes, requestPath, modelName)
return ok
}