mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-29 14:46:38 +08:00
* ✨ feat: channel support weight * 💄 improve: show version * 💄 improve: Channel add copy operation * 💄 improve: Channel support batch add
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"one-api/common"
|
||
"one-api/model"
|
||
"one-api/types"
|
||
|
||
"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
|
||
}
|
||
|
||
if err.Type == "insufficient_quota" || err.Code == "invalid_api_key" || err.Code == "account_deactivated" {
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
// 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 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,
|
||
})
|
||
}
|