积分消耗明细改版V1

This commit is contained in:
2026-08-12 11:49:53 +08:00
parent e3719eae09
commit eb7c60684e
39 changed files with 648 additions and 975 deletions
@@ -9,11 +9,11 @@ from app.services.generation.billing_service import build_credit_biz_key
class LlmBillingConfigurationError(RuntimeError):
"""LLM 场景固定预扣配置无效。"""
"""LLM 场景积分配置无效。"""
class LlmBillingStateError(RuntimeError):
"""业务 attempt 的固定预扣状态不允许继续执行。"""
"""业务 attempt 的 LLM 积分消费状态不允许继续执行。"""
class LlmProviderPostprocessError(RuntimeError):
@@ -26,7 +26,7 @@ class LlmProviderPostprocessError(RuntimeError):
@dataclass(slots=True, frozen=True)
class LlmBillingPolicy:
pre_deduct_credits: float
charge_credits: float
valid: bool = True
error: str | None = None
policy_id: str | None = None
@@ -65,13 +65,15 @@ class LlmBillingContext:
current_call_attempt_id: str | None = None
@property
def pre_deduct_biz_key(self) -> str:
def charge_biz_key(self) -> str:
return build_credit_biz_key(
owner_type=self.owner_type,
owner_id=self.owner_id,
attempt_no=self.attempt_no,
charge_kind=self.charge_kind,
action="pre_deduct",
# 使用独立 llm_charge 幂等键,避免与历史 Token 按量计费的 :charge 流水碰撞;
# CreditRecord 的业务动作仍然记录为 charge(真实消费)。
action="llm_charge",
)
@property
@@ -86,11 +88,11 @@ class LlmBillingContext:
@property
def billing_biz_key(self) -> str:
return self.pre_deduct_biz_key
return self.charge_biz_key
@dataclass(slots=True, frozen=True)
class LlmPreDeductResult:
class LlmChargeResult:
amount: float
state: LlmBillingLedgerState
created: bool = False
@@ -100,10 +102,10 @@ class LlmPreDeductResult:
@dataclass(slots=True, frozen=True)
class LlmPreDeductValidation:
class LlmChargeValidation:
can_execute: bool
amount: float
state: LlmBillingLedgerState
reason: str | None = None
pre_deduct_record_id: str | None = None
charge_record_id: str | None = None
execution_id: str | None = None