diff --git a/extensions/imessage/src/monitor.watch-subscribe-retry.test.ts b/extensions/imessage/src/monitor.watch-subscribe-retry.test.ts index 6c6e2f161f3..9812b16dc73 100644 --- a/extensions/imessage/src/monitor.watch-subscribe-retry.test.ts +++ b/extensions/imessage/src/monitor.watch-subscribe-retry.test.ts @@ -1,9 +1,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { monitorIMessageProvider } from "./monitor.js"; -const waitForTransportReadyMock = vi.hoisted(() => vi.fn(async () => {})); -const createIMessageRpcClientMock = vi.hoisted(() => vi.fn()); -const attachIMessageMonitorAbortHandlerMock = vi.hoisted(() => vi.fn(() => () => {})); +const waitForTransportReadyMock = vi.hoisted(() => + vi.fn<(...args: unknown[]) => Promise>(async () => {}), +); +const createIMessageRpcClientMock = vi.hoisted(() => vi.fn<(...args: unknown[]) => unknown>()); +const attachIMessageMonitorAbortHandlerMock = vi.hoisted(() => + vi.fn<(...args: unknown[]) => () => void>(() => () => {}), +); vi.mock("openclaw/plugin-sdk/infra-runtime", () => ({ waitForTransportReady: (...args: unknown[]) => waitForTransportReadyMock(...args), diff --git a/extensions/imessage/src/monitor/monitor-provider.ts b/extensions/imessage/src/monitor/monitor-provider.ts index 2b25013f80f..3467a65ea86 100644 --- a/extensions/imessage/src/monitor/monitor-provider.ts +++ b/extensions/imessage/src/monitor/monitor-provider.ts @@ -234,6 +234,15 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P }, }); + let client: IMessageRpcClient | undefined; + let detachAbortHandler = () => {}; + const getActiveClient = () => { + if (!client) { + throw new Error("imessage monitor client not initialized"); + } + return client; + }; + async function handleMessageNow(message: IMessagePayload) { const messageText = (message.text ?? "").trim(); @@ -343,7 +352,7 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P }, sendPairingReply: async (text) => { await sendMessageIMessage(sender, text, { - client, + client: getActiveClient(), maxBytes: mediaMaxBytes, accountId: accountInfo.accountId, ...(chatId ? { chatId } : {}), @@ -443,7 +452,7 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P await deliverReplies({ replies: [payload], target, - client, + client: getActiveClient(), accountId: accountInfo.accountId, runtime, maxBytes: mediaMaxBytes, @@ -539,14 +548,11 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P }, }); - let client: IMessageRpcClient | null = null; - let detachAbortHandler = () => {}; - for (let attempt = 1; attempt <= WATCH_SUBSCRIBE_MAX_ATTEMPTS; attempt++) { if (abort?.aborted) { return; } - let attemptClient: IMessageRpcClient | null = null; + let attemptClient: IMessageRpcClient | undefined; let attemptDetachAbortHandler = () => {}; let keepAttemptClient = false; try {