Version: 0.9.14.dev.260410
后端:
1. LLM 客户端从 newAgent/llm 提升为 infra/llm 基础设施层
- 删除 backend/newAgent/llm/(ark.go / ark_adapter.go / client.go / json.go)
- 等价迁移至 backend/infra/llm/,所有 newAgent node 与 service 统一改引用 infrallm
- 消除 newAgent 对模型客户端的私有依赖,为 memory / websearch 等多模块复用铺路
2. RAG 基础设施完成可运行态接入(factory / runtime / observer / service 四层成型)
- 新建 backend/infra/rag/factory.go / runtime.go / observe.go / observer.go /
service.go:工厂创建、运行时生命周期、轻量观测接口、检索服务门面
- 更新 infra/rag/config/config.go:补齐 Milvus / Embed / Reranker 全部配置项与默认值
- 更新 infra/rag/embed/eino_embedder.go:增强 Eino embedding 适配,支持 BaseURL / APIKey 环境变量 / 超时 /
维度等参数
- 更新 infra/rag/store/milvus_store.go:完整实现 Milvus 向量存储(建集合 / 建 Index / Upsert / Search /
Delete),支持 COSINE / L2 / IP 度量
- 更新 infra/rag/core/pipeline.go:适配 Runtime 接口,Pipeline 由 factory 注入而非手动拼装
- 更新 infra/rag/corpus/memory_corpus.go / vector_store.go:对接 Memory 模块数据源与 Store 接口扩展
3. Memory 模块从 Day1 骨架升级为 Day2 完整可运行态
- 新建 memory/module.go:统一门面 Module,对外封装 EnqueueExtract / ReadService / ManageService / WithTx /
StartWorker,启动层只依赖这一个入口
- 新建 memory/orchestrator/llm_write_orchestrator.go:LLM 驱动的记忆抽取编排器,替代原 mock 抽取
- 新建 memory/service/read_service.go:按用户开关过滤 + 轻量重排 + 访问时间刷新的读取链路
- 新建 memory/service/manage_service.go:记忆管理面能力(列出 / 软删除 / 开关读写),删除同步写审计日志
- 新建 memory/service/common.go:服务层公共工具
- 新建 memory/worker/loop.go:后台轮询循环 RunPollingLoop,定时抢占 pending 任务并推进
- 新建 memory/utils/audit.go / settings.go:审计日志构造、用户设置过滤等纯函数
- 更新 memory/model/item.go / job.go / settings.go / config.go / status.go:补齐 DTO 字段与状态常量
- 更新 memory/repo/item_repo.go / job_repo.go / audit_repo.go / settings_repo.go:补齐 CRUD 与查询能力
- 更新 memory/worker/runner.go:Runner 对接 Module 与 LLM 抽取器,任务状态机完整化
- 更新 memory/README.md:同步模块现状说明
4. newAgent 接入 Memory 读取注入与工具注册依赖预埋
- 新建 service/agentsvc/agent_memory.go:定义 MemoryReader 接口 + injectMemoryContext,在 graph
执行前统一补充记忆上下文
- 更新 service/agentsvc/agent.go:新增 memoryReader 字段与 SetMemoryReader 方法
- 更新 service/agentsvc/agent_newagent.go:调用 injectMemoryContext 注入 pinned block,检索失败仅降级不阻断主链路
- 更新 newAgent/tools/registry.go:新增 DefaultRegistryDeps(含 RAGRuntime),工具注册表支持依赖注入
5. 启动流程与事件处理器接线更新
- 更新 cmd/start.go:初始化 RAG Runtime → Memory Module → 注册事件处理器 → 启动 Worker 后台轮询
- 更新 service/events/memory_extract_requested.go:改用 memory.Module.WithTx(tx) 统一门面,事件处理器不再直接依赖
repo/service 内部包
6. 缓存插件与配置同步
- 更新 middleware/cache_deleter.go:静默忽略 MemoryJob / MemoryItem / MemoryAuditLog / MemoryUserSetting
等新模型,避免日志刷屏;清理冗余注释
- 更新 config.example.yaml:补齐 rag / memory / websearch 配置段及默认值
- 更新 go.mod / go.sum:新增 eino-ext/openai / json-patch / go-openai 依赖
前端:无 仓库:无
This commit is contained in:
@@ -56,6 +56,7 @@ type AgentService struct {
|
||||
scheduleProvider newagentmodel.ScheduleStateProvider
|
||||
schedulePersistor newagentmodel.SchedulePersistor
|
||||
agentStateStore newagentmodel.AgentStateStore
|
||||
memoryReader MemoryReader
|
||||
}
|
||||
|
||||
// NewAgentService 构造 AgentService。
|
||||
|
||||
161
backend/service/agentsvc/agent_memory.go
Normal file
161
backend/service/agentsvc/agent_memory.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package agentsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
|
||||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||||
)
|
||||
|
||||
const (
|
||||
newAgentMemoryBlockKey = "memory_context"
|
||||
newAgentMemoryRetrieveLimit = 5
|
||||
newAgentMemoryBlockTitle = "相关记忆"
|
||||
newAgentMemoryIntroLine = "以下是与当前对话相关的用户记忆,仅在自然且确实有帮助时参考,不要生硬复述。"
|
||||
)
|
||||
|
||||
// MemoryReader 描述 newAgent 主链路读取记忆所需的最小能力。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只负责“按当前输入取回候选记忆”;
|
||||
// 2. 不负责 prompt 拼装,也不要求调用方感知 memory 模块内部 repo/service 结构;
|
||||
// 3. 返回值直接复用 memory DTO,避免 service 层再维护一套重复结构。
|
||||
type MemoryReader interface {
|
||||
Retrieve(ctx context.Context, req memorymodel.RetrieveRequest) ([]memorymodel.ItemDTO, error)
|
||||
}
|
||||
|
||||
// SetMemoryReader 注入 newAgent 主链路读取记忆所需的薄接口。
|
||||
func (s *AgentService) SetMemoryReader(reader MemoryReader) {
|
||||
s.memoryReader = reader
|
||||
}
|
||||
|
||||
// injectMemoryContext 在 graph 执行前,把本轮相关记忆写入 ConversationContext 的 pinned block。
|
||||
//
|
||||
// 步骤说明:
|
||||
// 1. 先做前置门控:没有 reader、没有有效用户、或输入属于“确认/应答型短句”时,直接清掉旧 block,避免快照残留污染本轮 prompt。
|
||||
// 2. 再调用 memory 检索:查询失败只记日志,不中断主链路,保证 newAgent 的可用性优先。
|
||||
// 3. 检索成功后把结果渲染成稳定的中文文本,并用固定 key 覆盖写入,确保每轮都能刷新而不是越积越多。
|
||||
func (s *AgentService) injectMemoryContext(
|
||||
ctx context.Context,
|
||||
conversationContext *newagentmodel.ConversationContext,
|
||||
userID int,
|
||||
chatID string,
|
||||
userMessage string,
|
||||
) {
|
||||
if conversationContext == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s.memoryReader == nil || userID <= 0 || !shouldInjectMemoryForInput(userMessage) {
|
||||
conversationContext.RemovePinnedBlock(newAgentMemoryBlockKey)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := s.memoryReader.Retrieve(ctx, memorymodel.RetrieveRequest{
|
||||
Query: strings.TrimSpace(userMessage),
|
||||
UserID: userID,
|
||||
ConversationID: strings.TrimSpace(chatID),
|
||||
Limit: newAgentMemoryRetrieveLimit,
|
||||
Now: time.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
conversationContext.RemovePinnedBlock(newAgentMemoryBlockKey)
|
||||
log.Printf("读取记忆上下文失败 user=%d chat=%s err=%v", userID, chatID, err)
|
||||
return
|
||||
}
|
||||
|
||||
content := renderMemoryPinnedContent(items)
|
||||
if content == "" {
|
||||
conversationContext.RemovePinnedBlock(newAgentMemoryBlockKey)
|
||||
return
|
||||
}
|
||||
|
||||
conversationContext.UpsertPinnedBlock(newagentmodel.ContextBlock{
|
||||
Key: newAgentMemoryBlockKey,
|
||||
Title: newAgentMemoryBlockTitle,
|
||||
Content: content,
|
||||
})
|
||||
}
|
||||
|
||||
// shouldInjectMemoryForInput 判断当前输入是否值得触发一次记忆召回。
|
||||
//
|
||||
// 步骤说明:
|
||||
// 1. 空输入直接跳过;
|
||||
// 2. 对“好/确认/ok”这类弱语义应答做显式拦截,避免 legacy fallback 在无查询价值时注入一批高分但不相关的旧记忆;
|
||||
// 3. 其余输入一律放行,优先保证 MVP 可用。
|
||||
func shouldInjectMemoryForInput(userMessage string) bool {
|
||||
trimmed := strings.TrimSpace(userMessage)
|
||||
if trimmed == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
switch strings.ToLower(trimmed) {
|
||||
case "好", "好的", "嗯", "嗯嗯", "行", "可以", "收到", "明白", "确认", "取消", "是", "不是", "对", "不对", "ok", "okay", "yes", "no":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// renderMemoryPinnedContent 把召回结果转成一段稳定、紧凑、适合 prompt 注入的自然语言文本。
|
||||
func renderMemoryPinnedContent(items []memorymodel.ItemDTO) string {
|
||||
if len(items) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString(newAgentMemoryIntroLine)
|
||||
|
||||
seen := make(map[string]struct{}, len(items))
|
||||
written := 0
|
||||
for _, item := range items {
|
||||
line := buildMemoryPinnedLine(item)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[line]; exists {
|
||||
continue
|
||||
}
|
||||
seen[line] = struct{}{}
|
||||
sb.WriteString("\n- ")
|
||||
sb.WriteString(line)
|
||||
written++
|
||||
}
|
||||
|
||||
if written == 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(sb.String())
|
||||
}
|
||||
|
||||
// buildMemoryPinnedLine 把单条记忆渲染成“[类型] 内容”的简洁格式。
|
||||
func buildMemoryPinnedLine(item memorymodel.ItemDTO) string {
|
||||
text := strings.TrimSpace(item.Content)
|
||||
if text == "" {
|
||||
text = strings.TrimSpace(item.Title)
|
||||
}
|
||||
if text == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("[%s] %s", localizeMemoryType(item.MemoryType), text)
|
||||
}
|
||||
|
||||
// localizeMemoryType 把 memory 类型映射成 prompt 里更自然的中文标签。
|
||||
func localizeMemoryType(memoryType string) string {
|
||||
switch strings.TrimSpace(memoryType) {
|
||||
case memorymodel.MemoryTypePreference:
|
||||
return "偏好"
|
||||
case memorymodel.MemoryTypeConstraint:
|
||||
return "约束"
|
||||
case memorymodel.MemoryTypeTodoHint:
|
||||
return "待办线索"
|
||||
case memorymodel.MemoryTypeFact:
|
||||
return "事实"
|
||||
default:
|
||||
return "记忆"
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
infrallm "github.com/LoveLosita/smartflow/backend/infra/llm"
|
||||
newagentconv "github.com/LoveLosita/smartflow/backend/newAgent/conv"
|
||||
newagentgraph "github.com/LoveLosita/smartflow/backend/newAgent/graph"
|
||||
newagentllm "github.com/LoveLosita/smartflow/backend/newAgent/llm"
|
||||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||||
newagentstream "github.com/LoveLosita/smartflow/backend/newAgent/stream"
|
||||
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
|
||||
@@ -107,6 +107,10 @@ func (s *AgentService) runNewAgentGraph(
|
||||
} else {
|
||||
conversationContext = s.loadConversationContext(requestCtx, chatID, userMessage)
|
||||
}
|
||||
// 5.1. 在 graph 执行前统一补充与当前输入相关的记忆上下文。
|
||||
// 5.1.1 这里采用 pinned block 注入,这样 chat / plan / execute / deliver 各阶段都能自动复用。
|
||||
// 5.1.2 检索失败只降级为“本轮不注入记忆”,不阻断主链路。
|
||||
s.injectMemoryContext(requestCtx, conversationContext, userID, chatID, userMessage)
|
||||
|
||||
// 5.5 若 extra 携带 task_class_ids,校验后写入 CommonState(仅首轮/尚未设置时生效,跨轮持久化)。
|
||||
// 校验:通过 LoadTaskClassMetas → GetCompleteTaskClassesByIDs 检查所有 ID 是否存在且属于当前用户;
|
||||
@@ -141,10 +145,10 @@ func (s *AgentService) runNewAgentGraph(
|
||||
graphRequest.Normalize()
|
||||
|
||||
// 7. 适配 LLM clients(从 AIHub 的 ark.ChatModel 转换为 newAgent LLM Client)。
|
||||
chatClient := newagentllm.WrapArkClient(s.AIHub.Worker)
|
||||
planClient := newagentllm.WrapArkClient(s.AIHub.Worker)
|
||||
executeClient := newagentllm.WrapArkClient(s.AIHub.Worker)
|
||||
deliverClient := newagentllm.WrapArkClient(s.AIHub.Worker)
|
||||
chatClient := infrallm.WrapArkClient(s.AIHub.Worker)
|
||||
planClient := infrallm.WrapArkClient(s.AIHub.Worker)
|
||||
executeClient := infrallm.WrapArkClient(s.AIHub.Worker)
|
||||
deliverClient := infrallm.WrapArkClient(s.AIHub.Worker)
|
||||
|
||||
// 8. 适配 SSE emitter。
|
||||
sseEmitter := newagentstream.NewSSEPayloadEmitter(outChan)
|
||||
|
||||
@@ -13,9 +13,8 @@ import (
|
||||
|
||||
kafkabus "github.com/LoveLosita/smartflow/backend/infra/kafka"
|
||||
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
|
||||
"github.com/LoveLosita/smartflow/backend/memory"
|
||||
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"
|
||||
@@ -32,10 +31,11 @@ const (
|
||||
// 职责边界:
|
||||
// 1. 只负责把事件转为 memory_jobs 任务;
|
||||
// 2. 不在消费回调里执行 LLM 重计算;
|
||||
// 3. 用 outbox 通用事务保证“任务入库 + consumed 推进”原子一致。
|
||||
// 3. 通过 memory.Module.WithTx(tx) 复用同一套接入门面,保证事务边界仍由 outbox 掌控。
|
||||
func RegisterMemoryExtractRequestedHandler(
|
||||
bus *outboxinfra.EventBus,
|
||||
outboxRepo *outboxinfra.Repository,
|
||||
memoryModule *memory.Module,
|
||||
) error {
|
||||
if bus == nil {
|
||||
return errors.New("event bus is nil")
|
||||
@@ -43,6 +43,9 @@ func RegisterMemoryExtractRequestedHandler(
|
||||
if outboxRepo == nil {
|
||||
return errors.New("outbox repository is nil")
|
||||
}
|
||||
if memoryModule == nil {
|
||||
return errors.New("memory module is nil")
|
||||
}
|
||||
|
||||
handler := func(ctx context.Context, envelope kafkabus.Envelope) error {
|
||||
var payload model.MemoryExtractRequestedPayload
|
||||
@@ -57,7 +60,6 @@ func RegisterMemoryExtractRequestedHandler(
|
||||
}
|
||||
|
||||
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),
|
||||
@@ -70,7 +72,7 @@ func RegisterMemoryExtractRequestedHandler(
|
||||
TraceID: strings.TrimSpace(payload.TraceID),
|
||||
IdempotencyKey: strings.TrimSpace(payload.IdempotencyKey),
|
||||
}
|
||||
return enqueueService.EnqueueExtractJob(ctx, jobPayload, envelope.EventID)
|
||||
return memoryModule.WithTx(tx).EnqueueExtract(ctx, jobPayload, envelope.EventID)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -80,8 +82,8 @@ func RegisterMemoryExtractRequestedHandler(
|
||||
// EnqueueMemoryExtractRequestedInTx 在事务内写入 memory.extract.requested outbox 消息。
|
||||
//
|
||||
// 设计目的:
|
||||
// 1. 让“聊天消息已落库”与“记忆抽取事件已入队”同事务提交;
|
||||
// 2. 任何一步失败都整体回滚,避免出现链路断点。
|
||||
// 1. 让“聊天消息已落库”和“记忆抽取事件已入队”同事务提交;
|
||||
// 2. 任意一步失败都整体回滚,避免出现链路断点。
|
||||
func EnqueueMemoryExtractRequestedInTx(
|
||||
ctx context.Context,
|
||||
outboxRepo *outboxinfra.Repository,
|
||||
@@ -128,6 +130,7 @@ func buildMemoryExtractPayloadFromChat(chatPayload model.ChatHistoryPersistPaylo
|
||||
if role != "user" {
|
||||
return model.MemoryExtractRequestedPayload{}, false
|
||||
}
|
||||
|
||||
sourceText := strings.TrimSpace(chatPayload.Message)
|
||||
if sourceText == "" {
|
||||
return model.MemoryExtractRequestedPayload{}, false
|
||||
@@ -179,6 +182,7 @@ func truncateByRune(raw string, max int) string {
|
||||
if max <= 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
runes := []rune(raw)
|
||||
if len(runes) <= max {
|
||||
return raw
|
||||
|
||||
Reference in New Issue
Block a user