channel.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, reason)
  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.OpenAIErrorWithStatusCode) bool {
  24. if !common.AutomaticDisableChannelEnabled {
  25. return false
  26. }
  27. if err == nil {
  28. return false
  29. }
  30. if err.LocalError {
  31. return false
  32. }
  33. if err.StatusCode == http.StatusUnauthorized || err.StatusCode == http.StatusForbidden {
  34. return true
  35. }
  36. switch err.Error.Code {
  37. case "invalid_api_key":
  38. return true
  39. case "account_deactivated":
  40. return true
  41. case "billing_not_active":
  42. return true
  43. }
  44. switch err.Error.Type {
  45. case "insufficient_quota":
  46. return true
  47. // https://docs.anthropic.com/claude/reference/errors
  48. case "authentication_error":
  49. return true
  50. case "permission_error":
  51. return true
  52. case "forbidden":
  53. return true
  54. }
  55. if strings.HasPrefix(err.Error.Message, "Your credit balance is too low") { // anthropic
  56. return true
  57. } else if strings.HasPrefix(err.Error.Message, "This organization has been disabled.") {
  58. return true
  59. } else if strings.HasPrefix(err.Error.Message, "You exceeded your current quota") {
  60. return true
  61. } else if strings.HasPrefix(err.Error.Message, "Permission denied") {
  62. return true
  63. }
  64. return false
  65. }
  66. func ShouldEnableChannel(err error, openAIErr *relaymodel.OpenAIError, status int) bool {
  67. if !common.AutomaticEnableChannelEnabled {
  68. return false
  69. }
  70. if err != nil {
  71. return false
  72. }
  73. if openAIErr != nil {
  74. return false
  75. }
  76. if status != common.ChannelStatusAutoDisabled {
  77. return false
  78. }
  79. return true
  80. }