Version: 0.9.22.dev.260416

后端:
1. 品牌文案与聊天定位统一切到 SmartMate,并放宽非排程问答能力
   - 系统人设、路由、排程、查询、交付提示统一从 SmartFlow 改为 SmartMate
   - 明确普通问答/生活建议/开放讨论可正常回答,deep_answer 不再输出“让我想想”等占位话术
   - thinkingMode=auto 时,deep_answer 默认开启 thinking,execute 继续跟随路由决策,其余路由默认关闭
2. Memory 读取链路升级为“结构化强约束 + 语义候选”hybrid 模式,并补齐注入渲染 / Execute 消费
   - 新增 read.mode、四类记忆预算、inject.renderMode 等配置及默认值
   - 落地 HybridRetrieve,统一 MySQL/RAG 读侧作用域、三级去重(ID/hash/text)、统一重排与按类型预算裁剪
   - 新增 FindPinnedByUser、content_hash DTO/兜底补算、legacy/RAG 共用读侧查询口径与 fallback 逻辑
   - 记忆注入支持 flat/typed_v2 两种渲染,execute msg3 正式消费 memory_context,主链路注入 MemoryReader 时同步透传 memory 配置
3. Memory 第二步/第三步 handoff 与治理文档补齐
   - HANDOFF_Memory向Mem0靠拢三步冲刺计划.md 从 newAgent 迁到 memory 目录,并补充“我的记忆”增删改查与最小留痕口径
   - 新增 backend/memory/记忆模块第二步计划.md、backend/memory/第三步治理与观测落地计划.md,分别拆解 hybrid 读取注入闭环与治理/观测/清理路线
   - 同步更新 backend/memory/Log.txt 调试日志
前端:
1. 助手输入区新增“智能编排”任务类选择器,并把 task_class_ids 作为请求 extra 透传
   - 新建 frontend/src/components/assistant/TaskClassPlanningPicker.vue,支持拉取任务类列表、临时勾选、已选标签回显与清空
   - 更新 frontend/src/components/dashboard/AssistantPanel.vue、frontend/src/types/dashboard.ts:Chat extra 正式建模 task_class_ids / retry 字段;当本轮带编排任务类时强制新起会话,避免把现有会话历史误混入新编排
2. 会话上下文窗口统计接入前端展示
   - 更新 frontend/src/api/agent.ts、新建 frontend/src/components/assistant/ContextWindowMeter.vue、更新 frontend/src/components/dashboard/AssistantPanel.vue、frontend/src/types/dashboard.ts:接入 /agent/context-stats,兼容 object/string/null 三种返回;在输入工具栏展示 msg0~msg3 占比与预算使用率
3. 助手面板交互细节优化
   - 更新 frontend/src/components/dashboard/AssistantPanel.vue:thinking 开关改为 auto/true/false 三态选择;切会话与重试后同步刷新 context stats;历史列表首屏不足时自动继续分页直到形成滚动区
仓库:无
This commit is contained in:
Losita
2026-04-16 18:29:17 +08:00
parent 634a9fb926
commit a1b2ffedb8
38 changed files with 3150 additions and 277 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/LoveLosita/smartflow/backend/dao"
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
"github.com/LoveLosita/smartflow/backend/inits"
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
"github.com/LoveLosita/smartflow/backend/model"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
@@ -58,6 +59,7 @@ type AgentService struct {
agentStateStore newagentmodel.AgentStateStore
compactionStore newagentmodel.CompactionStore
memoryReader MemoryReader
memoryCfg memorymodel.Config
}
// NewAgentService 构造 AgentService。

View File

@@ -2,7 +2,6 @@ package agentsvc
import (
"context"
"fmt"
"log"
"strings"
"time"
@@ -28,9 +27,10 @@ type MemoryReader interface {
Retrieve(ctx context.Context, req memorymodel.RetrieveRequest) ([]memorymodel.ItemDTO, error)
}
// SetMemoryReader 注入 newAgent 主链路读取记忆所需的薄接口。
func (s *AgentService) SetMemoryReader(reader MemoryReader) {
// SetMemoryReader 注入 newAgent 主链路读取记忆所需的薄接口与渲染配置
func (s *AgentService) SetMemoryReader(reader MemoryReader, cfg memorymodel.Config) {
s.memoryReader = reader
s.memoryCfg = cfg
}
// injectMemoryContext 在 graph 执行前,把本轮相关记忆写入 ConversationContext 的 pinned block。
@@ -68,7 +68,7 @@ func (s *AgentService) injectMemoryContext(
return
}
content := renderMemoryPinnedContent(items)
content := renderMemoryPinnedContentByMode(items, s.memoryCfg.EffectiveInjectRenderMode())
if content == "" {
conversationContext.RemovePinnedBlock(newAgentMemoryBlockKey)
return
@@ -100,62 +100,3 @@ func shouldInjectMemoryForInput(userMessage string) bool {
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 "记忆"
}
}

View File

@@ -0,0 +1,159 @@
package agentsvc
import (
"fmt"
"strings"
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
)
// renderMemoryPinnedContentByMode 根据配置选择记忆渲染方式。
func renderMemoryPinnedContentByMode(items []memorymodel.ItemDTO, renderMode string) string {
switch memorymodel.NormalizeInjectRenderMode(renderMode) {
case memorymodel.MemoryInjectRenderModeTypedV2:
return RenderTypedMemoryContent(items)
default:
return RenderFlatMemoryContent(items)
}
}
// RenderFlatMemoryContent 生成兼容旧链路的扁平记忆文本。
func RenderFlatMemoryContent(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())
}
// RenderTypedMemoryContent 按记忆类型分段渲染。
//
// 步骤化说明:
// 1. 先按固定类型顺序分组,避免同类记忆在 prompt 中被打散;
// 2. 每组内部继续做文本级去重,兜底保护历史脏数据;
// 3. 只输出非空分组,减少 Execute / Plan 阶段的无效噪音。
func RenderTypedMemoryContent(items []memorymodel.ItemDTO) string {
if len(items) == 0 {
return ""
}
type renderSection struct {
Title string
Items []string
}
orderedTypes := []string{
memorymodel.MemoryTypeConstraint,
memorymodel.MemoryTypePreference,
memorymodel.MemoryTypeFact,
memorymodel.MemoryTypeTodoHint,
}
sectionTitle := map[string]string{
memorymodel.MemoryTypeConstraint: "必守约束",
memorymodel.MemoryTypePreference: "用户偏好",
memorymodel.MemoryTypeFact: "当前话题相关事实",
memorymodel.MemoryTypeTodoHint: "近期待办",
}
grouped := make(map[string][]string, len(orderedTypes))
seen := make(map[string]struct{}, len(items))
for _, item := range items {
content := buildMemoryRenderContent(item)
if content == "" {
continue
}
dedupKey := strings.TrimSpace(item.MemoryType) + "::" + content
if _, exists := seen[dedupKey]; exists {
continue
}
seen[dedupKey] = struct{}{}
memoryType := memorymodel.NormalizeMemoryType(item.MemoryType)
if memoryType == "" {
memoryType = memorymodel.MemoryTypeFact
}
grouped[memoryType] = append(grouped[memoryType], content)
}
sections := make([]renderSection, 0, len(orderedTypes))
for _, memoryType := range orderedTypes {
contentList := grouped[memoryType]
if len(contentList) == 0 {
continue
}
sections = append(sections, renderSection{
Title: sectionTitle[memoryType],
Items: contentList,
})
}
if len(sections) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString(newAgentMemoryIntroLine)
for _, section := range sections {
sb.WriteString("\n\n【")
sb.WriteString(section.Title)
sb.WriteString("】")
for _, line := range section.Items {
sb.WriteString("\n- ")
sb.WriteString(line)
}
}
return strings.TrimSpace(sb.String())
}
// buildMemoryPinnedLine 把单条记忆渲染成“[类型] 内容”的简洁格式。
func buildMemoryPinnedLine(item memorymodel.ItemDTO) string {
text := buildMemoryRenderContent(item)
if text == "" {
return ""
}
return fmt.Sprintf("[%s] %s", localizeMemoryType(item.MemoryType), text)
}
func buildMemoryRenderContent(item memorymodel.ItemDTO) string {
text := strings.TrimSpace(item.Content)
if text == "" {
text = strings.TrimSpace(item.Title)
}
return 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 "记忆"
}
}

View File

@@ -38,7 +38,7 @@ const (
conversationTitleTokenAdjustReason = "conversation_title_async"
)
const conversationTitlePrompt = `你是 SmartFlow 的会话标题生成器。
const conversationTitlePrompt = `你是 SmartMate 的会话标题生成器。
请基于给定对话内容,生成一个简短中文标题。
要求: