后端: 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。
170 lines
5.4 KiB
Go
170 lines
5.4 KiB
Go
package outbox
|
||
|
||
import (
|
||
"strings"
|
||
)
|
||
|
||
const (
|
||
ServiceNameAgent = "agent"
|
||
ServiceNameTask = "task"
|
||
ServiceNameMemory = "memory"
|
||
ServiceNameActiveScheduler = "active-scheduler"
|
||
ServiceNameNotification = "notification"
|
||
ServiceNameTaskClassForum = "taskclass-forum"
|
||
ServiceNameLLM = "llm"
|
||
ServiceNameTokenStore = "token-store"
|
||
)
|
||
|
||
// ServiceRoute 描述一个 outbox 服务的终态路由信息。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只承载服务级 outbox 的 table/topic/group 目录信息;
|
||
// 2. 不承载 handler、事务或 Kafka 连接对象;
|
||
// 3. 允许上层按事件类型先查服务,再由服务查到自己的物理资源。
|
||
type ServiceRoute struct {
|
||
ServiceName string
|
||
TableName string
|
||
Topic string
|
||
GroupID string
|
||
}
|
||
|
||
var builtinServiceRoutes = map[string]ServiceRoute{
|
||
ServiceNameAgent: {
|
||
ServiceName: ServiceNameAgent,
|
||
TableName: "agent_outbox_messages",
|
||
Topic: "smartflow.agent.outbox",
|
||
GroupID: "smartflow-agent-outbox-consumer",
|
||
},
|
||
ServiceNameTask: {
|
||
ServiceName: ServiceNameTask,
|
||
TableName: "task_outbox_messages",
|
||
Topic: "smartflow.task.outbox",
|
||
GroupID: "smartflow-task-outbox-consumer",
|
||
},
|
||
ServiceNameMemory: {
|
||
ServiceName: ServiceNameMemory,
|
||
TableName: "memory_outbox_messages",
|
||
Topic: "smartflow.memory.outbox",
|
||
GroupID: "smartflow-memory-outbox-consumer",
|
||
},
|
||
ServiceNameActiveScheduler: {
|
||
ServiceName: ServiceNameActiveScheduler,
|
||
TableName: "active_scheduler_outbox_messages",
|
||
Topic: "smartflow.active-scheduler.outbox",
|
||
GroupID: "smartflow-active-scheduler-outbox-consumer",
|
||
},
|
||
ServiceNameNotification: {
|
||
ServiceName: ServiceNameNotification,
|
||
TableName: "notification_outbox_messages",
|
||
Topic: "smartflow.notification.outbox",
|
||
GroupID: "smartflow-notification-outbox-consumer",
|
||
},
|
||
ServiceNameTaskClassForum: {
|
||
ServiceName: ServiceNameTaskClassForum,
|
||
TableName: "taskclass_forum_outbox_messages",
|
||
Topic: "smartflow.taskclass-forum.outbox",
|
||
GroupID: "smartflow-taskclass-forum-outbox-consumer",
|
||
},
|
||
ServiceNameLLM: {
|
||
ServiceName: ServiceNameLLM,
|
||
TableName: "llm_outbox_messages",
|
||
Topic: "smartflow.llm.outbox",
|
||
GroupID: "smartflow-llm-outbox-consumer",
|
||
},
|
||
ServiceNameTokenStore: {
|
||
ServiceName: ServiceNameTokenStore,
|
||
TableName: "token_store_outbox_messages",
|
||
Topic: "smartflow.token-store.outbox",
|
||
GroupID: "smartflow-token-store-outbox-consumer",
|
||
},
|
||
}
|
||
|
||
// DefaultServiceRoutes 返回当前已知服务的默认路由清单。
|
||
//
|
||
// 说明:
|
||
// 1. 这里是“目录初始值”,用于自动建表和首次注册时兜底;
|
||
// 2. 运行时若显式注册了服务路由,会以显式注册结果为准;
|
||
// 3. 返回值是拷贝,调用方可安全遍历,不会污染全局目录。
|
||
func DefaultServiceRoutes() []ServiceRoute {
|
||
return []ServiceRoute{
|
||
builtinServiceRoutes[ServiceNameAgent],
|
||
builtinServiceRoutes[ServiceNameTask],
|
||
builtinServiceRoutes[ServiceNameMemory],
|
||
builtinServiceRoutes[ServiceNameActiveScheduler],
|
||
builtinServiceRoutes[ServiceNameNotification],
|
||
builtinServiceRoutes[ServiceNameTaskClassForum],
|
||
builtinServiceRoutes[ServiceNameLLM],
|
||
builtinServiceRoutes[ServiceNameTokenStore],
|
||
}
|
||
}
|
||
|
||
// DefaultServiceRoute 根据服务名生成终态路由。
|
||
//
|
||
// 规则:
|
||
// 1. 已知服务直接返回约定映射;
|
||
// 2. 未知服务按命名约定生成 table/topic/group,避免继续落回共享 topic;
|
||
// 3. 空服务名回退到 agent 兼容路径,保住历史单体模式。
|
||
func DefaultServiceRoute(serviceName string) ServiceRoute {
|
||
serviceName = normalizeServiceName(serviceName)
|
||
if serviceName == "" {
|
||
serviceName = ServiceNameAgent
|
||
}
|
||
if route, ok := builtinServiceRoutes[serviceName]; ok {
|
||
return route
|
||
}
|
||
|
||
tablePrefix := strings.NewReplacer("-", "_").Replace(serviceName)
|
||
if tablePrefix == "" {
|
||
tablePrefix = ServiceNameAgent
|
||
}
|
||
|
||
return ServiceRoute{
|
||
ServiceName: serviceName,
|
||
TableName: tablePrefix + "_outbox_messages",
|
||
Topic: "smartflow." + serviceName + ".outbox",
|
||
GroupID: "smartflow-" + serviceName + "-outbox-consumer",
|
||
}
|
||
}
|
||
|
||
func normalizeServiceName(serviceName string) string {
|
||
return strings.TrimSpace(serviceName)
|
||
}
|
||
|
||
// normalizeServiceRoute 把空字段补成可执行的默认值。
|
||
//
|
||
// 说明:
|
||
// 1. 只做字符串裁剪和缺省补齐,不做注册副作用;
|
||
// 2. 服务名为空时只保留历史兼容路径,不强行把它当成新服务;
|
||
// 3. 这一步是 route 目录的最后一道兜底,避免上层拿到半成品路由。
|
||
func normalizeServiceRoute(route ServiceRoute) ServiceRoute {
|
||
route.ServiceName = normalizeServiceName(route.ServiceName)
|
||
route.TableName = strings.TrimSpace(route.TableName)
|
||
route.Topic = strings.TrimSpace(route.Topic)
|
||
route.GroupID = strings.TrimSpace(route.GroupID)
|
||
|
||
if route.ServiceName == "" {
|
||
if route.TableName == "" {
|
||
route.TableName = builtinServiceRoutes[ServiceNameAgent].TableName
|
||
}
|
||
if route.Topic == "" {
|
||
route.Topic = builtinServiceRoutes[ServiceNameAgent].Topic
|
||
}
|
||
if route.GroupID == "" {
|
||
route.GroupID = builtinServiceRoutes[ServiceNameAgent].GroupID
|
||
}
|
||
return route
|
||
}
|
||
|
||
defaultRoute := DefaultServiceRoute(route.ServiceName)
|
||
if route.TableName == "" {
|
||
route.TableName = defaultRoute.TableName
|
||
}
|
||
if route.Topic == "" {
|
||
route.Topic = defaultRoute.Topic
|
||
}
|
||
if route.GroupID == "" {
|
||
route.GroupID = defaultRoute.GroupID
|
||
}
|
||
return route
|
||
}
|