Move system messages init into separate module (#4251) * Move system messages init into separate module * getSystemMessages => initSystemMessages
Signed| @@ -179,7 +179,7 @@ import { | |||
| 179 | import { debounce_timeout, IGNORE_SYMBOL } from './scripts/constants.js'; | 179 | import { debounce_timeout, IGNORE_SYMBOL } from './scripts/constants.js'; |
| 180 | 180 | ||
| 181 | import { cancelDebouncedMetadataSave, doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js'; | 181 | import { cancelDebouncedMetadataSave, doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js'; |
| 182 | import { COMMENT_NAME_DEFAULT, CONNECT_API_MAP, executeSlashCommandsOnChatInput, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, stopScriptExecution, UNIQUE_APIS } from './scripts/slash-commands.js'; | 182 | import { COMMENT_NAME_DEFAULT, CONNECT_API_MAP, executeSlashCommandsOnChatInput, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, stopScriptExecution, UNIQUE_APIS } from './scripts/slash-commands.js'; |
| 183 | import { | 183 | import { |
| 184 | tag_map, | 184 | tag_map, |
| 185 | tags, | 185 | tags, |
| @@ -245,7 +245,6 @@ import { currentUser, setUserControls } from './scripts/user.js'; | |||
| 245 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js'; | 245 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js'; |
| 246 | import { renderTemplate, renderTemplateAsync } from './scripts/templates.js'; | 246 | import { renderTemplate, renderTemplateAsync } from './scripts/templates.js'; |
| 247 | import { initScrapers } from './scripts/scrapers.js'; | 247 | import { initScrapers } from './scripts/scrapers.js'; |
| 248 | import { SlashCommandBrowser } from './scripts/slash-commands/SlashCommandBrowser.js'; | ||
| 249 | import { initCustomSelectedSamplers, validateDisabledSamplers } from './scripts/samplerSelect.js'; | 248 | import { initCustomSelectedSamplers, validateDisabledSamplers } from './scripts/samplerSelect.js'; |
| 250 | import { DragAndDropHandler } from './scripts/dragdrop.js'; | 249 | import { DragAndDropHandler } from './scripts/dragdrop.js'; |
| 251 | import { INTERACTABLE_CONTROL_CLASS, initKeyboard } from './scripts/keyboard.js'; | 250 | import { INTERACTABLE_CONTROL_CLASS, initKeyboard } from './scripts/keyboard.js'; |
| @@ -266,6 +265,7 @@ import { accountStorage } from './scripts/util/AccountStorage.js'; | |||
| 266 | import { initWelcomeScreen, openPermanentAssistantChat, openPermanentAssistantCard, getPermanentAssistantAvatar } from './scripts/welcome-screen.js'; | 265 | import { initWelcomeScreen, openPermanentAssistantChat, openPermanentAssistantCard, getPermanentAssistantAvatar } from './scripts/welcome-screen.js'; |
| 267 | import { initDataMaid } from './scripts/data-maid.js'; | 266 | import { initDataMaid } from './scripts/data-maid.js'; |
| 268 | import { clearItemizedPrompts, deleteItemizedPrompts, findItemizedPromptSet, initItemizedPrompts, itemizedParams, itemizedPrompts, loadItemizedPrompts, promptItemize, replaceItemizedPromptText, saveItemizedPrompts } from './scripts/itemized-prompts.js'; | 267 | import { clearItemizedPrompts, deleteItemizedPrompts, findItemizedPromptSet, initItemizedPrompts, itemizedParams, itemizedPrompts, loadItemizedPrompts, promptItemize, replaceItemizedPromptText, saveItemizedPrompts } from './scripts/itemized-prompts.js'; |
| 268 | import { getSystemMessageByType, initSystemMessages, SAFETY_CHAT, sendSystemMessage, system_message_types, system_messages } from './scripts/system-messages.js'; | ||
| 269 | 269 | ||
| 270 | // API OBJECT FOR EXTERNAL WIRING | 270 | // API OBJECT FOR EXTERNAL WIRING |
| 271 | globalThis.SillyTavern = { | 271 | globalThis.SillyTavern = { |
| @@ -297,6 +297,10 @@ export { | |||
| 297 | novelai_setting_names, | 297 | novelai_setting_names, |
| 298 | UNIQUE_APIS, | 298 | UNIQUE_APIS, |
| 299 | CONNECT_API_MAP, | 299 | CONNECT_API_MAP, |
| 300 | system_messages, | ||
| 301 | system_message_types, | ||
| 302 | sendSystemMessage, | ||
| 303 | getSystemMessageByType, | ||
| 300 | }; | 304 | }; |
| 301 | 305 | ||
| 302 | /** | 306 | /** |
| @@ -506,25 +510,6 @@ export const saveCharacterDebounced = debounce(() => $('#create_button').trigger | |||
| 506 | export const printCharactersDebounced = debounce(() => { printCharacters(false); }, DEFAULT_PRINT_TIMEOUT); | 510 | export const printCharactersDebounced = debounce(() => { printCharacters(false); }, DEFAULT_PRINT_TIMEOUT); |
| 507 | 511 | ||
| 508 | /** | 512 | /** |
| 509 | * @enum {string} System message types | ||
| 510 | */ | ||
| 511 | export const system_message_types = { | ||
| 512 | HELP: 'help', | ||
| 513 | WELCOME: 'welcome', | ||
| 514 | EMPTY: 'empty', | ||
| 515 | GENERIC: 'generic', | ||
| 516 | NARRATOR: 'narrator', | ||
| 517 | COMMENT: 'comment', | ||
| 518 | SLASH_COMMANDS: 'slash_commands', | ||
| 519 | FORMATTING: 'formatting', | ||
| 520 | HOTKEYS: 'hotkeys', | ||
| 521 | MACROS: 'macros', | ||
| 522 | WELCOME_PROMPT: 'welcome_prompt', | ||
| 523 | ASSISTANT_NOTE: 'assistant_note', | ||
| 524 | ASSISTANT_MESSAGE: 'assistant_message', | ||
| 525 | }; | ||
| 526 | |||
| 527 | /** | ||
| 528 | * @enum {number} Extension prompt types | 513 | * @enum {number} Extension prompt types |
| 529 | */ | 514 | */ |
| 530 | export const extension_prompt_types = { | 515 | export const extension_prompt_types = { |
| @@ -545,106 +530,6 @@ export const extension_prompt_roles = { | |||
| 545 | 530 | ||
| 546 | export const MAX_INJECTION_DEPTH = 10000; | 531 | export const MAX_INJECTION_DEPTH = 10000; |
| 547 | 532 | ||
| 548 | // Initialized in getSystemMessages() | ||
| 549 | const SAFETY_CHAT = []; | ||
| 550 | |||
| 551 | export let system_messages = {}; | ||
| 552 | |||
| 553 | async function getSystemMessages() { | ||
| 554 | system_messages = { | ||
| 555 | help: { | ||
| 556 | name: systemUserName, | ||
| 557 | force_avatar: system_avatar, | ||
| 558 | is_user: false, | ||
| 559 | is_system: true, | ||
| 560 | mes: await renderTemplateAsync('help'), | ||
| 561 | }, | ||
| 562 | slash_commands: { | ||
| 563 | name: systemUserName, | ||
| 564 | force_avatar: system_avatar, | ||
| 565 | is_user: false, | ||
| 566 | is_system: true, | ||
| 567 | mes: '', | ||
| 568 | }, | ||
| 569 | hotkeys: { | ||
| 570 | name: systemUserName, | ||
| 571 | force_avatar: system_avatar, | ||
| 572 | is_user: false, | ||
| 573 | is_system: true, | ||
| 574 | mes: await renderTemplateAsync('hotkeys'), | ||
| 575 | }, | ||
| 576 | formatting: { | ||
| 577 | name: systemUserName, | ||
| 578 | force_avatar: system_avatar, | ||
| 579 | is_user: false, | ||
| 580 | is_system: true, | ||
| 581 | mes: await renderTemplateAsync('formatting'), | ||
| 582 | }, | ||
| 583 | macros: { | ||
| 584 | name: systemUserName, | ||
| 585 | force_avatar: system_avatar, | ||
| 586 | is_user: false, | ||
| 587 | is_system: true, | ||
| 588 | mes: await renderTemplateAsync('macros'), | ||
| 589 | }, | ||
| 590 | welcome: | ||
| 591 | { | ||
| 592 | name: systemUserName, | ||
| 593 | force_avatar: system_avatar, | ||
| 594 | is_user: false, | ||
| 595 | is_system: true, | ||
| 596 | uses_system_ui: true, | ||
| 597 | mes: await renderTemplateAsync('welcome', { displayVersion }), | ||
| 598 | }, | ||
| 599 | empty: { | ||
| 600 | name: systemUserName, | ||
| 601 | force_avatar: system_avatar, | ||
| 602 | is_user: false, | ||
| 603 | is_system: true, | ||
| 604 | mes: 'No one hears you. <b>Hint:</b> add more members to the group!', | ||
| 605 | }, | ||
| 606 | generic: { | ||
| 607 | name: systemUserName, | ||
| 608 | force_avatar: system_avatar, | ||
| 609 | is_user: false, | ||
| 610 | is_system: true, | ||
| 611 | mes: 'Generic system message. User `text` parameter to override the contents', | ||
| 612 | }, | ||
| 613 | welcome_prompt: { | ||
| 614 | name: systemUserName, | ||
| 615 | force_avatar: system_avatar, | ||
| 616 | is_user: false, | ||
| 617 | is_system: true, | ||
| 618 | uses_system_ui: true, | ||
| 619 | mes: await renderTemplateAsync('welcomePrompt'), | ||
| 620 | extra: { | ||
| 621 | isSmallSys: true, | ||
| 622 | }, | ||
| 623 | }, | ||
| 624 | assistant_note: { | ||
| 625 | name: systemUserName, | ||
| 626 | force_avatar: system_avatar, | ||
| 627 | is_user: false, | ||
| 628 | is_system: true, | ||
| 629 | mes: await renderTemplateAsync('assistantNote'), | ||
| 630 | uses_system_ui: true, | ||
| 631 | extra: { | ||
| 632 | isSmallSys: true, | ||
| 633 | }, | ||
| 634 | }, | ||
| 635 | }; | ||
| 636 | |||
| 637 | const safetyMessage = { | ||
| 638 | name: systemUserName, | ||
| 639 | force_avatar: system_avatar, | ||
| 640 | is_system: true, | ||
| 641 | is_user: false, | ||
| 642 | create_date: 0, | ||
| 643 | mes: t`You deleted a character/chat and arrived back here for safety reasons! Pick another character!`, | ||
| 644 | }; | ||
| 645 | SAFETY_CHAT.splice(0, SAFETY_CHAT.length, safetyMessage); | ||
| 646 | } | ||
| 647 | |||
| 648 | async function getClientVersion() { | 533 | async function getClientVersion() { |
| 649 | try { | 534 | try { |
| 650 | const response = await fetch('/version'); | 535 | const response = await fetch('/version'); |
| @@ -863,7 +748,7 @@ async function firstLoadInit() { | |||
| 863 | initExtensionSlashCommands(); | 748 | initExtensionSlashCommands(); |
| 864 | ToolManager.initToolSlashCommands(); | 749 | ToolManager.initToolSlashCommands(); |
| 865 | await initPresetManager(); | 750 | await initPresetManager(); |
| 866 | await getSystemMessages(); | 751 | await initSystemMessages(); |
| 867 | await getSettings(); | 752 | await getSettings(); |
| 868 | initKeyboard(); | 753 | initKeyboard(); |
| 869 | initDynamicStyles(); | 754 | initDynamicStyles(); |
| @@ -2589,60 +2474,6 @@ export async function processCommands(message) { | |||
| 2589 | } | 2474 | } |
| 2590 | 2475 | ||
| 2591 | /** | 2476 | /** |
| 2592 | * Gets a system message by type. | ||
| 2593 | * @param {string} type Type of system message | ||
| 2594 | * @param {string} [text] Text to be sent | ||
| 2595 | * @param {object} [extra] Additional data to be added to the message | ||
| 2596 | * @returns {object} System message object | ||
| 2597 | */ | ||
| 2598 | export function getSystemMessageByType(type, text, extra = {}) { | ||
| 2599 | const systemMessage = system_messages[type]; | ||
| 2600 | |||
| 2601 | if (!systemMessage) { | ||
| 2602 | return; | ||
| 2603 | } | ||
| 2604 | |||
| 2605 | const newMessage = { ...systemMessage, send_date: getMessageTimeStamp() }; | ||
| 2606 | |||
| 2607 | if (text) { | ||
| 2608 | newMessage.mes = text; | ||
| 2609 | } | ||
| 2610 | |||
| 2611 | if (type === system_message_types.SLASH_COMMANDS) { | ||
| 2612 | newMessage.mes = getSlashCommandsHelp(); | ||
| 2613 | } | ||
| 2614 | |||
| 2615 | if (!newMessage.extra) { | ||
| 2616 | newMessage.extra = {}; | ||
| 2617 | } | ||
| 2618 | |||
| 2619 | newMessage.extra = Object.assign(newMessage.extra, extra); | ||
| 2620 | newMessage.extra.type = type; | ||
| 2621 | return newMessage; | ||
| 2622 | } | ||
| 2623 | |||
| 2624 | /** | ||
| 2625 | * Sends a system message to the chat. | ||
| 2626 | * @param {string} type Type of system message | ||
| 2627 | * @param {string} [text] Text to be sent | ||
| 2628 | * @param {object} [extra] Additional data to be added to the message | ||
| 2629 | */ | ||
| 2630 | export function sendSystemMessage(type, text, extra = {}) { | ||
| 2631 | const newMessage = getSystemMessageByType(type, text, extra); | ||
| 2632 | chat.push(newMessage); | ||
| 2633 | addOneMessage(newMessage); | ||
| 2634 | is_send_press = false; | ||
| 2635 | if (type === system_message_types.SLASH_COMMANDS) { | ||
| 2636 | const browser = new SlashCommandBrowser(); | ||
| 2637 | const spinner = document.querySelector('#chat .last_mes .custom-slashHelp'); | ||
| 2638 | const parent = spinner.parentElement; | ||
| 2639 | spinner.remove(); | ||
| 2640 | browser.renderInto(parent); | ||
| 2641 | browser.search.focus(); | ||
| 2642 | } | ||
| 2643 | } | ||
| 2644 | |||
| 2645 | /** | ||
| 2646 | * Extracts the contents of bias macros from a message. | 2477 | * Extracts the contents of bias macros from a message. |
| 2647 | * @param {string} message Message text | 2478 | * @param {string} message Message text |
| 2648 | * @returns {string} Message bias extracted from the message (or an empty string if not found) | 2479 | * @returns {string} Message bias extracted from the message (or an empty string if not found) |
| @@ -0,0 +1,181 @@ | |||
| 1 | import { addOneMessage, chat, displayVersion, setSendButtonState, system_avatar, systemUserName } from '../script.js'; | ||
| 2 | import { t } from './i18n.js'; | ||
| 3 | import { getMessageTimeStamp } from './RossAscends-mods.js'; | ||
| 4 | import { getSlashCommandsHelp } from './slash-commands.js'; | ||
| 5 | import { SlashCommandBrowser } from './slash-commands/SlashCommandBrowser.js'; | ||
| 6 | import { renderTemplateAsync } from './templates.js'; | ||
| 7 | |||
| 8 | // Initialized in getSystemMessages() | ||
| 9 | export const system_messages = {}; | ||
| 10 | export const SAFETY_CHAT = []; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * @enum {string} System message types | ||
| 14 | */ | ||
| 15 | export const system_message_types = { | ||
| 16 | HELP: 'help', | ||
| 17 | WELCOME: 'welcome', | ||
| 18 | EMPTY: 'empty', | ||
| 19 | GENERIC: 'generic', | ||
| 20 | NARRATOR: 'narrator', | ||
| 21 | COMMENT: 'comment', | ||
| 22 | SLASH_COMMANDS: 'slash_commands', | ||
| 23 | FORMATTING: 'formatting', | ||
| 24 | HOTKEYS: 'hotkeys', | ||
| 25 | MACROS: 'macros', | ||
| 26 | WELCOME_PROMPT: 'welcome_prompt', | ||
| 27 | ASSISTANT_NOTE: 'assistant_note', | ||
| 28 | ASSISTANT_MESSAGE: 'assistant_message', | ||
| 29 | }; | ||
| 30 | |||
| 31 | export async function initSystemMessages() { | ||
| 32 | const result = { | ||
| 33 | help: { | ||
| 34 | name: systemUserName, | ||
| 35 | force_avatar: system_avatar, | ||
| 36 | is_user: false, | ||
| 37 | is_system: true, | ||
| 38 | mes: await renderTemplateAsync('help'), | ||
| 39 | }, | ||
| 40 | slash_commands: { | ||
| 41 | name: systemUserName, | ||
| 42 | force_avatar: system_avatar, | ||
| 43 | is_user: false, | ||
| 44 | is_system: true, | ||
| 45 | mes: '', | ||
| 46 | }, | ||
| 47 | hotkeys: { | ||
| 48 | name: systemUserName, | ||
| 49 | force_avatar: system_avatar, | ||
| 50 | is_user: false, | ||
| 51 | is_system: true, | ||
| 52 | mes: await renderTemplateAsync('hotkeys'), | ||
| 53 | }, | ||
| 54 | formatting: { | ||
| 55 | name: systemUserName, | ||
| 56 | force_avatar: system_avatar, | ||
| 57 | is_user: false, | ||
| 58 | is_system: true, | ||
| 59 | mes: await renderTemplateAsync('formatting'), | ||
| 60 | }, | ||
| 61 | macros: { | ||
| 62 | name: systemUserName, | ||
| 63 | force_avatar: system_avatar, | ||
| 64 | is_user: false, | ||
| 65 | is_system: true, | ||
| 66 | mes: await renderTemplateAsync('macros'), | ||
| 67 | }, | ||
| 68 | welcome: | ||
| 69 | { | ||
| 70 | name: systemUserName, | ||
| 71 | force_avatar: system_avatar, | ||
| 72 | is_user: false, | ||
| 73 | is_system: true, | ||
| 74 | uses_system_ui: true, | ||
| 75 | mes: await renderTemplateAsync('welcome', { displayVersion }), | ||
| 76 | }, | ||
| 77 | empty: { | ||
| 78 | name: systemUserName, | ||
| 79 | force_avatar: system_avatar, | ||
| 80 | is_user: false, | ||
| 81 | is_system: true, | ||
| 82 | mes: 'No one hears you. <b>Hint:</b> add more members to the group!', | ||
| 83 | }, | ||
| 84 | generic: { | ||
| 85 | name: systemUserName, | ||
| 86 | force_avatar: system_avatar, | ||
| 87 | is_user: false, | ||
| 88 | is_system: true, | ||
| 89 | mes: 'Generic system message. User `text` parameter to override the contents', | ||
| 90 | }, | ||
| 91 | welcome_prompt: { | ||
| 92 | name: systemUserName, | ||
| 93 | force_avatar: system_avatar, | ||
| 94 | is_user: false, | ||
| 95 | is_system: true, | ||
| 96 | uses_system_ui: true, | ||
| 97 | mes: await renderTemplateAsync('welcomePrompt'), | ||
| 98 | extra: { | ||
| 99 | isSmallSys: true, | ||
| 100 | }, | ||
| 101 | }, | ||
| 102 | assistant_note: { | ||
| 103 | name: systemUserName, | ||
| 104 | force_avatar: system_avatar, | ||
| 105 | is_user: false, | ||
| 106 | is_system: true, | ||
| 107 | mes: await renderTemplateAsync('assistantNote'), | ||
| 108 | uses_system_ui: true, | ||
| 109 | extra: { | ||
| 110 | isSmallSys: true, | ||
| 111 | }, | ||
| 112 | }, | ||
| 113 | }; | ||
| 114 | |||
| 115 | Object.assign(system_messages, result); | ||
| 116 | |||
| 117 | const safetyMessage = { | ||
| 118 | name: systemUserName, | ||
| 119 | force_avatar: system_avatar, | ||
| 120 | is_system: true, | ||
| 121 | is_user: false, | ||
| 122 | create_date: 0, | ||
| 123 | mes: t`You deleted a character/chat and arrived back here for safety reasons! Pick another character!`, | ||
| 124 | }; | ||
| 125 | SAFETY_CHAT.splice(0, SAFETY_CHAT.length, safetyMessage); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | /** | ||
| 130 | * Gets a system message by type. | ||
| 131 | * @param {string} type Type of system message | ||
| 132 | * @param {string} [text] Text to be sent | ||
| 133 | * @param {object} [extra] Additional data to be added to the message | ||
| 134 | * @returns {object} System message object | ||
| 135 | */ | ||
| 136 | export function getSystemMessageByType(type, text, extra = {}) { | ||
| 137 | const systemMessage = system_messages[type]; | ||
| 138 | |||
| 139 | if (!systemMessage) { | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | const newMessage = { ...systemMessage, send_date: getMessageTimeStamp() }; | ||
| 144 | |||
| 145 | if (text) { | ||
| 146 | newMessage.mes = text; | ||
| 147 | } | ||
| 148 | |||
| 149 | if (type === system_message_types.SLASH_COMMANDS) { | ||
| 150 | newMessage.mes = getSlashCommandsHelp(); | ||
| 151 | } | ||
| 152 | |||
| 153 | if (!newMessage.extra) { | ||
| 154 | newMessage.extra = {}; | ||
| 155 | } | ||
| 156 | |||
| 157 | newMessage.extra = Object.assign(newMessage.extra, extra); | ||
| 158 | newMessage.extra.type = type; | ||
| 159 | return newMessage; | ||
| 160 | } | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Sends a system message to the chat. | ||
| 164 | * @param {string} type Type of system message | ||
| 165 | * @param {string} [text] Text to be sent | ||
| 166 | * @param {object} [extra] Additional data to be added to the message | ||
| 167 | */ | ||
| 168 | export function sendSystemMessage(type, text, extra = {}) { | ||
| 169 | const newMessage = getSystemMessageByType(type, text, extra); | ||
| 170 | chat.push(newMessage); | ||
| 171 | addOneMessage(newMessage); | ||
| 172 | setSendButtonState(false); | ||
| 173 | if (type === system_message_types.SLASH_COMMANDS) { | ||
| 174 | const browser = new SlashCommandBrowser(); | ||
| 175 | const spinner = document.querySelector('#chat .last_mes .custom-slashHelp'); | ||
| 176 | const parent = spinner.parentElement; | ||
| 177 | spinner.remove(); | ||
| 178 | browser.renderInto(parent); | ||
| 179 | browser.search.focus(); | ||
| 180 | } | ||
| 181 | } | ||