feat(memory-graph): Step 1 - 集成记忆工具到插件系统

完成记忆系统工具的插件化集成:

1. 创建记忆工具适配器 (memory_plugin_tools.py)
   - CreateMemoryTool: 创建新记忆
   - LinkMemoriesTool: 关联两条记忆
   - SearchMemoriesTool: 搜索相关记忆
   - 适配 BaseTool 接口,支持 LLM 调用

2. 创建全局 MemoryManager 单例 (manager_singleton.py)
   - initialize_memory_manager(): 初始化全局实例
   - get_memory_manager(): 获取单例实例
   - shutdown_memory_manager(): 关闭管理器
   - 线程安全的单例模式

3. 创建记忆系统插件 (plugins/memory_graph_plugin/)
   - MemoryGraphPlugin: 插件主类
   - 自动注册3个记忆工具到系统
   - on_plugin_loaded(): 初始化 MemoryManager
   - on_unload(): 清理资源

4. 修复类型问题
   - ToolParamType.OBJECT  STRING (JSON格式)
   - ToolParamType.NUMBER  FLOAT
   - attributes 参数支持 JSON 字符串解析
   - 修复 min_importance None 比较错误

5. 添加集成测试 (test_plugin_integration.py)
   - 测试工具导入和定义
   - 测试 MemoryManager 初始化
   - 测试工具执行(创建、搜索记忆)
   - 测试单例模式

测试结果:  所有测试通过
LLM 现在可以通过工具调用主动管理记忆!
This commit is contained in:
Windpicker-owo
2025-11-05 18:42:27 +08:00
parent 64b8636e9e
commit fc71aad817
6 changed files with 543 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
"""
记忆系统插件
集成记忆管理功能到 Bot 系统中
"""
from src.plugin_system.base.plugin_metadata import PluginMetadata
__plugin_meta__ = PluginMetadata(
name="记忆图系统 (Memory Graph)",
description="基于图的记忆管理系统,支持记忆创建、关联和检索",
usage="LLM 可以通过工具调用创建和管理记忆,系统自动在回复时检索相关记忆",
version="0.1.0",
author="MoFox-Studio",
license="GPL-v3.0",
repository_url="https://github.com/MoFox-Studio",
keywords=["记忆", "知识图谱", "RAG", "长期记忆"],
categories=["AI", "Knowledge Management"],
extra={"is_built_in": False, "plugin_type": "memory"},
)

View File

@@ -0,0 +1,79 @@
"""
记忆系统插件主类
"""
from typing import ClassVar
from src.common.logger import get_logger
from src.plugin_system import BasePlugin, register_plugin
from src.plugin_system.base.component_types import ComponentInfo, ToolInfo
logger = get_logger("memory_graph_plugin")
@register_plugin
class MemoryGraphPlugin(BasePlugin):
"""记忆图系统插件"""
plugin_name = "memory_graph_plugin"
enable_plugin = True
dependencies: ClassVar = []
python_dependencies: ClassVar = []
config_file_name = "config.toml"
config_schema: ClassVar = {}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
logger.info(f"{self.log_prefix} 插件已加载")
def get_plugin_components(self):
"""返回插件组件列表"""
from src.memory_graph.plugin_tools.memory_plugin_tools import (
CreateMemoryTool,
LinkMemoriesTool,
SearchMemoriesTool,
)
components = []
# 添加工具组件
for tool_class in [CreateMemoryTool, LinkMemoriesTool, SearchMemoriesTool]:
tool_info = tool_class.get_tool_info()
components.append((tool_info, tool_class))
return components
async def on_plugin_loaded(self):
"""插件加载后的回调"""
try:
from src.memory_graph.manager_singleton import initialize_memory_manager
logger.info(f"{self.log_prefix} 正在初始化记忆系统...")
await initialize_memory_manager()
logger.info(f"{self.log_prefix} ✅ 记忆系统初始化成功")
except Exception as e:
logger.error(f"{self.log_prefix} 初始化记忆系统失败: {e}", exc_info=True)
raise
def on_unload(self):
"""插件卸载时的回调"""
try:
import asyncio
from src.memory_graph.manager_singleton import shutdown_memory_manager
logger.info(f"{self.log_prefix} 正在关闭记忆系统...")
# 在事件循环中运行异步关闭
loop = asyncio.get_event_loop()
if loop.is_running():
# 如果循环正在运行,创建任务
asyncio.create_task(shutdown_memory_manager())
else:
# 如果循环未运行,直接运行
loop.run_until_complete(shutdown_memory_manager())
logger.info(f"{self.log_prefix} ✅ 记忆系统已关闭")
except Exception as e:
logger.error(f"{self.log_prefix} 关闭记忆系统时出错: {e}", exc_info=True)