后端: 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。
393 lines
12 KiB
Go
393 lines
12 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/LoveLosita/smartflow/backend/services/tokenstore/rpc/pb"
|
|
creditcontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/creditstore"
|
|
"github.com/LoveLosita/smartflow/backend/shared/respond"
|
|
)
|
|
|
|
func (h *Handler) GetCreditBalanceSnapshot(ctx context.Context, req *pb.GetCreditBalanceSnapshotRequest) (*pb.GetCreditBalanceSnapshotResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
snapshot, err := svc.GetCreditBalanceSnapshot(ctx, req.UserId)
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.GetCreditBalanceSnapshotResponse{Snapshot: creditBalanceSnapshotToPB(snapshot)}, nil
|
|
}
|
|
|
|
func (h *Handler) GetCreditConsumptionDashboard(ctx context.Context, req *pb.GetCreditConsumptionDashboardRequest) (*pb.GetCreditConsumptionDashboardResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
dashboard, err := svc.GetCreditConsumptionDashboard(ctx, creditcontracts.GetCreditConsumptionDashboardRequest{
|
|
ActorUserID: req.ActorUserId,
|
|
Period: req.Period,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.GetCreditConsumptionDashboardResponse{Dashboard: creditConsumptionDashboardToPB(dashboard)}, nil
|
|
}
|
|
|
|
func (h *Handler) ListCreditProducts(ctx context.Context, req *pb.ListCreditProductsRequest) (*pb.ListCreditProductsResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
items, err := svc.ListCreditProducts(ctx, req.ActorUserId)
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.ListCreditProductsResponse{Items: creditProductsToPB(items)}, nil
|
|
}
|
|
|
|
func (h *Handler) CreateCreditOrder(ctx context.Context, req *pb.CreateCreditOrderRequest) (*pb.CreateCreditOrderResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
order, err := svc.CreateCreditOrder(ctx, creditcontracts.CreateCreditOrderRequest{
|
|
ActorUserID: req.ActorUserId,
|
|
ProductID: req.ProductId,
|
|
Quantity: int(req.Quantity),
|
|
IdempotencyKey: req.IdempotencyKey,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.CreateCreditOrderResponse{Order: creditOrderToPB(order)}, nil
|
|
}
|
|
|
|
func (h *Handler) ListCreditOrders(ctx context.Context, req *pb.ListCreditOrdersRequest) (*pb.ListCreditOrdersResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
items, page, err := svc.ListCreditOrders(ctx, creditcontracts.ListCreditOrdersRequest{
|
|
ActorUserID: req.ActorUserId,
|
|
Page: int(req.Page),
|
|
PageSize: int(req.PageSize),
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.ListCreditOrdersResponse{
|
|
Items: creditOrdersToPB(items),
|
|
Page: creditPageToPB(page),
|
|
}, nil
|
|
}
|
|
|
|
func (h *Handler) GetCreditOrder(ctx context.Context, req *pb.GetCreditOrderRequest) (*pb.GetCreditOrderResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
order, err := svc.GetCreditOrder(ctx, req.ActorUserId, req.OrderId)
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.GetCreditOrderResponse{Order: creditOrderToPB(order)}, nil
|
|
}
|
|
|
|
func (h *Handler) MockPaidCreditOrder(ctx context.Context, req *pb.MockPaidCreditOrderRequest) (*pb.MockPaidCreditOrderResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
order, err := svc.MockPaidCreditOrder(ctx, creditcontracts.MockPaidCreditOrderRequest{
|
|
ActorUserID: req.ActorUserId,
|
|
OrderID: req.OrderId,
|
|
MockChannel: req.MockChannel,
|
|
IdempotencyKey: req.IdempotencyKey,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.MockPaidCreditOrderResponse{Order: creditOrderToPB(order)}, nil
|
|
}
|
|
|
|
func (h *Handler) ListCreditTransactions(ctx context.Context, req *pb.ListCreditTransactionsRequest) (*pb.ListCreditTransactionsResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
items, page, err := svc.ListCreditTransactions(ctx, creditcontracts.ListCreditTransactionsRequest{
|
|
ActorUserID: req.ActorUserId,
|
|
Page: int(req.Page),
|
|
PageSize: int(req.PageSize),
|
|
Source: req.Source,
|
|
Direction: req.Direction,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.ListCreditTransactionsResponse{
|
|
Items: creditTransactionsToPB(items),
|
|
Page: creditPageToPB(page),
|
|
}, nil
|
|
}
|
|
|
|
func (h *Handler) ListCreditPriceRules(ctx context.Context, req *pb.ListCreditPriceRulesRequest) (*pb.ListCreditPriceRulesResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
items, err := svc.ListCreditPriceRules(ctx, creditcontracts.ListCreditPriceRulesRequest{
|
|
Scene: req.Scene,
|
|
ProviderName: req.ProviderName,
|
|
ModelName: req.ModelName,
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.ListCreditPriceRulesResponse{Items: creditPriceRulesToPB(items)}, nil
|
|
}
|
|
|
|
func (h *Handler) ListCreditRewardRules(ctx context.Context, req *pb.ListCreditRewardRulesRequest) (*pb.ListCreditRewardRulesResponse, error) {
|
|
svc, err := h.service()
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
if req == nil {
|
|
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
|
}
|
|
|
|
items, err := svc.ListCreditRewardRules(ctx, creditcontracts.ListCreditRewardRulesRequest{
|
|
Source: req.Source,
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
return nil, grpcErrorFromServiceError(err)
|
|
}
|
|
return &pb.ListCreditRewardRulesResponse{Items: creditRewardRulesToPB(items)}, nil
|
|
}
|
|
|
|
func creditPageToPB(page creditcontracts.PageResult) *pb.PageResponse {
|
|
return &pb.PageResponse{
|
|
Page: int32(page.Page),
|
|
PageSize: int32(page.PageSize),
|
|
Total: int32(page.Total),
|
|
HasMore: page.HasMore,
|
|
}
|
|
}
|
|
|
|
func creditBalanceSnapshotToPB(snapshot *creditcontracts.CreditBalanceSnapshot) *pb.CreditBalanceSnapshotView {
|
|
if snapshot == nil {
|
|
return nil
|
|
}
|
|
return &pb.CreditBalanceSnapshotView{
|
|
UserId: snapshot.UserID,
|
|
Balance: snapshot.Balance,
|
|
TotalRecharged: snapshot.TotalRecharged,
|
|
TotalRewarded: snapshot.TotalRewarded,
|
|
TotalConsumed: snapshot.TotalConsumed,
|
|
IsBlocked: snapshot.IsBlocked,
|
|
SnapshotSource: snapshot.SnapshotSource,
|
|
UpdatedAt: snapshot.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func creditConsumptionDashboardToPB(view *creditcontracts.CreditConsumptionDashboardView) *pb.CreditConsumptionDashboardView {
|
|
if view == nil {
|
|
return nil
|
|
}
|
|
return &pb.CreditConsumptionDashboardView{
|
|
Period: view.Period,
|
|
CreditConsumed: view.CreditConsumed,
|
|
TokenConsumed: view.TokenConsumed,
|
|
}
|
|
}
|
|
|
|
func creditProductToPB(product creditcontracts.CreditProductView) *pb.CreditProductView {
|
|
return &pb.CreditProductView{
|
|
ProductId: product.ProductID,
|
|
Name: product.Name,
|
|
Description: product.Description,
|
|
CreditAmount: product.CreditAmount,
|
|
PriceCent: product.PriceCent,
|
|
OriginalPriceCent: product.OriginalPriceCent,
|
|
PriceText: product.PriceText,
|
|
Currency: product.Currency,
|
|
Badge: product.Badge,
|
|
Status: product.Status,
|
|
SortOrder: int32(product.SortOrder),
|
|
}
|
|
}
|
|
|
|
func creditProductsToPB(items []creditcontracts.CreditProductView) []*pb.CreditProductView {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]*pb.CreditProductView, 0, len(items))
|
|
for i := range items {
|
|
result = append(result, creditProductToPB(items[i]))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func creditOrderToPB(order *creditcontracts.CreditOrderView) *pb.CreditOrderView {
|
|
if order == nil {
|
|
return nil
|
|
}
|
|
return &pb.CreditOrderView{
|
|
OrderId: order.OrderID,
|
|
OrderNo: order.OrderNo,
|
|
Status: order.Status,
|
|
CreditAmount: order.CreditAmount,
|
|
AmountCent: order.AmountCent,
|
|
PriceText: order.PriceText,
|
|
Currency: order.Currency,
|
|
PaymentMode: order.PaymentMode,
|
|
CreatedAt: order.CreatedAt,
|
|
PaidAt: tokenStringFromPtr(order.PaidAt),
|
|
CreditedAt: tokenStringFromPtr(order.CreditedAt),
|
|
ProductSnapshot: order.ProductSnapshot,
|
|
ProductName: order.ProductName,
|
|
Quantity: int32(order.Quantity),
|
|
}
|
|
}
|
|
|
|
func creditOrdersToPB(items []creditcontracts.CreditOrderView) []*pb.CreditOrderView {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]*pb.CreditOrderView, 0, len(items))
|
|
for i := range items {
|
|
item := items[i]
|
|
result = append(result, creditOrderToPB(&item))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func creditTransactionToPB(item creditcontracts.CreditTransactionView) *pb.CreditTransactionView {
|
|
result := &pb.CreditTransactionView{
|
|
TransactionId: item.TransactionID,
|
|
EventId: item.EventID,
|
|
Source: item.Source,
|
|
SourceLabel: item.SourceLabel,
|
|
Direction: item.Direction,
|
|
Amount: item.Amount,
|
|
BalanceAfter: item.BalanceAfter,
|
|
Status: item.Status,
|
|
Description: item.Description,
|
|
MetadataJson: item.MetadataJSON,
|
|
CreatedAt: item.CreatedAt,
|
|
}
|
|
if item.OrderID != nil {
|
|
result.OrderId = *item.OrderID
|
|
}
|
|
return result
|
|
}
|
|
|
|
func creditTransactionsToPB(items []creditcontracts.CreditTransactionView) []*pb.CreditTransactionView {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]*pb.CreditTransactionView, 0, len(items))
|
|
for i := range items {
|
|
result = append(result, creditTransactionToPB(items[i]))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func creditPriceRuleToPB(rule creditcontracts.CreditPriceRuleView) *pb.CreditPriceRuleView {
|
|
return &pb.CreditPriceRuleView{
|
|
RuleId: rule.RuleID,
|
|
Scene: rule.Scene,
|
|
ProviderName: rule.ProviderName,
|
|
ModelName: rule.ModelName,
|
|
InputPriceMicros: rule.InputPriceMicros,
|
|
OutputPriceMicros: rule.OutputPriceMicros,
|
|
CachedPriceMicros: rule.CachedPriceMicros,
|
|
ReasoningPriceMicros: rule.ReasoningPriceMicros,
|
|
CreditPerYuan: rule.CreditPerYuan,
|
|
Status: rule.Status,
|
|
Priority: int32(rule.Priority),
|
|
Description: rule.Description,
|
|
}
|
|
}
|
|
|
|
func creditPriceRulesToPB(items []creditcontracts.CreditPriceRuleView) []*pb.CreditPriceRuleView {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]*pb.CreditPriceRuleView, 0, len(items))
|
|
for i := range items {
|
|
result = append(result, creditPriceRuleToPB(items[i]))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func creditRewardRuleToPB(rule creditcontracts.CreditRewardRuleView) *pb.CreditRewardRuleView {
|
|
return &pb.CreditRewardRuleView{
|
|
RuleId: rule.RuleID,
|
|
Source: rule.Source,
|
|
Name: rule.Name,
|
|
Amount: rule.Amount,
|
|
Status: rule.Status,
|
|
Description: rule.Description,
|
|
}
|
|
}
|
|
|
|
func creditRewardRulesToPB(items []creditcontracts.CreditRewardRuleView) []*pb.CreditRewardRuleView {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]*pb.CreditRewardRuleView, 0, len(items))
|
|
for i := range items {
|
|
result = append(result, creditRewardRuleToPB(items[i]))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func tokenStringFromPtr(value *string) string {
|
|
if value == nil {
|
|
return ""
|
|
}
|
|
return *value
|
|
}
|