Merge pull request #3584 from SillyTavern/chid-unify-type

Always use string for this_chid
This commit is contained in:
Cohee 2025-03-01 00:04:21 +02:00 committed by GitHub
commit 969156d819
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 9 deletions

View File

@ -553,6 +553,10 @@ let generatedPromptCache = '';
let generation_started = new Date(); let generation_started = new Date();
/** @type {import('./scripts/char-data.js').v1CharData[]} */ /** @type {import('./scripts/char-data.js').v1CharData[]} */
export let characters = []; export let characters = [];
/**
* Stringified index of a currently chosen entity in the characters array.
* @type {string|undefined} Yes, we hate it as much as you do.
*/
export let this_chid; export let this_chid;
let saveCharactersPage = 0; let saveCharactersPage = 0;
export const default_avatar = 'img/ai4.png'; export const default_avatar = 'img/ai4.png';
@ -1378,7 +1382,7 @@ export async function selectCharacterById(id) {
return; return;
} }
if (selected_group || this_chid !== id) { if (selected_group || String(this_chid) !== String(id)) {
//if clicked on a different character from what was currently selected //if clicked on a different character from what was currently selected
if (!is_send_press) { if (!is_send_press) {
await clearChat(); await clearChat();
@ -1386,7 +1390,7 @@ export async function selectCharacterById(id) {
resetSelectedGroup(); resetSelectedGroup();
this_edit_mes_id = undefined; this_edit_mes_id = undefined;
selected_button = 'character_edit'; selected_button = 'character_edit';
this_chid = id; setCharacterId(id);
chat.length = 0; chat.length = 0;
chat_metadata = {}; chat_metadata = {};
await getChat(); await getChat();
@ -6210,7 +6214,7 @@ export function resetChatState() {
// replaces deleted charcter name with system user since it will be displayed next. // replaces deleted charcter name with system user since it will be displayed next.
name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName; name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName;
//unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays) //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)
this_chid = undefined; setCharacterId(undefined);
// sets up system user to tell user about having deleted a character // sets up system user to tell user about having deleted a character
chat.splice(0, chat.length, ...SAFETY_CHAT); chat.splice(0, chat.length, ...SAFETY_CHAT);
// resets chat metadata // resets chat metadata
@ -6233,8 +6237,29 @@ export function setExternalAbortController(controller) {
abortController = controller; abortController = controller;
} }
/**
* Sets a character array index.
* @param {number|string|undefined} value
*/
export function setCharacterId(value) { export function setCharacterId(value) {
this_chid = value; switch (typeof value) {
case 'bigint':
case 'number':
this_chid = String(value);
break;
case 'string':
this_chid = !isNaN(parseInt(value)) ? value : undefined;
break;
case 'object':
this_chid = characters.indexOf(value) !== -1 ? String(characters.indexOf(value)) : undefined;
break;
case 'undefined':
this_chid = undefined;
break;
default:
console.error('Invalid character ID type:', value);
break;
}
} }
export function setCharacterName(value) { export function setCharacterName(value) {
@ -6350,13 +6375,13 @@ export async function renameCharacter(name = null, { silent = false, renameChats
if (newChId !== -1) { if (newChId !== -1) {
// Select the character after the renaming // Select the character after the renaming
this_chid = -1; setCharacterId(undefined);
await selectCharacterById(newChId); await selectCharacterById(newChId);
// Async delay to update UI // Async delay to update UI
await delay(1); await delay(1);
if (this_chid === -1) { if (this_chid === undefined) {
throw new Error('New character not selected'); throw new Error('New character not selected');
} }
@ -7658,7 +7683,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
if (previousCharId) { if (previousCharId) {
const newId = characters.findIndex((x) => x.avatar == previousCharId); const newId = characters.findIndex((x) => x.avatar == previousCharId);
if (newId >= 0) { if (newId >= 0) {
this_chid = newId; setCharacterId(newId);
} }
} }
} }

View File

@ -395,7 +395,7 @@ class BulkEditOverlay {
/** /**
* @typedef {object} LastSelected - An object noting the last selected character and its state. * @typedef {object} LastSelected - An object noting the last selected character and its state.
* @property {string} [characterId] - The character id of the last selected character. * @property {number} [characterId] - The character id of the last selected character.
* @property {boolean} [select] - The selected state of the last selected character. <c>true</c> if it was selected, <c>false</c> if it was deselected. * @property {boolean} [select] - The selected state of the last selected character. <c>true</c> if it was selected, <c>false</c> if it was deselected.
*/ */
@ -675,7 +675,7 @@ class BulkEditOverlay {
const characterId = Number(currentCharacter.getAttribute('data-chid')); const characterId = Number(currentCharacter.getAttribute('data-chid'));
const select = !this.selectedCharacters.includes(characterId); const select = !this.selectedCharacters.includes(characterId);
if (this.lastSelected.characterId && this.lastSelected.select !== undefined) { if (this.lastSelected.characterId >= 0 && this.lastSelected.select !== undefined) {
// Only if select state and the last select state match we execute the range select // Only if select state and the last select state match we execute the range select
if (select === this.lastSelected.select) { if (select === this.lastSelected.select) {
this.toggleCharactersInRange(currentCharacter, select); this.toggleCharactersInRange(currentCharacter, select);