mirror of
https://github.com/langgenius/dify.git
synced 2026-09-24 23:22:26 +08:00
feat: improve ai-credits display (#38589)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -55,6 +55,7 @@ class QuotaBalanceResult(TypedDict):
|
||||
reserved: int
|
||||
quota: int
|
||||
usage: int
|
||||
exhausted_at: NotRequired[int]
|
||||
|
||||
|
||||
class QuotaConsumeCappedResult(TypedDict):
|
||||
|
||||
@@ -32,6 +32,7 @@ class CreditPoolBalance:
|
||||
pool_type: str
|
||||
quota_limit: int
|
||||
quota_used: int
|
||||
exhausted_at: int | None = None
|
||||
|
||||
@property
|
||||
def remaining_credits(self) -> int:
|
||||
@@ -133,6 +134,7 @@ class CreditPoolService:
|
||||
pool_type=normalized_pool_type,
|
||||
quota_limit=balance["quota"],
|
||||
quota_used=balance["usage"],
|
||||
exhausted_at=balance.get("exhausted_at"),
|
||||
)
|
||||
|
||||
session = cls._require_session(session)
|
||||
|
||||
@@ -9,6 +9,15 @@ from services.account_service import TenantService
|
||||
from services.feature_service import FeatureService
|
||||
|
||||
|
||||
def _set_credit_pool_info(
|
||||
tenant_info: dict[str, object], *, quota_limit: int, quota_used: int, exhausted_at: int | None = None
|
||||
) -> None:
|
||||
tenant_info["trial_credits"] = quota_limit
|
||||
tenant_info["trial_credits_used"] = quota_used
|
||||
if isinstance(exhausted_at, int) and exhausted_at > 0 and quota_limit > 0 and quota_used >= quota_limit:
|
||||
tenant_info["trial_credits_exhausted_at"] = exhausted_at
|
||||
|
||||
|
||||
class WorkspaceService:
|
||||
@classmethod
|
||||
def get_tenant_info(cls, tenant: Tenant, session: Session):
|
||||
@@ -54,7 +63,7 @@ class WorkspaceService:
|
||||
if dify_config.EDITION == "CLOUD":
|
||||
tenant_info["next_credit_reset_date"] = feature.next_credit_reset_date
|
||||
|
||||
from services.credit_pool_service import CreditPoolService
|
||||
from services.credit_pool_service import CreditPoolBalance, CreditPoolService
|
||||
|
||||
paid_pool = CreditPoolService.get_pool(tenant_id=tenant.id, pool_type="paid", session=session)
|
||||
# if the tenant is not on the sandbox plan and the paid pool is not full, use the paid pool
|
||||
@@ -63,12 +72,22 @@ class WorkspaceService:
|
||||
and paid_pool is not None
|
||||
and (paid_pool.quota_limit == -1 or paid_pool.quota_limit > paid_pool.quota_used)
|
||||
):
|
||||
tenant_info["trial_credits"] = paid_pool.quota_limit
|
||||
tenant_info["trial_credits_used"] = paid_pool.quota_used
|
||||
exhausted_at = paid_pool.exhausted_at if isinstance(paid_pool, CreditPoolBalance) else None
|
||||
_set_credit_pool_info(
|
||||
tenant_info,
|
||||
quota_limit=paid_pool.quota_limit,
|
||||
quota_used=paid_pool.quota_used,
|
||||
exhausted_at=exhausted_at,
|
||||
)
|
||||
else:
|
||||
trial_pool = CreditPoolService.get_pool(tenant_id=tenant.id, pool_type="trial", session=session)
|
||||
if trial_pool:
|
||||
tenant_info["trial_credits"] = trial_pool.quota_limit
|
||||
tenant_info["trial_credits_used"] = trial_pool.quota_used
|
||||
exhausted_at = trial_pool.exhausted_at if isinstance(trial_pool, CreditPoolBalance) else None
|
||||
_set_credit_pool_info(
|
||||
tenant_info,
|
||||
quota_limit=trial_pool.quota_limit,
|
||||
quota_used=trial_pool.quota_used,
|
||||
exhausted_at=exhausted_at,
|
||||
)
|
||||
|
||||
return tenant_info
|
||||
|
||||
Reference in New Issue
Block a user