Skip to content

Commit 4ee7f58

Browse files
JasonW404Dallas98
andauthored
feat: add active memory tools (StoreMemoryTool, SearchMemoryTool) (#3197)
- Implement StoreMemoryTool for explicit memory storage during agent reasoning - Implement SearchMemoryTool for on-demand memory retrieval during conversations - Integrate tools into agent creation flow (create_agent_info.py) - Register tools in nexent_agent.py and tools/__init__.py - Add MEMORY_OPERATION tool sign for proper categorization - Fix memory_core.py cache key to include event loop ID (prevents cross-loop conflicts) - Add comprehensive test coverage for both tools - Add procedural memory verification documentation Tools follow existing patterns: lazy imports, observer integration, error handling, and respect user memory preferences (agent_share_option, disabled_agent_ids). Co-authored-by: Dallas98 <40557804+Dallas98@users.noreply.github.com>
1 parent ff1b0d1 commit 4ee7f58

11 files changed

Lines changed: 1130 additions & 8 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ data/
6161
sdk/benchmark/.env
6262
/docker/.env.bak
6363

64-
.venv
64+
.venv
65+
66+
.pytest-tmp
67+
doc/mermaid

backend/agents/create_agent_info.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import threading
1+
import json
2+
import threading
23
import logging
34
from typing import List, Optional
45
from urllib.parse import urljoin
@@ -383,6 +384,77 @@ async def create_agent_config(
383384
# Bubble up to streaming layer so it can emit <MEM_FAILED> and fall back
384385
raise Exception(f"Failed to retrieve memory list: {e}")
385386

387+
# Append active memory tools if memory is enabled
388+
if memory_context.user_config.memory_switch and memory_context.memory_config:
389+
try:
390+
memory_metadata = {
391+
"memory_config": memory_context.memory_config,
392+
"memory_user_config": memory_context.user_config,
393+
"tenant_id": memory_context.tenant_id,
394+
"user_id": memory_context.user_id,
395+
"agent_id": memory_context.agent_id,
396+
}
397+
398+
store_tool_config = ToolConfig(
399+
class_name="StoreMemoryTool",
400+
name="store_memory",
401+
description=(
402+
"Save important information to long-term memory for future recall. "
403+
"Use this when the user shares personal preferences, facts about themselves, "
404+
"project context, or instructions that should persist across conversations. "
405+
"Do NOT store transient information like temporary calculations, information "
406+
"already in the knowledge base, or data the user explicitly says to forget."
407+
),
408+
inputs=json.dumps({
409+
"content": {
410+
"type": "string",
411+
"description": "The information to remember",
412+
"description_zh": "需要记住的信息"
413+
}
414+
}, ensure_ascii=False),
415+
output_type="string",
416+
params={},
417+
source="local",
418+
usage=None,
419+
metadata=memory_metadata,
420+
)
421+
tool_list.append(store_tool_config)
422+
423+
search_tool_config = ToolConfig(
424+
class_name="SearchMemoryTool",
425+
name="search_memory",
426+
description=(
427+
"Search long-term memory for relevant information from previous interactions. "
428+
"Use this when you need context about the user's preferences, past decisions, "
429+
"or previously discussed topics that aren't in the current conversation. "
430+
"The system already provides some memory context automatically -- use this tool "
431+
"when you need to search for specific information not already available."
432+
),
433+
inputs=json.dumps({
434+
"query": {
435+
"type": "string",
436+
"description": "Natural language query describing what to search for",
437+
"description_zh": "描述要搜索内容的自然语言查询"
438+
},
439+
"top_k": {
440+
"type": "integer",
441+
"description": "Maximum number of results to return",
442+
"description_zh": "返回结果的最大数量",
443+
"default": 5,
444+
"nullable": True
445+
}
446+
}, ensure_ascii=False),
447+
output_type="string",
448+
params={},
449+
source="local",
450+
usage=None,
451+
metadata=memory_metadata,
452+
)
453+
tool_list.append(search_tool_config)
454+
logger.debug("Active memory tools appended to agent tool list")
455+
except Exception as e:
456+
logger.warning(f"Failed to append active memory tools: {e}")
457+
386458
# Build knowledge base summary
387459
knowledge_base_summary = ""
388460
try:

0 commit comments

Comments
 (0)