agentao

Memory Management Update - 2024-12-28

概述

为 Agentao 添加完整的 memory 管理功能,包括搜索、删除、清空和标签过滤。

新功能

1. LLM 工具(4个新工具)

SearchMemoryTool

DeleteMemoryTool

ClearMemoryTool

FilterMemoryByTagTool

2. CLI 命令增强

扩展 /memory 命令支持子命令:

# 基础命令
/memory              # 列出所有记忆
/memory list         # 同上

# 搜索和过滤
/memory search <query>    # 搜索记忆
/memory tag <tag>         # 按标签过滤

# 管理
/memory delete <key>      # 删除指定记忆
/memory clear             # 清空所有(需确认)

3. SaveMemoryTool 扩展

添加新的辅助方法:

def filter_by_tag(self, tag: str) -> list
    """按标签过滤记忆"""

def delete_memory(self, key: str) -> bool
    """删除指定记忆"""

def clear_all_memories(self) -> int
    """清空所有记忆,返回删除数量"""

原有方法保持不变:

文件变更

修改的文件

  1. agentao/tools/memory.py
    • 添加 4 个辅助方法到 SaveMemoryTool
    • 添加 4 个新工具类
    • 约 +250 行代码
  2. agentao/tools/__init__.py
    • 导出新的 memory 工具类
    • 更新 __all__ 列表
  3. agentao/agent.py
    • 导入新工具
    • _register_tools() 中注册新工具
  4. agentao/cli/commands.py
    • 重写 show_memories() 方法支持子命令
    • 更新 /memory 命令处理逻辑
    • 更新欢迎消息和帮助文档
  5. CLAUDE.md
    • 更新 Memory System 部分
    • 添加新工具和命令的文档

新增的文件

  1. tests/test_memory_management.py
    • 完整的测试套件
    • 测试所有 5 个工具的功能
    • 所有测试通过 ✅
  2. docs/features/memory-management.md
    • 完整的用户文档
    • 包含使用示例、最佳实践、API 参考
    • 故障排除指南
  3. docs/updates/2024-12-28-memory-management.md
    • 本更新日志

测试结果

Test 1: Saving memories...
✓ Saved memory: project_name
✓ Saved memory: user_preference
✓ Saved memory: reminder

Test 2: Searching memories...
✓ Found memories matching 'project'

Test 3: Filtering by tag...
✓ Found memories with tag 'python'

Test 4: Deleting specific memory...
✓ Successfully deleted memory: reminder
✓ Deletion verified

Test 5: Clearing all memories...
✓ Successfully cleared 2 memory(ies)
✓ All memories cleared

✅ All tests passed!

使用示例

AI 自动管理

用户:记住我喜欢用 Python 3.11+,标签为 preference
AI:[调用 save_memory]

用户:我之前保存过什么关于 python 的?
AI:[调用 search_memory] 找到 1 条记忆...

用户:删除 old_config 这条记忆
AI:[调用 delete_memory] 已删除

用户手动管理

# 查看所有
/memory

# 搜索
/memory search config

# 按标签过滤
/memory tag important

# 删除
/memory delete old_setting

# 清空(会要求确认)
/memory clear

架构设计

双层接口

  1. LLM 工具层: AI 可以自动调用管理 memory
  2. CLI 命令层: 用户可以直接控制 memory

数据流

用户输入 → CLI 解析 → Agent → LLM → 工具调用
                ↓                        ↓
            show_memories()         Memory Tools
                ↓                        ↓
            直接操作 ←──────────────→ SaveMemoryTool
                                         ↓
                                .agentao_memory.json

确认机制

向后兼容性

完全兼容

性能影响

未来改进

可能的优化方向:

  1. 索引系统: 为大量记忆添加索引
  2. 分页显示: 记忆很多时分页显示
  3. 导出/导入: 支持导出为 JSON/CSV
  4. 备份机制: 自动备份防止误删
  5. 并发安全: 文件锁支持多进程

相关文档

总结

此次更新为 Agentao 带来了完整的 memory 管理能力,用户可以:

✅ 通过 CLI 命令直接管理 memory ✅ 让 AI 自动管理 memory ✅ 搜索和过滤已保存的信息 ✅ 安全地删除单个或所有 memory ✅ 使用标签组织和查找 memory

所有功能经过测试,保持向后兼容,文档完善。