feature_service.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. from enum import StrEnum
  2. from pydantic import BaseModel, ConfigDict
  3. from configs import dify_config
  4. from services.billing_service import BillingService
  5. from services.enterprise.enterprise_service import EnterpriseService
  6. class SubscriptionModel(BaseModel):
  7. plan: str = "sandbox"
  8. interval: str = ""
  9. class BillingModel(BaseModel):
  10. enabled: bool = False
  11. subscription: SubscriptionModel = SubscriptionModel()
  12. class LimitationModel(BaseModel):
  13. size: int = 0
  14. limit: int = 0
  15. class LicenseStatus(StrEnum):
  16. NONE = "none"
  17. INACTIVE = "inactive"
  18. ACTIVE = "active"
  19. EXPIRING = "expiring"
  20. EXPIRED = "expired"
  21. LOST = "lost"
  22. class LicenseModel(BaseModel):
  23. status: LicenseStatus = LicenseStatus.NONE
  24. expired_at: str = ""
  25. class FeatureModel(BaseModel):
  26. billing: BillingModel = BillingModel()
  27. members: LimitationModel = LimitationModel(size=0, limit=1)
  28. apps: LimitationModel = LimitationModel(size=0, limit=10)
  29. vector_space: LimitationModel = LimitationModel(size=0, limit=5)
  30. knowledge_rate_limit: int = 10
  31. annotation_quota_limit: LimitationModel = LimitationModel(size=0, limit=10)
  32. documents_upload_quota: LimitationModel = LimitationModel(size=0, limit=50)
  33. docs_processing: str = "standard"
  34. can_replace_logo: bool = False
  35. model_load_balancing_enabled: bool = False
  36. dataset_operator_enabled: bool = False
  37. # pydantic configs
  38. model_config = ConfigDict(protected_namespaces=())
  39. class KnowledgeRateLimitModel(BaseModel):
  40. enabled: bool = False
  41. limit: int = 10
  42. subscription_plan: str = ""
  43. class SystemFeatureModel(BaseModel):
  44. sso_enforced_for_signin: bool = False
  45. sso_enforced_for_signin_protocol: str = ""
  46. sso_enforced_for_web: bool = False
  47. sso_enforced_for_web_protocol: str = ""
  48. enable_web_sso_switch_component: bool = False
  49. enable_marketplace: bool = False
  50. max_plugin_package_size: int = dify_config.PLUGIN_MAX_PACKAGE_SIZE
  51. enable_email_code_login: bool = False
  52. enable_email_password_login: bool = True
  53. enable_social_oauth_login: bool = False
  54. is_allow_register: bool = False
  55. is_allow_create_workspace: bool = False
  56. is_email_setup: bool = False
  57. license: LicenseModel = LicenseModel()
  58. class FeatureService:
  59. @classmethod
  60. def get_features(cls, tenant_id: str) -> FeatureModel:
  61. features = FeatureModel()
  62. cls._fulfill_params_from_env(features)
  63. if dify_config.BILLING_ENABLED and tenant_id:
  64. cls._fulfill_params_from_billing_api(features, tenant_id)
  65. return features
  66. @classmethod
  67. def get_knowledge_rate_limit(cls, tenant_id: str):
  68. knowledge_rate_limit = KnowledgeRateLimitModel()
  69. if dify_config.BILLING_ENABLED and tenant_id:
  70. knowledge_rate_limit.enabled = True
  71. limit_info = BillingService.get_knowledge_rate_limit(tenant_id)
  72. knowledge_rate_limit.limit = limit_info.get("limit", 10)
  73. knowledge_rate_limit.subscription_plan = limit_info.get("subscription_plan", "sandbox")
  74. return knowledge_rate_limit
  75. @classmethod
  76. def get_system_features(cls) -> SystemFeatureModel:
  77. system_features = SystemFeatureModel()
  78. cls._fulfill_system_params_from_env(system_features)
  79. if dify_config.ENTERPRISE_ENABLED:
  80. system_features.enable_web_sso_switch_component = True
  81. cls._fulfill_params_from_enterprise(system_features)
  82. if dify_config.MARKETPLACE_ENABLED:
  83. system_features.enable_marketplace = True
  84. return system_features
  85. @classmethod
  86. def _fulfill_system_params_from_env(cls, system_features: SystemFeatureModel):
  87. system_features.enable_email_code_login = dify_config.ENABLE_EMAIL_CODE_LOGIN
  88. system_features.enable_email_password_login = dify_config.ENABLE_EMAIL_PASSWORD_LOGIN
  89. system_features.enable_social_oauth_login = dify_config.ENABLE_SOCIAL_OAUTH_LOGIN
  90. system_features.is_allow_register = dify_config.ALLOW_REGISTER
  91. # system_features.is_allow_create_workspace = dify_config.ALLOW_CREATE_WORKSPACE
  92. system_features.is_allow_create_workspace = False
  93. system_features.is_email_setup = dify_config.MAIL_TYPE is not None and dify_config.MAIL_TYPE != ""
  94. @classmethod
  95. def _fulfill_params_from_env(cls, features: FeatureModel):
  96. features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
  97. features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
  98. features.dataset_operator_enabled = dify_config.DATASET_OPERATOR_ENABLED
  99. @classmethod
  100. def _fulfill_params_from_billing_api(cls, features: FeatureModel, tenant_id: str):
  101. billing_info = BillingService.get_info(tenant_id)
  102. features.billing.enabled = billing_info["enabled"]
  103. features.billing.subscription.plan = billing_info["subscription"]["plan"]
  104. features.billing.subscription.interval = billing_info["subscription"]["interval"]
  105. if "members" in billing_info:
  106. features.members.size = billing_info["members"]["size"]
  107. features.members.limit = billing_info["members"]["limit"]
  108. if "apps" in billing_info:
  109. features.apps.size = billing_info["apps"]["size"]
  110. features.apps.limit = billing_info["apps"]["limit"]
  111. if "vector_space" in billing_info:
  112. features.vector_space.size = billing_info["vector_space"]["size"]
  113. features.vector_space.limit = billing_info["vector_space"]["limit"]
  114. if "documents_upload_quota" in billing_info:
  115. features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
  116. features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
  117. if "annotation_quota_limit" in billing_info:
  118. features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
  119. features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
  120. if "docs_processing" in billing_info:
  121. features.docs_processing = billing_info["docs_processing"]
  122. if "can_replace_logo" in billing_info:
  123. features.can_replace_logo = billing_info["can_replace_logo"]
  124. if "model_load_balancing_enabled" in billing_info:
  125. features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
  126. if "knowledge_rate_limit" in billing_info:
  127. features.knowledge_rate_limit = billing_info["knowledge_rate_limit"]["limit"]
  128. @classmethod
  129. def _fulfill_params_from_enterprise(cls, features):
  130. enterprise_info = EnterpriseService.get_info()
  131. if "sso_enforced_for_signin" in enterprise_info:
  132. features.sso_enforced_for_signin = enterprise_info["sso_enforced_for_signin"]
  133. if "sso_enforced_for_signin_protocol" in enterprise_info:
  134. features.sso_enforced_for_signin_protocol = enterprise_info["sso_enforced_for_signin_protocol"]
  135. if "sso_enforced_for_web" in enterprise_info:
  136. features.sso_enforced_for_web = enterprise_info["sso_enforced_for_web"]
  137. if "sso_enforced_for_web_protocol" in enterprise_info:
  138. features.sso_enforced_for_web_protocol = enterprise_info["sso_enforced_for_web_protocol"]
  139. if "enable_email_code_login" in enterprise_info:
  140. features.enable_email_code_login = enterprise_info["enable_email_code_login"]
  141. if "enable_email_password_login" in enterprise_info:
  142. features.enable_email_password_login = enterprise_info["enable_email_password_login"]
  143. if "is_allow_register" in enterprise_info:
  144. features.is_allow_register = enterprise_info["is_allow_register"]
  145. if "is_allow_create_workspace" in enterprise_info:
  146. features.is_allow_create_workspace = enterprise_info["is_allow_create_workspace"]
  147. if "license" in enterprise_info:
  148. license_info = enterprise_info["license"]
  149. if "status" in license_info:
  150. features.license.status = LicenseStatus(license_info.get("status", LicenseStatus.INACTIVE))
  151. if "expired_at" in license_info:
  152. features.license.expired_at = license_info["expired_at"]