feat(agents): Phase 8.4-10.5 built-in plugins, bundled skills, coordinator
This commit is contained in:
155
development-doc/plan/memory-update/README.md
Normal file
155
development-doc/plan/memory-update/README.md
Normal file
@@ -0,0 +1,155 @@
|
||||
# Jarvis Memory 升级计划索引
|
||||
|
||||
本目录用于存放 Jarvis 记忆系统的分阶段升级规划文档。
|
||||
|
||||
## 文档说明
|
||||
|
||||
| 文件 | 说明 |
|
||||
|------|------|
|
||||
| `README.md` | 总览、阶段关系、实施顺序 |
|
||||
| `phase-m-0-current-state.md` | 当前现状、问题、目标架构 |
|
||||
| `phase-m-1-importance-scoring.md` | 重要性评分系统 |
|
||||
| `phase-m-2-forgetting-system.md` | 遗忘曲线系统 |
|
||||
| `phase-m-3-proactive-reminder.md` | 主动提醒系统 |
|
||||
| `checklist.md` | 执行清单 |
|
||||
|
||||
## 推荐阅读顺序
|
||||
|
||||
1. 先读 `phase-m-0-current-state.md`
|
||||
2. 再按顺序阅读 phase m-1 ~ m-3
|
||||
3. 实施时严格按阶段推进
|
||||
4. 参考 `checklist.md` 进行任务追踪
|
||||
|
||||
---
|
||||
|
||||
## 总体升级原则
|
||||
|
||||
1. **频率追踪** - 每次交互更新记忆频率
|
||||
2. **重要性分层** - 高频/情绪/影响面 → 重要记忆
|
||||
3. **遗忘曲线** - 低频记忆自然衰减
|
||||
4. **主动关心** - 定期生成提醒,而非被动响应
|
||||
5. **可独立推进** - Phase M 可与 Agent Phase 1-5 并行
|
||||
|
||||
---
|
||||
|
||||
## 阶段总览图
|
||||
|
||||
```
|
||||
M.0 ──────────────────────────────────────────────────────────────┐
|
||||
│ 现状与目标 │
|
||||
│ - 当前记忆架构分析 │
|
||||
│ - 短板识别 │
|
||||
│ - 拟人记忆目标 │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
M.1 ──────────────────────────────────────────────────────────────┐
|
||||
│ 重要性评分系统 │
|
||||
│ - MemoryFrequencyTracker (频率追踪) │
|
||||
│ - EmotionAnalyzer (情绪分析) │
|
||||
│ - ImpactScorer (影响面评估) │
|
||||
│ │
|
||||
│ 核心文件: services/memory/importance_scorer.py │
|
||||
│ 工作量: 4 天 │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
M.2 ──────────────────────────────────────────────────────────────┐
|
||||
│ 遗忘曲线系统 │
|
||||
│ - ForgettingCurve (遗忘曲线) │
|
||||
│ - MemoryDecay (记忆衰减) │
|
||||
│ - ReinforcementTrigger (强化触发) │
|
||||
│ │
|
||||
│ 核心文件: services/memory/forgetting_curve.py │
|
||||
│ 依赖: M.1 │
|
||||
│ 工作量: 3 天 │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
M.3 ──────────────────────────────────────────────────────────────┐
|
||||
│ 主动提醒系统 │
|
||||
│ - DailyDigestGenerator (每日摘要) │
|
||||
│ - ReminderScheduler (提醒调度) │
|
||||
│ - ProactiveMemoryInformer (主动提醒) │
|
||||
│ │
|
||||
│ 核心文件: services/memory/proactive_reminder.py │
|
||||
│ 依赖: M.1, M.2 │
|
||||
│ 工作量: 5 天 │
|
||||
└────────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 核心借鉴
|
||||
|
||||
| 借鉴点 | 来源 | 难度 |
|
||||
|--------|------|------|
|
||||
| 频率追踪 | 儿童认知发育模型 | 🟢 低 |
|
||||
| 艾宾浩斯遗忘曲线 | 心理学研究 | 🟢 低 |
|
||||
| 重要性评分 | Jarvis 自身需求 | 🟡 中 |
|
||||
| 主动提醒 | 儿童认知发育模型 | 🟡 中 |
|
||||
|
||||
**注:本升级不借鉴 VCPToolBox,因为 VCPToolBox 解决的是「检索精度」问题,而本升级解决的是「记忆价值判断」问题。**
|
||||
|
||||
---
|
||||
|
||||
## 实施顺序
|
||||
|
||||
```
|
||||
M.0 → M.1 → M.2 → M.3
|
||||
│ │ │
|
||||
│ │ └── 主动提醒系统
|
||||
│ └── 遗忘曲线系统
|
||||
└── 现状与目标
|
||||
```
|
||||
|
||||
**注意:** M.1 是基础,M.2 和 M.3 都依赖 M.1。
|
||||
|
||||
---
|
||||
|
||||
## 文件变更追踪
|
||||
|
||||
| Phase | 新增文件 | 修改文件 |
|
||||
|-------|---------|---------|
|
||||
| M.1 | `services/memory/importance_scorer.py`, `services/memory/frequency_tracker.py`, `services/memory/emotion_analyzer.py`, `tests/test_importance_scorer.py` | `models/memory.py`, `services/memory_service.py` |
|
||||
| M.2 | `services/memory/forgetting_curve.py`, `tests/test_forgetting_curve.py` | `models/memory.py`, `services/memory_service.py` |
|
||||
| M.3 | `services/memory/daily_digest.py`, `services/memory/reminder_scheduler.py`, `tests/test_proactive_reminder.py` | `services/memory_service.py`, `services/scheduler_service.py` |
|
||||
|
||||
---
|
||||
|
||||
## 与 Agent Phase 1-5 的关系
|
||||
|
||||
| Agent Phase | Memory 协作内容 |
|
||||
|-------------|----------------|
|
||||
| Phase 1 | Memory 追踪用户交互频率 |
|
||||
| Phase 2 | Memory 服务被 Librarian Agent 调用 |
|
||||
| Phase 3 | 支持动态协作时的记忆共享 |
|
||||
| Phase 4 | Memory 重要性可视化 |
|
||||
| Phase 5 | 高级记忆关联分析 |
|
||||
| **Phase M** | **独立 Memory 升级路径,可与 Phase 1-5 并行推进** |
|
||||
|
||||
---
|
||||
|
||||
## 注意事项
|
||||
|
||||
| 注意事项 | 说明 |
|
||||
|---------|------|
|
||||
| M.1 是基础 | M.2 和 M.3 都依赖 M.1 的重要性评分 |
|
||||
| 渐进式遗忘 | 不是删除,是降权和归档 |
|
||||
| 主动提醒需用户授权 | 提醒推送需要用户明确开启 |
|
||||
| 不改变现有检索逻辑 | Memory 升级是独立于 RAG 的 |
|
||||
|
||||
---
|
||||
|
||||
## 目标:拟人化记忆
|
||||
|
||||
```
|
||||
现在的 Jarvis:
|
||||
用户问什么,Jarvis 答什么,不问就不说
|
||||
|
||||
升级后的 Jarvis:
|
||||
- 知道什么对你重要(频率+情绪+影响面)
|
||||
- 知道什么是你的痛点(反复问的问题)
|
||||
- 会主动提醒你关心的事(不是等用户问)
|
||||
- 知道什么可以忘记(低频记忆自然衰减)
|
||||
```
|
||||
410
development-doc/plan/memory-update/checklist.md
Normal file
410
development-doc/plan/memory-update/checklist.md
Normal file
@@ -0,0 +1,410 @@
|
||||
# Jarvis Memory 升级执行清单
|
||||
|
||||
日期:2026-04-04
|
||||
状态:执行清单
|
||||
升级方向:拟人化记忆系统
|
||||
|
||||
---
|
||||
|
||||
## 使用说明
|
||||
|
||||
- 完成前使用 `- [ ]`
|
||||
- 完成后改成 `- [x]`
|
||||
- Day M.2 默认依赖 Day M.1 的重要性评分完成后再推进
|
||||
- Day M.3 默认依赖 Day M.1 和 M.2 完成后再推进
|
||||
|
||||
---
|
||||
|
||||
## Day M.1:重要性评分系统(4天)
|
||||
|
||||
Day M.1 目标:让 Jarvis 知道「什么对你重要」。
|
||||
|
||||
### Task M.1.1:实现 FrequencyTracker
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/frequency_tracker.py`
|
||||
|
||||
- [ ] 实现 `FrequencyTracker` 类
|
||||
|
||||
- [ ] 实现 `increment()` 方法
|
||||
```python
|
||||
def increment(self, memory: UserMemory) -> UserMemory:
|
||||
memory.frequency_count += 1
|
||||
memory.last_recalled_at = datetime.now()
|
||||
return memory
|
||||
```
|
||||
|
||||
- [ ] 实现 `get_time_decay()` 方法
|
||||
|
||||
### Task M.1.2:实现 EmotionAnalyzer
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/emotion_analyzer.py`
|
||||
|
||||
- [ ] 实现 `EmotionAnalyzer` 类
|
||||
|
||||
- [ ] 定义 `EMOTION_KEYWORDS` 字典
|
||||
```python
|
||||
EMOTION_KEYWORDS = {
|
||||
"急": 1.0,
|
||||
"很重要": 0.9,
|
||||
"困扰": 0.8,
|
||||
"担心": 0.7,
|
||||
"想解决": 0.6,
|
||||
"无所谓": 0.1,
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] 实现 `extract()` 方法 - 从文本提取情绪关键词
|
||||
|
||||
- [ ] 实现 `calculate_score()` 方法 - 计算情绪分数
|
||||
|
||||
### Task M.1.3:实现 ImpactEvaluator
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/impact_evaluator.py`
|
||||
|
||||
- [ ] 实现 `ImpactEvaluator` 类
|
||||
|
||||
- [ ] 实现 `evaluate()` 方法
|
||||
```python
|
||||
def evaluate(self, memory: UserMemory) -> float:
|
||||
# 关联话题越多,影响面越大
|
||||
return min(1.0, len(memory.associated_topics) / IMPACT_THRESHOLD)
|
||||
```
|
||||
|
||||
### Task M.1.4:实现 ImportanceScorer
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/importance_scorer.py`
|
||||
|
||||
- [ ] 实现 `ImportanceScorer` 类
|
||||
|
||||
- [ ] 实现 `calculate_score()` 综合评分方法
|
||||
```python
|
||||
def calculate_score(self, memory: UserMemory) -> float:
|
||||
frequency = self.tracker.get_frequency_score(memory) * 0.35
|
||||
recency = self.tracker.get_recency_score(memory) * 0.20
|
||||
emotion = self.emotion_analyzer.calculate_score(memory) * 0.25
|
||||
impact = self.impact_evaluator.evaluate(memory) * 0.20
|
||||
return frequency + recency + emotion + impact
|
||||
```
|
||||
|
||||
- [ ] 实现 `get_importance_level()` 方法
|
||||
|
||||
- [ ] 实现 `should_escalate()` 方法
|
||||
|
||||
### Task M.1.5:修改 UserMemory 模型
|
||||
|
||||
- [ ] 修改 `backend/app/models/memory.py`
|
||||
|
||||
- [ ] 增加字段:
|
||||
```python
|
||||
frequency_count: int = 0
|
||||
last_recalled_at: DateTime = None
|
||||
emotion_tags: list[str] = []
|
||||
importance_score: float = 0.5
|
||||
importance_level: str = "medium"
|
||||
associated_topics: list[str] = []
|
||||
```
|
||||
|
||||
### Task M.1.6:集成到 MemoryService
|
||||
|
||||
- [ ] 修改 `backend/app/services/memory_service.py`
|
||||
|
||||
- [ ] 集成 `ImportanceScorer`
|
||||
|
||||
- [ ] 修改 `add_memory()` 方法计算重要性
|
||||
|
||||
- [ ] 修改 `recall_memories()` 方法按重要性排序
|
||||
|
||||
### Task M.1.7:补测试
|
||||
|
||||
- [ ] 新增 `backend/tests/services/test_importance_scorer.py`
|
||||
|
||||
- [ ] 测试频率追踪
|
||||
|
||||
- [ ] 测试情绪分析
|
||||
|
||||
- [ ] 测试重要性评分
|
||||
|
||||
- [ ] 测试重要性等级划分
|
||||
|
||||
### Day M.1 验收
|
||||
|
||||
- [ ] 频率追踪正常(recall_count 每次 +1)
|
||||
- [ ] 情绪识别准确(「急」「很重要」等能识别)
|
||||
- [ ] 重要性分数正确(高频+情绪 = importance >= 0.8)
|
||||
- [ ] 评分影响排序(高重要性记忆排在前面)
|
||||
- [ ] 单元测试覆盖率 > 80%
|
||||
|
||||
---
|
||||
|
||||
## Day M.2:遗忘曲线系统(3天)
|
||||
|
||||
Day M.2 目标:让 Jarvis 知道「什么可以忘记」。
|
||||
|
||||
### Task M.2.1:实现 ForgettingCurve
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/forgetting_curve.py`
|
||||
|
||||
- [ ] 实现 `ForgettingCurve` 类
|
||||
|
||||
- [ ] 实现 `calculate_decay()` 方法
|
||||
```python
|
||||
def calculate_decay(self, memory: UserMemory) -> float:
|
||||
half_life = self.get_half_life(memory)
|
||||
days = (datetime.now() - memory.last_accessed_at).days
|
||||
return exp(-days / half_life)
|
||||
```
|
||||
|
||||
- [ ] 实现 `get_half_life()` 方法(重要性影响半衰期)
|
||||
|
||||
### Task M.2.2:实现 MemoryDecay
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/memory_decay.py`
|
||||
|
||||
- [ ] 实现 `MemoryDecay` 类
|
||||
|
||||
- [ ] 实现 `should_archive()` 方法(decay < 0.2)
|
||||
|
||||
- [ ] 实现 `should_deprioritize()` 方法(decay < 0.5)
|
||||
|
||||
- [ ] 实现 `archive_memory()` 方法
|
||||
|
||||
- [ ] 实现 `restore_from_archive()` 方法
|
||||
|
||||
### Task M.2.3:实现 MemoryReinforcement
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/reinforcement.py`
|
||||
|
||||
- [ ] 实现 `MemoryReinforcement` 类
|
||||
|
||||
- [ ] 实现 `trigger()` 方法(召回时强化)
|
||||
|
||||
- [ ] 实现 `auto_reinforce()` 方法(定期强化 high 级别)
|
||||
|
||||
### Task M.2.4:修改 UserMemory 模型
|
||||
|
||||
- [ ] 修改 `backend/app/models/memory.py`
|
||||
|
||||
- [ ] 增加字段:
|
||||
```python
|
||||
decay_score: float = 1.0
|
||||
is_archived: bool = False
|
||||
last_accessed_at: DateTime = None
|
||||
archive_at: DateTime = None
|
||||
```
|
||||
|
||||
### Task M.2.5:集成到 MemoryService
|
||||
|
||||
- [ ] 修改 `backend/app/services/memory_service.py`
|
||||
|
||||
- [ ] 集成 ForgettingCurve
|
||||
|
||||
- [ ] 修改 recall_memories() 更新 last_accessed_at
|
||||
|
||||
- [ ] 集成 MemoryReinforcement
|
||||
|
||||
### Task M.2.6:添加调度任务
|
||||
|
||||
- [ ] 修改 `backend/app/services/scheduler_service.py`
|
||||
|
||||
- [ ] 添加每日遗忘检查(cron: 03:00)
|
||||
|
||||
- [ ] 添加每周强化任务(cron: 周一 04:00)
|
||||
|
||||
### Task M.2.7:补测试
|
||||
|
||||
- [ ] 新增 `backend/tests/services/test_forgetting_curve.py`
|
||||
|
||||
- [ ] 测试遗忘曲线计算
|
||||
|
||||
- [ ] 测试高重要性记忆衰减慢
|
||||
|
||||
- [ ] 测试归档/恢复
|
||||
|
||||
### Day M.2 验收
|
||||
|
||||
- [ ] 遗忘曲线正确(30 天后 decay ≈ 0.5)
|
||||
- [ ] 高重要性记忆衰减慢(high 衰减速度是 low 的 1/6)
|
||||
- [ ] 归档正常(decay < 0.2 自动归档)
|
||||
- [ ] 恢复正常(归档记忆可以恢复)
|
||||
- [ ] 调度任务正常(每日检查、周强化执行)
|
||||
- [ ] 单元测试覆盖率 > 80%
|
||||
|
||||
---
|
||||
|
||||
## Day M.3:主动提醒系统(6天)
|
||||
|
||||
Day M.3 目标:让 Jarvis 从「等用户问」变成「主动关心」。
|
||||
|
||||
### Task M.3.1:实现 DailyDigestGenerator
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/daily_digest.py`
|
||||
|
||||
- [ ] 实现 `DailyDigestGenerator` 类
|
||||
|
||||
- [ ] 定义 `DailyDigest` 数据类
|
||||
|
||||
- [ ] 实现 `generate()` 方法
|
||||
```python
|
||||
async def generate(self, user_id: int, date: date = None) -> DailyDigest:
|
||||
# 1. 获取今日对话摘要
|
||||
# 2. 获取高重要性记忆
|
||||
# 3. 获取待解答问题
|
||||
# 4. 生成建议
|
||||
```
|
||||
|
||||
- [ ] 实现 `get_recent_digests()` 方法
|
||||
|
||||
### Task M.3.2:实现 ReminderScheduler
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/reminder_scheduler.py`
|
||||
|
||||
- [ ] 定义 `Reminder` 数据类
|
||||
|
||||
- [ ] 实现 `ReminderScheduler` 类
|
||||
|
||||
- [ ] 实现 `create_reminder()` 方法
|
||||
|
||||
- [ ] 实现 `get_due_reminders()` 方法
|
||||
|
||||
- [ ] 实现 `snooze()` 方法
|
||||
|
||||
- [ ] 实现 `dismiss()` 方法
|
||||
|
||||
### Task M.3.3:实现 ProactiveInformer
|
||||
|
||||
- [ ] 新增 `backend/app/services/memory/proactive_informer.py`
|
||||
|
||||
- [ ] 实现 `ProactiveInformer` 类
|
||||
|
||||
- [ ] 定义 `TRIGGERS` 配置
|
||||
|
||||
- [ ] 定义 `INFORM_PROBABILITY` 配置
|
||||
|
||||
- [ ] 实现 `should_inform()` 方法
|
||||
|
||||
- [ ] 实现 `get_inform_message()` 方法
|
||||
|
||||
- [ ] 实现 `check_and_inform()` 方法
|
||||
|
||||
### Task M.3.4:创建提醒数据模型
|
||||
|
||||
- [ ] 修改数据库支持 `reminders` 表
|
||||
|
||||
- [ ] 新增 `backend/app/models/reminder.py`
|
||||
|
||||
- [ ] 或在现有模型文件中增加 Reminder 类
|
||||
|
||||
### Task M.3.5:集成到 MemoryService
|
||||
|
||||
- [ ] 修改 `backend/app/services/memory_service.py`
|
||||
|
||||
- [ ] 集成 DailyDigestGenerator
|
||||
|
||||
- [ ] 集成 ProactiveInformer
|
||||
|
||||
- [ ] 修改 recall_memories() 触发主动告知检查
|
||||
|
||||
### Task M.3.6:集成到 SchedulerService
|
||||
|
||||
- [ ] 修改 `backend/app/services/scheduler_service.py`
|
||||
|
||||
- [ ] 添加每日摘要生成(cron: 22:00)
|
||||
|
||||
- [ ] 添加提醒检查任务(cron: 每 15 分钟)
|
||||
|
||||
### Task M.3.7:前端 - 每日摘要展示
|
||||
|
||||
- [ ] 修改前端对话页面
|
||||
|
||||
- [ ] 新增每日摘要卡片组件
|
||||
|
||||
- [ ] 获取和展示今日摘要
|
||||
|
||||
### Task M.3.8:前端 - 主动提醒推送
|
||||
|
||||
- [ ] 新增主动提醒 Toast 组件
|
||||
|
||||
- [ ] 实现稍后/知道了按钮
|
||||
|
||||
- [ ] 推送 WebSocket 集成
|
||||
|
||||
### Task M.3.9:补测试
|
||||
|
||||
- [ ] 新增 `backend/tests/services/test_proactive_reminder.py`
|
||||
|
||||
- [ ] 测试每日摘要生成
|
||||
|
||||
- [ ] 测试提醒创建和调度
|
||||
|
||||
- [ ] 测试主动告知概率
|
||||
|
||||
### Day M.3 验收
|
||||
|
||||
- [ ] 每日摘要生成正常(22:00 自动生成)
|
||||
- [ ] 提醒创建正常(用户可创建提醒)
|
||||
- [ ] 提醒到期触发(定时推送)
|
||||
- [ ] 主动告知概率正确(按配置的概率触发)
|
||||
- [ ] 告知消息自然(像人说话,不生硬)
|
||||
- [ ] 用户可控制(可以关闭主动提醒)
|
||||
- [ ] 单元测试覆盖率 > 80%
|
||||
|
||||
---
|
||||
|
||||
## 总验收清单
|
||||
|
||||
### Phase M.1-M.3 必须完成
|
||||
|
||||
- [ ] 重要性评分系统正常工作
|
||||
- [ ] 遗忘曲线系统正常工作
|
||||
- [ ] 主动提醒系统正常工作
|
||||
- [ ] 单元测试覆盖率 > 80%
|
||||
- [ ] 集成测试通过
|
||||
- [ ] 原有记忆功能无回退
|
||||
|
||||
---
|
||||
|
||||
## 总工作量估算
|
||||
|
||||
| Phase | 工作量 |
|
||||
|-------|--------|
|
||||
| M.1 重要性评分 | 4 天 |
|
||||
| M.2 遗忘曲线 | 3 天 |
|
||||
| M.3 主动提醒 | 6 天 |
|
||||
| **合计** | **13 天** |
|
||||
|
||||
---
|
||||
|
||||
## 产出清单
|
||||
|
||||
| 产出 | 对应 Phase |
|
||||
|------|-----------|
|
||||
| `services/memory/frequency_tracker.py` | M.1 |
|
||||
| `services/memory/emotion_analyzer.py` | M.1 |
|
||||
| `services/memory/impact_evaluator.py` | M.1 |
|
||||
| `services/memory/importance_scorer.py` | M.1 |
|
||||
| `services/memory/forgetting_curve.py` | M.2 |
|
||||
| `services/memory/memory_decay.py` | M.2 |
|
||||
| `services/memory/reinforcement.py` | M.2 |
|
||||
| `services/memory/daily_digest.py` | M.3 |
|
||||
| `services/memory/reminder_scheduler.py` | M.3 |
|
||||
| `services/memory/proactive_informer.py` | M.3 |
|
||||
| `models/memory.py` 更新 | M.1, M.2 |
|
||||
| `models/reminder.py` 新增 | M.3 |
|
||||
| 前端摘要卡片 | M.3 |
|
||||
| 前端提醒 Toast | M.3 |
|
||||
| 单元测试 > 80% | M.1, M.2, M.3 |
|
||||
| 集成测试通过 | M.1, M.2, M.3 |
|
||||
|
||||
---
|
||||
|
||||
## 与 Agent Phase 关系
|
||||
|
||||
| Agent Phase | Memory 协作内容 |
|
||||
|-------------|----------------|
|
||||
| Phase 1 | Memory 追踪用户交互频率 |
|
||||
| Phase 2 | Memory 服务被 Librarian Agent 调用 |
|
||||
| Phase 3 | 支持动态协作时的记忆共享 |
|
||||
| Phase 4 | Memory 重要性可视化 |
|
||||
| Phase 5 | 高级记忆关联分析 |
|
||||
|
||||
**Phase M 可与 Agent Phase 1-5 并行推进。**
|
||||
125
development-doc/plan/memory-update/phase-m-0-current-state.md
Normal file
125
development-doc/plan/memory-update/phase-m-0-current-state.md
Normal file
@@ -0,0 +1,125 @@
|
||||
# Phase M.0:Memory 现状与目标
|
||||
|
||||
日期:2026-04-04
|
||||
状态:已完成
|
||||
升级方向:拟人化记忆系统
|
||||
|
||||
---
|
||||
|
||||
## 1. 本阶段目的
|
||||
|
||||
本文件用于统一背景认知,明确:
|
||||
|
||||
- Jarvis 当前记忆系统处于什么水平
|
||||
- 主要短板是什么
|
||||
- 为什么要升级
|
||||
- 升级后的目标形态是什么
|
||||
|
||||
---
|
||||
|
||||
## 2. 当前 Jarvis Memory 架构
|
||||
|
||||
### 2.1 核心流程
|
||||
|
||||
```
|
||||
用户对话 → MemoryService → Mem0 (facts/preferences/goals)
|
||||
→ BrainService → BrainMemory
|
||||
→ SQLite (memory_summaries)
|
||||
```
|
||||
|
||||
### 2.2 核心文件
|
||||
|
||||
| 文件 | 职责 |
|
||||
|------|------|
|
||||
| `backend/app/services/memory_service.py` | 三层记忆管理 |
|
||||
| `backend/app/services/brain_service.py` | Brain 学习与回忆 |
|
||||
| `backend/app/models/memory.py` | MemorySummary, UserMemory 模型 |
|
||||
| `backend/app/models/brain.py` | BrainEvent, BrainMemory, BrainTag 模型 |
|
||||
|
||||
---
|
||||
|
||||
## 3. 当前能力矩阵
|
||||
|
||||
| 能力 | 状态 | 说明 |
|
||||
|------|------|------|
|
||||
| 短期记忆 | ✅ | messages 表存储当前会话 |
|
||||
| 中期记忆 | ✅ | memory_summaries 跨会话摘要 |
|
||||
| 长期记忆 | ✅ | Mem0 facts/preferences/goals |
|
||||
| 记忆检索 | ✅ | Mem0 语义搜索 + SQLite LIKE |
|
||||
| 记忆重要性 | ❌ | 无重要性评分 |
|
||||
| 频率追踪 | ❌ | 无频率统计 |
|
||||
| 遗忘机制 | ❌ | 存了就不删 |
|
||||
| 主动提醒 | ❌ | 完全被动响应 |
|
||||
|
||||
---
|
||||
|
||||
## 4. 当前短板
|
||||
|
||||
| 短板 | 严重程度 | 影响 |
|
||||
|------|----------|------|
|
||||
| 无重要性评分 | 🔴 高 | 无法区分重要/不重要记忆 |
|
||||
| 无频率追踪 | 🔴 高 | 反复出现的痛点无法识别 |
|
||||
| 无遗忘机制 | 🟡 中 | 存储无限增长,冷门知识占空间 |
|
||||
| 无主动提醒 | 🔴 高 | 用户不问就不说,不像助理 |
|
||||
| 无情绪感知 | 🟡 中 | 无法区分用户急/重要的表达 |
|
||||
|
||||
---
|
||||
|
||||
## 5. 目标架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ 用户对话输入 │
|
||||
└─────────────────────────┬───────────────────────────────────┘
|
||||
│
|
||||
┌───────────┴───────────┐
|
||||
│ Importance Scorer │ ← M.1 新增
|
||||
│ - 频率 (你提几次了) │
|
||||
│ - 情绪 (急/重要/困扰) │
|
||||
│ - 影响面 (关联多少) │
|
||||
│ - 时间 (最近/当时) │
|
||||
└───────────┬───────────┘
|
||||
│
|
||||
┌────────────────┼────────────────┐
|
||||
▼ ▼ ▼
|
||||
┌───────────┐ ┌───────────┐ ┌───────────┐
|
||||
│ 高优先级 │ │ 中优先级 │ │ 低优先级 │
|
||||
│ (主动提醒)│ │ (记住) │ │ (归档) │
|
||||
└─────┬─────┘ └─────┬─────┘ └─────┬─────┘
|
||||
│ │ │
|
||||
▼ ▼ ▼
|
||||
形成观点 选择性回忆 自然遗忘
|
||||
主动提醒 需要时召回 (降权归档)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 与 VCPToolBox 的关系
|
||||
|
||||
| VCPToolBox 能力 | 本升级是否借鉴 | 理由 |
|
||||
|-----------------|---------------|------|
|
||||
| TagMemo 向量检索 | ❌ 不借鉴 | 你需要的是「记住什么」,不是「检索更准」 |
|
||||
| LIF 脉冲扩散 | ❌ 不借鉴 | 同上 |
|
||||
| EPA 语义分析 | ❌ 不借鉴 | 同上 |
|
||||
| 频率追踪 | ✅ 参考 | 这是你核心需要的,VCPToolBox 没有,自己设计 |
|
||||
|
||||
**结论:VCPToolBox 解决的是「知识管理」,本升级解决的是「记忆价值判断」,是两个不同的问题。**
|
||||
|
||||
---
|
||||
|
||||
## 7. 升级后的直观改变
|
||||
|
||||
| 现在 | 升级后 |
|
||||
|------|--------|
|
||||
| 问 3 次同一个问题,Jarvis 每次都当新问题 | 第 2 次就记住,第 3 次能说"你之前问过..." |
|
||||
| 很少用的知识自动沉底 | 低频知识自动归档,需要时能恢复 |
|
||||
| 你问"明天干嘛" → 只能看日程 | 主动说"你昨天说想换工作,提醒你查一下 JD" |
|
||||
| 告诉 Jarvis 的事可能下次就忘了 | 重要的事主动记,冷门的事知道就行 |
|
||||
|
||||
---
|
||||
|
||||
## 8. 本阶段产出要求
|
||||
|
||||
- [x] 团队对 Jarvis 当前记忆问题和目标方向达成一致
|
||||
- [x] 明确了与 VCPToolBox 的关系(不借鉴 VCPToolBox)
|
||||
- [x] 后续 phase 文档能够在这个认知基础上展开
|
||||
@@ -0,0 +1,219 @@
|
||||
# Phase M.1:重要性评分系统
|
||||
|
||||
日期:2026-04-04
|
||||
状态:规划中
|
||||
依赖:无
|
||||
工作量:4 天
|
||||
|
||||
---
|
||||
|
||||
## 1. 本阶段目的
|
||||
|
||||
建立记忆重要性评分体系,让 Jarvis 知道「什么对你重要」。
|
||||
|
||||
核心问题:
|
||||
- 你提了 3 次同一个问题 → 这是你的痛点,应该深入解决
|
||||
- 你说「急」「很重要」「困扰我」 → 情绪标记,应该优先级高
|
||||
- 这个话题关联了多少其他话题 → 影响面越大越重要
|
||||
|
||||
---
|
||||
|
||||
## 2. 核心架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ ImportanceScorer │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ calculate_score(memory) → importance_score (0.0-1.0) │
|
||||
│ │
|
||||
│ 评分维度: │
|
||||
│ - frequency_score (频率) × 0.35 │
|
||||
│ - recency_score (时效) × 0.20 │
|
||||
│ - emotion_score (情绪) × 0.25 │
|
||||
│ - impact_score (影响面) × 0.20 │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 评分维度详解
|
||||
|
||||
### 3.1 Frequency Score (频率)
|
||||
|
||||
```python
|
||||
frequency_score = min(1.0, recall_count / FREQUENCY_THRESHOLD)
|
||||
# FREQUENCY_THRESHOLD = 3 # 提 3 次算高频
|
||||
|
||||
# 同时考虑时间衰减
|
||||
time_decay = exp(-days_since_last_recall / HALF_LIFE_DAYS)
|
||||
# HALF_LIFE_DAYS = 7 # 7 天减半
|
||||
```
|
||||
|
||||
### 3.2 Recency Score (时效性)
|
||||
|
||||
```python
|
||||
recency_score = exp(-days_since_creation / RECENCY_HALF_LIFE)
|
||||
# RECENCY_HALF_LIFE = 30 # 30 天减半
|
||||
|
||||
# 但重要事件例外:即使久远也保持高时效
|
||||
if is_emotion_tagged:
|
||||
recency_score = max(recency_score, 0.7)
|
||||
```
|
||||
|
||||
### 3.3 Emotion Score (情绪)
|
||||
|
||||
```python
|
||||
EMOTION_KEYWORDS = {
|
||||
"急": 1.0,
|
||||
"很重要": 0.9,
|
||||
"困扰": 0.8,
|
||||
"担心": 0.7,
|
||||
"想解决": 0.6,
|
||||
"无所谓": 0.1,
|
||||
}
|
||||
|
||||
emotion_score = max([EMOTION_KEYWORDS.get(kw, 0)
|
||||
for kw in extracted_emotions], default=0.0)
|
||||
```
|
||||
|
||||
### 3.4 Impact Score (影响面)
|
||||
|
||||
```python
|
||||
# 关联多少其他记忆/话题
|
||||
impact_score = min(1.0, associated_memory_count / IMPACT_THRESHOLD)
|
||||
# IMPACT_THRESHOLD = 5 # 关联 5 个算满
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. 核心文件
|
||||
|
||||
### 4.1 新增文件
|
||||
|
||||
| 文件 | 职责 |
|
||||
|------|------|
|
||||
| `services/memory/frequency_tracker.py` | 频率追踪器 |
|
||||
| `services/memory/emotion_analyzer.py` | 情绪分析器 |
|
||||
| `services/memory/impact_evaluator.py` | 影响面评估器 |
|
||||
| `services/memory/importance_scorer.py` | 综合评分器 |
|
||||
|
||||
### 4.2 修改文件
|
||||
|
||||
| 文件 | 修改内容 |
|
||||
|------|---------|
|
||||
| `models/memory.py` | 增加 frequency_count, last_recalled_at, emotion_tags 字段 |
|
||||
| `services/memory_service.py` | 集成 ImportanceScorer |
|
||||
|
||||
---
|
||||
|
||||
## 5. API 设计
|
||||
|
||||
### 5.1 ImportanceScorer
|
||||
|
||||
```python
|
||||
class ImportanceScorer:
|
||||
def calculate_score(self, memory: UserMemory) -> float:
|
||||
"""返回 0.0-1.0 的重要性分数"""
|
||||
|
||||
def get_importance_level(self, memory: UserMemory) -> Literal["high", "medium", "low"]:
|
||||
"""返回重要性等级"""
|
||||
|
||||
def should_escalate(self, memory: UserMemory) -> bool:
|
||||
"""是否应该升级为高优先级记忆"""
|
||||
```
|
||||
|
||||
### 5.2 集成到 MemoryService
|
||||
|
||||
```python
|
||||
class MemoryService:
|
||||
async def add_memory(self, user_id: int, content: str, emotion_tags: list[str] = None):
|
||||
# 添加记忆时计算初始重要性
|
||||
importance = self.scorer.calculate_score(new_memory)
|
||||
|
||||
async def recall_memories(self, query: str, user_id: int, top_k: int = 5):
|
||||
# 检索时考虑重要性排序
|
||||
# 高重要性记忆排在前面
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 数据模型变更
|
||||
|
||||
### 6.1 UserMemory 扩展
|
||||
|
||||
```python
|
||||
class UserMemory:
|
||||
# 现有字段...
|
||||
frequency_count: int = 0 # 被回忆次数
|
||||
last_recalled_at: DateTime = None # 上次被回忆时间
|
||||
emotion_tags: list[str] = [] # 情绪标签
|
||||
importance_score: float = 0.5 # 重要性分数
|
||||
importance_level: str = "medium" # high/medium/low
|
||||
associated_topics: list[str] = [] # 关联话题
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. 测试设计
|
||||
|
||||
### 7.1 频率追踪测试
|
||||
|
||||
```python
|
||||
def test_frequency_increase():
|
||||
memory = create_memory(frequency_count=0)
|
||||
memory = tracker.increment(memory)
|
||||
assert memory.frequency_count == 1
|
||||
|
||||
def test_frequency_decay_over_time():
|
||||
# 7 天后频率应该衰减
|
||||
pass
|
||||
```
|
||||
|
||||
### 7.2 情绪分析测试
|
||||
|
||||
```python
|
||||
def test_emotion_extraction():
|
||||
text = "这个问题很急,急需解决"
|
||||
emotions = analyzer.extract(text)
|
||||
assert "急" in emotions
|
||||
|
||||
def test_emotion_scoring():
|
||||
score = scorer.calculate_emotion_score(["急", "很重要"])
|
||||
assert score >= 0.9
|
||||
```
|
||||
|
||||
### 7.3 综合评分测试
|
||||
|
||||
```python
|
||||
def test_importance_ranking():
|
||||
memories = [low_freq, high_freq, emotional]
|
||||
ranked = sorter.rank_by_importance(memories)
|
||||
assert ranked[0] == emotional # 情绪标签最重
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. 验收标准
|
||||
|
||||
| 标准 | 说明 |
|
||||
|------|------|
|
||||
| 频率追踪正常 | recall_count 每次召回 +1 |
|
||||
| 情绪识别准确 | 「急」「很重要」等能识别 |
|
||||
| 重要性分数正确 | 高频+情绪 = importance >= 0.8 |
|
||||
| 评分影响排序 | 高重要性记忆排在检索结果前面 |
|
||||
| 单元测试覆盖率 | > 80% |
|
||||
|
||||
---
|
||||
|
||||
## 9. 工作量估算
|
||||
|
||||
| 任务 | 工作量 |
|
||||
|------|--------|
|
||||
| FrequencyTracker | 0.5 天 |
|
||||
| EmotionAnalyzer | 0.5 天 |
|
||||
| ImpactEvaluator | 0.5 天 |
|
||||
| ImportanceScorer | 1 天 |
|
||||
| 模型变更 | 0.5 天 |
|
||||
| 集成到 MemoryService | 0.5 天 |
|
||||
| 测试 | 1 天 |
|
||||
| **合计** | **4 天** |
|
||||
@@ -0,0 +1,228 @@
|
||||
# Phase M.2:遗忘曲线系统
|
||||
|
||||
日期:2026-04-04
|
||||
状态:规划中
|
||||
依赖:M.1 (重要性评分)
|
||||
工作量:3 天
|
||||
|
||||
---
|
||||
|
||||
## 1. 本阶段目的
|
||||
|
||||
实现「选择性遗忘」机制,让 Jarvis 知道「什么可以忘记」。
|
||||
|
||||
核心问题:
|
||||
- 冷门知识不应该一直占存储
|
||||
- 但不能直接删,要有归档机制
|
||||
- 重要记忆应该被强化,不容易忘
|
||||
|
||||
---
|
||||
|
||||
## 2. 核心架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ ForgettingCurve │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ decay_score = exp(-time_since_access / half_life) │
|
||||
│ │
|
||||
│ 遗忘策略: │
|
||||
│ - decay_score < 0.2 → 归档到 cold_storage │
|
||||
│ - decay_score < 0.5 → 降权,不参与主动提醒 │
|
||||
│ - importance_level=high → 半衰期延长 3x │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 艾宾浩斯遗忘曲线模型
|
||||
|
||||
### 3.1 基础遗忘曲线
|
||||
|
||||
```
|
||||
保留率
|
||||
100% |████████████
|
||||
80% |██████████
|
||||
60% |███████
|
||||
40% |█████
|
||||
20% |██
|
||||
0% |________________________ 时间
|
||||
1天 7天 30天 90天
|
||||
```
|
||||
|
||||
### 3.2 Jarvis 遗忘策略
|
||||
|
||||
```python
|
||||
# 基础半衰期:30 天
|
||||
BASE_HALF_LIFE_DAYS = 30
|
||||
|
||||
# 重要性影响半衰期
|
||||
if importance_level == "high":
|
||||
half_life = BASE_HALF_LIFE_DAYS * 3 # 90 天
|
||||
elif importance_level == "medium":
|
||||
half_life = BASE_HALF_LIFE_DAYS * 1 # 30 天
|
||||
else:
|
||||
half_life = BASE_HALF_LIFE_DAYS * 0.5 # 15 天
|
||||
|
||||
# 遗忘分数
|
||||
decay_score = exp(-days_since_access / half_life)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. 核心文件
|
||||
|
||||
### 4.1 新增文件
|
||||
|
||||
| 文件 | 职责 |
|
||||
|------|------|
|
||||
| `services/memory/forgetting_curve.py` | 遗忘曲线计算 |
|
||||
| `services/memory/memory_decay.py` | 记忆衰减处理 |
|
||||
| `services/memory/reinforcement.py` | 记忆强化触发 |
|
||||
|
||||
### 4.2 修改文件
|
||||
|
||||
| 文件 | 修改内容 |
|
||||
|------|---------|
|
||||
| `models/memory.py` | 增加 decay_score, is_archived, last_accessed_at 字段 |
|
||||
| `services/memory_service.py` | 集成遗忘逻辑 |
|
||||
|
||||
---
|
||||
|
||||
## 5. API 设计
|
||||
|
||||
### 5.1 ForgettingCurve
|
||||
|
||||
```python
|
||||
class ForgettingCurve:
|
||||
def calculate_decay(self, memory: UserMemory) -> float:
|
||||
"""返回 0.0-1.0 的保留分数"""
|
||||
|
||||
def should_archive(self, memory: UserMemory) -> bool:
|
||||
"""是否应该归档"""
|
||||
|
||||
def should_deprioritize(self, memory: UserMemory) -> bool:
|
||||
"""是否应该降权(不参与主动提醒)"""
|
||||
```
|
||||
|
||||
### 5.2 Reinforcement
|
||||
|
||||
```python
|
||||
class MemoryReinforcement:
|
||||
def trigger(self, memory_id: int) -> None:
|
||||
"""被召回时触发强化"""
|
||||
# 频率 +1
|
||||
# decay_score 重置
|
||||
|
||||
def auto_reinforce(self, user_id: int) -> None:
|
||||
"""定期自动强化高重要性记忆"""
|
||||
# 每周检查,对 high 级别记忆做轻量强化
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 归档机制
|
||||
|
||||
### 6.1 热/冷存储分离
|
||||
|
||||
```python
|
||||
# 热存储:活跃记忆,参与检索和主动提醒
|
||||
HOT_MEMORIES = []
|
||||
|
||||
# 冷存储:归档记忆,不参与主动提醒,按需恢复
|
||||
COLD_MEMORIES = []
|
||||
```
|
||||
|
||||
### 6.2 归档恢复
|
||||
|
||||
```python
|
||||
async def restore_from_archive(self, memory_id: int) -> UserMemory:
|
||||
"""从归档恢复记忆"""
|
||||
# 恢复后 decay_score 重置为 0.8
|
||||
# 重新加入热存储
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. 调度任务
|
||||
|
||||
### 7.1 每日遗忘检查
|
||||
|
||||
```python
|
||||
# 每天凌晨执行
|
||||
@scheduler.scheduled_task("cron", hour=3)
|
||||
async def daily_forgetting_check():
|
||||
"""每日遗忘检查"""
|
||||
# 1. 计算所有记忆的 decay_score
|
||||
# 2. 归档 decay < 0.2 的记忆
|
||||
# 3. 降权 decay < 0.5 的记忆
|
||||
# 4. 强化被召回的记忆
|
||||
```
|
||||
|
||||
### 7.2 每周自动强化
|
||||
|
||||
```python
|
||||
@scheduler.scheduled_task("cron", day_of_week="mon", hour=4)
|
||||
async def weekly_reinforcement():
|
||||
"""每周自动强化"""
|
||||
# 对 high 重要性记忆做轻量强化
|
||||
# frequency_count *= 1.1 (上限 10)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. 测试设计
|
||||
|
||||
### 8.1 遗忘曲线测试
|
||||
|
||||
```python
|
||||
def test_decay_after_30_days():
|
||||
memory = create_memory(last_accessed_at=days_ago(30))
|
||||
decay = curve.calculate_decay(memory)
|
||||
assert 0.4 < decay < 0.6 # 约 50% 保留
|
||||
|
||||
def test_high_importance_slower_decay():
|
||||
high = create_memory(importance_level="high", last_accessed_at=days_ago(30))
|
||||
low = create_memory(importance_level="low", last_accessed_at=days_ago(30))
|
||||
assert curve.calculate_decay(high) > curve.calculate_decay(low)
|
||||
```
|
||||
|
||||
### 8.2 归档测试
|
||||
|
||||
```python
|
||||
def test_archive_low_decay():
|
||||
memory = create_memory(decay_score=0.15)
|
||||
assert curve.should_archive(memory) == True
|
||||
|
||||
def test_restore_from_archive():
|
||||
memory = service.restore_from_archive(memory_id)
|
||||
assert memory.is_archived == False
|
||||
assert memory.decay_score > 0.5
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. 验收标准
|
||||
|
||||
| 标准 | 说明 |
|
||||
|------|------|
|
||||
| 遗忘曲线正确 | 30 天后 decay ≈ 0.5 |
|
||||
| 高重要性记忆衰减慢 | high 级别衰减速度是 low 的 1/6 |
|
||||
| 归档正常 | decay < 0.2 自动归档 |
|
||||
| 恢复正常 | 归档记忆可以恢复 |
|
||||
| 调度任务正常 | 每日检查、周强化执行 |
|
||||
| 单元测试覆盖率 | > 80% |
|
||||
|
||||
---
|
||||
|
||||
## 10. 工作量估算
|
||||
|
||||
| 任务 | 工作量 |
|
||||
|------|--------|
|
||||
| ForgettingCurve | 0.5 天 |
|
||||
| MemoryDecay | 0.5 天 |
|
||||
| Reinforcement | 0.5 天 |
|
||||
| 归档机制 | 0.5 天 |
|
||||
| 调度任务 | 0.5 天 |
|
||||
| 测试 | 0.5 天 |
|
||||
| **合计** | **3 天** |
|
||||
@@ -0,0 +1,360 @@
|
||||
# Phase M.3:主动提醒系统
|
||||
|
||||
日期:2026-04-04
|
||||
状态:规划中
|
||||
依赖:M.1 (重要性评分), M.2 (遗忘曲线)
|
||||
工作量:5 天
|
||||
|
||||
---
|
||||
|
||||
## 1. 本阶段目的
|
||||
|
||||
让 Jarvis 从「等用户问」变成「主动关心」。
|
||||
|
||||
核心问题:
|
||||
- Jarvis 知道用户关心什么(高重要性记忆)
|
||||
- Jarvis 知道用户最近做了什么(每日摘要)
|
||||
- Jarvis 主动提醒,而不是等用户问
|
||||
|
||||
---
|
||||
|
||||
## 2. 核心架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ ProactiveReminderSystem │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ │
|
||||
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
||||
│ │ DailyDigest │ + │ Reminder │ + │ MemoryInformer│ │
|
||||
│ │ Generator │ │ Scheduler │ │ │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │ 每日摘要生成 │ │ 提醒调度 │ │ 主动提醒推送 │ │
|
||||
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
||||
│ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 每日摘要生成器 (DailyDigestGenerator)
|
||||
|
||||
### 3.1 输入来源
|
||||
|
||||
```
|
||||
今日输入:
|
||||
- 用户今日对话摘要 (memory_summaries)
|
||||
- 用户今日提到的重点 (high importance memories)
|
||||
- 用户今日创建的任务 (tasks)
|
||||
- 用户今日查阅的知识 (knowledge retrieval logs)
|
||||
```
|
||||
|
||||
### 3.2 输出格式
|
||||
|
||||
```json
|
||||
{
|
||||
"date": "2026-04-04",
|
||||
"summary": "今天你主要在处理工作问题,提到了换工作的想法",
|
||||
"key_points": [
|
||||
{"content": "想换工作", "importance": 0.9, "source": "conversation"},
|
||||
{"content": "项目 deadline 是周五", "importance": 0.8, "source": "task"}
|
||||
],
|
||||
"pending_questions": [
|
||||
{"content": "量子计算的问题还没完全理解", "importance": 0.6}
|
||||
],
|
||||
"suggestions": [
|
||||
{"text": "明天可以继续聊换工作的话题", "reason": "重要性高且今天没深入"},
|
||||
{"text": "量子计算的资料可以找找更通俗的解释", "reason": "还没理解"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 3.3 生成时机
|
||||
|
||||
```python
|
||||
# 每天晚上 10 点生成
|
||||
@scheduler.scheduled_task("cron", hour=22)
|
||||
async def generate_daily_digest():
|
||||
"""生成每日摘要"""
|
||||
digest = await generator.generate(user_id)
|
||||
await storage.save(digest)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. 提醒调度器 (ReminderScheduler)
|
||||
|
||||
### 4.1 提醒类型
|
||||
|
||||
| 类型 | 触发条件 | 推送时机 |
|
||||
|------|---------|---------|
|
||||
| 后续提醒 | 用户说「回头提醒我」 | 指定时间 |
|
||||
| 关联提醒 | 提到的话题有关联记忆 | 下次对话时 |
|
||||
| 周期提醒 | 每周/每月固定事项 | 周期首日 |
|
||||
| 遗忘提醒 | 归档记忆被重新提到 | 恢复后提醒 |
|
||||
|
||||
### 4.2 调度逻辑
|
||||
|
||||
```python
|
||||
class ReminderScheduler:
|
||||
def schedule_reminder(self, user_id: int, reminder: Reminder):
|
||||
"""安排提醒"""
|
||||
|
||||
def get_due_reminders(self, user_id: int) -> list[Reminder]:
|
||||
"""获取到期的提醒"""
|
||||
|
||||
def snooze_reminder(self, reminder_id: int, minutes: int):
|
||||
"""推迟提醒"""
|
||||
```
|
||||
|
||||
### 4.3 提醒存储
|
||||
|
||||
```python
|
||||
class Reminder:
|
||||
id: int
|
||||
user_id: int
|
||||
content: str
|
||||
trigger_type: str # "time" / "context" / "periodic"
|
||||
trigger_at: DateTime
|
||||
context: dict # 关联的 memory_id 等
|
||||
status: str # "pending" / "sent" / "snoozed"
|
||||
created_at: DateTime
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. 主动记忆告知器 (ProactiveMemoryInformer)
|
||||
|
||||
### 5.1 触发条件
|
||||
|
||||
```python
|
||||
TRIGGERS = {
|
||||
"high_importance_topic": {
|
||||
"condition": "用户提到高重要性话题",
|
||||
"action": "提及关联记忆",
|
||||
"example": "你说想换工作,要不要看看之前收藏的 JD?"
|
||||
},
|
||||
"repeat_question": {
|
||||
"condition": "用户重复问某个问题",
|
||||
"action": "主动说之前回答过",
|
||||
"example": "你之前问过量子计算,我再给你解释一下?"
|
||||
},
|
||||
"forgotten_context": {
|
||||
"condition": "用户提到已归档的记忆",
|
||||
"action": "提示可以恢复",
|
||||
"example": "这个话题你一个月前聊过,要我恢复一下吗?"
|
||||
},
|
||||
"pending_goal": {
|
||||
"condition": "用户设了目标但没进展",
|
||||
"action": "温和提醒",
|
||||
"example": "你之前说想学 Python,有进展吗?"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 5.2 告知时机
|
||||
|
||||
```python
|
||||
# 不是每次对话都告知,有概率控制
|
||||
INFORM_PROBABILITY = {
|
||||
"high_importance_topic": 0.8, # 高重要性话题 80% 主动提
|
||||
"repeat_question": 1.0, # 重复问题 100% 主动提
|
||||
"forgotten_context": 0.5, # 已归档话题 50% 提示
|
||||
"pending_goal": 0.3, # 待办目标 30% 温和提醒
|
||||
}
|
||||
```
|
||||
|
||||
### 5.3 告知风格
|
||||
|
||||
```python
|
||||
INFORM_STYLE = {
|
||||
"casual": "对了,你之前提到...",
|
||||
"gentle": "不知道你有没有注意到...",
|
||||
"helpful": "我记起你关心这个,要不看看..."
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 核心文件
|
||||
|
||||
### 6.1 新增文件
|
||||
|
||||
| 文件 | 职责 |
|
||||
|------|------|
|
||||
| `services/memory/daily_digest.py` | 每日摘要生成 |
|
||||
| `services/memory/reminder_scheduler.py` | 提醒调度 |
|
||||
| `services/memory/proactive_informer.py` | 主动告知 |
|
||||
| `services/memory/reminder_model.py` | 提醒数据模型 |
|
||||
|
||||
### 6.2 修改文件
|
||||
|
||||
| 文件 | 修改内容 |
|
||||
|------|---------|
|
||||
| `services/scheduler_service.py` | 集成主动提醒调度 |
|
||||
| `services/memory_service.py` | 集成 ProactiveInformer |
|
||||
| `routers/conversation.py` | 主动告知触发点 |
|
||||
|
||||
---
|
||||
|
||||
## 7. API 设计
|
||||
|
||||
### 7.1 DailyDigestGenerator
|
||||
|
||||
```python
|
||||
class DailyDigestGenerator:
|
||||
async def generate(self, user_id: int, date: date = None) -> DailyDigest:
|
||||
"""生成每日摘要"""
|
||||
|
||||
async def get_recent_digests(self, user_id: int, limit: int = 7) -> list[Digest]:
|
||||
"""获取最近 N 天的摘要"""
|
||||
```
|
||||
|
||||
### 7.2 ReminderScheduler
|
||||
|
||||
```python
|
||||
class ReminderScheduler:
|
||||
async def create_reminder(self, user_id: int, content: str, trigger_at: datetime):
|
||||
"""创建提醒"""
|
||||
|
||||
async def get_due_reminders(self, user_id: int) -> list[Reminder]:
|
||||
"""获取到期提醒"""
|
||||
|
||||
async def snooze(self, reminder_id: int, minutes: int):
|
||||
"""推迟提醒"""
|
||||
```
|
||||
|
||||
### 7.3 ProactiveInformer
|
||||
|
||||
```python
|
||||
class ProactiveInformer:
|
||||
def should_inform(self, user_id: int, trigger_type: str) -> bool:
|
||||
"""是否应该告知"""
|
||||
|
||||
def get_inform_message(self, user_id: int, trigger_type: str, context: dict) -> str:
|
||||
"""生成告知消息"""
|
||||
|
||||
async def check_and_inform(self, conversation_context: dict) -> str | None:
|
||||
"""检查并返回告知消息,无则返回 None"""
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. 前端集成
|
||||
|
||||
### 8.1 每日摘要展示
|
||||
|
||||
```vue
|
||||
<!-- 每日摘要卡片 -->
|
||||
<div v-if="dailyDigest">
|
||||
<h3>今日摘要</h3>
|
||||
<p>{{ dailyDigest.summary }}</p>
|
||||
<div v-for="point in dailyDigest.keyPoints">
|
||||
- {{ point.content }}
|
||||
</div>
|
||||
<div v-if="dailyDigest.suggestions.length">
|
||||
<h4>建议</h4>
|
||||
<p>{{ dailyDigest.suggestions[0].text }}</p>
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
### 8.2 主动提醒推送
|
||||
|
||||
```vue
|
||||
<!-- 主动提醒弹窗 -->
|
||||
<div v-if="activeReminder" class="reminder-toast">
|
||||
<p>{{ activeReminder.content }}</p>
|
||||
<button @click="snooze">稍后</button>
|
||||
<button @click="dismiss">知道了</button>
|
||||
</div>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. 测试设计
|
||||
|
||||
### 9.1 每日摘要测试
|
||||
|
||||
```python
|
||||
async def test_digest_generation():
|
||||
digest = await generator.generate(user_id=1)
|
||||
assert digest.summary is not None
|
||||
assert len(digest.key_points) > 0
|
||||
|
||||
async def test_digest_includes_high_importance():
|
||||
# 高重要性记忆应该出现在摘要中
|
||||
pass
|
||||
```
|
||||
|
||||
### 9.2 提醒调度测试
|
||||
|
||||
```python
|
||||
async def test_schedule_reminder():
|
||||
reminder = await scheduler.create_reminder(
|
||||
user_id=1,
|
||||
content="检查邮件",
|
||||
trigger_at=datetime.now() + timedelta(hours=1)
|
||||
)
|
||||
assert reminder.status == "pending"
|
||||
|
||||
async def test_get_due_reminders():
|
||||
due = await scheduler.get_due_reminders(user_id=1)
|
||||
assert len(due) >= 0
|
||||
```
|
||||
|
||||
### 9.3 主动告知测试
|
||||
|
||||
```python
|
||||
def test_should_inform_probability():
|
||||
# 高重要性话题 80% 触发
|
||||
count = sum(informer.should_inform(1, "high_importance_topic")
|
||||
for _ in range(100))
|
||||
assert 70 < count < 90 # 允许一点随机波动
|
||||
|
||||
def test_repeat_question_always_informs():
|
||||
# 重复问题 100% 触发
|
||||
assert informer.should_inform(1, "repeat_question") == True
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. 验收标准
|
||||
|
||||
| 标准 | 说明 |
|
||||
|------|------|
|
||||
| 每日摘要生成正常 | 22:00 自动生成 |
|
||||
| 提醒创建正常 | 用户可创建提醒 |
|
||||
| 提醒到期触发 | 定时推送 |
|
||||
| 主动告知概率正确 | 按配置的概率触发 |
|
||||
| 告知消息自然 | 像人说话,不生硬 |
|
||||
| 用户可控制 | 可以关闭主动提醒 |
|
||||
| 单元测试覆盖率 | > 80% |
|
||||
|
||||
---
|
||||
|
||||
## 11. 工作量估算
|
||||
|
||||
| 任务 | 工作量 |
|
||||
|------|--------|
|
||||
| DailyDigestGenerator | 1.5 天 |
|
||||
| ReminderScheduler | 1.5 天 |
|
||||
| ProactiveInformer | 1 天 |
|
||||
| 前端摘要展示 | 0.5 天 |
|
||||
| 前端提醒推送 | 0.5 天 |
|
||||
| 测试 | 1 天 |
|
||||
| **合计** | **6 天** |
|
||||
|
||||
---
|
||||
|
||||
## 12. 与现有系统的关系
|
||||
|
||||
```
|
||||
现有 SchedulerService
|
||||
│
|
||||
├── 凌晨任务重建 → 现有功能
|
||||
├── 每日摘要生成 → M.3 新增
|
||||
└── 提醒检查 → M.3 新增
|
||||
```
|
||||
|
||||
提醒系统独立于现有任务系统,但可以复用调度基础设施。
|
||||
Reference in New Issue
Block a user