Files
openclaw/extensions/imessage/src/message-tool-api.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

125 lines
3.6 KiB
TypeScript

// Imessage API module exposes the plugin public contract.
import { createActionGate } from "openclaw/plugin-sdk/channel-actions";
import type {
ChannelMessageActionAdapter,
ChannelMessageActionName,
} from "openclaw/plugin-sdk/channel-contract";
import { Type } from "typebox";
import { resolveIMessageAccount } from "./accounts.js";
import { IMESSAGE_ACTION_NAMES, IMESSAGE_ACTIONS } from "./actions-contract.js";
import {
getCachedIMessagePrivateApiStatus,
imessageRpcSupportsMethod,
} from "./private-api-status.js";
import { inferIMessageTargetChatType } from "./targets.js";
const PRIVATE_API_ACTIONS = new Set<ChannelMessageActionName>([
"react",
"edit",
"unsend",
"reply",
"sendWithEffect",
"renameGroup",
"setGroupIcon",
"addParticipant",
"removeParticipant",
"leaveGroup",
"sendAttachment",
"poll",
"poll-vote",
]);
function isGroupTarget(raw?: string | null): boolean {
if (!raw) {
return false;
}
return inferIMessageTargetChatType(raw) === "group";
}
export function describeIMessageMessageTool({
cfg,
accountId,
currentChannelId,
}: Parameters<NonNullable<ChannelMessageActionAdapter["describeMessageTool"]>>[0]) {
const account = resolveIMessageAccount({ cfg, accountId });
if (!account.enabled || !account.configured) {
return null;
}
const cliPath = account.config.cliPath?.trim() || "imsg";
const privateApiStatus = getCachedIMessagePrivateApiStatus(cliPath);
const gate = createActionGate(account.config.actions);
const actions = new Set<ChannelMessageActionName>();
for (const action of IMESSAGE_ACTION_NAMES) {
const spec = IMESSAGE_ACTIONS[action];
if (!spec?.gate || !gate(spec.gate)) {
continue;
}
if (privateApiStatus?.available === false && PRIVATE_API_ACTIONS.has(action)) {
continue;
}
if (
action === "edit" &&
privateApiStatus?.selectors &&
!privateApiStatus.selectors.editMessage &&
!privateApiStatus.selectors.editMessageItem
) {
continue;
}
if (action === "unsend" && privateApiStatus?.selectors?.retractMessagePart !== true) {
continue;
}
// Keep first-dispatch discovery optimistic while the status cache is empty;
// handleAction probes lazily and enforces the exact selector before sending.
if (
action === "poll" &&
privateApiStatus?.selectors &&
!privateApiStatus.selectors.pollPayloadMessage
) {
continue;
}
if (
action === "poll-vote" &&
privateApiStatus?.selectors &&
!privateApiStatus.selectors.pollVoteMessage
) {
continue;
}
// The injected helper can outlive the selected imsg binary. Require both
// the native initializer and a binary new enough to advertise poll.vote.
if (
action === "poll-vote" &&
privateApiStatus &&
!imessageRpcSupportsMethod(privateApiStatus, "poll.vote")
) {
continue;
}
actions.add(action);
}
if (!isGroupTarget(currentChannelId)) {
for (const action of IMESSAGE_ACTION_NAMES) {
if ("groupOnly" in IMESSAGE_ACTIONS[action] && IMESSAGE_ACTIONS[action].groupOnly) {
actions.delete(action);
}
}
}
if (actions.delete("sendAttachment")) {
actions.add("upload-file");
}
return {
actions: Array.from(actions),
...(actions.has("poll-vote")
? {
schema: {
properties: {
pollOptionText: Type.Optional(
Type.String({ description: "Exact iMessage poll option text." }),
),
},
actions: ["poll-vote" as const],
visibility: "all-configured" as const,
},
}
: {}),
};
}