From 40bf65f5a4c8326f36f98ce1eb2200e0d856cc90 Mon Sep 17 00:00:00 2001 From: Youzini-afk <13153778771cx@gmail.com> Date: Tue, 14 Apr 2026 00:34:12 +0800 Subject: [PATCH] fix: restore chat identity helpers for graph load --- index.js | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) diff --git a/index.js b/index.js index 00122a9..5834ba7 100644 --- a/index.js +++ b/index.js @@ -313,6 +313,48 @@ let _themesModule = null; const SERVER_SETTINGS_FILENAME = "st-bme-settings.json"; const SERVER_SETTINGS_URL = `/user/files/${SERVER_SETTINGS_FILENAME}`; +function normalizeChatIdCandidate(value = "") { + return String(value ?? "").trim(); +} + +function readGlobalCurrentChatId() { + try { + return normalizeChatIdCandidate( + globalThis.SillyTavern?.getCurrentChatId?.() || + globalThis.getCurrentChatId?.() || + "", + ); + } catch { + return ""; + } +} + +function hasLikelySelectedChatContext(context = getContext()) { + if (!context || typeof context !== "object") { + return false; + } + + const hasMeaningfulChatMetadata = + context.chatMetadata && + typeof context.chatMetadata === "object" && + !Array.isArray(context.chatMetadata) && + Object.keys(context.chatMetadata).length > 0; + const hasChatMessages = + Array.isArray(context.chat) && context.chat.length > 0; + const hasCharacterId = + context.characterId !== undefined && + context.characterId !== null && + String(context.characterId).trim() !== ""; + const hasGroupId = + context.groupId !== undefined && + context.groupId !== null && + String(context.groupId).trim() !== ""; + + return ( + hasMeaningfulChatMetadata || hasChatMessages || hasCharacterId || hasGroupId + ); +} + function getChatMetadataIntegrity(context = getContext()) { return normalizeChatIdCandidate(context?.chatMetadata?.integrity); } @@ -321,6 +363,129 @@ function getChatCommitMarker(context = getContext()) { return readGraphCommitMarker(context); } +function resolveCurrentHostChatId(context = getContext()) { + const candidates = [ + context?.chatId, + context?.getCurrentChatId?.(), + readGlobalCurrentChatId(), + context?.chatMetadata?.chat_id, + context?.chatMetadata?.chatId, + context?.chatMetadata?.session_id, + context?.chatMetadata?.sessionId, + ]; + + return ( + candidates + .map((candidate) => normalizeChatIdCandidate(candidate)) + .find(Boolean) || "" + ); +} + +function resolveCurrentChatIdentity(context = getContext()) { + const hostChatId = resolveCurrentHostChatId(context); + const integrity = + typeof getChatMetadataIntegrity === "function" + ? getChatMetadataIntegrity(context) + : normalizeChatIdCandidate( + context?.chatMetadata?.integrity || + context?.chatMetadata?.chat_id || + context?.chatMetadata?.chatId || + "", + ); + const aliasedChatId = + !integrity && + hostChatId && + typeof resolveGraphIdentityAliasByHostChatId === "function" + ? resolveGraphIdentityAliasByHostChatId(hostChatId) + : ""; + const chatId = integrity || aliasedChatId || hostChatId; + + return { + chatId, + hostChatId, + integrity, + identitySource: integrity + ? "integrity" + : aliasedChatId + ? "alias" + : hostChatId + ? "host-chat-id" + : "", + hasLikelySelectedChat: hasLikelySelectedChatContext(context), + }; +} + +function getCurrentChatId(context = getContext()) { + return resolveCurrentChatIdentity(context).chatId; +} + +function rememberResolvedGraphIdentityAlias( + context = getContext(), + persistenceChatId = getCurrentChatId(context), +) { + const identity = resolveCurrentChatIdentity(context); + if (!identity.integrity || !persistenceChatId) { + return null; + } + + return rememberGraphIdentityAlias({ + integrity: identity.integrity, + hostChatId: identity.hostChatId, + persistenceChatId, + }); +} + +function doesChatIdMatchResolvedGraphIdentity( + candidateChatId, + identity = resolveCurrentChatIdentity(getContext()), +) { + const normalizedCandidate = normalizeChatIdCandidate(candidateChatId); + if (!normalizedCandidate || !identity || typeof identity !== "object") { + return false; + } + + const knownChatIds = new Set(); + const addKnownChatId = (value) => { + const normalized = normalizeChatIdCandidate(value); + if (normalized) { + knownChatIds.add(normalized); + } + }; + + addKnownChatId(identity.chatId); + addKnownChatId(identity.hostChatId); + addKnownChatId(identity.integrity); + + for (const aliasCandidate of getGraphIdentityAliasCandidates({ + integrity: identity.integrity, + hostChatId: identity.hostChatId, + persistenceChatId: identity.chatId, + })) { + addKnownChatId(aliasCandidate); + } + + return knownChatIds.has(normalizedCandidate); +} + +function areChatIdsEquivalentForResolvedIdentity( + candidateChatId, + referenceChatId, + identity = resolveCurrentChatIdentity(getContext()), +) { + const normalizedCandidate = normalizeChatIdCandidate(candidateChatId); + const normalizedReference = normalizeChatIdCandidate(referenceChatId); + if (!normalizedCandidate || !normalizedReference) { + return normalizedCandidate === normalizedReference; + } + if (normalizedCandidate === normalizedReference) { + return true; + } + return ( + doesChatIdMatchResolvedGraphIdentity(normalizedCandidate, identity) && + doesChatIdMatchResolvedGraphIdentity(normalizedReference, identity) + ); +} + function syncCommitMarkerToPersistenceState(context = getContext()) { const marker = getChatCommitMarker(context); updateGraphPersistenceState({ @@ -4406,6 +4571,48 @@ function readLegacyGraphFromChatMetadata(chatId, context = getContext()) { } } +function buildLegacyGraphIdentityCandidates( + targetChatId, + context = getContext(), + { shadowSnapshot = null } = {}, +) { + const normalizedTargetChatId = normalizeChatIdCandidate(targetChatId); + const identity = resolveCurrentChatIdentity(context); + const candidates = new Set(); + const addCandidate = (value) => { + const normalized = normalizeChatIdCandidate(value); + if (!normalized || normalized === normalizedTargetChatId) return; + candidates.add(normalized); + }; + + addCandidate(identity.hostChatId); + for (const aliasCandidate of getGraphIdentityAliasCandidates({ + integrity: identity.integrity, + hostChatId: identity.hostChatId, + persistenceChatId: normalizedTargetChatId, + })) { + addCandidate(aliasCandidate); + } + + const currentGraphMeta = getGraphPersistenceMeta(currentGraph) || {}; + const runtimeGraphIntegrity = normalizeChatIdCandidate( + currentGraphMeta.integrity || graphPersistenceState.metadataIntegrity, + ); + if ( + identity.integrity && + runtimeGraphIntegrity && + runtimeGraphIntegrity === identity.integrity + ) { + addCandidate(graphPersistenceState.chatId); + addCandidate(currentGraph?.historyState?.chatId); + addCandidate(currentGraphMeta.chatId); + } + + addCandidate(shadowSnapshot?.chatId); + addCandidate(shadowSnapshot?.persistedChatId); + return Array.from(candidates); +} + async function maybeRecoverIndexedDbGraphFromStableIdentity( chatId, context = getContext(),