You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.9 KiB
80 lines
2.9 KiB
|
1 month ago
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
mem0 Commands for OpenClaw
|
||
|
|
"""
|
||
|
|
|
||
|
|
from .mem0_client import Mem0Client
|
||
|
|
|
||
|
|
mem0 = Mem0Client()
|
||
|
|
|
||
|
|
async def handle_memory_command(args: str, context: dict) -> str:
|
||
|
|
"""处理 /memory 命令"""
|
||
|
|
parts = args.split(' ', 2)
|
||
|
|
action = parts[0] if parts else 'help'
|
||
|
|
|
||
|
|
user_id = context.get('user_id', 'default')
|
||
|
|
agent_id = context.get('agent_id', 'main')
|
||
|
|
|
||
|
|
if action == 'add':
|
||
|
|
content = parts[1] if len(parts) > 1 else ''
|
||
|
|
return await handle_add_memory(content, user_id, agent_id)
|
||
|
|
elif action == 'search':
|
||
|
|
query = parts[1] if len(parts) > 1 else ''
|
||
|
|
return await handle_search_memory(query, user_id, agent_id)
|
||
|
|
elif action == 'list':
|
||
|
|
return await handle_list_memories(user_id, agent_id)
|
||
|
|
elif action == 'delete':
|
||
|
|
memory_id = parts[1] if len(parts) > 1 else ''
|
||
|
|
return await handle_delete_memory(memory_id, user_id, agent_id)
|
||
|
|
elif action == 'sync':
|
||
|
|
return await handle_sync_memory(user_id, agent_id)
|
||
|
|
elif action == 'status':
|
||
|
|
return handle_status(user_id, agent_id)
|
||
|
|
else:
|
||
|
|
return get_help_text()
|
||
|
|
|
||
|
|
async def handle_add_memory(content: str, user_id: str, agent_id: str) -> str:
|
||
|
|
if not content:
|
||
|
|
return "❌ 请提供记忆内容"
|
||
|
|
result = mem0.add([{"role": "user", "content": content}], user_id=user_id, agent_id=agent_id)
|
||
|
|
if 'error' in result:
|
||
|
|
return f"❌ 添加失败:{result['error']}"
|
||
|
|
return f"✅ 记忆已添加:{content[:100]}..."
|
||
|
|
|
||
|
|
async def handle_search_memory(query: str, user_id: str, agent_id: str) -> str:
|
||
|
|
if not query:
|
||
|
|
return "❌ 请提供关键词"
|
||
|
|
results = mem0.search(query, user_id=user_id, agent_id=agent_id, limit=5)
|
||
|
|
if not results:
|
||
|
|
return f"🔍 未找到相关记忆"
|
||
|
|
response = f"🔍 找到 {len(results)} 条记忆:\n\n"
|
||
|
|
for i, mem in enumerate(results, 1):
|
||
|
|
response += f"{i}. {mem.get('memory', 'N/A')}\n"
|
||
|
|
return response
|
||
|
|
|
||
|
|
async def handle_list_memories(user_id: str, agent_id: str) -> str:
|
||
|
|
results = mem0.get_all(user_id=user_id, agent_id=agent_id)
|
||
|
|
if not results:
|
||
|
|
return "📭 暂无记忆"
|
||
|
|
return f"📋 共 {len(results)} 条记忆"
|
||
|
|
|
||
|
|
async def handle_delete_memory(memory_id: str, user_id: str, agent_id: str) -> str:
|
||
|
|
if not memory_id:
|
||
|
|
return "❌ 请提供 ID"
|
||
|
|
success = mem0.delete(memory_id, user_id=user_id, agent_id=agent_id)
|
||
|
|
return "✅ 已删除" if success else "❌ 删除失败"
|
||
|
|
|
||
|
|
async def handle_sync_memory(user_id: str, agent_id: str) -> str:
|
||
|
|
return "🔄 同步功能开发中"
|
||
|
|
|
||
|
|
def handle_status(user_id: str, agent_id: str) -> str:
|
||
|
|
status = mem0.get_status()
|
||
|
|
response = "📊 mem0 状态:\n"
|
||
|
|
response += f"本地:{'✅' if status['local_initialized'] else '❌'}\n"
|
||
|
|
response += f"共享:{'✅' if status['master_initialized'] else '❌'}\n"
|
||
|
|
return response
|
||
|
|
|
||
|
|
def get_help_text() -> str:
|
||
|
|
return """📖 用法:/memory <命令>
|
||
|
|
命令:add, search, list, delete, sync, status"""
|