diff --git a/public/script.js b/public/script.js index 373d2077a..58f3acbc2 100644 --- a/public/script.js +++ b/public/script.js @@ -1304,10 +1304,11 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs"). if (main_api === 'extension') { if (typeof extension_generation_function !== 'function') { callPopup('No extensions are hooked up to a generation process. Check you extension settings!', 'text'); + activateSendButtons(); return; } - await extension_generation_function(chat2, storyString, mesExamplesArray, promptBias, extension_prompt, worldInfoBefore, worldInfoAfter); + await extension_generation_function(type, chat2, storyString, mesExamplesArray, promptBias, extension_prompt, worldInfoBefore, worldInfoAfter); return; } @@ -1734,47 +1735,7 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs"). if (force_name2) this_mes_is_name = true; //getMessage = getMessage.replace(/^\s+/g, ''); if (getMessage.length > 0) { - if (chat.length && (chat[chat.length - 1]['swipe_id'] === undefined || - chat[chat.length - 1]['is_user'])) { - type = 'normal'; - } - if (type === 'swipe') { - - chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipes'].length] = getMessage; - if (chat[chat.length - 1]['swipe_id'] === chat[chat.length - 1]['swipes'].length - 1) { - //console.log(getMessage); - chat[chat.length - 1]['mes'] = getMessage; - // console.log('runGenerate calls addOneMessage for swipe'); - addOneMessage(chat[chat.length - 1], 'swipe'); - } else { - chat[chat.length - 1]['mes'] = getMessage; - } - is_send_press = false; - } else { - console.log('entering chat update routine for non-swipe post'); - is_send_press = false; - chat[chat.length] = {}; - chat[chat.length - 1]['name'] = name2; - chat[chat.length - 1]['is_user'] = false; - chat[chat.length - 1]['is_name'] = this_mes_is_name; - chat[chat.length - 1]['send_date'] = humanizedDateTime(); - getMessage = $.trim(getMessage); - chat[chat.length - 1]['mes'] = getMessage; - - if (selected_group) { - console.log('entering chat update for groups'); - let avatarImg = 'img/ai4.png'; - if (characters[this_chid].avatar != 'none') { - avatarImg = `/thumbnail?type=avatar&file=${encodeURIComponent(characters[this_chid].avatar)}&${Date.now()}`; - } - chat[chat.length - 1]['is_name'] = true; - chat[chat.length - 1]['force_avatar'] = avatarImg; - } - //console.log('runGenerate calls addOneMessage'); - addOneMessage(chat[chat.length - 1]); - - activateSendButtons(); - } + ({ type, getMessage } = saveReply(type, getMessage, this_mes_is_name)); } else { // regenerate with character speech reenforced // to make sure we leave on swipe type while also adding the name2 appendage @@ -1818,6 +1779,51 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs"). console.log('generate ending'); } //generate ends +function saveReply(type, getMessage, this_mes_is_name) { + if (chat.length && (chat[chat.length - 1]['swipe_id'] === undefined || + chat[chat.length - 1]['is_user'])) { + type = 'normal'; + } + + if (type === 'swipe') { + chat[chat.length - 1]['swipes'][chat[chat.length - 1]['swipes'].length] = getMessage; + if (chat[chat.length - 1]['swipe_id'] === chat[chat.length - 1]['swipes'].length - 1) { + //console.log(getMessage); + chat[chat.length - 1]['mes'] = getMessage; + // console.log('runGenerate calls addOneMessage for swipe'); + addOneMessage(chat[chat.length - 1], 'swipe'); + } else { + chat[chat.length - 1]['mes'] = getMessage; + } + is_send_press = false; + } else { + console.log('entering chat update routine for non-swipe post'); + is_send_press = false; + chat[chat.length] = {}; + chat[chat.length - 1]['name'] = name2; + chat[chat.length - 1]['is_user'] = false; + chat[chat.length - 1]['is_name'] = this_mes_is_name; + chat[chat.length - 1]['send_date'] = humanizedDateTime(); + getMessage = $.trim(getMessage); + chat[chat.length - 1]['mes'] = getMessage; + + if (selected_group) { + console.log('entering chat update for groups'); + let avatarImg = 'img/ai4.png'; + if (characters[this_chid].avatar != 'none') { + avatarImg = `/thumbnail?type=avatar&file=${encodeURIComponent(characters[this_chid].avatar)}&${Date.now()}`; + } + chat[chat.length - 1]['is_name'] = true; + chat[chat.length - 1]['force_avatar'] = avatarImg; + } + //console.log('runGenerate calls addOneMessage'); + addOneMessage(chat[chat.length - 1]); + + activateSendButtons(); + } + return { type, getMessage }; +} + function isMultigenEnabled() { return multigen && (main_api == 'textgenerationwebui' || main_api == 'kobold' || main_api == 'novel'); } @@ -2094,6 +2100,7 @@ function changeMainAPI() { if (main_api == "extension") { online_status = "Connected"; + checkOnlineStatus(); } } @@ -2934,6 +2941,7 @@ window["TavernAI"].getContext = function () { generationFunction: extension_generation_function, activateSendButtons, deactivateSendButtons, + saveReply, }; }; diff --git a/public/scripts/extensions/poe/index.js b/public/scripts/extensions/poe/index.js index 0fd46bf4d..d150f750c 100644 --- a/public/scripts/extensions/poe/index.js +++ b/public/scripts/extensions/poe/index.js @@ -1,5 +1,6 @@ import { getContext, getApiUrl } from "../../extensions.js"; import { delay } from "../../utils.js"; +import { showSwipeButtons, hideSwipeButtons } from "../../../script.js"; const TOKEN_KEY = 'extensions_poe_token'; const BOT_KEY = 'extensions_poe_bot'; @@ -73,56 +74,56 @@ function onBotChange() { saveSettings(); } -async function generate(chat2, storyString, mesExamplesArray, promptBias, extension_prompt, worldInfoBefore, worldInfoAfter) { +async function generate(type, chat2, storyString, mesExamplesArray, promptBias, extension_prompt, worldInfoBefore, worldInfoAfter) { const context = getContext(); context.deactivateSendButtons(); + hideSwipeButtons(); - await purgeConversation(); - - let jailbroken = false; - - for (let retryNumber = 0; retryNumber < MAX_RETRIES_FOR_ACTIVATION; retryNumber++) { - const reply = await sendMessage(jailbreak_message); - - if (reply.toLowerCase().includes(jailbreak_response.toLowerCase())) { - jailbroken = true; - break; + try { + await purgeConversation(); + + let jailbroken = false; + + for (let retryNumber = 0; retryNumber < MAX_RETRIES_FOR_ACTIVATION; retryNumber++) { + const reply = await sendMessage(jailbreak_message); + + if (reply.toLowerCase().includes(jailbreak_response.toLowerCase())) { + jailbroken = true; + break; + } } - } - - if (!jailbroken) { - console.log('Could not jailbreak the bot'); - } - - let prompt = [worldInfoBefore, storyString, worldInfoAfter, extension_prompt, promptBias].join('\n').replace(//gm, '').trim(); - let messageSendString = ''; - - for (const item of [...chat2, ...mesExamplesArray]) { - const promptLength = context.encode(prompt + messageSendString + item).length; - await delay(1); - - if (promptLength >= Number(max_context)) { - break; + + if (!jailbroken) { + console.log('Could not jailbreak the bot'); } - - messageSendString = item + messageSendString; + + let prompt = [worldInfoBefore, storyString, worldInfoAfter, extension_prompt, promptBias].join('\n').replace(//gm, '').trim(); + let messageSendString = ''; + + for (const item of [...chat2, ...mesExamplesArray]) { + const promptLength = context.encode(prompt + messageSendString + item).length; + await delay(1); + + if (promptLength >= Number(max_context)) { + break; + } + + messageSendString = item + messageSendString; + } + + const finalPrompt = [prompt, messageSendString, `[Write the next reply as ${context.name2} only]`].join('\n'); + const reply = await sendMessage(finalPrompt); + + context.saveReply(type, reply, true); + context.saveChat(); + } + catch (err) { + console.error(err); + } + finally { + context.activateSendButtons(); + showSwipeButtons(); } - - const finalPrompt = [prompt, messageSendString, `[Write the next reply as ${context.name2} only]`].join('\n'); - const reply = await sendMessage(finalPrompt); - - const message = { - name: context.name2, - is_user: false, - is_name: true, - send_date: Date.now(), - mes: reply, - }; - - context.chat.push(message); - context.addOneMessage(message); - context.saveChat(); - context.activateSendButtons(); } async function purgeConversation() {