channel.go 2.4 KB

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