mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
同步方法进行包装避免阻塞
This commit is contained in:
@@ -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}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user