@ouro.bot/cli 0.1.0-alpha.4 → 0.1.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/heart/config.js +34 -0
- package/dist/heart/core.js +9 -0
- package/dist/heart/daemon/daemon-cli.js +86 -1
- package/dist/heart/daemon/hatch-animation.js +28 -0
- package/dist/heart/daemon/hatch-flow.js +1 -0
- package/dist/heart/daemon/specialist-orchestrator.js +160 -0
- package/dist/heart/daemon/specialist-prompt.js +40 -0
- package/dist/heart/daemon/specialist-session.js +142 -0
- package/dist/heart/daemon/specialist-tools.js +128 -0
- package/dist/heart/identity.js +14 -0
- package/dist/mind/friends/channel.js +8 -0
- package/dist/mind/friends/types.js +1 -1
- package/dist/mind/prompt.js +3 -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/package.json +2 -1
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
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.handleBlueBubblesEvent = handleBlueBubblesEvent;
|
|
37
|
+
exports.createBlueBubblesWebhookHandler = createBlueBubblesWebhookHandler;
|
|
38
|
+
exports.startBlueBubblesApp = startBlueBubblesApp;
|
|
39
|
+
const http = __importStar(require("node:http"));
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
41
|
+
const core_1 = require("../heart/core");
|
|
42
|
+
const config_1 = require("../heart/config");
|
|
43
|
+
const identity_1 = require("../heart/identity");
|
|
44
|
+
const context_1 = require("../mind/context");
|
|
45
|
+
const tokens_1 = require("../mind/friends/tokens");
|
|
46
|
+
const resolver_1 = require("../mind/friends/resolver");
|
|
47
|
+
const store_file_1 = require("../mind/friends/store-file");
|
|
48
|
+
const prompt_1 = require("../mind/prompt");
|
|
49
|
+
const runtime_1 = require("../nerves/runtime");
|
|
50
|
+
const bluebubbles_model_1 = require("./bluebubbles-model");
|
|
51
|
+
const bluebubbles_client_1 = require("./bluebubbles-client");
|
|
52
|
+
const bluebubbles_mutation_log_1 = require("./bluebubbles-mutation-log");
|
|
53
|
+
const defaultDeps = {
|
|
54
|
+
getAgentName: identity_1.getAgentName,
|
|
55
|
+
buildSystem: prompt_1.buildSystem,
|
|
56
|
+
runAgent: core_1.runAgent,
|
|
57
|
+
loadSession: context_1.loadSession,
|
|
58
|
+
postTurn: context_1.postTurn,
|
|
59
|
+
sessionPath: config_1.sessionPath,
|
|
60
|
+
accumulateFriendTokens: tokens_1.accumulateFriendTokens,
|
|
61
|
+
createClient: () => (0, bluebubbles_client_1.createBlueBubblesClient)(),
|
|
62
|
+
recordMutation: bluebubbles_mutation_log_1.recordBlueBubblesMutation,
|
|
63
|
+
createFriendStore: () => new store_file_1.FileFriendStore(path.join((0, identity_1.getAgentRoot)(), "friends")),
|
|
64
|
+
createFriendResolver: (store, params) => new resolver_1.FriendResolver(store, params),
|
|
65
|
+
createServer: http.createServer,
|
|
66
|
+
};
|
|
67
|
+
function resolveFriendParams(event) {
|
|
68
|
+
if (event.chat.isGroup) {
|
|
69
|
+
const groupKey = event.chat.chatGuid ?? event.chat.chatIdentifier ?? event.sender.externalId;
|
|
70
|
+
return {
|
|
71
|
+
provider: "imessage-handle",
|
|
72
|
+
externalId: `group:${groupKey}`,
|
|
73
|
+
displayName: event.chat.displayName ?? "Unknown Group",
|
|
74
|
+
channel: "bluebubbles",
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
provider: "imessage-handle",
|
|
79
|
+
externalId: event.sender.externalId || event.sender.rawId,
|
|
80
|
+
displayName: event.sender.displayName || "Unknown",
|
|
81
|
+
channel: "bluebubbles",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function buildInboundText(event) {
|
|
85
|
+
const baseText = event.repairNotice?.trim()
|
|
86
|
+
? `${event.textForAgent}\n[${event.repairNotice.trim()}]`
|
|
87
|
+
: event.textForAgent;
|
|
88
|
+
if (!event.chat.isGroup)
|
|
89
|
+
return baseText;
|
|
90
|
+
if (event.kind === "mutation") {
|
|
91
|
+
return `${event.sender.displayName} ${baseText}`;
|
|
92
|
+
}
|
|
93
|
+
return `${event.sender.displayName}: ${baseText}`;
|
|
94
|
+
}
|
|
95
|
+
function createBlueBubblesCallbacks(client, chat, replyToMessageGuid) {
|
|
96
|
+
let textBuffer = "";
|
|
97
|
+
return {
|
|
98
|
+
onModelStart() {
|
|
99
|
+
(0, runtime_1.emitNervesEvent)({
|
|
100
|
+
component: "senses",
|
|
101
|
+
event: "senses.bluebubbles_turn_start",
|
|
102
|
+
message: "bluebubbles turn started",
|
|
103
|
+
meta: { chatGuid: chat.chatGuid ?? null },
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
onModelStreamStart() {
|
|
107
|
+
(0, runtime_1.emitNervesEvent)({
|
|
108
|
+
component: "senses",
|
|
109
|
+
event: "senses.bluebubbles_stream_start",
|
|
110
|
+
message: "bluebubbles non-streaming response started",
|
|
111
|
+
meta: {},
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
onTextChunk(text) {
|
|
115
|
+
textBuffer += text;
|
|
116
|
+
},
|
|
117
|
+
onReasoningChunk(_text) { },
|
|
118
|
+
onToolStart(name, _args) {
|
|
119
|
+
(0, runtime_1.emitNervesEvent)({
|
|
120
|
+
component: "senses",
|
|
121
|
+
event: "senses.bluebubbles_tool_start",
|
|
122
|
+
message: "bluebubbles tool execution started",
|
|
123
|
+
meta: { name },
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
onToolEnd(name, summary, success) {
|
|
127
|
+
(0, runtime_1.emitNervesEvent)({
|
|
128
|
+
component: "senses",
|
|
129
|
+
event: "senses.bluebubbles_tool_end",
|
|
130
|
+
message: "bluebubbles tool execution completed",
|
|
131
|
+
meta: { name, success, summary },
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
onError(error, severity) {
|
|
135
|
+
(0, runtime_1.emitNervesEvent)({
|
|
136
|
+
level: severity === "terminal" ? "error" : "warn",
|
|
137
|
+
component: "senses",
|
|
138
|
+
event: "senses.bluebubbles_turn_error",
|
|
139
|
+
message: "bluebubbles turn callback error",
|
|
140
|
+
meta: { severity, reason: error.message },
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
onClearText() {
|
|
144
|
+
textBuffer = "";
|
|
145
|
+
},
|
|
146
|
+
async flush() {
|
|
147
|
+
const trimmed = textBuffer.trim();
|
|
148
|
+
if (!trimmed) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
textBuffer = "";
|
|
152
|
+
await client.sendText({
|
|
153
|
+
chat,
|
|
154
|
+
text: trimmed,
|
|
155
|
+
replyToMessageGuid,
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
async function readRequestBody(req) {
|
|
161
|
+
let body = "";
|
|
162
|
+
for await (const chunk of req) {
|
|
163
|
+
body += chunk.toString();
|
|
164
|
+
}
|
|
165
|
+
return body;
|
|
166
|
+
}
|
|
167
|
+
function writeJson(res, statusCode, payload) {
|
|
168
|
+
res.statusCode = statusCode;
|
|
169
|
+
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
170
|
+
res.end(JSON.stringify(payload));
|
|
171
|
+
}
|
|
172
|
+
function isWebhookPasswordValid(url, expectedPassword) {
|
|
173
|
+
const provided = url.searchParams.get("password");
|
|
174
|
+
return !provided || provided === expectedPassword;
|
|
175
|
+
}
|
|
176
|
+
async function handleBlueBubblesEvent(payload, deps = {}) {
|
|
177
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
178
|
+
const client = resolvedDeps.createClient();
|
|
179
|
+
const event = await client.repairEvent((0, bluebubbles_model_1.normalizeBlueBubblesEvent)(payload));
|
|
180
|
+
if (event.fromMe) {
|
|
181
|
+
(0, runtime_1.emitNervesEvent)({
|
|
182
|
+
component: "senses",
|
|
183
|
+
event: "senses.bluebubbles_from_me_ignored",
|
|
184
|
+
message: "ignored from-me bluebubbles event",
|
|
185
|
+
meta: {
|
|
186
|
+
messageGuid: event.messageGuid,
|
|
187
|
+
kind: event.kind,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
return { handled: true, notifiedAgent: false, kind: event.kind, reason: "from_me" };
|
|
191
|
+
}
|
|
192
|
+
if (event.kind === "mutation") {
|
|
193
|
+
try {
|
|
194
|
+
resolvedDeps.recordMutation(resolvedDeps.getAgentName(), event);
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
(0, runtime_1.emitNervesEvent)({
|
|
198
|
+
level: "error",
|
|
199
|
+
component: "senses",
|
|
200
|
+
event: "senses.bluebubbles_mutation_log_error",
|
|
201
|
+
message: "failed recording bluebubbles mutation sidecar",
|
|
202
|
+
meta: {
|
|
203
|
+
messageGuid: event.messageGuid,
|
|
204
|
+
mutationType: event.mutationType,
|
|
205
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (event.kind === "mutation" && !event.shouldNotifyAgent) {
|
|
211
|
+
(0, runtime_1.emitNervesEvent)({
|
|
212
|
+
component: "senses",
|
|
213
|
+
event: "senses.bluebubbles_state_mutation_recorded",
|
|
214
|
+
message: "recorded non-notify bluebubbles mutation",
|
|
215
|
+
meta: {
|
|
216
|
+
messageGuid: event.messageGuid,
|
|
217
|
+
mutationType: event.mutationType,
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
return { handled: true, notifiedAgent: false, kind: event.kind, reason: "mutation_state_only" };
|
|
221
|
+
}
|
|
222
|
+
const store = resolvedDeps.createFriendStore();
|
|
223
|
+
const resolver = resolvedDeps.createFriendResolver(store, resolveFriendParams(event));
|
|
224
|
+
const context = await resolver.resolve();
|
|
225
|
+
const toolContext = {
|
|
226
|
+
signin: async () => undefined,
|
|
227
|
+
friendStore: store,
|
|
228
|
+
summarize: (0, core_1.createSummarize)(),
|
|
229
|
+
context,
|
|
230
|
+
};
|
|
231
|
+
const friendId = context.friend.id;
|
|
232
|
+
const sessPath = resolvedDeps.sessionPath(friendId, "bluebubbles", event.chat.sessionKey);
|
|
233
|
+
const existing = resolvedDeps.loadSession(sessPath);
|
|
234
|
+
const messages = existing?.messages && existing.messages.length > 0
|
|
235
|
+
? existing.messages
|
|
236
|
+
: [{ role: "system", content: await resolvedDeps.buildSystem("bluebubbles", undefined, context) }];
|
|
237
|
+
messages.push({ role: "user", content: buildInboundText(event) });
|
|
238
|
+
const callbacks = createBlueBubblesCallbacks(client, event.chat, event.kind === "message" ? event.messageGuid : undefined);
|
|
239
|
+
const controller = new AbortController();
|
|
240
|
+
const agentOptions = {
|
|
241
|
+
toolContext,
|
|
242
|
+
};
|
|
243
|
+
const result = await resolvedDeps.runAgent(messages, callbacks, "bluebubbles", controller.signal, agentOptions);
|
|
244
|
+
await callbacks.flush();
|
|
245
|
+
resolvedDeps.postTurn(messages, sessPath, result.usage);
|
|
246
|
+
await resolvedDeps.accumulateFriendTokens(store, friendId, result.usage);
|
|
247
|
+
(0, runtime_1.emitNervesEvent)({
|
|
248
|
+
component: "senses",
|
|
249
|
+
event: "senses.bluebubbles_turn_end",
|
|
250
|
+
message: "bluebubbles event handled",
|
|
251
|
+
meta: {
|
|
252
|
+
messageGuid: event.messageGuid,
|
|
253
|
+
kind: event.kind,
|
|
254
|
+
sessionKey: event.chat.sessionKey,
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
return {
|
|
258
|
+
handled: true,
|
|
259
|
+
notifiedAgent: true,
|
|
260
|
+
kind: event.kind,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
function createBlueBubblesWebhookHandler(deps = {}) {
|
|
264
|
+
return async (req, res) => {
|
|
265
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
266
|
+
const channelConfig = (0, config_1.getBlueBubblesChannelConfig)();
|
|
267
|
+
const runtimeConfig = (0, config_1.getBlueBubblesConfig)();
|
|
268
|
+
if (url.pathname !== channelConfig.webhookPath) {
|
|
269
|
+
writeJson(res, 404, { error: "Not found" });
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (req.method !== "POST") {
|
|
273
|
+
writeJson(res, 405, { error: "Method not allowed" });
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (!isWebhookPasswordValid(url, runtimeConfig.password)) {
|
|
277
|
+
writeJson(res, 401, { error: "Unauthorized" });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
let payload;
|
|
281
|
+
try {
|
|
282
|
+
const rawBody = await readRequestBody(req);
|
|
283
|
+
payload = JSON.parse(rawBody);
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
(0, runtime_1.emitNervesEvent)({
|
|
287
|
+
level: "warn",
|
|
288
|
+
component: "senses",
|
|
289
|
+
event: "senses.bluebubbles_webhook_bad_json",
|
|
290
|
+
message: "failed to parse bluebubbles webhook body",
|
|
291
|
+
meta: {
|
|
292
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
writeJson(res, 400, { error: "Invalid JSON body" });
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
try {
|
|
299
|
+
const result = await handleBlueBubblesEvent(payload, deps);
|
|
300
|
+
writeJson(res, 200, result);
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
(0, runtime_1.emitNervesEvent)({
|
|
304
|
+
level: "error",
|
|
305
|
+
component: "senses",
|
|
306
|
+
event: "senses.bluebubbles_webhook_error",
|
|
307
|
+
message: "bluebubbles webhook handling failed",
|
|
308
|
+
meta: {
|
|
309
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
writeJson(res, 500, {
|
|
313
|
+
error: error instanceof Error ? error.message : String(error),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function startBlueBubblesApp(deps = {}) {
|
|
319
|
+
const resolvedDeps = { ...defaultDeps, ...deps };
|
|
320
|
+
resolvedDeps.createClient();
|
|
321
|
+
const channelConfig = (0, config_1.getBlueBubblesChannelConfig)();
|
|
322
|
+
const server = resolvedDeps.createServer(createBlueBubblesWebhookHandler(deps));
|
|
323
|
+
server.listen(channelConfig.port, () => {
|
|
324
|
+
(0, runtime_1.emitNervesEvent)({
|
|
325
|
+
component: "channels",
|
|
326
|
+
event: "channel.app_started",
|
|
327
|
+
message: "BlueBubbles sense started",
|
|
328
|
+
meta: { port: channelConfig.port, webhookPath: channelConfig.webhookPath },
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
return server;
|
|
332
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ouro.bot/cli",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.6",
|
|
4
4
|
"main": "dist/heart/daemon/ouro-entry.js",
|
|
5
5
|
"bin": {
|
|
6
6
|
"ouro": "dist/heart/daemon/ouro-entry.js",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"daemon": "tsc && node dist/heart/daemon/daemon-entry.js",
|
|
21
21
|
"ouro": "tsc && node dist/heart/daemon/ouro-entry.js",
|
|
22
22
|
"teams": "tsc && node dist/senses/teams-entry.js --agent ouroboros",
|
|
23
|
+
"bluebubbles": "tsc && node dist/senses/bluebubbles-entry.js --agent ouroboros",
|
|
23
24
|
"test": "vitest run",
|
|
24
25
|
"test:coverage:vitest": "vitest run --coverage",
|
|
25
26
|
"test:coverage": "node scripts/run-coverage-gate.cjs",
|