From 5d1f3b13ea3ed8f573496e991ee1a2f4ea3d8f46 Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Wed, 7 Feb 2024 22:51:41 +0200 Subject: [PATCH 1/4] add /getchatname slash command to get name of current chat file Example: /getchatname | /echo {{pipe}} --- public/script.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/public/script.js b/public/script.js index 908714892..d732f3c25 100644 --- a/public/script.js +++ b/public/script.js @@ -7824,6 +7824,12 @@ async function doDeleteChat() { $('#dialogue_popup_ok').trigger('click', { fromSlashCommand: true }); } +async function doGetChatName() { // `/getchatname` slash command + const group = selected_group ? groups.find(x => x.id === selected_group) : null; + const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; + return currentChat; +} + const isPwaMode = window.navigator.standalone; if (isPwaMode) { $('body').addClass('PWA'); } @@ -7975,6 +7981,7 @@ jQuery(async function () { registerSlashCommand('api', connectAPISlash, [], `(${Object.keys(CONNECT_API_MAP).join(', ')}) – connect to an API`, true, true); registerSlashCommand('impersonate', doImpersonate, ['imp'], '– calls an impersonation response', true, true); registerSlashCommand('delchat', doDeleteChat, [], '– deletes the current chat', true, true); + registerSlashCommand('getchatname', doGetChatName, [], '- gets the name of the current chat file', false, true); registerSlashCommand('closechat', doCloseChat, [], '– closes the current chat', true, true); registerSlashCommand('panels', doTogglePanels, ['togglepanels'], '– toggle UI panels on/off', true, true); registerSlashCommand('forcesave', doForceSave, [], '– forces a save of the current chat and settings', true, true); From 5183fb40a245d3e4ef8362d304acf9c05fd03f7b Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Wed, 7 Feb 2024 23:09:51 +0200 Subject: [PATCH 2/4] refactor to improve proposed implementation of /getchatname --- public/script.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/public/script.js b/public/script.js index d732f3c25..297e9ac0a 100644 --- a/public/script.js +++ b/public/script.js @@ -6089,6 +6089,14 @@ export async function getPastCharacterChats(characterId = null) { return data; } +function getCurrentChatDetails() { // helper for `displayPastChats`, to make the same info consistently available for other functions + const group = selected_group ? groups.find(x => x.id === selected_group) : null; + const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; + const displayName = selected_group ? group?.name : characters[this_chid].name; + const avatarImg = selected_group ? group?.avatar_url : getThumbnailUrl('avatar', characters[this_chid]['avatar']); + return { sessionName: currentChat, group: group, characterName: displayName, avatarImgURL: avatarImg }; +} + /** * Displays the past chats for a character or a group based on the selected context. * The function first fetches the chats, processes them, and then displays them in @@ -6099,7 +6107,6 @@ export async function displayPastChats() { $('#select_chat_div').empty(); $('#select_chat_search').val('').off('input'); - const group = selected_group ? groups.find(x => x.id === selected_group) : null; const data = await (selected_group ? getGroupPastChats(selected_group) : getPastCharacterChats()); if (!data) { @@ -6107,10 +6114,14 @@ export async function displayPastChats() { return; } - const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; - const displayName = selected_group ? group?.name : characters[this_chid].name; - const avatarImg = selected_group ? group?.avatar_url : getThumbnailUrl('avatar', characters[this_chid]['avatar']); + const chatDetails = getCurrentChatDetails(); + const group = chatDetails.group; + const currentChat = chatDetails.sessionName; + const displayName = chatDetails.characterName; + const avatarImg = chatDetails.avatarImgURL; + const rawChats = await getChatsFromFiles(data, selected_group); + // Sort by last message date descending data.sort((a, b) => sortMoments(timestampToMoment(a.last_mes), timestampToMoment(b.last_mes))); console.log(data); @@ -7825,9 +7836,7 @@ async function doDeleteChat() { } async function doGetChatName() { // `/getchatname` slash command - const group = selected_group ? groups.find(x => x.id === selected_group) : null; - const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; - return currentChat; + return getCurrentChatDetails().sessionName; } const isPwaMode = window.navigator.standalone; From c1a5b50aaeecf37177ede8d3575393da10db5012 Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Wed, 7 Feb 2024 23:12:21 +0200 Subject: [PATCH 3/4] improve description for consistency --- public/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index 297e9ac0a..f7ca8624e 100644 --- a/public/script.js +++ b/public/script.js @@ -7990,7 +7990,7 @@ jQuery(async function () { registerSlashCommand('api', connectAPISlash, [], `(${Object.keys(CONNECT_API_MAP).join(', ')}) – connect to an API`, true, true); registerSlashCommand('impersonate', doImpersonate, ['imp'], '– calls an impersonation response', true, true); registerSlashCommand('delchat', doDeleteChat, [], '– deletes the current chat', true, true); - registerSlashCommand('getchatname', doGetChatName, [], '- gets the name of the current chat file', false, true); + registerSlashCommand('getchatname', doGetChatName, [], '- returns the name of the current chat file into the pipe', false, true); registerSlashCommand('closechat', doCloseChat, [], '– closes the current chat', true, true); registerSlashCommand('panels', doTogglePanels, ['togglepanels'], '– toggle UI panels on/off', true, true); registerSlashCommand('forcesave', doForceSave, [], '– forces a save of the current chat and settings', true, true); From 04372848c87535022a1f9e77c7ab3b4515f9820d Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 7 Feb 2024 23:58:05 +0200 Subject: [PATCH 4/4] Fix for undefined chats --- public/script.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index f7ca8624e..88b6cab6c 100644 --- a/public/script.js +++ b/public/script.js @@ -6089,7 +6089,14 @@ export async function getPastCharacterChats(characterId = null) { return data; } -function getCurrentChatDetails() { // helper for `displayPastChats`, to make the same info consistently available for other functions +/** + * Helper for `displayPastChats`, to make the same info consistently available for other functions + */ +function getCurrentChatDetails() { + if (!characters[this_chid] && !selected_group) { + return { sessionName: '', group: null, characterName: '', avatarImgURL: '' }; + } + const group = selected_group ? groups.find(x => x.id === selected_group) : null; const currentChat = selected_group ? group?.chat_id : characters[this_chid]['chat']; const displayName = selected_group ? group?.name : characters[this_chid].name; @@ -7835,7 +7842,10 @@ async function doDeleteChat() { $('#dialogue_popup_ok').trigger('click', { fromSlashCommand: true }); } -async function doGetChatName() { // `/getchatname` slash command +/** + * /getchatname` slash command + */ +async function doGetChatName() { return getCurrentChatDetails().sessionName; } @@ -7990,7 +8000,7 @@ jQuery(async function () { registerSlashCommand('api', connectAPISlash, [], `(${Object.keys(CONNECT_API_MAP).join(', ')}) – connect to an API`, true, true); registerSlashCommand('impersonate', doImpersonate, ['imp'], '– calls an impersonation response', true, true); registerSlashCommand('delchat', doDeleteChat, [], '– deletes the current chat', true, true); - registerSlashCommand('getchatname', doGetChatName, [], '- returns the name of the current chat file into the pipe', false, true); + registerSlashCommand('getchatname', doGetChatName, [], '– returns the name of the current chat file into the pipe', false, true); registerSlashCommand('closechat', doCloseChat, [], '– closes the current chat', true, true); registerSlashCommand('panels', doTogglePanels, ['togglepanels'], '– toggle UI panels on/off', true, true); registerSlashCommand('forcesave', doForceSave, [], '– forces a save of the current chat and settings', true, true);