channel.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 err.StatusCode == http.StatusUnauthorized {
  53. return true
  54. }
  55. if err.StatusCode == http.StatusForbidden {
  56. switch channelType {
  57. case constant.ChannelTypeGemini:
  58. return true
  59. }
  60. }
  61. oaiErr := err.ToOpenAIError()
  62. switch oaiErr.Code {
  63. case "invalid_api_key":
  64. return true
  65. case "account_deactivated":
  66. return true
  67. case "billing_not_active":
  68. return true
  69. case "pre_consume_token_quota_failed":
  70. return true
  71. case "Arrearage":
  72. return true
  73. }
  74. switch oaiErr.Type {
  75. case "insufficient_quota":
  76. return true
  77. case "insufficient_user_quota":
  78. return true
  79. // https://docs.anthropic.com/claude/reference/errors
  80. case "authentication_error":
  81. return true
  82. case "permission_error":
  83. return true
  84. case "forbidden":
  85. return true
  86. }
  87. lowerMessage := strings.ToLower(err.Error())
  88. search, _ := AcSearch(lowerMessage, operation_setting.AutomaticDisableKeywords, true)
  89. return search
  90. }
  91. func ShouldEnableChannel(newAPIError *types.NewAPIError, status int) bool {
  92. if !common.AutomaticEnableChannelEnabled {
  93. return false
  94. }
  95. if newAPIError != nil {
  96. return false
  97. }
  98. if status != common.ChannelStatusAutoDisabled {
  99. return false
  100. }
  101. return true
  102. }