同步方法进行包装避免阻塞

This commit is contained in:
Sakura-RanChen
2026-07-08 11:47:04 +08:00
parent 8808b69303
commit 0b8bb728e2
2 changed files with 17 additions and 8 deletions
@@ -1,4 +1,5 @@
import json
import asyncio
import traceback
from ..base import MemoryProviderBase, logger
@@ -84,7 +85,9 @@ class MemoryProvider(MemoryProviderBase):
except (json.JSONDecodeError, KeyError):
pass
results = self.client.search(search_query, filters=filters)
results = await asyncio.to_thread(
self.client.search, search_query, filters=filters
)
if not results or "results" not in results:
return ""
@@ -186,13 +186,19 @@ class MemoryProvider(MemoryProviderBase):
messages.append({"role": message.role, "content": content})
# Add memory using PowerMem SDK
result = self.memory_client.add(
messages=messages,
user_id=self.role_id
)
# Handle both sync and async returns
if asyncio.iscoroutine(result):
result = await result
if self.enable_user_profile:
# UserMemory uses sync add
result = await asyncio.to_thread(
self.memory_client.add,
messages=messages,
user_id=self.role_id
)
else:
# AsyncMemory uses async add
result = await self.memory_client.add(
messages=messages,
user_id=self.role_id
)
logger.bind(tag=TAG).debug(f"Save memory result: {result}")