cclawd 1.0.4 → 1.0.6

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 (123) hide show
  1. package/dist/{active-listener-BLd27Pxd.js → active-listener-Dkhmfuwx.js} +1 -1
  2. package/dist/{api-key-rotation-Dg3JlNDQ.js → api-key-rotation-BOfI3cG3.js} +1 -1
  3. package/dist/{audio-preflight-eV5m9mMp.js → audio-preflight-CtkZ5SAs.js} +6 -6
  4. package/dist/{audio-transcription-runner-CQU4Eg1M.js → audio-transcription-runner-CbPqoiHX.js} +6 -6
  5. package/dist/build-info.json +3 -3
  6. package/dist/bundled/boot-md/handler.js +23 -23
  7. package/dist/bundled/session-memory/handler.js +23 -23
  8. package/dist/{channel-activity-dT3cYb0e.js → channel-activity-DWAER4wd.js} +1 -1
  9. package/dist/{commands-registry-CyLMCPuP.js → commands-registry-BUyiA7nE.js} +1 -1
  10. package/dist/compact.runtime-DpcZpcTl.js +39 -0
  11. package/dist/{deliver-B6eTtXSk.js → deliver-aHOaRbkt.js} +4 -4
  12. package/dist/{deliver-runtime-CLDpY6AW.js → deliver-runtime-D4bCsr6d.js} +7 -7
  13. package/dist/{deps-send-discord.runtime-CxADlame.js → deps-send-discord.runtime-BziKU-pE.js} +7 -7
  14. package/dist/{deps-send-imessage.runtime-Wi79xm6H.js → deps-send-imessage.runtime-CFRnDTqp.js} +7 -7
  15. package/dist/{deps-send-signal.runtime-BDtzvsnR.js → deps-send-signal.runtime-BuOtABJm.js} +6 -6
  16. package/dist/{deps-send-slack.runtime-CgX24hgT.js → deps-send-slack.runtime-BOLqvMxW.js} +5 -5
  17. package/dist/{deps-send-telegram.runtime-CEWc7ePn.js → deps-send-telegram.runtime-DeEoFLv5.js} +6 -6
  18. package/dist/deps-send-whatsapp.runtime-CG1uXYLY.js +43 -0
  19. package/dist/{diagnostic-BZmAxdu9.js → diagnostic-BdcXX9iJ.js} +1 -1
  20. package/dist/{fetch-CMLoICyN.js → fetch-D9NUULbj.js} +2 -2
  21. package/dist/{fetch-guard-DCj3k042.js → fetch-guard-B5ZMnGaN.js} +1 -1
  22. package/dist/{image-Bt49ybRv.js → image-4x07m4Jl.js} +2 -2
  23. package/dist/{image-runtime-Cilhq73U.js → image-runtime-smkMrIol.js} +2 -2
  24. package/dist/{ir-CVtBjUiL.js → ir-CsgNUpOU.js} +2 -2
  25. package/dist/llm-slug-generator.js +23 -23
  26. package/dist/{login-D0fUoX-p.js → login-p_O59TVQ.js} +2 -2
  27. package/dist/{login-qr-ClBxstxZ.js → login-qr-BCJpDsAy.js} +2 -2
  28. package/dist/{manager-DSfEj66R.js → manager-CwYv8O3T.js} +3 -3
  29. package/dist/{manager-runtime-BrZlGJsj.js → manager-runtime-D_jEoBr9.js} +4 -4
  30. package/dist/{model-selection-CMEj8bpy.js → model-selection-Cv2Puf5z.js} +11 -11
  31. package/dist/{outbound-BxIJyMzV.js → outbound-Chpiwybe.js} +4 -4
  32. package/dist/{outbound-attachment-CVJwpypG.js → outbound-attachment-BnAVJDLe.js} +2 -2
  33. package/dist/{pi-embedded-CHNPEUAv.js → pi-embedded-CJVNBk0y.js} +57 -57
  34. package/dist/{pi-model-discovery-D-r5y7kV.js → pi-model-discovery-7IzK0Uc3.js} +1 -1
  35. package/dist/{pi-model-discovery-runtime-DZQXYmdu.js → pi-model-discovery-runtime-DABef3qy.js} +2 -2
  36. package/dist/{pi-tools.before-tool-call.runtime-DagGpfw0.js → pi-tools.before-tool-call.runtime-BP2UvGJb.js} +2 -2
  37. package/dist/plugin-sdk/index.js +35 -35
  38. package/dist/plugin-sdk/signal.js +2 -2
  39. package/dist/{pw-ai-CoIUdns_.js → pw-ai-DwH5GpEO.js} +1 -1
  40. package/dist/{runtime-whatsapp-login.runtime-ChqE9BkX.js → runtime-whatsapp-login.runtime-BI3U306v.js} +3 -3
  41. package/dist/{runtime-whatsapp-outbound.runtime-yiy6jzKk.js → runtime-whatsapp-outbound.runtime-Bsc2uD09.js} +6 -6
  42. package/dist/{send-V1MRV7QF.js → send-BDnOgWIp.js} +3 -3
  43. package/dist/{send-EDBPXjTT.js → send-C-Q_WPMf.js} +3 -3
  44. package/dist/{send-K2mAG7KC.js → send-DUibfNQD.js} +4 -4
  45. package/dist/{send-4rRrSKp9.js → send-DtBvCnPQ.js} +4 -4
  46. package/dist/{send-BKO1-P1t.js → send-ORtn50qg.js} +3 -3
  47. package/dist/{session-CuVCho2m.js → session-B7imi6T5.js} +1 -1
  48. package/dist/{skill-commands-B55LOaMB.js → skill-commands-B9brPuiL.js} +2 -2
  49. package/dist/{slash-commands.runtime-BchS0VkW.js → slash-commands.runtime-Cf6ygfBp.js} +2 -2
  50. package/dist/{slash-dispatch.runtime-BIKRY3fr.js → slash-dispatch.runtime-CsmvhO5K.js} +23 -23
  51. package/dist/{slash-skill-commands.runtime-BP4jBHU9.js → slash-skill-commands.runtime-CX7stIEP.js} +3 -3
  52. package/dist/{subagent-registry-runtime-DjEYzSyM.js → subagent-registry-runtime-B_S1nf7y.js} +23 -23
  53. package/dist/{tables-BAGqh2XD.js → tables-CjQqTOdD.js} +1 -1
  54. package/dist/{target-errors-CeBF8Pws.js → target-errors-BZE1mc-W.js} +1 -1
  55. package/dist/{web-BRSmQdtm.js → web-Cd8yK1Zq.js} +27 -27
  56. package/dist/{whatsapp-actions-Dxb2K2Xh.js → whatsapp-actions-CYEzUMBI.js} +7 -7
  57. package/extensions/mfa-auth/index.ts +20 -39
  58. package/extensions/mfa-auth/src/auth-manager.ts +0 -4
  59. package/extensions/mfa-auth/src/notification-service.ts +3 -7
  60. package/package.json +1 -1
  61. package/dist/compact.runtime-DGRl4st4.js +0 -39
  62. package/dist/deps-send-whatsapp.runtime-B1KJ7YOp.js +0 -43
  63. package/dist/plugin-sdk/active-listener-CN-tMEvN.js +0 -35
  64. package/dist/plugin-sdk/api-key-rotation-CimGYMBc.js +0 -176
  65. package/dist/plugin-sdk/audio-preflight-C-xXBoE2.js +0 -51
  66. package/dist/plugin-sdk/audio-transcription-runner-CTIHpebA.js +0 -2173
  67. package/dist/plugin-sdk/audit-membership-runtime-BFatB2LJ.js +0 -58
  68. package/dist/plugin-sdk/channel-activity-DO0FEzyj.js +0 -95
  69. package/dist/plugin-sdk/channel-web-Da-__nUF.js +0 -2238
  70. package/dist/plugin-sdk/commands-registry-6no2NNrY.js +0 -1118
  71. package/dist/plugin-sdk/compact.runtime-CCoclu5e.js +0 -35
  72. package/dist/plugin-sdk/config-B9ODwgpz.js +0 -37426
  73. package/dist/plugin-sdk/deliver-B1fFpKjV.js +0 -1757
  74. package/dist/plugin-sdk/deliver-runtime-DB-VRMe1.js +0 -15
  75. package/dist/plugin-sdk/deps-send-discord.runtime-DklqycYG.js +0 -15
  76. package/dist/plugin-sdk/deps-send-imessage.runtime-Chs8zeon.js +0 -14
  77. package/dist/plugin-sdk/deps-send-signal.runtime-clW9aSJP.js +0 -13
  78. package/dist/plugin-sdk/deps-send-slack.runtime-BUx0LYY1.js +0 -13
  79. package/dist/plugin-sdk/deps-send-telegram.runtime-LECSHgMG.js +0 -16
  80. package/dist/plugin-sdk/deps-send-whatsapp.runtime-D2d65fw0.js +0 -40
  81. package/dist/plugin-sdk/diagnostic-CxIvS-C2.js +0 -315
  82. package/dist/plugin-sdk/dispatch-BqlR4dPx.js +0 -105863
  83. package/dist/plugin-sdk/env-b9k1PHMI.js +0 -34
  84. package/dist/plugin-sdk/fetch-PoxzAANT.js +0 -326
  85. package/dist/plugin-sdk/fetch-guard-4UVSZ0uS.js +0 -164
  86. package/dist/plugin-sdk/image-Ch6M4tnJ.js +0 -2420
  87. package/dist/plugin-sdk/image-runtime-CSh2o5wY.js +0 -8
  88. package/dist/plugin-sdk/ir-CugsqGH8.js +0 -1312
  89. package/dist/plugin-sdk/local-roots-adnEg9zb.js +0 -217
  90. package/dist/plugin-sdk/logger-D6zRubj0.js +0 -1164
  91. package/dist/plugin-sdk/login-CYvkQ0At.js +0 -54
  92. package/dist/plugin-sdk/login-qr-ll4NtaT5.js +0 -316
  93. package/dist/plugin-sdk/manager-CHy8IclH.js +0 -3959
  94. package/dist/plugin-sdk/manager-runtime-C70EkEr7.js +0 -11
  95. package/dist/plugin-sdk/outbound-Wzs2iN7X.js +0 -216
  96. package/dist/plugin-sdk/outbound-attachment-khXJwucX.js +0 -17
  97. package/dist/plugin-sdk/paths-BtVqCdw4.js +0 -3063
  98. package/dist/plugin-sdk/pi-model-discovery-Dh4ziodY.js +0 -131
  99. package/dist/plugin-sdk/pi-model-discovery-runtime-b83Xe-HT.js +0 -8
  100. package/dist/plugin-sdk/pi-tools.before-tool-call.runtime-C1z5CDBF.js +0 -349
  101. package/dist/plugin-sdk/proxy-fetch-CJEmoBxi.js +0 -54
  102. package/dist/plugin-sdk/pw-ai-Dj3Cvlzl.js +0 -1990
  103. package/dist/plugin-sdk/qmd-manager-egHUAseQ.js +0 -1581
  104. package/dist/plugin-sdk/resolve-outbound-target-BiICvIKs.js +0 -38
  105. package/dist/plugin-sdk/runtime-whatsapp-login.runtime-DNApufzW.js +0 -9
  106. package/dist/plugin-sdk/runtime-whatsapp-outbound.runtime-CBmtfIQ8.js +0 -13
  107. package/dist/plugin-sdk/send-CScblaI4.js +0 -532
  108. package/dist/plugin-sdk/send-CeHhnld6.js +0 -407
  109. package/dist/plugin-sdk/send-DP_c8JfR.js +0 -3277
  110. package/dist/plugin-sdk/send-Dc5fI6e8.js +0 -495
  111. package/dist/plugin-sdk/send-l-77_s1_.js +0 -2507
  112. package/dist/plugin-sdk/session-CkOKZaqa.js +0 -166
  113. package/dist/plugin-sdk/skill-commands-BohYCgkq.js +0 -336
  114. package/dist/plugin-sdk/slash-commands.runtime-DpLfVTM6.js +0 -8
  115. package/dist/plugin-sdk/slash-dispatch.runtime-CASMHwpm.js +0 -35
  116. package/dist/plugin-sdk/slash-skill-commands.runtime-D7rrJEci.js +0 -9
  117. package/dist/plugin-sdk/sqlite-CJE3X7Mv.js +0 -1005
  118. package/dist/plugin-sdk/subagent-registry-runtime-B1oo5bih.js +0 -35
  119. package/dist/plugin-sdk/tables-D5VgpTmm.js +0 -53
  120. package/dist/plugin-sdk/target-errors-C6zZ_OpA.js +0 -191
  121. package/dist/plugin-sdk/tokens-DUnJnpMS.js +0 -50
  122. package/dist/plugin-sdk/web-TfUM1nSi.js +0 -39
  123. package/dist/plugin-sdk/whatsapp-actions-DuWJ0j1r.js +0 -71
@@ -1,2238 +0,0 @@
1
- import { Ct as DEFAULT_MAIN_KEY, Et as buildGroupHistoryKey, Ft as DEFAULT_ACCOUNT_ID, kt as normalizeAgentId, s as resolveStorePath, wt as buildAgentMainSessionKey } from "./paths-BtVqCdw4.js";
2
- import { Do as resolveChannelGroupPolicy, Ga as getWebAuthAgeMs, Nr as updateLastRoute, Oo as resolveChannelGroupRequireMention, Po as resolveGroupSessionKey, Qc as normalizeChatChannelId, Ua as resolveWhatsAppMediaMaxBytes, Ul as formatCliCommand, Va as resolveWhatsAppAccount, Za as readWebSelfId, jr as recordSessionMetaFromInbound, kr as loadSessionStore, r as loadConfig, ws as saveMediaBuffer } from "./config-B9ODwgpz.js";
3
- import { $ as hasControlCommand, Ct as deriveLastRoutePolicy, D as resolveMentionGating, E as createConnectedChannelStatusPatch, Et as finalizeInboundContext, Ft as formatInboundEnvelope, Gt as resolveDmGroupAccessWithLists, H as recordPendingHistoryEntryIfEnabled, I as buildHistoryContextFromEntries, J as shouldAckReactionForWhatsApp, Nt as issuePairingChallenge, Pt as resolveInboundSessionEnvelopeContext, Q as resolveInboundDebounceMs, St as buildAgentSessionKey, Tt as resolveInboundLastRouteSessionKey, Vt as readStoreAllowFromForDmPolicy, Wt as resolveDmGroupAccessWithCommandGate, Y as dispatchReplyWithBufferedBlockDispatcher, Z as createInboundDebouncer, _t as resolveIdentityNamePrefix, at as sleepWithAbort, ct as parseActivationCommand, et as shouldComputeCommandAuthorized, ht as enqueueSystemEvent, it as computeBackoff, j as createReplyPrefixOptions, mt as formatDurationPrecise, nt as normalizeMentionText, ot as createDedupeCache, qt as resolvePinnedMainDmOwnerFromAllowlist, r as getReplyFromConfig, st as normalizeGroupActivation, tt as buildMentionRegexes, vt as resolveMessagePrefix, wt as resolveAgentRoute } from "./dispatch-BqlR4dPx.js";
4
- import { B as shouldLogVerbose, C as normalizeE164, E as resolveJidToE164, K as getChildLogger, N as toWhatsappJid, R as logVerbose, S as jidToE164, a as createSubsystemLogger, j as sleep, l as defaultRuntime, m as clamp, x as isSelfChatMode } from "./logger-D6zRubj0.js";
5
- import { It as warnMissingProviderGroupPolicyFallbackOnce, Nt as resolveDefaultGroupPolicy, Pt as resolveOpenProviderRuntimeGroupPolicy } from "./send-DP_c8JfR.js";
6
- import { n as recordChannelActivity } from "./channel-activity-DO0FEzyj.js";
7
- import { t as getAgentScopedMediaLocalRoots } from "./local-roots-adnEg9zb.js";
8
- import { c as chunkMarkdownTextWithMode, d as resolveChunkMode, f as resolveTextChunkLimit, i as resolveMarkdownTableMode, v as loadWebMedia } from "./ir-CugsqGH8.js";
9
- import { t as convertMarkdownTables } from "./tables-D5VgpTmm.js";
10
- import { $ as readChannelAllowFromStoreSync, J as formatLocationText, Y as toLocationContext, tt as upsertChannelPairingRequest } from "./send-l-77_s1_.js";
11
- import { p as registerUnhandledRejectionHandler } from "./audio-transcription-runner-CTIHpebA.js";
12
- import { r as setActiveWebListener } from "./active-listener-CN-tMEvN.js";
13
- import { i as markdownToWhatsApp, r as sendReactionWhatsApp } from "./outbound-Wzs2iN7X.js";
14
- import { i as waitForWaConnection, n as formatError, r as getStatusCode, t as createWaSocket } from "./session-CkOKZaqa.js";
15
- import { randomUUID } from "node:crypto";
16
- import { DisconnectReason, downloadMediaMessage, extractMessageContent, getContentType, isJidGroup, normalizeMessageContent } from "@whiskeysockets/baileys";
17
- //#endregion
18
- //#region src/channels/plugins/whatsapp-heartbeat.ts
19
- function getSessionRecipients(cfg) {
20
- if ((cfg.session?.scope ?? "per-sender") === "global") return [];
21
- const store = loadSessionStore(resolveStorePath(cfg.session?.store));
22
- const isGroupKey = (key) => key.includes(":group:") || key.includes(":channel:") || key.includes("@g.us");
23
- const isCronKey = (key) => key.startsWith("cron:");
24
- const recipients = Object.entries(store).filter(([key]) => key !== "global" && key !== "unknown").filter(([key]) => !isGroupKey(key) && !isCronKey(key)).map(([_, entry]) => ({
25
- to: normalizeChatChannelId(entry?.lastChannel) === "whatsapp" && entry?.lastTo ? normalizeE164(entry.lastTo) : "",
26
- updatedAt: entry?.updatedAt ?? 0
27
- })).filter(({ to }) => to.length > 1).toSorted((a, b) => b.updatedAt - a.updatedAt);
28
- const seen = /* @__PURE__ */ new Set();
29
- return recipients.filter((r) => {
30
- if (seen.has(r.to)) return false;
31
- seen.add(r.to);
32
- return true;
33
- });
34
- }
35
- function resolveWhatsAppHeartbeatRecipients(cfg, opts = {}) {
36
- if (opts.to) return {
37
- recipients: [normalizeE164(opts.to)],
38
- source: "flag"
39
- };
40
- const sessionRecipients = getSessionRecipients(cfg);
41
- const configuredAllowFrom = Array.isArray(cfg.channels?.whatsapp?.allowFrom) && cfg.channels.whatsapp.allowFrom.length > 0 ? cfg.channels.whatsapp.allowFrom.filter((v) => v !== "*").map(normalizeE164) : [];
42
- const storeAllowFrom = readChannelAllowFromStoreSync("whatsapp", process.env, DEFAULT_ACCOUNT_ID).map(normalizeE164);
43
- const unique = (list) => [...new Set(list.filter(Boolean))];
44
- const allowFrom = unique([...configuredAllowFrom, ...storeAllowFrom]);
45
- if (opts.all) return {
46
- recipients: unique([...sessionRecipients.map((s) => s.to), ...allowFrom]),
47
- source: "all"
48
- };
49
- if (allowFrom.length > 0) {
50
- const allowSet = new Set(allowFrom);
51
- const authorizedSessionRecipients = sessionRecipients.map((entry) => entry.to).filter((recipient) => allowSet.has(recipient));
52
- if (authorizedSessionRecipients.length === 1) return {
53
- recipients: [authorizedSessionRecipients[0]],
54
- source: "session-single"
55
- };
56
- if (authorizedSessionRecipients.length > 1) return {
57
- recipients: authorizedSessionRecipients,
58
- source: "session-ambiguous"
59
- };
60
- return {
61
- recipients: allowFrom,
62
- source: "allowFrom"
63
- };
64
- }
65
- if (sessionRecipients.length === 1) return {
66
- recipients: [sessionRecipients[0].to],
67
- source: "session-single"
68
- };
69
- if (sessionRecipients.length > 1) return {
70
- recipients: sessionRecipients.map((s) => s.to),
71
- source: "session-ambiguous"
72
- };
73
- return {
74
- recipients: allowFrom,
75
- source: "allowFrom"
76
- };
77
- }
78
- const DEFAULT_RECONNECT_POLICY = {
79
- initialMs: 2e3,
80
- maxMs: 3e4,
81
- factor: 1.8,
82
- jitter: .25,
83
- maxAttempts: 12
84
- };
85
- function resolveHeartbeatSeconds(cfg, overrideSeconds) {
86
- const candidate = overrideSeconds ?? cfg.web?.heartbeatSeconds;
87
- if (typeof candidate === "number" && candidate > 0) return candidate;
88
- return 60;
89
- }
90
- function resolveReconnectPolicy(cfg, overrides) {
91
- const reconnectOverrides = cfg.web?.reconnect ?? {};
92
- const overrideConfig = overrides ?? {};
93
- const merged = {
94
- ...DEFAULT_RECONNECT_POLICY,
95
- ...reconnectOverrides,
96
- ...overrideConfig
97
- };
98
- merged.initialMs = Math.max(250, merged.initialMs);
99
- merged.maxMs = Math.max(merged.initialMs, merged.maxMs);
100
- merged.factor = clamp(merged.factor, 1.1, 10);
101
- merged.jitter = clamp(merged.jitter, 0, 1);
102
- merged.maxAttempts = Math.max(0, Math.floor(merged.maxAttempts));
103
- return merged;
104
- }
105
- function newConnectionId() {
106
- return randomUUID();
107
- }
108
- //#endregion
109
- //#region src/web/auto-reply/loggers.ts
110
- const whatsappLog = createSubsystemLogger("gateway/channels/whatsapp");
111
- const whatsappInboundLog = whatsappLog.child("inbound");
112
- const whatsappOutboundLog = whatsappLog.child("outbound");
113
- const whatsappHeartbeatLog = whatsappLog.child("heartbeat");
114
- //#endregion
115
- //#region src/cli/wait.ts
116
- function waitForever() {
117
- setInterval(() => {}, 1e6).unref();
118
- return new Promise(() => {});
119
- }
120
- //#endregion
121
- //#region src/web/inbound/dedupe.ts
122
- const recentInboundMessages = createDedupeCache({
123
- ttlMs: 20 * 6e4,
124
- maxSize: 5e3
125
- });
126
- function isRecentInboundMessage(key) {
127
- return recentInboundMessages.check(key);
128
- }
129
- //#endregion
130
- //#region src/web/vcard.ts
131
- const ALLOWED_VCARD_KEYS = new Set([
132
- "FN",
133
- "N",
134
- "TEL"
135
- ]);
136
- function parseVcard(vcard) {
137
- if (!vcard) return { phones: [] };
138
- const lines = vcard.split(/\r?\n/);
139
- let nameFromN;
140
- let nameFromFn;
141
- const phones = [];
142
- for (const rawLine of lines) {
143
- const line = rawLine.trim();
144
- if (!line) continue;
145
- const colonIndex = line.indexOf(":");
146
- if (colonIndex === -1) continue;
147
- const key = line.slice(0, colonIndex).toUpperCase();
148
- const rawValue = line.slice(colonIndex + 1).trim();
149
- if (!rawValue) continue;
150
- const baseKey = normalizeVcardKey(key);
151
- if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) continue;
152
- const value = cleanVcardValue(rawValue);
153
- if (!value) continue;
154
- if (baseKey === "FN" && !nameFromFn) {
155
- nameFromFn = normalizeVcardName(value);
156
- continue;
157
- }
158
- if (baseKey === "N" && !nameFromN) {
159
- nameFromN = normalizeVcardName(value);
160
- continue;
161
- }
162
- if (baseKey === "TEL") {
163
- const phone = normalizeVcardPhone(value);
164
- if (phone) phones.push(phone);
165
- }
166
- }
167
- return {
168
- name: nameFromFn ?? nameFromN,
169
- phones
170
- };
171
- }
172
- function normalizeVcardKey(key) {
173
- const [primary] = key.split(";");
174
- if (!primary) return;
175
- const segments = primary.split(".");
176
- return segments[segments.length - 1] || void 0;
177
- }
178
- function cleanVcardValue(value) {
179
- return value.replace(/\\n/gi, " ").replace(/\\,/g, ",").replace(/\\;/g, ";").trim();
180
- }
181
- function normalizeVcardName(value) {
182
- return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
183
- }
184
- function normalizeVcardPhone(value) {
185
- const trimmed = value.trim();
186
- if (!trimmed) return "";
187
- if (trimmed.toLowerCase().startsWith("tel:")) return trimmed.slice(4).trim();
188
- return trimmed;
189
- }
190
- //#endregion
191
- //#region src/web/inbound/extract.ts
192
- function unwrapMessage$1(message) {
193
- return normalizeMessageContent(message);
194
- }
195
- function extractContextInfo(message) {
196
- if (!message) return;
197
- const contentType = getContentType(message);
198
- const candidate = contentType ? message[contentType] : void 0;
199
- const contextInfo = candidate && typeof candidate === "object" && "contextInfo" in candidate ? candidate.contextInfo : void 0;
200
- if (contextInfo) return contextInfo;
201
- const fallback = message.extendedTextMessage?.contextInfo ?? message.imageMessage?.contextInfo ?? message.videoMessage?.contextInfo ?? message.documentMessage?.contextInfo ?? message.audioMessage?.contextInfo ?? message.stickerMessage?.contextInfo ?? message.buttonsResponseMessage?.contextInfo ?? message.listResponseMessage?.contextInfo ?? message.templateButtonReplyMessage?.contextInfo ?? message.interactiveResponseMessage?.contextInfo ?? message.buttonsMessage?.contextInfo ?? message.listMessage?.contextInfo;
202
- if (fallback) return fallback;
203
- for (const value of Object.values(message)) {
204
- if (!value || typeof value !== "object") continue;
205
- if (!("contextInfo" in value)) continue;
206
- const candidateContext = value.contextInfo;
207
- if (candidateContext) return candidateContext;
208
- }
209
- }
210
- function extractMentionedJids(rawMessage) {
211
- const message = unwrapMessage$1(rawMessage);
212
- if (!message) return;
213
- const flattened = [
214
- message.extendedTextMessage?.contextInfo?.mentionedJid,
215
- message.extendedTextMessage?.contextInfo?.quotedMessage?.extendedTextMessage?.contextInfo?.mentionedJid,
216
- message.imageMessage?.contextInfo?.mentionedJid,
217
- message.videoMessage?.contextInfo?.mentionedJid,
218
- message.documentMessage?.contextInfo?.mentionedJid,
219
- message.audioMessage?.contextInfo?.mentionedJid,
220
- message.stickerMessage?.contextInfo?.mentionedJid,
221
- message.buttonsResponseMessage?.contextInfo?.mentionedJid,
222
- message.listResponseMessage?.contextInfo?.mentionedJid
223
- ].flatMap((arr) => arr ?? []).filter(Boolean);
224
- if (flattened.length === 0) return;
225
- return Array.from(new Set(flattened));
226
- }
227
- function extractText(rawMessage) {
228
- const message = unwrapMessage$1(rawMessage);
229
- if (!message) return;
230
- const extracted = extractMessageContent(message);
231
- const candidates = [message, extracted && extracted !== message ? extracted : void 0];
232
- for (const candidate of candidates) {
233
- if (!candidate) continue;
234
- if (typeof candidate.conversation === "string" && candidate.conversation.trim()) return candidate.conversation.trim();
235
- const extended = candidate.extendedTextMessage?.text;
236
- if (extended?.trim()) return extended.trim();
237
- const caption = candidate.imageMessage?.caption ?? candidate.videoMessage?.caption ?? candidate.documentMessage?.caption;
238
- if (caption?.trim()) return caption.trim();
239
- }
240
- const contactPlaceholder = extractContactPlaceholder(message) ?? (extracted && extracted !== message ? extractContactPlaceholder(extracted) : void 0);
241
- if (contactPlaceholder) return contactPlaceholder;
242
- }
243
- function extractMediaPlaceholder(rawMessage) {
244
- const message = unwrapMessage$1(rawMessage);
245
- if (!message) return;
246
- if (message.imageMessage) return "<media:image>";
247
- if (message.videoMessage) return "<media:video>";
248
- if (message.audioMessage) return "<media:audio>";
249
- if (message.documentMessage) return "<media:document>";
250
- if (message.stickerMessage) return "<media:sticker>";
251
- }
252
- function extractContactPlaceholder(rawMessage) {
253
- const message = unwrapMessage$1(rawMessage);
254
- if (!message) return;
255
- const contact = message.contactMessage ?? void 0;
256
- if (contact) {
257
- const { name, phones } = describeContact({
258
- displayName: contact.displayName,
259
- vcard: contact.vcard
260
- });
261
- return formatContactPlaceholder(name, phones);
262
- }
263
- const contactsArray = message.contactsArrayMessage?.contacts ?? void 0;
264
- if (!contactsArray || contactsArray.length === 0) return;
265
- return formatContactsPlaceholder(contactsArray.map((entry) => describeContact({
266
- displayName: entry.displayName,
267
- vcard: entry.vcard
268
- })).map((entry) => formatContactLabel(entry.name, entry.phones)).filter((value) => Boolean(value)), contactsArray.length);
269
- }
270
- function describeContact(input) {
271
- const displayName = (input.displayName ?? "").trim();
272
- const parsed = parseVcard(input.vcard ?? void 0);
273
- return {
274
- name: displayName || parsed.name,
275
- phones: parsed.phones
276
- };
277
- }
278
- function formatContactPlaceholder(name, phones) {
279
- const label = formatContactLabel(name, phones);
280
- if (!label) return "<contact>";
281
- return `<contact: ${label}>`;
282
- }
283
- function formatContactsPlaceholder(labels, total) {
284
- const cleaned = labels.map((label) => label.trim()).filter(Boolean);
285
- if (cleaned.length === 0) return `<contacts: ${total} ${total === 1 ? "contact" : "contacts"}>`;
286
- const remaining = Math.max(total - cleaned.length, 0);
287
- const suffix = remaining > 0 ? ` +${remaining} more` : "";
288
- return `<contacts: ${cleaned.join(", ")}${suffix}>`;
289
- }
290
- function formatContactLabel(name, phones) {
291
- const parts = [name, formatPhoneList(phones)].filter((value) => Boolean(value));
292
- if (parts.length === 0) return;
293
- return parts.join(", ");
294
- }
295
- function formatPhoneList(phones) {
296
- const cleaned = phones?.map((phone) => phone.trim()).filter(Boolean) ?? [];
297
- if (cleaned.length === 0) return;
298
- const { shown, remaining } = summarizeList(cleaned, cleaned.length, 1);
299
- const [primary] = shown;
300
- if (!primary) return;
301
- if (remaining === 0) return primary;
302
- return `${primary} (+${remaining} more)`;
303
- }
304
- function summarizeList(values, total, maxShown) {
305
- const shown = values.slice(0, maxShown);
306
- return {
307
- shown,
308
- remaining: Math.max(total - shown.length, 0)
309
- };
310
- }
311
- function extractLocationData(rawMessage) {
312
- const message = unwrapMessage$1(rawMessage);
313
- if (!message) return null;
314
- const live = message.liveLocationMessage ?? void 0;
315
- if (live) {
316
- const latitudeRaw = live.degreesLatitude;
317
- const longitudeRaw = live.degreesLongitude;
318
- if (latitudeRaw != null && longitudeRaw != null) {
319
- const latitude = Number(latitudeRaw);
320
- const longitude = Number(longitudeRaw);
321
- if (Number.isFinite(latitude) && Number.isFinite(longitude)) return {
322
- latitude,
323
- longitude,
324
- accuracy: live.accuracyInMeters ?? void 0,
325
- caption: live.caption ?? void 0,
326
- source: "live",
327
- isLive: true
328
- };
329
- }
330
- }
331
- const location = message.locationMessage ?? void 0;
332
- if (location) {
333
- const latitudeRaw = location.degreesLatitude;
334
- const longitudeRaw = location.degreesLongitude;
335
- if (latitudeRaw != null && longitudeRaw != null) {
336
- const latitude = Number(latitudeRaw);
337
- const longitude = Number(longitudeRaw);
338
- if (Number.isFinite(latitude) && Number.isFinite(longitude)) {
339
- const isLive = Boolean(location.isLive);
340
- return {
341
- latitude,
342
- longitude,
343
- accuracy: location.accuracyInMeters ?? void 0,
344
- name: location.name ?? void 0,
345
- address: location.address ?? void 0,
346
- caption: location.comment ?? void 0,
347
- source: isLive ? "live" : location.name || location.address ? "place" : "pin",
348
- isLive
349
- };
350
- }
351
- }
352
- }
353
- return null;
354
- }
355
- function describeReplyContext(rawMessage) {
356
- const message = unwrapMessage$1(rawMessage);
357
- if (!message) return null;
358
- const contextInfo = extractContextInfo(message);
359
- const quoted = normalizeMessageContent(contextInfo?.quotedMessage);
360
- if (!quoted) return null;
361
- const location = extractLocationData(quoted);
362
- const locationText = location ? formatLocationText(location) : void 0;
363
- let body = [extractText(quoted), locationText].filter(Boolean).join("\n").trim();
364
- if (!body) body = extractMediaPlaceholder(quoted);
365
- if (!body) {
366
- const quotedType = quoted ? getContentType(quoted) : void 0;
367
- logVerbose(`Quoted message missing extractable body${quotedType ? ` (type ${quotedType})` : ""}`);
368
- return null;
369
- }
370
- const senderJid = contextInfo?.participant ?? void 0;
371
- const senderE164 = senderJid ? jidToE164(senderJid) ?? senderJid : void 0;
372
- const sender = senderE164 ?? "unknown sender";
373
- return {
374
- id: contextInfo?.stanzaId ? String(contextInfo.stanzaId) : void 0,
375
- body,
376
- sender,
377
- senderJid,
378
- senderE164
379
- };
380
- }
381
- //#endregion
382
- //#region src/web/inbound/access-control.ts
383
- const PAIRING_REPLY_HISTORY_GRACE_MS = 3e4;
384
- function resolveWhatsAppRuntimeGroupPolicy(params) {
385
- return resolveOpenProviderRuntimeGroupPolicy({
386
- providerConfigPresent: params.providerConfigPresent,
387
- groupPolicy: params.groupPolicy,
388
- defaultGroupPolicy: params.defaultGroupPolicy
389
- });
390
- }
391
- async function checkInboundAccessControl(params) {
392
- const cfg = loadConfig();
393
- const account = resolveWhatsAppAccount({
394
- cfg,
395
- accountId: params.accountId
396
- });
397
- const dmPolicy = account.dmPolicy ?? "pairing";
398
- const configuredAllowFrom = account.allowFrom ?? [];
399
- const storeAllowFrom = await readStoreAllowFromForDmPolicy({
400
- provider: "whatsapp",
401
- accountId: account.accountId,
402
- dmPolicy
403
- });
404
- const defaultAllowFrom = configuredAllowFrom.length === 0 && params.selfE164 ? [params.selfE164] : [];
405
- const dmAllowFrom = configuredAllowFrom.length > 0 ? configuredAllowFrom : defaultAllowFrom;
406
- const groupAllowFrom = account.groupAllowFrom ?? (configuredAllowFrom.length > 0 ? configuredAllowFrom : void 0);
407
- const isSamePhone = params.from === params.selfE164;
408
- const isSelfChat = account.selfChatMode ?? isSelfChatMode(params.selfE164, configuredAllowFrom);
409
- const pairingGraceMs = typeof params.pairingGraceMs === "number" && params.pairingGraceMs > 0 ? params.pairingGraceMs : PAIRING_REPLY_HISTORY_GRACE_MS;
410
- const suppressPairingReply = typeof params.connectedAtMs === "number" && typeof params.messageTimestampMs === "number" && params.messageTimestampMs < params.connectedAtMs - pairingGraceMs;
411
- const defaultGroupPolicy = resolveDefaultGroupPolicy(cfg);
412
- const { groupPolicy, providerMissingFallbackApplied } = resolveWhatsAppRuntimeGroupPolicy({
413
- providerConfigPresent: cfg.channels?.whatsapp !== void 0,
414
- groupPolicy: account.groupPolicy,
415
- defaultGroupPolicy
416
- });
417
- warnMissingProviderGroupPolicyFallbackOnce({
418
- providerMissingFallbackApplied,
419
- providerKey: "whatsapp",
420
- accountId: account.accountId,
421
- log: (message) => logVerbose(message)
422
- });
423
- const normalizedDmSender = normalizeE164(params.from);
424
- const normalizedGroupSender = typeof params.senderE164 === "string" ? normalizeE164(params.senderE164) : null;
425
- const access = resolveDmGroupAccessWithLists({
426
- isGroup: params.group,
427
- dmPolicy,
428
- groupPolicy,
429
- allowFrom: params.group ? configuredAllowFrom : dmAllowFrom,
430
- groupAllowFrom,
431
- storeAllowFrom,
432
- isSenderAllowed: (allowEntries) => {
433
- if (allowEntries.includes("*")) return true;
434
- const normalizedEntrySet = new Set(allowEntries.map((entry) => normalizeE164(String(entry))).filter((entry) => Boolean(entry)));
435
- if (!params.group && isSamePhone) return true;
436
- return params.group ? Boolean(normalizedGroupSender && normalizedEntrySet.has(normalizedGroupSender)) : normalizedEntrySet.has(normalizedDmSender);
437
- }
438
- });
439
- if (params.group && access.decision !== "allow") {
440
- if (access.reason === "groupPolicy=disabled") logVerbose("Blocked group message (groupPolicy: disabled)");
441
- else if (access.reason === "groupPolicy=allowlist (empty allowlist)") logVerbose("Blocked group message (groupPolicy: allowlist, no groupAllowFrom)");
442
- else logVerbose(`Blocked group message from ${params.senderE164 ?? "unknown sender"} (groupPolicy: allowlist)`);
443
- return {
444
- allowed: false,
445
- shouldMarkRead: false,
446
- isSelfChat,
447
- resolvedAccountId: account.accountId
448
- };
449
- }
450
- if (!params.group) {
451
- if (params.isFromMe && !isSamePhone) {
452
- logVerbose("Skipping outbound DM (fromMe); no pairing reply needed.");
453
- return {
454
- allowed: false,
455
- shouldMarkRead: false,
456
- isSelfChat,
457
- resolvedAccountId: account.accountId
458
- };
459
- }
460
- if (access.decision === "block" && access.reason === "dmPolicy=disabled") {
461
- logVerbose("Blocked dm (dmPolicy: disabled)");
462
- return {
463
- allowed: false,
464
- shouldMarkRead: false,
465
- isSelfChat,
466
- resolvedAccountId: account.accountId
467
- };
468
- }
469
- if (access.decision === "pairing" && !isSamePhone) {
470
- const candidate = params.from;
471
- if (suppressPairingReply) logVerbose(`Skipping pairing reply for historical DM from ${candidate}.`);
472
- else await issuePairingChallenge({
473
- channel: "whatsapp",
474
- senderId: candidate,
475
- senderIdLine: `Your WhatsApp phone number: ${candidate}`,
476
- meta: { name: (params.pushName ?? "").trim() || void 0 },
477
- upsertPairingRequest: async ({ id, meta }) => await upsertChannelPairingRequest({
478
- channel: "whatsapp",
479
- id,
480
- accountId: account.accountId,
481
- meta
482
- }),
483
- onCreated: () => {
484
- logVerbose(`whatsapp pairing request sender=${candidate} name=${params.pushName ?? "unknown"}`);
485
- },
486
- sendPairingReply: async (text) => {
487
- await params.sock.sendMessage(params.remoteJid, { text });
488
- },
489
- onReplyError: (err) => {
490
- logVerbose(`whatsapp pairing reply failed for ${candidate}: ${String(err)}`);
491
- }
492
- });
493
- return {
494
- allowed: false,
495
- shouldMarkRead: false,
496
- isSelfChat,
497
- resolvedAccountId: account.accountId
498
- };
499
- }
500
- if (access.decision !== "allow") {
501
- logVerbose(`Blocked unauthorized sender ${params.from} (dmPolicy=${dmPolicy})`);
502
- return {
503
- allowed: false,
504
- shouldMarkRead: false,
505
- isSelfChat,
506
- resolvedAccountId: account.accountId
507
- };
508
- }
509
- }
510
- return {
511
- allowed: true,
512
- shouldMarkRead: true,
513
- isSelfChat,
514
- resolvedAccountId: account.accountId
515
- };
516
- }
517
- //#endregion
518
- //#region src/web/inbound/media.ts
519
- function unwrapMessage(message) {
520
- return normalizeMessageContent(message);
521
- }
522
- /**
523
- * Resolve the MIME type for an inbound media message.
524
- * Falls back to WhatsApp's standard formats when Baileys omits the MIME.
525
- */
526
- function resolveMediaMimetype(message) {
527
- const explicit = message.imageMessage?.mimetype ?? message.videoMessage?.mimetype ?? message.documentMessage?.mimetype ?? message.audioMessage?.mimetype ?? message.stickerMessage?.mimetype ?? void 0;
528
- if (explicit) return explicit;
529
- if (message.audioMessage) return "audio/ogg; codecs=opus";
530
- if (message.imageMessage) return "image/jpeg";
531
- if (message.videoMessage) return "video/mp4";
532
- if (message.stickerMessage) return "image/webp";
533
- }
534
- async function downloadInboundMedia(msg, sock) {
535
- const message = unwrapMessage(msg.message);
536
- if (!message) return;
537
- const mimetype = resolveMediaMimetype(message);
538
- const fileName = message.documentMessage?.fileName ?? void 0;
539
- if (!message.imageMessage && !message.videoMessage && !message.documentMessage && !message.audioMessage && !message.stickerMessage) return;
540
- try {
541
- return {
542
- buffer: await downloadMediaMessage(msg, "buffer", {}, {
543
- reuploadRequest: sock.updateMediaMessage,
544
- logger: sock.logger
545
- }),
546
- mimetype,
547
- fileName
548
- };
549
- } catch (err) {
550
- logVerbose(`downloadMediaMessage failed: ${String(err)}`);
551
- return;
552
- }
553
- }
554
- //#endregion
555
- //#region src/web/inbound/send-api.ts
556
- function recordWhatsAppOutbound(accountId) {
557
- recordChannelActivity({
558
- channel: "whatsapp",
559
- accountId,
560
- direction: "outbound"
561
- });
562
- }
563
- function resolveOutboundMessageId(result) {
564
- return typeof result === "object" && result && "key" in result ? String(result.key?.id ?? "unknown") : "unknown";
565
- }
566
- function createWebSendApi(params) {
567
- return {
568
- sendMessage: async (to, text, mediaBuffer, mediaType, sendOptions) => {
569
- const jid = toWhatsappJid(to);
570
- let payload;
571
- if (mediaBuffer && mediaType) if (mediaType.startsWith("image/")) payload = {
572
- image: mediaBuffer,
573
- caption: text || void 0,
574
- mimetype: mediaType
575
- };
576
- else if (mediaType.startsWith("audio/")) payload = {
577
- audio: mediaBuffer,
578
- ptt: true,
579
- mimetype: mediaType
580
- };
581
- else if (mediaType.startsWith("video/")) {
582
- const gifPlayback = sendOptions?.gifPlayback;
583
- payload = {
584
- video: mediaBuffer,
585
- caption: text || void 0,
586
- mimetype: mediaType,
587
- ...gifPlayback ? { gifPlayback: true } : {}
588
- };
589
- } else payload = {
590
- document: mediaBuffer,
591
- fileName: sendOptions?.fileName?.trim() || "file",
592
- caption: text || void 0,
593
- mimetype: mediaType
594
- };
595
- else payload = { text };
596
- const result = await params.sock.sendMessage(jid, payload);
597
- recordWhatsAppOutbound(sendOptions?.accountId ?? params.defaultAccountId);
598
- return { messageId: resolveOutboundMessageId(result) };
599
- },
600
- sendPoll: async (to, poll) => {
601
- const jid = toWhatsappJid(to);
602
- const result = await params.sock.sendMessage(jid, { poll: {
603
- name: poll.question,
604
- values: poll.options,
605
- selectableCount: poll.maxSelections ?? 1
606
- } });
607
- recordWhatsAppOutbound(params.defaultAccountId);
608
- return { messageId: resolveOutboundMessageId(result) };
609
- },
610
- sendReaction: async (chatJid, messageId, emoji, fromMe, participant) => {
611
- const jid = toWhatsappJid(chatJid);
612
- await params.sock.sendMessage(jid, { react: {
613
- text: emoji,
614
- key: {
615
- remoteJid: jid,
616
- id: messageId,
617
- fromMe,
618
- participant: participant ? toWhatsappJid(participant) : void 0
619
- }
620
- } });
621
- },
622
- sendComposingTo: async (to) => {
623
- const jid = toWhatsappJid(to);
624
- await params.sock.sendPresenceUpdate("composing", jid);
625
- }
626
- };
627
- }
628
- //#endregion
629
- //#region src/web/inbound/monitor.ts
630
- async function monitorWebInbox(options) {
631
- const inboundLogger = getChildLogger({ module: "web-inbound" });
632
- const inboundConsoleLog = createSubsystemLogger("gateway/channels/whatsapp").child("inbound");
633
- const sock = await createWaSocket(false, options.verbose, { authDir: options.authDir });
634
- await waitForWaConnection(sock);
635
- const connectedAtMs = Date.now();
636
- let onCloseResolve = null;
637
- const onClose = new Promise((resolve) => {
638
- onCloseResolve = resolve;
639
- });
640
- const resolveClose = (reason) => {
641
- if (!onCloseResolve) return;
642
- const resolver = onCloseResolve;
643
- onCloseResolve = null;
644
- resolver(reason);
645
- };
646
- try {
647
- await sock.sendPresenceUpdate("available");
648
- if (shouldLogVerbose()) logVerbose("Sent global 'available' presence on connect");
649
- } catch (err) {
650
- logVerbose(`Failed to send 'available' presence on connect: ${String(err)}`);
651
- }
652
- const selfJid = sock.user?.id;
653
- const selfE164 = selfJid ? jidToE164(selfJid) : null;
654
- const debouncer = createInboundDebouncer({
655
- debounceMs: options.debounceMs ?? 0,
656
- buildKey: (msg) => {
657
- const senderKey = msg.chatType === "group" ? msg.senderJid ?? msg.senderE164 ?? msg.senderName ?? msg.from : msg.from;
658
- if (!senderKey) return null;
659
- const conversationKey = msg.chatType === "group" ? msg.chatId : msg.from;
660
- return `${msg.accountId}:${conversationKey}:${senderKey}`;
661
- },
662
- shouldDebounce: options.shouldDebounce,
663
- onFlush: async (entries) => {
664
- const last = entries.at(-1);
665
- if (!last) return;
666
- if (entries.length === 1) {
667
- await options.onMessage(last);
668
- return;
669
- }
670
- const mentioned = /* @__PURE__ */ new Set();
671
- for (const entry of entries) for (const jid of entry.mentionedJids ?? []) mentioned.add(jid);
672
- const combinedBody = entries.map((entry) => entry.body).filter(Boolean).join("\n");
673
- const combinedMessage = {
674
- ...last,
675
- body: combinedBody,
676
- mentionedJids: mentioned.size > 0 ? Array.from(mentioned) : void 0
677
- };
678
- await options.onMessage(combinedMessage);
679
- },
680
- onError: (err) => {
681
- inboundLogger.error({ error: String(err) }, "failed handling inbound web message");
682
- inboundConsoleLog.error(`Failed handling inbound web message: ${String(err)}`);
683
- }
684
- });
685
- const groupMetaCache = /* @__PURE__ */ new Map();
686
- const GROUP_META_TTL_MS = 300 * 1e3;
687
- const lidLookup = sock.signalRepository?.lidMapping;
688
- const resolveInboundJid = async (jid) => resolveJidToE164(jid, {
689
- authDir: options.authDir,
690
- lidLookup
691
- });
692
- const getGroupMeta = async (jid) => {
693
- const cached = groupMetaCache.get(jid);
694
- if (cached && cached.expires > Date.now()) return cached;
695
- try {
696
- const meta = await sock.groupMetadata(jid);
697
- const participants = (await Promise.all(meta.participants?.map(async (p) => {
698
- return await resolveInboundJid(p.id) ?? p.id;
699
- }) ?? [])).filter(Boolean) ?? [];
700
- const entry = {
701
- subject: meta.subject,
702
- participants,
703
- expires: Date.now() + GROUP_META_TTL_MS
704
- };
705
- groupMetaCache.set(jid, entry);
706
- return entry;
707
- } catch (err) {
708
- logVerbose(`Failed to fetch group metadata for ${jid}: ${String(err)}`);
709
- return { expires: Date.now() + GROUP_META_TTL_MS };
710
- }
711
- };
712
- const normalizeInboundMessage = async (msg) => {
713
- const id = msg.key?.id ?? void 0;
714
- const remoteJid = msg.key?.remoteJid;
715
- if (!remoteJid) return null;
716
- if (remoteJid.endsWith("@status") || remoteJid.endsWith("@broadcast")) return null;
717
- const group = isJidGroup(remoteJid) === true;
718
- if (id) {
719
- if (isRecentInboundMessage(`${options.accountId}:${remoteJid}:${id}`)) return null;
720
- }
721
- const participantJid = msg.key?.participant ?? void 0;
722
- const from = group ? remoteJid : await resolveInboundJid(remoteJid);
723
- if (!from) return null;
724
- const senderE164 = group ? participantJid ? await resolveInboundJid(participantJid) : null : from;
725
- let groupSubject;
726
- let groupParticipants;
727
- if (group) {
728
- const meta = await getGroupMeta(remoteJid);
729
- groupSubject = meta.subject;
730
- groupParticipants = meta.participants;
731
- }
732
- const messageTimestampMs = msg.messageTimestamp ? Number(msg.messageTimestamp) * 1e3 : void 0;
733
- const access = await checkInboundAccessControl({
734
- accountId: options.accountId,
735
- from,
736
- selfE164,
737
- senderE164,
738
- group,
739
- pushName: msg.pushName ?? void 0,
740
- isFromMe: Boolean(msg.key?.fromMe),
741
- messageTimestampMs,
742
- connectedAtMs,
743
- sock: { sendMessage: (jid, content) => sock.sendMessage(jid, content) },
744
- remoteJid
745
- });
746
- if (!access.allowed) return null;
747
- return {
748
- id,
749
- remoteJid,
750
- group,
751
- participantJid,
752
- from,
753
- senderE164,
754
- groupSubject,
755
- groupParticipants,
756
- messageTimestampMs,
757
- access
758
- };
759
- };
760
- const maybeMarkInboundAsRead = async (inbound) => {
761
- const { id, remoteJid, participantJid, access } = inbound;
762
- if (id && !access.isSelfChat && options.sendReadReceipts !== false) try {
763
- await sock.readMessages([{
764
- remoteJid,
765
- id,
766
- participant: participantJid,
767
- fromMe: false
768
- }]);
769
- if (shouldLogVerbose()) logVerbose(`Marked message ${id} as read for ${remoteJid}${participantJid ? ` (participant ${participantJid})` : ""}`);
770
- } catch (err) {
771
- logVerbose(`Failed to mark message ${id} read: ${String(err)}`);
772
- }
773
- else if (id && access.isSelfChat && shouldLogVerbose()) logVerbose(`Self-chat mode: skipping read receipt for ${id}`);
774
- };
775
- const enrichInboundMessage = async (msg) => {
776
- const location = extractLocationData(msg.message ?? void 0);
777
- const locationText = location ? formatLocationText(location) : void 0;
778
- let body = extractText(msg.message ?? void 0);
779
- if (locationText) body = [body, locationText].filter(Boolean).join("\n").trim();
780
- if (!body) {
781
- body = extractMediaPlaceholder(msg.message ?? void 0);
782
- if (!body) return null;
783
- }
784
- const replyContext = describeReplyContext(msg.message);
785
- let mediaPath;
786
- let mediaType;
787
- let mediaFileName;
788
- try {
789
- const inboundMedia = await downloadInboundMedia(msg, sock);
790
- if (inboundMedia) {
791
- const maxBytes = (typeof options.mediaMaxMb === "number" && options.mediaMaxMb > 0 ? options.mediaMaxMb : 50) * 1024 * 1024;
792
- mediaPath = (await saveMediaBuffer(inboundMedia.buffer, inboundMedia.mimetype, "inbound", maxBytes, inboundMedia.fileName)).path;
793
- mediaType = inboundMedia.mimetype;
794
- mediaFileName = inboundMedia.fileName;
795
- }
796
- } catch (err) {
797
- logVerbose(`Inbound media download failed: ${String(err)}`);
798
- }
799
- return {
800
- body,
801
- location: location ?? void 0,
802
- replyContext,
803
- mediaPath,
804
- mediaType,
805
- mediaFileName
806
- };
807
- };
808
- const enqueueInboundMessage = async (msg, inbound, enriched) => {
809
- const chatJid = inbound.remoteJid;
810
- const sendComposing = async () => {
811
- try {
812
- await sock.sendPresenceUpdate("composing", chatJid);
813
- } catch (err) {
814
- logVerbose(`Presence update failed: ${String(err)}`);
815
- }
816
- };
817
- const reply = async (text) => {
818
- await sock.sendMessage(chatJid, { text });
819
- };
820
- const sendMedia = async (payload) => {
821
- await sock.sendMessage(chatJid, payload);
822
- };
823
- const timestamp = inbound.messageTimestampMs;
824
- const mentionedJids = extractMentionedJids(msg.message);
825
- const senderName = msg.pushName ?? void 0;
826
- inboundLogger.info({
827
- from: inbound.from,
828
- to: selfE164 ?? "me",
829
- body: enriched.body,
830
- mediaPath: enriched.mediaPath,
831
- mediaType: enriched.mediaType,
832
- mediaFileName: enriched.mediaFileName,
833
- timestamp
834
- }, "inbound message");
835
- const inboundMessage = {
836
- id: inbound.id,
837
- from: inbound.from,
838
- conversationId: inbound.from,
839
- to: selfE164 ?? "me",
840
- accountId: inbound.access.resolvedAccountId,
841
- body: enriched.body,
842
- pushName: senderName,
843
- timestamp,
844
- chatType: inbound.group ? "group" : "direct",
845
- chatId: inbound.remoteJid,
846
- senderJid: inbound.participantJid,
847
- senderE164: inbound.senderE164 ?? void 0,
848
- senderName,
849
- replyToId: enriched.replyContext?.id,
850
- replyToBody: enriched.replyContext?.body,
851
- replyToSender: enriched.replyContext?.sender,
852
- replyToSenderJid: enriched.replyContext?.senderJid,
853
- replyToSenderE164: enriched.replyContext?.senderE164,
854
- groupSubject: inbound.groupSubject,
855
- groupParticipants: inbound.groupParticipants,
856
- mentionedJids: mentionedJids ?? void 0,
857
- selfJid,
858
- selfE164,
859
- fromMe: Boolean(msg.key?.fromMe),
860
- location: enriched.location ?? void 0,
861
- sendComposing,
862
- reply,
863
- sendMedia,
864
- mediaPath: enriched.mediaPath,
865
- mediaType: enriched.mediaType,
866
- mediaFileName: enriched.mediaFileName
867
- };
868
- try {
869
- Promise.resolve(debouncer.enqueue(inboundMessage)).catch((err) => {
870
- inboundLogger.error({ error: String(err) }, "failed handling inbound web message");
871
- inboundConsoleLog.error(`Failed handling inbound web message: ${String(err)}`);
872
- });
873
- } catch (err) {
874
- inboundLogger.error({ error: String(err) }, "failed handling inbound web message");
875
- inboundConsoleLog.error(`Failed handling inbound web message: ${String(err)}`);
876
- }
877
- };
878
- const handleMessagesUpsert = async (upsert) => {
879
- if (upsert.type !== "notify" && upsert.type !== "append") return;
880
- for (const msg of upsert.messages ?? []) {
881
- recordChannelActivity({
882
- channel: "whatsapp",
883
- accountId: options.accountId,
884
- direction: "inbound"
885
- });
886
- const inbound = await normalizeInboundMessage(msg);
887
- if (!inbound) continue;
888
- await maybeMarkInboundAsRead(inbound);
889
- if (upsert.type === "append") continue;
890
- const enriched = await enrichInboundMessage(msg);
891
- if (!enriched) continue;
892
- await enqueueInboundMessage(msg, inbound, enriched);
893
- }
894
- };
895
- sock.ev.on("messages.upsert", handleMessagesUpsert);
896
- const handleConnectionUpdate = (update) => {
897
- try {
898
- if (update.connection === "close") {
899
- const status = getStatusCode(update.lastDisconnect?.error);
900
- resolveClose({
901
- status,
902
- isLoggedOut: status === DisconnectReason.loggedOut,
903
- error: update.lastDisconnect?.error
904
- });
905
- }
906
- } catch (err) {
907
- inboundLogger.error({ error: String(err) }, "connection.update handler error");
908
- resolveClose({
909
- status: void 0,
910
- isLoggedOut: false,
911
- error: err
912
- });
913
- }
914
- };
915
- sock.ev.on("connection.update", handleConnectionUpdate);
916
- return {
917
- close: async () => {
918
- try {
919
- const ev = sock.ev;
920
- const messagesUpsertHandler = handleMessagesUpsert;
921
- const connectionUpdateHandler = handleConnectionUpdate;
922
- if (typeof ev.off === "function") {
923
- ev.off("messages.upsert", messagesUpsertHandler);
924
- ev.off("connection.update", connectionUpdateHandler);
925
- } else if (typeof ev.removeListener === "function") {
926
- ev.removeListener("messages.upsert", messagesUpsertHandler);
927
- ev.removeListener("connection.update", connectionUpdateHandler);
928
- }
929
- sock.ws?.close();
930
- } catch (err) {
931
- logVerbose(`Socket close failed: ${String(err)}`);
932
- }
933
- },
934
- onClose,
935
- signalClose: (reason) => {
936
- resolveClose(reason ?? {
937
- status: void 0,
938
- isLoggedOut: false,
939
- error: "closed"
940
- });
941
- },
942
- ...createWebSendApi({
943
- sock: {
944
- sendMessage: (jid, content) => sock.sendMessage(jid, content),
945
- sendPresenceUpdate: (presence, jid) => sock.sendPresenceUpdate(presence, jid)
946
- },
947
- defaultAccountId: options.accountId
948
- })
949
- };
950
- }
951
- //#endregion
952
- //#region src/web/auto-reply/mentions.ts
953
- function buildMentionConfig(cfg, agentId) {
954
- return {
955
- mentionRegexes: buildMentionRegexes(cfg, agentId),
956
- allowFrom: cfg.channels?.whatsapp?.allowFrom
957
- };
958
- }
959
- function resolveMentionTargets(msg, authDir) {
960
- const jidOptions = authDir ? { authDir } : void 0;
961
- return {
962
- normalizedMentions: msg.mentionedJids?.length ? msg.mentionedJids.map((jid) => jidToE164(jid, jidOptions) ?? jid).filter(Boolean) : [],
963
- selfE164: msg.selfE164 ?? (msg.selfJid ? jidToE164(msg.selfJid, jidOptions) : null),
964
- selfJid: msg.selfJid ? msg.selfJid.replace(/:\\d+/, "") : null
965
- };
966
- }
967
- function isBotMentionedFromTargets(msg, mentionCfg, targets) {
968
- const clean = (text) => normalizeMentionText(text);
969
- const isSelfChat = isSelfChatMode(targets.selfE164, mentionCfg.allowFrom);
970
- const hasMentions = (msg.mentionedJids?.length ?? 0) > 0;
971
- if (hasMentions && !isSelfChat) {
972
- if (targets.selfE164 && targets.normalizedMentions.includes(targets.selfE164)) return true;
973
- if (targets.selfJid) {
974
- if (targets.normalizedMentions.includes(targets.selfJid)) return true;
975
- }
976
- return false;
977
- } else if (hasMentions && isSelfChat) {}
978
- const bodyClean = clean(msg.body);
979
- if (mentionCfg.mentionRegexes.some((re) => re.test(bodyClean))) return true;
980
- if (targets.selfE164) {
981
- const selfDigits = targets.selfE164.replace(/\D/g, "");
982
- if (selfDigits) {
983
- if (bodyClean.replace(/[^\d]/g, "").includes(selfDigits)) return true;
984
- const bodyNoSpace = msg.body.replace(/[\s-]/g, "");
985
- if (new RegExp(`\\+?${selfDigits}`, "i").test(bodyNoSpace)) return true;
986
- }
987
- }
988
- return false;
989
- }
990
- function debugMention(msg, mentionCfg, authDir) {
991
- const mentionTargets = resolveMentionTargets(msg, authDir);
992
- return {
993
- wasMentioned: isBotMentionedFromTargets(msg, mentionCfg, mentionTargets),
994
- details: {
995
- from: msg.from,
996
- body: msg.body,
997
- bodyClean: normalizeMentionText(msg.body),
998
- mentionedJids: msg.mentionedJids ?? null,
999
- normalizedMentionedJids: mentionTargets.normalizedMentions.length ? mentionTargets.normalizedMentions : null,
1000
- selfJid: msg.selfJid ?? null,
1001
- selfJidBare: mentionTargets.selfJid,
1002
- selfE164: msg.selfE164 ?? null,
1003
- resolvedSelfE164: mentionTargets.selfE164
1004
- }
1005
- };
1006
- }
1007
- function resolveOwnerList(mentionCfg, selfE164) {
1008
- const allowFrom = mentionCfg.allowFrom;
1009
- return (Array.isArray(allowFrom) && allowFrom.length > 0 ? allowFrom : selfE164 ? [selfE164] : []).filter((entry) => Boolean(entry && entry !== "*")).map((entry) => normalizeE164(entry)).filter((entry) => Boolean(entry));
1010
- }
1011
- //#endregion
1012
- //#region src/web/auto-reply/monitor/echo.ts
1013
- function createEchoTracker(params) {
1014
- const recentlySent = /* @__PURE__ */ new Set();
1015
- const maxItems = Math.max(1, params.maxItems ?? 100);
1016
- const buildCombinedKey = (p) => `combined:${p.sessionKey}:${p.combinedBody}`;
1017
- const trim = () => {
1018
- while (recentlySent.size > maxItems) {
1019
- const firstKey = recentlySent.values().next().value;
1020
- if (!firstKey) break;
1021
- recentlySent.delete(firstKey);
1022
- }
1023
- };
1024
- const rememberText = (text, opts) => {
1025
- if (!text) return;
1026
- recentlySent.add(text);
1027
- if (opts.combinedBody && opts.combinedBodySessionKey) recentlySent.add(buildCombinedKey({
1028
- sessionKey: opts.combinedBodySessionKey,
1029
- combinedBody: opts.combinedBody
1030
- }));
1031
- if (opts.logVerboseMessage) params.logVerbose?.(`Added to echo detection set (size now: ${recentlySent.size}): ${text.substring(0, 50)}...`);
1032
- trim();
1033
- };
1034
- return {
1035
- rememberText,
1036
- has: (key) => recentlySent.has(key),
1037
- forget: (key) => {
1038
- recentlySent.delete(key);
1039
- },
1040
- buildCombinedKey
1041
- };
1042
- }
1043
- //#endregion
1044
- //#region src/web/auto-reply/monitor/broadcast.ts
1045
- function buildBroadcastRouteKeys(params) {
1046
- const sessionKey = buildAgentSessionKey({
1047
- agentId: params.agentId,
1048
- channel: "whatsapp",
1049
- accountId: params.route.accountId,
1050
- peer: {
1051
- kind: params.msg.chatType === "group" ? "group" : "direct",
1052
- id: params.peerId
1053
- },
1054
- dmScope: params.cfg.session?.dmScope,
1055
- identityLinks: params.cfg.session?.identityLinks
1056
- });
1057
- const mainSessionKey = buildAgentMainSessionKey({
1058
- agentId: params.agentId,
1059
- mainKey: DEFAULT_MAIN_KEY
1060
- });
1061
- return {
1062
- sessionKey,
1063
- mainSessionKey,
1064
- lastRoutePolicy: deriveLastRoutePolicy({
1065
- sessionKey,
1066
- mainSessionKey
1067
- })
1068
- };
1069
- }
1070
- async function maybeBroadcastMessage(params) {
1071
- const broadcastAgents = params.cfg.broadcast?.[params.peerId];
1072
- if (!broadcastAgents || !Array.isArray(broadcastAgents)) return false;
1073
- if (broadcastAgents.length === 0) return false;
1074
- const strategy = params.cfg.broadcast?.strategy || "parallel";
1075
- whatsappInboundLog.info(`Broadcasting message to ${broadcastAgents.length} agents (${strategy})`);
1076
- const agentIds = params.cfg.agents?.list?.map((agent) => normalizeAgentId(agent.id));
1077
- const hasKnownAgents = (agentIds?.length ?? 0) > 0;
1078
- const groupHistorySnapshot = params.msg.chatType === "group" ? params.groupHistories.get(params.groupHistoryKey) ?? [] : void 0;
1079
- const processForAgent = async (agentId) => {
1080
- const normalizedAgentId = normalizeAgentId(agentId);
1081
- if (hasKnownAgents && !agentIds?.includes(normalizedAgentId)) {
1082
- whatsappInboundLog.warn(`Broadcast agent ${agentId} not found in agents.list; skipping`);
1083
- return false;
1084
- }
1085
- const routeKeys = buildBroadcastRouteKeys({
1086
- cfg: params.cfg,
1087
- msg: params.msg,
1088
- route: params.route,
1089
- peerId: params.peerId,
1090
- agentId: normalizedAgentId
1091
- });
1092
- const agentRoute = {
1093
- ...params.route,
1094
- agentId: normalizedAgentId,
1095
- ...routeKeys
1096
- };
1097
- try {
1098
- return await params.processMessage(params.msg, agentRoute, params.groupHistoryKey, {
1099
- groupHistory: groupHistorySnapshot,
1100
- suppressGroupHistoryClear: true
1101
- });
1102
- } catch (err) {
1103
- whatsappInboundLog.error(`Broadcast agent ${agentId} failed: ${formatError(err)}`);
1104
- return false;
1105
- }
1106
- };
1107
- if (strategy === "sequential") for (const agentId of broadcastAgents) await processForAgent(agentId);
1108
- else await Promise.allSettled(broadcastAgents.map(processForAgent));
1109
- if (params.msg.chatType === "group") params.groupHistories.set(params.groupHistoryKey, []);
1110
- return true;
1111
- }
1112
- //#endregion
1113
- //#region src/web/auto-reply/monitor/commands.ts
1114
- function stripMentionsForCommand(text, mentionRegexes, selfE164) {
1115
- let result = text;
1116
- for (const re of mentionRegexes) result = result.replace(re, " ");
1117
- if (selfE164) {
1118
- const digits = selfE164.replace(/\D/g, "");
1119
- if (digits) {
1120
- const pattern = new RegExp(`\\+?${digits}`, "g");
1121
- result = result.replace(pattern, " ");
1122
- }
1123
- }
1124
- return result.replace(/\s+/g, " ").trim();
1125
- }
1126
- //#endregion
1127
- //#region src/web/auto-reply/monitor/group-activation.ts
1128
- function resolveGroupPolicyFor(cfg, conversationId) {
1129
- const groupId = resolveGroupSessionKey({
1130
- From: conversationId,
1131
- ChatType: "group",
1132
- Provider: "whatsapp"
1133
- })?.id;
1134
- const whatsappCfg = cfg.channels?.whatsapp;
1135
- const hasGroupAllowFrom = Boolean(whatsappCfg?.groupAllowFrom?.length || whatsappCfg?.allowFrom?.length);
1136
- return resolveChannelGroupPolicy({
1137
- cfg,
1138
- channel: "whatsapp",
1139
- groupId: groupId ?? conversationId,
1140
- hasGroupAllowFrom
1141
- });
1142
- }
1143
- function resolveGroupRequireMentionFor(cfg, conversationId) {
1144
- const groupId = resolveGroupSessionKey({
1145
- From: conversationId,
1146
- ChatType: "group",
1147
- Provider: "whatsapp"
1148
- })?.id;
1149
- return resolveChannelGroupRequireMention({
1150
- cfg,
1151
- channel: "whatsapp",
1152
- groupId: groupId ?? conversationId
1153
- });
1154
- }
1155
- function resolveGroupActivationFor(params) {
1156
- const entry = loadSessionStore(resolveStorePath(params.cfg.session?.store, { agentId: params.agentId }))[params.sessionKey];
1157
- const defaultActivation = !resolveGroupRequireMentionFor(params.cfg, params.conversationId) ? "always" : "mention";
1158
- return normalizeGroupActivation(entry?.groupActivation) ?? defaultActivation;
1159
- }
1160
- //#endregion
1161
- //#region src/web/auto-reply/monitor/group-members.ts
1162
- function appendNormalizedUnique(entries, seen, ordered) {
1163
- for (const entry of entries) {
1164
- const normalized = normalizeE164(entry) ?? entry;
1165
- if (!normalized || seen.has(normalized)) continue;
1166
- seen.add(normalized);
1167
- ordered.push(normalized);
1168
- }
1169
- }
1170
- function noteGroupMember(groupMemberNames, conversationId, e164, name) {
1171
- if (!e164 || !name) return;
1172
- const key = normalizeE164(e164) ?? e164;
1173
- if (!key) return;
1174
- let roster = groupMemberNames.get(conversationId);
1175
- if (!roster) {
1176
- roster = /* @__PURE__ */ new Map();
1177
- groupMemberNames.set(conversationId, roster);
1178
- }
1179
- roster.set(key, name);
1180
- }
1181
- function formatGroupMembers(params) {
1182
- const { participants, roster, fallbackE164 } = params;
1183
- const seen = /* @__PURE__ */ new Set();
1184
- const ordered = [];
1185
- if (participants?.length) appendNormalizedUnique(participants, seen, ordered);
1186
- if (roster) appendNormalizedUnique(roster.keys(), seen, ordered);
1187
- if (ordered.length === 0 && fallbackE164) {
1188
- const normalized = normalizeE164(fallbackE164) ?? fallbackE164;
1189
- if (normalized) ordered.push(normalized);
1190
- }
1191
- if (ordered.length === 0) return;
1192
- return ordered.map((entry) => {
1193
- const name = roster?.get(entry);
1194
- return name ? `${name} (${entry})` : entry;
1195
- }).join(", ");
1196
- }
1197
- //#endregion
1198
- //#region src/web/auto-reply/monitor/group-gating.ts
1199
- function isOwnerSender(baseMentionConfig, msg) {
1200
- const sender = normalizeE164(msg.senderE164 ?? "");
1201
- if (!sender) return false;
1202
- return resolveOwnerList(baseMentionConfig, msg.selfE164 ?? void 0).includes(sender);
1203
- }
1204
- function recordPendingGroupHistoryEntry(params) {
1205
- const sender = params.msg.senderName && params.msg.senderE164 ? `${params.msg.senderName} (${params.msg.senderE164})` : params.msg.senderName ?? params.msg.senderE164 ?? "Unknown";
1206
- recordPendingHistoryEntryIfEnabled({
1207
- historyMap: params.groupHistories,
1208
- historyKey: params.groupHistoryKey,
1209
- limit: params.groupHistoryLimit,
1210
- entry: {
1211
- sender,
1212
- body: params.msg.body,
1213
- timestamp: params.msg.timestamp,
1214
- id: params.msg.id,
1215
- senderJid: params.msg.senderJid
1216
- }
1217
- });
1218
- }
1219
- function skipGroupMessageAndStoreHistory(params, verboseMessage) {
1220
- params.logVerbose(verboseMessage);
1221
- recordPendingGroupHistoryEntry({
1222
- msg: params.msg,
1223
- groupHistories: params.groupHistories,
1224
- groupHistoryKey: params.groupHistoryKey,
1225
- groupHistoryLimit: params.groupHistoryLimit
1226
- });
1227
- return { shouldProcess: false };
1228
- }
1229
- function applyGroupGating(params) {
1230
- const groupPolicy = resolveGroupPolicyFor(params.cfg, params.conversationId);
1231
- if (groupPolicy.allowlistEnabled && !groupPolicy.allowed) {
1232
- params.logVerbose(`Skipping group message ${params.conversationId} (not in allowlist)`);
1233
- return { shouldProcess: false };
1234
- }
1235
- noteGroupMember(params.groupMemberNames, params.groupHistoryKey, params.msg.senderE164, params.msg.senderName);
1236
- const mentionConfig = buildMentionConfig(params.cfg, params.agentId);
1237
- const commandBody = stripMentionsForCommand(params.msg.body, mentionConfig.mentionRegexes, params.msg.selfE164);
1238
- const activationCommand = parseActivationCommand(commandBody);
1239
- const owner = isOwnerSender(params.baseMentionConfig, params.msg);
1240
- const shouldBypassMention = owner && hasControlCommand(commandBody, params.cfg);
1241
- if (activationCommand.hasCommand && !owner) return skipGroupMessageAndStoreHistory(params, `Ignoring /activation from non-owner in group ${params.conversationId}`);
1242
- const mentionDebug = debugMention(params.msg, mentionConfig, params.authDir);
1243
- params.replyLogger.debug({
1244
- conversationId: params.conversationId,
1245
- wasMentioned: mentionDebug.wasMentioned,
1246
- ...mentionDebug.details
1247
- }, "group mention debug");
1248
- const wasMentioned = mentionDebug.wasMentioned;
1249
- const requireMention = resolveGroupActivationFor({
1250
- cfg: params.cfg,
1251
- agentId: params.agentId,
1252
- sessionKey: params.sessionKey,
1253
- conversationId: params.conversationId
1254
- }) !== "always";
1255
- const selfJid = params.msg.selfJid?.replace(/:\\d+/, "");
1256
- const replySenderJid = params.msg.replyToSenderJid?.replace(/:\\d+/, "");
1257
- const selfE164 = params.msg.selfE164 ? normalizeE164(params.msg.selfE164) : null;
1258
- const replySenderE164 = params.msg.replyToSenderE164 ? normalizeE164(params.msg.replyToSenderE164) : null;
1259
- const mentionGate = resolveMentionGating({
1260
- requireMention,
1261
- canDetectMention: true,
1262
- wasMentioned,
1263
- implicitMention: Boolean(selfJid && replySenderJid && selfJid === replySenderJid || selfE164 && replySenderE164 && selfE164 === replySenderE164),
1264
- shouldBypassMention
1265
- });
1266
- params.msg.wasMentioned = mentionGate.effectiveWasMentioned;
1267
- if (!shouldBypassMention && requireMention && mentionGate.shouldSkip) return skipGroupMessageAndStoreHistory(params, `Group message stored for context (no mention detected) in ${params.conversationId}: ${params.msg.body}`);
1268
- return { shouldProcess: true };
1269
- }
1270
- //#endregion
1271
- //#region src/web/auto-reply/monitor/last-route.ts
1272
- function trackBackgroundTask(backgroundTasks, task) {
1273
- backgroundTasks.add(task);
1274
- task.finally(() => {
1275
- backgroundTasks.delete(task);
1276
- });
1277
- }
1278
- function updateLastRouteInBackground(params) {
1279
- const storePath = resolveStorePath(params.cfg.session?.store, { agentId: params.storeAgentId });
1280
- const task = updateLastRoute({
1281
- storePath,
1282
- sessionKey: params.sessionKey,
1283
- deliveryContext: {
1284
- channel: params.channel,
1285
- to: params.to,
1286
- accountId: params.accountId
1287
- },
1288
- ctx: params.ctx
1289
- }).catch((err) => {
1290
- params.warn({
1291
- error: formatError(err),
1292
- storePath,
1293
- sessionKey: params.sessionKey,
1294
- to: params.to
1295
- }, "failed updating last route");
1296
- });
1297
- trackBackgroundTask(params.backgroundTasks, task);
1298
- }
1299
- //#endregion
1300
- //#region src/web/auto-reply/monitor/peer.ts
1301
- function resolvePeerId(msg) {
1302
- if (msg.chatType === "group") return msg.conversationId ?? msg.from;
1303
- if (msg.senderE164) return normalizeE164(msg.senderE164) ?? msg.senderE164;
1304
- if (msg.from.includes("@")) return jidToE164(msg.from) ?? msg.from;
1305
- return normalizeE164(msg.from) ?? msg.from;
1306
- }
1307
- //#endregion
1308
- //#region src/web/auto-reply/util.ts
1309
- function elide(text, limit = 400) {
1310
- if (!text) return text;
1311
- if (text.length <= limit) return text;
1312
- return `${text.slice(0, limit)}… (truncated ${text.length - limit} chars)`;
1313
- }
1314
- function isLikelyWhatsAppCryptoError(reason) {
1315
- const formatReason = (value) => {
1316
- if (value == null) return "";
1317
- if (typeof value === "string") return value;
1318
- if (value instanceof Error) return `${value.message}\n${value.stack ?? ""}`;
1319
- if (typeof value === "object") try {
1320
- return JSON.stringify(value);
1321
- } catch {
1322
- return Object.prototype.toString.call(value);
1323
- }
1324
- if (typeof value === "number") return String(value);
1325
- if (typeof value === "boolean") return String(value);
1326
- if (typeof value === "bigint") return String(value);
1327
- if (typeof value === "symbol") return value.description ?? value.toString();
1328
- if (typeof value === "function") return value.name ? `[function ${value.name}]` : "[function]";
1329
- return Object.prototype.toString.call(value);
1330
- };
1331
- const haystack = (reason instanceof Error ? `${reason.message}\n${reason.stack ?? ""}` : formatReason(reason)).toLowerCase();
1332
- if (!(haystack.includes("unsupported state or unable to authenticate data") || haystack.includes("bad mac"))) return false;
1333
- return haystack.includes("@whiskeysockets/baileys") || haystack.includes("baileys") || haystack.includes("noise-handler") || haystack.includes("aesdecryptgcm");
1334
- }
1335
- //#endregion
1336
- //#region src/web/auto-reply/deliver-reply.ts
1337
- const REASONING_PREFIX = "reasoning:";
1338
- function shouldSuppressReasoningReply(payload) {
1339
- if (payload.isReasoning === true) return true;
1340
- const text = payload.text;
1341
- if (typeof text !== "string") return false;
1342
- return text.trimStart().toLowerCase().startsWith(REASONING_PREFIX);
1343
- }
1344
- async function deliverWebReply(params) {
1345
- const { replyResult, msg, maxMediaBytes, textLimit, replyLogger, connectionId, skipLog } = params;
1346
- const replyStarted = Date.now();
1347
- if (shouldSuppressReasoningReply(replyResult)) {
1348
- whatsappOutboundLog.debug(`Suppressed reasoning payload to ${msg.from}`);
1349
- return;
1350
- }
1351
- const tableMode = params.tableMode ?? "code";
1352
- const chunkMode = params.chunkMode ?? "length";
1353
- const textChunks = chunkMarkdownTextWithMode(markdownToWhatsApp(convertMarkdownTables(replyResult.text || "", tableMode)), textLimit, chunkMode);
1354
- const mediaList = replyResult.mediaUrls?.length ? replyResult.mediaUrls : replyResult.mediaUrl ? [replyResult.mediaUrl] : [];
1355
- const sendWithRetry = async (fn, label, maxAttempts = 3) => {
1356
- let lastErr;
1357
- for (let attempt = 1; attempt <= maxAttempts; attempt++) try {
1358
- return await fn();
1359
- } catch (err) {
1360
- lastErr = err;
1361
- const errText = formatError(err);
1362
- const isLast = attempt === maxAttempts;
1363
- if (!/closed|reset|timed\s*out|disconnect/i.test(errText) || isLast) throw err;
1364
- const backoffMs = 500 * attempt;
1365
- logVerbose(`Retrying ${label} to ${msg.from} after failure (${attempt}/${maxAttempts - 1}) in ${backoffMs}ms: ${errText}`);
1366
- await sleep(backoffMs);
1367
- }
1368
- throw lastErr;
1369
- };
1370
- if (mediaList.length === 0 && textChunks.length) {
1371
- const totalChunks = textChunks.length;
1372
- for (const [index, chunk] of textChunks.entries()) {
1373
- const chunkStarted = Date.now();
1374
- await sendWithRetry(() => msg.reply(chunk), "text");
1375
- if (!skipLog) {
1376
- const durationMs = Date.now() - chunkStarted;
1377
- whatsappOutboundLog.debug(`Sent chunk ${index + 1}/${totalChunks} to ${msg.from} (${durationMs.toFixed(0)}ms)`);
1378
- }
1379
- }
1380
- replyLogger.info({
1381
- correlationId: msg.id ?? newConnectionId(),
1382
- connectionId: connectionId ?? null,
1383
- to: msg.from,
1384
- from: msg.to,
1385
- text: elide(replyResult.text, 240),
1386
- mediaUrl: null,
1387
- mediaSizeBytes: null,
1388
- mediaKind: null,
1389
- durationMs: Date.now() - replyStarted
1390
- }, "auto-reply sent (text)");
1391
- return;
1392
- }
1393
- const remainingText = [...textChunks];
1394
- for (const [index, mediaUrl] of mediaList.entries()) {
1395
- const caption = index === 0 ? remainingText.shift() || void 0 : void 0;
1396
- try {
1397
- const media = await loadWebMedia(mediaUrl, {
1398
- maxBytes: maxMediaBytes,
1399
- localRoots: params.mediaLocalRoots
1400
- });
1401
- if (shouldLogVerbose()) {
1402
- logVerbose(`Web auto-reply media size: ${(media.buffer.length / (1024 * 1024)).toFixed(2)}MB`);
1403
- logVerbose(`Web auto-reply media source: ${mediaUrl} (kind ${media.kind})`);
1404
- }
1405
- if (media.kind === "image") await sendWithRetry(() => msg.sendMedia({
1406
- image: media.buffer,
1407
- caption,
1408
- mimetype: media.contentType
1409
- }), "media:image");
1410
- else if (media.kind === "audio") await sendWithRetry(() => msg.sendMedia({
1411
- audio: media.buffer,
1412
- ptt: true,
1413
- mimetype: media.contentType,
1414
- caption
1415
- }), "media:audio");
1416
- else if (media.kind === "video") await sendWithRetry(() => msg.sendMedia({
1417
- video: media.buffer,
1418
- caption,
1419
- mimetype: media.contentType
1420
- }), "media:video");
1421
- else {
1422
- const fileName = media.fileName ?? mediaUrl.split("/").pop() ?? "file";
1423
- const mimetype = media.contentType ?? "application/octet-stream";
1424
- await sendWithRetry(() => msg.sendMedia({
1425
- document: media.buffer,
1426
- fileName,
1427
- caption,
1428
- mimetype
1429
- }), "media:document");
1430
- }
1431
- whatsappOutboundLog.info(`Sent media reply to ${msg.from} (${(media.buffer.length / (1024 * 1024)).toFixed(2)}MB)`);
1432
- replyLogger.info({
1433
- correlationId: msg.id ?? newConnectionId(),
1434
- connectionId: connectionId ?? null,
1435
- to: msg.from,
1436
- from: msg.to,
1437
- text: caption ?? null,
1438
- mediaUrl,
1439
- mediaSizeBytes: media.buffer.length,
1440
- mediaKind: media.kind,
1441
- durationMs: Date.now() - replyStarted
1442
- }, "auto-reply sent (media)");
1443
- } catch (err) {
1444
- whatsappOutboundLog.error(`Failed sending web media to ${msg.from}: ${formatError(err)}`);
1445
- replyLogger.warn({
1446
- err,
1447
- mediaUrl
1448
- }, "failed to send web media reply");
1449
- if (index === 0) {
1450
- const warning = err instanceof Error ? `⚠️ Media failed: ${err.message}` : "⚠️ Media failed.";
1451
- const fallbackText = [remainingText.shift() ?? caption ?? "", warning].filter(Boolean).join("\n");
1452
- if (fallbackText) {
1453
- whatsappOutboundLog.warn(`Media skipped; sent text-only to ${msg.from}`);
1454
- await msg.reply(fallbackText);
1455
- }
1456
- }
1457
- }
1458
- }
1459
- for (const chunk of remainingText) await msg.reply(chunk);
1460
- }
1461
- //#endregion
1462
- //#region src/web/auto-reply/monitor/ack-reaction.ts
1463
- function maybeSendAckReaction(params) {
1464
- if (!params.msg.id) return;
1465
- const ackConfig = params.cfg.channels?.whatsapp?.ackReaction;
1466
- const emoji = (ackConfig?.emoji ?? "").trim();
1467
- const directEnabled = ackConfig?.direct ?? true;
1468
- const groupMode = ackConfig?.group ?? "mentions";
1469
- const conversationIdForCheck = params.msg.conversationId ?? params.msg.from;
1470
- const activation = params.msg.chatType === "group" ? resolveGroupActivationFor({
1471
- cfg: params.cfg,
1472
- agentId: params.agentId,
1473
- sessionKey: params.sessionKey,
1474
- conversationId: conversationIdForCheck
1475
- }) : null;
1476
- const shouldSendReaction = () => shouldAckReactionForWhatsApp({
1477
- emoji,
1478
- isDirect: params.msg.chatType === "direct",
1479
- isGroup: params.msg.chatType === "group",
1480
- directEnabled,
1481
- groupMode,
1482
- wasMentioned: params.msg.wasMentioned === true,
1483
- groupActivated: activation === "always"
1484
- });
1485
- if (!shouldSendReaction()) return;
1486
- params.info({
1487
- chatId: params.msg.chatId,
1488
- messageId: params.msg.id,
1489
- emoji
1490
- }, "sending ack reaction");
1491
- sendReactionWhatsApp(params.msg.chatId, params.msg.id, emoji, {
1492
- verbose: params.verbose,
1493
- fromMe: false,
1494
- participant: params.msg.senderJid,
1495
- accountId: params.accountId
1496
- }).catch((err) => {
1497
- params.warn({
1498
- error: formatError(err),
1499
- chatId: params.msg.chatId,
1500
- messageId: params.msg.id
1501
- }, "failed to send ack reaction");
1502
- logVerbose(`WhatsApp ack reaction failed for chat ${params.msg.chatId}: ${formatError(err)}`);
1503
- });
1504
- }
1505
- //#endregion
1506
- //#region src/web/auto-reply/monitor/message-line.ts
1507
- function formatReplyContext(msg) {
1508
- if (!msg.replyToBody) return null;
1509
- return `[Replying to ${msg.replyToSender ?? "unknown sender"}${msg.replyToId ? ` id:${msg.replyToId}` : ""}]\n${msg.replyToBody}\n[/Replying]`;
1510
- }
1511
- function buildInboundLine(params) {
1512
- const { cfg, msg, agentId, previousTimestamp, envelope } = params;
1513
- const messagePrefix = resolveMessagePrefix(cfg, agentId, {
1514
- configured: cfg.channels?.whatsapp?.messagePrefix,
1515
- hasAllowFrom: (cfg.channels?.whatsapp?.allowFrom?.length ?? 0) > 0
1516
- });
1517
- const prefixStr = messagePrefix ? `${messagePrefix} ` : "";
1518
- const replyContext = formatReplyContext(msg);
1519
- const baseLine = `${prefixStr}${msg.body}${replyContext ? `\n\n${replyContext}` : ""}`;
1520
- return formatInboundEnvelope({
1521
- channel: "WhatsApp",
1522
- from: msg.chatType === "group" ? msg.from : msg.from?.replace(/^whatsapp:/, ""),
1523
- timestamp: msg.timestamp,
1524
- body: baseLine,
1525
- chatType: msg.chatType,
1526
- sender: {
1527
- name: msg.senderName,
1528
- e164: msg.senderE164,
1529
- id: msg.senderJid
1530
- },
1531
- previousTimestamp,
1532
- envelope,
1533
- fromMe: msg.fromMe
1534
- });
1535
- }
1536
- //#endregion
1537
- //#region src/web/auto-reply/monitor/process-message.ts
1538
- async function resolveWhatsAppCommandAuthorized(params) {
1539
- const useAccessGroups = params.cfg.commands?.useAccessGroups !== false;
1540
- if (!useAccessGroups) return true;
1541
- const isGroup = params.msg.chatType === "group";
1542
- const senderE164 = normalizeE164(isGroup ? params.msg.senderE164 ?? "" : params.msg.senderE164 ?? params.msg.from ?? "");
1543
- if (!senderE164) return false;
1544
- const account = resolveWhatsAppAccount({
1545
- cfg: params.cfg,
1546
- accountId: params.msg.accountId
1547
- });
1548
- const dmPolicy = account.dmPolicy ?? "pairing";
1549
- const groupPolicy = account.groupPolicy ?? "allowlist";
1550
- const configuredAllowFrom = account.allowFrom ?? [];
1551
- const configuredGroupAllowFrom = account.groupAllowFrom ?? (configuredAllowFrom.length > 0 ? configuredAllowFrom : void 0);
1552
- const storeAllowFrom = isGroup ? [] : await readStoreAllowFromForDmPolicy({
1553
- provider: "whatsapp",
1554
- accountId: params.msg.accountId,
1555
- dmPolicy
1556
- });
1557
- return resolveDmGroupAccessWithCommandGate({
1558
- isGroup,
1559
- dmPolicy,
1560
- groupPolicy,
1561
- allowFrom: configuredAllowFrom.length > 0 ? configuredAllowFrom : params.msg.selfE164 ? [params.msg.selfE164] : [],
1562
- groupAllowFrom: configuredGroupAllowFrom,
1563
- storeAllowFrom,
1564
- isSenderAllowed: (allowEntries) => {
1565
- if (allowEntries.includes("*")) return true;
1566
- return allowEntries.map((entry) => normalizeE164(String(entry))).filter((entry) => Boolean(entry)).includes(senderE164);
1567
- },
1568
- command: {
1569
- useAccessGroups,
1570
- allowTextCommands: true,
1571
- hasControlCommand: true
1572
- }
1573
- }).commandAuthorized;
1574
- }
1575
- function resolvePinnedMainDmRecipient(params) {
1576
- const account = resolveWhatsAppAccount({
1577
- cfg: params.cfg,
1578
- accountId: params.msg.accountId
1579
- });
1580
- return resolvePinnedMainDmOwnerFromAllowlist({
1581
- dmScope: params.cfg.session?.dmScope,
1582
- allowFrom: account.allowFrom,
1583
- normalizeEntry: (entry) => normalizeE164(entry)
1584
- });
1585
- }
1586
- async function processMessage(params) {
1587
- const conversationId = params.msg.conversationId ?? params.msg.from;
1588
- const { storePath, envelopeOptions, previousTimestamp } = resolveInboundSessionEnvelopeContext({
1589
- cfg: params.cfg,
1590
- agentId: params.route.agentId,
1591
- sessionKey: params.route.sessionKey
1592
- });
1593
- let combinedBody = buildInboundLine({
1594
- cfg: params.cfg,
1595
- msg: params.msg,
1596
- agentId: params.route.agentId,
1597
- previousTimestamp,
1598
- envelope: envelopeOptions
1599
- });
1600
- let shouldClearGroupHistory = false;
1601
- if (params.msg.chatType === "group") {
1602
- const history = params.groupHistory ?? params.groupHistories.get(params.groupHistoryKey) ?? [];
1603
- if (history.length > 0) combinedBody = buildHistoryContextFromEntries({
1604
- entries: history.map((m) => ({
1605
- sender: m.sender,
1606
- body: m.body,
1607
- timestamp: m.timestamp
1608
- })),
1609
- currentMessage: combinedBody,
1610
- excludeLast: false,
1611
- formatEntry: (entry) => {
1612
- return formatInboundEnvelope({
1613
- channel: "WhatsApp",
1614
- from: conversationId,
1615
- timestamp: entry.timestamp,
1616
- body: entry.body,
1617
- chatType: "group",
1618
- senderLabel: entry.sender,
1619
- envelope: envelopeOptions
1620
- });
1621
- }
1622
- });
1623
- shouldClearGroupHistory = !(params.suppressGroupHistoryClear ?? false);
1624
- }
1625
- const combinedEchoKey = params.buildCombinedEchoKey({
1626
- sessionKey: params.route.sessionKey,
1627
- combinedBody
1628
- });
1629
- if (params.echoHas(combinedEchoKey)) {
1630
- logVerbose("Skipping auto-reply: detected echo for combined message");
1631
- params.echoForget(combinedEchoKey);
1632
- return false;
1633
- }
1634
- maybeSendAckReaction({
1635
- cfg: params.cfg,
1636
- msg: params.msg,
1637
- agentId: params.route.agentId,
1638
- sessionKey: params.route.sessionKey,
1639
- conversationId,
1640
- verbose: params.verbose,
1641
- accountId: params.route.accountId,
1642
- info: params.replyLogger.info.bind(params.replyLogger),
1643
- warn: params.replyLogger.warn.bind(params.replyLogger)
1644
- });
1645
- const correlationId = params.msg.id ?? newConnectionId();
1646
- params.replyLogger.info({
1647
- connectionId: params.connectionId,
1648
- correlationId,
1649
- from: params.msg.chatType === "group" ? conversationId : params.msg.from,
1650
- to: params.msg.to,
1651
- body: elide(combinedBody, 240),
1652
- mediaType: params.msg.mediaType ?? null,
1653
- mediaPath: params.msg.mediaPath ?? null
1654
- }, "inbound web message");
1655
- const fromDisplay = params.msg.chatType === "group" ? conversationId : params.msg.from;
1656
- const kindLabel = params.msg.mediaType ? `, ${params.msg.mediaType}` : "";
1657
- whatsappInboundLog.info(`Inbound message ${fromDisplay} -> ${params.msg.to} (${params.msg.chatType}${kindLabel}, ${combinedBody.length} chars)`);
1658
- if (shouldLogVerbose()) whatsappInboundLog.debug(`Inbound body: ${elide(combinedBody, 400)}`);
1659
- const dmRouteTarget = params.msg.chatType !== "group" ? (() => {
1660
- if (params.msg.senderE164) return normalizeE164(params.msg.senderE164);
1661
- if (params.msg.from.includes("@")) return jidToE164(params.msg.from);
1662
- return normalizeE164(params.msg.from);
1663
- })() : void 0;
1664
- const textLimit = params.maxMediaTextChunkLimit ?? resolveTextChunkLimit(params.cfg, "whatsapp");
1665
- const chunkMode = resolveChunkMode(params.cfg, "whatsapp", params.route.accountId);
1666
- const tableMode = resolveMarkdownTableMode({
1667
- cfg: params.cfg,
1668
- channel: "whatsapp",
1669
- accountId: params.route.accountId
1670
- });
1671
- const mediaLocalRoots = getAgentScopedMediaLocalRoots(params.cfg, params.route.agentId);
1672
- let didLogHeartbeatStrip = false;
1673
- let didSendReply = false;
1674
- const commandAuthorized = shouldComputeCommandAuthorized(params.msg.body, params.cfg) ? await resolveWhatsAppCommandAuthorized({
1675
- cfg: params.cfg,
1676
- msg: params.msg
1677
- }) : void 0;
1678
- const configuredResponsePrefix = params.cfg.messages?.responsePrefix;
1679
- const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({
1680
- cfg: params.cfg,
1681
- agentId: params.route.agentId,
1682
- channel: "whatsapp",
1683
- accountId: params.route.accountId
1684
- });
1685
- const isSelfChat = params.msg.chatType !== "group" && Boolean(params.msg.selfE164) && normalizeE164(params.msg.from) === normalizeE164(params.msg.selfE164 ?? "");
1686
- const responsePrefix = prefixOptions.responsePrefix ?? (configuredResponsePrefix === void 0 && isSelfChat ? resolveIdentityNamePrefix(params.cfg, params.route.agentId) : void 0);
1687
- const inboundHistory = params.msg.chatType === "group" ? (params.groupHistory ?? params.groupHistories.get(params.groupHistoryKey) ?? []).map((entry) => ({
1688
- sender: entry.sender,
1689
- body: entry.body,
1690
- timestamp: entry.timestamp
1691
- })) : void 0;
1692
- const ctxPayload = finalizeInboundContext({
1693
- Body: combinedBody,
1694
- BodyForAgent: params.msg.body,
1695
- InboundHistory: inboundHistory,
1696
- RawBody: params.msg.body,
1697
- CommandBody: params.msg.body,
1698
- From: params.msg.from,
1699
- To: params.msg.to,
1700
- SessionKey: params.route.sessionKey,
1701
- AccountId: params.route.accountId,
1702
- MessageSid: params.msg.id,
1703
- ReplyToId: params.msg.replyToId,
1704
- ReplyToBody: params.msg.replyToBody,
1705
- ReplyToSender: params.msg.replyToSender,
1706
- MediaPath: params.msg.mediaPath,
1707
- MediaUrl: params.msg.mediaUrl,
1708
- MediaType: params.msg.mediaType,
1709
- ChatType: params.msg.chatType,
1710
- ConversationLabel: params.msg.chatType === "group" ? conversationId : params.msg.from,
1711
- GroupSubject: params.msg.groupSubject,
1712
- GroupMembers: formatGroupMembers({
1713
- participants: params.msg.groupParticipants,
1714
- roster: params.groupMemberNames.get(params.groupHistoryKey),
1715
- fallbackE164: params.msg.senderE164
1716
- }),
1717
- SenderName: params.msg.senderName,
1718
- SenderId: params.msg.senderJid?.trim() || params.msg.senderE164,
1719
- SenderE164: params.msg.senderE164,
1720
- CommandAuthorized: commandAuthorized,
1721
- WasMentioned: params.msg.wasMentioned,
1722
- ...params.msg.location ? toLocationContext(params.msg.location) : {},
1723
- Provider: "whatsapp",
1724
- Surface: "whatsapp",
1725
- OriginatingChannel: "whatsapp",
1726
- OriginatingTo: params.msg.from
1727
- });
1728
- const pinnedMainDmRecipient = resolvePinnedMainDmRecipient({
1729
- cfg: params.cfg,
1730
- msg: params.msg
1731
- });
1732
- const shouldUpdateMainLastRoute = !pinnedMainDmRecipient || pinnedMainDmRecipient === dmRouteTarget;
1733
- const inboundLastRouteSessionKey = resolveInboundLastRouteSessionKey({
1734
- route: params.route,
1735
- sessionKey: params.route.sessionKey
1736
- });
1737
- if (dmRouteTarget && inboundLastRouteSessionKey === params.route.mainSessionKey && shouldUpdateMainLastRoute) updateLastRouteInBackground({
1738
- cfg: params.cfg,
1739
- backgroundTasks: params.backgroundTasks,
1740
- storeAgentId: params.route.agentId,
1741
- sessionKey: params.route.mainSessionKey,
1742
- channel: "whatsapp",
1743
- to: dmRouteTarget,
1744
- accountId: params.route.accountId,
1745
- ctx: ctxPayload,
1746
- warn: params.replyLogger.warn.bind(params.replyLogger)
1747
- });
1748
- else if (dmRouteTarget && inboundLastRouteSessionKey === params.route.mainSessionKey && pinnedMainDmRecipient) logVerbose(`Skipping main-session last route update for ${dmRouteTarget} (pinned owner ${pinnedMainDmRecipient})`);
1749
- const metaTask = recordSessionMetaFromInbound({
1750
- storePath,
1751
- sessionKey: params.route.sessionKey,
1752
- ctx: ctxPayload
1753
- }).catch((err) => {
1754
- params.replyLogger.warn({
1755
- error: formatError(err),
1756
- storePath,
1757
- sessionKey: params.route.sessionKey
1758
- }, "failed updating session meta");
1759
- });
1760
- trackBackgroundTask(params.backgroundTasks, metaTask);
1761
- const { queuedFinal } = await dispatchReplyWithBufferedBlockDispatcher({
1762
- ctx: ctxPayload,
1763
- cfg: params.cfg,
1764
- replyResolver: params.replyResolver,
1765
- dispatcherOptions: {
1766
- ...prefixOptions,
1767
- responsePrefix,
1768
- onHeartbeatStrip: () => {
1769
- if (!didLogHeartbeatStrip) {
1770
- didLogHeartbeatStrip = true;
1771
- logVerbose("Stripped stray HEARTBEAT_OK token from web reply");
1772
- }
1773
- },
1774
- deliver: async (payload, info) => {
1775
- if (info.kind !== "final") return;
1776
- await deliverWebReply({
1777
- replyResult: payload,
1778
- msg: params.msg,
1779
- mediaLocalRoots,
1780
- maxMediaBytes: params.maxMediaBytes,
1781
- textLimit,
1782
- chunkMode,
1783
- replyLogger: params.replyLogger,
1784
- connectionId: params.connectionId,
1785
- skipLog: false,
1786
- tableMode
1787
- });
1788
- didSendReply = true;
1789
- const shouldLog = payload.text ? true : void 0;
1790
- params.rememberSentText(payload.text, {
1791
- combinedBody,
1792
- combinedBodySessionKey: params.route.sessionKey,
1793
- logVerboseMessage: shouldLog
1794
- });
1795
- const fromDisplay = params.msg.chatType === "group" ? conversationId : params.msg.from ?? "unknown";
1796
- const hasMedia = Boolean(payload.mediaUrl || payload.mediaUrls?.length);
1797
- whatsappOutboundLog.info(`Auto-replied to ${fromDisplay}${hasMedia ? " (media)" : ""}`);
1798
- if (shouldLogVerbose()) {
1799
- const preview = payload.text != null ? elide(payload.text, 400) : "<media>";
1800
- whatsappOutboundLog.debug(`Reply body: ${preview}${hasMedia ? " (media)" : ""}`);
1801
- }
1802
- },
1803
- onError: (err, info) => {
1804
- const label = info.kind === "tool" ? "tool update" : info.kind === "block" ? "block update" : "auto-reply";
1805
- whatsappOutboundLog.error(`Failed sending web ${label} to ${params.msg.from ?? conversationId}: ${formatError(err)}`);
1806
- },
1807
- onReplyStart: params.msg.sendComposing
1808
- },
1809
- replyOptions: {
1810
- disableBlockStreaming: true,
1811
- onModelSelected
1812
- }
1813
- });
1814
- if (!queuedFinal) {
1815
- if (shouldClearGroupHistory) params.groupHistories.set(params.groupHistoryKey, []);
1816
- logVerbose("Skipping auto-reply: silent token or no text/media returned from resolver");
1817
- return false;
1818
- }
1819
- if (shouldClearGroupHistory) params.groupHistories.set(params.groupHistoryKey, []);
1820
- return didSendReply;
1821
- }
1822
- //#endregion
1823
- //#region src/web/auto-reply/monitor/on-message.ts
1824
- function createWebOnMessageHandler(params) {
1825
- const processForRoute = async (msg, route, groupHistoryKey, opts) => processMessage({
1826
- cfg: params.cfg,
1827
- msg,
1828
- route,
1829
- groupHistoryKey,
1830
- groupHistories: params.groupHistories,
1831
- groupMemberNames: params.groupMemberNames,
1832
- connectionId: params.connectionId,
1833
- verbose: params.verbose,
1834
- maxMediaBytes: params.maxMediaBytes,
1835
- replyResolver: params.replyResolver,
1836
- replyLogger: params.replyLogger,
1837
- backgroundTasks: params.backgroundTasks,
1838
- rememberSentText: params.echoTracker.rememberText,
1839
- echoHas: params.echoTracker.has,
1840
- echoForget: params.echoTracker.forget,
1841
- buildCombinedEchoKey: params.echoTracker.buildCombinedKey,
1842
- groupHistory: opts?.groupHistory,
1843
- suppressGroupHistoryClear: opts?.suppressGroupHistoryClear
1844
- });
1845
- return async (msg) => {
1846
- const conversationId = msg.conversationId ?? msg.from;
1847
- const peerId = resolvePeerId(msg);
1848
- const route = resolveAgentRoute({
1849
- cfg: loadConfig(),
1850
- channel: "whatsapp",
1851
- accountId: msg.accountId,
1852
- peer: {
1853
- kind: msg.chatType === "group" ? "group" : "direct",
1854
- id: peerId
1855
- }
1856
- });
1857
- const groupHistoryKey = msg.chatType === "group" ? buildGroupHistoryKey({
1858
- channel: "whatsapp",
1859
- accountId: route.accountId,
1860
- peerKind: "group",
1861
- peerId
1862
- }) : route.sessionKey;
1863
- if (msg.from === msg.to) logVerbose(`📱 Same-phone mode detected (from === to: ${msg.from})`);
1864
- if (params.echoTracker.has(msg.body)) {
1865
- logVerbose("Skipping auto-reply: detected echo (message matches recently sent text)");
1866
- params.echoTracker.forget(msg.body);
1867
- return;
1868
- }
1869
- if (msg.chatType === "group") {
1870
- const metaCtx = {
1871
- From: msg.from,
1872
- To: msg.to,
1873
- SessionKey: route.sessionKey,
1874
- AccountId: route.accountId,
1875
- ChatType: msg.chatType,
1876
- ConversationLabel: conversationId,
1877
- GroupSubject: msg.groupSubject,
1878
- SenderName: msg.senderName,
1879
- SenderId: msg.senderJid?.trim() || msg.senderE164,
1880
- SenderE164: msg.senderE164,
1881
- Provider: "whatsapp",
1882
- Surface: "whatsapp",
1883
- OriginatingChannel: "whatsapp",
1884
- OriginatingTo: conversationId
1885
- };
1886
- updateLastRouteInBackground({
1887
- cfg: params.cfg,
1888
- backgroundTasks: params.backgroundTasks,
1889
- storeAgentId: route.agentId,
1890
- sessionKey: route.sessionKey,
1891
- channel: "whatsapp",
1892
- to: conversationId,
1893
- accountId: route.accountId,
1894
- ctx: metaCtx,
1895
- warn: params.replyLogger.warn.bind(params.replyLogger)
1896
- });
1897
- if (!applyGroupGating({
1898
- cfg: params.cfg,
1899
- msg,
1900
- conversationId,
1901
- groupHistoryKey,
1902
- agentId: route.agentId,
1903
- sessionKey: route.sessionKey,
1904
- baseMentionConfig: params.baseMentionConfig,
1905
- authDir: params.account.authDir,
1906
- groupHistories: params.groupHistories,
1907
- groupHistoryLimit: params.groupHistoryLimit,
1908
- groupMemberNames: params.groupMemberNames,
1909
- logVerbose,
1910
- replyLogger: params.replyLogger
1911
- }).shouldProcess) return;
1912
- } else if (!msg.senderE164 && peerId && peerId.startsWith("+")) msg.senderE164 = normalizeE164(peerId) ?? msg.senderE164;
1913
- if (await maybeBroadcastMessage({
1914
- cfg: params.cfg,
1915
- msg,
1916
- peerId,
1917
- route,
1918
- groupHistoryKey,
1919
- groupHistories: params.groupHistories,
1920
- processMessage: processForRoute
1921
- })) return;
1922
- await processForRoute(msg, route, groupHistoryKey);
1923
- };
1924
- }
1925
- //#endregion
1926
- //#region src/web/auto-reply/monitor.ts
1927
- function isNonRetryableWebCloseStatus(statusCode) {
1928
- return statusCode === 440;
1929
- }
1930
- async function monitorWebChannel(verbose, listenerFactory = monitorWebInbox, keepAlive = true, replyResolver = getReplyFromConfig, runtime = defaultRuntime, abortSignal, tuning = {}) {
1931
- const runId = newConnectionId();
1932
- const replyLogger = getChildLogger({
1933
- module: "web-auto-reply",
1934
- runId
1935
- });
1936
- const heartbeatLogger = getChildLogger({
1937
- module: "web-heartbeat",
1938
- runId
1939
- });
1940
- const reconnectLogger = getChildLogger({
1941
- module: "web-reconnect",
1942
- runId
1943
- });
1944
- const status = {
1945
- running: true,
1946
- connected: false,
1947
- reconnectAttempts: 0,
1948
- lastConnectedAt: null,
1949
- lastDisconnect: null,
1950
- lastMessageAt: null,
1951
- lastEventAt: null,
1952
- lastError: null
1953
- };
1954
- const emitStatus = () => {
1955
- tuning.statusSink?.({
1956
- ...status,
1957
- lastDisconnect: status.lastDisconnect ? { ...status.lastDisconnect } : null
1958
- });
1959
- };
1960
- emitStatus();
1961
- const baseCfg = loadConfig();
1962
- const account = resolveWhatsAppAccount({
1963
- cfg: baseCfg,
1964
- accountId: tuning.accountId
1965
- });
1966
- const cfg = {
1967
- ...baseCfg,
1968
- channels: {
1969
- ...baseCfg.channels,
1970
- whatsapp: {
1971
- ...baseCfg.channels?.whatsapp,
1972
- ackReaction: account.ackReaction,
1973
- messagePrefix: account.messagePrefix,
1974
- allowFrom: account.allowFrom,
1975
- groupAllowFrom: account.groupAllowFrom,
1976
- groupPolicy: account.groupPolicy,
1977
- textChunkLimit: account.textChunkLimit,
1978
- chunkMode: account.chunkMode,
1979
- mediaMaxMb: account.mediaMaxMb,
1980
- blockStreaming: account.blockStreaming,
1981
- groups: account.groups
1982
- }
1983
- }
1984
- };
1985
- const maxMediaBytes = resolveWhatsAppMediaMaxBytes(account);
1986
- const heartbeatSeconds = resolveHeartbeatSeconds(cfg, tuning.heartbeatSeconds);
1987
- const reconnectPolicy = resolveReconnectPolicy(cfg, tuning.reconnect);
1988
- const baseMentionConfig = buildMentionConfig(cfg);
1989
- const groupHistoryLimit = cfg.channels?.whatsapp?.accounts?.[tuning.accountId ?? ""]?.historyLimit ?? cfg.channels?.whatsapp?.historyLimit ?? cfg.messages?.groupChat?.historyLimit ?? 50;
1990
- const groupHistories = /* @__PURE__ */ new Map();
1991
- const groupMemberNames = /* @__PURE__ */ new Map();
1992
- const echoTracker = createEchoTracker({
1993
- maxItems: 100,
1994
- logVerbose
1995
- });
1996
- const sleep = tuning.sleep ?? ((ms, signal) => sleepWithAbort(ms, signal ?? abortSignal));
1997
- const stopRequested = () => abortSignal?.aborted === true;
1998
- const abortPromise = abortSignal && new Promise((resolve) => abortSignal.addEventListener("abort", () => resolve("aborted"), { once: true }));
1999
- const currentMaxListeners = process.getMaxListeners?.() ?? 10;
2000
- if (process.setMaxListeners && currentMaxListeners < 50) process.setMaxListeners(50);
2001
- let sigintStop = false;
2002
- const handleSigint = () => {
2003
- sigintStop = true;
2004
- };
2005
- process.once("SIGINT", handleSigint);
2006
- let reconnectAttempts = 0;
2007
- while (true) {
2008
- if (stopRequested()) break;
2009
- const connectionId = newConnectionId();
2010
- const startedAt = Date.now();
2011
- let heartbeat = null;
2012
- let watchdogTimer = null;
2013
- let lastMessageAt = null;
2014
- let handledMessages = 0;
2015
- let unregisterUnhandled = null;
2016
- const MESSAGE_TIMEOUT_MS = tuning.messageTimeoutMs ?? 1800 * 1e3;
2017
- const WATCHDOG_CHECK_MS = tuning.watchdogCheckMs ?? 60 * 1e3;
2018
- const backgroundTasks = /* @__PURE__ */ new Set();
2019
- const onMessage = createWebOnMessageHandler({
2020
- cfg,
2021
- verbose,
2022
- connectionId,
2023
- maxMediaBytes,
2024
- groupHistoryLimit,
2025
- groupHistories,
2026
- groupMemberNames,
2027
- echoTracker,
2028
- backgroundTasks,
2029
- replyResolver: replyResolver ?? getReplyFromConfig,
2030
- replyLogger,
2031
- baseMentionConfig,
2032
- account
2033
- });
2034
- const inboundDebounceMs = resolveInboundDebounceMs({
2035
- cfg,
2036
- channel: "whatsapp"
2037
- });
2038
- const shouldDebounce = (msg) => {
2039
- if (msg.mediaPath || msg.mediaType) return false;
2040
- if (msg.location) return false;
2041
- if (msg.replyToId || msg.replyToBody) return false;
2042
- return !hasControlCommand(msg.body, cfg);
2043
- };
2044
- const listener = await (listenerFactory ?? monitorWebInbox)({
2045
- verbose,
2046
- accountId: account.accountId,
2047
- authDir: account.authDir,
2048
- mediaMaxMb: account.mediaMaxMb,
2049
- sendReadReceipts: account.sendReadReceipts,
2050
- debounceMs: inboundDebounceMs,
2051
- shouldDebounce,
2052
- onMessage: async (msg) => {
2053
- handledMessages += 1;
2054
- lastMessageAt = Date.now();
2055
- status.lastMessageAt = lastMessageAt;
2056
- status.lastEventAt = lastMessageAt;
2057
- emitStatus();
2058
- await onMessage(msg);
2059
- }
2060
- });
2061
- Object.assign(status, createConnectedChannelStatusPatch());
2062
- status.lastError = null;
2063
- emitStatus();
2064
- const { e164: selfE164 } = readWebSelfId(account.authDir);
2065
- const connectRoute = resolveAgentRoute({
2066
- cfg,
2067
- channel: "whatsapp",
2068
- accountId: account.accountId
2069
- });
2070
- enqueueSystemEvent(`WhatsApp gateway connected${selfE164 ? ` as ${selfE164}` : ""}.`, { sessionKey: connectRoute.sessionKey });
2071
- setActiveWebListener(account.accountId, listener);
2072
- unregisterUnhandled = registerUnhandledRejectionHandler((reason) => {
2073
- if (!isLikelyWhatsAppCryptoError(reason)) return false;
2074
- const errorStr = formatError(reason);
2075
- reconnectLogger.warn({
2076
- connectionId,
2077
- error: errorStr
2078
- }, "web reconnect: unhandled rejection from WhatsApp socket; forcing reconnect");
2079
- listener.signalClose?.({
2080
- status: 499,
2081
- isLoggedOut: false,
2082
- error: reason
2083
- });
2084
- return true;
2085
- });
2086
- const closeListener = async () => {
2087
- setActiveWebListener(account.accountId, null);
2088
- if (unregisterUnhandled) {
2089
- unregisterUnhandled();
2090
- unregisterUnhandled = null;
2091
- }
2092
- if (heartbeat) clearInterval(heartbeat);
2093
- if (watchdogTimer) clearInterval(watchdogTimer);
2094
- if (backgroundTasks.size > 0) {
2095
- await Promise.allSettled(backgroundTasks);
2096
- backgroundTasks.clear();
2097
- }
2098
- try {
2099
- await listener.close();
2100
- } catch (err) {
2101
- logVerbose(`Socket close failed: ${formatError(err)}`);
2102
- }
2103
- };
2104
- if (keepAlive) {
2105
- heartbeat = setInterval(() => {
2106
- const authAgeMs = getWebAuthAgeMs(account.authDir);
2107
- const minutesSinceLastMessage = lastMessageAt ? Math.floor((Date.now() - lastMessageAt) / 6e4) : null;
2108
- const logData = {
2109
- connectionId,
2110
- reconnectAttempts,
2111
- messagesHandled: handledMessages,
2112
- lastMessageAt,
2113
- authAgeMs,
2114
- uptimeMs: Date.now() - startedAt,
2115
- ...minutesSinceLastMessage !== null && minutesSinceLastMessage > 30 ? { minutesSinceLastMessage } : {}
2116
- };
2117
- if (minutesSinceLastMessage && minutesSinceLastMessage > 30) heartbeatLogger.warn(logData, "⚠️ web gateway heartbeat - no messages in 30+ minutes");
2118
- else heartbeatLogger.info(logData, "web gateway heartbeat");
2119
- }, heartbeatSeconds * 1e3);
2120
- watchdogTimer = setInterval(() => {
2121
- if (!lastMessageAt) return;
2122
- const timeSinceLastMessage = Date.now() - lastMessageAt;
2123
- if (timeSinceLastMessage <= MESSAGE_TIMEOUT_MS) return;
2124
- const minutesSinceLastMessage = Math.floor(timeSinceLastMessage / 6e4);
2125
- heartbeatLogger.warn({
2126
- connectionId,
2127
- minutesSinceLastMessage,
2128
- lastMessageAt: new Date(lastMessageAt),
2129
- messagesHandled: handledMessages
2130
- }, "Message timeout detected - forcing reconnect");
2131
- whatsappHeartbeatLog.warn(`No messages received in ${minutesSinceLastMessage}m - restarting connection`);
2132
- closeListener().catch((err) => {
2133
- logVerbose(`Close listener failed: ${formatError(err)}`);
2134
- });
2135
- listener.signalClose?.({
2136
- status: 499,
2137
- isLoggedOut: false,
2138
- error: "watchdog-timeout"
2139
- });
2140
- }, WATCHDOG_CHECK_MS);
2141
- }
2142
- whatsappLog.info("Listening for personal WhatsApp inbound messages.");
2143
- if (process.stdout.isTTY || process.stderr.isTTY) whatsappLog.raw("Ctrl+C to stop.");
2144
- if (!keepAlive) {
2145
- await closeListener();
2146
- process.removeListener("SIGINT", handleSigint);
2147
- return;
2148
- }
2149
- const reason = await Promise.race([listener.onClose?.catch((err) => {
2150
- reconnectLogger.error({ error: formatError(err) }, "listener.onClose rejected");
2151
- return {
2152
- status: 500,
2153
- isLoggedOut: false,
2154
- error: err
2155
- };
2156
- }) ?? waitForever(), abortPromise ?? waitForever()]);
2157
- if (Date.now() - startedAt > heartbeatSeconds * 1e3) reconnectAttempts = 0;
2158
- status.reconnectAttempts = reconnectAttempts;
2159
- emitStatus();
2160
- if (stopRequested() || sigintStop || reason === "aborted") {
2161
- await closeListener();
2162
- break;
2163
- }
2164
- const statusCode = (typeof reason === "object" && reason && "status" in reason ? reason.status : void 0) ?? "unknown";
2165
- const loggedOut = typeof reason === "object" && reason && "isLoggedOut" in reason && reason.isLoggedOut;
2166
- const errorStr = formatError(reason);
2167
- status.connected = false;
2168
- status.lastEventAt = Date.now();
2169
- status.lastDisconnect = {
2170
- at: status.lastEventAt,
2171
- status: typeof statusCode === "number" ? statusCode : void 0,
2172
- error: errorStr,
2173
- loggedOut: Boolean(loggedOut)
2174
- };
2175
- status.lastError = errorStr;
2176
- status.reconnectAttempts = reconnectAttempts;
2177
- emitStatus();
2178
- reconnectLogger.info({
2179
- connectionId,
2180
- status: statusCode,
2181
- loggedOut,
2182
- reconnectAttempts,
2183
- error: errorStr
2184
- }, "web reconnect: connection closed");
2185
- enqueueSystemEvent(`WhatsApp gateway disconnected (status ${statusCode ?? "unknown"})`, { sessionKey: connectRoute.sessionKey });
2186
- if (loggedOut) {
2187
- runtime.error(`WhatsApp session logged out. Run \`${formatCliCommand("openclaw channels login --channel web")}\` to relink.`);
2188
- await closeListener();
2189
- break;
2190
- }
2191
- if (isNonRetryableWebCloseStatus(statusCode)) {
2192
- reconnectLogger.warn({
2193
- connectionId,
2194
- status: statusCode,
2195
- error: errorStr
2196
- }, "web reconnect: non-retryable close status; stopping monitor");
2197
- runtime.error(`WhatsApp Web connection closed (status ${statusCode}: session conflict). Resolve conflicting WhatsApp Web sessions, then relink with \`${formatCliCommand("openclaw channels login --channel web")}\`. Stopping web monitoring.`);
2198
- await closeListener();
2199
- break;
2200
- }
2201
- reconnectAttempts += 1;
2202
- status.reconnectAttempts = reconnectAttempts;
2203
- emitStatus();
2204
- if (reconnectPolicy.maxAttempts > 0 && reconnectAttempts >= reconnectPolicy.maxAttempts) {
2205
- reconnectLogger.warn({
2206
- connectionId,
2207
- status: statusCode,
2208
- reconnectAttempts,
2209
- maxAttempts: reconnectPolicy.maxAttempts
2210
- }, "web reconnect: max attempts reached; continuing in degraded mode");
2211
- runtime.error(`WhatsApp Web reconnect: max attempts reached (${reconnectAttempts}/${reconnectPolicy.maxAttempts}). Stopping web monitoring.`);
2212
- await closeListener();
2213
- break;
2214
- }
2215
- const delay = computeBackoff(reconnectPolicy, reconnectAttempts);
2216
- reconnectLogger.info({
2217
- connectionId,
2218
- status: statusCode,
2219
- reconnectAttempts,
2220
- maxAttempts: reconnectPolicy.maxAttempts || "unlimited",
2221
- delayMs: delay
2222
- }, "web reconnect: scheduling retry");
2223
- runtime.error(`WhatsApp Web connection closed (status ${statusCode}). Retry ${reconnectAttempts}/${reconnectPolicy.maxAttempts || "∞"} in ${formatDurationPrecise(delay)}… (${errorStr})`);
2224
- await closeListener();
2225
- try {
2226
- await sleep(delay, abortSignal);
2227
- } catch {
2228
- break;
2229
- }
2230
- }
2231
- status.running = false;
2232
- status.connected = false;
2233
- status.lastEventAt = Date.now();
2234
- emitStatus();
2235
- process.removeListener("SIGINT", handleSigint);
2236
- }
2237
- //#endregion
2238
- export { monitorWebInbox as n, resolveWhatsAppHeartbeatRecipients as r, monitorWebChannel as t };