feature_service.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from pydantic import BaseModel, ConfigDict
  2. from configs import dify_config
  3. from services.billing_service import BillingService
  4. from services.enterprise.enterprise_service import EnterpriseService
  5. class SubscriptionModel(BaseModel):
  6. plan: str = "sandbox"
  7. interval: str = ""
  8. class BillingModel(BaseModel):
  9. enabled: bool = False
  10. subscription: SubscriptionModel = SubscriptionModel()
  11. class LimitationModel(BaseModel):
  12. size: int = 0
  13. limit: int = 0
  14. class FeatureModel(BaseModel):
  15. billing: BillingModel = BillingModel()
  16. members: LimitationModel = LimitationModel(size=0, limit=1)
  17. apps: LimitationModel = LimitationModel(size=0, limit=10)
  18. vector_space: LimitationModel = LimitationModel(size=0, limit=5)
  19. annotation_quota_limit: LimitationModel = LimitationModel(size=0, limit=10)
  20. documents_upload_quota: LimitationModel = LimitationModel(size=0, limit=50)
  21. docs_processing: str = "standard"
  22. can_replace_logo: bool = False
  23. model_load_balancing_enabled: bool = False
  24. dataset_operator_enabled: bool = False
  25. # pydantic configs
  26. model_config = ConfigDict(protected_namespaces=())
  27. class SystemFeatureModel(BaseModel):
  28. sso_enforced_for_signin: bool = False
  29. sso_enforced_for_signin_protocol: str = ""
  30. sso_enforced_for_web: bool = False
  31. sso_enforced_for_web_protocol: str = ""
  32. enable_web_sso_switch_component: bool = False
  33. enable_email_code_login: bool = False
  34. enable_email_password_login: bool = True
  35. enable_social_oauth_login: bool = False
  36. is_allow_register: bool = False
  37. is_allow_create_workspace: bool = False
  38. class FeatureService:
  39. @classmethod
  40. def get_features(cls, tenant_id: str) -> FeatureModel:
  41. features = FeatureModel()
  42. cls._fulfill_params_from_env(features)
  43. if dify_config.BILLING_ENABLED:
  44. cls._fulfill_params_from_billing_api(features, tenant_id)
  45. return features
  46. @classmethod
  47. def get_system_features(cls) -> SystemFeatureModel:
  48. system_features = SystemFeatureModel()
  49. cls._fulfill_system_params_from_env(system_features)
  50. if dify_config.ENTERPRISE_ENABLED:
  51. system_features.enable_web_sso_switch_component = True
  52. cls._fulfill_params_from_enterprise(system_features)
  53. return system_features
  54. @classmethod
  55. def _fulfill_system_params_from_env(cls, system_features: SystemFeatureModel):
  56. system_features.enable_email_code_login = dify_config.ENABLE_EMAIL_CODE_LOGIN
  57. system_features.enable_email_password_login = dify_config.ENABLE_EMAIL_PASSWORD_LOGIN
  58. system_features.enable_social_oauth_login = dify_config.ENABLE_SOCIAL_OAUTH_LOGIN
  59. system_features.is_allow_register = dify_config.ALLOW_REGISTER
  60. system_features.is_allow_create_workspace = dify_config.ALLOW_CREATE_WORKSPACE
  61. @classmethod
  62. def _fulfill_params_from_env(cls, features: FeatureModel):
  63. features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
  64. features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
  65. features.dataset_operator_enabled = dify_config.DATASET_OPERATOR_ENABLED
  66. @classmethod
  67. def _fulfill_params_from_billing_api(cls, features: FeatureModel, tenant_id: str):
  68. billing_info = BillingService.get_info(tenant_id)
  69. features.billing.enabled = billing_info["enabled"]
  70. features.billing.subscription.plan = billing_info["subscription"]["plan"]
  71. features.billing.subscription.interval = billing_info["subscription"]["interval"]
  72. if "members" in billing_info:
  73. features.members.size = billing_info["members"]["size"]
  74. features.members.limit = billing_info["members"]["limit"]
  75. if "apps" in billing_info:
  76. features.apps.size = billing_info["apps"]["size"]
  77. features.apps.limit = billing_info["apps"]["limit"]
  78. if "vector_space" in billing_info:
  79. features.vector_space.size = billing_info["vector_space"]["size"]
  80. features.vector_space.limit = billing_info["vector_space"]["limit"]
  81. if "documents_upload_quota" in billing_info:
  82. features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
  83. features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
  84. if "annotation_quota_limit" in billing_info:
  85. features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
  86. features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
  87. if "docs_processing" in billing_info:
  88. features.docs_processing = billing_info["docs_processing"]
  89. if "can_replace_logo" in billing_info:
  90. features.can_replace_logo = billing_info["can_replace_logo"]
  91. if "model_load_balancing_enabled" in billing_info:
  92. features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
  93. @classmethod
  94. def _fulfill_params_from_enterprise(cls, features):
  95. enterprise_info = EnterpriseService.get_info()
  96. if "sso_enforced_for_signin" in enterprise_info:
  97. features.sso_enforced_for_signin = enterprise_info["sso_enforced_for_signin"]
  98. if "sso_enforced_for_signin_protocol" in enterprise_info:
  99. features.sso_enforced_for_signin_protocol = enterprise_info["sso_enforced_for_signin_protocol"]
  100. if "sso_enforced_for_web" in enterprise_info:
  101. features.sso_enforced_for_web = enterprise_info["sso_enforced_for_web"]
  102. if "sso_enforced_for_web_protocol" in enterprise_info:
  103. features.sso_enforced_for_web_protocol = enterprise_info["sso_enforced_for_web_protocol"]
  104. if "enable_email_code_login" in enterprise_info:
  105. features.enable_email_code_login = enterprise_info["enable_email_code_login"]
  106. if "enable_email_password_login" in enterprise_info:
  107. features.enable_email_password_login = enterprise_info["enable_email_password_login"]
  108. if "is_allow_register" in enterprise_info:
  109. features.is_allow_register = enterprise_info["is_allow_register"]
  110. if "is_allow_create_workspace" in enterprise_info:
  111. features.is_allow_create_workspace = enterprise_info["is_allow_create_workspace"]