2024-10-07 19:08:20 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2024-10-07 19:15:14 +08:00
|
|
|
|
"errors"
|
2024-10-07 19:08:20 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"one-api/common"
|
2025-02-18 14:54:21 +08:00
|
|
|
|
constant2 "one-api/constant"
|
2024-10-07 19:08:20 +08:00
|
|
|
|
"one-api/dto"
|
|
|
|
|
|
"one-api/model"
|
|
|
|
|
|
relaycommon "one-api/relay/common"
|
2025-02-20 16:41:46 +08:00
|
|
|
|
"one-api/relay/helper"
|
2024-12-25 19:31:12 +08:00
|
|
|
|
"one-api/setting"
|
2025-03-08 01:30:50 +08:00
|
|
|
|
"one-api/setting/operation_setting"
|
2024-11-07 17:28:21 +08:00
|
|
|
|
"strings"
|
2024-10-07 19:08:20 +08:00
|
|
|
|
"time"
|
2025-01-04 15:46:35 +08:00
|
|
|
|
|
2025-03-08 21:55:50 +08:00
|
|
|
|
"github.com/bytedance/gopkg/util/gopool"
|
|
|
|
|
|
|
2025-01-04 15:46:35 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-03-08 21:55:50 +08:00
|
|
|
|
"github.com/shopspring/decimal"
|
2024-10-07 19:08:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-01-04 15:46:35 +08:00
|
|
|
|
type TokenDetails struct {
|
|
|
|
|
|
TextTokens int
|
|
|
|
|
|
AudioTokens int
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type QuotaInfo struct {
|
|
|
|
|
|
InputDetails TokenDetails
|
|
|
|
|
|
OutputDetails TokenDetails
|
|
|
|
|
|
ModelName string
|
|
|
|
|
|
UsePrice bool
|
|
|
|
|
|
ModelPrice float64
|
|
|
|
|
|
ModelRatio float64
|
|
|
|
|
|
GroupRatio float64
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func calculateAudioQuota(info QuotaInfo) int {
|
|
|
|
|
|
if info.UsePrice {
|
2025-03-08 21:55:50 +08:00
|
|
|
|
modelPrice := decimal.NewFromFloat(info.ModelPrice)
|
|
|
|
|
|
quotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit)
|
|
|
|
|
|
groupRatio := decimal.NewFromFloat(info.GroupRatio)
|
|
|
|
|
|
|
|
|
|
|
|
quota := modelPrice.Mul(quotaPerUnit).Mul(groupRatio)
|
|
|
|
|
|
return int(quota.IntPart())
|
2025-01-04 15:46:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-08 21:55:50 +08:00
|
|
|
|
completionRatio := decimal.NewFromFloat(operation_setting.GetCompletionRatio(info.ModelName))
|
|
|
|
|
|
audioRatio := decimal.NewFromFloat(operation_setting.GetAudioRatio(info.ModelName))
|
|
|
|
|
|
audioCompletionRatio := decimal.NewFromFloat(operation_setting.GetAudioCompletionRatio(info.ModelName))
|
|
|
|
|
|
|
|
|
|
|
|
groupRatio := decimal.NewFromFloat(info.GroupRatio)
|
|
|
|
|
|
modelRatio := decimal.NewFromFloat(info.ModelRatio)
|
|
|
|
|
|
ratio := groupRatio.Mul(modelRatio)
|
|
|
|
|
|
|
|
|
|
|
|
inputTextTokens := decimal.NewFromInt(int64(info.InputDetails.TextTokens))
|
|
|
|
|
|
outputTextTokens := decimal.NewFromInt(int64(info.OutputDetails.TextTokens))
|
|
|
|
|
|
inputAudioTokens := decimal.NewFromInt(int64(info.InputDetails.AudioTokens))
|
|
|
|
|
|
outputAudioTokens := decimal.NewFromInt(int64(info.OutputDetails.AudioTokens))
|
|
|
|
|
|
|
|
|
|
|
|
quota := decimal.Zero
|
|
|
|
|
|
quota = quota.Add(inputTextTokens)
|
|
|
|
|
|
quota = quota.Add(outputTextTokens.Mul(completionRatio))
|
|
|
|
|
|
quota = quota.Add(inputAudioTokens.Mul(audioRatio))
|
|
|
|
|
|
quota = quota.Add(outputAudioTokens.Mul(audioRatio).Mul(audioCompletionRatio))
|
2025-01-04 15:46:35 +08:00
|
|
|
|
|
2025-03-08 21:55:50 +08:00
|
|
|
|
quota = quota.Mul(ratio)
|
2025-01-04 15:46:35 +08:00
|
|
|
|
|
2025-03-08 21:55:50 +08:00
|
|
|
|
// If ratio is not zero and quota is less than or equal to zero, set quota to 1
|
|
|
|
|
|
if !ratio.IsZero() && quota.LessThanOrEqual(decimal.Zero) {
|
|
|
|
|
|
quota = decimal.NewFromInt(1)
|
2025-01-04 15:46:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-08 21:55:50 +08:00
|
|
|
|
return int(quota.Round(0).IntPart())
|
2025-01-04 15:46:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-07 19:08:20 +08:00
|
|
|
|
func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage *dto.RealtimeUsage) error {
|
|
|
|
|
|
if relayInfo.UsePrice {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2024-12-29 16:50:26 +08:00
|
|
|
|
userQuota, err := model.GetUserQuota(relayInfo.UserId, false)
|
2024-10-07 19:15:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2024-11-07 17:28:21 +08:00
|
|
|
|
|
2024-12-30 17:10:48 +08:00
|
|
|
|
token, err := model.GetTokenByKey(strings.TrimLeft(relayInfo.TokenKey, "sk-"), false)
|
2024-11-07 17:28:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-20 16:41:46 +08:00
|
|
|
|
modelName := relayInfo.OriginModelName
|
2024-10-07 19:08:20 +08:00
|
|
|
|
textInputTokens := usage.InputTokenDetails.TextTokens
|
|
|
|
|
|
textOutTokens := usage.OutputTokenDetails.TextTokens
|
|
|
|
|
|
audioInputTokens := usage.InputTokenDetails.AudioTokens
|
|
|
|
|
|
audioOutTokens := usage.OutputTokenDetails.AudioTokens
|
2024-12-25 19:31:12 +08:00
|
|
|
|
groupRatio := setting.GetGroupRatio(relayInfo.Group)
|
2025-03-08 01:30:50 +08:00
|
|
|
|
modelRatio, _ := operation_setting.GetModelRatio(modelName)
|
2024-10-07 19:08:20 +08:00
|
|
|
|
|
2025-01-04 15:46:35 +08:00
|
|
|
|
quotaInfo := QuotaInfo{
|
|
|
|
|
|
InputDetails: TokenDetails{
|
|
|
|
|
|
TextTokens: textInputTokens,
|
|
|
|
|
|
AudioTokens: audioInputTokens,
|
|
|
|
|
|
},
|
|
|
|
|
|
OutputDetails: TokenDetails{
|
|
|
|
|
|
TextTokens: textOutTokens,
|
|
|
|
|
|
AudioTokens: audioOutTokens,
|
|
|
|
|
|
},
|
|
|
|
|
|
ModelName: modelName,
|
|
|
|
|
|
UsePrice: relayInfo.UsePrice,
|
|
|
|
|
|
ModelRatio: modelRatio,
|
|
|
|
|
|
GroupRatio: groupRatio,
|
2024-10-07 19:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-04 15:46:35 +08:00
|
|
|
|
quota := calculateAudioQuota(quotaInfo)
|
|
|
|
|
|
|
2024-10-07 19:15:14 +08:00
|
|
|
|
if userQuota < quota {
|
2025-02-21 16:42:48 +08:00
|
|
|
|
return fmt.Errorf("user quota is not enough, user quota: %s, need quota: %s", common.FormatQuota(userQuota), common.FormatQuota(quota))
|
2024-10-07 19:15:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-04 16:20:24 +08:00
|
|
|
|
if !token.UnlimitedQuota && token.RemainQuota < quota {
|
2025-02-21 16:42:48 +08:00
|
|
|
|
return fmt.Errorf("token quota is not enough, token remain quota: %s, need quota: %s", common.FormatQuota(token.RemainQuota), common.FormatQuota(quota))
|
2024-11-07 17:28:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-18 14:54:21 +08:00
|
|
|
|
err = PostConsumeQuota(relayInfo, quota, 0, false)
|
2024-10-07 19:08:20 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2024-10-07 19:15:14 +08:00
|
|
|
|
common.LogInfo(ctx, "realtime streaming consume quota success, quota: "+fmt.Sprintf("%d", quota))
|
2024-10-07 19:08:20 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func PostWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, modelName string,
|
2025-01-04 15:46:35 +08:00
|
|
|
|
usage *dto.RealtimeUsage, preConsumedQuota int, userQuota int, modelRatio float64, groupRatio float64,
|
2024-10-07 19:08:20 +08:00
|
|
|
|
modelPrice float64, usePrice bool, extraContent string) {
|
|
|
|
|
|
|
|
|
|
|
|
useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
|
|
|
|
|
|
textInputTokens := usage.InputTokenDetails.TextTokens
|
|
|
|
|
|
textOutTokens := usage.OutputTokenDetails.TextTokens
|
|
|
|
|
|
|
|
|
|
|
|
audioInputTokens := usage.InputTokenDetails.AudioTokens
|
|
|
|
|
|
audioOutTokens := usage.OutputTokenDetails.AudioTokens
|
|
|
|
|
|
|
|
|
|
|
|
tokenName := ctx.GetString("token_name")
|
2025-03-08 21:55:50 +08:00
|
|
|
|
completionRatio := decimal.NewFromFloat(operation_setting.GetCompletionRatio(modelName))
|
|
|
|
|
|
audioRatio := decimal.NewFromFloat(operation_setting.GetAudioRatio(relayInfo.OriginModelName))
|
|
|
|
|
|
audioCompletionRatio := decimal.NewFromFloat(operation_setting.GetAudioCompletionRatio(modelName))
|
2024-10-07 19:08:20 +08:00
|
|
|
|
|
2025-01-04 15:46:35 +08:00
|
|
|
|
quotaInfo := QuotaInfo{
|
|
|
|
|
|
InputDetails: TokenDetails{
|
|
|
|
|
|
TextTokens: textInputTokens,
|
|
|
|
|
|
AudioTokens: audioInputTokens,
|
|
|
|
|
|
},
|
|
|
|
|
|
OutputDetails: TokenDetails{
|
|
|
|
|
|
TextTokens: textOutTokens,
|
|
|
|
|
|
AudioTokens: audioOutTokens,
|
|
|
|
|
|
},
|
|
|
|
|
|
ModelName: modelName,
|
|
|
|
|
|
UsePrice: usePrice,
|
|
|
|
|
|
ModelRatio: modelRatio,
|
|
|
|
|
|
GroupRatio: groupRatio,
|
2024-10-07 19:08:20 +08:00
|
|
|
|
}
|
2025-01-04 15:46:35 +08:00
|
|
|
|
|
|
|
|
|
|
quota := calculateAudioQuota(quotaInfo)
|
|
|
|
|
|
|
2024-10-07 19:08:20 +08:00
|
|
|
|
totalTokens := usage.TotalTokens
|
|
|
|
|
|
var logContent string
|
|
|
|
|
|
if !usePrice {
|
2025-03-08 21:55:50 +08:00
|
|
|
|
logContent = fmt.Sprintf("模型倍率 %.2f,补全倍率 %.2f,音频倍率 %.2f,音频补全倍率 %.2f,分组倍率 %.2f",
|
|
|
|
|
|
modelRatio, completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), groupRatio)
|
2024-10-07 19:08:20 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
logContent = fmt.Sprintf("模型价格 %.2f,分组倍率 %.2f", modelPrice, groupRatio)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// record all the consume log even if quota is 0
|
|
|
|
|
|
if totalTokens == 0 {
|
|
|
|
|
|
// in this case, must be some error happened
|
|
|
|
|
|
// we cannot just return, because we may have to return the pre-consumed quota
|
|
|
|
|
|
quota = 0
|
|
|
|
|
|
logContent += fmt.Sprintf("(可能是上游超时)")
|
|
|
|
|
|
common.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+
|
|
|
|
|
|
"tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, modelName, preConsumedQuota))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota)
|
|
|
|
|
|
model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logModel := modelName
|
2024-11-07 16:12:09 +08:00
|
|
|
|
if extraContent != "" {
|
|
|
|
|
|
logContent += ", " + extraContent
|
|
|
|
|
|
}
|
2025-03-08 21:55:50 +08:00
|
|
|
|
other := GenerateWssOtherInfo(ctx, relayInfo, usage, modelRatio, groupRatio,
|
|
|
|
|
|
completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), modelPrice)
|
2024-11-07 16:12:09 +08:00
|
|
|
|
model.RecordConsumeLog(ctx, relayInfo.UserId, relayInfo.ChannelId, usage.InputTokens, usage.OutputTokens, logModel,
|
2024-12-24 14:48:11 +08:00
|
|
|
|
tokenName, quota, logContent, relayInfo.TokenId, userQuota, int(useTimeSeconds), relayInfo.IsStream, relayInfo.Group, other)
|
2024-11-07 16:12:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
|
2025-02-20 16:41:46 +08:00
|
|
|
|
usage *dto.Usage, preConsumedQuota int, userQuota int, priceData helper.PriceData, extraContent string) {
|
2024-11-07 16:12:09 +08:00
|
|
|
|
|
|
|
|
|
|
useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
|
|
|
|
|
|
textInputTokens := usage.PromptTokensDetails.TextTokens
|
|
|
|
|
|
textOutTokens := usage.CompletionTokenDetails.TextTokens
|
|
|
|
|
|
|
|
|
|
|
|
audioInputTokens := usage.PromptTokensDetails.AudioTokens
|
|
|
|
|
|
audioOutTokens := usage.CompletionTokenDetails.AudioTokens
|
|
|
|
|
|
|
|
|
|
|
|
tokenName := ctx.GetString("token_name")
|
2025-03-08 21:55:50 +08:00
|
|
|
|
completionRatio := decimal.NewFromFloat(operation_setting.GetCompletionRatio(relayInfo.OriginModelName))
|
|
|
|
|
|
audioRatio := decimal.NewFromFloat(operation_setting.GetAudioRatio(relayInfo.OriginModelName))
|
|
|
|
|
|
audioCompletionRatio := decimal.NewFromFloat(operation_setting.GetAudioCompletionRatio(relayInfo.OriginModelName))
|
2025-02-20 16:41:46 +08:00
|
|
|
|
|
|
|
|
|
|
modelRatio := priceData.ModelRatio
|
|
|
|
|
|
groupRatio := priceData.GroupRatio
|
|
|
|
|
|
modelPrice := priceData.ModelPrice
|
|
|
|
|
|
usePrice := priceData.UsePrice
|
2024-11-07 16:12:09 +08:00
|
|
|
|
|
2025-01-04 15:46:35 +08:00
|
|
|
|
quotaInfo := QuotaInfo{
|
|
|
|
|
|
InputDetails: TokenDetails{
|
|
|
|
|
|
TextTokens: textInputTokens,
|
|
|
|
|
|
AudioTokens: audioInputTokens,
|
|
|
|
|
|
},
|
|
|
|
|
|
OutputDetails: TokenDetails{
|
|
|
|
|
|
TextTokens: textOutTokens,
|
|
|
|
|
|
AudioTokens: audioOutTokens,
|
|
|
|
|
|
},
|
2025-02-20 16:41:46 +08:00
|
|
|
|
ModelName: relayInfo.OriginModelName,
|
2025-01-04 15:46:35 +08:00
|
|
|
|
UsePrice: usePrice,
|
|
|
|
|
|
ModelRatio: modelRatio,
|
|
|
|
|
|
GroupRatio: groupRatio,
|
2024-11-07 16:12:09 +08:00
|
|
|
|
}
|
2025-01-04 15:46:35 +08:00
|
|
|
|
|
|
|
|
|
|
quota := calculateAudioQuota(quotaInfo)
|
|
|
|
|
|
|
2024-11-07 16:12:09 +08:00
|
|
|
|
totalTokens := usage.TotalTokens
|
|
|
|
|
|
var logContent string
|
|
|
|
|
|
if !usePrice {
|
2025-03-08 21:55:50 +08:00
|
|
|
|
logContent = fmt.Sprintf("模型倍率 %.2f,补全倍率 %.2f,音频倍率 %.2f,音频补全倍率 %.2f,分组倍率 %.2f",
|
|
|
|
|
|
modelRatio, completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), groupRatio)
|
2024-11-07 16:12:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
logContent = fmt.Sprintf("模型价格 %.2f,分组倍率 %.2f", modelPrice, groupRatio)
|
2024-10-07 19:08:20 +08:00
|
|
|
|
}
|
2024-11-07 16:12:09 +08:00
|
|
|
|
|
|
|
|
|
|
// record all the consume log even if quota is 0
|
|
|
|
|
|
if totalTokens == 0 {
|
|
|
|
|
|
// in this case, must be some error happened
|
|
|
|
|
|
// we cannot just return, because we may have to return the pre-consumed quota
|
|
|
|
|
|
quota = 0
|
|
|
|
|
|
logContent += fmt.Sprintf("(可能是上游超时)")
|
|
|
|
|
|
common.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+
|
2025-02-20 16:41:46 +08:00
|
|
|
|
"tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, relayInfo.OriginModelName, preConsumedQuota))
|
2024-11-07 16:12:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
quotaDelta := quota - preConsumedQuota
|
|
|
|
|
|
if quotaDelta != 0 {
|
2025-02-18 14:54:21 +08:00
|
|
|
|
err := PostConsumeQuota(relayInfo, quotaDelta, preConsumedQuota, true)
|
2024-11-07 16:12:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.LogError(ctx, "error consuming token remain quota: "+err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota)
|
|
|
|
|
|
model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota)
|
2024-10-07 19:08:20 +08:00
|
|
|
|
}
|
2024-11-07 16:12:09 +08:00
|
|
|
|
|
2025-02-20 16:41:46 +08:00
|
|
|
|
logModel := relayInfo.OriginModelName
|
2024-10-07 19:08:20 +08:00
|
|
|
|
if extraContent != "" {
|
|
|
|
|
|
logContent += ", " + extraContent
|
|
|
|
|
|
}
|
2025-03-08 21:55:50 +08:00
|
|
|
|
other := GenerateAudioOtherInfo(ctx, relayInfo, usage, modelRatio, groupRatio,
|
|
|
|
|
|
completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), modelPrice)
|
2024-11-07 16:12:09 +08:00
|
|
|
|
model.RecordConsumeLog(ctx, relayInfo.UserId, relayInfo.ChannelId, usage.PromptTokens, usage.CompletionTokens, logModel,
|
2024-12-24 14:48:11 +08:00
|
|
|
|
tokenName, quota, logContent, relayInfo.TokenId, userQuota, int(useTimeSeconds), relayInfo.IsStream, relayInfo.Group, other)
|
2024-10-07 19:08:20 +08:00
|
|
|
|
}
|
2025-02-18 14:54:21 +08:00
|
|
|
|
|
|
|
|
|
|
func PreConsumeTokenQuota(relayInfo *relaycommon.RelayInfo, quota int) error {
|
|
|
|
|
|
if quota < 0 {
|
|
|
|
|
|
return errors.New("quota 不能为负数!")
|
|
|
|
|
|
}
|
|
|
|
|
|
if relayInfo.IsPlayground {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
//if relayInfo.TokenUnlimited {
|
|
|
|
|
|
// return nil
|
|
|
|
|
|
//}
|
2025-02-19 15:12:26 +08:00
|
|
|
|
token, err := model.GetTokenByKey(relayInfo.TokenKey, false)
|
2025-02-18 14:54:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !relayInfo.TokenUnlimited && token.RemainQuota < quota {
|
2025-02-21 16:42:48 +08:00
|
|
|
|
return fmt.Errorf("token quota is not enough, token remain quota: %s, need quota: %s", common.FormatQuota(token.RemainQuota), common.FormatQuota(quota))
|
2025-02-18 14:54:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
err = model.DecreaseTokenQuota(relayInfo.TokenId, relayInfo.TokenKey, quota)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func PostConsumeQuota(relayInfo *relaycommon.RelayInfo, quota int, preConsumedQuota int, sendEmail bool) (err error) {
|
|
|
|
|
|
|
|
|
|
|
|
if quota > 0 {
|
|
|
|
|
|
err = model.DecreaseUserQuota(relayInfo.UserId, quota)
|
|
|
|
|
|
} else {
|
2025-02-25 20:56:16 +08:00
|
|
|
|
err = model.IncreaseUserQuota(relayInfo.UserId, -quota, false)
|
2025-02-18 14:54:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !relayInfo.IsPlayground {
|
|
|
|
|
|
if quota > 0 {
|
|
|
|
|
|
err = model.DecreaseTokenQuota(relayInfo.TokenId, relayInfo.TokenKey, quota)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err = model.IncreaseTokenQuota(relayInfo.TokenId, relayInfo.TokenKey, -quota)
|
|
|
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if sendEmail {
|
|
|
|
|
|
if (quota + preConsumedQuota) != 0 {
|
2025-02-25 20:56:16 +08:00
|
|
|
|
checkAndSendQuotaNotify(relayInfo, quota, preConsumedQuota)
|
2025-02-18 14:54:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-25 20:56:16 +08:00
|
|
|
|
func checkAndSendQuotaNotify(relayInfo *relaycommon.RelayInfo, quota int, preConsumedQuota int) {
|
2025-02-18 14:54:21 +08:00
|
|
|
|
gopool.Go(func() {
|
2025-02-25 20:56:16 +08:00
|
|
|
|
userSetting := relayInfo.UserSetting
|
2025-02-18 14:54:21 +08:00
|
|
|
|
threshold := common.QuotaRemindThreshold
|
|
|
|
|
|
if userCustomThreshold, ok := userSetting[constant2.UserSettingQuotaWarningThreshold]; ok {
|
|
|
|
|
|
threshold = int(userCustomThreshold.(float64))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//noMoreQuota := userCache.Quota-(quota+preConsumedQuota) <= 0
|
|
|
|
|
|
quotaTooLow := false
|
|
|
|
|
|
consumeQuota := quota + preConsumedQuota
|
2025-02-25 20:56:16 +08:00
|
|
|
|
if relayInfo.UserQuota-consumeQuota < threshold {
|
2025-02-18 14:54:21 +08:00
|
|
|
|
quotaTooLow = true
|
|
|
|
|
|
}
|
|
|
|
|
|
if quotaTooLow {
|
|
|
|
|
|
prompt := "您的额度即将用尽"
|
|
|
|
|
|
topUpLink := fmt.Sprintf("%s/topup", setting.ServerAddress)
|
|
|
|
|
|
content := "{{value}},当前剩余额度为 {{value}},为了不影响您的使用,请及时充值。<br/>充值链接:<a href='{{value}}'>{{value}}</a>"
|
2025-02-25 20:56:16 +08:00
|
|
|
|
err := NotifyUser(relayInfo.UserId, relayInfo.UserEmail, relayInfo.UserSetting, dto.NewNotify(dto.NotifyTypeQuotaExceed, prompt, content, []interface{}{prompt, common.FormatQuota(relayInfo.UserQuota), topUpLink, topUpLink}))
|
2025-02-18 14:54:21 +08:00
|
|
|
|
if err != nil {
|
2025-02-25 20:56:16 +08:00
|
|
|
|
common.SysError(fmt.Sprintf("failed to send quota notify to user %d: %s", relayInfo.UserId, err.Error()))
|
2025-02-18 14:54:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|