history 重构
This commit is contained in:
@@ -70,6 +70,9 @@ class ChatManager:
|
|||||||
self.chapter_chain = load_chapters(raw_markdown, raw_markdown_prompts, raw_score_prompts)
|
self.chapter_chain = load_chapters(raw_markdown, raw_markdown_prompts, raw_score_prompts)
|
||||||
self.bb = None
|
self.bb = None
|
||||||
self.chat_historys = []
|
self.chat_historys = []
|
||||||
|
self.chapter_chain_now = -1
|
||||||
|
self.scores = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def send_to_vscode(self, event: str, data: dict, room: str = None, namespace: str = '/vscode'):
|
def send_to_vscode(self, event: str, data: dict, room: str = None, namespace: str = '/vscode'):
|
||||||
|
|||||||
@@ -33,10 +33,15 @@ def on_connect_to_ase(client, entry, init_data, **kwargs):
|
|||||||
namespace_entry.emit('message', "Agents服务已连接, 加载教案中", room=init_data['room'], namespace='/agent')
|
namespace_entry.emit('message', "Agents服务已连接, 加载教案中", room=init_data['room'], namespace='/agent')
|
||||||
|
|
||||||
restart = init_data.get("restart", False)
|
restart = init_data.get("restart", False)
|
||||||
if restart: # 重启时,从-1章进入 0章
|
# 不再需要手动next_chapter,因为已经在on_login中根据mongo进度恢复了正确的章节
|
||||||
ase_client.chatmanager.next_chapter()
|
# if restart: # 重启时,从-1章进入 0章
|
||||||
|
# ase_client.chatmanager.next_chapter()
|
||||||
|
|
||||||
|
# 根据restart决定是否重新加载代码
|
||||||
ase_client.chatmanager.load_now_chapter(load_code=restart)
|
ase_client.chatmanager.load_now_chapter(load_code=restart)
|
||||||
print("load_now_chapter with restart:", restart)
|
print("load_now_chapter with restart:", restart)
|
||||||
|
|
||||||
|
# 只有当需要restart时才发送chapter-start
|
||||||
if restart:
|
if restart:
|
||||||
ase_client.send_text("chapter-start", "")
|
ase_client.send_text("chapter-start", "")
|
||||||
namespace_entry.emit('message', "教案加载完毕", room=init_data['room'], namespace='/agent')
|
namespace_entry.emit('message', "教案加载完毕", room=init_data['room'], namespace='/agent')
|
||||||
|
|||||||
@@ -72,22 +72,20 @@ class AgentNamespace(Namespace):
|
|||||||
course_id: 课程ID
|
course_id: 课程ID
|
||||||
chapter_name: 章节名称
|
chapter_name: 章节名称
|
||||||
lesson_name: 课时名称
|
lesson_name: 课时名称
|
||||||
continue_learn: 是否继续学习
|
continue_learn: 是否继续学习(现在总是为True,保持兼容性)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
int: 需要跳过的章节数
|
int: 需要跳过的章节数
|
||||||
"""
|
"""
|
||||||
need_skip_chapters = 0
|
need_skip_chapters = 0
|
||||||
|
|
||||||
if continue_learn: # 尝试加载用户学习进度
|
# 总是从mongo加载学习进度
|
||||||
progress_result = load_learning_progress(user_id, course_id, chapter_name, lesson_name)
|
progress_result = load_learning_progress(user_id, course_id, chapter_name, lesson_name)
|
||||||
|
|
||||||
if progress_result['exists']:
|
if progress_result['exists']:
|
||||||
progress = progress_result['data']
|
progress = progress_result['data']
|
||||||
# 恢复学习进度数据
|
# 恢复学习进度数据
|
||||||
if 'scores' in progress:
|
if 'scores' in progress:
|
||||||
# 根据scores的数量确定需要跳过的章节数
|
|
||||||
need_skip_chapters = len(progress['scores'])
|
|
||||||
chatmanager.scores = progress['scores']
|
chatmanager.scores = progress['scores']
|
||||||
|
|
||||||
# 恢复聊天历史
|
# 恢复聊天历史
|
||||||
@@ -100,10 +98,15 @@ class AgentNamespace(Namespace):
|
|||||||
if 'chapter_chain_now' in progress:
|
if 'chapter_chain_now' in progress:
|
||||||
chatmanager.chapter_chain_now = progress['chapter_chain_now']
|
chatmanager.chapter_chain_now = progress['chapter_chain_now']
|
||||||
upload_learning_progress_to_cloud(progress)
|
upload_learning_progress_to_cloud(progress)
|
||||||
|
|
||||||
|
# 计算需要跳过的章节数:从初始的-1到当前的chapter_chain_now
|
||||||
|
need_skip_chapters = chatmanager.chapter_chain_now + 1
|
||||||
else:
|
else:
|
||||||
# 使用默认值,确保有完整的结构
|
# 使用默认值,确保有完整的结构
|
||||||
chatmanager.chat_historys = progress_result['data']['chat_historys']
|
chatmanager.chat_historys = progress_result['data']['chat_historys']
|
||||||
chatmanager.scores = progress_result['data']['scores']
|
chatmanager.scores = progress_result['data']['scores']
|
||||||
|
chatmanager.chapter_chain_now = -1
|
||||||
|
need_skip_chapters = 0
|
||||||
|
|
||||||
return need_skip_chapters
|
return need_skip_chapters
|
||||||
|
|
||||||
@@ -131,8 +134,9 @@ class AgentNamespace(Namespace):
|
|||||||
chatmanager.function_manager.add_function(next_chapter, {'user_uuid': user_uuid})
|
chatmanager.function_manager.add_function(next_chapter, {'user_uuid': user_uuid})
|
||||||
|
|
||||||
clear_user_session(current_app.config["ASE_ENGINE_URL"], current_app.config["ASE_ENGINE_URL_TOKEN"], user_id)
|
clear_user_session(current_app.config["ASE_ENGINE_URL"], current_app.config["ASE_ENGINE_URL_TOKEN"], user_id)
|
||||||
continue_learn = not chatmanager.restart
|
|
||||||
need_skip_chapters = self._load_learning_progress(chatmanager, user_id, course_id, chapter_name, lesson_name, continue_learn)
|
# 总是从mongo加载进度,不管restart状态
|
||||||
|
need_skip_chapters = self._load_learning_progress(chatmanager, user_id, course_id, chapter_name, lesson_name, True)
|
||||||
|
|
||||||
fmd,fmdp,fsp = load_full_markdown_file(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)
|
user_uuid2ase_client[user_uuid] = HSAEngineClient(current_app.config["ASE_ENGINE_URL"], current_app.config["ASE_ENGINE_NAMESPACE"],id=user_id, chatmanager = chatmanager)
|
||||||
@@ -141,8 +145,8 @@ class AgentNamespace(Namespace):
|
|||||||
backboard_manager.add_backboard(user_uuid, user_id, course_id,
|
backboard_manager.add_backboard(user_uuid, user_id, course_id,
|
||||||
lesson_name, root_path=f'/home/{user_id}/{course_id}/{chapter_name}/{lesson_name}')
|
lesson_name, root_path=f'/home/{user_id}/{course_id}/{chapter_name}/{lesson_name}')
|
||||||
chatmanager.bb = backboard_manager.get_backboard(user_uuid)
|
chatmanager.bb = backboard_manager.get_backboard(user_uuid)
|
||||||
# 如果有保存的进度,需要跳过已经完成的章节、无需清空代码、对话历史恢复
|
|
||||||
if continue_learn:
|
# 总是根据进度跳过章节,恢复对话历史
|
||||||
for _ in range(need_skip_chapters):
|
for _ in range(need_skip_chapters):
|
||||||
chatmanager.next_chapter()
|
chatmanager.next_chapter()
|
||||||
emit('next_chapter', room=user_uuid, namespace='/agent')
|
emit('next_chapter', room=user_uuid, namespace='/agent')
|
||||||
@@ -152,10 +156,15 @@ class AgentNamespace(Namespace):
|
|||||||
|
|
||||||
self.chatmanager = chatmanager
|
self.chatmanager = chatmanager
|
||||||
self.ase_client = user_uuid2ase_client[user_uuid]
|
self.ase_client = user_uuid2ase_client[user_uuid]
|
||||||
|
|
||||||
|
# 将restart设置为False,避免后续重复处理
|
||||||
|
restart = chatmanager.restart
|
||||||
|
chatmanager.restart = False
|
||||||
|
|
||||||
user_uuid2ase_client[user_uuid].register_on_connect_entry(
|
user_uuid2ase_client[user_uuid].register_on_connect_entry(
|
||||||
callback=on_connect_to_ase,
|
callback=on_connect_to_ase,
|
||||||
entry=(self, user_uuid2ase_client[user_uuid]),
|
entry=(self, user_uuid2ase_client[user_uuid]),
|
||||||
init_data={'room':user_uuid, 'restart':chatmanager.restart}
|
init_data={'room':user_uuid, 'restart':restart}
|
||||||
)
|
)
|
||||||
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
user_uuid2ase_client[user_uuid].register_route_with_entry(
|
||||||
route='dialog',
|
route='dialog',
|
||||||
|
|||||||
@@ -69,8 +69,23 @@ def desktop(user_uuid, course_id, chapter_name, lesson_name):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
chatmanager = ChatManager(restart=not load_history)
|
restart = not load_history
|
||||||
|
chatmanager = ChatManager(restart=restart)
|
||||||
current_app.extensions["user_uuid2chatmanager"][user_uuid] = chatmanager
|
current_app.extensions["user_uuid2chatmanager"][user_uuid] = chatmanager
|
||||||
|
|
||||||
|
# 如果restart为True,删除mongo中的学习进度
|
||||||
|
if restart:
|
||||||
|
try:
|
||||||
|
mongo = current_app.extensions["mongo"]
|
||||||
|
mongo.db.learning_progress.delete_one({
|
||||||
|
"user_id": user_id,
|
||||||
|
"material_id": course_id,
|
||||||
|
"chapter_name": chapter_name,
|
||||||
|
"lesson_name": lesson_name
|
||||||
|
})
|
||||||
|
current_app.logger.info(f"删除学习进度成功: user_id={user_id}, course_id={course_id}, chapter={chapter_name}, lesson={lesson_name}")
|
||||||
|
except Exception as e:
|
||||||
|
current_app.logger.error(f"删除学习进度失败: {str(e)}")
|
||||||
code_server_port = 10000 + int(uuid.uuid4().int % 10000)
|
code_server_port = 10000 + int(uuid.uuid4().int % 10000)
|
||||||
# chatmanager.start_code_server(user_uuid, user_id, path_dir, code_server_port)
|
# chatmanager.start_code_server(user_uuid, user_id, path_dir, code_server_port)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user