new-api/service/channel.go

102 lines
2.7 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/constant"
"one-api/dto"
2024-02-29 01:08:18 +08:00
"one-api/model"
"one-api/setting/operation_setting"
"one-api/types"
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(channelError types.ChannelError, reason string) {
success := model.UpdateChannelStatus(channelError.ChannelId, channelError.UsingKey, common.ChannelStatusAutoDisabled, reason)
if success {
subject := fmt.Sprintf("通道「%s」#%d已被禁用", channelError.ChannelName, channelError.ChannelId)
content := fmt.Sprintf("通道「%s」#%d已被禁用原因%s", channelError.ChannelName, channelError.ChannelId, reason)
NotifyRootUser(formatNotifyType(channelError.ChannelId, common.ChannelStatusAutoDisabled), subject, content)
}
2024-02-29 01:08:18 +08:00
}
func EnableChannel(channelId int, usingKey string, channelName string) {
success := model.UpdateChannelStatus(channelId, usingKey, 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 *types.NewAPIError) bool {
2024-02-29 01:08:18 +08:00
if !common.AutomaticDisableChannelEnabled {
return false
}
if err == nil {
return false
}
if types.IsChannelError(err) {
return true
}
if types.IsLocalError(err) {
2024-06-18 11:03:24 +08:00
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 constant.ChannelTypeGemini:
2024-07-04 22:46:33 +08:00
return true
}
}
oaiErr := err.ToOpenAIError()
switch oaiErr.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
case "pre_consume_token_quota_failed":
return true
2024-04-04 16:35:44 +08:00
}
switch oaiErr.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())
search, _ := AcSearch(lowerMessage, operation_setting.AutomaticDisableKeywords, true)
return search
2024-02-29 01:08:18 +08:00
}
func ShouldEnableChannel(newAPIError *types.NewAPIError, status int) bool {
2024-02-29 01:08:18 +08:00
if !common.AutomaticEnableChannelEnabled {
return false
}
if newAPIError != 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
}