Skip to content

Commit 73e497f

Browse files
committed
refactor: cache hot channel imports
1 parent 8591284 commit 73e497f

4 files changed

Lines changed: 59 additions & 7 deletions

File tree

extensions/discord/src/monitor/preflight-audio.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
22
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
33

4+
type DiscordPreflightAudioRuntime = typeof import("./preflight-audio.runtime.js");
5+
6+
let discordPreflightAudioRuntimePromise: Promise<DiscordPreflightAudioRuntime> | undefined;
7+
8+
function loadDiscordPreflightAudioRuntime(): Promise<DiscordPreflightAudioRuntime> {
9+
discordPreflightAudioRuntimePromise ??= import("./preflight-audio.runtime.js");
10+
return discordPreflightAudioRuntimePromise;
11+
}
12+
413
type DiscordAudioAttachment = {
514
content_type?: string;
615
url?: string;
@@ -50,7 +59,7 @@ export async function resolveDiscordPreflightAudioMentionContext(params: {
5059
};
5160
}
5261
try {
53-
const { transcribeFirstAudio } = await import("./preflight-audio.runtime.js");
62+
const { transcribeFirstAudio } = await loadDiscordPreflightAudioRuntime();
5463
if (params.abortSignal?.aborted) {
5564
return {
5665
hasAudioAttachment,

extensions/discord/src/outbound-adapter.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ export const DISCORD_TEXT_CHUNK_LIMIT = 2000;
2626
type DiscordSendRuntime = typeof import("./send.js");
2727
type DiscordSendFn = DiscordSendRuntime["sendMessageDiscord"];
2828
type DiscordComponentSendFn = typeof import("./send.components.js").sendDiscordComponentMessage;
29+
type DiscordSharedInteractiveModule = typeof import("./shared-interactive.js");
30+
type DiscordThreadBindingsModule = typeof import("./monitor/thread-bindings.js");
2931

3032
let discordSendRuntimePromise: Promise<DiscordSendRuntime> | undefined;
3133
let discordComponentSendPromise: Promise<DiscordComponentSendFn> | undefined;
34+
let discordSharedInteractivePromise: Promise<DiscordSharedInteractiveModule> | undefined;
35+
let discordThreadBindingsPromise: Promise<DiscordThreadBindingsModule> | undefined;
3236

3337
async function loadDiscordSendRuntime(): Promise<DiscordSendRuntime> {
3438
discordSendRuntimePromise ??= import("./send.js");
@@ -46,6 +50,16 @@ async function sendDiscordComponentMessageLazy(
4650
)(...args);
4751
}
4852

53+
function loadDiscordSharedInteractive(): Promise<DiscordSharedInteractiveModule> {
54+
discordSharedInteractivePromise ??= import("./shared-interactive.js");
55+
return discordSharedInteractivePromise;
56+
}
57+
58+
function loadDiscordThreadBindings(): Promise<DiscordThreadBindingsModule> {
59+
discordThreadBindingsPromise ??= import("./monitor/thread-bindings.js");
60+
return discordThreadBindingsPromise;
61+
}
62+
4963
function hasApprovalChannelData(payload: { channelData?: unknown }): boolean {
5064
const channelData = payload.channelData;
5165
if (!channelData || typeof channelData !== "object" || Array.isArray(channelData)) {
@@ -113,7 +127,7 @@ async function maybeSendDiscordWebhookText(params: {
113127
if (!threadId) {
114128
return null;
115129
}
116-
const { getThreadBindingManager } = await import("./monitor/thread-bindings.js");
130+
const { getThreadBindingManager } = await loadDiscordThreadBindings();
117131
const manager = getThreadBindingManager(params.accountId ?? undefined);
118132
if (!manager) {
119133
return null;
@@ -158,7 +172,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
158172
const rawComponentSpec =
159173
discordData?.components ??
160174
(payload.interactive
161-
? (await import("./shared-interactive.js")).buildDiscordInteractiveComponents(
175+
? (await loadDiscordSharedInteractive()).buildDiscordInteractiveComponents(
162176
payload.interactive,
163177
)
164178
: undefined);

extensions/matrix/src/matrix/monitor/handler.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ let acpBindingRuntimePromise:
7878
let sessionBindingRuntimePromise:
7979
| Promise<typeof import("openclaw/plugin-sdk/session-binding-runtime")>
8080
| undefined;
81+
let matrixReactionEventsPromise: Promise<typeof import("./reaction-events.js")> | undefined;
82+
let matrixDraftStreamPromise: Promise<typeof import("../draft-stream.js")> | undefined;
8183

8284
function loadMatrixSendModule(): Promise<typeof import("../send.js")> {
8385
matrixSendModulePromise ??= import("../send.js");
@@ -97,6 +99,17 @@ function loadSessionBindingRuntime(): Promise<
9799
sessionBindingRuntimePromise ??= import("openclaw/plugin-sdk/session-binding-runtime");
98100
return sessionBindingRuntimePromise;
99101
}
102+
103+
function loadMatrixReactionEvents(): Promise<typeof import("./reaction-events.js")> {
104+
matrixReactionEventsPromise ??= import("./reaction-events.js");
105+
return matrixReactionEventsPromise;
106+
}
107+
108+
function loadMatrixDraftStream(): Promise<typeof import("../draft-stream.js")> {
109+
matrixDraftStreamPromise ??= import("../draft-stream.js");
110+
return matrixDraftStreamPromise;
111+
}
112+
100113
const MAX_TRACKED_PAIRING_REPLY_SENDERS = 512;
101114
const MAX_TRACKED_SHARED_DM_CONTEXT_NOTICES = 512;
102115
type MatrixAllowBotsMode = "off" | "mentions" | "all";
@@ -739,7 +752,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
739752

740753
if (isReactionEvent) {
741754
const senderName = await getSenderName();
742-
const { handleInboundMatrixReaction } = await import("./reaction-events.js");
755+
const { handleInboundMatrixReaction } = await loadMatrixReactionEvents();
743756
await handleInboundMatrixReaction({
744757
client,
745758
core,
@@ -1366,7 +1379,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
13661379
const quietDraftStreaming = streaming === "quiet";
13671380
const draftReplyToId = replyToMode !== "off" && !threadTarget ? _messageId : undefined;
13681381
const draftStream: MatrixDraftStreamHandle | undefined = draftStreamingEnabled
1369-
? await import("../draft-stream.js").then(({ createMatrixDraftStream }) =>
1382+
? await loadMatrixDraftStream().then(({ createMatrixDraftStream }) =>
13701383
createMatrixDraftStream({
13711384
roomId,
13721385
client,

extensions/telegram/src/bot-message-context.body.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ import { buildTelegramGroupPeerId } from "./bot/helpers.js";
4949
import type { TelegramContext } from "./bot/types.js";
5050
import { isTelegramForumServiceMessage } from "./forum-service-message.js";
5151

52+
type StickerVisionRuntime = typeof import("./sticker-vision.runtime.js");
53+
type MediaUnderstandingRuntime = typeof import("./media-understanding.runtime.js");
54+
55+
let stickerVisionRuntimePromise: Promise<StickerVisionRuntime> | undefined;
56+
let mediaUnderstandingRuntimePromise: Promise<MediaUnderstandingRuntime> | undefined;
57+
58+
function loadStickerVisionRuntime(): Promise<StickerVisionRuntime> {
59+
stickerVisionRuntimePromise ??= import("./sticker-vision.runtime.js");
60+
return stickerVisionRuntimePromise;
61+
}
62+
63+
function loadMediaUnderstandingRuntime(): Promise<MediaUnderstandingRuntime> {
64+
mediaUnderstandingRuntimePromise ??= import("./media-understanding.runtime.js");
65+
return mediaUnderstandingRuntimePromise;
66+
}
67+
5268
export type TelegramInboundBodyResult = {
5369
bodyText: string;
5470
rawBody: string;
@@ -66,7 +82,7 @@ async function resolveStickerVisionSupport(params: {
6682
agentId?: string;
6783
}): Promise<boolean> {
6884
try {
69-
const { resolveStickerVisionSupportRuntime } = await import("./sticker-vision.runtime.js");
85+
const { resolveStickerVisionSupportRuntime } = await loadStickerVisionRuntime();
7086
return await resolveStickerVisionSupportRuntime(params);
7187
} catch {
7288
return false;
@@ -193,7 +209,7 @@ export async function resolveTelegramInboundBody(params: {
193209

194210
if (needsPreflightTranscription) {
195211
try {
196-
const { transcribeFirstAudio } = await import("./media-understanding.runtime.js");
212+
const { transcribeFirstAudio } = await loadMediaUnderstandingRuntime();
197213
const tempCtx: MsgContext = {
198214
MediaPaths: allMedia.length > 0 ? allMedia.map((m) => m.path) : undefined,
199215
MediaTypes:

0 commit comments

Comments
 (0)