var currentChatName = null;
function setChatListHeader() {
var grHistoryRefreshBtn = gradioApp().querySelector('button#gr-history-refresh-btn');
var grHistoryUploadBtn = gradioApp().querySelector('button#gr-history-upload-btn');
grHistoryRefreshBtn.className = "";
grHistoryUploadBtn.className = "";
grHistoryRefreshBtn.innerHTML = HistoryRefreshIcon;
grHistoryUploadBtn.innerHTML = HistoryUploadIcon;
}
function setChatList() {
var selectedChat = null;
var chatList = gradioApp().querySelector('fieldset#history-select-dropdown');
selectedChat = chatList.querySelector("label.selected")
if (!selectedChat) {
currentChatName = null;
return;
}
// if (userLogged) {
// currentChatName = username + "/" + selectedChat.querySelector('span').innerText;
// } else {
currentChatName = selectedChat.querySelector('span').innerText;
// }
if (selectedChat.classList.contains('added-chat-btns')) {
return;
}
chatList.querySelector('.chat-selected-btns')?.remove(); // remove old buttons
chatList.querySelectorAll('.added-chat-btns').forEach(chat => chat.classList.remove('added-chat-btns'));
var ChatSelectedBtns = document.createElement('div');
ChatSelectedBtns.classList.add('chat-selected-btns');
selectedChat.classList.add('added-chat-btns');
ChatSelectedBtns.innerHTML = selectedChatBtns;
var renameBtn = ChatSelectedBtns.querySelector('#history-rename-btn');
renameBtn.addEventListener('click', function () {
gradioApp().querySelector('#gr-history-save-btn').click();
});
var deleteBtn = ChatSelectedBtns.querySelector('#history-delete-btn');
deleteBtn.addEventListener('click', function () {
gradioApp().querySelector('#gr-history-delete-btn').click();
});
selectedChat.appendChild(ChatSelectedBtns);
return;
}
function saveChatHistory(a, b, c, d) {
var fileName = b;
while (true) {
var result = prompt(renameChat_i18n, fileName);
if (result === null) {
throw new Error("rename operation cancelled");
// 不返回原文件名,而是使用 throw new Error() 打断程序,避免 gradio 进行保存操作
// break;
} else if (isValidFileName(result)) {
return [a, result, c, d];
} else {
alert(validFileName_i18n + "!@#$%^&*()<>?/\\|}{~:");
}
}
return [a, b, c, d]; // 兜底保障
}
function isValidFileName(fileName) {
// 使用正则表达式来检查文件名是否包含不合格字符
var regex = /[!@#$%^&*()<>?/\\|}{~:]/;
return !regex.test(fileName) && fileName.trim() !== "";
}
const selectedChatBtns = `
`
const HistoryRefreshIcon = '';
const HistoryUploadIcon = '';