|
1 | 1 | // Whatsapp plugin module implements monitor behavior. |
| 2 | +import type { WAMessageKey } from "baileys"; |
2 | 3 | import { resolveAccountEntry } from "openclaw/plugin-sdk/account-core"; |
3 | 4 | import { CHANNEL_APPROVAL_NATIVE_RUNTIME_CONTEXT_CAPABILITY } from "openclaw/plugin-sdk/approval-handler-runtime"; |
4 | 5 | import { resolveInboundDebounceMs } from "openclaw/plugin-sdk/channel-inbound-debounce"; |
@@ -27,7 +28,13 @@ import {
|
27 | 28 | } from "../connection-controller.js"; |
28 | 29 | import { resolveWhatsAppInboundPolicy } from "../inbound-policy.js"; |
29 | 30 | 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"; |
31 | 38 | import type { WebInboundMessageInput } from "../inbound/types.js"; |
32 | 39 | import { |
33 | 40 | newConnectionId, |
@@ -193,6 +200,8 @@ export async function monitorWebChannel(
|
193 | 200 | >(); |
194 | 201 | const groupMemberNames = new Map<string, Map<string, string>>(); |
195 | 202 | const groupMetadataCache: WhatsAppGroupMetadataCache = new Map(); |
| 203 | +const recentMessageKeys: WhatsAppBaileysMessageCache = new Map(); |
| 204 | +const baileysGroupMetaCache: WhatsAppBaileysGroupMetadataCache = new Map(); |
196 | 205 | const echoTracker = createEchoTracker({ maxItems: 100, logVerbose }); |
197 | 206 | |
198 | 207 | const sleep = |
@@ -268,6 +277,14 @@ export async function monitorWebChannel(
|
268 | 277 | try { |
269 | 278 | connection = await controller.openConnection({ |
270 | 279 | connectionId, |
| 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; |
| 287 | +}, |
271 | 288 | createListener: async ({ sock, connection: connectionLocal }) => { |
272 | 289 | const onMessage = createWebOnMessageHandler({ |
273 | 290 | cfg, |
@@ -303,6 +320,8 @@ export async function monitorWebChannel(
|
303 | 320 | disconnectRetryPolicy: reconnectPolicy, |
304 | 321 | disconnectRetryAbortSignal: controller.getDisconnectRetryAbortSignal(), |
305 | 322 | groupMetadataCache, |
| 323 | + recentMessageKeys, |
| 324 | + baileysGroupMetaCache, |
306 | 325 | onMessage: async (msg: WebInboundMessageInput) => { |
307 | 326 | const normalized = normalizeWebInboundMessage(msg); |
308 | 327 | const inboundAt = Date.now(); |
|