channel.go 2.7 KB

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