Add bulk and drag&drop to chats import (#4652)

* Add bulk and drag-drop to chats import

* Perform list refresh only once
This commit is contained in:
Cohee
2025-10-15 00:43:30 +03:00
committed by GitHub
parent 0748af59e3
commit 3234045d64
3 changed files with 46 additions and 44 deletions
+1 -1
View File
@@ -6248,7 +6248,7 @@
<div name="selectChatPopupHeader" class="flex-container alignitemscenter justifySpaceBetween flexGap10">
<div id="select_chat_import"> <!-- import chat popup header -->
<form id="form_import_chat" action="javascript:void(null);" method="post" enctype="multipart/form-data" style="display: none;">
<input type="file" id="chat_import_file" accept=".json, .jsonl" name="avatar">
<input type="file" id="chat_import_file" accept=".json, .jsonl" multiple name="avatar">
<input id="chat_import_file_type" name="file_type" class="text_pole" value="" autocomplete="off" style="display: none;">
<input id="chat_import_avatar_url" name="avatar_url" class="text_pole" value="" autocomplete="off" style="display: none;">
<input id="chat_import_character_name" name="character_name" class="text_pole" value="" autocomplete="off" style="display: none;">
+39 -36
View File
@@ -409,6 +409,7 @@ let fav_ch_checked = false;
let scrollLock = false;
export let abortStatusCheck = new AbortController();
export let charDragDropHandler = null;
export let chatDragDropHandler = null;
/** @type {debounce_timeout} The debounce timeout used for chat/settings save. debounce_timeout.long: 1.000 ms */
export const DEFAULT_SAVE_EDIT_TIMEOUT = debounce_timeout.relaxed;
@@ -8050,9 +8051,10 @@ export async function saveChatConditional() {
/**
* Saves the chat to the server.
* @param {FormData} formData Form data to send to the server.
* @param {EventTarget} eventTarget Event target to trigger the event on.
* @param {object} [options={}] Options for the import
* @param {boolean} [options.refresh] Whether to refresh the group chat list after import
*/
async function importCharacterChat(formData, eventTarget) {
async function importCharacterChat(formData, { refresh = true } = {}) {
const fetchResult = await fetch('/api/chats/import', {
method: 'POST',
body: formData,
@@ -8062,14 +8064,10 @@ async function importCharacterChat(formData, eventTarget) {
if (fetchResult.ok) {
const data = await fetchResult.json();
if (data.res) {
if (data.res && refresh) {
await displayPastChats();
}
}
if (eventTarget instanceof HTMLInputElement) {
eventTarget.value = '';
}
}
function updateViewMessageIds(startFromZero = false) {
@@ -10612,41 +10610,38 @@ jQuery(async function () {
});
$('#chat_import_file').on('change', async function (e) {
const targetElement = /** @type {HTMLInputElement} */ (e.target);
if (!(targetElement instanceof HTMLInputElement)) {
return;
}
const file = targetElement.files[0];
if (!file) {
const targetElement = e.target;
const formElement = document.getElementById('form_import_chat');
if (!(targetElement instanceof HTMLInputElement) || !(formElement instanceof HTMLFormElement)) {
return;
}
const ext = file.name.match(/\.(\w+)$/);
if (
!ext ||
(ext[1].toLowerCase() != 'json' && ext[1].toLowerCase() != 'jsonl')
) {
return;
for (const file of targetElement.files) {
const ext = file.name.match(/\.(\w+)$/);
const format = ext?.[1]?.toLowerCase();
if (!['json', 'jsonl'].includes(format)) {
toastr.warning(t`Only JSON and JSONL files are supported for chat imports.`);
continue;
}
if (selected_group && format === 'json') {
toastr.warning(t`Only SillyTavern's own format is supported for group chat imports. Sorry!`);
continue;
}
const formData = new FormData(formElement);
formData.set('file_type', format);
formData.set('avatar', file);
formData.set('user_name', name1);
const importFn = selected_group ? importGroupChat : importCharacterChat;
await importFn(formData, { refresh: false });
}
if (selected_group && file.name.endsWith('.json')) {
toastr.warning('Only SillyTavern\'s own format is supported for group chat imports. Sorry!');
return;
}
await displayPastChats();
const format = ext[1].toLowerCase();
$('#chat_import_file_type').val(format);
const formData = new FormData(/** @type {HTMLFormElement} */($('#form_import_chat').get(0)));
formData.append('user_name', name1);
$('#select_chat_div').html('');
if (selected_group) {
await importGroupChat(formData, e.originalEvent.target);
} else {
await importCharacterChat(formData, e.originalEvent.target);
}
targetElement.value = '';
});
$('#rm_button_group_chats').on('click', function () {
@@ -11092,6 +11087,14 @@ jQuery(async function () {
await processDroppedFiles(files);
}, { noAnimation: true });
chatDragDropHandler = new DragAndDropHandler('#select_chat_popup', async (_, event) => {
const importFile = document.getElementById('chat_import_file');
if (importFile instanceof HTMLInputElement) {
importFile.files = event.originalEvent.dataTransfer.files;
$(importFile).trigger('change');
}
});
$('#charListGridToggle').on('click', async () => {
doCharListDisplaySwitch();
});
+6 -7
View File
@@ -2064,9 +2064,10 @@ export async function deleteGroupChat(groupId, chatId, { jumpToNewChat = true }
/**
* Imports a group chat from a file and adds it to the group.
* @param {FormData} formData Form data to send to the server
* @param {EventTarget} eventTarget Element that triggered the import
* @param {object} [options={}] Options for the import
* @param {boolean} [options.refresh] Whether to refresh the group chat list after import
*/
export async function importGroupChat(formData, eventTarget) {
export async function importGroupChat(formData, { refresh = true } = {}) {
const fetchResult = await fetch('/api/chats/group/import', {
method: 'POST',
headers: getRequestHeaders({ omitContentType: true }),
@@ -2083,14 +2084,12 @@ export async function importGroupChat(formData, eventTarget) {
if (group) {
group.chats.push(chatId);
await editGroup(selected_group, true, true);
await displayPastChats();
if (refresh) {
await displayPastChats();
}
}
}
}
if (eventTarget instanceof HTMLInputElement) {
eventTarget.value = '';
}
}
export async function saveGroupBookmarkChat(groupId, name, metadata, mesId) {