Thread int32 saturation clamps from tiered settlement and video task recompute into the consume/task logs under admin_info, so oversized or malformed billing inputs stay auditable. Clamp negative audio duration before token conversion and gate the saturation UI markers on admin.
39 lines
1.4 KiB
Go
39 lines
1.4 KiB
Go
package billingexpr
|
|
|
|
import "github.com/QuantumNous/new-api/common"
|
|
|
|
// quotaConversion converts raw expression output to quota based on the
|
|
// expression version. This is the central dispatch point for future versions
|
|
// that may use a different conversion formula.
|
|
func quotaConversion(exprOutput float64, snap *BillingSnapshot) float64 {
|
|
switch snap.ExprVersion {
|
|
default: // v1: coefficients are $/1M tokens prices
|
|
return exprOutput / 1_000_000 * snap.QuotaPerUnit
|
|
}
|
|
}
|
|
|
|
// ComputeTieredQuota runs the Expr from a frozen BillingSnapshot against
|
|
// actual token counts and returns the settlement result.
|
|
func ComputeTieredQuota(snap *BillingSnapshot, params TokenParams) (TieredResult, error) {
|
|
return ComputeTieredQuotaWithRequest(snap, params, RequestInput{})
|
|
}
|
|
|
|
func ComputeTieredQuotaWithRequest(snap *BillingSnapshot, params TokenParams, request RequestInput) (TieredResult, error) {
|
|
cost, trace, err := RunExprByHashWithRequest(snap.ExprString, snap.ExprHash, params, request)
|
|
if err != nil {
|
|
return TieredResult{}, err
|
|
}
|
|
|
|
quotaBeforeGroup := quotaConversion(cost, snap)
|
|
afterGroup, clamp := common.QuotaRoundChecked(quotaBeforeGroup * snap.GroupRatio)
|
|
crossed := trace.MatchedTier != snap.EstimatedTier
|
|
|
|
return TieredResult{
|
|
ActualQuotaBeforeGroup: quotaBeforeGroup,
|
|
ActualQuotaAfterGroup: afterGroup,
|
|
MatchedTier: trace.MatchedTier,
|
|
CrossedTier: crossed,
|
|
Clamp: clamp,
|
|
}, nil
|
|
}
|