hyperclaw 5.2.8 → 5.2.9
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/a2ui-protocol-DEsfqO7h.js +75 -0
- package/dist/agents-routing-ChorJKFL.js +6 -0
- package/dist/agents-routing-Cpg20-1e.js +398 -0
- package/dist/api-key-validation-DgOBmp8Y.js +64 -0
- package/dist/api-keys-guide--73Bq0Ey.js +149 -0
- package/dist/audit-Dntx9fG9.js +445 -0
- package/dist/banner-B_rR7FPO.js +7 -0
- package/dist/banner-Bb7CderS.js +143 -0
- package/dist/bounty-tools-Dn9Yq7V1.js +211 -0
- package/dist/browser-tools-CJ9M9o22.js +179 -0
- package/dist/browser-tools-CXxWxA1c.js +5 -0
- package/dist/chat-D1Zc73I5.js +513 -0
- package/dist/claw-tasks-ChqkHdFs.js +80 -0
- package/dist/config-Bzues-G2.js +7 -0
- package/dist/config-DGAAJ49A.js +261 -0
- package/dist/connector-1efnZgQN.js +566 -0
- package/dist/connector-B5q1_srT.js +286 -0
- package/dist/connector-BMFLJ4fr.js +508 -0
- package/dist/connector-BZYv9siY.js +189 -0
- package/dist/connector-B_XkXm-j.js +218 -0
- package/dist/connector-BkY1I6gf.js +189 -0
- package/dist/connector-C2Z3RN1z.js +350 -0
- package/dist/connector-C7aYvVzU.js +225 -0
- package/dist/connector-CXXYOyHw.js +164 -0
- package/dist/connector-Ca1J23HX.js +167 -0
- package/dist/connector-Cjqc656I.js +181 -0
- package/dist/connector-CluRienN.js +425 -0
- package/dist/connector-CuNz2eP-.js +305 -0
- package/dist/connector-DHL_oeHy.js +552 -0
- package/dist/connector-DJ79rd1L.js +173 -0
- package/dist/connector-DcLE6xCZ.js +239 -0
- package/dist/connector-DcyQTJnJ.js +162 -0
- package/dist/connector-DgyX9qSc.js +192 -0
- package/dist/connector-DnDXXZ7L.js +182 -0
- package/dist/connector-DqUslg-X.js +213 -0
- package/dist/connector-Duc-HmRZ.js +85 -0
- package/dist/connector-IV0QFer8.js +498 -0
- package/dist/connector-OnsLxE8G.js +194 -0
- package/dist/connector-UYu_TS0X.js +204 -0
- package/dist/connector-cCA5ffmp.js +340 -0
- package/dist/connector-fbNfx4_L.js +568 -0
- package/dist/connector-fi2vlwbO.js +276 -0
- package/dist/connector-l3LD8IDN.js +419 -0
- package/dist/cost-tracker-DCXDUzBI.js +103 -0
- package/dist/credentials-store-Bmm9e1SS.js +7 -0
- package/dist/credentials-store-D8ERbGR2.js +89 -0
- package/dist/cron-tasks-OQbgmenS.js +85 -0
- package/dist/daemon-aVX-WGz1.js +421 -0
- package/dist/daemon-iFmVBPXN.js +7 -0
- package/dist/delivery-Pko6sSkt.js +4 -0
- package/dist/delivery-hMHFRZwy.js +95 -0
- package/dist/destructive-gate-C2TrWsp2.js +101 -0
- package/dist/developer-keys-BWXHaWxY.js +127 -0
- package/dist/developer-keys-CzDxVczE.js +8 -0
- package/dist/device-auth-store-C1bCwXO2.js +7 -0
- package/dist/device-auth-store-DIZTOz4V.js +88 -0
- package/dist/doctor-C6nAGdH8.js +233 -0
- package/dist/doctor-mgWumA25.js +6 -0
- package/dist/engine-BGRTi0fo.js +327 -0
- package/dist/engine-BvCEhaUn.js +7 -0
- package/dist/env-resolve-CHxjGo8u.js +151 -0
- package/dist/env-resolve-pIETNTpQ.js +10 -0
- package/dist/extraction-tools-BE6z_Yw5.js +91 -0
- package/dist/extraction-tools-CJsdyBST.js +5 -0
- package/dist/form_data-B2_0EoWj.js +8657 -0
- package/dist/gmail-watch-setup-1RGIHNdu.js +42 -0
- package/dist/health-BMUmUNoe.js +152 -0
- package/dist/health-C8n9RH5O.js +6 -0
- package/dist/heartbeat-engine-aRbab8a5.js +89 -0
- package/dist/hub-BJM2KXLO.js +6 -0
- package/dist/hub-Bb_dYECE.js +512 -0
- package/dist/hyperclawbot-CgrV46DA.js +516 -0
- package/dist/inference-CFBkvLpE.js +2854 -0
- package/dist/inference-G4MbdErG.js +8 -0
- package/dist/knowledge-graph-9UNrsiIY.js +134 -0
- package/dist/loader-DIPs649t.js +410 -0
- package/dist/loader-Dq_cDlOW.js +6 -0
- package/dist/logger-CG38Idq4.js +86 -0
- package/dist/manager--wG9JdFW.js +120 -0
- package/dist/manager-BNc21xgG.js +6 -0
- package/dist/manager-DWJ7WZcY.js +250 -0
- package/dist/manager-DYfzqckn.js +35 -0
- package/dist/mcp-SsMEvl28.js +142 -0
- package/dist/mcp-loader-DLrp_oZJ.js +93 -0
- package/dist/memory-BCYvdqwo.js +273 -0
- package/dist/memory-BGgCBSx1.js +6 -0
- package/dist/memory-auto-Bh52cQJP.js +5 -0
- package/dist/memory-auto-BupvVwNd.js +306 -0
- package/dist/memory-integration-4oGqX6rr.js +91 -0
- package/dist/moltbook-B3DNdh40.js +81 -0
- package/dist/node-C4esBfbX.js +226 -0
- package/dist/nodes-registry-DLUZhEMS.js +52 -0
- package/dist/oauth-flow-BbLQTzZk.js +148 -0
- package/dist/oauth-provider-UZyG84s7.js +111 -0
- package/dist/observability-BtLyuxcz.js +89 -0
- package/dist/onboard-B3drurt-.js +3812 -0
- package/dist/onboard-DEwuHrMj.js +14 -0
- package/dist/orchestrator-BXyIgAeH.js +189 -0
- package/dist/orchestrator-MLTc4NYu.js +6 -0
- package/dist/osint-Bsf6RGPv.js +283 -0
- package/dist/osint-chat-Ark94iFc.js +789 -0
- package/dist/pairing-2vAiDBjN.js +6 -0
- package/dist/pairing-B4NoBvyE.js +207 -0
- package/dist/pc-access-BLnc-DYp.js +858 -0
- package/dist/pc-access-CTNtG2LD.js +8 -0
- package/dist/pending-approval-C4ZaHHWl.js +22 -0
- package/dist/reminders-store-BixTWL1h.js +58 -0
- package/dist/renderer-B1ToXngl.js +228 -0
- package/dist/rules-Cqen1Mpt.js +106 -0
- package/dist/run-main.js +94 -93
- package/dist/runner-Cr1_mwnU.js +1274 -0
- package/dist/security-B4vH02lO.js +4 -0
- package/dist/security-HOOGCw5z.js +90 -0
- package/dist/server-CRhXiGI9.js +4 -0
- package/dist/server-De4H45Ju.js +1365 -0
- package/dist/session-store-7sEPyV16.js +5 -0
- package/dist/session-store-Ce2QMSL_.js +141 -0
- package/dist/sessions-tools-CWr_pDtp.js +5 -0
- package/dist/sessions-tools-DKVu9aIN.js +95 -0
- package/dist/skill-loader-DK19Jm4e.js +7 -0
- package/dist/skill-loader-Dfs9VNM-.js +160 -0
- package/dist/skill-runtime-BeKh2rD_.js +5 -0
- package/dist/skill-runtime-CcpC2Zfc.js +104 -0
- package/dist/src-BxmkZ9RH.js +63 -0
- package/dist/src-DjTtmSRg.js +20 -0
- package/dist/src-n_46LPDi.js +458 -0
- package/dist/sub-agent-tools-q5JqEze_.js +39 -0
- package/dist/tool-policy-QXF3MMxu.js +189 -0
- package/dist/tts-elevenlabs-y6HGWWDS.js +64 -0
- package/dist/update-check-C4te7JmY.js +6 -0
- package/dist/update-check-GDue_it1.js +98 -0
- package/dist/vision-tools-CbVfyTZT.js +5 -0
- package/dist/vision-tools-DieVQp6s.js +51 -0
- package/dist/vision-u4kMThdo.js +167 -0
- package/dist/voice-transcription-DgON5qaF.js +170 -0
- package/dist/website-watch-tools-B8L_NgwT.js +5 -0
- package/dist/website-watch-tools-Drm5HjIX.js +176 -0
- package/package.json +1 -1
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-jS-bbMI5.js');
|
|
2
|
+
const chalk = require_chunk.__toESM(require("chalk"));
|
|
3
|
+
const fs_extra = require_chunk.__toESM(require("fs-extra"));
|
|
4
|
+
const path = require_chunk.__toESM(require("path"));
|
|
5
|
+
const os = require_chunk.__toESM(require("os"));
|
|
6
|
+
const crypto = require_chunk.__toESM(require("crypto"));
|
|
7
|
+
const https = require_chunk.__toESM(require("https"));
|
|
8
|
+
const events = require_chunk.__toESM(require("events"));
|
|
9
|
+
|
|
10
|
+
//#region extensions/line/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "line-state.json");
|
|
12
|
+
const TEXT_CHUNK = 5e3;
|
|
13
|
+
const DEFAULT_MEDIA_MAX_MB = 10;
|
|
14
|
+
function lineReq(token, endpoint, body) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const payload = JSON.stringify(body);
|
|
17
|
+
const req = https.default.request({
|
|
18
|
+
hostname: "api.line.me",
|
|
19
|
+
port: 443,
|
|
20
|
+
path: endpoint,
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: {
|
|
23
|
+
Authorization: `Bearer ${token}`,
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
26
|
+
}
|
|
27
|
+
}, (res) => {
|
|
28
|
+
let data = "";
|
|
29
|
+
res.on("data", (c) => data += c);
|
|
30
|
+
res.on("end", () => {
|
|
31
|
+
try {
|
|
32
|
+
resolve(JSON.parse(data));
|
|
33
|
+
} catch {
|
|
34
|
+
resolve({});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
req.on("error", reject);
|
|
39
|
+
req.write(payload);
|
|
40
|
+
req.end();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Start the LINE loading indicator for a chat. */
|
|
44
|
+
function lineLoadingIndicator(token, chatId) {
|
|
45
|
+
return lineReq(token, "/v2/bot/chat/loading/start", {
|
|
46
|
+
chatId,
|
|
47
|
+
loadingSeconds: 20
|
|
48
|
+
}).catch(() => {});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Strip markdown for plain-text delivery.
|
|
52
|
+
* Code blocks and tables are converted to Flex cards when channelData allows;
|
|
53
|
+
* for plain-text paths they become preformatted text.
|
|
54
|
+
*/
|
|
55
|
+
function stripMarkdown(text) {
|
|
56
|
+
return text.replace(/```[\s\S]*?```/g, (m) => m.replace(/```\w*\n?/g, "").trim()).replace(/`([^`]+)`/g, "$1").replace(/^#{1,6}\s+/gm, "").replace(/\*\*(.+?)\*\*/g, "$1").replace(/\*(.+?)\*/g, "$1").replace(/~~(.+?)~~/g, "$1").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/^\s*[-*+]\s+/gm, "• ").replace(/^\s*\d+\.\s+/gm, (m) => m.trimStart());
|
|
57
|
+
}
|
|
58
|
+
function buildQuickReply(items) {
|
|
59
|
+
return { items: items.slice(0, 13).map((label) => ({
|
|
60
|
+
type: "action",
|
|
61
|
+
action: {
|
|
62
|
+
type: "message",
|
|
63
|
+
label: label.slice(0, 20),
|
|
64
|
+
text: label
|
|
65
|
+
}
|
|
66
|
+
})) };
|
|
67
|
+
}
|
|
68
|
+
function buildMessages(text, channelData) {
|
|
69
|
+
const msgs = [];
|
|
70
|
+
const plain = stripMarkdown(text);
|
|
71
|
+
const qr = channelData?.quickReplies?.length ? buildQuickReply(channelData.quickReplies) : void 0;
|
|
72
|
+
if (channelData?.location) {
|
|
73
|
+
const loc = channelData.location;
|
|
74
|
+
msgs.push({
|
|
75
|
+
type: "location",
|
|
76
|
+
title: loc.title,
|
|
77
|
+
address: loc.address,
|
|
78
|
+
latitude: loc.latitude,
|
|
79
|
+
longitude: loc.longitude
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (channelData?.flexMessage) {
|
|
83
|
+
msgs.push({
|
|
84
|
+
type: "flex",
|
|
85
|
+
altText: channelData.flexMessage.altText,
|
|
86
|
+
contents: channelData.flexMessage.contents,
|
|
87
|
+
...qr ? { quickReply: qr } : {}
|
|
88
|
+
});
|
|
89
|
+
return msgs;
|
|
90
|
+
}
|
|
91
|
+
if (channelData?.templateMessage) {
|
|
92
|
+
const t = channelData.templateMessage;
|
|
93
|
+
msgs.push({
|
|
94
|
+
type: "template",
|
|
95
|
+
altText: t.text,
|
|
96
|
+
template: {
|
|
97
|
+
type: "confirm",
|
|
98
|
+
text: t.text,
|
|
99
|
+
actions: [{
|
|
100
|
+
type: "postback",
|
|
101
|
+
label: t.confirmLabel,
|
|
102
|
+
data: t.confirmData
|
|
103
|
+
}, {
|
|
104
|
+
type: "postback",
|
|
105
|
+
label: t.cancelLabel,
|
|
106
|
+
data: t.cancelData
|
|
107
|
+
}]
|
|
108
|
+
},
|
|
109
|
+
...qr ? { quickReply: qr } : {}
|
|
110
|
+
});
|
|
111
|
+
return msgs;
|
|
112
|
+
}
|
|
113
|
+
const chunks = chunkText(plain);
|
|
114
|
+
chunks.forEach((chunk, i) => {
|
|
115
|
+
const isLast = i === chunks.length - 1;
|
|
116
|
+
msgs.push({
|
|
117
|
+
type: "text",
|
|
118
|
+
text: chunk,
|
|
119
|
+
...isLast && qr ? { quickReply: qr } : {}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
return msgs;
|
|
123
|
+
}
|
|
124
|
+
function chunkText(text) {
|
|
125
|
+
const chunks = [];
|
|
126
|
+
for (let i = 0; i < text.length; i += TEXT_CHUNK) chunks.push(text.slice(i, i + TEXT_CHUNK));
|
|
127
|
+
return chunks.length ? chunks : [""];
|
|
128
|
+
}
|
|
129
|
+
var LINEConnector = class extends events.EventEmitter {
|
|
130
|
+
config;
|
|
131
|
+
running = false;
|
|
132
|
+
_token = "";
|
|
133
|
+
_secret = "";
|
|
134
|
+
constructor(config) {
|
|
135
|
+
super();
|
|
136
|
+
this.config = {
|
|
137
|
+
dmPolicy: "pairing",
|
|
138
|
+
allowFrom: [],
|
|
139
|
+
groupPolicy: "allowlist",
|
|
140
|
+
groupAllowFrom: [],
|
|
141
|
+
approvedPairings: [],
|
|
142
|
+
pendingPairings: {},
|
|
143
|
+
mediaMaxMb: DEFAULT_MEDIA_MAX_MB,
|
|
144
|
+
...config
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Resolve token and secret (inline value takes priority over file). */
|
|
148
|
+
async resolveCredentials() {
|
|
149
|
+
this._token = this.config.channelAccessToken || (this.config.tokenFile ? (await fs_extra.default.readFile(this.config.tokenFile, "utf8")).trim() : "");
|
|
150
|
+
this._secret = this.config.channelSecret || (this.config.secretFile ? (await fs_extra.default.readFile(this.config.secretFile, "utf8")).trim() : "");
|
|
151
|
+
if (!this._token || !this._secret) throw new Error("LINE: channelAccessToken and channelSecret are required (or set tokenFile/secretFile).");
|
|
152
|
+
}
|
|
153
|
+
async connect() {
|
|
154
|
+
await this.resolveCredentials();
|
|
155
|
+
await this.loadState();
|
|
156
|
+
this.running = true;
|
|
157
|
+
const wPath = this.config.webhookPath || "/line/webhook";
|
|
158
|
+
console.log(chalk.default.green(` 🟩 LINE: connected (webhook mode — ${wPath})`));
|
|
159
|
+
this.emit("connected", {});
|
|
160
|
+
}
|
|
161
|
+
disconnect() {
|
|
162
|
+
this.running = false;
|
|
163
|
+
}
|
|
164
|
+
verifySignature(rawBody, signature) {
|
|
165
|
+
const expected = crypto.default.createHmac("sha256", this._secret).update(rawBody).digest("base64");
|
|
166
|
+
try {
|
|
167
|
+
return crypto.default.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
|
|
168
|
+
} catch {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async handleWebhook(rawBody, signature) {
|
|
173
|
+
if (!this.verifySignature(rawBody, signature)) {
|
|
174
|
+
console.log(chalk.default.yellow(" ⚠ LINE: invalid signature — check channelSecret"));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
let payload;
|
|
178
|
+
try {
|
|
179
|
+
payload = JSON.parse(rawBody);
|
|
180
|
+
} catch {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
for (const event of payload.events || []) await this.handleEvent(event);
|
|
184
|
+
}
|
|
185
|
+
async handleEvent(event) {
|
|
186
|
+
const src = event.source || {};
|
|
187
|
+
const sourceType = src.type || "user";
|
|
188
|
+
const userId = src.userId || "";
|
|
189
|
+
const groupId = src.groupId || src.roomId || "";
|
|
190
|
+
const replyToken = event.replyToken || "";
|
|
191
|
+
const isGroup = sourceType === "group" || sourceType === "room";
|
|
192
|
+
if (event.type === "postback") {
|
|
193
|
+
const data = event.postback?.data || "";
|
|
194
|
+
if (!data || !userId) return;
|
|
195
|
+
if (isGroup) {
|
|
196
|
+
if (!this.isGroupAllowed(groupId, userId)) return;
|
|
197
|
+
} else if (!await this.checkDMPolicy(userId, data, replyToken)) return;
|
|
198
|
+
this.emit("message", {
|
|
199
|
+
channelId: "line",
|
|
200
|
+
chatId: isGroup ? groupId : userId,
|
|
201
|
+
from: userId,
|
|
202
|
+
text: data,
|
|
203
|
+
replyToken,
|
|
204
|
+
isDM: !isGroup
|
|
205
|
+
});
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (event.type !== "message") return;
|
|
209
|
+
const msgType = event.message?.type || "";
|
|
210
|
+
const messageId = event.message?.id || "";
|
|
211
|
+
let text = "";
|
|
212
|
+
switch (msgType) {
|
|
213
|
+
case "text":
|
|
214
|
+
text = event.message.text || "";
|
|
215
|
+
break;
|
|
216
|
+
case "location":
|
|
217
|
+
text = [
|
|
218
|
+
event.message.title,
|
|
219
|
+
event.message.address,
|
|
220
|
+
`${event.message.latitude},${event.message.longitude}`
|
|
221
|
+
].filter(Boolean).join(" | ");
|
|
222
|
+
break;
|
|
223
|
+
case "sticker":
|
|
224
|
+
text = `[sticker:${event.message.packageId}/${event.message.stickerId}]`;
|
|
225
|
+
break;
|
|
226
|
+
case "image":
|
|
227
|
+
case "video":
|
|
228
|
+
case "audio":
|
|
229
|
+
case "file": {
|
|
230
|
+
const maxBytes = (this.config.mediaMaxMb ?? DEFAULT_MEDIA_MAX_MB) * 1024 * 1024;
|
|
231
|
+
const contentUrl = `/v2/bot/message/${messageId}/content`;
|
|
232
|
+
text = `[${msgType}:${contentUrl} max:${maxBytes}b]`;
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
default: return;
|
|
236
|
+
}
|
|
237
|
+
if (!userId || !text) return;
|
|
238
|
+
if (isGroup) {
|
|
239
|
+
if (!this.isGroupAllowed(groupId, userId)) return;
|
|
240
|
+
lineLoadingIndicator(this._token, groupId);
|
|
241
|
+
this.emit("message", {
|
|
242
|
+
channelId: "line",
|
|
243
|
+
chatId: groupId,
|
|
244
|
+
from: userId,
|
|
245
|
+
text,
|
|
246
|
+
replyToken,
|
|
247
|
+
isDM: false
|
|
248
|
+
});
|
|
249
|
+
} else {
|
|
250
|
+
const allowed = await this.checkDMPolicy(userId, text, replyToken);
|
|
251
|
+
if (!allowed) return;
|
|
252
|
+
lineLoadingIndicator(this._token, userId);
|
|
253
|
+
this.emit("message", {
|
|
254
|
+
channelId: "line",
|
|
255
|
+
chatId: userId,
|
|
256
|
+
from: userId,
|
|
257
|
+
text,
|
|
258
|
+
replyToken,
|
|
259
|
+
isDM: true,
|
|
260
|
+
timestamp: new Date(event.timestamp).toISOString()
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
isGroupAllowed(groupId, userId) {
|
|
265
|
+
if (this.config.groupPolicy === "disabled") return false;
|
|
266
|
+
if (this.config.groupPolicy === "open") return true;
|
|
267
|
+
const perGroup = this.config.groups?.[groupId]?.allowFrom;
|
|
268
|
+
const list = perGroup ?? this.config.groupAllowFrom;
|
|
269
|
+
return list.includes(userId) || list.includes(groupId);
|
|
270
|
+
}
|
|
271
|
+
async checkDMPolicy(from, text, replyToken) {
|
|
272
|
+
switch (this.config.dmPolicy) {
|
|
273
|
+
case "disabled": return false;
|
|
274
|
+
case "open": return true;
|
|
275
|
+
case "allowlist":
|
|
276
|
+
if (this.config.allowFrom.includes(from)) return true;
|
|
277
|
+
await this.replyMessage(replyToken, "HyperClaw: Not on allowlist.");
|
|
278
|
+
return false;
|
|
279
|
+
case "pairing": {
|
|
280
|
+
if (this.config.approvedPairings.includes(from)) return true;
|
|
281
|
+
const upper = text.trim().toUpperCase();
|
|
282
|
+
if (this.config.pendingPairings[upper]) {
|
|
283
|
+
this.config.approvedPairings.push(from);
|
|
284
|
+
delete this.config.pendingPairings[upper];
|
|
285
|
+
await this.saveState();
|
|
286
|
+
await this.replyMessage(replyToken, "Paired!");
|
|
287
|
+
this.emit("pairing:approved", {
|
|
288
|
+
userId: from,
|
|
289
|
+
channelId: "line"
|
|
290
|
+
});
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
294
|
+
this.config.pendingPairings[code] = from;
|
|
295
|
+
await this.saveState();
|
|
296
|
+
await this.replyMessage(replyToken, `Pairing code: ${code}\nApprove: hyperclaw pairing approve line ${code}`);
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
default: return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
async replyMessage(replyToken, text, channelData) {
|
|
303
|
+
const messages = buildMessages(text, channelData).slice(0, 5);
|
|
304
|
+
await lineReq(this._token, "/v2/bot/message/reply", {
|
|
305
|
+
replyToken,
|
|
306
|
+
messages
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
async pushMessage(to, text, channelData) {
|
|
310
|
+
const messages = buildMessages(text, channelData).slice(0, 5);
|
|
311
|
+
await lineReq(this._token, "/v2/bot/message/push", {
|
|
312
|
+
to,
|
|
313
|
+
messages
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/** Unified send — uses replyToken when available, falls back to push. */
|
|
317
|
+
async sendMessage(chatId, text, replyToken, channelData) {
|
|
318
|
+
const id = String(chatId);
|
|
319
|
+
if (replyToken) await this.replyMessage(replyToken, text, channelData);
|
|
320
|
+
else await this.pushMessage(id, text, channelData);
|
|
321
|
+
}
|
|
322
|
+
approvePairing(code) {
|
|
323
|
+
const upper = code.toUpperCase();
|
|
324
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
325
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
326
|
+
delete this.config.pendingPairings[upper];
|
|
327
|
+
this.saveState();
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
async loadState() {
|
|
331
|
+
try {
|
|
332
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
333
|
+
if (s.p) this.config.pendingPairings = s.p;
|
|
334
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
335
|
+
} catch {}
|
|
336
|
+
}
|
|
337
|
+
async saveState() {
|
|
338
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
339
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
340
|
+
p: this.config.pendingPairings,
|
|
341
|
+
a: this.config.approvedPairings
|
|
342
|
+
}, { spaces: 2 });
|
|
343
|
+
}
|
|
344
|
+
isRunning() {
|
|
345
|
+
return this.running;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
//#endregion
|
|
350
|
+
exports.LINEConnector = LINEConnector;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-jS-bbMI5.js');
|
|
2
|
+
const fs_extra = require_chunk.__toESM(require("fs-extra"));
|
|
3
|
+
const path = require_chunk.__toESM(require("path"));
|
|
4
|
+
const os = require_chunk.__toESM(require("os"));
|
|
5
|
+
const child_process = require_chunk.__toESM(require("child_process"));
|
|
6
|
+
const events = require_chunk.__toESM(require("events"));
|
|
7
|
+
|
|
8
|
+
//#region extensions/imessage-native/src/connector.ts
|
|
9
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "imessage-native-state.json");
|
|
10
|
+
const PAIRING_TTL_MS = 60 * 60 * 1e3;
|
|
11
|
+
var IMessageNativeConnector = class extends events.EventEmitter {
|
|
12
|
+
config;
|
|
13
|
+
rpcProc = null;
|
|
14
|
+
running = false;
|
|
15
|
+
lastMessageTs = 0;
|
|
16
|
+
rpcSeq = 1;
|
|
17
|
+
pending = /* @__PURE__ */ new Map();
|
|
18
|
+
buf = "";
|
|
19
|
+
constructor(config = {}) {
|
|
20
|
+
super();
|
|
21
|
+
this.config = {
|
|
22
|
+
cliPath: config.cliPath || process.env.IMSG_PATH || "imsg",
|
|
23
|
+
dbPath: config.dbPath || path.default.join(os.default.homedir(), "Library", "Messages", "chat.db"),
|
|
24
|
+
dmPolicy: config.dmPolicy ?? "pairing",
|
|
25
|
+
allowFrom: config.allowFrom ?? [],
|
|
26
|
+
approvedPairings: config.approvedPairings ?? [],
|
|
27
|
+
pendingPairings: config.pendingPairings ?? {},
|
|
28
|
+
pendingPairingTs: config.pendingPairingTs ?? {}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async connect() {
|
|
32
|
+
if (process.platform !== "darwin") throw new Error("imessage-native is macOS only. For new deployments use BlueBubbles.");
|
|
33
|
+
await this.loadState();
|
|
34
|
+
this.running = true;
|
|
35
|
+
this.emit("connected", {});
|
|
36
|
+
this.startRpc();
|
|
37
|
+
}
|
|
38
|
+
startRpc() {
|
|
39
|
+
if (!this.running) return;
|
|
40
|
+
const bin = this.config.cliPath;
|
|
41
|
+
const args = ["rpc"];
|
|
42
|
+
if (this.config.dbPath) args.push("--db", this.config.dbPath);
|
|
43
|
+
this.rpcProc = (0, child_process.spawn)(bin, args, { stdio: [
|
|
44
|
+
"pipe",
|
|
45
|
+
"pipe",
|
|
46
|
+
"pipe"
|
|
47
|
+
] });
|
|
48
|
+
this.rpcProc.stdout?.on("data", (chunk) => {
|
|
49
|
+
this.buf += chunk.toString();
|
|
50
|
+
const lines = this.buf.split("\n");
|
|
51
|
+
this.buf = lines.pop() || "";
|
|
52
|
+
for (const line of lines) {
|
|
53
|
+
if (!line.trim()) continue;
|
|
54
|
+
try {
|
|
55
|
+
this.handleRpcLine(JSON.parse(line));
|
|
56
|
+
} catch {}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
this.rpcProc.stderr?.on("data", () => {});
|
|
60
|
+
this.rpcProc.on("error", () => {});
|
|
61
|
+
this.rpcProc.on("exit", (code) => {
|
|
62
|
+
this.rpcProc = null;
|
|
63
|
+
this.buf = "";
|
|
64
|
+
for (const [, cb] of this.pending) cb.reject(new Error("imsg rpc exited"));
|
|
65
|
+
this.pending.clear();
|
|
66
|
+
if (this.running && code !== 0) setTimeout(() => this.startRpc(), 5e3);
|
|
67
|
+
});
|
|
68
|
+
this.rpcSend("messages.watch", {}).catch(() => {});
|
|
69
|
+
}
|
|
70
|
+
handleRpcLine(msg) {
|
|
71
|
+
if (!msg.id && msg.method === "message" && msg.params) {
|
|
72
|
+
this.onIncomingMessage(msg.params);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (msg.id != null) {
|
|
76
|
+
const cb = this.pending.get(msg.id);
|
|
77
|
+
if (!cb) return;
|
|
78
|
+
this.pending.delete(msg.id);
|
|
79
|
+
if (msg.error) cb.reject(new Error(msg.error.message));
|
|
80
|
+
else cb.resolve(msg.result);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
onIncomingMessage(params) {
|
|
84
|
+
if (params.isFromMe) return;
|
|
85
|
+
const handle = params.handle?.address || params.sender || params.chatId || params.from;
|
|
86
|
+
const text = params.text || params.body || params.message;
|
|
87
|
+
if (!handle || !text) return;
|
|
88
|
+
const ts = params.dateCreated || params.timestamp || Date.now();
|
|
89
|
+
if (ts <= this.lastMessageTs) return;
|
|
90
|
+
this.lastMessageTs = ts;
|
|
91
|
+
this.saveState();
|
|
92
|
+
this.checkDMPolicyAndEmit(handle, text);
|
|
93
|
+
}
|
|
94
|
+
pruneExpiredCodes() {
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
const ts = this.config.pendingPairingTs;
|
|
97
|
+
for (const code of Object.keys(ts)) if (now - ts[code] > PAIRING_TTL_MS) {
|
|
98
|
+
delete this.config.pendingPairings[code];
|
|
99
|
+
delete ts[code];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async checkDMPolicyAndEmit(from, text) {
|
|
103
|
+
if (this.config.dmPolicy === "disabled") return;
|
|
104
|
+
if (this.config.dmPolicy === "open") {
|
|
105
|
+
this.emit("message", {
|
|
106
|
+
chatId: from,
|
|
107
|
+
text
|
|
108
|
+
});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
112
|
+
if (this.config.allowFrom.includes(from)) this.emit("message", {
|
|
113
|
+
chatId: from,
|
|
114
|
+
text
|
|
115
|
+
});
|
|
116
|
+
else await this.sendMessage(from, "HyperClaw: Not on allowlist.");
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
this.pruneExpiredCodes();
|
|
120
|
+
if (this.config.approvedPairings.includes(from)) {
|
|
121
|
+
this.emit("message", {
|
|
122
|
+
chatId: from,
|
|
123
|
+
text
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const code = (text.trim().toUpperCase().match(/[A-Z0-9]{6}/) || [])[0];
|
|
128
|
+
if (code && this.config.pendingPairings[code]) {
|
|
129
|
+
this.config.approvedPairings.push(from);
|
|
130
|
+
delete this.config.pendingPairings[code];
|
|
131
|
+
delete this.config.pendingPairingTs[code];
|
|
132
|
+
await this.saveState();
|
|
133
|
+
await this.sendMessage(from, "Paired!");
|
|
134
|
+
this.emit("pairing:approved", {
|
|
135
|
+
userId: from,
|
|
136
|
+
channelId: "imessage-native"
|
|
137
|
+
});
|
|
138
|
+
this.emit("message", {
|
|
139
|
+
chatId: from,
|
|
140
|
+
text
|
|
141
|
+
});
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const newCode = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
145
|
+
this.config.pendingPairings[newCode] = from;
|
|
146
|
+
this.config.pendingPairingTs[newCode] = Date.now();
|
|
147
|
+
await this.saveState();
|
|
148
|
+
await this.sendMessage(from, `Pairing code: ${newCode}\nApprove: hyperclaw pairing approve imessage-native ${newCode}\n(expires in 1 hour)`);
|
|
149
|
+
}
|
|
150
|
+
async sendMessage(chatId, text) {
|
|
151
|
+
const to = String(chatId).trim();
|
|
152
|
+
if (!to) return;
|
|
153
|
+
if (!this.rpcProc) throw new Error("imsg rpc not running");
|
|
154
|
+
await this.rpcSend("messages.send", {
|
|
155
|
+
to,
|
|
156
|
+
text
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
rpcSend(method, params) {
|
|
160
|
+
return new Promise((resolve, reject) => {
|
|
161
|
+
if (!this.rpcProc?.stdin) {
|
|
162
|
+
reject(new Error("imsg rpc stdin unavailable"));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const id = this.rpcSeq++;
|
|
166
|
+
const req = {
|
|
167
|
+
jsonrpc: "2.0",
|
|
168
|
+
id,
|
|
169
|
+
method,
|
|
170
|
+
params
|
|
171
|
+
};
|
|
172
|
+
this.pending.set(id, {
|
|
173
|
+
resolve,
|
|
174
|
+
reject
|
|
175
|
+
});
|
|
176
|
+
this.rpcProc.stdin.write(JSON.stringify(req) + "\n");
|
|
177
|
+
const timer = setTimeout(() => {
|
|
178
|
+
if (this.pending.has(id)) {
|
|
179
|
+
this.pending.delete(id);
|
|
180
|
+
reject(new Error(`imsg rpc timeout for method ${method}`));
|
|
181
|
+
}
|
|
182
|
+
}, 15e3);
|
|
183
|
+
const orig = this.pending.get(id);
|
|
184
|
+
this.pending.set(id, {
|
|
185
|
+
resolve: (v) => {
|
|
186
|
+
clearTimeout(timer);
|
|
187
|
+
orig.resolve(v);
|
|
188
|
+
},
|
|
189
|
+
reject: (e) => {
|
|
190
|
+
clearTimeout(timer);
|
|
191
|
+
orig.reject(e);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
disconnect() {
|
|
197
|
+
this.running = false;
|
|
198
|
+
this.rpcProc?.kill();
|
|
199
|
+
this.rpcProc = null;
|
|
200
|
+
this.buf = "";
|
|
201
|
+
for (const [, cb] of this.pending) cb.reject(new Error("disconnected"));
|
|
202
|
+
this.pending.clear();
|
|
203
|
+
}
|
|
204
|
+
async loadState() {
|
|
205
|
+
try {
|
|
206
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
207
|
+
this.lastMessageTs = s.lastTs || 0;
|
|
208
|
+
if (s.p) this.config.pendingPairings = s.p;
|
|
209
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
210
|
+
if (s.pts) this.config.pendingPairingTs = s.pts;
|
|
211
|
+
} catch {}
|
|
212
|
+
}
|
|
213
|
+
async saveState() {
|
|
214
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
215
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
216
|
+
lastTs: this.lastMessageTs,
|
|
217
|
+
p: this.config.pendingPairings,
|
|
218
|
+
a: this.config.approvedPairings,
|
|
219
|
+
pts: this.config.pendingPairingTs
|
|
220
|
+
}, { spaces: 2 });
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
//#endregion
|
|
225
|
+
exports.IMessageNativeConnector = IMessageNativeConnector;
|