151 lines
7.5 KiB
Python
151 lines
7.5 KiB
Python
# myapp/sockets/namespaces.py
|
|
import json
|
|
from flask import current_app, request, session
|
|
from flask_socketio import Namespace, join_room, leave_room, emit
|
|
|
|
from ..function_tools import judge
|
|
from ..function_tools import next_chapter
|
|
from ..services.memory_service import save_chapter_memory_by_uuid
|
|
from ..services.backboard_service import realtime_response
|
|
from ..services.markdown_service import load_full_markdown_file
|
|
from ..extension_ase.ase_client import HSAEngineClient, ChatManager
|
|
|
|
|
|
|
|
class VSCodeNamespace(Namespace):
|
|
def on_login(self,data):
|
|
ex = current_app.extensions
|
|
uuid2username = ex["uuid2username"]
|
|
backboard_manager = ex["backboard_manager"]
|
|
print("VSCode client connected")
|
|
print(data)
|
|
dataconfig = data['config']
|
|
user_uuid = dataconfig.get('user_uuid')
|
|
path = dataconfig.get('path')
|
|
course_id = dataconfig.get('course_id')
|
|
lesson_id = dataconfig.get('chapter_id')
|
|
print(f"User {user_uuid} connected with path: {path}")
|
|
join_room(user_uuid, namespace='/vscode')
|
|
if backboard_manager.get_backboard(user_uuid) == None:
|
|
backboard_manager.add_backboard(user_uuid,uuid2username[user_uuid], course_id, lesson_id, 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, data):
|
|
print("VSCode client disconnected")
|
|
print("Disconnect reason:"+str(data))
|
|
|
|
|
|
class AgentNamespace(Namespace):
|
|
def on_login(self, data):
|
|
self.current_app = current_app
|
|
ex = current_app.extensions
|
|
uuid2username = ex["uuid2username"]
|
|
backboard_manager = ex["backboard_manager"]
|
|
data = json.loads(data)
|
|
user_uuid = data['user_uuid']
|
|
course_id = data['course_id']
|
|
lesson_name = data['lesson_name']
|
|
chapter_name = data['chapter_name']
|
|
user_id = uuid2username[user_uuid]
|
|
session['user_uuid'] = user_uuid
|
|
print(f'User connected with session user_uuid: {user_uuid}')
|
|
user_uuid2chatmanager = ex["user_uuid2chatmanager"]
|
|
user_uuid2ase_client = ex["user_uuid2ase_client"]
|
|
user_uuid2ase_client[user_uuid] = HSAEngineClient(current_app.config["ASE_ENGINE_URL"], current_app.config["ASE_ENGINE_NAMESPACE"],id=user_id)
|
|
user_uuid2ase_client[user_uuid].connect()
|
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
|
route='dialog',
|
|
callback=self.receive_ase_dialog,
|
|
entry=self,
|
|
init_data={'room': user_uuid}
|
|
)
|
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
|
route='judge',
|
|
callback=self.receive_ase_judge,
|
|
entry=self,
|
|
init_data={'room': user_uuid}
|
|
)
|
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
|
route='next_chapter',
|
|
callback=self.receive_ase_next_chapter,
|
|
entry=self,
|
|
init_data={'room': user_uuid}
|
|
)
|
|
|
|
fmd,fmdp,fsp = load_full_markdown_file(course_id, chapter_name, lesson_name)
|
|
chatmanager = user_uuid2chatmanager[user_uuid]
|
|
chatmanager.init(user_uuid, user_uuid2ase_client[user_uuid], fmd, fmdp, fsp, max_iter=5, app=current_app, socketio=self)
|
|
chatmanager.function_manager.add_function(judge, {'course_id': course_id, 'lesson_name': lesson_name, 'root_path': f'../../study/{user_id}/{course_id}/{chapter_name}/{lesson_name}'})
|
|
chatmanager.function_manager.add_function(next_chapter, {'user_uuid': user_uuid})
|
|
|
|
join_room(user_uuid, namespace='/agent') # 将该用户加入以user_id为名的room
|
|
backboard_manager.add_backboard(user_uuid, user_id, course_id, lesson_name, root_path=f'../../study/{user_id}/{course_id}/{chapter_name}/{lesson_name}')
|
|
chatmanager.bb = backboard_manager.get_backboard(user_uuid)
|
|
chatmanager.focus_chapter(0)
|
|
|
|
def on_language(self, language):
|
|
ex = current_app.extensions
|
|
print(f"Language changed to: {language}")
|
|
|
|
def on_message(self, data):
|
|
print(f"Message from client: {data}")
|
|
ex = current_app.extensions
|
|
chatmanager = ex["user_uuid2chatmanager"]
|
|
uuid = session.get('user_uuid')
|
|
user_uuid2ase_client = ex["user_uuid2ase_client"]
|
|
if (type(data)==str):
|
|
data = json.loads(data)
|
|
if data['type'] == 'text':
|
|
res = user_uuid2ase_client[uuid].send_text('dialog', data['data'])
|
|
print(f"Send text to route 'dialog' success: {res}")
|
|
# print('=*='*20)
|
|
# print(res.content)
|
|
# reply = f"{res.content['speak']}"
|
|
# with current_app.app_context():
|
|
# emit('message',reply, room=uuid, namespace='/agent')
|
|
# emit('request_function',res.content['function'], room=uuid, namespace='/agent')
|
|
|
|
if data['type'] == 'function': # 在这里 前端会发送允许执行的function与参数
|
|
print("function_call", data['data'])
|
|
chatmanager[uuid].function_call(uuid, data['data'])
|
|
|
|
|
|
def receive_ase_dialog(client_entry: HSAEngineClient, namespace_entry: Namespace, data, init_data):
|
|
print("receive_ase_dialog", data)
|
|
namespace_entry.emit('message', data['data'], room=init_data['room'], namespace='/agent')
|
|
|
|
def receive_ase_judge(client_entry: HSAEngineClient, namespace_entry: Namespace, data, init_data):
|
|
print("receive_ase_judge", data)
|
|
namespace_entry.emit('request_function', data['data'], room=init_data['room'], namespace='/agent') # data 是一个列表 /dict也支持
|
|
|
|
def receive_ase_next_chapter(client_entry: HSAEngineClient, namespace_entry: Namespace, data, init_data):
|
|
print("receive_ase_next_chapter", data)
|
|
namespace_entry.emit('request_function',data['data'], room=init_data['room'], namespace='/agent') # data 是一个列表 /dict也支持
|
|
# namespace_entry.emit('next_chapter', room=init_data['room'], namespace='/agent')
|
|
|
|
def on_initiative(self,data):
|
|
print("User active function call")
|
|
ex = current_app.extensions
|
|
agent_manager = ex["agent_manager"]
|
|
backboard_manager = ex["backboard_manager"]
|
|
uuid = session.get('user_uuid')
|
|
if data['name'] == 'sample_judge':
|
|
agent_manager.sample_judge(uuid, backboard_manager.get_backboard(uuid))
|
|
if data['name'] == 'judge':
|
|
agent_manager.judge(uuid, backboard_manager.get_backboard(uuid))
|
|
|
|
|
|
|
|
def on_disconnect(self,data):
|
|
ex = current_app.extensions
|
|
uuid = session.get('user_uuid')
|
|
user_uuid2chatmanager = ex["user_uuid2chatmanager"]
|
|
user_uuid2chatmanager[uuid].disconnect(uuid)
|
|
|
|
print("VSCode client disconnected")
|
|
print("Disconnect reason:"+str(data)) |