@ouro.bot/cli 0.1.0-alpha.13 → 0.1.0-alpha.131
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 +2 -2
- package/AdoptionSpecialist.ouro/psyche/identities/monty.md +2 -2
- package/README.md +147 -205
- package/changelog.json +814 -0
- package/dist/heart/active-work.js +622 -0
- package/dist/heart/bridges/manager.js +358 -0
- package/dist/heart/bridges/state-machine.js +135 -0
- package/dist/heart/bridges/store.js +123 -0
- package/dist/heart/commitments.js +105 -0
- package/dist/heart/config.js +66 -21
- package/dist/heart/core.js +518 -100
- package/dist/heart/cross-chat-delivery.js +146 -0
- package/dist/heart/daemon/agent-discovery.js +81 -0
- package/dist/heart/daemon/auth-flow.js +457 -0
- package/dist/heart/daemon/daemon-cli.js +1516 -195
- package/dist/heart/daemon/daemon-entry.js +43 -2
- package/dist/heart/daemon/daemon-runtime-sync.js +212 -0
- package/dist/heart/daemon/daemon.js +261 -1
- package/dist/heart/daemon/hatch-animation.js +10 -3
- package/dist/heart/daemon/hatch-flow.js +7 -72
- package/dist/heart/daemon/hooks/bundle-meta.js +92 -0
- package/dist/heart/daemon/launchd.js +159 -0
- package/dist/heart/daemon/log-tailer.js +4 -3
- package/dist/heart/daemon/message-router.js +17 -8
- package/dist/heart/daemon/ouro-bot-global-installer.js +128 -0
- package/dist/heart/daemon/ouro-path-installer.js +57 -29
- package/dist/heart/daemon/ouro-version-manager.js +171 -0
- package/dist/heart/daemon/process-manager.js +13 -0
- package/dist/heart/daemon/run-hooks.js +37 -0
- package/dist/heart/daemon/runtime-logging.js +58 -15
- package/dist/heart/daemon/runtime-metadata.js +219 -0
- package/dist/heart/daemon/runtime-mode.js +67 -0
- package/dist/heart/daemon/sense-manager.js +50 -2
- package/dist/heart/daemon/skill-management-installer.js +94 -0
- package/dist/heart/daemon/socket-client.js +202 -0
- package/dist/heart/daemon/specialist-orchestrator.js +2 -2
- package/dist/heart/daemon/specialist-prompt.js +7 -4
- package/dist/heart/daemon/specialist-tools.js +52 -3
- package/dist/heart/daemon/staged-restart.js +114 -0
- package/dist/heart/daemon/thoughts.js +507 -0
- package/dist/heart/daemon/update-checker.js +111 -0
- package/dist/heart/daemon/update-hooks.js +138 -0
- package/dist/heart/daemon/wrapper-publish-guard.js +86 -0
- package/dist/heart/delegation.js +62 -0
- package/dist/heart/identity.js +64 -21
- package/dist/heart/kicks.js +1 -19
- package/dist/heart/model-capabilities.js +48 -0
- package/dist/heart/obligations.js +197 -0
- package/dist/heart/progress-story.js +42 -0
- package/dist/heart/provider-failover.js +88 -0
- package/dist/heart/provider-ping.js +159 -0
- package/dist/heart/providers/anthropic-token.js +163 -0
- package/dist/heart/providers/anthropic.js +195 -34
- package/dist/heart/providers/azure.js +115 -9
- package/dist/heart/providers/github-copilot.js +157 -0
- package/dist/heart/providers/minimax.js +33 -3
- package/dist/heart/providers/openai-codex.js +49 -14
- package/dist/heart/safe-workspace.js +381 -0
- package/dist/heart/session-activity.js +173 -0
- package/dist/heart/session-recall.js +216 -0
- package/dist/heart/streaming.js +108 -24
- package/dist/heart/target-resolution.js +123 -0
- package/dist/heart/tool-loop.js +194 -0
- package/dist/heart/turn-coordinator.js +28 -0
- package/dist/mind/associative-recall.js +14 -2
- package/dist/mind/bundle-manifest.js +12 -0
- package/dist/mind/context.js +60 -14
- package/dist/mind/first-impressions.js +16 -2
- package/dist/mind/friends/channel.js +35 -0
- package/dist/mind/friends/group-context.js +144 -0
- package/dist/mind/friends/store-file.js +19 -0
- package/dist/mind/friends/trust-explanation.js +74 -0
- package/dist/mind/friends/types.js +8 -0
- package/dist/mind/memory.js +27 -26
- package/dist/mind/obligation-steering.js +221 -0
- package/dist/mind/pending.js +76 -9
- package/dist/mind/phrases.js +1 -0
- package/dist/mind/prompt.js +456 -77
- package/dist/mind/token-estimate.js +8 -12
- package/dist/nerves/cli-logging.js +15 -2
- package/dist/nerves/coverage/run-artifacts.js +1 -1
- package/dist/nerves/index.js +12 -0
- package/dist/nerves/runtime.js +5 -1
- package/dist/repertoire/ado-client.js +4 -2
- package/dist/repertoire/coding/context-pack.js +254 -0
- package/dist/repertoire/coding/feedback.js +301 -0
- package/dist/repertoire/coding/index.js +4 -1
- package/dist/repertoire/coding/manager.js +210 -4
- package/dist/repertoire/coding/spawner.js +39 -9
- package/dist/repertoire/coding/tools.js +171 -4
- package/dist/repertoire/data/ado-endpoints.json +188 -0
- package/dist/repertoire/guardrails.js +290 -0
- package/dist/repertoire/mcp-client.js +254 -0
- package/dist/repertoire/mcp-manager.js +198 -0
- package/dist/repertoire/skills.js +3 -26
- package/dist/repertoire/tasks/board.js +12 -0
- package/dist/repertoire/tasks/index.js +23 -9
- package/dist/repertoire/tasks/transitions.js +1 -2
- package/dist/repertoire/tools-base.js +925 -250
- package/dist/repertoire/tools-bluebubbles.js +93 -0
- package/dist/repertoire/tools-teams.js +58 -25
- package/dist/repertoire/tools.js +106 -53
- package/dist/senses/bluebubbles-client.js +210 -5
- package/dist/senses/bluebubbles-entry.js +2 -0
- package/dist/senses/bluebubbles-inbound-log.js +109 -0
- package/dist/senses/bluebubbles-media.js +339 -0
- package/dist/senses/bluebubbles-model.js +12 -4
- package/dist/senses/bluebubbles-mutation-log.js +45 -5
- package/dist/senses/bluebubbles-runtime-state.js +109 -0
- package/dist/senses/bluebubbles-session-cleanup.js +72 -0
- package/dist/senses/bluebubbles.js +915 -45
- package/dist/senses/cli-layout.js +187 -0
- package/dist/senses/cli.js +374 -131
- package/dist/senses/continuity.js +94 -0
- package/dist/senses/debug-activity.js +154 -0
- package/dist/senses/inner-dialog-worker.js +47 -18
- package/dist/senses/inner-dialog.js +388 -83
- package/dist/senses/pipeline.js +444 -0
- package/dist/senses/teams.js +607 -129
- package/dist/senses/trust-gate.js +112 -2
- package/package.json +9 -3
- package/subagents/README.md +4 -70
- package/dist/heart/daemon/subagent-installer.js +0 -134
- package/subagents/work-doer.md +0 -233
- package/subagents/work-merger.md +0 -624
- package/subagents/work-planner.md +0 -373
|
@@ -34,22 +34,40 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.handleBlueBubblesEvent = handleBlueBubblesEvent;
|
|
37
|
+
exports.recoverMissedBlueBubblesMessages = recoverMissedBlueBubblesMessages;
|
|
37
38
|
exports.createBlueBubblesWebhookHandler = createBlueBubblesWebhookHandler;
|
|
39
|
+
exports.sendProactiveBlueBubblesMessageToSession = sendProactiveBlueBubblesMessageToSession;
|
|
40
|
+
exports.drainAndSendPendingBlueBubbles = drainAndSendPendingBlueBubbles;
|
|
38
41
|
exports.startBlueBubblesApp = startBlueBubblesApp;
|
|
42
|
+
const fs = __importStar(require("node:fs"));
|
|
39
43
|
const http = __importStar(require("node:http"));
|
|
40
44
|
const path = __importStar(require("node:path"));
|
|
41
45
|
const core_1 = require("../heart/core");
|
|
42
46
|
const config_1 = require("../heart/config");
|
|
43
47
|
const identity_1 = require("../heart/identity");
|
|
48
|
+
const turn_coordinator_1 = require("../heart/turn-coordinator");
|
|
44
49
|
const context_1 = require("../mind/context");
|
|
45
50
|
const tokens_1 = require("../mind/friends/tokens");
|
|
51
|
+
const group_context_1 = require("../mind/friends/group-context");
|
|
46
52
|
const resolver_1 = require("../mind/friends/resolver");
|
|
47
53
|
const store_file_1 = require("../mind/friends/store-file");
|
|
54
|
+
const types_1 = require("../mind/friends/types");
|
|
55
|
+
const channel_1 = require("../mind/friends/channel");
|
|
56
|
+
const pending_1 = require("../mind/pending");
|
|
48
57
|
const prompt_1 = require("../mind/prompt");
|
|
58
|
+
const mcp_manager_1 = require("../repertoire/mcp-manager");
|
|
59
|
+
const phrases_1 = require("../mind/phrases");
|
|
49
60
|
const runtime_1 = require("../nerves/runtime");
|
|
50
61
|
const bluebubbles_model_1 = require("./bluebubbles-model");
|
|
51
62
|
const bluebubbles_client_1 = require("./bluebubbles-client");
|
|
63
|
+
const bluebubbles_inbound_log_1 = require("./bluebubbles-inbound-log");
|
|
52
64
|
const bluebubbles_mutation_log_1 = require("./bluebubbles-mutation-log");
|
|
65
|
+
const bluebubbles_runtime_state_1 = require("./bluebubbles-runtime-state");
|
|
66
|
+
const bluebubbles_session_cleanup_1 = require("./bluebubbles-session-cleanup");
|
|
67
|
+
const debug_activity_1 = require("./debug-activity");
|
|
68
|
+
const trust_gate_1 = require("./trust-gate");
|
|
69
|
+
const pipeline_1 = require("./pipeline");
|
|
70
|
+
const bbFailoverStates = new Map();
|
|
53
71
|
const defaultDeps = {
|
|
54
72
|
getAgentName: identity_1.getAgentName,
|
|
55
73
|
buildSystem: prompt_1.buildSystem,
|
|
@@ -64,6 +82,7 @@ const defaultDeps = {
|
|
|
64
82
|
createFriendResolver: (store, params) => new resolver_1.FriendResolver(store, params),
|
|
65
83
|
createServer: http.createServer,
|
|
66
84
|
};
|
|
85
|
+
const BLUEBUBBLES_RUNTIME_SYNC_INTERVAL_MS = 30_000;
|
|
67
86
|
function resolveFriendParams(event) {
|
|
68
87
|
if (event.chat.isGroup) {
|
|
69
88
|
const groupKey = event.chat.chatGuid ?? event.chat.chatIdentifier ?? event.sender.externalId;
|
|
@@ -81,21 +100,309 @@ function resolveFriendParams(event) {
|
|
|
81
100
|
channel: "bluebubbles",
|
|
82
101
|
};
|
|
83
102
|
}
|
|
84
|
-
function
|
|
103
|
+
function resolveGroupExternalId(event) {
|
|
104
|
+
const groupKey = event.chat.chatGuid ?? event.chat.chatIdentifier ?? event.sender.externalId;
|
|
105
|
+
return `group:${groupKey}`;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Check if any participant in a group chat is a known family member.
|
|
109
|
+
* Looks up each participant handle in the friend store.
|
|
110
|
+
*/
|
|
111
|
+
async function checkGroupHasFamilyMember(store, event) {
|
|
112
|
+
if (!event.chat.isGroup)
|
|
113
|
+
return false;
|
|
114
|
+
for (const handle of event.chat.participantHandles ?? []) {
|
|
115
|
+
const friend = await store.findByExternalId("imessage-handle", handle);
|
|
116
|
+
if (friend?.trustLevel === "family")
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if an acquaintance shares any group chat with a family member.
|
|
123
|
+
* Compares group-prefixed externalIds between the acquaintance and all family members.
|
|
124
|
+
*/
|
|
125
|
+
async function checkHasExistingGroupWithFamily(store, senderFriend) {
|
|
126
|
+
const trustLevel = senderFriend.trustLevel ?? "friend";
|
|
127
|
+
if (trustLevel !== "acquaintance")
|
|
128
|
+
return false;
|
|
129
|
+
const acquaintanceGroups = new Set((senderFriend.externalIds ?? [])
|
|
130
|
+
.filter((eid) => eid.externalId.startsWith("group:"))
|
|
131
|
+
.map((eid) => eid.externalId));
|
|
132
|
+
if (acquaintanceGroups.size === 0)
|
|
133
|
+
return false;
|
|
134
|
+
const allFriends = await (store.listAll?.() ?? Promise.resolve([]));
|
|
135
|
+
for (const friend of allFriends) {
|
|
136
|
+
if (friend.trustLevel !== "family")
|
|
137
|
+
continue;
|
|
138
|
+
const friendGroups = (friend.externalIds ?? [])
|
|
139
|
+
.filter((eid) => eid.externalId.startsWith("group:"))
|
|
140
|
+
.map((eid) => eid.externalId);
|
|
141
|
+
for (const group of friendGroups) {
|
|
142
|
+
if (acquaintanceGroups.has(group))
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
function extractMessageText(content) {
|
|
149
|
+
if (typeof content === "string")
|
|
150
|
+
return content;
|
|
151
|
+
if (!Array.isArray(content))
|
|
152
|
+
return "";
|
|
153
|
+
return content
|
|
154
|
+
.map((part) => {
|
|
155
|
+
if (part && typeof part === "object" && "type" in part && part.type === "text" && typeof part.text === "string") {
|
|
156
|
+
return part.text;
|
|
157
|
+
}
|
|
158
|
+
return "";
|
|
159
|
+
})
|
|
160
|
+
.filter(Boolean)
|
|
161
|
+
.join("\n");
|
|
162
|
+
}
|
|
163
|
+
function isHistoricalLaneMetadataLine(line) {
|
|
164
|
+
return /^\[(conversation scope|recent active lanes|routing control):?/i.test(line)
|
|
165
|
+
|| /^- (top_level|thread:[^:]+):/i.test(line);
|
|
166
|
+
}
|
|
167
|
+
function extractHistoricalLaneSummary(messages) {
|
|
168
|
+
const seen = new Set();
|
|
169
|
+
const summaries = [];
|
|
170
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
171
|
+
const message = messages[index];
|
|
172
|
+
if (message.role !== "user")
|
|
173
|
+
continue;
|
|
174
|
+
const text = extractMessageText(message.content);
|
|
175
|
+
if (!text)
|
|
176
|
+
continue;
|
|
177
|
+
const firstLine = text.split("\n")[0].trim();
|
|
178
|
+
const threadMatch = firstLine.match(/thread id: ([^\]|]+)/i);
|
|
179
|
+
const laneKey = threadMatch
|
|
180
|
+
? `thread:${threadMatch[1].trim()}`
|
|
181
|
+
: /top[-_]level/i.test(firstLine)
|
|
182
|
+
? "top_level"
|
|
183
|
+
: null;
|
|
184
|
+
if (!laneKey || seen.has(laneKey))
|
|
185
|
+
continue;
|
|
186
|
+
seen.add(laneKey);
|
|
187
|
+
const snippet = text
|
|
188
|
+
.split("\n")
|
|
189
|
+
.slice(1)
|
|
190
|
+
.map((line) => line.trim())
|
|
191
|
+
.find((line) => line.length > 0 && !isHistoricalLaneMetadataLine(line))
|
|
192
|
+
?.slice(0, 80) ?? "(no recent text)";
|
|
193
|
+
summaries.push({
|
|
194
|
+
key: laneKey,
|
|
195
|
+
label: laneKey === "top_level" ? "top_level" : laneKey,
|
|
196
|
+
snippet,
|
|
197
|
+
});
|
|
198
|
+
if (summaries.length >= 5)
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
return summaries;
|
|
202
|
+
}
|
|
203
|
+
function buildConversationScopePrefix(event, existingMessages) {
|
|
204
|
+
if (event.kind !== "message") {
|
|
205
|
+
return "";
|
|
206
|
+
}
|
|
207
|
+
const summaries = extractHistoricalLaneSummary(existingMessages);
|
|
208
|
+
const lines = [];
|
|
209
|
+
if (event.threadOriginatorGuid?.trim()) {
|
|
210
|
+
lines.push(`[conversation scope: existing chat trunk | current inbound lane: thread | current thread id: ${event.threadOriginatorGuid.trim()} | default outbound target for this turn: current_lane]`);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
lines.push("[conversation scope: existing chat trunk | current inbound lane: top_level | default outbound target for this turn: top_level]");
|
|
214
|
+
}
|
|
215
|
+
if (summaries.length > 0) {
|
|
216
|
+
lines.push("[recent active lanes]");
|
|
217
|
+
for (const summary of summaries) {
|
|
218
|
+
lines.push(`- ${summary.label}: ${summary.snippet}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (event.threadOriginatorGuid?.trim() || summaries.some((summary) => summary.key.startsWith("thread:"))) {
|
|
222
|
+
lines.push("[routing control: use bluebubbles_set_reply_target with target=top_level to widen back out, or target=thread plus a listed thread id to route into a specific active thread]");
|
|
223
|
+
}
|
|
224
|
+
return lines.join("\n");
|
|
225
|
+
}
|
|
226
|
+
function buildInboundText(event, existingMessages) {
|
|
227
|
+
const metadataPrefix = buildConversationScopePrefix(event, existingMessages);
|
|
85
228
|
const baseText = event.repairNotice?.trim()
|
|
86
229
|
? `${event.textForAgent}\n[${event.repairNotice.trim()}]`
|
|
87
230
|
: event.textForAgent;
|
|
88
|
-
if (!event.chat.isGroup)
|
|
89
|
-
return baseText;
|
|
231
|
+
if (!event.chat.isGroup) {
|
|
232
|
+
return metadataPrefix ? `${metadataPrefix}\n${baseText}` : baseText;
|
|
233
|
+
}
|
|
234
|
+
const scopedText = metadataPrefix ? `${metadataPrefix}\n${baseText}` : baseText;
|
|
90
235
|
if (event.kind === "mutation") {
|
|
91
|
-
return `${event.sender.displayName} ${
|
|
236
|
+
return `${event.sender.displayName} ${scopedText}`;
|
|
237
|
+
}
|
|
238
|
+
return `${event.sender.displayName}: ${scopedText}`;
|
|
239
|
+
}
|
|
240
|
+
function buildInboundContent(event, existingMessages) {
|
|
241
|
+
const text = buildInboundText(event, existingMessages);
|
|
242
|
+
if (event.kind !== "message" || !event.inputPartsForAgent || event.inputPartsForAgent.length === 0) {
|
|
243
|
+
return text;
|
|
92
244
|
}
|
|
93
|
-
return
|
|
245
|
+
return [
|
|
246
|
+
{ type: "text", text },
|
|
247
|
+
...event.inputPartsForAgent,
|
|
248
|
+
];
|
|
249
|
+
}
|
|
250
|
+
function sessionLikelyContainsMessage(event, existingMessages) {
|
|
251
|
+
const fragment = event.textForAgent.trim();
|
|
252
|
+
if (!fragment)
|
|
253
|
+
return false;
|
|
254
|
+
return existingMessages.some((message) => {
|
|
255
|
+
if (message.role !== "user")
|
|
256
|
+
return false;
|
|
257
|
+
return extractMessageText(message.content).includes(fragment);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
function mutationEntryToEvent(entry) {
|
|
261
|
+
return {
|
|
262
|
+
kind: "mutation",
|
|
263
|
+
eventType: entry.eventType,
|
|
264
|
+
mutationType: entry.mutationType,
|
|
265
|
+
messageGuid: entry.messageGuid,
|
|
266
|
+
targetMessageGuid: entry.targetMessageGuid ?? undefined,
|
|
267
|
+
timestamp: Date.parse(entry.recordedAt) || Date.now(),
|
|
268
|
+
fromMe: entry.fromMe,
|
|
269
|
+
sender: {
|
|
270
|
+
provider: "imessage-handle",
|
|
271
|
+
externalId: entry.chatIdentifier ?? entry.chatGuid ?? "unknown",
|
|
272
|
+
rawId: entry.chatIdentifier ?? entry.chatGuid ?? "unknown",
|
|
273
|
+
displayName: entry.chatIdentifier ?? entry.chatGuid ?? "Unknown",
|
|
274
|
+
},
|
|
275
|
+
chat: {
|
|
276
|
+
chatGuid: entry.chatGuid ?? undefined,
|
|
277
|
+
chatIdentifier: entry.chatIdentifier ?? undefined,
|
|
278
|
+
displayName: undefined,
|
|
279
|
+
isGroup: Boolean(entry.chatGuid?.includes(";+;")),
|
|
280
|
+
sessionKey: entry.sessionKey,
|
|
281
|
+
sendTarget: entry.chatGuid
|
|
282
|
+
? { kind: "chat_guid", value: entry.chatGuid }
|
|
283
|
+
: { kind: "chat_identifier", value: entry.chatIdentifier ?? "unknown" },
|
|
284
|
+
participantHandles: [],
|
|
285
|
+
},
|
|
286
|
+
shouldNotifyAgent: entry.shouldNotifyAgent,
|
|
287
|
+
textForAgent: entry.textForAgent,
|
|
288
|
+
requiresRepair: true,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function getBlueBubblesContinuityIngressTexts(event) {
|
|
292
|
+
if (event.kind !== "message")
|
|
293
|
+
return [];
|
|
294
|
+
const text = event.textForAgent.trim();
|
|
295
|
+
if (text.length > 0)
|
|
296
|
+
return [text];
|
|
297
|
+
const fallbackText = (event.inputPartsForAgent ?? [])
|
|
298
|
+
.map((part) => {
|
|
299
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
300
|
+
return part.text.trim();
|
|
301
|
+
}
|
|
302
|
+
return "";
|
|
303
|
+
})
|
|
304
|
+
.filter(Boolean)
|
|
305
|
+
.join("\n");
|
|
306
|
+
return fallbackText ? [fallbackText] : [];
|
|
307
|
+
}
|
|
308
|
+
function createReplyTargetController(event) {
|
|
309
|
+
const defaultTargetLabel = event.kind === "message" && event.threadOriginatorGuid?.trim() ? "current_lane" : "top_level";
|
|
310
|
+
let selection = event.kind === "message" && event.threadOriginatorGuid?.trim()
|
|
311
|
+
? { target: "current_lane" }
|
|
312
|
+
: { target: "top_level" };
|
|
313
|
+
return {
|
|
314
|
+
getReplyToMessageGuid() {
|
|
315
|
+
if (event.kind !== "message")
|
|
316
|
+
return undefined;
|
|
317
|
+
if (selection.target === "top_level")
|
|
318
|
+
return undefined;
|
|
319
|
+
if (selection.target === "thread")
|
|
320
|
+
return selection.threadOriginatorGuid.trim();
|
|
321
|
+
return event.threadOriginatorGuid?.trim() ? event.messageGuid : undefined;
|
|
322
|
+
},
|
|
323
|
+
setSelection(next) {
|
|
324
|
+
selection = next;
|
|
325
|
+
if (next.target === "top_level") {
|
|
326
|
+
return "bluebubbles reply target override: top_level";
|
|
327
|
+
}
|
|
328
|
+
if (next.target === "thread") {
|
|
329
|
+
return `bluebubbles reply target override: thread:${next.threadOriginatorGuid}`;
|
|
330
|
+
}
|
|
331
|
+
return `bluebubbles reply target: using default for this turn (${defaultTargetLabel})`;
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
function emitBlueBubblesMarkReadWarning(chat, error) {
|
|
336
|
+
(0, runtime_1.emitNervesEvent)({
|
|
337
|
+
level: "warn",
|
|
338
|
+
component: "senses",
|
|
339
|
+
event: "senses.bluebubbles_mark_read_error",
|
|
340
|
+
message: "failed to mark bluebubbles chat as read",
|
|
341
|
+
meta: {
|
|
342
|
+
chatGuid: chat.chatGuid ?? null,
|
|
343
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
344
|
+
},
|
|
345
|
+
});
|
|
94
346
|
}
|
|
95
|
-
function createBlueBubblesCallbacks(client, chat,
|
|
347
|
+
function createBlueBubblesCallbacks(client, chat, replyTarget, isGroupChat) {
|
|
96
348
|
let textBuffer = "";
|
|
349
|
+
const phrases = (0, phrases_1.getPhrases)();
|
|
350
|
+
const activity = (0, debug_activity_1.createDebugActivityController)({
|
|
351
|
+
thinkingPhrases: phrases.thinking,
|
|
352
|
+
followupPhrases: phrases.followup,
|
|
353
|
+
startTypingOnModelStart: !isGroupChat,
|
|
354
|
+
startTypingOnFirstTextChunk: isGroupChat,
|
|
355
|
+
suppressInitialModelStatus: true,
|
|
356
|
+
suppressFollowupPhraseStatus: true,
|
|
357
|
+
transport: {
|
|
358
|
+
sendStatus: async (text) => {
|
|
359
|
+
const sent = await client.sendText({
|
|
360
|
+
chat,
|
|
361
|
+
text,
|
|
362
|
+
replyToMessageGuid: replyTarget.getReplyToMessageGuid(),
|
|
363
|
+
});
|
|
364
|
+
return sent.messageGuid;
|
|
365
|
+
},
|
|
366
|
+
editStatus: async (_messageGuid, text) => {
|
|
367
|
+
await client.sendText({
|
|
368
|
+
chat,
|
|
369
|
+
text,
|
|
370
|
+
replyToMessageGuid: replyTarget.getReplyToMessageGuid(),
|
|
371
|
+
});
|
|
372
|
+
},
|
|
373
|
+
setTyping: async (active) => {
|
|
374
|
+
if (!active) {
|
|
375
|
+
await client.setTyping(chat, false);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const [markReadResult, typingResult] = await Promise.allSettled([
|
|
379
|
+
client.markChatRead(chat),
|
|
380
|
+
client.setTyping(chat, true),
|
|
381
|
+
]);
|
|
382
|
+
if (markReadResult.status === "rejected") {
|
|
383
|
+
emitBlueBubblesMarkReadWarning(chat, markReadResult.reason);
|
|
384
|
+
}
|
|
385
|
+
if (typingResult.status === "rejected") {
|
|
386
|
+
throw typingResult.reason;
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
onTransportError: (operation, error) => {
|
|
391
|
+
(0, runtime_1.emitNervesEvent)({
|
|
392
|
+
level: "warn",
|
|
393
|
+
component: "senses",
|
|
394
|
+
event: "senses.bluebubbles_activity_error",
|
|
395
|
+
message: "bluebubbles activity transport failed",
|
|
396
|
+
meta: {
|
|
397
|
+
operation,
|
|
398
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
},
|
|
402
|
+
});
|
|
97
403
|
return {
|
|
98
404
|
onModelStart() {
|
|
405
|
+
activity.onModelStart();
|
|
99
406
|
(0, runtime_1.emitNervesEvent)({
|
|
100
407
|
component: "senses",
|
|
101
408
|
event: "senses.bluebubbles_turn_start",
|
|
@@ -112,10 +419,12 @@ function createBlueBubblesCallbacks(client, chat, replyToMessageGuid) {
|
|
|
112
419
|
});
|
|
113
420
|
},
|
|
114
421
|
onTextChunk(text) {
|
|
422
|
+
activity.onTextChunk(text);
|
|
115
423
|
textBuffer += text;
|
|
116
424
|
},
|
|
117
425
|
onReasoningChunk(_text) { },
|
|
118
426
|
onToolStart(name, _args) {
|
|
427
|
+
activity.onToolStart(name, _args);
|
|
119
428
|
(0, runtime_1.emitNervesEvent)({
|
|
120
429
|
component: "senses",
|
|
121
430
|
event: "senses.bluebubbles_tool_start",
|
|
@@ -124,6 +433,7 @@ function createBlueBubblesCallbacks(client, chat, replyToMessageGuid) {
|
|
|
124
433
|
});
|
|
125
434
|
},
|
|
126
435
|
onToolEnd(name, summary, success) {
|
|
436
|
+
activity.onToolEnd(name, summary, success);
|
|
127
437
|
(0, runtime_1.emitNervesEvent)({
|
|
128
438
|
component: "senses",
|
|
129
439
|
event: "senses.bluebubbles_tool_end",
|
|
@@ -132,6 +442,7 @@ function createBlueBubblesCallbacks(client, chat, replyToMessageGuid) {
|
|
|
132
442
|
});
|
|
133
443
|
},
|
|
134
444
|
onError(error, severity) {
|
|
445
|
+
activity.onError(error);
|
|
135
446
|
(0, runtime_1.emitNervesEvent)({
|
|
136
447
|
level: severity === "terminal" ? "error" : "warn",
|
|
137
448
|
component: "senses",
|
|
@@ -144,17 +455,23 @@ function createBlueBubblesCallbacks(client, chat, replyToMessageGuid) {
|
|
|
144
455
|
textBuffer = "";
|
|
145
456
|
},
|
|
146
457
|
async flush() {
|
|
458
|
+
await activity.drain();
|
|
147
459
|
const trimmed = textBuffer.trim();
|
|
148
460
|
if (!trimmed) {
|
|
461
|
+
await activity.finish();
|
|
149
462
|
return;
|
|
150
463
|
}
|
|
151
464
|
textBuffer = "";
|
|
465
|
+
await activity.finish();
|
|
152
466
|
await client.sendText({
|
|
153
467
|
chat,
|
|
154
468
|
text: trimmed,
|
|
155
|
-
replyToMessageGuid,
|
|
469
|
+
replyToMessageGuid: replyTarget.getReplyToMessageGuid(),
|
|
156
470
|
});
|
|
157
471
|
},
|
|
472
|
+
async finish() {
|
|
473
|
+
await activity.finish();
|
|
474
|
+
},
|
|
158
475
|
};
|
|
159
476
|
}
|
|
160
477
|
async function readRequestBody(req) {
|
|
@@ -173,10 +490,8 @@ function isWebhookPasswordValid(url, expectedPassword) {
|
|
|
173
490
|
const provided = url.searchParams.get("password");
|
|
174
491
|
return !provided || provided === expectedPassword;
|
|
175
492
|
}
|
|
176
|
-
async function
|
|
177
|
-
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
493
|
+
async function handleBlueBubblesNormalizedEvent(event, resolvedDeps, source) {
|
|
178
494
|
const client = resolvedDeps.createClient();
|
|
179
|
-
const event = await client.repairEvent((0, bluebubbles_model_1.normalizeBlueBubblesEvent)(payload));
|
|
180
495
|
if (event.fromMe) {
|
|
181
496
|
(0, runtime_1.emitNervesEvent)({
|
|
182
497
|
component: "senses",
|
|
@@ -219,46 +534,290 @@ async function handleBlueBubblesEvent(payload, deps = {}) {
|
|
|
219
534
|
});
|
|
220
535
|
return { handled: true, notifiedAgent: false, kind: event.kind, reason: "mutation_state_only" };
|
|
221
536
|
}
|
|
537
|
+
// ── Adapter setup: friend, session, content, callbacks ──────────
|
|
222
538
|
const store = resolvedDeps.createFriendStore();
|
|
223
539
|
const resolver = resolvedDeps.createFriendResolver(store, resolveFriendParams(event));
|
|
224
|
-
const
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
friendStore: store,
|
|
228
|
-
summarize: (0, core_1.createSummarize)(),
|
|
229
|
-
context,
|
|
230
|
-
};
|
|
540
|
+
const baseContext = await resolver.resolve();
|
|
541
|
+
const context = { ...baseContext, isGroupChat: event.chat.isGroup };
|
|
542
|
+
const replyTarget = createReplyTargetController(event);
|
|
231
543
|
const friendId = context.friend.id;
|
|
232
544
|
const sessPath = resolvedDeps.sessionPath(friendId, "bluebubbles", event.chat.sessionKey);
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
545
|
+
try {
|
|
546
|
+
(0, bluebubbles_session_cleanup_1.findObsoleteBlueBubblesThreadSessions)(sessPath);
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
(0, runtime_1.emitNervesEvent)({
|
|
550
|
+
level: "warn",
|
|
551
|
+
component: "senses",
|
|
552
|
+
event: "senses.bluebubbles_thread_lane_cleanup_error",
|
|
553
|
+
message: "failed to inspect obsolete bluebubbles thread-lane sessions",
|
|
554
|
+
meta: {
|
|
555
|
+
sessionPath: sessPath,
|
|
556
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
557
|
+
},
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
return (0, turn_coordinator_1.withSharedTurnLock)("bluebubbles", sessPath, async () => {
|
|
561
|
+
// Pre-load session inside the turn lock so same-chat deliveries cannot race on stale trunk state.
|
|
562
|
+
const existing = resolvedDeps.loadSession(sessPath);
|
|
563
|
+
const mcpManager = await (0, mcp_manager_1.getSharedMcpManager)() ?? undefined;
|
|
564
|
+
const sessionMessages = existing?.messages && existing.messages.length > 0
|
|
565
|
+
? existing.messages
|
|
566
|
+
: [{ role: "system", content: await resolvedDeps.buildSystem("bluebubbles", { mcpManager }, context) }];
|
|
567
|
+
if (event.kind === "message") {
|
|
568
|
+
const agentName = resolvedDeps.getAgentName();
|
|
569
|
+
if ((0, bluebubbles_inbound_log_1.hasRecordedBlueBubblesInbound)(agentName, event.chat.sessionKey, event.messageGuid)) {
|
|
570
|
+
(0, runtime_1.emitNervesEvent)({
|
|
571
|
+
component: "senses",
|
|
572
|
+
event: "senses.bluebubbles_recovery_skip",
|
|
573
|
+
message: "skipped bluebubbles message already recorded as handled",
|
|
574
|
+
meta: {
|
|
575
|
+
messageGuid: event.messageGuid,
|
|
576
|
+
sessionKey: event.chat.sessionKey,
|
|
577
|
+
source,
|
|
578
|
+
},
|
|
579
|
+
});
|
|
580
|
+
return { handled: true, notifiedAgent: false, kind: event.kind, reason: "already_processed" };
|
|
581
|
+
}
|
|
582
|
+
// Record EARLY to prevent duplicate processing. BB webhooks can retry
|
|
583
|
+
// before the first turn completes — recording after the turn is too late.
|
|
584
|
+
(0, bluebubbles_inbound_log_1.recordBlueBubblesInbound)(agentName, event, source);
|
|
585
|
+
if (source !== "webhook" && sessionLikelyContainsMessage(event, existing?.messages ?? sessionMessages)) {
|
|
586
|
+
(0, bluebubbles_inbound_log_1.recordBlueBubblesInbound)(agentName, event, "recovery-bootstrap");
|
|
587
|
+
(0, runtime_1.emitNervesEvent)({
|
|
588
|
+
component: "senses",
|
|
589
|
+
event: "senses.bluebubbles_recovery_skip",
|
|
590
|
+
message: "skipped bluebubbles recovery because the session already contains the message text",
|
|
591
|
+
meta: {
|
|
592
|
+
messageGuid: event.messageGuid,
|
|
593
|
+
sessionKey: event.chat.sessionKey,
|
|
594
|
+
source,
|
|
595
|
+
},
|
|
596
|
+
});
|
|
597
|
+
return { handled: true, notifiedAgent: false, kind: event.kind, reason: "already_processed" };
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (event.kind === "message" && event.chat.isGroup) {
|
|
601
|
+
await (0, group_context_1.upsertGroupContextParticipants)({
|
|
602
|
+
store,
|
|
603
|
+
participants: (event.chat.participantHandles ?? []).map((externalId) => ({
|
|
604
|
+
provider: "imessage-handle",
|
|
605
|
+
externalId,
|
|
606
|
+
})),
|
|
607
|
+
groupExternalId: resolveGroupExternalId(event),
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
// Build inbound user message (adapter concern: BB-specific content formatting)
|
|
611
|
+
const userMessage = {
|
|
612
|
+
role: "user",
|
|
613
|
+
content: buildInboundContent(event, existing?.messages ?? sessionMessages),
|
|
614
|
+
};
|
|
615
|
+
const callbacks = createBlueBubblesCallbacks(client, event.chat, replyTarget, event.chat.isGroup);
|
|
616
|
+
const controller = new AbortController();
|
|
617
|
+
// BB-specific tool context wrappers
|
|
618
|
+
const summarize = (0, core_1.createSummarize)();
|
|
619
|
+
const bbCapabilities = (0, channel_1.getChannelCapabilities)("bluebubbles");
|
|
620
|
+
const pendingDir = (0, pending_1.getPendingDir)(resolvedDeps.getAgentName(), friendId, "bluebubbles", event.chat.sessionKey);
|
|
621
|
+
// ── Compute trust gate context for group/acquaintance rules ─────
|
|
622
|
+
const groupHasFamilyMember = await checkGroupHasFamilyMember(store, event);
|
|
623
|
+
const hasExistingGroupWithFamily = event.chat.isGroup
|
|
624
|
+
? false
|
|
625
|
+
: await checkHasExistingGroupWithFamily(store, context.friend);
|
|
626
|
+
// ── Call shared pipeline ──────────────────────────────────────────
|
|
627
|
+
try {
|
|
628
|
+
const result = await (0, pipeline_1.handleInboundTurn)({
|
|
629
|
+
channel: "bluebubbles",
|
|
630
|
+
sessionKey: event.chat.sessionKey,
|
|
631
|
+
capabilities: bbCapabilities,
|
|
632
|
+
messages: [userMessage],
|
|
633
|
+
continuityIngressTexts: getBlueBubblesContinuityIngressTexts(event),
|
|
634
|
+
friendResolver: { resolve: () => Promise.resolve(context) },
|
|
635
|
+
sessionLoader: {
|
|
636
|
+
loadOrCreate: () => Promise.resolve({
|
|
637
|
+
messages: sessionMessages,
|
|
638
|
+
sessionPath: sessPath,
|
|
639
|
+
state: existing?.state,
|
|
640
|
+
}),
|
|
641
|
+
},
|
|
642
|
+
pendingDir,
|
|
643
|
+
friendStore: store,
|
|
644
|
+
provider: "imessage-handle",
|
|
645
|
+
externalId: event.sender.externalId || event.sender.rawId,
|
|
646
|
+
isGroupChat: event.chat.isGroup,
|
|
647
|
+
groupHasFamilyMember,
|
|
648
|
+
hasExistingGroupWithFamily,
|
|
649
|
+
enforceTrustGate: trust_gate_1.enforceTrustGate,
|
|
650
|
+
drainPending: pending_1.drainPending,
|
|
651
|
+
drainDeferredReturns: (deferredFriendId) => (0, pending_1.drainDeferredReturns)(resolvedDeps.getAgentName(), deferredFriendId),
|
|
652
|
+
runAgent: (msgs, cb, channel, sig, opts) => resolvedDeps.runAgent(msgs, cb, channel, sig, {
|
|
653
|
+
...opts,
|
|
654
|
+
toolContext: {
|
|
655
|
+
/* v8 ignore next -- default no-op signin; pipeline provides the real one @preserve */
|
|
656
|
+
signin: async () => undefined,
|
|
657
|
+
...opts?.toolContext,
|
|
658
|
+
summarize,
|
|
659
|
+
bluebubblesReplyTarget: {
|
|
660
|
+
setSelection: (selection) => replyTarget.setSelection(selection),
|
|
661
|
+
},
|
|
662
|
+
codingFeedback: {
|
|
663
|
+
send: async (message) => {
|
|
664
|
+
await client.sendText({
|
|
665
|
+
chat: event.chat,
|
|
666
|
+
text: message,
|
|
667
|
+
replyToMessageGuid: replyTarget.getReplyToMessageGuid(),
|
|
668
|
+
});
|
|
669
|
+
},
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
}),
|
|
673
|
+
postTurn: resolvedDeps.postTurn,
|
|
674
|
+
accumulateFriendTokens: resolvedDeps.accumulateFriendTokens,
|
|
675
|
+
signal: controller.signal,
|
|
676
|
+
runAgentOptions: { mcpManager },
|
|
677
|
+
callbacks,
|
|
678
|
+
failoverState: (() => {
|
|
679
|
+
if (!bbFailoverStates.has(event.chat.sessionKey)) {
|
|
680
|
+
bbFailoverStates.set(event.chat.sessionKey, { pending: null });
|
|
681
|
+
}
|
|
682
|
+
return bbFailoverStates.get(event.chat.sessionKey);
|
|
683
|
+
})(),
|
|
684
|
+
});
|
|
685
|
+
/* v8 ignore start -- failover display: tested via pipeline integration tests @preserve */
|
|
686
|
+
if (result.failoverMessage) {
|
|
687
|
+
await client.sendText({ chat: event.chat, text: result.failoverMessage });
|
|
688
|
+
}
|
|
689
|
+
/* v8 ignore stop */
|
|
690
|
+
// ── Handle gate result ────────────────────────────────────────
|
|
691
|
+
if (!result.gateResult.allowed) {
|
|
692
|
+
// Send auto-reply via BB API if the gate provides one
|
|
693
|
+
if ("autoReply" in result.gateResult && result.gateResult.autoReply) {
|
|
694
|
+
await client.sendText({
|
|
695
|
+
chat: event.chat,
|
|
696
|
+
text: result.gateResult.autoReply,
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
if (event.kind === "message") {
|
|
700
|
+
(0, bluebubbles_inbound_log_1.recordBlueBubblesInbound)(resolvedDeps.getAgentName(), event, source);
|
|
701
|
+
}
|
|
702
|
+
return {
|
|
703
|
+
handled: true,
|
|
704
|
+
notifiedAgent: false,
|
|
705
|
+
kind: event.kind,
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
// Gate allowed — flush the agent's reply
|
|
709
|
+
await callbacks.flush();
|
|
710
|
+
if (event.kind === "message") {
|
|
711
|
+
(0, bluebubbles_inbound_log_1.recordBlueBubblesInbound)(resolvedDeps.getAgentName(), event, source);
|
|
712
|
+
}
|
|
713
|
+
(0, runtime_1.emitNervesEvent)({
|
|
714
|
+
component: "senses",
|
|
715
|
+
event: "senses.bluebubbles_turn_end",
|
|
716
|
+
message: "bluebubbles event handled",
|
|
717
|
+
meta: {
|
|
718
|
+
messageGuid: event.messageGuid,
|
|
719
|
+
kind: event.kind,
|
|
720
|
+
sessionKey: event.chat.sessionKey,
|
|
721
|
+
},
|
|
722
|
+
});
|
|
723
|
+
return {
|
|
724
|
+
handled: true,
|
|
725
|
+
notifiedAgent: true,
|
|
726
|
+
kind: event.kind,
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
finally {
|
|
730
|
+
await callbacks.finish();
|
|
731
|
+
}
|
|
256
732
|
});
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
733
|
+
}
|
|
734
|
+
async function handleBlueBubblesEvent(payload, deps = {}) {
|
|
735
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
736
|
+
const client = resolvedDeps.createClient();
|
|
737
|
+
const event = await client.repairEvent((0, bluebubbles_model_1.normalizeBlueBubblesEvent)(payload));
|
|
738
|
+
return handleBlueBubblesNormalizedEvent(event, resolvedDeps, "webhook");
|
|
739
|
+
}
|
|
740
|
+
function countPendingRecoveryCandidates(agentName) {
|
|
741
|
+
return (0, bluebubbles_mutation_log_1.listBlueBubblesRecoveryCandidates)(agentName)
|
|
742
|
+
.filter((entry) => !(0, bluebubbles_inbound_log_1.hasRecordedBlueBubblesInbound)(agentName, entry.sessionKey, entry.messageGuid))
|
|
743
|
+
.length;
|
|
744
|
+
}
|
|
745
|
+
async function syncBlueBubblesRuntime(deps = {}) {
|
|
746
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
747
|
+
const agentName = resolvedDeps.getAgentName();
|
|
748
|
+
const client = resolvedDeps.createClient();
|
|
749
|
+
const checkedAt = new Date().toISOString();
|
|
750
|
+
try {
|
|
751
|
+
await client.checkHealth();
|
|
752
|
+
const recovery = await recoverMissedBlueBubblesMessages(resolvedDeps);
|
|
753
|
+
(0, bluebubbles_runtime_state_1.writeBlueBubblesRuntimeState)(agentName, {
|
|
754
|
+
upstreamStatus: recovery.pending > 0 || recovery.failed > 0 ? "error" : "ok",
|
|
755
|
+
detail: recovery.failed > 0
|
|
756
|
+
? `recovery failures: ${recovery.failed}`
|
|
757
|
+
: recovery.pending > 0
|
|
758
|
+
? `pending recovery: ${recovery.pending}`
|
|
759
|
+
: "upstream reachable",
|
|
760
|
+
lastCheckedAt: checkedAt,
|
|
761
|
+
pendingRecoveryCount: recovery.pending,
|
|
762
|
+
lastRecoveredAt: recovery.recovered > 0 ? checkedAt : undefined,
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
catch (error) {
|
|
766
|
+
(0, bluebubbles_runtime_state_1.writeBlueBubblesRuntimeState)(agentName, {
|
|
767
|
+
upstreamStatus: "error",
|
|
768
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
769
|
+
lastCheckedAt: checkedAt,
|
|
770
|
+
pendingRecoveryCount: countPendingRecoveryCandidates(agentName),
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
async function recoverMissedBlueBubblesMessages(deps = {}) {
|
|
775
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
776
|
+
const agentName = resolvedDeps.getAgentName();
|
|
777
|
+
const client = resolvedDeps.createClient();
|
|
778
|
+
const result = { recovered: 0, skipped: 0, pending: 0, failed: 0 };
|
|
779
|
+
for (const candidate of (0, bluebubbles_mutation_log_1.listBlueBubblesRecoveryCandidates)(agentName)) {
|
|
780
|
+
if ((0, bluebubbles_inbound_log_1.hasRecordedBlueBubblesInbound)(agentName, candidate.sessionKey, candidate.messageGuid)) {
|
|
781
|
+
result.skipped++;
|
|
782
|
+
continue;
|
|
783
|
+
}
|
|
784
|
+
try {
|
|
785
|
+
const repaired = await client.repairEvent(mutationEntryToEvent(candidate));
|
|
786
|
+
if (repaired.kind !== "message") {
|
|
787
|
+
result.pending++;
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
const handled = await handleBlueBubblesNormalizedEvent(repaired, resolvedDeps, "mutation-recovery");
|
|
791
|
+
if (handled.reason === "already_processed") {
|
|
792
|
+
result.skipped++;
|
|
793
|
+
}
|
|
794
|
+
else {
|
|
795
|
+
result.recovered++;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
catch (error) {
|
|
799
|
+
result.failed++;
|
|
800
|
+
(0, runtime_1.emitNervesEvent)({
|
|
801
|
+
level: "warn",
|
|
802
|
+
component: "senses",
|
|
803
|
+
event: "senses.bluebubbles_recovery_error",
|
|
804
|
+
message: "bluebubbles backlog recovery failed",
|
|
805
|
+
meta: {
|
|
806
|
+
messageGuid: candidate.messageGuid,
|
|
807
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
808
|
+
},
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
if (result.recovered > 0 || result.skipped > 0 || result.pending > 0 || result.failed > 0) {
|
|
813
|
+
(0, runtime_1.emitNervesEvent)({
|
|
814
|
+
component: "senses",
|
|
815
|
+
event: "senses.bluebubbles_recovery_complete",
|
|
816
|
+
message: "bluebubbles backlog recovery pass completed",
|
|
817
|
+
meta: { ...result },
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
return result;
|
|
262
821
|
}
|
|
263
822
|
function createBlueBubblesWebhookHandler(deps = {}) {
|
|
264
823
|
return async (req, res) => {
|
|
@@ -315,11 +874,321 @@ function createBlueBubblesWebhookHandler(deps = {}) {
|
|
|
315
874
|
}
|
|
316
875
|
};
|
|
317
876
|
}
|
|
877
|
+
function findImessageHandle(friend) {
|
|
878
|
+
for (const ext of friend.externalIds) {
|
|
879
|
+
if (ext.provider === "imessage-handle" && !ext.externalId.startsWith("group:")) {
|
|
880
|
+
return ext.externalId;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return undefined;
|
|
884
|
+
}
|
|
885
|
+
function normalizeBlueBubblesSessionKey(sessionKey) {
|
|
886
|
+
const trimmed = sessionKey.trim();
|
|
887
|
+
if (trimmed.startsWith("chat_identifier_")) {
|
|
888
|
+
return `chat_identifier:${trimmed.slice("chat_identifier_".length)}`;
|
|
889
|
+
}
|
|
890
|
+
if (trimmed.startsWith("chat_")) {
|
|
891
|
+
return `chat:${trimmed.slice("chat_".length)}`;
|
|
892
|
+
}
|
|
893
|
+
return trimmed;
|
|
894
|
+
}
|
|
895
|
+
function extractChatIdentifierFromSessionKey(sessionKey) {
|
|
896
|
+
const normalizedKey = normalizeBlueBubblesSessionKey(sessionKey);
|
|
897
|
+
if (normalizedKey.startsWith("chat:")) {
|
|
898
|
+
const chatGuid = normalizedKey.slice("chat:".length).trim();
|
|
899
|
+
const parts = chatGuid.split(";");
|
|
900
|
+
return parts.length >= 3 ? parts[2]?.trim() || undefined : undefined;
|
|
901
|
+
}
|
|
902
|
+
if (normalizedKey.startsWith("chat_identifier:")) {
|
|
903
|
+
const identifier = normalizedKey.slice("chat_identifier:".length).trim();
|
|
904
|
+
return identifier || undefined;
|
|
905
|
+
}
|
|
906
|
+
return undefined;
|
|
907
|
+
}
|
|
908
|
+
function buildChatRefForSessionKey(friend, sessionKey) {
|
|
909
|
+
const normalizedKey = normalizeBlueBubblesSessionKey(sessionKey);
|
|
910
|
+
if (normalizedKey.startsWith("chat:")) {
|
|
911
|
+
const chatGuid = normalizedKey.slice("chat:".length).trim();
|
|
912
|
+
if (!chatGuid)
|
|
913
|
+
return null;
|
|
914
|
+
return {
|
|
915
|
+
chatGuid,
|
|
916
|
+
chatIdentifier: extractChatIdentifierFromSessionKey(sessionKey) ?? findImessageHandle(friend),
|
|
917
|
+
isGroup: chatGuid.includes(";+;"),
|
|
918
|
+
sessionKey,
|
|
919
|
+
sendTarget: { kind: "chat_guid", value: chatGuid },
|
|
920
|
+
participantHandles: [],
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
const chatIdentifier = extractChatIdentifierFromSessionKey(sessionKey) ?? findImessageHandle(friend);
|
|
924
|
+
if (!chatIdentifier)
|
|
925
|
+
return null;
|
|
926
|
+
return {
|
|
927
|
+
chatIdentifier,
|
|
928
|
+
isGroup: false,
|
|
929
|
+
sessionKey,
|
|
930
|
+
sendTarget: { kind: "chat_identifier", value: chatIdentifier },
|
|
931
|
+
participantHandles: [],
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
async function sendProactiveBlueBubblesMessageToSession(params, deps = {}) {
|
|
935
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
936
|
+
const client = resolvedDeps.createClient();
|
|
937
|
+
const store = resolvedDeps.createFriendStore();
|
|
938
|
+
let friend;
|
|
939
|
+
try {
|
|
940
|
+
friend = await store.get(params.friendId);
|
|
941
|
+
}
|
|
942
|
+
catch {
|
|
943
|
+
friend = null;
|
|
944
|
+
}
|
|
945
|
+
if (!friend) {
|
|
946
|
+
(0, runtime_1.emitNervesEvent)({
|
|
947
|
+
level: "warn",
|
|
948
|
+
component: "senses",
|
|
949
|
+
event: "senses.bluebubbles_proactive_no_friend",
|
|
950
|
+
message: "proactive send skipped: friend not found",
|
|
951
|
+
meta: { friendId: params.friendId, sessionKey: params.sessionKey },
|
|
952
|
+
});
|
|
953
|
+
return { delivered: false, reason: "friend_not_found" };
|
|
954
|
+
}
|
|
955
|
+
const explicitCrossChatAuthorized = params.intent === "explicit_cross_chat"
|
|
956
|
+
&& types_1.TRUSTED_LEVELS.has(params.authorizingSession?.trustLevel ?? "stranger");
|
|
957
|
+
if (!explicitCrossChatAuthorized && !types_1.TRUSTED_LEVELS.has(friend.trustLevel ?? "stranger")) {
|
|
958
|
+
(0, runtime_1.emitNervesEvent)({
|
|
959
|
+
component: "senses",
|
|
960
|
+
event: "senses.bluebubbles_proactive_trust_skip",
|
|
961
|
+
message: "proactive send skipped: trust level not allowed",
|
|
962
|
+
meta: {
|
|
963
|
+
friendId: params.friendId,
|
|
964
|
+
sessionKey: params.sessionKey,
|
|
965
|
+
trustLevel: friend.trustLevel ?? "unknown",
|
|
966
|
+
intent: params.intent ?? "generic_outreach",
|
|
967
|
+
authorizingTrustLevel: params.authorizingSession?.trustLevel ?? null,
|
|
968
|
+
},
|
|
969
|
+
});
|
|
970
|
+
return { delivered: false, reason: "trust_skip" };
|
|
971
|
+
}
|
|
972
|
+
const chat = buildChatRefForSessionKey(friend, params.sessionKey);
|
|
973
|
+
if (!chat) {
|
|
974
|
+
(0, runtime_1.emitNervesEvent)({
|
|
975
|
+
level: "warn",
|
|
976
|
+
component: "senses",
|
|
977
|
+
event: "senses.bluebubbles_proactive_no_handle",
|
|
978
|
+
message: "proactive send skipped: no iMessage handle found",
|
|
979
|
+
meta: { friendId: params.friendId, sessionKey: params.sessionKey },
|
|
980
|
+
});
|
|
981
|
+
return { delivered: false, reason: "missing_target" };
|
|
982
|
+
}
|
|
983
|
+
try {
|
|
984
|
+
await client.sendText({ chat, text: params.text });
|
|
985
|
+
(0, runtime_1.emitNervesEvent)({
|
|
986
|
+
component: "senses",
|
|
987
|
+
event: "senses.bluebubbles_proactive_sent",
|
|
988
|
+
message: "proactive bluebubbles message sent",
|
|
989
|
+
meta: {
|
|
990
|
+
friendId: params.friendId,
|
|
991
|
+
sessionKey: params.sessionKey,
|
|
992
|
+
chatGuid: chat.chatGuid ?? null,
|
|
993
|
+
chatIdentifier: chat.chatIdentifier ?? null,
|
|
994
|
+
},
|
|
995
|
+
});
|
|
996
|
+
return { delivered: true };
|
|
997
|
+
}
|
|
998
|
+
catch (error) {
|
|
999
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1000
|
+
level: "error",
|
|
1001
|
+
component: "senses",
|
|
1002
|
+
event: "senses.bluebubbles_proactive_send_error",
|
|
1003
|
+
message: "proactive bluebubbles send failed",
|
|
1004
|
+
meta: {
|
|
1005
|
+
friendId: params.friendId,
|
|
1006
|
+
sessionKey: params.sessionKey,
|
|
1007
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
1008
|
+
},
|
|
1009
|
+
});
|
|
1010
|
+
return { delivered: false, reason: "send_error" };
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
function scanPendingBlueBubblesFiles(pendingRoot) {
|
|
1014
|
+
const results = [];
|
|
1015
|
+
let friendIds;
|
|
1016
|
+
try {
|
|
1017
|
+
friendIds = fs.readdirSync(pendingRoot);
|
|
1018
|
+
}
|
|
1019
|
+
catch {
|
|
1020
|
+
return results;
|
|
1021
|
+
}
|
|
1022
|
+
for (const friendId of friendIds) {
|
|
1023
|
+
const bbDir = path.join(pendingRoot, friendId, "bluebubbles");
|
|
1024
|
+
let keys;
|
|
1025
|
+
try {
|
|
1026
|
+
keys = fs.readdirSync(bbDir);
|
|
1027
|
+
}
|
|
1028
|
+
catch {
|
|
1029
|
+
continue;
|
|
1030
|
+
}
|
|
1031
|
+
for (const key of keys) {
|
|
1032
|
+
const keyDir = path.join(bbDir, key);
|
|
1033
|
+
let files;
|
|
1034
|
+
try {
|
|
1035
|
+
files = fs.readdirSync(keyDir);
|
|
1036
|
+
}
|
|
1037
|
+
catch {
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
for (const file of files.filter((f) => f.endsWith(".json")).sort()) {
|
|
1041
|
+
const filePath = path.join(keyDir, file);
|
|
1042
|
+
try {
|
|
1043
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
1044
|
+
results.push({ friendId, key, filePath, content });
|
|
1045
|
+
}
|
|
1046
|
+
catch {
|
|
1047
|
+
// skip unreadable files
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
return results;
|
|
1053
|
+
}
|
|
1054
|
+
async function drainAndSendPendingBlueBubbles(deps = {}, pendingRoot) {
|
|
1055
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
1056
|
+
const root = pendingRoot ?? path.join((0, identity_1.getAgentRoot)(), "state", "pending");
|
|
1057
|
+
const client = resolvedDeps.createClient();
|
|
1058
|
+
const store = resolvedDeps.createFriendStore();
|
|
1059
|
+
const pendingFiles = scanPendingBlueBubblesFiles(root);
|
|
1060
|
+
const result = { sent: 0, skipped: 0, failed: 0 };
|
|
1061
|
+
for (const { friendId, filePath, content } of pendingFiles) {
|
|
1062
|
+
let parsed;
|
|
1063
|
+
try {
|
|
1064
|
+
parsed = JSON.parse(content);
|
|
1065
|
+
}
|
|
1066
|
+
catch {
|
|
1067
|
+
result.failed++;
|
|
1068
|
+
try {
|
|
1069
|
+
fs.unlinkSync(filePath);
|
|
1070
|
+
}
|
|
1071
|
+
catch { /* ignore */ }
|
|
1072
|
+
continue;
|
|
1073
|
+
}
|
|
1074
|
+
const messageText = typeof parsed.content === "string" ? parsed.content : "";
|
|
1075
|
+
if (!messageText.trim()) {
|
|
1076
|
+
result.skipped++;
|
|
1077
|
+
try {
|
|
1078
|
+
fs.unlinkSync(filePath);
|
|
1079
|
+
}
|
|
1080
|
+
catch { /* ignore */ }
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
let friend;
|
|
1084
|
+
try {
|
|
1085
|
+
friend = await store.get(friendId);
|
|
1086
|
+
}
|
|
1087
|
+
catch {
|
|
1088
|
+
friend = null;
|
|
1089
|
+
}
|
|
1090
|
+
if (!friend) {
|
|
1091
|
+
result.skipped++;
|
|
1092
|
+
try {
|
|
1093
|
+
fs.unlinkSync(filePath);
|
|
1094
|
+
}
|
|
1095
|
+
catch { /* ignore */ }
|
|
1096
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1097
|
+
level: "warn",
|
|
1098
|
+
component: "senses",
|
|
1099
|
+
event: "senses.bluebubbles_proactive_no_friend",
|
|
1100
|
+
message: "proactive send skipped: friend not found",
|
|
1101
|
+
meta: { friendId },
|
|
1102
|
+
});
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
if (!types_1.TRUSTED_LEVELS.has(friend.trustLevel ?? "stranger")) {
|
|
1106
|
+
result.skipped++;
|
|
1107
|
+
try {
|
|
1108
|
+
fs.unlinkSync(filePath);
|
|
1109
|
+
}
|
|
1110
|
+
catch { /* ignore */ }
|
|
1111
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1112
|
+
component: "senses",
|
|
1113
|
+
event: "senses.bluebubbles_proactive_trust_skip",
|
|
1114
|
+
message: "proactive send skipped: trust level not allowed",
|
|
1115
|
+
meta: { friendId, trustLevel: friend.trustLevel ?? "unknown" },
|
|
1116
|
+
});
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
const handle = findImessageHandle(friend);
|
|
1120
|
+
if (!handle) {
|
|
1121
|
+
result.skipped++;
|
|
1122
|
+
try {
|
|
1123
|
+
fs.unlinkSync(filePath);
|
|
1124
|
+
}
|
|
1125
|
+
catch { /* ignore */ }
|
|
1126
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1127
|
+
level: "warn",
|
|
1128
|
+
component: "senses",
|
|
1129
|
+
event: "senses.bluebubbles_proactive_no_handle",
|
|
1130
|
+
message: "proactive send skipped: no iMessage handle found",
|
|
1131
|
+
meta: { friendId },
|
|
1132
|
+
});
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
const chat = {
|
|
1136
|
+
chatIdentifier: handle,
|
|
1137
|
+
isGroup: false,
|
|
1138
|
+
sessionKey: friendId,
|
|
1139
|
+
sendTarget: { kind: "chat_identifier", value: handle },
|
|
1140
|
+
participantHandles: [],
|
|
1141
|
+
};
|
|
1142
|
+
try {
|
|
1143
|
+
await client.sendText({ chat, text: messageText });
|
|
1144
|
+
result.sent++;
|
|
1145
|
+
try {
|
|
1146
|
+
fs.unlinkSync(filePath);
|
|
1147
|
+
}
|
|
1148
|
+
catch { /* ignore */ }
|
|
1149
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1150
|
+
component: "senses",
|
|
1151
|
+
event: "senses.bluebubbles_proactive_sent",
|
|
1152
|
+
message: "proactive bluebubbles message sent",
|
|
1153
|
+
meta: { friendId, handle },
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
catch (error) {
|
|
1157
|
+
result.failed++;
|
|
1158
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1159
|
+
level: "error",
|
|
1160
|
+
component: "senses",
|
|
1161
|
+
event: "senses.bluebubbles_proactive_send_error",
|
|
1162
|
+
message: "proactive bluebubbles send failed",
|
|
1163
|
+
meta: {
|
|
1164
|
+
friendId,
|
|
1165
|
+
handle,
|
|
1166
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
1167
|
+
},
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
if (result.sent > 0 || result.skipped > 0 || result.failed > 0) {
|
|
1172
|
+
(0, runtime_1.emitNervesEvent)({
|
|
1173
|
+
component: "senses",
|
|
1174
|
+
event: "senses.bluebubbles_proactive_drain_complete",
|
|
1175
|
+
message: "bluebubbles proactive drain complete",
|
|
1176
|
+
meta: { sent: result.sent, skipped: result.skipped, failed: result.failed },
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
return result;
|
|
1180
|
+
}
|
|
318
1181
|
function startBlueBubblesApp(deps = {}) {
|
|
319
1182
|
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
320
1183
|
resolvedDeps.createClient();
|
|
321
1184
|
const channelConfig = (0, config_1.getBlueBubblesChannelConfig)();
|
|
322
1185
|
const server = resolvedDeps.createServer(createBlueBubblesWebhookHandler(deps));
|
|
1186
|
+
const runtimeTimer = setInterval(() => {
|
|
1187
|
+
void syncBlueBubblesRuntime(resolvedDeps);
|
|
1188
|
+
}, BLUEBUBBLES_RUNTIME_SYNC_INTERVAL_MS);
|
|
1189
|
+
server.on?.("close", () => {
|
|
1190
|
+
clearInterval(runtimeTimer);
|
|
1191
|
+
});
|
|
323
1192
|
server.listen(channelConfig.port, () => {
|
|
324
1193
|
(0, runtime_1.emitNervesEvent)({
|
|
325
1194
|
component: "channels",
|
|
@@ -328,5 +1197,6 @@ function startBlueBubblesApp(deps = {}) {
|
|
|
328
1197
|
meta: { port: channelConfig.port, webhookPath: channelConfig.webhookPath },
|
|
329
1198
|
});
|
|
330
1199
|
});
|
|
1200
|
+
void syncBlueBubblesRuntime(resolvedDeps);
|
|
331
1201
|
return server;
|
|
332
1202
|
}
|