feat: 网页端固定主用户,Matrix /new创建新会话,实时同步

This commit is contained in:
2026-04-11 12:29:11 +08:00
parent b05a03e198
commit b228283133
5 changed files with 206 additions and 128 deletions

View File

@@ -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();