后端: 1. Memory Day1 链路打通(chat_history -> outbox -> memory_jobs) - 更新 service/events/chat_history_persist.go:聊天消息落库同事务追加 memory.extract.requested 事件(仅 user 消息,失败回滚后由 outbox 重试) - 新建 service/events/memory_extract_requested.go:消费 memory.extract.requested 并幂等入队 memory_jobs,补齐 payload 校验、文本截断与 idempotency key - 更新 cmd/start.go:注册 RegisterMemoryExtractRequestedHandler 2. Memory 模块骨架落地(先跑通状态机,再接入真实抽取) - 新建 memory/model、repo、service、orchestrator、worker、utils 目录与 Day1 mock 抽取执行链 - 新建 model/memory.go:补齐 memory_items / memory_jobs / memory_audit_logs / memory_user_settings 与事件 payload 模型 - 更新 inits/mysql.go:接入 4 张 memory 相关表 AutoMigrate 3. RAG 复用基础设施预埋(依赖可替换) - 新建 infra/rag:core pipeline + chunk/embed/retrieve/rerank/store/corpus/config 分层实现 - 默认接入 MockEmbedder + InMemoryStore,预留 Milvus / Eino 适配实现 - 新增 infra/rag/RAG复用接口实施计划.md 4. 本地依赖与交接文档同步 - 更新 docker-compose.yml:新增 etcd / minio / milvus / attu 服务与数据卷 - 删除 newAgent/HANDOFF_工具研究与运行态重置.md、newAgent/阶段3_上下文瘦身设计.md - 新增 newAgent/HANDOFF_WebSearch两阶段实施计划.md、memory/HANDOFF-RAG复用后续实施计划.md、memory/README.md 前端:无 仓库:无
195 lines
6.1 KiB
Go
195 lines
6.1 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
kafkabus "github.com/LoveLosita/smartflow/backend/infra/kafka"
|
|
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
|
|
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
|
|
memoryrepo "github.com/LoveLosita/smartflow/backend/memory/repo"
|
|
memoryservice "github.com/LoveLosita/smartflow/backend/memory/service"
|
|
"github.com/LoveLosita/smartflow/backend/model"
|
|
"github.com/spf13/viper"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
// EventTypeMemoryExtractRequested 是“记忆抽取请求”事件类型。
|
|
EventTypeMemoryExtractRequested = "memory.extract.requested"
|
|
maxMemorySourceTextLength = 1500
|
|
)
|
|
|
|
// RegisterMemoryExtractRequestedHandler 注册“记忆抽取请求”消费者。
|
|
//
|
|
// 职责边界:
|
|
// 1. 只负责把事件转为 memory_jobs 任务;
|
|
// 2. 不在消费回调里执行 LLM 重计算;
|
|
// 3. 用 outbox 通用事务保证“任务入库 + consumed 推进”原子一致。
|
|
func RegisterMemoryExtractRequestedHandler(
|
|
bus *outboxinfra.EventBus,
|
|
outboxRepo *outboxinfra.Repository,
|
|
) error {
|
|
if bus == nil {
|
|
return errors.New("event bus is nil")
|
|
}
|
|
if outboxRepo == nil {
|
|
return errors.New("outbox repository is nil")
|
|
}
|
|
|
|
handler := func(ctx context.Context, envelope kafkabus.Envelope) error {
|
|
var payload model.MemoryExtractRequestedPayload
|
|
if unmarshalErr := json.Unmarshal(envelope.Payload, &payload); unmarshalErr != nil {
|
|
_ = outboxRepo.MarkDead(ctx, envelope.OutboxID, "解析记忆抽取载荷失败: "+unmarshalErr.Error())
|
|
return nil
|
|
}
|
|
|
|
if validateErr := validateMemoryExtractPayload(payload); validateErr != nil {
|
|
_ = outboxRepo.MarkDead(ctx, envelope.OutboxID, "记忆抽取载荷非法: "+validateErr.Error())
|
|
return nil
|
|
}
|
|
|
|
return outboxRepo.ConsumeAndMarkConsumed(ctx, envelope.OutboxID, func(tx *gorm.DB) error {
|
|
enqueueService := memoryservice.NewEnqueueService(memoryrepo.NewJobRepo(tx))
|
|
jobPayload := memorymodel.ExtractJobPayload{
|
|
UserID: payload.UserID,
|
|
ConversationID: strings.TrimSpace(payload.ConversationID),
|
|
AssistantID: strings.TrimSpace(payload.AssistantID),
|
|
RunID: strings.TrimSpace(payload.RunID),
|
|
SourceMessageID: payload.SourceMessageID,
|
|
SourceRole: strings.TrimSpace(payload.SourceRole),
|
|
SourceText: strings.TrimSpace(payload.SourceText),
|
|
OccurredAt: payload.OccurredAt,
|
|
TraceID: strings.TrimSpace(payload.TraceID),
|
|
IdempotencyKey: strings.TrimSpace(payload.IdempotencyKey),
|
|
}
|
|
return enqueueService.EnqueueExtractJob(ctx, jobPayload, envelope.EventID)
|
|
})
|
|
}
|
|
|
|
return bus.RegisterEventHandler(EventTypeMemoryExtractRequested, handler)
|
|
}
|
|
|
|
// EnqueueMemoryExtractRequestedInTx 在事务内写入 memory.extract.requested outbox 消息。
|
|
//
|
|
// 设计目的:
|
|
// 1. 让“聊天消息已落库”与“记忆抽取事件已入队”同事务提交;
|
|
// 2. 任何一步失败都整体回滚,避免出现链路断点。
|
|
func EnqueueMemoryExtractRequestedInTx(
|
|
ctx context.Context,
|
|
outboxRepo *outboxinfra.Repository,
|
|
kafkaCfg kafkabus.Config,
|
|
chatPayload model.ChatHistoryPersistPayload,
|
|
) error {
|
|
if !isMemoryWriteEnabled() {
|
|
return nil
|
|
}
|
|
if outboxRepo == nil {
|
|
return errors.New("outbox repository is nil")
|
|
}
|
|
|
|
memoryPayload, shouldEnqueue := buildMemoryExtractPayloadFromChat(chatPayload)
|
|
if !shouldEnqueue {
|
|
return nil
|
|
}
|
|
|
|
payloadJSON, err := json.Marshal(memoryPayload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
outboxPayload := outboxinfra.OutboxEventPayload{
|
|
EventType: EventTypeMemoryExtractRequested,
|
|
EventVersion: outboxinfra.DefaultEventVersion,
|
|
AggregateID: strings.TrimSpace(chatPayload.ConversationID),
|
|
Payload: payloadJSON,
|
|
}
|
|
|
|
_, err = outboxRepo.CreateMessage(
|
|
ctx,
|
|
EventTypeMemoryExtractRequested,
|
|
kafkaCfg.Topic,
|
|
strings.TrimSpace(chatPayload.ConversationID),
|
|
outboxPayload,
|
|
kafkaCfg.MaxRetry,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func buildMemoryExtractPayloadFromChat(chatPayload model.ChatHistoryPersistPayload) (model.MemoryExtractRequestedPayload, bool) {
|
|
role := strings.ToLower(strings.TrimSpace(chatPayload.Role))
|
|
if role != "user" {
|
|
return model.MemoryExtractRequestedPayload{}, false
|
|
}
|
|
sourceText := strings.TrimSpace(chatPayload.Message)
|
|
if sourceText == "" {
|
|
return model.MemoryExtractRequestedPayload{}, false
|
|
}
|
|
|
|
truncatedSourceText := truncateByRune(sourceText, maxMemorySourceTextLength)
|
|
now := time.Now()
|
|
return model.MemoryExtractRequestedPayload{
|
|
UserID: chatPayload.UserID,
|
|
ConversationID: strings.TrimSpace(chatPayload.ConversationID),
|
|
// Day1 先保留 assistant_id/run_id 空值,后续从主链路上下文补齐。
|
|
AssistantID: "",
|
|
RunID: "",
|
|
SourceMessageID: 0,
|
|
SourceRole: role,
|
|
SourceText: truncatedSourceText,
|
|
OccurredAt: now,
|
|
TraceID: "",
|
|
IdempotencyKey: buildMemoryExtractIdempotencyKey(chatPayload.UserID, chatPayload.ConversationID, truncatedSourceText),
|
|
}, true
|
|
}
|
|
|
|
func validateMemoryExtractPayload(payload model.MemoryExtractRequestedPayload) error {
|
|
if payload.UserID <= 0 {
|
|
return errors.New("user_id is invalid")
|
|
}
|
|
if strings.TrimSpace(payload.ConversationID) == "" {
|
|
return errors.New("conversation_id is empty")
|
|
}
|
|
if strings.TrimSpace(payload.SourceRole) == "" {
|
|
return errors.New("source_role is empty")
|
|
}
|
|
if strings.TrimSpace(payload.SourceText) == "" {
|
|
return errors.New("source_text is empty")
|
|
}
|
|
if strings.TrimSpace(payload.IdempotencyKey) == "" {
|
|
return errors.New("idempotency_key is empty")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func buildMemoryExtractIdempotencyKey(userID int, conversationID, sourceText string) string {
|
|
raw := fmt.Sprintf("%d|%s|%s", userID, strings.TrimSpace(conversationID), strings.TrimSpace(sourceText))
|
|
sum := sha256.Sum256([]byte(raw))
|
|
return "memory_extract_" + strconv.Itoa(userID) + "_" + hex.EncodeToString(sum[:8])
|
|
}
|
|
|
|
func truncateByRune(raw string, max int) string {
|
|
if max <= 0 {
|
|
return ""
|
|
}
|
|
runes := []rune(raw)
|
|
if len(runes) <= max {
|
|
return raw
|
|
}
|
|
return string(runes[:max])
|
|
}
|
|
|
|
func isMemoryWriteEnabled() bool {
|
|
if !viper.IsSet("memory.enabled") {
|
|
return true
|
|
}
|
|
return viper.GetBool("memory.enabled")
|
|
}
|