Merge pull request #3584 from SillyTavern/chid-unify-type Always use string for this_chid

969156d819501b0c9b264aec20dc9ff2bff2dc54

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

Signed
2 files changed, +34 -9Ignore whitespace
public/script.js+32 -7
@@ -553,6 +553,10 @@ let generatedPromptCache = '';
553553let generation_started = new Date();
554554/** @type {import('./scripts/char-data.js').v1CharData[]} */
555555export let characters = [];
556+/**
557+ * Stringified index of a currently chosen entity in the characters array.
558+ * @type {string|undefined} Yes, we hate it as much as you do.
559+ */
556560export let this_chid;
557561let saveCharactersPage = 0;
558562export const default_avatar = 'img/ai4.png';
@@ -1378,7 +1382,7 @@ export async function selectCharacterById(id) {
13781382 return;
13791383 }
13801384
13811385 if (selected_group || String(this_chid) !== String(id)) {
13821386 //if clicked on a different character from what was currently selected
13831387 if (!is_send_press) {
13841388 await clearChat();
@@ -1386,7 +1390,7 @@ export async function selectCharacterById(id) {
13861390 resetSelectedGroup();
13871391 this_edit_mes_id = undefined;
13881392 selected_button = 'character_edit';
1389- this_chid = id;
1393+ setCharacterId(id);
13901394 chat.length = 0;
13911395 chat_metadata = {};
13921396 await getChat();
@@ -6210,7 +6214,7 @@ export function resetChatState() {
62106214 // replaces deleted charcter name with system user since it will be displayed next.
62116215 name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName;
62126216 //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays)
6213- this_chid = undefined;
6217+ setCharacterId(undefined);
62146218 // sets up system user to tell user about having deleted a character
62156219 chat.splice(0, chat.length, ...SAFETY_CHAT);
62166220 // resets chat metadata
@@ -6233,8 +6237,29 @@ export function setExternalAbortController(controller) {
62336237 abortController = controller;
62346238}
62356239
6240+/**
6241+ * Sets a character array index.
6242+ * @param {number|string|undefined} value
6243+ */
62366244export function setCharacterId(value) {
6237- this_chid = value;
6245+ switch (typeof value) {
6246+ case 'bigint':
6247+ case 'number':
6248+ this_chid = String(value);
6249+ break;
6250+ case 'string':
6251+ this_chid = !isNaN(parseInt(value)) ? value : undefined;
6252+ break;
6253+ case 'object':
6254+ this_chid = characters.indexOf(value) !== -1 ? String(characters.indexOf(value)) : undefined;
6255+ break;
6256+ case 'undefined':
6257+ this_chid = undefined;
6258+ break;
6259+ default:
6260+ console.error('Invalid character ID type:', value);
6261+ break;
6262+ }
62386263}
62396264
62406265export function setCharacterName(value) {
@@ -6350,13 +6375,13 @@ export async function renameCharacter(name = null, { silent = false, renameChats
63506375
63516376 if (newChId !== -1) {
63526377 // Select the character after the renaming
6353- this_chid = -1;
6378+ setCharacterId(undefined);
63546379 await selectCharacterById(newChId);
63556380
63566381 // Async delay to update UI
63576382 await delay(1);
63586383
63596384 if (this_chid === -1undefined) {
63606385 throw new Error('New character not selected');
63616386 }
63626387
@@ -7658,7 +7683,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
76587683 if (previousCharId) {
76597684 const newId = characters.findIndex((x) => x.avatar == previousCharId);
76607685 if (newId >= 0) {
7661- this_chid = newId;
7686+ setCharacterId(newId);
76627687 }
76637688 }
76647689}
public/scripts/BulkEditOverlay.js+2 -2
@@ -395,7 +395,7 @@ class BulkEditOverlay {
395395
396396 /**
397397 * @typedef {object} LastSelected - An object noting the last selected character and its state.
398398 * @property {stringnumber} [characterId] - The character id of the last selected character.
399399 * @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.
400400 */
401401
@@ -675,7 +675,7 @@ class BulkEditOverlay {
675675 const characterId = Number(currentCharacter.getAttribute('data-chid'));
676676 const select = !this.selectedCharacters.includes(characterId);
677677
678678 if (this.lastSelected.characterId >= 0 && this.lastSelected.select !== undefined) {
679679 // Only if select state and the last select state match we execute the range select
680680 if (select === this.lastSelected.select) {
681681 this.toggleCharactersInRange(currentCharacter, select);