@openclaw/imessage 2026.9.1-beta.1 → 2026.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{accounts-DIpGOIiN.js → accounts-CCirBEIm.js} +2 -3
- package/dist/{actions.runtime-BhggncbG.js → actions.runtime-DCjNT81u.js} +5 -5
- package/dist/api.js +5 -5
- package/dist/{approval-handler.runtime-BC-YsgAf.js → approval-handler.runtime-BReeID1z.js} +14 -9
- package/dist/{approval-reactions-Cvwmdlev.js → approval-reactions-DcLRPx7O.js} +6 -10
- package/dist/channel-config-api.js +1 -1
- package/dist/channel-plugin-api.js +1 -1
- package/dist/{channel-CnKQ2MVB.js → channel-zDmMTHWD.js} +16 -12
- package/dist/{channel.runtime-BQLCh7bE.js → channel.runtime-qQBHCle8.js} +59 -14
- package/dist/{config-schema-DV0KP5nC.js → config-schema-DuGbA8Kp.js} +8 -7
- package/dist/contract-api.js +1 -1
- package/dist/doctor-contract-api.js +8 -281
- package/dist/{group-policy-BipELT2x.js → group-policy-BWqG2esy.js} +18 -12
- package/dist/media-contract-api.js +1 -1
- package/dist/{media-contract-BVXuQHG2.js → media-contract-pns3TSS5.js} +3 -2
- package/dist/{message-tool-api-BwIxJDoz.js → message-tool-api-CUoFESHj.js} +11 -6
- package/dist/message-tool-api.js +1 -1
- package/dist/{monitor-DRD_Dnp9.js → monitor-AmSj6QIH.js} +293 -27
- package/dist/{remote-file-CyTTuRAt.js → remote-file-COTN7196.js} +9 -8
- package/dist/runtime-Cza4CY5T.js +8 -0
- package/dist/runtime-api.js +7 -7
- package/dist/{sanitize-outbound-g651fIhi.js → sanitize-outbound-CN3Hxbjy.js} +393 -82
- package/dist/{send-DVQY6M5M.js → send-v0blZfaI.js} +145 -23
- package/dist/state-contract-DvahEY4X.js +56 -0
- package/dist/state-migrations-D-fyHVcb.js +277 -0
- package/node_modules/typebox/build/format/email.mjs +1 -1
- package/node_modules/typebox/build/format/idna/idn-hostname.mjs +19 -1
- package/node_modules/typebox/package.json +1 -1
- package/openclaw.plugin.json +0 -6
- package/package.json +5 -5
- package/dist/catchup-Dh-Rv4Ur.js +0 -294
- package/dist/monitor-reply-cache-BdeUQaHO.js +0 -353
- package/dist/persisted-echo-cache-DGxy-J4t.js +0 -143
|
@@ -1,15 +1,353 @@
|
|
|
1
|
-
import { u as expandIMessageUserPath } from "./accounts-
|
|
2
|
-
import {
|
|
1
|
+
import { u as expandIMessageUserPath } from "./accounts-CCirBEIm.js";
|
|
2
|
+
import { m as invalidateCachedIMessagePrivateApiStatus } from "./message-tool-api-CUoFESHj.js";
|
|
3
|
+
import { t as getIMessageRuntime } from "./runtime-Cza4CY5T.js";
|
|
4
|
+
import { a as IMESSAGE_REPLY_CACHE_NAMESPACE, f as resolveIMessageReplyCacheEntryKey, i as IMESSAGE_REPLY_CACHE_MAX_ENTRIES, n as IMESSAGE_REPLY_CACHE_COUNTER_KEY, o as IMESSAGE_REPLY_CACHE_TTL_MS, r as IMESSAGE_REPLY_CACHE_COUNTER_NAMESPACE } from "./state-contract-DvahEY4X.js";
|
|
5
|
+
import { normalizeE164 } from "openclaw/plugin-sdk/account-resolution";
|
|
6
|
+
import { normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
7
|
+
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
|
|
3
8
|
import { resolveUserPath } from "openclaw/plugin-sdk/text-utility-runtime";
|
|
4
9
|
import { spawn } from "node:child_process";
|
|
5
10
|
import { StringDecoder } from "node:string_decoder";
|
|
6
11
|
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
|
|
7
12
|
import { FormatCapabilityProfile, findCodeRegions, isInsideCode, markdownToIR, renderMarkdownWithAttributedRanges, sanitizeAssistantVisibleText, stripMarkdown, tokenizeHtmlTags } from "openclaw/plugin-sdk/text-chunking";
|
|
13
|
+
//#region extensions/imessage/src/chat-context.ts
|
|
14
|
+
const EMAIL_HANDLE_PATTERN = /^[^\s@]+@[^\s@]+$/u;
|
|
15
|
+
function parseDirectChatIdentity(raw) {
|
|
16
|
+
const trimmed = raw.trim();
|
|
17
|
+
const parts = trimmed.split(";");
|
|
18
|
+
if (parts.length === 3 && parts[1] === "-" && parts[2]) {
|
|
19
|
+
const service = parts[0]?.toLowerCase();
|
|
20
|
+
if (service === "imessage" || service === "sms" || service === "any") {
|
|
21
|
+
const identifier = parts[2];
|
|
22
|
+
return {
|
|
23
|
+
service,
|
|
24
|
+
identifier: EMAIL_HANDLE_PATTERN.test(identifier) ? identifier.toLowerCase() : identifier
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (parts.length !== 1) return;
|
|
29
|
+
if (trimmed.startsWith("+") || EMAIL_HANDLE_PATTERN.test(trimmed)) return { identifier: trimmed.toLowerCase() };
|
|
30
|
+
}
|
|
31
|
+
function resolveIMessageDirectChatService(configuredService, chatGuid) {
|
|
32
|
+
if (configuredService === "imessage" || configuredService === "sms") return configuredService;
|
|
33
|
+
const observedService = chatGuid ? parseDirectChatIdentity(chatGuid)?.service : void 0;
|
|
34
|
+
return observedService === "imessage" || observedService === "sms" ? observedService : void 0;
|
|
35
|
+
}
|
|
36
|
+
function isIMessageEmailChatIdentifier(raw) {
|
|
37
|
+
const identity = parseDirectChatIdentity(raw);
|
|
38
|
+
return Boolean(identity && EMAIL_HANDLE_PATTERN.test(identity.identifier));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Strip the `iMessage;-;` / `SMS;-;` / `any;-;` service prefix that Messages
|
|
42
|
+
* uses for direct chats. Different layers report direct DMs in different
|
|
43
|
+
* forms, so raw comparison would falsely treat one DM as different chats.
|
|
44
|
+
*/
|
|
45
|
+
function normalizeDirectChatIdentifier(raw) {
|
|
46
|
+
const trimmed = raw.trim();
|
|
47
|
+
return parseDirectChatIdentity(trimmed)?.identifier ?? trimmed;
|
|
48
|
+
}
|
|
49
|
+
function chatContextFromIMessageTarget(target, effectiveService) {
|
|
50
|
+
if (target.kind === "chat_id") return { chatId: target.chatId };
|
|
51
|
+
if (target.kind === "chat_guid") return { chatGuid: target.chatGuid };
|
|
52
|
+
if (target.kind === "chat_identifier") return { chatIdentifier: target.chatIdentifier };
|
|
53
|
+
const trimmedHandle = target.to.trim();
|
|
54
|
+
const canonicalHandle = trimmedHandle.startsWith("+") ? normalizeE164(trimmedHandle) : /^[^\s@]+@[^\s@]+$/u.test(trimmedHandle) ? trimmedHandle.toLowerCase() : void 0;
|
|
55
|
+
if (!canonicalHandle) return {};
|
|
56
|
+
const service = target.service === "auto" ? effectiveService : target.service;
|
|
57
|
+
if (service !== "imessage" && service !== "sms") return {};
|
|
58
|
+
return { chatIdentifier: `${service === "sms" ? "SMS" : "iMessage"};-;${canonicalHandle}` };
|
|
59
|
+
}
|
|
60
|
+
function compareOptional(left, right) {
|
|
61
|
+
return left === void 0 || right === void 0 ? void 0 : left === right;
|
|
62
|
+
}
|
|
63
|
+
function compareChatSelector(cachedRaw, currentRaw, crossKind = false) {
|
|
64
|
+
const cached = normalizeOptionalString(cachedRaw);
|
|
65
|
+
const current = normalizeOptionalString(currentRaw);
|
|
66
|
+
if (!cached || !current) return;
|
|
67
|
+
if (cached === current) return true;
|
|
68
|
+
const cachedDirect = parseDirectChatIdentity(cached);
|
|
69
|
+
const currentDirect = parseDirectChatIdentity(current);
|
|
70
|
+
if (!cachedDirect || !currentDirect) return crossKind ? void 0 : false;
|
|
71
|
+
if (cachedDirect.identifier !== currentDirect.identifier) return false;
|
|
72
|
+
if (cachedDirect.service === "any") return true;
|
|
73
|
+
if (!cachedDirect.service || !currentDirect.service) return;
|
|
74
|
+
return cachedDirect.service === currentDirect.service;
|
|
75
|
+
}
|
|
76
|
+
function resolveIMessageChatMatch(cached, current) {
|
|
77
|
+
const cachedChatGuid = normalizeOptionalString(cached.chatGuid);
|
|
78
|
+
const currentChatGuid = normalizeOptionalString(current.chatGuid);
|
|
79
|
+
const cachedChatIdentifier = normalizeOptionalString(cached.chatIdentifier);
|
|
80
|
+
const currentChatIdentifier = normalizeOptionalString(current.chatIdentifier);
|
|
81
|
+
const comparisons = [
|
|
82
|
+
compareChatSelector(cachedChatGuid, currentChatGuid),
|
|
83
|
+
compareChatSelector(cachedChatIdentifier, currentChatIdentifier),
|
|
84
|
+
compareOptional(cached.chatId, current.chatId),
|
|
85
|
+
compareChatSelector(cachedChatGuid, currentChatIdentifier, true),
|
|
86
|
+
compareChatSelector(cachedChatIdentifier, currentChatGuid, true)
|
|
87
|
+
].filter((comparison) => comparison !== void 0);
|
|
88
|
+
if (comparisons.length === 0) return "unknown";
|
|
89
|
+
return comparisons.every(Boolean) ? "match" : "mismatch";
|
|
90
|
+
}
|
|
91
|
+
function isPositiveIMessageChatMatch(cached, current) {
|
|
92
|
+
return resolveIMessageChatMatch(cached, current) === "match";
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
8
95
|
//#region extensions/imessage/src/constants.ts
|
|
9
96
|
/** Default timeout for iMessage probe/RPC operations (10 seconds). */
|
|
10
97
|
const DEFAULT_IMESSAGE_PROBE_TIMEOUT_MS = 1e4;
|
|
11
98
|
const DEFAULT_IMESSAGE_SEND_TIMEOUT_MS = 18e4;
|
|
12
99
|
//#endregion
|
|
100
|
+
//#region extensions/imessage/src/monitor-reply-cache.ts
|
|
101
|
+
/** Recency window for the "react to the latest message" fallback. */
|
|
102
|
+
const LATEST_FALLBACK_MS = 6e5;
|
|
103
|
+
let persistenceFailureLogged = false;
|
|
104
|
+
function reportPersistenceFailure(scope, err) {
|
|
105
|
+
if (persistenceFailureLogged) return;
|
|
106
|
+
persistenceFailureLogged = true;
|
|
107
|
+
logVerbose(`imessage reply-cache: ${scope} disabled after first failure: ${String(err)}`);
|
|
108
|
+
}
|
|
109
|
+
const imessageReplyCacheByMessageId = /* @__PURE__ */ new Map();
|
|
110
|
+
const imessageShortIdToUuid = /* @__PURE__ */ new Map();
|
|
111
|
+
const imessageUuidToShortId = /* @__PURE__ */ new Map();
|
|
112
|
+
let imessageShortIdCounter = 0;
|
|
113
|
+
function openReplyCacheStore() {
|
|
114
|
+
return getIMessageRuntime().state.openSyncKeyedStore({
|
|
115
|
+
namespace: IMESSAGE_REPLY_CACHE_NAMESPACE,
|
|
116
|
+
maxEntries: IMESSAGE_REPLY_CACHE_MAX_ENTRIES
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function openReplyCacheCounterStore() {
|
|
120
|
+
return getIMessageRuntime().state.openSyncKeyedStore({
|
|
121
|
+
namespace: IMESSAGE_REPLY_CACHE_COUNTER_NAMESPACE,
|
|
122
|
+
maxEntries: 1
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function remainingTtlMs(timestamp) {
|
|
126
|
+
const remaining = IMESSAGE_REPLY_CACHE_TTL_MS - Math.max(0, Date.now() - timestamp);
|
|
127
|
+
return remaining > 0 ? remaining : void 0;
|
|
128
|
+
}
|
|
129
|
+
let hydrated = false;
|
|
130
|
+
function hydrateFromStoreOnce() {
|
|
131
|
+
if (hydrated) return;
|
|
132
|
+
hydrated = true;
|
|
133
|
+
const cutoff = Date.now() - IMESSAGE_REPLY_CACHE_TTL_MS;
|
|
134
|
+
let entries;
|
|
135
|
+
try {
|
|
136
|
+
const counter = openReplyCacheCounterStore().lookup(IMESSAGE_REPLY_CACHE_COUNTER_KEY);
|
|
137
|
+
if (counter && Number.isSafeInteger(counter.counter) && counter.counter > 0) imessageShortIdCounter = Math.max(imessageShortIdCounter, counter.counter);
|
|
138
|
+
entries = openReplyCacheStore().entries().map(({ value }) => value).filter((entry) => entry.timestamp >= cutoff).toSorted((a, b) => a.timestamp - b.timestamp).slice(-IMESSAGE_REPLY_CACHE_MAX_ENTRIES);
|
|
139
|
+
for (const entry of entries) {
|
|
140
|
+
const numeric = Number.parseInt(entry.shortId, 10);
|
|
141
|
+
if (Number.isFinite(numeric) && numeric > imessageShortIdCounter) imessageShortIdCounter = numeric;
|
|
142
|
+
}
|
|
143
|
+
} catch (err) {
|
|
144
|
+
reportPersistenceFailure("read", err);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (entries.length === 0) return;
|
|
148
|
+
for (const entry of entries) {
|
|
149
|
+
imessageReplyCacheByMessageId.set(entry.messageId, entry);
|
|
150
|
+
imessageShortIdToUuid.set(entry.shortId, entry.messageId);
|
|
151
|
+
imessageUuidToShortId.set(entry.messageId, entry.shortId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function persistReplyCacheEntry(entry) {
|
|
155
|
+
const ttlMs = remainingTtlMs(entry.timestamp);
|
|
156
|
+
if (!ttlMs) return;
|
|
157
|
+
try {
|
|
158
|
+
openReplyCacheStore().register(resolveIMessageReplyCacheEntryKey(entry.messageId), entry, { ttlMs });
|
|
159
|
+
} catch (err) {
|
|
160
|
+
reportPersistenceFailure("write", err);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function deleteReplyCacheEntry(messageId) {
|
|
164
|
+
try {
|
|
165
|
+
openReplyCacheStore().delete(resolveIMessageReplyCacheEntryKey(messageId));
|
|
166
|
+
} catch (err) {
|
|
167
|
+
reportPersistenceFailure("delete", err);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function persistReplyCacheCounter() {
|
|
171
|
+
try {
|
|
172
|
+
openReplyCacheCounterStore().register(IMESSAGE_REPLY_CACHE_COUNTER_KEY, { counter: imessageShortIdCounter });
|
|
173
|
+
} catch (err) {
|
|
174
|
+
reportPersistenceFailure("counter", err);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function buildReplyCacheEntry(entry, messageId, shortId) {
|
|
178
|
+
return {
|
|
179
|
+
accountId: entry.accountId,
|
|
180
|
+
messageId,
|
|
181
|
+
shortId,
|
|
182
|
+
timestamp: entry.timestamp,
|
|
183
|
+
...typeof entry.chatGuid === "string" ? { chatGuid: entry.chatGuid } : {},
|
|
184
|
+
...typeof entry.chatIdentifier === "string" ? { chatIdentifier: entry.chatIdentifier } : {},
|
|
185
|
+
...typeof entry.chatId === "number" ? { chatId: entry.chatId } : {},
|
|
186
|
+
...typeof entry.isFromMe === "boolean" ? { isFromMe: entry.isFromMe } : {}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function generateShortId() {
|
|
190
|
+
imessageShortIdCounter += 1;
|
|
191
|
+
persistReplyCacheCounter();
|
|
192
|
+
return String(imessageShortIdCounter);
|
|
193
|
+
}
|
|
194
|
+
function rememberIMessageReplyCache(entry) {
|
|
195
|
+
hydrateFromStoreOnce();
|
|
196
|
+
const messageId = entry.messageId.trim();
|
|
197
|
+
if (!messageId) return {
|
|
198
|
+
...entry,
|
|
199
|
+
shortId: ""
|
|
200
|
+
};
|
|
201
|
+
let shortId = imessageUuidToShortId.get(messageId);
|
|
202
|
+
if (!shortId) {
|
|
203
|
+
shortId = generateShortId();
|
|
204
|
+
imessageShortIdToUuid.set(shortId, messageId);
|
|
205
|
+
imessageUuidToShortId.set(messageId, shortId);
|
|
206
|
+
}
|
|
207
|
+
const fullEntry = buildReplyCacheEntry(entry, messageId, shortId);
|
|
208
|
+
imessageReplyCacheByMessageId.delete(messageId);
|
|
209
|
+
imessageReplyCacheByMessageId.set(messageId, fullEntry);
|
|
210
|
+
const cutoff = Date.now() - IMESSAGE_REPLY_CACHE_TTL_MS;
|
|
211
|
+
let evicted = false;
|
|
212
|
+
const deletedMessageIds = [];
|
|
213
|
+
for (const [key, value] of imessageReplyCacheByMessageId) {
|
|
214
|
+
if (value.timestamp >= cutoff) break;
|
|
215
|
+
imessageReplyCacheByMessageId.delete(key);
|
|
216
|
+
deletedMessageIds.push(key);
|
|
217
|
+
if (value.shortId) {
|
|
218
|
+
imessageShortIdToUuid.delete(value.shortId);
|
|
219
|
+
imessageUuidToShortId.delete(key);
|
|
220
|
+
}
|
|
221
|
+
evicted = true;
|
|
222
|
+
}
|
|
223
|
+
while (imessageReplyCacheByMessageId.size > IMESSAGE_REPLY_CACHE_MAX_ENTRIES) {
|
|
224
|
+
const oldest = imessageReplyCacheByMessageId.keys().next().value;
|
|
225
|
+
if (!oldest) break;
|
|
226
|
+
const oldEntry = imessageReplyCacheByMessageId.get(oldest);
|
|
227
|
+
imessageReplyCacheByMessageId.delete(oldest);
|
|
228
|
+
deletedMessageIds.push(oldest);
|
|
229
|
+
if (oldEntry?.shortId) {
|
|
230
|
+
imessageShortIdToUuid.delete(oldEntry.shortId);
|
|
231
|
+
imessageUuidToShortId.delete(oldest);
|
|
232
|
+
}
|
|
233
|
+
evicted = true;
|
|
234
|
+
}
|
|
235
|
+
if (evicted) for (const messageIdToDelete of deletedMessageIds) deleteReplyCacheEntry(messageIdToDelete);
|
|
236
|
+
persistReplyCacheEntry(fullEntry);
|
|
237
|
+
return fullEntry;
|
|
238
|
+
}
|
|
239
|
+
function hasChatScope(ctx) {
|
|
240
|
+
if (!ctx) return false;
|
|
241
|
+
return Boolean(normalizeOptionalString(ctx.chatGuid) || normalizeOptionalString(ctx.chatIdentifier) || typeof ctx.chatId === "number");
|
|
242
|
+
}
|
|
243
|
+
function isCrossChatMismatch(cached, ctx) {
|
|
244
|
+
return resolveIMessageChatMatch(cached, ctx) === "mismatch";
|
|
245
|
+
}
|
|
246
|
+
function describeChatForError(values) {
|
|
247
|
+
const parts = [];
|
|
248
|
+
if (normalizeOptionalString(values.chatGuid)) parts.push("chatGuid=<redacted>");
|
|
249
|
+
if (normalizeOptionalString(values.chatIdentifier)) parts.push("chatIdentifier=<redacted>");
|
|
250
|
+
if (typeof values.chatId === "number") parts.push("chatId=<redacted>");
|
|
251
|
+
return parts.length === 0 ? "<unknown chat>" : parts.join(", ");
|
|
252
|
+
}
|
|
253
|
+
function describeMessageIdForError(inputId, inputKind) {
|
|
254
|
+
if (inputKind === "short") return `<short:${inputId.length}-digit>`;
|
|
255
|
+
return `<uuid:${inputId.slice(0, 8)}...>`;
|
|
256
|
+
}
|
|
257
|
+
function buildCrossChatError(inputId, inputKind, cached, ctx) {
|
|
258
|
+
const remediation = inputKind === "short" ? "Use a message ID from the current chat target; MessageSidFull from another chat is rejected." : "Retry with the correct chat target.";
|
|
259
|
+
return /* @__PURE__ */ new Error(`iMessage message id ${describeMessageIdForError(inputId, inputKind)} belongs to a different chat (${describeChatForError(cached)}) than the current call target (${describeChatForError(ctx)}). ${remediation}`);
|
|
260
|
+
}
|
|
261
|
+
function resolveIMessageMessageId(shortOrUuid, opts) {
|
|
262
|
+
const trimmed = shortOrUuid.trim();
|
|
263
|
+
if (!trimmed) return trimmed;
|
|
264
|
+
hydrateFromStoreOnce();
|
|
265
|
+
if (/^\d+$/.test(trimmed)) {
|
|
266
|
+
const uuid = imessageShortIdToUuid.get(trimmed);
|
|
267
|
+
if (uuid) {
|
|
268
|
+
const cached = imessageReplyCacheByMessageId.get(uuid);
|
|
269
|
+
if (opts?.chatContext && hasChatScope(opts.chatContext)) {
|
|
270
|
+
if (cached && isCrossChatMismatch(cached, opts.chatContext)) throw buildCrossChatError(trimmed, "short", cached, opts.chatContext);
|
|
271
|
+
}
|
|
272
|
+
if (opts?.requireFromMe && cached?.isFromMe !== true) throw buildFromMeError(trimmed, "short");
|
|
273
|
+
return uuid;
|
|
274
|
+
}
|
|
275
|
+
if (opts?.requireKnownShortId && !hasChatScope(opts.chatContext)) throw new Error(`iMessage short message id ${describeMessageIdForError(trimmed, "short")} requires a chat scope (chatGuid / chatIdentifier / chatId or a target).`);
|
|
276
|
+
if (opts?.requireKnownShortId) throw new Error(`iMessage short message id ${describeMessageIdForError(trimmed, "short")} is no longer available. Use MessageSidFull.`);
|
|
277
|
+
return trimmed;
|
|
278
|
+
}
|
|
279
|
+
const cached = imessageReplyCacheByMessageId.get(trimmed);
|
|
280
|
+
if (opts?.chatContext) {
|
|
281
|
+
if (cached && isCrossChatMismatch(cached, opts.chatContext)) throw buildCrossChatError(trimmed, "uuid", cached, opts.chatContext);
|
|
282
|
+
}
|
|
283
|
+
if (opts?.requireFromMe && cached?.isFromMe !== true) throw buildFromMeError(trimmed, "uuid");
|
|
284
|
+
return trimmed;
|
|
285
|
+
}
|
|
286
|
+
function isKnownFromMeIMessageMessageId(messageId, ctx) {
|
|
287
|
+
const trimmed = normalizeOptionalString(messageId);
|
|
288
|
+
if (!trimmed || !ctx.accountId || !hasChatScope(ctx)) return false;
|
|
289
|
+
hydrateFromStoreOnce();
|
|
290
|
+
const cached = imessageReplyCacheByMessageId.get(trimmed);
|
|
291
|
+
if (!cached || cached.isFromMe !== true || cached.accountId !== ctx.accountId) return false;
|
|
292
|
+
return isPositiveIMessageChatMatch(cached, ctx);
|
|
293
|
+
}
|
|
294
|
+
function buildFromMeError(inputId, inputKind) {
|
|
295
|
+
return /* @__PURE__ */ new Error(`iMessage message id ${describeMessageIdForError(inputId, inputKind)} is not one this agent sent. edit and unsend can only target messages the gateway delivered itself; messages received from other participants cannot be modified.`);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Return the most recent cached entry whose chat scope matches the supplied
|
|
299
|
+
* context. Used as a fallback when an agent calls a per-message action (e.g.
|
|
300
|
+
* `react`) without specifying a `messageId` — the natural intent is "react
|
|
301
|
+
* to the message I just received in this chat."
|
|
302
|
+
*
|
|
303
|
+
* Strict semantics for safety:
|
|
304
|
+
* - Caller must supply a chat scope. We refuse to "guess" the active chat.
|
|
305
|
+
* - Cached entry must positively match on at least one identifier kind
|
|
306
|
+
* (chatGuid, chatIdentifier, chatId, or normalized direct-DM fingerprint).
|
|
307
|
+
* We do NOT fall through on "no overlapping identifier" — that's how a
|
|
308
|
+
* cached entry from a foreign chat could be returned when the caller's
|
|
309
|
+
* context didn't share any identifier kind with the cache.
|
|
310
|
+
* - Caller must supply an accountId; we never cross account boundaries.
|
|
311
|
+
* - We only consider entries newer than `LATEST_FALLBACK_MS`. The intent
|
|
312
|
+
* of "react to the latest" is "the message I just received," not
|
|
313
|
+
* "anything in this chat from any time."
|
|
314
|
+
*/
|
|
315
|
+
function findLatestIMessageEntryForChat(ctx) {
|
|
316
|
+
if (!hasChatScope(ctx)) return;
|
|
317
|
+
if (!ctx.accountId) return;
|
|
318
|
+
const cutoff = Date.now() - LATEST_FALLBACK_MS;
|
|
319
|
+
let best;
|
|
320
|
+
for (const entry of imessageReplyCacheByMessageId.values()) {
|
|
321
|
+
if (entry.accountId !== ctx.accountId) continue;
|
|
322
|
+
if (entry.timestamp < cutoff) continue;
|
|
323
|
+
if (!isPositiveIMessageChatMatch(entry, ctx)) continue;
|
|
324
|
+
if (!best || entry.timestamp > best.timestamp) best = entry;
|
|
325
|
+
}
|
|
326
|
+
return best;
|
|
327
|
+
}
|
|
328
|
+
function resolveIMessageCachedResourceBinding(messageId, ctx) {
|
|
329
|
+
hydrateFromStoreOnce();
|
|
330
|
+
const entry = imessageReplyCacheByMessageId.get(messageId.trim());
|
|
331
|
+
if (!entry) return "unknown";
|
|
332
|
+
if (Date.now() - entry.timestamp > 216e5) return "unknown";
|
|
333
|
+
if (entry.accountId !== ctx.accountId) return "mismatch";
|
|
334
|
+
const chatMatch = resolveIMessageChatMatch(entry, ctx);
|
|
335
|
+
if (chatMatch !== "match") return chatMatch;
|
|
336
|
+
return "match";
|
|
337
|
+
}
|
|
338
|
+
function isIMessageCurrentMessageInChat(params) {
|
|
339
|
+
if (!params.accountId || !hasChatScope(params.chatContext)) return false;
|
|
340
|
+
const currentMessageId = normalizeOptionalString(String(params.currentMessageId));
|
|
341
|
+
if (!currentMessageId) return false;
|
|
342
|
+
hydrateFromStoreOnce();
|
|
343
|
+
const fullMessageId = /^\d+$/.test(currentMessageId) ? imessageShortIdToUuid.get(currentMessageId) : currentMessageId;
|
|
344
|
+
if (!fullMessageId) return false;
|
|
345
|
+
return resolveIMessageCachedResourceBinding(fullMessageId, {
|
|
346
|
+
...params.chatContext,
|
|
347
|
+
accountId: params.accountId
|
|
348
|
+
}) === "match";
|
|
349
|
+
}
|
|
350
|
+
//#endregion
|
|
13
351
|
//#region extensions/imessage/src/client.ts
|
|
14
352
|
var IMessageRpcRequestError = class extends Error {
|
|
15
353
|
constructor(message, code, data) {
|
|
@@ -19,6 +357,16 @@ var IMessageRpcRequestError = class extends Error {
|
|
|
19
357
|
this.name = "IMessageRpcRequestError";
|
|
20
358
|
}
|
|
21
359
|
};
|
|
360
|
+
function isIMessageBridgeStall(error) {
|
|
361
|
+
if (!(error instanceof Error)) return false;
|
|
362
|
+
return error.message.includes("Timed out waiting for response");
|
|
363
|
+
}
|
|
364
|
+
const BRIDGE_STALL_GUIDANCE = "The imsg private API bridge stopped responding. Run `imsg launch` to re-inject the dylib, then `openclaw channels status --probe` to refresh capability detection.";
|
|
365
|
+
function describeIMessageBridgeStall(error) {
|
|
366
|
+
if (error instanceof IMessageRpcRequestError) return new IMessageRpcRequestError(`${error.message} ${BRIDGE_STALL_GUIDANCE}`, error.code, error.data);
|
|
367
|
+
if (error instanceof Error) return new Error(`${error.message} ${BRIDGE_STALL_GUIDANCE}`, { cause: error });
|
|
368
|
+
return error;
|
|
369
|
+
}
|
|
22
370
|
const PUBLIC_IMESSAGE_FULL_DISK_ACCESS_ERROR = "imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.";
|
|
23
371
|
const IMESSAGE_RPC_CLOSE_GRACE_MS = 500;
|
|
24
372
|
function isTestEnv() {
|
|
@@ -44,7 +392,8 @@ var IMessageRpcClient = class {
|
|
|
44
392
|
this.stderrDecoder = new StringDecoder("utf8");
|
|
45
393
|
this.nextId = 1;
|
|
46
394
|
this.publicProcessError = null;
|
|
47
|
-
this.
|
|
395
|
+
this.configuredCliPath = opts.cliPath?.trim() || "imsg";
|
|
396
|
+
this.cliPath = expandIMessageUserPath(this.configuredCliPath);
|
|
48
397
|
const dbPath = opts.dbPath?.trim();
|
|
49
398
|
this.dbPath = dbPath ? opts.remoteHost?.trim() ? dbPath : resolveUserPath(dbPath) : void 0;
|
|
50
399
|
this.runtime = opts.runtime;
|
|
@@ -129,7 +478,15 @@ var IMessageRpcClient = class {
|
|
|
129
478
|
} catch (err) {
|
|
130
479
|
this.failTransport(err, this.child);
|
|
131
480
|
}
|
|
132
|
-
|
|
481
|
+
try {
|
|
482
|
+
return await response;
|
|
483
|
+
} catch (err) {
|
|
484
|
+
if (isIMessageBridgeStall(err)) {
|
|
485
|
+
invalidateCachedIMessagePrivateApiStatus(this.configuredCliPath);
|
|
486
|
+
throw describeIMessageBridgeStall(err);
|
|
487
|
+
}
|
|
488
|
+
throw err;
|
|
489
|
+
}
|
|
133
490
|
}
|
|
134
491
|
async stopChild(child) {
|
|
135
492
|
try {
|
|
@@ -329,107 +686,61 @@ function codeDelimiter(content) {
|
|
|
329
686
|
const longestRun = Math.max(0, ...[...content.matchAll(/`+/gu)].map((match) => match[0].length));
|
|
330
687
|
return "`".repeat(longestRun + 1);
|
|
331
688
|
}
|
|
332
|
-
function
|
|
333
|
-
const protections = [];
|
|
334
|
-
let markdown = "";
|
|
335
|
-
let cursor = 0;
|
|
336
|
-
for (const match of input.matchAll(/__[\p{L}_][\p{L}\p{N}_]*__/gu)) {
|
|
337
|
-
const identifier = match[0];
|
|
338
|
-
const start = match.index ?? 0;
|
|
339
|
-
const end = start + identifier.length;
|
|
340
|
-
const before = input[start - 1];
|
|
341
|
-
const beforeBefore = input[start - 2];
|
|
342
|
-
const after = input[end];
|
|
343
|
-
const member = before === ".";
|
|
344
|
-
const call = after === "(";
|
|
345
|
-
const functionArgument = before === "(" && /[\p{L}\p{N}_]/u.test(beforeBefore ?? "");
|
|
346
|
-
if (!member && !call && !functionArgument && !(after === "[")) continue;
|
|
347
|
-
let token = `OCdunder${protections.length}token`;
|
|
348
|
-
while (input.includes(token)) token += "x";
|
|
349
|
-
markdown += input.slice(cursor, start) + token;
|
|
350
|
-
protections.push({
|
|
351
|
-
token,
|
|
352
|
-
identifier
|
|
353
|
-
});
|
|
354
|
-
cursor = end;
|
|
355
|
-
}
|
|
356
|
-
return {
|
|
357
|
-
markdown: markdown + input.slice(cursor),
|
|
358
|
-
protections
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
function applyTextEdits(text, edits) {
|
|
362
|
-
const ordered = edits.toSorted((left, right) => left.start - right.start);
|
|
689
|
+
function restoreCodeMarkers(text, ranges, codeRanges) {
|
|
363
690
|
let rendered = "";
|
|
364
691
|
let cursor = 0;
|
|
365
|
-
|
|
366
|
-
rendered += text.slice(cursor, edit.start) + edit.text;
|
|
367
|
-
cursor = edit.end;
|
|
368
|
-
}
|
|
369
|
-
rendered += text.slice(cursor);
|
|
370
|
-
return {
|
|
371
|
-
text: rendered,
|
|
372
|
-
mapOffset: (offset) => offset + ordered.reduce((delta, edit) => delta + (edit.end <= offset ? edit.text.length - edit.end + edit.start : 0), 0)
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
function restoreDunderIdentifiers(text, ranges, codeRanges, protections) {
|
|
376
|
-
const edited = applyTextEdits(text, protections.flatMap((protection) => {
|
|
377
|
-
const start = text.indexOf(protection.token);
|
|
378
|
-
return start === -1 ? [] : [{
|
|
379
|
-
start,
|
|
380
|
-
end: start + protection.token.length,
|
|
381
|
-
text: protection.identifier
|
|
382
|
-
}];
|
|
383
|
-
}));
|
|
384
|
-
const mapRange = (range) => ({
|
|
385
|
-
...range,
|
|
386
|
-
start: edited.mapOffset(range.start),
|
|
387
|
-
length: edited.mapOffset(range.start + range.length) - edited.mapOffset(range.start)
|
|
388
|
-
});
|
|
389
|
-
return {
|
|
390
|
-
text: edited.text,
|
|
391
|
-
ranges: ranges.map(mapRange),
|
|
392
|
-
codeRanges: codeRanges.map(mapRange)
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
function restoreCodeMarkers(text, ranges, codeRanges) {
|
|
396
|
-
const edited = applyTextEdits(text, codeRanges.map((range) => {
|
|
692
|
+
const edits = codeRanges.map((range) => {
|
|
397
693
|
const end = range.start + range.length;
|
|
398
694
|
const content = text.slice(range.start, end);
|
|
399
695
|
const marker = codeDelimiter(content);
|
|
400
696
|
const padding = content.startsWith("`") || content.endsWith("`") ? " " : "";
|
|
697
|
+
rendered += text.slice(cursor, range.start) + `${marker}${padding}${content}${padding}${marker}`;
|
|
698
|
+
cursor = end;
|
|
401
699
|
return {
|
|
402
|
-
start: range.start,
|
|
403
700
|
end,
|
|
404
|
-
|
|
701
|
+
shift: rendered.length - end
|
|
405
702
|
};
|
|
406
|
-
})
|
|
703
|
+
});
|
|
704
|
+
rendered += text.slice(cursor);
|
|
705
|
+
const mapOffset = (offset) => {
|
|
706
|
+
let low = 0;
|
|
707
|
+
let high = edits.length;
|
|
708
|
+
while (low < high) {
|
|
709
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
710
|
+
const edit = edits[middle];
|
|
711
|
+
if (edit && edit.end <= offset) low = middle + 1;
|
|
712
|
+
else high = middle;
|
|
713
|
+
}
|
|
714
|
+
return offset + (edits[low - 1]?.shift ?? 0);
|
|
715
|
+
};
|
|
407
716
|
return {
|
|
408
|
-
text:
|
|
409
|
-
ranges: ranges.map((range) =>
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
717
|
+
text: rendered,
|
|
718
|
+
ranges: ranges.map((range) => {
|
|
719
|
+
const start = mapOffset(range.start);
|
|
720
|
+
return {
|
|
721
|
+
...range,
|
|
722
|
+
start,
|
|
723
|
+
length: mapOffset(range.start + range.length) - start
|
|
724
|
+
};
|
|
725
|
+
})
|
|
414
726
|
};
|
|
415
727
|
}
|
|
416
728
|
function extractMarkdownFormatRuns(input) {
|
|
417
|
-
const
|
|
418
|
-
const ir = markdownToIR(protectedDunders.markdown, {
|
|
729
|
+
const ir = markdownToIR(input, {
|
|
419
730
|
autolink: false,
|
|
420
731
|
enableHtmlUnderline: true,
|
|
421
732
|
headingStyle: "rich",
|
|
422
733
|
linkify: false,
|
|
734
|
+
preserveDunderIdentifiers: true,
|
|
423
735
|
preserveSourceBlockSpacing: true
|
|
424
736
|
});
|
|
425
737
|
const rendered = renderMarkdownWithAttributedRanges(ir, { styleMap: IMESSAGE_STYLE_MAP }, IMESSAGE_FORMAT_PROFILE);
|
|
426
738
|
const code = renderMarkdownWithAttributedRanges(ir, { styleMap: { code: "code" } }, IMESSAGE_CODE_PROFILE);
|
|
427
|
-
|
|
739
|
+
return restoreCodeMarkers(rendered.text, rendered.ranges.map(({ start, length, style }) => ({
|
|
428
740
|
start,
|
|
429
741
|
length,
|
|
430
742
|
styles: [style]
|
|
431
|
-
})), code.ranges
|
|
432
|
-
return restoreCodeMarkers(dunders.text, dunders.ranges, dunders.codeRanges);
|
|
743
|
+
})), code.ranges);
|
|
433
744
|
}
|
|
434
745
|
//#endregion
|
|
435
746
|
//#region extensions/imessage/src/monitor/sanitize-outbound.ts
|
|
@@ -893,4 +1204,4 @@ function sanitizeOutboundText(text) {
|
|
|
893
1204
|
return cleaned;
|
|
894
1205
|
}
|
|
895
1206
|
//#endregion
|
|
896
|
-
export { IMessageRpcRequestError as a,
|
|
1207
|
+
export { normalizeDirectChatIdentifier as _, IMessageRpcRequestError as a, isIMessageCurrentMessageInChat as c, resolveIMessageCachedResourceBinding as d, resolveIMessageMessageId as f, isIMessageEmailChatIdentifier as g, chatContextFromIMessageTarget as h, extractMarkdownFormatRuns as i, isKnownFromMeIMessageMessageId as l, DEFAULT_IMESSAGE_SEND_TIMEOUT_MS as m, sanitizeIMessageFinalOutboundText as n, createIMessageRpcClient as o, DEFAULT_IMESSAGE_PROBE_TIMEOUT_MS as p, sanitizeOutboundText as r, findLatestIMessageEntryForChat as s, protectIMessageFencedRoleMarkers as t, rememberIMessageReplyCache as u, resolveIMessageDirectChatService as v };
|