
























@@ -1,24 +1,73 @@
1-import { createDedupeCache } from "openclaw/plugin-sdk/core";
21import { createClaimableDedupe } from "openclaw/plugin-sdk/persistent-dedupe";
3243const RECENT_WEB_MESSAGE_TTL_MS = 20 * 60_000;
54const RECENT_WEB_MESSAGE_MAX = 5000;
65const RECENT_OUTBOUND_MESSAGE_TTL_MS = 20 * 60_000;
76const RECENT_OUTBOUND_MESSAGE_MAX = 5000;
879-const recentInboundMessages = createDedupeCache({
10-ttlMs: RECENT_WEB_MESSAGE_TTL_MS,
11-maxSize: RECENT_WEB_MESSAGE_MAX,
12-});
138const claimableInboundMessages = createClaimableDedupe({
149ttlMs: RECENT_WEB_MESSAGE_TTL_MS,
1510memoryMaxSize: RECENT_WEB_MESSAGE_MAX,
1611});
17-const recentOutboundMessages = createDedupeCache({
12+const recentOutboundMessages = createRecentMessageCache({
1813ttlMs: RECENT_OUTBOUND_MESSAGE_TTL_MS,
1914maxSize: RECENT_OUTBOUND_MESSAGE_MAX,
2015});
211617+function createRecentMessageCache(options: { ttlMs: number; maxSize: number }) {
18+const ttlMs = Math.max(0, options.ttlMs);
19+const maxSize = Math.max(0, Math.floor(options.maxSize));
20+const cache = new Map<string, number>();
21+22+const prune = (now: number) => {
23+if (ttlMs > 0) {
24+const cutoff = now - ttlMs;
25+for (const [key, timestamp] of cache) {
26+if (timestamp < cutoff) {
27+cache.delete(key);
28+}
29+}
30+}
31+while (cache.size > maxSize) {
32+const oldest = cache.keys().next().value;
33+if (!oldest) {
34+break;
35+}
36+cache.delete(oldest);
37+}
38+};
39+40+const peek = (key: string | null, now = Date.now()): boolean => {
41+if (!key) {
42+return false;
43+}
44+const timestamp = cache.get(key);
45+if (timestamp === undefined) {
46+return false;
47+}
48+if (ttlMs > 0 && now - timestamp >= ttlMs) {
49+cache.delete(key);
50+return false;
51+}
52+return true;
53+};
54+55+return {
56+check: (key: string | null, now = Date.now()): boolean => {
57+if (!key) {
58+return false;
59+}
60+const existed = peek(key, now);
61+cache.delete(key);
62+cache.set(key, now);
63+prune(now);
64+return existed;
65+},
66+ peek,
67+clear: () => cache.clear(),
68+};
69+}
70+2271export class WhatsAppRetryableInboundError extends Error {
2372constructor(message: string, options?: ErrorOptions) {
2473super(message, options);
@@ -41,7 +90,6 @@ function buildMessageKey(params: {
4190}
42914392export function resetWebInboundDedupe(): void {
44-recentInboundMessages.clear();
4593claimableInboundMessages.clearMemory();
4694recentOutboundMessages.clear();
4795}
@@ -53,7 +101,6 @@ export async function claimRecentInboundMessage(key: string): Promise<boolean> {
5310154102export async function commitRecentInboundMessage(key: string): Promise<void> {
55103await claimableInboundMessages.commit(key);
56-recentInboundMessages.check(key);
57104}
5810559106export function releaseRecentInboundMessage(key: string, error?: unknown): void {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。