diff --git a/Html/apps/extension_ase/ase_client/manager.py b/Html/apps/extension_ase/ase_client/manager.py index 583b642..1af1639 100644 --- a/Html/apps/extension_ase/ase_client/manager.py +++ b/Html/apps/extension_ase/ase_client/manager.py @@ -129,10 +129,11 @@ class ChatManager: self.chapter_chain_now = chapter_index self.chapter_chain[chapter_index].Focus() - def load_now_chapter(self): + def load_now_chapter(self, load_code = True): print(f"now load now chapter markdown {self.chapter_chain_now}") self.ase_client.loadmarkdown(self.chapter_chain[self.chapter_chain_now].chapter, self.chapter_chain[self.chapter_chain_now].chapter_prompt, self.chapter_chain[self.chapter_chain_now].score_prompt) - self.load_code_in_markdown() + if load_code: + self.load_code_in_markdown() def upload_bb(self): diff --git a/Html/apps/services/asectrl_service.py b/Html/apps/services/asectrl_service.py index f7ca0a9..5505f7f 100644 --- a/Html/apps/services/asectrl_service.py +++ b/Html/apps/services/asectrl_service.py @@ -34,8 +34,8 @@ def on_connect_to_ase(client, entry, init_data, **kwargs): clear_user_session(namespace_entry, init_data['room']) restart = init_data.get("restart", False) + ase_client.chatmanager.load_now_chapter(load_code=restart) if restart: - ase_client.chatmanager.load_now_chapter() ase_client.send_text("chapter-start", "") namespace_entry.emit('message', "教案加载完毕", room=init_data['room'], namespace='/agent') diff --git a/Html/apps/sockets/namespaces.py b/Html/apps/sockets/namespaces.py index c385e59..c1fa2f7 100644 --- a/Html/apps/sockets/namespaces.py +++ b/Html/apps/sockets/namespaces.py @@ -136,10 +136,15 @@ class AgentNamespace(Namespace): 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) - # 如果有保存的进度,需要跳过已经完成的章节 + # 如果有保存的进度,需要跳过已经完成的章节、无需清空代码、对话历史恢复 if continue_learn: for _ in range(need_skip_chapters): chatmanager.next_chapter() + + # 恢复对话历史到前端 - 使用批量发送消息列表 + if chatmanager.chat_historys: + emit('messages', chatmanager.chat_historys, room=user_uuid, namespace='/agent') + self.chatmanager = chatmanager self.ase_client = user_uuid2ase_client[user_uuid] user_uuid2ase_client[user_uuid].register_on_connect_entry( diff --git a/Html/apps/static/js/chatbox.js b/Html/apps/static/js/chatbox.js index 7ba5155..346c07b 100644 --- a/Html/apps/static/js/chatbox.js +++ b/Html/apps/static/js/chatbox.js @@ -2,6 +2,57 @@ var socket; let system_message_idx = 0 const activeApprovals = {}; +let chatManager; // 全局聊天管理器实例 + +// 聊天消息管理器 - 更现代的数据结构 +class ChatMessageManager { + constructor() { + this.chatbox = document.getElementById('chatbox'); + this.messageTemplate = { + user: `
`, + assistant: `