You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

feature_service.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. from enum import StrEnum
  2. from pydantic import BaseModel, ConfigDict, Field
  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 EducationModel(BaseModel):
  13. enabled: bool = False
  14. activated: bool = False
  15. class LimitationModel(BaseModel):
  16. size: int = 0
  17. limit: int = 0
  18. class LicenseLimitationModel(BaseModel):
  19. """
  20. - enabled: whether this limit is enforced
  21. - size: current usage count
  22. - limit: maximum allowed count; 0 means unlimited
  23. """
  24. enabled: bool = Field(False, description="Whether this limit is currently active")
  25. size: int = Field(0, description="Number of resources already consumed")
  26. limit: int = Field(0, description="Maximum number of resources allowed; 0 means no limit")
  27. def is_available(self, required: int = 1) -> bool:
  28. """
  29. Determine whether the requested amount can be allocated.
  30. Returns True if:
  31. - this limit is not active, or
  32. - the limit is zero (unlimited), or
  33. - there is enough remaining quota.
  34. """
  35. if not self.enabled or self.limit == 0:
  36. return True
  37. return (self.limit - self.size) >= required
  38. class LicenseStatus(StrEnum):
  39. NONE = "none"
  40. INACTIVE = "inactive"
  41. ACTIVE = "active"
  42. EXPIRING = "expiring"
  43. EXPIRED = "expired"
  44. LOST = "lost"
  45. class LicenseModel(BaseModel):
  46. status: LicenseStatus = LicenseStatus.NONE
  47. expired_at: str = ""
  48. workspaces: LicenseLimitationModel = LicenseLimitationModel(enabled=False, size=0, limit=0)
  49. class BrandingModel(BaseModel):
  50. enabled: bool = False
  51. application_title: str = ""
  52. login_page_logo: str = ""
  53. workspace_logo: str = ""
  54. favicon: str = ""
  55. class WebAppAuthSSOModel(BaseModel):
  56. protocol: str = ""
  57. class WebAppAuthModel(BaseModel):
  58. enabled: bool = False
  59. allow_sso: bool = False
  60. sso_config: WebAppAuthSSOModel = WebAppAuthSSOModel()
  61. allow_email_code_login: bool = False
  62. allow_email_password_login: bool = False
  63. class FeatureModel(BaseModel):
  64. billing: BillingModel = BillingModel()
  65. education: EducationModel = EducationModel()
  66. members: LimitationModel = LimitationModel(size=0, limit=1)
  67. apps: LimitationModel = LimitationModel(size=0, limit=10)
  68. vector_space: LimitationModel = LimitationModel(size=0, limit=5)
  69. knowledge_rate_limit: int = 10
  70. annotation_quota_limit: LimitationModel = LimitationModel(size=0, limit=10)
  71. documents_upload_quota: LimitationModel = LimitationModel(size=0, limit=50)
  72. docs_processing: str = "standard"
  73. can_replace_logo: bool = False
  74. model_load_balancing_enabled: bool = False
  75. dataset_operator_enabled: bool = False
  76. webapp_copyright_enabled: bool = False
  77. workspace_members: LicenseLimitationModel = LicenseLimitationModel(enabled=False, size=0, limit=0)
  78. # pydantic configs
  79. model_config = ConfigDict(protected_namespaces=())
  80. class KnowledgeRateLimitModel(BaseModel):
  81. enabled: bool = False
  82. limit: int = 10
  83. subscription_plan: str = ""
  84. class SystemFeatureModel(BaseModel):
  85. sso_enforced_for_signin: bool = False
  86. sso_enforced_for_signin_protocol: str = ""
  87. enable_marketplace: bool = False
  88. max_plugin_package_size: int = dify_config.PLUGIN_MAX_PACKAGE_SIZE
  89. enable_email_code_login: bool = False
  90. enable_email_password_login: bool = True
  91. enable_social_oauth_login: bool = False
  92. is_allow_register: bool = False
  93. is_allow_create_workspace: bool = False
  94. is_email_setup: bool = False
  95. license: LicenseModel = LicenseModel()
  96. branding: BrandingModel = BrandingModel()
  97. webapp_auth: WebAppAuthModel = WebAppAuthModel()
  98. class FeatureService:
  99. @classmethod
  100. def get_features(cls, tenant_id: str) -> FeatureModel:
  101. features = FeatureModel()
  102. cls._fulfill_params_from_env(features)
  103. if dify_config.BILLING_ENABLED and tenant_id:
  104. cls._fulfill_params_from_billing_api(features, tenant_id)
  105. if dify_config.ENTERPRISE_ENABLED:
  106. features.webapp_copyright_enabled = True
  107. cls._fulfill_params_from_workspace_info(features, tenant_id)
  108. return features
  109. @classmethod
  110. def get_knowledge_rate_limit(cls, tenant_id: str):
  111. knowledge_rate_limit = KnowledgeRateLimitModel()
  112. if dify_config.BILLING_ENABLED and tenant_id:
  113. knowledge_rate_limit.enabled = True
  114. limit_info = BillingService.get_knowledge_rate_limit(tenant_id)
  115. knowledge_rate_limit.limit = limit_info.get("limit", 10)
  116. knowledge_rate_limit.subscription_plan = limit_info.get("subscription_plan", "sandbox")
  117. return knowledge_rate_limit
  118. @classmethod
  119. def get_system_features(cls) -> SystemFeatureModel:
  120. system_features = SystemFeatureModel()
  121. cls._fulfill_system_params_from_env(system_features)
  122. if dify_config.ENTERPRISE_ENABLED:
  123. system_features.branding.enabled = True
  124. system_features.webapp_auth.enabled = True
  125. cls._fulfill_params_from_enterprise(system_features)
  126. if dify_config.MARKETPLACE_ENABLED:
  127. system_features.enable_marketplace = True
  128. return system_features
  129. @classmethod
  130. def _fulfill_system_params_from_env(cls, system_features: SystemFeatureModel):
  131. system_features.enable_email_code_login = dify_config.ENABLE_EMAIL_CODE_LOGIN
  132. system_features.enable_email_password_login = dify_config.ENABLE_EMAIL_PASSWORD_LOGIN
  133. system_features.enable_social_oauth_login = dify_config.ENABLE_SOCIAL_OAUTH_LOGIN
  134. system_features.is_allow_register = dify_config.ALLOW_REGISTER
  135. system_features.is_allow_create_workspace = dify_config.ALLOW_CREATE_WORKSPACE
  136. system_features.is_email_setup = dify_config.MAIL_TYPE is not None and dify_config.MAIL_TYPE != ""
  137. @classmethod
  138. def _fulfill_params_from_env(cls, features: FeatureModel):
  139. features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
  140. features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
  141. features.dataset_operator_enabled = dify_config.DATASET_OPERATOR_ENABLED
  142. features.education.enabled = dify_config.EDUCATION_ENABLED
  143. @classmethod
  144. def _fulfill_params_from_workspace_info(cls, features: FeatureModel, tenant_id: str):
  145. workspace_info = EnterpriseService.get_workspace_info(tenant_id)
  146. if "WorkspaceMembers" in workspace_info:
  147. features.workspace_members.size = workspace_info["WorkspaceMembers"]["used"]
  148. features.workspace_members.limit = workspace_info["WorkspaceMembers"]["limit"]
  149. features.workspace_members.enabled = workspace_info["WorkspaceMembers"]["enabled"]
  150. @classmethod
  151. def _fulfill_params_from_billing_api(cls, features: FeatureModel, tenant_id: str):
  152. billing_info = BillingService.get_info(tenant_id)
  153. features.billing.enabled = billing_info["enabled"]
  154. features.billing.subscription.plan = billing_info["subscription"]["plan"]
  155. features.billing.subscription.interval = billing_info["subscription"]["interval"]
  156. features.education.activated = billing_info["subscription"].get("education", False)
  157. if features.billing.subscription.plan != "sandbox":
  158. features.webapp_copyright_enabled = True
  159. if "members" in billing_info:
  160. features.members.size = billing_info["members"]["size"]
  161. features.members.limit = billing_info["members"]["limit"]
  162. if "apps" in billing_info:
  163. features.apps.size = billing_info["apps"]["size"]
  164. features.apps.limit = billing_info["apps"]["limit"]
  165. if "vector_space" in billing_info:
  166. features.vector_space.size = billing_info["vector_space"]["size"]
  167. features.vector_space.limit = billing_info["vector_space"]["limit"]
  168. if "documents_upload_quota" in billing_info:
  169. features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
  170. features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
  171. if "annotation_quota_limit" in billing_info:
  172. features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
  173. features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
  174. if "docs_processing" in billing_info:
  175. features.docs_processing = billing_info["docs_processing"]
  176. if "can_replace_logo" in billing_info:
  177. features.can_replace_logo = billing_info["can_replace_logo"]
  178. if "model_load_balancing_enabled" in billing_info:
  179. features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
  180. if "knowledge_rate_limit" in billing_info:
  181. features.knowledge_rate_limit = billing_info["knowledge_rate_limit"]["limit"]
  182. @classmethod
  183. def _fulfill_params_from_enterprise(cls, features: SystemFeatureModel):
  184. enterprise_info = EnterpriseService.get_info()
  185. if "SSOEnforcedForSignin" in enterprise_info:
  186. features.sso_enforced_for_signin = enterprise_info["SSOEnforcedForSignin"]
  187. if "SSOEnforcedForSigninProtocol" in enterprise_info:
  188. features.sso_enforced_for_signin_protocol = enterprise_info["SSOEnforcedForSigninProtocol"]
  189. if "EnableEmailCodeLogin" in enterprise_info:
  190. features.enable_email_code_login = enterprise_info["EnableEmailCodeLogin"]
  191. if "EnableEmailPasswordLogin" in enterprise_info:
  192. features.enable_email_password_login = enterprise_info["EnableEmailPasswordLogin"]
  193. if "IsAllowRegister" in enterprise_info:
  194. features.is_allow_register = enterprise_info["IsAllowRegister"]
  195. if "IsAllowCreateWorkspace" in enterprise_info:
  196. features.is_allow_create_workspace = enterprise_info["IsAllowCreateWorkspace"]
  197. if "Branding" in enterprise_info:
  198. features.branding.application_title = enterprise_info["Branding"].get("applicationTitle", "")
  199. features.branding.login_page_logo = enterprise_info["Branding"].get("loginPageLogo", "")
  200. features.branding.workspace_logo = enterprise_info["Branding"].get("workspaceLogo", "")
  201. features.branding.favicon = enterprise_info["Branding"].get("favicon", "")
  202. if "WebAppAuth" in enterprise_info:
  203. features.webapp_auth.allow_sso = enterprise_info["WebAppAuth"].get("allowSso", False)
  204. features.webapp_auth.allow_email_code_login = enterprise_info["WebAppAuth"].get(
  205. "allowEmailCodeLogin", False
  206. )
  207. features.webapp_auth.allow_email_password_login = enterprise_info["WebAppAuth"].get(
  208. "allowEmailPasswordLogin", False
  209. )
  210. features.webapp_auth.sso_config.protocol = enterprise_info.get("SSOEnforcedForWebProtocol", "")
  211. if "License" in enterprise_info:
  212. license_info = enterprise_info["License"]
  213. if "status" in license_info:
  214. features.license.status = LicenseStatus(license_info.get("status", LicenseStatus.INACTIVE))
  215. if "expiredAt" in license_info:
  216. features.license.expired_at = license_info["expiredAt"]
  217. if "workspaces" in license_info:
  218. features.license.workspaces.enabled = license_info["workspaces"]["enabled"]
  219. features.license.workspaces.limit = license_info["workspaces"]["limit"]
  220. features.license.workspaces.size = license_info["workspaces"]["used"]