@openclaw/whatsapp 2026.5.2 → 2026.5.3-beta.2

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/send.ts DELETED
@@ -1,292 +0,0 @@
1
- import { formatCliCommand } from "openclaw/plugin-sdk/cli-runtime";
2
- import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
3
- import { generateSecureUuid } from "openclaw/plugin-sdk/core";
4
- import { redactIdentifier } from "openclaw/plugin-sdk/logging-core";
5
- import {
6
- convertMarkdownTables,
7
- resolveMarkdownTableMode,
8
- } from "openclaw/plugin-sdk/markdown-table-runtime";
9
- import { requireRuntimeConfig } from "openclaw/plugin-sdk/plugin-config-runtime";
10
- import { normalizePollInput, type PollInput } from "openclaw/plugin-sdk/poll-runtime";
11
- import { createSubsystemLogger, getChildLogger } from "openclaw/plugin-sdk/runtime-env";
12
- import {
13
- resolveDefaultWhatsAppAccountId,
14
- resolveWhatsAppAccount,
15
- resolveWhatsAppMediaMaxBytes,
16
- } from "./accounts.js";
17
- import { getRegisteredWhatsAppConnectionController } from "./connection-controller-registry.js";
18
- import type { ActiveWebListener, ActiveWebSendOptions } from "./inbound/types.js";
19
- import { isWhatsAppNewsletterJid } from "./normalize.js";
20
- import {
21
- normalizeWhatsAppPayloadText,
22
- prepareWhatsAppOutboundMedia,
23
- resolveWhatsAppOutboundMediaUrls,
24
- } from "./outbound-media-contract.js";
25
- import { loadOutboundMediaFromUrl } from "./outbound-media.runtime.js";
26
- import { markdownToWhatsApp, toWhatsappJid } from "./text-runtime.js";
27
-
28
- const outboundLog = createSubsystemLogger("gateway/channels/whatsapp").child("outbound");
29
-
30
- function resolveOutboundWhatsAppAccountId(params: {
31
- cfg: OpenClawConfig;
32
- accountId?: string;
33
- }): string | undefined {
34
- const explicitAccountId = params.accountId?.trim();
35
- if (explicitAccountId) {
36
- return explicitAccountId;
37
- }
38
- return resolveDefaultWhatsAppAccountId(params.cfg);
39
- }
40
-
41
- function requireOutboundActiveWebListener(params: { cfg: OpenClawConfig; accountId?: string }): {
42
- accountId: string;
43
- listener: ActiveWebListener;
44
- } {
45
- const accountId = resolveOutboundWhatsAppAccountId(params);
46
- const resolvedAccountId = accountId ?? resolveDefaultWhatsAppAccountId(params.cfg);
47
- const listener =
48
- getRegisteredWhatsAppConnectionController(resolvedAccountId)?.getActiveListener() ?? null;
49
- if (!listener) {
50
- throw new Error(
51
- `No active WhatsApp Web listener (account: ${resolvedAccountId}). Start the gateway, then link WhatsApp with: ${formatCliCommand(`openclaw channels login --channel whatsapp --account ${resolvedAccountId}`)}.`,
52
- );
53
- }
54
- return { accountId: resolvedAccountId, listener };
55
- }
56
-
57
- export async function sendMessageWhatsApp(
58
- to: string,
59
- body: string,
60
- options: {
61
- verbose: boolean;
62
- cfg: OpenClawConfig;
63
- mediaUrl?: string;
64
- mediaUrls?: readonly string[];
65
- mediaAccess?: {
66
- localRoots?: readonly string[];
67
- readFile?: (filePath: string) => Promise<Buffer>;
68
- };
69
- mediaLocalRoots?: readonly string[];
70
- mediaReadFile?: (filePath: string) => Promise<Buffer>;
71
- gifPlayback?: boolean;
72
- audioAsVoice?: boolean;
73
- accountId?: string;
74
- quotedMessageKey?: {
75
- id: string;
76
- remoteJid: string;
77
- fromMe: boolean;
78
- participant?: string;
79
- messageText?: string;
80
- };
81
- preserveLeadingWhitespace?: boolean;
82
- },
83
- ): Promise<{ messageId: string; toJid: string }> {
84
- let text = options.preserveLeadingWhitespace ? body : normalizeWhatsAppPayloadText(body);
85
- const jid = toWhatsappJid(to);
86
- const mediaUrls = resolveWhatsAppOutboundMediaUrls(options);
87
- const primaryMediaUrl = mediaUrls[0];
88
- if (!text && !primaryMediaUrl) {
89
- return { messageId: "", toJid: jid };
90
- }
91
- const correlationId = generateSecureUuid();
92
- const startedAt = Date.now();
93
- const cfg = requireRuntimeConfig(options.cfg, "WhatsApp send");
94
- const { listener: active, accountId: resolvedAccountId } = requireOutboundActiveWebListener({
95
- cfg,
96
- accountId: options.accountId,
97
- });
98
- const account = resolveWhatsAppAccount({
99
- cfg,
100
- accountId: resolvedAccountId ?? options.accountId,
101
- });
102
- const tableMode = resolveMarkdownTableMode({
103
- cfg,
104
- channel: "whatsapp",
105
- accountId: resolvedAccountId ?? options.accountId,
106
- });
107
- text = convertMarkdownTables(text ?? "", tableMode);
108
- text = markdownToWhatsApp(text);
109
- const redactedTo = redactIdentifier(to);
110
- const logger = getChildLogger({
111
- module: "web-outbound",
112
- correlationId,
113
- to: redactedTo,
114
- });
115
- try {
116
- const redactedJid = redactIdentifier(jid);
117
- let mediaBuffer: Buffer | undefined;
118
- let mediaType: string | undefined;
119
- let documentFileName: string | undefined;
120
- let visibleTextAfterVoice: string | undefined;
121
- if (primaryMediaUrl) {
122
- const media = await prepareWhatsAppOutboundMedia(
123
- await loadOutboundMediaFromUrl(primaryMediaUrl, {
124
- maxBytes: resolveWhatsAppMediaMaxBytes(account),
125
- mediaAccess: options.mediaAccess,
126
- mediaLocalRoots: options.mediaLocalRoots,
127
- mediaReadFile: options.mediaReadFile,
128
- }),
129
- primaryMediaUrl,
130
- );
131
- const caption = text || undefined;
132
- mediaBuffer = media.buffer;
133
- mediaType = media.mimetype;
134
- if (media.kind === "audio" && caption) {
135
- visibleTextAfterVoice = caption;
136
- text = "";
137
- } else if (media.kind === "document") {
138
- text = caption ?? "";
139
- documentFileName = media.fileName;
140
- } else {
141
- text = caption ?? "";
142
- }
143
- }
144
- outboundLog.info(`Sending message -> ${redactedJid}${primaryMediaUrl ? " (media)" : ""}`);
145
- logger.info({ jid: redactedJid, hasMedia: Boolean(primaryMediaUrl) }, "sending message");
146
- if (!isWhatsAppNewsletterJid(jid)) {
147
- await active.sendComposingTo(to);
148
- }
149
- const hasExplicitAccountId = Boolean(options.accountId?.trim());
150
- const accountId = hasExplicitAccountId ? resolvedAccountId : undefined;
151
- const sendOptions: ActiveWebSendOptions | undefined =
152
- options.gifPlayback || accountId || documentFileName || options.quotedMessageKey
153
- ? {
154
- ...(options.gifPlayback ? { gifPlayback: true } : {}),
155
- ...(documentFileName ? { fileName: documentFileName } : {}),
156
- ...(options.quotedMessageKey ? { quotedMessageKey: options.quotedMessageKey } : {}),
157
- accountId,
158
- }
159
- : undefined;
160
- const result = sendOptions
161
- ? await active.sendMessage(to, text, mediaBuffer, mediaType, sendOptions)
162
- : await active.sendMessage(to, text, mediaBuffer, mediaType);
163
- if (visibleTextAfterVoice) {
164
- if (sendOptions) {
165
- await active.sendMessage(to, visibleTextAfterVoice, undefined, undefined, sendOptions);
166
- } else {
167
- await active.sendMessage(to, visibleTextAfterVoice, undefined, undefined);
168
- }
169
- }
170
- const messageId = (result as { messageId?: string })?.messageId ?? "unknown";
171
- const durationMs = Date.now() - startedAt;
172
- outboundLog.info(
173
- `Sent message ${messageId} -> ${redactedJid}${primaryMediaUrl ? " (media)" : ""} (${durationMs}ms)`,
174
- );
175
- logger.info({ jid: redactedJid, messageId }, "sent message");
176
- return { messageId, toJid: jid };
177
- } catch (err) {
178
- logger.error(
179
- { err: String(err), to: redactedTo, hasMedia: Boolean(primaryMediaUrl) },
180
- "failed to send via web session",
181
- );
182
- throw err;
183
- }
184
- }
185
-
186
- export async function sendTypingWhatsApp(
187
- to: string,
188
- options: {
189
- cfg: OpenClawConfig;
190
- accountId?: string;
191
- },
192
- ): Promise<void> {
193
- const cfg = requireRuntimeConfig(options.cfg, "WhatsApp typing send");
194
- const { listener: active } = requireOutboundActiveWebListener({
195
- cfg,
196
- accountId: options.accountId,
197
- });
198
- if (!isWhatsAppNewsletterJid(toWhatsappJid(to))) {
199
- await active.sendComposingTo(to);
200
- }
201
- }
202
-
203
- export async function sendReactionWhatsApp(
204
- chatJid: string,
205
- messageId: string,
206
- emoji: string,
207
- options: {
208
- verbose: boolean;
209
- fromMe?: boolean;
210
- participant?: string;
211
- accountId?: string;
212
- cfg: OpenClawConfig;
213
- },
214
- ): Promise<void> {
215
- const correlationId = generateSecureUuid();
216
- const cfg = requireRuntimeConfig(options.cfg, "WhatsApp reaction");
217
- const { listener: active } = requireOutboundActiveWebListener({
218
- cfg,
219
- accountId: options.accountId,
220
- });
221
- const redactedChatJid = redactIdentifier(chatJid);
222
- const logger = getChildLogger({
223
- module: "web-outbound",
224
- correlationId,
225
- chatJid: redactedChatJid,
226
- messageId,
227
- });
228
- try {
229
- const jid = toWhatsappJid(chatJid);
230
- const redactedJid = redactIdentifier(jid);
231
- outboundLog.info(`Sending reaction "${emoji}" -> message ${messageId}`);
232
- logger.info({ chatJid: redactedJid, messageId, emoji }, "sending reaction");
233
- await active.sendReaction(
234
- chatJid,
235
- messageId,
236
- emoji,
237
- options.fromMe ?? false,
238
- options.participant,
239
- );
240
- outboundLog.info(`Sent reaction "${emoji}" -> message ${messageId}`);
241
- logger.info({ chatJid: redactedJid, messageId, emoji }, "sent reaction");
242
- } catch (err) {
243
- logger.error(
244
- { err: String(err), chatJid: redactedChatJid, messageId, emoji },
245
- "failed to send reaction via web session",
246
- );
247
- throw err;
248
- }
249
- }
250
-
251
- export async function sendPollWhatsApp(
252
- to: string,
253
- poll: PollInput,
254
- options: { verbose: boolean; accountId?: string; cfg: OpenClawConfig },
255
- ): Promise<{ messageId: string; toJid: string }> {
256
- const correlationId = generateSecureUuid();
257
- const startedAt = Date.now();
258
- const cfg = requireRuntimeConfig(options.cfg, "WhatsApp poll");
259
- const { listener: active } = requireOutboundActiveWebListener({
260
- cfg,
261
- accountId: options.accountId,
262
- });
263
- const redactedTo = redactIdentifier(to);
264
- const logger = getChildLogger({
265
- module: "web-outbound",
266
- correlationId,
267
- to: redactedTo,
268
- });
269
- try {
270
- const jid = toWhatsappJid(to);
271
- const redactedJid = redactIdentifier(jid);
272
- const normalized = normalizePollInput(poll, { maxOptions: 12 });
273
- outboundLog.info(`Sending poll -> ${redactedJid}`);
274
- logger.info(
275
- {
276
- jid: redactedJid,
277
- optionCount: normalized.options.length,
278
- maxSelections: normalized.maxSelections,
279
- },
280
- "sending poll",
281
- );
282
- const result = await active.sendPoll(to, normalized);
283
- const messageId = (result as { messageId?: string })?.messageId ?? "unknown";
284
- const durationMs = Date.now() - startedAt;
285
- outboundLog.info(`Sent poll ${messageId} -> ${redactedJid} (${durationMs}ms)`);
286
- logger.info({ jid: redactedJid, messageId }, "sent poll");
287
- return { messageId, toJid: jid };
288
- } catch (err) {
289
- logger.error({ err: String(err), to: redactedTo }, "failed to send poll via web session");
290
- throw err;
291
- }
292
- }
@@ -1,31 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- canonicalizeLegacySessionKey,
4
- deriveLegacySessionChatType,
5
- isLegacyGroupSessionKey,
6
- } from "./session-contract.js";
7
-
8
- describe("whatsapp legacy session contract", () => {
9
- it("canonicalizes legacy WhatsApp group keys to channel-qualified agent keys", () => {
10
- expect(canonicalizeLegacySessionKey({ key: "group:123@g.us", agentId: "main" })).toBe(
11
- "agent:main:whatsapp:group:123@g.us",
12
- );
13
- expect(canonicalizeLegacySessionKey({ key: "123@g.us", agentId: "main" })).toBe(
14
- "agent:main:whatsapp:group:123@g.us",
15
- );
16
- expect(canonicalizeLegacySessionKey({ key: "whatsapp:123@g.us", agentId: "main" })).toBe(
17
- "agent:main:whatsapp:group:123@g.us",
18
- );
19
- });
20
-
21
- it("does not claim generic non-WhatsApp group keys", () => {
22
- expect(isLegacyGroupSessionKey("group:abc")).toBe(false);
23
- expect(deriveLegacySessionChatType("group:abc")).toBeUndefined();
24
- expect(canonicalizeLegacySessionKey({ key: "group:abc", agentId: "main" })).toBeNull();
25
- });
26
-
27
- it("derives chat type for legacy WhatsApp group keys", () => {
28
- expect(deriveLegacySessionChatType("123@g.us")).toBe("group");
29
- expect(deriveLegacySessionChatType("whatsapp:123@g.us")).toBe("group");
30
- });
31
- });
@@ -1,43 +0,0 @@
1
- import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
2
-
3
- function extractLegacyWhatsAppGroupId(key: string): string | null {
4
- const trimmed = key.trim();
5
- if (!trimmed) {
6
- return null;
7
- }
8
- const lower = normalizeLowercaseStringOrEmpty(trimmed);
9
- if (trimmed.startsWith("group:")) {
10
- const id = trimmed.slice("group:".length).trim();
11
- return normalizeLowercaseStringOrEmpty(id).includes("@g.us") ? id : null;
12
- }
13
- if (!lower.includes("@g.us")) {
14
- return null;
15
- }
16
- if (!trimmed.includes(":")) {
17
- return trimmed;
18
- }
19
- if (lower.startsWith("whatsapp:") && !trimmed.includes(":group:")) {
20
- const remainder = trimmed.slice("whatsapp:".length).trim();
21
- const cleaned = remainder.replace(/^group:/i, "").trim();
22
- return cleaned || null;
23
- }
24
- return null;
25
- }
26
-
27
- export function isLegacyGroupSessionKey(key: string): boolean {
28
- return extractLegacyWhatsAppGroupId(key) !== null;
29
- }
30
-
31
- export function deriveLegacySessionChatType(key: string): "group" | undefined {
32
- return isLegacyGroupSessionKey(key) ? "group" : undefined;
33
- }
34
-
35
- export function canonicalizeLegacySessionKey(params: {
36
- key: string;
37
- agentId: string;
38
- }): string | null {
39
- const legacyGroupId = extractLegacyWhatsAppGroupId(params.key);
40
- return legacyGroupId
41
- ? `agent:${normalizeLowercaseStringOrEmpty(params.agentId)}:whatsapp:group:${normalizeLowercaseStringOrEmpty(legacyGroupId)}`
42
- : null;
43
- }
@@ -1,125 +0,0 @@
1
- function safeStringify(value: unknown, limit = 800): string {
2
- try {
3
- const seen = new WeakSet();
4
- const raw = JSON.stringify(
5
- value,
6
- (_key, v) => {
7
- if (typeof v === "bigint") {
8
- return v.toString();
9
- }
10
- if (typeof v === "function") {
11
- const maybeName = (v as { name?: unknown }).name;
12
- const name =
13
- typeof maybeName === "string" && maybeName.length > 0 ? maybeName : "anonymous";
14
- return `[Function ${name}]`;
15
- }
16
- if (typeof v === "object" && v) {
17
- if (seen.has(v)) {
18
- return "[Circular]";
19
- }
20
- seen.add(v);
21
- }
22
- return v;
23
- },
24
- 2,
25
- );
26
- if (!raw) {
27
- return String(value);
28
- }
29
- return raw.length > limit ? `${raw.slice(0, limit)}…` : raw;
30
- } catch {
31
- return String(value);
32
- }
33
- }
34
-
35
- function extractBoomDetails(err: unknown): {
36
- statusCode?: number;
37
- error?: string;
38
- message?: string;
39
- } | null {
40
- if (!err || typeof err !== "object") {
41
- return null;
42
- }
43
- const output = (err as { output?: unknown })?.output as
44
- | { statusCode?: unknown; payload?: unknown }
45
- | undefined;
46
- if (!output || typeof output !== "object") {
47
- return null;
48
- }
49
- const payload = (output as { payload?: unknown }).payload as
50
- | { error?: unknown; message?: unknown; statusCode?: unknown }
51
- | undefined;
52
- const statusCode =
53
- typeof (output as { statusCode?: unknown }).statusCode === "number"
54
- ? ((output as { statusCode?: unknown }).statusCode as number)
55
- : typeof payload?.statusCode === "number"
56
- ? payload.statusCode
57
- : undefined;
58
- const error = typeof payload?.error === "string" ? payload.error : undefined;
59
- const message = typeof payload?.message === "string" ? payload.message : undefined;
60
- if (!statusCode && !error && !message) {
61
- return null;
62
- }
63
- return { statusCode, error, message };
64
- }
65
-
66
- export function getStatusCode(err: unknown) {
67
- return (
68
- (err as { output?: { statusCode?: number } })?.output?.statusCode ??
69
- (err as { status?: number })?.status ??
70
- (err as { error?: { output?: { statusCode?: number } } })?.error?.output?.statusCode
71
- );
72
- }
73
-
74
- export function formatError(err: unknown): string {
75
- if (err instanceof Error) {
76
- return err.message;
77
- }
78
- if (typeof err === "string") {
79
- return err;
80
- }
81
- if (!err || typeof err !== "object") {
82
- return String(err);
83
- }
84
-
85
- const boom =
86
- extractBoomDetails(err) ??
87
- extractBoomDetails((err as { error?: unknown })?.error) ??
88
- extractBoomDetails((err as { lastDisconnect?: { error?: unknown } })?.lastDisconnect?.error);
89
-
90
- const status = boom?.statusCode ?? getStatusCode(err);
91
- const code = (err as { code?: unknown })?.code;
92
- const codeText = typeof code === "string" || typeof code === "number" ? String(code) : undefined;
93
-
94
- const messageCandidates = [
95
- boom?.message,
96
- typeof (err as { message?: unknown })?.message === "string"
97
- ? ((err as { message?: unknown }).message as string)
98
- : undefined,
99
- typeof (err as { error?: { message?: unknown } })?.error?.message === "string"
100
- ? ((err as { error?: { message?: unknown } }).error?.message as string)
101
- : undefined,
102
- ];
103
- const message = messageCandidates.find((value): value is string =>
104
- Boolean(value && value.trim().length > 0),
105
- );
106
-
107
- const pieces: string[] = [];
108
- if (typeof status === "number") {
109
- pieces.push(`status=${status}`);
110
- }
111
- if (boom?.error) {
112
- pieces.push(boom.error);
113
- }
114
- if (message) {
115
- pieces.push(message);
116
- }
117
- if (codeText) {
118
- pieces.push(`code=${codeText}`);
119
- }
120
-
121
- if (pieces.length > 0) {
122
- return pieces.join(" ");
123
- }
124
- return safeStringify(err);
125
- }
@@ -1,41 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { resolveWhatsAppOutboundSessionRoute } from "./session-route.js";
3
-
4
- describe("resolveWhatsAppOutboundSessionRoute", () => {
5
- it("routes newsletter JIDs as channel sessions", () => {
6
- const route = resolveWhatsAppOutboundSessionRoute({
7
- cfg: {},
8
- agentId: "main",
9
- target: "120363401234567890@newsletter",
10
- });
11
-
12
- expect(route).toMatchObject({
13
- sessionKey: "agent:main:whatsapp:channel:120363401234567890@newsletter",
14
- baseSessionKey: "agent:main:whatsapp:channel:120363401234567890@newsletter",
15
- peer: {
16
- kind: "channel",
17
- id: "120363401234567890@newsletter",
18
- },
19
- chatType: "channel",
20
- from: "120363401234567890@newsletter",
21
- to: "120363401234567890@newsletter",
22
- });
23
- });
24
-
25
- it("keeps direct user targets on direct session semantics", () => {
26
- const route = resolveWhatsAppOutboundSessionRoute({
27
- cfg: { session: { dmScope: "per-channel-peer" } },
28
- agentId: "main",
29
- target: "+15551234567",
30
- });
31
-
32
- expect(route).toMatchObject({
33
- sessionKey: "agent:main:whatsapp:direct:+15551234567",
34
- peer: {
35
- kind: "direct",
36
- id: "+15551234567",
37
- },
38
- chatType: "direct",
39
- });
40
- });
41
- });
@@ -1,32 +0,0 @@
1
- import {
2
- buildChannelOutboundSessionRoute,
3
- type ChannelOutboundSessionRouteParams,
4
- } from "openclaw/plugin-sdk/core";
5
- import {
6
- isWhatsAppGroupJid,
7
- isWhatsAppNewsletterJid,
8
- normalizeWhatsAppTarget,
9
- } from "./normalize.js";
10
-
11
- export function resolveWhatsAppOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
12
- const normalized = normalizeWhatsAppTarget(params.target);
13
- if (!normalized) {
14
- return null;
15
- }
16
- const isGroup = isWhatsAppGroupJid(normalized);
17
- const isNewsletter = isWhatsAppNewsletterJid(normalized);
18
- const chatType = isGroup ? "group" : isNewsletter ? "channel" : "direct";
19
- return buildChannelOutboundSessionRoute({
20
- cfg: params.cfg,
21
- agentId: params.agentId,
22
- channel: "whatsapp",
23
- accountId: params.accountId,
24
- peer: {
25
- kind: chatType,
26
- id: normalized,
27
- },
28
- chatType,
29
- from: normalized,
30
- to: normalized,
31
- });
32
- }
@@ -1,8 +0,0 @@
1
- export {
2
- BufferJSON,
3
- DisconnectReason,
4
- fetchLatestBaileysVersion,
5
- makeCacheableSignalKeyStore,
6
- makeWASocket,
7
- useMultiFileAuthState,
8
- } from "@whiskeysockets/baileys";