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.

преди 11 месеца
преди 1 година
преди 11 месеца
преди 9 месеца
преди 9 месеца
преди 1 година
преди 1 година
преди 1 година
преди 11 месеца
преди 1 година
преди 9 месеца
преди 1 година
преди 1 година
преди 1 година
преди 9 месеца
преди 1 година
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
преди 11 месеца
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. class SystemFeatureModel(BaseModel):
  43. sso_enforced_for_signin: bool = False
  44. sso_enforced_for_signin_protocol: str = ""
  45. sso_enforced_for_web: bool = False
  46. sso_enforced_for_web_protocol: str = ""
  47. enable_web_sso_switch_component: bool = False
  48. enable_email_code_login: bool = False
  49. enable_email_password_login: bool = True
  50. enable_social_oauth_login: bool = False
  51. is_allow_register: bool = False
  52. is_allow_create_workspace: bool = False
  53. is_email_setup: bool = False
  54. license: LicenseModel = LicenseModel()
  55. class FeatureService:
  56. @classmethod
  57. def get_features(cls, tenant_id: str) -> FeatureModel:
  58. features = FeatureModel()
  59. cls._fulfill_params_from_env(features)
  60. if dify_config.BILLING_ENABLED and tenant_id:
  61. cls._fulfill_params_from_billing_api(features, tenant_id)
  62. return features
  63. @classmethod
  64. def get_knowledge_rate_limit(cls, tenant_id: str):
  65. knowledge_rate_limit = KnowledgeRateLimitModel()
  66. if dify_config.BILLING_ENABLED and tenant_id:
  67. knowledge_rate_limit.enabled = True
  68. knowledge_rate_limit.limit = BillingService.get_knowledge_rate_limit(tenant_id)
  69. return knowledge_rate_limit
  70. @classmethod
  71. def get_system_features(cls) -> SystemFeatureModel:
  72. system_features = SystemFeatureModel()
  73. cls._fulfill_system_params_from_env(system_features)
  74. if dify_config.ENTERPRISE_ENABLED:
  75. system_features.enable_web_sso_switch_component = True
  76. cls._fulfill_params_from_enterprise(system_features)
  77. return system_features
  78. @classmethod
  79. def _fulfill_system_params_from_env(cls, system_features: SystemFeatureModel):
  80. system_features.enable_email_code_login = dify_config.ENABLE_EMAIL_CODE_LOGIN
  81. system_features.enable_email_password_login = dify_config.ENABLE_EMAIL_PASSWORD_LOGIN
  82. system_features.enable_social_oauth_login = dify_config.ENABLE_SOCIAL_OAUTH_LOGIN
  83. system_features.is_allow_register = dify_config.ALLOW_REGISTER
  84. system_features.is_allow_create_workspace = dify_config.ALLOW_CREATE_WORKSPACE
  85. system_features.is_email_setup = dify_config.MAIL_TYPE is not None and dify_config.MAIL_TYPE != ""
  86. @classmethod
  87. def _fulfill_params_from_env(cls, features: FeatureModel):
  88. features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
  89. features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
  90. features.dataset_operator_enabled = dify_config.DATASET_OPERATOR_ENABLED
  91. @classmethod
  92. def _fulfill_params_from_billing_api(cls, features: FeatureModel, tenant_id: str):
  93. billing_info = BillingService.get_info(tenant_id)
  94. features.billing.enabled = billing_info["enabled"]
  95. features.billing.subscription.plan = billing_info["subscription"]["plan"]
  96. features.billing.subscription.interval = billing_info["subscription"]["interval"]
  97. if "members" in billing_info:
  98. features.members.size = billing_info["members"]["size"]
  99. features.members.limit = billing_info["members"]["limit"]
  100. if "apps" in billing_info:
  101. features.apps.size = billing_info["apps"]["size"]
  102. features.apps.limit = billing_info["apps"]["limit"]
  103. if "vector_space" in billing_info:
  104. features.vector_space.size = billing_info["vector_space"]["size"]
  105. features.vector_space.limit = billing_info["vector_space"]["limit"]
  106. if "documents_upload_quota" in billing_info:
  107. features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
  108. features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
  109. if "annotation_quota_limit" in billing_info:
  110. features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
  111. features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
  112. if "docs_processing" in billing_info:
  113. features.docs_processing = billing_info["docs_processing"]
  114. if "can_replace_logo" in billing_info:
  115. features.can_replace_logo = billing_info["can_replace_logo"]
  116. if "model_load_balancing_enabled" in billing_info:
  117. features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
  118. if "knowledge_rate_limit" in billing_info:
  119. features.knowledge_rate_limit = billing_info["knowledge_rate_limit"]["limit"]
  120. @classmethod
  121. def _fulfill_params_from_enterprise(cls, features):
  122. enterprise_info = EnterpriseService.get_info()
  123. if "sso_enforced_for_signin" in enterprise_info:
  124. features.sso_enforced_for_signin = enterprise_info["sso_enforced_for_signin"]
  125. if "sso_enforced_for_signin_protocol" in enterprise_info:
  126. features.sso_enforced_for_signin_protocol = enterprise_info["sso_enforced_for_signin_protocol"]
  127. if "sso_enforced_for_web" in enterprise_info:
  128. features.sso_enforced_for_web = enterprise_info["sso_enforced_for_web"]
  129. if "sso_enforced_for_web_protocol" in enterprise_info:
  130. features.sso_enforced_for_web_protocol = enterprise_info["sso_enforced_for_web_protocol"]
  131. if "enable_email_code_login" in enterprise_info:
  132. features.enable_email_code_login = enterprise_info["enable_email_code_login"]
  133. if "enable_email_password_login" in enterprise_info:
  134. features.enable_email_password_login = enterprise_info["enable_email_password_login"]
  135. if "is_allow_register" in enterprise_info:
  136. features.is_allow_register = enterprise_info["is_allow_register"]
  137. if "is_allow_create_workspace" in enterprise_info:
  138. features.is_allow_create_workspace = enterprise_info["is_allow_create_workspace"]
  139. if "license" in enterprise_info:
  140. license_info = enterprise_info["license"]
  141. if "status" in license_info:
  142. features.license.status = LicenseStatus(license_info.get("status", LicenseStatus.INACTIVE))
  143. if "expired_at" in license_info:
  144. features.license.expired_at = license_info["expired_at"]