Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

feature_service.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 PluginInstallationScope(StrEnum):
  64. NONE = "none"
  65. OFFICIAL_ONLY = "official_only"
  66. OFFICIAL_AND_SPECIFIC_PARTNERS = "official_and_specific_partners"
  67. ALL = "all"
  68. class PluginInstallationPermissionModel(BaseModel):
  69. # Plugin installation scope – possible values:
  70. # none: prohibit all plugin installations
  71. # official_only: allow only Dify official plugins
  72. # official_and_specific_partners: allow official and specific partner plugins
  73. # all: allow installation of all plugins
  74. plugin_installation_scope: PluginInstallationScope = PluginInstallationScope.ALL
  75. # If True, restrict plugin installation to the marketplace only
  76. # Equivalent to ForceEnablePluginVerification
  77. restrict_to_marketplace_only: bool = False
  78. class FeatureModel(BaseModel):
  79. billing: BillingModel = BillingModel()
  80. education: EducationModel = EducationModel()
  81. members: LimitationModel = LimitationModel(size=0, limit=1)
  82. apps: LimitationModel = LimitationModel(size=0, limit=10)
  83. vector_space: LimitationModel = LimitationModel(size=0, limit=5)
  84. knowledge_rate_limit: int = 10
  85. annotation_quota_limit: LimitationModel = LimitationModel(size=0, limit=10)
  86. documents_upload_quota: LimitationModel = LimitationModel(size=0, limit=50)
  87. docs_processing: str = "standard"
  88. can_replace_logo: bool = False
  89. model_load_balancing_enabled: bool = False
  90. dataset_operator_enabled: bool = False
  91. webapp_copyright_enabled: bool = False
  92. workspace_members: LicenseLimitationModel = LicenseLimitationModel(enabled=False, size=0, limit=0)
  93. # pydantic configs
  94. model_config = ConfigDict(protected_namespaces=())
  95. class KnowledgeRateLimitModel(BaseModel):
  96. enabled: bool = False
  97. limit: int = 10
  98. subscription_plan: str = ""
  99. class SystemFeatureModel(BaseModel):
  100. sso_enforced_for_signin: bool = False
  101. sso_enforced_for_signin_protocol: str = ""
  102. enable_marketplace: bool = False
  103. max_plugin_package_size: int = dify_config.PLUGIN_MAX_PACKAGE_SIZE
  104. enable_email_code_login: bool = False
  105. enable_email_password_login: bool = True
  106. enable_social_oauth_login: bool = False
  107. is_allow_register: bool = False
  108. is_allow_create_workspace: bool = False
  109. is_email_setup: bool = False
  110. license: LicenseModel = LicenseModel()
  111. branding: BrandingModel = BrandingModel()
  112. webapp_auth: WebAppAuthModel = WebAppAuthModel()
  113. plugin_installation_permission: PluginInstallationPermissionModel = PluginInstallationPermissionModel()
  114. class FeatureService:
  115. @classmethod
  116. def get_features(cls, tenant_id: str) -> FeatureModel:
  117. features = FeatureModel()
  118. cls._fulfill_params_from_env(features)
  119. if dify_config.BILLING_ENABLED and tenant_id:
  120. cls._fulfill_params_from_billing_api(features, tenant_id)
  121. if dify_config.ENTERPRISE_ENABLED:
  122. features.webapp_copyright_enabled = True
  123. cls._fulfill_params_from_workspace_info(features, tenant_id)
  124. return features
  125. @classmethod
  126. def get_knowledge_rate_limit(cls, tenant_id: str):
  127. knowledge_rate_limit = KnowledgeRateLimitModel()
  128. if dify_config.BILLING_ENABLED and tenant_id:
  129. knowledge_rate_limit.enabled = True
  130. limit_info = BillingService.get_knowledge_rate_limit(tenant_id)
  131. knowledge_rate_limit.limit = limit_info.get("limit", 10)
  132. knowledge_rate_limit.subscription_plan = limit_info.get("subscription_plan", "sandbox")
  133. return knowledge_rate_limit
  134. @classmethod
  135. def get_system_features(cls) -> SystemFeatureModel:
  136. system_features = SystemFeatureModel()
  137. cls._fulfill_system_params_from_env(system_features)
  138. if dify_config.ENTERPRISE_ENABLED:
  139. system_features.branding.enabled = True
  140. system_features.webapp_auth.enabled = True
  141. cls._fulfill_params_from_enterprise(system_features)
  142. if dify_config.MARKETPLACE_ENABLED:
  143. system_features.enable_marketplace = True
  144. return system_features
  145. @classmethod
  146. def _fulfill_system_params_from_env(cls, system_features: SystemFeatureModel):
  147. system_features.enable_email_code_login = dify_config.ENABLE_EMAIL_CODE_LOGIN
  148. system_features.enable_email_password_login = dify_config.ENABLE_EMAIL_PASSWORD_LOGIN
  149. system_features.enable_social_oauth_login = dify_config.ENABLE_SOCIAL_OAUTH_LOGIN
  150. system_features.is_allow_register = dify_config.ALLOW_REGISTER
  151. system_features.is_allow_create_workspace = dify_config.ALLOW_CREATE_WORKSPACE
  152. system_features.is_email_setup = dify_config.MAIL_TYPE is not None and dify_config.MAIL_TYPE != ""
  153. @classmethod
  154. def _fulfill_params_from_env(cls, features: FeatureModel):
  155. features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
  156. features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
  157. features.dataset_operator_enabled = dify_config.DATASET_OPERATOR_ENABLED
  158. features.education.enabled = dify_config.EDUCATION_ENABLED
  159. @classmethod
  160. def _fulfill_params_from_workspace_info(cls, features: FeatureModel, tenant_id: str):
  161. workspace_info = EnterpriseService.get_workspace_info(tenant_id)
  162. if "WorkspaceMembers" in workspace_info:
  163. features.workspace_members.size = workspace_info["WorkspaceMembers"]["used"]
  164. features.workspace_members.limit = workspace_info["WorkspaceMembers"]["limit"]
  165. features.workspace_members.enabled = workspace_info["WorkspaceMembers"]["enabled"]
  166. @classmethod
  167. def _fulfill_params_from_billing_api(cls, features: FeatureModel, tenant_id: str):
  168. billing_info = BillingService.get_info(tenant_id)
  169. features.billing.enabled = billing_info["enabled"]
  170. features.billing.subscription.plan = billing_info["subscription"]["plan"]
  171. features.billing.subscription.interval = billing_info["subscription"]["interval"]
  172. features.education.activated = billing_info["subscription"].get("education", False)
  173. if features.billing.subscription.plan != "sandbox":
  174. features.webapp_copyright_enabled = True
  175. if "members" in billing_info:
  176. features.members.size = billing_info["members"]["size"]
  177. features.members.limit = billing_info["members"]["limit"]
  178. if "apps" in billing_info:
  179. features.apps.size = billing_info["apps"]["size"]
  180. features.apps.limit = billing_info["apps"]["limit"]
  181. if "vector_space" in billing_info:
  182. features.vector_space.size = billing_info["vector_space"]["size"]
  183. features.vector_space.limit = billing_info["vector_space"]["limit"]
  184. if "documents_upload_quota" in billing_info:
  185. features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
  186. features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
  187. if "annotation_quota_limit" in billing_info:
  188. features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
  189. features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
  190. if "docs_processing" in billing_info:
  191. features.docs_processing = billing_info["docs_processing"]
  192. if "can_replace_logo" in billing_info:
  193. features.can_replace_logo = billing_info["can_replace_logo"]
  194. if "model_load_balancing_enabled" in billing_info:
  195. features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
  196. if "knowledge_rate_limit" in billing_info:
  197. features.knowledge_rate_limit = billing_info["knowledge_rate_limit"]["limit"]
  198. @classmethod
  199. def _fulfill_params_from_enterprise(cls, features: SystemFeatureModel):
  200. enterprise_info = EnterpriseService.get_info()
  201. if "SSOEnforcedForSignin" in enterprise_info:
  202. features.sso_enforced_for_signin = enterprise_info["SSOEnforcedForSignin"]
  203. if "SSOEnforcedForSigninProtocol" in enterprise_info:
  204. features.sso_enforced_for_signin_protocol = enterprise_info["SSOEnforcedForSigninProtocol"]
  205. if "EnableEmailCodeLogin" in enterprise_info:
  206. features.enable_email_code_login = enterprise_info["EnableEmailCodeLogin"]
  207. if "EnableEmailPasswordLogin" in enterprise_info:
  208. features.enable_email_password_login = enterprise_info["EnableEmailPasswordLogin"]
  209. if "IsAllowRegister" in enterprise_info:
  210. features.is_allow_register = enterprise_info["IsAllowRegister"]
  211. if "IsAllowCreateWorkspace" in enterprise_info:
  212. features.is_allow_create_workspace = enterprise_info["IsAllowCreateWorkspace"]
  213. if "Branding" in enterprise_info:
  214. features.branding.application_title = enterprise_info["Branding"].get("applicationTitle", "")
  215. features.branding.login_page_logo = enterprise_info["Branding"].get("loginPageLogo", "")
  216. features.branding.workspace_logo = enterprise_info["Branding"].get("workspaceLogo", "")
  217. features.branding.favicon = enterprise_info["Branding"].get("favicon", "")
  218. if "WebAppAuth" in enterprise_info:
  219. features.webapp_auth.allow_sso = enterprise_info["WebAppAuth"].get("allowSso", False)
  220. features.webapp_auth.allow_email_code_login = enterprise_info["WebAppAuth"].get(
  221. "allowEmailCodeLogin", False
  222. )
  223. features.webapp_auth.allow_email_password_login = enterprise_info["WebAppAuth"].get(
  224. "allowEmailPasswordLogin", False
  225. )
  226. features.webapp_auth.sso_config.protocol = enterprise_info.get("SSOEnforcedForWebProtocol", "")
  227. if "License" in enterprise_info:
  228. license_info = enterprise_info["License"]
  229. if "status" in license_info:
  230. features.license.status = LicenseStatus(license_info.get("status", LicenseStatus.INACTIVE))
  231. if "expiredAt" in license_info:
  232. features.license.expired_at = license_info["expiredAt"]
  233. if "workspaces" in license_info:
  234. features.license.workspaces.enabled = license_info["workspaces"]["enabled"]
  235. features.license.workspaces.limit = license_info["workspaces"]["limit"]
  236. features.license.workspaces.size = license_info["workspaces"]["used"]
  237. if "PluginInstallationPermission" in enterprise_info:
  238. plugin_installation_info = enterprise_info["PluginInstallationPermission"]
  239. features.plugin_installation_permission.plugin_installation_scope = plugin_installation_info[
  240. "pluginInstallationScope"
  241. ]
  242. features.plugin_installation_permission.restrict_to_marketplace_only = plugin_installation_info[
  243. "restrictToMarketplaceOnly"
  244. ]