From 3c272ac859ba45bc94c1d5186ebf3ef6bb8e975d Mon Sep 17 00:00:00 2001 From: "jinli.yl" Date: Fri, 23 Jan 2026 00:11:00 +0800 Subject: [PATCH] feat(memory): add history management tools with dynamic registration --- reme/tool/gallery/__init__.py | 6 ++-- reme/tool/memory/__init__.py | 9 ++++-- reme/tool/memory/add_history.py | 47 +++++++++++++++++++++++++++++ reme/tool/memory/read_history.py | 46 ++++++++++++++++++++++++++++ reme/tool/search/__init__.py | 6 ++-- reme_ai/mem_tool/v4/read_history.py | 38 ----------------------- 6 files changed, 106 insertions(+), 46 deletions(-) create mode 100644 reme/tool/memory/add_history.py create mode 100644 reme/tool/memory/read_history.py delete mode 100644 reme_ai/mem_tool/v4/read_history.py diff --git a/reme/tool/gallery/__init__.py b/reme/tool/gallery/__init__.py index f7bc08e6..30e1d931 100644 --- a/reme/tool/gallery/__init__.py +++ b/reme/tool/gallery/__init__.py @@ -11,6 +11,6 @@ __all__ = [ "ThinkTool", ] -R.op.register()(ExecuteCode) -R.op.register()(ExecuteShell) -R.op.register()(ThinkTool) +for name in __all__: + tool_class = globals()[name] + R.op.register()(tool_class) diff --git a/reme/tool/memory/__init__.py b/reme/tool/memory/__init__.py index 2585e3d4..2b24f341 100644 --- a/reme/tool/memory/__init__.py +++ b/reme/tool/memory/__init__.py @@ -1,15 +1,20 @@ """memory tools""" +from .add_history import AddHistory from .base_memory_tool import BaseMemoryTool +from .read_history import ReadHistory from .read_user_profile import ReadUserProfile from .update_user_profile import UpdateUserProfile from ...core import R __all__ = [ + "AddHistory", "BaseMemoryTool", + "ReadHistory", "ReadUserProfile", "UpdateUserProfile", ] -R.op.register()(ReadUserProfile) -R.op.register()(UpdateUserProfile) +for name in __all__: + tool_class = globals()[name] + R.op.register()(tool_class) diff --git a/reme/tool/memory/add_history.py b/reme/tool/memory/add_history.py new file mode 100644 index 00000000..c61a130a --- /dev/null +++ b/reme/tool/memory/add_history.py @@ -0,0 +1,47 @@ +"""Add history tool""" + +from loguru import logger + +from .base_memory_tool import BaseMemoryTool +from ...core.enumeration import MemoryType +from ...core.schema import ToolCall, MemoryNode, Message +from ...core.utils import format_messages + + +class AddHistory(BaseMemoryTool): + """Tool to add historical dialogue to vector store""" + + def __init__(self, **kwargs): + kwargs["enable_multiple"] = False + super().__init__(**kwargs) + + def _build_tool_call(self) -> ToolCall: + """Build and return the tool call schema""" + return ToolCall( + **{ + "description": "Add original history dialogue.", + "parameters": { + "type": "object", + "properties": {}, + "required": [], + }, + } + ) + + async def execute(self): + """Execute the add history operation""" + self.context.messages = [Message(**m) if isinstance(m, dict) else m for m in self.context.messages] + history_content: str = (self.context.description + "\n" + format_messages(self.context.messages)).strip() + history_node = MemoryNode( + memory_type=MemoryType.HISTORY, + when_to_use=history_content[:100], + content=history_content, + author=self.author, + ) + logger.info(f"Adding history node: {history_node.model_dump_json(indent=2, exclude={'content'})}") + + vector_node = history_node.to_vector_node() + await self.vector_store.delete(vector_node.memory_id) + await self.vector_store.insert([vector_node]) + + return f"Successfully added history: {history_node.memory_id}" diff --git a/reme/tool/memory/read_history.py b/reme/tool/memory/read_history.py new file mode 100644 index 00000000..bf43819e --- /dev/null +++ b/reme/tool/memory/read_history.py @@ -0,0 +1,46 @@ +"""Read history memory tool""" + +from loguru import logger + +from .base_memory_tool import BaseMemoryTool +from ...core.schema import MemoryNode, ToolCall + + +class ReadHistory(BaseMemoryTool): + """Read history memory tool""" + + def __init__(self, **kwargs): + kwargs["enable_multiple"] = False + super().__init__(**kwargs) + + def _build_tool_call(self) -> ToolCall: + """Build and return the tool call schema""" + return ToolCall( + **{ + "description": "Read original history dialogue.", + "parameters": { + "type": "object", + "properties": { + "history_id": { + "type": "string", + "description": "history_id", + }, + }, + "required": ["history_id"], + }, + }, + ) + + async def execute(self): + history_id = self.context.history_id + nodes = await self.vector_store.get(vector_ids=[history_id]) + + if not nodes: + output = f"No history: {history_id}" + logger.warning(output) + return output + + memory = MemoryNode.from_vector_node(nodes[0]) + output = f"Historical Dialogue[{history_id}]\n{memory.content}" + logger.info(f"Successfully read history memory: {history_id}") + return output diff --git a/reme/tool/search/__init__.py b/reme/tool/search/__init__.py index a9e87ef5..6230c7a2 100644 --- a/reme/tool/search/__init__.py +++ b/reme/tool/search/__init__.py @@ -11,6 +11,6 @@ __all__ = [ "TavilySearch", ] -R.op.register()(DashscopeSearch) -R.op.register()(MockSearch) -R.op.register()(TavilySearch) +for name in __all__: + tool_class = globals()[name] + R.op.register()(tool_class) diff --git a/reme_ai/mem_tool/v4/read_history.py b/reme_ai/mem_tool/v4/read_history.py deleted file mode 100644 index 78a90eb4..00000000 --- a/reme_ai/mem_tool/v4/read_history.py +++ /dev/null @@ -1,38 +0,0 @@ -from loguru import logger - -from ..base_memory_tool import BaseMemoryTool -from ...core.schema import MemoryNode - - -class ReadHistory(BaseMemoryTool): - def __init__(self, **kwargs): - kwargs["enable_multiple"] = False - super().__init__(**kwargs) - - def _build_tool_description(self) -> str: - return "Read original history dialogue." - - def _build_parameters(self) -> dict: - return { - "type": "object", - "properties": { - "history_id": { - "type": "string", - "description": "history_id", - }, - }, - "required": ["history_id"], - } - - async def execute(self): - history_id = self.context.get("history_id", "") - nodes = await self.vector_store.get(vector_ids=[history_id]) - - if not nodes: - self.output = f"No history: {history_id}" - logger.warning(self.output) - return - - memory = MemoryNode.from_vector_node(nodes[0]) - self.output = f"### Historical Dialogue\n{memory.content}" - logger.info(f"Successfully read history memory: {history_id}")