vscode plugin ws ok
This commit is contained in:
Binary file not shown.
40
Html/app.py
40
Html/app.py
@@ -20,7 +20,7 @@ import logging
|
||||
app.secret_key = 'cakebaker'
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
# 配置 CORS
|
||||
# CORS(app, resources={r"/*": {"origins": "http://localhost:9888"}}, supports_credentials=True)
|
||||
CORS(app, resources={r"/*": {"origins": "http://localhost:9888"},}, supports_credentials=True)
|
||||
|
||||
|
||||
userid_recorder = {} # user_id&path -> session['user_id']
|
||||
@@ -37,32 +37,31 @@ user_id2uuid = {}
|
||||
|
||||
# 定义命名空间:用于和 VSCode 插件交流
|
||||
class VSCodeNamespace(Namespace):
|
||||
def on_connect(self):
|
||||
def on_login(self,data):
|
||||
print("VSCode client connected")
|
||||
|
||||
def on_message(self, data):
|
||||
print(f"Received from VSCode client: {data}")
|
||||
data = json.loads(data)
|
||||
datatype = data['type']
|
||||
print(data)
|
||||
dataconfig = data['config']
|
||||
user_id = dataconfig.get('user_id')
|
||||
path = dataconfig.get('path')
|
||||
print(f"User {user_id} connected with path: {path}")
|
||||
emit('response', {'message': 'Config data received and connection established'})
|
||||
useruuid = user_id2uuid[user_id]
|
||||
if backboard_manager.get_backboard(useruuid) == None:
|
||||
backboard_manager.add_backboard(useruuid, path)
|
||||
|
||||
|
||||
def on_message(self, data):
|
||||
print(f"Received from VSCode client: {data}")
|
||||
dataconfig = data['config']
|
||||
realtime_response(dataconfig, data)
|
||||
# emit('response', {'message': 'Config data received and connection established'})
|
||||
def on_disconnect(self):
|
||||
print("VSCode client disconnected")
|
||||
@app.route('/backboard')
|
||||
def backboard():
|
||||
|
||||
return jsonify({'status': 'success'})
|
||||
|
||||
|
||||
def realtime_response(config, realtime_action):
|
||||
user_id = config['user_id']
|
||||
folder_path = config['path']
|
||||
useruuid = user_id2uuid[user_id]
|
||||
if backboard_manager.get_backboard(useruuid) == None:
|
||||
backboard_manager.add_backboard(useruuid, folder_path)
|
||||
bb = backboard_manager.get_backboard(useruuid)
|
||||
assert type(bb) == Backboard
|
||||
|
||||
@@ -105,14 +104,17 @@ class AgentNamespace(Namespace):
|
||||
session['user_id'] = user_uuid
|
||||
print(f'User connected with session user_id: {user_uuid}')
|
||||
# 从markdown、markdown_prompts、score_prompts中各读取数据folder.md并进行new_agent
|
||||
with open(f'books/markdown/{folder_name}.md','r') as fmd,\
|
||||
open(f'books/markdown_prompts/{folder_name}.md','r')as fmdp,\
|
||||
open(f'books/score_prompts/{folder_name}.md','r') as fsp:
|
||||
with open(f'books/markdown/{folder_name}.md','r',encoding='UTF-8') as fmd,\
|
||||
open(f'books/markdown_prompts/{folder_name}.md','r',encoding='UTF-8')as fmdp,\
|
||||
open(f'books/score_prompts/{folder_name}.md','r',encoding='UTF-8') as fsp:
|
||||
markdown = fmd.read()
|
||||
markdown_prompts = fmdp.read()
|
||||
score_prompts = fsp.read()
|
||||
user_uuid, agent = agent_manager.new_agent(markdown, markdown_prompts, score_prompts, id=user_uuid)
|
||||
print(user_uuid)
|
||||
join_room(user_uuid) # 将该用户加入以user_id为名的room
|
||||
backboard_manager.add_backboard(user_uuid, folder_name)
|
||||
|
||||
|
||||
def on_message(self, data):
|
||||
print(f"Message from client: {data}")
|
||||
@@ -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):
|
||||
@@ -237,5 +239,5 @@ def get_session():
|
||||
if __name__ == '__main__':
|
||||
socketio.on_namespace(VSCodeNamespace('/vscode'))
|
||||
socketio.on_namespace(AgentNamespace('/agent'))
|
||||
socketio.run(app, host='localhost', port=5500, debug=True)
|
||||
socketio.run(app, host='0.0.0.0', port=5000, debug=True)
|
||||
|
||||
|
||||
@@ -42,7 +42,6 @@ class Backboard:
|
||||
for h in self.history[-5:]:
|
||||
five_history += h['type'] + "\n" + h.get('filePath', '') + "\n\n"
|
||||
return (f"###Global Info:###"
|
||||
f"- User is {self.user_id}"
|
||||
f"- User's total study time is {self.get_deltatime_mmss(self.create_time, time.time())}"
|
||||
f"- User's current chapter study time is {self.get_deltatime_mmss(self.enter_chapter_time, time.time())}"
|
||||
f"- Activated file path: {self.active_file_path}"
|
||||
@@ -55,8 +54,8 @@ class Backboard:
|
||||
def add_history(self, realtime_action):
|
||||
if(realtime_action['type']=='config'):return
|
||||
if len(self.history)!=0:
|
||||
if (self.history[-1]['type']=='edit'):
|
||||
if(self.history[-1]['filepath'] == realtime_action['filepath']):
|
||||
if (self.history[-1]['type']=='fileEdit'):
|
||||
if(self.history[-1]['filePath'] == realtime_action['filePath']):
|
||||
self.history[-1]['content'] = realtime_action['content']
|
||||
return
|
||||
self.history.append(realtime_action)
|
||||
|
||||
@@ -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:5000/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 = `<div class="chat-message server-message"><b>服务器:</b> ${marked.parse(data)}</div>`;
|
||||
document.getElementById('chatbox').appendChild(serverMessage);
|
||||
|
||||
// 滚动到最新消息
|
||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -78,7 +77,7 @@ function sendMessage() {
|
||||
document.getElementById('chatbox').appendChild(userMessage);
|
||||
|
||||
// 发送消息到服务器
|
||||
socket.emit('send_message', input);
|
||||
socket.emit('message', input);
|
||||
|
||||
// 清空输入框
|
||||
document.getElementById('messageInput').value = '';
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
document.cookie = "vscode-tkn=44edc269-6f88-46ab-9790-dc253f66ac36; path=/; SameSite=None; Secure";
|
||||
</script>
|
||||
<script src="https://cdn.bootcdn.net/ajax/libs/marked/13.0.2/marked.min.js"></script>
|
||||
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<script src="https://unpkg.com/split.js/dist/split.min.js"></script>
|
||||
</head>
|
||||
|
||||
Reference in New Issue
Block a user