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 6.8KB

11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
11 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from enum import Enum
  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(str, Enum):
  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. annotation_quota_limit: LimitationModel = LimitationModel(size=0, limit=10)
  31. documents_upload_quota: LimitationModel = LimitationModel(size=0, limit=50)
  32. docs_processing: str = "standard"
  33. can_replace_logo: bool = False
  34. model_load_balancing_enabled: bool = False
  35. dataset_operator_enabled: bool = False
  36. # pydantic configs
  37. model_config = ConfigDict(protected_namespaces=())
  38. class SystemFeatureModel(BaseModel):
  39. sso_enforced_for_signin: bool = False
  40. sso_enforced_for_signin_protocol: str = ""
  41. sso_enforced_for_web: bool = False
  42. sso_enforced_for_web_protocol: str = ""
  43. enable_web_sso_switch_component: bool = False
  44. enable_email_code_login: bool = False
  45. enable_email_password_login: bool = True
  46. enable_social_oauth_login: bool = False
  47. is_allow_register: bool = False
  48. is_allow_create_workspace: bool = False
  49. license: LicenseModel = LicenseModel()
  50. class FeatureService:
  51. @classmethod
  52. def get_features(cls, tenant_id: str) -> FeatureModel:
  53. features = FeatureModel()
  54. cls._fulfill_params_from_env(features)
  55. if dify_config.BILLING_ENABLED:
  56. cls._fulfill_params_from_billing_api(features, tenant_id)
  57. return features
  58. @classmethod
  59. def get_system_features(cls) -> SystemFeatureModel:
  60. system_features = SystemFeatureModel()
  61. cls._fulfill_system_params_from_env(system_features)
  62. if dify_config.ENTERPRISE_ENABLED:
  63. system_features.enable_web_sso_switch_component = True
  64. cls._fulfill_params_from_enterprise(system_features)
  65. return system_features
  66. @classmethod
  67. def _fulfill_system_params_from_env(cls, system_features: SystemFeatureModel):
  68. system_features.enable_email_code_login = dify_config.ENABLE_EMAIL_CODE_LOGIN
  69. system_features.enable_email_password_login = dify_config.ENABLE_EMAIL_PASSWORD_LOGIN
  70. system_features.enable_social_oauth_login = dify_config.ENABLE_SOCIAL_OAUTH_LOGIN
  71. system_features.is_allow_register = dify_config.ALLOW_REGISTER
  72. system_features.is_allow_create_workspace = dify_config.ALLOW_CREATE_WORKSPACE
  73. @classmethod
  74. def _fulfill_params_from_env(cls, features: FeatureModel):
  75. features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
  76. features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
  77. features.dataset_operator_enabled = dify_config.DATASET_OPERATOR_ENABLED
  78. @classmethod
  79. def _fulfill_params_from_billing_api(cls, features: FeatureModel, tenant_id: str):
  80. billing_info = BillingService.get_info(tenant_id)
  81. features.billing.enabled = billing_info["enabled"]
  82. features.billing.subscription.plan = billing_info["subscription"]["plan"]
  83. features.billing.subscription.interval = billing_info["subscription"]["interval"]
  84. if "members" in billing_info:
  85. features.members.size = billing_info["members"]["size"]
  86. features.members.limit = billing_info["members"]["limit"]
  87. if "apps" in billing_info:
  88. features.apps.size = billing_info["apps"]["size"]
  89. features.apps.limit = billing_info["apps"]["limit"]
  90. if "vector_space" in billing_info:
  91. features.vector_space.size = billing_info["vector_space"]["size"]
  92. features.vector_space.limit = billing_info["vector_space"]["limit"]
  93. if "documents_upload_quota" in billing_info:
  94. features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
  95. features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
  96. if "annotation_quota_limit" in billing_info:
  97. features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
  98. features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
  99. if "docs_processing" in billing_info:
  100. features.docs_processing = billing_info["docs_processing"]
  101. if "can_replace_logo" in billing_info:
  102. features.can_replace_logo = billing_info["can_replace_logo"]
  103. if "model_load_balancing_enabled" in billing_info:
  104. features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
  105. @classmethod
  106. def _fulfill_params_from_enterprise(cls, features):
  107. enterprise_info = EnterpriseService.get_info()
  108. if "sso_enforced_for_signin" in enterprise_info:
  109. features.sso_enforced_for_signin = enterprise_info["sso_enforced_for_signin"]
  110. if "sso_enforced_for_signin_protocol" in enterprise_info:
  111. features.sso_enforced_for_signin_protocol = enterprise_info["sso_enforced_for_signin_protocol"]
  112. if "sso_enforced_for_web" in enterprise_info:
  113. features.sso_enforced_for_web = enterprise_info["sso_enforced_for_web"]
  114. if "sso_enforced_for_web_protocol" in enterprise_info:
  115. features.sso_enforced_for_web_protocol = enterprise_info["sso_enforced_for_web_protocol"]
  116. if "enable_email_code_login" in enterprise_info:
  117. features.enable_email_code_login = enterprise_info["enable_email_code_login"]
  118. if "enable_email_password_login" in enterprise_info:
  119. features.enable_email_password_login = enterprise_info["enable_email_password_login"]
  120. if "is_allow_register" in enterprise_info:
  121. features.is_allow_register = enterprise_info["is_allow_register"]
  122. if "is_allow_create_workspace" in enterprise_info:
  123. features.is_allow_create_workspace = enterprise_info["is_allow_create_workspace"]
  124. if "license" in enterprise_info:
  125. if "status" in enterprise_info["license"]:
  126. features.license.status = enterprise_info["license"]["status"]
  127. if "expired_at" in enterprise_info["license"]:
  128. features.license.expired_at = enterprise_info["license"]["expired_at"]