后端:
1. Memory 预取缓存改为会话级隔离 + 管理面自动失效 + 空检索清理
- 预取缓存 key 从 smartflow:memory_prefetch:{userID} 改为 smartflow:memory_prefetch:u:{userID}:c:{chatID},隔离不同会话的记忆上下文,避免会话间互相覆盖
- 新增 DeleteMemoryPrefetchCacheByUser 方法,使用 SCAN+UNLINK 按模式批量删除指定用户所有会话的预取缓存
- ItemRepo 四个变更方法(SoftDeleteByID / RestoreByIDAt / UpdateManagedFieldsByIDAt / UpdateStatusByIDAt)通过 Model 携带 UserID,使 GORM cache deleter 可精准定位用户
- GormCachePlugin 将 MemoryItem 从忽略列表移至主动处理,新增 invalidMemoryPrefetchCache 异步失效方法
- 后台检索返回空结果时主动清除该用户所有预取缓存,避免过期记忆残留
2. 修复 RAG 召回未过滤 deleted 状态记忆的严重 bug
- MemoryCorpus.BuildRetrieveFilter 新增 status="active" 硬过滤,Milvus 向量检索直接排除已删除/已归档记忆
- 此前删除记忆后即使 MySQL 标记为 deleted,Milvus 中向量仍可被语义召回并注入 prompt
前端:无
仓库:无
82 lines
2.5 KiB
Go
82 lines
2.5 KiB
Go
package utils
|
|
|
|
import (
|
|
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
|
|
"github.com/LoveLosita/smartflow/backend/model"
|
|
)
|
|
|
|
// EffectiveUserSetting 返回用户记忆设置的生效值。
|
|
//
|
|
// 规则说明:
|
|
// 1. 用户未显式配置时,走系统默认值;
|
|
// 2. 默认允许普通记忆和隐式记忆,但默认关闭敏感记忆;
|
|
// 3. 返回值始终是完整对象,方便调用方直接使用,不再分支判空。
|
|
func EffectiveUserSetting(setting *model.MemoryUserSetting, userID int) model.MemoryUserSetting {
|
|
if setting == nil {
|
|
return model.MemoryUserSetting{
|
|
UserID: userID,
|
|
MemoryEnabled: true,
|
|
ImplicitMemoryEnabled: true,
|
|
SensitiveMemoryEnabled: false,
|
|
}
|
|
}
|
|
return *setting
|
|
}
|
|
|
|
// FilterFactsBySetting 按用户记忆开关过滤候选事实。
|
|
func FilterFactsBySetting(facts []memorymodel.NormalizedFact, setting model.MemoryUserSetting) []memorymodel.NormalizedFact {
|
|
if !setting.MemoryEnabled || len(facts) == 0 {
|
|
return nil
|
|
}
|
|
|
|
result := make([]memorymodel.NormalizedFact, 0, len(facts))
|
|
for _, fact := range facts {
|
|
if !setting.ImplicitMemoryEnabled && !fact.IsExplicit {
|
|
continue
|
|
}
|
|
if !setting.SensitiveMemoryEnabled && fact.SensitivityLevel > 0 {
|
|
continue
|
|
}
|
|
result = append(result, fact)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// FilterItemsBySetting 按用户记忆开关过滤已入库记忆。
|
|
func FilterItemsBySetting(items []model.MemoryItem, setting model.MemoryUserSetting) []model.MemoryItem {
|
|
if !setting.MemoryEnabled || len(items) == 0 {
|
|
return nil
|
|
}
|
|
|
|
result := make([]model.MemoryItem, 0, len(items))
|
|
for _, item := range items {
|
|
if !setting.ImplicitMemoryEnabled && !item.IsExplicit {
|
|
continue
|
|
}
|
|
if !setting.SensitiveMemoryEnabled && item.SensitivityLevel > 0 {
|
|
continue
|
|
}
|
|
result = append(result, item)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// FilterFactsByConfidence 按置信度阈值过滤候选事实。
|
|
//
|
|
// 说明:
|
|
// 1. minConfidence <= 0 时不做过滤,保持向后兼容;
|
|
// 2. 过滤在 FilterFactsBySetting 之后执行,是写入链路的第二道程序化门槛;
|
|
// 3. 阈值由 memory.write.minConfidence 配置控制,默认 0.5。
|
|
func FilterFactsByConfidence(facts []memorymodel.NormalizedFact, minConfidence float64) []memorymodel.NormalizedFact {
|
|
if minConfidence <= 0 || len(facts) == 0 {
|
|
return facts
|
|
}
|
|
result := make([]memorymodel.NormalizedFact, 0, len(facts))
|
|
for _, fact := range facts {
|
|
if fact.Confidence >= minConfidence {
|
|
result = append(result, fact)
|
|
}
|
|
}
|
|
return result
|
|
}
|