mirror of
https://github.com/langgenius/dify.git
synced 2026-11-30 00:28:01 +08:00
337 lines
16 KiB
Python
337 lines
16 KiB
Python
from configs import dify_config
|
|
from core.model_billing_profile import ModelBillingProfileService
|
|
from enums import CloudPlan, DeploymentEdition, HostedTrialProvider
|
|
from repositories.tokener_signup_canary_repository import signup_used_staging_package
|
|
from services.account_education_service import EDUCATION_EDITIONS
|
|
from services.billing_service import BillingInfo, BillingService
|
|
from services.enterprise.enterprise_service import EnterpriseService
|
|
from services.entities import feature_entities
|
|
from services.knowledge_fs.feature_service import get_knowledge_fs_features
|
|
|
|
|
|
class FeatureService:
|
|
@staticmethod
|
|
def is_im_centrally_managed() -> bool:
|
|
"""Enterprise centrally manages IM channels and default bindings."""
|
|
return dify_config.DEPLOYMENT_EDITION == DeploymentEdition.ENTERPRISE
|
|
|
|
@staticmethod
|
|
def use_tenant_scoped_email_config() -> bool:
|
|
"""Cloud uses tenant Email Channels instead of the global mail client."""
|
|
return dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD
|
|
|
|
@staticmethod
|
|
def is_agent_preview_enabled() -> bool:
|
|
"""Agent App preview runs are available on Cloud and Enterprise deployments only."""
|
|
return dify_config.DEPLOYMENT_EDITION in {DeploymentEdition.CLOUD, DeploymentEdition.ENTERPRISE}
|
|
|
|
@classmethod
|
|
def can_import_premium_site_settings(cls, tenant_id: str) -> bool:
|
|
"""Keep Cloud-only Web app customization out of free workspace imports."""
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return True
|
|
return cls._is_paid_cloud_workspace(tenant_id)
|
|
|
|
@classmethod
|
|
def get_workspace_plan(cls, tenant_id: str) -> CloudPlan | None:
|
|
"""Return the workspace's Cloud subscription plan, or ``None`` outside Cloud.
|
|
|
|
This is the single entry for reading one workspace's plan. Only Cloud has
|
|
subscription plans, so other editions return ``None`` without calling
|
|
billing; ``None`` is never a stand-in for Sandbox. On Cloud it makes one
|
|
billing subscription request. Batch jobs use
|
|
``BillingService.get_plan_bulk_with_cache`` instead.
|
|
|
|
Billing failures propagate unchanged, and each caller chooses what they
|
|
mean: permission checks reject the request (directly or as their own
|
|
unavailable error), while queue routing and other scheduling decisions
|
|
catch the failure and fall back to their degraded plan.
|
|
"""
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return None
|
|
|
|
return BillingService.get_cloud_plan(tenant_id)
|
|
|
|
@classmethod
|
|
def _is_paid_cloud_workspace(cls, tenant_id: str, plan: CloudPlan | None = None) -> bool:
|
|
"""Return whether a Cloud workspace has a paid plan, using ``plan`` when the caller already loaded it."""
|
|
workspace_plan = plan if plan is not None else cls.get_workspace_plan(tenant_id)
|
|
return workspace_plan is not None and workspace_plan.is_paid
|
|
|
|
@classmethod
|
|
def can_use_workflow_collaboration(cls, tenant_id: str, *, plan: CloudPlan | None = None) -> bool:
|
|
"""Return the workspace entitlement using its loaded plan, or propagate billing failures."""
|
|
if not dify_config.ENABLE_COLLABORATION_MODE:
|
|
return False
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return True
|
|
if not tenant_id:
|
|
return False
|
|
return cls._is_paid_cloud_workspace(tenant_id, plan)
|
|
|
|
@staticmethod
|
|
def is_evaluation_supported() -> bool:
|
|
"""Return whether this deployment ships Evaluation; Cloud workspaces still need a paid plan."""
|
|
return dify_config.DEPLOYMENT_EDITION in {DeploymentEdition.ENTERPRISE, DeploymentEdition.CLOUD}
|
|
|
|
@classmethod
|
|
def can_use_evaluation(cls, tenant_id: str, *, plan: CloudPlan | None = None) -> bool:
|
|
"""Return the workspace entitlement using its loaded plan, or propagate billing failures."""
|
|
if not cls.is_evaluation_supported():
|
|
return False
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return True
|
|
if not tenant_id:
|
|
return False
|
|
return cls._is_paid_cloud_workspace(tenant_id, plan)
|
|
|
|
@classmethod
|
|
def get_features(cls, tenant_id: str, exclude_vector_space: bool = False) -> feature_entities.FeatureModel:
|
|
features = feature_entities.FeatureModel()
|
|
features.im_centrally_managed = cls.is_im_centrally_managed()
|
|
features.agent_preview_enabled = cls.is_agent_preview_enabled()
|
|
if exclude_vector_space:
|
|
features.vector_space = None
|
|
|
|
cls._fulfill_params_from_env(features)
|
|
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
cls._lift_plan_quotas(features)
|
|
|
|
if dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD and tenant_id:
|
|
cls._fulfill_params_from_billing_api(
|
|
features,
|
|
tenant_id,
|
|
exclude_vector_space=exclude_vector_space,
|
|
)
|
|
|
|
if dify_config.DEPLOYMENT_EDITION == DeploymentEdition.ENTERPRISE:
|
|
features.webapp_copyright_enabled = True
|
|
features.knowledge_pipeline.publish_enabled = True
|
|
cls._fulfill_params_from_workspace_info(features, tenant_id)
|
|
|
|
features.human_input_email_delivery_enabled = cls._resolve_human_input_email_delivery_enabled(
|
|
features=features,
|
|
tenant_id=tenant_id,
|
|
)
|
|
plan = features.billing.subscription.plan
|
|
features.workflow_collaboration_enabled = cls.can_use_workflow_collaboration(tenant_id, plan=plan)
|
|
features.workflow_collaboration_upgrade_available = (
|
|
dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD
|
|
and plan == CloudPlan.SANDBOX
|
|
and cls.can_use_workflow_collaboration(tenant_id, plan=CloudPlan.PROFESSIONAL)
|
|
)
|
|
features.evaluation_enabled = cls.can_use_evaluation(tenant_id, plan=plan)
|
|
features.evaluation_upgrade_available = (
|
|
dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD
|
|
and plan == CloudPlan.SANDBOX
|
|
and cls.can_use_evaluation(tenant_id, plan=CloudPlan.PROFESSIONAL)
|
|
)
|
|
|
|
if tenant_id:
|
|
model_billing = ModelBillingProfileService.resolve(tenant_id)
|
|
features.model_billing_source = model_billing.model_billing_source.value
|
|
from core.model_invocation_routing import migration_display_status
|
|
|
|
features.model_billing_migration_status = migration_display_status(tenant_id)
|
|
|
|
return features
|
|
|
|
@classmethod
|
|
def get_vector_space(cls, tenant_id: str) -> feature_entities.VectorSpaceLimitationModel:
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return feature_entities.VectorSpaceLimitationModel(size=0, limit=-1)
|
|
|
|
vector_space = feature_entities.VectorSpaceLimitationModel(size=0, limit=5)
|
|
if dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD and tenant_id:
|
|
billing_vector_space = BillingService.get_vector_space(tenant_id)
|
|
# NOTE: billing API returns vector_space.size as float (e.g. 0.0),
|
|
# but feature API keeps LimitationModel.size as int for compatibility.
|
|
vector_space.size = int(billing_vector_space["size"])
|
|
vector_space.limit = billing_vector_space["limit"]
|
|
vector_space.usage_unknown = billing_vector_space.get("usage_unknown", False)
|
|
|
|
return vector_space
|
|
|
|
@staticmethod
|
|
def has_knowledge_subscription_limits() -> bool:
|
|
"""Whether knowledge writes are subject to workspace subscription limits."""
|
|
return dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD
|
|
|
|
@classmethod
|
|
def get_knowledge_rate_limit(cls, tenant_id: str) -> feature_entities.KnowledgeRateLimitModel:
|
|
knowledge_rate_limit = feature_entities.KnowledgeRateLimitModel()
|
|
if dify_config.DEPLOYMENT_EDITION == DeploymentEdition.CLOUD and tenant_id:
|
|
limit_info = BillingService.get_knowledge_rate_limit(tenant_id)
|
|
knowledge_rate_limit = feature_entities.KnowledgeRateLimitModel.model_validate(
|
|
{
|
|
"enabled": True,
|
|
"limit": limit_info.get("limit", 10),
|
|
"subscription_plan": limit_info.get("subscription_plan", CloudPlan.SANDBOX),
|
|
}
|
|
)
|
|
return knowledge_rate_limit
|
|
|
|
@classmethod
|
|
def get_knowledge_file_size_limit(cls, tenant_id: str | None) -> int:
|
|
default_limit = dify_config.UPLOAD_FILE_SIZE_LIMIT
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD or not tenant_id:
|
|
return default_limit
|
|
|
|
if cls._is_paid_cloud_workspace(tenant_id):
|
|
return max(default_limit, dify_config.KNOWLEDGE_UPLOAD_FILE_SIZE_LIMIT_FOR_PAID_PLAN)
|
|
|
|
return default_limit
|
|
|
|
@classmethod
|
|
def _resolve_human_input_email_delivery_enabled(
|
|
cls, *, features: feature_entities.FeatureModel, tenant_id: str | None
|
|
) -> bool:
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return True
|
|
if not tenant_id:
|
|
return False
|
|
return features.billing.subscription.plan.is_paid
|
|
|
|
@classmethod
|
|
def _fulfill_trial_models_from_env(cls, quota_types: tuple[str, ...] | None = None) -> list[str]:
|
|
allowed_quota_types = quota_types or ("PAID", "TRIAL")
|
|
return [
|
|
provider.value
|
|
for provider in HostedTrialProvider
|
|
if any(
|
|
getattr(dify_config, f"HOSTED_{provider.config_key}_{quota_type}_ENABLED", False)
|
|
for quota_type in allowed_quota_types
|
|
)
|
|
]
|
|
|
|
@classmethod
|
|
def get_trial_models(cls, tenant_id: str) -> list[str]:
|
|
"""Return hosted catalog flags without changing quota admission or billing.
|
|
|
|
Only a Tokener-funded workspace with a consumed staging signup intent
|
|
and an explicit trusted staging Billing target can see both configured catalogs.
|
|
For all other Cloud workspaces the existing plan filter still applies;
|
|
a failed staging binding read never grants broader visibility.
|
|
"""
|
|
if dify_config.DEPLOYMENT_EDITION != DeploymentEdition.CLOUD:
|
|
return cls._fulfill_trial_models_from_env()
|
|
|
|
if (
|
|
tenant_id
|
|
and dify_config.get_agent_dev_staging_tokener_billing_url() is not None
|
|
and signup_used_staging_package(tenant_id)
|
|
and ModelBillingProfileService.resolve(tenant_id).uses_tokener
|
|
):
|
|
return cls._fulfill_trial_models_from_env()
|
|
|
|
quota_types = ("PAID", "TRIAL") if cls._is_paid_cloud_workspace(tenant_id) else ("TRIAL",)
|
|
return cls._fulfill_trial_models_from_env(quota_types)
|
|
|
|
@classmethod
|
|
def _fulfill_params_from_env(cls, features: feature_entities.FeatureModel) -> None:
|
|
features.can_replace_logo = dify_config.CAN_REPLACE_LOGO
|
|
features.model_load_balancing_enabled = dify_config.MODEL_LB_ENABLED
|
|
features.education.enabled = (
|
|
dify_config.EDUCATION_ENABLED and dify_config.DEPLOYMENT_EDITION in EDUCATION_EDITIONS
|
|
)
|
|
features.enable_skill = dify_config.ENABLE_SKILL
|
|
features.enable_knowledge_fs = get_knowledge_fs_features().enabled
|
|
features.enable_file_scan = dify_config.FILE_SCAN_ENABLED
|
|
|
|
@classmethod
|
|
def _lift_plan_quotas(cls, features: feature_entities.FeatureModel):
|
|
"""Subscription quotas only exist on Cloud; other editions report them as unlimited."""
|
|
features.members = feature_entities.LimitationModel(size=0, limit=0)
|
|
features.apps = feature_entities.LimitationModel(size=0, limit=0)
|
|
features.annotation_quota_limit = feature_entities.LimitationModel(size=0, limit=0)
|
|
features.documents_upload_quota = feature_entities.LimitationModel(size=0, limit=0)
|
|
if features.vector_space is not None:
|
|
features.vector_space = feature_entities.LimitationModel(size=0, limit=-1)
|
|
features.trigger_event = feature_entities.Quota(usage=0, limit=-1, reset_date=-1)
|
|
features.api_rate_limit = feature_entities.Quota(usage=0, limit=-1, reset_date=-1)
|
|
|
|
@classmethod
|
|
def _fulfill_params_from_workspace_info(cls, features: feature_entities.FeatureModel, tenant_id: str) -> None:
|
|
workspace_info = EnterpriseService.get_workspace_info(tenant_id)
|
|
if "WorkspaceMembers" in workspace_info:
|
|
features.workspace_members.size = workspace_info["WorkspaceMembers"]["used"]
|
|
features.workspace_members.limit = workspace_info["WorkspaceMembers"]["limit"]
|
|
features.workspace_members.enabled = workspace_info["WorkspaceMembers"]["enabled"]
|
|
|
|
@classmethod
|
|
def _fulfill_params_from_billing_api(
|
|
cls,
|
|
features: feature_entities.FeatureModel,
|
|
tenant_id: str,
|
|
exclude_vector_space: bool = False,
|
|
) -> None:
|
|
if exclude_vector_space:
|
|
billing_info = BillingService.get_info(tenant_id, exclude_vector_space=True)
|
|
else:
|
|
billing_info = BillingService.get_info(tenant_id)
|
|
|
|
features_usage_info = BillingService.get_quota_info(tenant_id)
|
|
|
|
features.billing.subscription = feature_entities.SubscriptionModel.model_validate(billing_info["subscription"])
|
|
features.education.activated = billing_info["subscription"].get("education", False)
|
|
|
|
if features.billing.subscription.plan != CloudPlan.SANDBOX:
|
|
features.webapp_copyright_enabled = True
|
|
else:
|
|
features.is_allow_transfer_workspace = False
|
|
|
|
if "trigger_event" in features_usage_info:
|
|
features.trigger_event.usage = features_usage_info["trigger_event"]["usage"]
|
|
features.trigger_event.limit = features_usage_info["trigger_event"]["limit"]
|
|
features.trigger_event.reset_date = features_usage_info["trigger_event"].get("reset_date", -1)
|
|
|
|
if "api_rate_limit" in features_usage_info:
|
|
features.api_rate_limit.usage = features_usage_info["api_rate_limit"]["usage"]
|
|
features.api_rate_limit.limit = features_usage_info["api_rate_limit"]["limit"]
|
|
features.api_rate_limit.reset_date = features_usage_info["api_rate_limit"].get("reset_date", -1)
|
|
|
|
if "members" in billing_info:
|
|
features.members.size = billing_info["members"]["size"]
|
|
features.members.limit = billing_info["members"]["limit"]
|
|
|
|
if "apps" in billing_info:
|
|
features.apps.size = billing_info["apps"]["size"]
|
|
features.apps.limit = billing_info["apps"]["limit"]
|
|
|
|
if not exclude_vector_space:
|
|
assert features.vector_space is not None
|
|
cls._fulfill_vector_space_from_billing_info(features.vector_space, billing_info)
|
|
|
|
if "documents_upload_quota" in billing_info:
|
|
features.documents_upload_quota.size = billing_info["documents_upload_quota"]["size"]
|
|
features.documents_upload_quota.limit = billing_info["documents_upload_quota"]["limit"]
|
|
|
|
if "annotation_quota_limit" in billing_info:
|
|
features.annotation_quota_limit.size = billing_info["annotation_quota_limit"]["size"]
|
|
features.annotation_quota_limit.limit = billing_info["annotation_quota_limit"]["limit"]
|
|
|
|
if "can_replace_logo" in billing_info:
|
|
features.can_replace_logo = billing_info["can_replace_logo"]
|
|
|
|
if "model_load_balancing_enabled" in billing_info:
|
|
features.model_load_balancing_enabled = billing_info["model_load_balancing_enabled"]
|
|
|
|
if "knowledge_pipeline_publish_enabled" in billing_info:
|
|
features.knowledge_pipeline.publish_enabled = billing_info["knowledge_pipeline_publish_enabled"]
|
|
|
|
if "next_credit_reset_date" in billing_info:
|
|
features.next_credit_reset_date = billing_info["next_credit_reset_date"]
|
|
|
|
@classmethod
|
|
def _fulfill_vector_space_from_billing_info(
|
|
cls, vector_space: feature_entities.LimitationModel, billing_info: BillingInfo
|
|
) -> None:
|
|
if "vector_space" not in billing_info:
|
|
return
|
|
|
|
# NOTE: billing API returns vector_space.size as float (e.g. 0.0),
|
|
# but feature API keeps LimitationModel.size as int for compatibility.
|
|
vector_space.size = int(billing_info["vector_space"]["size"])
|
|
vector_space.limit = billing_info["vector_space"]["limit"]
|