diff --git a/ai_chat.db b/ai_chat.db index 4d26ed5..5ed26fb 100644 Binary files a/ai_chat.db and b/ai_chat.db differ diff --git a/main.py b/main.py index 3d9542b..654e697 100644 --- a/main.py +++ b/main.py @@ -457,6 +457,64 @@ async def handle_matrix_message(action: str, conversation_id: str = None, user_m db.close() return + if action == "generate_ai_response": + # 生成AI回复并同步 + db = SessionLocal() + try: + conv_service = ConversationService(db) + conversation = conv_service.get_conversation(conversation_id) + + if conversation: + # 发送"正在输入"状态 + try: + await matrix_bot.client.room_typing(room_id, typing_state=True) + except: + pass + + # 获取会话历史 + history = conv_service.get_conversation_history(conversation_id, limit=20) + + # 调用AI + ai_response = await ai_service.chat(history) + + # 保存AI回复 + assistant_msg = conv_service.add_message( + conversation_id=conversation.id, + role='assistant', + content=ai_response, + source='matrix' + ) + + # 发送到Matrix + await matrix_bot.send_message(room_id, ai_response) + + # 关闭"正在输入"状态 + try: + await matrix_bot.client.room_typing(room_id, typing_state=False) + except: + pass + + # 同步到网页端WebSocket + await manager.send_to_user(MAIN_USER_ID, { + "type": "assistant_message", + "conversation_id": conversation_id, + "message": { + "id": assistant_msg.id, + "role": "assistant", + "content": ai_response, + "source": "matrix", + "created_at": assistant_msg.created_at.isoformat() + } + }) + + logger.info(f"Matrix AI回复已发送: {ai_response[:50]}") + except Exception as e: + logger.error(f"生成AI回复失败: {e}") + await matrix_bot.send_message(room_id, f"处理消息时出错: {str(e)}") + finally: + db.close() + return + if action == "chat": db = SessionLocal() try: diff --git a/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.blacklisted_devices b/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.blacklisted_devices new file mode 100644 index 0000000..e69de29 diff --git a/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.ignored_devices b/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.ignored_devices new file mode 100644 index 0000000..e69de29 diff --git a/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.trusted_devices b/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.trusted_devices new file mode 100644 index 0000000..fc15253 --- /dev/null +++ b/matrix_store/@tester:matrix.tphai.com_GALBNVJOSG.trusted_devices @@ -0,0 +1,8 @@ +@tester:matrix.tphai.com AJFVRTHLJY matrix-ed25519 4mRjLhM8xbwjkwQP2T/iB3UZJoaADgP6cCVUiB8AtSk +@tester:matrix.tphai.com BDTRXIGPBE matrix-ed25519 gjQNtLEpIEYCjmzUx5ma91G498n4UADh84KUmiReJUM +@tester:matrix.tphai.com GVSFGGYNJL matrix-ed25519 8qV2own4G3m2nki+izFDBOrAxtbGl8RoneM3qUPkThU +@tester:matrix.tphai.com IMEQIQPXTR matrix-ed25519 6Yd4lmhP6jdkkNvh1rIw6TRK331ZUyiAt5G5hPeYqSE +@tester:matrix.tphai.com MIPPYHRVAS matrix-ed25519 s8Ol56sxLCjCOi0Gkv/Kj7LqVMp/8ZmuAJ6QA1rUi7o +@tester:matrix.tphai.com UKJGJYQQLT matrix-ed25519 opC9rhsz1nzrvQqNWMKTF5FxWIGuHTDfixx+q/Y8ea0 +@tester:matrix.tphai.com UPMZGRLESG matrix-ed25519 86c6XPCIYHgesq83C2k5xhXNa0EYMnqTq4jFrTwJX8I +@huangzhuang_bro:matrix.tphai.com BQHGFLQEPR matrix-ed25519 IrEHmvqotfHKLyx1JRJp4RthUVyBT8qQX72qBifRRyQ diff --git a/services/__pycache__/matrix_service.cpython-310.pyc b/services/__pycache__/matrix_service.cpython-310.pyc index 9f15698..3090148 100644 Binary files a/services/__pycache__/matrix_service.cpython-310.pyc and b/services/__pycache__/matrix_service.cpython-310.pyc differ diff --git a/services/matrix_service.py b/services/matrix_service.py index 2cb4143..cefe093 100644 --- a/services/matrix_service.py +++ b/services/matrix_service.py @@ -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]}")