fix: 修复AI重复调用问题,统一消息处理流程

- _process_message 只保存用户消息并通知
- 新增 generate_ai_response action 统一处理AI回复
- 避免重复调用AI和重复发送消息
This commit is contained in:
2026-04-12 00:34:30 +08:00
parent f3636dddd6
commit a07de626ad
7 changed files with 69 additions and 32 deletions

View File

@@ -250,41 +250,12 @@ class MatrixBot:
sender=sender
)
# 发送"正在输入"状态
try:
await self.client.room_typing(room.room_id, typing_state=True)
except Exception as e:
logger.warning(f"发送输入状态失败: {e}")
# 获取AI回复 - 使用字典格式
messages = conv_service.get_conversation_history(conversation.conversation_id)
ai_response = await ai_service.chat(messages)
# 保存AI回复
conv_service.add_message(
conversation_id=conversation.id,
role='assistant',
content=ai_response,
source='matrix'
)
# 发送回复
await self.send_message(room.room_id, ai_response)
# 关闭"正在输入"状态
try:
await self.client.room_typing(room.room_id, typing_state=False)
except Exception as e:
logger.warning(f"关闭输入状态失败: {e}")
# 调用回调(用于网页同步)
# 通知回调处理 AI 回复(不再在这里直接调用 AI
if self.on_message_callback:
await self.on_message_callback(
action="chat",
action="generate_ai_response",
conversation_id=conversation.conversation_id,
user_message=message_text,
room_id=room.room_id,
message_id=user_msg.id
room_id=room.room_id
)
logger.info(f"Matrix回复已发送: {ai_response[:50]}")