Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
400be9a2
authored
Jun 18, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: auto ban
parent
e5225230
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
10 deletions
+18
-10
controller/channel-test.go
+6
-1
controller/relay.go
+1
-1
service/channel.go
+11
-8
No files found.
controller/channel-test.go
View file @
400be9a2
...
...
@@ -219,7 +219,12 @@ func testAllChannels(notify bool) error {
if
channel
.
AutoBan
!=
nil
&&
*
channel
.
AutoBan
==
0
{
ban
=
false
}
if
isChannelEnabled
&&
service
.
ShouldDisableChannel
(
openaiErr
,
-
1
)
&&
ban
{
openAiErrWithStatus
:=
dto
.
OpenAIErrorWithStatusCode
{
StatusCode
:
-
1
,
Error
:
*
openaiErr
,
LocalError
:
false
,
}
if
isChannelEnabled
&&
service
.
ShouldDisableChannel
(
&
openAiErrWithStatus
)
&&
ban
{
service
.
DisableChannel
(
channel
.
Id
,
channel
.
Name
,
err
.
Error
())
}
if
!
isChannelEnabled
&&
service
.
ShouldEnableChannel
(
err
,
openaiErr
,
channel
.
Status
)
{
...
...
controller/relay.go
View file @
400be9a2
...
...
@@ -128,7 +128,7 @@ func shouldRetry(c *gin.Context, channelId int, openaiErr *dto.OpenAIErrorWithSt
func
processChannelError
(
c
*
gin
.
Context
,
channelId
int
,
err
*
dto
.
OpenAIErrorWithStatusCode
)
{
autoBan
:=
c
.
GetBool
(
"auto_ban"
)
common
.
LogError
(
c
.
Request
.
Context
(),
fmt
.
Sprintf
(
"relay error (channel #%d, status code: %d): %s"
,
channelId
,
err
.
StatusCode
,
err
.
Error
.
Message
))
if
service
.
ShouldDisableChannel
(
&
err
.
Error
,
err
.
StatusCode
)
&&
autoBan
{
if
service
.
ShouldDisableChannel
(
err
)
&&
autoBan
{
channelName
:=
c
.
GetString
(
"channel_name"
)
service
.
DisableChannel
(
channelId
,
channelName
,
err
.
Error
.
Message
)
}
...
...
service/channel.go
View file @
400be9a2
...
...
@@ -24,17 +24,20 @@ func EnableChannel(channelId int, channelName string) {
notifyRootUser
(
subject
,
content
)
}
func
ShouldDisableChannel
(
err
*
relaymodel
.
OpenAIError
,
statusCode
int
)
bool
{
func
ShouldDisableChannel
(
err
*
relaymodel
.
OpenAIError
WithStatusCode
)
bool
{
if
!
common
.
AutomaticDisableChannelEnabled
{
return
false
}
if
err
==
nil
{
return
false
}
if
statusCode
==
http
.
StatusUnauthorized
||
statusCode
==
http
.
StatusForbidden
{
if
err
.
LocalError
{
return
false
}
if
err
.
StatusCode
==
http
.
StatusUnauthorized
||
err
.
StatusCode
==
http
.
StatusForbidden
{
return
true
}
switch
err
.
Code
{
switch
err
.
Error
.
Code
{
case
"invalid_api_key"
:
return
true
case
"account_deactivated"
:
...
...
@@ -42,7 +45,7 @@ func ShouldDisableChannel(err *relaymodel.OpenAIError, statusCode int) bool {
case
"billing_not_active"
:
return
true
}
switch
err
.
Type
{
switch
err
.
Error
.
Type
{
case
"insufficient_quota"
:
return
true
// https://docs.anthropic.com/claude/reference/errors
...
...
@@ -53,13 +56,13 @@ func ShouldDisableChannel(err *relaymodel.OpenAIError, statusCode int) bool {
case
"forbidden"
:
return
true
}
if
strings
.
HasPrefix
(
err
.
Message
,
"Your credit balance is too low"
)
{
// anthropic
if
strings
.
HasPrefix
(
err
.
Error
.
Message
,
"Your credit balance is too low"
)
{
// anthropic
return
true
}
else
if
strings
.
HasPrefix
(
err
.
Message
,
"This organization has been disabled."
)
{
}
else
if
strings
.
HasPrefix
(
err
.
Error
.
Message
,
"This organization has been disabled."
)
{
return
true
}
else
if
strings
.
HasPrefix
(
err
.
Message
,
"You exceeded your current quota"
)
{
}
else
if
strings
.
HasPrefix
(
err
.
Error
.
Message
,
"You exceeded your current quota"
)
{
return
true
}
else
if
strings
.
HasPrefix
(
err
.
Message
,
"Permission denied"
)
{
}
else
if
strings
.
HasPrefix
(
err
.
Error
.
Message
,
"Permission denied"
)
{
return
true
}
return
false
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment