后端:
1.阶段 6 CP4/CP5 目录收口与共享边界纯化
- 将 backend 根目录收口为 services、client、gateway、cmd、shared 五个一级目录
- 收拢 bootstrap、inits、infra/kafka、infra/outbox、conv、respond、pkg、middleware,移除根目录旧实现与空目录
- 将 utils 下沉到 services/userauth/internal/auth,将 logic 下沉到 services/schedule/core/planning
- 将迁移期 runtime 桥接实现统一收拢到 services/runtime/{conv,dao,eventsvc,model},删除 shared/legacy 与未再被 import 的旧 service 实现
- 将 gateway/shared/respond 收口为 HTTP/Gin 错误写回适配,shared/respond 仅保留共享错误语义与状态映射
- 将 HTTP IdempotencyMiddleware 与 RateLimitMiddleware 收口到 gateway/middleware
- 将 GormCachePlugin 下沉到 shared/infra/gormcache,将共享 RateLimiter 下沉到 shared/infra/ratelimit,将 agent token budget 下沉到 services/agent/shared
- 删除 InitEino 兼容壳,收缩 cmd/internal/coreinit 仅保留旧组合壳残留域初始化语义
- 更新微服务迁移计划与桌面 checklist,补齐 CP4/CP5 当前切流点、目录终态与验证结果
- 完成 go test ./...、git diff --check 与最终真实 smoke;health、register/login、task/create+get、schedule/today、task-class/list、memory/items、agent chat/meta/timeline/context-stats 全部 200,SSE 合并结果为 CP5_OK 且 [DONE] 只有 1 个
131 lines
4.0 KiB
Go
131 lines
4.0 KiB
Go
package eventsvc
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"log"
|
||
|
||
agentmodel "github.com/LoveLosita/smartflow/backend/services/agent/model"
|
||
"github.com/LoveLosita/smartflow/backend/services/runtime/dao"
|
||
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
|
||
kafkabus "github.com/LoveLosita/smartflow/backend/shared/infra/kafka"
|
||
outboxinfra "github.com/LoveLosita/smartflow/backend/shared/infra/outbox"
|
||
"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 *agentmodel.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)
|
||
}
|
||
}
|