Merge pull request #3584 from SillyTavern/chid-unify-type Always use string for this_chid
Signed| @@ -553,6 +553,10 @@ let generatedPromptCache = ''; | |||
| 553 | let generation_started = new Date(); | 553 | let generation_started = new Date(); |
| 554 | /** @type {import('./scripts/char-data.js').v1CharData[]} */ | 554 | /** @type {import('./scripts/char-data.js').v1CharData[]} */ |
| 555 | export let characters = []; | 555 | export 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 | */ | ||
| 556 | export let this_chid; | 560 | export let this_chid; |
| 557 | let saveCharactersPage = 0; | 561 | let saveCharactersPage = 0; |
| 558 | export const default_avatar = 'img/ai4.png'; | 562 | export const default_avatar = 'img/ai4.png'; |
| @@ -1378,7 +1382,7 @@ export async function selectCharacterById(id) { | |||
| 1378 | return; | 1382 | return; |
| 1379 | } | 1383 | } |
| 1380 | 1384 | ||
| 1381 | if (selected_group || this_chid !== id) { | 1385 | if (selected_group || String(this_chid) !== String(id)) { |
| 1382 | //if clicked on a different character from what was currently selected | 1386 | //if clicked on a different character from what was currently selected |
| 1383 | if (!is_send_press) { | 1387 | if (!is_send_press) { |
| 1384 | await clearChat(); | 1388 | await clearChat(); |
| @@ -1386,7 +1390,7 @@ export async function selectCharacterById(id) { | |||
| 1386 | resetSelectedGroup(); | 1390 | resetSelectedGroup(); |
| 1387 | this_edit_mes_id = undefined; | 1391 | this_edit_mes_id = undefined; |
| 1388 | selected_button = 'character_edit'; | 1392 | selected_button = 'character_edit'; |
| 1389 | this_chid = id; | 1393 | setCharacterId(id); |
| 1390 | chat.length = 0; | 1394 | chat.length = 0; |
| 1391 | chat_metadata = {}; | 1395 | chat_metadata = {}; |
| 1392 | await getChat(); | 1396 | await getChat(); |
| @@ -6210,7 +6214,7 @@ export function resetChatState() { | |||
| 6210 | // replaces deleted charcter name with system user since it will be displayed next. | 6214 | // replaces deleted charcter name with system user since it will be displayed next. |
| 6211 | name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName; | 6215 | name2 = (this_chid === undefined && neutralCharacterName) ? neutralCharacterName : systemUserName; |
| 6212 | //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays) | 6216 | //unsets expected chid before reloading (related to getCharacters/printCharacters from using old arrays) |
| 6213 | this_chid = undefined; | 6217 | setCharacterId(undefined); |
| 6214 | // sets up system user to tell user about having deleted a character | 6218 | // sets up system user to tell user about having deleted a character |
| 6215 | chat.splice(0, chat.length, ...SAFETY_CHAT); | 6219 | chat.splice(0, chat.length, ...SAFETY_CHAT); |
| 6216 | // resets chat metadata | 6220 | // resets chat metadata |
| @@ -6233,8 +6237,29 @@ export function setExternalAbortController(controller) { | |||
| 6233 | abortController = controller; | 6237 | abortController = controller; |
| 6234 | } | 6238 | } |
| 6235 | 6239 | ||
| 6240 | /** | ||
| 6241 | * Sets a character array index. | ||
| 6242 | * @param {number|string|undefined} value | ||
| 6243 | */ | ||
| 6236 | export function setCharacterId(value) { | 6244 | export 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 | } | ||
| 6238 | } | 6263 | } |
| 6239 | 6264 | ||
| 6240 | export function setCharacterName(value) { | 6265 | export function setCharacterName(value) { |
| @@ -6350,13 +6375,13 @@ export async function renameCharacter(name = null, { silent = false, renameChats | |||
| 6350 | 6375 | ||
| 6351 | if (newChId !== -1) { | 6376 | if (newChId !== -1) { |
| 6352 | // Select the character after the renaming | 6377 | // Select the character after the renaming |
| 6353 | this_chid = -1; | 6378 | setCharacterId(undefined); |
| 6354 | await selectCharacterById(newChId); | 6379 | await selectCharacterById(newChId); |
| 6355 | 6380 | ||
| 6356 | // Async delay to update UI | 6381 | // Async delay to update UI |
| 6357 | await delay(1); | 6382 | await delay(1); |
| 6358 | 6383 | ||
| 6359 | if (this_chid === -1) { | 6384 | if (this_chid === undefined) { |
| 6360 | throw new Error('New character not selected'); | 6385 | throw new Error('New character not selected'); |
| 6361 | } | 6386 | } |
| 6362 | 6387 | ||
| @@ -7658,7 +7683,7 @@ export function select_rm_info(type, charId, previousCharId = null) { | |||
| 7658 | if (previousCharId) { | 7683 | if (previousCharId) { |
| 7659 | const newId = characters.findIndex((x) => x.avatar == previousCharId); | 7684 | const newId = characters.findIndex((x) => x.avatar == previousCharId); |
| 7660 | if (newId >= 0) { | 7685 | if (newId >= 0) { |
| 7661 | this_chid = newId; | 7686 | setCharacterId(newId); |
| 7662 | } | 7687 | } |
| 7663 | } | 7688 | } |
| 7664 | } | 7689 | } |
| @@ -395,7 +395,7 @@ class BulkEditOverlay { | |||
| 395 | 395 | ||
| 396 | /** | 396 | /** |
| 397 | * @typedef {object} LastSelected - An object noting the last selected character and its state. | 397 | * @typedef {object} LastSelected - An object noting the last selected character and its state. |
| 398 | * @property {string} [characterId] - The character id of the last selected character. | 398 | * @property {number} [characterId] - The character id of the last selected character. |
| 399 | * @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. | 399 | * @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. |
| 400 | */ | 400 | */ |
| 401 | 401 | ||
| @@ -675,7 +675,7 @@ class BulkEditOverlay { | |||
| 675 | const characterId = Number(currentCharacter.getAttribute('data-chid')); | 675 | const characterId = Number(currentCharacter.getAttribute('data-chid')); |
| 676 | const select = !this.selectedCharacters.includes(characterId); | 676 | const select = !this.selectedCharacters.includes(characterId); |
| 677 | 677 | ||
| 678 | if (this.lastSelected.characterId && this.lastSelected.select !== undefined) { | 678 | if (this.lastSelected.characterId >= 0 && this.lastSelected.select !== undefined) { |
| 679 | // Only if select state and the last select state match we execute the range select | 679 | // Only if select state and the last select state match we execute the range select |
| 680 | if (select === this.lastSelected.select) { | 680 | if (select === this.lastSelected.select) { |
| 681 | this.toggleCharactersInRange(currentCharacter, select); | 681 | this.toggleCharactersInRange(currentCharacter, select); |