@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 @@
1
+ export { whatsappOutbound } from "./src/outbound-adapter.js";
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@openclaw/whatsapp",
3
+ "version": "2026.5.1-beta.1",
4
+ "description": "OpenClaw WhatsApp channel plugin",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/openclaw/openclaw"
8
+ },
9
+ "type": "module",
10
+ "dependencies": {
11
+ "@whiskeysockets/baileys": "7.0.0-rc.9",
12
+ "https-proxy-agent": "^9.0.0",
13
+ "jimp": "^1.6.1",
14
+ "typebox": "1.1.37",
15
+ "undici": "8.1.0"
16
+ },
17
+ "devDependencies": {
18
+ "@openclaw/plugin-sdk": "workspace:*",
19
+ "openclaw": "workspace:*"
20
+ },
21
+ "peerDependencies": {
22
+ "openclaw": ">=2026.4.25"
23
+ },
24
+ "peerDependenciesMeta": {
25
+ "openclaw": {
26
+ "optional": true
27
+ }
28
+ },
29
+ "openclaw": {
30
+ "extensions": [
31
+ "./index.ts"
32
+ ],
33
+ "setupEntry": "./setup-entry.ts",
34
+ "setupFeatures": {
35
+ "legacyStateMigrations": true,
36
+ "legacySessionSurfaces": true
37
+ },
38
+ "channel": {
39
+ "id": "whatsapp",
40
+ "label": "WhatsApp",
41
+ "selectionLabel": "WhatsApp (QR link)",
42
+ "detailLabel": "WhatsApp Web",
43
+ "docsPath": "/channels/whatsapp",
44
+ "docsLabel": "whatsapp",
45
+ "blurb": "works with your own number; recommend a separate phone + eSIM.",
46
+ "systemImage": "message",
47
+ "persistedAuthState": {
48
+ "specifier": "./auth-presence",
49
+ "exportName": "hasAnyWhatsAppAuth"
50
+ },
51
+ "cliAddOptions": [
52
+ {
53
+ "flags": "--auth-dir <path>",
54
+ "description": "WhatsApp auth directory override"
55
+ }
56
+ ]
57
+ },
58
+ "install": {
59
+ "npmSpec": "@openclaw/whatsapp",
60
+ "defaultChoice": "npm",
61
+ "minHostVersion": ">=2026.4.25"
62
+ },
63
+ "compat": {
64
+ "pluginApi": ">=2026.4.25"
65
+ },
66
+ "build": {
67
+ "openclawVersion": "2026.5.1-beta.1"
68
+ },
69
+ "release": {
70
+ "publishToClawHub": true,
71
+ "publishToNpm": true
72
+ }
73
+ }
74
+ }
package/runtime-api.ts ADDED
@@ -0,0 +1,86 @@
1
+ export {
2
+ getActiveWebListener,
3
+ resolveWebAccountId,
4
+ type ActiveWebListener,
5
+ type ActiveWebSendOptions,
6
+ } from "./src/active-listener.js";
7
+ export { handleWhatsAppAction, whatsAppActionRuntime } from "./src/action-runtime.js";
8
+ export { createWhatsAppLoginTool } from "./src/agent-tools-login.js";
9
+ export {
10
+ formatWhatsAppWebAuthStatusState,
11
+ getWebAuthAgeMs,
12
+ hasWebCredsSync,
13
+ logWebSelfId,
14
+ logoutWeb,
15
+ pickWebChannel,
16
+ readCredsJsonRaw,
17
+ readWebAuthExistsBestEffort,
18
+ readWebAuthExistsForDecision,
19
+ readWebAuthSnapshot,
20
+ readWebAuthSnapshotBestEffort,
21
+ readWebAuthState,
22
+ readWebSelfId,
23
+ readWebSelfIdentity,
24
+ readWebSelfIdentityForDecision,
25
+ resolveDefaultWebAuthDir,
26
+ resolveWebCredsBackupPath,
27
+ resolveWebCredsPath,
28
+ restoreCredsFromBackupIfNeeded,
29
+ WA_WEB_AUTH_DIR,
30
+ webAuthExists,
31
+ WHATSAPP_AUTH_UNSTABLE_CODE,
32
+ WhatsAppAuthUnstableError,
33
+ type WhatsAppWebAuthState,
34
+ } from "./src/auth-store.js";
35
+ export {
36
+ DEFAULT_WEB_MEDIA_BYTES,
37
+ HEARTBEAT_PROMPT,
38
+ HEARTBEAT_TOKEN,
39
+ monitorWebChannel,
40
+ resolveHeartbeatRecipients,
41
+ runWebHeartbeatOnce,
42
+ SILENT_REPLY_TOKEN,
43
+ stripHeartbeatToken,
44
+ type WebChannelStatus,
45
+ type WebMonitorTuning,
46
+ } from "./src/auto-reply.js";
47
+ export {
48
+ extractContactContext,
49
+ extractLocationData,
50
+ extractMediaPlaceholder,
51
+ extractText,
52
+ monitorWebInbox,
53
+ resetWebInboundDedupe,
54
+ type WebInboundMessage,
55
+ type WebListenerCloseReason,
56
+ } from "./src/inbound.js";
57
+ export { loginWeb } from "./src/login.js";
58
+ export {
59
+ getDefaultLocalRoots,
60
+ loadWebMedia,
61
+ loadWebMediaRaw,
62
+ LocalMediaAccessError,
63
+ optimizeImageToJpeg,
64
+ optimizeImageToPng,
65
+ type LocalMediaAccessErrorCode,
66
+ type WebMediaResult,
67
+ } from "./src/media.js";
68
+ export {
69
+ sendMessageWhatsApp,
70
+ sendPollWhatsApp,
71
+ sendReactionWhatsApp,
72
+ sendTypingWhatsApp,
73
+ } from "./src/send.js";
74
+ export {
75
+ createWaSocket,
76
+ formatError,
77
+ getStatusCode,
78
+ newConnectionId,
79
+ waitForCredsSaveQueue,
80
+ waitForCredsSaveQueueWithTimeout,
81
+ waitForWaConnection,
82
+ writeCredsJsonAtomically,
83
+ type CredsQueueWaitResult,
84
+ } from "./src/session.js";
85
+ export { setWhatsAppRuntime } from "./src/runtime.js";
86
+ export { startWebLoginWithQr, waitForWebLogin } from "./login-qr-runtime.js";
@@ -0,0 +1,4 @@
1
+ // WhatsApp does not expose secret-contract surfaces.
2
+ export const secretTargetRegistryEntries: readonly [] = [];
3
+
4
+ export function collectRuntimeConfigAssignments(): void {}
@@ -0,0 +1,4 @@
1
+ export {
2
+ collectUnsupportedSecretRefConfigCandidates,
3
+ unsupportedSecretRefSurfacePatterns,
4
+ } from "./src/security-contract.js";
@@ -0,0 +1,21 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ vi.mock("@whiskeysockets/baileys", () => {
4
+ throw new Error("setup plugin load must not load Baileys");
5
+ });
6
+
7
+ describe("whatsapp setup entry", () => {
8
+ it("loads the setup plugin without installing or importing runtime dependencies", async () => {
9
+ const { default: setupEntry } = await import("./setup-entry.js");
10
+ const { whatsappSetupPlugin } = await import("./setup-plugin-api.js");
11
+
12
+ expect(setupEntry.kind).toBe("bundled-channel-setup-entry");
13
+ expect(whatsappSetupPlugin.id).toBe("whatsapp");
14
+ });
15
+
16
+ it("loads the delegated setup wizard without importing runtime dependencies", async () => {
17
+ const { whatsappSetupWizard } = await import("./src/setup-surface.js");
18
+
19
+ expect(whatsappSetupWizard.channel).toBe("whatsapp");
20
+ });
21
+ });
package/setup-entry.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { defineBundledChannelSetupEntry } from "openclaw/plugin-sdk/channel-entry-contract";
2
+
3
+ export default defineBundledChannelSetupEntry({
4
+ importMetaUrl: import.meta.url,
5
+ features: {
6
+ legacyStateMigrations: true,
7
+ legacySessionSurfaces: true,
8
+ },
9
+ plugin: {
10
+ specifier: "./setup-plugin-api.js",
11
+ exportName: "whatsappSetupPlugin",
12
+ },
13
+ legacyStateMigrations: {
14
+ specifier: "./legacy-state-migrations-api.js",
15
+ exportName: "detectWhatsAppLegacyStateMigrations",
16
+ },
17
+ legacySessionSurface: {
18
+ specifier: "./legacy-session-surface-api.js",
19
+ exportName: "whatsappLegacySessionSurface",
20
+ },
21
+ });
@@ -0,0 +1,3 @@
1
+ // Keep bundled setup entry imports narrow so setup loads do not pull the
2
+ // broader WhatsApp channel plugin surface.
3
+ export { whatsappSetupPlugin } from "./src/channel.setup.js";
@@ -0,0 +1,77 @@
1
+ import {
2
+ DEFAULT_ACCOUNT_ID,
3
+ mergeAccountConfig,
4
+ resolveAccountEntry,
5
+ resolveMergedAccountConfig,
6
+ type OpenClawConfig,
7
+ } from "openclaw/plugin-sdk/account-core";
8
+ import {
9
+ resolveChannelStreamingBlockEnabled,
10
+ resolveChannelStreamingChunkMode,
11
+ } from "openclaw/plugin-sdk/channel-streaming";
12
+ import type { WhatsAppAccountConfig } from "./account-types.js";
13
+
14
+ function resolveWhatsAppDefaultAccountSharedConfig(
15
+ cfg: OpenClawConfig,
16
+ ): Partial<WhatsAppAccountConfig> | undefined {
17
+ const defaultAccount = resolveAccountEntry(cfg.channels?.whatsapp?.accounts, DEFAULT_ACCOUNT_ID);
18
+ if (!defaultAccount) {
19
+ return undefined;
20
+ }
21
+ const {
22
+ enabled: _ignoredEnabled,
23
+ name: _ignoredName,
24
+ authDir: _ignoredAuthDir,
25
+ selfChatMode: _ignoredSelfChatMode,
26
+ ...sharedDefaults
27
+ } = defaultAccount;
28
+ return sharedDefaults;
29
+ }
30
+
31
+ function _resolveWhatsAppAccountConfig(
32
+ cfg: OpenClawConfig,
33
+ accountId: string,
34
+ ): WhatsAppAccountConfig | undefined {
35
+ return resolveAccountEntry(cfg.channels?.whatsapp?.accounts, accountId);
36
+ }
37
+
38
+ function resolveMergedNamedWhatsAppAccountConfig(params: {
39
+ cfg: OpenClawConfig;
40
+ accountId: string;
41
+ }): WhatsAppAccountConfig {
42
+ const rootCfg = params.cfg.channels?.whatsapp;
43
+ const accountConfig = _resolveWhatsAppAccountConfig(params.cfg, params.accountId);
44
+ return {
45
+ ...mergeAccountConfig<WhatsAppAccountConfig>({
46
+ channelConfig: rootCfg as WhatsAppAccountConfig | undefined,
47
+ accountConfig: undefined,
48
+ omitKeys: ["defaultAccount"],
49
+ }),
50
+ ...resolveWhatsAppDefaultAccountSharedConfig(params.cfg),
51
+ ...accountConfig,
52
+ };
53
+ }
54
+
55
+ export function resolveMergedWhatsAppAccountConfig(params: {
56
+ cfg: OpenClawConfig;
57
+ accountId?: string | null;
58
+ }): WhatsAppAccountConfig & { accountId: string } {
59
+ const rootCfg = params.cfg.channels?.whatsapp;
60
+ const accountId = params.accountId?.trim() || rootCfg?.defaultAccount || DEFAULT_ACCOUNT_ID;
61
+ const base = resolveMergedAccountConfig<WhatsAppAccountConfig>({
62
+ channelConfig: rootCfg as WhatsAppAccountConfig | undefined,
63
+ accounts: rootCfg?.accounts as Record<string, Partial<WhatsAppAccountConfig>> | undefined,
64
+ accountId,
65
+ omitKeys: ["defaultAccount"],
66
+ });
67
+ const merged =
68
+ accountId === DEFAULT_ACCOUNT_ID
69
+ ? base
70
+ : resolveMergedNamedWhatsAppAccountConfig({ cfg: params.cfg, accountId });
71
+ return {
72
+ accountId,
73
+ ...merged,
74
+ chunkMode: resolveChannelStreamingChunkMode(merged) ?? merged.chunkMode,
75
+ blockStreaming: resolveChannelStreamingBlockEnabled(merged) ?? merged.blockStreaming,
76
+ };
77
+ }
@@ -0,0 +1,13 @@
1
+ import { createAccountListHelpers } from "openclaw/plugin-sdk/account-core";
2
+
3
+ const {
4
+ listConfiguredAccountIds,
5
+ listAccountIds,
6
+ resolveDefaultAccountId: resolveDefaultWhatsAppAccountId,
7
+ } = createAccountListHelpers("whatsapp");
8
+
9
+ export {
10
+ listConfiguredAccountIds,
11
+ listAccountIds as listWhatsAppAccountIds,
12
+ resolveDefaultWhatsAppAccountId,
13
+ };
@@ -0,0 +1,5 @@
1
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
2
+
3
+ export type WhatsAppAccountConfig = NonNullable<
4
+ NonNullable<NonNullable<OpenClawConfig["channels"]>["whatsapp"]>["accounts"]
5
+ >[string];
@@ -0,0 +1,182 @@
1
+ import path from "node:path";
2
+ import { describe, expect, it } from "vitest";
3
+ import { resolveWhatsAppAccount, resolveWhatsAppAuthDir } from "./accounts.js";
4
+
5
+ describe("resolveWhatsAppAuthDir", () => {
6
+ const stubCfg = { channels: { whatsapp: { accounts: {} } } } as Parameters<
7
+ typeof resolveWhatsAppAuthDir
8
+ >[0]["cfg"];
9
+
10
+ it("sanitizes path traversal sequences in accountId", () => {
11
+ const { authDir } = resolveWhatsAppAuthDir({
12
+ cfg: stubCfg,
13
+ accountId: "../../../etc/passwd",
14
+ });
15
+ // Sanitized accountId must not escape the whatsapp auth directory.
16
+ expect(authDir).not.toContain("..");
17
+ expect(path.basename(authDir)).not.toContain("/");
18
+ });
19
+
20
+ it("sanitizes special characters in accountId", () => {
21
+ const { authDir } = resolveWhatsAppAuthDir({
22
+ cfg: stubCfg,
23
+ accountId: "foo/bar\\baz",
24
+ });
25
+ // Sprawdzaj sanityzacje na segmencie accountId, nie na calej sciezce
26
+ // (Windows uzywa backslash jako separator katalogow).
27
+ const segment = path.basename(authDir);
28
+ expect(segment).not.toContain("/");
29
+ expect(segment).not.toContain("\\");
30
+ });
31
+
32
+ it("returns default directory for empty accountId", () => {
33
+ const { authDir } = resolveWhatsAppAuthDir({
34
+ cfg: stubCfg,
35
+ accountId: "",
36
+ });
37
+ expect(authDir).toMatch(/whatsapp[/\\]default$/);
38
+ });
39
+
40
+ it("preserves valid accountId unchanged", () => {
41
+ const { authDir } = resolveWhatsAppAuthDir({
42
+ cfg: stubCfg,
43
+ accountId: "my-account-1",
44
+ });
45
+ expect(authDir).toMatch(/whatsapp[/\\]my-account-1$/);
46
+ });
47
+
48
+ it("merges top-level and account-specific config through shared helpers", () => {
49
+ const resolved = resolveWhatsAppAccount({
50
+ cfg: {
51
+ messages: {
52
+ messagePrefix: "[global]",
53
+ },
54
+ channels: {
55
+ whatsapp: {
56
+ sendReadReceipts: false,
57
+ messagePrefix: "[root]",
58
+ debounceMs: 100,
59
+ accounts: {
60
+ work: {
61
+ debounceMs: 250,
62
+ },
63
+ },
64
+ },
65
+ },
66
+ } as Parameters<typeof resolveWhatsAppAccount>[0]["cfg"],
67
+ accountId: "work",
68
+ });
69
+
70
+ expect(resolved.sendReadReceipts).toBe(false);
71
+ expect(resolved.messagePrefix).toBe("[root]");
72
+ expect(resolved.debounceMs).toBe(250);
73
+ });
74
+
75
+ it("inherits shared defaults from accounts.default for named accounts", () => {
76
+ const resolved = resolveWhatsAppAccount({
77
+ cfg: {
78
+ channels: {
79
+ whatsapp: {
80
+ accounts: {
81
+ default: {
82
+ dmPolicy: "allowlist",
83
+ allowFrom: ["+15550001111"],
84
+ groupPolicy: "open",
85
+ groupAllowFrom: ["+15550002222"],
86
+ defaultTo: "+15550003333",
87
+ reactionLevel: "extensive",
88
+ historyLimit: 42,
89
+ mediaMaxMb: 12,
90
+ },
91
+ work: {
92
+ authDir: "/tmp/work",
93
+ },
94
+ },
95
+ },
96
+ },
97
+ } as Parameters<typeof resolveWhatsAppAccount>[0]["cfg"],
98
+ accountId: "work",
99
+ });
100
+
101
+ expect(resolved.dmPolicy).toBe("allowlist");
102
+ expect(resolved.allowFrom).toEqual(["+15550001111"]);
103
+ expect(resolved.groupPolicy).toBe("open");
104
+ expect(resolved.groupAllowFrom).toEqual(["+15550002222"]);
105
+ expect(resolved.defaultTo).toBe("+15550003333");
106
+ expect(resolved.reactionLevel).toBe("extensive");
107
+ expect(resolved.historyLimit).toBe(42);
108
+ expect(resolved.mediaMaxMb).toBe(12);
109
+ });
110
+
111
+ it("prefers account overrides and accounts.default over root defaults", () => {
112
+ const resolved = resolveWhatsAppAccount({
113
+ cfg: {
114
+ channels: {
115
+ whatsapp: {
116
+ dmPolicy: "open",
117
+ allowFrom: ["*"],
118
+ groupPolicy: "disabled",
119
+ accounts: {
120
+ default: {
121
+ dmPolicy: "allowlist",
122
+ allowFrom: ["+15550001111"],
123
+ groupPolicy: "open",
124
+ },
125
+ work: {
126
+ authDir: "/tmp/work",
127
+ dmPolicy: "pairing",
128
+ },
129
+ },
130
+ },
131
+ },
132
+ } as Parameters<typeof resolveWhatsAppAccount>[0]["cfg"],
133
+ accountId: "work",
134
+ });
135
+
136
+ expect(resolved.dmPolicy).toBe("pairing");
137
+ expect(resolved.allowFrom).toEqual(["+15550001111"]);
138
+ expect(resolved.groupPolicy).toBe("open");
139
+ });
140
+
141
+ it("does not inherit default-account authDir for named accounts", () => {
142
+ const resolved = resolveWhatsAppAccount({
143
+ cfg: {
144
+ channels: {
145
+ whatsapp: {
146
+ accounts: {
147
+ default: {
148
+ authDir: "/tmp/default-auth",
149
+ name: "Personal",
150
+ },
151
+ work: {},
152
+ },
153
+ },
154
+ },
155
+ } as Parameters<typeof resolveWhatsAppAccount>[0]["cfg"],
156
+ accountId: "work",
157
+ });
158
+
159
+ expect(resolved.authDir).toMatch(/whatsapp[/\\]work$/);
160
+ expect(resolved.name).toBeUndefined();
161
+ });
162
+
163
+ it("does not inherit default-account selfChatMode for named accounts", () => {
164
+ const resolved = resolveWhatsAppAccount({
165
+ cfg: {
166
+ channels: {
167
+ whatsapp: {
168
+ accounts: {
169
+ default: {
170
+ selfChatMode: true,
171
+ },
172
+ work: {},
173
+ },
174
+ },
175
+ },
176
+ } as Parameters<typeof resolveWhatsAppAccount>[0]["cfg"],
177
+ accountId: "work",
178
+ });
179
+
180
+ expect(resolved.selfChatMode).toBeUndefined();
181
+ });
182
+ });
@@ -0,0 +1,176 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {
4
+ DEFAULT_ACCOUNT_ID,
5
+ normalizeAccountId,
6
+ resolveUserPath,
7
+ type OpenClawConfig,
8
+ } from "openclaw/plugin-sdk/account-core";
9
+ import type { DmPolicy, GroupPolicy, ReplyToMode } from "openclaw/plugin-sdk/config-types";
10
+ import { resolveOAuthDir } from "openclaw/plugin-sdk/state-paths";
11
+ import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
12
+ import { resolveMergedWhatsAppAccountConfig } from "./account-config.js";
13
+ import {
14
+ listConfiguredAccountIds,
15
+ listWhatsAppAccountIds,
16
+ resolveDefaultWhatsAppAccountId,
17
+ } from "./account-ids.js";
18
+ import type { WhatsAppAccountConfig } from "./account-types.js";
19
+ import { hasWebCredsSync } from "./creds-files.js";
20
+
21
+ export { listWhatsAppAccountIds, resolveDefaultWhatsAppAccountId } from "./account-ids.js";
22
+
23
+ export type ResolvedWhatsAppAccount = {
24
+ accountId: string;
25
+ name?: string;
26
+ enabled: boolean;
27
+ sendReadReceipts: boolean;
28
+ messagePrefix?: string;
29
+ defaultTo?: string;
30
+ authDir: string;
31
+ isLegacyAuthDir: boolean;
32
+ selfChatMode?: boolean;
33
+ allowFrom?: string[];
34
+ groupAllowFrom?: string[];
35
+ groupPolicy?: GroupPolicy;
36
+ dmPolicy?: DmPolicy;
37
+ historyLimit?: number;
38
+ textChunkLimit?: number;
39
+ chunkMode?: "length" | "newline";
40
+ mediaMaxMb?: number;
41
+ blockStreaming?: boolean;
42
+ ackReaction?: WhatsAppAccountConfig["ackReaction"];
43
+ reactionLevel?: WhatsAppAccountConfig["reactionLevel"];
44
+ groups?: WhatsAppAccountConfig["groups"];
45
+ direct?: WhatsAppAccountConfig["direct"];
46
+ debounceMs?: number;
47
+ replyToMode?: ReplyToMode;
48
+ };
49
+
50
+ export const DEFAULT_WHATSAPP_MEDIA_MAX_MB = 50;
51
+
52
+ export function listWhatsAppAuthDirs(cfg: OpenClawConfig): string[] {
53
+ const oauthDir = resolveOAuthDir();
54
+ const whatsappDir = path.join(oauthDir, "whatsapp");
55
+ const authDirs = new Set<string>([oauthDir, path.join(whatsappDir, DEFAULT_ACCOUNT_ID)]);
56
+
57
+ const accountIds = listConfiguredAccountIds(cfg);
58
+ for (const accountId of accountIds) {
59
+ authDirs.add(resolveWhatsAppAuthDir({ cfg, accountId }).authDir);
60
+ }
61
+
62
+ try {
63
+ const entries = fs.readdirSync(whatsappDir, { withFileTypes: true });
64
+ for (const entry of entries) {
65
+ if (!entry.isDirectory()) {
66
+ continue;
67
+ }
68
+ authDirs.add(path.join(whatsappDir, entry.name));
69
+ }
70
+ } catch {
71
+ // ignore missing dirs
72
+ }
73
+
74
+ return Array.from(authDirs);
75
+ }
76
+
77
+ export function hasAnyWhatsAppAuth(cfg: OpenClawConfig): boolean {
78
+ return listWhatsAppAuthDirs(cfg).some((authDir) => hasWebCredsSync(authDir));
79
+ }
80
+
81
+ function resolveDefaultAuthDir(accountId: string): string {
82
+ return path.join(resolveOAuthDir(), "whatsapp", normalizeAccountId(accountId));
83
+ }
84
+
85
+ function resolveLegacyAuthDir(): string {
86
+ // Legacy Baileys creds lived in the same directory as OAuth tokens.
87
+ return resolveOAuthDir();
88
+ }
89
+
90
+ function legacyAuthExists(authDir: string): boolean {
91
+ try {
92
+ return fs.existsSync(path.join(authDir, "creds.json"));
93
+ } catch {
94
+ return false;
95
+ }
96
+ }
97
+
98
+ export function resolveWhatsAppAuthDir(params: { cfg: OpenClawConfig; accountId: string }): {
99
+ authDir: string;
100
+ isLegacy: boolean;
101
+ } {
102
+ const accountId = params.accountId.trim() || DEFAULT_ACCOUNT_ID;
103
+ const account = resolveMergedWhatsAppAccountConfig({ cfg: params.cfg, accountId });
104
+ const configured = account?.authDir?.trim();
105
+ if (configured) {
106
+ return { authDir: resolveUserPath(configured), isLegacy: false };
107
+ }
108
+
109
+ const defaultDir = resolveDefaultAuthDir(accountId);
110
+ if (accountId === DEFAULT_ACCOUNT_ID) {
111
+ const legacyDir = resolveLegacyAuthDir();
112
+ if (legacyAuthExists(legacyDir) && !legacyAuthExists(defaultDir)) {
113
+ return { authDir: legacyDir, isLegacy: true };
114
+ }
115
+ }
116
+
117
+ return { authDir: defaultDir, isLegacy: false };
118
+ }
119
+
120
+ export function resolveWhatsAppAccount(params: {
121
+ cfg: OpenClawConfig;
122
+ accountId?: string | null;
123
+ }): ResolvedWhatsAppAccount {
124
+ const merged = resolveMergedWhatsAppAccountConfig({
125
+ cfg: params.cfg,
126
+ accountId: params.accountId?.trim() || resolveDefaultWhatsAppAccountId(params.cfg),
127
+ });
128
+ const accountId = merged.accountId;
129
+ const enabled = merged.enabled !== false;
130
+ const { authDir, isLegacy } = resolveWhatsAppAuthDir({
131
+ cfg: params.cfg,
132
+ accountId,
133
+ });
134
+ return {
135
+ accountId,
136
+ name: normalizeOptionalString(merged.name),
137
+ enabled,
138
+ sendReadReceipts: merged.sendReadReceipts ?? true,
139
+ messagePrefix: merged.messagePrefix ?? params.cfg.messages?.messagePrefix,
140
+ defaultTo: merged.defaultTo,
141
+ authDir,
142
+ isLegacyAuthDir: isLegacy,
143
+ selfChatMode: merged.selfChatMode,
144
+ dmPolicy: merged.dmPolicy,
145
+ allowFrom: merged.allowFrom,
146
+ groupAllowFrom: merged.groupAllowFrom,
147
+ groupPolicy: merged.groupPolicy,
148
+ historyLimit: merged.historyLimit,
149
+ textChunkLimit: merged.textChunkLimit,
150
+ chunkMode: merged.chunkMode,
151
+ mediaMaxMb: merged.mediaMaxMb,
152
+ blockStreaming: merged.blockStreaming,
153
+ ackReaction: merged.ackReaction,
154
+ reactionLevel: merged.reactionLevel,
155
+ groups: merged.groups,
156
+ direct: merged.direct,
157
+ debounceMs: merged.debounceMs,
158
+ replyToMode: merged.replyToMode,
159
+ };
160
+ }
161
+
162
+ export function resolveWhatsAppMediaMaxBytes(
163
+ account: Pick<ResolvedWhatsAppAccount, "mediaMaxMb">,
164
+ ): number {
165
+ const mediaMaxMb =
166
+ typeof account.mediaMaxMb === "number" && account.mediaMaxMb > 0
167
+ ? account.mediaMaxMb
168
+ : DEFAULT_WHATSAPP_MEDIA_MAX_MB;
169
+ return mediaMaxMb * 1024 * 1024;
170
+ }
171
+
172
+ export function listEnabledWhatsAppAccounts(cfg: OpenClawConfig): ResolvedWhatsAppAccount[] {
173
+ return listWhatsAppAccountIds(cfg)
174
+ .map((accountId) => resolveWhatsAppAccount({ cfg, accountId }))
175
+ .filter((account) => account.enabled);
176
+ }