channel.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package service
  2. import (
  3. "fmt"
  4. "net/http"
  5. "one-api/common"
  6. relaymodel "one-api/dto"
  7. "one-api/model"
  8. "strings"
  9. )
  10. // disable & notify
  11. func DisableChannel(channelId int, channelName string, reason string) {
  12. model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
  13. subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
  14. content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
  15. notifyRootUser(subject, content)
  16. }
  17. func EnableChannel(channelId int, channelName string) {
  18. model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
  19. subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  20. content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  21. notifyRootUser(subject, content)
  22. }
  23. func ShouldDisableChannel(channelType int, err *relaymodel.OpenAIErrorWithStatusCode) bool {
  24. if !common.AutomaticDisableChannelEnabled {
  25. return false
  26. }
  27. if err == nil {
  28. return false
  29. }
  30. if err.LocalError {
  31. return false
  32. }
  33. if err.StatusCode == http.StatusUnauthorized {
  34. return true
  35. }
  36. if err.StatusCode == http.StatusForbidden {
  37. switch channelType {
  38. case common.ChannelTypeGemini:
  39. return true
  40. }
  41. }
  42. switch err.Error.Code {
  43. case "invalid_api_key":
  44. return true
  45. case "account_deactivated":
  46. return true
  47. case "billing_not_active":
  48. return true
  49. }
  50. switch err.Error.Type {
  51. case "insufficient_quota":
  52. return true
  53. case "insufficient_user_quota":
  54. return true
  55. // https://docs.anthropic.com/claude/reference/errors
  56. case "authentication_error":
  57. return true
  58. case "permission_error":
  59. return true
  60. case "forbidden":
  61. return true
  62. }
  63. if strings.HasPrefix(err.Error.Message, "Your credit balance is too low") { // anthropic
  64. return true
  65. } else if strings.HasPrefix(err.Error.Message, "This organization has been disabled.") {
  66. return true
  67. } else if strings.HasPrefix(err.Error.Message, "You exceeded your current quota") {
  68. return true
  69. } else if strings.HasPrefix(err.Error.Message, "Permission denied") {
  70. return true
  71. }
  72. if strings.Contains(err.Error.Message, "The security token included in the request is invalid") { // anthropic
  73. return true
  74. } else if strings.Contains(err.Error.Message, "Operation not allowed") {
  75. return true
  76. } else if strings.Contains(err.Error.Message, "Your account is not authorized") {
  77. return true
  78. }
  79. return false
  80. }
  81. func ShouldEnableChannel(err error, openaiWithStatusErr *relaymodel.OpenAIErrorWithStatusCode, status int) bool {
  82. if !common.AutomaticEnableChannelEnabled {
  83. return false
  84. }
  85. if err != nil {
  86. return false
  87. }
  88. if openaiWithStatusErr != nil {
  89. return false
  90. }
  91. if status != common.ChannelStatusAutoDisabled {
  92. return false
  93. }
  94. return true
  95. }