Commit 70821e20 by Seefs Committed by GitHub

feat: auto fetch upstream models (#2979)

* feat: add upstream model update detection with scheduled sync and manual apply flows

* feat: support upstream model removal sync and selectable deletes in update modal

* feat: add detect-only upstream updates and show compact +/- model badges

* feat: improve upstream model update UX

* feat: improve upstream model update UX

* fix: respect model_mapping in upstream update detection

* feat: improve upstream update modal to prevent missed add/remove actions

* feat: add admin upstream model update notifications with digest and truncation

* fix: avoid repeated partial-submit confirmation in upstream update modal

* feat: improve ui/ux

* feat: suppress upstream update alerts for unchanged channel-count within 24h

* fix: submit upstream update choices even when no models are selected

* feat: improve upstream model update flow and split frontend updater

* fix merge conflict
parent 151264df
...@@ -209,158 +209,15 @@ func FetchUpstreamModels(c *gin.Context) { ...@@ -209,158 +209,15 @@ func FetchUpstreamModels(c *gin.Context) {
return return
} }
baseURL := constant.ChannelBaseURLs[channel.Type] ids, err := fetchChannelUpstreamModelIDs(channel)
if channel.GetBaseURL() != "" {
baseURL = channel.GetBaseURL()
}
// 对于 Ollama 渠道,使用特殊处理
if channel.Type == constant.ChannelTypeOllama {
key := strings.Split(channel.Key, "\n")[0]
models, err := ollama.FetchOllamaModels(baseURL, key)
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
"message": fmt.Sprintf("获取Ollama模型失败: %s", err.Error()), "message": fmt.Sprintf("获取模型列表失败: %s", err.Error()),
}) })
return return
} }
result := OpenAIModelsResponse{
Data: make([]OpenAIModel, 0, len(models)),
}
for _, modelInfo := range models {
metadata := map[string]any{}
if modelInfo.Size > 0 {
metadata["size"] = modelInfo.Size
}
if modelInfo.Digest != "" {
metadata["digest"] = modelInfo.Digest
}
if modelInfo.ModifiedAt != "" {
metadata["modified_at"] = modelInfo.ModifiedAt
}
details := modelInfo.Details
if details.ParentModel != "" || details.Format != "" || details.Family != "" || len(details.Families) > 0 || details.ParameterSize != "" || details.QuantizationLevel != "" {
metadata["details"] = modelInfo.Details
}
if len(metadata) == 0 {
metadata = nil
}
result.Data = append(result.Data, OpenAIModel{
ID: modelInfo.Name,
Object: "model",
Created: 0,
OwnedBy: "ollama",
Metadata: metadata,
})
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": result.Data,
})
return
}
// 对于 Gemini 渠道,使用特殊处理
if channel.Type == constant.ChannelTypeGemini {
// 获取用于请求的可用密钥(多密钥渠道优先使用启用状态的密钥)
key, _, apiErr := channel.GetNextEnabledKey()
if apiErr != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": fmt.Sprintf("获取渠道密钥失败: %s", apiErr.Error()),
})
return
}
key = strings.TrimSpace(key)
models, err := gemini.FetchGeminiModels(baseURL, key, channel.GetSetting().Proxy)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": fmt.Sprintf("获取Gemini模型失败: %s", err.Error()),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": models,
})
return
}
var url string
switch channel.Type {
case constant.ChannelTypeAli:
url = fmt.Sprintf("%s/compatible-mode/v1/models", baseURL)
case constant.ChannelTypeZhipu_v4:
if plan, ok := constant.ChannelSpecialBases[baseURL]; ok && plan.OpenAIBaseURL != "" {
url = fmt.Sprintf("%s/models", plan.OpenAIBaseURL)
} else {
url = fmt.Sprintf("%s/api/paas/v4/models", baseURL)
}
case constant.ChannelTypeVolcEngine:
if plan, ok := constant.ChannelSpecialBases[baseURL]; ok && plan.OpenAIBaseURL != "" {
url = fmt.Sprintf("%s/v1/models", plan.OpenAIBaseURL)
} else {
url = fmt.Sprintf("%s/v1/models", baseURL)
}
case constant.ChannelTypeMoonshot:
if plan, ok := constant.ChannelSpecialBases[baseURL]; ok && plan.OpenAIBaseURL != "" {
url = fmt.Sprintf("%s/models", plan.OpenAIBaseURL)
} else {
url = fmt.Sprintf("%s/v1/models", baseURL)
}
default:
url = fmt.Sprintf("%s/v1/models", baseURL)
}
// 获取用于请求的可用密钥(多密钥渠道优先使用启用状态的密钥)
key, _, apiErr := channel.GetNextEnabledKey()
if apiErr != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": fmt.Sprintf("获取渠道密钥失败: %s", apiErr.Error()),
})
return
}
key = strings.TrimSpace(key)
headers, err := buildFetchModelsHeaders(channel, key)
if err != nil {
common.ApiError(c, err)
return
}
body, err := GetResponseBody("GET", url, channel, headers)
if err != nil {
common.ApiError(c, err)
return
}
var result OpenAIModelsResponse
if err = json.Unmarshal(body, &result); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": fmt.Sprintf("解析响应失败: %s", err.Error()),
})
return
}
var ids []string
for _, model := range result.Data {
id := model.ID
if channel.Type == constant.ChannelTypeGemini {
id = strings.TrimPrefix(id, "models/")
}
ids = append(ids, id)
}
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": true, "success": true,
"message": "", "message": "",
......
package controller
import (
"testing"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/model"
"github.com/stretchr/testify/require"
)
func TestNormalizeModelNames(t *testing.T) {
result := normalizeModelNames([]string{
" gpt-4o ",
"",
"gpt-4o",
"gpt-4.1",
" ",
})
require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, result)
}
func TestMergeModelNames(t *testing.T) {
result := mergeModelNames(
[]string{"gpt-4o", "gpt-4.1"},
[]string{"gpt-4.1", " gpt-4.1-mini ", "gpt-4o"},
)
require.Equal(t, []string{"gpt-4o", "gpt-4.1", "gpt-4.1-mini"}, result)
}
func TestSubtractModelNames(t *testing.T) {
result := subtractModelNames(
[]string{"gpt-4o", "gpt-4.1", "gpt-4.1-mini"},
[]string{"gpt-4.1", "not-exists"},
)
require.Equal(t, []string{"gpt-4o", "gpt-4.1-mini"}, result)
}
func TestIntersectModelNames(t *testing.T) {
result := intersectModelNames(
[]string{"gpt-4o", "gpt-4.1", "gpt-4.1", "not-exists"},
[]string{"gpt-4.1", "gpt-4o-mini", "gpt-4o"},
)
require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, result)
}
func TestApplySelectedModelChanges(t *testing.T) {
t.Run("add and remove together", func(t *testing.T) {
result := applySelectedModelChanges(
[]string{"gpt-4o", "gpt-4.1", "claude-3"},
[]string{"gpt-4.1-mini"},
[]string{"claude-3"},
)
require.Equal(t, []string{"gpt-4o", "gpt-4.1", "gpt-4.1-mini"}, result)
})
t.Run("add wins when conflict with remove", func(t *testing.T) {
result := applySelectedModelChanges(
[]string{"gpt-4o"},
[]string{"gpt-4.1"},
[]string{"gpt-4.1"},
)
require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, result)
})
}
func TestCollectPendingApplyUpstreamModelChanges(t *testing.T) {
settings := dto.ChannelOtherSettings{
UpstreamModelUpdateLastDetectedModels: []string{" gpt-4o ", "gpt-4o", "gpt-4.1"},
UpstreamModelUpdateLastRemovedModels: []string{" old-model ", "", "old-model"},
}
pendingAddModels, pendingRemoveModels := collectPendingApplyUpstreamModelChanges(settings)
require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, pendingAddModels)
require.Equal(t, []string{"old-model"}, pendingRemoveModels)
}
func TestNormalizeChannelModelMapping(t *testing.T) {
modelMapping := `{
" alias-model ": " upstream-model ",
"": "invalid",
"invalid-target": ""
}`
channel := &model.Channel{
ModelMapping: &modelMapping,
}
result := normalizeChannelModelMapping(channel)
require.Equal(t, map[string]string{
"alias-model": "upstream-model",
}, result)
}
func TestCollectPendingUpstreamModelChangesFromModels_WithModelMapping(t *testing.T) {
pendingAddModels, pendingRemoveModels := collectPendingUpstreamModelChangesFromModels(
[]string{"alias-model", "gpt-4o", "stale-model"},
[]string{"gpt-4o", "gpt-4.1", "mapped-target"},
[]string{"gpt-4.1"},
map[string]string{
"alias-model": "mapped-target",
},
)
require.Equal(t, []string{}, pendingAddModels)
require.Equal(t, []string{"stale-model"}, pendingRemoveModels)
}
func TestBuildUpstreamModelUpdateTaskNotificationContent_OmitOverflowDetails(t *testing.T) {
channelSummaries := make([]upstreamModelUpdateChannelSummary, 0, 12)
for i := 0; i < 12; i++ {
channelSummaries = append(channelSummaries, upstreamModelUpdateChannelSummary{
ChannelName: "channel-" + string(rune('A'+i)),
AddCount: i + 1,
RemoveCount: i,
})
}
content := buildUpstreamModelUpdateTaskNotificationContent(
24,
12,
56,
21,
9,
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
channelSummaries,
[]string{
"gpt-4.1", "gpt-4.1-mini", "o3", "o4-mini", "gemini-2.5-pro", "claude-3.7-sonnet",
"qwen-max", "deepseek-r1", "llama-3.3-70b", "mistral-large", "command-r-plus", "doubao-pro-32k",
"hunyuan-large",
},
[]string{
"gpt-3.5-turbo", "claude-2.1", "gemini-1.5-pro", "mixtral-8x7b", "qwen-plus", "glm-4",
"yi-large", "moonshot-v1", "doubao-lite",
},
)
require.Contains(t, content, "其余 4 个渠道已省略")
require.Contains(t, content, "其余 1 个已省略")
require.Contains(t, content, "失败渠道 ID(展示 10/12)")
require.Contains(t, content, "其余 2 个已省略")
}
func TestShouldSendUpstreamModelUpdateNotification(t *testing.T) {
channelUpstreamModelUpdateNotifyState.Lock()
channelUpstreamModelUpdateNotifyState.lastNotifiedAt = 0
channelUpstreamModelUpdateNotifyState.lastChangedChannels = 0
channelUpstreamModelUpdateNotifyState.lastFailedChannels = 0
channelUpstreamModelUpdateNotifyState.Unlock()
baseTime := int64(2000000)
require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime, 6, 0))
require.False(t, shouldSendUpstreamModelUpdateNotification(baseTime+3600, 6, 0))
require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+3600, 7, 0))
require.False(t, shouldSendUpstreamModelUpdateNotification(baseTime+7200, 7, 0))
require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+8000, 0, 3))
require.False(t, shouldSendUpstreamModelUpdateNotification(baseTime+9000, 0, 3))
require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+10000, 0, 4))
require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+90000, 7, 0))
require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+90001, 0, 0))
}
...@@ -1041,6 +1041,7 @@ type UpdateUserSettingRequest struct { ...@@ -1041,6 +1041,7 @@ type UpdateUserSettingRequest struct {
GotifyUrl string `json:"gotify_url,omitempty"` GotifyUrl string `json:"gotify_url,omitempty"`
GotifyToken string `json:"gotify_token,omitempty"` GotifyToken string `json:"gotify_token,omitempty"`
GotifyPriority int `json:"gotify_priority,omitempty"` GotifyPriority int `json:"gotify_priority,omitempty"`
UpstreamModelUpdateNotifyEnabled *bool `json:"upstream_model_update_notify_enabled,omitempty"`
AcceptUnsetModelRatioModel bool `json:"accept_unset_model_ratio_model"` AcceptUnsetModelRatioModel bool `json:"accept_unset_model_ratio_model"`
RecordIpLog bool `json:"record_ip_log"` RecordIpLog bool `json:"record_ip_log"`
} }
...@@ -1132,11 +1133,17 @@ func UpdateUserSetting(c *gin.Context) { ...@@ -1132,11 +1133,17 @@ func UpdateUserSetting(c *gin.Context) {
common.ApiError(c, err) common.ApiError(c, err)
return return
} }
existingSettings := user.GetSetting()
upstreamModelUpdateNotifyEnabled := existingSettings.UpstreamModelUpdateNotifyEnabled
if user.Role >= common.RoleAdminUser && req.UpstreamModelUpdateNotifyEnabled != nil {
upstreamModelUpdateNotifyEnabled = *req.UpstreamModelUpdateNotifyEnabled
}
// 构建设置 // 构建设置
settings := dto.UserSetting{ settings := dto.UserSetting{
NotifyType: req.QuotaWarningType, NotifyType: req.QuotaWarningType,
QuotaWarningThreshold: req.QuotaWarningThreshold, QuotaWarningThreshold: req.QuotaWarningThreshold,
UpstreamModelUpdateNotifyEnabled: upstreamModelUpdateNotifyEnabled,
AcceptUnsetRatioModel: req.AcceptUnsetModelRatioModel, AcceptUnsetRatioModel: req.AcceptUnsetModelRatioModel,
RecordIpLog: req.RecordIpLog, RecordIpLog: req.RecordIpLog,
} }
......
...@@ -29,11 +29,17 @@ type ChannelOtherSettings struct { ...@@ -29,11 +29,17 @@ type ChannelOtherSettings struct {
OpenRouterEnterprise *bool `json:"openrouter_enterprise,omitempty"` OpenRouterEnterprise *bool `json:"openrouter_enterprise,omitempty"`
ClaudeBetaQuery bool `json:"claude_beta_query,omitempty"` // Claude 渠道是否强制追加 ?beta=true ClaudeBetaQuery bool `json:"claude_beta_query,omitempty"` // Claude 渠道是否强制追加 ?beta=true
AllowServiceTier bool `json:"allow_service_tier,omitempty"` // 是否允许 service_tier 透传(默认过滤以避免额外计费) AllowServiceTier bool `json:"allow_service_tier,omitempty"` // 是否允许 service_tier 透传(默认过滤以避免额外计费)
AllowInferenceGeo bool `json:"allow_inference_geo,omitempty"` // 是否允许 inference_geo 透传(仅 Claude,默认过滤以满足数据驻留合规) AllowInferenceGeo bool `json:"allow_inference_geo,omitempty"` // 是否允许 inference_geo 透传(仅 Claude,默认过滤以满足数据驻留合规
DisableStore bool `json:"disable_store,omitempty"` // 是否禁用 store 透传(默认允许透传,禁用后可能导致 Codex 无法使用)
AllowSafetyIdentifier bool `json:"allow_safety_identifier,omitempty"` // 是否允许 safety_identifier 透传(默认过滤以保护用户隐私) AllowSafetyIdentifier bool `json:"allow_safety_identifier,omitempty"` // 是否允许 safety_identifier 透传(默认过滤以保护用户隐私)
AllowIncludeObfuscation bool `json:"allow_include_obfuscation,omitempty"` // 是否允许 stream_options.include_obfuscation 透传(默认过滤以避免关闭流混淆保护) DisableStore bool `json:"disable_store,omitempty"` // 是否禁用 store 透传(默认允许透传,禁用后可能导致 Codex 无法使用)
AllowIncludeObfuscation bool `json:"allow_include_obfuscation, omitempty"` // 是否允许 stream_options.include_obfuscation 透传(默认过滤以避免关闭流混淆保护)
AwsKeyType AwsKeyType `json:"aws_key_type,omitempty"` AwsKeyType AwsKeyType `json:"aws_key_type,omitempty"`
UpstreamModelUpdateCheckEnabled bool `json:"upstream_model_update_check_enabled,omitempty"` // 是否检测上游模型更新
UpstreamModelUpdateAutoSyncEnabled bool `json:"upstream_model_update_auto_sync_enabled,omitempty"` // 是否自动同步上游模型更新
UpstreamModelUpdateLastCheckTime int64 `json:"upstream_model_update_last_check_time,omitempty"` // 上次检测时间
UpstreamModelUpdateLastDetectedModels []string `json:"upstream_model_update_last_detected_models,omitempty"` // 上次检测到的可加入模型
UpstreamModelUpdateLastRemovedModels []string `json:"upstream_model_update_last_removed_models,omitempty"` // 上次检测到的可删除模型
UpstreamModelUpdateIgnoredModels []string `json:"upstream_model_update_ignored_models,omitempty"` // 手动忽略的模型
} }
func (s *ChannelOtherSettings) IsOpenRouterEnterprise() bool { func (s *ChannelOtherSettings) IsOpenRouterEnterprise() bool {
......
...@@ -10,6 +10,7 @@ type UserSetting struct { ...@@ -10,6 +10,7 @@ type UserSetting struct {
GotifyUrl string `json:"gotify_url,omitempty"` // GotifyUrl Gotify服务器地址 GotifyUrl string `json:"gotify_url,omitempty"` // GotifyUrl Gotify服务器地址
GotifyToken string `json:"gotify_token,omitempty"` // GotifyToken Gotify应用令牌 GotifyToken string `json:"gotify_token,omitempty"` // GotifyToken Gotify应用令牌
GotifyPriority int `json:"gotify_priority"` // GotifyPriority Gotify消息优先级 GotifyPriority int `json:"gotify_priority"` // GotifyPriority Gotify消息优先级
UpstreamModelUpdateNotifyEnabled bool `json:"upstream_model_update_notify_enabled,omitempty"` // 是否接收上游模型更新定时检测通知(仅管理员)
AcceptUnsetRatioModel bool `json:"accept_unset_model_ratio_model,omitempty"` // AcceptUnsetRatioModel 是否接受未设置价格的模型 AcceptUnsetRatioModel bool `json:"accept_unset_model_ratio_model,omitempty"` // AcceptUnsetRatioModel 是否接受未设置价格的模型
RecordIpLog bool `json:"record_ip_log,omitempty"` // 是否记录请求和错误日志IP RecordIpLog bool `json:"record_ip_log,omitempty"` // 是否记录请求和错误日志IP
SidebarModules string `json:"sidebar_modules,omitempty"` // SidebarModules 左侧边栏模块配置 SidebarModules string `json:"sidebar_modules,omitempty"` // SidebarModules 左侧边栏模块配置
......
...@@ -121,6 +121,9 @@ func main() { ...@@ -121,6 +121,9 @@ func main() {
return a return a
} }
// Channel upstream model update check task
controller.StartChannelUpstreamModelUpdateTask()
if common.IsMasterNode && constant.UpdateTask { if common.IsMasterNode && constant.UpdateTask {
gopool.Go(func() { gopool.Go(func() {
controller.UpdateMidjourneyTaskBulk() controller.UpdateMidjourneyTaskBulk()
......
...@@ -237,6 +237,10 @@ func SetApiRouter(router *gin.Engine) { ...@@ -237,6 +237,10 @@ func SetApiRouter(router *gin.Engine) {
channelRoute.GET("/tag/models", controller.GetTagModels) channelRoute.GET("/tag/models", controller.GetTagModels)
channelRoute.POST("/copy/:id", controller.CopyChannel) channelRoute.POST("/copy/:id", controller.CopyChannel)
channelRoute.POST("/multi_key/manage", controller.ManageMultiKeys) channelRoute.POST("/multi_key/manage", controller.ManageMultiKeys)
channelRoute.POST("/upstream_updates/apply", controller.ApplyChannelUpstreamModelUpdates)
channelRoute.POST("/upstream_updates/apply_all", controller.ApplyAllChannelUpstreamModelUpdates)
channelRoute.POST("/upstream_updates/detect", controller.DetectChannelUpstreamModelUpdates)
channelRoute.POST("/upstream_updates/detect_all", controller.DetectAllChannelUpstreamModelUpdates)
} }
tokenRoute := apiRouter.Group("/token") tokenRoute := apiRouter.Group("/token")
tokenRoute.Use(middleware.UserAuth()) tokenRoute.Use(middleware.UserAuth())
......
...@@ -616,7 +616,9 @@ type mockAdaptor struct { ...@@ -616,7 +616,9 @@ type mockAdaptor struct {
} }
func (m *mockAdaptor) Init(_ *relaycommon.RelayInfo) {} func (m *mockAdaptor) Init(_ *relaycommon.RelayInfo) {}
func (m *mockAdaptor) FetchTask(string, string, map[string]any, string) (*http.Response, error) { return nil, nil } func (m *mockAdaptor) FetchTask(string, string, map[string]any, string) (*http.Response, error) {
return nil, nil
}
func (m *mockAdaptor) ParseTaskResult([]byte) (*relaycommon.TaskInfo, error) { return nil, nil } func (m *mockAdaptor) ParseTaskResult([]byte) (*relaycommon.TaskInfo, error) { return nil, nil }
func (m *mockAdaptor) AdjustBillingOnComplete(_ *model.Task, _ *relaycommon.TaskInfo) int { func (m *mockAdaptor) AdjustBillingOnComplete(_ *model.Task, _ *relaycommon.TaskInfo) int {
return m.adjustReturn return m.adjustReturn
......
...@@ -22,6 +22,32 @@ func NotifyRootUser(t string, subject string, content string) { ...@@ -22,6 +22,32 @@ func NotifyRootUser(t string, subject string, content string) {
} }
} }
func NotifyUpstreamModelUpdateWatchers(subject string, content string) {
var users []model.User
if err := model.DB.
Select("id", "email", "role", "status", "setting").
Where("status = ? AND role >= ?", common.UserStatusEnabled, common.RoleAdminUser).
Find(&users).Error; err != nil {
common.SysLog(fmt.Sprintf("failed to query upstream update notification users: %s", err.Error()))
return
}
notification := dto.NewNotify(dto.NotifyTypeChannelUpdate, subject, content, nil)
sentCount := 0
for _, user := range users {
userSetting := user.GetSetting()
if !userSetting.UpstreamModelUpdateNotifyEnabled {
continue
}
if err := NotifyUser(user.Id, user.Email, userSetting, notification); err != nil {
common.SysLog(fmt.Sprintf("failed to notify user %d for upstream model update: %s", user.Id, err.Error()))
continue
}
sentCount++
}
common.SysLog(fmt.Sprintf("upstream model update notifications sent: %d", sentCount))
}
func NotifyUser(userId int, userEmail string, userSetting dto.UserSetting, data dto.Notify) error { func NotifyUser(userId int, userEmail string, userSetting dto.UserSetting, data dto.Notify) error {
notifyType := userSetting.NotifyType notifyType := userSetting.NotifyType
if notifyType == "" { if notifyType == "" {
......
...@@ -86,6 +86,7 @@ const PersonalSetting = () => { ...@@ -86,6 +86,7 @@ const PersonalSetting = () => {
gotifyUrl: '', gotifyUrl: '',
gotifyToken: '', gotifyToken: '',
gotifyPriority: 5, gotifyPriority: 5,
upstreamModelUpdateNotifyEnabled: false,
acceptUnsetModelRatioModel: false, acceptUnsetModelRatioModel: false,
recordIpLog: false, recordIpLog: false,
}); });
...@@ -158,6 +159,8 @@ const PersonalSetting = () => { ...@@ -158,6 +159,8 @@ const PersonalSetting = () => {
gotifyToken: settings.gotify_token || '', gotifyToken: settings.gotify_token || '',
gotifyPriority: gotifyPriority:
settings.gotify_priority !== undefined ? settings.gotify_priority : 5, settings.gotify_priority !== undefined ? settings.gotify_priority : 5,
upstreamModelUpdateNotifyEnabled:
settings.upstream_model_update_notify_enabled === true,
acceptUnsetModelRatioModel: acceptUnsetModelRatioModel:
settings.accept_unset_model_ratio_model || false, settings.accept_unset_model_ratio_model || false,
recordIpLog: settings.record_ip_log || false, recordIpLog: settings.record_ip_log || false,
...@@ -426,6 +429,8 @@ const PersonalSetting = () => { ...@@ -426,6 +429,8 @@ const PersonalSetting = () => {
const parsed = parseInt(notificationSettings.gotifyPriority); const parsed = parseInt(notificationSettings.gotifyPriority);
return isNaN(parsed) ? 5 : parsed; return isNaN(parsed) ? 5 : parsed;
})(), })(),
upstream_model_update_notify_enabled:
notificationSettings.upstreamModelUpdateNotifyEnabled === true,
accept_unset_model_ratio_model: accept_unset_model_ratio_model:
notificationSettings.acceptUnsetModelRatioModel, notificationSettings.acceptUnsetModelRatioModel,
record_ip_log: notificationSettings.recordIpLog, record_ip_log: notificationSettings.recordIpLog,
......
...@@ -58,6 +58,7 @@ const NotificationSettings = ({ ...@@ -58,6 +58,7 @@ const NotificationSettings = ({
const formApiRef = useRef(null); const formApiRef = useRef(null);
const [statusState] = useContext(StatusContext); const [statusState] = useContext(StatusContext);
const [userState] = useContext(UserContext); const [userState] = useContext(UserContext);
const isAdminOrRoot = (userState?.user?.role || 0) >= 10;
// 左侧边栏设置相关状态 // 左侧边栏设置相关状态
const [sidebarLoading, setSidebarLoading] = useState(false); const [sidebarLoading, setSidebarLoading] = useState(false);
...@@ -470,6 +471,21 @@ const NotificationSettings = ({ ...@@ -470,6 +471,21 @@ const NotificationSettings = ({
]} ]}
/> />
{isAdminOrRoot && (
<Form.Switch
field='upstreamModelUpdateNotifyEnabled'
label={t('接收上游模型更新通知')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleFormChange('upstreamModelUpdateNotifyEnabled', value)
}
extraText={t(
'仅管理员可用。开启后,当系统定时检测全部渠道发现上游模型变更或检测异常时,将按你选择的通知方式发送汇总通知;渠道或模型过多时会自动省略部分明细。',
)}
/>
)}
{/* 邮件通知设置 */} {/* 邮件通知设置 */}
{notificationSettings.warningType === 'email' && ( {notificationSettings.warningType === 'email' && (
<Form.Input <Form.Input
......
...@@ -36,6 +36,10 @@ const ChannelsActions = ({ ...@@ -36,6 +36,10 @@ const ChannelsActions = ({
fixChannelsAbilities, fixChannelsAbilities,
updateAllChannelsBalance, updateAllChannelsBalance,
deleteAllDisabledChannels, deleteAllDisabledChannels,
applyAllUpstreamUpdates,
detectAllUpstreamUpdates,
detectAllUpstreamUpdatesLoading,
applyAllUpstreamUpdatesLoading,
compactMode, compactMode,
setCompactMode, setCompactMode,
idSort, idSort,
...@@ -96,6 +100,8 @@ const ChannelsActions = ({ ...@@ -96,6 +100,8 @@ const ChannelsActions = ({
size='small' size='small'
type='tertiary' type='tertiary'
className='w-full' className='w-full'
loading={detectAllUpstreamUpdatesLoading}
disabled={detectAllUpstreamUpdatesLoading}
onClick={() => { onClick={() => {
Modal.confirm({ Modal.confirm({
title: t('确定?'), title: t('确定?'),
...@@ -149,6 +155,46 @@ const ChannelsActions = ({ ...@@ -149,6 +155,46 @@ const ChannelsActions = ({
<Dropdown.Item> <Dropdown.Item>
<Button <Button
size='small' size='small'
type='tertiary'
className='w-full'
onClick={() => {
Modal.confirm({
title: t('确定?'),
content: t(
'确定要仅检测全部渠道上游模型更新吗?(不执行新增/删除)',
),
onOk: () => detectAllUpstreamUpdates(),
size: 'sm',
centered: true,
});
}}
>
{t('检测全部渠道上游更新')}
</Button>
</Dropdown.Item>
<Dropdown.Item>
<Button
size='small'
type='primary'
className='w-full'
loading={applyAllUpstreamUpdatesLoading}
disabled={applyAllUpstreamUpdatesLoading}
onClick={() => {
Modal.confirm({
title: t('确定?'),
content: t('确定要对全部渠道执行上游模型更新吗?'),
onOk: () => applyAllUpstreamUpdates(),
size: 'sm',
centered: true,
});
}}
>
{t('处理全部渠道上游更新')}
</Button>
</Dropdown.Item>
<Dropdown.Item>
<Button
size='small'
type='danger' type='danger'
className='w-full' className='w-full'
onClick={() => { onClick={() => {
......
...@@ -37,8 +37,13 @@ import { ...@@ -37,8 +37,13 @@ import {
renderQuotaWithAmount, renderQuotaWithAmount,
showSuccess, showSuccess,
showError, showError,
showInfo,
} from '../../../helpers'; } from '../../../helpers';
import { CHANNEL_OPTIONS } from '../../../constants'; import {
CHANNEL_OPTIONS,
MODEL_FETCHABLE_CHANNEL_TYPES,
} from '../../../constants';
import { parseUpstreamUpdateMeta } from '../../../hooks/channels/upstreamUpdateUtils';
import { import {
IconTreeTriangleDown, IconTreeTriangleDown,
IconMore, IconMore,
...@@ -270,6 +275,35 @@ const isRequestPassThroughEnabled = (record) => { ...@@ -270,6 +275,35 @@ const isRequestPassThroughEnabled = (record) => {
} }
}; };
const getUpstreamUpdateMeta = (record) => {
const supported =
!!record &&
record.children === undefined &&
MODEL_FETCHABLE_CHANNEL_TYPES.has(record.type);
if (!record || record.children !== undefined) {
return {
supported: false,
enabled: false,
pendingAddModels: [],
pendingRemoveModels: [],
};
}
const parsed =
record?.upstreamUpdateMeta && typeof record.upstreamUpdateMeta === 'object'
? record.upstreamUpdateMeta
: parseUpstreamUpdateMeta(record?.settings);
return {
supported,
enabled: parsed?.enabled === true,
pendingAddModels: Array.isArray(parsed?.pendingAddModels)
? parsed.pendingAddModels
: [],
pendingRemoveModels: Array.isArray(parsed?.pendingRemoveModels)
? parsed.pendingRemoveModels
: [],
};
};
export const getChannelsColumns = ({ export const getChannelsColumns = ({
t, t,
COLUMN_KEYS, COLUMN_KEYS,
...@@ -291,6 +325,8 @@ export const getChannelsColumns = ({ ...@@ -291,6 +325,8 @@ export const getChannelsColumns = ({
checkOllamaVersion, checkOllamaVersion,
setShowMultiKeyManageModal, setShowMultiKeyManageModal,
setCurrentMultiKeyChannel, setCurrentMultiKeyChannel,
openUpstreamUpdateModal,
detectChannelUpstreamUpdates,
}) => { }) => {
return [ return [
{ {
...@@ -304,6 +340,14 @@ export const getChannelsColumns = ({ ...@@ -304,6 +340,14 @@ export const getChannelsColumns = ({
dataIndex: 'name', dataIndex: 'name',
render: (text, record, index) => { render: (text, record, index) => {
const passThroughEnabled = isRequestPassThroughEnabled(record); const passThroughEnabled = isRequestPassThroughEnabled(record);
const upstreamUpdateMeta = getUpstreamUpdateMeta(record);
const pendingAddCount = upstreamUpdateMeta.pendingAddModels.length;
const pendingRemoveCount =
upstreamUpdateMeta.pendingRemoveModels.length;
const showUpstreamUpdateTag =
upstreamUpdateMeta.supported &&
upstreamUpdateMeta.enabled &&
(pendingAddCount > 0 || pendingRemoveCount > 0);
const nameNode = const nameNode =
record.remark && record.remark.trim() !== '' ? ( record.remark && record.remark.trim() !== '' ? (
<Tooltip <Tooltip
...@@ -339,13 +383,14 @@ export const getChannelsColumns = ({ ...@@ -339,13 +383,14 @@ export const getChannelsColumns = ({
<span>{text}</span> <span>{text}</span>
); );
if (!passThroughEnabled) { if (!passThroughEnabled && !showUpstreamUpdateTag) {
return nameNode; return nameNode;
} }
return ( return (
<Space spacing={6} align='center'> <Space spacing={6} align='center'>
{nameNode} {nameNode}
{passThroughEnabled && (
<Tooltip <Tooltip
content={t( content={t(
'该渠道已开启请求透传:参数覆写、模型重定向、渠道适配等 NewAPI 内置功能将失效,非最佳实践;如因此产生问题,请勿提交 issue 反馈。', '该渠道已开启请求透传:参数覆写、模型重定向、渠道适配等 NewAPI 内置功能将失效,非最佳实践;如因此产生问题,请勿提交 issue 反馈。',
...@@ -359,6 +404,55 @@ export const getChannelsColumns = ({ ...@@ -359,6 +404,55 @@ export const getChannelsColumns = ({
/> />
</span> </span>
</Tooltip> </Tooltip>
)}
{showUpstreamUpdateTag && (
<Space spacing={4} align='center'>
{pendingAddCount > 0 ? (
<Tooltip content={t('点击处理新增模型')} position='top'>
<Tag
color='green'
type='light'
size='small'
shape='circle'
className='cursor-pointer transition-all duration-150 hover:opacity-85 hover:-translate-y-px active:scale-95'
onClick={(e) => {
e.stopPropagation();
openUpstreamUpdateModal(
record,
upstreamUpdateMeta.pendingAddModels,
upstreamUpdateMeta.pendingRemoveModels,
'add',
);
}}
>
+{pendingAddCount}
</Tag>
</Tooltip>
) : null}
{pendingRemoveCount > 0 ? (
<Tooltip content={t('点击处理删除模型')} position='top'>
<Tag
color='red'
type='light'
size='small'
shape='circle'
className='cursor-pointer transition-all duration-150 hover:opacity-85 hover:-translate-y-px active:scale-95'
onClick={(e) => {
e.stopPropagation();
openUpstreamUpdateModal(
record,
upstreamUpdateMeta.pendingAddModels,
upstreamUpdateMeta.pendingRemoveModels,
'remove',
);
}}
>
-{pendingRemoveCount}
</Tag>
</Tooltip>
) : null}
</Space>
)}
</Space> </Space>
); );
}, },
...@@ -585,6 +679,7 @@ export const getChannelsColumns = ({ ...@@ -585,6 +679,7 @@ export const getChannelsColumns = ({
fixed: 'right', fixed: 'right',
render: (text, record, index) => { render: (text, record, index) => {
if (record.children === undefined) { if (record.children === undefined) {
const upstreamUpdateMeta = getUpstreamUpdateMeta(record);
const moreMenuItems = [ const moreMenuItems = [
{ {
node: 'item', node: 'item',
...@@ -622,6 +717,47 @@ export const getChannelsColumns = ({ ...@@ -622,6 +717,47 @@ export const getChannelsColumns = ({
}, },
]; ];
if (upstreamUpdateMeta.supported) {
moreMenuItems.push({
node: 'item',
name: t('仅检测上游模型更新'),
type: 'tertiary',
onClick: () => {
if (!upstreamUpdateMeta.enabled) {
showInfo(t('该渠道未开启上游模型更新检测'));
return;
}
detectChannelUpstreamUpdates(record);
},
});
moreMenuItems.push({
node: 'item',
name: t('处理上游模型更新'),
type: 'tertiary',
onClick: () => {
if (!upstreamUpdateMeta.enabled) {
showInfo(t('该渠道未开启上游模型更新检测'));
return;
}
if (
upstreamUpdateMeta.pendingAddModels.length === 0 &&
upstreamUpdateMeta.pendingRemoveModels.length === 0
) {
showInfo(t('该渠道暂无可处理的上游模型更新'));
return;
}
openUpstreamUpdateModal(
record,
upstreamUpdateMeta.pendingAddModels,
upstreamUpdateMeta.pendingRemoveModels,
upstreamUpdateMeta.pendingAddModels.length > 0
? 'add'
: 'remove',
);
},
});
}
if (record.type === 4) { if (record.type === 4) {
moreMenuItems.unshift({ moreMenuItems.unshift({
node: 'item', node: 'item',
......
...@@ -61,6 +61,8 @@ const ChannelsTable = (channelsData) => { ...@@ -61,6 +61,8 @@ const ChannelsTable = (channelsData) => {
// Multi-key management // Multi-key management
setShowMultiKeyManageModal, setShowMultiKeyManageModal,
setCurrentMultiKeyChannel, setCurrentMultiKeyChannel,
openUpstreamUpdateModal,
detectChannelUpstreamUpdates,
} = channelsData; } = channelsData;
// Get all columns // Get all columns
...@@ -86,6 +88,8 @@ const ChannelsTable = (channelsData) => { ...@@ -86,6 +88,8 @@ const ChannelsTable = (channelsData) => {
checkOllamaVersion, checkOllamaVersion,
setShowMultiKeyManageModal, setShowMultiKeyManageModal,
setCurrentMultiKeyChannel, setCurrentMultiKeyChannel,
openUpstreamUpdateModal,
detectChannelUpstreamUpdates,
}); });
}, [ }, [
t, t,
...@@ -108,6 +112,8 @@ const ChannelsTable = (channelsData) => { ...@@ -108,6 +112,8 @@ const ChannelsTable = (channelsData) => {
checkOllamaVersion, checkOllamaVersion,
setShowMultiKeyManageModal, setShowMultiKeyManageModal,
setCurrentMultiKeyChannel, setCurrentMultiKeyChannel,
openUpstreamUpdateModal,
detectChannelUpstreamUpdates,
]); ]);
// Filter columns based on visibility settings // Filter columns based on visibility settings
......
...@@ -33,6 +33,7 @@ import ColumnSelectorModal from './modals/ColumnSelectorModal'; ...@@ -33,6 +33,7 @@ import ColumnSelectorModal from './modals/ColumnSelectorModal';
import EditChannelModal from './modals/EditChannelModal'; import EditChannelModal from './modals/EditChannelModal';
import EditTagModal from './modals/EditTagModal'; import EditTagModal from './modals/EditTagModal';
import MultiKeyManageModal from './modals/MultiKeyManageModal'; import MultiKeyManageModal from './modals/MultiKeyManageModal';
import ChannelUpstreamUpdateModal from './modals/ChannelUpstreamUpdateModal';
import { createCardProPagination } from '../../../helpers/utils'; import { createCardProPagination } from '../../../helpers/utils';
const ChannelsPage = () => { const ChannelsPage = () => {
...@@ -63,6 +64,15 @@ const ChannelsPage = () => { ...@@ -63,6 +64,15 @@ const ChannelsPage = () => {
channel={channelsData.currentMultiKeyChannel} channel={channelsData.currentMultiKeyChannel}
onRefresh={channelsData.refresh} onRefresh={channelsData.refresh}
/> />
<ChannelUpstreamUpdateModal
visible={channelsData.showUpstreamUpdateModal}
addModels={channelsData.upstreamUpdateAddModels}
removeModels={channelsData.upstreamUpdateRemoveModels}
preferredTab={channelsData.upstreamUpdatePreferredTab}
confirmLoading={channelsData.upstreamApplyLoading}
onConfirm={channelsData.applyUpstreamUpdates}
onCancel={channelsData.closeUpstreamUpdateModal}
/>
{/* Main Content */} {/* Main Content */}
{channelsData.globalPassThroughEnabled ? ( {channelsData.globalPassThroughEnabled ? (
......
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Modal,
Checkbox,
Empty,
Input,
Tabs,
Typography,
} from '@douyinfe/semi-ui';
import {
IllustrationNoResult,
IllustrationNoResultDark,
} from '@douyinfe/semi-illustrations';
import { IconSearch } from '@douyinfe/semi-icons';
import { useIsMobile } from '../../../../hooks/common/useIsMobile';
const normalizeModels = (models = []) =>
Array.from(
new Set(
(models || []).map((model) => String(model || '').trim()).filter(Boolean),
),
);
const filterByKeyword = (models = [], keyword = '') => {
const normalizedKeyword = String(keyword || '')
.trim()
.toLowerCase();
if (!normalizedKeyword) {
return models;
}
return models.filter((model) =>
String(model).toLowerCase().includes(normalizedKeyword),
);
};
const ChannelUpstreamUpdateModal = ({
visible,
addModels = [],
removeModels = [],
preferredTab = 'add',
confirmLoading = false,
onConfirm,
onCancel,
}) => {
const { t } = useTranslation();
const isMobile = useIsMobile();
const normalizedAddModels = useMemo(
() => normalizeModels(addModels),
[addModels],
);
const normalizedRemoveModels = useMemo(
() => normalizeModels(removeModels),
[removeModels],
);
const [selectedAddModels, setSelectedAddModels] = useState([]);
const [selectedRemoveModels, setSelectedRemoveModels] = useState([]);
const [keyword, setKeyword] = useState('');
const [activeTab, setActiveTab] = useState('add');
const [partialSubmitConfirmed, setPartialSubmitConfirmed] = useState(false);
const addTabEnabled = normalizedAddModels.length > 0;
const removeTabEnabled = normalizedRemoveModels.length > 0;
const filteredAddModels = useMemo(
() => filterByKeyword(normalizedAddModels, keyword),
[normalizedAddModels, keyword],
);
const filteredRemoveModels = useMemo(
() => filterByKeyword(normalizedRemoveModels, keyword),
[normalizedRemoveModels, keyword],
);
useEffect(() => {
if (!visible) {
return;
}
setSelectedAddModels([]);
setSelectedRemoveModels([]);
setKeyword('');
setPartialSubmitConfirmed(false);
const normalizedPreferredTab = preferredTab === 'remove' ? 'remove' : 'add';
if (normalizedPreferredTab === 'remove' && removeTabEnabled) {
setActiveTab('remove');
return;
}
if (normalizedPreferredTab === 'add' && addTabEnabled) {
setActiveTab('add');
return;
}
setActiveTab(addTabEnabled ? 'add' : 'remove');
}, [visible, addTabEnabled, removeTabEnabled, preferredTab]);
const currentModels =
activeTab === 'add' ? filteredAddModels : filteredRemoveModels;
const currentSelectedModels =
activeTab === 'add' ? selectedAddModels : selectedRemoveModels;
const currentSetSelectedModels =
activeTab === 'add' ? setSelectedAddModels : setSelectedRemoveModels;
const selectedAddCount = selectedAddModels.length;
const selectedRemoveCount = selectedRemoveModels.length;
const checkedCount = currentModels.filter((model) =>
currentSelectedModels.includes(model),
).length;
const isAllChecked =
currentModels.length > 0 && checkedCount === currentModels.length;
const isIndeterminate =
checkedCount > 0 && checkedCount < currentModels.length;
const handleToggleAllCurrent = (checked) => {
if (checked) {
const merged = normalizeModels([
...currentSelectedModels,
...currentModels,
]);
currentSetSelectedModels(merged);
return;
}
const currentSet = new Set(currentModels);
currentSetSelectedModels(
currentSelectedModels.filter((model) => !currentSet.has(model)),
);
};
const tabList = [
{
itemKey: 'add',
tab: `${t('新增模型')} (${selectedAddCount}/${normalizedAddModels.length})`,
disabled: !addTabEnabled,
},
{
itemKey: 'remove',
tab: `${t('删除模型')} (${selectedRemoveCount}/${normalizedRemoveModels.length})`,
disabled: !removeTabEnabled,
},
];
const submitSelectedChanges = () => {
onConfirm?.({
addModels: selectedAddModels,
removeModels: selectedRemoveModels,
});
};
const handleSubmit = () => {
const hasAnySelected = selectedAddCount > 0 || selectedRemoveCount > 0;
if (!hasAnySelected) {
submitSelectedChanges();
return;
}
const hasBothPending = addTabEnabled && removeTabEnabled;
const hasUnselectedAdd = addTabEnabled && selectedAddCount === 0;
const hasUnselectedRemove = removeTabEnabled && selectedRemoveCount === 0;
if (hasBothPending && (hasUnselectedAdd || hasUnselectedRemove)) {
if (partialSubmitConfirmed) {
submitSelectedChanges();
return;
}
const missingTab = hasUnselectedAdd ? 'add' : 'remove';
const missingType = hasUnselectedAdd ? t('新增') : t('删除');
const missingCount = hasUnselectedAdd
? normalizedAddModels.length
: normalizedRemoveModels.length;
setActiveTab(missingTab);
Modal.confirm({
title: t('仍有未处理项'),
content: t(
'你还没有处理{{type}}模型({{count}}个)。是否仅提交当前已勾选内容?',
{
type: missingType,
count: missingCount,
},
),
okText: t('仅提交已勾选'),
cancelText: t('去处理{{type}}', { type: missingType }),
centered: true,
onOk: () => {
setPartialSubmitConfirmed(true);
submitSelectedChanges();
},
});
return;
}
submitSelectedChanges();
};
return (
<Modal
visible={visible}
title={t('处理上游模型更新')}
okText={t('确定')}
cancelText={t('取消')}
size={isMobile ? 'full-width' : 'medium'}
centered
closeOnEsc
maskClosable
confirmLoading={confirmLoading}
onCancel={onCancel}
onOk={handleSubmit}
>
<div className='flex flex-col gap-3'>
<Typography.Text type='secondary' size='small'>
{t(
'可勾选需要执行的变更:新增会加入渠道模型列表,删除会从渠道模型列表移除。',
)}
</Typography.Text>
<Tabs
type='slash'
size='small'
tabList={tabList}
activeKey={activeTab}
onChange={(key) => setActiveTab(key)}
/>
<div className='flex items-center gap-3 text-xs text-gray-500'>
<span>
{t('新增已选 {{selected}} / {{total}}', {
selected: selectedAddCount,
total: normalizedAddModels.length,
})}
</span>
<span>
{t('删除已选 {{selected}} / {{total}}', {
selected: selectedRemoveCount,
total: normalizedRemoveModels.length,
})}
</span>
</div>
<Input
prefix={<IconSearch size={14} />}
placeholder={t('搜索模型')}
value={keyword}
onChange={(value) => setKeyword(value)}
showClear
/>
<div style={{ maxHeight: 320, overflowY: 'auto', paddingRight: 8 }}>
{currentModels.length === 0 ? (
<Empty
image={
<IllustrationNoResult style={{ width: 150, height: 150 }} />
}
darkModeImage={
<IllustrationNoResultDark style={{ width: 150, height: 150 }} />
}
description={t('暂无匹配模型')}
style={{ padding: 24 }}
/>
) : (
<Checkbox.Group
value={currentSelectedModels}
onChange={(values) =>
currentSetSelectedModels(normalizeModels(values))
}
>
<div className='grid grid-cols-1 md:grid-cols-2 gap-x-4'>
{currentModels.map((model) => (
<Checkbox
key={`${activeTab}:${model}`}
value={model}
className='my-1'
>
{model}
</Checkbox>
))}
</div>
</Checkbox.Group>
)}
</div>
<div className='flex items-center justify-end gap-2'>
<Typography.Text type='secondary' size='small'>
{t('已选择 {{selected}} / {{total}}', {
selected: checkedCount,
total: currentModels.length,
})}
</Typography.Text>
<Checkbox
checked={isAllChecked}
indeterminate={isIndeterminate}
aria-label={t('全选当前列表模型')}
onChange={(e) => handleToggleAllCurrent(e.target.checked)}
/>
</div>
</div>
</Modal>
);
};
export default ChannelUpstreamUpdateModal;
...@@ -191,4 +191,9 @@ export const CHANNEL_OPTIONS = [ ...@@ -191,4 +191,9 @@ export const CHANNEL_OPTIONS = [
}, },
]; ];
// Channel types that support upstream model list fetching in UI.
export const MODEL_FETCHABLE_CHANNEL_TYPES = new Set([
1, 4, 14, 34, 17, 26, 27, 24, 47, 25, 20, 23, 31, 40, 42, 48, 43,
]);
export const MODEL_TABLE_PAGE_SIZE = 10; export const MODEL_TABLE_PAGE_SIZE = 10;
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
export const normalizeModelList = (models = []) =>
Array.from(
new Set(
(models || []).map((model) => String(model || '').trim()).filter(Boolean),
),
);
export const parseUpstreamUpdateMeta = (settings) => {
let parsed = null;
if (settings && typeof settings === 'object') {
parsed = settings;
} else if (typeof settings === 'string') {
try {
parsed = JSON.parse(settings);
} catch (error) {
parsed = null;
}
}
if (!parsed || typeof parsed !== 'object') {
return {
enabled: false,
pendingAddModels: [],
pendingRemoveModels: [],
};
}
return {
enabled: parsed.upstream_model_update_check_enabled === true,
pendingAddModels: normalizeModelList(
parsed.upstream_model_update_last_detected_models,
),
pendingRemoveModels: normalizeModelList(
parsed.upstream_model_update_last_removed_models,
),
};
};
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { useRef, useState } from 'react';
import { API, showError, showInfo, showSuccess } from '../../helpers';
import { normalizeModelList } from './upstreamUpdateUtils';
export const useChannelUpstreamUpdates = ({ t, refresh }) => {
const [showUpstreamUpdateModal, setShowUpstreamUpdateModal] = useState(false);
const [upstreamUpdateChannel, setUpstreamUpdateChannel] = useState(null);
const [upstreamUpdateAddModels, setUpstreamUpdateAddModels] = useState([]);
const [upstreamUpdateRemoveModels, setUpstreamUpdateRemoveModels] = useState(
[],
);
const [upstreamUpdatePreferredTab, setUpstreamUpdatePreferredTab] =
useState('add');
const [upstreamApplyLoading, setUpstreamApplyLoading] = useState(false);
const [detectAllUpstreamUpdatesLoading, setDetectAllUpstreamUpdatesLoading] =
useState(false);
const [applyAllUpstreamUpdatesLoading, setApplyAllUpstreamUpdatesLoading] =
useState(false);
const applyUpstreamUpdatesInFlightRef = useRef(false);
const detectChannelUpstreamUpdatesInFlightRef = useRef(false);
const detectAllUpstreamUpdatesInFlightRef = useRef(false);
const applyAllUpstreamUpdatesInFlightRef = useRef(false);
const openUpstreamUpdateModal = (
record,
pendingAddModels = [],
pendingRemoveModels = [],
preferredTab = 'add',
) => {
const normalizedAddModels = normalizeModelList(pendingAddModels);
const normalizedRemoveModels = normalizeModelList(pendingRemoveModels);
if (
!record?.id ||
(normalizedAddModels.length === 0 && normalizedRemoveModels.length === 0)
) {
showInfo(t('该渠道暂无可处理的上游模型更新'));
return;
}
setUpstreamUpdateChannel(record);
setUpstreamUpdateAddModels(normalizedAddModels);
setUpstreamUpdateRemoveModels(normalizedRemoveModels);
const normalizedPreferredTab = preferredTab === 'remove' ? 'remove' : 'add';
setUpstreamUpdatePreferredTab(normalizedPreferredTab);
setShowUpstreamUpdateModal(true);
};
const closeUpstreamUpdateModal = () => {
setShowUpstreamUpdateModal(false);
setUpstreamUpdateChannel(null);
setUpstreamUpdateAddModels([]);
setUpstreamUpdateRemoveModels([]);
setUpstreamUpdatePreferredTab('add');
};
const applyUpstreamUpdates = async ({
addModels: selectedAddModels = [],
removeModels: selectedRemoveModels = [],
} = {}) => {
if (applyUpstreamUpdatesInFlightRef.current) {
showInfo(t('正在处理,请稍候'));
return;
}
if (!upstreamUpdateChannel?.id) {
closeUpstreamUpdateModal();
return;
}
applyUpstreamUpdatesInFlightRef.current = true;
setUpstreamApplyLoading(true);
try {
const normalizedSelectedAddModels = normalizeModelList(selectedAddModels);
const normalizedSelectedRemoveModels =
normalizeModelList(selectedRemoveModels);
const selectedAddSet = new Set(normalizedSelectedAddModels);
const ignoreModels = upstreamUpdateAddModels.filter(
(model) => !selectedAddSet.has(model),
);
const res = await API.post(
'/api/channel/upstream_updates/apply',
{
id: upstreamUpdateChannel.id,
add_models: normalizedSelectedAddModels,
ignore_models: ignoreModels,
remove_models: normalizedSelectedRemoveModels,
},
{ skipErrorHandler: true },
);
const { success, message, data } = res.data || {};
if (!success) {
showError(message || t('操作失败'));
return;
}
const addedCount = data?.added_models?.length || 0;
const removedCount = data?.removed_models?.length || 0;
const ignoredCount = data?.ignored_models?.length || 0;
showSuccess(
t(
'已处理上游模型更新:加入 {{added}} 个,删除 {{removed}} 个,忽略 {{ignored}} 个',
{
added: addedCount,
removed: removedCount,
ignored: ignoredCount,
},
),
);
closeUpstreamUpdateModal();
await refresh();
} catch (error) {
showError(
error?.response?.data?.message || error?.message || t('操作失败'),
);
} finally {
applyUpstreamUpdatesInFlightRef.current = false;
setUpstreamApplyLoading(false);
}
};
const applyAllUpstreamUpdates = async () => {
if (applyAllUpstreamUpdatesInFlightRef.current) {
showInfo(t('正在批量处理,请稍候'));
return;
}
applyAllUpstreamUpdatesInFlightRef.current = true;
setApplyAllUpstreamUpdatesLoading(true);
try {
const res = await API.post(
'/api/channel/upstream_updates/apply_all',
{},
{ skipErrorHandler: true },
);
const { success, message, data } = res.data || {};
if (!success) {
showError(message || t('批量处理失败'));
return;
}
const channelCount = data?.processed_channels || 0;
const addedCount = data?.added_models || 0;
const removedCount = data?.removed_models || 0;
const failedCount = (data?.failed_channel_ids || []).length;
showSuccess(
t(
'已批量处理上游模型更新:渠道 {{channels}} 个,加入 {{added}} 个,删除 {{removed}} 个,失败 {{fails}} 个',
{
channels: channelCount,
added: addedCount,
removed: removedCount,
fails: failedCount,
},
),
);
await refresh();
} catch (error) {
showError(
error?.response?.data?.message || error?.message || t('批量处理失败'),
);
} finally {
applyAllUpstreamUpdatesInFlightRef.current = false;
setApplyAllUpstreamUpdatesLoading(false);
}
};
const detectChannelUpstreamUpdates = async (channel) => {
if (detectChannelUpstreamUpdatesInFlightRef.current) {
showInfo(t('正在检测,请稍候'));
return;
}
if (!channel?.id) {
return;
}
detectChannelUpstreamUpdatesInFlightRef.current = true;
try {
const res = await API.post(
'/api/channel/upstream_updates/detect',
{
id: channel.id,
},
{ skipErrorHandler: true },
);
const { success, message, data } = res.data || {};
if (!success) {
showError(message || t('检测失败'));
return;
}
const addCount = data?.add_models?.length || 0;
const removeCount = data?.remove_models?.length || 0;
showSuccess(
t('检测完成:新增 {{add}} 个,删除 {{remove}} 个', {
add: addCount,
remove: removeCount,
}),
);
await refresh();
} catch (error) {
showError(
error?.response?.data?.message || error?.message || t('检测失败'),
);
} finally {
detectChannelUpstreamUpdatesInFlightRef.current = false;
}
};
const detectAllUpstreamUpdates = async () => {
if (detectAllUpstreamUpdatesInFlightRef.current) {
showInfo(t('正在批量检测,请稍候'));
return;
}
detectAllUpstreamUpdatesInFlightRef.current = true;
setDetectAllUpstreamUpdatesLoading(true);
try {
const res = await API.post(
'/api/channel/upstream_updates/detect_all',
{},
{ skipErrorHandler: true },
);
const { success, message, data } = res.data || {};
if (!success) {
showError(message || t('批量检测失败'));
return;
}
const channelCount = data?.processed_channels || 0;
const addCount = data?.detected_add_models || 0;
const removeCount = data?.detected_remove_models || 0;
const failedCount = (data?.failed_channel_ids || []).length;
showSuccess(
t(
'批量检测完成:渠道 {{channels}} 个,新增 {{add}} 个,删除 {{remove}} 个,失败 {{fails}} 个',
{
channels: channelCount,
add: addCount,
remove: removeCount,
fails: failedCount,
},
),
);
await refresh();
} catch (error) {
showError(
error?.response?.data?.message || error?.message || t('批量检测失败'),
);
} finally {
detectAllUpstreamUpdatesInFlightRef.current = false;
setDetectAllUpstreamUpdatesLoading(false);
}
};
return {
showUpstreamUpdateModal,
setShowUpstreamUpdateModal,
upstreamUpdateChannel,
upstreamUpdateAddModels,
upstreamUpdateRemoveModels,
upstreamUpdatePreferredTab,
upstreamApplyLoading,
detectAllUpstreamUpdatesLoading,
applyAllUpstreamUpdatesLoading,
openUpstreamUpdateModal,
closeUpstreamUpdateModal,
applyUpstreamUpdates,
applyAllUpstreamUpdates,
detectChannelUpstreamUpdates,
detectAllUpstreamUpdates,
};
};
...@@ -35,6 +35,8 @@ import { ...@@ -35,6 +35,8 @@ import {
} from '../../constants'; } from '../../constants';
import { useIsMobile } from '../common/useIsMobile'; import { useIsMobile } from '../common/useIsMobile';
import { useTableCompactMode } from '../common/useTableCompactMode'; import { useTableCompactMode } from '../common/useTableCompactMode';
import { useChannelUpstreamUpdates } from './useChannelUpstreamUpdates';
import { parseUpstreamUpdateMeta } from './upstreamUpdateUtils';
import { Modal, Button } from '@douyinfe/semi-ui'; import { Modal, Button } from '@douyinfe/semi-ui';
import { openCodexUsageModal } from '../../components/table/channels/modals/CodexUsageModal'; import { openCodexUsageModal } from '../../components/table/channels/modals/CodexUsageModal';
...@@ -235,6 +237,9 @@ export const useChannelsData = () => { ...@@ -235,6 +237,9 @@ export const useChannelsData = () => {
let channelTags = {}; let channelTags = {};
for (let i = 0; i < channels.length; i++) { for (let i = 0; i < channels.length; i++) {
channels[i].upstreamUpdateMeta = parseUpstreamUpdateMeta(
channels[i].settings,
);
channels[i].key = '' + channels[i].id; channels[i].key = '' + channels[i].id;
if (!enableTagMode) { if (!enableTagMode) {
channelDates.push(channels[i]); channelDates.push(channels[i]);
...@@ -432,6 +437,8 @@ export const useChannelsData = () => { ...@@ -432,6 +437,8 @@ export const useChannelsData = () => {
} }
}; };
const upstreamUpdates = useChannelUpstreamUpdates({ t, refresh });
// Channel management // Channel management
const manageChannel = async (id, action, record, value) => { const manageChannel = async (id, action, record, value) => {
let data = { id }; let data = { id };
...@@ -1194,6 +1201,7 @@ export const useChannelsData = () => { ...@@ -1194,6 +1201,7 @@ export const useChannelsData = () => {
setShowMultiKeyManageModal, setShowMultiKeyManageModal,
currentMultiKeyChannel, currentMultiKeyChannel,
setCurrentMultiKeyChannel, setCurrentMultiKeyChannel,
...upstreamUpdates,
// Form // Form
formApi, formApi,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment