channel.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/model"
  10. "github.com/QuantumNous/new-api/setting/operation_setting"
  11. "github.com/QuantumNous/new-api/types"
  12. )
  13. func formatNotifyType(channelId int, status int) string {
  14. return fmt.Sprintf("%s_%d_%d", dto.NotifyTypeChannelUpdate, channelId, status)
  15. }
  16. // disable & notify
  17. func DisableChannel(channelError types.ChannelError, reason string) {
  18. common.SysLog(fmt.Sprintf("通道「%s」(#%d)发生错误,准备禁用,原因:%s", channelError.ChannelName, channelError.ChannelId, reason))
  19. // 检查是否启用自动禁用功能
  20. if !channelError.AutoBan {
  21. common.SysLog(fmt.Sprintf("通道「%s」(#%d)未启用自动禁用功能,跳过禁用操作", channelError.ChannelName, channelError.ChannelId))
  22. return
  23. }
  24. success := model.UpdateChannelStatus(channelError.ChannelId, channelError.UsingKey, common.ChannelStatusAutoDisabled, reason)
  25. if success {
  26. subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelError.ChannelName, channelError.ChannelId)
  27. content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelError.ChannelName, channelError.ChannelId, reason)
  28. NotifyRootUser(formatNotifyType(channelError.ChannelId, common.ChannelStatusAutoDisabled), subject, content)
  29. }
  30. }
  31. func EnableChannel(channelId int, usingKey string, channelName string) {
  32. success := model.UpdateChannelStatus(channelId, usingKey, common.ChannelStatusEnabled, "")
  33. if success {
  34. subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  35. content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  36. NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusEnabled), subject, content)
  37. }
  38. }
  39. func ShouldDisableChannel(channelType int, err *types.NewAPIError) bool {
  40. if !common.AutomaticDisableChannelEnabled {
  41. return false
  42. }
  43. if err == nil {
  44. return false
  45. }
  46. if types.IsChannelError(err) {
  47. return true
  48. }
  49. if types.IsSkipRetryError(err) {
  50. return false
  51. }
  52. if operation_setting.ShouldDisableByStatusCode(err.StatusCode) {
  53. return true
  54. }
  55. //if err.StatusCode == http.StatusUnauthorized {
  56. // return true
  57. //}
  58. if err.StatusCode == http.StatusForbidden {
  59. switch channelType {
  60. case constant.ChannelTypeGemini:
  61. return true
  62. }
  63. }
  64. oaiErr := err.ToOpenAIError()
  65. switch oaiErr.Code {
  66. case "invalid_api_key":
  67. return true
  68. case "account_deactivated":
  69. return true
  70. case "billing_not_active":
  71. return true
  72. case "pre_consume_token_quota_failed":
  73. return true
  74. case "Arrearage":
  75. return true
  76. }
  77. switch oaiErr.Type {
  78. case "insufficient_quota":
  79. return true
  80. case "insufficient_user_quota":
  81. return true
  82. // https://docs.anthropic.com/claude/reference/errors
  83. case "authentication_error":
  84. return true
  85. case "permission_error":
  86. return true
  87. case "forbidden":
  88. return true
  89. }
  90. lowerMessage := strings.ToLower(err.Error())
  91. search, _ := AcSearch(lowerMessage, operation_setting.AutomaticDisableKeywords, true)
  92. return search
  93. }
  94. func ShouldEnableChannel(newAPIError *types.NewAPIError, status int) bool {
  95. if !common.AutomaticEnableChannelEnabled {
  96. return false
  97. }
  98. if newAPIError != nil {
  99. return false
  100. }
  101. if status != common.ChannelStatusAutoDisabled {
  102. return false
  103. }
  104. return true
  105. }