@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,61 @@
1
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
2
+ import type { MsgContext } from "autobot/plugin-sdk/reply-runtime";
3
+ import { formatError } from "../../session.js";
4
+ import { resolveStorePath, updateLastRoute } from "../config.runtime.js";
5
+
6
+ export function trackBackgroundTask(
7
+ backgroundTasks: Set<Promise<unknown>>,
8
+ task: Promise<unknown>,
9
+ ) {
10
+ backgroundTasks.add(task);
11
+ const cleanup = () => {
12
+ backgroundTasks.delete(task);
13
+ };
14
+ task.then(cleanup, cleanup);
15
+ }
16
+
17
+ export function updateLastRouteInBackground(params: {
18
+ cfg: AutoBotConfig;
19
+ backgroundTasks: Set<Promise<unknown>>;
20
+ storeAgentId: string;
21
+ sessionKey: string;
22
+ channel: "whatsapp";
23
+ to: string;
24
+ accountId?: string;
25
+ ctx?: MsgContext;
26
+ warn: (obj: unknown, msg: string) => void;
27
+ }) {
28
+ const storePath = resolveStorePath(params.cfg.session?.store, {
29
+ agentId: params.storeAgentId,
30
+ });
31
+ const task = updateLastRoute({
32
+ storePath,
33
+ sessionKey: params.sessionKey,
34
+ deliveryContext: {
35
+ channel: params.channel,
36
+ to: params.to,
37
+ accountId: params.accountId,
38
+ },
39
+ ctx: params.ctx,
40
+ }).catch((err) => {
41
+ params.warn(
42
+ {
43
+ error: formatError(err),
44
+ storePath,
45
+ sessionKey: params.sessionKey,
46
+ to: params.to,
47
+ },
48
+ "failed updating last route",
49
+ );
50
+ });
51
+ trackBackgroundTask(params.backgroundTasks, task);
52
+ }
53
+
54
+ export function awaitBackgroundTasks(backgroundTasks: Set<Promise<unknown>>) {
55
+ if (backgroundTasks.size === 0) {
56
+ return Promise.resolve();
57
+ }
58
+ return Promise.allSettled(backgroundTasks).then(() => {
59
+ backgroundTasks.clear();
60
+ });
61
+ }
@@ -0,0 +1,28 @@
1
+ export function formatWhatsAppInboundListeningLog(account: {
2
+ groups?: Record<string, unknown>;
3
+ groupPolicy: "open" | "allowlist" | "disabled";
4
+ hasGroupAllowFrom: boolean;
5
+ }): string {
6
+ if (account.groupPolicy === "disabled") {
7
+ return "Listening for WhatsApp inbound messages (DM + groups disabled by groupPolicy).";
8
+ }
9
+ if (account.groupPolicy === "allowlist" && !account.hasGroupAllowFrom) {
10
+ return "Listening for WhatsApp inbound messages (DM + group inbound blocked by empty groupPolicy allowlist).";
11
+ }
12
+
13
+ const groups = account.groups ?? {};
14
+ if (Object.keys(groups).length === 0) {
15
+ const suffix =
16
+ account.groupPolicy === "allowlist"
17
+ ? "sender allowlist configured"
18
+ : "no group allowlist configured";
19
+ return `Listening for WhatsApp inbound messages (DM + all groups; ${suffix}).`;
20
+ }
21
+ if (Object.hasOwn(groups, "*")) {
22
+ return "Listening for WhatsApp inbound messages (DM + all groups; wildcard configured).";
23
+ }
24
+
25
+ const explicitGroupCount = Object.keys(groups).length;
26
+ const groupLabel = explicitGroupCount === 1 ? "group" : "groups";
27
+ return `Listening for WhatsApp inbound messages (DM + ${explicitGroupCount} configured ${groupLabel}).`;
28
+ }
@@ -0,0 +1,38 @@
1
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
2
+
3
+ export {
4
+ formatInboundEnvelope,
5
+ type EnvelopeFormatOptions,
6
+ } from "autobot/plugin-sdk/channel-envelope";
7
+
8
+ type WhatsAppMessagePrefixConfig = AutoBotConfig;
9
+
10
+ function normalizeAgentId(agentId: string): string {
11
+ return agentId.trim().toLowerCase() || "main";
12
+ }
13
+
14
+ function resolveIdentityNamePrefix(
15
+ cfg: WhatsAppMessagePrefixConfig,
16
+ agentId: string,
17
+ ): string | undefined {
18
+ const normalizedAgentId = normalizeAgentId(agentId);
19
+ const identityName = cfg.agents?.list
20
+ ?.find((agent) => normalizeAgentId(agent.id ?? "") === normalizedAgentId)
21
+ ?.identity?.name?.trim();
22
+ return identityName ? `[${identityName}]` : undefined;
23
+ }
24
+
25
+ export function resolveMessagePrefix(
26
+ cfg: WhatsAppMessagePrefixConfig,
27
+ agentId: string,
28
+ opts?: { configured?: string; hasAllowFrom?: boolean; fallback?: string },
29
+ ): string {
30
+ const configured = opts?.configured ?? cfg.messages?.messagePrefix;
31
+ if (configured !== undefined) {
32
+ return configured;
33
+ }
34
+ if (opts?.hasAllowFrom === true) {
35
+ return "";
36
+ }
37
+ return resolveIdentityNamePrefix(cfg, agentId) ?? opts?.fallback ?? "[autobot]";
38
+ }
@@ -0,0 +1,54 @@
1
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
2
+ import { getPrimaryIdentityId, getReplyContext, getSenderIdentity } from "../../identity.js";
3
+ import type { WebInboundMsg } from "../types.js";
4
+ import {
5
+ formatInboundEnvelope,
6
+ resolveMessagePrefix,
7
+ type EnvelopeFormatOptions,
8
+ } from "./message-line.runtime.js";
9
+
10
+ export function formatReplyContext(msg: WebInboundMsg) {
11
+ const replyTo = getReplyContext(msg);
12
+ if (!replyTo?.body) {
13
+ return null;
14
+ }
15
+ const sender = replyTo.sender?.label ?? replyTo.sender?.e164 ?? "unknown sender";
16
+ const idPart = replyTo.id ? ` id:${replyTo.id}` : "";
17
+ return `[Replying to ${sender}${idPart}]\n${replyTo.body}\n[/Replying]`;
18
+ }
19
+
20
+ export function buildInboundLine(params: {
21
+ cfg: AutoBotConfig;
22
+ msg: WebInboundMsg;
23
+ agentId: string;
24
+ previousTimestamp?: number;
25
+ envelope?: EnvelopeFormatOptions;
26
+ }) {
27
+ const { cfg, msg, agentId, previousTimestamp, envelope } = params;
28
+ // WhatsApp inbound prefix: channels.whatsapp.messagePrefix > legacy messages.messagePrefix > identity/defaults
29
+ const messagePrefix = resolveMessagePrefix(cfg, agentId, {
30
+ configured: cfg.channels?.whatsapp?.messagePrefix,
31
+ hasAllowFrom: (cfg.channels?.whatsapp?.allowFrom?.length ?? 0) > 0,
32
+ });
33
+ const prefixStr = messagePrefix ? `${messagePrefix} ` : "";
34
+ const replyContext = formatReplyContext(msg);
35
+ const baseLine = `${prefixStr}${msg.body}${replyContext ? `\n\n${replyContext}` : ""}`;
36
+ const sender = getSenderIdentity(msg);
37
+
38
+ // Wrap with standardized envelope for the agent.
39
+ return formatInboundEnvelope({
40
+ channel: "WhatsApp",
41
+ from: msg.chatType === "group" ? msg.from : msg.from?.replace(/^whatsapp:/, ""),
42
+ timestamp: msg.timestamp,
43
+ body: baseLine,
44
+ chatType: msg.chatType,
45
+ sender: {
46
+ name: sender.name ?? undefined,
47
+ e164: sender.e164 ?? undefined,
48
+ id: getPrimaryIdentityId(sender) ?? undefined,
49
+ },
50
+ previousTimestamp,
51
+ envelope,
52
+ fromMe: msg.fromMe,
53
+ });
54
+ }
@@ -0,0 +1,333 @@
1
+ import type { AckReactionHandle } from "autobot/plugin-sdk/channel-feedback";
2
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
3
+ import type { getReplyFromConfig } from "autobot/plugin-sdk/reply-runtime";
4
+ import type { MsgContext } from "autobot/plugin-sdk/reply-runtime";
5
+ import { resolveAgentRoute } from "autobot/plugin-sdk/routing";
6
+ import { buildGroupHistoryKey } from "autobot/plugin-sdk/routing";
7
+ import { logVerbose } from "autobot/plugin-sdk/runtime-env";
8
+ import { resolveWhatsAppAccount } from "../../accounts.js";
9
+ import { resolveWhatsAppGroupSessionRoute } from "../../group-session-key.js";
10
+ import { getPrimaryIdentityId, getSenderIdentity } from "../../identity.js";
11
+ import { normalizeE164 } from "../../text-runtime.js";
12
+ import { buildMentionConfig } from "../mentions.js";
13
+ import type { MentionConfig } from "../mentions.js";
14
+ import type { WebInboundMsg } from "../types.js";
15
+ import { maybeSendAckReaction } from "./ack-reaction.js";
16
+ import { maybeBroadcastMessage } from "./broadcast.js";
17
+ import type { EchoTracker } from "./echo.js";
18
+ import type { GroupHistoryEntry } from "./group-gating.js";
19
+ import { applyGroupGating } from "./group-gating.js";
20
+ import { updateLastRouteInBackground } from "./last-route.js";
21
+ import { resolvePeerId } from "./peer.js";
22
+ import { processMessage } from "./process-message.js";
23
+ import {
24
+ createWhatsAppStatusReactionController,
25
+ type StatusReactionController,
26
+ } from "./status-reaction.js";
27
+
28
+ export function createWebOnMessageHandler(params: {
29
+ cfg: AutoBotConfig;
30
+ loadConfig?: () => AutoBotConfig;
31
+ verbose: boolean;
32
+ connectionId: string;
33
+ maxMediaBytes: number;
34
+ groupHistoryLimit: number;
35
+ groupHistories: Map<string, GroupHistoryEntry[]>;
36
+ groupMemberNames: Map<string, Map<string, string>>;
37
+ echoTracker: EchoTracker;
38
+ backgroundTasks: Set<Promise<unknown>>;
39
+ replyResolver: typeof getReplyFromConfig;
40
+ replyLogger: ReturnType<(typeof import("autobot/plugin-sdk/runtime-env"))["getChildLogger"]>;
41
+ baseMentionConfig: MentionConfig;
42
+ account: { authDir?: string; accountId?: string; selfChatMode?: boolean };
43
+ }) {
44
+ const processForRoute = async (
45
+ cfg: AutoBotConfig,
46
+ msg: WebInboundMsg,
47
+ route: ReturnType<typeof resolveAgentRoute>,
48
+ groupHistoryKey: string,
49
+ opts?: {
50
+ groupHistory?: GroupHistoryEntry[];
51
+ suppressGroupHistoryClear?: boolean;
52
+ preflightAudioTranscript?: string | null;
53
+ ackAlreadySent?: boolean;
54
+ ackReaction?: AckReactionHandle | null;
55
+ statusReactionController?: StatusReactionController | null;
56
+ },
57
+ ) => {
58
+ const processParams: Parameters<typeof processMessage>[0] = {
59
+ cfg,
60
+ msg,
61
+ route,
62
+ groupHistoryKey,
63
+ groupHistories: params.groupHistories,
64
+ groupMemberNames: params.groupMemberNames,
65
+ connectionId: params.connectionId,
66
+ verbose: params.verbose,
67
+ maxMediaBytes: params.maxMediaBytes,
68
+ replyResolver: params.replyResolver,
69
+ replyLogger: params.replyLogger,
70
+ backgroundTasks: params.backgroundTasks,
71
+ rememberSentText: params.echoTracker.rememberText,
72
+ echoHas: params.echoTracker.has,
73
+ echoForget: params.echoTracker.forget,
74
+ buildCombinedEchoKey: params.echoTracker.buildCombinedKey,
75
+ };
76
+ if (opts?.groupHistory !== undefined) {
77
+ processParams.groupHistory = opts.groupHistory;
78
+ }
79
+ if (opts?.suppressGroupHistoryClear !== undefined) {
80
+ processParams.suppressGroupHistoryClear = opts.suppressGroupHistoryClear;
81
+ }
82
+ if (opts?.preflightAudioTranscript !== undefined) {
83
+ processParams.preflightAudioTranscript = opts.preflightAudioTranscript;
84
+ }
85
+ if (opts?.ackAlreadySent === true) {
86
+ processParams.ackAlreadySent = true;
87
+ }
88
+ if (opts?.ackReaction !== undefined) {
89
+ processParams.ackReaction = opts.ackReaction;
90
+ }
91
+ if (opts?.statusReactionController !== undefined) {
92
+ processParams.statusReactionController = opts.statusReactionController;
93
+ }
94
+ return processMessage(processParams);
95
+ };
96
+
97
+ return async (msg: WebInboundMsg) => {
98
+ const cfg = params.loadConfig?.() ?? params.cfg;
99
+ const conversationId = msg.conversationId ?? msg.from;
100
+ const peerId = resolvePeerId(msg);
101
+ const baseRoute = resolveAgentRoute({
102
+ cfg,
103
+ channel: "whatsapp",
104
+ accountId: msg.accountId,
105
+ peer: {
106
+ kind: msg.chatType === "group" ? "group" : "direct",
107
+ id: peerId,
108
+ },
109
+ });
110
+ const route =
111
+ msg.chatType === "group" ? resolveWhatsAppGroupSessionRoute(baseRoute) : baseRoute;
112
+ const groupHistoryKey =
113
+ msg.chatType === "group"
114
+ ? buildGroupHistoryKey({
115
+ channel: "whatsapp",
116
+ accountId: route.accountId,
117
+ peerKind: "group",
118
+ peerId,
119
+ })
120
+ : route.sessionKey;
121
+ const account = resolveWhatsAppAccount({
122
+ cfg,
123
+ accountId: route.accountId ?? msg.accountId ?? params.account.accountId,
124
+ });
125
+ const baseMentionConfig = buildMentionConfig(cfg);
126
+
127
+ // Same-phone mode logging retained
128
+ if (msg.from === msg.to) {
129
+ logVerbose(`📱 Same-phone mode detected (from === to: ${msg.from})`);
130
+ }
131
+
132
+ // Skip if this is a message we just sent (echo detection)
133
+ if (params.echoTracker.has(msg.body)) {
134
+ logVerbose("Skipping auto-reply: detected echo (message matches recently sent text)");
135
+ params.echoTracker.forget(msg.body);
136
+ return;
137
+ }
138
+
139
+ // Preflight audio transcription: run once before broadcast fan-out so all
140
+ // agents share the same transcript instead of each making a separate STT call.
141
+ // For DMs, only do this on the real inbound path after access-control/pairing
142
+ // checks have already passed in inbound/monitor.ts. For groups, the first
143
+ // gating pass must approve the group/sender before STT is attempted.
144
+ // null = preflight was attempted but produced no transcript (failed / disabled / no audio);
145
+ // undefined = preflight was not attempted (non-audio message).
146
+ let preflightAudioTranscript: string | null | undefined;
147
+ const hasAudioBody =
148
+ msg.mediaType?.startsWith("audio/") === true && msg.body === "<media:audio>";
149
+ const canRunEarlyAudioPreflight = msg.chatType === "group" || msg.accessControlPassed === true;
150
+ let ackAlreadySent = false;
151
+ let ackReaction: AckReactionHandle | null = null;
152
+ let statusReactionController: StatusReactionController | null = null;
153
+ const runAudioPreflightOnce = async () => {
154
+ if (
155
+ preflightAudioTranscript !== undefined ||
156
+ !canRunEarlyAudioPreflight ||
157
+ !hasAudioBody ||
158
+ !msg.mediaPath
159
+ ) {
160
+ return;
161
+ }
162
+ if (cfg.messages?.statusReactions?.enabled === true) {
163
+ statusReactionController = await createWhatsAppStatusReactionController({
164
+ cfg,
165
+ msg,
166
+ agentId: route.agentId,
167
+ sessionKey: route.sessionKey,
168
+ conversationId,
169
+ verbose: params.verbose,
170
+ accountId: route.accountId,
171
+ });
172
+ if (statusReactionController) {
173
+ await statusReactionController.setQueued();
174
+ }
175
+ } else {
176
+ ackReaction = await maybeSendAckReaction({
177
+ cfg,
178
+ msg,
179
+ agentId: route.agentId,
180
+ sessionKey: route.sessionKey,
181
+ conversationId,
182
+ verbose: params.verbose,
183
+ accountId: route.accountId,
184
+ info: params.replyLogger.info.bind(params.replyLogger),
185
+ warn: params.replyLogger.warn.bind(params.replyLogger),
186
+ });
187
+ ackAlreadySent = ackReaction !== null;
188
+ }
189
+ try {
190
+ const { transcribeFirstAudio } = await import("./audio-preflight.runtime.js");
191
+ // transcribeFirstAudio returns undefined on failure/disabled; store null so
192
+ // processMessage knows the attempt was already made and does not retry.
193
+ preflightAudioTranscript =
194
+ (await transcribeFirstAudio({
195
+ ctx: {
196
+ MediaPaths: [msg.mediaPath],
197
+ MediaTypes: msg.mediaType ? [msg.mediaType] : undefined,
198
+ From: msg.from,
199
+ To: msg.to,
200
+ Provider: "whatsapp",
201
+ Surface: "whatsapp",
202
+ OriginatingChannel: "whatsapp",
203
+ OriginatingTo: conversationId,
204
+ AccountId: route.accountId,
205
+ },
206
+ cfg,
207
+ })) ?? null;
208
+ } catch {
209
+ // Non-fatal: store null so per-agent retries are suppressed.
210
+ preflightAudioTranscript = null;
211
+ }
212
+ };
213
+
214
+ if (msg.chatType === "group") {
215
+ const sender = getSenderIdentity(msg);
216
+ const metaCtx = {
217
+ From: msg.from,
218
+ To: msg.to,
219
+ SessionKey: route.sessionKey,
220
+ AccountId: route.accountId,
221
+ ChatType: msg.chatType,
222
+ ConversationLabel: conversationId,
223
+ GroupSubject: msg.groupSubject,
224
+ SenderName: sender.name ?? undefined,
225
+ SenderId: getPrimaryIdentityId(sender) ?? undefined,
226
+ SenderE164: sender.e164 ?? undefined,
227
+ Provider: "whatsapp",
228
+ Surface: "whatsapp",
229
+ OriginatingChannel: "whatsapp",
230
+ OriginatingTo: conversationId,
231
+ } satisfies MsgContext;
232
+ updateLastRouteInBackground({
233
+ cfg,
234
+ backgroundTasks: params.backgroundTasks,
235
+ storeAgentId: route.agentId,
236
+ sessionKey: route.sessionKey,
237
+ channel: "whatsapp",
238
+ to: conversationId,
239
+ accountId: route.accountId,
240
+ ctx: metaCtx,
241
+ warn: params.replyLogger.warn.bind(params.replyLogger),
242
+ });
243
+
244
+ let gating = await applyGroupGating({
245
+ cfg,
246
+ msg,
247
+ deferMissingMention: hasAudioBody && Boolean(msg.mediaPath),
248
+ conversationId,
249
+ groupHistoryKey,
250
+ agentId: route.agentId,
251
+ sessionKey: route.sessionKey,
252
+ baseMentionConfig,
253
+ authDir: account.authDir,
254
+ selfChatMode: account.selfChatMode,
255
+ groupHistories: params.groupHistories,
256
+ groupHistoryLimit: params.groupHistoryLimit,
257
+ groupMemberNames: params.groupMemberNames,
258
+ logVerbose,
259
+ replyLogger: params.replyLogger,
260
+ });
261
+ if (
262
+ !gating.shouldProcess &&
263
+ "needsMentionText" in gating &&
264
+ gating.needsMentionText === true
265
+ ) {
266
+ await runAudioPreflightOnce();
267
+ gating = await applyGroupGating({
268
+ cfg,
269
+ msg,
270
+ ...(typeof preflightAudioTranscript === "string"
271
+ ? { mentionText: preflightAudioTranscript }
272
+ : {}),
273
+ conversationId,
274
+ groupHistoryKey,
275
+ agentId: route.agentId,
276
+ sessionKey: route.sessionKey,
277
+ baseMentionConfig,
278
+ authDir: account.authDir,
279
+ selfChatMode: account.selfChatMode,
280
+ groupHistories: params.groupHistories,
281
+ groupHistoryLimit: params.groupHistoryLimit,
282
+ groupMemberNames: params.groupMemberNames,
283
+ logVerbose,
284
+ replyLogger: params.replyLogger,
285
+ });
286
+ }
287
+ if (!gating.shouldProcess) {
288
+ return;
289
+ }
290
+ } else {
291
+ // Ensure `peerId` for DMs is stable and stored as E.164 when possible.
292
+ if (!msg.sender?.e164 && !msg.senderE164 && peerId && peerId.startsWith("+")) {
293
+ const normalized = normalizeE164(peerId);
294
+ if (normalized) {
295
+ msg.sender = { ...msg.sender, e164: normalized };
296
+ msg.senderE164 = normalized;
297
+ }
298
+ }
299
+ }
300
+
301
+ await runAudioPreflightOnce();
302
+
303
+ // Broadcast groups: when we'd reply anyway, run multiple agents.
304
+ // Does not bypass group mention/activation gating above.
305
+ if (
306
+ await maybeBroadcastMessage({
307
+ cfg,
308
+ msg,
309
+ peerId,
310
+ route,
311
+ groupHistoryKey,
312
+ groupHistories: params.groupHistories,
313
+ ...(preflightAudioTranscript !== undefined ? { preflightAudioTranscript } : {}),
314
+ // Group ack eligibility depends on the target agent/session, so a
315
+ // preflight ack attempt on the base route must not suppress downstream
316
+ // per-agent checks during broadcast fan-out.
317
+ ...(ackAlreadySent && msg.chatType !== "group" ? { ackAlreadySent: true } : {}),
318
+ ...(ackReaction && msg.chatType !== "group" ? { ackReaction } : {}),
319
+ ...(statusReactionController && msg.chatType !== "group" ? { ackAlreadySent: true } : {}),
320
+ processMessage: (m, r, k, opts) => processForRoute(cfg, m, r, k, opts),
321
+ })
322
+ ) {
323
+ return;
324
+ }
325
+
326
+ await processForRoute(cfg, msg, route, groupHistoryKey, {
327
+ ...(preflightAudioTranscript !== undefined ? { preflightAudioTranscript } : {}),
328
+ ...(ackAlreadySent ? { ackAlreadySent: true } : {}),
329
+ ...(ackReaction ? { ackReaction } : {}),
330
+ ...(statusReactionController ? { statusReactionController } : {}),
331
+ });
332
+ };
333
+ }
@@ -0,0 +1,17 @@
1
+ import { getSenderIdentity } from "../../identity.js";
2
+ import { jidToE164, normalizeE164 } from "../../text-runtime.js";
3
+ import type { WebInboundMsg } from "../types.js";
4
+
5
+ export function resolvePeerId(msg: WebInboundMsg) {
6
+ if (msg.chatType === "group") {
7
+ return msg.conversationId ?? msg.from;
8
+ }
9
+ const sender = getSenderIdentity(msg);
10
+ if (sender.e164) {
11
+ return normalizeE164(sender.e164) ?? sender.e164;
12
+ }
13
+ if (msg.from.includes("@")) {
14
+ return jidToE164(msg.from) ?? msg.from;
15
+ }
16
+ return normalizeE164(msg.from) ?? msg.from;
17
+ }