Skip to content

Commit 1284217

Browse files
committed
fix(whatsapp): persist inbound delivery in plugin state
1 parent a7e0fa0 commit 1284217

10 files changed

Lines changed: 1007 additions & 101 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Docs: https://docs.openclaw.ai
7070
- Diffs: continue hydrating later diff cards when one card fails so a single broken card no longer blanks the whole diff viewer. (#84775) Thanks @cosmopolitan033.
7171
- Mac app: use the native settings sidebar window chrome so the sidebar toggle stays on the left and content no longer clips under oversized titlebar padding.
7272
- QA-Lab/Codex: bundle auth/plugin fixture imports for flow scenarios and let terminal async media tools end Codex app-server turns without timing out. (#80397, refs #80323) Thanks @100yenadmin.
73+
- WhatsApp: persist inbound message delivery state through plugin state before dispatch and delay read receipts until handler completion, so retryable failures can redeliver without adding a plugin-local disk cache. Thanks @samzong.
7374
- Gateway/agents: preserve fresh session overrides and metadata when stale cached agent-session entries race with store updates, so subagent model/provider overrides and routing policy survive concurrent writes. (#19328) Thanks @CodeReclaimers.
7475
- Control UI/chat: keep chat session search inline with the session selector so the header no longer shows a duplicate standalone search row.
7576
- Control UI/chat: collapse focused-mode header chrome and suppress hidden-header scroll updates so focus mode no longer jumps while scrolling. Thanks @amknight.

‎extensions/whatsapp/src/inbound/dedupe.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,17 @@ export function resetWebInboundDedupe(): void {
9494
recentOutboundMessages.clear();
9595
}
9696

97-
export async function claimRecentInboundMessage(key: string): Promise<boolean> {
97+
export type RecentInboundMessageClaimKind = "claimed" | "duplicate" | "inflight";
98+
99+
export async function claimRecentInboundMessageDelivery(
100+
key: string,
101+
): Promise<RecentInboundMessageClaimKind> {
98102
const claim = await claimableInboundMessages.claim(key);
99-
return claim.kind === "claimed";
103+
return claim.kind;
104+
}
105+
106+
export async function claimRecentInboundMessage(key: string): Promise<boolean> {
107+
return (await claimRecentInboundMessageDelivery(key)) === "claimed";
100108
}
101109

102110
export async function commitRecentInboundMessage(key: string): Promise<void> {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { createHash } from "node:crypto";
2+
import type { WAMessage } from "baileys";
3+
import { createDurableInboundReceiveJournal } from "openclaw/plugin-sdk/channel-message";
4+
import { getWhatsAppRuntime } from "../runtime.js";
5+
import { BufferJSON } from "../session.runtime.js";
6+
7+
const WHATSAPP_DURABLE_INBOUND_PENDING_MAX_ENTRIES = 450;
8+
const WHATSAPP_DURABLE_INBOUND_COMPLETED_MAX_ENTRIES = 450;
9+
const WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS = 30 * 24 * 60 * 60 * 1000;
10+
const WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS = 7 * 24 * 60 * 60 * 1000;
11+
12+
export type WhatsAppReadReceiptTarget = {
13+
remoteJid: string;
14+
id: string;
15+
participant?: string;
16+
};
17+
18+
export type WhatsAppDurableInboundPayload = {
19+
message: unknown;
20+
upsertType?: string;
21+
receivedAt: number;
22+
};
23+
24+
export type WhatsAppDurableInboundMetadata = {
25+
readReceipt?: WhatsAppReadReceiptTarget;
26+
};
27+
28+
export type WhatsAppDurableInboundCompletedMetadata = {
29+
readReceipt?: WhatsAppReadReceiptTarget;
30+
};
31+
32+
function hashNamespacePart(value: string): string {
33+
return createHash("sha256").update(value).digest("hex").slice(0, 24);
34+
}
35+
36+
export function createWhatsAppDurableInboundMessageId(params: {
37+
remoteJid: string;
38+
id: string;
39+
}): string {
40+
return createHash("sha256").update(`${params.remoteJid}\n${params.id}`).digest("hex");
41+
}
42+
43+
export function serializeWhatsAppDurableInboundMessage(message: WAMessage): unknown {
44+
return JSON.parse(JSON.stringify(message, BufferJSON.replacer)) as unknown;
45+
}
46+
47+
export function deserializeWhatsAppDurableInboundMessage(message: unknown): WAMessage {
48+
return JSON.parse(JSON.stringify(message), BufferJSON.reviver) as WAMessage;
49+
}
50+
51+
export function createWhatsAppDurableInboundReceiveJournal(accountId: string) {
52+
const runtime = getWhatsAppRuntime();
53+
const accountPart = hashNamespacePart(accountId);
54+
return createDurableInboundReceiveJournal<
55+
WhatsAppDurableInboundPayload,
56+
WhatsAppDurableInboundMetadata,
57+
WhatsAppDurableInboundCompletedMetadata
58+
>({
59+
pendingStore: runtime.state.openKeyedStore({
60+
namespace: `inbound.v1.pending.${accountPart}`,
61+
maxEntries: WHATSAPP_DURABLE_INBOUND_PENDING_MAX_ENTRIES,
62+
defaultTtlMs: WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS,
63+
}),
64+
completedStore: runtime.state.openKeyedStore({
65+
namespace: `inbound.v1.completed.${accountPart}`,
66+
maxEntries: WHATSAPP_DURABLE_INBOUND_COMPLETED_MAX_ENTRIES,
67+
defaultTtlMs: WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS,
68+
}),
69+
pendingTtlMs: WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS,
70+
completedTtlMs: WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS,
71+
});
72+
}

0 commit comments

Comments
 (0)