@openclaw/whatsapp 2026.5.2 → 2026.5.3-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 (302) hide show
  1. package/dist/access-control-BQ3tCkH8.js +288 -0
  2. package/dist/account-config-CNQ33oav.js +47 -0
  3. package/dist/account-ids-DG18xddX.js +5 -0
  4. package/dist/accounts-BDqgpFEB.js +116 -0
  5. package/dist/action-runtime-DTGNgG-i.js +74 -0
  6. package/dist/action-runtime-api.js +2 -0
  7. package/dist/action-runtime.runtime.js +2 -0
  8. package/dist/active-listener-N7GFKsuN.js +11 -0
  9. package/dist/agent-tools-login-DpRxLNRB.js +89 -0
  10. package/dist/api.js +148 -0
  11. package/dist/audio-preflight.runtime-Bcdsl_ri.js +7 -0
  12. package/dist/auth-presence.js +32 -0
  13. package/dist/auth-store-BjCb50Ry.js +381 -0
  14. package/dist/channel-DQ-5Xoj9.js +542 -0
  15. package/dist/channel-config-api.js +2 -0
  16. package/dist/channel-plugin-api.js +2 -0
  17. package/dist/channel-react-action-Df20qAy9.js +42 -0
  18. package/dist/channel.runtime-DKXRnA8x.js +55 -0
  19. package/dist/channel.setup-DOHR2BGp.js +20 -0
  20. package/dist/command-policy-O8sRnqUV.js +8 -0
  21. package/dist/config-schema-Dmo66Ubb.js +27 -0
  22. package/dist/connection-controller-BiHAsTM8.js +596 -0
  23. package/dist/connection-controller-registry-D_iIGz5m.js +22 -0
  24. package/dist/constants-DLGCIbfl.js +4 -0
  25. package/dist/constants.js +2 -0
  26. package/dist/contract-api.js +19 -0
  27. package/dist/creds-files-_2V8jzQa.js +19 -0
  28. package/dist/directory-config-DVs4ffXu.js +37 -0
  29. package/dist/directory-contract-api.js +2 -0
  30. package/dist/doctor-A9tXm9Om.js +48 -0
  31. package/dist/doctor-contract-api.js +10 -0
  32. package/dist/group-session-contract-B8QBn0I2.js +15 -0
  33. package/dist/identity-QHAlV7Fu.js +75 -0
  34. package/dist/index.js +18 -0
  35. package/dist/legacy-session-surface-api.js +8 -0
  36. package/dist/legacy-state-migrations-api.js +2 -0
  37. package/dist/light-runtime-api.js +5 -0
  38. package/dist/login-DymdrKca.js +52 -0
  39. package/dist/login-qr-DIS0Iv-7.js +378 -0
  40. package/dist/login-qr-api.js +2 -0
  41. package/dist/login-qr-runtime.js +16 -0
  42. package/dist/monitor-C4Fhg_lD.js +3349 -0
  43. package/dist/normalize-CWYiGkXw.js +2 -0
  44. package/dist/normalize-target-BPVHNsM5.js +74 -0
  45. package/dist/outbound-adapter-BssVnZdz.js +29 -0
  46. package/dist/outbound-base-Bq1UYEK5.js +119 -0
  47. package/dist/outbound-media-contract-CGss_QHy.js +163 -0
  48. package/dist/outbound-payload-test-api.js +2 -0
  49. package/dist/quoted-message-TENQzyxV.js +108 -0
  50. package/dist/reaction-level-CRVEVAfi.js +16 -0
  51. package/dist/reply-resolver.runtime-BRCgoO4C.js +2 -0
  52. package/dist/resolve-outbound-target-CN-Wo6IY.js +39 -0
  53. package/dist/rolldown-runtime-RkAeH_Qm.js +27 -0
  54. package/dist/runtime-6fZ67abR.js +8 -0
  55. package/dist/runtime-api.js +15 -0
  56. package/dist/runtime-group-policy-Gp1lQ0at.js +11 -0
  57. package/dist/secret-contract-api.js +5 -0
  58. package/dist/security-contract-DqH8JDX6.js +26 -0
  59. package/dist/security-contract-api.js +2 -0
  60. package/dist/send-CIyLFxdZ.js +234 -0
  61. package/dist/session-contract-B0efQf9P.js +27 -0
  62. package/dist/session-errors-CSuKZPub.js +63 -0
  63. package/dist/session.runtime-C1eSE_KK.js +13 -0
  64. package/dist/setup-core-DZKvpzbH.js +46 -0
  65. package/dist/setup-entry.js +23 -0
  66. package/dist/setup-plugin-api.js +2 -0
  67. package/dist/setup-surface-ieFnIW4g.js +344 -0
  68. package/dist/shared-D_wzSVar.js +307 -0
  69. package/dist/state-migrations-DnCdvoYD.js +39 -0
  70. package/dist/targets.js +2 -0
  71. package/dist/test-api.js +3 -0
  72. package/dist/text-runtime-C1zNiZeO.js +116 -0
  73. package/package.json +14 -6
  74. package/action-runtime-api.ts +0 -1
  75. package/action-runtime.runtime.ts +0 -1
  76. package/api.ts +0 -213
  77. package/auth-presence.ts +0 -80
  78. package/channel-config-api.ts +0 -1
  79. package/channel-plugin-api.ts +0 -3
  80. package/config-api.ts +0 -4
  81. package/constants.ts +0 -1
  82. package/contract-api.ts +0 -29
  83. package/directory-contract-api.ts +0 -4
  84. package/doctor-contract-api.ts +0 -8
  85. package/index.test.ts +0 -13
  86. package/index.ts +0 -16
  87. package/legacy-session-surface-api.ts +0 -6
  88. package/legacy-state-migrations-api.ts +0 -1
  89. package/light-runtime-api.ts +0 -12
  90. package/login-qr-api.ts +0 -1
  91. package/login-qr-runtime.ts +0 -23
  92. package/outbound-payload-test-api.ts +0 -1
  93. package/runtime-api.ts +0 -84
  94. package/secret-contract-api.ts +0 -4
  95. package/security-contract-api.ts +0 -4
  96. package/setup-entry.test.ts +0 -21
  97. package/setup-entry.ts +0 -21
  98. package/setup-plugin-api.ts +0 -3
  99. package/src/account-config.ts +0 -77
  100. package/src/account-ids.ts +0 -13
  101. package/src/account-types.ts +0 -5
  102. package/src/accounts.test.ts +0 -182
  103. package/src/accounts.ts +0 -176
  104. package/src/accounts.whatsapp-auth.test.ts +0 -59
  105. package/src/action-runtime-target-auth.ts +0 -27
  106. package/src/action-runtime.test.ts +0 -330
  107. package/src/action-runtime.ts +0 -76
  108. package/src/active-listener.test.ts +0 -65
  109. package/src/active-listener.ts +0 -17
  110. package/src/agent-tools-login.test.ts +0 -81
  111. package/src/agent-tools-login.ts +0 -113
  112. package/src/approval-auth.test.ts +0 -24
  113. package/src/approval-auth.ts +0 -27
  114. package/src/auth-store.runtime.ts +0 -1
  115. package/src/auth-store.test.ts +0 -311
  116. package/src/auth-store.ts +0 -502
  117. package/src/auto-reply/config.runtime.ts +0 -16
  118. package/src/auto-reply/constants.ts +0 -1
  119. package/src/auto-reply/deliver-reply.test.ts +0 -843
  120. package/src/auto-reply/deliver-reply.ts +0 -279
  121. package/src/auto-reply/loggers.ts +0 -6
  122. package/src/auto-reply/mentions.ts +0 -131
  123. package/src/auto-reply/monitor/ack-reaction.test.ts +0 -170
  124. package/src/auto-reply/monitor/ack-reaction.ts +0 -99
  125. package/src/auto-reply/monitor/audio-preflight.runtime.ts +0 -9
  126. package/src/auto-reply/monitor/broadcast.ts +0 -153
  127. package/src/auto-reply/monitor/commands.ts +0 -19
  128. package/src/auto-reply/monitor/echo.ts +0 -64
  129. package/src/auto-reply/monitor/group-activation.runtime.ts +0 -1
  130. package/src/auto-reply/monitor/group-activation.test.ts +0 -189
  131. package/src/auto-reply/monitor/group-activation.ts +0 -73
  132. package/src/auto-reply/monitor/group-gating.audio-preflight.test.ts +0 -103
  133. package/src/auto-reply/monitor/group-gating.runtime.ts +0 -8
  134. package/src/auto-reply/monitor/group-gating.ts +0 -217
  135. package/src/auto-reply/monitor/group-members.test.ts +0 -56
  136. package/src/auto-reply/monitor/group-members.ts +0 -65
  137. package/src/auto-reply/monitor/inbound-context.test.ts +0 -97
  138. package/src/auto-reply/monitor/inbound-context.ts +0 -92
  139. package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +0 -21
  140. package/src/auto-reply/monitor/inbound-dispatch.test.ts +0 -895
  141. package/src/auto-reply/monitor/inbound-dispatch.ts +0 -422
  142. package/src/auto-reply/monitor/last-route.test.ts +0 -37
  143. package/src/auto-reply/monitor/last-route.ts +0 -61
  144. package/src/auto-reply/monitor/message-line.runtime.ts +0 -38
  145. package/src/auto-reply/monitor/message-line.ts +0 -54
  146. package/src/auto-reply/monitor/on-message.audio-preflight.test.ts +0 -354
  147. package/src/auto-reply/monitor/on-message.ts +0 -291
  148. package/src/auto-reply/monitor/peer.ts +0 -17
  149. package/src/auto-reply/monitor/process-message.audio-preflight.test.ts +0 -420
  150. package/src/auto-reply/monitor/process-message.test.ts +0 -355
  151. package/src/auto-reply/monitor/process-message.ts +0 -531
  152. package/src/auto-reply/monitor/runtime-api.ts +0 -30
  153. package/src/auto-reply/monitor-state.test.ts +0 -64
  154. package/src/auto-reply/monitor-state.ts +0 -106
  155. package/src/auto-reply/monitor.ts +0 -600
  156. package/src/auto-reply/reply-resolver.runtime.ts +0 -1
  157. package/src/auto-reply/types.ts +0 -48
  158. package/src/auto-reply/util.ts +0 -63
  159. package/src/auto-reply/web-auto-reply-monitor.test.ts +0 -687
  160. package/src/auto-reply/web-auto-reply-utils.test.ts +0 -392
  161. package/src/auto-reply.broadcast-groups.combined.test.ts +0 -246
  162. package/src/auto-reply.broadcast-groups.test-harness.ts +0 -47
  163. package/src/auto-reply.impl.ts +0 -6
  164. package/src/auto-reply.test-harness.ts +0 -421
  165. package/src/auto-reply.ts +0 -1
  166. package/src/auto-reply.web-auto-reply.compresses-common-formats-jpeg-cap.test.ts +0 -427
  167. package/src/auto-reply.web-auto-reply.connection-and-logging.e2e.test.ts +0 -908
  168. package/src/auto-reply.web-auto-reply.last-route.test.ts +0 -184
  169. package/src/channel-actions.runtime.ts +0 -7
  170. package/src/channel-actions.test.ts +0 -216
  171. package/src/channel-actions.ts +0 -84
  172. package/src/channel-outbound.test.ts +0 -131
  173. package/src/channel-outbound.ts +0 -36
  174. package/src/channel-react-action.runtime.ts +0 -7
  175. package/src/channel-react-action.test.ts +0 -294
  176. package/src/channel-react-action.ts +0 -84
  177. package/src/channel.runtime.ts +0 -117
  178. package/src/channel.setup.test.ts +0 -461
  179. package/src/channel.setup.ts +0 -28
  180. package/src/channel.ts +0 -339
  181. package/src/command-policy.ts +0 -7
  182. package/src/config-accessors.test.ts +0 -34
  183. package/src/config-accessors.ts +0 -22
  184. package/src/config-schema.test.ts +0 -121
  185. package/src/config-schema.ts +0 -6
  186. package/src/config-ui-hints.ts +0 -24
  187. package/src/connection-controller-registry.test.ts +0 -27
  188. package/src/connection-controller-registry.ts +0 -49
  189. package/src/connection-controller.test.ts +0 -282
  190. package/src/connection-controller.ts +0 -679
  191. package/src/creds-files.ts +0 -19
  192. package/src/creds-persistence.ts +0 -102
  193. package/src/directory-config.test.ts +0 -60
  194. package/src/directory-config.ts +0 -40
  195. package/src/directory-contract.test.ts +0 -56
  196. package/src/doctor-contract.ts +0 -11
  197. package/src/doctor.test.ts +0 -74
  198. package/src/doctor.ts +0 -56
  199. package/src/group-intro.ts +0 -15
  200. package/src/group-policy.test.ts +0 -36
  201. package/src/group-policy.ts +0 -40
  202. package/src/group-session-contract.ts +0 -20
  203. package/src/group-session-key.test.ts +0 -53
  204. package/src/group-session-key.ts +0 -41
  205. package/src/heartbeat.ts +0 -34
  206. package/src/identity.ts +0 -164
  207. package/src/inbound/access-control.test-harness.ts +0 -37
  208. package/src/inbound/access-control.test.ts +0 -375
  209. package/src/inbound/access-control.ts +0 -237
  210. package/src/inbound/dedupe.ts +0 -132
  211. package/src/inbound/extract.test.ts +0 -282
  212. package/src/inbound/extract.ts +0 -488
  213. package/src/inbound/lifecycle.ts +0 -39
  214. package/src/inbound/media.node.test.ts +0 -83
  215. package/src/inbound/media.ts +0 -102
  216. package/src/inbound/monitor.ts +0 -890
  217. package/src/inbound/runtime-api.ts +0 -7
  218. package/src/inbound/save-media.runtime.ts +0 -1
  219. package/src/inbound/send-api.test.ts +0 -319
  220. package/src/inbound/send-api.ts +0 -145
  221. package/src/inbound/send-result.ts +0 -61
  222. package/src/inbound/types.ts +0 -106
  223. package/src/inbound-context.contract.test.ts +0 -32
  224. package/src/inbound-policy.ts +0 -231
  225. package/src/inbound.media.test.ts +0 -320
  226. package/src/inbound.test.ts +0 -290
  227. package/src/inbound.ts +0 -9
  228. package/src/login-qr.test.ts +0 -483
  229. package/src/login-qr.ts +0 -542
  230. package/src/login.coverage.test.ts +0 -133
  231. package/src/login.test.ts +0 -89
  232. package/src/login.ts +0 -70
  233. package/src/logout.test.ts +0 -162
  234. package/src/media.test.ts +0 -480
  235. package/src/media.ts +0 -10
  236. package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +0 -400
  237. package/src/monitor-inbox.append-upsert.test-support.ts +0 -133
  238. package/src/monitor-inbox.behavior.test.ts +0 -5
  239. package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +0 -362
  240. package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +0 -306
  241. package/src/monitor-inbox.streams-inbound-messages.test-support.ts +0 -729
  242. package/src/monitor-inbox.test-harness.ts +0 -304
  243. package/src/normalize-target.ts +0 -123
  244. package/src/normalize.ts +0 -7
  245. package/src/outbound-adapter.poll.test.ts +0 -62
  246. package/src/outbound-adapter.sendpayload.test.ts +0 -195
  247. package/src/outbound-adapter.ts +0 -36
  248. package/src/outbound-base.test.ts +0 -560
  249. package/src/outbound-base.ts +0 -244
  250. package/src/outbound-media-contract.ts +0 -278
  251. package/src/outbound-media.runtime.ts +0 -36
  252. package/src/outbound-payload.contract.test.ts +0 -61
  253. package/src/outbound-send-deps.ts +0 -1
  254. package/src/outbound-test-support.ts +0 -16
  255. package/src/pairing-security.test-harness.ts +0 -59
  256. package/src/qr-image.ts +0 -1
  257. package/src/qr-terminal.ts +0 -1
  258. package/src/quoted-message.test.ts +0 -83
  259. package/src/quoted-message.ts +0 -184
  260. package/src/reaction-level.test.ts +0 -94
  261. package/src/reaction-level.ts +0 -21
  262. package/src/reconnect.test.ts +0 -51
  263. package/src/reconnect.ts +0 -55
  264. package/src/resolve-outbound-target.test.ts +0 -288
  265. package/src/resolve-outbound-target.ts +0 -58
  266. package/src/resolve-target.test.ts +0 -120
  267. package/src/runtime-api.ts +0 -59
  268. package/src/runtime-group-policy.ts +0 -16
  269. package/src/runtime.ts +0 -9
  270. package/src/security-contract.ts +0 -47
  271. package/src/security-fix.ts +0 -71
  272. package/src/send.test.ts +0 -534
  273. package/src/send.ts +0 -292
  274. package/src/session-contract.test.ts +0 -31
  275. package/src/session-contract.ts +0 -43
  276. package/src/session-errors.ts +0 -125
  277. package/src/session-route.test.ts +0 -41
  278. package/src/session-route.ts +0 -32
  279. package/src/session.runtime.ts +0 -8
  280. package/src/session.test.ts +0 -559
  281. package/src/session.ts +0 -330
  282. package/src/setup-core.ts +0 -52
  283. package/src/setup-finalize.ts +0 -446
  284. package/src/setup-surface.test.ts +0 -367
  285. package/src/setup-surface.ts +0 -69
  286. package/src/setup-test-helpers.ts +0 -216
  287. package/src/shared.ts +0 -291
  288. package/src/socket-timing.test.ts +0 -49
  289. package/src/socket-timing.ts +0 -38
  290. package/src/state-migrations.ts +0 -54
  291. package/src/status-issues.test.ts +0 -137
  292. package/src/status-issues.ts +0 -185
  293. package/src/system-prompt.test.ts +0 -199
  294. package/src/system-prompt.ts +0 -31
  295. package/src/targets-runtime.ts +0 -180
  296. package/src/test-helpers.ts +0 -690
  297. package/src/text-runtime.test.ts +0 -162
  298. package/src/text-runtime.ts +0 -11
  299. package/src/vcard.ts +0 -84
  300. package/targets.ts +0 -5
  301. package/test-api.ts +0 -2
  302. package/tsconfig.json +0 -16
package/src/auth-store.ts DELETED
@@ -1,502 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import fsSync from "node:fs";
3
- import fs from "node:fs/promises";
4
- import path from "node:path";
5
- import { formatCliCommand } from "openclaw/plugin-sdk/cli-runtime";
6
- import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/routing";
7
- import { info, success } from "openclaw/plugin-sdk/runtime-env";
8
- import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
9
- import { defaultRuntime, type RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
10
- import { resolveOAuthDir } from "./auth-store.runtime.js";
11
- import { hasWebCredsSync, resolveWebCredsBackupPath, resolveWebCredsPath } from "./creds-files.js";
12
- import {
13
- waitForCredsSaveQueueWithTimeout,
14
- type CredsQueueWaitResult,
15
- } from "./creds-persistence.js";
16
- import { resolveComparableIdentity, type WhatsAppSelfIdentity } from "./identity.js";
17
- import { resolveUserPath, type WebChannel } from "./text-runtime.js";
18
- export { hasWebCredsSync, resolveWebCredsBackupPath, resolveWebCredsPath };
19
-
20
- export const WHATSAPP_AUTH_UNSTABLE_CODE = "whatsapp-auth-unstable";
21
-
22
- const authStoreLogger = getChildLogger({ module: "web-auth-store" });
23
- const emptyWebSelfId = () => ({ e164: null, jid: null, lid: null }) as const;
24
- export type WhatsAppWebAuthState = "linked" | "not-linked" | "unstable";
25
-
26
- export class WhatsAppAuthUnstableError extends Error {
27
- readonly code = WHATSAPP_AUTH_UNSTABLE_CODE;
28
-
29
- constructor(message = "WhatsApp auth state is still stabilizing; retry shortly.") {
30
- super(message);
31
- this.name = "WhatsAppAuthUnstableError";
32
- }
33
- }
34
-
35
- export function resolveDefaultWebAuthDir(): string {
36
- return path.join(resolveOAuthDir(), "whatsapp", DEFAULT_ACCOUNT_ID);
37
- }
38
-
39
- export const WA_WEB_AUTH_DIR = resolveDefaultWebAuthDir();
40
-
41
- export function readCredsJsonRaw(filePath: string): string | null {
42
- try {
43
- if (!fsSync.existsSync(filePath)) {
44
- return null;
45
- }
46
- const stats = fsSync.statSync(filePath);
47
- if (!stats.isFile() || stats.size <= 1) {
48
- return null;
49
- }
50
- return fsSync.readFileSync(filePath, "utf-8");
51
- } catch {
52
- return null;
53
- }
54
- }
55
-
56
- async function waitForWebAuthBarrier(
57
- authDir: string,
58
- context: string,
59
- ): Promise<CredsQueueWaitResult> {
60
- const result = await waitForCredsSaveQueueWithTimeout(authDir);
61
- if (result === "timed_out") {
62
- authStoreLogger.warn(
63
- {
64
- authDir,
65
- context,
66
- },
67
- "timed out waiting for queued WhatsApp creds save before auth read",
68
- );
69
- }
70
- return result;
71
- }
72
-
73
- export async function restoreCredsFromBackupIfNeeded(authDir: string): Promise<boolean> {
74
- const logger = getChildLogger({ module: "web-session" });
75
- let tempRestorePath: string | null = null;
76
- try {
77
- const credsPath = resolveWebCredsPath(authDir);
78
- const backupPath = resolveWebCredsBackupPath(authDir);
79
- const raw = readCredsJsonRaw(credsPath);
80
- if (raw) {
81
- // Validate that creds.json is parseable.
82
- JSON.parse(raw);
83
- return false;
84
- }
85
-
86
- const backupRaw = readCredsJsonRaw(backupPath);
87
- if (!backupRaw) {
88
- return false;
89
- }
90
- const backupStats = await fs.lstat(backupPath).catch(() => null);
91
- if (!backupStats?.isFile()) {
92
- return false;
93
- }
94
-
95
- // Ensure backup is parseable before restoring.
96
- JSON.parse(backupRaw);
97
- tempRestorePath = path.join(authDir, `.creds.restore-${randomUUID()}.tmp`);
98
- await fs.writeFile(tempRestorePath, backupRaw, {
99
- encoding: "utf-8",
100
- mode: 0o600,
101
- flag: "wx",
102
- });
103
- await fs.rename(tempRestorePath, credsPath);
104
- tempRestorePath = null;
105
- logger.warn({ credsPath }, "restored corrupted WhatsApp creds.json from backup");
106
- return true;
107
- } catch {
108
- // ignore
109
- } finally {
110
- if (tempRestorePath) {
111
- await fs.rm(tempRestorePath, { force: true }).catch(() => {
112
- // best-effort temp cleanup
113
- });
114
- }
115
- }
116
- return false;
117
- }
118
-
119
- export async function webAuthExists(authDir: string = resolveDefaultWebAuthDir()) {
120
- const resolvedAuthDir = resolveUserPath(authDir);
121
- const credsPath = resolveWebCredsPath(resolvedAuthDir);
122
- try {
123
- await fs.access(resolvedAuthDir);
124
- } catch {
125
- return false;
126
- }
127
- try {
128
- const stats = await fs.stat(credsPath);
129
- if (!stats.isFile() || stats.size <= 1) {
130
- return false;
131
- }
132
- const raw = await fs.readFile(credsPath, "utf-8");
133
- JSON.parse(raw);
134
- return true;
135
- } catch {
136
- return false;
137
- }
138
- }
139
-
140
- function resolveWebAuthState(params: {
141
- linked: boolean;
142
- barrierResult: CredsQueueWaitResult;
143
- }): WhatsAppWebAuthState {
144
- if (params.barrierResult === "timed_out") {
145
- return "unstable";
146
- }
147
- return params.linked ? "linked" : "not-linked";
148
- }
149
-
150
- async function readWebAuthStateCore(
151
- authDir: string,
152
- context: string,
153
- ): Promise<{ authDir: string; linked: boolean; state: WhatsAppWebAuthState }> {
154
- const resolvedAuthDir = resolveUserPath(authDir);
155
- const barrierResult = await waitForWebAuthBarrier(resolvedAuthDir, context);
156
- const linked = await webAuthExists(resolvedAuthDir);
157
- return {
158
- authDir: resolvedAuthDir,
159
- linked,
160
- state: resolveWebAuthState({ linked, barrierResult }),
161
- };
162
- }
163
-
164
- export function formatWhatsAppWebAuthStatusState(state: WhatsAppWebAuthState): string {
165
- switch (state) {
166
- case "linked":
167
- return "linked";
168
- case "not-linked":
169
- return "not linked";
170
- case "unstable":
171
- return "auth stabilizing";
172
- }
173
- const exhaustive: never = state;
174
- return exhaustive;
175
- }
176
-
177
- export async function readWebAuthState(
178
- authDir: string = resolveDefaultWebAuthDir(),
179
- ): Promise<WhatsAppWebAuthState> {
180
- return (await readWebAuthStateCore(authDir, "readWebAuthState")).state;
181
- }
182
-
183
- export async function readWebAuthSnapshot(authDir: string = resolveDefaultWebAuthDir()) {
184
- const auth = await readWebAuthStateCore(authDir, "readWebAuthSnapshot");
185
- return {
186
- state: auth.state,
187
- authAgeMs: auth.state === "linked" ? getWebAuthAgeMs(auth.authDir) : null,
188
- selfId: auth.state === "linked" ? readWebSelfId(auth.authDir) : emptyWebSelfId(),
189
- } as const;
190
- }
191
-
192
- export async function readWebAuthExistsBestEffort(authDir: string = resolveDefaultWebAuthDir()) {
193
- const state = await readWebAuthState(authDir);
194
- return {
195
- exists: state === "linked",
196
- timedOut: state === "unstable",
197
- } as const;
198
- }
199
-
200
- export async function readWebAuthExistsForDecision(
201
- authDir: string = resolveDefaultWebAuthDir(),
202
- ): Promise<{ outcome: "stable"; exists: boolean } | { outcome: "unstable" }> {
203
- const state = await readWebAuthState(authDir);
204
- if (state === "unstable") {
205
- return { outcome: "unstable" };
206
- }
207
- return {
208
- outcome: "stable",
209
- exists: state === "linked",
210
- };
211
- }
212
-
213
- export async function readWebAuthSnapshotBestEffort(authDir: string = resolveDefaultWebAuthDir()) {
214
- const snapshot = await readWebAuthSnapshot(authDir);
215
- return {
216
- linked: snapshot.state === "linked",
217
- timedOut: snapshot.state === "unstable",
218
- authAgeMs: snapshot.authAgeMs,
219
- selfId: snapshot.selfId,
220
- } as const;
221
- }
222
-
223
- function isBaileysAuthFileName(name: string): boolean {
224
- if (name === "oauth.json") {
225
- return false;
226
- }
227
- if (name === "creds.json" || name === "creds.json.bak") {
228
- return true;
229
- }
230
- if (!name.endsWith(".json")) {
231
- return false;
232
- }
233
- return /^(app-state-sync|session|sender-key|pre-key)-/.test(name);
234
- }
235
-
236
- async function clearBaileysAuthFiles(authDir: string) {
237
- const rootStats = await fs.lstat(authDir).catch(() => null);
238
- if (!rootStats?.isDirectory() || rootStats.isSymbolicLink()) {
239
- return;
240
- }
241
- const entries = await fs.readdir(authDir, { withFileTypes: true });
242
- await Promise.all(
243
- entries.map(async (entry) => {
244
- if (!entry.isFile()) {
245
- return;
246
- }
247
- if (!isBaileysAuthFileName(entry.name)) {
248
- return;
249
- }
250
- await fs.rm(path.join(authDir, entry.name), { force: true });
251
- }),
252
- );
253
- }
254
-
255
- async function shouldClearOnLogout(authDir: string, isLegacyAuthDir: boolean): Promise<boolean> {
256
- try {
257
- const stats = await fs.lstat(authDir);
258
- if (!stats.isDirectory() || stats.isSymbolicLink()) {
259
- return false;
260
- }
261
- if (isLegacyAuthDir) {
262
- const entries = await fs.readdir(authDir, { withFileTypes: true });
263
- return entries.some((entry) => {
264
- if (!entry.isFile()) {
265
- return false;
266
- }
267
- return isBaileysAuthFileName(entry.name);
268
- });
269
- }
270
- const credsStats = await fs.lstat(resolveWebCredsPath(authDir)).catch(() => null);
271
- if (credsStats?.isFile()) {
272
- return true;
273
- }
274
- const backupStats = await fs.lstat(resolveWebCredsBackupPath(authDir)).catch(() => null);
275
- return backupStats?.isFile() === true;
276
- } catch (error) {
277
- const codeValue =
278
- error && typeof error === "object" && "code" in error
279
- ? (error as { code?: unknown }).code
280
- : undefined;
281
- const code = typeof codeValue === "string" ? codeValue : "";
282
- return code !== "ENOENT";
283
- }
284
- }
285
-
286
- function isPathInsideDirectory(baseDir: string, targetPath: string): boolean {
287
- const relativePath = path.relative(baseDir, targetPath);
288
- return relativePath !== "" && !relativePath.startsWith("..") && !path.isAbsolute(relativePath);
289
- }
290
-
291
- async function pathHasSymlinkComponent(baseDir: string, targetPath: string): Promise<boolean> {
292
- const relativePath = path.relative(baseDir, targetPath);
293
- let currentPath = baseDir;
294
- for (const segment of relativePath.split(path.sep)) {
295
- currentPath = path.join(currentPath, segment);
296
- const stats = await fs.lstat(currentPath).catch(() => null);
297
- if (!stats || stats.isSymbolicLink()) {
298
- return true;
299
- }
300
- }
301
- return false;
302
- }
303
-
304
- type WebAuthDirOwnership =
305
- | { kind: "owned"; authDir: string }
306
- | { kind: "unsafe-owned" }
307
- | { kind: "external" };
308
-
309
- async function isLegacyWebAuthDir(authDir: string): Promise<boolean> {
310
- const legacyAuthDir = path.resolve(resolveOAuthDir());
311
- const resolvedAuthDir = path.resolve(authDir);
312
- if (resolvedAuthDir !== legacyAuthDir) {
313
- return false;
314
- }
315
- const stats = await fs.lstat(resolvedAuthDir).catch(() => null);
316
- return stats?.isDirectory() === true && !stats.isSymbolicLink();
317
- }
318
-
319
- async function classifyWebAuthDirOwnership(authDir: string): Promise<WebAuthDirOwnership> {
320
- const whatsappAuthBase = path.resolve(resolveOAuthDir(), "whatsapp");
321
- const resolvedAuthDir = path.resolve(authDir);
322
- if (!isPathInsideDirectory(whatsappAuthBase, resolvedAuthDir)) {
323
- return { kind: "external" };
324
- }
325
-
326
- const [baseRealPath, authDirRealPath] = await Promise.all([
327
- fs.realpath(whatsappAuthBase).catch(() => null),
328
- fs.realpath(resolvedAuthDir).catch(() => null),
329
- ]);
330
- if (!baseRealPath || !authDirRealPath) {
331
- return { kind: "unsafe-owned" };
332
- }
333
- if (!isPathInsideDirectory(baseRealPath, authDirRealPath)) {
334
- return { kind: "unsafe-owned" };
335
- }
336
- if (await pathHasSymlinkComponent(whatsappAuthBase, resolvedAuthDir)) {
337
- return { kind: "unsafe-owned" };
338
- }
339
- return { kind: "owned", authDir: resolvedAuthDir };
340
- }
341
-
342
- export async function logoutWeb(params: {
343
- authDir?: string;
344
- isLegacyAuthDir?: boolean;
345
- runtime?: RuntimeEnv;
346
- }) {
347
- const runtime = params.runtime ?? defaultRuntime;
348
- const resolvedAuthDir = resolveUserPath(params.authDir ?? resolveDefaultWebAuthDir());
349
- const barrierResult = await waitForWebAuthBarrier(resolvedAuthDir, "logoutWeb");
350
- if (barrierResult === "timed_out") {
351
- runtime.log(
352
- info("WhatsApp auth state is still stabilizing; clearing cached credentials anyway."),
353
- );
354
- }
355
- if (!(await shouldClearOnLogout(resolvedAuthDir, Boolean(params.isLegacyAuthDir)))) {
356
- runtime.log(info("No WhatsApp Web session found; nothing to delete."));
357
- return false;
358
- }
359
- if (params.isLegacyAuthDir) {
360
- if (!(await isLegacyWebAuthDir(resolvedAuthDir))) {
361
- runtime.log(
362
- info("Skipped WhatsApp Web credential cleanup outside the managed legacy auth directory."),
363
- );
364
- return false;
365
- }
366
- await clearBaileysAuthFiles(resolvedAuthDir);
367
- } else {
368
- const ownership = await classifyWebAuthDirOwnership(resolvedAuthDir);
369
- if (ownership.kind === "owned") {
370
- await fs.rm(ownership.authDir, { recursive: true, force: true });
371
- } else if (ownership.kind === "unsafe-owned") {
372
- runtime.log(
373
- info(
374
- "Skipped WhatsApp Web credential cleanup because the auth directory crosses a symlink boundary.",
375
- ),
376
- );
377
- return false;
378
- } else {
379
- runtime.log(
380
- info("Skipped WhatsApp Web credential cleanup outside the managed auth directory."),
381
- );
382
- return false;
383
- }
384
- }
385
- runtime.log(success("Cleared WhatsApp Web credentials."));
386
- return true;
387
- }
388
-
389
- export function readWebSelfId(authDir: string = resolveDefaultWebAuthDir()) {
390
- // Read the cached WhatsApp Web identity (jid + E.164) from disk if present.
391
- try {
392
- const credsPath = resolveWebCredsPath(resolveUserPath(authDir));
393
- if (!fsSync.existsSync(credsPath)) {
394
- return emptyWebSelfId();
395
- }
396
- const raw = fsSync.readFileSync(credsPath, "utf-8");
397
- const parsed = JSON.parse(raw) as { me?: { id?: string; lid?: string } } | undefined;
398
- const identity = resolveComparableIdentity(
399
- {
400
- jid: parsed?.me?.id ?? null,
401
- lid: parsed?.me?.lid ?? null,
402
- },
403
- authDir,
404
- );
405
- return {
406
- e164: identity.e164 ?? null,
407
- jid: identity.jid ?? null,
408
- lid: identity.lid ?? null,
409
- } as const;
410
- } catch {
411
- return emptyWebSelfId();
412
- }
413
- }
414
-
415
- export async function readWebSelfIdentity(
416
- authDir: string = resolveDefaultWebAuthDir(),
417
- fallback?: { id?: string | null; lid?: string | null } | null,
418
- ): Promise<WhatsAppSelfIdentity> {
419
- const resolvedAuthDir = resolveUserPath(authDir);
420
- try {
421
- const raw = await fs.readFile(resolveWebCredsPath(resolvedAuthDir), "utf-8");
422
- const parsed = JSON.parse(raw) as { me?: { id?: string; lid?: string } } | undefined;
423
- return resolveComparableIdentity(
424
- {
425
- jid: parsed?.me?.id ?? null,
426
- lid: parsed?.me?.lid ?? null,
427
- },
428
- resolvedAuthDir,
429
- );
430
- } catch {
431
- return resolveComparableIdentity(
432
- {
433
- jid: fallback?.id ?? null,
434
- lid: fallback?.lid ?? null,
435
- },
436
- resolvedAuthDir,
437
- );
438
- }
439
- }
440
-
441
- export async function readWebSelfIdentityForDecision(
442
- authDir: string = resolveDefaultWebAuthDir(),
443
- fallback?: { id?: string | null; lid?: string | null } | null,
444
- ): Promise<{ outcome: "stable"; identity: WhatsAppSelfIdentity } | { outcome: "unstable" }> {
445
- const resolvedAuthDir = resolveUserPath(authDir);
446
- const result = await waitForWebAuthBarrier(resolvedAuthDir, "readWebSelfIdentityForDecision");
447
- if (result === "timed_out") {
448
- return { outcome: "unstable" };
449
- }
450
- return {
451
- outcome: "stable",
452
- identity: await readWebSelfIdentity(resolvedAuthDir, fallback),
453
- };
454
- }
455
-
456
- /**
457
- * Return the age (in milliseconds) of the cached WhatsApp web auth state, or null when missing.
458
- * Helpful for heartbeats/observability to spot stale credentials.
459
- */
460
- export function getWebAuthAgeMs(authDir: string = resolveDefaultWebAuthDir()): number | null {
461
- try {
462
- const stats = fsSync.statSync(resolveWebCredsPath(resolveUserPath(authDir)));
463
- return Date.now() - stats.mtimeMs;
464
- } catch {
465
- return null;
466
- }
467
- }
468
-
469
- export function logWebSelfId(
470
- authDir: string = resolveDefaultWebAuthDir(),
471
- runtime: RuntimeEnv = defaultRuntime,
472
- includeChannelPrefix = false,
473
- ) {
474
- // Human-friendly log of the currently linked personal web session.
475
- const { e164, jid, lid } = readWebSelfId(authDir);
476
- const parts = [jid ? `jid ${jid}` : null, lid ? `lid ${lid}` : null].filter(
477
- (value): value is string => Boolean(value),
478
- );
479
- const details =
480
- e164 || parts.length > 0
481
- ? `${e164 ?? "unknown"}${parts.length > 0 ? ` (${parts.join(", ")})` : ""}`
482
- : "unknown";
483
- const prefix = includeChannelPrefix ? "Web Channel: " : "";
484
- runtime.log(info(`${prefix}${details}`));
485
- }
486
-
487
- export async function pickWebChannel(
488
- pref: WebChannel | "auto",
489
- authDir: string = resolveDefaultWebAuthDir(),
490
- ): Promise<WebChannel> {
491
- const choice: WebChannel = pref === "auto" ? "web" : pref;
492
- const auth = await readWebAuthExistsForDecision(authDir);
493
- if (auth.outcome === "unstable") {
494
- throw new WhatsAppAuthUnstableError();
495
- }
496
- if (!auth.exists) {
497
- throw new Error(
498
- `No WhatsApp Web session found. Run \`${formatCliCommand("openclaw channels login --channel whatsapp --verbose")}\` to link.`,
499
- );
500
- }
501
- return choice;
502
- }
@@ -1,16 +0,0 @@
1
- export {
2
- evaluateSessionFreshness,
3
- loadSessionStore,
4
- resolveSessionKey,
5
- resolveSessionResetPolicy,
6
- resolveSessionResetType,
7
- resolveStorePath,
8
- resolveThreadFlag,
9
- resolveChannelResetConfig,
10
- updateLastRoute,
11
- } from "openclaw/plugin-sdk/session-store-runtime";
12
- export {
13
- getRuntimeConfig,
14
- getRuntimeConfigSourceSnapshot,
15
- } from "openclaw/plugin-sdk/runtime-config-snapshot";
16
- export { resolveChannelContextVisibilityMode } from "openclaw/plugin-sdk/context-visibility-runtime";
@@ -1 +0,0 @@
1
- export const DEFAULT_WEB_MEDIA_BYTES = 5 * 1024 * 1024;