diff --git a/Html/app.py b/Html/app.py index b14208f..d071e11 100644 --- a/Html/app.py +++ b/Html/app.py @@ -98,9 +98,10 @@ agent_manager = AgentManager() class AgentNamespace(Namespace): - def on_connect(self): - user = request.args.get('username') - folder_name = request.args.get('folder') + def on_login(self, data): + data = json.loads(data) + user = data['username'] + folder_name = data['folder'] user_uuid = user_id2uuid[user] session['user_id'] = user_uuid print(f'User connected with session user_id: {user_uuid}') @@ -114,6 +115,7 @@ class AgentNamespace(Namespace): user_uuid, agent = agent_manager.new_agent(markdown, markdown_prompts, score_prompts, id=user_uuid) join_room(user_uuid) # 将该用户加入以user_id为名的room + def on_message(self, data): print(f"Message from client: {data}") id = session.get('user_id') @@ -122,7 +124,7 @@ class AgentNamespace(Namespace): res = agent_manager.invoke(id, data, backboard_manager.get_backboard(id).get_info_prompt()) reply = f"AI: {res.content['speak']}" # 将回复发送回前端 - emit('receive_message', reply) + emit('message', reply) def on_disconnect(self): diff --git a/Html/static/js/chatbox.js b/Html/static/js/chatbox.js index 7aea86f..6a94d95 100644 --- a/Html/static/js/chatbox.js +++ b/Html/static/js/chatbox.js @@ -3,22 +3,21 @@ var socket; document.addEventListener('DOMContentLoaded', function() { data = window.appData; console.log(data); - socket = new WebSocket('ws://localhost:5500/agent'); - socket.onopen = () => { - console.log('Connected to agent WebSocket'); - ws.send(JSON.stringify({ message: 'Hello from client' })); // 发送数据 - }; + socket = io('http://localhost:5500/agent?username='+data.username+'&folder='+data.folder); + socket.on('connect', function() { + console.log('Connected to server'); + socket.emit('login',JSON.stringify(data)); + }); // 监听来自服务器的消息 - socket.onmessage = (event) => { + socket.on('message', function(data) { // 显示服务器的回复消息 - data = event.data const serverMessage = document.createElement('div'); serverMessage.innerHTML = `
`; document.getElementById('chatbox').appendChild(serverMessage); // 滚动到最新消息 document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight; - }; + }); }); @@ -45,23 +44,6 @@ chineseRadio.addEventListener('change', function() { if (chineseRadio.checked) language = 'zn'; }); -function sendMessage() { - const input = document.getElementById('messageInput').value; - if (input.trim() === '') return; - - // 创建一个新的消息元素 - const newMessage = document.createElement('div'); - newMessage.classList.add('chat-message'); - newMessage.innerHTML = `${language === 'en' ? 'You:' : '你:'} ${input}`; - chatbox.appendChild(newMessage); - - // 清空输入框 - document.getElementById('messageInput').value = ''; - - // 滚动到最新消息 - chatbox.scrollTop = chatbox.scrollHeight; -} - // 发送消息 @@ -78,7 +60,7 @@ function sendMessage() { document.getElementById('chatbox').appendChild(userMessage); // 发送消息到服务器 - socket.emit('send_message', input); + socket.emit('message', input); // 清空输入框 document.getElementById('messageInput').value = ''; diff --git a/Html/templates/index.html b/Html/templates/index.html index d2881ea..3bc8dd7 100644 --- a/Html/templates/index.html +++ b/Html/templates/index.html @@ -10,7 +10,8 @@ - + + diff --git a/cake/binary_search/binarySearch.py b/cake/binary_search/binarySearch.py index 90545a5..1cc3c45 100644 --- a/cake/binary_search/binarySearch.py +++ b/cake/binary_search/binarySearch.py @@ -3,4 +3,4 @@ array = list(map(int, input().split())) q = int(input()) while q!=0: q-=1 - \ No newline at end of file + 1 \ No newline at end of file diff --git a/cake/bootstrap.min.css.map/.config b/cake/bootstrap.min.css.map/.config new file mode 100644 index 0000000..7716b86 --- /dev/null +++ b/cake/bootstrap.min.css.map/.config @@ -0,0 +1,2 @@ +user_id=cake +path=bootstrap.min.css.map \ No newline at end of file diff --git a/cake/folder/.config b/cake/folder/.config index 955937b..b89ccf4 100644 --- a/cake/folder/.config +++ b/cake/folder/.config @@ -1,2 +1,4 @@ -user_id=cake -path=folder \ No newline at end of file +{ + "user_id": "cake", + "path": "folder" +}