hyperclaw 4.0.1 → 4.0.2
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/README.md +7 -4
- package/dist/a2ui-protocol-CT_jDEU9.js +75 -0
- package/dist/agents-routing-683Q2JGp.js +129 -0
- package/dist/agents-routing-BpZBswBH.js +4 -0
- package/dist/api-keys-guide-Dq5Obbp4.js +149 -0
- package/dist/audit-BYxPlnTQ.js +248 -0
- package/dist/bounty-tools-C6LyzxM-.js +211 -0
- package/dist/browser-tools-CQBSbIuO.js +5 -0
- package/dist/browser-tools-YQmwRLLM.js +179 -0
- package/dist/claw-tasks-BRLUvFRD.js +80 -0
- package/dist/connector-3HnyH8fn.js +167 -0
- package/dist/connector-6PMZo5Ky.js +189 -0
- package/dist/connector-B6eoF3DD.js +181 -0
- package/dist/connector-B9tLG8UZ.js +196 -0
- package/dist/connector-BOlqjXWP.js +182 -0
- package/dist/connector-BP8zsbP8.js +189 -0
- package/dist/connector-BPoSevxp.js +286 -0
- package/dist/connector-BRHj773i.js +163 -0
- package/dist/connector-BToxU-jV.js +267 -0
- package/dist/connector-BliDVsJQ.js +239 -0
- package/dist/connector-Bv6s9oP7.js +88 -0
- package/dist/connector-By5wWGTR.js +343 -0
- package/dist/connector-C1BaFFgN.js +213 -0
- package/dist/connector-CRRWY5Wv.js +167 -0
- package/dist/connector-CXPQVGyI.js +85 -0
- package/dist/connector-Cdk1CXKi.js +194 -0
- package/dist/connector-CwlgFgjx.js +181 -0
- package/dist/connector-DFchk6l7.js +178 -0
- package/dist/connector-DKw7tRAy.js +192 -0
- package/dist/connector-DRv1ahC_.js +2 -2
- package/dist/connector-DU63KW94.js +165 -0
- package/dist/connector-Dbvb1Cj9.js +280 -0
- package/dist/connector-DcZdQcgR.js +173 -0
- package/dist/connector-DxKL8VvZ.js +182 -0
- package/dist/connector-T_YdZtzv.js +162 -0
- package/dist/connector-i4gOS9xL.js +154 -0
- package/dist/connector-rHXE1ZD2.js +167 -0
- package/dist/connector-wdUXChwa.js +172 -0
- package/dist/cost-tracker-pVE15Yq4.js +103 -0
- package/dist/credentials-store-BvnMPJwi.js +4 -0
- package/dist/credentials-store-sb-TRLwR.js +77 -0
- package/dist/cron-tasks-BvDFNyiE.js +82 -0
- package/dist/delivery-D5Z98EVq.js +95 -0
- package/dist/delivery-DCOXhXEO.js +5 -0
- package/dist/destructive-gate-m-dWqUFg.js +101 -0
- package/dist/developer-keys-JaJK3T27.js +127 -0
- package/dist/developer-keys-kyqmtWK3.js +8 -0
- package/dist/doctor-3oi89QIc.js +175 -0
- package/dist/doctor-Cf1XSfp9.js +4 -0
- package/dist/engine-B4eMiTgl.js +7 -0
- package/dist/engine-B8M7dYul.js +7 -0
- package/dist/engine-BhT-1M9W.js +256 -0
- package/dist/engine-D49jnSd_.js +256 -0
- package/dist/env-resolve-DWOQ45jG.js +9 -0
- package/dist/env-resolve-szSWl0UF.js +94 -0
- package/dist/extraction-tools-D3qDFBJ1.js +91 -0
- package/dist/extraction-tools-DLr_AEwq.js +5 -0
- package/dist/form_data-B_hIUrxU.js +8657 -0
- package/dist/gmail-watch-setup-Czt8rXaX.js +40 -0
- package/dist/heartbeat-engine-CRqfPcFM.js +83 -0
- package/dist/hub-DTsqe5Bt.js +6 -0
- package/dist/hub-FrPTA33j.js +515 -0
- package/dist/hyperclawbot-D9KCtc4P.js +480 -0
- package/dist/hyperclawbot-Dw27pJo4.js +480 -0
- package/dist/inference-CTWJeX9Q.js +922 -0
- package/dist/inference-ix607p7k.js +6 -0
- package/dist/knowledge-graph-DqA-Fztl.js +131 -0
- package/dist/loader-CISCqBto.js +400 -0
- package/dist/loader-CYMQ8VOS.js +4 -0
- package/dist/logger-8tEtAd3y.js +83 -0
- package/dist/manager-CPjeRe-6.js +4 -0
- package/dist/manager-Cwzj7w5R.js +105 -0
- package/dist/manager-DLmZI-9R.js +6 -0
- package/dist/manager-DSGhn5i3.js +117 -0
- package/dist/manager-DgyF52mg.js +218 -0
- package/dist/manager-Dm8nrMFx.js +40 -0
- package/dist/mcp-B_9Ber63.js +139 -0
- package/dist/mcp-loader-DSM5UiFG.js +94 -0
- package/dist/mcp-loader-j5ZLLw5O.js +94 -0
- package/dist/memory-BI1kPkAN.js +4 -0
- package/dist/memory-BVFGkxxK.js +270 -0
- package/dist/memory-auto-Bc7euou4.js +306 -0
- package/dist/memory-auto-DPfbkMVt.js +5 -0
- package/dist/memory-integration-DZExqWr4.js +91 -0
- package/dist/moltbook-B6ZeGN5_.js +81 -0
- package/dist/node-pwL6O_KX.js +222 -0
- package/dist/nodes-registry-CsPm_-CJ.js +52 -0
- package/dist/oauth-flow-CpWlgvNB.js +150 -0
- package/dist/oauth-provider-BZb6qOw5.js +110 -0
- package/dist/observability-B43YvNQV.js +89 -0
- package/dist/onboard-Bd_wsYdi.js +4086 -0
- package/dist/onboard-CAN7x3me.js +3026 -0
- package/dist/onboard-DnegOHMh.js +4 -4
- package/dist/onboard-RYtDlYBw.js +9 -0
- package/dist/onboard-aTwlQs-4.js +9 -0
- package/dist/orchestrator-BSp2M5EU.js +189 -0
- package/dist/orchestrator-C7ko5tWa.js +6 -0
- package/dist/orchestrator-DfPkIx2Z.js +6 -0
- package/dist/orchestrator-NJQsmiBE.js +189 -0
- package/dist/pairing-DU0_J28n.js +87 -0
- package/dist/pairing-DWllbSbO.js +4 -0
- package/dist/pc-access-Ly-uA8mn.js +8 -0
- package/dist/pc-access-NxBvTrRj.js +819 -0
- package/dist/pending-approval-DIHvwwWS.js +22 -0
- package/dist/puppeteer-2o3QOwAy.js +2 -2
- package/dist/puppeteer-BYTMp3BI.js +2 -2
- package/dist/puppeteer-DQ45qwWk.js +2 -2
- package/dist/reminders-store-D79qdfN0.js +58 -0
- package/dist/renderer-pqlDRKbH.js +225 -0
- package/dist/rules-BooT_qFP.js +103 -0
- package/dist/run-main.js +289 -1031
- package/dist/runner-D1rjuMTJ.js +810 -0
- package/dist/sdk/index.js +2 -2
- package/dist/sdk/index.mjs +2 -2
- package/dist/security-C-5URby1.js +73 -0
- package/dist/security-_xve79aq.js +4 -0
- package/dist/server-0kgyELx4.js +1047 -0
- package/dist/server-BIuTobTC.js +4 -0
- package/dist/server-BRlCEjyT.js +1047 -0
- package/dist/server-CCI1hv45.js +2 -2
- package/dist/server-DU9POoWc.js +4 -0
- package/dist/session-store-CujxByI6.js +113 -0
- package/dist/session-store-qpJUg2M1.js +5 -0
- package/dist/sessions-tools-CB2qbwIk.js +5 -0
- package/dist/sessions-tools-DHMaTZIs.js +95 -0
- package/dist/skill-loader-BkceKkIg.js +7 -0
- package/dist/skill-loader-DhgIwK4J.js +159 -0
- package/dist/skill-runtime--LqxWrp5.js +102 -0
- package/dist/skill-runtime-C5l0Tgt-.js +5 -0
- package/dist/skill-runtime-DsXK_HYG.js +102 -0
- package/dist/skill-runtime-IVTiqrMR.js +5 -0
- package/dist/src-BEVLgaF1.js +63 -0
- package/dist/src-Bgu_OxTQ.js +458 -0
- package/dist/src-Bq-oKt7Z.js +458 -0
- package/dist/src-DWCUhnD4.js +20 -0
- package/dist/src-cfRTjFef.js +63 -0
- package/dist/sub-agent-tools-BD9DF8_g.js +39 -0
- package/dist/sub-agent-tools-V7b3T9_s.js +39 -0
- package/dist/tool-policy-DNvNRnve.js +189 -0
- package/dist/tts-elevenlabs-BUOGKL-k.js +61 -0
- package/dist/update-check-BD4qH7Am.js +81 -0
- package/dist/vision-DRq-f-Dj.js +121 -0
- package/dist/vision-tools-CFZEpQKm.js +5 -0
- package/dist/vision-tools-CQnBI9aa.js +51 -0
- package/dist/voice-transcription-CgWq54hn.js +138 -0
- package/dist/website-watch-tools-Bk_TnwuE.js +5 -0
- package/dist/website-watch-tools-DraMPxdl.js +139 -0
- package/package.json +1 -1
|
@@ -0,0 +1,280 @@
|
|
|
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 https = require_chunk.__toESM(require("https"));
|
|
7
|
+
const events = require_chunk.__toESM(require("events"));
|
|
8
|
+
|
|
9
|
+
//#region extensions/telegram/src/connector.ts
|
|
10
|
+
function tgDownloadFile(token, filePath) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const req = https.default.request({
|
|
13
|
+
hostname: "api.telegram.org",
|
|
14
|
+
port: 443,
|
|
15
|
+
path: `/file/bot${token}/${filePath}`,
|
|
16
|
+
method: "GET"
|
|
17
|
+
}, (res) => {
|
|
18
|
+
const chunks = [];
|
|
19
|
+
res.on("data", (c) => chunks.push(c));
|
|
20
|
+
res.on("end", () => resolve(Buffer.concat(chunks)));
|
|
21
|
+
});
|
|
22
|
+
req.on("error", reject);
|
|
23
|
+
req.setTimeout(3e4, () => {
|
|
24
|
+
req.destroy();
|
|
25
|
+
reject(new Error("Download timeout"));
|
|
26
|
+
});
|
|
27
|
+
req.end();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function tgRequest(token, method, body) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const payload = body ? JSON.stringify(body) : null;
|
|
33
|
+
const req = https.default.request({
|
|
34
|
+
hostname: "api.telegram.org",
|
|
35
|
+
port: 443,
|
|
36
|
+
path: `/bot${token}/${method}`,
|
|
37
|
+
method: payload ? "POST" : "GET",
|
|
38
|
+
headers: payload ? {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
41
|
+
} : {}
|
|
42
|
+
}, (res) => {
|
|
43
|
+
let data = "";
|
|
44
|
+
res.on("data", (c) => data += c);
|
|
45
|
+
res.on("end", () => {
|
|
46
|
+
try {
|
|
47
|
+
const r = JSON.parse(data);
|
|
48
|
+
if (r.ok) resolve(r.result);
|
|
49
|
+
else reject(new Error(r.description || "Telegram API error"));
|
|
50
|
+
} catch {
|
|
51
|
+
reject(new Error("Invalid JSON"));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
req.on("error", reject);
|
|
56
|
+
req.setTimeout(35e3, () => {
|
|
57
|
+
req.destroy();
|
|
58
|
+
reject(new Error("Timeout"));
|
|
59
|
+
});
|
|
60
|
+
if (payload) req.write(payload);
|
|
61
|
+
req.end();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "telegram-state.json");
|
|
65
|
+
function sleep(ms) {
|
|
66
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
67
|
+
}
|
|
68
|
+
function generateCode() {
|
|
69
|
+
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
70
|
+
return Array.from({ length: 6 }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
|
|
71
|
+
}
|
|
72
|
+
function chunkText(text, max) {
|
|
73
|
+
if (text.length <= max) return [text];
|
|
74
|
+
const chunks = [];
|
|
75
|
+
let i = 0;
|
|
76
|
+
while (i < text.length) {
|
|
77
|
+
let end = Math.min(i + max, text.length);
|
|
78
|
+
if (end < text.length) {
|
|
79
|
+
const nl = text.lastIndexOf("\n", end);
|
|
80
|
+
if (nl > i) end = nl + 1;
|
|
81
|
+
}
|
|
82
|
+
chunks.push(text.slice(i, end));
|
|
83
|
+
i = end;
|
|
84
|
+
}
|
|
85
|
+
return chunks;
|
|
86
|
+
}
|
|
87
|
+
var TelegramConnector = class extends events.EventEmitter {
|
|
88
|
+
token;
|
|
89
|
+
config;
|
|
90
|
+
offset = 0;
|
|
91
|
+
running = false;
|
|
92
|
+
botInfo = null;
|
|
93
|
+
constructor(token, config) {
|
|
94
|
+
super();
|
|
95
|
+
this.token = token;
|
|
96
|
+
this.config = {
|
|
97
|
+
token,
|
|
98
|
+
dmPolicy: "allowlist",
|
|
99
|
+
allowFrom: [],
|
|
100
|
+
pendingPairings: {},
|
|
101
|
+
approvedPairings: [],
|
|
102
|
+
...config
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async connect() {
|
|
106
|
+
this.botInfo = await tgRequest(this.token, "getMe");
|
|
107
|
+
console.log(chalk.default.green(` 🦅 Telegram: @${this.botInfo?.username} connected`));
|
|
108
|
+
await this.loadState();
|
|
109
|
+
this.running = true;
|
|
110
|
+
this.pollLoop();
|
|
111
|
+
this.emit("connected", this.botInfo);
|
|
112
|
+
}
|
|
113
|
+
async disconnect() {
|
|
114
|
+
this.running = false;
|
|
115
|
+
await this.saveState();
|
|
116
|
+
}
|
|
117
|
+
async pollLoop() {
|
|
118
|
+
while (this.running) try {
|
|
119
|
+
const updates = await tgRequest(this.token, "getUpdates", {
|
|
120
|
+
offset: this.offset,
|
|
121
|
+
timeout: 30,
|
|
122
|
+
allowed_updates: ["message"]
|
|
123
|
+
});
|
|
124
|
+
for (const u of updates) {
|
|
125
|
+
this.offset = u.update_id + 1;
|
|
126
|
+
await this.handleUpdate(u).catch((e) => console.log(chalk.default.yellow(` ⚠ ${e.message}`)));
|
|
127
|
+
}
|
|
128
|
+
} catch (e) {
|
|
129
|
+
if (this.running) {
|
|
130
|
+
console.log(chalk.default.yellow(` ⚠ Telegram poll: ${e.message}`));
|
|
131
|
+
await sleep(5e3);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async handleUpdate(u) {
|
|
136
|
+
const msg = u.message || u.edited_message;
|
|
137
|
+
if (!msg) return;
|
|
138
|
+
const text = msg.text?.trim() || "";
|
|
139
|
+
if (!text && !msg.voice) return;
|
|
140
|
+
const userId = String(msg.from?.id || "");
|
|
141
|
+
const isDM = msg.chat.type === "private";
|
|
142
|
+
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
|
|
143
|
+
if (isDM && !await this.checkDMPolicy(userId, msg.chat.id, text || "[voice note]")) return;
|
|
144
|
+
let finalText = text || "[voice note]";
|
|
145
|
+
if (isGroup) {
|
|
146
|
+
const groupAllowFrom = this.config.groupAllowFrom ?? [];
|
|
147
|
+
if (groupAllowFrom.length > 0 && !groupAllowFrom.includes(String(msg.chat.id))) return;
|
|
148
|
+
const activation = this.config.groupActivation ?? "mention";
|
|
149
|
+
if (activation === "mention") {
|
|
150
|
+
const botUsername = this.botInfo?.username ? `@${this.botInfo.username}`.toLowerCase() : "";
|
|
151
|
+
const mentioned = botUsername && text.toLowerCase().includes(botUsername);
|
|
152
|
+
const isReplyToBot = msg.reply_to_message?.from?.is_bot && msg.reply_to_message.from.id === this.botInfo?.id;
|
|
153
|
+
if (!mentioned && !isReplyToBot) return;
|
|
154
|
+
if (mentioned && botUsername) finalText = text.replace(new RegExp(botUsername, "gi"), "").trim() || finalText;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let audioPath;
|
|
158
|
+
if (msg?.voice?.file_id) try {
|
|
159
|
+
const file = await tgRequest(this.token, "getFile", { file_id: msg.voice.file_id });
|
|
160
|
+
const buf = await tgDownloadFile(this.token, file.file_path);
|
|
161
|
+
const ext = (file.file_path || "").split(".").pop() || "oga";
|
|
162
|
+
const tmp = path.default.join(os.default.tmpdir(), `hyperclaw-tg-voice-${msg.message_id}.${ext}`);
|
|
163
|
+
await fs_extra.default.writeFile(tmp, buf);
|
|
164
|
+
audioPath = tmp;
|
|
165
|
+
} catch (e) {
|
|
166
|
+
console.log(chalk.default.yellow(` ⚠ Telegram voice download failed: ${e.message}`));
|
|
167
|
+
}
|
|
168
|
+
this.emit("message", {
|
|
169
|
+
id: String(msg.message_id),
|
|
170
|
+
channelId: "telegram",
|
|
171
|
+
from: userId,
|
|
172
|
+
fromUsername: msg.from?.username,
|
|
173
|
+
chatId: msg.chat.id,
|
|
174
|
+
text: finalText,
|
|
175
|
+
audioPath,
|
|
176
|
+
timestamp: new Date(msg.date * 1e3).toISOString(),
|
|
177
|
+
isDM
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
async checkDMPolicy(userId, chatId, text) {
|
|
181
|
+
if (this.config.dmPolicy === "none") return false;
|
|
182
|
+
if (this.config.dmPolicy === "open") return true;
|
|
183
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
184
|
+
if (this.config.allowFrom.includes(userId)) return true;
|
|
185
|
+
await this.sendMessage(chatId, `🦅 *HyperClaw*\n\nYou are not on the allowlist.`);
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
if (this.config.dmPolicy === "pairing") {
|
|
189
|
+
if (this.config.approvedPairings.includes(userId)) return true;
|
|
190
|
+
const upper = text.trim().toUpperCase();
|
|
191
|
+
if (this.config.pendingPairings[upper]) {
|
|
192
|
+
this.config.approvedPairings.push(userId);
|
|
193
|
+
delete this.config.pendingPairings[upper];
|
|
194
|
+
await this.saveState();
|
|
195
|
+
await this.sendMessage(chatId, `🦅 *Paired!* You can now send messages.`);
|
|
196
|
+
this.emit("pairing:approved", {
|
|
197
|
+
userId,
|
|
198
|
+
channelId: "telegram"
|
|
199
|
+
});
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
const code = generateCode();
|
|
203
|
+
this.config.pendingPairings[code] = userId;
|
|
204
|
+
await this.saveState();
|
|
205
|
+
await this.sendMessage(chatId, `🦅 *HyperClaw Pairing*\n\nSend the owner this code:\n\`${code}\`\n\nApprove with:\n\`hyperclaw pairing approve telegram ${code}\``);
|
|
206
|
+
console.log(chalk.default.cyan(` 🦅 Telegram pairing from ${userId} — code: ${code}`));
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
async sendMessage(chatId, text, opts = {}) {
|
|
212
|
+
const chunks = chunkText(text, 4096);
|
|
213
|
+
let last = null;
|
|
214
|
+
for (const chunk of chunks) last = await tgRequest(this.token, "sendMessage", {
|
|
215
|
+
chat_id: chatId,
|
|
216
|
+
text: chunk,
|
|
217
|
+
parse_mode: opts.parse_mode || "Markdown",
|
|
218
|
+
disable_web_page_preview: opts.disable_web_page_preview ?? true,
|
|
219
|
+
...opts.reply_to_message_id ? { reply_to_message_id: opts.reply_to_message_id } : {}
|
|
220
|
+
});
|
|
221
|
+
return last;
|
|
222
|
+
}
|
|
223
|
+
async sendTyping(chatId) {
|
|
224
|
+
await tgRequest(this.token, "sendChatAction", {
|
|
225
|
+
chat_id: chatId,
|
|
226
|
+
action: "typing"
|
|
227
|
+
}).catch(() => {});
|
|
228
|
+
}
|
|
229
|
+
approvePairing(code) {
|
|
230
|
+
const upper = code.toUpperCase();
|
|
231
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
232
|
+
const userId = this.config.pendingPairings[upper];
|
|
233
|
+
this.config.approvedPairings.push(userId);
|
|
234
|
+
delete this.config.pendingPairings[upper];
|
|
235
|
+
this.saveState();
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
addToAllowlist(userId) {
|
|
239
|
+
if (!this.config.allowFrom.includes(userId)) {
|
|
240
|
+
this.config.allowFrom.push(userId);
|
|
241
|
+
this.saveState();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
listPendingPairings() {
|
|
245
|
+
return Object.entries(this.config.pendingPairings).map(([code, userId]) => ({
|
|
246
|
+
code,
|
|
247
|
+
userId
|
|
248
|
+
}));
|
|
249
|
+
}
|
|
250
|
+
handleWebhookPayload(body) {
|
|
251
|
+
try {
|
|
252
|
+
this.handleUpdate(JSON.parse(body));
|
|
253
|
+
} catch {}
|
|
254
|
+
}
|
|
255
|
+
async loadState() {
|
|
256
|
+
try {
|
|
257
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
258
|
+
this.offset = s.offset || 0;
|
|
259
|
+
if (s.pendingPairings) this.config.pendingPairings = s.pendingPairings;
|
|
260
|
+
if (s.approvedPairings) this.config.approvedPairings = s.approvedPairings;
|
|
261
|
+
} catch {}
|
|
262
|
+
}
|
|
263
|
+
async saveState() {
|
|
264
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
265
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
266
|
+
offset: this.offset,
|
|
267
|
+
pendingPairings: this.config.pendingPairings,
|
|
268
|
+
approvedPairings: this.config.approvedPairings
|
|
269
|
+
}, { spaces: 2 });
|
|
270
|
+
}
|
|
271
|
+
isRunning() {
|
|
272
|
+
return this.running;
|
|
273
|
+
}
|
|
274
|
+
getBotInfo() {
|
|
275
|
+
return this.botInfo;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
//#endregion
|
|
280
|
+
exports.TelegramConnector = TelegramConnector;
|
|
@@ -0,0 +1,173 @@
|
|
|
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/messenger/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "messenger-state.json");
|
|
12
|
+
function fbReq(token, endpoint, body) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const payload = JSON.stringify(body);
|
|
15
|
+
const req = https.default.request({
|
|
16
|
+
hostname: "graph.facebook.com",
|
|
17
|
+
port: 443,
|
|
18
|
+
path: `/v18.0${endpoint}`,
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: {
|
|
21
|
+
"Authorization": `Bearer ${token}`,
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
24
|
+
}
|
|
25
|
+
}, (res) => {
|
|
26
|
+
let data = "";
|
|
27
|
+
res.on("data", (c) => data += c);
|
|
28
|
+
res.on("end", () => {
|
|
29
|
+
try {
|
|
30
|
+
const r = JSON.parse(data);
|
|
31
|
+
if (r.error) reject(new Error(r.error.message));
|
|
32
|
+
else resolve(r);
|
|
33
|
+
} catch {
|
|
34
|
+
reject(new Error("Invalid JSON"));
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
req.on("error", reject);
|
|
39
|
+
req.write(payload);
|
|
40
|
+
req.end();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
var MessengerConnector = class extends events.EventEmitter {
|
|
44
|
+
config;
|
|
45
|
+
running = false;
|
|
46
|
+
processedIds = /* @__PURE__ */ new Set();
|
|
47
|
+
constructor(config) {
|
|
48
|
+
super();
|
|
49
|
+
this.config = {
|
|
50
|
+
dmPolicy: "allowlist",
|
|
51
|
+
allowFrom: [],
|
|
52
|
+
approvedPairings: [],
|
|
53
|
+
pendingPairings: {},
|
|
54
|
+
...config
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async connect() {
|
|
58
|
+
await this.loadState();
|
|
59
|
+
this.running = true;
|
|
60
|
+
console.log(chalk.default.green(` 🦅 Messenger: page ${this.config.pageId} connected`));
|
|
61
|
+
this.emit("connected", { pageId: this.config.pageId });
|
|
62
|
+
}
|
|
63
|
+
disconnect() {
|
|
64
|
+
this.running = false;
|
|
65
|
+
}
|
|
66
|
+
verifyWebhook(mode, token, challenge) {
|
|
67
|
+
return mode === "subscribe" && token === this.config.verifyToken ? challenge : null;
|
|
68
|
+
}
|
|
69
|
+
verifySignature(body, signature) {
|
|
70
|
+
const expected = "sha256=" + crypto.default.createHmac("sha256", this.config.appSecret).update(body).digest("hex");
|
|
71
|
+
try {
|
|
72
|
+
return crypto.default.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async handleWebhook(body, signature) {
|
|
78
|
+
if (signature && !this.verifySignature(body, signature)) {
|
|
79
|
+
console.log(chalk.default.yellow(" ⚠ Messenger: invalid signature"));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
let payload;
|
|
83
|
+
try {
|
|
84
|
+
payload = JSON.parse(body);
|
|
85
|
+
} catch {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
for (const entry of payload.entry || []) for (const messaging of entry.messaging || []) {
|
|
89
|
+
if (!messaging.message || messaging.message.is_echo) continue;
|
|
90
|
+
const msgId = messaging.message.mid;
|
|
91
|
+
if (this.processedIds.has(msgId)) continue;
|
|
92
|
+
this.processedIds.add(msgId);
|
|
93
|
+
const senderId = messaging.sender.id;
|
|
94
|
+
const text = messaging.message.text;
|
|
95
|
+
if (!text) continue;
|
|
96
|
+
const allowed = await this.checkDMPolicy(senderId, text);
|
|
97
|
+
if (!allowed) continue;
|
|
98
|
+
this.emit("message", {
|
|
99
|
+
id: msgId,
|
|
100
|
+
channelId: "messenger",
|
|
101
|
+
from: senderId,
|
|
102
|
+
chatId: senderId,
|
|
103
|
+
text,
|
|
104
|
+
timestamp: new Date(messaging.timestamp).toISOString(),
|
|
105
|
+
isDM: true
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async checkDMPolicy(from, text) {
|
|
110
|
+
if (this.config.dmPolicy === "none") return false;
|
|
111
|
+
if (this.config.dmPolicy === "open") return true;
|
|
112
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
113
|
+
if (this.config.allowFrom.includes(from)) return true;
|
|
114
|
+
await this.sendMessage(from, "🦅 HyperClaw: Not on allowlist.");
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
if (this.config.dmPolicy === "pairing") {
|
|
118
|
+
if (this.config.approvedPairings.includes(from)) return true;
|
|
119
|
+
const upper = text.trim().toUpperCase();
|
|
120
|
+
if (this.config.pendingPairings[upper]) {
|
|
121
|
+
this.config.approvedPairings.push(from);
|
|
122
|
+
delete this.config.pendingPairings[upper];
|
|
123
|
+
await this.saveState();
|
|
124
|
+
await this.sendMessage(from, "🦅 Paired!");
|
|
125
|
+
this.emit("pairing:approved", {
|
|
126
|
+
userId: from,
|
|
127
|
+
channelId: "messenger"
|
|
128
|
+
});
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
132
|
+
this.config.pendingPairings[code] = from;
|
|
133
|
+
await this.saveState();
|
|
134
|
+
await this.sendMessage(from, `🦅 Pairing code: ${code}\nApprove: hyperclaw pairing approve messenger ${code}`);
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
async sendMessage(recipientId, text) {
|
|
140
|
+
await fbReq(this.config.pageAccessToken, "/me/messages", {
|
|
141
|
+
recipient: { id: recipientId },
|
|
142
|
+
message: { text: text.slice(0, 2e3) }
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
approvePairing(code) {
|
|
146
|
+
const upper = code.toUpperCase();
|
|
147
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
148
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
149
|
+
delete this.config.pendingPairings[upper];
|
|
150
|
+
this.saveState();
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
async loadState() {
|
|
154
|
+
try {
|
|
155
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
156
|
+
if (s.p) this.config.pendingPairings = s.p;
|
|
157
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
158
|
+
} catch {}
|
|
159
|
+
}
|
|
160
|
+
async saveState() {
|
|
161
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
162
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
163
|
+
p: this.config.pendingPairings,
|
|
164
|
+
a: this.config.approvedPairings
|
|
165
|
+
}, { spaces: 2 });
|
|
166
|
+
}
|
|
167
|
+
isRunning() {
|
|
168
|
+
return this.running;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
exports.MessengerConnector = MessengerConnector;
|
|
@@ -0,0 +1,182 @@
|
|
|
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 http = require_chunk.__toESM(require("http"));
|
|
7
|
+
const https = require_chunk.__toESM(require("https"));
|
|
8
|
+
const events = require_chunk.__toESM(require("events"));
|
|
9
|
+
|
|
10
|
+
//#region extensions/mattermost/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "mattermost-state.json");
|
|
12
|
+
function mattermostApi(serverUrl, token, method, path$2, body) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const base = serverUrl.startsWith("https") ? serverUrl : `https://${serverUrl.replace(/^https?:\/\//, "")}`;
|
|
15
|
+
const url = new URL(path$2.startsWith("/") ? path$2 : "/" + path$2, base);
|
|
16
|
+
const payload = body ? JSON.stringify(body) : null;
|
|
17
|
+
const client = url.protocol === "https:" ? https.default : http.default;
|
|
18
|
+
const req = client.request({
|
|
19
|
+
hostname: url.hostname,
|
|
20
|
+
port: url.port || (url.protocol === "https:" ? 443 : 80),
|
|
21
|
+
path: url.pathname,
|
|
22
|
+
method,
|
|
23
|
+
headers: {
|
|
24
|
+
"Authorization": `Bearer ${token}`,
|
|
25
|
+
...payload ? {
|
|
26
|
+
"Content-Type": "application/json",
|
|
27
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
28
|
+
} : {}
|
|
29
|
+
}
|
|
30
|
+
}, (res) => {
|
|
31
|
+
let data = "";
|
|
32
|
+
res.on("data", (c) => data += c);
|
|
33
|
+
res.on("end", () => {
|
|
34
|
+
try {
|
|
35
|
+
const r = data ? JSON.parse(data) : {};
|
|
36
|
+
const ok = res.statusCode && res.statusCode >= 200 && res.statusCode < 300;
|
|
37
|
+
if (ok) resolve(r);
|
|
38
|
+
else reject(new Error(r.message || r.error || `HTTP ${res.statusCode}`));
|
|
39
|
+
} catch (e) {
|
|
40
|
+
reject(e || new Error("Parse error"));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
req.on("error", reject);
|
|
45
|
+
if (payload) req.write(payload);
|
|
46
|
+
req.end();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
var MattermostConnector = class extends events.EventEmitter {
|
|
50
|
+
config;
|
|
51
|
+
running = false;
|
|
52
|
+
constructor(config) {
|
|
53
|
+
super();
|
|
54
|
+
this.config = {
|
|
55
|
+
dmPolicy: "allowlist",
|
|
56
|
+
allowFrom: [],
|
|
57
|
+
approvedPairings: [],
|
|
58
|
+
pendingPairings: {},
|
|
59
|
+
...config
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async connect() {
|
|
63
|
+
const base = this.config.serverUrl.replace(/\/$/, "");
|
|
64
|
+
await mattermostApi(base, this.config.token, "GET", "/api/v4/users/me");
|
|
65
|
+
await this.loadState();
|
|
66
|
+
this.running = true;
|
|
67
|
+
console.log(chalk.default.green(` 🦅 Mattermost: connected to ${base}`));
|
|
68
|
+
this.emit("connected", { serverUrl: base });
|
|
69
|
+
}
|
|
70
|
+
disconnect() {
|
|
71
|
+
this.running = false;
|
|
72
|
+
}
|
|
73
|
+
/** Parse outgoing webhook payload (form-urlencoded or JSON) */
|
|
74
|
+
async handleWebhook(body, _opts) {
|
|
75
|
+
let params;
|
|
76
|
+
const trimmed = (body || "").trim();
|
|
77
|
+
if (trimmed.startsWith("{")) try {
|
|
78
|
+
params = JSON.parse(trimmed);
|
|
79
|
+
} catch {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
params = {};
|
|
84
|
+
for (const pair of body.split("&")) {
|
|
85
|
+
const eq = pair.indexOf("=");
|
|
86
|
+
const k = eq >= 0 ? decodeURIComponent(pair.slice(0, eq).replace(/\+/g, " ")) : decodeURIComponent(pair.replace(/\+/g, " "));
|
|
87
|
+
const v = eq >= 0 ? decodeURIComponent((pair.slice(eq + 1) || "").replace(/\+/g, " ")) : "";
|
|
88
|
+
if (k) params[k] = v;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const token = params.token;
|
|
92
|
+
if (token !== this.config.webhookToken) {
|
|
93
|
+
console.log(chalk.default.yellow(" ⚠ Mattermost: invalid webhook token"));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const channelId = params.channel_id;
|
|
97
|
+
const userId = params.user_id;
|
|
98
|
+
const userName = params.user_name || "";
|
|
99
|
+
let text = (params.text || "").trim();
|
|
100
|
+
const triggerWord = params.trigger_word || "";
|
|
101
|
+
if (triggerWord && text.startsWith(triggerWord)) text = text.slice(triggerWord.length).trim();
|
|
102
|
+
if (!channelId || !userId || !text) return;
|
|
103
|
+
const allowed = await this.checkDMPolicy(userId, text, channelId);
|
|
104
|
+
if (!allowed) return;
|
|
105
|
+
this.emit("message", {
|
|
106
|
+
id: params.post_id || `${channelId}-${Date.now()}`,
|
|
107
|
+
channelId: "mattermost",
|
|
108
|
+
from: userId,
|
|
109
|
+
chatId: channelId,
|
|
110
|
+
text,
|
|
111
|
+
timestamp: params.timestamp ? new Date(parseInt(params.timestamp) * 1e3).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
|
|
112
|
+
isDM: false,
|
|
113
|
+
userName
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async checkDMPolicy(userId, text, channelId) {
|
|
117
|
+
if (this.config.dmPolicy === "none") return false;
|
|
118
|
+
if (this.config.dmPolicy === "open") return true;
|
|
119
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
120
|
+
if (this.config.allowFrom.includes(userId)) return true;
|
|
121
|
+
await this.sendMessage(channelId, "🦅 HyperClaw: Not on allowlist.");
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (this.config.dmPolicy === "pairing") {
|
|
125
|
+
if (this.config.approvedPairings.includes(userId)) return true;
|
|
126
|
+
const upper = text.trim().toUpperCase();
|
|
127
|
+
if (this.config.pendingPairings[upper]) {
|
|
128
|
+
this.config.approvedPairings.push(userId);
|
|
129
|
+
delete this.config.pendingPairings[upper];
|
|
130
|
+
await this.saveState();
|
|
131
|
+
await this.sendMessage(channelId, "🦅 Paired!");
|
|
132
|
+
this.emit("pairing:approved", {
|
|
133
|
+
userId,
|
|
134
|
+
channelId: "mattermost"
|
|
135
|
+
});
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
139
|
+
this.config.pendingPairings[code] = userId;
|
|
140
|
+
await this.saveState();
|
|
141
|
+
await this.sendMessage(channelId, `🦅 Pairing code: ${code}\nApprove: hyperclaw pairing approve mattermost ${code}`);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
async sendMessage(channelId, text) {
|
|
147
|
+
const base = this.config.serverUrl.replace(/\/$/, "");
|
|
148
|
+
const chunks = text.match(/.{1,4000}/gs) || [text];
|
|
149
|
+
for (const chunk of chunks) await mattermostApi(base, this.config.token, "POST", "/api/v4/posts", {
|
|
150
|
+
channel_id: channelId,
|
|
151
|
+
message: chunk.slice(0, 16383)
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
approvePairing(code) {
|
|
155
|
+
const upper = code.toUpperCase();
|
|
156
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
157
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
158
|
+
delete this.config.pendingPairings[upper];
|
|
159
|
+
this.saveState();
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
async loadState() {
|
|
163
|
+
try {
|
|
164
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
165
|
+
if (s.pendingPairings) this.config.pendingPairings = s.pendingPairings;
|
|
166
|
+
if (s.approvedPairings) this.config.approvedPairings = s.approvedPairings;
|
|
167
|
+
} catch {}
|
|
168
|
+
}
|
|
169
|
+
async saveState() {
|
|
170
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
171
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
172
|
+
pendingPairings: this.config.pendingPairings,
|
|
173
|
+
approvedPairings: this.config.approvedPairings
|
|
174
|
+
}, { spaces: 2 });
|
|
175
|
+
}
|
|
176
|
+
isRunning() {
|
|
177
|
+
return this.running;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
exports.MattermostConnector = MattermostConnector;
|