@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,102 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { resolveWebCredsPath } from "./creds-files.js";
5
+
6
+ const CREDS_FILE_MODE = 0o600;
7
+ const CREDS_SAVE_FLUSH_TIMEOUT_MS = 15_000;
8
+
9
+ const credsSaveQueues = new Map<string, Promise<void>>();
10
+
11
+ export type CredsQueueWaitResult = "drained" | "timed_out";
12
+
13
+ async function stringifyCreds(creds: unknown): Promise<string> {
14
+ const { BufferJSON } = await import("./session.runtime.js");
15
+ return JSON.stringify(creds, BufferJSON.replacer);
16
+ }
17
+
18
+ async function syncDirectory(dirPath: string): Promise<void> {
19
+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;
20
+ try {
21
+ handle = await fs.open(dirPath, "r");
22
+ await handle.sync();
23
+ } catch {
24
+ // best-effort on platforms that do not support directory fsync
25
+ } finally {
26
+ await handle?.close().catch(() => {
27
+ // best-effort close
28
+ });
29
+ }
30
+ }
31
+
32
+ export async function writeCredsJsonAtomically(authDir: string, creds: unknown): Promise<void> {
33
+ const credsPath = resolveWebCredsPath(authDir);
34
+ const tempPath = path.join(authDir, `.creds.${process.pid}.${randomUUID()}.tmp`);
35
+ const json = await stringifyCreds(creds);
36
+
37
+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;
38
+ try {
39
+ handle = await fs.open(tempPath, "w", CREDS_FILE_MODE);
40
+ await handle.writeFile(json, { encoding: "utf-8" });
41
+ await handle.sync();
42
+ await handle.close();
43
+ handle = undefined;
44
+
45
+ await fs.rename(tempPath, credsPath);
46
+ await fs.chmod(credsPath, CREDS_FILE_MODE).catch(() => {
47
+ // best-effort on platforms that support it
48
+ });
49
+ await syncDirectory(path.dirname(credsPath));
50
+ } catch (error) {
51
+ await handle?.close().catch(() => {
52
+ // best-effort close
53
+ });
54
+ await fs.rm(tempPath, { force: true }).catch(() => {
55
+ // best-effort cleanup
56
+ });
57
+ throw error;
58
+ }
59
+ }
60
+
61
+ export function enqueueCredsSave(
62
+ authDir: string,
63
+ saveCreds: () => Promise<void> | void,
64
+ onError: (error: unknown) => void,
65
+ ): void {
66
+ const previous = credsSaveQueues.get(authDir) ?? Promise.resolve();
67
+ const next = previous
68
+ .then(() => saveCreds())
69
+ .catch((error) => {
70
+ onError(error);
71
+ })
72
+ .finally(() => {
73
+ if (credsSaveQueues.get(authDir) === next) {
74
+ credsSaveQueues.delete(authDir);
75
+ }
76
+ });
77
+ credsSaveQueues.set(authDir, next);
78
+ }
79
+
80
+ export function waitForCredsSaveQueue(authDir?: string): Promise<void> {
81
+ if (authDir) {
82
+ return credsSaveQueues.get(authDir) ?? Promise.resolve();
83
+ }
84
+ return Promise.all(credsSaveQueues.values()).then(() => {});
85
+ }
86
+
87
+ export async function waitForCredsSaveQueueWithTimeout(
88
+ authDir: string,
89
+ timeoutMs = CREDS_SAVE_FLUSH_TIMEOUT_MS,
90
+ ): Promise<CredsQueueWaitResult> {
91
+ let flushTimeout: ReturnType<typeof setTimeout> | undefined;
92
+ return await Promise.race([
93
+ waitForCredsSaveQueue(authDir).then(() => "drained" as const),
94
+ new Promise<CredsQueueWaitResult>((resolve) => {
95
+ flushTimeout = setTimeout(() => resolve("timed_out"), timeoutMs);
96
+ }),
97
+ ]).finally(() => {
98
+ if (flushTimeout) {
99
+ clearTimeout(flushTimeout);
100
+ }
101
+ });
102
+ }
@@ -0,0 +1,60 @@
1
+ import { createDirectoryTestRuntime } from "openclaw/plugin-sdk/channel-test-helpers";
2
+ import { describe, expect, it } from "vitest";
3
+ import {
4
+ listWhatsAppDirectoryGroupsFromConfig,
5
+ listWhatsAppDirectoryPeersFromConfig,
6
+ } from "./directory-config.js";
7
+ import type { OpenClawConfig } from "./runtime-api.js";
8
+
9
+ describe("whatsapp directory", () => {
10
+ const runtimeEnv = createDirectoryTestRuntime() as never;
11
+
12
+ it("lists peers and groups from config", async () => {
13
+ const cfg = {
14
+ channels: {
15
+ whatsapp: {
16
+ authDir: "/tmp/wa-auth",
17
+ allowFrom: [
18
+ "whatsapp:+15551230001",
19
+ "15551230002@s.whatsapp.net",
20
+ "120363999999999999@g.us",
21
+ ],
22
+ groups: {
23
+ "120363111111111111@g.us": {},
24
+ "120363222222222222@g.us": {},
25
+ },
26
+ },
27
+ },
28
+ } as unknown as OpenClawConfig;
29
+
30
+ await expect(
31
+ listWhatsAppDirectoryPeersFromConfig({
32
+ cfg,
33
+ accountId: undefined,
34
+ query: undefined,
35
+ limit: undefined,
36
+ runtime: runtimeEnv,
37
+ } as never),
38
+ ).resolves.toEqual(
39
+ expect.arrayContaining([
40
+ { kind: "user", id: "+15551230001" },
41
+ { kind: "user", id: "+15551230002" },
42
+ ]),
43
+ );
44
+
45
+ await expect(
46
+ listWhatsAppDirectoryGroupsFromConfig({
47
+ cfg,
48
+ accountId: undefined,
49
+ query: undefined,
50
+ limit: undefined,
51
+ runtime: runtimeEnv,
52
+ } as never),
53
+ ).resolves.toEqual(
54
+ expect.arrayContaining([
55
+ { kind: "group", id: "120363111111111111@g.us" },
56
+ { kind: "group", id: "120363222222222222@g.us" },
57
+ ]),
58
+ );
59
+ });
60
+ });
@@ -0,0 +1,40 @@
1
+ import {
2
+ listResolvedDirectoryGroupEntriesFromMapKeys,
3
+ listResolvedDirectoryUserEntriesFromAllowFrom,
4
+ type DirectoryConfigParams,
5
+ } from "openclaw/plugin-sdk/directory-config-runtime";
6
+ import { resolveMergedWhatsAppAccountConfig } from "./account-config.js";
7
+ import type { WhatsAppAccountConfig } from "./account-types.js";
8
+ import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "./normalize.js";
9
+
10
+ type WhatsAppDirectoryAccount = WhatsAppAccountConfig & { accountId: string };
11
+
12
+ function resolveWhatsAppDirectoryAccount(
13
+ cfg: DirectoryConfigParams["cfg"],
14
+ accountId?: string | null,
15
+ ): WhatsAppDirectoryAccount {
16
+ return resolveMergedWhatsAppAccountConfig({ cfg, accountId });
17
+ }
18
+
19
+ export async function listWhatsAppDirectoryPeersFromConfig(params: DirectoryConfigParams) {
20
+ return listResolvedDirectoryUserEntriesFromAllowFrom<WhatsAppDirectoryAccount>({
21
+ ...params,
22
+ resolveAccount: resolveWhatsAppDirectoryAccount,
23
+ resolveAllowFrom: (account) => account.allowFrom,
24
+ normalizeId: (entry) => {
25
+ const normalized = normalizeWhatsAppTarget(entry);
26
+ if (!normalized || isWhatsAppGroupJid(normalized)) {
27
+ return null;
28
+ }
29
+ return normalized;
30
+ },
31
+ });
32
+ }
33
+
34
+ export async function listWhatsAppDirectoryGroupsFromConfig(params: DirectoryConfigParams) {
35
+ return listResolvedDirectoryGroupEntriesFromMapKeys<WhatsAppDirectoryAccount>({
36
+ ...params,
37
+ resolveAccount: resolveWhatsAppDirectoryAccount,
38
+ resolveGroups: (account) => account.groups,
39
+ });
40
+ }
@@ -0,0 +1,56 @@
1
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
2
+ import { describe, expect, it } from "vitest";
3
+ import {
4
+ listWhatsAppDirectoryGroupsFromConfig,
5
+ listWhatsAppDirectoryPeersFromConfig,
6
+ } from "../directory-contract-api.js";
7
+
8
+ function ids(entries: Array<{ id: string }>) {
9
+ return entries.map((entry) => entry.id);
10
+ }
11
+
12
+ describe("WhatsApp directory contract", () => {
13
+ it("lists peers/groups from config", async () => {
14
+ const cfg = {
15
+ channels: {
16
+ whatsapp: {
17
+ allowFrom: ["+15550000000", "*", "123@g.us"],
18
+ groups: { "999@g.us": { requireMention: true }, "*": {} },
19
+ },
20
+ },
21
+ } as unknown as OpenClawConfig;
22
+
23
+ const peers = await listWhatsAppDirectoryPeersFromConfig({
24
+ cfg,
25
+ accountId: "default",
26
+ query: null,
27
+ limit: null,
28
+ });
29
+ const groups = await listWhatsAppDirectoryGroupsFromConfig({
30
+ cfg,
31
+ accountId: "default",
32
+ query: null,
33
+ limit: null,
34
+ });
35
+ expect(ids(peers)).toEqual(["+15550000000"]);
36
+ expect(ids(groups)).toEqual(["999@g.us"]);
37
+ });
38
+
39
+ it("applies query and limit filtering for config-backed directories", async () => {
40
+ const cfg = {
41
+ channels: {
42
+ whatsapp: {
43
+ groups: { "111@g.us": {}, "222@g.us": {}, "333@s.whatsapp.net": {} },
44
+ },
45
+ },
46
+ } as unknown as OpenClawConfig;
47
+
48
+ const groups = await listWhatsAppDirectoryGroupsFromConfig({
49
+ cfg,
50
+ accountId: "default",
51
+ query: "@g.us",
52
+ limit: 1,
53
+ });
54
+ expect(ids(groups)).toEqual(["111@g.us"]);
55
+ });
56
+ });
@@ -0,0 +1,11 @@
1
+ import type { ChannelDoctorConfigMutation } from "openclaw/plugin-sdk/channel-contract";
2
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
3
+ import { normalizeCompatibilityConfig as normalizeCompatibilityConfigImpl } from "./doctor.js";
4
+
5
+ export function normalizeCompatibilityConfig({
6
+ cfg,
7
+ }: {
8
+ cfg: OpenClawConfig;
9
+ }): ChannelDoctorConfigMutation {
10
+ return normalizeCompatibilityConfigImpl({ cfg });
11
+ }
@@ -0,0 +1,74 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { normalizeCompatibilityConfig } from "./doctor.js";
3
+
4
+ describe("whatsapp doctor compatibility", () => {
5
+ it("does not add whatsapp config when the channel is not configured", () => {
6
+ const result = normalizeCompatibilityConfig({
7
+ cfg: {
8
+ messages: {
9
+ ackReaction: "👀",
10
+ ackReactionScope: "group-mentions",
11
+ },
12
+ },
13
+ });
14
+
15
+ expect(result.config.channels?.whatsapp).toBeUndefined();
16
+ expect(result.changes).toEqual([]);
17
+ });
18
+
19
+ it("copies legacy ack reaction into configured whatsapp channel", () => {
20
+ const result = normalizeCompatibilityConfig({
21
+ cfg: {
22
+ messages: {
23
+ ackReaction: "👀",
24
+ ackReactionScope: "group-mentions",
25
+ },
26
+ channels: {
27
+ whatsapp: {
28
+ accounts: {
29
+ work: {
30
+ authDir: "/tmp/openclaw-wa-auth",
31
+ },
32
+ },
33
+ },
34
+ },
35
+ },
36
+ });
37
+
38
+ expect(result.config.channels?.whatsapp?.ackReaction).toEqual({
39
+ emoji: "👀",
40
+ direct: false,
41
+ group: "mentions",
42
+ });
43
+ expect(result.changes).toEqual([
44
+ "Copied messages.ackReaction → channels.whatsapp.ackReaction (scope: group-mentions).",
45
+ ]);
46
+ });
47
+
48
+ it("keeps existing whatsapp ack reaction", () => {
49
+ const result = normalizeCompatibilityConfig({
50
+ cfg: {
51
+ messages: {
52
+ ackReaction: "👀",
53
+ ackReactionScope: "all",
54
+ },
55
+ channels: {
56
+ whatsapp: {
57
+ ackReaction: {
58
+ emoji: "✅",
59
+ direct: true,
60
+ group: "always",
61
+ },
62
+ },
63
+ },
64
+ },
65
+ });
66
+
67
+ expect(result.config.channels?.whatsapp?.ackReaction).toEqual({
68
+ emoji: "✅",
69
+ direct: true,
70
+ group: "always",
71
+ });
72
+ expect(result.changes).toEqual([]);
73
+ });
74
+ });
package/src/doctor.ts ADDED
@@ -0,0 +1,56 @@
1
+ import type {
2
+ ChannelDoctorAdapter,
3
+ ChannelDoctorConfigMutation,
4
+ } from "openclaw/plugin-sdk/channel-contract";
5
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
6
+
7
+ export function normalizeCompatibilityConfig({
8
+ cfg,
9
+ }: {
10
+ cfg: OpenClawConfig;
11
+ }): ChannelDoctorConfigMutation {
12
+ const legacyAckReaction = cfg.messages?.ackReaction?.trim();
13
+ if (!legacyAckReaction || cfg.channels?.whatsapp === undefined) {
14
+ return { config: cfg, changes: [] };
15
+ }
16
+ if (cfg.channels.whatsapp?.ackReaction !== undefined) {
17
+ return { config: cfg, changes: [] };
18
+ }
19
+
20
+ const legacyScope = cfg.messages?.ackReactionScope ?? "group-mentions";
21
+ let direct = true;
22
+ let group: "always" | "mentions" | "never" = "mentions";
23
+ if (legacyScope === "all") {
24
+ direct = true;
25
+ group = "always";
26
+ } else if (legacyScope === "direct") {
27
+ direct = true;
28
+ group = "never";
29
+ } else if (legacyScope === "group-all") {
30
+ direct = false;
31
+ group = "always";
32
+ } else if (legacyScope === "group-mentions") {
33
+ direct = false;
34
+ group = "mentions";
35
+ }
36
+
37
+ return {
38
+ config: {
39
+ ...cfg,
40
+ channels: {
41
+ ...cfg.channels,
42
+ whatsapp: {
43
+ ...cfg.channels?.whatsapp,
44
+ ackReaction: { emoji: legacyAckReaction, direct, group },
45
+ },
46
+ },
47
+ },
48
+ changes: [
49
+ `Copied messages.ackReaction → channels.whatsapp.ackReaction (scope: ${legacyScope}).`,
50
+ ],
51
+ };
52
+ }
53
+
54
+ export const whatsappDoctor: ChannelDoctorAdapter = {
55
+ normalizeCompatibilityConfig,
56
+ };
@@ -0,0 +1,15 @@
1
+ const WHATSAPP_GROUP_INTRO_HINT =
2
+ "WhatsApp IDs: SenderId is the participant JID (group participant id).";
3
+
4
+ export function resolveWhatsAppGroupIntroHint(): string {
5
+ return WHATSAPP_GROUP_INTRO_HINT;
6
+ }
7
+
8
+ export function resolveWhatsAppMentionStripRegexes(ctx: { To?: string | null }): RegExp[] {
9
+ const selfE164 = (ctx.To ?? "").replace(/^whatsapp:/i, "");
10
+ if (!selfE164) {
11
+ return [];
12
+ }
13
+ const escaped = selfE164.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
14
+ return [new RegExp(escaped, "g"), new RegExp(`@${escaped}`, "g")];
15
+ }
@@ -0,0 +1,36 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ resolveWhatsAppGroupRequireMention,
4
+ resolveWhatsAppGroupToolPolicy,
5
+ } from "./group-policy.js";
6
+ import type { OpenClawConfig } from "./runtime-api.js";
7
+
8
+ describe("whatsapp group policy", () => {
9
+ it("uses generic channel group policy helpers", () => {
10
+ const cfg = {
11
+ channels: {
12
+ whatsapp: {
13
+ groups: {
14
+ "1203630@g.us": {
15
+ requireMention: false,
16
+ tools: { deny: ["exec"] },
17
+ },
18
+ "*": {
19
+ requireMention: true,
20
+ tools: { allow: ["message.send"] },
21
+ },
22
+ },
23
+ },
24
+ },
25
+ } as OpenClawConfig;
26
+
27
+ expect(resolveWhatsAppGroupRequireMention({ cfg, groupId: "1203630@g.us" })).toBe(false);
28
+ expect(resolveWhatsAppGroupRequireMention({ cfg, groupId: "other@g.us" })).toBe(true);
29
+ expect(resolveWhatsAppGroupToolPolicy({ cfg, groupId: "1203630@g.us" })).toEqual({
30
+ deny: ["exec"],
31
+ });
32
+ expect(resolveWhatsAppGroupToolPolicy({ cfg, groupId: "other@g.us" })).toEqual({
33
+ allow: ["message.send"],
34
+ });
35
+ });
36
+ });
@@ -0,0 +1,40 @@
1
+ import {
2
+ resolveChannelGroupRequireMention,
3
+ resolveChannelGroupToolsPolicy,
4
+ type GroupToolPolicyConfig,
5
+ } from "openclaw/plugin-sdk/channel-policy";
6
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
7
+
8
+ type WhatsAppGroupContext = {
9
+ cfg: OpenClawConfig;
10
+ accountId?: string | null;
11
+ groupId?: string | null;
12
+ senderId?: string | null;
13
+ senderName?: string | null;
14
+ senderUsername?: string | null;
15
+ senderE164?: string | null;
16
+ };
17
+
18
+ export function resolveWhatsAppGroupRequireMention(params: WhatsAppGroupContext): boolean {
19
+ return resolveChannelGroupRequireMention({
20
+ cfg: params.cfg,
21
+ channel: "whatsapp",
22
+ groupId: params.groupId,
23
+ accountId: params.accountId,
24
+ });
25
+ }
26
+
27
+ export function resolveWhatsAppGroupToolPolicy(
28
+ params: WhatsAppGroupContext,
29
+ ): GroupToolPolicyConfig | undefined {
30
+ return resolveChannelGroupToolsPolicy({
31
+ cfg: params.cfg,
32
+ channel: "whatsapp",
33
+ groupId: params.groupId,
34
+ accountId: params.accountId,
35
+ senderId: params.senderId,
36
+ senderName: params.senderName,
37
+ senderUsername: params.senderUsername,
38
+ senderE164: params.senderE164,
39
+ });
40
+ }
@@ -0,0 +1,20 @@
1
+ import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
2
+
3
+ export function resolveLegacyGroupSessionKey(ctx: { From?: string }): {
4
+ key: string;
5
+ channel: string;
6
+ id: string;
7
+ chatType: "group";
8
+ } | null {
9
+ const from = typeof ctx.From === "string" ? ctx.From.trim() : "";
10
+ const normalized = normalizeLowercaseStringOrEmpty(from);
11
+ if (!from || from.includes(":") || !normalized.endsWith("@g.us")) {
12
+ return null;
13
+ }
14
+ return {
15
+ key: `whatsapp:group:${normalized}`,
16
+ channel: "whatsapp",
17
+ id: normalized,
18
+ chatType: "group",
19
+ };
20
+ }
@@ -0,0 +1,53 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { resolveWhatsAppGroupSessionRoute, __testing } from "./group-session-key.js";
3
+
4
+ describe("resolveWhatsAppGroupSessionRoute", () => {
5
+ it("keeps default-account group routes unchanged", () => {
6
+ const route = {
7
+ agentId: "main",
8
+ channel: "whatsapp",
9
+ accountId: "default",
10
+ sessionKey: "agent:main:whatsapp:group:123@g.us",
11
+ mainSessionKey: "agent:main:main",
12
+ lastRoutePolicy: "session",
13
+ matchedBy: "default",
14
+ } as const;
15
+
16
+ expect(resolveWhatsAppGroupSessionRoute(route)).toEqual(route);
17
+ });
18
+
19
+ it("scopes named-account group routes through an account-specific thread suffix", () => {
20
+ const route = {
21
+ agentId: "main",
22
+ channel: "whatsapp",
23
+ accountId: "work",
24
+ sessionKey: "agent:main:whatsapp:group:123@g.us",
25
+ mainSessionKey: "agent:main:main",
26
+ lastRoutePolicy: "session",
27
+ matchedBy: "default",
28
+ } as const;
29
+
30
+ expect(resolveWhatsAppGroupSessionRoute(route)).toEqual({
31
+ ...route,
32
+ sessionKey: "agent:main:whatsapp:group:123@g.us:thread:whatsapp-account-work",
33
+ });
34
+ });
35
+
36
+ it("derives the legacy group session key from a named-account scoped group route", () => {
37
+ expect(
38
+ __testing.resolveWhatsAppLegacyGroupSessionKey({
39
+ accountId: "work",
40
+ sessionKey: "agent:main:whatsapp:group:123@g.us:thread:whatsapp-account-work",
41
+ }),
42
+ ).toBe("agent:main:whatsapp:group:123@g.us");
43
+ });
44
+
45
+ it("normalizes mixed-case account ids when resolving legacy scoped group keys", () => {
46
+ expect(
47
+ __testing.resolveWhatsAppLegacyGroupSessionKey({
48
+ accountId: "Work",
49
+ sessionKey: "agent:main:whatsapp:group:123@g.us:thread:whatsapp-account-work",
50
+ }),
51
+ ).toBe("agent:main:whatsapp:group:123@g.us");
52
+ });
53
+ });
@@ -0,0 +1,41 @@
1
+ import {
2
+ DEFAULT_ACCOUNT_ID,
3
+ normalizeAccountId,
4
+ resolveThreadSessionKeys,
5
+ type ResolvedAgentRoute,
6
+ } from "openclaw/plugin-sdk/routing";
7
+
8
+ function resolveWhatsAppGroupAccountThreadId(accountId: string): string {
9
+ return `whatsapp-account-${normalizeAccountId(accountId)}`;
10
+ }
11
+
12
+ export function resolveWhatsAppLegacyGroupSessionKey(params: {
13
+ sessionKey: string;
14
+ accountId?: string | null;
15
+ }): string | null {
16
+ const accountId = normalizeAccountId(params.accountId);
17
+ if (!accountId || accountId === DEFAULT_ACCOUNT_ID || !params.sessionKey.includes(":group:")) {
18
+ return null;
19
+ }
20
+ const suffix = `:thread:${resolveWhatsAppGroupAccountThreadId(accountId)}`;
21
+ return params.sessionKey.endsWith(suffix) ? params.sessionKey.slice(0, -suffix.length) : null;
22
+ }
23
+
24
+ export function resolveWhatsAppGroupSessionRoute(route: ResolvedAgentRoute): ResolvedAgentRoute {
25
+ if (route.accountId === DEFAULT_ACCOUNT_ID || !route.sessionKey.includes(":group:")) {
26
+ return route;
27
+ }
28
+ const scopedSession = resolveThreadSessionKeys({
29
+ baseSessionKey: route.sessionKey,
30
+ threadId: resolveWhatsAppGroupAccountThreadId(route.accountId),
31
+ });
32
+ return {
33
+ ...route,
34
+ sessionKey: scopedSession.sessionKey,
35
+ };
36
+ }
37
+
38
+ export const __testing = {
39
+ resolveWhatsAppGroupAccountThreadId,
40
+ resolveWhatsAppLegacyGroupSessionKey,
41
+ };
@@ -0,0 +1,6 @@
1
+ export { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/account-id";
2
+ export { normalizeE164 } from "openclaw/plugin-sdk/account-resolution";
3
+ export { readChannelAllowFromStoreSync } from "openclaw/plugin-sdk/channel-pairing";
4
+ export { normalizeChannelId } from "openclaw/plugin-sdk/channel-targets";
5
+ export { loadSessionStore, resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
6
+ export type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";