Files
new-api/service/channel.go

101 lines
2.5 KiB
Go
Raw Normal View History

2024-02-29 01:08:18 +08:00
package service
import (
"fmt"
"net/http"
"one-api/common"
"one-api/dto"
2024-02-29 01:08:18 +08:00
"one-api/model"
"one-api/setting/operation_setting"
2024-04-04 16:35:44 +08:00
"strings"
2024-02-29 01:08:18 +08:00
)
func formatNotifyType(channelId int, status int) string {
return fmt.Sprintf("%s_%d_%d", dto.NotifyTypeChannelUpdate, channelId, status)
}
2024-02-29 01:08:18 +08:00
// disable & notify
func DisableChannel(channelId int, channelName string, reason string) {
success := model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
if success {
subject := fmt.Sprintf("通道「%s」#%d已被禁用", channelName, channelId)
content := fmt.Sprintf("通道「%s」#%d已被禁用原因%s", channelName, channelId, reason)
NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusAutoDisabled), subject, content)
}
2024-02-29 01:08:18 +08:00
}
func EnableChannel(channelId int, channelName string) {
success := model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
if success {
subject := fmt.Sprintf("通道「%s」#%d已被启用", channelName, channelId)
content := fmt.Sprintf("通道「%s」#%d已被启用", channelName, channelId)
NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusEnabled), subject, content)
}
2024-02-29 01:08:18 +08:00
}
func ShouldDisableChannel(channelType int, err *dto.OpenAIErrorWithStatusCode) bool {
2024-02-29 01:08:18 +08:00
if !common.AutomaticDisableChannelEnabled {
return false
}
if err == nil {
return false
}
2024-06-18 11:03:24 +08:00
if err.LocalError {
return false
}
2024-07-04 22:46:33 +08:00
if err.StatusCode == http.StatusUnauthorized {
2024-02-29 01:08:18 +08:00
return true
}
2024-07-04 22:46:33 +08:00
if err.StatusCode == http.StatusForbidden {
switch channelType {
case common.ChannelTypeGemini:
return true
}
}
2024-06-18 11:03:24 +08:00
switch err.Error.Code {
2024-04-04 16:35:44 +08:00
case "invalid_api_key":
return true
case "account_deactivated":
return true
case "billing_not_active":
return true
}
2024-06-18 11:03:24 +08:00
switch err.Error.Type {
2024-04-04 16:35:44 +08:00
case "insufficient_quota":
return true
2024-08-24 17:23:24 +08:00
case "insufficient_user_quota":
return true
2024-04-04 16:35:44 +08:00
// https://docs.anthropic.com/claude/reference/errors
case "authentication_error":
return true
case "permission_error":
return true
case "forbidden":
return true
}
2024-10-08 23:15:57 +08:00
lowerMessage := strings.ToLower(err.Error.Message)
search, _ := AcSearch(lowerMessage, operation_setting.AutomaticDisableKeywords, true)
if search {
2024-10-08 23:15:57 +08:00
return true
}
2024-02-29 01:08:18 +08:00
return false
}
func ShouldEnableChannel(err error, openaiWithStatusErr *dto.OpenAIErrorWithStatusCode, status int) bool {
2024-02-29 01:08:18 +08:00
if !common.AutomaticEnableChannelEnabled {
return false
}
if err != nil {
return false
}
if openaiWithStatusErr != nil {
2024-02-29 01:08:18 +08:00
return false
}
if status != common.ChannelStatusAutoDisabled {
return false
}
2024-02-29 01:08:18 +08:00
return true
}