后端: 1. Credit 价格规则补齐利润率与实际计费单价语义:新增 `profit_rate_bps` 与 `charge_*_price_micros` 展示字段,下沉共享价格推导 helper,tokenstore rpc/client/proto/model/default rule 全链路同步,LLM usage 扣费统一改按加价后的 charge 单价换算。 2. task-class 更新链路修正全量覆盖与归属校验:`runtime/conv` 保留 item id,DAO 更新前显式校验 task-class 与 item 归属,改用显式字段 map 落库 nil/空切片/零值,避免 `RowsAffected=0` 误判越权,同时补齐任务项可编辑字段更新。 3. GormCache task-class 失效补空 user_id 保护:更新语句缺少模型上下文时直接跳过失效,避免缓存插件因空指针影响主事务。 前端: 4. 课表中心补齐任务类编辑能力:新增 `updateTaskClass` API,创建弹窗支持编辑态回填与 item id 提交,日程页支持先拉详情再编辑并在保存后刷新任务类详情与列表。 5. 计划广场详情补点赞交互与奖励提示:详情页新增点赞/取消点赞按钮、奖励反馈文案与计数展示,论坛类型补 `reward_hint`,评论区与帖子作者头像统一接入兜底头像工具。 6. 品牌与展示细节收口:侧边栏与 favicon 切到项目 logo,首页标题改为 `SmartMate`,主面板缩放上限微调,论坛列表头像显示与整体品牌观感同步统一。
208 lines
5.0 KiB
Go
208 lines
5.0 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
llmdao "github.com/LoveLosita/smartflow/backend/services/llm/dao"
|
|
creditcontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/creditstore"
|
|
)
|
|
|
|
const (
|
|
defaultPriceRuleCacheTTL = time.Minute
|
|
tokenPriceScalePer1K = int64(1000)
|
|
rmbMicrosPerYuan = int64(1_000_000)
|
|
)
|
|
|
|
type UsagePricingInput struct {
|
|
Scene string
|
|
ProviderName string
|
|
ModelName string
|
|
InputTokens int64
|
|
OutputTokens int64
|
|
CachedTokens int64
|
|
ReasoningTokens int64
|
|
}
|
|
|
|
type UsagePriceQuote struct {
|
|
RuleID uint64
|
|
RMBCostMicros int64
|
|
CreditCost int64
|
|
MatchedScene string
|
|
MatchedProvider string
|
|
MatchedModel string
|
|
}
|
|
|
|
type UsagePricingResolver interface {
|
|
Resolve(ctx context.Context, input UsagePricingInput) (UsagePriceQuote, error)
|
|
}
|
|
|
|
type CreditPriceResolverOptions struct {
|
|
DAO *llmdao.PriceRuleDAO
|
|
CacheTTL time.Duration
|
|
}
|
|
|
|
type CreditPriceResolver struct {
|
|
dao *llmdao.PriceRuleDAO
|
|
cacheTTL time.Duration
|
|
|
|
mu sync.RWMutex
|
|
cachedAt time.Time
|
|
cachedSet []llmdao.CreditPriceRule
|
|
}
|
|
|
|
func NewCreditPriceResolver(opts CreditPriceResolverOptions) *CreditPriceResolver {
|
|
cacheTTL := opts.CacheTTL
|
|
if cacheTTL <= 0 {
|
|
cacheTTL = defaultPriceRuleCacheTTL
|
|
}
|
|
return &CreditPriceResolver{
|
|
dao: opts.DAO,
|
|
cacheTTL: cacheTTL,
|
|
}
|
|
}
|
|
|
|
func (r *CreditPriceResolver) Resolve(ctx context.Context, input UsagePricingInput) (UsagePriceQuote, error) {
|
|
if r == nil || r.dao == nil {
|
|
return UsagePriceQuote{}, nil
|
|
}
|
|
|
|
rules, err := r.loadRules(ctx)
|
|
if err != nil {
|
|
return UsagePriceQuote{}, err
|
|
}
|
|
if len(rules) == 0 {
|
|
return UsagePriceQuote{}, nil
|
|
}
|
|
|
|
scene := strings.TrimSpace(input.Scene)
|
|
providerName := strings.TrimSpace(input.ProviderName)
|
|
modelName := strings.TrimSpace(input.ModelName)
|
|
|
|
for _, rule := range rules {
|
|
if !matchesPriceRuleField(rule.Scene, scene) {
|
|
continue
|
|
}
|
|
if !matchesPriceRuleField(rule.ProviderName, providerName) {
|
|
continue
|
|
}
|
|
if !matchesPriceRuleField(rule.ModelName, modelName) {
|
|
continue
|
|
}
|
|
return quoteUsagePrice(rule, input), nil
|
|
}
|
|
|
|
return UsagePriceQuote{}, nil
|
|
}
|
|
|
|
func (r *CreditPriceResolver) loadRules(ctx context.Context) ([]llmdao.CreditPriceRule, error) {
|
|
now := time.Now()
|
|
|
|
r.mu.RLock()
|
|
if len(r.cachedSet) > 0 && now.Sub(r.cachedAt) < r.cacheTTL {
|
|
rules := clonePriceRules(r.cachedSet)
|
|
r.mu.RUnlock()
|
|
return rules, nil
|
|
}
|
|
r.mu.RUnlock()
|
|
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
|
|
if len(r.cachedSet) > 0 && now.Sub(r.cachedAt) < r.cacheTTL {
|
|
return clonePriceRules(r.cachedSet), nil
|
|
}
|
|
|
|
rules, err := r.dao.ListActiveRules(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r.cachedSet = clonePriceRules(rules)
|
|
r.cachedAt = now
|
|
return clonePriceRules(r.cachedSet), nil
|
|
}
|
|
|
|
func clonePriceRules(input []llmdao.CreditPriceRule) []llmdao.CreditPriceRule {
|
|
if len(input) == 0 {
|
|
return nil
|
|
}
|
|
output := make([]llmdao.CreditPriceRule, len(input))
|
|
copy(output, input)
|
|
return output
|
|
}
|
|
|
|
func matchesPriceRuleField(ruleValue string, actual string) bool {
|
|
ruleValue = strings.TrimSpace(ruleValue)
|
|
actual = strings.TrimSpace(actual)
|
|
|
|
if ruleValue == "" || ruleValue == "*" {
|
|
return true
|
|
}
|
|
return strings.EqualFold(ruleValue, actual)
|
|
}
|
|
|
|
func quoteUsagePrice(rule llmdao.CreditPriceRule, input UsagePricingInput) UsagePriceQuote {
|
|
inputTokens := maxInt64(input.InputTokens, 0)
|
|
outputTokens := maxInt64(input.OutputTokens, 0)
|
|
cachedTokens := clampInt64(input.CachedTokens, 0, inputTokens)
|
|
reasoningTokens := clampInt64(input.ReasoningTokens, 0, outputTokens)
|
|
|
|
nonCachedInputTokens := inputTokens - cachedTokens
|
|
nonReasoningOutputTokens := outputTokens - reasoningTokens
|
|
|
|
chargePrices := creditcontracts.DeriveChargePriceMicrosSet(
|
|
rule.InputPriceMicros,
|
|
rule.OutputPriceMicros,
|
|
rule.CachedPriceMicros,
|
|
rule.ReasoningPriceMicros,
|
|
rule.ProfitRateBps,
|
|
)
|
|
|
|
totalMicrosScaled := nonCachedInputTokens*maxInt64(chargePrices.InputChargePriceMicros, 0) +
|
|
cachedTokens*maxInt64(chargePrices.CachedChargePriceMicros, 0) +
|
|
nonReasoningOutputTokens*maxInt64(chargePrices.OutputChargePriceMicros, 0) +
|
|
reasoningTokens*maxInt64(chargePrices.ReasoningChargePriceMicros, 0)
|
|
|
|
rmbCostMicros := ceilDivInt64(totalMicrosScaled, tokenPriceScalePer1K)
|
|
creditCost := int64(0)
|
|
if rmbCostMicros > 0 && rule.CreditPerYuan > 0 {
|
|
creditCost = ceilDivInt64(rmbCostMicros*rule.CreditPerYuan, rmbMicrosPerYuan)
|
|
}
|
|
|
|
return UsagePriceQuote{
|
|
RuleID: rule.ID,
|
|
RMBCostMicros: rmbCostMicros,
|
|
CreditCost: creditCost,
|
|
MatchedScene: strings.TrimSpace(rule.Scene),
|
|
MatchedProvider: strings.TrimSpace(rule.ProviderName),
|
|
MatchedModel: strings.TrimSpace(rule.ModelName),
|
|
}
|
|
}
|
|
|
|
func ceilDivInt64(numerator int64, denominator int64) int64 {
|
|
if numerator <= 0 || denominator <= 0 {
|
|
return 0
|
|
}
|
|
return (numerator + denominator - 1) / denominator
|
|
}
|
|
|
|
func clampInt64(value int64, minValue int64, maxValue int64) int64 {
|
|
if value < minValue {
|
|
return minValue
|
|
}
|
|
if value > maxValue {
|
|
return maxValue
|
|
}
|
|
return value
|
|
}
|
|
|
|
func maxInt64(value int64, minValue int64) int64 {
|
|
if value < minValue {
|
|
return minValue
|
|
}
|
|
return value
|
|
}
|