@@ -14,6 +14,7 @@ import {
1414import { logVerbose , shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env" ;
1515import type { WhatsAppSendResult } from "../inbound/send-result.js" ;
1616import { listWhatsAppSendResultMessageIds } from "../inbound/send-result.js" ;
17+ import type { WebInboundMessage } from "../inbound/types.js" ;
1718import { loadWebMedia } from "../media.js" ;
1819import {
1920 type DeliverableWhatsAppOutboundPayload ,
@@ -28,8 +29,7 @@ import { formatError } from "../session.js";
2829import { convertMarkdownTables } from "../text-runtime.js" ;
2930import { markdownToWhatsApp } from "../text-runtime.js" ;
3031import { whatsappOutboundLog } from "./loggers.js" ;
31- import type { WebInboundMsg } from "./types.js" ;
32- import { elide } from "./util.js" ;
32+ import { elide , markWhatsAppVisibleDeliveryError } from "./util.js" ;
3333
3434export type WhatsAppReplyDeliveryResult = {
3535 results : WhatsAppSendResult [ ] ;
@@ -84,24 +84,10 @@ function createWhatsAppReplyDeliveryReceipt(
8484 } ) ;
8585}
8686
87- function markWhatsAppVisibleDeliveryError ( error : unknown ) : unknown {
88- if ( typeof error === "object" && error !== null && ! Array . isArray ( error ) ) {
89- try {
90- Object . assign ( error , { sentBeforeError : true , visibleReplySent : true } ) ;
91- return error ;
92- } catch {
93- // Fall back to a wrapper when a platform error object is non-extensible.
94- }
95- }
96- const visibleError = new Error ( "visible WhatsApp reply delivery failed" , { cause : error } ) ;
97- Object . assign ( visibleError , { sentBeforeError : true , visibleReplySent : true } ) ;
98- return visibleError ;
99- }
100-
10187export async function deliverWebReply ( params : {
10288 replyResult : ReplyPayload ;
10389 normalizedReplyResult ?: DeliverableWhatsAppOutboundPayload < ReplyPayload > ;
104- msg : WebInboundMsg ;
90+ msg : WebInboundMessage ;
10591 mediaLocalRoots ?: readonly string [ ] ;
10692 maxMediaBytes : number ;
10793 textLimit : number ;
@@ -151,15 +137,20 @@ export async function deliverWebReply(params: {
151137 if ( ! replyResult . replyToId ) {
152138 return undefined ;
153139 }
154- // Use replyToId (not msg.id) so batched payloads quote the correct
140+ // Use replyToId (not msg.event. id) so batched payloads quote the correct
155141 // per-message target. Look up cached metadata for the specific
156- // message being quoted — msg.body may be a combined batch body.
157- const cached = lookupInboundMessageMeta ( msg . accountId , msg . chatId , replyResult . replyToId ) ;
142+ // message being quoted — msg.payload.body may be a combined batch body.
143+ const cached = lookupInboundMessageMeta (
144+ msg . accountId ,
145+ msg . platform . chatJid ,
146+ replyResult . replyToId ,
147+ ) ;
158148 return buildQuotedMessageOptions ( {
159149 messageId : replyResult . replyToId ,
160- remoteJid : msg . chatId ,
150+ remoteJid : msg . platform . chatJid ,
161151 fromMe : cached ?. fromMe ?? false ,
162- participant : cached ?. participant ?? ( msg . chatType === "group" ? msg . senderJid : undefined ) ,
152+ participant :
153+ cached ?. participant ?? ( msg . chatType === "group" ? msg . platform . senderJid : undefined ) ,
163154 messageText : cached ?. body ?? "" ,
164155 } ) ;
165156 } ;
@@ -189,7 +180,7 @@ export async function deliverWebReply(params: {
189180 for ( const [ index , chunk ] of textChunks . entries ( ) ) {
190181 const chunkStarted = Date . now ( ) ;
191182 const quote = getQuote ( ) ;
192- rememberSendResult ( await sendWithRetry ( ( ) => msg . reply ( chunk , quote ) , "text" ) ) ;
183+ rememberSendResult ( await sendWithRetry ( ( ) => msg . platform . reply ( chunk , quote ) , "text" ) ) ;
193184 if ( ! skipLog ) {
194185 const durationMs = Date . now ( ) - chunkStarted ;
195186 whatsappOutboundLog . debug (
@@ -199,10 +190,10 @@ export async function deliverWebReply(params: {
199190 }
200191 const delivery = finishDelivery ( ) ;
201192 const logPayload = {
202- correlationId : msg . id ?? newConnectionId ( ) ,
193+ correlationId : msg . event . id ?? newConnectionId ( ) ,
203194 connectionId : connectionId ?? null ,
204195 to : msg . from ,
205- from : msg . to ,
196+ from : msg . platform . recipientJid ,
206197 text : elide ( replyResult . text , 240 ) ,
207198 mediaUrl : null ,
208199 mediaSizeBytes : null ,
@@ -243,7 +234,7 @@ export async function deliverWebReply(params: {
243234 rememberSendResult (
244235 await sendWithRetry (
245236 ( ) =>
246- msg . sendMedia (
237+ msg . platform . sendMedia (
247238 {
248239 image : media . buffer ,
249240 caption,
@@ -259,7 +250,7 @@ export async function deliverWebReply(params: {
259250 rememberSendResult (
260251 await sendWithRetry (
261252 ( ) =>
262- msg . sendMedia (
253+ msg . platform . sendMedia (
263254 {
264255 audio : media . buffer ,
265256 ptt : true ,
@@ -272,15 +263,15 @@ export async function deliverWebReply(params: {
272263 ) ;
273264 if ( caption ) {
274265 rememberSendResult (
275- await sendWithRetry ( ( ) => msg . reply ( caption , quote ) , "media:audio-text" ) ,
266+ await sendWithRetry ( ( ) => msg . platform . reply ( caption , quote ) , "media:audio-text" ) ,
276267 ) ;
277268 }
278269 } else if ( media . kind === "video" ) {
279270 const quote = getQuote ( ) ;
280271 rememberSendResult (
281272 await sendWithRetry (
282273 ( ) =>
283- msg . sendMedia (
274+ msg . platform . sendMedia (
284275 {
285276 video : media . buffer ,
286277 caption,
@@ -296,7 +287,7 @@ export async function deliverWebReply(params: {
296287 rememberSendResult (
297288 await sendWithRetry (
298289 ( ) =>
299- msg . sendMedia (
290+ msg . platform . sendMedia (
300291 {
301292 document : media . buffer ,
302293 fileName : media . fileName ,
@@ -314,10 +305,10 @@ export async function deliverWebReply(params: {
314305 ) ;
315306 replyLogger . info (
316307 {
317- correlationId : msg . id ?? newConnectionId ( ) ,
308+ correlationId : msg . event . id ?? newConnectionId ( ) ,
318309 connectionId : connectionId ?? null ,
319310 to : msg . from ,
320- from : msg . to ,
311+ from : msg . platform . recipientJid ,
321312 text : caption ?? null ,
322313 mediaUrl,
323314 mediaSizeBytes : media . buffer . length ,
@@ -341,14 +332,19 @@ export async function deliverWebReply(params: {
341332 }
342333 whatsappOutboundLog . warn ( `Media skipped; sent text-only to ${ msg . from } ` ) ;
343334 rememberSendResult (
344- await sendWithRetry ( ( ) => msg . reply ( fallbackText , getQuote ( ) ) , "media:fallback-text" ) ,
335+ await sendWithRetry (
336+ ( ) => msg . platform . reply ( fallbackText , getQuote ( ) ) ,
337+ "media:fallback-text" ,
338+ ) ,
345339 ) ;
346340 } ,
347341 } ) ;
348342
349343 // Remaining text chunks after media
350344 for ( const chunk of remainingText ) {
351- rememberSendResult ( await sendWithRetry ( ( ) => msg . reply ( chunk , getQuote ( ) ) , "media:text" ) ) ;
345+ rememberSendResult (
346+ await sendWithRetry ( ( ) => msg . platform . reply ( chunk , getQuote ( ) ) , "media:text" ) ,
347+ ) ;
352348 }
353349 return finishDelivery ( ) ;
354350}
0 commit comments