后端: 1. 服务级 outbox 基础设施全量落地——新增 service route / service catalog / route registry,重构 outbox engine、repository、event bus 和 model,按 `event_type -> service -> table/topic/group` 统一写入与投递,保留 `agent` 兼容壳但不再依赖共享 outbox 2. Kafka 投递、消费与启动装配同步切换——更新 kafka config、consumer、envelope,接入服务级 topic 与 consumer group,并同步调整 mysql 初始化、start/main/router 装配,保证各服务 relay / consumer 独立装配 3. 业务事件处理器按服务归属重接新 bus——`active-scheduler` 触发链路,以及 `agent` / `memory` / `notification` / `task` 相关 outbox handler 统一切到新路由注册与服务目录,避免新流量回流共享表 4. 同步更新《微服务四步迁移与第二阶段并行开发计划》,把阶段 1 改成当前基线并补齐结构图、阶段快照、风险回退和多代理执行口径
131 lines
3.9 KiB
Go
131 lines
3.9 KiB
Go
package events
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"log"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/dao"
|
||
kafkabus "github.com/LoveLosita/smartflow/backend/infra/kafka"
|
||
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
|
||
"github.com/LoveLosita/smartflow/backend/model"
|
||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
)
|
||
|
||
const (
|
||
// EventTypeAgentStateSnapshotPersist 是"agent 状态快照持久化"的业务事件类型。
|
||
EventTypeAgentStateSnapshotPersist = "agent.state.snapshot.persist"
|
||
)
|
||
|
||
// AgentStateSnapshotPayload 是 outbox 事件的业务载荷。
|
||
type AgentStateSnapshotPayload struct {
|
||
ConversationID string `json:"conversation_id"`
|
||
UserID int `json:"user_id"`
|
||
Phase string `json:"phase"`
|
||
SnapshotJSON string `json:"snapshot_json"`
|
||
}
|
||
|
||
// RegisterAgentStateSnapshotHandler 注册"agent 状态快照持久化"消费者处理器。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只负责快照写入 agent_state_snapshot_records 表;
|
||
// 2. 使用 upsert 语义,同一 conversation_id 只保留最新快照;
|
||
// 3. 通过 outbox 通用消费事务保证"业务写入 + consumed 推进"原子一致。
|
||
func RegisterAgentStateSnapshotHandler(
|
||
bus OutboxBus,
|
||
outboxRepo *outboxinfra.Repository,
|
||
repoManager *dao.RepoManager,
|
||
) error {
|
||
if bus == nil {
|
||
return errors.New("event bus is nil")
|
||
}
|
||
if outboxRepo == nil {
|
||
return errors.New("outbox repository is nil")
|
||
}
|
||
if repoManager == nil {
|
||
return errors.New("repo manager is nil")
|
||
}
|
||
eventOutboxRepo, err := scopedOutboxRepoForEvent(outboxRepo, EventTypeAgentStateSnapshotPersist)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
handler := func(ctx context.Context, envelope kafkabus.Envelope) error {
|
||
var payload AgentStateSnapshotPayload
|
||
if unmarshalErr := json.Unmarshal(envelope.Payload, &payload); unmarshalErr != nil {
|
||
_ = eventOutboxRepo.MarkDead(ctx, envelope.OutboxID, "解析快照载荷失败: "+unmarshalErr.Error())
|
||
return nil
|
||
}
|
||
|
||
return eventOutboxRepo.ConsumeAndMarkConsumed(ctx, envelope.OutboxID, func(tx *gorm.DB) error {
|
||
record := model.AgentStateSnapshotRecord{
|
||
ConversationID: payload.ConversationID,
|
||
UserID: payload.UserID,
|
||
Phase: payload.Phase,
|
||
SnapshotJSON: payload.SnapshotJSON,
|
||
}
|
||
return tx.Clauses(clause.OnConflict{
|
||
Columns: []clause.Column{{Name: "conversation_id"}},
|
||
DoUpdates: clause.AssignmentColumns([]string{"user_id", "phase", "snapshot_json", "updated_at"}),
|
||
}).Create(&record).Error
|
||
})
|
||
}
|
||
|
||
return bus.RegisterEventHandler(EventTypeAgentStateSnapshotPersist, handler)
|
||
}
|
||
|
||
// PublishAgentStateSnapshot 发布"agent 状态快照持久化"事件到 outbox。
|
||
//
|
||
// 设计说明:
|
||
// 1. 将快照 JSON 序列化后通过 outbox 异步写入 MySQL;
|
||
// 2. publisher 为 nil 时静默降级(Kafka 未启用场景);
|
||
// 3. 发布失败只记日志,不中断主流程。
|
||
func PublishAgentStateSnapshot(
|
||
ctx context.Context,
|
||
publisher outboxinfra.EventPublisher,
|
||
snapshot *newagentmodel.AgentStateSnapshot,
|
||
conversationID string,
|
||
userID int,
|
||
) {
|
||
if publisher == nil {
|
||
return
|
||
}
|
||
if snapshot == nil {
|
||
return
|
||
}
|
||
|
||
snapshotJSON, err := json.Marshal(snapshot)
|
||
if err != nil {
|
||
log.Printf("[WARN] 序列化 agent 状态快照失败 chat=%s: %v", conversationID, err)
|
||
return
|
||
}
|
||
|
||
phase := ""
|
||
if snapshot.RuntimeState != nil {
|
||
cs := snapshot.RuntimeState.EnsureCommonState()
|
||
if cs != nil {
|
||
phase = string(cs.Phase)
|
||
}
|
||
}
|
||
|
||
payload := AgentStateSnapshotPayload{
|
||
ConversationID: conversationID,
|
||
UserID: userID,
|
||
Phase: phase,
|
||
SnapshotJSON: string(snapshotJSON),
|
||
}
|
||
|
||
if err := publisher.Publish(ctx, outboxinfra.PublishRequest{
|
||
EventType: EventTypeAgentStateSnapshotPersist,
|
||
EventVersion: outboxinfra.DefaultEventVersion,
|
||
MessageKey: conversationID,
|
||
AggregateID: conversationID,
|
||
Payload: payload,
|
||
}); err != nil {
|
||
log.Printf("[WARN] 发布 agent 状态快照事件失败 chat=%s: %v", conversationID, err)
|
||
}
|
||
}
|