refactor: cache hot channel imports

This commit is contained in:
Peter Steinberger
2026-04-18 20:19:37 +01:00
parent 85912849cc
commit 73e497f9be
4 changed files with 59 additions and 7 deletions

View File

@@ -1,6 +1,15 @@
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
type DiscordPreflightAudioRuntime = typeof import("./preflight-audio.runtime.js");
let discordPreflightAudioRuntimePromise: Promise<DiscordPreflightAudioRuntime> | undefined;
function loadDiscordPreflightAudioRuntime(): Promise<DiscordPreflightAudioRuntime> {
discordPreflightAudioRuntimePromise ??= import("./preflight-audio.runtime.js");
return discordPreflightAudioRuntimePromise;
}
type DiscordAudioAttachment = {
content_type?: string;
url?: string;
@@ -50,7 +59,7 @@ export async function resolveDiscordPreflightAudioMentionContext(params: {
};
}
try {
const { transcribeFirstAudio } = await import("./preflight-audio.runtime.js");
const { transcribeFirstAudio } = await loadDiscordPreflightAudioRuntime();
if (params.abortSignal?.aborted) {
return {
hasAudioAttachment,

View File

@@ -26,9 +26,13 @@ export const DISCORD_TEXT_CHUNK_LIMIT = 2000;
type DiscordSendRuntime = typeof import("./send.js");
type DiscordSendFn = DiscordSendRuntime["sendMessageDiscord"];
type DiscordComponentSendFn = typeof import("./send.components.js").sendDiscordComponentMessage;
type DiscordSharedInteractiveModule = typeof import("./shared-interactive.js");
type DiscordThreadBindingsModule = typeof import("./monitor/thread-bindings.js");
let discordSendRuntimePromise: Promise<DiscordSendRuntime> | undefined;
let discordComponentSendPromise: Promise<DiscordComponentSendFn> | undefined;
let discordSharedInteractivePromise: Promise<DiscordSharedInteractiveModule> | undefined;
let discordThreadBindingsPromise: Promise<DiscordThreadBindingsModule> | undefined;
async function loadDiscordSendRuntime(): Promise<DiscordSendRuntime> {
discordSendRuntimePromise ??= import("./send.js");
@@ -46,6 +50,16 @@ async function sendDiscordComponentMessageLazy(
)(...args);
}
function loadDiscordSharedInteractive(): Promise<DiscordSharedInteractiveModule> {
discordSharedInteractivePromise ??= import("./shared-interactive.js");
return discordSharedInteractivePromise;
}
function loadDiscordThreadBindings(): Promise<DiscordThreadBindingsModule> {
discordThreadBindingsPromise ??= import("./monitor/thread-bindings.js");
return discordThreadBindingsPromise;
}
function hasApprovalChannelData(payload: { channelData?: unknown }): boolean {
const channelData = payload.channelData;
if (!channelData || typeof channelData !== "object" || Array.isArray(channelData)) {
@@ -113,7 +127,7 @@ async function maybeSendDiscordWebhookText(params: {
if (!threadId) {
return null;
}
const { getThreadBindingManager } = await import("./monitor/thread-bindings.js");
const { getThreadBindingManager } = await loadDiscordThreadBindings();
const manager = getThreadBindingManager(params.accountId ?? undefined);
if (!manager) {
return null;
@@ -158,7 +172,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
const rawComponentSpec =
discordData?.components ??
(payload.interactive
? (await import("./shared-interactive.js")).buildDiscordInteractiveComponents(
? (await loadDiscordSharedInteractive()).buildDiscordInteractiveComponents(
payload.interactive,
)
: undefined);

View File

@@ -78,6 +78,8 @@ let acpBindingRuntimePromise:
let sessionBindingRuntimePromise:
| Promise<typeof import("openclaw/plugin-sdk/session-binding-runtime")>
| undefined;
let matrixReactionEventsPromise: Promise<typeof import("./reaction-events.js")> | undefined;
let matrixDraftStreamPromise: Promise<typeof import("../draft-stream.js")> | undefined;
function loadMatrixSendModule(): Promise<typeof import("../send.js")> {
matrixSendModulePromise ??= import("../send.js");
@@ -97,6 +99,17 @@ function loadSessionBindingRuntime(): Promise<
sessionBindingRuntimePromise ??= import("openclaw/plugin-sdk/session-binding-runtime");
return sessionBindingRuntimePromise;
}
function loadMatrixReactionEvents(): Promise<typeof import("./reaction-events.js")> {
matrixReactionEventsPromise ??= import("./reaction-events.js");
return matrixReactionEventsPromise;
}
function loadMatrixDraftStream(): Promise<typeof import("../draft-stream.js")> {
matrixDraftStreamPromise ??= import("../draft-stream.js");
return matrixDraftStreamPromise;
}
const MAX_TRACKED_PAIRING_REPLY_SENDERS = 512;
const MAX_TRACKED_SHARED_DM_CONTEXT_NOTICES = 512;
type MatrixAllowBotsMode = "off" | "mentions" | "all";
@@ -739,7 +752,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
if (isReactionEvent) {
const senderName = await getSenderName();
const { handleInboundMatrixReaction } = await import("./reaction-events.js");
const { handleInboundMatrixReaction } = await loadMatrixReactionEvents();
await handleInboundMatrixReaction({
client,
core,
@@ -1366,7 +1379,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
const quietDraftStreaming = streaming === "quiet";
const draftReplyToId = replyToMode !== "off" && !threadTarget ? _messageId : undefined;
const draftStream: MatrixDraftStreamHandle | undefined = draftStreamingEnabled
? await import("../draft-stream.js").then(({ createMatrixDraftStream }) =>
? await loadMatrixDraftStream().then(({ createMatrixDraftStream }) =>
createMatrixDraftStream({
roomId,
client,

View File

@@ -49,6 +49,22 @@ import { buildTelegramGroupPeerId } from "./bot/helpers.js";
import type { TelegramContext } from "./bot/types.js";
import { isTelegramForumServiceMessage } from "./forum-service-message.js";
type StickerVisionRuntime = typeof import("./sticker-vision.runtime.js");
type MediaUnderstandingRuntime = typeof import("./media-understanding.runtime.js");
let stickerVisionRuntimePromise: Promise<StickerVisionRuntime> | undefined;
let mediaUnderstandingRuntimePromise: Promise<MediaUnderstandingRuntime> | undefined;
function loadStickerVisionRuntime(): Promise<StickerVisionRuntime> {
stickerVisionRuntimePromise ??= import("./sticker-vision.runtime.js");
return stickerVisionRuntimePromise;
}
function loadMediaUnderstandingRuntime(): Promise<MediaUnderstandingRuntime> {
mediaUnderstandingRuntimePromise ??= import("./media-understanding.runtime.js");
return mediaUnderstandingRuntimePromise;
}
export type TelegramInboundBodyResult = {
bodyText: string;
rawBody: string;
@@ -66,7 +82,7 @@ async function resolveStickerVisionSupport(params: {
agentId?: string;
}): Promise<boolean> {
try {
const { resolveStickerVisionSupportRuntime } = await import("./sticker-vision.runtime.js");
const { resolveStickerVisionSupportRuntime } = await loadStickerVisionRuntime();
return await resolveStickerVisionSupportRuntime(params);
} catch {
return false;
@@ -193,7 +209,7 @@ export async function resolveTelegramInboundBody(params: {
if (needsPreflightTranscription) {
try {
const { transcribeFirstAudio } = await import("./media-understanding.runtime.js");
const { transcribeFirstAudio } = await loadMediaUnderstandingRuntime();
const tempCtx: MsgContext = {
MediaPaths: allMedia.length > 0 ? allMedia.map((m) => m.path) : undefined,
MediaTypes: