Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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