Version: 0.9.80.dev.260506
后端: 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。
This commit is contained in:
107
backend/services/llm/dao/cache.go
Normal file
107
backend/services/llm/dao/cache.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
const defaultCreditSnapshotTTL = 10 * time.Minute
|
||||
|
||||
// CreditBalanceSnapshot 是 LLM 准入守卫读取的余额快照。
|
||||
type CreditBalanceSnapshot struct {
|
||||
AvailableCredit int64 `json:"balance"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// CacheDAO 只承载 LLM 服务私有的 Redis Key 读写。
|
||||
type CacheDAO struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewCacheDAO(client *redis.Client) *CacheDAO {
|
||||
return &CacheDAO{client: client}
|
||||
}
|
||||
|
||||
func userCreditBalanceSnapshotKey(userID uint64) string {
|
||||
return fmt.Sprintf("smartflow:credit_balance_snapshot:%d", userID)
|
||||
}
|
||||
|
||||
func userCreditBlockedKey(userID uint64) string {
|
||||
return fmt.Sprintf("smartflow:credit_blocked:%d", userID)
|
||||
}
|
||||
|
||||
func (d *CacheDAO) GetUserCreditBalanceSnapshot(ctx context.Context, userID uint64) (*CreditBalanceSnapshot, bool, error) {
|
||||
if d == nil || d.client == nil || userID == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
value, err := d.client.Get(ctx, userCreditBalanceSnapshotKey(userID)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
var snapshot CreditBalanceSnapshot
|
||||
if err = json.Unmarshal([]byte(value), &snapshot); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return &snapshot, true, nil
|
||||
}
|
||||
|
||||
func (d *CacheDAO) SetUserCreditBalanceSnapshot(ctx context.Context, userID uint64, snapshot CreditBalanceSnapshot, ttl time.Duration) error {
|
||||
if d == nil || d.client == nil || userID == 0 {
|
||||
return nil
|
||||
}
|
||||
if ttl <= 0 {
|
||||
ttl = defaultCreditSnapshotTTL
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(snapshot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.client.Set(ctx, userCreditBalanceSnapshotKey(userID), raw, ttl).Err()
|
||||
}
|
||||
|
||||
func (d *CacheDAO) DeleteUserCreditBalanceSnapshot(ctx context.Context, userID uint64) error {
|
||||
if d == nil || d.client == nil || userID == 0 {
|
||||
return nil
|
||||
}
|
||||
return d.client.Del(ctx, userCreditBalanceSnapshotKey(userID)).Err()
|
||||
}
|
||||
|
||||
func (d *CacheDAO) IsUserCreditBlocked(ctx context.Context, userID uint64) (bool, error) {
|
||||
if d == nil || d.client == nil || userID == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
value, err := d.client.Get(ctx, userCreditBlockedKey(userID)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return value == "1", nil
|
||||
}
|
||||
|
||||
func (d *CacheDAO) SetUserCreditBlocked(ctx context.Context, userID uint64, ttl time.Duration) error {
|
||||
if d == nil || d.client == nil || userID == 0 {
|
||||
return nil
|
||||
}
|
||||
return d.client.Set(ctx, userCreditBlockedKey(userID), "1", ttl).Err()
|
||||
}
|
||||
|
||||
func (d *CacheDAO) DeleteUserCreditBlocked(ctx context.Context, userID uint64) error {
|
||||
if d == nil || d.client == nil || userID == 0 {
|
||||
return nil
|
||||
}
|
||||
return d.client.Del(ctx, userCreditBlockedKey(userID)).Err()
|
||||
}
|
||||
42
backend/services/llm/dao/connect.go
Normal file
42
backend/services/llm/dao/connect.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
|
||||
mysqlinfra "github.com/LoveLosita/smartflow/backend/shared/infra/mysql"
|
||||
outboxinfra "github.com/LoveLosita/smartflow/backend/shared/infra/outbox"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OpenDBFromConfig 负责打开 LLM 独立服务需要的数据库连接。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只初始化通用 MySQL 连接并补齐 LLM 自己的 outbox 表;
|
||||
// 2. 不负责启动 Kafka relay,也不负责装配 Redis/模型客户端;
|
||||
// 3. 当前阶段不额外声明业务私表,避免和主代理后续 Credit 表迁移交叉。
|
||||
func OpenDBFromConfig() (*gorm.DB, error) {
|
||||
db, err := mysqlinfra.OpenDBFromConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = autoMigrateLLMOutboxTable(db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func autoMigrateLLMOutboxTable(db *gorm.DB) error {
|
||||
if db == nil {
|
||||
return fmt.Errorf("llm database is not initialized")
|
||||
}
|
||||
|
||||
cfg, ok := outboxinfra.ResolveServiceConfig(outboxinfra.ServiceLLM)
|
||||
if !ok {
|
||||
return fmt.Errorf("resolve llm outbox config failed")
|
||||
}
|
||||
if err := db.Table(cfg.TableName).AutoMigrate(&model.AgentOutboxMessage{}); err != nil {
|
||||
return fmt.Errorf("auto migrate llm outbox table failed for %s (%s): %w", cfg.Name, cfg.TableName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
53
backend/services/llm/dao/pricing.go
Normal file
53
backend/services/llm/dao/pricing.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const creditPriceRuleStatusActive = "active"
|
||||
|
||||
type CreditPriceRule struct {
|
||||
ID uint64 `gorm:"column:id"`
|
||||
Scene string `gorm:"column:scene"`
|
||||
ProviderName string `gorm:"column:provider_name"`
|
||||
ModelName string `gorm:"column:model_name"`
|
||||
InputPriceMicros int64 `gorm:"column:input_price_micros"`
|
||||
OutputPriceMicros int64 `gorm:"column:output_price_micros"`
|
||||
CachedPriceMicros int64 `gorm:"column:cached_price_micros"`
|
||||
ReasoningPriceMicros int64 `gorm:"column:reasoning_price_micros"`
|
||||
CreditPerYuan int64 `gorm:"column:credit_per_yuan"`
|
||||
Status string `gorm:"column:status"`
|
||||
Priority int `gorm:"column:priority"`
|
||||
Description string `gorm:"column:description"`
|
||||
}
|
||||
|
||||
func (CreditPriceRule) TableName() string {
|
||||
return "credit_price_rules"
|
||||
}
|
||||
|
||||
type PriceRuleDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPriceRuleDAO(db *gorm.DB) *PriceRuleDAO {
|
||||
return &PriceRuleDAO{db: db}
|
||||
}
|
||||
|
||||
func (d *PriceRuleDAO) ListActiveRules(ctx context.Context) ([]CreditPriceRule, error) {
|
||||
if d == nil || d.db == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var rules []CreditPriceRule
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&CreditPriceRule{}).
|
||||
Where("status = ?", creditPriceRuleStatusActive).
|
||||
Order("priority DESC, id ASC").
|
||||
Find(&rules).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rules, nil
|
||||
}
|
||||
Reference in New Issue
Block a user