|
| 1 | +// Imessage plugin module owns persisted approval reaction poll targets. |
| 2 | +import { readApprovalReactionDecisionList } from "openclaw/plugin-sdk/approval-reaction-runtime"; |
| 3 | +import type { ExecApprovalReplyDecision } from "openclaw/plugin-sdk/approval-reply-runtime"; |
| 4 | +import { |
| 5 | + asDateTimestampMs, |
| 6 | + isFutureDateTimestampMs, |
| 7 | + resolveExpiresAtMsFromDurationMs, |
| 8 | +} from "openclaw/plugin-sdk/number-runtime"; |
| 9 | +import { |
| 10 | + createPluginStateErrorReporter, |
| 11 | + type PluginStateKeyedStore, |
| 12 | +} from "openclaw/plugin-sdk/plugin-state-runtime"; |
| 13 | +import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 14 | +import { |
| 15 | + normalizeConversationKey, |
| 16 | + normalizeIMessageGuid, |
| 17 | + type IMessageApprovalConversationKey, |
| 18 | +} from "./approval-target-keys.js"; |
| 19 | +import { getOptionalIMessageRuntime } from "./runtime.js"; |
| 20 | + |
| 21 | +const PERSISTENT_POLL_TARGET_NAMESPACE = "imessage.approval-reaction-poll-targets"; |
| 22 | +const PERSISTENT_MAX_ENTRIES = 1000; |
| 23 | +const DEFAULT_REACTION_TARGET_TTL_MS = 24 * 60 * 60 * 1000; |
| 24 | + |
| 25 | +export type PendingIMessageApprovalReactionPollTarget = { |
| 26 | + accountId: string; |
| 27 | + conversation: IMessageApprovalConversationKey; |
| 28 | + messageId: string; |
| 29 | + approvalId: string; |
| 30 | + approvalKind: "exec" | "plugin"; |
| 31 | + allowedDecisions: readonly ExecApprovalReplyDecision[]; |
| 32 | + expiresAtMs: number; |
| 33 | +}; |
| 34 | + |
| 35 | +const pendingReactionPollTargets = new Map<string, PendingIMessageApprovalReactionPollTarget>(); |
| 36 | + |
| 37 | +function prunePendingReactionPollTargets(nowMs = Date.now()): void { |
| 38 | + for (const [key, target] of pendingReactionPollTargets.entries()) { |
| 39 | + if (!isFutureDateTimestampMs(target.expiresAtMs, { nowMs })) { |
| 40 | + pendingReactionPollTargets.delete(key); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function resolvePendingReactionPollExpiry( |
| 46 | + ttlMs: number | undefined, |
| 47 | +): { ttlMs: number; expiresAtMs: number } | undefined { |
| 48 | + const nowMs = asDateTimestampMs(Date.now()); |
| 49 | + if (nowMs === undefined) { |
| 50 | + return undefined; |
| 51 | + } |
| 52 | + const expiresAtMs = |
| 53 | + resolveExpiresAtMsFromDurationMs(ttlMs ?? DEFAULT_REACTION_TARGET_TTL_MS, { nowMs }) ?? |
| 54 | + resolveExpiresAtMsFromDurationMs(DEFAULT_REACTION_TARGET_TTL_MS, { nowMs }); |
| 55 | + if (expiresAtMs === undefined) { |
| 56 | + return undefined; |
| 57 | + } |
| 58 | + return { |
| 59 | + ttlMs: expiresAtMs - nowMs, |
| 60 | + expiresAtMs, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function mergePollTargetConversation( |
| 65 | + left: IMessageApprovalConversationKey, |
| 66 | + right: IMessageApprovalConversationKey, |
| 67 | +): IMessageApprovalConversationKey { |
| 68 | + return { |
| 69 | + chatGuid: left.chatGuid ?? right.chatGuid, |
| 70 | + chatIdentifier: left.chatIdentifier ?? right.chatIdentifier, |
| 71 | + chatId: left.chatId ?? right.chatId, |
| 72 | + handle: left.handle ?? right.handle, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +const reportPersistentApprovalReactionError = createPluginStateErrorReporter( |
| 77 | + getOptionalIMessageRuntime, |
| 78 | + "imessage", |
| 79 | + "approval-reaction-state", |
| 80 | + "iMessage persistent approval reaction state failed", |
| 81 | +); |
| 82 | + |
| 83 | +let pendingReactionPollTargetStore: |
| 84 | + | PluginStateKeyedStore<PendingIMessageApprovalReactionPollTarget> |
| 85 | + | undefined; |
| 86 | +let pendingReactionPollTargetStoreDisabled = false; |
| 87 | + |
| 88 | +function disablePendingReactionPollTargetStore(error: unknown): void { |
| 89 | + pendingReactionPollTargetStoreDisabled = true; |
| 90 | + pendingReactionPollTargetStore = undefined; |
| 91 | + reportPersistentApprovalReactionError(error); |
| 92 | +} |
| 93 | + |
| 94 | +function getPendingReactionPollTargetStore(): |
| 95 | + | PluginStateKeyedStore<PendingIMessageApprovalReactionPollTarget> |
| 96 | + | undefined { |
| 97 | + if (pendingReactionPollTargetStoreDisabled) { |
| 98 | + return undefined; |
| 99 | + } |
| 100 | + if (pendingReactionPollTargetStore) { |
| 101 | + return pendingReactionPollTargetStore; |
| 102 | + } |
| 103 | + try { |
| 104 | + pendingReactionPollTargetStore = |
| 105 | + getOptionalIMessageRuntime()?.state.openKeyedStore<PendingIMessageApprovalReactionPollTarget>( |
| 106 | + { |
| 107 | + namespace: PERSISTENT_POLL_TARGET_NAMESPACE, |
| 108 | + maxEntries: PERSISTENT_MAX_ENTRIES, |
| 109 | + defaultTtlMs: DEFAULT_REACTION_TARGET_TTL_MS, |
| 110 | + }, |
| 111 | + ); |
| 112 | + return pendingReactionPollTargetStore; |
| 113 | + } catch (error) { |
| 114 | + disablePendingReactionPollTargetStore(error); |
| 115 | + return undefined; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function readPersistedPollTarget(value: unknown): PendingIMessageApprovalReactionPollTarget | null { |
| 120 | + const target = asOptionalRecord(value); |
| 121 | + if (!target) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + const accountId = typeof target.accountId === "string" ? target.accountId.trim() : ""; |
| 125 | + const messageId = typeof target.messageId === "string" ? target.messageId.trim() : ""; |
| 126 | + const approvalId = typeof target.approvalId === "string" ? target.approvalId.trim() : ""; |
| 127 | + const expiresAtMs = asDateTimestampMs(target.expiresAtMs); |
| 128 | + const allowedDecisions = readApprovalReactionDecisionList(target.allowedDecisions); |
| 129 | + const rawConversation = asOptionalRecord(target.conversation) ?? {}; |
| 130 | + const conversation: IMessageApprovalConversationKey = { |
| 131 | + ...(typeof rawConversation.chatGuid === "string" |
| 132 | + ? { chatGuid: rawConversation.chatGuid.trim() } |
| 133 | + : {}), |
| 134 | + ...(typeof rawConversation.chatIdentifier === "string" |
| 135 | + ? { chatIdentifier: rawConversation.chatIdentifier.trim() } |
| 136 | + : {}), |
| 137 | + ...(typeof rawConversation.chatId === "string" || typeof rawConversation.chatId === "number" |
| 138 | + ? { chatId: rawConversation.chatId } |
| 139 | + : {}), |
| 140 | + ...(typeof rawConversation.handle === "string" |
| 141 | + ? { handle: rawConversation.handle.trim() } |
| 142 | + : {}), |
| 143 | + }; |
| 144 | + if ( |
| 145 | + !accountId || |
| 146 | + !messageId || |
| 147 | + !approvalId || |
| 148 | + expiresAtMs === undefined || |
| 149 | + !allowedDecisions || |
| 150 | + (target.approvalKind !== "exec" && target.approvalKind !== "plugin") || |
| 151 | + !normalizeConversationKey(conversation) |
| 152 | + ) { |
| 153 | + return null; |
| 154 | + } |
| 155 | + return { |
| 156 | + accountId, |
| 157 | + conversation, |
| 158 | + messageId, |
| 159 | + approvalId, |
| 160 | + approvalKind: target.approvalKind, |
| 161 | + allowedDecisions, |
| 162 | + expiresAtMs, |
| 163 | + }; |
| 164 | +} |
| 165 | + |
| 166 | +export function recordIMessageApprovalReactionPollTarget(params: { |
| 167 | + keys: readonly string[]; |
| 168 | + accountId: string; |
| 169 | + conversation: IMessageApprovalConversationKey; |
| 170 | + messageId: string; |
| 171 | + approvalId: string; |
| 172 | + approvalKind: "exec" | "plugin"; |
| 173 | + allowedDecisions: readonly ExecApprovalReplyDecision[]; |
| 174 | + ttlMs?: number; |
| 175 | +}): { ttlMs: number; expiresAtMs: number } | null { |
| 176 | + const expiry = resolvePendingReactionPollExpiry(params.ttlMs); |
| 177 | + if (!expiry || params.keys.length === 0) { |
| 178 | + return null; |
| 179 | + } |
| 180 | + const target: PendingIMessageApprovalReactionPollTarget = { |
| 181 | + accountId: params.accountId, |
| 182 | + conversation: params.conversation, |
| 183 | + messageId: params.messageId, |
| 184 | + approvalId: params.approvalId, |
| 185 | + approvalKind: params.approvalKind, |
| 186 | + allowedDecisions: params.allowedDecisions, |
| 187 | + expiresAtMs: expiry.expiresAtMs, |
| 188 | + }; |
| 189 | + const store = getPendingReactionPollTargetStore(); |
| 190 | + for (const key of params.keys) { |
| 191 | + pendingReactionPollTargets.set(key, target); |
| 192 | + void store |
| 193 | + ?.register(key, target, { ttlMs: expiry.ttlMs }) |
| 194 | + .catch(disablePendingReactionPollTargetStore); |
| 195 | + } |
| 196 | + prunePendingReactionPollTargets(); |
| 197 | + return expiry; |
| 198 | +} |
| 199 | + |
| 200 | +export function deleteIMessageApprovalReactionPollTargets(keys: readonly string[]): void { |
| 201 | + const store = getPendingReactionPollTargetStore(); |
| 202 | + for (const key of keys) { |
| 203 | + pendingReactionPollTargets.delete(key); |
| 204 | + void store?.delete(key).catch(disablePendingReactionPollTargetStore); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +export async function listPendingIMessageApprovalReactionPollTargets(params: { |
| 209 | + accountId: string; |
| 210 | +}): Promise<PendingIMessageApprovalReactionPollTarget[]> { |
| 211 | + const accountId = params.accountId.trim(); |
| 212 | + if (!accountId) { |
| 213 | + return []; |
| 214 | + } |
| 215 | + const nowMs = Date.now(); |
| 216 | + const store = getPendingReactionPollTargetStore(); |
| 217 | + if (store) { |
| 218 | + try { |
| 219 | + for (const entry of await store.entries()) { |
| 220 | + const target = readPersistedPollTarget(entry.value); |
| 221 | + if (!target || !isFutureDateTimestampMs(target.expiresAtMs, { nowMs })) { |
| 222 | + await store.delete(entry.key); |
| 223 | + continue; |
| 224 | + } |
| 225 | + pendingReactionPollTargets.set(entry.key, target); |
| 226 | + } |
| 227 | + } catch (error) { |
| 228 | + disablePendingReactionPollTargetStore(error); |
| 229 | + } |
| 230 | + } |
| 231 | + prunePendingReactionPollTargets(nowMs); |
| 232 | + const targetByApprovalAndMessage = new Map<string, PendingIMessageApprovalReactionPollTarget>(); |
| 233 | + for (const target of pendingReactionPollTargets.values()) { |
| 234 | + if (target.accountId !== accountId) { |
| 235 | + continue; |
| 236 | + } |
| 237 | + const key = `${target.approvalId}:${normalizeIMessageGuid(target.messageId)}`; |
| 238 | + const existing = targetByApprovalAndMessage.get(key); |
| 239 | + if (!existing) { |
| 240 | + targetByApprovalAndMessage.set(key, target); |
| 241 | + continue; |
| 242 | + } |
| 243 | + targetByApprovalAndMessage.set(key, { |
| 244 | + ...existing, |
| 245 | + conversation: mergePollTargetConversation(existing.conversation, target.conversation), |
| 246 | + expiresAtMs: Math.max(existing.expiresAtMs, target.expiresAtMs), |
| 247 | + }); |
| 248 | + } |
| 249 | + return [...targetByApprovalAndMessage.values()]; |
| 250 | +} |
| 251 | + |
| 252 | +export function clearIMessageApprovalReactionPollTargetsForTest(): void { |
| 253 | + pendingReactionPollTargets.clear(); |
| 254 | + pendingReactionPollTargetStore = undefined; |
| 255 | + pendingReactionPollTargetStoreDisabled = false; |
| 256 | +} |
0 commit comments