feat: sync deployed v1.0.0-rc.38-custom.42

Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
This commit is contained in:
cneicy committed 2026-09-26 04:49:11 +08:00
1 parent aa6c3f50f4
commit ceddfd3d6b
828 files changed
+122253 -13702

No files matched your search

+69 -11
View File
@@ -3,6 +3,7 @@ package service
import (
"net/http"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/pkg/billingexpr"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
@@ -35,6 +36,37 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
}
img := float64(usage.PromptTokensDetails.ImageTokens)
imgCR := float64(0)
if usedVars["img_cr"] && !isClaudeUsageSemantic {
details := usage.PromptTokensDetails.CachedTokensDetails
if details != nil && details.ImageTokens != nil {
cachedImage := *details.ImageTokens
cached := usage.PromptTokensDetails.CachedTokens
image := usage.PromptTokensDetails.ImageTokens
valid := cachedImage >= 0 && cached >= cachedImage && image >= cachedImage &&
cached <= usage.PromptTokens && image <= usage.PromptTokens-(cached-cachedImage)
if valid {
remaining := cached - cachedImage
for _, count := range []*int{details.TextTokens, details.AudioTokens} {
if count == nil {
continue
}
if *count < 0 || *count > remaining {
valid = false
break
}
remaining -= *count
}
}
if valid {
imgCR = float64(cachedImage)
cr -= imgCR
img -= imgCR
} else {
common.SysError("invalid image cache token breakdown; using aggregate cache billing")
}
}
}
ai := float64(usage.PromptTokensDetails.AudioTokens)
imgO := float64(usage.CompletionTokenDetails.ImageTokens)
ao := float64(usage.CompletionTokenDetails.AudioTokens)
@@ -47,7 +79,13 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
inputLen = p + cr + cc5m + cc1h
}
if !isClaudeUsageSemantic {
if isClaudeUsageSemantic {
// Anthropic input excludes cache reads. When cr has no separate
// price, merge those tokens into the input category instead.
if !usedVars["cr"] {
p += cr
}
} else {
if usedVars["cr"] {
p -= cr
}
@@ -60,6 +98,9 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
if usedVars["img"] {
p -= img
}
if usedVars["img_cr"] {
p -= imgCR
}
if usedVars["ai"] {
p -= ai
}
@@ -81,16 +122,17 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
}
return billingexpr.TokenParams{
P: p,
C: c,
Len: inputLen,
CR: cr,
CC: cc5m,
CC1h: cc1h,
Img: img,
ImgO: imgO,
AI: ai,
AO: ao,
P: p,
C: c,
Len: inputLen,
CR: cr,
CC: cc5m,
CC1h: cc1h,
Img: img,
ImgCR: imgCR,
ImgO: imgO,
AI: ai,
AO: ao,
}
}
@@ -135,6 +177,7 @@ func PrepareTieredBillingForSelectedGroup(c *gin.Context, relayInfo *relaycommon
if snap == nil {
return nil
}
if relayInfo.DurableAttemptBilling { return nil }
if snap.GroupRatio == 0 {
// Paid-to-free keeps FreeModel as-is: FreeModel means "pre-consume was
// skipped", which is not true once a session exists, and settlement
@@ -170,6 +213,11 @@ func TryTieredSettle(relayInfo *relaycommon.RelayInfo, params billingexpr.TokenP
if relayInfo.BillingRequestInput != nil {
requestInput = *relayInfo.BillingRequestInput
}
if relayInfo.BillingImageCount != nil {
requestInput.ImageCount = relayInfo.BillingImageCount
} else if snap.EstimatedImageCount != nil {
requestInput.ImageCount = snap.EstimatedImageCount
}
tr, err := billingexpr.ComputeTieredQuotaWithRequest(snap, params, requestInput)
if err != nil {
@@ -187,3 +235,13 @@ func TryTieredSettle(relayInfo *relaycommon.RelayInfo, params billingexpr.TokenP
return true, tr.ActualQuotaAfterGroup, &tr
}
// A failed evaluation retains the reservation and its estimated billing unit.
// Successful evaluations always use the actual branch, including zero prices.
func isFixedPriceSettlement(info *relaycommon.RelayInfo, result *billingexpr.TieredResult) bool {
if result != nil {
return result.BillingUnit == billingexpr.BillingUnitRequest
}
snap := info.TieredBillingSnapshot
return snap != nil && snap.BillingMode == "tiered_expr" && snap.EstimatedBillingUnit == billingexpr.BillingUnitRequest
}