channel.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "strings"
  11. )
  12. func formatNotifyType(channelId int, status int) string {
  13. return fmt.Sprintf("%s_%d_%d", dto.NotifyTypeChannelUpdate, channelId, status)
  14. }
  15. // disable & notify
  16. func DisableChannel(channelId int, channelName string, reason string) {
  17. success := model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
  18. if success {
  19. subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
  20. content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
  21. NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusAutoDisabled), subject, content)
  22. }
  23. }
  24. func EnableChannel(channelId int, channelName string) {
  25. success := model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
  26. if success {
  27. subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  28. content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  29. NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusEnabled), subject, content)
  30. }
  31. }
  32. func ShouldDisableChannel(channelType int, err *dto.OpenAIErrorWithStatusCode) bool {
  33. if !common.AutomaticDisableChannelEnabled {
  34. return false
  35. }
  36. if err == nil {
  37. return false
  38. }
  39. if err.LocalError {
  40. return false
  41. }
  42. if err.StatusCode == http.StatusUnauthorized {
  43. return true
  44. }
  45. if err.StatusCode == http.StatusForbidden {
  46. switch channelType {
  47. case constant.ChannelTypeGemini:
  48. return true
  49. }
  50. }
  51. switch err.Error.Code {
  52. case "invalid_api_key":
  53. return true
  54. case "account_deactivated":
  55. return true
  56. case "billing_not_active":
  57. return true
  58. case "pre_consume_token_quota_failed":
  59. return true
  60. }
  61. switch err.Error.Type {
  62. case "insufficient_quota":
  63. return true
  64. case "insufficient_user_quota":
  65. return true
  66. // https://docs.anthropic.com/claude/reference/errors
  67. case "authentication_error":
  68. return true
  69. case "permission_error":
  70. return true
  71. case "forbidden":
  72. return true
  73. }
  74. lowerMessage := strings.ToLower(err.Error.Message)
  75. search, _ := AcSearch(lowerMessage, operation_setting.AutomaticDisableKeywords, true)
  76. if search {
  77. return true
  78. }
  79. return false
  80. }
  81. func ShouldEnableChannel(err error, openaiWithStatusErr *dto.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. }