@gakr-gakr/whatsapp 0.1.0

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 (159) hide show
  1. package/action-runtime-api.ts +1 -0
  2. package/action-runtime.runtime.ts +1 -0
  3. package/api.ts +67 -0
  4. package/auth-presence.ts +80 -0
  5. package/autobot.plugin.json +23 -0
  6. package/channel-config-api.ts +1 -0
  7. package/channel-plugin-api.ts +3 -0
  8. package/config-api.ts +4 -0
  9. package/constants.ts +1 -0
  10. package/contract-api.ts +29 -0
  11. package/directory-contract-api.ts +4 -0
  12. package/doctor-contract-api.ts +8 -0
  13. package/index.ts +16 -0
  14. package/legacy-session-surface-api.ts +6 -0
  15. package/legacy-state-migrations-api.ts +1 -0
  16. package/light-runtime-api.ts +12 -0
  17. package/login-qr-api.ts +1 -0
  18. package/login-qr-runtime.ts +23 -0
  19. package/outbound-payload-test-api.ts +1 -0
  20. package/package.json +76 -0
  21. package/runtime-api.ts +84 -0
  22. package/secret-contract-api.ts +4 -0
  23. package/security-contract-api.ts +4 -0
  24. package/setup-entry.ts +21 -0
  25. package/setup-plugin-api.ts +3 -0
  26. package/src/account-config.ts +77 -0
  27. package/src/account-ids.ts +17 -0
  28. package/src/account-types.ts +5 -0
  29. package/src/accounts.ts +176 -0
  30. package/src/action-runtime-target-auth.ts +27 -0
  31. package/src/action-runtime.ts +76 -0
  32. package/src/active-listener.ts +17 -0
  33. package/src/agent-tools-login.ts +113 -0
  34. package/src/approval-auth.ts +27 -0
  35. package/src/auth-store.runtime.ts +1 -0
  36. package/src/auth-store.ts +494 -0
  37. package/src/auto-reply/config.runtime.ts +16 -0
  38. package/src/auto-reply/constants.ts +1 -0
  39. package/src/auto-reply/deliver-reply.ts +332 -0
  40. package/src/auto-reply/loggers.ts +6 -0
  41. package/src/auto-reply/mentions.ts +131 -0
  42. package/src/auto-reply/monitor/ack-reaction.ts +99 -0
  43. package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
  44. package/src/auto-reply/monitor/broadcast.ts +153 -0
  45. package/src/auto-reply/monitor/commands.ts +19 -0
  46. package/src/auto-reply/monitor/echo.ts +64 -0
  47. package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
  48. package/src/auto-reply/monitor/group-activation.ts +73 -0
  49. package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
  50. package/src/auto-reply/monitor/group-gating.ts +218 -0
  51. package/src/auto-reply/monitor/group-members.ts +65 -0
  52. package/src/auto-reply/monitor/inbound-context.ts +92 -0
  53. package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
  54. package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
  55. package/src/auto-reply/monitor/last-route.ts +61 -0
  56. package/src/auto-reply/monitor/listener-log.ts +28 -0
  57. package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
  58. package/src/auto-reply/monitor/message-line.ts +54 -0
  59. package/src/auto-reply/monitor/on-message.ts +333 -0
  60. package/src/auto-reply/monitor/peer.ts +17 -0
  61. package/src/auto-reply/monitor/process-message.ts +584 -0
  62. package/src/auto-reply/monitor/runtime-api.ts +36 -0
  63. package/src/auto-reply/monitor/status-reaction.ts +108 -0
  64. package/src/auto-reply/monitor-state.ts +114 -0
  65. package/src/auto-reply/monitor.ts +720 -0
  66. package/src/auto-reply/reply-resolver.runtime.ts +1 -0
  67. package/src/auto-reply/types.ts +48 -0
  68. package/src/auto-reply/util.ts +62 -0
  69. package/src/auto-reply.impl.ts +6 -0
  70. package/src/auto-reply.ts +1 -0
  71. package/src/channel-actions.runtime.ts +7 -0
  72. package/src/channel-actions.ts +85 -0
  73. package/src/channel-outbound.ts +87 -0
  74. package/src/channel-react-action.runtime.ts +10 -0
  75. package/src/channel-react-action.ts +247 -0
  76. package/src/channel.runtime.ts +117 -0
  77. package/src/channel.setup.ts +32 -0
  78. package/src/channel.ts +356 -0
  79. package/src/command-policy.ts +7 -0
  80. package/src/config-accessors.ts +22 -0
  81. package/src/config-schema.ts +6 -0
  82. package/src/config-ui-hints.ts +24 -0
  83. package/src/connection-controller-registry.ts +49 -0
  84. package/src/connection-controller.ts +680 -0
  85. package/src/creds-files.ts +19 -0
  86. package/src/creds-persistence.ts +71 -0
  87. package/src/directory-config.ts +40 -0
  88. package/src/doctor-contract.ts +11 -0
  89. package/src/doctor.ts +56 -0
  90. package/src/document-filename.ts +17 -0
  91. package/src/group-intro.ts +15 -0
  92. package/src/group-policy.ts +40 -0
  93. package/src/group-session-contract.ts +20 -0
  94. package/src/group-session-key.ts +42 -0
  95. package/src/heartbeat.ts +34 -0
  96. package/src/identity.ts +164 -0
  97. package/src/inbound/access-control.ts +187 -0
  98. package/src/inbound/dedupe.ts +132 -0
  99. package/src/inbound/extract.ts +484 -0
  100. package/src/inbound/lifecycle.ts +39 -0
  101. package/src/inbound/media.ts +128 -0
  102. package/src/inbound/monitor.ts +1042 -0
  103. package/src/inbound/outbound-mentions.ts +260 -0
  104. package/src/inbound/runtime-api.ts +7 -0
  105. package/src/inbound/save-media.runtime.ts +1 -0
  106. package/src/inbound/send-api.ts +203 -0
  107. package/src/inbound/send-result.ts +109 -0
  108. package/src/inbound/types.ts +107 -0
  109. package/src/inbound-policy.ts +215 -0
  110. package/src/inbound.ts +9 -0
  111. package/src/login-qr.ts +542 -0
  112. package/src/login.ts +83 -0
  113. package/src/media.ts +10 -0
  114. package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
  115. package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
  116. package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
  117. package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
  118. package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
  119. package/src/normalize-target.ts +148 -0
  120. package/src/normalize.ts +8 -0
  121. package/src/outbound-adapter.ts +36 -0
  122. package/src/outbound-base.ts +256 -0
  123. package/src/outbound-media-contract.ts +307 -0
  124. package/src/outbound-media.runtime.ts +41 -0
  125. package/src/outbound-send-deps.ts +1 -0
  126. package/src/outbound-test-support.ts +16 -0
  127. package/src/qa-driver.runtime.ts +189 -0
  128. package/src/qr-image.ts +1 -0
  129. package/src/qr-terminal.ts +1 -0
  130. package/src/quoted-message.ts +184 -0
  131. package/src/reaction-level.ts +24 -0
  132. package/src/reconnect.ts +55 -0
  133. package/src/resolve-outbound-target.ts +58 -0
  134. package/src/runtime-api.ts +59 -0
  135. package/src/runtime-group-policy.ts +16 -0
  136. package/src/runtime.ts +9 -0
  137. package/src/security-contract.ts +47 -0
  138. package/src/security-fix.ts +71 -0
  139. package/src/send.ts +342 -0
  140. package/src/session-contract.ts +43 -0
  141. package/src/session-errors.ts +125 -0
  142. package/src/session-route.ts +32 -0
  143. package/src/session.runtime.ts +8 -0
  144. package/src/session.ts +327 -0
  145. package/src/setup-core.ts +52 -0
  146. package/src/setup-finalize.ts +450 -0
  147. package/src/setup-surface.ts +71 -0
  148. package/src/setup-test-helpers.ts +217 -0
  149. package/src/shared.ts +291 -0
  150. package/src/socket-timing.ts +38 -0
  151. package/src/state-migrations.ts +55 -0
  152. package/src/status-issues.ts +185 -0
  153. package/src/system-prompt.ts +31 -0
  154. package/src/targets-runtime.ts +221 -0
  155. package/src/text-runtime.ts +18 -0
  156. package/src/vcard.ts +84 -0
  157. package/targets.ts +5 -0
  158. package/test-api.ts +2 -0
  159. package/tsconfig.json +16 -0
@@ -0,0 +1,32 @@
1
+ import type { ChannelPlugin } from "autobot/plugin-sdk/core";
2
+ import { type ResolvedWhatsAppAccount } from "./accounts.js";
3
+ import { resolveWhatsAppGroupIntroHint } from "./group-intro.js";
4
+ import {
5
+ resolveWhatsAppGroupRequireMention,
6
+ resolveWhatsAppGroupToolPolicy,
7
+ } from "./group-policy.js";
8
+ import { whatsappSetupAdapter } from "./setup-core.js";
9
+ import { createWhatsAppPluginBase, whatsappSetupWizardProxy } from "./shared.js";
10
+ import { detectWhatsAppLegacyStateMigrations } from "./state-migrations.js";
11
+
12
+ async function isWhatsAppAuthConfigured(account: ResolvedWhatsAppAccount): Promise<boolean> {
13
+ const { readWebAuthState } = await import("./auth-store.js");
14
+ return (await readWebAuthState(account.authDir)) === "linked";
15
+ }
16
+
17
+ export const whatsappSetupPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
18
+ ...createWhatsAppPluginBase({
19
+ groups: {
20
+ resolveRequireMention: resolveWhatsAppGroupRequireMention,
21
+ resolveToolPolicy: resolveWhatsAppGroupToolPolicy,
22
+ resolveGroupIntroHint: resolveWhatsAppGroupIntroHint,
23
+ },
24
+ setupWizard: whatsappSetupWizardProxy,
25
+ setup: whatsappSetupAdapter,
26
+ isConfigured: isWhatsAppAuthConfigured,
27
+ }),
28
+ lifecycle: {
29
+ detectLegacyStateMigrations: ({ oauthDir }) =>
30
+ detectWhatsAppLegacyStateMigrations({ oauthDir }),
31
+ },
32
+ };
package/src/channel.ts ADDED
@@ -0,0 +1,356 @@
1
+ import { DEFAULT_ACCOUNT_ID } from "autobot/plugin-sdk/account-id";
2
+ import { buildDmGroupAccountAllowlistAdapter } from "autobot/plugin-sdk/allowlist-config-edit";
3
+ import { createChatChannelPlugin, type ChannelPlugin } from "autobot/plugin-sdk/channel-core";
4
+ import { createLazyRuntimeModule } from "autobot/plugin-sdk/lazy-runtime";
5
+ import {
6
+ createAsyncComputedAccountStatusAdapter,
7
+ createDefaultChannelRuntimeState,
8
+ } from "autobot/plugin-sdk/status-helpers";
9
+ import { resolveWhatsAppAccount, type ResolvedWhatsAppAccount } from "./accounts.js";
10
+ import { createWhatsAppLoginTool } from "./agent-tools-login.js";
11
+ import { whatsappApprovalAuth } from "./approval-auth.js";
12
+ import type { WebChannelStatus } from "./auto-reply/types.js";
13
+ import {
14
+ describeWhatsAppMessageActions,
15
+ resolveWhatsAppAgentReactionGuidance,
16
+ } from "./channel-actions.js";
17
+ import { whatsappChannelOutbound, whatsappMessageAdapter } from "./channel-outbound.js";
18
+ import { whatsappCommandPolicy } from "./command-policy.js";
19
+ import { formatWhatsAppConfigAllowFromEntries } from "./config-accessors.js";
20
+ import {
21
+ resolveWhatsAppGroupIntroHint,
22
+ resolveWhatsAppMentionStripRegexes,
23
+ } from "./group-intro.js";
24
+ import {
25
+ resolveWhatsAppGroupRequireMention,
26
+ resolveWhatsAppGroupToolPolicy,
27
+ } from "./group-policy.js";
28
+ import { checkWhatsAppHeartbeatReady } from "./heartbeat.js";
29
+ import {
30
+ isWhatsAppGroupJid,
31
+ isWhatsAppNewsletterJid,
32
+ looksLikeWhatsAppTargetId,
33
+ normalizeWhatsAppAllowFromEntry,
34
+ normalizeWhatsAppMessagingTarget,
35
+ normalizeWhatsAppTarget,
36
+ } from "./normalize.js";
37
+ import { getWhatsAppRuntime } from "./runtime.js";
38
+ import { sendTypingWhatsApp } from "./send.js";
39
+ import { resolveWhatsAppOutboundSessionRoute } from "./session-route.js";
40
+ import { whatsappSetupAdapter } from "./setup-core.js";
41
+ import {
42
+ createWhatsAppPluginBase,
43
+ loadWhatsAppChannelRuntime,
44
+ whatsappSetupWizardProxy,
45
+ } from "./shared.js";
46
+ import { detectWhatsAppLegacyStateMigrations } from "./state-migrations.js";
47
+ import { collectWhatsAppStatusIssues } from "./status-issues.js";
48
+
49
+ const loadWhatsAppDirectoryConfig = createLazyRuntimeModule(() => import("./directory-config.js"));
50
+ const loadWhatsAppChannelReactAction = createLazyRuntimeModule(
51
+ () => import("./channel-react-action.js"),
52
+ );
53
+
54
+ function parseWhatsAppExplicitTarget(raw: string) {
55
+ const normalized = normalizeWhatsAppTarget(raw);
56
+ if (!normalized) {
57
+ return null;
58
+ }
59
+ return {
60
+ to: normalized,
61
+ chatType: isWhatsAppGroupJid(normalized)
62
+ ? ("group" as const)
63
+ : isWhatsAppNewsletterJid(normalized)
64
+ ? ("channel" as const)
65
+ : ("direct" as const),
66
+ };
67
+ }
68
+
69
+ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> =
70
+ createChatChannelPlugin<ResolvedWhatsAppAccount>({
71
+ pairing: {
72
+ idLabel: "whatsappSenderId",
73
+ normalizeAllowEntry: (entry) => normalizeWhatsAppAllowFromEntry(entry) ?? "",
74
+ },
75
+ outbound: whatsappChannelOutbound,
76
+ threading: {
77
+ scopedAccountReplyToMode: {
78
+ resolveAccount: (cfg, accountId) => resolveWhatsAppAccount({ cfg, accountId }),
79
+ resolveReplyToMode: (account) => account.replyToMode,
80
+ },
81
+ },
82
+ base: {
83
+ ...createWhatsAppPluginBase({
84
+ groups: {
85
+ resolveRequireMention: resolveWhatsAppGroupRequireMention,
86
+ resolveToolPolicy: resolveWhatsAppGroupToolPolicy,
87
+ resolveGroupIntroHint: resolveWhatsAppGroupIntroHint,
88
+ },
89
+ setupWizard: whatsappSetupWizardProxy,
90
+ setup: whatsappSetupAdapter,
91
+ isConfigured: async (account) => {
92
+ const channelRuntime = await loadWhatsAppChannelRuntime();
93
+ return (await channelRuntime.readWebAuthState(account.authDir)) === "linked";
94
+ },
95
+ }),
96
+ agentTools: () => [createWhatsAppLoginTool()],
97
+ allowlist: buildDmGroupAccountAllowlistAdapter({
98
+ channelId: "whatsapp",
99
+ resolveAccount: resolveWhatsAppAccount,
100
+ normalize: ({ values }) => formatWhatsAppConfigAllowFromEntries(values),
101
+ resolveDmAllowFrom: (account) => account.allowFrom,
102
+ resolveGroupAllowFrom: (account) => account.groupAllowFrom,
103
+ resolveDmPolicy: (account) => account.dmPolicy,
104
+ resolveGroupPolicy: (account) => account.groupPolicy,
105
+ }),
106
+ mentions: {
107
+ stripRegexes: ({ ctx }) => resolveWhatsAppMentionStripRegexes(ctx),
108
+ },
109
+ commands: whatsappCommandPolicy,
110
+ agentPrompt: {
111
+ reactionGuidance: ({ cfg, accountId }) => {
112
+ const level = resolveWhatsAppAgentReactionGuidance({
113
+ cfg,
114
+ accountId: accountId ?? undefined,
115
+ });
116
+ return level ? { level, channelLabel: "WhatsApp" } : undefined;
117
+ },
118
+ },
119
+ messaging: {
120
+ targetPrefixes: ["whatsapp"],
121
+ normalizeTarget: normalizeWhatsAppMessagingTarget,
122
+ resolveOutboundSessionRoute: (params) => resolveWhatsAppOutboundSessionRoute(params),
123
+ parseExplicitTarget: ({ raw }) => parseWhatsAppExplicitTarget(raw),
124
+ inferTargetChatType: ({ to }) => parseWhatsAppExplicitTarget(to)?.chatType,
125
+ targetResolver: {
126
+ looksLikeId: looksLikeWhatsAppTargetId,
127
+ hint: "<E.164|group JID|newsletter JID>",
128
+ },
129
+ },
130
+ message: whatsappMessageAdapter,
131
+ directory: {
132
+ self: async ({ cfg, accountId }) => {
133
+ const account = resolveWhatsAppAccount({ cfg, accountId });
134
+ const { e164, jid } = (await loadWhatsAppChannelRuntime()).readWebSelfId(account.authDir);
135
+ const id = e164 ?? jid;
136
+ if (!id) {
137
+ return null;
138
+ }
139
+ return {
140
+ kind: "user",
141
+ id,
142
+ name: account.name,
143
+ raw: { e164, jid },
144
+ };
145
+ },
146
+ listPeers: async (params) =>
147
+ (await loadWhatsAppDirectoryConfig()).listWhatsAppDirectoryPeersFromConfig(params),
148
+ listGroups: async (params) =>
149
+ (await loadWhatsAppDirectoryConfig()).listWhatsAppDirectoryGroupsFromConfig(params),
150
+ },
151
+ actions: {
152
+ describeMessageTool: ({ cfg, accountId }) =>
153
+ describeWhatsAppMessageActions({ cfg, accountId }),
154
+ supportsAction: ({ action }) => action === "react" || action === "upload-file",
155
+ resolveExecutionMode: ({ action }) =>
156
+ action === "react" || action === "upload-file" ? "gateway" : "local",
157
+ handleAction: async ({
158
+ action,
159
+ params,
160
+ cfg,
161
+ accountId,
162
+ requesterSenderId,
163
+ mediaAccess,
164
+ mediaLocalRoots,
165
+ mediaReadFile,
166
+ toolContext,
167
+ }) =>
168
+ await (
169
+ await loadWhatsAppChannelReactAction()
170
+ ).handleWhatsAppMessageAction({
171
+ action,
172
+ params,
173
+ cfg,
174
+ accountId,
175
+ requesterSenderId,
176
+ mediaAccess,
177
+ mediaLocalRoots,
178
+ mediaReadFile,
179
+ toolContext,
180
+ }),
181
+ },
182
+ approvalCapability: whatsappApprovalAuth,
183
+ auth: {
184
+ login: async ({ cfg, accountId, runtime, verbose }) => {
185
+ const resolvedAccountId =
186
+ accountId?.trim() ||
187
+ whatsappPlugin.config.defaultAccountId?.(cfg) ||
188
+ DEFAULT_ACCOUNT_ID;
189
+ await (
190
+ await loadWhatsAppChannelRuntime()
191
+ ).loginWeb(Boolean(verbose), undefined, runtime, resolvedAccountId);
192
+ },
193
+ },
194
+ lifecycle: {
195
+ detectLegacyStateMigrations: ({ oauthDir }) =>
196
+ detectWhatsAppLegacyStateMigrations({ oauthDir }),
197
+ },
198
+ heartbeat: {
199
+ checkReady: async ({ cfg, accountId, deps }) =>
200
+ await checkWhatsAppHeartbeatReady({ cfg, accountId: accountId ?? undefined, deps }),
201
+ sendTyping: async ({ cfg, to, accountId }) => {
202
+ await sendTypingWhatsApp(to, {
203
+ cfg,
204
+ ...(accountId ? { accountId } : {}),
205
+ });
206
+ },
207
+ },
208
+ status: createAsyncComputedAccountStatusAdapter<ResolvedWhatsAppAccount>({
209
+ defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID, {
210
+ connected: false,
211
+ reconnectAttempts: 0,
212
+ lastConnectedAt: null,
213
+ lastDisconnect: null,
214
+ lastInboundAt: null,
215
+ lastMessageAt: null,
216
+ lastEventAt: null,
217
+ healthState: "stopped",
218
+ }),
219
+ collectStatusIssues: collectWhatsAppStatusIssues,
220
+ buildChannelSummary: async ({ account, snapshot }) => {
221
+ const channelRuntime = await loadWhatsAppChannelRuntime();
222
+ const authDir = account.authDir;
223
+ const auth = authDir
224
+ ? await channelRuntime.readWebAuthSnapshot(authDir)
225
+ : {
226
+ state: "not-linked" as const,
227
+ authAgeMs: null,
228
+ selfId: { e164: null, jid: null, lid: null },
229
+ };
230
+ const linked =
231
+ typeof snapshot.linked === "boolean"
232
+ ? snapshot.linked
233
+ : auth.state === "unstable"
234
+ ? undefined
235
+ : auth.state === "linked";
236
+ const summaryAuthState =
237
+ auth.state === "unstable"
238
+ ? auth.state
239
+ : linked === true
240
+ ? "linked"
241
+ : linked === false
242
+ ? "not-linked"
243
+ : undefined;
244
+ const statusState = summaryAuthState === undefined ? undefined : summaryAuthState;
245
+ const configured =
246
+ auth.state === "unstable"
247
+ ? typeof snapshot.configured === "boolean"
248
+ ? snapshot.configured
249
+ : true
250
+ : typeof linked === "boolean"
251
+ ? linked
252
+ : auth.state === "linked";
253
+ const authAgeMs = typeof linked === "boolean" && linked ? auth.authAgeMs : null;
254
+ const self =
255
+ typeof linked === "boolean" && linked
256
+ ? auth.selfId
257
+ : { e164: null, jid: null, lid: null };
258
+ return {
259
+ configured,
260
+ ...(statusState ? { statusState } : {}),
261
+ ...(typeof linked === "boolean" ? { linked } : {}),
262
+ authAgeMs,
263
+ self,
264
+ running: snapshot.running ?? false,
265
+ connected: snapshot.connected ?? false,
266
+ lastConnectedAt: snapshot.lastConnectedAt ?? null,
267
+ lastDisconnect: snapshot.lastDisconnect ?? null,
268
+ reconnectAttempts: snapshot.reconnectAttempts,
269
+ lastInboundAt: snapshot.lastInboundAt ?? snapshot.lastMessageAt ?? null,
270
+ lastMessageAt: snapshot.lastMessageAt ?? null,
271
+ lastEventAt: snapshot.lastEventAt ?? null,
272
+ lastError: snapshot.lastError ?? null,
273
+ healthState: snapshot.healthState ?? undefined,
274
+ };
275
+ },
276
+ resolveAccountSnapshot: async ({ account, runtime }) => {
277
+ const channelRuntime = await loadWhatsAppChannelRuntime();
278
+ const authState = await channelRuntime.readWebAuthState(account.authDir);
279
+ return {
280
+ accountId: account.accountId,
281
+ name: account.name,
282
+ enabled: account.enabled,
283
+ configured: true,
284
+ extra: {
285
+ statusState: authState,
286
+ ...(authState === "linked"
287
+ ? { linked: true }
288
+ : authState === "not-linked"
289
+ ? { linked: false }
290
+ : {}),
291
+ connected: runtime?.connected ?? false,
292
+ reconnectAttempts: runtime?.reconnectAttempts,
293
+ lastConnectedAt: runtime?.lastConnectedAt ?? null,
294
+ lastDisconnect: runtime?.lastDisconnect ?? null,
295
+ lastInboundAt: runtime?.lastInboundAt ?? runtime?.lastMessageAt ?? null,
296
+ lastMessageAt: runtime?.lastMessageAt ?? null,
297
+ lastEventAt: runtime?.lastEventAt ?? null,
298
+ healthState: runtime?.healthState ?? undefined,
299
+ dmPolicy: account.dmPolicy,
300
+ allowFrom: account.allowFrom,
301
+ },
302
+ };
303
+ },
304
+ resolveAccountState: ({ configured }) => (configured ? "linked" : "not linked"),
305
+ logSelfId: ({ account, runtime, includeChannelPrefix }) => {
306
+ void loadWhatsAppChannelRuntime().then((runtimeExports) =>
307
+ runtimeExports.logWebSelfId(account.authDir, runtime, includeChannelPrefix),
308
+ );
309
+ },
310
+ }),
311
+ gateway: {
312
+ startAccount: async (ctx) => {
313
+ const account = ctx.account;
314
+ const { e164, jid } = (await loadWhatsAppChannelRuntime()).readWebSelfId(account.authDir);
315
+ const identity = e164 ? e164 : jid ? `jid ${jid}` : "unknown";
316
+ ctx.log?.info(`[${account.accountId}] starting provider (${identity})`);
317
+ return (await loadWhatsAppChannelRuntime()).monitorWebChannel(
318
+ getWhatsAppRuntime().logging.shouldLogVerbose(),
319
+ undefined,
320
+ true,
321
+ undefined,
322
+ ctx.runtime,
323
+ ctx.abortSignal,
324
+ {
325
+ statusSink: (next: WebChannelStatus) =>
326
+ ctx.setStatus({ accountId: ctx.accountId, ...next }),
327
+ accountId: account.accountId,
328
+ },
329
+ );
330
+ },
331
+ loginWithQrStart: async ({ accountId, force, timeoutMs, verbose }) =>
332
+ await (
333
+ await loadWhatsAppChannelRuntime()
334
+ ).startWebLoginWithQr({
335
+ accountId,
336
+ force,
337
+ timeoutMs,
338
+ verbose,
339
+ }),
340
+ loginWithQrWait: async ({ accountId, timeoutMs, currentQrDataUrl }) =>
341
+ await (
342
+ await loadWhatsAppChannelRuntime()
343
+ ).waitForWebLogin({ accountId, timeoutMs, currentQrDataUrl }),
344
+ logoutAccount: async ({ account, runtime }) => {
345
+ const cleared = await (
346
+ await loadWhatsAppChannelRuntime()
347
+ ).logoutWeb({
348
+ authDir: account.authDir,
349
+ isLegacyAuthDir: account.isLegacyAuthDir,
350
+ runtime,
351
+ });
352
+ return { cleared, loggedOut: cleared };
353
+ },
354
+ },
355
+ },
356
+ });
@@ -0,0 +1,7 @@
1
+ import type { ChannelPlugin } from "autobot/plugin-sdk/core";
2
+
3
+ export const whatsappCommandPolicy: NonNullable<ChannelPlugin["commands"]> = {
4
+ enforceOwnerForCommands: true,
5
+ preferSenderE164ForCommands: true,
6
+ skipWhenConfigEmpty: true,
7
+ };
@@ -0,0 +1,22 @@
1
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
2
+ import { resolveWhatsAppAccount } from "./accounts.js";
3
+ import { normalizeWhatsAppAllowFromEntries } from "./normalize-target.js";
4
+
5
+ export function resolveWhatsAppConfigAllowFrom(params: {
6
+ cfg: AutoBotConfig;
7
+ accountId?: string | null;
8
+ }): string[] {
9
+ return [...(resolveWhatsAppAccount(params).allowFrom ?? [])];
10
+ }
11
+
12
+ export function formatWhatsAppConfigAllowFromEntries(allowFrom: Array<string | number>): string[] {
13
+ return normalizeWhatsAppAllowFromEntries(allowFrom);
14
+ }
15
+
16
+ export function resolveWhatsAppConfigDefaultTo(params: {
17
+ cfg: AutoBotConfig;
18
+ accountId?: string | null;
19
+ }): string | undefined {
20
+ const defaultTo = resolveWhatsAppAccount(params).defaultTo?.trim();
21
+ return defaultTo || undefined;
22
+ }
@@ -0,0 +1,6 @@
1
+ import { buildChannelConfigSchema, WhatsAppConfigSchema } from "../config-api.js";
2
+ import { whatsAppChannelConfigUiHints } from "./config-ui-hints.js";
3
+
4
+ export const WhatsAppChannelConfigSchema = buildChannelConfigSchema(WhatsAppConfigSchema, {
5
+ uiHints: whatsAppChannelConfigUiHints,
6
+ });
@@ -0,0 +1,24 @@
1
+ import type { ChannelConfigUiHint } from "autobot/plugin-sdk/core";
2
+
3
+ export const whatsAppChannelConfigUiHints = {
4
+ "": {
5
+ label: "WhatsApp",
6
+ help: "WhatsApp channel provider configuration for access policy and message batching behavior. Use this section to tune responsiveness and direct-message routing safety for WhatsApp chats.",
7
+ },
8
+ dmPolicy: {
9
+ label: "WhatsApp DM Policy",
10
+ help: 'Direct message access control ("pairing" recommended). "open" requires channels.whatsapp.allowFrom=["*"].',
11
+ },
12
+ selfChatMode: {
13
+ label: "WhatsApp Self-Phone Mode",
14
+ help: "Same-phone setup (bot uses your personal WhatsApp number).",
15
+ },
16
+ debounceMs: {
17
+ label: "WhatsApp Message Debounce (ms)",
18
+ help: "Debounce window (ms) for batching rapid consecutive messages from the same sender (0 to disable).",
19
+ },
20
+ configWrites: {
21
+ label: "WhatsApp Config Writes",
22
+ help: "Allow WhatsApp to write config in response to channel events/commands (default: true).",
23
+ },
24
+ } satisfies Record<string, ChannelConfigUiHint>;
@@ -0,0 +1,49 @@
1
+ import type { ActiveWebListener } from "./inbound/types.js";
2
+
3
+ type WhatsAppConnectionControllerHandle = {
4
+ getActiveListener(): ActiveWebListener | null;
5
+ };
6
+
7
+ type ConnectionRegistryState = {
8
+ controllers: Map<string, WhatsAppConnectionControllerHandle>;
9
+ };
10
+
11
+ const CONNECTION_REGISTRY_KEY = Symbol.for("autobot.whatsapp.connectionControllerRegistry");
12
+
13
+ function getConnectionRegistryState(): ConnectionRegistryState {
14
+ const globalState = globalThis as typeof globalThis & {
15
+ [CONNECTION_REGISTRY_KEY]?: ConnectionRegistryState;
16
+ };
17
+ const existing = globalState[CONNECTION_REGISTRY_KEY];
18
+ if (existing) {
19
+ return existing;
20
+ }
21
+ const created: ConnectionRegistryState = {
22
+ controllers: new Map<string, WhatsAppConnectionControllerHandle>(),
23
+ };
24
+ globalState[CONNECTION_REGISTRY_KEY] = created;
25
+ return created;
26
+ }
27
+
28
+ export function getRegisteredWhatsAppConnectionController(
29
+ accountId: string,
30
+ ): WhatsAppConnectionControllerHandle | null {
31
+ return getConnectionRegistryState().controllers.get(accountId) ?? null;
32
+ }
33
+
34
+ export function registerWhatsAppConnectionController(
35
+ accountId: string,
36
+ controller: WhatsAppConnectionControllerHandle,
37
+ ): void {
38
+ getConnectionRegistryState().controllers.set(accountId, controller);
39
+ }
40
+
41
+ export function unregisterWhatsAppConnectionController(
42
+ accountId: string,
43
+ controller: WhatsAppConnectionControllerHandle,
44
+ ): void {
45
+ const controllers = getConnectionRegistryState().controllers;
46
+ if (controllers.get(accountId) === controller) {
47
+ controllers.delete(accountId);
48
+ }
49
+ }