history ng patch 1

This commit is contained in:
binary-husky
2024-12-21 11:27:53 +08:00
parent 17b5c22e61
commit 4ec87fbb54
2 changed files with 29 additions and 21 deletions

View File

@@ -940,17 +940,17 @@ function gpt_academic_gradio_saveload(
function update_conversation_metadata() { function update_conversation_metadata() {
// Create a conversation UUID and timestamp // Create a conversation UUID and timestamp
try {
const conversationId = crypto.randomUUID(); const conversationId = crypto.randomUUID();
const timestamp = new Date().toISOString(); const timestamp = new Date().toISOString();
const conversationData = { const conversationData = {
id: conversationId, id: conversationId,
timestamp: timestamp timestamp: timestamp
}; };
// Save to cookie
setCookie("conversation_metadata", JSON.stringify(conversationData), 2); setCookie("conversation_metadata", JSON.stringify(conversationData), 2);
// read from cookie } catch (e) {
let conversation_metadata = getCookie("conversation_metadata"); console.error('Error in updating conversation metadata:', e);
// console.log("conversation_metadata", conversation_metadata); }
} }
@@ -970,9 +970,10 @@ async function save_conversation_history() {
let chatbot = await get_data_from_gradio_component('gpt-chatbot'); let chatbot = await get_data_from_gradio_component('gpt-chatbot');
let history = await get_data_from_gradio_component('history-ng'); let history = await get_data_from_gradio_component('history-ng');
let conversation_metadata = getCookie("conversation_metadata"); let conversation_metadata = getCookie("conversation_metadata");
let conversation = {};
try {
conversation_metadata = JSON.parse(conversation_metadata); conversation_metadata = JSON.parse(conversation_metadata);
// console.log("conversation_metadata", conversation_metadata); conversation = {
let conversation = {
timestamp: conversation_metadata.timestamp, timestamp: conversation_metadata.timestamp,
id: conversation_metadata.id, id: conversation_metadata.id,
metadata: conversation_metadata, metadata: conversation_metadata,
@@ -980,6 +981,12 @@ async function save_conversation_history() {
history: history, history: history,
preview: generatePreview(JSON.parse(history), conversation_metadata.timestamp) preview: generatePreview(JSON.parse(history), conversation_metadata.timestamp)
}; };
} catch (e) {
console.log(history);
console.error('Conversation metadata parse error');
update_conversation_metadata();
return;
}
// Get existing conversation history from local storage // Get existing conversation history from local storage
let conversation_history = []; let conversation_history = [];

View File

@@ -178,6 +178,7 @@ def update_ui(chatbot:ChatBotWithCookies, history:list, msg:str="正常", **kwar
else: else:
chatbot_gr = chatbot chatbot_gr = chatbot
history = [str(history_item) for history_item in history] # ensure all items are string
json_history = json.dumps(history, ensure_ascii=False) json_history = json.dumps(history, ensure_ascii=False)
yield cookies, chatbot_gr, json_history, msg yield cookies, chatbot_gr, json_history, msg