Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 66fa5db3d7 | |||
| 2c007c4801 | |||
| 027830b0d6 |
BIN
ai_chat.db
BIN
ai_chat.db
Binary file not shown.
28
main.py
28
main.py
@@ -258,7 +258,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = D
|
|||||||
|
|
||||||
# 同步到Matrix(如果有房间)
|
# 同步到Matrix(如果有房间)
|
||||||
if matrix_bot.is_running and matrix_bot.last_room_id:
|
if matrix_bot.is_running and matrix_bot.last_room_id:
|
||||||
await matrix_bot.send_message(matrix_bot.last_room_id, f"[网页] {message}")
|
await matrix_bot.send_message(matrix_bot.last_room_id, message)
|
||||||
|
|
||||||
# 获取对话历史
|
# 获取对话历史
|
||||||
history = conv_service.get_conversation_history(conversation_id, limit=20)
|
history = conv_service.get_conversation_history(conversation_id, limit=20)
|
||||||
@@ -422,7 +422,7 @@ async def update_config(data: dict, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
# ==================== Matrix消息处理回调 ====================
|
# ==================== Matrix消息处理回调 ====================
|
||||||
|
|
||||||
async def handle_matrix_message(action: str, conversation_id: str = None, user_message: str = None, room_id: str = None, message_id: int = None):
|
async def handle_matrix_message(action: str, conversation_id: str = None, user_message: str = None, room_id: str = None, message_id: int = None, sender: str = None):
|
||||||
"""处理从Matrix收到的消息"""
|
"""处理从Matrix收到的消息"""
|
||||||
|
|
||||||
if action == "new_conversation":
|
if action == "new_conversation":
|
||||||
@@ -433,6 +433,30 @@ async def handle_matrix_message(action: str, conversation_id: str = None, user_m
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if action == "user_message":
|
||||||
|
# 用户消息 - 同步到网页端
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
conv_service = ConversationService(db)
|
||||||
|
conversation = conv_service.get_conversation(conversation_id)
|
||||||
|
if conversation:
|
||||||
|
# 发送用户消息通知到网页端
|
||||||
|
await manager.send_to_user(MAIN_USER_ID, {
|
||||||
|
"type": "user_message",
|
||||||
|
"conversation_id": conversation_id,
|
||||||
|
"message": {
|
||||||
|
"id": message_id,
|
||||||
|
"role": "user",
|
||||||
|
"content": user_message,
|
||||||
|
"source": "matrix",
|
||||||
|
"sender": sender,
|
||||||
|
"created_at": datetime.now().isoformat()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
return
|
||||||
|
|
||||||
if action == "chat":
|
if action == "chat":
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
@huangzhuang_bro:matrix.tphai.com BQHGFLQEPR matrix-ed25519 IrEHmvqotfHKLyx1JRJp4RthUVyBT8qQX72qBifRRyQ
|
||||||
|
@tester:matrix.tphai.com AJFVRTHLJY matrix-ed25519 4mRjLhM8xbwjkwQP2T/iB3UZJoaADgP6cCVUiB8AtSk
|
||||||
|
@tester:matrix.tphai.com GVSFGGYNJL matrix-ed25519 8qV2own4G3m2nki+izFDBOrAxtbGl8RoneM3qUPkThU
|
||||||
|
@tester:matrix.tphai.com IMEQIQPXTR matrix-ed25519 6Yd4lmhP6jdkkNvh1rIw6TRK331ZUyiAt5G5hPeYqSE
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
@tester:matrix.tphai.com AJFVRTHLJY matrix-ed25519 4mRjLhM8xbwjkwQP2T/iB3UZJoaADgP6cCVUiB8AtSk
|
||||||
|
@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
|
||||||
|
@huangzhuang_bro:matrix.tphai.com BQHGFLQEPR matrix-ed25519 IrEHmvqotfHKLyx1JRJp4RthUVyBT8qQX72qBifRRyQ
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
@huangzhuang_bro:matrix.tphai.com BQHGFLQEPR matrix-ed25519 IrEHmvqotfHKLyx1JRJp4RthUVyBT8qQX72qBifRRyQ
|
||||||
|
@tester:matrix.tphai.com AJFVRTHLJY matrix-ed25519 4mRjLhM8xbwjkwQP2T/iB3UZJoaADgP6cCVUiB8AtSk
|
||||||
|
@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
|
||||||
Binary file not shown.
@@ -224,6 +224,17 @@ class MatrixBot:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 立即通知网页端有新用户消息(通过回调)
|
||||||
|
if self.on_message_callback:
|
||||||
|
await self.on_message_callback(
|
||||||
|
action="user_message",
|
||||||
|
conversation_id=conversation.conversation_id,
|
||||||
|
user_message=message_text,
|
||||||
|
room_id=room.room_id,
|
||||||
|
message_id=user_msg.id,
|
||||||
|
sender=sender
|
||||||
|
)
|
||||||
|
|
||||||
# 发送"正在输入"状态
|
# 发送"正在输入"状态
|
||||||
try:
|
try:
|
||||||
await self.client.room_typing(room.room_id, typing_state=True)
|
await self.client.room_typing(room.room_id, typing_state=True)
|
||||||
|
|||||||
@@ -467,11 +467,10 @@
|
|||||||
messageDiv.className = `message ${role}`;
|
messageDiv.className = `message ${role}`;
|
||||||
|
|
||||||
const avatar = role === 'user' ? '👤' : '🤖';
|
const avatar = role === 'user' ? '👤' : '🤖';
|
||||||
const sourceLabel = source === 'matrix' ? ' [Matrix]' : '';
|
|
||||||
|
|
||||||
messageDiv.innerHTML = `
|
messageDiv.innerHTML = `
|
||||||
<div class="message-avatar">${avatar}</div>
|
<div class="message-avatar">${avatar}</div>
|
||||||
<div class="message-content">${formatContent(content)}${sourceLabel}</div>
|
<div class="message-content">${formatContent(content)}</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
container.appendChild(messageDiv);
|
container.appendChild(messageDiv);
|
||||||
|
|||||||
Reference in New Issue
Block a user