Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d018342b45 | |||
| ac7a329e53 | |||
| 585d4ce39c | |||
| 8742d5932f | |||
| d72534c0c3 | |||
| 6625dda349 | |||
| 0d25cdc344 | |||
| 65297d7321 | |||
| d3236413f3 | |||
| b228283133 |
BIN
ai_chat.db
BIN
ai_chat.db
Binary file not shown.
@@ -17,7 +17,7 @@ configs = [
|
||||
{'key': 'ai_model', 'value': 'auto', 'description': 'AI模型名称'},
|
||||
|
||||
# 配置Matrix Bot
|
||||
{'key': 'matrix_homeserver', 'value': 'https://matrix.tphai.com', 'description': 'Matrix服务器地址'},
|
||||
{'key': 'matrix_homeserver', 'value': 'http://matrix.tphai.com', 'description': 'Matrix服务器地址'},
|
||||
{'key': 'matrix_username', 'value': '@tester:matrix.tphai.com', 'description': 'Matrix Bot用户名'},
|
||||
{'key': 'matrix_access_token', 'value': 'syt_dGVzdGVy_eMwWfezCXSyBgHzvkmly_4dWFtM', 'description': 'Matrix Bot Access Token'},
|
||||
]
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py:445: DeprecationWarning:
|
||||
on_event is deprecated, use lifespan event handlers instead.
|
||||
|
||||
Read more about it in the
|
||||
[FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
|
||||
|
||||
@app.on_event("startup")
|
||||
/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py:470: DeprecationWarning:
|
||||
on_event is deprecated, use lifespan event handlers instead.
|
||||
|
||||
Read more about it in the
|
||||
[FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
|
||||
|
||||
@app.on_event("shutdown")
|
||||
INFO: Started server process [1418843]
|
||||
INFO: Waiting for application startup.
|
||||
INFO:__main__:数据库初始化完成
|
||||
INFO:services.ai_service:AI配置已更新: api_base=http://192.168.2.17:19007/v1, model=auto, use_mock=False
|
||||
INFO:__main__:AI配置已加载: http://192.168.2.17:19007/v1, model=auto
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||
WARNING:nio.clieWARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
async_client:Timed out, sleeping for 25s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 51s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||
154
main.py
154
main.py
@@ -83,15 +83,14 @@ async def admin(request: Request):
|
||||
|
||||
# ==================== API路由 ====================
|
||||
|
||||
# 固定主用户ID(与Matrix AI用户关联)
|
||||
MAIN_USER_ID = "main_user"
|
||||
|
||||
@app.get("/api/conversations")
|
||||
async def get_conversations(user_id: str = None, db: Session = Depends(get_db)):
|
||||
"""获取会话列表"""
|
||||
if not user_id:
|
||||
# 临时用户ID(实际应用中应该从session获取)
|
||||
user_id = "web_anonymous"
|
||||
|
||||
async def get_conversations(db: Session = Depends(get_db)):
|
||||
"""获取会话列表(使用固定主用户)"""
|
||||
conv_service = ConversationService(db)
|
||||
user = conv_service.get_or_create_user(user_id, user_type='web')
|
||||
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
|
||||
conversations = conv_service.get_user_conversations(user.id)
|
||||
|
||||
return {
|
||||
@@ -106,15 +105,30 @@ async def get_conversations(user_id: str = None, db: Session = Depends(get_db)):
|
||||
]
|
||||
}
|
||||
|
||||
@app.get("/api/conversations/latest")
|
||||
async def get_latest_conversation(db: Session = Depends(get_db)):
|
||||
"""获取最新会话(用于Matrix同步)"""
|
||||
conv_service = ConversationService(db)
|
||||
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
|
||||
conversations = conv_service.get_user_conversations(user.id)
|
||||
|
||||
if conversations:
|
||||
latest = conversations[0] # 已按更新时间倒序
|
||||
return {
|
||||
"conversation": {
|
||||
"id": latest.conversation_id,
|
||||
"title": latest.title or "新对话",
|
||||
"updated_at": latest.updated_at.isoformat()
|
||||
}
|
||||
}
|
||||
return {"conversation": None}
|
||||
|
||||
|
||||
@app.post("/api/conversations")
|
||||
async def create_conversation(user_id: str = None, db: Session = Depends(get_db)):
|
||||
"""创建新会话"""
|
||||
if not user_id:
|
||||
user_id = "web_anonymous"
|
||||
|
||||
async def create_conversation(db: Session = Depends(get_db)):
|
||||
"""创建新会话(使用固定主用户)"""
|
||||
conv_service = ConversationService(db)
|
||||
user = conv_service.get_or_create_user(user_id, user_type='web')
|
||||
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
|
||||
conversation = conv_service.create_conversation(user.id)
|
||||
|
||||
return {
|
||||
@@ -165,10 +179,12 @@ async def delete_conversation(conversation_id: str, db: Session = Depends(get_db
|
||||
|
||||
@app.websocket("/ws/{user_id}")
|
||||
async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = Depends(get_db)):
|
||||
"""WebSocket连接 - 实时对话"""
|
||||
await manager.connect(websocket, user_id)
|
||||
"""WebSocket连接 - 实时对话(所有连接使用主用户)"""
|
||||
# 统一使用主用户ID
|
||||
actual_user_id = MAIN_USER_ID
|
||||
await manager.connect(websocket, actual_user_id)
|
||||
conv_service = ConversationService(db)
|
||||
user = conv_service.get_or_create_user(user_id, user_type='web')
|
||||
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
|
||||
|
||||
current_conversation_id = None
|
||||
|
||||
@@ -227,8 +243,8 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = D
|
||||
source='web'
|
||||
)
|
||||
|
||||
# 通知用户消息已收到
|
||||
await manager.send_to_user(user_id, {
|
||||
# 广播用户消息(同步到所有客户端)
|
||||
await manager.send_to_user(MAIN_USER_ID, {
|
||||
"type": "user_message",
|
||||
"conversation_id": conversation_id,
|
||||
"message": {
|
||||
@@ -240,6 +256,10 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = D
|
||||
}
|
||||
})
|
||||
|
||||
# 同步到Matrix(如果有房间)
|
||||
if matrix_bot.is_running and matrix_bot.last_room_id:
|
||||
await matrix_bot.send_message(matrix_bot.last_room_id, f"[网页] {message}")
|
||||
|
||||
# 获取对话历史
|
||||
history = conv_service.get_conversation_history(conversation_id, limit=20)
|
||||
|
||||
@@ -255,18 +275,23 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = D
|
||||
source='web'
|
||||
)
|
||||
|
||||
# 发送AI回复
|
||||
await manager.send_to_user(user_id, {
|
||||
# 广播AI回复(同步到所有客户端)
|
||||
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": "web",
|
||||
"created_at": assistant_msg.created_at.isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
# 同步AI回复到Matrix
|
||||
if matrix_bot.is_running and matrix_bot.last_room_id:
|
||||
await matrix_bot.send_message(matrix_bot.last_room_id, ai_response)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"AI调用失败: {e}")
|
||||
await websocket.send_json({
|
||||
@@ -397,47 +422,58 @@ async def update_config(data: dict, db: Session = Depends(get_db)):
|
||||
|
||||
# ==================== Matrix消息处理回调 ====================
|
||||
|
||||
async def handle_matrix_message(conversation_id: str, user_message: str, user_id: str, room_id: str):
|
||||
async def handle_matrix_message(action: str, conversation_id: str = None, user_message: str = None, room_id: str = None, message_id: int = None):
|
||||
"""处理从Matrix收到的消息"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
conv_service = ConversationService(db)
|
||||
|
||||
# 获取会话历史
|
||||
history = conv_service.get_conversation_history(conversation_id, limit=20)
|
||||
|
||||
# 调用AI
|
||||
ai_response = await ai_service.chat(history)
|
||||
|
||||
# 保存AI回复
|
||||
conversation = conv_service.get_conversation(conversation_id)
|
||||
if conversation:
|
||||
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)
|
||||
|
||||
# 同时推送到网页端
|
||||
await manager.send_to_user(user_id, {
|
||||
"type": "assistant_message",
|
||||
"conversation_id": conversation_id,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": ai_response,
|
||||
"source": "matrix",
|
||||
"created_at": datetime.utcnow().isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理Matrix消息失败: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
if action == "new_conversation":
|
||||
# 创建新会话 - 通知WebSocket客户端
|
||||
await manager.send_to_user(MAIN_USER_ID, {
|
||||
"type": "new_conversation",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
return
|
||||
|
||||
if action == "chat":
|
||||
db = SessionLocal()
|
||||
try:
|
||||
conv_service = ConversationService(db)
|
||||
|
||||
# 获取会话历史
|
||||
history = conv_service.get_conversation_history(conversation_id, limit=20)
|
||||
|
||||
# 调用AI
|
||||
ai_response = await ai_service.chat(history)
|
||||
|
||||
# 保存AI回复
|
||||
conversation = conv_service.get_conversation(conversation_id)
|
||||
if conversation:
|
||||
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)
|
||||
|
||||
# 同步到网页端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()
|
||||
}
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"处理Matrix消息失败: {e}")
|
||||
await matrix_bot.send_message(room_id, f"处理消息时出错: {str(e)}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ==================== 启动和关闭 ====================
|
||||
|
||||
Binary file not shown.
@@ -1,208 +1,351 @@
|
||||
"""
|
||||
Matrix Bot 服务 - 处理Matrix消息收发
|
||||
Matrix Bot 服务 - 使用 nio 库处理消息收发(支持加密)
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Optional, Callable
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessageText, LoginResponse, SyncResponse
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional, Callable
|
||||
from nio import AsyncClient, RoomMessageText, MegolmEvent
|
||||
|
||||
from models import SessionLocal, User, Conversation, Message, MatrixRoomMapping, SystemConfig
|
||||
from models import SessionLocal, User, Conversation, Message, SystemConfig
|
||||
from services.conversation_service import ConversationService
|
||||
from services import ai_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MAIN_USER_ID = "main_user"
|
||||
STORE_PATH = "/home/xian/.openclaw/workspace-coder/works/ai-chat/matrix_store"
|
||||
|
||||
|
||||
class MatrixBot:
|
||||
def __init__(self):
|
||||
self.client: Optional[AsyncClient] = None
|
||||
self.homeserver: str = ""
|
||||
self.username: str = ""
|
||||
self.user_id: str = ""
|
||||
self.password: str = ""
|
||||
self.access_token: str = ""
|
||||
self.is_running: bool = False
|
||||
self.on_message_callback: Optional[Callable] = None
|
||||
self.last_room_id: str = ""
|
||||
|
||||
async def init_from_config(self):
|
||||
"""从数据库配置初始化"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
configs = {c.key: c.value for c in db.query(SystemConfig).all()}
|
||||
self.homeserver = configs.get('matrix_homeserver', 'https://matrix.tphai.com')
|
||||
self.username = configs.get('matrix_username', '')
|
||||
self.password = configs.get('matrix_password', '')
|
||||
self.access_token = configs.get('matrix_access_token', '')
|
||||
self.homeserver = configs.get('matrix_homeserver', 'http://matrix.tphai.com')
|
||||
self.user_id = configs.get('matrix_username', '@tester:matrix.tphai.com')
|
||||
self.password = configs.get('matrix_password', 'tester12345@!')
|
||||
|
||||
if self.username and (self.password or self.access_token):
|
||||
await self.connect()
|
||||
logger.info(f"Matrix配置: homeserver={self.homeserver}, user={self.user_id}")
|
||||
|
||||
if self.user_id and self.password:
|
||||
# 创建存储目录
|
||||
os.makedirs(STORE_PATH, exist_ok=True)
|
||||
|
||||
# 使用加密存储
|
||||
self.client = AsyncClient(
|
||||
self.homeserver,
|
||||
self.user_id,
|
||||
store_path=STORE_PATH
|
||||
)
|
||||
self.is_running = True
|
||||
logger.info(f"Matrix nio 客户端已初始化,存储路径: {STORE_PATH}")
|
||||
return True
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def connect(self):
|
||||
"""连接到Matrix服务器"""
|
||||
if not self.homeserver or not self.username:
|
||||
logger.warning("Matrix配置不完整,跳过连接")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 创建客户端
|
||||
self.client = AsyncClient(
|
||||
self.homeserver,
|
||||
self.username,
|
||||
store_path="/tmp/matrix_store"
|
||||
)
|
||||
|
||||
# 如果有access_token,直接设置
|
||||
if self.access_token:
|
||||
self.client.access_token = self.access_token
|
||||
self.client.user_id = self.username
|
||||
logger.info(f"Matrix已设置access_token: {self.username}")
|
||||
self.is_running = True
|
||||
return True
|
||||
|
||||
# 否则使用密码登录
|
||||
response = await self.client.login(self.password)
|
||||
|
||||
if isinstance(response, LoginResponse):
|
||||
logger.info(f"Matrix连接成功: {self.username}")
|
||||
self.is_running = True
|
||||
return True
|
||||
else:
|
||||
logger.error(f"Matrix登录失败: {response}")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Matrix连接错误: {e}")
|
||||
return False
|
||||
return False
|
||||
|
||||
async def start_sync(self, message_handler: Callable = None):
|
||||
"""开始同步消息"""
|
||||
if not self.client:
|
||||
logger.warning("Matrix客户端未初始化")
|
||||
if not self.is_running or not self.client:
|
||||
logger.warning("Matrix未连接")
|
||||
return
|
||||
|
||||
self.on_message_callback = message_handler
|
||||
|
||||
# 注册消息处理器
|
||||
self.client.add_event_callback(self._handle_room_message, RoomMessageText)
|
||||
|
||||
# 首先执行一次同步获取房间信息
|
||||
# 登录
|
||||
try:
|
||||
sync_response = await self.client.sync(timeout=10000)
|
||||
logger.info(f"Matrix初始同步完成")
|
||||
login_resp = await self.client.login(self.password)
|
||||
logger.info(f"Matrix登录成功: {login_resp}")
|
||||
|
||||
# 加载加密存储
|
||||
logger.info("加载加密存储...")
|
||||
|
||||
# 第一次同步获取房间密钥
|
||||
sync_resp = await self.client.sync(full_state=True)
|
||||
logger.info(f"初始同步完成: next_batch={sync_resp.next_batch}")
|
||||
|
||||
# 注册消息处理器 - 普通文本消息
|
||||
self.client.add_event_callback(self._handle_nio_message, RoomMessageText)
|
||||
# 注册加密消息处理器(解密后)
|
||||
self.client.add_event_callback(self._handle_encrypted_message, MegolmEvent)
|
||||
|
||||
logger.info("Matrix消息回调已注册")
|
||||
|
||||
# 使用 sync_forever 自动处理事件(后台任务)
|
||||
asyncio.create_task(self._run_sync_forever())
|
||||
logger.info("Matrix nio sync_forever 任务已启动")
|
||||
except Exception as e:
|
||||
logger.warning(f"Matrix初始同步失败: {e}, 将尝试继续")
|
||||
logger.error(f"Matrix启动失败: {e}")
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
async def _run_sync_forever(self):
|
||||
"""运行 sync_forever,自动处理所有事件"""
|
||||
try:
|
||||
logger.info("开始 Matrix sync_forever...")
|
||||
# 不使用 full_state,只做增量同步
|
||||
await self.client.sync_forever(timeout=30000)
|
||||
except Exception as e:
|
||||
logger.error(f"sync_forever 错误: {e}")
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
self.is_running = False
|
||||
|
||||
async def _handle_encrypted_message(self, room, event):
|
||||
"""处理加密消息(MegolmEvent)"""
|
||||
logger.info(f"收到加密消息: [{room.room_id}] event_id={event.event_id}")
|
||||
|
||||
# 启动后台同步任务(不阻塞)
|
||||
asyncio.create_task(self._sync_loop())
|
||||
logger.info("Matrix同步任务已启动")
|
||||
# 检查是否已解密
|
||||
logger.info(f"event.decrypted: {event.decrypted}")
|
||||
|
||||
try:
|
||||
body = None
|
||||
sender = event.sender
|
||||
|
||||
# 方法1: 检查 decrypted 属性
|
||||
if event.decrypted:
|
||||
logger.info(f"消息已解密: {event.decrypted}")
|
||||
# 使用 parse_decrypted_event 获取解密后的完整事件
|
||||
try:
|
||||
decrypted_event = event.parse_decrypted_event(event.decrypted)
|
||||
if decrypted_event:
|
||||
logger.info(f"解密事件类型: {type(decrypted_event)}")
|
||||
if hasattr(decrypted_event, 'body'):
|
||||
body = decrypted_event.body
|
||||
sender = decrypted_event.sender if hasattr(decrypted_event, 'sender') else event.sender
|
||||
logger.info(f"从 decrypted 获取: {body}")
|
||||
except Exception as e:
|
||||
logger.warning(f"parse_decrypted_event 失败: {e}")
|
||||
|
||||
# 方法2: 从 source 获取(如果是自己发的消息)
|
||||
if not body and hasattr(event, 'source') and event.source:
|
||||
content = event.source.get('content', {})
|
||||
# 检查是否有 plaintext body
|
||||
if 'body' in content:
|
||||
body = content.get('body', '')
|
||||
sender = event.source.get('sender', event.sender)
|
||||
logger.info(f"从source.content获取: {body[:50] if body else 'empty'}")
|
||||
|
||||
# 方法3: 直接尝试 event.body (某些情况下可能有)
|
||||
if not body and hasattr(event, 'body') and event.body:
|
||||
body = event.body
|
||||
logger.info(f"从event.body获取: {body[:50]}")
|
||||
|
||||
if body:
|
||||
logger.info(f"解密成功: sender={sender}, body={body[:50]}")
|
||||
await self._process_message(room, sender, body, event.event_id)
|
||||
else:
|
||||
logger.warning(f"加密消息无法解密: event_id={event.event_id}")
|
||||
logger.warning(f"event.decrypted={event.decrypted}, 需要密钥")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理加密消息失败: {e}")
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
async def _sync_loop(self):
|
||||
"""后台同步循环"""
|
||||
while self.is_running:
|
||||
try:
|
||||
# 使用较短的超时,避免长时间阻塞
|
||||
await self.client.sync(timeout=5000)
|
||||
await asyncio.sleep(1) # 每秒同步一次
|
||||
except Exception as e:
|
||||
logger.warning(f"Matrix同步错误: {e}")
|
||||
await asyncio.sleep(5) # 出错后等待5秒再重试
|
||||
|
||||
async def _handle_room_message(self, room: MatrixRoom, event: RoomMessageText):
|
||||
"""处理收到的房间消息"""
|
||||
async def _handle_nio_message(self, room, event):
|
||||
"""处理 nio 收到的消息(普通文本)"""
|
||||
# 忽略自己发送的消息
|
||||
if event.sender == self.username:
|
||||
sender = event.sender
|
||||
if sender == self.user_id:
|
||||
logger.debug(f"忽略自己发送的消息: {sender}")
|
||||
return
|
||||
|
||||
message_text = event.body.strip()
|
||||
|
||||
logger.info(f"Matrix收到文本消息: [{room.room_id}] {sender}: {message_text}")
|
||||
|
||||
# 调用统一处理方法
|
||||
await self._process_message(room, sender, message_text, event.event_id)
|
||||
|
||||
async def _process_message(self, room, sender, message_text, event_id):
|
||||
"""处理消息的核心逻辑"""
|
||||
# 忽略自己发送的消息
|
||||
if sender == self.user_id:
|
||||
logger.debug(f"忽略自己发送的消息: {sender}")
|
||||
return
|
||||
|
||||
# 保存房间ID
|
||||
self.last_room_id = room.room_id
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
conv_service = ConversationService(db)
|
||||
|
||||
# 获取或创建Matrix用户
|
||||
matrix_user_id = event.sender
|
||||
user = conv_service.get_or_create_user(
|
||||
user_id=matrix_user_id,
|
||||
display_name=room.users.get(matrix_user_id, {}).get('display_name', matrix_user_id),
|
||||
user_type='matrix',
|
||||
matrix_user_id=matrix_user_id
|
||||
# 使用固定主用户
|
||||
main_user = conv_service.get_or_create_user(
|
||||
MAIN_USER_ID,
|
||||
display_name="主用户",
|
||||
user_type='web'
|
||||
)
|
||||
|
||||
# 获取或创建房间映射
|
||||
mapping = db.query(MatrixRoomMapping).filter(
|
||||
MatrixRoomMapping.room_id == room.room_id
|
||||
).first()
|
||||
# 处理 /new 命令
|
||||
if message_text == "/new":
|
||||
conversation = conv_service.create_conversation(main_user.id)
|
||||
await self.send_message(room.room_id, "✅ 已创建新会话")
|
||||
logger.info(f"Matrix创建新会话: {conversation.conversation_id}")
|
||||
|
||||
if self.on_message_callback:
|
||||
await self.on_message_callback(
|
||||
action="new_conversation",
|
||||
conversation_id=conversation.conversation_id,
|
||||
room_id=room.room_id
|
||||
)
|
||||
return
|
||||
|
||||
if not mapping:
|
||||
# 为这个房间创建新会话
|
||||
conversation = conv_service.create_conversation(user.id, title=f"Matrix: {room.display_name}")
|
||||
mapping = MatrixRoomMapping(
|
||||
room_id=room.room_id,
|
||||
user_id=user.id,
|
||||
conversation_id=conversation.id
|
||||
)
|
||||
db.add(mapping)
|
||||
db.commit()
|
||||
db.refresh(mapping)
|
||||
# 获取最新会话
|
||||
conversations = conv_service.get_user_conversations(main_user.id)
|
||||
if not conversations:
|
||||
conversation = conv_service.create_conversation(main_user.id)
|
||||
else:
|
||||
conversation = conversations[0]
|
||||
|
||||
# 保存用户消息
|
||||
conv_service.add_message(
|
||||
conversation_id=mapping.conversation_id,
|
||||
user_msg = conv_service.add_message(
|
||||
conversation_id=conversation.id,
|
||||
role='user',
|
||||
content=event.body,
|
||||
content=message_text,
|
||||
source='matrix',
|
||||
extra_data={'event_id': event.event_id, 'room_id': room.room_id}
|
||||
extra_data={
|
||||
'event_id': event_id,
|
||||
'room_id': room.room_id,
|
||||
'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}")
|
||||
|
||||
# 调用回调(用于网页同步)
|
||||
if self.on_message_callback:
|
||||
await self.on_message_callback(
|
||||
conversation_id=mapping.conversation.conversation_id,
|
||||
user_message=event.body,
|
||||
user_id=user.user_id,
|
||||
room_id=room.room_id
|
||||
action="chat",
|
||||
conversation_id=conversation.conversation_id,
|
||||
user_message=message_text,
|
||||
room_id=room.room_id,
|
||||
message_id=user_msg.id
|
||||
)
|
||||
|
||||
logger.info(f"Matrix回复已发送: {ai_response[:50]}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理Matrix消息失败: {e}")
|
||||
import traceback
|
||||
logger.error(traceback.format_exc())
|
||||
await self.send_message(room.room_id, f"处理消息时出错: {str(e)}")
|
||||
try:
|
||||
await self.client.room_typing(room.room_id, typing_state=False)
|
||||
except:
|
||||
pass
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def send_message(self, room_id: str, message: str):
|
||||
"""发送消息到Matrix房间"""
|
||||
if not self.client:
|
||||
logger.error("Matrix客户端未初始化")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 先尝试直接发送
|
||||
await self.client.room_send(
|
||||
room_id=room_id,
|
||||
message_type="m.room.message",
|
||||
content={
|
||||
room_id,
|
||||
"m.room.message",
|
||||
{
|
||||
"msgtype": "m.text",
|
||||
"body": message
|
||||
}
|
||||
)
|
||||
logger.info(f"Matrix消息发送成功: {room_id}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"发送Matrix消息失败: {e}")
|
||||
return False
|
||||
|
||||
async def get_room_id_for_user(self, user_matrix_id: str) -> Optional[str]:
|
||||
"""获取与用户的对话房间ID"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
user = db.query(User).filter(User.matrix_user_id == user_matrix_id).first()
|
||||
if user:
|
||||
mapping = db.query(MatrixRoomMapping).filter(
|
||||
MatrixRoomMapping.user_id == user.id
|
||||
).first()
|
||||
if mapping:
|
||||
return mapping.room_id
|
||||
return None
|
||||
finally:
|
||||
db.close()
|
||||
error_str = str(e)
|
||||
if "not verified" in error_str or "OlmUnverifiedDeviceError" in error_str:
|
||||
logger.warning(f"设备未验证,尝试验证设备后发送: {e}")
|
||||
try:
|
||||
# 验证所有未验证的设备
|
||||
for user_id, devices in self.client.device_store.items():
|
||||
for device_id, device in devices.items():
|
||||
if not device.verified:
|
||||
logger.info(f"验证设备: {user_id} {device_id}")
|
||||
self.client.verify_device(device)
|
||||
|
||||
# 再次尝试发送
|
||||
await self.client.room_send(
|
||||
room_id,
|
||||
"m.room.message",
|
||||
{
|
||||
"msgtype": "m.text",
|
||||
"body": message
|
||||
}
|
||||
)
|
||||
logger.info(f"Matrix消息发送成功(验证设备后): {room_id}")
|
||||
return True
|
||||
except Exception as e2:
|
||||
logger.error(f"验证设备后发送仍失败: {e2}")
|
||||
# 最后尝试用 HTTP API 发送(不加密)
|
||||
try:
|
||||
import httpx
|
||||
async with httpx.AsyncClient() as http_client:
|
||||
txn_id = f"m{int(asyncio.get_event_loop().time() * 1000)}"
|
||||
resp = await http_client.put(
|
||||
f"{self.homeserver}/_matrix/client/v3/rooms/{room_id}/send/m.room.message/{txn_id}",
|
||||
headers={"Authorization": f"Bearer {self.client.access_token}"},
|
||||
json={"msgtype": "m.text", "body": message}
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
logger.info(f"Matrix消息通过HTTP发送成功: {room_id}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"HTTP发送失败: {resp.status_code}")
|
||||
return False
|
||||
except Exception as e3:
|
||||
logger.error(f"HTTP发送失败: {e3}")
|
||||
return False
|
||||
else:
|
||||
logger.error(f"发送Matrix消息错误: {e}")
|
||||
return False
|
||||
|
||||
async def disconnect(self):
|
||||
"""断开连接"""
|
||||
self.is_running = False
|
||||
if self.client:
|
||||
await self.client.logout()
|
||||
await self.client.close()
|
||||
self.is_running = False
|
||||
|
||||
|
||||
# 全局实例
|
||||
|
||||
@@ -341,20 +341,39 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// 全局变量
|
||||
// 全局变量 - 使用固定主用户(与Matrix AI用户相同)
|
||||
let ws = null;
|
||||
let userId = 'web_' + Math.random().toString(36).substr(2, 9);
|
||||
let userId = 'main_user'; // 固定主用户ID
|
||||
let currentConversationId = null;
|
||||
let conversations = [];
|
||||
|
||||
// 初始化
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
document.getElementById('userIdDisplay').textContent = `用户: ${userId}`;
|
||||
document.getElementById('userIdDisplay').textContent = '主用户模式';
|
||||
connectWebSocket();
|
||||
loadConversations();
|
||||
setupTextarea();
|
||||
|
||||
// 监听最新会话更新
|
||||
setInterval(checkLatestConversation, 2000);
|
||||
});
|
||||
|
||||
// 检查最新会话(用于Matrix同步)
|
||||
async function checkLatestConversation() {
|
||||
try {
|
||||
const response = await fetch('/api/conversations/latest');
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data.conversation && data.conversation.id !== currentConversationId) {
|
||||
// 有新会话,自动切换
|
||||
selectConversation(data.conversation.id);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// 忽略错误
|
||||
}
|
||||
}
|
||||
|
||||
// WebSocket连接
|
||||
function connectWebSocket() {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
@@ -391,8 +410,27 @@
|
||||
loadConversations();
|
||||
break;
|
||||
|
||||
case 'new_conversation':
|
||||
// Matrix创建了新会话,自动切换
|
||||
currentConversationId = data.conversation_id;
|
||||
loadConversations();
|
||||
// 清空消息区域
|
||||
const container = document.getElementById('messagesContainer');
|
||||
container.innerHTML = `
|
||||
<div class="welcome">
|
||||
<h2>👋 开始新对话</h2>
|
||||
<p>输入您的问题开始对话</p>
|
||||
</div>
|
||||
`;
|
||||
break;
|
||||
|
||||
case 'user_message':
|
||||
appendMessage('user', data.message.content, data.message.source);
|
||||
// 如果消息来自Matrix,切换到对应会话
|
||||
if (data.message.source === 'matrix' && data.conversation_id) {
|
||||
currentConversationId = data.conversation_id;
|
||||
renderConversations();
|
||||
}
|
||||
break;
|
||||
|
||||
case 'assistant_message':
|
||||
@@ -451,10 +489,15 @@
|
||||
// 加载会话列表
|
||||
async function loadConversations() {
|
||||
try {
|
||||
const response = await fetch('/api/conversations?user_id=' + userId);
|
||||
const response = await fetch('/api/conversations');
|
||||
const data = await response.json();
|
||||
conversations = data.conversations;
|
||||
renderConversations();
|
||||
|
||||
// 如果没有当前会话且有会话列表,自动选择最新的
|
||||
if (!currentConversationId && conversations.length > 0) {
|
||||
selectConversation(conversations[0].id);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载会话失败:', error);
|
||||
}
|
||||
@@ -496,7 +539,7 @@
|
||||
// 创建新会话
|
||||
async function createNewConversation() {
|
||||
try {
|
||||
const response = await fetch('/api/conversations?user_id=' + userId, {
|
||||
const response = await fetch('/api/conversations', {
|
||||
method: 'POST'
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user