@ouro.bot/cli 0.1.0-alpha.1 → 0.1.0-alpha.10
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/AdoptionSpecialist.ouro/psyche/SOUL.md +3 -1
- package/dist/heart/config.js +34 -0
- package/dist/heart/core.js +41 -2
- package/dist/heart/daemon/daemon-cli.js +280 -22
- package/dist/heart/daemon/daemon.js +3 -0
- package/dist/heart/daemon/hatch-animation.js +28 -0
- package/dist/heart/daemon/hatch-flow.js +3 -1
- package/dist/heart/daemon/hatch-specialist.js +6 -1
- package/dist/heart/daemon/log-tailer.js +146 -0
- package/dist/heart/daemon/os-cron.js +260 -0
- package/dist/heart/daemon/ouro-bot-entry.js +0 -0
- package/dist/heart/daemon/ouro-bot-wrapper.js +4 -3
- package/dist/heart/daemon/ouro-entry.js +0 -0
- package/dist/heart/daemon/process-manager.js +18 -1
- package/dist/heart/daemon/runtime-logging.js +9 -5
- package/dist/heart/daemon/specialist-orchestrator.js +161 -0
- package/dist/heart/daemon/specialist-prompt.js +56 -0
- package/dist/heart/daemon/specialist-session.js +150 -0
- package/dist/heart/daemon/specialist-tools.js +132 -0
- package/dist/heart/daemon/task-scheduler.js +4 -1
- package/dist/heart/identity.js +28 -3
- package/dist/heart/providers/anthropic.js +3 -0
- package/dist/heart/streaming.js +3 -0
- package/dist/mind/associative-recall.js +23 -2
- package/dist/mind/context.js +85 -1
- package/dist/mind/friends/channel.js +8 -0
- package/dist/mind/friends/types.js +1 -1
- package/dist/mind/memory.js +62 -0
- package/dist/mind/pending.js +93 -0
- package/dist/mind/prompt-refresh.js +20 -0
- package/dist/mind/prompt.js +101 -0
- package/dist/nerves/coverage/file-completeness.js +14 -4
- package/dist/repertoire/tools-base.js +92 -0
- package/dist/repertoire/tools.js +3 -3
- package/dist/senses/bluebubbles-client.js +279 -0
- package/dist/senses/bluebubbles-entry.js +11 -0
- package/dist/senses/bluebubbles-model.js +253 -0
- package/dist/senses/bluebubbles-mutation-log.js +76 -0
- package/dist/senses/bluebubbles.js +332 -0
- package/dist/senses/cli.js +89 -8
- package/dist/senses/inner-dialog.js +15 -0
- package/dist/senses/session-lock.js +119 -0
- package/dist/senses/teams.js +1 -0
- package/package.json +4 -3
- package/subagents/README.md +3 -1
- package/subagents/work-merger.md +33 -2
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createBlueBubblesClient = createBlueBubblesClient;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const config_1 = require("../heart/config");
|
|
6
|
+
const runtime_1 = require("../nerves/runtime");
|
|
7
|
+
const bluebubbles_model_1 = require("./bluebubbles-model");
|
|
8
|
+
function buildBlueBubblesApiUrl(baseUrl, endpoint, password) {
|
|
9
|
+
const root = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
10
|
+
const url = new URL(endpoint.replace(/^\//, ""), root);
|
|
11
|
+
url.searchParams.set("password", password);
|
|
12
|
+
return url.toString();
|
|
13
|
+
}
|
|
14
|
+
function asRecord(value) {
|
|
15
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
16
|
+
? value
|
|
17
|
+
: null;
|
|
18
|
+
}
|
|
19
|
+
function extractMessageGuid(payload) {
|
|
20
|
+
if (!payload || typeof payload !== "object")
|
|
21
|
+
return undefined;
|
|
22
|
+
const record = payload;
|
|
23
|
+
const data = record.data && typeof record.data === "object" && !Array.isArray(record.data)
|
|
24
|
+
? record.data
|
|
25
|
+
: null;
|
|
26
|
+
const candidates = [
|
|
27
|
+
record.messageGuid,
|
|
28
|
+
record.messageId,
|
|
29
|
+
record.guid,
|
|
30
|
+
data?.messageGuid,
|
|
31
|
+
data?.messageId,
|
|
32
|
+
data?.guid,
|
|
33
|
+
];
|
|
34
|
+
for (const candidate of candidates) {
|
|
35
|
+
if (typeof candidate === "string" && candidate.trim()) {
|
|
36
|
+
return candidate.trim();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
async function parseJsonBody(response) {
|
|
42
|
+
const raw = await response.text();
|
|
43
|
+
if (!raw.trim())
|
|
44
|
+
return null;
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(raw);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function buildRepairUrl(baseUrl, messageGuid, password) {
|
|
53
|
+
const url = buildBlueBubblesApiUrl(baseUrl, `/api/v1/message/${encodeURIComponent(messageGuid)}`, password);
|
|
54
|
+
const parsed = new URL(url);
|
|
55
|
+
parsed.searchParams.set("with", "attachments,payloadData,chats,messageSummaryInfo");
|
|
56
|
+
return parsed.toString();
|
|
57
|
+
}
|
|
58
|
+
function collectPreviewStrings(value, out, depth = 0) {
|
|
59
|
+
if (depth > 4 || out.length >= 4)
|
|
60
|
+
return;
|
|
61
|
+
if (typeof value === "string") {
|
|
62
|
+
const trimmed = value.trim();
|
|
63
|
+
if (trimmed)
|
|
64
|
+
out.push(trimmed);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(value)) {
|
|
68
|
+
for (const entry of value)
|
|
69
|
+
collectPreviewStrings(entry, out, depth + 1);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const record = asRecord(value);
|
|
73
|
+
if (!record)
|
|
74
|
+
return;
|
|
75
|
+
const preferredKeys = ["title", "summary", "subtitle", "previewText", "siteName", "host", "url"];
|
|
76
|
+
for (const key of preferredKeys) {
|
|
77
|
+
if (out.length >= 4)
|
|
78
|
+
break;
|
|
79
|
+
collectPreviewStrings(record[key], out, depth + 1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function extractLinkPreviewText(data) {
|
|
83
|
+
const values = [];
|
|
84
|
+
collectPreviewStrings(data.payloadData, values);
|
|
85
|
+
collectPreviewStrings(data.messageSummaryInfo, values);
|
|
86
|
+
const unique = [...new Set(values.map((value) => value.trim()).filter(Boolean))];
|
|
87
|
+
if (unique.length === 0)
|
|
88
|
+
return undefined;
|
|
89
|
+
return unique.slice(0, 2).join(" — ");
|
|
90
|
+
}
|
|
91
|
+
function applyRepairNotice(event, notice) {
|
|
92
|
+
return {
|
|
93
|
+
...event,
|
|
94
|
+
requiresRepair: false,
|
|
95
|
+
repairNotice: notice,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function hydrateTextForAgent(event, rawData) {
|
|
99
|
+
if (event.kind !== "message") {
|
|
100
|
+
return { ...event, requiresRepair: false };
|
|
101
|
+
}
|
|
102
|
+
if (event.balloonBundleId !== "com.apple.messages.URLBalloonProvider") {
|
|
103
|
+
return { ...event, requiresRepair: false };
|
|
104
|
+
}
|
|
105
|
+
const previewText = extractLinkPreviewText(rawData);
|
|
106
|
+
if (!previewText) {
|
|
107
|
+
return { ...event, requiresRepair: false };
|
|
108
|
+
}
|
|
109
|
+
const base = event.text.trim();
|
|
110
|
+
const textForAgent = base
|
|
111
|
+
? `${base}\n[link preview: ${previewText}]`
|
|
112
|
+
: `[link preview: ${previewText}]`;
|
|
113
|
+
return {
|
|
114
|
+
...event,
|
|
115
|
+
textForAgent,
|
|
116
|
+
requiresRepair: false,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function extractRepairData(payload) {
|
|
120
|
+
const record = asRecord(payload);
|
|
121
|
+
return asRecord(record?.data) ?? record;
|
|
122
|
+
}
|
|
123
|
+
function createBlueBubblesClient(config = (0, config_1.getBlueBubblesConfig)(), channelConfig = (0, config_1.getBlueBubblesChannelConfig)()) {
|
|
124
|
+
return {
|
|
125
|
+
async sendText(params) {
|
|
126
|
+
const trimmedText = params.text.trim();
|
|
127
|
+
if (!trimmedText) {
|
|
128
|
+
throw new Error("BlueBubbles send requires non-empty text.");
|
|
129
|
+
}
|
|
130
|
+
if (!params.chat.chatGuid) {
|
|
131
|
+
throw new Error("BlueBubbles send currently requires chat.chatGuid from the inbound event.");
|
|
132
|
+
}
|
|
133
|
+
const url = buildBlueBubblesApiUrl(config.serverUrl, "/api/v1/message/text", config.password);
|
|
134
|
+
const body = {
|
|
135
|
+
chatGuid: params.chat.chatGuid,
|
|
136
|
+
tempGuid: (0, node_crypto_1.randomUUID)(),
|
|
137
|
+
message: trimmedText,
|
|
138
|
+
};
|
|
139
|
+
if (params.replyToMessageGuid?.trim()) {
|
|
140
|
+
body.method = "private-api";
|
|
141
|
+
body.selectedMessageGuid = params.replyToMessageGuid.trim();
|
|
142
|
+
body.partIndex = 0;
|
|
143
|
+
}
|
|
144
|
+
(0, runtime_1.emitNervesEvent)({
|
|
145
|
+
component: "senses",
|
|
146
|
+
event: "senses.bluebubbles_send_start",
|
|
147
|
+
message: "sending bluebubbles message",
|
|
148
|
+
meta: {
|
|
149
|
+
chatGuid: params.chat.chatGuid,
|
|
150
|
+
hasReplyTarget: Boolean(params.replyToMessageGuid?.trim()),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
const response = await fetch(url, {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: { "Content-Type": "application/json" },
|
|
156
|
+
body: JSON.stringify(body),
|
|
157
|
+
signal: AbortSignal.timeout(channelConfig.requestTimeoutMs),
|
|
158
|
+
});
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
const errorText = await response.text().catch(() => "");
|
|
161
|
+
(0, runtime_1.emitNervesEvent)({
|
|
162
|
+
level: "error",
|
|
163
|
+
component: "senses",
|
|
164
|
+
event: "senses.bluebubbles_send_error",
|
|
165
|
+
message: "bluebubbles send failed",
|
|
166
|
+
meta: {
|
|
167
|
+
status: response.status,
|
|
168
|
+
reason: errorText || "unknown",
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
throw new Error(`BlueBubbles send failed (${response.status}): ${errorText || "unknown"}`);
|
|
172
|
+
}
|
|
173
|
+
const payload = await parseJsonBody(response);
|
|
174
|
+
const messageGuid = extractMessageGuid(payload);
|
|
175
|
+
(0, runtime_1.emitNervesEvent)({
|
|
176
|
+
component: "senses",
|
|
177
|
+
event: "senses.bluebubbles_send_end",
|
|
178
|
+
message: "bluebubbles message sent",
|
|
179
|
+
meta: {
|
|
180
|
+
chatGuid: params.chat.chatGuid,
|
|
181
|
+
messageGuid: messageGuid ?? null,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
return { messageGuid };
|
|
185
|
+
},
|
|
186
|
+
async repairEvent(event) {
|
|
187
|
+
if (!event.requiresRepair) {
|
|
188
|
+
(0, runtime_1.emitNervesEvent)({
|
|
189
|
+
component: "senses",
|
|
190
|
+
event: "senses.bluebubbles_repair_skipped",
|
|
191
|
+
message: "bluebubbles event repair skipped",
|
|
192
|
+
meta: {
|
|
193
|
+
kind: event.kind,
|
|
194
|
+
messageGuid: event.messageGuid,
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
return event;
|
|
198
|
+
}
|
|
199
|
+
(0, runtime_1.emitNervesEvent)({
|
|
200
|
+
component: "senses",
|
|
201
|
+
event: "senses.bluebubbles_repair_start",
|
|
202
|
+
message: "repairing bluebubbles event by guid",
|
|
203
|
+
meta: {
|
|
204
|
+
kind: event.kind,
|
|
205
|
+
messageGuid: event.messageGuid,
|
|
206
|
+
eventType: event.eventType,
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
const url = buildRepairUrl(config.serverUrl, event.messageGuid, config.password);
|
|
210
|
+
try {
|
|
211
|
+
const response = await fetch(url, {
|
|
212
|
+
method: "GET",
|
|
213
|
+
signal: AbortSignal.timeout(channelConfig.requestTimeoutMs),
|
|
214
|
+
});
|
|
215
|
+
if (!response.ok) {
|
|
216
|
+
const errorText = await response.text().catch(() => "");
|
|
217
|
+
const repaired = applyRepairNotice(event, `BlueBubbles repair failed: ${errorText || `HTTP ${response.status}`}`);
|
|
218
|
+
(0, runtime_1.emitNervesEvent)({
|
|
219
|
+
level: "warn",
|
|
220
|
+
component: "senses",
|
|
221
|
+
event: "senses.bluebubbles_repair_error",
|
|
222
|
+
message: "bluebubbles repair request failed",
|
|
223
|
+
meta: {
|
|
224
|
+
messageGuid: event.messageGuid,
|
|
225
|
+
status: response.status,
|
|
226
|
+
reason: errorText || "unknown",
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
return repaired;
|
|
230
|
+
}
|
|
231
|
+
const payload = await parseJsonBody(response);
|
|
232
|
+
const data = extractRepairData(payload);
|
|
233
|
+
if (!data || typeof data.guid !== "string") {
|
|
234
|
+
const repaired = applyRepairNotice(event, "BlueBubbles repair failed: invalid message payload");
|
|
235
|
+
(0, runtime_1.emitNervesEvent)({
|
|
236
|
+
level: "warn",
|
|
237
|
+
component: "senses",
|
|
238
|
+
event: "senses.bluebubbles_repair_error",
|
|
239
|
+
message: "bluebubbles repair returned unusable payload",
|
|
240
|
+
meta: {
|
|
241
|
+
messageGuid: event.messageGuid,
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
return repaired;
|
|
245
|
+
}
|
|
246
|
+
const normalized = (0, bluebubbles_model_1.normalizeBlueBubblesEvent)({
|
|
247
|
+
type: event.eventType,
|
|
248
|
+
data,
|
|
249
|
+
});
|
|
250
|
+
const hydrated = hydrateTextForAgent(normalized, data);
|
|
251
|
+
(0, runtime_1.emitNervesEvent)({
|
|
252
|
+
component: "senses",
|
|
253
|
+
event: "senses.bluebubbles_repair_end",
|
|
254
|
+
message: "bluebubbles event repaired",
|
|
255
|
+
meta: {
|
|
256
|
+
kind: hydrated.kind,
|
|
257
|
+
messageGuid: hydrated.messageGuid,
|
|
258
|
+
repairedFrom: event.kind,
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
return hydrated;
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
265
|
+
(0, runtime_1.emitNervesEvent)({
|
|
266
|
+
level: "warn",
|
|
267
|
+
component: "senses",
|
|
268
|
+
event: "senses.bluebubbles_repair_error",
|
|
269
|
+
message: "bluebubbles repair threw",
|
|
270
|
+
meta: {
|
|
271
|
+
messageGuid: event.messageGuid,
|
|
272
|
+
reason,
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
return applyRepairNotice(event, `BlueBubbles repair failed: ${reason}`);
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Thin entrypoint for `npm run bluebubbles` / `node dist/senses/bluebubbles-entry.js --agent <name>`.
|
|
3
|
+
// Separated from bluebubbles.ts so the BlueBubbles adapter stays testable.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
if (!process.argv.includes("--agent")) {
|
|
6
|
+
// eslint-disable-next-line no-console -- pre-boot guard: --agent check before imports
|
|
7
|
+
console.error("Missing required --agent <name> argument.\nUsage: node dist/senses/bluebubbles-entry.js --agent ouroboros");
|
|
8
|
+
process.exit(1);
|
|
9
|
+
}
|
|
10
|
+
const bluebubbles_1 = require("./bluebubbles");
|
|
11
|
+
(0, bluebubbles_1.startBlueBubblesApp)();
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeBlueBubblesEvent = normalizeBlueBubblesEvent;
|
|
4
|
+
const runtime_1 = require("../nerves/runtime");
|
|
5
|
+
function asRecord(value) {
|
|
6
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
7
|
+
? value
|
|
8
|
+
: null;
|
|
9
|
+
}
|
|
10
|
+
function readString(record, key) {
|
|
11
|
+
if (!record)
|
|
12
|
+
return undefined;
|
|
13
|
+
const value = record[key];
|
|
14
|
+
return typeof value === "string" ? value : undefined;
|
|
15
|
+
}
|
|
16
|
+
function readNumber(record, key) {
|
|
17
|
+
if (!record)
|
|
18
|
+
return undefined;
|
|
19
|
+
const value = record[key];
|
|
20
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
21
|
+
}
|
|
22
|
+
function readBoolean(record, key) {
|
|
23
|
+
const value = record[key];
|
|
24
|
+
return typeof value === "boolean" ? value : undefined;
|
|
25
|
+
}
|
|
26
|
+
function normalizeHandle(raw) {
|
|
27
|
+
const trimmed = raw.trim();
|
|
28
|
+
if (!trimmed)
|
|
29
|
+
return "";
|
|
30
|
+
if (trimmed.includes("@"))
|
|
31
|
+
return trimmed.toLowerCase();
|
|
32
|
+
const compact = trimmed.replace(/[^\d+]/g, "");
|
|
33
|
+
return compact || trimmed;
|
|
34
|
+
}
|
|
35
|
+
function extractChatIdentifierFromGuid(chatGuid) {
|
|
36
|
+
if (!chatGuid)
|
|
37
|
+
return undefined;
|
|
38
|
+
const parts = chatGuid.split(";");
|
|
39
|
+
return parts.length >= 3 ? parts[2]?.trim() || undefined : undefined;
|
|
40
|
+
}
|
|
41
|
+
function buildChatRef(data, threadOriginatorGuid) {
|
|
42
|
+
const chats = Array.isArray(data.chats) ? data.chats : [];
|
|
43
|
+
const chat = asRecord(chats[0]) ?? null;
|
|
44
|
+
const chatGuid = readString(chat, "guid");
|
|
45
|
+
const chatIdentifier = readString(chat, "chatIdentifier") ??
|
|
46
|
+
readString(chat, "identifier") ??
|
|
47
|
+
extractChatIdentifierFromGuid(chatGuid);
|
|
48
|
+
const displayName = readString(chat, "displayName")?.trim() || undefined;
|
|
49
|
+
const style = readNumber(chat, "style");
|
|
50
|
+
const isGroup = style === 43 || (chatGuid?.includes(";+;") ?? false) || Boolean(displayName);
|
|
51
|
+
const baseKey = chatGuid?.trim()
|
|
52
|
+
? `chat:${chatGuid.trim()}`
|
|
53
|
+
: `chat_identifier:${(chatIdentifier ?? "unknown").trim()}`;
|
|
54
|
+
const sessionKey = threadOriginatorGuid?.trim()
|
|
55
|
+
? `${baseKey}:thread:${threadOriginatorGuid.trim()}`
|
|
56
|
+
: baseKey;
|
|
57
|
+
return {
|
|
58
|
+
chatGuid: chatGuid?.trim() || undefined,
|
|
59
|
+
chatIdentifier: chatIdentifier?.trim() || undefined,
|
|
60
|
+
displayName,
|
|
61
|
+
isGroup,
|
|
62
|
+
sessionKey,
|
|
63
|
+
sendTarget: chatGuid?.trim()
|
|
64
|
+
? { kind: "chat_guid", value: chatGuid.trim() }
|
|
65
|
+
: { kind: "chat_identifier", value: (chatIdentifier ?? "unknown").trim() },
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function extractSender(data, chat) {
|
|
69
|
+
const handle = asRecord(data.handle) ?? asRecord(data.sender) ?? null;
|
|
70
|
+
const rawId = readString(handle, "address") ??
|
|
71
|
+
readString(handle, "id") ??
|
|
72
|
+
readString(data, "senderId") ??
|
|
73
|
+
chat.chatIdentifier ??
|
|
74
|
+
chat.chatGuid ??
|
|
75
|
+
"unknown";
|
|
76
|
+
const externalId = normalizeHandle(rawId);
|
|
77
|
+
const displayName = externalId || rawId || "Unknown";
|
|
78
|
+
return {
|
|
79
|
+
provider: "imessage-handle",
|
|
80
|
+
externalId,
|
|
81
|
+
rawId,
|
|
82
|
+
displayName,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function extractAttachments(data) {
|
|
86
|
+
const raw = Array.isArray(data.attachments) ? data.attachments : [];
|
|
87
|
+
return raw
|
|
88
|
+
.map((entry) => asRecord(entry))
|
|
89
|
+
.filter((entry) => entry !== null)
|
|
90
|
+
.map((entry) => ({
|
|
91
|
+
guid: readString(entry, "guid"),
|
|
92
|
+
mimeType: readString(entry, "mimeType"),
|
|
93
|
+
transferName: readString(entry, "transferName"),
|
|
94
|
+
totalBytes: readNumber(entry, "totalBytes"),
|
|
95
|
+
height: readNumber(entry, "height"),
|
|
96
|
+
width: readNumber(entry, "width"),
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
function formatAttachmentText(attachments) {
|
|
100
|
+
if (attachments.length === 0)
|
|
101
|
+
return "";
|
|
102
|
+
const [first] = attachments;
|
|
103
|
+
const mime = first.mimeType ?? "";
|
|
104
|
+
const label = mime.startsWith("image/")
|
|
105
|
+
? "image attachment"
|
|
106
|
+
: mime.startsWith("audio/")
|
|
107
|
+
? "audio attachment"
|
|
108
|
+
: "attachment";
|
|
109
|
+
const name = first.transferName ? `: ${first.transferName}` : "";
|
|
110
|
+
const dimensions = typeof first.width === "number" && typeof first.height === "number" && first.width > 0 && first.height > 0
|
|
111
|
+
? ` (${first.width}x${first.height})`
|
|
112
|
+
: "";
|
|
113
|
+
return `[${label}${name}${dimensions}]`;
|
|
114
|
+
}
|
|
115
|
+
function formatMessageText(data, attachments) {
|
|
116
|
+
const text = readString(data, "text")?.trim() ?? "";
|
|
117
|
+
const balloonBundleId = readString(data, "balloonBundleId")?.trim();
|
|
118
|
+
if (text) {
|
|
119
|
+
if (balloonBundleId === "com.apple.messages.URLBalloonProvider") {
|
|
120
|
+
return `${text}\n[link preview attached]`;
|
|
121
|
+
}
|
|
122
|
+
return text;
|
|
123
|
+
}
|
|
124
|
+
return formatAttachmentText(attachments);
|
|
125
|
+
}
|
|
126
|
+
function normalizeReactionName(value) {
|
|
127
|
+
if (typeof value !== "string")
|
|
128
|
+
return undefined;
|
|
129
|
+
const trimmed = value.trim();
|
|
130
|
+
return trimmed ? trimmed.toLowerCase() : undefined;
|
|
131
|
+
}
|
|
132
|
+
function stripPartPrefix(guid) {
|
|
133
|
+
if (!guid)
|
|
134
|
+
return undefined;
|
|
135
|
+
const trimmed = guid.trim();
|
|
136
|
+
const marker = trimmed.lastIndexOf("/");
|
|
137
|
+
return marker >= 0 ? trimmed.slice(marker + 1) : trimmed;
|
|
138
|
+
}
|
|
139
|
+
function buildMutationText(mutationType, data, reactionName) {
|
|
140
|
+
if (mutationType === "reaction") {
|
|
141
|
+
return `reacted with ${reactionName}`;
|
|
142
|
+
}
|
|
143
|
+
if (mutationType === "edit") {
|
|
144
|
+
const editedText = readString(data, "text")?.trim() ?? "";
|
|
145
|
+
return editedText ? `edited message: ${editedText}` : "edited a message";
|
|
146
|
+
}
|
|
147
|
+
if (mutationType === "unsend") {
|
|
148
|
+
return "unsent a message";
|
|
149
|
+
}
|
|
150
|
+
if (mutationType === "read") {
|
|
151
|
+
return "message marked as read";
|
|
152
|
+
}
|
|
153
|
+
return "message marked as delivered";
|
|
154
|
+
}
|
|
155
|
+
function detectMutationType(eventType, data, reactionName) {
|
|
156
|
+
if (reactionName)
|
|
157
|
+
return "reaction";
|
|
158
|
+
if (eventType === "updated-message") {
|
|
159
|
+
if (readNumber(data, "dateRetracted"))
|
|
160
|
+
return "unsend";
|
|
161
|
+
if (readNumber(data, "dateEdited"))
|
|
162
|
+
return "edit";
|
|
163
|
+
if (readNumber(data, "dateRead"))
|
|
164
|
+
return "read";
|
|
165
|
+
if (readBoolean(data, "isDelivered") || readNumber(data, "dateDelivered"))
|
|
166
|
+
return "delivery";
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
function normalizeBlueBubblesEvent(payload) {
|
|
171
|
+
const envelope = asRecord(payload);
|
|
172
|
+
const eventType = readString(envelope, "type")?.trim() ?? "";
|
|
173
|
+
const data = asRecord(envelope?.data);
|
|
174
|
+
if (!eventType || !data) {
|
|
175
|
+
(0, runtime_1.emitNervesEvent)({
|
|
176
|
+
level: "warn",
|
|
177
|
+
component: "senses",
|
|
178
|
+
event: "senses.bluebubbles_event_ignored",
|
|
179
|
+
message: "ignored invalid bluebubbles payload",
|
|
180
|
+
meta: { hasEnvelope: Boolean(envelope), eventType },
|
|
181
|
+
});
|
|
182
|
+
throw new Error("Invalid BlueBubbles payload");
|
|
183
|
+
}
|
|
184
|
+
const messageGuid = readString(data, "guid")?.trim();
|
|
185
|
+
if (!messageGuid) {
|
|
186
|
+
(0, runtime_1.emitNervesEvent)({
|
|
187
|
+
level: "warn",
|
|
188
|
+
component: "senses",
|
|
189
|
+
event: "senses.bluebubbles_event_ignored",
|
|
190
|
+
message: "ignored bluebubbles payload without guid",
|
|
191
|
+
meta: { eventType },
|
|
192
|
+
});
|
|
193
|
+
throw new Error("BlueBubbles payload is missing data.guid");
|
|
194
|
+
}
|
|
195
|
+
const threadOriginatorGuid = readString(data, "threadOriginatorGuid")?.trim() || undefined;
|
|
196
|
+
const chat = buildChatRef(data, threadOriginatorGuid);
|
|
197
|
+
const sender = extractSender(data, chat);
|
|
198
|
+
const timestamp = readNumber(data, "dateCreated") ?? Date.now();
|
|
199
|
+
const fromMe = readBoolean(data, "isFromMe") ?? false;
|
|
200
|
+
const attachments = extractAttachments(data);
|
|
201
|
+
const reactionName = normalizeReactionName(data.associatedMessageType);
|
|
202
|
+
const mutationType = detectMutationType(eventType, data, reactionName);
|
|
203
|
+
const requiresRepair = (readBoolean(data, "hasPayloadData") ?? false) ||
|
|
204
|
+
attachments.length > 0 ||
|
|
205
|
+
eventType === "updated-message";
|
|
206
|
+
const result = mutationType
|
|
207
|
+
? {
|
|
208
|
+
kind: "mutation",
|
|
209
|
+
eventType,
|
|
210
|
+
mutationType,
|
|
211
|
+
messageGuid,
|
|
212
|
+
targetMessageGuid: mutationType === "reaction"
|
|
213
|
+
? stripPartPrefix(readString(data, "associatedMessageGuid"))
|
|
214
|
+
: undefined,
|
|
215
|
+
timestamp,
|
|
216
|
+
fromMe,
|
|
217
|
+
sender,
|
|
218
|
+
chat,
|
|
219
|
+
shouldNotifyAgent: mutationType === "reaction" || mutationType === "edit" || mutationType === "unsend",
|
|
220
|
+
textForAgent: buildMutationText(mutationType, data, reactionName),
|
|
221
|
+
requiresRepair,
|
|
222
|
+
}
|
|
223
|
+
: {
|
|
224
|
+
kind: "message",
|
|
225
|
+
eventType,
|
|
226
|
+
messageGuid,
|
|
227
|
+
timestamp,
|
|
228
|
+
fromMe,
|
|
229
|
+
sender,
|
|
230
|
+
chat,
|
|
231
|
+
text: readString(data, "text")?.trim() ?? "",
|
|
232
|
+
textForAgent: formatMessageText(data, attachments),
|
|
233
|
+
attachments,
|
|
234
|
+
balloonBundleId: readString(data, "balloonBundleId")?.trim() || undefined,
|
|
235
|
+
hasPayloadData: readBoolean(data, "hasPayloadData") ?? false,
|
|
236
|
+
requiresRepair,
|
|
237
|
+
threadOriginatorGuid,
|
|
238
|
+
replyToGuid: threadOriginatorGuid,
|
|
239
|
+
};
|
|
240
|
+
(0, runtime_1.emitNervesEvent)({
|
|
241
|
+
component: "senses",
|
|
242
|
+
event: "senses.bluebubbles_event_normalized",
|
|
243
|
+
message: "normalized bluebubbles event",
|
|
244
|
+
meta: {
|
|
245
|
+
eventType,
|
|
246
|
+
kind: result.kind,
|
|
247
|
+
mutationType: result.kind === "mutation" ? result.mutationType : null,
|
|
248
|
+
sessionKey: result.chat.sessionKey,
|
|
249
|
+
fromMe,
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getBlueBubblesMutationLogPath = getBlueBubblesMutationLogPath;
|
|
37
|
+
exports.recordBlueBubblesMutation = recordBlueBubblesMutation;
|
|
38
|
+
const fs = __importStar(require("node:fs"));
|
|
39
|
+
const os = __importStar(require("node:os"));
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
41
|
+
const runtime_1 = require("../nerves/runtime");
|
|
42
|
+
function sanitizeKey(key) {
|
|
43
|
+
return key.replace(/[/:]/g, "_");
|
|
44
|
+
}
|
|
45
|
+
function getBlueBubblesMutationLogPath(agentName, sessionKey) {
|
|
46
|
+
return path.join(os.homedir(), ".agentstate", agentName, "senses", "bluebubbles", "mutations", `${sanitizeKey(sessionKey)}.ndjson`);
|
|
47
|
+
}
|
|
48
|
+
function recordBlueBubblesMutation(agentName, event) {
|
|
49
|
+
const filePath = getBlueBubblesMutationLogPath(agentName, event.chat.sessionKey);
|
|
50
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
51
|
+
fs.appendFileSync(filePath, JSON.stringify({
|
|
52
|
+
recordedAt: new Date(event.timestamp).toISOString(),
|
|
53
|
+
eventType: event.eventType,
|
|
54
|
+
mutationType: event.mutationType,
|
|
55
|
+
messageGuid: event.messageGuid,
|
|
56
|
+
targetMessageGuid: event.targetMessageGuid ?? null,
|
|
57
|
+
chatGuid: event.chat.chatGuid ?? null,
|
|
58
|
+
chatIdentifier: event.chat.chatIdentifier ?? null,
|
|
59
|
+
sessionKey: event.chat.sessionKey,
|
|
60
|
+
shouldNotifyAgent: event.shouldNotifyAgent,
|
|
61
|
+
textForAgent: event.textForAgent,
|
|
62
|
+
fromMe: event.fromMe,
|
|
63
|
+
}) + "\n", "utf-8");
|
|
64
|
+
(0, runtime_1.emitNervesEvent)({
|
|
65
|
+
component: "senses",
|
|
66
|
+
event: "senses.bluebubbles_mutation_logged",
|
|
67
|
+
message: "recorded bluebubbles mutation to sidecar log",
|
|
68
|
+
meta: {
|
|
69
|
+
agentName,
|
|
70
|
+
mutationType: event.mutationType,
|
|
71
|
+
messageGuid: event.messageGuid,
|
|
72
|
+
path: filePath,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
return filePath;
|
|
76
|
+
}
|