Files
openclaw/extensions/imessage/src/channel.runtime.ts
Omar Shahine c92c33d108 feat(imessage): native poll support — create, read, vote (#98421)
* feat(imessage): add native poll action

Wire the imsg CLI 'poll send' bridge command into the iMessage channel
message-tool action surface, mirroring the existing Discord poll action.
Adds the 'poll' action (gate: polls), a sendPoll runtime, selector-gated
capability advertisement (pollPayloadMessage), config type + zod schema,
regenerated channel metadata, docs, and tests.

* feat(imessage): read inbound polls, vote, and suppress vote echo

Builds on the native poll send action:

- Inbound polls now render to the agent as a readable line (question +
  numbered options + tallies) instead of the raw 0xFFFD balloon placeholder,
  so a received poll no longer reads as an empty message.
- New `poll-vote` action casts a vote via `imsg poll vote`, resolving a
  1-based option index / text / UUID to the poll's option identifier.
- message_tool_only echo guard: the model tends to narrate its choice in a
  text reply right after voting ("Blue."), which is redundant since the vote
  shows on the poll. A new `poll_vote_echo` suppression reason (alongside
  inbound_metadata_echo / internal_runtime_context_echo) drops a send/reply
  that exactly restates the just-cast vote, using the option label imsg
  returns. Extra content passes through untouched.

* fix(imessage): gate poll-vote on imsg poll.vote rpc capability

Released imsg carries the pollPayloadMessage selector (poll create) but
predates the poll.vote CLI/RPC. Gating both poll and poll-vote on that
selector alone would advertise a vote action the released CLI rejects.
Gate poll-vote additionally on the advertised poll.vote rpc method so this
plugin can ship ahead of the imsg release.

* fix(imessage): enforce poll.vote capability at execution, not just discovery

Codex review flagged the discovery gate as bypassable: a caller that already
knows action=poll-vote skips describeMessageTool and reaches handleAction
directly. Add the same imessageRpcSupportsMethod(status, 'poll.vote') check in
the poll-vote execution path (after assertPrivateApiEnabled), so a direct
dispatch on released imsg fails closed with a clear message instead of an
opaque CLI rejection. Adds a negative handleAction test.

* fix(imessage): harden native poll support

* fix(message): validate targets before channel discovery

* fix(message): validate targets before channel discovery

---------

Co-authored-by: Omar Shahine <lobster@users.noreply.github.com>
Co-authored-by: Omar Shahine <10343873+omarshahine@users.noreply.github.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-01 10:48:32 +01:00

109 lines
3.9 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;
}) {
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,
});
return 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,
});
}
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 };