fix: useasyncio.create_task to avoid block
This commit is contained in:
parent
740294fd74
commit
98a762452a
@ -21,7 +21,7 @@ from nanobot.agent.tools.spawn import SpawnTool
|
|||||||
from nanobot.agent.tools.cron import CronTool
|
from nanobot.agent.tools.cron import CronTool
|
||||||
from nanobot.agent.memory import MemoryStore
|
from nanobot.agent.memory import MemoryStore
|
||||||
from nanobot.agent.subagent import SubagentManager
|
from nanobot.agent.subagent import SubagentManager
|
||||||
from nanobot.session.manager import SessionManager
|
from nanobot.session.manager import Session, SessionManager
|
||||||
|
|
||||||
|
|
||||||
class AgentLoop:
|
class AgentLoop:
|
||||||
@ -243,20 +243,31 @@ class AgentLoop:
|
|||||||
# Handle slash commands
|
# Handle slash commands
|
||||||
cmd = msg.content.strip().lower()
|
cmd = msg.content.strip().lower()
|
||||||
if cmd == "/new":
|
if cmd == "/new":
|
||||||
await self._consolidate_memory(session, archive_all=True)
|
# Capture messages before clearing (avoid race condition with background task)
|
||||||
|
messages_to_archive = session.messages.copy()
|
||||||
session.clear()
|
session.clear()
|
||||||
self.sessions.save(session)
|
self.sessions.save(session)
|
||||||
# Clear cache to force reload from disk on next request
|
# Clear cache to force reload from disk on next request
|
||||||
self.sessions._cache.pop(session.key, None)
|
self.sessions._cache.pop(session.key, None)
|
||||||
|
|
||||||
|
# Consolidate in background (non-blocking)
|
||||||
|
async def _consolidate_and_cleanup():
|
||||||
|
# Create a temporary session with archived messages
|
||||||
|
temp_session = Session(key=session.key)
|
||||||
|
temp_session.messages = messages_to_archive
|
||||||
|
await self._consolidate_memory(temp_session, archive_all=True)
|
||||||
|
|
||||||
|
asyncio.create_task(_consolidate_and_cleanup())
|
||||||
return OutboundMessage(channel=msg.channel, chat_id=msg.chat_id,
|
return OutboundMessage(channel=msg.channel, chat_id=msg.chat_id,
|
||||||
content="New session started. Memory consolidated.")
|
content="New session started. Memory consolidation in progress.")
|
||||||
if cmd == "/help":
|
if cmd == "/help":
|
||||||
return OutboundMessage(channel=msg.channel, chat_id=msg.chat_id,
|
return OutboundMessage(channel=msg.channel, chat_id=msg.chat_id,
|
||||||
content="🐈 nanobot commands:\n/new — Start a new conversation\n/help — Show available commands")
|
content="🐈 nanobot commands:\n/new — Start a new conversation\n/help — Show available commands")
|
||||||
|
|
||||||
# Consolidate memory before processing if session is too large
|
# Consolidate memory before processing if session is too large
|
||||||
|
# Run in background to avoid blocking main conversation
|
||||||
if len(session.messages) > self.memory_window:
|
if len(session.messages) > self.memory_window:
|
||||||
await self._consolidate_memory(session)
|
asyncio.create_task(self._consolidate_memory(session))
|
||||||
|
|
||||||
# Update tool contexts
|
# Update tool contexts
|
||||||
self._set_tool_context(msg.channel, msg.chat_id)
|
self._set_tool_context(msg.channel, msg.chat_id)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user