mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-23 10:55:31 -06:00
ee320a6e41
Co-authored-by: Peter Steinberger <steipete@macos.shared>
119 lines
4.5 KiB
TypeScript
119 lines
4.5 KiB
TypeScript
// Imessage plugin module implements channel behavior.
|
|
import { resolveOutboundSendDep } from "openclaw/plugin-sdk/channel-outbound";
|
|
import { resolveIMessageDuplicateSourceOwner, type ResolvedIMessageAccount } from "./accounts.js";
|
|
import { PAIRING_APPROVED_MESSAGE, resolveChannelMediaMaxBytes } from "./channel-api.js";
|
|
import type { ChannelPlugin } from "./channel-api.js";
|
|
import { monitorIMessageProvider } from "./monitor.js";
|
|
import { IMESSAGE_LEGACY_OUTBOUND_SEND_DEP_KEYS } from "./outbound-send-deps.js";
|
|
import { probeIMessage } from "./probe.js";
|
|
import { sendMessageIMessage } from "./send.js";
|
|
import { imessageSetupWizard } from "./setup-surface.js";
|
|
|
|
type IMessageSendFn = typeof sendMessageIMessage;
|
|
|
|
export async function sendIMessageOutbound(params: {
|
|
cfg: Parameters<typeof import("./accounts.js").resolveIMessageAccount>[0]["cfg"];
|
|
to: string;
|
|
text: string;
|
|
mediaUrl?: string;
|
|
mediaLocalRoots?: readonly string[];
|
|
audioAsVoice?: boolean;
|
|
accountId?: string;
|
|
deps?: { [channelId: string]: unknown };
|
|
replyToId?: string;
|
|
conversationReadOrigin?: "delegated" | "direct-operator";
|
|
onDeliveryResult?: NonNullable<Parameters<IMessageSendFn>[2]["onDeliveryResult"]>;
|
|
}) {
|
|
const send =
|
|
resolveOutboundSendDep<IMessageSendFn>(params.deps, "imessage", {
|
|
legacyKeys: IMESSAGE_LEGACY_OUTBOUND_SEND_DEP_KEYS,
|
|
}) ?? sendMessageIMessage;
|
|
const maxBytes = resolveChannelMediaMaxBytes({
|
|
cfg: params.cfg,
|
|
resolveChannelLimitMb: ({ cfg, accountId }) =>
|
|
cfg.channels?.imessage?.accounts?.[accountId]?.mediaMaxMb ??
|
|
cfg.channels?.imessage?.mediaMaxMb,
|
|
accountId: params.accountId,
|
|
});
|
|
const result = await send(params.to, params.text, {
|
|
config: params.cfg,
|
|
...(params.mediaUrl ? { mediaUrl: params.mediaUrl } : {}),
|
|
...(params.mediaLocalRoots?.length ? { mediaLocalRoots: params.mediaLocalRoots } : {}),
|
|
...(params.audioAsVoice ? { audioAsVoice: true } : {}),
|
|
maxBytes,
|
|
accountId: params.accountId ?? undefined,
|
|
replyToId: params.replyToId ?? undefined,
|
|
conversationReadOrigin: params.conversationReadOrigin,
|
|
...(params.onDeliveryResult ? { onDeliveryResult: params.onDeliveryResult } : {}),
|
|
});
|
|
const meta = {
|
|
...(result as typeof result & { meta?: Record<string, unknown> }).meta,
|
|
...(result.guid ? { imessageMessageGuid: result.guid } : {}),
|
|
...(result.sentText ? { imessageVisibleText: result.sentText } : {}),
|
|
};
|
|
return Object.keys(meta).length > 0 ? { ...result, meta } : result;
|
|
}
|
|
|
|
export async function notifyIMessageApproval(params: {
|
|
cfg: Parameters<typeof import("./accounts.js").resolveIMessageAccount>[0]["cfg"];
|
|
id: string;
|
|
}): Promise<void> {
|
|
await sendMessageIMessage(params.id, PAIRING_APPROVED_MESSAGE, { config: params.cfg });
|
|
}
|
|
|
|
export async function probeIMessageAccount(params?: {
|
|
timeoutMs?: number;
|
|
cliPath?: string;
|
|
dbPath?: string;
|
|
}) {
|
|
return await probeIMessage(params?.timeoutMs, {
|
|
cliPath: params?.cliPath,
|
|
dbPath: params?.dbPath,
|
|
forceRefresh: true,
|
|
});
|
|
}
|
|
|
|
export async function startIMessageGatewayAccount(
|
|
ctx: Parameters<
|
|
NonNullable<NonNullable<ChannelPlugin<ResolvedIMessageAccount>["gateway"]>["startAccount"]>
|
|
>[0],
|
|
) {
|
|
const account = ctx.account;
|
|
const cliPath = account.config.cliPath?.trim() || "imsg";
|
|
const dbPath = account.config.dbPath?.trim();
|
|
ctx.setStatus({
|
|
accountId: account.accountId,
|
|
cliPath,
|
|
dbPath: dbPath ?? null,
|
|
});
|
|
const ownerAccountId = resolveIMessageDuplicateSourceOwner({ cfg: ctx.cfg, account });
|
|
if (ownerAccountId) {
|
|
// openclaw/openclaw#65141: this account shares a local Messages source with
|
|
// an already-owning account, so spawning a second `imsg rpc` would deliver
|
|
// every inbound twice. Keep the account enabled for outbound sends, status,
|
|
// and capability surfaces; just park the watcher slot until shutdown.
|
|
ctx.log?.info?.(
|
|
`[${account.accountId}] skipping watcher: duplicate iMessage source; using account "${ownerAccountId}"`,
|
|
);
|
|
if (ctx.abortSignal.aborted) {
|
|
return;
|
|
}
|
|
await new Promise<void>((resolve) => {
|
|
ctx.abortSignal.addEventListener("abort", () => resolve(), { once: true });
|
|
});
|
|
return;
|
|
}
|
|
ctx.log?.info?.(
|
|
`[${account.accountId}] starting provider (${cliPath}${dbPath ? ` db=${dbPath}` : ""})`,
|
|
);
|
|
return await monitorIMessageProvider({
|
|
accountId: account.accountId,
|
|
config: ctx.cfg,
|
|
runtime: ctx.runtime,
|
|
abortSignal: ctx.abortSignal,
|
|
channelRuntime: ctx.channelRuntime,
|
|
});
|
|
}
|
|
|
|
export { imessageSetupWizard };
|