Skip to content

Commit 79aa212

Browse files
committed
refactor(whatsapp): lazy-load send and action runtimes
1 parent dae0400 commit 79aa212

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

‎extensions/whatsapp/src/channel.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
resolveWhatsAppAccount,
1313
type ResolvedWhatsAppAccount,
1414
} from "./accounts.js";
15-
import { handleWhatsAppAction } from "./action-runtime.js";
1615
import { createWhatsAppLoginTool } from "./agent-tools-login.js";
1716
import { whatsappApprovalAuth } from "./approval-auth.js";
1817
import type { WebChannelStatus } from "./auto-reply/types.js";
@@ -43,7 +42,6 @@ import {
4342
normalizeWhatsAppTarget,
4443
} from "./runtime-api.js";
4544
import { getWhatsAppRuntime } from "./runtime.js";
46-
import { sendMessageWhatsApp, sendPollWhatsApp } from "./send.js";
4745
import { resolveWhatsAppOutboundSessionRoute } from "./session-route.js";
4846
import { whatsappSetupAdapter } from "./setup-core.js";
4947
import {
@@ -54,6 +52,22 @@ import {
5452
} from "./shared.js";
5553
import { collectWhatsAppStatusIssues } from "./status-issues.js";
5654

55+
type WhatsAppSendModule = typeof import("./send.js");
56+
type WhatsAppActionRuntimeModule = typeof import("./action-runtime.js");
57+
58+
let whatsAppSendModulePromise: Promise<WhatsAppSendModule> | undefined;
59+
let whatsAppActionRuntimeModulePromise: Promise<WhatsAppActionRuntimeModule> | undefined;
60+
61+
async function loadWhatsAppSendModule() {
62+
whatsAppSendModulePromise ??= import("./send.js");
63+
return await whatsAppSendModulePromise;
64+
}
65+
66+
async function loadWhatsAppActionRuntimeModule() {
67+
whatsAppActionRuntimeModulePromise ??= import("./action-runtime.js");
68+
return await whatsAppActionRuntimeModulePromise;
69+
}
70+
5771
function normalizeWhatsAppPayloadText(text: string | undefined): string {
5872
return (text ?? "").replace(/^(?:[ \t]*\r?\n)+/, "");
5973
}
@@ -125,8 +139,10 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> =
125139
outbound: {
126140
...createWhatsAppOutboundBase({
127141
chunker: chunkText,
128-
sendMessageWhatsApp,
129-
sendPollWhatsApp,
142+
sendMessageWhatsApp: async (...args) =>
143+
await (await loadWhatsAppSendModule()).sendMessageWhatsApp(...args),
144+
sendPollWhatsApp: async (...args) =>
145+
await (await loadWhatsAppSendModule()).sendPollWhatsApp(...args),
130146
shouldLogVerbose: () => getWhatsAppRuntime().logging.shouldLogVerbose(),
131147
resolveTarget: ({ to, allowFrom, mode }) =>
132148
resolveWhatsAppOutboundTarget({ to, allowFrom, mode }),
@@ -260,7 +276,9 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> =
260276
const messageId = String(messageIdRaw);
261277
const emoji = readStringParam(params, "emoji", { allowEmpty: true });
262278
const remove = typeof params.remove === "boolean" ? params.remove : undefined;
263-
return await handleWhatsAppAction(
279+
return await (
280+
await loadWhatsAppActionRuntimeModule()
281+
).handleWhatsAppAction(
264282
{
265283
action: "react",
266284
chatJid:

0 commit comments

Comments
 (0)