perf(sessions): use loaded thread-info seam

This commit is contained in:
Vincent Koc
2026-04-13 17:49:35 +01:00
parent 8cfdc8dea1
commit 31233a1995
3 changed files with 51 additions and 3 deletions

View File

@@ -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)
);
}

View File

@@ -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: {

View File

@@ -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);
}