@openclaw/whatsapp 2026.5.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +213 -0
- package/auth-presence.ts +80 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/dist/.boundary-tsc.stamp +1 -0
- package/dist/.boundary-tsc.tsbuildinfo +1 -0
- package/doctor-contract-api.ts +8 -0
- package/index.test.ts +13 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/openclaw.plugin.json +742 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +74 -0
- package/runtime-api.ts +86 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.test.ts +21 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +13 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.test.ts +182 -0
- package/src/accounts.ts +176 -0
- package/src/accounts.whatsapp-auth.test.ts +59 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.test.ts +330 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.test.ts +65 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.test.ts +81 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.test.ts +311 -0
- package/src/auth-store.ts +502 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.test.ts +843 -0
- package/src/auto-reply/deliver-reply.ts +279 -0
- package/src/auto-reply/heartbeat-runner.runtime.ts +33 -0
- package/src/auto-reply/heartbeat-runner.test.ts +214 -0
- package/src/auto-reply/heartbeat-runner.ts +330 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.test.ts +170 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.test.ts +189 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.audio-preflight.test.ts +103 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +217 -0
- package/src/auto-reply/monitor/group-members.test.ts +56 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.test.ts +97 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +21 -0
- package/src/auto-reply/monitor/inbound-dispatch.test.ts +895 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +422 -0
- package/src/auto-reply/monitor/last-route.test.ts +37 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.audio-preflight.test.ts +354 -0
- package/src/auto-reply/monitor/on-message.ts +291 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.audio-preflight.test.ts +420 -0
- package/src/auto-reply/monitor/process-message.test.ts +355 -0
- package/src/auto-reply/monitor/process-message.ts +531 -0
- package/src/auto-reply/monitor/runtime-api.ts +30 -0
- package/src/auto-reply/monitor-state.test.ts +64 -0
- package/src/auto-reply/monitor-state.ts +106 -0
- package/src/auto-reply/monitor.ts +600 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/session-snapshot.ts +69 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +63 -0
- package/src/auto-reply/web-auto-reply-monitor.test.ts +687 -0
- package/src/auto-reply/web-auto-reply-utils.test.ts +327 -0
- package/src/auto-reply.broadcast-groups.combined.test.ts +246 -0
- package/src/auto-reply.broadcast-groups.test-harness.ts +47 -0
- package/src/auto-reply.impl.ts +7 -0
- package/src/auto-reply.test-harness.ts +421 -0
- package/src/auto-reply.ts +1 -0
- package/src/auto-reply.web-auto-reply.compresses-common-formats-jpeg-cap.test.ts +427 -0
- package/src/auto-reply.web-auto-reply.connection-and-logging.e2e.test.ts +908 -0
- package/src/auto-reply.web-auto-reply.last-route.test.ts +184 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.test.ts +216 -0
- package/src/channel-actions.ts +84 -0
- package/src/channel-outbound.test.ts +131 -0
- package/src/channel-outbound.ts +36 -0
- package/src/channel-react-action.runtime.ts +7 -0
- package/src/channel-react-action.test.ts +294 -0
- package/src/channel-react-action.ts +84 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.test.ts +461 -0
- package/src/channel.setup.ts +28 -0
- package/src/channel.ts +336 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.test.ts +34 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.test.ts +121 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.test.ts +27 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.test.ts +282 -0
- package/src/connection-controller.ts +679 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +102 -0
- package/src/directory-config.test.ts +60 -0
- package/src/directory-config.ts +40 -0
- package/src/directory-contract.test.ts +56 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.test.ts +74 -0
- package/src/doctor.ts +56 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.test.ts +36 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.test.ts +53 -0
- package/src/group-session-key.ts +41 -0
- package/src/heartbeat-recipients.runtime.ts +6 -0
- package/src/heartbeat-recipients.test.ts +203 -0
- package/src/heartbeat-recipients.ts +104 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.test-harness.ts +37 -0
- package/src/inbound/access-control.test.ts +375 -0
- package/src/inbound/access-control.ts +237 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.test.ts +282 -0
- package/src/inbound/extract.ts +488 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.node.test.ts +83 -0
- package/src/inbound/media.ts +102 -0
- package/src/inbound/monitor.ts +890 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.test.ts +307 -0
- package/src/inbound/send-api.ts +141 -0
- package/src/inbound/send-result.ts +61 -0
- package/src/inbound/types.ts +106 -0
- package/src/inbound-context.contract.test.ts +32 -0
- package/src/inbound-policy.ts +231 -0
- package/src/inbound.media.test.ts +320 -0
- package/src/inbound.test.ts +290 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.test.ts +483 -0
- package/src/login-qr.ts +542 -0
- package/src/login.coverage.test.ts +133 -0
- package/src/login.test.ts +89 -0
- package/src/login.ts +70 -0
- package/src/logout.test.ts +162 -0
- package/src/media.test.ts +480 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +400 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.behavior.test.ts +5 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +362 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +306 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +729 -0
- package/src/monitor-inbox.test-harness.ts +304 -0
- package/src/normalize-target.ts +112 -0
- package/src/normalize.ts +6 -0
- package/src/outbound-adapter.poll.test.ts +62 -0
- package/src/outbound-adapter.sendpayload.test.ts +195 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.test.ts +560 -0
- package/src/outbound-base.ts +244 -0
- package/src/outbound-media-contract.ts +278 -0
- package/src/outbound-media.runtime.ts +36 -0
- package/src/outbound-payload.contract.test.ts +61 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/pairing-security.test-harness.ts +59 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.test.ts +83 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.test.ts +94 -0
- package/src/reaction-level.ts +21 -0
- package/src/reconnect.test.ts +51 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.test.ts +265 -0
- package/src/resolve-outbound-target.ts +54 -0
- package/src/resolve-target.test.ts +97 -0
- package/src/runtime-api.ts +60 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.test.ts +515 -0
- package/src/send.ts +287 -0
- package/src/session-contract.test.ts +31 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +26 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.test.ts +559 -0
- package/src/session.ts +330 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +446 -0
- package/src/setup-surface.test.ts +367 -0
- package/src/setup-surface.ts +69 -0
- package/src/setup-test-helpers.ts +216 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.test.ts +49 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +54 -0
- package/src/status-issues.test.ts +137 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.test.ts +199 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +180 -0
- package/src/test-helpers.ts +690 -0
- package/src/text-runtime.test.ts +162 -0
- package/src/text-runtime.ts +11 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
cacheInboundMessageMeta,
|
|
4
|
+
lookupInboundMessageMeta,
|
|
5
|
+
lookupInboundMessageMetaForTarget,
|
|
6
|
+
} from "./quoted-message.js";
|
|
7
|
+
|
|
8
|
+
describe("quoted message metadata cache", () => {
|
|
9
|
+
it("scopes cached metadata by account id", () => {
|
|
10
|
+
cacheInboundMessageMeta("account-a", "1555@s.whatsapp.net", "msg-1", {
|
|
11
|
+
participant: "111@s.whatsapp.net",
|
|
12
|
+
body: "hello from a",
|
|
13
|
+
fromMe: true,
|
|
14
|
+
});
|
|
15
|
+
cacheInboundMessageMeta("account-b", "1555@s.whatsapp.net", "msg-1", {
|
|
16
|
+
participant: "222@s.whatsapp.net",
|
|
17
|
+
body: "hello from b",
|
|
18
|
+
fromMe: false,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(lookupInboundMessageMeta("account-a", "1555@s.whatsapp.net", "msg-1")).toEqual({
|
|
22
|
+
participant: "111@s.whatsapp.net",
|
|
23
|
+
body: "hello from a",
|
|
24
|
+
fromMe: true,
|
|
25
|
+
});
|
|
26
|
+
expect(lookupInboundMessageMeta("account-b", "1555@s.whatsapp.net", "msg-1")).toEqual({
|
|
27
|
+
participant: "222@s.whatsapp.net",
|
|
28
|
+
body: "hello from b",
|
|
29
|
+
fromMe: false,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("can recover the original remoteJid for a matching direct-chat target", () => {
|
|
34
|
+
cacheInboundMessageMeta("account-c", "277038292303944@lid", "msg-2", {
|
|
35
|
+
participant: "5511976136970@s.whatsapp.net",
|
|
36
|
+
body: "hello from lid chat",
|
|
37
|
+
fromMe: true,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(
|
|
41
|
+
lookupInboundMessageMetaForTarget("account-c", "5511976136970@s.whatsapp.net", "msg-2"),
|
|
42
|
+
).toEqual({
|
|
43
|
+
remoteJid: "277038292303944@lid",
|
|
44
|
+
participant: "5511976136970@s.whatsapp.net",
|
|
45
|
+
body: "hello from lid chat",
|
|
46
|
+
fromMe: true,
|
|
47
|
+
});
|
|
48
|
+
expect(
|
|
49
|
+
lookupInboundMessageMetaForTarget("account-c", "99999999999@s.whatsapp.net", "msg-2"),
|
|
50
|
+
).toBeUndefined();
|
|
51
|
+
expect(
|
|
52
|
+
lookupInboundMessageMetaForTarget("missing", "5511976136970@s.whatsapp.net", "msg-2"),
|
|
53
|
+
).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("can recover a direct-chat remoteJid when only sender E164 was cached", () => {
|
|
57
|
+
cacheInboundMessageMeta("account-e", "277038292303944@lid", "msg-4", {
|
|
58
|
+
participantE164: "+5511976136970",
|
|
59
|
+
body: "hello from e164 participant",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(
|
|
63
|
+
lookupInboundMessageMetaForTarget("account-e", "5511976136970@s.whatsapp.net", "msg-4"),
|
|
64
|
+
).toEqual({
|
|
65
|
+
remoteJid: "277038292303944@lid",
|
|
66
|
+
participant: undefined,
|
|
67
|
+
participantE164: "+5511976136970",
|
|
68
|
+
body: "hello from e164 participant",
|
|
69
|
+
fromMe: undefined,
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("does not recover metadata from another chat when the target conversation differs", () => {
|
|
74
|
+
cacheInboundMessageMeta("account-d", "120363400000000000@g.us", "msg-3", {
|
|
75
|
+
participant: "111@s.whatsapp.net",
|
|
76
|
+
body: "group secret",
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
expect(
|
|
80
|
+
lookupInboundMessageMetaForTarget("account-d", "222@s.whatsapp.net", "msg-3"),
|
|
81
|
+
).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { MiscMessageGenerationOptions } from "@whiskeysockets/baileys";
|
|
2
|
+
import { jidToE164 } from "./text-runtime.js";
|
|
3
|
+
|
|
4
|
+
// ── Inbound message metadata cache ──────────────────────────────────────
|
|
5
|
+
// Maps messageId → { participant, participantE164, body, fromMe } so the
|
|
6
|
+
// outbound adapter can
|
|
7
|
+
// populate the quote key with the sender JID and preview text even though
|
|
8
|
+
// the outbound path only receives a bare messageId string.
|
|
9
|
+
|
|
10
|
+
type QuotedMeta = {
|
|
11
|
+
participant?: string;
|
|
12
|
+
participantE164?: string;
|
|
13
|
+
body?: string;
|
|
14
|
+
fromMe?: boolean;
|
|
15
|
+
};
|
|
16
|
+
type CacheEntry = QuotedMeta & { ts: number };
|
|
17
|
+
type QuotedMetaLookup = QuotedMeta & { remoteJid: string };
|
|
18
|
+
|
|
19
|
+
const CACHE_TTL_MS = 10 * 60 * 1000;
|
|
20
|
+
const MAX_ENTRIES = 500;
|
|
21
|
+
const cache = new Map<string, CacheEntry>();
|
|
22
|
+
|
|
23
|
+
function makeCacheKey(accountId: string, remoteJid: string, messageId: string): string {
|
|
24
|
+
return `${accountId}:${remoteJid}:${messageId}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function cacheInboundMessageMeta(
|
|
28
|
+
accountId: string,
|
|
29
|
+
remoteJid: string,
|
|
30
|
+
messageId: string,
|
|
31
|
+
meta: QuotedMeta,
|
|
32
|
+
): void {
|
|
33
|
+
if (!accountId || !messageId || !remoteJid) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (cache.size >= MAX_ENTRIES) {
|
|
37
|
+
const oldest = cache.keys().next().value;
|
|
38
|
+
if (oldest) {
|
|
39
|
+
cache.delete(oldest);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
cache.set(makeCacheKey(accountId, remoteJid, messageId), { ...meta, ts: Date.now() });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function lookupInboundMessageMeta(
|
|
46
|
+
accountId: string,
|
|
47
|
+
remoteJid: string,
|
|
48
|
+
messageId: string,
|
|
49
|
+
): QuotedMeta | undefined {
|
|
50
|
+
const cacheKey = makeCacheKey(accountId, remoteJid, messageId);
|
|
51
|
+
const entry = cache.get(cacheKey);
|
|
52
|
+
if (!entry) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
if (Date.now() - entry.ts > CACHE_TTL_MS) {
|
|
56
|
+
cache.delete(cacheKey);
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
participant: entry.participant,
|
|
61
|
+
participantE164: entry.participantE164,
|
|
62
|
+
body: entry.body,
|
|
63
|
+
fromMe: entry.fromMe,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function normalizeComparableJid(jid: string | undefined): string | undefined {
|
|
68
|
+
const normalized = jid?.trim().replace(/:\d+/, "").toLowerCase();
|
|
69
|
+
return normalized || undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isGroupJid(jid: string | undefined): boolean {
|
|
73
|
+
return Boolean(jid && jid.endsWith("@g.us"));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function areComparableE164sEqual(left: string | undefined, right: string | undefined): boolean {
|
|
77
|
+
const normalizedLeft = left?.trim();
|
|
78
|
+
const normalizedRight = right?.trim();
|
|
79
|
+
if (!normalizedLeft || !normalizedRight) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return normalizedLeft === normalizedRight;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function areComparableJidsEqual(left: string | undefined, right: string | undefined): boolean {
|
|
86
|
+
const normalizedLeft = normalizeComparableJid(left);
|
|
87
|
+
const normalizedRight = normalizeComparableJid(right);
|
|
88
|
+
if (!normalizedLeft || !normalizedRight) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (normalizedLeft === normalizedRight) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
const leftE164 = jidToE164(normalizedLeft);
|
|
95
|
+
const rightE164 = jidToE164(normalizedRight);
|
|
96
|
+
return Boolean(leftE164 && rightE164 && leftE164 === rightE164);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function matchesQuotedConversationTarget(targetJid: string, candidate: QuotedMetaLookup): boolean {
|
|
100
|
+
if (areComparableJidsEqual(targetJid, candidate.remoteJid)) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
if (isGroupJid(targetJid) || isGroupJid(candidate.remoteJid)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return (
|
|
107
|
+
areComparableJidsEqual(targetJid, candidate.participant) ||
|
|
108
|
+
areComparableE164sEqual(jidToE164(targetJid) ?? undefined, candidate.participantE164)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function lookupInboundMessageMetaForTarget(
|
|
113
|
+
accountId: string,
|
|
114
|
+
targetJid: string,
|
|
115
|
+
messageId: string,
|
|
116
|
+
): QuotedMetaLookup | undefined {
|
|
117
|
+
if (!accountId || !messageId || !targetJid) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const exact = lookupInboundMessageMeta(accountId, targetJid, messageId);
|
|
121
|
+
if (exact) {
|
|
122
|
+
return {
|
|
123
|
+
remoteJid: targetJid,
|
|
124
|
+
participant: exact.participant,
|
|
125
|
+
participantE164: exact.participantE164,
|
|
126
|
+
body: exact.body,
|
|
127
|
+
fromMe: exact.fromMe,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const prefix = `${accountId}:`;
|
|
131
|
+
const suffix = `:${messageId}`;
|
|
132
|
+
let matched: QuotedMetaLookup | undefined;
|
|
133
|
+
for (const [cacheKey, entry] of cache.entries()) {
|
|
134
|
+
if (!cacheKey.startsWith(prefix) || !cacheKey.endsWith(suffix)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (Date.now() - entry.ts > CACHE_TTL_MS) {
|
|
138
|
+
cache.delete(cacheKey);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const remoteJid = cacheKey.slice(prefix.length, cacheKey.length - suffix.length);
|
|
142
|
+
const candidate = {
|
|
143
|
+
remoteJid,
|
|
144
|
+
participant: entry.participant,
|
|
145
|
+
participantE164: entry.participantE164,
|
|
146
|
+
body: entry.body,
|
|
147
|
+
fromMe: entry.fromMe,
|
|
148
|
+
};
|
|
149
|
+
if (!matchesQuotedConversationTarget(targetJid, candidate)) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (matched) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
matched = candidate;
|
|
156
|
+
}
|
|
157
|
+
return matched;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function buildQuotedMessageOptions(params: {
|
|
161
|
+
messageId?: string | null;
|
|
162
|
+
remoteJid?: string | null;
|
|
163
|
+
fromMe?: boolean;
|
|
164
|
+
participant?: string;
|
|
165
|
+
/** Original message text — shown in the quote preview bubble. */
|
|
166
|
+
messageText?: string;
|
|
167
|
+
}): MiscMessageGenerationOptions | undefined {
|
|
168
|
+
const id = params.messageId?.trim();
|
|
169
|
+
const remoteJid = params.remoteJid?.trim();
|
|
170
|
+
if (!id || !remoteJid) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
quoted: {
|
|
175
|
+
key: {
|
|
176
|
+
remoteJid,
|
|
177
|
+
id,
|
|
178
|
+
fromMe: params.fromMe ?? false,
|
|
179
|
+
participant: params.participant,
|
|
180
|
+
},
|
|
181
|
+
message: { conversation: params.messageText ?? "" },
|
|
182
|
+
},
|
|
183
|
+
} as MiscMessageGenerationOptions;
|
|
184
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { resolveWhatsAppReactionLevel } from "./reaction-level.js";
|
|
4
|
+
|
|
5
|
+
describe("resolveWhatsAppReactionLevel", () => {
|
|
6
|
+
it("defaults to minimal level when reactionLevel is not set", () => {
|
|
7
|
+
const cfg: OpenClawConfig = {
|
|
8
|
+
channels: { whatsapp: {} },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const result = resolveWhatsAppReactionLevel({ cfg });
|
|
12
|
+
expect(result).toEqual({
|
|
13
|
+
level: "minimal",
|
|
14
|
+
ackEnabled: false,
|
|
15
|
+
agentReactionsEnabled: true,
|
|
16
|
+
agentReactionGuidance: "minimal",
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("returns off level with no reactions enabled", () => {
|
|
21
|
+
const cfg: OpenClawConfig = {
|
|
22
|
+
channels: { whatsapp: { reactionLevel: "off" } },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const result = resolveWhatsAppReactionLevel({ cfg });
|
|
26
|
+
expect(result).toEqual({
|
|
27
|
+
level: "off",
|
|
28
|
+
ackEnabled: false,
|
|
29
|
+
agentReactionsEnabled: false,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("returns ack level with only ackEnabled", () => {
|
|
34
|
+
const cfg: OpenClawConfig = {
|
|
35
|
+
channels: { whatsapp: { reactionLevel: "ack" } },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const result = resolveWhatsAppReactionLevel({ cfg });
|
|
39
|
+
expect(result).toEqual({
|
|
40
|
+
level: "ack",
|
|
41
|
+
ackEnabled: true,
|
|
42
|
+
agentReactionsEnabled: false,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("returns minimal level with agent reactions enabled and minimal guidance", () => {
|
|
47
|
+
const cfg: OpenClawConfig = {
|
|
48
|
+
channels: { whatsapp: { reactionLevel: "minimal" } },
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const result = resolveWhatsAppReactionLevel({ cfg });
|
|
52
|
+
expect(result).toEqual({
|
|
53
|
+
level: "minimal",
|
|
54
|
+
ackEnabled: false,
|
|
55
|
+
agentReactionsEnabled: true,
|
|
56
|
+
agentReactionGuidance: "minimal",
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("returns extensive level with agent reactions enabled and extensive guidance", () => {
|
|
61
|
+
const cfg: OpenClawConfig = {
|
|
62
|
+
channels: { whatsapp: { reactionLevel: "extensive" } },
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const result = resolveWhatsAppReactionLevel({ cfg });
|
|
66
|
+
expect(result).toEqual({
|
|
67
|
+
level: "extensive",
|
|
68
|
+
ackEnabled: false,
|
|
69
|
+
agentReactionsEnabled: true,
|
|
70
|
+
agentReactionGuidance: "extensive",
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("resolves reaction level from a specific account", () => {
|
|
75
|
+
const cfg: OpenClawConfig = {
|
|
76
|
+
channels: {
|
|
77
|
+
whatsapp: {
|
|
78
|
+
reactionLevel: "minimal",
|
|
79
|
+
accounts: {
|
|
80
|
+
work: { reactionLevel: "extensive" },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const result = resolveWhatsAppReactionLevel({ cfg, accountId: "work" });
|
|
87
|
+
expect(result).toEqual({
|
|
88
|
+
level: "extensive",
|
|
89
|
+
ackEnabled: false,
|
|
90
|
+
agentReactionsEnabled: true,
|
|
91
|
+
agentReactionGuidance: "extensive",
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
2
|
+
import { resolveReactionLevel, type ResolvedReactionLevel } from "openclaw/plugin-sdk/text-runtime";
|
|
3
|
+
import { resolveMergedWhatsAppAccountConfig } from "./account-config.js";
|
|
4
|
+
|
|
5
|
+
type ResolvedWhatsAppReactionLevel = ResolvedReactionLevel;
|
|
6
|
+
|
|
7
|
+
/** Resolve the effective reaction level and its implications for WhatsApp. */
|
|
8
|
+
export function resolveWhatsAppReactionLevel(params: {
|
|
9
|
+
cfg: OpenClawConfig;
|
|
10
|
+
accountId?: string;
|
|
11
|
+
}): ResolvedWhatsAppReactionLevel {
|
|
12
|
+
const account = resolveMergedWhatsAppAccountConfig({
|
|
13
|
+
cfg: params.cfg,
|
|
14
|
+
accountId: params.accountId,
|
|
15
|
+
});
|
|
16
|
+
return resolveReactionLevel({
|
|
17
|
+
value: account.reactionLevel,
|
|
18
|
+
defaultLevel: "minimal",
|
|
19
|
+
invalidFallback: "minimal",
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
computeBackoff,
|
|
5
|
+
DEFAULT_HEARTBEAT_SECONDS,
|
|
6
|
+
DEFAULT_RECONNECT_POLICY,
|
|
7
|
+
resolveHeartbeatSeconds,
|
|
8
|
+
resolveReconnectPolicy,
|
|
9
|
+
sleepWithAbort,
|
|
10
|
+
} from "./reconnect.js";
|
|
11
|
+
|
|
12
|
+
describe("web reconnect helpers", () => {
|
|
13
|
+
const cfg: OpenClawConfig = {};
|
|
14
|
+
|
|
15
|
+
it("resolves sane reconnect defaults with clamps", () => {
|
|
16
|
+
const policy = resolveReconnectPolicy(cfg, {
|
|
17
|
+
initialMs: 100,
|
|
18
|
+
maxMs: 5,
|
|
19
|
+
factor: 20,
|
|
20
|
+
jitter: 2,
|
|
21
|
+
maxAttempts: -1,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expect(policy.initialMs).toBe(250); // clamped to minimum
|
|
25
|
+
expect(policy.maxMs).toBeGreaterThanOrEqual(policy.initialMs);
|
|
26
|
+
expect(policy.factor).toBeLessThanOrEqual(10);
|
|
27
|
+
expect(policy.jitter).toBeLessThanOrEqual(1);
|
|
28
|
+
expect(policy.maxAttempts).toBeGreaterThanOrEqual(0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("computes increasing backoff with jitter", () => {
|
|
32
|
+
const policy = { ...DEFAULT_RECONNECT_POLICY, jitter: 0 };
|
|
33
|
+
const first = computeBackoff(policy, 1);
|
|
34
|
+
const second = computeBackoff(policy, 2);
|
|
35
|
+
expect(first).toBe(policy.initialMs);
|
|
36
|
+
expect(second).toBeGreaterThan(first);
|
|
37
|
+
expect(second).toBeLessThanOrEqual(policy.maxMs);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("returns heartbeat default when unset", () => {
|
|
41
|
+
expect(resolveHeartbeatSeconds(cfg)).toBe(DEFAULT_HEARTBEAT_SECONDS);
|
|
42
|
+
expect(resolveHeartbeatSeconds(cfg, 5)).toBe(5);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("sleepWithAbort rejects on abort", async () => {
|
|
46
|
+
const controller = new AbortController();
|
|
47
|
+
const promise = sleepWithAbort(50, controller.signal);
|
|
48
|
+
controller.abort();
|
|
49
|
+
await expect(promise).rejects.toThrow("aborted");
|
|
50
|
+
});
|
|
51
|
+
});
|
package/src/reconnect.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
3
|
+
import {
|
|
4
|
+
computeBackoff,
|
|
5
|
+
sleepWithAbort,
|
|
6
|
+
type BackoffPolicy,
|
|
7
|
+
} from "openclaw/plugin-sdk/runtime-env";
|
|
8
|
+
import { clamp } from "openclaw/plugin-sdk/text-runtime";
|
|
9
|
+
|
|
10
|
+
export type ReconnectPolicy = BackoffPolicy & {
|
|
11
|
+
maxAttempts: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_HEARTBEAT_SECONDS = 60;
|
|
15
|
+
export const DEFAULT_RECONNECT_POLICY: ReconnectPolicy = {
|
|
16
|
+
initialMs: 2_000,
|
|
17
|
+
maxMs: 30_000,
|
|
18
|
+
factor: 1.8,
|
|
19
|
+
jitter: 0.25,
|
|
20
|
+
maxAttempts: 12,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function resolveHeartbeatSeconds(cfg: OpenClawConfig, overrideSeconds?: number): number {
|
|
24
|
+
const candidate = overrideSeconds ?? cfg.web?.heartbeatSeconds;
|
|
25
|
+
if (typeof candidate === "number" && candidate > 0) {
|
|
26
|
+
return candidate;
|
|
27
|
+
}
|
|
28
|
+
return DEFAULT_HEARTBEAT_SECONDS;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolveReconnectPolicy(
|
|
32
|
+
cfg: OpenClawConfig,
|
|
33
|
+
overrides?: Partial<ReconnectPolicy>,
|
|
34
|
+
): ReconnectPolicy {
|
|
35
|
+
const reconnectOverrides = cfg.web?.reconnect ?? {};
|
|
36
|
+
const overrideConfig = overrides ?? {};
|
|
37
|
+
const merged = {
|
|
38
|
+
...DEFAULT_RECONNECT_POLICY,
|
|
39
|
+
...reconnectOverrides,
|
|
40
|
+
...overrideConfig,
|
|
41
|
+
} as ReconnectPolicy;
|
|
42
|
+
|
|
43
|
+
merged.initialMs = Math.max(250, merged.initialMs);
|
|
44
|
+
merged.maxMs = Math.max(merged.initialMs, merged.maxMs);
|
|
45
|
+
merged.factor = clamp(merged.factor, 1.1, 10);
|
|
46
|
+
merged.jitter = clamp(merged.jitter, 0, 1);
|
|
47
|
+
merged.maxAttempts = Math.max(0, Math.floor(merged.maxAttempts));
|
|
48
|
+
return merged;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { computeBackoff, sleepWithAbort };
|
|
52
|
+
|
|
53
|
+
export function newConnectionId() {
|
|
54
|
+
return randomUUID();
|
|
55
|
+
}
|