channel.go 3.0 KB

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