Chat without character selected (#2757) * WIP: Enable chatting without character selected * Add burger menu item * Neutral assistant chat space * Revert "Neutral assistant chat space" This reverts commit be2da4a629fb8cfef2f7387272eec9d88b9c4328. * Revert "Add burger menu item" This reverts commit d4e8d990c1ae847a4bc18ee0d4b886cbcd065b7f. * Use simple ephemeral chats * Only start a new assistant chat if not already in it * Don't emit event not to clear pending files * Allow edits, disable checkpoints and branches * Exclude from tags, allow copy * Allow hide/unhide * Allow zooming avatar * Suppress warning * Rename neutral character * Rename neutral back to Assistant

f7de4953210bc611ca272f6ba64631d629c7cc53

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
5 files changed, +77 -30Ignore whitespace
public/script.js+66 -28
@@ -484,9 +484,10 @@ const promptStorage = new localforage.createInstance({ name: 'SillyTavern_Prompt
484484export let itemizedPrompts = [];
485485
486486export const systemUserName = 'SillyTavern System';
487+export const neutralCharacterName = 'Assistant';
487488let default_user_name = 'User';
488489export let name1 = default_user_name;
489490export let name2 = 'SillyTavern System'systemUserName;
490491export let chat = [];
491492let chatSaveTimeout;
492493let importFlashTimeout;
@@ -563,6 +564,8 @@ export const system_message_types = {
563564 FORMATTING: 'formatting',
564565 HOTKEYS: 'hotkeys',
565566 MACROS: 'macros',
567+ WELCOME_PROMPT: 'welcome_prompt',
568+ ASSISTANT_NOTE: 'assistant_note',
566569};
567570
568571/**
@@ -680,6 +683,26 @@ async function getSystemMessages() {
680683 is_system: true,
681684 mes: 'Click here to return to the previous chat: <a class="bookmark_link" file_name="{0}" href="javascript:void(null);">Return</a>',
682685 },
686+ welcome_prompt: {
687+ name: systemUserName,
688+ force_avatar: system_avatar,
689+ is_user: false,
690+ is_system: true,
691+ mes: await renderTemplateAsync('welcomePrompt'),
692+ extra: {
693+ isSmallSys: true,
694+ },
695+ },
696+ assistant_note: {
697+ name: systemUserName,
698+ force_avatar: system_avatar,
699+ is_user: false,
700+ is_system: true,
701+ mes: await renderTemplateAsync('assistantNote'),
702+ extra: {
703+ isSmallSys: true,
704+ },
705+ },
683706 };
684707}
685708
@@ -924,6 +947,7 @@ async function firstLoadInit() {
924947 initTextGenModels();
925948 await getSystemMessages();
926949 sendSystemMessage(system_message_types.WELCOME);
950+ sendSystemMessage(system_message_types.WELCOME_PROMPT);
927951 await getSettings();
928952 initKeyboard();
929953 initDynamicStyles();
@@ -1849,7 +1873,7 @@ export async function reloadCurrentChat() {
18491873/**
18501874 * Send the message currently typed into the chat box.
18511875 */
18521876export async function sendTextareaMessage() {
18531877 if (is_send_press) return;
18541878 if (isExecutingCommandsFromChatInput) return;
18551879
@@ -1867,6 +1891,10 @@ export function sendTextareaMessage() {
18671891 generateType = 'continue';
18681892 }
18691893
1894+ if (textareaText && !selected_group && this_chid === undefined && name2 !== neutralCharacterName) {
1895+ await newAssistantChat();
1896+ }
1897+
18701898 Generate(generateType);
18711899}
18721900
@@ -1946,9 +1974,7 @@ export function messageFormatting(mes, ch_name, isSystem, isUser, messageId) {
19461974 mes = mes.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
19471975 }
19481976
19491977 if (this_chid === undefined && !selected_groupisSystem) {
1950- mes = mes.replace(/\*\*(.+?)\*\*/g, '<b>$1</b>');
1951- } else if (!isSystem) {
19521978 // Save double quotes in tags as a special character to prevent them from being encoded
19531979 if (!power_user.encode_tags) {
19541980 mes = mes.replace(/<([^>]+)>/g, function (_, contents) {
@@ -1987,13 +2013,6 @@ export function messageFormatting(mes, ch_name, isSystem, isUser, messageId) {
19872013 });
19882014 }
19892015
1990- /*
1991- // Hides bias from empty messages send with slash commands
1992- if (isSystem) {
1993- mes = mes.replace(/\{\{[\s\S]*?\}\}/gm, "");
1994- }
1995- */
1996-
19972016 if (!power_user.allow_name2_display && ch_name && !isUser && !isSystem) {
19982017 mes = mes.replace(new RegExp(`(^|\n)${escapeRegex(ch_name)}:`, 'g'), '$1');
19992018 }
@@ -3400,14 +3419,11 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
34003419 quiet_prompt = main_api == 'novel' && !quietToLoud ? adjustNovelInstructionPrompt(quiet_prompt) : quiet_prompt;
34013420 }
34023421
34033422 const isChatValidhasBackendConnection = online_status !== 'no_connection' && this_chid !== undefined;
34043423
34053424 // We can't do anything because we're not in a chat right now. (Unless it's a dry run, in which case we need to
34063425 // assemble the prompt so we can count its tokens regardless of whether a chat is active.)
34073426 if (!dryRun && !isChatValidhasBackendConnection) {
3408- if (this_chid === undefined) {
3409- toastr.warning('Сharacter is not selected');
3410- }
34113427 is_send_press = false;
34123428 return Promise.resolve();
34133429 }
@@ -3493,9 +3509,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
34933509 setExtensionPrompt('DEPTH_PROMPT_' + index, value.text, extension_prompt_types.IN_CHAT, value.depth, extension_settings.note.allowWIScan, role);
34943510 });
34953511 } else {
34963512 const depthPromptText = baseChatReplace(characters[this_chid]?.data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2) || '';
34973513 const depthPromptDepth = characters[this_chid]?.data?.extensions?.depth_prompt?.depth ?? depth_prompt_depth_default;
34983514 const depthPromptRole = getExtensionPromptRoleByName(characters[this_chid]?.data?.extensions?.depth_prompt?.role ?? depth_prompt_role_default);
34993515 setExtensionPrompt('DEPTH_PROMPT', depthPromptText, extension_prompt_types.IN_CHAT, depthPromptDepth, extension_settings.note.allowWIScan, depthPromptRole);
35003516 }
35013517
@@ -5905,11 +5921,16 @@ export function saveChatDebounced() {
59055921 }, 1000);
59065922}
59075923
59085924export async function saveChat(chat_namechatName, withMetadata, mesId) {
59095925 const metadata = { ...chat_metadata, ...(withMetadata || {}) };
59105926 letconst file_namefileName = chat_namechatName ?? characters[this_chid]?.chat;
59115927
59125928 if (!file_namefileName && name2 === neutralCharacterName) {
5929+ // TODO: Do something for a temporary chat with no character.
5930+ return;
5931+ }
5932+
5933+ if (!fileName) {
59135934 console.warn('saveChat called without chat_name and no chat file found');
59145935 return;
59155936 }
@@ -5950,7 +5971,7 @@ export async function saveChat(chat_name, withMetadata, mesId) {
59505971 url: '/api/chats/save',
59515972 data: JSON.stringify({
59525973 ch_name: characters[this_chid].name,
59535974 file_name: file_namefileName,
59545975 chat: save_chat,
59555976 avatar_url: characters[this_chid].avatar,
59565977 }),
@@ -6665,7 +6686,7 @@ function updateMessage(div) {
66656686function openMessageDelete(fromSlashCommand) {
66666687 closeMessageEditor();
66676688 hideSwipeButtons();
66686689 if (fromSlashCommand || (this_chid != undefined && !is_send_press) || (selected_group && !is_group_generating)) {
66696690 $('#dialogue_del_mes').css('display', 'block');
66706691 $('#send_form').css('display', 'none');
66716692 $('.del_checkbox').each(function () {
@@ -8218,6 +8239,11 @@ function swipe_left() { // when we swipe left..but no generation.
82188239 * @returns {Promise<string>} Branch file name
82198240 */
82208241async function branchChat(mesId) {
8242+ if (this_chid === undefined && !selected_group) {
8243+ toastr.info('No character selected.', 'Branch creation aborted');
8244+ return;
8245+ }
8246+
82218247 const fileName = await createBranch(mesId);
82228248 await saveItemizedPrompts(fileName);
82238249
@@ -8955,6 +8981,14 @@ async function removeCharacterFromUI() {
89558981 saveSettingsDebounced();
89568982}
89578983
8984+async function newAssistantChat() {
8985+ await clearChat();
8986+ chat.splice(0, chat.length);
8987+ chat_metadata = {};
8988+ setCharacterName(neutralCharacterName);
8989+ sendSystemMessage(system_message_types.ASSISTANT_NOTE);
8990+}
8991+
89588992function doTogglePanels() {
89598993 $('#option_settings').trigger('click');
89608994 return '';
@@ -9800,6 +9834,9 @@ jQuery(async function () {
98009834
98019835 await doNewChat({ deleteCurrentChat: deleteCurrentChat });
98029836 }
9837+ if (!selected_group && this_chid === undefined && !is_send_press) {
9838+ await newAssistantChat();
9839+ }
98039840 }
98049841
98059842 else if (id == 'option_regenerate') {
@@ -9850,6 +9887,7 @@ jQuery(async function () {
98509887 $('#rm_button_selected_ch').children('h2').text('');
98519888 select_rm_characters();
98529889 sendSystemMessage(system_message_types.WELCOME);
9890+ sendSystemMessage(system_message_types.WELCOME_PROMPT);
98539891 eventSource.emit(event_types.CHAT_CHANGED, getCurrentChatId());
98549892 await getClientVersion();
98559893 } else {
@@ -10048,7 +10086,7 @@ jQuery(async function () {
1004810086 }
1004910087 else {
1005010088 $(document).on('pointerup', '.mes_copy', function () {
1005110089 if (this_chid !== undefined || selected_group || name2 === neutralCharacterName) {
1005210090 try {
1005310091 const messageId = $(this).closest('.mes').attr('mesid');
1005410092 const text = chat[messageId]['mes'];
@@ -10072,7 +10110,7 @@ jQuery(async function () {
1007210110 //********************
1007310111 //***Message Editor***
1007410112 $(document).on('click', '.mes_edit', async function () {
1007510113 if (this_chid !== undefined || selected_group || name2 === neutralCharacterName) {
1007610114 // Previously system messages we're allowed to be edited
1007710115 /*const message = $(this).closest(".mes");
1007810116
@@ -10664,7 +10702,7 @@ jQuery(async function () {
1066410702 });
1066510703 }
1066610704
1066710705 const avatarSrc = (isDataURL(thumbURL) || /^\/?img\/(?:.+)/.test(thumbURL)) ? thumbURL : charsPath + targetAvatarImg;
1066810706 if ($(`.zoomed_avatar[forChar="${charname}"]`).length) {
1066910707 console.debug('removing container as it already existed');
1067010708 $(`.zoomed_avatar[forChar="${charname}"]`).fadeOut(animation_duration, () => {
public/scripts/bookmarks.js+5 -0
@@ -168,6 +168,11 @@ export async function createBranch(mesId) {
168168}
169169
170170async function createNewBookmark(mesId) {
171+ if (this_chid === undefined && !selected_group) {
172+ toastr.info('No character selected.', 'Checkpoint creation aborted');
173+ return;
174+ }
175+
171176 if (!chat.length) {
172177 toastr.warning('The chat is empty.', 'Checkpoint creation failed');
173178 return;
public/scripts/chats.js+0 -2
@@ -125,8 +125,6 @@ function getConverter(type) {
125125 * @returns {Promise<void>}
126126 */
127127export async function hideChatMessageRange(start, end, unhide) {
128- if (!getCurrentChatId()) return;
129-
130128 if (isNaN(start)) return;
131129 if (!end) end = start;
132130 const hide = !unhide;
public/scripts/templates/assistantNote.html+3 -0
@@ -0,0 +1,3 @@
1+<div>
2+ <b>Note:</b> this chat is temporary and will be deleted as soon as you leave it.
3+</div>
public/scripts/templates/welcomePrompt.html+3 -0
@@ -0,0 +1,3 @@
1+<strong data-i18n="Connect to an API and ask me anything!">
2+ Connect to an API and ask me anything!
3+</strong>