back to working after merge,

formatting should match
fixed bug with send butting showing up midway through long pyg generations
optimized changeMainAPI function
This commit is contained in:
RossAsscends
2023-03-18 07:01:25 +09:00
parent 5f2c6b8853
commit bd60065e90
2 changed files with 572 additions and 556 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,15 @@
import { encode } from "../scripts/gpt-2-3-tokenizer/mod.js"; import { encode } from "../scripts/gpt-2-3-tokenizer/mod.js";
import { import {
Generate, Generate,
this_chid, this_chid,
characters, characters,
online_status, online_status,
main_api, main_api,
api_server, api_server,
api_key_novel, api_key_novel,
is_send_press, is_send_press,
} from "../script.js"; } from "../script.js";
import { LoadLocal, SaveLocal, ClearLocal, CheckLocal, LoadLocalBool } from "./f-localStorage.js"; import { LoadLocal, SaveLocal, ClearLocal, CheckLocal, LoadLocalBool } from "./f-localStorage.js";
@ -23,8 +23,8 @@ var AdvancedCharDefsPopup = document.getElementById("character_popup");
var ConfirmationPopup = document.getElementById("dialogue_popup"); var ConfirmationPopup = document.getElementById("dialogue_popup");
var AutoConnectCheckbox = document.getElementById("auto-connect-checkbox"); var AutoConnectCheckbox = document.getElementById("auto-connect-checkbox");
var AutoLoadChatCheckbox = document.getElementById("auto-load-chat-checkbox"); var AutoLoadChatCheckbox = document.getElementById("auto-load-chat-checkbox");
var SelectedNavTab = ("#"+LoadLocal('SelectedNavTab')); var SelectedNavTab = ("#" + LoadLocal('SelectedNavTab'));
var create_save_name; var create_save_name;
var create_save_description; var create_save_description;
var create_save_personality; var create_save_personality;
@ -36,14 +36,14 @@ var perm_tokens;
const observerConfig = { childList: true, subtree: true }; const observerConfig = { childList: true, subtree: true };
const observer = new MutationObserver(function(mutations) { const observer = new MutationObserver(function (mutations) {
mutations.forEach(function(mutation) { mutations.forEach(function (mutation) {
if (mutation.target.id === "online_status_text2") { if (mutation.target.id === "online_status_text2") {
RA_checkOnlineStatus(); RA_checkOnlineStatus();
} else if (mutation.target.parentNode === SelectedCharacterTab) { } else if (mutation.target.parentNode === SelectedCharacterTab) {
setTimeout(RA_CountCharTokens, 200); setTimeout(RA_CountCharTokens, 200);
} }
}); });
}); });
observer.observe(document.documentElement, observerConfig); observer.observe(document.documentElement, observerConfig);
@ -54,19 +54,19 @@ observer.observe(document.documentElement, observerConfig);
//Does not break old characters/chats, as the code just uses whatever timestamp exists in the chat. //Does not break old characters/chats, as the code just uses whatever timestamp exists in the chat.
//New chats made with characters will use this new formatting. //New chats made with characters will use this new formatting.
export function humanizedDateTime() { export function humanizedDateTime() {
let baseDate = new Date(Date.now()); let baseDate = new Date(Date.now());
let humanYear = baseDate.getFullYear(); let humanYear = baseDate.getFullYear();
let humanMonth = baseDate.getMonth() + 1; let humanMonth = baseDate.getMonth() + 1;
let humanDate = baseDate.getDate(); let humanDate = baseDate.getDate();
let humanHour = (baseDate.getHours() < 10 ? "0" : "") + baseDate.getHours(); let humanHour = (baseDate.getHours() < 10 ? "0" : "") + baseDate.getHours();
let humanMinute = let humanMinute =
(baseDate.getMinutes() < 10 ? "0" : "") + baseDate.getMinutes(); (baseDate.getMinutes() < 10 ? "0" : "") + baseDate.getMinutes();
let humanSecond = let humanSecond =
(baseDate.getSeconds() < 10 ? "0" : "") + baseDate.getSeconds(); (baseDate.getSeconds() < 10 ? "0" : "") + baseDate.getSeconds();
let humanMillisecond = let humanMillisecond =
(baseDate.getMilliseconds() < 10 ? "0" : "") + baseDate.getMilliseconds(); (baseDate.getMilliseconds() < 10 ? "0" : "") + baseDate.getMilliseconds();
let HumanizedDateTime = let HumanizedDateTime =
humanYear +"-" +humanMonth +"-" +humanDate +" @" +humanHour +"h " +humanMinute +"m " +humanSecond +"s " +humanMillisecond +"ms"; humanYear + "-" + humanMonth + "-" + humanDate + " @" + humanHour + "h " + humanMinute + "m " + humanSecond + "s " + humanMillisecond + "ms";
return HumanizedDateTime; return HumanizedDateTime;
} }
@ -74,60 +74,61 @@ export function humanizedDateTime() {
// triggers: // triggers:
$("#rm_button_create").on("click", function () { //when "+New Character" is clicked $("#rm_button_create").on("click", function () { //when "+New Character" is clicked
$(SelectedCharacterTab).children("h2").html(''); // empty nav's 3rd panel tab $(SelectedCharacterTab).children("h2").html(''); // empty nav's 3rd panel tab
//empty temp vars to store new char data for counting //empty temp vars to store new char data for counting
create_save_name=""; create_save_name = "";
create_save_description=""; create_save_description = "";
create_save_personality=""; create_save_personality = "";
create_save_first_message=""; create_save_first_message = "";
create_save_scenario=""; create_save_scenario = "";
create_save_mes_example=""; create_save_mes_example = "";
$("#result_info").html('Type to start counting tokens!'); $("#result_info").html('Type to start counting tokens!');
}); });
$("#rm_ch_create_block").on("input", function () {RA_CountCharTokens();}); //when any input is made to the create/edit character form textareas $("#rm_ch_create_block").on("input", function () { RA_CountCharTokens(); }); //when any input is made to the create/edit character form textareas
$("#character_popup").on("input", function () {RA_CountCharTokens();}); //when any input is made to the advanced editing popup textareas $("#character_popup").on("input", function () { RA_CountCharTokens(); }); //when any input is made to the advanced editing popup textareas
//function: //function:
function RA_CountCharTokens() { function RA_CountCharTokens() {
$("#result_info").html(""); $("#result_info").html("");
console.log('RA_TC -- starting with this_chid = '+this_chid); console.log('RA_TC -- starting with this_chid = ' + this_chid);
if (document.getElementById('name_div').style.display == "block"){ //if new char if (document.getElementById('name_div').style.display == "block") { //if new char
$("#form_create").on("input", function () { //fill temp vars with form_create values $("#form_create").on("input", function () { //fill temp vars with form_create values
create_save_name = $("#character_name_pole").val(); create_save_name = $("#character_name_pole").val();
create_save_description = $("#description_textarea").val(); create_save_description = $("#description_textarea").val();
create_save_first_message = $("#firstmessage_textarea").val(); create_save_first_message = $("#firstmessage_textarea").val();
}); });
$("#character_popup").on("input", function () { //fill temp vars with advanced popup values $("#character_popup").on("input", function () { //fill temp vars with advanced popup values
create_save_personality = $("#personality_textarea").val(); create_save_personality = $("#personality_textarea").val();
create_save_scenario = $("#scenario_pole").val(); create_save_scenario = $("#scenario_pole").val();
create_save_mes_example = $("#mes_example_textarea").val(); create_save_mes_example = $("#mes_example_textarea").val();
});
//count total tokens, including those that will be removed from context once chat history is long });
count_tokens = encode(JSON.stringify(
create_save_name +
create_save_description +
create_save_personality +
create_save_scenario +
create_save_first_message +
create_save_mes_example
)).length;
//count permanent tokens that will never get flushed out of context //count total tokens, including those that will be removed from context once chat history is long
perm_tokens = encode(JSON.stringify( count_tokens = encode(JSON.stringify(
create_save_name + create_save_name +
create_save_description + create_save_description +
create_save_personality + create_save_personality +
create_save_scenario create_save_scenario +
)).length; create_save_first_message +
create_save_mes_example
)).length;
} else {if (this_chid !== undefined && this_chid !== "invalid-safety-id") { // if we are counting a valid pre-saved char //count permanent tokens that will never get flushed out of context
perm_tokens = encode(JSON.stringify(
create_save_name +
create_save_description +
create_save_personality +
create_save_scenario
)).length;
} else {
if (this_chid !== undefined && this_chid !== "invalid-safety-id") { // if we are counting a valid pre-saved char
//same as above, all tokens including temporary ones //same as above, all tokens including temporary ones
count_tokens = encode( count_tokens = encode(
JSON.stringify( JSON.stringify(
characters[this_chid].description + characters[this_chid].description +
characters[this_chid].personality + characters[this_chid].personality +
characters[this_chid].scenario + characters[this_chid].scenario +
characters[this_chid].first_mes + characters[this_chid].first_mes +
@ -137,134 +138,134 @@ function RA_CountCharTokens() {
//permanent tokens count //permanent tokens count
perm_tokens = encode( perm_tokens = encode(
JSON.stringify( JSON.stringify(
characters[this_chid].name + characters[this_chid].name +
characters[this_chid].description + characters[this_chid].description +
characters[this_chid].personality + characters[this_chid].personality +
characters[this_chid].scenario characters[this_chid].scenario
)).length; )).length;
} else {console.log("RA_TC -- no valid char found, closing.");} // if neither, probably safety char or some error in loading } else { console.log("RA_TC -- no valid char found, closing."); } // if neither, probably safety char or some error in loading
} }
// display the counted tokens // display the counted tokens
if (count_tokens < 1024 && perm_tokens < 1024) { if (count_tokens < 1024 && perm_tokens < 1024) {
$("#result_info").html(count_tokens + " Tokens (" + perm_tokens + " Permanent Tokens)"); //display normal if both counts are under 1024 $("#result_info").html(count_tokens + " Tokens (" + perm_tokens + " Permanent Tokens)"); //display normal if both counts are under 1024
} else {$("#result_info").html("<font color=red>" +count_tokens +" Tokens (" +perm_tokens +" Permanent Tokens)(TOO MANY)</font>");} //warn if either are over 1024 } else { $("#result_info").html("<font color=red>" + count_tokens + " Tokens (" + perm_tokens + " Permanent Tokens)(TOO MANY)</font>"); } //warn if either are over 1024
} }
//Auto Load Last Charcter -- (fires when active_character is defined and auto_load_chat is true) //Auto Load Last Charcter -- (fires when active_character is defined and auto_load_chat is true)
async function RA_autoloadchat() { async function RA_autoloadchat() {
if (document.getElementById('CharID0') !== null){ if (document.getElementById('CharID0') !== null) {
console.log('char list loaded! clicking activeChar') console.log('char list loaded! clicking activeChar')
var CharToAutoLoad = document.getElementById('CharID'+LoadLocal('ActiveChar')); var CharToAutoLoad = document.getElementById('CharID' + LoadLocal('ActiveChar'));
if (CharToAutoLoad !=null){ if (CharToAutoLoad != null) {
CharToAutoLoad.click(); CharToAutoLoad.click();
}else{ } else {
console.log(CharToAutoLoad + ' ActiveChar local var - not found: '+LoadLocal('ActiveChar')); console.log(CharToAutoLoad + ' ActiveChar local var - not found: ' + LoadLocal('ActiveChar'));
} }
RestoreNavTab(); RestoreNavTab();
}else{ } else {
console.log('no char list yet..') console.log('no char list yet..')
setTimeout(RA_autoloadchat,100) // if the charcter list hadn't been loaded yet, try again. setTimeout(RA_autoloadchat, 100) // if the charcter list hadn't been loaded yet, try again.
} }
} }
//only triggers when AutoLoadChat is enabled, consider adding this as an independent feature later. //only triggers when AutoLoadChat is enabled, consider adding this as an independent feature later.
function RestoreNavTab(){ function RestoreNavTab() {
if($(rm_button_selected_ch).children("h2").text()!==''){ //check for a change in the character edit tab name if ($(rm_button_selected_ch).children("h2").text() !== '') { //check for a change in the character edit tab name
console.log('detected ALC char finished loaded, proceeding to restore tab.'); console.log('detected ALC char finished loaded, proceeding to restore tab.');
$(SelectedNavTab).click() //click to restore saved tab when name has changed (signalling char load is done) $(SelectedNavTab).click() //click to restore saved tab when name has changed (signalling char load is done)
}else{ } else {
setTimeout(RestoreNavTab,100) //if not changed yet, check again after 100ms setTimeout(RestoreNavTab, 100) //if not changed yet, check again after 100ms
} }
} }
//changes input bar and send button display depending on connection status //changes input bar and send button display depending on connection status
function RA_checkOnlineStatus() { function RA_checkOnlineStatus() {
if (online_status == "no_connection") { if (online_status == "no_connection") {
$("#send_textarea").attr("placeholder", "Not connected to API!"); //Input bar placeholder tells users they are not connected $("#send_textarea").attr("placeholder", "Not connected to API!"); //Input bar placeholder tells users they are not connected
$("#send_form").css("background-color", "rgba(100,0,0,0.7)"); //entire input form area is red when not connected $("#send_form").css("background-color", "rgba(100,0,0,0.7)"); //entire input form area is red when not connected
$("#send_but").css("display", "none"); //send button is hidden when not connected; $("#send_but").css("display", "none"); //send button is hidden when not connected;
} else { } else {
if (online_status !== undefined && online_status !== "no_connection") { if (online_status !== undefined && online_status !== "no_connection") {
$("#send_textarea").attr("placeholder", "Type a message..."); //on connect, placeholder tells user to type message $("#send_textarea").attr("placeholder", "Type a message..."); //on connect, placeholder tells user to type message
$("#send_form").css("background-color", "rgba(0,0,0,0.7)"); //on connect, form BG changes to transprent black $("#send_form").css("background-color", "rgba(0,0,0,0.7)"); //on connect, form BG changes to transprent black
if (!is_send_press && !(selected_group && is_group_generating)) { if (!is_send_press && !(selected_group && is_group_generating)) {
$("#send_but").css("display", "inline"); //on connect, send button shows $("#send_but").css("display", "inline"); //on connect, send button shows
} }
} }
} }
} }
//Auto-connect to API (when set to kobold, API URL exists, and auto_connect is true) //Auto-connect to API (when set to kobold, API URL exists, and auto_connect is true)
function RA_autoconnect() { function RA_autoconnect() {
if (typeof online_status !== 'undefined' && (api_server !== '' || api_key_novel !== '')) { if (typeof online_status !== 'undefined' && (api_server !== '' || api_key_novel !== '')) {
if (online_status === "no_connection" && LoadLocalBool('AutoConnectEnabled')) { if (online_status === "no_connection" && LoadLocalBool('AutoConnectEnabled')) {
if (isUrlOrAPIKey(api_server) && main_api === "kobold") { if (isUrlOrAPIKey(api_server) && main_api === "kobold") {
$("#api_url_text").val(api_server); $("#api_url_text").val(api_server);
$("#api_button").click(); $("#api_button").click();
//} else if (isUrlOrAPIKey(api_key_novel) && main_api === "novel") { //} else if (isUrlOrAPIKey(api_key_novel) && main_api === "novel") {
// $("#api_key_novel").val(api_key_novel); // $("#api_key_novel").val(api_key_novel);
// $("#api_button").click(); // $("#api_button").click();
} }
}
} else {
setTimeout(RA_autoconnect, 100);
} }
} } else {
setTimeout(RA_autoconnect, 100);
function isUrlOrAPIKey(string) { }
//const pattern = /^\d{3}-\d{3}-\d{3}-\d{3}$/; //need a sample novelAI key to set this format }
try {
new URL(string); function isUrlOrAPIKey(string) {
return true; //const pattern = /^\d{3}-\d{3}-\d{3}-\d{3}$/; //need a sample novelAI key to set this format
} catch (_) { try {
// return pattern.test(string); new URL(string);
} return true;
} } catch (_) {
// return pattern.test(string);
}
}
$("document").ready(function () { $("document").ready(function () {
// read the state of Nav Lock and whether the nav was open or not before page load. // read the state of Nav Lock and whether the nav was open or not before page load.
$(PanelPin).prop('checked', LoadLocalBool("NavLockOn")); $(PanelPin).prop('checked', LoadLocalBool("NavLockOn"));
if (LoadLocalBool("NavLockOn") == true){$(NavToggle).prop("checked", LoadLocalBool("NavOpened"));} if (LoadLocalBool("NavLockOn") == true) { $(NavToggle).prop("checked", LoadLocalBool("NavOpened")); }
// read the state of AutoConnect and AutoLoadChat. // read the state of AutoConnect and AutoLoadChat.
$(AutoConnectCheckbox).prop("checked",LoadLocalBool("AutoConnectEnabled")); $(AutoConnectCheckbox).prop("checked", LoadLocalBool("AutoConnectEnabled"));
$(AutoLoadChatCheckbox).prop("checked",LoadLocalBool("AutoLoadChatEnabled")); $(AutoLoadChatCheckbox).prop("checked", LoadLocalBool("AutoLoadChatEnabled"));
if (LoadLocalBool('AutoLoadChatEnabled') == true) {RA_autoloadchat();} if (LoadLocalBool('AutoLoadChatEnabled') == true) { RA_autoloadchat(); }
//Autoconnect on page load if enabled, or when api type is changed //Autoconnect on page load if enabled, or when api type is changed
if (LoadLocalBool("AutoConnectEnabled") == true) {RA_autoconnect()} if (LoadLocalBool("AutoConnectEnabled") == true) { RA_autoconnect() }
$("#main_api").change(function () {RA_autoconnect();}); $("#main_api").change(function () { RA_autoconnect(); });
$("#api_button").click(function () {setTimeout(RA_checkOnlineStatus, 100);}); $("#api_button").click(function () { setTimeout(RA_checkOnlineStatus, 100); });
//close the RightNav panel when user clicks outside of it or related panels (adv editing popup, or dialog popups) //close the RightNav panel when user clicks outside of it or related panels (adv editing popup, or dialog popups)
$("html").click(function (e) { $("html").click(function (e) {
if ($(NavToggle).prop("checked") && if ($(NavToggle).prop("checked") &&
!$(PanelPin).prop("checked") && !$(PanelPin).prop("checked") &&
$(e.target).attr("id") !== "nav-toggle" && $(e.target).attr("id") !== "nav-toggle" &&
!RightNavPanel.contains(e.target) && !RightNavPanel.contains(e.target) &&
!AdvancedCharDefsPopup.contains(e.target) && !AdvancedCharDefsPopup.contains(e.target) &&
!ConfirmationPopup.contains(e.target)) { !ConfirmationPopup.contains(e.target)) {
NavToggle.click(); NavToggle.click();
} }
}); });
//save NavLock prefs and record state of the Nav being open or closed //save NavLock prefs and record state of the Nav being open or closed
$(NavToggle).on("change", function () {SaveLocal("NavOpened", $(NavToggle).prop("checked")); }); $(NavToggle).on("change", function () { SaveLocal("NavOpened", $(NavToggle).prop("checked")); });
$(PanelPin).on("change",function () {SaveLocal("NavLockOn", $(PanelPin).prop("checked")); }); $(PanelPin).on("change", function () { SaveLocal("NavLockOn", $(PanelPin).prop("checked")); });
//save AutoConnect and AutoLoadChat prefs //save AutoConnect and AutoLoadChat prefs
$(AutoConnectCheckbox).on("change",function () {SaveLocal("AutoConnectEnabled", $(AutoConnectCheckbox).prop("checked")); }); $(AutoConnectCheckbox).on("change", function () { SaveLocal("AutoConnectEnabled", $(AutoConnectCheckbox).prop("checked")); });
$(AutoLoadChatCheckbox).on("change",function () {SaveLocal("AutoLoadChatEnabled", $(AutoLoadChatCheckbox).prop("checked")); }); $(AutoLoadChatCheckbox).on("change", function () { SaveLocal("AutoLoadChatEnabled", $(AutoLoadChatCheckbox).prop("checked")); });
$("#rm_button_extensions").click(function() { $("#rm_button_extensions").click(function () {
SaveLocal('SelectedNavTab','rm_button_extensions'); SaveLocal('SelectedNavTab', 'rm_button_extensions');
}); });
$("#rm_button_settings").click( function (){SaveLocal('SelectedNavTab','rm_button_settings'); }); $("#rm_button_settings").click(function () { SaveLocal('SelectedNavTab', 'rm_button_settings'); });
$(SelectedCharacterTab).click(function () {SaveLocal('SelectedNavTab','rm_button_selected_ch'); }); $(SelectedCharacterTab).click(function () { SaveLocal('SelectedNavTab', 'rm_button_selected_ch'); });
$("#rm_button_characters").click( function () { //if char list is clicked, in addition to saving it... $("#rm_button_characters").click(function () { //if char list is clicked, in addition to saving it...
SaveLocal('SelectedNavTab','rm_button_characters'); SaveLocal('SelectedNavTab', 'rm_button_characters');
characters.sort(Intl.Collator().compare); // we sort the list characters.sort(Intl.Collator().compare); // we sort the list
}); });
// when a char is selected from the list, save them as the auto-load character for next page load // when a char is selected from the list, save them as the auto-load character for next page load
$(document).on("click", ".character_select",function () {SaveLocal('ActiveChar',$(this).attr('chid')); }); $(document).on("click", ".character_select", function () { SaveLocal('ActiveChar', $(this).attr('chid')); });
//this makes the chat input text area resize vertically to match the text size (limited by CSS at 50% window height) //this makes the chat input text area resize vertically to match the text size (limited by CSS at 50% window height)
$('#send_textarea').on('input', function () { $('#send_textarea').on('input', function () {
@ -274,20 +275,20 @@ $("document").ready(function () {
//Regenerate if user swipes on the last mesage in chat //Regenerate if user swipes on the last mesage in chat
document.addEventListener('swiped-left', function(e) { document.addEventListener('swiped-left', function (e) {
var SwipeButR = $('.swipe_right:last'); var SwipeButR = $('.swipe_right:last');
var SwipeTargetMesClassParent = e.target.closest('.last_mes'); var SwipeTargetMesClassParent = e.target.closest('.last_mes');
if (SwipeTargetMesClassParent !== null){ if (SwipeTargetMesClassParent !== null) {
if(SwipeButR.attr('style')=='display: flex;' == true){ if (SwipeButR.attr('style') == 'display: flex;' == true) {
SwipeButR.click(); SwipeButR.click();
} }
} }
}); });
document.addEventListener('swiped-right', function(e) { document.addEventListener('swiped-right', function (e) {
var SwipeButL = $('.swipe_left:last'); var SwipeButL = $('.swipe_left:last');
var SwipeTargetMesClassParent = e.target.closest('.last_mes'); var SwipeTargetMesClassParent = e.target.closest('.last_mes');
if (SwipeTargetMesClassParent !== null){ if (SwipeTargetMesClassParent !== null) {
if(SwipeButL.attr('style')=='display: flex;' == true){ if (SwipeButL.attr('style') == 'display: flex;' == true) {
SwipeButL.click(); SwipeButL.click();
} }
} }
@ -296,16 +297,16 @@ $("document").ready(function () {
//Additional hotkeys CTRL+ENTER and CTRL+UPARROW //Additional hotkeys CTRL+ENTER and CTRL+UPARROW
document.addEventListener("keydown", (event) => { document.addEventListener("keydown", (event) => {
if (event.ctrlKey && event.key == "Enter") { if (event.ctrlKey && event.key == "Enter") {
// Ctrl+Enter for Regeneration Last Response // Ctrl+Enter for Regeneration Last Response
if (is_send_press == false) { if (is_send_press == false) {
Generate("regenerate"); Generate("regenerate");
} }
} }
if (event.ctrlKey && event.key == "ArrowUp") { if (event.ctrlKey && event.key == "ArrowUp") {
//Ctrl+UpArrow for Connect to last server //Ctrl+UpArrow for Connect to last server
if (online_status === "no_connection") { if (online_status === "no_connection") {
document.getElementById("api_button").click(); document.getElementById("api_button").click();
} }
} }
if (event.ctrlKey && event.key == "ArrowLeft") { //for debug, show all local stored vars if (event.ctrlKey && event.key == "ArrowLeft") { //for debug, show all local stored vars
@ -321,5 +322,5 @@ $("document").ready(function () {
$('.swipe_right:last').click(); $('.swipe_right:last').click();
} }
}); });
}) })