2024-02-29 01:08:18 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"one-api/common"
|
2025-02-18 15:59:17 +08:00
|
|
|
|
"one-api/dto"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
"one-api/model"
|
2025-02-13 16:39:17 +08:00
|
|
|
|
"one-api/setting"
|
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) {
|
2024-06-15 01:06:23 +08:00
|
|
|
|
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)
|
2025-02-18 15:59:17 +08:00
|
|
|
|
NotifyRootUser(subject, content, dto.NotifyTypeChannelUpdate)
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func EnableChannel(channelId int, channelName string) {
|
2024-06-15 01:06:23 +08:00
|
|
|
|
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)
|
2025-02-18 15:59:17 +08:00
|
|
|
|
NotifyRootUser(subject, content, dto.NotifyTypeChannelUpdate)
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-18 15:59:17 +08:00
|
|
|
|
func ShouldDisableChannel(channelType int, err *dto.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-10-08 23:15:57 +08:00
|
|
|
|
|
2025-02-13 16:39:17 +08:00
|
|
|
|
lowerMessage := strings.ToLower(err.Error.Message)
|
|
|
|
|
|
search, _ := AcSearch(lowerMessage, setting.AutomaticDisableKeywords, true)
|
|
|
|
|
|
if search {
|
2024-10-08 23:15:57 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-18 15:59:17 +08:00
|
|
|
|
func ShouldEnableChannel(err error, openaiWithStatusErr *dto.OpenAIErrorWithStatusCode, status int) bool {
|
2024-02-29 01:08:18 +08:00
|
|
|
|
if !common.AutomaticEnableChannelEnabled {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2024-07-14 01:21:05 +08:00
|
|
|
|
if openaiWithStatusErr != 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
|
|
|
|
|
|
}
|