channel.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package service
  2. import (
  3. "fmt"
  4. "net/http"
  5. "one-api/common"
  6. relaymodel "one-api/dto"
  7. "one-api/model"
  8. "strings"
  9. )
  10. // disable & notify
  11. func DisableChannel(channelId int, channelName string, reason string) {
  12. model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
  13. subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
  14. content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
  15. notifyRootUser(subject, content)
  16. }
  17. func EnableChannel(channelId int, channelName string) {
  18. model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
  19. subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  20. content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  21. notifyRootUser(subject, content)
  22. }
  23. func ShouldDisableChannel(err *relaymodel.OpenAIError, statusCode int) bool {
  24. if !common.AutomaticDisableChannelEnabled {
  25. return false
  26. }
  27. if err == nil {
  28. return false
  29. }
  30. if statusCode == http.StatusUnauthorized {
  31. return true
  32. }
  33. switch err.Code {
  34. case "invalid_api_key":
  35. return true
  36. case "account_deactivated":
  37. return true
  38. case "billing_not_active":
  39. return true
  40. }
  41. switch err.Type {
  42. case "insufficient_quota":
  43. return true
  44. // https://docs.anthropic.com/claude/reference/errors
  45. case "authentication_error":
  46. return true
  47. case "permission_error":
  48. return true
  49. case "forbidden":
  50. return true
  51. }
  52. if strings.HasPrefix(err.Message, "Your credit balance is too low") { // anthropic
  53. return true
  54. } else if strings.HasPrefix(err.Message, "This organization has been disabled.") {
  55. return true
  56. }
  57. return false
  58. }
  59. func ShouldEnableChannel(err error, openAIErr *relaymodel.OpenAIError) bool {
  60. if !common.AutomaticEnableChannelEnabled {
  61. return false
  62. }
  63. if err != nil {
  64. return false
  65. }
  66. if openAIErr != nil {
  67. return false
  68. }
  69. return true
  70. }