Persist active chat across devices.

This commit is contained in:
city-unit 2023-07-29 19:48:08 -04:00
parent 314b194891
commit 6829f5308f
2 changed files with 45 additions and 8 deletions

View File

@ -737,6 +737,9 @@ let token;
var PromptArrayItemForRawPromptDisplay; var PromptArrayItemForRawPromptDisplay;
export let active_character = ""
export let active_group = ""
export function getRequestHeaders() { export function getRequestHeaders() {
return { return {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -786,6 +789,14 @@ function checkOnlineStatus() {
} }
} }
export function setActiveCharacter(character) {
active_character = character;
}
export function setActiveGroup(group) {
active_group = group;
}
async function getStatus() { async function getStatus() {
if (is_get_status) { if (is_get_status) {
if (main_api == "koboldhorde") { if (main_api == "koboldhorde") {
@ -5009,6 +5020,10 @@ async function getSettings(type) {
highlightSelectedAvatar(); highlightSelectedAvatar();
setPersonaDescription(); setPersonaDescription();
//Load the active character and group
active_character = settings.active_character;
active_group = settings.active_group;
//Load the API server URL from settings //Load the API server URL from settings
api_server = settings.api_server; api_server = settings.api_server;
$("#api_url_text").val(api_server); $("#api_url_text").val(api_server);
@ -5049,6 +5064,8 @@ async function saveSettings(type) {
data: JSON.stringify({ data: JSON.stringify({
firstRun: firstRun, firstRun: firstRun,
username: name1, username: name1,
active_character: active_character,
active_group: active_group,
api_server: api_server, api_server: api_server,
api_server_textgenerationwebui: api_server_textgenerationwebui, api_server_textgenerationwebui: api_server_textgenerationwebui,
preset_settings: preset_settings, preset_settings: preset_settings,

View File

@ -13,6 +13,10 @@ import {
menu_type, menu_type,
max_context, max_context,
saveSettingsDebounced, saveSettingsDebounced,
active_group,
active_character,
setActiveGroup,
setActiveCharacter,
} from "../script.js"; } from "../script.js";
import { import {
@ -330,11 +334,23 @@ export function RA_CountCharTokens() {
characterStatsHandler(characters, this_chid); characterStatsHandler(characters, this_chid);
}); });
} }
//Auto Load Last Charcter -- (fires when active_character is defined and auto_load_chat is true) /**
* Auto load chat with the last active character or group.
* Fires when active_character is defined and auto_load_chat is true.
* The function first tries to find a character with a specific ID from the global settings.
* If it doesn't exist, it tries to find a group with a specific grid from the global settings.
* If the character list hadn't been loaded yet, it calls itself again after 100ms delay.
* The character or group is selected (clicked) if it is found.
*/
async function RA_autoloadchat() { async function RA_autoloadchat() {
if (document.getElementById('CharID0') !== null) { if (document.getElementById('CharID0') !== null) {
var charToAutoLoad = document.getElementById('CharID' + LoadLocal('ActiveChar')); // active character is the name, we should look it up in the character list and get the id
let groupToAutoLoad = document.querySelector(`.group_select[grid="${LoadLocal('ActiveGroup')}"]`); let active_character_id = Object.keys(characters).find(key => characters[key].avatar === active_character);
// active group is the name, we should look it up in the group list and get the id
let active_group_id = groups.find(x => x.name === active_group)?.id;
var charToAutoLoad = document.getElementById('CharID' + active_character_id);
let groupToAutoLoad = document.querySelector(`.group_select[grid="${active_group_id}"]`);
if (charToAutoLoad != null) { if (charToAutoLoad != null) {
$(charToAutoLoad).click(); $(charToAutoLoad).click();
} }
@ -342,7 +358,7 @@ async function RA_autoloadchat() {
$(groupToAutoLoad).click(); $(groupToAutoLoad).click();
} }
// if the charcter list hadn't been loaded yet, try again. // if the character list hadn't been loaded yet, try again.
} else { setTimeout(RA_autoloadchat, 100); } } else { setTimeout(RA_autoloadchat, 100); }
} }
@ -903,16 +919,20 @@ $("document").ready(function () {
$("#rm_button_characters").click(function () { SaveLocal('SelectedNavTab', 'rm_button_characters'); }); $("#rm_button_characters").click(function () { SaveLocal('SelectedNavTab', 'rm_button_characters'); });
// 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
// when a char is selected from the list, save their name as the auto-load character for next page load
$(document).on("click", ".character_select", function () { $(document).on("click", ".character_select", function () {
SaveLocal('ActiveChar', $(this).attr('chid')); setActiveCharacter($(this).find('.avatar').attr('title'));
SaveLocal('ActiveGroup', null); setActiveGroup(null);
}); });
$(document).on("click", ".group_select", function () { $(document).on("click", ".group_select", function () {
SaveLocal('ActiveChar', null); setActiveCharacter(null);
SaveLocal('ActiveGroup', $(this).data('id')); setActiveGroup($(this).data('id'));
}); });
//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 () {
this.style.height = '40px'; this.style.height = '40px';