Merge branch 'staging' into neo-server

This commit is contained in:
Cohee 2024-04-19 15:07:38 +03:00
commit 09d43403b2
3 changed files with 64 additions and 24 deletions

View File

@ -3524,9 +3524,6 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
} else {
break;
}
// Prevent UI thread lock on tokenization
await delay(1);
}
for (let i = 0; i < chat2.length; i++) {
@ -3554,9 +3551,6 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
} else {
break;
}
// Prevent UI thread lock on tokenization
await delay(1);
}
// Add user alignment message if last message is not a user message
@ -3599,7 +3593,6 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
} else {
break;
}
await delay(1);
}
}

View File

@ -50,10 +50,18 @@ import { decodeTextTokens, getFriendlyTokenizerName, getTextTokens, getTokenCoun
import { delay, isFalseBoolean, isTrueBoolean, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
import { registerVariableCommands, resolveVariable } from './variables.js';
import { background_settings } from './backgrounds.js';
export {
executeSlashCommands, getSlashCommandsHelp, registerSlashCommand,
};
/**
* @typedef {object} SlashCommand
* @property {function} callback - The callback function to execute
* @property {string} helpString - The help string for the command
* @property {boolean} interruptsGeneration - Whether the command interrupts message generation
* @property {boolean} purgeFromMessage - Whether the command should be purged from the message
*/
/**
* Provides a parser for slash commands.
*/
class SlashCommandParser {
static COMMENT_KEYWORDS = ['#', '/'];
static RESERVED_KEYWORDS = [
@ -61,10 +69,26 @@ class SlashCommandParser {
];
constructor() {
/**
* @type {Record<string, SlashCommand>} - Slash commands registered in the parser
*/
this.commands = {};
/**
* @type {Record<string, string>} - Help strings for each command
*/
this.helpStrings = {};
}
/**
* Adds a slash command to the parser.
* @param {string} command - The command name
* @param {function} callback - The callback function to execute
* @param {string[]} aliases - The command aliases
* @param {string} helpString - The help string for the command
* @param {boolean} [interruptsGeneration] - Whether the command interrupts message generation
* @param {boolean} [purgeFromMessage] - Whether the command should be purged from the message
* @returns {void}
*/
addCommand(command, callback, aliases, helpString = '', interruptsGeneration = false, purgeFromMessage = true) {
const fnObj = { callback, helpString, interruptsGeneration, purgeFromMessage };
@ -96,7 +120,7 @@ class SlashCommandParser {
/**
* Parses a slash command to extract the command name, the (named) arguments and the remaining text
* @param {string} text - Slash command text
* @returns {{command: string, args: object, value: string}} - The parsed command, its arguments and the remaining text
* @returns {{command: SlashCommand, args: object, value: string, commandName: string}} - The parsed command, its arguments and the remaining text
*/
parse(text) {
// Parses a command even when spaces are present in arguments
@ -118,6 +142,20 @@ class SlashCommandParser {
console.debug('command:' + command);
}
if (SlashCommandParser.COMMENT_KEYWORDS.includes(command)) {
return {
commandName: command,
command: {
callback: () => {},
helpString: '',
interruptsGeneration: false,
purgeFromMessage: true,
},
args: {},
value: '',
};
}
// parse the rest of the string to extract named arguments, the remainder is the "unnamedArg" which is usually text, like the prompt to send
while (remainingText.length > 0) {
// does the remaining text is like nameArg=[value] or nameArg=[value,value] or nameArg=[ value , value , value]
@ -176,7 +214,7 @@ class SlashCommandParser {
// your weird complex command is now transformed into a juicy tiny text or something useful :)
if (this.commands[command]) {
return { command: this.commands[command], args: argObj, value: unnamedArg };
return { command: this.commands[command], args: argObj, value: unnamedArg, commandName: command };
}
return null;
@ -197,8 +235,13 @@ class SlashCommandParser {
}
const parser = new SlashCommandParser();
const registerSlashCommand = parser.addCommand.bind(parser);
const getSlashCommandsHelp = parser.getHelpString.bind(parser);
/**
* Registers a slash command in the parser.
* @type {(command: string, callback: function, aliases: string[], helpString: string, interruptsGeneration?: boolean, purgeFromMessage?: boolean) => void}
*/
export const registerSlashCommand = parser.addCommand.bind(parser);
export const getSlashCommandsHelp = parser.getHelpString.bind(parser);
parser.addCommand('?', helpCommandCallback, ['help'], ' get help on macros, chat formatting and commands', true, true);
parser.addCommand('name', setNameCallback, ['persona'], '<span class="monospace">(name)</span> sets user name and persona avatar (if set)', true, true);
@ -1293,7 +1336,7 @@ export async function generateSystemMessage(_, prompt) {
// Generate and regex the output if applicable
toastr.info('Please wait', 'Generating...');
let message = await generateQuietPrompt(prompt);
let message = await generateQuietPrompt(prompt, false, false);
message = getRegexedString(message, regex_placement.SLASH_COMMAND);
sendNarratorMessage(_, message);
@ -1485,7 +1528,7 @@ export async function promptQuietForLoudResponse(who, text) {
//text = `${text}${power_user.instruct.enabled ? '' : '\n'}${(power_user.always_force_name2 && who != 'raw') ? characters[character_id].name + ":" : ""}`
let reply = await generateQuietPrompt(text, true);
let reply = await generateQuietPrompt(text, true, false);
text = await getRegexedString(reply, regex_placement.SLASH_COMMAND);
const message = {
@ -1717,7 +1760,7 @@ function modelCallback(_, model) {
* @param {boolean} unescape Whether to unescape the batch separator
* @returns {Promise<{interrupt: boolean, newText: string, pipe: string} | boolean>}
*/
async function executeSlashCommands(text, unescape = false) {
export async function executeSlashCommands(text, unescape = false) {
if (!text) {
return false;
}
@ -1758,7 +1801,8 @@ async function executeSlashCommands(text, unescape = false) {
}
// Skip comment commands. They don't run macros or interrupt pipes.
if (SlashCommandParser.COMMENT_KEYWORDS.includes(result.command)) {
if (SlashCommandParser.COMMENT_KEYWORDS.includes(result.commandName)) {
result.command.purgeFromMessage && linesToRemove.push(lines[index]);
continue;
}
@ -1822,8 +1866,8 @@ async function executeSlashCommands(text, unescape = false) {
function setSlashCommandAutocomplete(textarea) {
textarea.autocomplete({
source: (input, output) => {
// Only show for slash commands and if there's no space
if (!input.term.startsWith('/') || input.term.includes(' ')) {
// Only show for slash commands (requiring at least 1 letter after the slash) and if there's no space
if (!input.term.startsWith('/') || input.term.includes(' ') || input.term === '/') {
output([]);
return;
}
@ -1833,7 +1877,7 @@ function setSlashCommandAutocomplete(textarea) {
.keys(parser.helpStrings) // Get all slash commands
.filter(x => x.startsWith(slashCommand)) // Filter by the input
.sort((a, b) => a.localeCompare(b)) // Sort alphabetically
// .slice(0, 20) // Limit to 20 results
.slice(0, 5) // Limit to 5 results
.map(x => ({ label: parser.helpStrings[x], value: `/${x} ` })); // Map to the help string
output(result); // Return the results

View File

@ -158,6 +158,7 @@ body {
border-top: 20px solid transparent;
min-height: 40px;
}
::-webkit-scrollbar-thumb:horizontal {
background-color: var(--grey7070a);
box-shadow: inset 0 0 0 1px var(--black50a);
@ -801,7 +802,7 @@ body .panelControlBar {
.mes {
display: flex;
align-items: flex-start;
padding: 20px 10px 0 10px;
padding: 10px 10px 0 10px;
margin-top: 0;
width: 100%;
color: var(--SmartThemeBodyColor);
@ -2242,7 +2243,8 @@ grammarly-extension {
margin-right: 25px;
}
#shadow_popup, .shadow_popup {
#shadow_popup,
.shadow_popup {
backdrop-filter: blur(calc(var(--SmartThemeBlurStrength) * 2));
-webkit-backdrop-filter: blur(calc(var(--SmartThemeBlurStrength) * 2));
background-color: var(--black30a);
@ -2254,6 +2256,7 @@ grammarly-extension {
height: 100svh;
z-index: 9999;
top: 0;
&.shadow_popup {
z-index: 9998;
}
@ -4012,4 +4015,4 @@ body:not(.movingUI) .drawer-content.maximized {
height: 100vh;
z-index: 9999;
}
}
}