后端: 1. LLM 独立服务与统一计费出口落地:新增 `cmd/llm`、`client/llm` 与 `services/llm/rpc`,补齐 BillingContext、CreditBalanceGuard、价格规则解析、stream usage 归集与 `credit.charge.requested` outbox 发布,active-scheduler / agent / course / memory / gateway fallback 全部改走 llm zrpc,不再各自本地初始化模型。 2. TokenStore 收口为 Credit 权威账本:新增 credit account / ledger / product / order / price-rule / reward-rule 能力与 Redis 快照缓存,扩展 tokenstore rpc/client 支撑余额快照、消耗看板、商品、订单、流水、价格规则和奖励规则,并接入 LLM charge 事件消费完成 Credit 扣费落账。 3. 计费旧链路下线与网关切口切换:`/token-store` 语义整体切到 `/credit-store`,agent chat 移除旧 TokenQuotaGuard,userauth 的 CheckTokenQuota / AdjustTokenUsage 改为废弃,聊天历史落库不再同步旧 token 额度账本,course 图片解析请求补 user_id 进入新计费口径。 前端: 4. 计划广场从 mock 数据切到真实接口:新增 forum api/types,首页支持真实列表、标签、搜索、防抖、点赞、导入和发布计划,详情页补齐帖子详情、评论树、回复和删除评论链路,同时补上“至少一个标签”的前后端约束与默认标签兜底。 5. 商店页切到 Credit 体系并重做展示:顶部改为余额 + Credit/Token 消耗看板,支持 24h/7d/30d/all 周期切换;套餐区展示原价与当前价;历史区改为当前用户 Credit 流水并支持查看更多,整体视觉和交互同步收口。 仓库: 6. 配置与本地启动体系补齐 llm / outbox 编排:`config.example.yaml` 增加 llm rpc 和统一 outbox service 配置,`dev-common.ps1` 把 llm 纳入多服务依赖并自动建 Kafka topic,`docker-compose.yml` 同步初始化 agent/task/memory/active-scheduler/notification/taskclass-forum/llm/token-store 全量 outbox topic。
212 lines
6.4 KiB
Go
212 lines
6.4 KiB
Go
package llm
|
||
|
||
import (
|
||
"context"
|
||
"log"
|
||
"strings"
|
||
"time"
|
||
|
||
llmcontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/llm"
|
||
sharedevents "github.com/LoveLosita/smartflow/backend/shared/events"
|
||
outboxinfra "github.com/LoveLosita/smartflow/backend/shared/infra/outbox"
|
||
"github.com/cloudwego/eino/schema"
|
||
"github.com/google/uuid"
|
||
)
|
||
|
||
const (
|
||
defaultOutboxMaxRetry = 20
|
||
defaultBillingPersistWindow = 2 * time.Second
|
||
)
|
||
|
||
// ChargeRecorder 负责把一次已完成的 LLM usage 写入 LLM 自己的 outbox。
|
||
type ChargeRecorder struct {
|
||
publisher *outboxinfra.RepositoryPublisher
|
||
providerName string
|
||
pricing UsagePricingResolver
|
||
}
|
||
|
||
type ChargeRecorderOptions struct {
|
||
Repo *outboxinfra.Repository
|
||
MaxRetry int
|
||
ProviderName string
|
||
Pricing UsagePricingResolver
|
||
}
|
||
|
||
func NewChargeRecorder(opts ChargeRecorderOptions) (*ChargeRecorder, error) {
|
||
if err := RegisterCreditChargeRoute(); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
providerName := strings.TrimSpace(opts.ProviderName)
|
||
if providerName == "" {
|
||
providerName = llmcontracts.ProviderNameArk
|
||
}
|
||
|
||
if opts.Repo == nil {
|
||
return &ChargeRecorder{providerName: providerName}, nil
|
||
}
|
||
|
||
maxRetry := opts.MaxRetry
|
||
if maxRetry <= 0 {
|
||
maxRetry = defaultOutboxMaxRetry
|
||
}
|
||
return &ChargeRecorder{
|
||
// 1. 当前 outbox infra 仍是“由归属服务自己 dispatch + consume 自己的 outbox”模型。
|
||
// 2. 因此这里必须让 Repository 按事件归属把 credit 事件写进 token-store 的 outbox,
|
||
// 不能再强绑到 llm 自己的 route,否则消息只会停在 published 而无人消费。
|
||
publisher: outboxinfra.NewRepositoryPublisher(opts.Repo, maxRetry),
|
||
providerName: providerName,
|
||
pricing: opts.Pricing,
|
||
}, nil
|
||
}
|
||
|
||
func RegisterCreditChargeRoute() error {
|
||
return outboxinfra.RegisterEventService(sharedevents.CreditChargeRequestedEventType, outboxinfra.ServiceTokenStore)
|
||
}
|
||
|
||
func (r *ChargeRecorder) RecordTextUsage(ctx context.Context, billing BillingContext, modelAlias, modelName, defaultScene string, usage *schema.TokenUsage) error {
|
||
if usage == nil {
|
||
return nil
|
||
}
|
||
return r.publish(ctx, billing, publishUsageInput{
|
||
ModelAlias: modelAlias,
|
||
ModelName: modelName,
|
||
DefaultScene: defaultScene,
|
||
InputTokens: int64(usage.PromptTokens),
|
||
OutputTokens: int64(usage.CompletionTokens),
|
||
CachedTokens: int64(usage.PromptTokenDetails.CachedTokens),
|
||
ReasoningTokens: int64(usage.CompletionTokensDetails.ReasoningTokens),
|
||
TotalTokens: int64(usage.TotalTokens),
|
||
})
|
||
}
|
||
|
||
func (r *ChargeRecorder) RecordResponsesUsage(ctx context.Context, billing BillingContext, modelAlias, modelName, defaultScene string, usage *ArkResponsesUsage) error {
|
||
if usage == nil {
|
||
return nil
|
||
}
|
||
return r.publish(ctx, billing, publishUsageInput{
|
||
ModelAlias: modelAlias,
|
||
ModelName: modelName,
|
||
DefaultScene: defaultScene,
|
||
InputTokens: usage.InputTokens,
|
||
OutputTokens: usage.OutputTokens,
|
||
TotalTokens: usage.TotalTokens,
|
||
})
|
||
}
|
||
|
||
type publishUsageInput struct {
|
||
ModelAlias string
|
||
ModelName string
|
||
DefaultScene string
|
||
InputTokens int64
|
||
OutputTokens int64
|
||
CachedTokens int64
|
||
ReasoningTokens int64
|
||
TotalTokens int64
|
||
}
|
||
|
||
func (r *ChargeRecorder) publish(ctx context.Context, billing BillingContext, input publishUsageInput) error {
|
||
if r == nil || r.publisher == nil {
|
||
return nil
|
||
}
|
||
|
||
billing = billing.Normalize()
|
||
if billing.UserID == 0 {
|
||
return nil
|
||
}
|
||
|
||
eventID := firstNonEmptyString(strings.TrimSpace(billing.EventID), uuid.NewString())
|
||
requestID := firstNonEmptyString(strings.TrimSpace(billing.RequestID), eventID)
|
||
scene := firstNonEmptyString(strings.TrimSpace(billing.Scene), strings.TrimSpace(input.DefaultScene))
|
||
modelAlias := firstNonEmptyString(strings.TrimSpace(billing.ModelAlias), strings.TrimSpace(input.ModelAlias))
|
||
modelName := firstNonEmptyString(strings.TrimSpace(input.ModelName), modelAlias)
|
||
totalTokens := input.TotalTokens
|
||
if totalTokens <= 0 {
|
||
totalTokens = input.InputTokens + input.OutputTokens
|
||
}
|
||
|
||
payload := sharedevents.CreditChargeRequestedPayload{
|
||
EventID: eventID,
|
||
UserID: billing.UserID,
|
||
Scene: scene,
|
||
RequestID: requestID,
|
||
ConversationID: strings.TrimSpace(billing.ConversationID),
|
||
ModelAlias: modelAlias,
|
||
ProviderName: r.providerName,
|
||
ModelName: modelName,
|
||
InputTokens: input.InputTokens,
|
||
OutputTokens: input.OutputTokens,
|
||
CachedTokens: input.CachedTokens,
|
||
ReasoningTokens: input.ReasoningTokens,
|
||
TotalTokens: totalTokens,
|
||
RMBCostMicros: 0,
|
||
CreditCost: 0,
|
||
TriggeredAt: time.Now(),
|
||
SkipCharge: billing.SkipCharge,
|
||
}
|
||
if !billing.SkipCharge {
|
||
quote, err := r.resolvePriceQuote(ctx, payload)
|
||
if err != nil {
|
||
log.Printf("llm price quote resolve failed: event_id=%s user_id=%d err=%v", payload.EventID, payload.UserID, err)
|
||
} else {
|
||
payload.RMBCostMicros = quote.RMBCostMicros
|
||
payload.CreditCost = quote.CreditCost
|
||
}
|
||
}
|
||
if err := payload.Validate(); err != nil {
|
||
return err
|
||
}
|
||
|
||
recordCtx, cancel := detachedBillingContext(ctx)
|
||
defer cancel()
|
||
return r.publisher.Publish(recordCtx, outboxinfra.PublishRequest{
|
||
EventID: payload.EventID,
|
||
EventType: sharedevents.CreditChargeRequestedEventType,
|
||
EventVersion: sharedevents.CreditChargeEventVersion,
|
||
MessageKey: payload.MessageKey(),
|
||
AggregateID: payload.AggregateID(),
|
||
Payload: payload,
|
||
})
|
||
}
|
||
|
||
func (r *ChargeRecorder) resolvePriceQuote(ctx context.Context, payload sharedevents.CreditChargeRequestedPayload) (UsagePriceQuote, error) {
|
||
if r == nil || r.pricing == nil {
|
||
return UsagePriceQuote{}, nil
|
||
}
|
||
|
||
return r.pricing.Resolve(ctx, UsagePricingInput{
|
||
Scene: payload.Scene,
|
||
ProviderName: payload.ProviderName,
|
||
ModelName: payload.ModelName,
|
||
InputTokens: payload.InputTokens,
|
||
OutputTokens: payload.OutputTokens,
|
||
CachedTokens: payload.CachedTokens,
|
||
ReasoningTokens: payload.ReasoningTokens,
|
||
})
|
||
}
|
||
|
||
func detachedBillingContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||
base := context.Background()
|
||
if ctx != nil {
|
||
base = context.WithoutCancel(ctx)
|
||
}
|
||
return context.WithTimeout(base, defaultBillingPersistWindow)
|
||
}
|
||
|
||
func logChargeRecordError(scene string, err error) {
|
||
if err == nil {
|
||
return
|
||
}
|
||
log.Printf("llm charge record failed: scene=%s err=%v", strings.TrimSpace(scene), err)
|
||
}
|
||
|
||
func firstNonEmptyString(values ...string) string {
|
||
for _, value := range values {
|
||
trimmed := strings.TrimSpace(value)
|
||
if trimmed != "" {
|
||
return trimmed
|
||
}
|
||
}
|
||
return ""
|
||
}
|