Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions service/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type QuotaInfo struct {
GroupRatio float64
}

func hasCustomModelRatio(modelName string, currentRatio float64) bool {
defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName]
if !exists {
return true
}
return currentRatio != defaultRatio
}
Comment on lines +41 to +47

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid fragile float equality when detecting custom ratios

Direct float equality can misclassify defaults due to precision drift. Use a tolerance.

 func hasCustomModelRatio(modelName string, currentRatio float64) bool {
   defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName]
   if !exists {
     return true
   }
-  return currentRatio != defaultRatio
+  // Avoid false positives caused by floating-point rounding
+  return math.Abs(currentRatio-defaultRatio) > 1e-9
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func hasCustomModelRatio(modelName string, currentRatio float64) bool {
defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName]
if !exists {
return true
}
return currentRatio != defaultRatio
}
func hasCustomModelRatio(modelName string, currentRatio float64) bool {
defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName]
if !exists {
return true
}
// Avoid false positives caused by floating-point rounding
return math.Abs(currentRatio-defaultRatio) > 1e-9
}
🤖 Prompt for AI Agents
In service/quota.go around lines 40 to 46, the function uses direct float
equality to detect custom ratios which is fragile; change the comparison to use
a tolerance (epsilon) and treat the ratio as custom when math.Abs(currentRatio -
defaultRatio) > epsilon (choose a small constant like 1e-9 or make it
configurable), ensure math is imported and consider handling NaN/Inf by treating
them as custom if encountered.


func calculateAudioQuota(info QuotaInfo) int {
if info.UsePrice {
modelPrice := decimal.NewFromFloat(info.ModelPrice)
Expand Down Expand Up @@ -246,9 +254,10 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,

if relayInfo.ChannelType == constant.ChannelTypeOpenRouter {
promptTokens -= cacheTokens
if cacheCreationTokens == 0 && relayInfo.PriceData.CacheCreationRatio != 1 && usage.Cost != 0 {
isUsingCustomSettings := relayInfo.PriceData.UsePrice || hasCustomModelRatio(modelName, relayInfo.PriceData.ModelRatio)
if cacheCreationTokens == 0 && relayInfo.PriceData.CacheCreationRatio != 1 && usage.Cost != 0 && !isUsingCustomSettings {
maybeCacheCreationTokens := CalcOpenRouterCacheCreateTokens(*usage, relayInfo.PriceData)
if promptTokens >= maybeCacheCreationTokens {
if maybeCacheCreationTokens >= 0 && promptTokens >= maybeCacheCreationTokens {
cacheCreationTokens = maybeCacheCreationTokens
}
}
Expand Down