Export a function for renaming an active chat

This commit is contained in:
Cohee 2024-05-14 21:15:01 +03:00
parent 3113109f0a
commit e2e7d5870a
1 changed files with 61 additions and 51 deletions

View File

@ -7566,6 +7566,7 @@ window['SillyTavern'].getContext = function () {
getCurrentChatId: getCurrentChatId, getCurrentChatId: getCurrentChatId,
getRequestHeaders: getRequestHeaders, getRequestHeaders: getRequestHeaders,
reloadCurrentChat: reloadCurrentChat, reloadCurrentChat: reloadCurrentChat,
renameChat: renameChat,
saveSettingsDebounced: saveSettingsDebounced, saveSettingsDebounced: saveSettingsDebounced,
onlineStatus: online_status, onlineStatus: online_status,
maxContext: Number(max_context), maxContext: Number(max_context),
@ -8288,6 +8289,58 @@ async function doDeleteChat() {
$('#dialogue_popup_ok').trigger('click', { fromSlashCommand: true }); $('#dialogue_popup_ok').trigger('click', { fromSlashCommand: true });
} }
/**
* Renames the currently selected chat.
* @param {string} oldFileName Old name of the chat (no JSONL extension)
* @param {string} newName New name for the chat (no JSONL extension)
*/
export async function renameChat(oldFileName, newName) {
const body = {
is_group: !!selected_group,
avatar_url: characters[this_chid]?.avatar,
original_file: `${oldFileName}.jsonl`,
renamed_file: `${newName}.jsonl`,
};
try {
showLoader();
const response = await fetch('/api/chats/rename', {
method: 'POST',
body: JSON.stringify(body),
headers: getRequestHeaders(),
});
if (!response.ok) {
throw new Error('Unsuccessful request.');
}
const data = await response.json();
if (data.error) {
throw new Error('Server returned an error.');
}
if (selected_group) {
await renameGroupChat(selected_group, oldFileName, newName);
}
else {
if (characters[this_chid].chat == oldFileName) {
characters[this_chid].chat = newName;
$('#selected_chat_pole').val(characters[this_chid].chat);
await createOrEditCharacter();
}
}
await reloadCurrentChat();
} catch {
hideLoader();
await delay(500);
await callPopup('An error has occurred. Chat was not renamed.', 'text');
} finally {
hideLoader();
}
}
/** /**
* /getchatname` slash command * /getchatname` slash command
*/ */
@ -8966,69 +9019,26 @@ jQuery(async function () {
$(document).on('click', '.renameChatButton', async function (e) { $(document).on('click', '.renameChatButton', async function (e) {
e.stopPropagation(); e.stopPropagation();
const old_filenamefull = $(this).closest('.select_chat_block_wrapper').find('.select_chat_block_filename').text(); const oldFileNameFull = $(this).closest('.select_chat_block_wrapper').find('.select_chat_block_filename').text();
const old_filename = old_filenamefull.replace('.jsonl', ''); const oldFileName = oldFileNameFull.replace('.jsonl', '');
const popupText = `<h3>Enter the new name for the chat:<h3> const popupText = `<h3>Enter the new name for the chat:<h3>
<small>!!Using an existing filename will produce an error!!<br> <small>!!Using an existing filename will produce an error!!<br>
This will break the link between checkpoint chats.<br> This will break the link between checkpoint chats.<br>
No need to add '.jsonl' at the end.<br> No need to add '.jsonl' at the end.<br>
</small>`; </small>`;
const newName = await callPopup(popupText, 'input', old_filename); const newName = await callPopup(popupText, 'input', oldFileName);
if (!newName || newName == old_filename) { if (!newName || newName == oldFileName) {
console.log('no new name found, aborting'); console.log('no new name found, aborting');
return; return;
} }
const body = { await renameChat(oldFileName, newName);
is_group: !!selected_group,
avatar_url: characters[this_chid]?.avatar,
original_file: `${old_filename}.jsonl`,
renamed_file: `${newName}.jsonl`,
};
try { await delay(250);
showLoader(); $('#option_select_chat').trigger('click');
const response = await fetch('/api/chats/rename', { $('#options').hide();
method: 'POST',
body: JSON.stringify(body),
headers: getRequestHeaders(),
});
if (!response.ok) {
throw new Error('Unsuccessful request.');
}
const data = await response.json();
if (data.error) {
throw new Error('Server returned an error.');
}
if (selected_group) {
await renameGroupChat(selected_group, old_filename, newName);
}
else {
if (characters[this_chid].chat == old_filename) {
characters[this_chid].chat = newName;
$('#selected_chat_pole').val(characters[this_chid].chat);
await createOrEditCharacter();
}
}
await reloadCurrentChat();
await delay(250);
$('#option_select_chat').trigger('click');
$('#options').hide();
} catch {
hideLoader();
await delay(500);
await callPopup('An error has occurred. Chat was not renamed.', 'text');
} finally {
hideLoader();
}
}); });
$(document).on('click', '.exportChatButton, .exportRawChatButton', async function (e) { $(document).on('click', '.exportChatButton, .exportRawChatButton', async function (e) {