web: handle multi-payload replies

This commit is contained in:
Peter Steinberger
2025-12-02 23:46:11 +00:00
parent 10182f1182
commit ec46932259

View File

@@ -656,67 +656,70 @@ export async function monitorWebProvider(
}, },
); );
if ( const replyList = replyResult
!replyResult || ? Array.isArray(replyResult)
(!replyResult.text && ? replyResult
!replyResult.mediaUrl && : [replyResult]
!replyResult.mediaUrls?.length) : [];
) {
if (replyList.length === 0) {
logVerbose("Skipping auto-reply: no text/media returned from resolver"); logVerbose("Skipping auto-reply: no text/media returned from resolver");
return; return;
} }
// Apply response prefix if configured (skip for HEARTBEAT_OK to preserve exact match) // Apply response prefix if configured (skip for HEARTBEAT_OK to preserve exact match)
const responsePrefix = cfg.inbound?.responsePrefix; const responsePrefix = cfg.inbound?.responsePrefix;
if (
responsePrefix && for (const replyPayload of replyList) {
replyResult.text && if (
replyResult.text.trim() !== HEARTBEAT_TOKEN responsePrefix &&
) { replyPayload.text &&
if (!replyResult.text.startsWith(responsePrefix)) { replyPayload.text.trim() !== HEARTBEAT_TOKEN &&
replyResult.text = `${responsePrefix} ${replyResult.text}`; !replyPayload.text.startsWith(responsePrefix)
) {
replyPayload.text = `${responsePrefix} ${replyPayload.text}`;
} }
}
try { try {
await deliverWebReply({ await deliverWebReply({
replyResult, replyResult: replyPayload,
msg: latest, msg: latest,
maxMediaBytes, maxMediaBytes,
replyLogger, replyLogger,
runtime, runtime,
connectionId, connectionId,
}); });
if (replyResult.text) { if (replyPayload.text) {
recentlySent.add(replyResult.text); recentlySent.add(replyPayload.text);
recentlySent.add(combinedBody); // Prevent echo on the batch text itself recentlySent.add(combinedBody); // Prevent echo on the batch text itself
logVerbose( logVerbose(
`Added to echo detection set (size now: ${recentlySent.size}): ${replyResult.text.substring(0, 50)}...`, `Added to echo detection set (size now: ${recentlySent.size}): ${replyPayload.text.substring(0, 50)}...`,
); );
if (recentlySent.size > MAX_RECENT_MESSAGES) { if (recentlySent.size > MAX_RECENT_MESSAGES) {
const firstKey = recentlySent.values().next().value; const firstKey = recentlySent.values().next().value;
if (firstKey) recentlySent.delete(firstKey); if (firstKey) recentlySent.delete(firstKey);
}
} }
}
if (isVerbose()) { if (isVerbose()) {
console.log( console.log(
success( success(
`↩️ Auto-replied to ${from} (web${replyResult.mediaUrl || replyResult.mediaUrls?.length ? ", media" : ""}; batched ${messages.length})`, `↩️ Auto-replied to ${from} (web${replyPayload.mediaUrl || replyPayload.mediaUrls?.length ? ", media" : ""}; batched ${messages.length})`,
), ),
); );
} else { } else {
console.log( console.log(
success( success(
`↩️ ${replyResult.text ?? "<media>"}${replyResult.mediaUrl || replyResult.mediaUrls?.length ? " (media)" : ""}`, `↩️ ${replyPayload.text ?? "<media>"}${replyPayload.mediaUrl || replyPayload.mediaUrls?.length ? " (media)" : ""}`,
), ),
);
}
} catch (err) {
console.error(
danger(`Failed sending web auto-reply to ${from}: ${String(err)}`),
); );
} }
} catch (err) {
console.error(
danger(`Failed sending web auto-reply to ${from}: ${String(err)}`),
);
} }
}; };