diff --git a/src/channels/plugins/session-thread-info-loaded.ts b/src/channels/plugins/session-thread-info-loaded.ts new file mode 100644 index 00000000000..9462f65d6de --- /dev/null +++ b/src/channels/plugins/session-thread-info-loaded.ts @@ -0,0 +1,47 @@ +import { + parseRawSessionConversationRef, + parseThreadSessionSuffix, + type ParsedThreadSessionSuffix, +} from "../../sessions/session-key-utils.js"; +import { normalizeOptionalString } from "../../shared/string-coerce.js"; +import { getLoadedChannelPluginForRead } from "./registry-loaded-read.js"; + +type SessionConversationHookResult = { + id: string; + threadId?: string | null; +}; + +function resolveLoadedSessionConversationThreadInfo( + sessionKey: string | undefined | null, +): ParsedThreadSessionSuffix | null { + const raw = parseRawSessionConversationRef(sessionKey); + if (!raw) { + return null; + } + const rawId = raw.rawId.trim(); + if (!rawId) { + return null; + } + const messaging = getLoadedChannelPluginForRead(raw.channel)?.messaging; + const resolved = messaging?.resolveSessionConversation?.({ + kind: raw.kind, + rawId, + }) as SessionConversationHookResult | null | undefined; + if (!resolved?.id?.trim()) { + return null; + } + const id = resolved.id.trim(); + const threadId = normalizeOptionalString(resolved.threadId); + return { + baseSessionKey: threadId ? `${raw.prefix}:${id}` : normalizeOptionalString(sessionKey), + threadId, + }; +} + +export function resolveLoadedSessionThreadInfo( + sessionKey: string | undefined | null, +): ParsedThreadSessionSuffix { + return ( + resolveLoadedSessionConversationThreadInfo(sessionKey) ?? parseThreadSessionSuffix(sessionKey) + ); +} diff --git a/src/config/sessions/reset.ts b/src/config/sessions/reset.ts index 84109795117..6e147f8fda4 100644 --- a/src/config/sessions/reset.ts +++ b/src/config/sessions/reset.ts @@ -1,4 +1,4 @@ -import { resolveSessionThreadInfo } from "../../channels/plugins/session-conversation.js"; +import { resolveLoadedSessionThreadInfo } from "../../channels/plugins/session-thread-info-loaded.js"; import { normalizeLowercaseStringOrEmpty, normalizeOptionalLowercaseString, @@ -28,7 +28,7 @@ export const DEFAULT_RESET_AT_HOUR = 4; const GROUP_SESSION_MARKERS = [":group:", ":channel:"]; export function isThreadSessionKey(sessionKey?: string | null): boolean { - return Boolean(resolveSessionThreadInfo(sessionKey, { bundledFallback: false }).threadId); + return Boolean(resolveLoadedSessionThreadInfo(sessionKey).threadId); } export function resolveSessionResetType(params: { diff --git a/src/config/sessions/thread-info.ts b/src/config/sessions/thread-info.ts index c1d16c41494..9ccbc8c1004 100644 --- a/src/config/sessions/thread-info.ts +++ b/src/config/sessions/thread-info.ts @@ -1,4 +1,5 @@ import { resolveSessionThreadInfo } from "../../channels/plugins/session-conversation.js"; +import { resolveLoadedSessionThreadInfo } from "../../channels/plugins/session-thread-info-loaded.js"; /** * Extract deliveryContext and threadId from a sessionKey. @@ -15,5 +16,5 @@ export function parseSessionThreadInfoFast(sessionKey: string | undefined): { baseSessionKey: string | undefined; threadId: string | undefined; } { - return resolveSessionThreadInfo(sessionKey, { bundledFallback: false }); + return resolveLoadedSessionThreadInfo(sessionKey); }