Skip to content

Commit d7f9771

Browse files
committed
fix: harden WhatsApp Baileys retry caches
1 parent 03ce7e1 commit d7f9771

5 files changed

Lines changed: 469 additions & 161 deletions

File tree

extensions/whatsapp/src/auto-reply/monitor.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Whatsapp plugin module implements monitor behavior.
2+
import type { WAMessageKey } from "baileys";
23
import { resolveAccountEntry } from "openclaw/plugin-sdk/account-core";
34
import { CHANNEL_APPROVAL_NATIVE_RUNTIME_CONTEXT_CAPABILITY } from "openclaw/plugin-sdk/approval-handler-runtime";
45
import { resolveInboundDebounceMs } from "openclaw/plugin-sdk/channel-inbound-debounce";
@@ -27,7 +28,13 @@ import {
2728
} from "../connection-controller.js";
2829
import { resolveWhatsAppInboundPolicy } from "../inbound-policy.js";
2930
import { normalizeWebInboundMessage } from "../inbound/message-aliases.js";
30-
import { attachWebInboxToSocket, type WhatsAppGroupMetadataCache } from "../inbound/monitor.js";
31+
import {
32+
attachWebInboxToSocket,
33+
readWhatsAppBaileysCacheEntry,
34+
type WhatsAppBaileysGroupMetadataCache,
35+
type WhatsAppBaileysMessageCache,
36+
type WhatsAppGroupMetadataCache,
37+
} from "../inbound/monitor.js";
3138
import type { WebInboundMessageInput } from "../inbound/types.js";
3239
import {
3340
newConnectionId,
@@ -193,10 +200,8 @@ export async function monitorWebChannel(
193200
>();
194201
const groupMemberNames = new Map<string, Map<string, string>>();
195202
const groupMetadataCache: WhatsAppGroupMetadataCache = new Map();
196-
// Bounded in-memory message store for Baileys getMessage retry support.
197-
const recentMessageKeys = new Map<string, import("baileys").proto.IMessage>();
198-
// Baileys-level group metadata cache for cachedGroupMetadata socket option.
199-
const baileysGroupMetaCache = new Map<string, import("baileys").GroupMetadata>();
203+
const recentMessageKeys: WhatsAppBaileysMessageCache = new Map();
204+
const baileysGroupMetaCache: WhatsAppBaileysGroupMetadataCache = new Map();
200205
const echoTracker = createEchoTracker({ maxItems: 100, logVerbose });
201206

202207
const sleep =
@@ -272,13 +277,14 @@ export async function monitorWebChannel(
272277
try {
273278
connection = await controller.openConnection({
274279
connectionId,
275-
getMessage: async (key: import("baileys").WAMessageKey) => {
276-
if (!key.id || !key.remoteJid) {
277-
return undefined;
278-
}
279-
return recentMessageKeys.get(`${key.remoteJid}:${key.id}`);
280+
getMessage: async (key: WAMessageKey) =>
281+
key.id && key.remoteJid
282+
? readWhatsAppBaileysCacheEntry(recentMessageKeys, `${key.remoteJid}:${key.id}`)
283+
: undefined,
284+
cachedGroupMetadata: async (jid: string) => {
285+
const meta = readWhatsAppBaileysCacheEntry(baileysGroupMetaCache, jid);
286+
return meta?.participants?.length ? meta : undefined;
280287
},
281-
cachedGroupMetadata: async (jid: string) => baileysGroupMetaCache.get(jid),
282288
createListener: async ({ sock, connection: connectionLocal }) => {
283289
const onMessage = createWebOnMessageHandler({
284290
cfg,

extensions/whatsapp/src/connection-controller.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Whatsapp plugin module implements connection controller behavior.
2-
import type { WASocket } from "baileys";
2+
import type { GroupMetadata, WASocket, WAMessageKey, proto } from "baileys";
33
import { info } from "openclaw/plugin-sdk/runtime-env";
44
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
55
import {
@@ -528,12 +528,8 @@ export class WhatsAppConnectionController {
528528
}) => Promise<ManagedWhatsAppListener>;
529529
onHeartbeat?: (snapshot: WhatsAppConnectionSnapshot) => void;
530530
onWatchdogTimeout?: (snapshot: WhatsAppConnectionSnapshot) => void;
531-
/** Baileys getMessage callback for message retry/decryption support. */
532-
getMessage?: (
533-
key: import("baileys").WAMessageKey,
534-
) => Promise<import("baileys").proto.IMessage | undefined>;
535-
/** Baileys cachedGroupMetadata callback. */
536-
cachedGroupMetadata?: (jid: string) => Promise<import("baileys").GroupMetadata | undefined>;
531+
getMessage?: (key: WAMessageKey) => Promise<proto.IMessage | undefined>;
532+
cachedGroupMetadata?: (jid: string) => Promise<GroupMetadata | undefined>;
537533
}): Promise<WhatsAppLiveConnection> {
538534
if (this.current) {
539535
await this.closeCurrentConnection();

0 commit comments

Comments
 (0)