@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.
Files changed (239) hide show
  1. package/action-runtime-api.ts +1 -0
  2. package/action-runtime.runtime.ts +1 -0
  3. package/api.ts +213 -0
  4. package/auth-presence.ts +80 -0
  5. package/channel-config-api.ts +1 -0
  6. package/channel-plugin-api.ts +3 -0
  7. package/config-api.ts +4 -0
  8. package/constants.ts +1 -0
  9. package/contract-api.ts +29 -0
  10. package/directory-contract-api.ts +4 -0
  11. package/dist/.boundary-tsc.stamp +1 -0
  12. package/dist/.boundary-tsc.tsbuildinfo +1 -0
  13. package/doctor-contract-api.ts +8 -0
  14. package/index.test.ts +13 -0
  15. package/index.ts +16 -0
  16. package/legacy-session-surface-api.ts +6 -0
  17. package/legacy-state-migrations-api.ts +1 -0
  18. package/light-runtime-api.ts +12 -0
  19. package/login-qr-api.ts +1 -0
  20. package/login-qr-runtime.ts +23 -0
  21. package/openclaw.plugin.json +742 -0
  22. package/outbound-payload-test-api.ts +1 -0
  23. package/package.json +74 -0
  24. package/runtime-api.ts +86 -0
  25. package/secret-contract-api.ts +4 -0
  26. package/security-contract-api.ts +4 -0
  27. package/setup-entry.test.ts +21 -0
  28. package/setup-entry.ts +21 -0
  29. package/setup-plugin-api.ts +3 -0
  30. package/src/account-config.ts +77 -0
  31. package/src/account-ids.ts +13 -0
  32. package/src/account-types.ts +5 -0
  33. package/src/accounts.test.ts +182 -0
  34. package/src/accounts.ts +176 -0
  35. package/src/accounts.whatsapp-auth.test.ts +59 -0
  36. package/src/action-runtime-target-auth.ts +27 -0
  37. package/src/action-runtime.test.ts +330 -0
  38. package/src/action-runtime.ts +76 -0
  39. package/src/active-listener.test.ts +65 -0
  40. package/src/active-listener.ts +17 -0
  41. package/src/agent-tools-login.test.ts +81 -0
  42. package/src/agent-tools-login.ts +113 -0
  43. package/src/approval-auth.test.ts +24 -0
  44. package/src/approval-auth.ts +27 -0
  45. package/src/auth-store.runtime.ts +1 -0
  46. package/src/auth-store.test.ts +311 -0
  47. package/src/auth-store.ts +502 -0
  48. package/src/auto-reply/config.runtime.ts +16 -0
  49. package/src/auto-reply/constants.ts +1 -0
  50. package/src/auto-reply/deliver-reply.test.ts +843 -0
  51. package/src/auto-reply/deliver-reply.ts +279 -0
  52. package/src/auto-reply/heartbeat-runner.runtime.ts +33 -0
  53. package/src/auto-reply/heartbeat-runner.test.ts +214 -0
  54. package/src/auto-reply/heartbeat-runner.ts +330 -0
  55. package/src/auto-reply/loggers.ts +6 -0
  56. package/src/auto-reply/mentions.ts +131 -0
  57. package/src/auto-reply/monitor/ack-reaction.test.ts +170 -0
  58. package/src/auto-reply/monitor/ack-reaction.ts +99 -0
  59. package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
  60. package/src/auto-reply/monitor/broadcast.ts +153 -0
  61. package/src/auto-reply/monitor/commands.ts +19 -0
  62. package/src/auto-reply/monitor/echo.ts +64 -0
  63. package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
  64. package/src/auto-reply/monitor/group-activation.test.ts +189 -0
  65. package/src/auto-reply/monitor/group-activation.ts +73 -0
  66. package/src/auto-reply/monitor/group-gating.audio-preflight.test.ts +103 -0
  67. package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
  68. package/src/auto-reply/monitor/group-gating.ts +217 -0
  69. package/src/auto-reply/monitor/group-members.test.ts +56 -0
  70. package/src/auto-reply/monitor/group-members.ts +65 -0
  71. package/src/auto-reply/monitor/inbound-context.test.ts +97 -0
  72. package/src/auto-reply/monitor/inbound-context.ts +92 -0
  73. package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +21 -0
  74. package/src/auto-reply/monitor/inbound-dispatch.test.ts +895 -0
  75. package/src/auto-reply/monitor/inbound-dispatch.ts +422 -0
  76. package/src/auto-reply/monitor/last-route.test.ts +37 -0
  77. package/src/auto-reply/monitor/last-route.ts +61 -0
  78. package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
  79. package/src/auto-reply/monitor/message-line.ts +54 -0
  80. package/src/auto-reply/monitor/on-message.audio-preflight.test.ts +354 -0
  81. package/src/auto-reply/monitor/on-message.ts +291 -0
  82. package/src/auto-reply/monitor/peer.ts +17 -0
  83. package/src/auto-reply/monitor/process-message.audio-preflight.test.ts +420 -0
  84. package/src/auto-reply/monitor/process-message.test.ts +355 -0
  85. package/src/auto-reply/monitor/process-message.ts +531 -0
  86. package/src/auto-reply/monitor/runtime-api.ts +30 -0
  87. package/src/auto-reply/monitor-state.test.ts +64 -0
  88. package/src/auto-reply/monitor-state.ts +106 -0
  89. package/src/auto-reply/monitor.ts +600 -0
  90. package/src/auto-reply/reply-resolver.runtime.ts +1 -0
  91. package/src/auto-reply/session-snapshot.ts +69 -0
  92. package/src/auto-reply/types.ts +48 -0
  93. package/src/auto-reply/util.ts +63 -0
  94. package/src/auto-reply/web-auto-reply-monitor.test.ts +687 -0
  95. package/src/auto-reply/web-auto-reply-utils.test.ts +327 -0
  96. package/src/auto-reply.broadcast-groups.combined.test.ts +246 -0
  97. package/src/auto-reply.broadcast-groups.test-harness.ts +47 -0
  98. package/src/auto-reply.impl.ts +7 -0
  99. package/src/auto-reply.test-harness.ts +421 -0
  100. package/src/auto-reply.ts +1 -0
  101. package/src/auto-reply.web-auto-reply.compresses-common-formats-jpeg-cap.test.ts +427 -0
  102. package/src/auto-reply.web-auto-reply.connection-and-logging.e2e.test.ts +908 -0
  103. package/src/auto-reply.web-auto-reply.last-route.test.ts +184 -0
  104. package/src/channel-actions.runtime.ts +7 -0
  105. package/src/channel-actions.test.ts +216 -0
  106. package/src/channel-actions.ts +84 -0
  107. package/src/channel-outbound.test.ts +131 -0
  108. package/src/channel-outbound.ts +36 -0
  109. package/src/channel-react-action.runtime.ts +7 -0
  110. package/src/channel-react-action.test.ts +294 -0
  111. package/src/channel-react-action.ts +84 -0
  112. package/src/channel.runtime.ts +117 -0
  113. package/src/channel.setup.test.ts +461 -0
  114. package/src/channel.setup.ts +28 -0
  115. package/src/channel.ts +336 -0
  116. package/src/command-policy.ts +7 -0
  117. package/src/config-accessors.test.ts +34 -0
  118. package/src/config-accessors.ts +22 -0
  119. package/src/config-schema.test.ts +121 -0
  120. package/src/config-schema.ts +6 -0
  121. package/src/config-ui-hints.ts +24 -0
  122. package/src/connection-controller-registry.test.ts +27 -0
  123. package/src/connection-controller-registry.ts +49 -0
  124. package/src/connection-controller.test.ts +282 -0
  125. package/src/connection-controller.ts +679 -0
  126. package/src/creds-files.ts +19 -0
  127. package/src/creds-persistence.ts +102 -0
  128. package/src/directory-config.test.ts +60 -0
  129. package/src/directory-config.ts +40 -0
  130. package/src/directory-contract.test.ts +56 -0
  131. package/src/doctor-contract.ts +11 -0
  132. package/src/doctor.test.ts +74 -0
  133. package/src/doctor.ts +56 -0
  134. package/src/group-intro.ts +15 -0
  135. package/src/group-policy.test.ts +36 -0
  136. package/src/group-policy.ts +40 -0
  137. package/src/group-session-contract.ts +20 -0
  138. package/src/group-session-key.test.ts +53 -0
  139. package/src/group-session-key.ts +41 -0
  140. package/src/heartbeat-recipients.runtime.ts +6 -0
  141. package/src/heartbeat-recipients.test.ts +203 -0
  142. package/src/heartbeat-recipients.ts +104 -0
  143. package/src/heartbeat.ts +34 -0
  144. package/src/identity.ts +164 -0
  145. package/src/inbound/access-control.test-harness.ts +37 -0
  146. package/src/inbound/access-control.test.ts +375 -0
  147. package/src/inbound/access-control.ts +237 -0
  148. package/src/inbound/dedupe.ts +132 -0
  149. package/src/inbound/extract.test.ts +282 -0
  150. package/src/inbound/extract.ts +488 -0
  151. package/src/inbound/lifecycle.ts +39 -0
  152. package/src/inbound/media.node.test.ts +83 -0
  153. package/src/inbound/media.ts +102 -0
  154. package/src/inbound/monitor.ts +890 -0
  155. package/src/inbound/runtime-api.ts +7 -0
  156. package/src/inbound/save-media.runtime.ts +1 -0
  157. package/src/inbound/send-api.test.ts +307 -0
  158. package/src/inbound/send-api.ts +141 -0
  159. package/src/inbound/send-result.ts +61 -0
  160. package/src/inbound/types.ts +106 -0
  161. package/src/inbound-context.contract.test.ts +32 -0
  162. package/src/inbound-policy.ts +231 -0
  163. package/src/inbound.media.test.ts +320 -0
  164. package/src/inbound.test.ts +290 -0
  165. package/src/inbound.ts +9 -0
  166. package/src/login-qr.test.ts +483 -0
  167. package/src/login-qr.ts +542 -0
  168. package/src/login.coverage.test.ts +133 -0
  169. package/src/login.test.ts +89 -0
  170. package/src/login.ts +70 -0
  171. package/src/logout.test.ts +162 -0
  172. package/src/media.test.ts +480 -0
  173. package/src/media.ts +10 -0
  174. package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +400 -0
  175. package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
  176. package/src/monitor-inbox.behavior.test.ts +5 -0
  177. package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +362 -0
  178. package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +306 -0
  179. package/src/monitor-inbox.streams-inbound-messages.test-support.ts +729 -0
  180. package/src/monitor-inbox.test-harness.ts +304 -0
  181. package/src/normalize-target.ts +112 -0
  182. package/src/normalize.ts +6 -0
  183. package/src/outbound-adapter.poll.test.ts +62 -0
  184. package/src/outbound-adapter.sendpayload.test.ts +195 -0
  185. package/src/outbound-adapter.ts +36 -0
  186. package/src/outbound-base.test.ts +560 -0
  187. package/src/outbound-base.ts +244 -0
  188. package/src/outbound-media-contract.ts +278 -0
  189. package/src/outbound-media.runtime.ts +36 -0
  190. package/src/outbound-payload.contract.test.ts +61 -0
  191. package/src/outbound-send-deps.ts +1 -0
  192. package/src/outbound-test-support.ts +16 -0
  193. package/src/pairing-security.test-harness.ts +59 -0
  194. package/src/qr-image.ts +1 -0
  195. package/src/qr-terminal.ts +1 -0
  196. package/src/quoted-message.test.ts +83 -0
  197. package/src/quoted-message.ts +184 -0
  198. package/src/reaction-level.test.ts +94 -0
  199. package/src/reaction-level.ts +21 -0
  200. package/src/reconnect.test.ts +51 -0
  201. package/src/reconnect.ts +55 -0
  202. package/src/resolve-outbound-target.test.ts +265 -0
  203. package/src/resolve-outbound-target.ts +54 -0
  204. package/src/resolve-target.test.ts +97 -0
  205. package/src/runtime-api.ts +60 -0
  206. package/src/runtime-group-policy.ts +16 -0
  207. package/src/runtime.ts +9 -0
  208. package/src/security-contract.ts +47 -0
  209. package/src/security-fix.ts +71 -0
  210. package/src/send.test.ts +515 -0
  211. package/src/send.ts +287 -0
  212. package/src/session-contract.test.ts +31 -0
  213. package/src/session-contract.ts +43 -0
  214. package/src/session-errors.ts +125 -0
  215. package/src/session-route.ts +26 -0
  216. package/src/session.runtime.ts +8 -0
  217. package/src/session.test.ts +559 -0
  218. package/src/session.ts +330 -0
  219. package/src/setup-core.ts +52 -0
  220. package/src/setup-finalize.ts +446 -0
  221. package/src/setup-surface.test.ts +367 -0
  222. package/src/setup-surface.ts +69 -0
  223. package/src/setup-test-helpers.ts +216 -0
  224. package/src/shared.ts +291 -0
  225. package/src/socket-timing.test.ts +49 -0
  226. package/src/socket-timing.ts +38 -0
  227. package/src/state-migrations.ts +54 -0
  228. package/src/status-issues.test.ts +137 -0
  229. package/src/status-issues.ts +185 -0
  230. package/src/system-prompt.test.ts +199 -0
  231. package/src/system-prompt.ts +31 -0
  232. package/src/targets-runtime.ts +180 -0
  233. package/src/test-helpers.ts +690 -0
  234. package/src/text-runtime.test.ts +162 -0
  235. package/src/text-runtime.ts +11 -0
  236. package/src/vcard.ts +84 -0
  237. package/targets.ts +5 -0
  238. package/test-api.ts +2 -0
  239. package/tsconfig.json +16 -0
@@ -0,0 +1,687 @@
1
+ import fs from "node:fs/promises";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
5
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
6
+ import type { WhatsAppSendResult } from "../inbound/send-result.js";
7
+ import { buildMentionConfig } from "./mentions.js";
8
+ import { applyGroupGating, type GroupHistoryEntry } from "./monitor/group-gating.js";
9
+ import { buildInboundLine, formatReplyContext } from "./monitor/message-line.js";
10
+ import type { WebInboundMsg } from "./types.js";
11
+
12
+ let sessionDir: string | undefined;
13
+ let sessionStorePath: string;
14
+
15
+ function acceptedSendResult(kind: "media" | "text", id: string): WhatsAppSendResult {
16
+ return {
17
+ kind,
18
+ messageId: id,
19
+ messageIds: [id],
20
+ keys: [{ id }],
21
+ providerAccepted: true,
22
+ };
23
+ }
24
+
25
+ beforeEach(async () => {
26
+ sessionDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-group-gating-"));
27
+ sessionStorePath = path.join(sessionDir, "sessions.json");
28
+ await fs.writeFile(sessionStorePath, "{}");
29
+ });
30
+
31
+ afterEach(async () => {
32
+ if (sessionDir) {
33
+ await fs.rm(sessionDir, { recursive: true, force: true });
34
+ sessionDir = undefined;
35
+ }
36
+ });
37
+
38
+ const makeConfig = (overrides: Record<string, unknown>) =>
39
+ ({
40
+ channels: {
41
+ whatsapp: {
42
+ groupPolicy: "open",
43
+ groups: { "*": { requireMention: true } },
44
+ },
45
+ },
46
+ session: { store: sessionStorePath },
47
+ ...overrides,
48
+ }) as unknown as import("openclaw/plugin-sdk/config-types").OpenClawConfig;
49
+
50
+ async function runGroupGating(params: {
51
+ cfg: import("openclaw/plugin-sdk/config-types").OpenClawConfig;
52
+ msg: WebInboundMsg;
53
+ conversationId?: string;
54
+ agentId?: string;
55
+ selfChatMode?: boolean;
56
+ authDir?: string;
57
+ }) {
58
+ const groupHistories = new Map<string, GroupHistoryEntry[]>();
59
+ const conversationId = params.conversationId ?? "123@g.us";
60
+ const agentId = params.agentId ?? "main";
61
+ const sessionKey = `agent:${agentId}:whatsapp:group:${conversationId}`;
62
+ const baseMentionConfig = buildMentionConfig(params.cfg, undefined);
63
+ const result = await applyGroupGating({
64
+ cfg: params.cfg,
65
+ msg: params.msg,
66
+ conversationId,
67
+ groupHistoryKey: `whatsapp:default:group:${conversationId}`,
68
+ agentId,
69
+ sessionKey,
70
+ baseMentionConfig,
71
+ authDir: params.authDir,
72
+ selfChatMode: params.selfChatMode,
73
+ groupHistories,
74
+ groupHistoryLimit: 10,
75
+ groupMemberNames: new Map(),
76
+ logVerbose: () => {},
77
+ replyLogger: { debug: () => {} },
78
+ });
79
+ return { result, groupHistories };
80
+ }
81
+
82
+ function createGroupMessage(overrides: Partial<WebInboundMsg> = {}): WebInboundMsg {
83
+ return {
84
+ id: "g1",
85
+ from: "123@g.us",
86
+ conversationId: "123@g.us",
87
+ chatId: "123@g.us",
88
+ chatType: "group",
89
+ to: "+2",
90
+ accountId: "default",
91
+ body: "hello group",
92
+ senderE164: "+111",
93
+ senderName: "Alice",
94
+ selfE164: "+999",
95
+ sendComposing: async () => {},
96
+ reply: async (_text, _options) => acceptedSendResult("text", "r1"),
97
+ sendMedia: async (_payload, _options) => acceptedSendResult("media", "m1"),
98
+ ...overrides,
99
+ };
100
+ }
101
+
102
+ function makeOwnerGroupConfig() {
103
+ return makeConfig({
104
+ channels: {
105
+ whatsapp: {
106
+ allowFrom: ["+111"],
107
+ groups: { "*": { requireMention: true } },
108
+ },
109
+ },
110
+ });
111
+ }
112
+
113
+ function makeInboundCfg(messagePrefix = "") {
114
+ return {
115
+ agents: { defaults: { workspace: "/tmp/openclaw" } },
116
+ channels: { whatsapp: { messagePrefix } },
117
+ } as never;
118
+ }
119
+
120
+ describe("applyGroupGating", () => {
121
+ it("treats reply-to-bot as implicit mention", async () => {
122
+ const cfg = makeConfig({});
123
+ const { result } = await runGroupGating({
124
+ cfg,
125
+ msg: createGroupMessage({
126
+ id: "m1",
127
+ to: "+15550000",
128
+ accountId: "default",
129
+ body: "following up",
130
+ timestamp: Date.now(),
131
+ selfJid: "15551234567@s.whatsapp.net",
132
+ selfE164: "+15551234567",
133
+ replyToId: "m0",
134
+ replyToBody: "bot said hi",
135
+ replyToSender: "+15551234567",
136
+ replyToSenderJid: "15551234567@s.whatsapp.net",
137
+ replyToSenderE164: "+15551234567",
138
+ }),
139
+ });
140
+
141
+ expect(result.shouldProcess).toBe(true);
142
+ });
143
+
144
+ it("does not treat self-number quoted replies as implicit mention in selfChatMode groups", async () => {
145
+ const cfg = makeConfig({
146
+ channels: {
147
+ whatsapp: {
148
+ selfChatMode: true,
149
+ groupPolicy: "open",
150
+ groups: { "*": { requireMention: true } },
151
+ },
152
+ },
153
+ });
154
+ const { result } = await runGroupGating({
155
+ cfg,
156
+ selfChatMode: true,
157
+ msg: createGroupMessage({
158
+ id: "m-self-reply",
159
+ to: "+15550000",
160
+ accountId: "default",
161
+ body: "following up on my own message",
162
+ timestamp: Date.now(),
163
+ senderE164: "+15551234567",
164
+ senderJid: "15551234567@s.whatsapp.net",
165
+ selfJid: "15551234567@s.whatsapp.net",
166
+ selfE164: "+15551234567",
167
+ replyToId: "m0",
168
+ replyToBody: "my earlier message",
169
+ replyToSender: "+15551234567",
170
+ replyToSenderJid: "15551234567@s.whatsapp.net",
171
+ replyToSenderE164: "+15551234567",
172
+ }),
173
+ });
174
+
175
+ expect(result.shouldProcess).toBe(false);
176
+ });
177
+
178
+ it("still treats reply-to-bot as implicit mention in selfChatMode when sender is a different user", async () => {
179
+ const cfg = makeConfig({
180
+ channels: {
181
+ whatsapp: {
182
+ selfChatMode: true,
183
+ groupPolicy: "open",
184
+ groups: { "*": { requireMention: true } },
185
+ },
186
+ },
187
+ });
188
+ const { result } = await runGroupGating({
189
+ cfg,
190
+ selfChatMode: true,
191
+ msg: createGroupMessage({
192
+ id: "m-other-reply",
193
+ to: "+15550000",
194
+ accountId: "default",
195
+ body: "following up on bot reply",
196
+ timestamp: Date.now(),
197
+ senderE164: "+15559999999",
198
+ senderJid: "15559999999@s.whatsapp.net",
199
+ selfJid: "15551234567@s.whatsapp.net",
200
+ selfE164: "+15551234567",
201
+ replyToId: "m0",
202
+ replyToBody: "bot earlier response",
203
+ replyToSender: "+15551234567",
204
+ replyToSenderJid: "15551234567@s.whatsapp.net",
205
+ replyToSenderE164: "+15551234567",
206
+ }),
207
+ });
208
+
209
+ expect(result.shouldProcess).toBe(true);
210
+ });
211
+
212
+ it("processes explicit group @mentions when self is in allowFrom (#49317)", async () => {
213
+ if (!sessionDir) {
214
+ throw new Error("sessionDir not initialized");
215
+ }
216
+ await fs.writeFile(
217
+ path.join(sessionDir, "lid-mapping-216372600647751_reverse.json"),
218
+ JSON.stringify("+15551234567"),
219
+ );
220
+ const cfg = makeConfig({
221
+ channels: {
222
+ whatsapp: {
223
+ allowFrom: ["+15551234567"],
224
+ groupPolicy: "open",
225
+ groups: { "*": { requireMention: true } },
226
+ },
227
+ },
228
+ });
229
+ const msg = createGroupMessage({
230
+ id: "g-self-lid-mention",
231
+ accountId: "default",
232
+ body: "@216372600647751 can you see this?",
233
+ mentionedJids: ["216372600647751@lid"],
234
+ senderE164: "+15550001111",
235
+ senderName: "Alice",
236
+ selfE164: "+15551234567",
237
+ selfJid: "15551234567@s.whatsapp.net",
238
+ });
239
+
240
+ const { result, groupHistories } = await runGroupGating({
241
+ cfg,
242
+ authDir: sessionDir,
243
+ msg,
244
+ });
245
+
246
+ expect(result.shouldProcess).toBe(true);
247
+ expect(msg.wasMentioned).toBe(true);
248
+ expect(groupHistories.get("whatsapp:default:group:123@g.us")).toBeUndefined();
249
+ });
250
+
251
+ it("honors per-account selfChatMode overrides before suppressing implicit mentions", async () => {
252
+ const cfg = makeConfig({
253
+ channels: {
254
+ whatsapp: {
255
+ selfChatMode: true,
256
+ groupPolicy: "open",
257
+ groups: { "*": { requireMention: true } },
258
+ accounts: {
259
+ work: {
260
+ selfChatMode: false,
261
+ },
262
+ },
263
+ },
264
+ },
265
+ });
266
+ // Per-account override: work account has selfChatMode: false despite root being true
267
+ const { result } = await runGroupGating({
268
+ cfg,
269
+ selfChatMode: false,
270
+ msg: createGroupMessage({
271
+ id: "m-account-override",
272
+ to: "+15550000",
273
+ accountId: "work",
274
+ body: "following up on bot reply",
275
+ timestamp: Date.now(),
276
+ senderE164: "+15551234567",
277
+ senderJid: "15551234567@s.whatsapp.net",
278
+ selfJid: "15551234567@s.whatsapp.net",
279
+ selfE164: "+15551234567",
280
+ replyToId: "m0",
281
+ replyToBody: "bot earlier response",
282
+ replyToSender: "+15551234567",
283
+ replyToSenderJid: "15551234567@s.whatsapp.net",
284
+ replyToSenderE164: "+15551234567",
285
+ }),
286
+ });
287
+
288
+ expect(result.shouldProcess).toBe(true);
289
+ });
290
+
291
+ it("uses account-scoped groupPolicy and groupAllowFrom for named-account group gating", async () => {
292
+ const cfg = makeConfig({
293
+ channels: {
294
+ whatsapp: {
295
+ groupPolicy: "allowlist",
296
+ accounts: {
297
+ work: {
298
+ groupPolicy: "allowlist",
299
+ groupAllowFrom: ["+111"],
300
+ },
301
+ },
302
+ },
303
+ },
304
+ });
305
+
306
+ const { result } = await runGroupGating({
307
+ cfg,
308
+ msg: createGroupMessage({
309
+ id: "g-account-policy",
310
+ accountId: "work",
311
+ body: "following up",
312
+ senderE164: "+111",
313
+ senderJid: "111@s.whatsapp.net",
314
+ selfJid: "15551234567@s.whatsapp.net",
315
+ selfE164: "+15551234567",
316
+ replyToId: "m0",
317
+ replyToBody: "bot said hi",
318
+ replyToSender: "+15551234567",
319
+ replyToSenderJid: "15551234567@s.whatsapp.net",
320
+ replyToSenderE164: "+15551234567",
321
+ }),
322
+ });
323
+
324
+ expect(result.shouldProcess).toBe(true);
325
+ });
326
+
327
+ it("inherits group gating defaults from accounts.default for named accounts", async () => {
328
+ const cfg = makeConfig({
329
+ channels: {
330
+ whatsapp: {
331
+ groupPolicy: "allowlist",
332
+ accounts: {
333
+ default: {
334
+ groupPolicy: "open",
335
+ groups: {
336
+ "*": {
337
+ requireMention: false,
338
+ },
339
+ },
340
+ },
341
+ work: {},
342
+ },
343
+ },
344
+ },
345
+ });
346
+
347
+ const { result } = await runGroupGating({
348
+ cfg,
349
+ msg: createGroupMessage({
350
+ id: "g-default-inheritance",
351
+ accountId: "work",
352
+ body: "plain group message",
353
+ senderE164: "+111",
354
+ senderJid: "111@s.whatsapp.net",
355
+ selfJid: "15551234567@s.whatsapp.net",
356
+ selfE164: "+15551234567",
357
+ }),
358
+ });
359
+
360
+ expect(result.shouldProcess).toBe(true);
361
+ });
362
+
363
+ it("preserves allowFrom fallback for named-account group gating when groupAllowFrom is empty", async () => {
364
+ const cfg = makeConfig({
365
+ channels: {
366
+ whatsapp: {
367
+ groupPolicy: "allowlist",
368
+ accounts: {
369
+ work: {
370
+ groupPolicy: "allowlist",
371
+ allowFrom: ["+111"],
372
+ groupAllowFrom: [],
373
+ groups: {
374
+ "*": {
375
+ requireMention: false,
376
+ },
377
+ },
378
+ },
379
+ },
380
+ },
381
+ },
382
+ });
383
+
384
+ const { result } = await runGroupGating({
385
+ cfg,
386
+ msg: createGroupMessage({
387
+ id: "g-empty-group-allow-fallback",
388
+ accountId: "work",
389
+ body: "plain group message",
390
+ senderE164: "+111",
391
+ senderJid: "111@s.whatsapp.net",
392
+ selfJid: "15551234567@s.whatsapp.net",
393
+ selfE164: "+15551234567",
394
+ }),
395
+ });
396
+
397
+ expect(result.shouldProcess).toBe(true);
398
+ });
399
+
400
+ it("uses account-scoped allowFrom when bypassing mention gating for owner commands", async () => {
401
+ const cfg = makeConfig({
402
+ channels: {
403
+ whatsapp: {
404
+ allowFrom: ["+999"],
405
+ accounts: {
406
+ work: {
407
+ allowFrom: ["+111"],
408
+ },
409
+ },
410
+ },
411
+ },
412
+ });
413
+
414
+ const { result } = await runGroupGating({
415
+ cfg,
416
+ msg: createGroupMessage({
417
+ id: "g-account-owner",
418
+ accountId: "work",
419
+ body: "/new",
420
+ senderE164: "+111",
421
+ senderName: "Owner",
422
+ }),
423
+ });
424
+
425
+ expect(result.shouldProcess).toBe(true);
426
+ });
427
+
428
+ it("does not treat group mention gating as self-chat under implicit self fallback", async () => {
429
+ const cfg = makeConfig({
430
+ channels: {
431
+ whatsapp: {
432
+ groups: { "*": { requireMention: true } },
433
+ },
434
+ },
435
+ messages: { groupChat: { mentionPatterns: ["@openclaw"] } },
436
+ });
437
+
438
+ const { result, groupHistories } = await runGroupGating({
439
+ cfg,
440
+ msg: createGroupMessage({
441
+ id: "g-other-mention",
442
+ body: "@openclaw please check this",
443
+ mentionedJids: ["15550000000@s.whatsapp.net"],
444
+ selfE164: "+15551234567",
445
+ selfJid: "15551234567@s.whatsapp.net",
446
+ }),
447
+ });
448
+
449
+ expect(result.shouldProcess).toBe(false);
450
+ expect(groupHistories.get("whatsapp:default:group:123@g.us")?.length).toBe(1);
451
+ });
452
+
453
+ it.each([
454
+ { id: "g-new", command: "/new" },
455
+ { id: "g-status", command: "/status" },
456
+ ])("bypasses mention gating for owner $command in group chats", async ({ id, command }) => {
457
+ const { result } = await runGroupGating({
458
+ cfg: makeOwnerGroupConfig(),
459
+ msg: createGroupMessage({
460
+ id,
461
+ body: command,
462
+ senderE164: "+111",
463
+ senderName: "Owner",
464
+ }),
465
+ });
466
+
467
+ expect(result.shouldProcess).toBe(true);
468
+ });
469
+
470
+ it("does not bypass mention gating for non-owner /new in group chats", async () => {
471
+ const cfg = makeConfig({
472
+ channels: {
473
+ whatsapp: {
474
+ allowFrom: ["+999"],
475
+ groups: { "*": { requireMention: true } },
476
+ },
477
+ },
478
+ });
479
+
480
+ const { result, groupHistories } = await runGroupGating({
481
+ cfg,
482
+ msg: createGroupMessage({
483
+ id: "g-new-unauth",
484
+ body: "/new",
485
+ senderE164: "+111",
486
+ senderName: "NotOwner",
487
+ }),
488
+ });
489
+
490
+ expect(result.shouldProcess).toBe(false);
491
+ expect(groupHistories.get("whatsapp:default:group:123@g.us")?.length).toBe(1);
492
+ });
493
+
494
+ it("uses per-agent mention patterns for group gating (routing + mentionPatterns)", async () => {
495
+ const cfg = makeConfig({
496
+ channels: {
497
+ whatsapp: {
498
+ allowFrom: ["*"],
499
+ groups: { "*": { requireMention: true } },
500
+ },
501
+ },
502
+ messages: {
503
+ groupChat: { mentionPatterns: ["@global"] },
504
+ },
505
+ agents: {
506
+ list: [
507
+ {
508
+ id: "work",
509
+ groupChat: { mentionPatterns: ["@workbot"] },
510
+ },
511
+ ],
512
+ },
513
+ bindings: [
514
+ {
515
+ agentId: "work",
516
+ match: {
517
+ provider: "whatsapp",
518
+ peer: { kind: "group", id: "123@g.us" },
519
+ },
520
+ },
521
+ ],
522
+ });
523
+
524
+ const route = resolveAgentRoute({
525
+ cfg,
526
+ channel: "whatsapp",
527
+ peer: { kind: "group", id: "123@g.us" },
528
+ });
529
+ expect(route.agentId).toBe("work");
530
+
531
+ const { result: globalMention } = await runGroupGating({
532
+ cfg,
533
+ agentId: route.agentId,
534
+ msg: createGroupMessage({
535
+ id: "g1",
536
+ body: "@global ping",
537
+ senderE164: "+111",
538
+ senderName: "Alice",
539
+ }),
540
+ });
541
+ expect(globalMention.shouldProcess).toBe(false);
542
+
543
+ const { result: workMention } = await runGroupGating({
544
+ cfg,
545
+ agentId: route.agentId,
546
+ msg: createGroupMessage({
547
+ id: "g2",
548
+ body: "@workbot ping",
549
+ senderE164: "+222",
550
+ senderName: "Bob",
551
+ }),
552
+ });
553
+ expect(workMention.shouldProcess).toBe(true);
554
+ });
555
+
556
+ it("allows group messages when whatsapp groups default disables mention gating", async () => {
557
+ const cfg = makeConfig({
558
+ channels: {
559
+ whatsapp: {
560
+ allowFrom: ["*"],
561
+ groups: { "*": { requireMention: false } },
562
+ },
563
+ },
564
+ messages: { groupChat: { mentionPatterns: ["@openclaw"] } },
565
+ });
566
+
567
+ const { result } = await runGroupGating({
568
+ cfg,
569
+ msg: createGroupMessage(),
570
+ });
571
+
572
+ expect(result.shouldProcess).toBe(true);
573
+ });
574
+
575
+ it("blocks group messages when whatsapp groups is set without a wildcard", async () => {
576
+ const cfg = makeConfig({
577
+ channels: {
578
+ whatsapp: {
579
+ allowFrom: ["*"],
580
+ groups: {
581
+ "999@g.us": { requireMention: false },
582
+ },
583
+ },
584
+ },
585
+ });
586
+
587
+ const { result } = await runGroupGating({
588
+ cfg,
589
+ msg: createGroupMessage({
590
+ body: "@workbot ping",
591
+ mentionedJids: ["999@s.whatsapp.net"],
592
+ selfJid: "999@s.whatsapp.net",
593
+ }),
594
+ });
595
+
596
+ expect(result.shouldProcess).toBe(false);
597
+ });
598
+ });
599
+
600
+ describe("buildInboundLine", () => {
601
+ it("prefixes group messages with sender", () => {
602
+ const line = buildInboundLine({
603
+ cfg: makeInboundCfg(""),
604
+ agentId: "main",
605
+ msg: createGroupMessage({
606
+ to: "+15550009999",
607
+ accountId: "default",
608
+ body: "ping",
609
+ timestamp: 1700000000000,
610
+ senderJid: "111@s.whatsapp.net",
611
+ senderE164: "+15550001111",
612
+ senderName: "Bob",
613
+ }) as never,
614
+ });
615
+
616
+ expect(line).toContain("Bob (+15550001111):");
617
+ expect(line).toContain("ping");
618
+ });
619
+
620
+ it("includes reply-to context blocks when replyToBody is present", () => {
621
+ const line = buildInboundLine({
622
+ cfg: makeInboundCfg(""),
623
+ agentId: "main",
624
+ msg: {
625
+ from: "+1555",
626
+ to: "+1555",
627
+ body: "hello",
628
+ chatType: "direct",
629
+ replyToId: "q1",
630
+ replyToBody: "original",
631
+ replyToSender: "+1999",
632
+ } as never,
633
+ envelope: { includeTimestamp: false },
634
+ });
635
+
636
+ expect(line).toContain("[Replying to +1999 id:q1]");
637
+ expect(line).toContain("original");
638
+ expect(line).toContain("[/Replying]");
639
+ });
640
+
641
+ it("applies the WhatsApp messagePrefix when configured", () => {
642
+ const line = buildInboundLine({
643
+ cfg: makeInboundCfg("[PFX]"),
644
+ agentId: "main",
645
+ msg: {
646
+ from: "+1555",
647
+ to: "+2666",
648
+ body: "ping",
649
+ chatType: "direct",
650
+ } as never,
651
+ envelope: { includeTimestamp: false },
652
+ });
653
+
654
+ expect(line).toContain("[PFX] ping");
655
+ });
656
+
657
+ it("normalizes direct from labels by stripping whatsapp: prefix", () => {
658
+ const line = buildInboundLine({
659
+ cfg: makeInboundCfg(""),
660
+ agentId: "main",
661
+ msg: {
662
+ from: "whatsapp:+15550001111",
663
+ to: "+2666",
664
+ body: "ping",
665
+ chatType: "direct",
666
+ } as never,
667
+ envelope: { includeTimestamp: false },
668
+ });
669
+
670
+ expect(line).toContain("+15550001111");
671
+ expect(line).not.toContain("whatsapp:+15550001111");
672
+ });
673
+ });
674
+
675
+ describe("formatReplyContext", () => {
676
+ it("returns null when replyToBody is missing", () => {
677
+ expect(formatReplyContext({} as never)).toBeNull();
678
+ });
679
+
680
+ it("uses unknown sender label when reply sender is absent", () => {
681
+ expect(
682
+ formatReplyContext({
683
+ replyToBody: "original",
684
+ } as never),
685
+ ).toBe("[Replying to unknown sender]\noriginal\n[/Replying]");
686
+ });
687
+ });