Add lock=on/off to /gen and /genraw commands

This commit is contained in:
Cohee 2023-11-24 15:18:49 +02:00
parent 8e16f28827
commit dd17c2483f
3 changed files with 53 additions and 12 deletions

View File

@ -2702,7 +2702,7 @@ class StreamingProcessor {
* Generates a message using the provided prompt. * Generates a message using the provided prompt.
* @param {string} prompt Prompt to generate a message from * @param {string} prompt Prompt to generate a message from
* @param {string} api API to use. Main API is used if not specified. * @param {string} api API to use. Main API is used if not specified.
* @param {string} instructOverride If 0, false or off, disables instruct formatting * @param {boolean} instructOverride true to override instruct mode, false to use the default value
* @returns {Promise<string>} Generated message * @returns {Promise<string>} Generated message
*/ */
export async function generateRaw(prompt, api, instructOverride) { export async function generateRaw(prompt, api, instructOverride) {
@ -2711,8 +2711,7 @@ export async function generateRaw(prompt, api, instructOverride) {
} }
const abortController = new AbortController(); const abortController = new AbortController();
const instructDisabled = instructOverride === '0' || instructOverride === 'false' || instructOverride === 'off'; const isInstruct = power_user.instruct.enabled && main_api !== 'openai' && main_api !== 'novel' && !instructOverride;
const isInstruct = power_user.instruct.enabled && main_api !== 'openai' && main_api !== 'novel' && !instructDisabled;
prompt = substituteParams(prompt); prompt = substituteParams(prompt);
prompt = api == 'novel' ? adjustNovelInstructionPrompt(prompt) : prompt; prompt = api == 'novel' ? adjustNovelInstructionPrompt(prompt) : prompt;

View File

@ -26,6 +26,8 @@ import {
setCharacterName, setCharacterName,
generateRaw, generateRaw,
callPopup, callPopup,
deactivateSendButtons,
activateSendButtons,
} from "../script.js"; } from "../script.js";
import { getMessageTimeStamp } from "./RossAscends-mods.js"; import { getMessageTimeStamp } from "./RossAscends-mods.js";
import { findGroupMemberId, groups, is_group_generating, resetSelectedGroup, saveGroupChat, selected_group } from "./group-chats.js"; import { findGroupMemberId, groups, is_group_generating, resetSelectedGroup, saveGroupChat, selected_group } from "./group-chats.js";
@ -34,7 +36,7 @@ import { addEphemeralStoppingString, chat_styles, power_user } from "./power-use
import { autoSelectPersona } from "./personas.js"; import { autoSelectPersona } from "./personas.js";
import { getContext } from "./extensions.js"; import { getContext } from "./extensions.js";
import { hideChatMessage, unhideChatMessage } from "./chats.js"; import { hideChatMessage, unhideChatMessage } from "./chats.js";
import { delay, stringToRange } from "./utils.js"; import { delay, isFalseBoolean, isTrueBoolean, stringToRange } from "./utils.js";
import { registerVariableCommands } from "./variables.js"; import { registerVariableCommands } from "./variables.js";
export { export {
executeSlashCommands, executeSlashCommands,
@ -161,8 +163,8 @@ parser.addCommand('memberdown', moveGroupMemberDownCallback, ['downmember'], '<s
parser.addCommand('peek', peekCallback, [], '<span class="monospace">(message index or range)</span> shows a group member character card without switching chats', true, true); parser.addCommand('peek', peekCallback, [], '<span class="monospace">(message index or range)</span> shows a group member character card without switching chats', true, true);
parser.addCommand('delswipe', deleteSwipeCallback, ['swipedel'], '<span class="monospace">(optional 1-based id)</span> deletes a swipe from the last chat message. If swipe id not provided - deletes the current swipe.', true, true); parser.addCommand('delswipe', deleteSwipeCallback, ['swipedel'], '<span class="monospace">(optional 1-based id)</span> deletes a swipe from the last chat message. If swipe id not provided - deletes the current swipe.', true, true);
parser.addCommand('echo', echoCallback, [], '<span class="monospace">(text)</span> echoes the text to toast message. Useful for pipes debugging.', true, true); parser.addCommand('echo', echoCallback, [], '<span class="monospace">(text)</span> echoes the text to toast message. Useful for pipes debugging.', true, true);
parser.addCommand('gen', generateCallback, [], '<span class="monospace">(prompt)</span> generates text using the provided prompt and passes it to the next command through the pipe.', true, true); parser.addCommand('gen', generateCallback, [], '<span class="monospace">(lock=on/off [prompt])</span> generates text using the provided prompt and passes it to the next command through the pipe, optionally locking user input while generating.', true, true);
parser.addCommand('genraw', generateRawCallback, [], '<span class="monospace">(prompt)</span> generates text using the provided prompt and passes it to the next command through the pipe. Does not include chat history or character card. Use instruct=off to skip instruct formatting, e.g. <tt>/genraw instruct=off Why is the sky blue?</tt>. Use stop=... with a JSON-serialized array to add one-time custom stop strings, e.g. <tt>/genraw stop=["\\n"] Say hi</tt>', true, true); parser.addCommand('genraw', generateRawCallback, [], '<span class="monospace">(lock=on/off [prompt])</span> generates text using the provided prompt and passes it to the next command through the pipe, optionally locking user input while generating. Does not include chat history or character card. Use instruct=off to skip instruct formatting, e.g. <tt>/genraw instruct=off Why is the sky blue?</tt>. Use stop=... with a JSON-serialized array to add one-time custom stop strings, e.g. <tt>/genraw stop=["\\n"] Say hi</tt>', true, true);
parser.addCommand('addswipe', addSwipeCallback, ['swipeadd'], '<span class="monospace">(text)</span> adds a swipe to the last chat message.', true, true); parser.addCommand('addswipe', addSwipeCallback, ['swipeadd'], '<span class="monospace">(text)</span> adds a swipe to the last chat message.', true, true);
parser.addCommand('abort', abortCallback, [], ' aborts the slash command batch execution', true, true); parser.addCommand('abort', abortCallback, [], ' aborts the slash command batch execution', true, true);
parser.addCommand('fuzzy', fuzzyCallback, [], 'list=["a","b","c"] (search value) performs a fuzzy match of the provided search using the provided list of value and passes the closest match to the next command through the pipe.', true, true); parser.addCommand('fuzzy', fuzzyCallback, [], 'list=["a","b","c"] (search value) performs a fuzzy match of the provided search using the provided list of value and passes the closest match to the next command through the pipe.', true, true);
@ -242,6 +244,7 @@ async function generateRawCallback(args, value) {
// Prevent generate recursion // Prevent generate recursion
$('#send_textarea').val(''); $('#send_textarea').val('');
const lock = isTrueBoolean(args?.lock);
if (typeof args.stop === 'string' && args.stop.length) { if (typeof args.stop === 'string' && args.stop.length) {
try { try {
@ -256,21 +259,42 @@ async function generateRawCallback(args, value) {
} }
} }
const result = await generateRaw(value, '', args.instruct); try {
if (lock) {
deactivateSendButtons();
}
const result = await generateRaw(value, '', isFalseBoolean(args?.instruct));
return result; return result;
} finally {
if (lock) {
activateSendButtons();
}
}
} }
async function generateCallback(_, arg) { async function generateCallback(args, value) {
if (!arg) { if (!value) {
console.warn('WARN: No argument provided for /gen command'); console.warn('WARN: No argument provided for /gen command');
return; return;
} }
// Prevent generate recursion // Prevent generate recursion
$('#send_textarea').val(''); $('#send_textarea').val('');
const lock = isTrueBoolean(args?.lock);
const result = await generateQuietPrompt(arg, false, false, ''); try {
if (lock) {
deactivateSendButtons();
}
const result = await generateQuietPrompt(value, false, false, '');
return result; return result;
} finally {
if (lock) {
activateSendButtons();
}
}
} }
async function echoCallback(_, arg) { async function echoCallback(_, arg) {

View File

@ -565,6 +565,24 @@ export function countOccurrences(string, character) {
return count; return count;
} }
/**
* Checks if a string is "true" value.
* @param {string} arg String to check
* @returns {boolean} True if the string is true, false otherwise.
*/
export function isTrueBoolean(arg) {
return ['on', 'true', '1'].includes(arg);
}
/**
* Checks if a string is "false" value.
* @param {string} arg String to check
* @returns {boolean} True if the string is false, false otherwise.
*/
export function isFalseBoolean(arg) {
return ['off', 'false', '0'].includes(arg);
}
/** /**
* Checks if a number is odd. * Checks if a number is odd.
* @param {number} number The number to check. * @param {number} number The number to check.