2024-04-05 17:50:12 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-09-21 14:31:53 +00:00
|
|
|
|
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
2024-04-05 17:50:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ShouldDisableChannel(err *model.Error, statusCode int) bool {
|
|
|
|
if !config.AutomaticDisableChannelEnabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if statusCode == http.StatusUnauthorized {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch err.Type {
|
2024-09-21 14:31:53 +00:00
|
|
|
case "insufficient_quota", "authentication_error", "permission_error", "forbidden":
|
2024-04-05 17:50:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err.Code == "invalid_api_key" || err.Code == "account_deactivated" {
|
|
|
|
return true
|
|
|
|
}
|
2024-09-21 14:31:53 +00:00
|
|
|
|
|
|
|
lowerMessage := strings.ToLower(err.Message)
|
|
|
|
if strings.Contains(lowerMessage, "your access was terminated") ||
|
|
|
|
strings.Contains(lowerMessage, "violation of our policies") ||
|
|
|
|
strings.Contains(lowerMessage, "your credit balance is too low") ||
|
|
|
|
strings.Contains(lowerMessage, "organization has been disabled") ||
|
|
|
|
strings.Contains(lowerMessage, "credit") ||
|
|
|
|
strings.Contains(lowerMessage, "balance") ||
|
|
|
|
strings.Contains(lowerMessage, "permission denied") ||
|
|
|
|
strings.Contains(lowerMessage, "organization has been restricted") || // groq
|
|
|
|
strings.Contains(lowerMessage, "已欠费") {
|
2024-09-21 14:12:09 +00:00
|
|
|
return true
|
|
|
|
}
|
2024-04-05 17:50:12 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShouldEnableChannel(err error, openAIErr *model.Error) bool {
|
|
|
|
if !config.AutomaticEnableChannelEnabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if openAIErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|