前端对话历史
This commit is contained in:
@@ -129,9 +129,10 @@ class ChatManager:
|
|||||||
self.chapter_chain_now = chapter_index
|
self.chapter_chain_now = chapter_index
|
||||||
self.chapter_chain[chapter_index].Focus()
|
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}")
|
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.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)
|
||||||
|
if load_code:
|
||||||
self.load_code_in_markdown()
|
self.load_code_in_markdown()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ def on_connect_to_ase(client, entry, init_data, **kwargs):
|
|||||||
clear_user_session(namespace_entry, init_data['room'])
|
clear_user_session(namespace_entry, init_data['room'])
|
||||||
|
|
||||||
restart = init_data.get("restart", False)
|
restart = init_data.get("restart", False)
|
||||||
|
ase_client.chatmanager.load_now_chapter(load_code=restart)
|
||||||
if restart:
|
if restart:
|
||||||
ase_client.chatmanager.load_now_chapter()
|
|
||||||
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')
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
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}')
|
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)
|
chatmanager.bb = backboard_manager.get_backboard(user_uuid)
|
||||||
# 如果有保存的进度,需要跳过已经完成的章节
|
# 如果有保存的进度,需要跳过已经完成的章节、无需清空代码、对话历史恢复
|
||||||
if continue_learn:
|
if continue_learn:
|
||||||
for _ in range(need_skip_chapters):
|
for _ in range(need_skip_chapters):
|
||||||
chatmanager.next_chapter()
|
chatmanager.next_chapter()
|
||||||
|
|
||||||
|
# 恢复对话历史到前端 - 使用批量发送消息列表
|
||||||
|
if chatmanager.chat_historys:
|
||||||
|
emit('messages', chatmanager.chat_historys, room=user_uuid, namespace='/agent')
|
||||||
|
|
||||||
self.chatmanager = chatmanager
|
self.chatmanager = chatmanager
|
||||||
self.ase_client = user_uuid2ase_client[user_uuid]
|
self.ase_client = user_uuid2ase_client[user_uuid]
|
||||||
user_uuid2ase_client[user_uuid].register_on_connect_entry(
|
user_uuid2ase_client[user_uuid].register_on_connect_entry(
|
||||||
|
|||||||
@@ -2,6 +2,57 @@
|
|||||||
var socket;
|
var socket;
|
||||||
let system_message_idx = 0
|
let system_message_idx = 0
|
||||||
const activeApprovals = {};
|
const activeApprovals = {};
|
||||||
|
let chatManager; // 全局聊天管理器实例
|
||||||
|
|
||||||
|
// 聊天消息管理器 - 更现代的数据结构
|
||||||
|
class ChatMessageManager {
|
||||||
|
constructor() {
|
||||||
|
this.chatbox = document.getElementById('chatbox');
|
||||||
|
this.messageTemplate = {
|
||||||
|
user: `<div class="chat-message user-message"><b>你:</b> {{content}}</div>`,
|
||||||
|
assistant: `<div><b>华实君:</b></div><div class="chat-message server-message"> {{content}}</div>`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染单个消息
|
||||||
|
renderMessage(message) {
|
||||||
|
let html;
|
||||||
|
let content;
|
||||||
|
|
||||||
|
// 处理不同的数据格式
|
||||||
|
if (typeof message === 'string') {
|
||||||
|
// 旧格式:直接字符串
|
||||||
|
content = marked.parse(message);
|
||||||
|
html = this.messageTemplate.assistant.replace('{{content}}', content);
|
||||||
|
} else if (typeof message === 'object' && message !== null) {
|
||||||
|
// 新格式:{role, content} 对象
|
||||||
|
content = marked.parse(message.content);
|
||||||
|
if (message.role === 'user') {
|
||||||
|
html = this.messageTemplate.user.replace('{{content}}', content);
|
||||||
|
} else {
|
||||||
|
html = this.messageTemplate.assistant.replace('{{content}}', content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (html) {
|
||||||
|
const messageDiv = document.createElement('div');
|
||||||
|
messageDiv.innerHTML = html;
|
||||||
|
this.chatbox.appendChild(messageDiv);
|
||||||
|
this.scrollToBottom();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染多条消息(用于恢复历史)
|
||||||
|
renderMessages(messages) {
|
||||||
|
messages.forEach(message => this.renderMessage(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 滚动到底部
|
||||||
|
scrollToBottom() {
|
||||||
|
this.chatbox.scrollTop = this.chatbox.scrollHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 清理过期的批准记录
|
// 清理过期的批准记录
|
||||||
function cleanupExpiredApprovals() {
|
function cleanupExpiredApprovals() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@@ -24,6 +75,10 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
folder: data.folder
|
folder: data.folder
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 初始化聊天消息管理器
|
||||||
|
const chatManager = new ChatMessageManager();
|
||||||
|
|
||||||
socket.on('connect', function () {
|
socket.on('connect', function () {
|
||||||
console.log('Connected to server');
|
console.log('Connected to server');
|
||||||
socket.emit('login',JSON.stringify(data));
|
socket.emit('login',JSON.stringify(data));
|
||||||
@@ -38,12 +93,13 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
input.value += data;
|
input.value += data;
|
||||||
});
|
});
|
||||||
socket.on('message', function (data) {
|
socket.on('message', function (data) {
|
||||||
// 显示服务器的回复消息
|
// 显示服务器的回复消息 - 单个消息
|
||||||
const serverMessage = document.createElement('div');
|
chatManager.renderMessage(data);
|
||||||
serverMessage.innerHTML = `<div><b>华实君:</b></div><div class="chat-message server-message"> ${marked.parse(data)}</div>`;
|
});
|
||||||
document.getElementById('chatbox').appendChild(serverMessage);
|
|
||||||
// 滚动到最新消息
|
socket.on('messages', function (data) {
|
||||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
// 显示服务器发送的消息列表 - 用于恢复对话历史
|
||||||
|
chatManager.renderMessages(data);
|
||||||
});
|
});
|
||||||
socket.on('message-hint', function(data){
|
socket.on('message-hint', function(data){
|
||||||
console.log('get message-hint');
|
console.log('get message-hint');
|
||||||
@@ -210,22 +266,14 @@ function sendMessage() {
|
|||||||
const input = document.getElementById('messageInput').value;
|
const input = document.getElementById('messageInput').value;
|
||||||
if (input.trim() === '') return;
|
if (input.trim() === '') return;
|
||||||
|
|
||||||
// 使用 marked.js 解析 markdown
|
|
||||||
const markdownHtml = marked.parse(input);
|
|
||||||
|
|
||||||
// 显示用户发送的消息
|
|
||||||
const userMessage = document.createElement('div');
|
|
||||||
userMessage.innerHTML = `<div class="chat-message user-message"><b>你:</b> ${markdownHtml}</div>`;
|
|
||||||
document.getElementById('chatbox').appendChild(userMessage);
|
|
||||||
|
|
||||||
// 发送消息到服务器
|
// 发送消息到服务器
|
||||||
socket.emit('message', JSON.stringify({ data: input, type: 'text' }));
|
socket.emit('message', JSON.stringify({ data: input, type: 'text' }));
|
||||||
|
|
||||||
|
// 使用聊天管理器显示用户消息
|
||||||
|
chatManager.renderMessage({ role: 'user', content: input });
|
||||||
|
|
||||||
// 清空输入框
|
// 清空输入框
|
||||||
document.getElementById('messageInput').value = '';
|
document.getElementById('messageInput').value = '';
|
||||||
|
|
||||||
// 滚动到最新消息
|
|
||||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user