@openclaw/bluebubbles 2026.1.29

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/src/targets.ts ADDED
@@ -0,0 +1,323 @@
1
+ export type BlueBubblesService = "imessage" | "sms" | "auto";
2
+
3
+ export type BlueBubblesTarget =
4
+ | { kind: "chat_id"; chatId: number }
5
+ | { kind: "chat_guid"; chatGuid: string }
6
+ | { kind: "chat_identifier"; chatIdentifier: string }
7
+ | { kind: "handle"; to: string; service: BlueBubblesService };
8
+
9
+ export type BlueBubblesAllowTarget =
10
+ | { kind: "chat_id"; chatId: number }
11
+ | { kind: "chat_guid"; chatGuid: string }
12
+ | { kind: "chat_identifier"; chatIdentifier: string }
13
+ | { kind: "handle"; handle: string };
14
+
15
+ const CHAT_ID_PREFIXES = ["chat_id:", "chatid:", "chat:"];
16
+ const CHAT_GUID_PREFIXES = ["chat_guid:", "chatguid:", "guid:"];
17
+ const CHAT_IDENTIFIER_PREFIXES = ["chat_identifier:", "chatidentifier:", "chatident:"];
18
+ const SERVICE_PREFIXES: Array<{ prefix: string; service: BlueBubblesService }> = [
19
+ { prefix: "imessage:", service: "imessage" },
20
+ { prefix: "sms:", service: "sms" },
21
+ { prefix: "auto:", service: "auto" },
22
+ ];
23
+ const CHAT_IDENTIFIER_UUID_RE =
24
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
25
+ const CHAT_IDENTIFIER_HEX_RE = /^[0-9a-f]{24,64}$/i;
26
+
27
+ function parseRawChatGuid(value: string): string | null {
28
+ const trimmed = value.trim();
29
+ if (!trimmed) return null;
30
+ const parts = trimmed.split(";");
31
+ if (parts.length !== 3) return null;
32
+ const service = parts[0]?.trim();
33
+ const separator = parts[1]?.trim();
34
+ const identifier = parts[2]?.trim();
35
+ if (!service || !identifier) return null;
36
+ if (separator !== "+" && separator !== "-") return null;
37
+ return `${service};${separator};${identifier}`;
38
+ }
39
+
40
+ function stripPrefix(value: string, prefix: string): string {
41
+ return value.slice(prefix.length).trim();
42
+ }
43
+
44
+ function stripBlueBubblesPrefix(value: string): string {
45
+ const trimmed = value.trim();
46
+ if (!trimmed) return "";
47
+ if (!trimmed.toLowerCase().startsWith("bluebubbles:")) return trimmed;
48
+ return trimmed.slice("bluebubbles:".length).trim();
49
+ }
50
+
51
+ function looksLikeRawChatIdentifier(value: string): boolean {
52
+ const trimmed = value.trim();
53
+ if (!trimmed) return false;
54
+ if (/^chat\d+$/i.test(trimmed)) return true;
55
+ return CHAT_IDENTIFIER_UUID_RE.test(trimmed) || CHAT_IDENTIFIER_HEX_RE.test(trimmed);
56
+ }
57
+
58
+ export function normalizeBlueBubblesHandle(raw: string): string {
59
+ const trimmed = raw.trim();
60
+ if (!trimmed) return "";
61
+ const lowered = trimmed.toLowerCase();
62
+ if (lowered.startsWith("imessage:")) return normalizeBlueBubblesHandle(trimmed.slice(9));
63
+ if (lowered.startsWith("sms:")) return normalizeBlueBubblesHandle(trimmed.slice(4));
64
+ if (lowered.startsWith("auto:")) return normalizeBlueBubblesHandle(trimmed.slice(5));
65
+ if (trimmed.includes("@")) return trimmed.toLowerCase();
66
+ return trimmed.replace(/\s+/g, "");
67
+ }
68
+
69
+ /**
70
+ * Extracts the handle from a chat_guid if it's a DM (1:1 chat).
71
+ * BlueBubbles chat_guid format for DM: "service;-;handle" (e.g., "iMessage;-;+19257864429")
72
+ * Group chat format: "service;+;groupId" (has "+" instead of "-")
73
+ */
74
+ export function extractHandleFromChatGuid(chatGuid: string): string | null {
75
+ const parts = chatGuid.split(";");
76
+ // DM format: service;-;handle (3 parts, middle is "-")
77
+ if (parts.length === 3 && parts[1] === "-") {
78
+ const handle = parts[2]?.trim();
79
+ if (handle) return normalizeBlueBubblesHandle(handle);
80
+ }
81
+ return null;
82
+ }
83
+
84
+ export function normalizeBlueBubblesMessagingTarget(raw: string): string | undefined {
85
+ let trimmed = raw.trim();
86
+ if (!trimmed) return undefined;
87
+ trimmed = stripBlueBubblesPrefix(trimmed);
88
+ if (!trimmed) return undefined;
89
+ try {
90
+ const parsed = parseBlueBubblesTarget(trimmed);
91
+ if (parsed.kind === "chat_id") return `chat_id:${parsed.chatId}`;
92
+ if (parsed.kind === "chat_guid") {
93
+ // For DM chat_guids, normalize to just the handle for easier comparison.
94
+ // This allows "chat_guid:iMessage;-;+1234567890" to match "+1234567890".
95
+ const handle = extractHandleFromChatGuid(parsed.chatGuid);
96
+ if (handle) return handle;
97
+ // For group chats or unrecognized formats, keep the full chat_guid
98
+ return `chat_guid:${parsed.chatGuid}`;
99
+ }
100
+ if (parsed.kind === "chat_identifier") return `chat_identifier:${parsed.chatIdentifier}`;
101
+ const handle = normalizeBlueBubblesHandle(parsed.to);
102
+ if (!handle) return undefined;
103
+ return parsed.service === "auto" ? handle : `${parsed.service}:${handle}`;
104
+ } catch {
105
+ return trimmed;
106
+ }
107
+ }
108
+
109
+ export function looksLikeBlueBubblesTargetId(raw: string, normalized?: string): boolean {
110
+ const trimmed = raw.trim();
111
+ if (!trimmed) return false;
112
+ const candidate = stripBlueBubblesPrefix(trimmed);
113
+ if (!candidate) return false;
114
+ if (parseRawChatGuid(candidate)) return true;
115
+ const lowered = candidate.toLowerCase();
116
+ if (/^(imessage|sms|auto):/.test(lowered)) return true;
117
+ if (
118
+ /^(chat_id|chatid|chat|chat_guid|chatguid|guid|chat_identifier|chatidentifier|chatident|group):/.test(
119
+ lowered,
120
+ )
121
+ ) {
122
+ return true;
123
+ }
124
+ // Recognize chat<digits> patterns (e.g., "chat660250192681427962") as chat IDs
125
+ if (/^chat\d+$/i.test(candidate)) return true;
126
+ if (looksLikeRawChatIdentifier(candidate)) return true;
127
+ if (candidate.includes("@")) return true;
128
+ const digitsOnly = candidate.replace(/[\s().-]/g, "");
129
+ if (/^\+?\d{3,}$/.test(digitsOnly)) return true;
130
+ if (normalized) {
131
+ const normalizedTrimmed = normalized.trim();
132
+ if (!normalizedTrimmed) return false;
133
+ const normalizedLower = normalizedTrimmed.toLowerCase();
134
+ if (
135
+ /^(imessage|sms|auto):/.test(normalizedLower) ||
136
+ /^(chat_id|chat_guid|chat_identifier):/.test(normalizedLower)
137
+ ) {
138
+ return true;
139
+ }
140
+ }
141
+ return false;
142
+ }
143
+
144
+ export function parseBlueBubblesTarget(raw: string): BlueBubblesTarget {
145
+ const trimmed = stripBlueBubblesPrefix(raw);
146
+ if (!trimmed) throw new Error("BlueBubbles target is required");
147
+ const lower = trimmed.toLowerCase();
148
+
149
+ for (const { prefix, service } of SERVICE_PREFIXES) {
150
+ if (lower.startsWith(prefix)) {
151
+ const remainder = stripPrefix(trimmed, prefix);
152
+ if (!remainder) throw new Error(`${prefix} target is required`);
153
+ const remainderLower = remainder.toLowerCase();
154
+ const isChatTarget =
155
+ CHAT_ID_PREFIXES.some((p) => remainderLower.startsWith(p)) ||
156
+ CHAT_GUID_PREFIXES.some((p) => remainderLower.startsWith(p)) ||
157
+ CHAT_IDENTIFIER_PREFIXES.some((p) => remainderLower.startsWith(p)) ||
158
+ remainderLower.startsWith("group:");
159
+ if (isChatTarget) {
160
+ return parseBlueBubblesTarget(remainder);
161
+ }
162
+ return { kind: "handle", to: remainder, service };
163
+ }
164
+ }
165
+
166
+ for (const prefix of CHAT_ID_PREFIXES) {
167
+ if (lower.startsWith(prefix)) {
168
+ const value = stripPrefix(trimmed, prefix);
169
+ const chatId = Number.parseInt(value, 10);
170
+ if (!Number.isFinite(chatId)) {
171
+ throw new Error(`Invalid chat_id: ${value}`);
172
+ }
173
+ return { kind: "chat_id", chatId };
174
+ }
175
+ }
176
+
177
+ for (const prefix of CHAT_GUID_PREFIXES) {
178
+ if (lower.startsWith(prefix)) {
179
+ const value = stripPrefix(trimmed, prefix);
180
+ if (!value) throw new Error("chat_guid is required");
181
+ return { kind: "chat_guid", chatGuid: value };
182
+ }
183
+ }
184
+
185
+ for (const prefix of CHAT_IDENTIFIER_PREFIXES) {
186
+ if (lower.startsWith(prefix)) {
187
+ const value = stripPrefix(trimmed, prefix);
188
+ if (!value) throw new Error("chat_identifier is required");
189
+ return { kind: "chat_identifier", chatIdentifier: value };
190
+ }
191
+ }
192
+
193
+ if (lower.startsWith("group:")) {
194
+ const value = stripPrefix(trimmed, "group:");
195
+ const chatId = Number.parseInt(value, 10);
196
+ if (Number.isFinite(chatId)) {
197
+ return { kind: "chat_id", chatId };
198
+ }
199
+ if (!value) throw new Error("group target is required");
200
+ return { kind: "chat_guid", chatGuid: value };
201
+ }
202
+
203
+ const rawChatGuid = parseRawChatGuid(trimmed);
204
+ if (rawChatGuid) {
205
+ return { kind: "chat_guid", chatGuid: rawChatGuid };
206
+ }
207
+
208
+ // Handle chat<digits> pattern (e.g., "chat660250192681427962") as chat_identifier
209
+ // These are BlueBubbles chat identifiers (the third part of a chat GUID), not numeric IDs
210
+ if (/^chat\d+$/i.test(trimmed)) {
211
+ return { kind: "chat_identifier", chatIdentifier: trimmed };
212
+ }
213
+
214
+ // Handle UUID/hex chat identifiers (e.g., "8b9c1a10536d4d86a336ea03ab7151cc")
215
+ if (looksLikeRawChatIdentifier(trimmed)) {
216
+ return { kind: "chat_identifier", chatIdentifier: trimmed };
217
+ }
218
+
219
+ return { kind: "handle", to: trimmed, service: "auto" };
220
+ }
221
+
222
+ export function parseBlueBubblesAllowTarget(raw: string): BlueBubblesAllowTarget {
223
+ const trimmed = raw.trim();
224
+ if (!trimmed) return { kind: "handle", handle: "" };
225
+ const lower = trimmed.toLowerCase();
226
+
227
+ for (const { prefix } of SERVICE_PREFIXES) {
228
+ if (lower.startsWith(prefix)) {
229
+ const remainder = stripPrefix(trimmed, prefix);
230
+ if (!remainder) return { kind: "handle", handle: "" };
231
+ return parseBlueBubblesAllowTarget(remainder);
232
+ }
233
+ }
234
+
235
+ for (const prefix of CHAT_ID_PREFIXES) {
236
+ if (lower.startsWith(prefix)) {
237
+ const value = stripPrefix(trimmed, prefix);
238
+ const chatId = Number.parseInt(value, 10);
239
+ if (Number.isFinite(chatId)) return { kind: "chat_id", chatId };
240
+ }
241
+ }
242
+
243
+ for (const prefix of CHAT_GUID_PREFIXES) {
244
+ if (lower.startsWith(prefix)) {
245
+ const value = stripPrefix(trimmed, prefix);
246
+ if (value) return { kind: "chat_guid", chatGuid: value };
247
+ }
248
+ }
249
+
250
+ for (const prefix of CHAT_IDENTIFIER_PREFIXES) {
251
+ if (lower.startsWith(prefix)) {
252
+ const value = stripPrefix(trimmed, prefix);
253
+ if (value) return { kind: "chat_identifier", chatIdentifier: value };
254
+ }
255
+ }
256
+
257
+ if (lower.startsWith("group:")) {
258
+ const value = stripPrefix(trimmed, "group:");
259
+ const chatId = Number.parseInt(value, 10);
260
+ if (Number.isFinite(chatId)) return { kind: "chat_id", chatId };
261
+ if (value) return { kind: "chat_guid", chatGuid: value };
262
+ }
263
+
264
+ // Handle chat<digits> pattern (e.g., "chat660250192681427962") as chat_identifier
265
+ // These are BlueBubbles chat identifiers (the third part of a chat GUID), not numeric IDs
266
+ if (/^chat\d+$/i.test(trimmed)) {
267
+ return { kind: "chat_identifier", chatIdentifier: trimmed };
268
+ }
269
+
270
+ // Handle UUID/hex chat identifiers (e.g., "8b9c1a10536d4d86a336ea03ab7151cc")
271
+ if (looksLikeRawChatIdentifier(trimmed)) {
272
+ return { kind: "chat_identifier", chatIdentifier: trimmed };
273
+ }
274
+
275
+ return { kind: "handle", handle: normalizeBlueBubblesHandle(trimmed) };
276
+ }
277
+
278
+ export function isAllowedBlueBubblesSender(params: {
279
+ allowFrom: Array<string | number>;
280
+ sender: string;
281
+ chatId?: number | null;
282
+ chatGuid?: string | null;
283
+ chatIdentifier?: string | null;
284
+ }): boolean {
285
+ const allowFrom = params.allowFrom.map((entry) => String(entry).trim());
286
+ if (allowFrom.length === 0) return true;
287
+ if (allowFrom.includes("*")) return true;
288
+
289
+ const senderNormalized = normalizeBlueBubblesHandle(params.sender);
290
+ const chatId = params.chatId ?? undefined;
291
+ const chatGuid = params.chatGuid?.trim();
292
+ const chatIdentifier = params.chatIdentifier?.trim();
293
+
294
+ for (const entry of allowFrom) {
295
+ if (!entry) continue;
296
+ const parsed = parseBlueBubblesAllowTarget(entry);
297
+ if (parsed.kind === "chat_id" && chatId !== undefined) {
298
+ if (parsed.chatId === chatId) return true;
299
+ } else if (parsed.kind === "chat_guid" && chatGuid) {
300
+ if (parsed.chatGuid === chatGuid) return true;
301
+ } else if (parsed.kind === "chat_identifier" && chatIdentifier) {
302
+ if (parsed.chatIdentifier === chatIdentifier) return true;
303
+ } else if (parsed.kind === "handle" && senderNormalized) {
304
+ if (parsed.handle === senderNormalized) return true;
305
+ }
306
+ }
307
+ return false;
308
+ }
309
+
310
+ export function formatBlueBubblesChatTarget(params: {
311
+ chatId?: number | null;
312
+ chatGuid?: string | null;
313
+ chatIdentifier?: string | null;
314
+ }): string {
315
+ if (params.chatId && Number.isFinite(params.chatId)) {
316
+ return `chat_id:${params.chatId}`;
317
+ }
318
+ const guid = params.chatGuid?.trim();
319
+ if (guid) return `chat_guid:${guid}`;
320
+ const identifier = params.chatIdentifier?.trim();
321
+ if (identifier) return `chat_identifier:${identifier}`;
322
+ return "";
323
+ }
package/src/types.ts ADDED
@@ -0,0 +1,127 @@
1
+ export type DmPolicy = "pairing" | "allowlist" | "open" | "disabled";
2
+ export type GroupPolicy = "open" | "disabled" | "allowlist";
3
+
4
+ export type BlueBubblesGroupConfig = {
5
+ /** If true, only respond in this group when mentioned. */
6
+ requireMention?: boolean;
7
+ /** Optional tool policy overrides for this group. */
8
+ tools?: { allow?: string[]; deny?: string[] };
9
+ };
10
+
11
+ export type BlueBubblesAccountConfig = {
12
+ /** Optional display name for this account (used in CLI/UI lists). */
13
+ name?: string;
14
+ /** Optional provider capability tags used for agent/runtime guidance. */
15
+ capabilities?: string[];
16
+ /** Allow channel-initiated config writes (default: true). */
17
+ configWrites?: boolean;
18
+ /** If false, do not start this BlueBubbles account. Default: true. */
19
+ enabled?: boolean;
20
+ /** Base URL for the BlueBubbles API. */
21
+ serverUrl?: string;
22
+ /** Password for BlueBubbles API authentication. */
23
+ password?: string;
24
+ /** Webhook path for the gateway HTTP server. */
25
+ webhookPath?: string;
26
+ /** Direct message access policy (default: pairing). */
27
+ dmPolicy?: DmPolicy;
28
+ allowFrom?: Array<string | number>;
29
+ /** Optional allowlist for group senders. */
30
+ groupAllowFrom?: Array<string | number>;
31
+ /** Group message handling policy. */
32
+ groupPolicy?: GroupPolicy;
33
+ /** Max group messages to keep as history context (0 disables). */
34
+ historyLimit?: number;
35
+ /** Max DM turns to keep as history context. */
36
+ dmHistoryLimit?: number;
37
+ /** Per-DM config overrides keyed by user ID. */
38
+ dms?: Record<string, unknown>;
39
+ /** Outbound text chunk size (chars). Default: 4000. */
40
+ textChunkLimit?: number;
41
+ /** Chunking mode: "newline" (default) splits on every newline; "length" splits by size. */
42
+ chunkMode?: "length" | "newline";
43
+ blockStreaming?: boolean;
44
+ /** Merge streamed block replies before sending. */
45
+ blockStreamingCoalesce?: Record<string, unknown>;
46
+ /** Max outbound media size in MB. */
47
+ mediaMaxMb?: number;
48
+ /** Send read receipts for incoming messages (default: true). */
49
+ sendReadReceipts?: boolean;
50
+ /** Per-group configuration keyed by chat GUID or identifier. */
51
+ groups?: Record<string, BlueBubblesGroupConfig>;
52
+ };
53
+
54
+ export type BlueBubblesActionConfig = {
55
+ reactions?: boolean;
56
+ edit?: boolean;
57
+ unsend?: boolean;
58
+ reply?: boolean;
59
+ sendWithEffect?: boolean;
60
+ renameGroup?: boolean;
61
+ addParticipant?: boolean;
62
+ removeParticipant?: boolean;
63
+ leaveGroup?: boolean;
64
+ sendAttachment?: boolean;
65
+ };
66
+
67
+ export type BlueBubblesConfig = {
68
+ /** Optional per-account BlueBubbles configuration (multi-account). */
69
+ accounts?: Record<string, BlueBubblesAccountConfig>;
70
+ /** Per-action tool gating (default: true for all). */
71
+ actions?: BlueBubblesActionConfig;
72
+ } & BlueBubblesAccountConfig;
73
+
74
+ export type BlueBubblesSendTarget =
75
+ | { kind: "chat_id"; chatId: number }
76
+ | { kind: "chat_guid"; chatGuid: string }
77
+ | { kind: "chat_identifier"; chatIdentifier: string }
78
+ | { kind: "handle"; address: string; service?: "imessage" | "sms" | "auto" };
79
+
80
+ export type BlueBubblesAttachment = {
81
+ guid?: string;
82
+ uti?: string;
83
+ mimeType?: string;
84
+ transferName?: string;
85
+ totalBytes?: number;
86
+ height?: number;
87
+ width?: number;
88
+ originalROWID?: number;
89
+ };
90
+
91
+ const DEFAULT_TIMEOUT_MS = 10_000;
92
+
93
+ export function normalizeBlueBubblesServerUrl(raw: string): string {
94
+ const trimmed = raw.trim();
95
+ if (!trimmed) {
96
+ throw new Error("BlueBubbles serverUrl is required");
97
+ }
98
+ const withScheme = /^https?:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
99
+ return withScheme.replace(/\/+$/, "");
100
+ }
101
+
102
+ export function buildBlueBubblesApiUrl(params: {
103
+ baseUrl: string;
104
+ path: string;
105
+ password?: string;
106
+ }): string {
107
+ const normalized = normalizeBlueBubblesServerUrl(params.baseUrl);
108
+ const url = new URL(params.path, `${normalized}/`);
109
+ if (params.password) {
110
+ url.searchParams.set("password", params.password);
111
+ }
112
+ return url.toString();
113
+ }
114
+
115
+ export async function blueBubblesFetchWithTimeout(
116
+ url: string,
117
+ init: RequestInit,
118
+ timeoutMs = DEFAULT_TIMEOUT_MS,
119
+ ) {
120
+ const controller = new AbortController();
121
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
122
+ try {
123
+ return await fetch(url, { ...init, signal: controller.signal });
124
+ } finally {
125
+ clearTimeout(timer);
126
+ }
127
+ }