new-api/service/channel.go

103 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"
relaymodel "one-api/dto"
"one-api/model"
2024-04-04 16:35:44 +08:00
"strings"
2024-02-29 01:08:18 +08:00
)
// disable & notify
func DisableChannel(channelId int, channelName string, reason string) {
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
2024-02-29 01:08:18 +08:00
subject := fmt.Sprintf("通道「%s」#%d已被禁用", channelName, channelId)
content := fmt.Sprintf("通道「%s」#%d已被禁用原因%s", channelName, channelId, reason)
notifyRootUser(subject, content)
}
func EnableChannel(channelId int, channelName string) {
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
2024-02-29 01:08:18 +08:00
subject := fmt.Sprintf("通道「%s」#%d已被启用", channelName, channelId)
content := fmt.Sprintf("通道「%s」#%d已被启用", channelName, channelId)
notifyRootUser(subject, content)
}
2024-07-04 22:46:33 +08:00
func ShouldDisableChannel(channelType int, err *relaymodel.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-06-18 11:03:24 +08:00
if strings.HasPrefix(err.Error.Message, "Your credit balance is too low") { // anthropic
2024-04-04 16:35:44 +08:00
return true
2024-06-18 11:03:24 +08:00
} else if strings.HasPrefix(err.Error.Message, "This organization has been disabled.") {
2024-02-29 01:08:18 +08:00
return true
2024-06-18 11:03:24 +08:00
} else if strings.HasPrefix(err.Error.Message, "You exceeded your current quota") {
return true
2024-06-18 11:03:24 +08:00
} else if strings.HasPrefix(err.Error.Message, "Permission denied") {
2024-06-16 15:45:41 +08:00
return true
2024-02-29 01:08:18 +08:00
}
2024-10-08 23:15:57 +08:00
if strings.Contains(err.Error.Message, "The security token included in the request is invalid") { // anthropic
return true
} else if strings.Contains(err.Error.Message, "Operation not allowed") {
return true
} else if strings.Contains(err.Error.Message, "Your account is not authorized") {
return true
}
2024-02-29 01:08:18 +08:00
return false
}
func ShouldEnableChannel(err error, openaiWithStatusErr *relaymodel.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
}