Enhance MoviePilotAgent to handle empty agent messages gracefully by providing a default error response, ensuring better user experience. Refactor message processing to streamline event loop execution.

This commit is contained in:
jxxghp
2025-11-19 12:51:08 +08:00
parent b5a6794381
commit 5ae7c10a00
2 changed files with 22 additions and 30 deletions

View File

@@ -221,15 +221,20 @@ class MoviePilotAgent:
agent_message = await self.callback_handler.get_message()
# 发送Agent回复给用户通过原渠道
await self.send_agent_message(agent_message)
if agent_message:
# 发送回复
await self.send_agent_message(agent_message)
# 添加Agent回复到记忆
await self.memory_manager.add_memory(
session_id=self.session_id,
user_id=self.user_id,
role="agent",
content=agent_message
)
# 添加Agent回复到记忆
await self.memory_manager.add_memory(
session_id=self.session_id,
user_id=self.user_id,
role="agent",
content=agent_message
)
else:
agent_message = "很抱歉,智能体出错了,未能生成回复内容。"
await self.send_agent_message(agent_message)
return agent_message

View File

@@ -948,29 +948,16 @@ class MessageChain(ChainBase):
session_id = self._get_or_create_session_id(userid)
# 在事件循环中处理
try:
GlobalVar.CURRENT_EVENT_LOOP.run_until_complete(
agent_manager.process_message(
session_id=session_id,
user_id=str(userid),
message=user_message,
channel=channel.value if channel else None,
source=source,
username=username
)
)
except RuntimeError:
# 如果没有事件循环,创建新的
asyncio.run(
agent_manager.process_message(
session_id=session_id,
user_id=str(userid),
message=user_message,
channel=channel.value if channel else None,
source=source,
username=username
)
GlobalVar.CURRENT_EVENT_LOOP.run_until_complete(
agent_manager.process_message(
session_id=session_id,
user_id=str(userid),
message=user_message,
channel=channel.value if channel else None,
source=source,
username=username
)
)
except Exception as e:
logger.error(f"处理AI智能体消息失败: {e}")