




















@@ -0,0 +1,79 @@
1+import { createHash } from "node:crypto";
2+import type { WAMessage } from "baileys";
3+import { createDurableInboundReceiveJournal } from "openclaw/plugin-sdk/channel-message";
4+import type { PluginJsonValue } from "openclaw/plugin-sdk/plugin-entry";
5+import { getWhatsAppRuntime } from "../runtime.js";
6+import { BufferJSON } from "../session.runtime.js";
7+8+const WHATSAPP_DURABLE_INBOUND_PENDING_MAX_ENTRIES = 450;
9+const WHATSAPP_DURABLE_INBOUND_COMPLETED_MAX_ENTRIES = 450;
10+const WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS = 30 * 24 * 60 * 60 * 1000;
11+const WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS = 7 * 24 * 60 * 60 * 1000;
12+13+export type WhatsAppReadReceiptTarget = {
14+remoteJid: string;
15+id: string;
16+participant?: string;
17+};
18+19+export type SerializedWhatsAppDurableInboundMessage = PluginJsonValue;
20+21+export type WhatsAppDurableInboundPayload = {
22+message: SerializedWhatsAppDurableInboundMessage;
23+upsertType?: string;
24+receivedAt: number;
25+};
26+27+export type WhatsAppDurableInboundMetadata = {
28+readReceipt?: WhatsAppReadReceiptTarget;
29+};
30+31+export type WhatsAppDurableInboundCompletedMetadata = {
32+readReceipt?: WhatsAppReadReceiptTarget;
33+};
34+35+function hashNamespacePart(value: string): string {
36+return createHash("sha256").update(value).digest("hex").slice(0, 24);
37+}
38+39+export function createWhatsAppDurableInboundMessageId(params: {
40+remoteJid: string;
41+id: string;
42+}): string {
43+return createHash("sha256").update(`${params.remoteJid}\n${params.id}`).digest("hex");
44+}
45+46+export function serializeWhatsAppDurableInboundMessage(
47+message: WAMessage,
48+): SerializedWhatsAppDurableInboundMessage {
49+return JSON.parse(JSON.stringify(message, BufferJSON.replacer)) as PluginJsonValue;
50+}
51+52+export function deserializeWhatsAppDurableInboundMessage(
53+message: SerializedWhatsAppDurableInboundMessage,
54+): WAMessage {
55+return JSON.parse(JSON.stringify(message), BufferJSON.reviver) as WAMessage;
56+}
57+58+export function createWhatsAppDurableInboundReceiveJournal(accountId: string) {
59+const runtime = getWhatsAppRuntime();
60+const accountPart = hashNamespacePart(accountId);
61+return createDurableInboundReceiveJournal<
62+WhatsAppDurableInboundPayload,
63+WhatsAppDurableInboundMetadata,
64+WhatsAppDurableInboundCompletedMetadata
65+>({
66+pendingStore: runtime.state.openKeyedStore({
67+namespace: `inbound.v1.pending.${accountPart}`,
68+maxEntries: WHATSAPP_DURABLE_INBOUND_PENDING_MAX_ENTRIES,
69+defaultTtlMs: WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS,
70+}),
71+completedStore: runtime.state.openKeyedStore({
72+namespace: `inbound.v1.completed.${accountPart}`,
73+maxEntries: WHATSAPP_DURABLE_INBOUND_COMPLETED_MAX_ENTRIES,
74+defaultTtlMs: WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS,
75+}),
76+pendingTtlMs: WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS,
77+completedTtlMs: WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS,
78+});
79+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。