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)
|
|
|
|
|
|
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)
|
|
|
|
|
|
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
|
|
|
|
|
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
|
|
|
|
|
notifyRootUser(subject, content)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ShouldDisableChannel(err *relaymodel.OpenAIError, statusCode int) bool {
|
|
|
|
|
|
if !common.AutomaticDisableChannelEnabled {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if statusCode == http.StatusUnauthorized {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2024-04-04 16:35:44 +08:00
|
|
|
|
switch err.Code {
|
|
|
|
|
|
case "invalid_api_key":
|
|
|
|
|
|
return true
|
|
|
|
|
|
case "account_deactivated":
|
|
|
|
|
|
return true
|
|
|
|
|
|
case "billing_not_active":
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
switch err.Type {
|
|
|
|
|
|
case "insufficient_quota":
|
|
|
|
|
|
return true
|
|
|
|
|
|
// https://docs.anthropic.com/claude/reference/errors
|
|
|
|
|
|
case "authentication_error":
|
|
|
|
|
|
return true
|
|
|
|
|
|
case "permission_error":
|
|
|
|
|
|
return true
|
|
|
|
|
|
case "forbidden":
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
if strings.HasPrefix(err.Message, "Your credit balance is too low") { // anthropic
|
|
|
|
|
|
return true
|
|
|
|
|
|
} else if strings.HasPrefix(err.Message, "This organization has been disabled.") {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return true
|
2024-04-07 22:22:27 +08:00
|
|
|
|
} else if strings.HasPrefix(err.Message, "You exceeded your current quota") {
|
|
|
|
|
|
return true
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-12 19:29:25 +08:00
|
|
|
|
func ShouldEnableChannel(err error, openAIErr *relaymodel.OpenAIError, status int) bool {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
if !common.AutomaticEnableChannelEnabled {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if openAIErr != nil {
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|