Skip to content

Commit fcc5e6c

Browse files
committed
1 parent 71b5de4 commit fcc5e6c

5 files changed

Lines changed: 85 additions & 53 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,8 +1416,8 @@ extensions/whatsapp/src/inbound/extract.ts 10
14161416
extensions/whatsapp/src/inbound/group-metadata-cache.ts 7
14171417
extensions/whatsapp/src/inbound/ingress-lifecycle.ts 2
14181418
extensions/whatsapp/src/inbound/media.ts 4
1419-
extensions/whatsapp/src/inbound/message-delivery.ts 2
1420-
extensions/whatsapp/src/inbound/message-enrichment.ts 4
1419+
extensions/whatsapp/src/inbound/message-delivery.ts 1
1420+
extensions/whatsapp/src/inbound/message-enrichment.ts 3
14211421
extensions/whatsapp/src/inbound/outbound-mentions.ts 1
14221422
extensions/whatsapp/src/inbound/send-api.ts 5
14231423
extensions/whatsapp/src/inbound/socket-session.ts 5

‎extensions/whatsapp/src/inbound/extract.ts‎

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ function getFutureProofInnerMessage(message: proto.IMessage): proto.IMessage | u
3535
return undefined;
3636
}
3737

38-
function buildMessageChain(message: proto.IMessage | undefined): proto.IMessage[] {
38+
type WhatsAppInboundMessageProjection = readonly proto.IMessage[];
39+
type WhatsAppInboundMessageSource = proto.IMessage | WhatsAppInboundMessageProjection | undefined;
40+
41+
export function projectWhatsAppInboundMessage(
42+
message: proto.IMessage | undefined,
43+
): WhatsAppInboundMessageProjection {
3944
const chain: proto.IMessage[] = [];
4045
let current = normalizeMessageContent(message);
4146
while (current && chain.length < 4) {
@@ -45,11 +50,25 @@ function buildMessageChain(message: proto.IMessage | undefined): proto.IMessage[
4550
return chain;
4651
}
4752

53+
function isWhatsAppInboundMessageProjection(
54+
message: WhatsAppInboundMessageSource,
55+
): message is WhatsAppInboundMessageProjection {
56+
return Array.isArray(message);
57+
}
58+
59+
function resolveWhatsAppInboundMessageProjection(
60+
message: WhatsAppInboundMessageSource,
61+
): WhatsAppInboundMessageProjection {
62+
return isWhatsAppInboundMessageProjection(message)
63+
? message
64+
: projectWhatsAppInboundMessage(message);
65+
}
66+
4867
export function findMessageSection<K extends keyof proto.IMessage>(
4968
rawMessage: proto.IMessage | undefined,
5069
sectionNames: readonly K[],
5170
): { name: K; value: Record<string, unknown> } | undefined {
52-
const chain = buildMessageChain(rawMessage);
71+
const chain = projectWhatsAppInboundMessage(rawMessage);
5372
for (const name of sectionNames) {
5473
for (const message of chain) {
5574
const value = message[name];
@@ -61,9 +80,8 @@ export function findMessageSection<K extends keyof proto.IMessage>(
6180
return undefined;
6281
}
6382

64-
function unwrapMessage(message: proto.IMessage | undefined): proto.IMessage | undefined {
65-
const chain = buildMessageChain(message);
66-
return chain.at(-1);
83+
function unwrapMessage(message: WhatsAppInboundMessageSource): proto.IMessage | undefined {
84+
return resolveWhatsAppInboundMessageProjection(message).at(-1);
6785
}
6886

6987
function extractContextInfoFromMessage(message: proto.IMessage): proto.IContextInfo | undefined {
@@ -117,9 +135,9 @@ function extractContextInfoFromMessage(message: proto.IMessage): proto.IContextI
117135
}
118136

119137
export function extractContextInfo(
120-
message: proto.IMessage | undefined,
138+
message: WhatsAppInboundMessageSource,
121139
): proto.IContextInfo | undefined {
122-
for (const candidate of buildMessageChain(message)) {
140+
for (const candidate of resolveWhatsAppInboundMessageProjection(message)) {
123141
const contextInfo = extractContextInfoFromMessage(candidate);
124142
if (contextInfo) {
125143
return contextInfo;
@@ -128,9 +146,9 @@ export function extractContextInfo(
128146
return undefined;
129147
}
130148

131-
export function extractMentionedJids(rawMessage: proto.IMessage | undefined): string[] | undefined {
149+
export function extractMentionedJids(message: WhatsAppInboundMessageSource): string[] | undefined {
132150
// Context ownership already follows Baileys envelopes without entering quoted messages.
133-
const mentionedJids = extractContextInfo(rawMessage)?.mentionedJid?.filter(Boolean);
151+
const mentionedJids = extractContextInfo(message)?.mentionedJid?.filter(Boolean);
134152
if (!mentionedJids?.length) {
135153
return undefined;
136154
}
@@ -157,8 +175,9 @@ function extractNativeFlowResponseText(
157175
}
158176
}
159177

160-
export function extractText(rawMessage: proto.IMessage | undefined): string | undefined {
161-
const message = unwrapMessage(rawMessage);
178+
export function extractText(source: WhatsAppInboundMessageSource): string | undefined {
179+
const projection = resolveWhatsAppInboundMessageProjection(source);
180+
const message = unwrapMessage(projection);
162181
if (!message) {
163182
return undefined;
164183
}
@@ -215,7 +234,7 @@ export function extractText(rawMessage: proto.IMessage | undefined): string | un
215234
}
216235
}
217236
const contactPlaceholder =
218-
extractContactPlaceholder(message) ??
237+
extractContactPlaceholder(projection) ??
219238
(extracted && extracted !== message
220239
? extractContactPlaceholder(extracted as proto.IMessage | undefined)
221240
: undefined);
@@ -225,14 +244,14 @@ export function extractText(rawMessage: proto.IMessage | undefined): string | un
225244
return undefined;
226245
}
227246

228-
export function extractExternalAdReplyContext(rawMessage: proto.IMessage | undefined):
247+
export function extractExternalAdReplyContext(source: WhatsAppInboundMessageSource):
229248
| {
230249
title?: string;
231250
sourceUrl?: string;
232251
body?: string;
233252
}
234253
| undefined {
235-
const message = unwrapMessage(rawMessage);
254+
const message = unwrapMessage(source);
236255
const adReply =
237256
message?.imageMessage?.contextInfo?.externalAdReply ??
238257
message?.videoMessage?.contextInfo?.externalAdReply;
@@ -246,9 +265,9 @@ export function extractExternalAdReplyContext(rawMessage: proto.IMessage | undef
246265
}
247266

248267
export function extractMediaKind(
249-
rawMessage: proto.IMessage | undefined,
268+
source: WhatsAppInboundMessageSource,
250269
): NonNullable<ChannelInboundMediaInput["kind"]> | undefined {
251-
const message = unwrapMessage(rawMessage);
270+
const message = unwrapMessage(source);
252271
if (!message) {
253272
return undefined;
254273
}
@@ -271,8 +290,8 @@ export function extractMediaKind(
271290
return undefined;
272291
}
273292

274-
function extractContactPlaceholder(rawMessage: proto.IMessage | undefined): string | undefined {
275-
const contactContext = extractContactContext(rawMessage);
293+
function extractContactPlaceholder(source: WhatsAppInboundMessageSource): string | undefined {
294+
const contactContext = extractContactContext(source);
276295
if (!contactContext) {
277296
return undefined;
278297
}
@@ -284,9 +303,9 @@ function extractContactPlaceholder(rawMessage: proto.IMessage | undefined): stri
284303
}
285304

286305
export function extractContactContext(
287-
rawMessage: proto.IMessage | undefined,
306+
source: WhatsAppInboundMessageSource,
288307
): WhatsAppStructuredContactContext | undefined {
289-
const message = unwrapMessage(rawMessage);
308+
const message = unwrapMessage(source);
290309
if (!message) {
291310
return undefined;
292311
}
@@ -326,9 +345,9 @@ function describeContact(input: { displayName?: string | null; vcard?: string |
326345
}
327346

328347
export function extractLocationData(
329-
rawMessage: proto.IMessage | undefined,
348+
source: WhatsAppInboundMessageSource,
330349
): NormalizedLocation | null {
331-
const message = unwrapMessage(rawMessage);
350+
const message = unwrapMessage(source);
332351
if (!message) {
333352
return null;
334353
}
@@ -380,13 +399,18 @@ export function extractLocationData(
380399
}
381400

382401
export function describeReplyContext(
383-
rawMessage: proto.IMessage | undefined,
402+
source: WhatsAppInboundMessageSource,
384403
): WhatsAppReplyContext | null {
385-
const message = unwrapMessage(rawMessage);
404+
const projection = resolveWhatsAppInboundMessageProjection(source);
405+
const message = unwrapMessage(projection);
386406
if (!message) {
387407
return null;
388408
}
389-
const contextInfo = extractContextInfo(message);
409+
const contextProjection =
410+
projection.length === 1 && projection[0] === message
411+
? projection
412+
: projectWhatsAppInboundMessage(message);
413+
const contextInfo = extractContextInfo(contextProjection);
390414
const quoted = normalizeMessageContent(contextInfo?.quotedMessage as proto.IMessage | undefined);
391415
if (!quoted && !contextInfo?.stanzaId) {
392416
return null;
@@ -404,11 +428,12 @@ export function describeReplyContext(
404428
sender,
405429
};
406430
}
407-
const location = extractLocationData(quoted);
431+
const quotedProjection = projectWhatsAppInboundMessage(quoted);
432+
const location = extractLocationData(quotedProjection);
408433
const locationText = location ? formatLocationText(location) : undefined;
409-
const text = extractText(quoted);
434+
const text = extractText(quotedProjection);
410435
const body = [text, locationText].filter(Boolean).join("\n").trim();
411-
const mediaKind = extractMediaKind(quoted);
436+
const mediaKind = extractMediaKind(quotedProjection);
412437
const media = mediaKind
413438
? { kind: mediaKind, contentType: resolveInboundMediaMimetype(quoted) }
414439
: undefined;
@@ -450,22 +475,23 @@ function hasInteractiveResponseContent(message: proto.IMessage | undefined): boo
450475
* `messages.upsert` stream as real messages but should not trigger pairing
451476
* access-control side effects.
452477
*/
453-
export function hasInboundUserContent(rawMessage: proto.IMessage | undefined): boolean {
454-
if (!rawMessage) {
478+
export function hasInboundUserContent(source: WhatsAppInboundMessageSource): boolean {
479+
const projection = resolveWhatsAppInboundMessageProjection(source);
480+
if (projection.length === 0) {
455481
return false;
456482
}
457-
if (extractText(rawMessage)) {
483+
if (extractText(projection)) {
458484
return true;
459485
}
460-
if (extractMediaKind(rawMessage)) {
486+
if (extractMediaKind(projection)) {
461487
return true;
462488
}
463-
if (extractLocationData(rawMessage)) {
489+
if (extractLocationData(projection)) {
464490
return true;
465491
}
466492
// Walk wrappers (ephemeral, viewOnce, etc.) — interactive responses
467493
// can arrive nested.
468-
for (const candidate of buildMessageChain(rawMessage)) {
494+
for (const candidate of projection) {
469495
if (hasInteractiveResponseContent(candidate)) {
470496
return true;
471497
}

‎extensions/whatsapp/src/inbound/media.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export async function downloadInboundMedia(
1616
msg: proto.IWebMessageInfo,
1717
sock: Awaited<ReturnType<typeof createWaSocket>>,
1818
maxBytes = 50 * 1024 * 1024,
19+
normalizedMessage?: proto.IMessage,
1920
): Promise<{ saved: SavedMedia; mimetype?: string; fileName?: string } | undefined> {
20-
const message = unwrapMessage(msg.message as proto.IMessage | undefined);
21+
const message = normalizedMessage ?? unwrapMessage(msg.message as proto.IMessage | undefined);
2122
if (!message) {
2223
return undefined;
2324
}

‎extensions/whatsapp/src/inbound/message-delivery.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
// Whatsapp plugin module owns inbound message admission and delivery.
22
import { createHash } from "node:crypto";
3-
import type {
4-
AnyMessageContent,
5-
MiscMessageGenerationOptions,
6-
proto,
7-
WAMessage,
8-
WASocket,
9-
} from "baileys";
3+
import type { AnyMessageContent, MiscMessageGenerationOptions, WAMessage, WASocket } from "baileys";
104
import { recordChannelActivity } from "openclaw/plugin-sdk/channel-activity-runtime";
115
import { getChildLogger } from "openclaw/plugin-sdk/logging-core";
126
import { parseStrictFiniteNumber } from "openclaw/plugin-sdk/number-runtime";
@@ -27,7 +21,6 @@ import {
2721
type WhatsAppIngressLifecycle,
2822
type WhatsAppReadReceiptTarget,
2923
} from "./durable-receive.js";
30-
import { extractMentionedJids } from "./extract.js";
3124
import type { WhatsAppGroupMetadataCacheOwner } from "./group-metadata-cache.js";
3225
import {
3326
createWhatsAppInboundMessageDebouncer,
@@ -270,7 +263,7 @@ export function createWhatsAppMessageDeliveryCoordinator(options: WhatsAppMessag
270263
return normalizeWhatsAppSendResult(result, "media");
271264
};
272265
const timestamp = inbound.messageTimestampMs;
273-
const mentionedJids = extractMentionedJids(msg.message as proto.IMessage | undefined);
266+
const mentionedJids = enriched.mentionedJids;
274267
const senderName = msg.pushName ?? undefined;
275268

276269
inboundLogger.info(

‎extensions/whatsapp/src/inbound/message-enrichment.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {
1414
extractExternalAdReplyContext,
1515
extractLocationData,
1616
extractMediaKind,
17+
extractMentionedJids,
1718
extractText,
19+
projectWhatsAppInboundMessage,
1820
} from "./extract.js";
1921
import { resolveInboundMediaMimetype } from "./media-mimetype.js";
2022
import { downloadInboundMedia, downloadQuotedInboundMedia } from "./media.js";
@@ -78,6 +80,7 @@ export type WhatsAppEnrichedInboundMessage = {
7880
mediaFileName?: string;
7981
mediaKind?: NonNullable<ReturnType<typeof extractMediaKind>>;
8082
nativeMedia?: MediaPlaceholderTextFact;
83+
mentionedJids?: string[];
8184
};
8285

8386
export async function enrichWhatsAppInboundMessage(params: {
@@ -87,12 +90,13 @@ export async function enrichWhatsAppInboundMessage(params: {
8790
logVerbose: (message: string) => void;
8891
}): Promise<WhatsAppEnrichedInboundMessage | null> {
8992
const { msg, sock } = params;
90-
const location = extractLocationData(msg.message ?? undefined);
93+
const messageProjection = projectWhatsAppInboundMessage(msg.message ?? undefined);
94+
const location = extractLocationData(messageProjection);
9195
const locationText = location ? formatLocationText(location) : undefined;
92-
const contactContext = extractContactContext(msg.message ?? undefined);
93-
const externalAdReplyContext = extractExternalAdReplyContext(msg.message ?? undefined);
94-
let mediaKind = extractMediaKind(msg.message ?? undefined);
95-
let body = extractText(msg.message ?? undefined);
96+
const contactContext = extractContactContext(messageProjection);
97+
const externalAdReplyContext = extractExternalAdReplyContext(messageProjection);
98+
let mediaKind = extractMediaKind(messageProjection);
99+
let body = extractText(messageProjection);
96100
if (locationText) {
97101
body = [body, locationText].filter(Boolean).join("\n").trim();
98102
}
@@ -101,7 +105,7 @@ export async function enrichWhatsAppInboundMessage(params: {
101105
}
102106
body = body ?? "";
103107
const commandBody = body;
104-
const replyContext = describeReplyContext(msg.message as proto.IMessage | undefined);
108+
const replyContext = describeReplyContext(messageProjection);
105109

106110
let mediaPath: string | undefined;
107111
let mediaType = mediaKind
@@ -123,8 +127,15 @@ export async function enrichWhatsAppInboundMessage(params: {
123127
mediaFileName = inboundMedia.fileName;
124128
};
125129
try {
130+
// Entry zero is exactly the Baileys normalization that downloadInboundMedia performed here
131+
// previously; later projection entries are extraction-only future-proof payloads.
126132
await saveInboundMedia(
127-
await downloadInboundMedia(msg as proto.IWebMessageInfo, sock, maxBytes),
133+
await downloadInboundMedia(
134+
msg as proto.IWebMessageInfo,
135+
sock,
136+
maxBytes,
137+
messageProjection[0],
138+
),
128139
);
129140
} catch (error) {
130141
logMediaMaterializationFailure({
@@ -175,5 +186,6 @@ export async function enrichWhatsAppInboundMessage(params: {
175186
mediaFileName,
176187
mediaKind,
177188
nativeMedia,
189+
mentionedJids: extractMentionedJids(messageProjection),
178190
};
179191
}

0 commit comments

Comments
 (0)