mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-29 14:46:38 +08:00
122 lines
2.7 KiB
Go
122 lines
2.7 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"one-api/common"
|
||
"one-api/common/notify"
|
||
"one-api/model"
|
||
"one-api/types"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
func shouldEnableChannel(err error, openAIErr *types.OpenAIError) bool {
|
||
if !common.AutomaticEnableChannelEnabled {
|
||
return false
|
||
}
|
||
if err != nil {
|
||
return false
|
||
}
|
||
if openAIErr != nil {
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
func ShouldDisableChannel(err *types.OpenAIError, statusCode int) bool {
|
||
if !common.AutomaticDisableChannelEnabled {
|
||
return false
|
||
}
|
||
|
||
if err == nil {
|
||
return false
|
||
}
|
||
|
||
if statusCode == http.StatusUnauthorized {
|
||
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 err.Code == "invalid_api_key" || err.Code == "account_deactivated" {
|
||
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.") {
|
||
return true
|
||
}
|
||
|
||
if strings.Contains(err.Message, "credit") {
|
||
return true
|
||
}
|
||
if strings.Contains(err.Message, "balance") {
|
||
return true
|
||
}
|
||
|
||
if strings.Contains(err.Message, "Access denied") {
|
||
return true
|
||
}
|
||
return false
|
||
|
||
}
|
||
|
||
// disable & notify
|
||
func DisableChannel(channelId int, channelName string, reason string, sendNotify bool) {
|
||
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
||
if !sendNotify {
|
||
return
|
||
}
|
||
|
||
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
||
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
||
notify.Send(subject, content)
|
||
}
|
||
|
||
// enable & notify
|
||
func EnableChannel(channelId int, channelName string, sendNotify bool) {
|
||
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
|
||
if !sendNotify {
|
||
return
|
||
}
|
||
|
||
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||
notify.Send(subject, content)
|
||
}
|
||
|
||
func RelayNotImplemented(c *gin.Context) {
|
||
err := types.OpenAIError{
|
||
Message: "API not implemented",
|
||
Type: "one_api_error",
|
||
Param: "",
|
||
Code: "api_not_implemented",
|
||
}
|
||
c.JSON(http.StatusNotImplemented, gin.H{
|
||
"error": err,
|
||
})
|
||
}
|
||
|
||
func RelayNotFound(c *gin.Context) {
|
||
err := types.OpenAIError{
|
||
Message: fmt.Sprintf("Invalid URL (%s %s)", c.Request.Method, c.Request.URL.Path),
|
||
Type: "invalid_request_error",
|
||
Param: "",
|
||
Code: "",
|
||
}
|
||
c.JSON(http.StatusNotFound, gin.H{
|
||
"error": err,
|
||
})
|
||
}
|