90 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

# 记忆路由Agent后端
仓库路径: /guanghu-self-hosted/memory-router/
优先级: P0
分支名: feat/py-memory-router
创建时间: 2026年4月25日 12:49
审核结果: ✅ 审核通过 · 霜砚AG-SY-WEB-001 · 2026-04-25T13:00 CST
▸ 仓库: qinfendebingshuo/guanghulab
▸ 分支: feat/py-memory-router @ 2bd1503
▸ 路径: /guanghu-self-hosted/memory-router/
【文件清单 9/9 ✅】
1. http://config.py (3296B) ✅ Pydantic配置·环境变量·pgvector参数·PersonaDB连接
2. memory_schema.sql (2540B) ✅ PostgreSQL DDL·pgvector扩展·IVFFlat索引·accessed_at触发器
3. memory_http://store.py (14530B) ✅ 双后端(PgMemoryStore async + SqliteMemoryStore sync)·write_short_term/write_long_term/search_semantic/get_permanent
4. memory_http://compressor.py (9146B) ✅ HLDP母语压缩·话题/决策/情绪提取·SessionSummary结构化输出
5. memory_http://router.py (11025B) ✅ 5条路由规则(NONE/HOT_ONLY/HOT_AND_WARM/SEMANTIC_SEARCH/PERMANENT/FULL)·async+sync上下文组装·logger追溯
6. memory_http://api.py (7697B) ✅ FastAPI 5端点(/route /memories /memories/search /memories/permanent/{persona_id} /compress)·lifespan管理
7. test_memory_http://router.py (8129B) ✅ 15条测试·SQLite fallback·覆盖路由/存储/压缩/全流程
8. requirements.txt (81B) ✅ fastapi+asyncpg+pydantic+uvicorn+pgvector
9. http://README.md (3474B) ✅ 项目说明·架构图·快速开始
【约束验证】
✅ Python 3.10+ · FastAPI · UTF-8
✅ 依赖完整: fastapi+asyncpg+pydantic+pgvector
✅ PostgreSQL 15+ · pgvector · 与PersonaDB同一实例
✅ 测试包含test_memory_http://router.py · SQLite fallback
✅ 记忆压缩输出HLDP母语格式
✅ http://路由决策有logger.info日志可追溯
✅ 隔离: 仅/guanghu-self-hosted/memory-router/
✅ 禁触: 未触碰仓库其他目录
✅ 前缀: PY-A04
【备注】
· /memories/search 端点实现为POST(而非工单中写的GET)因发送embedding向量体用POST更合理属于合理优化
· memory_type枚举hot/warm/cold/permanent完整实现
· 写回机制(短期→热/长期→冷/永久)接口完备
开发内容: 开发记忆路由Agent后端Memory Router· 参考 HLDP-ARCH-001 中 L3 记忆路由Agent设计。
1. 在 /guanghu-self-hosted/memory-router/ 下创建:
- memory_http://router.py: 记忆路由核心类
· route_query(user_input, session_id, persona_id) → 判断需要什么上下文
· assemble_context(query_result) → 组装上下文片段发给模型
· 分层路由逻辑: 热记忆(最近5轮·零延迟) / 温记忆(本次对话早期·HLDP压缩摘要) / 冷记忆(PersonaDB语义检索·pgvector) / 永久记忆(身份/Layer Zero/思维路径)
- memory_http://store.py: 记忆存储层
· write_short_term(session_id, summary) → 短期记忆(滚动窗口)
· write_long_term(persona_id, content, memory_type) → 长期记忆(持久存储)
· search_semantic(query, persona_id, top_k) → pgvector语义检索
· get_permanent(persona_id) → 获取永久记忆(身份/思维路径/价值锚点)
- memory_http://compressor.py: 记忆压缩器
· compress_to_hldp(conversation_turns) → 将对话压缩为HLDP母语摘要
· summarize_session(session_id) → 生成会话摘要
- memory_schema.sql: 记忆表建表SQLmemories表: memory_id, persona_id, session_id, memory_type[hot/warm/cold/permanent], content, embedding vector(1536), created_at, accessed_at, access_count, status
- memory_http://api.py: FastAPI路由
· POST /route → 路由查询·返回组装好的上下文
· POST /memories → 写入记忆
· GET /memories/search → 语义检索
· GET /memories/permanent/{persona_id} → 获取永久记忆
· POST /compress → 压缩对话为HLDP摘要
- http://config.py: 配置PersonaDB连接·pgvector参数·热记忆窗口大小·压缩阈值
- test_memory_http://router.py: 测试用例(路由判断·语义检索·压缩·写回·完整流程)
3. memory_type枚举: hot/warm/cold/permanent
4. 路由决策逻辑: 用户说话→判断需要什么上下文→按需检索→组装→返回
5. 写回机制: 对话后自动写回(本轮摘要→短期·重要决策→长期·情绪变化→关系层)
状态: 已完成
约束: 编码: Python 3.10+ · FastAPI · UTF-8
依赖: fastapi+asyncpg+pydantic+pgvector
SQL: PostgreSQL 15+ · pgvector扩展 · 与PersonaDB同一数据库实例
测试: 必须包含test_memory_http://router.py · 可用SQLite做本地测试fallback(语义检索用mock)
禁触: 不要修改仓库其他目录的任何文件
注意: 记忆压缩输出HLDP母语格式 · 路由决策必须有日志可追溯
参考: HLDP-ARCH-001 L3层设计 · 工具回执系统(L2)的代码风格
编号: PY-A04-20260425-003
自检结果: 9/9 ✅ 全部通过
- http://config.py (3296B): ✅ Pydantic配置·环境变量·pgvector参数
- memory_schema.sql (2540B): ✅ PostgreSQL DDL·pgvector索引·触发器
- memory_http://store.py (14530B): ✅ 双后端(Pg async + SQLite sync)·写入/检索/永久记忆
- memory_http://compressor.py (9146B): ✅ HLDP母语压缩·话题/决策/情绪提取
- memory_http://router.py (11025B): ✅ 5条路由规则·async/sync上下文组装
- memory_http://api.py (7697B): ✅ FastAPI 5端点·lifespan管理
- test_memory_http://router.py (8129B): ✅ 15条测试·SQLite fallback
- requirements.txt (81B): ✅ 依赖完整
- http://README.md (3474B): ✅ 项目说明
语法: ✅ import交叉引用正确
隔离: ✅ 仅/guanghu-self-hosted/memory-router/
禁触: ✅ 未触碰仓库其他目录
前缀: ✅ PY-A04
分支: feat/py-memory-router @ 2bd1503
负责Agent: 培园A04
阶段编号: Phase-0-006