@@ -9,16 +9,114 @@ import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
99import { jidToE164 } from "openclaw/plugin-sdk/text-runtime" ;
1010import { parseVcard } from "../vcard.js" ;
1111
12+ const MESSAGE_WRAPPER_KEYS = [
13+ "ephemeralMessage" ,
14+ "viewOnceMessage" ,
15+ "viewOnceMessageV2" ,
16+ "viewOnceMessageV2Extension" ,
17+ "documentWithCaptionMessage" ,
18+ ] as const ;
19+
20+ const MESSAGE_CONTENT_KEYS = [
21+ "conversation" ,
22+ "extendedTextMessage" ,
23+ "imageMessage" ,
24+ "videoMessage" ,
25+ "audioMessage" ,
26+ "documentMessage" ,
27+ "stickerMessage" ,
28+ "locationMessage" ,
29+ "liveLocationMessage" ,
30+ "contactMessage" ,
31+ "contactsArrayMessage" ,
32+ "buttonsResponseMessage" ,
33+ "listResponseMessage" ,
34+ "templateButtonReplyMessage" ,
35+ "interactiveResponseMessage" ,
36+ "buttonsMessage" ,
37+ "listMessage" ,
38+ ] as const ;
39+
40+ function fallbackNormalizeMessageContent (
41+ message : proto . IMessage | undefined ,
42+ ) : proto . IMessage | undefined {
43+ let current = message as unknown ;
44+ while ( current && typeof current === "object" ) {
45+ let unwrapped = false ;
46+ for ( const key of MESSAGE_WRAPPER_KEYS ) {
47+ const candidate = ( current as Record < string , unknown > ) [ key ] ;
48+ if (
49+ candidate &&
50+ typeof candidate === "object" &&
51+ "message" in ( candidate as Record < string , unknown > ) &&
52+ ( candidate as { message ?: unknown } ) . message
53+ ) {
54+ current = ( candidate as { message : unknown } ) . message ;
55+ unwrapped = true ;
56+ break ;
57+ }
58+ }
59+ if ( ! unwrapped ) {
60+ break ;
61+ }
62+ }
63+ return current as proto . IMessage | undefined ;
64+ }
65+
66+ function normalizeMessage ( message : proto . IMessage | undefined ) : proto . IMessage | undefined {
67+ if ( typeof normalizeMessageContent === "function" ) {
68+ return normalizeMessageContent ( message ) ;
69+ }
70+ return fallbackNormalizeMessageContent ( message ) ;
71+ }
72+
73+ function fallbackGetContentType (
74+ message : proto . IMessage | undefined ,
75+ ) : keyof proto . IMessage | undefined {
76+ const normalized = fallbackNormalizeMessageContent ( message ) ;
77+ if ( ! normalized || typeof normalized !== "object" ) {
78+ return undefined ;
79+ }
80+ for ( const key of MESSAGE_CONTENT_KEYS ) {
81+ if ( ( normalized as Record < string , unknown > ) [ key ] != null ) {
82+ return key as keyof proto . IMessage ;
83+ }
84+ }
85+ return undefined ;
86+ }
87+
88+ function getMessageContentType (
89+ message : proto . IMessage | undefined ,
90+ ) : keyof proto . IMessage | undefined {
91+ if ( typeof getContentType === "function" ) {
92+ return getContentType ( message ) ;
93+ }
94+ return fallbackGetContentType ( message ) ;
95+ }
96+
97+ function extractMessage ( message : proto . IMessage | undefined ) : proto . IMessage | undefined {
98+ if ( typeof extractMessageContent === "function" ) {
99+ return extractMessageContent ( message ) as proto . IMessage | undefined ;
100+ }
101+ const normalized = fallbackNormalizeMessageContent ( message ) ;
102+ const contentType = fallbackGetContentType ( normalized ) ;
103+ if ( ! normalized || ! contentType || contentType === "conversation" ) {
104+ return normalized ;
105+ }
106+ const candidate = ( normalized as Record < string , unknown > ) [ contentType ] ;
107+ return candidate && typeof candidate === "object" ? ( candidate as proto . IMessage ) : normalized ;
108+ }
109+
12110function unwrapMessage ( message : proto . IMessage | undefined ) : proto . IMessage | undefined {
13- const normalized = normalizeMessageContent ( message ) ;
111+ const normalized = normalizeMessage ( message ) ;
14112 return normalized ;
15113}
16114
17115function extractContextInfo ( message : proto . IMessage | undefined ) : proto . IContextInfo | undefined {
18116 if ( ! message ) {
19117 return undefined ;
20118 }
21- const contentType = getContentType ( message ) ;
119+ const contentType = getMessageContentType ( message ) ;
22120 const candidate = contentType ? ( message as Record < string , unknown > ) [ contentType ] : undefined ;
23121 const contextInfo =
24122 candidate && typeof candidate === "object" && "contextInfo" in candidate
@@ -89,7 +187,7 @@ export function extractText(rawMessage: proto.IMessage | undefined): string | un
89187 if ( ! message ) {
90188 return undefined ;
91189 }
92- const extracted = extractMessageContent ( message ) ;
190+ const extracted = extractMessage ( message ) ;
93191 const candidates = [ message , extracted && extracted !== message ? extracted : undefined ] ;
94192 for ( const candidate of candidates ) {
95193 if ( ! candidate ) {
@@ -300,7 +398,7 @@ export function describeReplyContext(rawMessage: proto.IMessage | undefined): {
300398 return null ;
301399 }
302400 const contextInfo = extractContextInfo ( message ) ;
303- const quoted = normalizeMessageContent ( contextInfo ?. quotedMessage as proto . IMessage | undefined ) ;
401+ const quoted = normalizeMessage ( contextInfo ?. quotedMessage as proto . IMessage | undefined ) ;
304402 if ( ! quoted ) {
305403 return null ;
306404 }
@@ -312,7 +410,7 @@ export function describeReplyContext(rawMessage: proto.IMessage | undefined): {
312410 body = extractMediaPlaceholder ( quoted ) ;
313411 }
314412 if ( ! body ) {
315- const quotedType = quoted ? getContentType ( quoted ) : undefined ;
413+ const quotedType = quoted ? getMessageContentType ( quoted ) : undefined ;
316414 logVerbose (
317415 `Quoted message missing extractable body${ quotedType ? ` (type ${ quotedType } )` : "" } ` ,
318416 ) ;
0 commit comments