Skip to content

Commit 59b08b4

Browse files
authored
refactor(shared): consolidate remaining channel lazy loaders (#99302)
1 parent 3ad465d commit 59b08b4

45 files changed

Lines changed: 246 additions & 529 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extensions/device-pair/index.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,24 @@
22
import { rm } from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
5+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
56
import { definePluginEntry, type OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
67
import {
78
normalizeLowercaseStringOrEmpty,
89
normalizeOptionalString,
910
} from "openclaw/plugin-sdk/string-coerce-runtime";
1011
import { buildDevicePairPairingQrChannelData } from "./pairing-qr-channel-data.js";
11-
12-
type DevicePairApiModule = typeof import("./api.js");
1312
type NotifyModule = typeof import("./notify.js");
14-
type PairCommandApproveModule = typeof import("./pair-command-approve.js");
15-
type PairCommandAuthModule = typeof import("./pair-command-auth.js");
16-
17-
let devicePairApiModulePromise: Promise<DevicePairApiModule> | undefined;
18-
let notifyModulePromise: Promise<NotifyModule> | undefined;
19-
let pairCommandApproveModulePromise: Promise<PairCommandApproveModule> | undefined;
20-
let pairCommandAuthModulePromise: Promise<PairCommandAuthModule> | undefined;
2113

22-
function loadDevicePairApiModule(): Promise<DevicePairApiModule> {
23-
devicePairApiModulePromise ??= import("./api.js");
24-
return devicePairApiModulePromise;
25-
}
14+
const loadDevicePairApiModule = createLazyRuntimeModule(() => import("./api.js"));
2615

27-
function loadNotifyModule(): Promise<NotifyModule> {
28-
notifyModulePromise ??= import("./notify.js");
29-
return notifyModulePromise;
30-
}
16+
const loadNotifyModule = createLazyRuntimeModule(() => import("./notify.js"));
3117

32-
function loadPairCommandApproveModule(): Promise<PairCommandApproveModule> {
33-
pairCommandApproveModulePromise ??= import("./pair-command-approve.js");
34-
return pairCommandApproveModulePromise;
35-
}
18+
const loadPairCommandApproveModule = createLazyRuntimeModule(
19+
() => import("./pair-command-approve.js"),
20+
);
3621

37-
function loadPairCommandAuthModule(): Promise<PairCommandAuthModule> {
38-
pairCommandAuthModulePromise ??= import("./pair-command-auth.js");
39-
return pairCommandAuthModulePromise;
40-
}
22+
const loadPairCommandAuthModule = createLazyRuntimeModule(() => import("./pair-command-auth.js"));
4123

4224
function formatDurationMinutes(expiresAtMs: number): string {
4325
const msRemaining = Math.max(0, expiresAtMs - Date.now());

extensions/feishu/src/monitor.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
12
// Feishu plugin module implements monitor behavior.
23
import type { ClawdbotConfig, PluginRuntime, RuntimeEnv } from "../runtime-api.js";
34
import { listEnabledFeishuAccounts, resolveFeishuRuntimeAccount } from "./accounts.js";
@@ -40,12 +41,7 @@ export type FeishuStatusSink = (patch: {
4041
lastError?: string | null;
4142
}) => void;
4243

43-
let monitorAccountRuntimePromise: Promise<typeof import("./monitor.account.js")> | undefined;
44-
45-
async function loadMonitorAccountRuntime() {
46-
monitorAccountRuntimePromise ??= import("./monitor.account.js");
47-
return await monitorAccountRuntimePromise;
48-
}
44+
const loadMonitorAccountRuntime = createLazyRuntimeModule(() => import("./monitor.account.js"));
4945

5046
export {
5147
clearFeishuWebhookRateLimitStateForTest,

extensions/feishu/src/setup-surface.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
12
// Feishu plugin module implements setup surface behavior.
23
import {
34
DEFAULT_ACCOUNT_ID,
@@ -248,16 +249,7 @@ function applyNewAppSecurityPolicy(
248249
return next;
249250
}
250251

251-
// ---------------------------------------------------------------------------
252-
// Scan-to-create flow
253-
// ---------------------------------------------------------------------------
254-
255-
let appRegistrationModulePromise: Promise<typeof import("./app-registration.js")> | null = null;
256-
257-
const loadAppRegistrationModule = async () => {
258-
appRegistrationModulePromise ??= import("./app-registration.js");
259-
return await appRegistrationModulePromise;
260-
};
252+
const loadAppRegistrationModule = createLazyRuntimeModule(() => import("./app-registration.js"));
261253

262254
async function promptFeishuDomain(params: {
263255
prompter: WizardPrompter;

extensions/feishu/subagent-hooks-api.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
// Feishu API module exposes the plugin public contract.
22
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/channel-entry-contract";
3+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
34

4-
type FeishuSubagentHooksModule = typeof import("./src/subagent-hooks.js");
5-
6-
let feishuSubagentHooksPromise: Promise<FeishuSubagentHooksModule> | null = null;
7-
8-
function loadFeishuSubagentHooksModule() {
9-
feishuSubagentHooksPromise ??= import("./src/subagent-hooks.js");
10-
return feishuSubagentHooksPromise;
11-
}
5+
const loadFeishuSubagentHooksModule = createLazyRuntimeModule(
6+
() => import("./src/subagent-hooks.js"),
7+
);
128

139
export function registerFeishuSubagentHooks(api: OpenClawPluginApi): void {
1410
api.on("subagent_delivery_target", async (event) => {

extensions/google-meet/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
errorShape,
1111
type GatewayRequestHandlerOptions,
1212
} from "openclaw/plugin-sdk/gateway-runtime";
13+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
1314
import { definePluginEntry, type OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
1415
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
1516
import { Type } from "typebox";
@@ -42,18 +43,9 @@ import {
4243
import { GoogleMeetRuntime } from "./src/runtime.js";
4344
import { isGoogleMeetBrowserManualActionError } from "./src/transports/chrome-create.js";
4445

45-
let googleMeetCreateModulePromise: Promise<typeof import("./src/create.js")> | null = null;
46-
let googleMeetCliModulePromise: Promise<typeof import("./src/cli.js")> | null = null;
46+
const loadGoogleMeetCreateModule = createLazyRuntimeModule(() => import("./src/create.js"));
4747

48-
const loadGoogleMeetCreateModule = async () => {
49-
googleMeetCreateModulePromise ??= import("./src/create.js");
50-
return await googleMeetCreateModulePromise;
51-
};
52-
53-
const loadGoogleMeetCliModule = async () => {
54-
googleMeetCliModulePromise ??= import("./src/cli.js");
55-
return await googleMeetCliModulePromise;
56-
};
48+
const loadGoogleMeetCliModule = createLazyRuntimeModule(() => import("./src/cli.js"));
5749

5850
const googleMeetConfigSchema = {
5951
parse(value: unknown) {

extensions/imessage/src/approval-reactions.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "openclaw/plugin-sdk/approval-reaction-runtime";
1010
import type { ExecApprovalReplyDecision } from "openclaw/plugin-sdk/approval-reply-runtime";
1111
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
12+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
1213
import {
1314
asDateTimestampMs,
1415
isFutureDateTimestampMs,
@@ -58,13 +59,10 @@ export type PendingIMessageApprovalReactionPollTarget = {
5859
expiresAtMs: number;
5960
};
6061

61-
let resolverRuntimePromise: Promise<typeof import("./approval-resolver.js")> | undefined;
62+
const resolverRuntimeLoader = createLazyRuntimeModule(() => import("./approval-resolver.js"));
6263
const pendingReactionPollTargets = new Map<string, PendingIMessageApprovalReactionPollTarget>();
6364

64-
function loadApprovalResolver(): Promise<typeof import("./approval-resolver.js")> {
65-
resolverRuntimePromise ??= import("./approval-resolver.js");
66-
return resolverRuntimePromise;
67-
}
65+
const loadApprovalResolver = resolverRuntimeLoader;
6866

6967
function chatIdToKeyValue(chatId: number | string | undefined): string | null {
7068
if (chatId == null || chatId === "") {
@@ -639,5 +637,5 @@ export async function maybeResolveIMessageApprovalReaction(params: {
639637
export function clearIMessageApprovalReactionTargetsForTest(): void {
640638
imessageApprovalReactionTargets.clearForTest();
641639
pendingReactionPollTargets.clear();
642-
resolverRuntimePromise = undefined;
640+
resolverRuntimeLoader.clear();
643641
}

extensions/irc/src/channel.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createChannelDirectoryAdapter,
1616
createResolvedDirectoryEntriesLister,
1717
} from "openclaw/plugin-sdk/directory-runtime";
18+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
1819
import {
1920
createComputedAccountStatusAdapter,
2021
createDefaultChannelRuntimeState,
@@ -62,14 +63,7 @@ const meta = {
6263
markdownCapable: true,
6364
};
6465

65-
type IrcChannelRuntimeModule = typeof import("./channel-runtime.js");
66-
67-
let ircChannelRuntimePromise: Promise<IrcChannelRuntimeModule> | undefined;
68-
69-
async function loadIrcChannelRuntime(): Promise<IrcChannelRuntimeModule> {
70-
ircChannelRuntimePromise ??= import("./channel-runtime.js");
71-
return await ircChannelRuntimePromise;
72-
}
66+
const loadIrcChannelRuntime = createLazyRuntimeModule(() => import("./channel-runtime.js"));
7367

7468
function normalizePairingTarget(raw: string): string {
7569
const normalized = normalizeIrcAllowEntry(raw);

extensions/irc/src/gateway.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
// Irc plugin module implements gateway behavior.
22
import { runStoppablePassiveMonitor } from "openclaw/plugin-sdk/extension-shared";
3+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
34
import type { ChannelAccountSnapshot } from "openclaw/plugin-sdk/status-helpers";
45
import type { ResolvedIrcAccount } from "./accounts.js";
56
import { createAccountStatusSink } from "./channel-api.js";
67
import type { RuntimeEnv } from "./runtime-api.js";
78
import type { CoreConfig } from "./types.js";
89

9-
type IrcChannelRuntimeModule = typeof import("./channel-runtime.js");
10-
11-
let ircChannelRuntimePromise: Promise<IrcChannelRuntimeModule> | undefined;
12-
13-
async function loadIrcChannelRuntime(): Promise<IrcChannelRuntimeModule> {
14-
ircChannelRuntimePromise ??= import("./channel-runtime.js");
15-
return await ircChannelRuntimePromise;
16-
}
10+
const loadIrcChannelRuntime = createLazyRuntimeModule(() => import("./channel-runtime.js"));
1711

1812
export async function startIrcGatewayAccount(ctx: {
1913
cfg: CoreConfig;

extensions/line/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import {
44
type OpenClawPluginCommandDefinition,
55
type OpenClawPluginApi,
66
} from "openclaw/plugin-sdk/channel-entry-contract";
7+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
78

89
type RegisteredLineCardCommand = OpenClawPluginCommandDefinition;
910

10-
let lineCardCommandPromise: Promise<RegisteredLineCardCommand> | null = null;
11-
12-
async function loadLineCardCommand(api: OpenClawPluginApi): Promise<RegisteredLineCardCommand> {
13-
lineCardCommandPromise ??= (async () => {
11+
function createLineCardCommandLoader(api: OpenClawPluginApi) {
12+
return createLazyRuntimeModule<RegisteredLineCardCommand>(async () => {
1413
let registered: RegisteredLineCardCommand | null = null;
1514
const { registerLineCardCommand } = await import("./src/card-command.js");
1615
registerLineCardCommand({
@@ -23,8 +22,7 @@ async function loadLineCardCommand(api: OpenClawPluginApi): Promise<RegisteredLi
2322
throw new Error("LINE card command registration unavailable");
2423
}
2524
return registered;
26-
})();
27-
return await lineCardCommandPromise;
25+
});
2826
}
2927

3028
export default defineBundledChannelEntry({
@@ -41,13 +39,14 @@ export default defineBundledChannelEntry({
4139
exportName: "setLineRuntime",
4240
},
4341
registerFull(api) {
42+
const loadLineCardCommand = createLineCardCommandLoader(api);
4443
api.registerCommand({
4544
name: "card",
4645
description: "Send a rich card message (LINE).",
4746
acceptsArgs: true,
4847
requireAuth: false,
4948
async handler(ctx) {
50-
const command = await loadLineCardCommand(api);
49+
const command = await loadLineCardCommand();
5150
return await command.handler(ctx);
5251
},
5352
});

extensions/matrix/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ import {
33
defineBundledChannelEntry,
44
type OpenClawPluginApi,
55
} from "openclaw/plugin-sdk/channel-entry-contract";
6+
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
67
import { registerMatrixCliMetadata } from "./cli-metadata.js";
78
import { registerMatrixSubagentHooks } from "./subagent-hooks-api.js";
89

9-
type MatrixHandlersRuntimeModule = typeof import("./plugin-entry.handlers.runtime.js");
10-
11-
let matrixHandlersRuntimePromise: Promise<MatrixHandlersRuntimeModule> | null = null;
12-
13-
function loadMatrixHandlersRuntimeModule() {
14-
matrixHandlersRuntimePromise ??= import("./plugin-entry.handlers.runtime.js");
15-
return matrixHandlersRuntimePromise;
16-
}
10+
const loadMatrixHandlersRuntimeModule = createLazyRuntimeModule(
11+
() => import("./plugin-entry.handlers.runtime.js"),
12+
);
1713

1814
export function registerMatrixFullRuntime(api: OpenClawPluginApi): void {
1915
api.registerGatewayMethod("matrix.verify.recoveryKey", async (ctx) => {

0 commit comments

Comments
 (0)