diff --git a/Html/apps/sockets/namespaces.py b/Html/apps/sockets/namespaces.py
index 4061cc5..e4bfa70 100644
--- a/Html/apps/sockets/namespaces.py
+++ b/Html/apps/sockets/namespaces.py
@@ -62,43 +62,32 @@ class VSCodeNamespace(Namespace):
class AgentNamespace(Namespace):
- def _parse_login_data(self, data):
- """解析登录数据"""
+ def on_login(self, data):
+ self.app = current_app
+ ex = current_app.extensions
+ uuid2username = ex["uuid2username"]
+ backboard_manager = ex["backboard_manager"]
data = json.loads(data)
- return {
- 'user_uuid': data['user_uuid'],
- 'course_id': data['course_id'],
- 'lesson_name': data['lesson_name'],
- 'chapter_name': data['chapter_name']
- }
-
- def _setup_user_session(self, user_uuid, uuid2username):
- """设置用户会话和加入房间"""
+ 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
join_room(user_uuid, namespace='/agent') # 将该用户加入以user_id为名的room
print(f'User connected with session user_uuid: {user_uuid}')
- return user_id
-
- def _manage_ase_client_connection(self, user_uuid, ex):
- """管理ASE客户端连接"""
+
+ user_uuid2chatmanager = ex["user_uuid2chatmanager"]
user_uuid2ase_client = ex["user_uuid2ase_client"]
- if (user_uuid2ase_client.get(user_uuid, None) is not None and user_uuid2ase_client[user_uuid].connected):
+ if (user_uuid2ase_client.get(user_uuid,None) is not None and user_uuid2ase_client[user_uuid].connected):
user_uuid2ase_client[user_uuid].disconnect()
- return user_uuid2ase_client
-
- def _configure_chatmanager_functions(self, chatmanager, course_id, user_id, chapter_name, lesson_name, user_uuid):
- """配置聊天管理器的函数"""
+ chatmanager = user_uuid2chatmanager[user_uuid]
chatmanager.function_manager.add_function(judge, {'course_id': course_id, 'root_path': f'/home/{user_id}/{course_id}/{chapter_name}/{lesson_name}'})
chatmanager.function_manager.add_function(next_chapter, {'user_uuid': user_uuid})
-
- def _load_and_restore_progress(self, chatmanager, user_id, course_id, chapter_name, lesson_name):
- """加载和恢复学习进度"""
continue_learn = not chatmanager.restart
- need_skip_chapters = 0
-
- if continue_learn: # 尝试加载用户学习进度
+ if continue_learn:# 尝试加载用户学习进度
progress_result = load_learning_progress(user_id, course_id, chapter_name, lesson_name)
+ need_skip_chapters = 0
if progress_result['exists']:
progress = progress_result['data']
@@ -122,121 +111,72 @@ class AgentNamespace(Namespace):
# 使用默认值,确保有完整的结构
chatmanager.chat_historys = progress_result['data']['chat_historys']
chatmanager.scores = progress_result['data']['scores']
-
- return continue_learn, need_skip_chapters
-
- def _initialize_chatmanager(self, user_uuid, user_id, course_id, chapter_name, lesson_name, chatmanager, user_uuid2ase_client):
- """初始化聊天管理器"""
- fmd, fmdp, fsp = load_full_markdown_file(course_id, chapter_name, lesson_name)
- user_uuid2ase_client[user_uuid] = HSAEngineClient(
- current_app.config["ASE_ENGINE_URL"],
- current_app.config["ASE_ENGINE_NAMESPACE"],
- id=user_id,
- chatmanager=chatmanager
- )
- chatmanager.init(
- user_uuid,
- user_uuid2ase_client[user_uuid],
- fmd, fmdp, fsp,
- course_id, chapter_name, lesson_name,
- max_iter=5,
- app=current_app,
- socketio=self
- )
- return user_uuid2ase_client
-
- def _setup_backboard(self, user_uuid, user_id, course_id, chapter_name, lesson_name, chatmanager, backboard_manager):
- """设置背板管理器"""
- backboard_manager.add_backboard(
- user_uuid,
- user_id,
- course_id,
- lesson_name,
- root_path=f'/home/{user_id}/{course_id}/{chapter_name}/{lesson_name}'
- )
+
+ fmd,fmdp,fsp = load_full_markdown_file(course_id, chapter_name, lesson_name)
+ user_uuid2ase_client[user_uuid] = HSAEngineClient(current_app.config["ASE_ENGINE_URL"], current_app.config["ASE_ENGINE_NAMESPACE"],id=user_id, chatmanager = chatmanager)
+ chatmanager.init(user_uuid, user_uuid2ase_client[user_uuid], fmd, fmdp, fsp, course_id, chapter_name, lesson_name, max_iter=5, app=current_app, socketio=self)
+ backboard_manager.add_backboard(user_uuid, user_id, course_id, lesson_name, root_path=f'/home/{user_id}/{course_id}/{chapter_name}/{lesson_name}')
chatmanager.bb = backboard_manager.get_backboard(user_uuid)
-
- def _skip_completed_chapters(self, chatmanager, continue_learn, need_skip_chapters):
- """跳过已完成章节"""
+ # 如果有保存的进度,需要跳过已经完成的章节
if continue_learn:
for _ in range(need_skip_chapters):
chatmanager.next_chapter()
-
- def _register_ase_routes(self, user_uuid, user_uuid2ase_client, chatmanager):
- """注册ASE客户端的各种路由回调"""
+ self.chatmanager = chatmanager
+ self.ase_client = user_uuid2ase_client[user_uuid]
user_uuid2ase_client[user_uuid].register_on_connect_entry(
callback=on_connect_to_ase,
entry=(self, user_uuid2ase_client[user_uuid]),
- init_data={'room': user_uuid, 'restart': chatmanager.restart}
+ init_data={'room':user_uuid, 'restart':chatmanager.restart}
)
-
- # 注册各种路由回调
- route_configs = [
- {'route': 'dialog', 'callback': receive_ase_dialog, 'entry': self},
- {'route': 'dialog-hint', 'callback': receive_ase_message_hint, 'entry': self},
- {'route': 'voice_out', 'callback': receive_ase_voice_out, 'entry': self},
- {'route': 'judge', 'callback': receive_ase_judge, 'entry': self},
- {'route': 'next_chapter', 'callback': receive_ase_next_chapter, 'entry': self},
- {'route': 'chapter_score', 'callback': receive_ase_chapter_score, 'entry': (get_or_load_current_user(), chatmanager)},
- {'route': 'process', 'callback': receive_ase_process, 'entry': self},
- {'route': 'paste_detected', 'callback': receive_ase_paste_detected, 'entry': self}
- ]
-
- for config in route_configs:
- user_uuid2ase_client[user_uuid].register_route_with_entry(
- route=config['route'],
- callback=config['callback'],
- entry=config['entry'],
- init_data={'room': user_uuid}
- )
-
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='dialog',
+ callback=receive_ase_dialog,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='dialog-hint',
+ callback=receive_ase_message_hint,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='voice_out',
+ callback=receive_ase_voice_out,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='judge',
+ callback=receive_ase_judge,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='next_chapter',
+ callback=receive_ase_next_chapter,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='chapter_score',
+ callback=receive_ase_chapter_score,
+ entry=(get_or_load_current_user(),chatmanager),
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='process',
+ callback=receive_ase_process,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
+ user_uuid2ase_client[user_uuid].register_route_with_entry(
+ route='paste_detected',
+ callback=receive_ase_paste_detected,
+ entry=self,
+ init_data={'room': user_uuid}
+ )
user_uuid2ase_client[user_uuid].connect()
-
- def on_login(self, data):
- """登录处理主方法"""
- self.app = current_app
- ex = current_app.extensions
- uuid2username = ex["uuid2username"]
- backboard_manager = ex["backboard_manager"]
-
- # 1. 解析登录数据
- login_data = self._parse_login_data(data)
- user_uuid = login_data['user_uuid']
- course_id = login_data['course_id']
- lesson_name = login_data['lesson_name']
- chapter_name = login_data['chapter_name']
-
- # 2. 设置用户会话和加入房间
- user_id = self._setup_user_session(user_uuid, uuid2username)
-
- # 3. 获取聊天管理器
- user_uuid2chatmanager = ex["user_uuid2chatmanager"]
- chatmanager = user_uuid2chatmanager[user_uuid]
-
- # 4. 管理ASE客户端连接
- user_uuid2ase_client = self._manage_ase_client_connection(user_uuid, ex)
-
- # 5. 配置聊天管理器的函数
- self._configure_chatmanager_functions(chatmanager, course_id, user_id, chapter_name, lesson_name, user_uuid)
-
- # 6. 加载和恢复学习进度
- continue_learn, need_skip_chapters = self._load_and_restore_progress(chatmanager, user_id, course_id, chapter_name, lesson_name)
-
- # 7. 初始化聊天管理器
- user_uuid2ase_client = self._initialize_chatmanager(user_uuid, user_id, course_id, chapter_name, lesson_name, chatmanager, user_uuid2ase_client)
-
- # 8. 设置背板管理器
- self._setup_backboard(user_uuid, user_id, course_id, lesson_name, chatmanager, backboard_manager)
-
- # 9. 跳过已完成章节
- self._skip_completed_chapters(chatmanager, continue_learn, need_skip_chapters)
-
- # 10. 保存引用
- self.chatmanager = chatmanager
- self.ase_client = user_uuid2ase_client[user_uuid]
-
- # 11. 注册ASE路由
- self._register_ase_routes(user_uuid, user_uuid2ase_client, chatmanager)
def on_language(self, language):