channel.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package service
  2. import (
  3. "fmt"
  4. "net/http"
  5. "one-api/common"
  6. relaymodel "one-api/dto"
  7. "one-api/model"
  8. )
  9. // disable & notify
  10. func DisableChannel(channelId int, channelName string, reason string) {
  11. model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
  12. subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
  13. content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
  14. notifyRootUser(subject, content)
  15. }
  16. func EnableChannel(channelId int, channelName string) {
  17. model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
  18. subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  19. content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
  20. notifyRootUser(subject, content)
  21. }
  22. func ShouldDisableChannel(err *relaymodel.OpenAIError, statusCode int) bool {
  23. if !common.AutomaticDisableChannelEnabled {
  24. return false
  25. }
  26. if err == nil {
  27. return false
  28. }
  29. if statusCode == http.StatusUnauthorized {
  30. return true
  31. }
  32. if err.Type == "insufficient_quota" || err.Code == "invalid_api_key" || err.Code == "account_deactivated" || err.Code == "billing_not_active" {
  33. return true
  34. }
  35. return false
  36. }
  37. func ShouldEnableChannel(err error, openAIErr *relaymodel.OpenAIError) bool {
  38. if !common.AutomaticEnableChannelEnabled {
  39. return false
  40. }
  41. if err != nil {
  42. return false
  43. }
  44. if openAIErr != nil {
  45. return false
  46. }
  47. return true
  48. }