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:
Losita
2026-05-06 20:16:53 +08:00
parent 7d324b77aa
commit 61db646805
104 changed files with 9527 additions and 3925 deletions

View File

@@ -7,13 +7,12 @@ import (
"os/signal"
"syscall"
llmclient "github.com/LoveLosita/smartflow/backend/client/llm"
activeadapters "github.com/LoveLosita/smartflow/backend/services/active_scheduler/core/adapters"
activeschedulerdao "github.com/LoveLosita/smartflow/backend/services/active_scheduler/dao"
activeschedulerrpc "github.com/LoveLosita/smartflow/backend/services/active_scheduler/rpc"
activeschedulersv "github.com/LoveLosita/smartflow/backend/services/active_scheduler/sv"
llmservice "github.com/LoveLosita/smartflow/backend/services/llm"
"github.com/LoveLosita/smartflow/backend/shared/infra/bootstrap"
einoinfra "github.com/LoveLosita/smartflow/backend/shared/infra/eino"
kafkabus "github.com/LoveLosita/smartflow/backend/shared/infra/kafka"
"github.com/spf13/viper"
)
@@ -31,16 +30,17 @@ func main() {
log.Fatalf("failed to connect active-scheduler database: %v", err)
}
aiHub, err := einoinfra.InitEino()
if err != nil {
log.Fatalf("failed to initialize active-scheduler Eino runtime: %v", err)
}
llmService := llmservice.New(llmservice.Options{
AIHub: aiHub,
APIKey: os.Getenv("ARK_API_KEY"),
BaseURL: viper.GetString("agent.baseURL"),
llmService, err := llmclient.NewService(llmclient.ServiceConfig{
ClientConfig: llmclient.ClientConfig{
Endpoints: viper.GetStringSlice("llm.rpc.endpoints"),
Target: viper.GetString("llm.rpc.target"),
Timeout: viper.GetDuration("llm.rpc.timeout"),
},
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
if err != nil {
log.Fatalf("failed to initialize active-scheduler llm client: %v", err)
}
svc, err := activeschedulersv.New(db, llmService, activeschedulersv.Options{
JobScanEvery: viper.GetDuration("activeScheduler.jobScanEvery"),

View File

@@ -5,9 +5,9 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
llmclient "github.com/LoveLosita/smartflow/backend/client/llm"
memoryclient "github.com/LoveLosita/smartflow/backend/client/memory"
scheduleclient "github.com/LoveLosita/smartflow/backend/client/schedule"
taskclient "github.com/LoveLosita/smartflow/backend/client/task"
@@ -34,7 +34,6 @@ import (
schedulesv "github.com/LoveLosita/smartflow/backend/services/schedule/sv"
taskdao "github.com/LoveLosita/smartflow/backend/services/task/dao"
tasksv "github.com/LoveLosita/smartflow/backend/services/task/sv"
einoinfra "github.com/LoveLosita/smartflow/backend/shared/infra/eino"
gormcache "github.com/LoveLosita/smartflow/backend/shared/infra/gormcache"
kafkabus "github.com/LoveLosita/smartflow/backend/shared/infra/kafka"
mysqlinfra "github.com/LoveLosita/smartflow/backend/shared/infra/mysql"
@@ -254,10 +253,6 @@ func (r *agentRuntime) startWorkers(ctx context.Context) error {
log.Println("Agent outbox consumer is disabled")
return nil
}
if r.userAuthClient == nil {
return fmt.Errorf("agent outbox consumer requires userauth zrpc client")
}
// 1. 先登记 agent 自己消费的 handler同时补齐 memory.extract.requested 的服务路由。
// 2. 这里明确只接 agent 边界memory 消费仍归 cmd/memorytask 事件仍是 publish-only 写入 task outbox。
// 3. 注册完成后再启动总线,避免服务一起来就抢先消费到尚未挂 handler 的消息。
@@ -268,7 +263,6 @@ func (r *agentRuntime) startWorkers(ctx context.Context) error {
r.agentRepo,
r.cacheRepo,
nil,
r.userAuthClient,
); err != nil {
return fmt.Errorf("register agent outbox handlers failed: %w", err)
}
@@ -370,16 +364,14 @@ func ensureAgentRuntimeDependencyTables(db *gorm.DB) error {
}
func buildAgentLLMService() (*llmservice.Service, error) {
aiHub, err := einoinfra.InitEino()
if err != nil {
return nil, err
}
return llmservice.New(llmservice.Options{
AIHub: aiHub,
APIKey: os.Getenv("ARK_API_KEY"),
BaseURL: viper.GetString("agent.baseURL"),
return llmclient.NewService(llmclient.ServiceConfig{
ClientConfig: llmclient.ClientConfig{
Endpoints: viper.GetStringSlice("llm.rpc.endpoints"),
Target: viper.GetString("llm.rpc.target"),
Timeout: viper.GetDuration("llm.rpc.timeout"),
},
CourseVisionModel: viper.GetString("courseImport.visionModel"),
}), nil
})
}
func buildAgentRAGService(ctx context.Context) (*ragservice.Service, error) {

View File

@@ -7,10 +7,10 @@ import (
"os/signal"
"syscall"
llmclient "github.com/LoveLosita/smartflow/backend/client/llm"
coursedao "github.com/LoveLosita/smartflow/backend/services/course/dao"
courserpc "github.com/LoveLosita/smartflow/backend/services/course/rpc"
coursesv "github.com/LoveLosita/smartflow/backend/services/course/sv"
llmservice "github.com/LoveLosita/smartflow/backend/services/llm"
rootdao "github.com/LoveLosita/smartflow/backend/services/runtime/dao"
"github.com/LoveLosita/smartflow/backend/shared/infra/bootstrap"
"github.com/spf13/viper"
@@ -33,15 +33,21 @@ func main() {
// 2. scheduleRepo 用于复用既有冲突检查,后续若切 schedule RPC bridge 再替换这里。
courseRepo := coursedao.NewCourseDAO(db)
scheduleRepo := rootdao.NewScheduleDAO(db)
courseImageClient := llmservice.NewArkResponsesClient(
os.Getenv("ARK_API_KEY"),
viper.GetString("agent.baseURL"),
viper.GetString("courseImport.visionModel"),
)
llmService, err := llmclient.NewService(llmclient.ServiceConfig{
ClientConfig: llmclient.ClientConfig{
Endpoints: viper.GetStringSlice("llm.rpc.endpoints"),
Target: viper.GetString("llm.rpc.target"),
Timeout: viper.GetDuration("llm.rpc.timeout"),
},
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
if err != nil {
log.Fatalf("failed to initialize course llm client: %v", err)
}
svc := coursesv.NewCourseService(
courseRepo,
scheduleRepo,
courseImageClient,
llmService.CourseImageResponsesClient(),
coursesv.NewCourseImageParseConfig(
viper.GetInt64("courseImport.maxImageBytes"),
viper.GetInt("courseImport.maxTokens"),

157
backend/cmd/llm/main.go Normal file
View File

@@ -0,0 +1,157 @@
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"
tokenstoreclient "github.com/LoveLosita/smartflow/backend/client/tokenstore"
llmservice "github.com/LoveLosita/smartflow/backend/services/llm"
llmdao "github.com/LoveLosita/smartflow/backend/services/llm/dao"
llmrpc "github.com/LoveLosita/smartflow/backend/services/llm/rpc"
creditcontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/creditstore"
"github.com/LoveLosita/smartflow/backend/shared/infra/bootstrap"
einoinfra "github.com/LoveLosita/smartflow/backend/shared/infra/eino"
kafkabus "github.com/LoveLosita/smartflow/backend/shared/infra/kafka"
outboxinfra "github.com/LoveLosita/smartflow/backend/shared/infra/outbox"
redisinfra "github.com/LoveLosita/smartflow/backend/shared/infra/redis"
"github.com/spf13/viper"
)
func main() {
if err := bootstrap.LoadConfig(); err != nil {
log.Fatalf("failed to load config: %v", err)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
db, err := llmdao.OpenDBFromConfig()
if err != nil {
log.Fatalf("failed to connect llm database: %v", err)
}
redisClient, err := redisinfra.OpenRedisFromConfig()
if err != nil {
log.Fatalf("failed to connect llm redis: %v", err)
}
defer redisClient.Close()
aiHub, err := einoinfra.InitEino()
if err != nil {
log.Fatalf("failed to initialize llm Eino runtime: %v", err)
}
legacyService := llmservice.New(llmservice.Options{
AIHub: aiHub,
APIKey: os.Getenv("ARK_API_KEY"),
BaseURL: viper.GetString("agent.baseURL"),
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
balanceSnapshotProvider := &tokenStoreSnapshotProvider{
cfg: tokenstoreclient.ClientConfig{
Endpoints: viper.GetStringSlice("tokenstore.rpc.endpoints"),
Target: viper.GetString("tokenstore.rpc.target"),
Timeout: viper.GetDuration("tokenstore.rpc.timeout"),
},
}
outboxRepo := outboxinfra.NewRepository(db)
priceRuleDAO := llmdao.NewPriceRuleDAO(db)
dispatchEngine, err := buildLLMOutboxDispatchEngine(outboxRepo)
if err != nil {
log.Fatalf("failed to initialize llm outbox dispatch engine: %v", err)
}
if dispatchEngine != nil {
dispatchEngine.StartDispatch(ctx)
defer dispatchEngine.Close()
log.Println("llm outbox dispatch started")
} else {
log.Println("llm outbox dispatch is disabled")
}
runtimeService, err := llmservice.NewRuntimeService(llmservice.RuntimeServiceOptions{
LegacyService: legacyService,
CacheDAO: llmdao.NewCacheDAO(redisClient),
PriceRuleDAO: priceRuleDAO,
SnapshotProvider: balanceSnapshotProvider,
OutboxRepo: outboxRepo,
OutboxMaxRetry: kafkabus.LoadConfig().MaxRetry,
ProviderName: viper.GetString("llm.providerName"),
LiteModelName: viper.GetString("agent.liteModel"),
ProModelName: viper.GetString("agent.proModel"),
MaxModelName: viper.GetString("agent.maxModel"),
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
if err != nil {
log.Fatalf("failed to initialize llm runtime service: %v", err)
}
server, listenOn, err := llmrpc.NewServer(llmrpc.ServerOptions{
ListenOn: viper.GetString("llm.rpc.listenOn"),
Timeout: viper.GetDuration("llm.rpc.timeout"),
Service: runtimeService,
})
if err != nil {
log.Fatalf("failed to build llm zrpc server: %v", err)
}
defer server.Stop()
go func() {
log.Printf("llm zrpc service starting on %s", listenOn)
server.Start()
}()
<-ctx.Done()
log.Println("llm service stopping")
}
func buildLLMOutboxDispatchEngine(outboxRepo *outboxinfra.Repository) (*outboxinfra.Engine, error) {
kafkaCfg := kafkabus.LoadConfig()
if !kafkaCfg.Enabled || outboxRepo == nil {
return nil, nil
}
route, _ := outboxinfra.ResolveServiceRoute(outboxinfra.ServiceLLM)
kafkaCfg.ServiceName = outboxinfra.ServiceLLM
return outboxinfra.NewEngine(outboxRepo.WithRoute(route), kafkaCfg)
}
type tokenStoreSnapshotProvider struct {
cfg tokenstoreclient.ClientConfig
mu sync.Mutex
client *tokenstoreclient.Client
}
func (p *tokenStoreSnapshotProvider) GetCreditBalanceSnapshot(ctx context.Context, userID uint64) (*creditcontracts.CreditBalanceSnapshot, error) {
client, err := p.ensureClient()
if err != nil {
return nil, err
}
return client.GetCreditBalanceSnapshot(ctx, userID)
}
func (p *tokenStoreSnapshotProvider) ensureClient() (*tokenstoreclient.Client, error) {
if p == nil {
return nil, nil
}
p.mu.Lock()
defer p.mu.Unlock()
if p.client != nil {
return p.client, nil
}
client, err := tokenstoreclient.NewClient(p.cfg)
if err != nil {
return nil, err
}
p.client = client
return p.client, nil
}

View File

@@ -8,6 +8,7 @@ import (
"os/signal"
"syscall"
llmclient "github.com/LoveLosita/smartflow/backend/client/llm"
llmservice "github.com/LoveLosita/smartflow/backend/services/llm"
memorymodule "github.com/LoveLosita/smartflow/backend/services/memory"
memorydao "github.com/LoveLosita/smartflow/backend/services/memory/dao"
@@ -17,7 +18,6 @@ import (
ragservice "github.com/LoveLosita/smartflow/backend/services/rag"
ragconfig "github.com/LoveLosita/smartflow/backend/services/rag/config"
"github.com/LoveLosita/smartflow/backend/shared/infra/bootstrap"
einoinfra "github.com/LoveLosita/smartflow/backend/shared/infra/eino"
kafkabus "github.com/LoveLosita/smartflow/backend/shared/infra/kafka"
outboxinfra "github.com/LoveLosita/smartflow/backend/shared/infra/outbox"
"github.com/spf13/viper"
@@ -96,20 +96,21 @@ func main() {
//
// 说明:
// 1. CP1 先复用既有 llm-service canonical 入口,不在 memory 服务里重建模型调用封装;
// 2. 当前启动入口与 cmd/start.go / cmd/active-scheduler 都需要 Eino 初始化,后续若出现第三处重复装配,应抽公共 bootstrap
// 2. 现在统一改走独立 llm zrpc clientmemory 进程不再本地初始化 AIHub
// 3. 返回 ProClient 是因为现有 memory.Module 只需要 llmservice.Client不需要完整 Service。
func buildMemoryLLMClient() (*llmservice.Client, error) {
aiHub, err := einoinfra.InitEino()
remoteService, err := llmclient.NewService(llmclient.ServiceConfig{
ClientConfig: llmclient.ClientConfig{
Endpoints: viper.GetStringSlice("llm.rpc.endpoints"),
Target: viper.GetString("llm.rpc.target"),
Timeout: viper.GetDuration("llm.rpc.timeout"),
},
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
if err != nil {
return nil, err
}
llmService := llmservice.New(llmservice.Options{
AIHub: aiHub,
APIKey: os.Getenv("ARK_API_KEY"),
BaseURL: viper.GetString("agent.baseURL"),
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
return llmService.ProClient(), nil
return remoteService.ProClient(), nil
}
// buildMemoryRAGRuntime 初始化 memory 检索与向量同步使用的 RAG Runtime。

View File

@@ -14,6 +14,7 @@ import (
activeschedulerclient "github.com/LoveLosita/smartflow/backend/client/activescheduler"
agentclient "github.com/LoveLosita/smartflow/backend/client/agent"
courseclient "github.com/LoveLosita/smartflow/backend/client/course"
llmclient "github.com/LoveLosita/smartflow/backend/client/llm"
memoryclient "github.com/LoveLosita/smartflow/backend/client/memory"
notificationclient "github.com/LoveLosita/smartflow/backend/client/notification"
scheduleclient "github.com/LoveLosita/smartflow/backend/client/schedule"
@@ -53,7 +54,6 @@ import (
taskdao "github.com/LoveLosita/smartflow/backend/services/task/dao"
tasksv "github.com/LoveLosita/smartflow/backend/services/task/sv"
"github.com/LoveLosita/smartflow/backend/shared/infra/bootstrap"
einoinfra "github.com/LoveLosita/smartflow/backend/shared/infra/eino"
gormcache "github.com/LoveLosita/smartflow/backend/shared/infra/gormcache"
kafkabus "github.com/LoveLosita/smartflow/backend/shared/infra/kafka"
outboxinfra "github.com/LoveLosita/smartflow/backend/shared/infra/outbox"
@@ -273,16 +273,17 @@ func buildRuntime(ctx context.Context) (*appRuntime, error) {
if shouldBuildGatewayAgentFallback() {
log.Println("Gateway agent RPC fallback is enabled; building local AgentService compatibility path")
aiHub, err := einoinfra.InitEino()
if err != nil {
return nil, fmt.Errorf("failed to initialize Eino: %w", err)
}
llmService := llmservice.New(llmservice.Options{
AIHub: aiHub,
APIKey: os.Getenv("ARK_API_KEY"),
BaseURL: viper.GetString("agent.baseURL"),
llmService, err := llmclient.NewService(llmclient.ServiceConfig{
ClientConfig: llmclient.ClientConfig{
Endpoints: viper.GetStringSlice("llm.rpc.endpoints"),
Target: viper.GetString("llm.rpc.target"),
Timeout: viper.GetDuration("llm.rpc.timeout"),
},
CourseVisionModel: viper.GetString("courseImport.visionModel"),
})
if err != nil {
return nil, fmt.Errorf("failed to initialize llm zrpc client: %w", err)
}
ragService, err := buildRAGService(ctx)
if err != nil {

View File

@@ -29,7 +29,19 @@ func main() {
log.Fatalf("failed to connect tokenstore database: %v", err)
}
svc := tokenstoresv.New(tokenstoresv.Options{DB: db})
var creditCache *tokenstoredao.CreditCacheDAO
rdb, err := tokenstoredao.OpenRedisFromConfig()
if err != nil {
log.Printf("tokenstore redis is unavailable, credit cache disabled: %v", err)
} else {
creditCache = tokenstoredao.NewCreditCacheDAO(rdb)
log.Println("Tokenstore credit cache enabled")
}
svc := tokenstoresv.New(tokenstoresv.Options{
DB: db,
CreditCache: creditCache,
})
outboxRepo := outboxinfra.NewRepository(db)
eventBus, err := outboxinfra.NewEventBus(outboxRepo, kafkabus.LoadConfig())
@@ -40,6 +52,9 @@ func main() {
if err := tokenstoresv.RegisterForumRewardHandlers(eventBus, outboxRepo, svc); err != nil {
log.Fatalf("failed to register tokenstore outbox handlers: %v", err)
}
if err := tokenstoresv.RegisterCreditChargeHandlers(eventBus, outboxRepo, svc); err != nil {
log.Fatalf("failed to register credit charge handlers: %v", err)
}
eventBus.Start(ctx)
defer eventBus.Close()
log.Println("Tokenstore outbox consumer started")