2024-02-29 01:08:18 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"one-api/common"
|
2025-07-03 13:10:25 +08:00
|
|
|
|
"one-api/constant"
|
2025-02-18 15:59:17 +08:00
|
|
|
|
"one-api/dto"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
"one-api/model"
|
2025-03-04 18:52:08 +08:00
|
|
|
|
"one-api/setting/operation_setting"
|
2025-07-10 15:02:40 +08:00
|
|
|
|
"one-api/types"
|
2024-04-04 16:35:44 +08:00
|
|
|
|
"strings"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-03-06 17:46:03 +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
|
2025-07-10 17:49:53 +08:00
|
|
|
|
func DisableChannel(channelError types.ChannelError, reason string) {
|
|
|
|
|
|
success := model.UpdateChannelStatus(channelError.ChannelId, channelError.UsingKey, common.ChannelStatusAutoDisabled, reason)
|
2025-03-06 17:46:03 +08:00
|
|
|
|
if success {
|
2025-07-10 17:49:53 +08:00
|
|
|
|
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)
|
2025-03-06 17:46:03 +08:00
|
|
|
|
}
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-10 17:49:53 +08:00
|
|
|
|
func EnableChannel(channelId int, usingKey string, channelName string) {
|
|
|
|
|
|
success := model.UpdateChannelStatus(channelId, usingKey, common.ChannelStatusEnabled, "")
|
2025-03-06 17:46:03 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-10 15:02:40 +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
|
|
|
|
|
|
}
|
2025-07-10 15:02:40 +08:00
|
|
|
|
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 {
|
2025-07-03 13:10:25 +08:00
|
|
|
|
case constant.ChannelTypeGemini:
|
2024-07-04 22:46:33 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-10 15:02:40 +08:00
|
|
|
|
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
|
2025-06-17 16:46:52 +08:00
|
|
|
|
case "pre_consume_token_quota_failed":
|
|
|
|
|
|
return true
|
2024-04-04 16:35:44 +08:00
|
|
|
|
}
|
2025-07-10 15:02:40 +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
|
|
|
|
|
2025-07-10 15:02:40 +08:00
|
|
|
|
lowerMessage := strings.ToLower(err.Error())
|
2025-03-04 18:52:08 +08:00
|
|
|
|
search, _ := AcSearch(lowerMessage, operation_setting.AutomaticDisableKeywords, true)
|
2025-07-10 15:02:40 +08:00
|
|
|
|
return search
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-10 17:49:53 +08:00
|
|
|
|
func ShouldEnableChannel(newAPIError *types.NewAPIError, status int) bool {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
if !common.AutomaticEnableChannelEnabled {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2025-07-10 15:02:40 +08:00
|
|
|
|
if newAPIError != nil {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
2024-05-12 19:29:25 +08:00
|
|
|
|
if status != common.ChannelStatusAutoDisabled {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|