Add tri-state argument for /lock command

This commit is contained in:
Cohee 2024-06-26 20:18:05 +03:00
parent 112e26a0ff
commit d9536ae3a8
2 changed files with 66 additions and 7 deletions

View File

@ -590,7 +590,37 @@ function selectCurrentPersona() {
} }
} }
async function lockUserNameToChat() { /**
* Checks if the persona is locked for the current chat.
* @returns {boolean} Whether the persona is locked
*/
function isPersonaLocked() {
return !!chat_metadata['persona'];
}
/**
* Locks or unlocks the persona for the current chat.
* @param {boolean} state Desired lock state
* @returns {Promise<void>}
*/
export async function setPersonaLockState(state) {
return state ? await lockPersona() : await unlockPersona();
}
/**
* Toggle the persona lock state for the current chat.
* @returns {Promise<void>}
*/
export async function togglePersonaLock() {
return isPersonaLocked()
? await unlockPersona()
: await lockPersona();
}
/**
* Unlock the persona for the current chat.
*/
async function unlockPersona() {
if (chat_metadata['persona']) { if (chat_metadata['persona']) {
console.log(`Unlocking persona for this chat ${chat_metadata['persona']}`); console.log(`Unlocking persona for this chat ${chat_metadata['persona']}`);
delete chat_metadata['persona']; delete chat_metadata['persona'];
@ -599,9 +629,13 @@ async function lockUserNameToChat() {
toastr.info('User persona is now unlocked for this chat. Click the "Lock" again to revert.', 'Persona unlocked'); toastr.info('User persona is now unlocked for this chat. Click the "Lock" again to revert.', 'Persona unlocked');
} }
updateUserLockIcon(); updateUserLockIcon();
return;
} }
}
/**
* Lock the persona for the current chat.
*/
async function lockPersona() {
if (!(user_avatar in power_user.personas)) { if (!(user_avatar in power_user.personas)) {
console.log(`Creating a new persona ${user_avatar}`); console.log(`Creating a new persona ${user_avatar}`);
if (power_user.persona_show_notifications) { if (power_user.persona_show_notifications) {
@ -625,6 +659,7 @@ async function lockUserNameToChat() {
updateUserLockIcon(); updateUserLockIcon();
} }
async function deleteUserAvatar(e) { async function deleteUserAvatar(e) {
e?.stopPropagation(); e?.stopPropagation();
const avatarId = $(this).closest('.avatar-container').find('.avatar').attr('imgfile'); const avatarId = $(this).closest('.avatar-container').find('.avatar').attr('imgfile');
@ -973,7 +1008,7 @@ export function initPersonas() {
$(document).on('click', '.bind_user_name', bindUserNameToPersona); $(document).on('click', '.bind_user_name', bindUserNameToPersona);
$(document).on('click', '.set_default_persona', setDefaultPersona); $(document).on('click', '.set_default_persona', setDefaultPersona);
$(document).on('click', '.delete_avatar', deleteUserAvatar); $(document).on('click', '.delete_avatar', deleteUserAvatar);
$('#lock_user_name').on('click', lockUserNameToChat); $('#lock_user_name').on('click', togglePersonaLock);
$('#create_dummy_persona').on('click', createDummyPersona); $('#create_dummy_persona').on('click', createDummyPersona);
$('#persona_description').on('input', onPersonaDescriptionInput); $('#persona_description').on('input', onPersonaDescriptionInput);
$('#persona_description_position').on('input', onPersonaDescriptionPositionInput); $('#persona_description_position').on('input', onPersonaDescriptionPositionInput);

View File

@ -46,7 +46,7 @@ import { extension_settings, getContext, saveMetadataDebounced } from './extensi
import { getRegexedString, regex_placement } from './extensions/regex/engine.js'; import { getRegexedString, regex_placement } from './extensions/regex/engine.js';
import { findGroupMemberId, getGroupMembers, groups, is_group_generating, openGroupById, resetSelectedGroup, saveGroupChat, selected_group } from './group-chats.js'; import { findGroupMemberId, getGroupMembers, groups, is_group_generating, openGroupById, resetSelectedGroup, saveGroupChat, selected_group } from './group-chats.js';
import { chat_completion_sources, oai_settings, setupChatCompletionPromptManager } from './openai.js'; import { chat_completion_sources, oai_settings, setupChatCompletionPromptManager } from './openai.js';
import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, user_avatar } from './personas.js'; import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockState, togglePersonaLock, user_avatar } from './personas.js';
import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js'; import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';
import { textgen_types, textgenerationwebui_settings } from './textgen-settings.js'; import { textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
import { decodeTextTokens, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync } from './tokenizers.js'; import { decodeTextTokens, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync } from './tokenizers.js';
@ -118,9 +118,18 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'lock', name: 'lock',
callback: bindCallback, callback: lockPersonaCallback,
aliases: ['bind'], aliases: ['bind'],
helpString: 'Locks/unlocks a persona (name and avatar) to the current chat', helpString: 'Locks/unlocks a persona (name and avatar) to the current chat',
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'state',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
defaultValue: 'toggle',
enumProvider: commonEnumProviders.boolean('onOffToggle'),
}),
],
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'bg', name: 'bg',
@ -2576,8 +2585,23 @@ function syncCallback() {
return ''; return '';
} }
function bindCallback() { async function lockPersonaCallback(_args, value) {
$('#lock_user_name').trigger('click'); if (['toggle', 't', ''].includes(value.trim().toLowerCase())) {
await togglePersonaLock();
return '';
}
if (isTrueBoolean(value)) {
await setPersonaLockState(true);
return '';
}
if (isFalseBoolean(value)) {
await setPersonaLockState(false);
return '';
}
return ''; return '';
} }