


















@@ -28,7 +28,14 @@ import { cacheInboundMessageMeta } from "../quoted-message.js";
2828import { DEFAULT_RECONNECT_POLICY, computeBackoff, sleepWithAbort } from "../reconnect.js";
2929import type { OpenClawConfig } from "../runtime-api.js";
3030import { createWaSocket, formatError, getStatusCode, waitForWaConnection } from "../session.js";
31-import { resolveWhatsAppSocketTiming } from "../socket-timing.js";
31+import {
32+createWhatsAppSocketOperationTimeoutAdapter,
33+isWhatsAppSocketOperationTimeoutError,
34+resolveWhatsAppSocketOperationTimeoutMs,
35+resolveWhatsAppSocketTiming,
36+type WhatsAppSocketOperationAdapter,
37+type WhatsAppSocketTimingOptions,
38+} from "../socket-timing.js";
3239import { resolveJidToE164 } from "../text-runtime.js";
3340import { checkInboundAccessControl } from "./access-control.js";
3441import {
@@ -170,6 +177,9 @@ function recordAcceptedInboundActivity(accountId: string): void {
170177}
171178172179function isRetryableSendDisconnectError(err: unknown): boolean {
180+if (isWhatsAppSocketOperationTimeoutError(err)) {
181+return false;
182+}
173183return /closed|reset|timed\s*out|disconnect|no active socket/i.test(formatError(err));
174184}
175185@@ -184,6 +194,7 @@ function isNonEmptyString(value: string | undefined): value is string {
184194type MonitorWebInboxOptions = {
185195cfg: OpenClawConfig;
186196loadConfig?: () => OpenClawConfig;
197+socketTiming?: Required<WhatsAppSocketTimingOptions>;
187198verbose: boolean;
188199accountId: string;
189200authDir: string;
@@ -217,8 +228,9 @@ type MonitorWebInboxOptions = {
217228218229type AttachWebInboxToSocketOptions = Omit<
219230MonitorWebInboxOptions,
220-"onMessage" | "shouldDebounce"
231+"onMessage" | "shouldDebounce" | "socketTiming"
221232> & {
233+socketTiming: Required<WhatsAppSocketTimingOptions>;
222234onMessage: (msg: WebInboundMessageInput) => Promise<void>;
223235shouldDebounce?: (msg: WebInboundMessageInput) => boolean;
224236};
@@ -241,6 +253,9 @@ export async function attachWebInboxToSocket(
241253disconnectRetryPolicy.maxAttempts > 0
242254 ? disconnectRetryPolicy.maxAttempts
243255 : DEFAULT_RECONNECT_POLICY.maxAttempts;
256+const sendOperationTimeoutMs = resolveWhatsAppSocketOperationTimeoutMs(
257+options.socketTiming.defaultQueryTimeoutMs,
258+);
244259245260let onCloseResolve: ((reason: WebListenerCloseReason) => void) | null = null;
246261const onClose = new Promise<WebListenerCloseReason>((resolve) => {
@@ -257,7 +272,10 @@ export async function attachWebInboxToSocket(
257272const presence = options.selfChatMode ? "unavailable" : "available";
258273259274try {
260-await sock.sendPresenceUpdate(presence);
275+await createWhatsAppSocketOperationTimeoutAdapter(
276+sock,
277+sendOperationTimeoutMs,
278+).sendPresenceUpdate(presence);
261279logWhatsAppVerbose(options.verbose, `Sent global '${presence}' presence on connect`);
262280} catch (err) {
263281logWhatsAppVerbose(
@@ -476,6 +494,16 @@ export async function attachWebInboxToSocket(
476494 messageId,
477495});
478496};
497+const trackLateAcceptedSend = (jid: string, promise: Promise<WAMessage | undefined>) => {
498+// The local send has failed terminally, but Baileys may still deliver it.
499+// Track a late message id only to suppress the resulting self-echo.
500+void promise.then(
501+(result) => {
502+rememberOutboundMessage(jid, result);
503+},
504+() => {},
505+);
506+};
479507480508const sendTrackedMessage = async (
481509jid: string,
@@ -487,9 +515,15 @@ export async function attachWebInboxToSocket(
487515const currentSock = getCurrentSock();
488516if (currentSock) {
489517try {
490-const result = sendOptions
491- ? await currentSock.sendMessage(jid, content, sendOptions)
492- : await currentSock.sendMessage(jid, content);
518+const result = await createWhatsAppSocketOperationTimeoutAdapter(
519+currentSock,
520+sendOperationTimeoutMs,
521+{
522+onSendMessageTimeout: ({ jid: timedOutJid, promise }) => {
523+trackLateAcceptedSend(timedOutJid, promise);
524+},
525+},
526+).sendMessage(jid, content, sendOptions);
493527rememberOutboundMessage(jid, result);
494528return result;
495529} catch (err) {
@@ -523,6 +557,19 @@ export async function attachWebInboxToSocket(
523557}
524558}
525559};
560+const sendApiSocketOperations: WhatsAppSocketOperationAdapter = {
561+sendMessage: (jid, content, sendOptions) => sendTrackedMessage(jid, content, sendOptions),
562+sendPresenceUpdate: async (presenceLocal, jid) => {
563+const currentSock = getCurrentSock();
564+if (!currentSock) {
565+throw new Error(RECONNECT_IN_PROGRESS_ERROR);
566+}
567+return await createWhatsAppSocketOperationTimeoutAdapter(
568+currentSock,
569+sendOperationTimeoutMs,
570+).sendPresenceUpdate(presenceLocal, jid);
571+},
572+};
526573527574const summarizeGroupMeta = async (meta: GroupMetadata) => {
528575const participantEntries = await Promise.all(
@@ -1019,12 +1066,11 @@ export async function attachWebInboxToSocket(
10191066) => {
10201067const chatJid = inbound.remoteJid;
10211068const sendComposing = async () => {
1022-const currentSock = getCurrentSock();
1023-if (!currentSock) {
1069+if (!getCurrentSock()) {
10241070return;
10251071}
10261072try {
1027-await currentSock.sendPresenceUpdate("composing", chatJid);
1073+await sendApiSocketOperations.sendPresenceUpdate("composing", chatJid);
10281074} catch (err) {
10291075logWhatsAppVerbose(options.verbose, `Presence update failed: ${String(err)}`);
10301076}
@@ -1335,20 +1381,7 @@ export async function attachWebInboxToSocket(
13351381})();
1336138213371383const sendApi = createWebSendApi({
1338-sock: {
1339-sendMessage: (
1340-jid: string,
1341-content: AnyMessageContent,
1342-optionsLocal?: MiscMessageGenerationOptions,
1343-) => sendTrackedMessage(jid, content, optionsLocal),
1344-sendPresenceUpdate: async (presenceLocal, jid?: string) => {
1345-const currentSock = getCurrentSock();
1346-if (!currentSock) {
1347-throw new Error(RECONNECT_IN_PROGRESS_ERROR);
1348-}
1349-return currentSock.sendPresenceUpdate(presenceLocal, jid);
1350-},
1351-},
1384+sock: sendApiSocketOperations,
13521385defaultAccountId: options.accountId,
13531386resolveOutboundMentions: ({ jid, text }) => resolveOutboundMentionsForGroup(jid, text),
13541387authDir: options.authDir,
@@ -1381,7 +1414,7 @@ export async function attachWebInboxToSocket(
13811414}
1382141513831416export async function monitorWebInbox(options: MonitorWebInboxOptions) {
1384-const socketTiming = resolveWhatsAppSocketTiming(options.cfg);
1417+const socketTiming = options.socketTiming ?? resolveWhatsAppSocketTiming(options.cfg);
13851418const sock = await createWaSocket(false, options.verbose, {
13861419authDir: options.authDir,
13871420 ...socketTiming,
@@ -1401,6 +1434,7 @@ export async function monitorWebInbox(options: MonitorWebInboxOptions) {
14011434shouldDebounce: shouldDebounce
14021435 ? (msg) => shouldDebounce(normalizeWebInboundMessage(msg))
14031436 : undefined,
1437+ socketTiming,
14041438 sock,
14051439});
14061440}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。