11// Whatsapp plugin module implements dedupe behavior.
2+ import { createDedupeCache } from "openclaw/plugin-sdk/dedupe-runtime" ;
23import { createClaimableDedupe } from "openclaw/plugin-sdk/persistent-dedupe" ;
34
45export const WHATSAPP_INBOUND_DEDUPE_TTL_MS = 20 * 60_000 ;
@@ -10,65 +11,11 @@ const claimableInboundMessages = createClaimableDedupe({
1011 ttlMs : WHATSAPP_INBOUND_DEDUPE_TTL_MS ,
1112 memoryMaxSize : RECENT_WEB_MESSAGE_MAX ,
1213} ) ;
13- const recentOutboundMessages = createRecentMessageCache ( {
14+ const recentOutboundMessages = createDedupeCache ( {
1415 ttlMs : RECENT_OUTBOUND_MESSAGE_TTL_MS ,
1516 maxSize : RECENT_OUTBOUND_MESSAGE_MAX ,
1617} ) ;
1718
18- function createRecentMessageCache ( options : { ttlMs : number ; maxSize : number } ) {
19- const ttlMs = Math . max ( 0 , options . ttlMs ) ;
20- const maxSize = Math . max ( 0 , Math . floor ( options . maxSize ) ) ;
21- const cache = new Map < string , number > ( ) ;
22-
23- const prune = ( now : number ) => {
24- if ( ttlMs > 0 ) {
25- const cutoff = now - ttlMs ;
26- for ( const [ key , timestamp ] of cache ) {
27- if ( timestamp < cutoff ) {
28- cache . delete ( key ) ;
29- }
30- }
31- }
32- while ( cache . size > maxSize ) {
33- const oldest = cache . keys ( ) . next ( ) . value ;
34- if ( ! oldest ) {
35- break ;
36- }
37- cache . delete ( oldest ) ;
38- }
39- } ;
40-
41- const peek = ( key : string | null , now = Date . now ( ) ) : boolean => {
42- if ( ! key ) {
43- return false ;
44- }
45- const timestamp = cache . get ( key ) ;
46- if ( timestamp === undefined ) {
47- return false ;
48- }
49- if ( ttlMs > 0 && now - timestamp >= ttlMs ) {
50- cache . delete ( key ) ;
51- return false ;
52- }
53- return true ;
54- } ;
55-
56- return {
57- check : ( key : string | null , now = Date . now ( ) ) : boolean => {
58- if ( ! key ) {
59- return false ;
60- }
61- const existed = peek ( key , now ) ;
62- cache . delete ( key ) ;
63- cache . set ( key , now ) ;
64- prune ( now ) ;
65- return existed ;
66- } ,
67- peek,
68- clear : ( ) => cache . clear ( ) ,
69- } ;
70- }
71-
7219export class WhatsAppRetryableInboundError extends Error {
7320 constructor ( message : string , options ?: ErrorOptions ) {
7421 super ( message , options ) ;
0 commit comments