diff --git a/controller/channel-test.go b/controller/channel-test.go index 8599fe6b..bba9a657 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -80,7 +80,10 @@ func testChannel(channel *model.Channel, request ChatRequest) (err error, openai if err != nil { return fmt.Errorf("Error: %s\nResp body: %s", err, body), nil } - if response.Usage.CompletionTokens == 0 && (response.Error.Type != "" && response.Error.Code != "" && response.Error.Message != "") { + if response.Usage.CompletionTokens == 0 { + if response.Error.Message == "" { + response.Error.Message = "补全 tokens 非预期返回 0" + } return errors.New(fmt.Sprintf("type %s, code %v, message %s", response.Error.Type, response.Error.Code, response.Error.Message)), &response.Error } return nil, nil @@ -142,32 +145,30 @@ func TestChannel(c *gin.Context) { var testAllChannelsLock sync.Mutex var testAllChannelsRunning bool = false -// disable & notify -func disableChannel(channelId int, channelName string, reason string) { +func notifyRootUser(subject string, content string) { if common.RootUserEmail == "" { common.RootUserEmail = model.GetRootUserEmail() } - model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled) - subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId) - content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason) err := common.SendEmail(subject, common.RootUserEmail, content) if err != nil { common.SysError(fmt.Sprintf("failed to send email: %s", err.Error())) } } +// disable & notify +func disableChannel(channelId int, channelName string, reason string) { + model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled) + subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId) + content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason) + notifyRootUser(subject, content) +} + // enable & notify func enableChannel(channelId int, channelName string) { - if common.RootUserEmail == "" { - common.RootUserEmail = model.GetRootUserEmail() - } model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled) subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId) - content := fmt.Sprintf("通道「%s」(#%d)现在已被启用", channelName, channelId) - err := common.SendEmail(subject, common.RootUserEmail, content) - if err != nil { - common.SysError(fmt.Sprintf("failed to send email: %s", err.Error())) - } + content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId) + notifyRootUser(subject, content) } func testAllChannels(notify bool) error { @@ -192,18 +193,19 @@ func testAllChannels(notify bool) error { } go func() { for _, channel := range channels { + isChannelEnabled := channel.Status == common.ChannelStatusEnabled tik := time.Now() err, openaiErr := testChannel(channel, *testRequest) tok := time.Now() milliseconds := tok.Sub(tik).Milliseconds() - if milliseconds > disableThreshold { + if isChannelEnabled && milliseconds > disableThreshold { err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0)) disableChannel(channel.Id, channel.Name, err.Error()) } - if shouldDisableChannel(openaiErr, -1) { + if isChannelEnabled && shouldDisableChannel(openaiErr, -1) { disableChannel(channel.Id, channel.Name, err.Error()) } - if shouldEnableChannel(channel.Status, err, openaiErr, -1) { + if !isChannelEnabled && shouldEnableChannel(err, openaiErr) { enableChannel(channel.Id, channel.Name) } channel.UpdateResponseTime(milliseconds) @@ -212,58 +214,6 @@ func testAllChannels(notify bool) error { testAllChannelsLock.Lock() testAllChannelsRunning = false testAllChannelsLock.Unlock() - if notify { - err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到启用/禁用通知,说明所有通道都正常") - if err != nil { - common.SysError(fmt.Sprintf("failed to send email: %s", err.Error())) - } - } - }() - return nil -} - -func testAllEnableChannels(notify bool) error { - if common.RootUserEmail == "" { - common.RootUserEmail = model.GetRootUserEmail() - } - testAllChannelsLock.Lock() - if testAllChannelsRunning { - testAllChannelsLock.Unlock() - return errors.New("测试已在运行中") - } - testAllChannelsRunning = true - testAllChannelsLock.Unlock() - channels, err := model.GetAllChannels(0, 0, true) - if err != nil { - return err - } - testRequest := buildTestRequest() - var disableThreshold = int64(common.ChannelDisableThreshold * 1000) - if disableThreshold == 0 { - disableThreshold = 10000000 // a impossible value - } - go func() { - for _, channel := range channels { - if channel.Status != common.ChannelStatusEnabled { - continue - } - tik := time.Now() - err, openaiErr := testChannel(channel, *testRequest) - tok := time.Now() - milliseconds := tok.Sub(tik).Milliseconds() - if milliseconds > disableThreshold { - err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0)) - disableChannel(channel.Id, channel.Name, err.Error()) - } - if shouldDisableChannel(openaiErr, -1) { - disableChannel(channel.Id, channel.Name, err.Error()) - } - channel.UpdateResponseTime(milliseconds) - time.Sleep(common.RequestInterval) - } - testAllChannelsLock.Lock() - testAllChannelsRunning = false - testAllChannelsLock.Unlock() if notify { err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常") if err != nil { @@ -290,22 +240,6 @@ func TestAllChannels(c *gin.Context) { return } -func TestAllEnableChannels(c *gin.Context) { - err := testAllEnableChannels(true) - if err != nil { - c.JSON(http.StatusOK, gin.H{ - "success": false, - "message": err.Error(), - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "success": true, - "message": "", - }) - return -} - func AutomaticallyTestChannels(frequency int) { for { time.Sleep(time.Duration(frequency) * time.Minute) diff --git a/controller/relay-utils.go b/controller/relay-utils.go index 1abac6a2..5b172d35 100644 --- a/controller/relay-utils.go +++ b/controller/relay-utils.go @@ -145,19 +145,13 @@ func shouldDisableChannel(err *OpenAIError, statusCode int) bool { return false } -func shouldEnableChannel(status int, err error, openAIErr *OpenAIError, statusCode int) bool { - if status == common.ChannelStatusEnabled { - return false - } +func shouldEnableChannel(err error, openAIErr *OpenAIError) bool { if !common.AutomaticEnableChannelEnabled { return false } if err != nil { return false } - if statusCode == http.StatusUnauthorized { - return false - } if openAIErr != nil { return false } diff --git a/router/api-router.go b/router/api-router.go index d5450329..da3f9e61 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -68,8 +68,7 @@ func SetApiRouter(router *gin.Engine) { channelRoute.GET("/search", controller.SearchChannels) channelRoute.GET("/models", controller.ListModels) channelRoute.GET("/:id", controller.GetChannel) - channelRoute.GET("/testAll", controller.TestAllChannels) - channelRoute.GET("/test", controller.TestAllEnableChannels) + channelRoute.GET("/test", controller.TestAllChannels) channelRoute.GET("/test/:id", controller.TestChannel) channelRoute.GET("/update_balance", controller.UpdateAllChannelsBalance) channelRoute.GET("/update_balance/:id", controller.UpdateChannelBalance) diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index 64c40f7f..5d68e2da 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -231,20 +231,10 @@ const ChannelsTable = () => { }; const testAllChannels = async () => { - const res = await API.get(`/api/channel/testAll`); - const { success, message } = res.data; - if (success) { - showInfo('已成功开始测试所有通道,请刷新页面查看结果。'); - } else { - showError(message); - } - }; - - const testAllEnableChannels = async () => { const res = await API.get(`/api/channel/test`); const { success, message } = res.data; if (success) { - showInfo('已成功开始测试所有已启用通道,请刷新页面查看结果。'); + showInfo('已成功开始测试所有通道,请刷新页面查看结果。'); } else { showError(message); } @@ -533,9 +523,6 @@ const ChannelsTable = () => { 添加新的渠道 -