hyperclaw 5.0.0 → 5.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/LICENSE +2 -1
- package/README.md +449 -99
- package/dist/a2ui-protocol-Gzm29Gaw.js +75 -0
- package/dist/agents-routing-Biy5ew4a.js +4 -0
- package/dist/agents-routing-CL3HQNoM.js +327 -0
- package/dist/api-keys-guide-CGn5BSF7.js +149 -0
- package/dist/api-keys-guide-ChbThbPj.js +149 -0
- package/dist/audit-BJohI_vC.js +441 -0
- package/dist/audit-NPIMmOSq.js +441 -0
- package/dist/bounty-tools-BUqUKjt0.js +211 -0
- package/dist/bounty-tools-CY_i91DU.js +211 -0
- package/dist/browser-tools-CxJY6pAn.js +5 -0
- package/dist/browser-tools-JZ9ji6AW.js +179 -0
- package/dist/chat-qVuqhlPu.js +258 -0
- package/dist/claw-tasks-B-8RRMdq.js +80 -0
- package/dist/claw-tasks-Cyzdbhz_.js +80 -0
- package/dist/connector-1x1rCBHz.js +162 -0
- package/dist/connector-B4jeCULG.js +305 -0
- package/dist/connector-B7qngfkT.js +286 -0
- package/dist/connector-B8BK0GBo.js +531 -0
- package/dist/connector-BE9eJs8-.js +182 -0
- package/dist/connector-BEe-DTGQ.js +189 -0
- package/dist/connector-BU7p5ZgB.js +167 -0
- package/dist/connector-BUzzq7Ij.js +568 -0
- package/dist/connector-BpDqLgnW.js +419 -0
- package/dist/connector-BpW88ut2.js +189 -0
- package/dist/connector-Bxv-gy8U.js +167 -0
- package/dist/connector-Bz14zcJv.js +213 -0
- package/dist/connector-C1zP5-5q.js +85 -0
- package/dist/connector-CAcpcovF.js +498 -0
- package/dist/connector-CJgVjS58.js +181 -0
- package/dist/connector-Cf53D6qV.js +425 -0
- package/dist/connector-CyHmlbNz.js +508 -0
- package/dist/connector-D22mJGVu.js +340 -0
- package/dist/connector-D6RtMmlL.js +225 -0
- package/dist/connector-D9EnT8A4.js +280 -0
- package/dist/connector-DNDwIh37.js +239 -0
- package/dist/connector-Di27MeO4.js +350 -0
- package/dist/connector-Do0BPiHt.js +194 -0
- package/dist/connector-DvLwOfJy.js +192 -0
- package/dist/connector-DvU83NSq.js +181 -0
- package/dist/connector-DxskpDc_.js +173 -0
- package/dist/connector-byy3eISx.js +552 -0
- package/dist/connector-vV89hsyd.js +218 -0
- package/dist/cost-tracker-Ca1UPZ33.js +103 -0
- package/dist/cost-tracker-fnaj_6M9.js +103 -0
- package/dist/credentials-store-BxijEirw.js +77 -0
- package/dist/credentials-store-CA8UtK0T.js +77 -0
- package/dist/credentials-store-CPkVO6-z.js +4 -0
- package/dist/credentials-store-Cm7DH-kh.js +4 -0
- package/dist/cron-tasks-L0mz1yyU.js +82 -0
- package/dist/cron-tasks-_pqQCmxc.js +82 -0
- package/dist/daemon-7ViroziB.js +5 -0
- package/dist/daemon-BfyKmZhr.js +318 -0
- package/dist/daemon-CNyunwkR.js +5 -0
- package/dist/daemon-CindY8OK.js +318 -0
- package/dist/delivery-DVHmv1IR.js +4 -0
- package/dist/delivery-DgiZcJBp.js +4 -0
- package/dist/delivery-DpMX0Yyc.js +95 -0
- package/dist/delivery-otAU4alM.js +95 -0
- package/dist/destructive-gate-CA0DtA5K.js +101 -0
- package/dist/destructive-gate-DZt71UZR.js +101 -0
- package/dist/developer-keys-Cnd1kswV.js +127 -0
- package/dist/developer-keys-DENo3ZA6.js +8 -0
- package/dist/doctor-Dgjoc3DG.js +230 -0
- package/dist/doctor-RwsOhtAl.js +6 -0
- package/dist/engine-B0kLfRL0.js +256 -0
- package/dist/engine-BJUpRUOv.js +7 -0
- package/dist/engine-D_VeoZHw.js +305 -0
- package/dist/engine-JjRnhlsE.js +7 -0
- package/dist/env-resolve-17ekEU6p.js +10 -0
- package/dist/env-resolve-BFJXWl94.js +115 -0
- package/dist/env-resolve-Z2XF6leB.js +115 -0
- package/dist/env-resolve-bDYssfih.js +10 -0
- package/dist/extraction-tools-DbxnxIco.js +5 -0
- package/dist/extraction-tools-Dg7AHS35.js +91 -0
- package/dist/form_data-CGAy4HE0.js +8657 -0
- package/dist/gmail-watch-setup-C3uSWznp.js +40 -0
- package/dist/health-DUjluWHQ.js +6 -0
- package/dist/health-DVfkpUQW.js +152 -0
- package/dist/heartbeat-engine-CrgL4mrP.js +83 -0
- package/dist/heartbeat-engine-Ut6pXBD6.js +83 -0
- package/dist/hub-9LaKnLjY.js +6 -0
- package/dist/hub-BO6bj8Yj.js +515 -0
- package/dist/hub-Bu52YZqW.js +6 -0
- package/dist/hub-CfwUz9YW.js +515 -0
- package/dist/hyperclawbot-BrcoYLOp.js +505 -0
- package/dist/hyperclawbot-CBiDSKsa.js +505 -0
- package/dist/inference-0mlFQqIm.js +922 -0
- package/dist/inference-DHR82Gh7.js +6 -0
- package/dist/inference-DhA8jpfH.js +2692 -0
- package/dist/inference-SzqFe_nk.js +6 -0
- package/dist/knowledge-graph-BrYpSgxW.js +131 -0
- package/dist/knowledge-graph-DE5lSF02.js +131 -0
- package/dist/loader-9JqY6Nlq.js +4 -0
- package/dist/loader-BkDi8MD9.js +400 -0
- package/dist/loader-Cjdd1kw4.js +400 -0
- package/dist/loader-DI2qDRPC.js +4 -0
- package/dist/logger-Cp8wC7F8.js +83 -0
- package/dist/logger-DCT2l9GV.js +83 -0
- package/dist/manager-3cq3DydI.js +4 -0
- package/dist/manager-B2Gls5RG.js +218 -0
- package/dist/manager-BUrFrPuq.js +117 -0
- package/dist/manager-Bi9UYyVR.js +105 -0
- package/dist/manager-Biz9ixWJ.js +40 -0
- package/dist/manager-CBUHJiY7.js +6 -0
- package/dist/manager-CVLLaKmq.js +218 -0
- package/dist/manager-CWNSML5D.js +117 -0
- package/dist/manager-SJe9gt-q.js +4 -0
- package/dist/mcp-CUoTCMw-.js +139 -0
- package/dist/mcp-loader-BIz-450x.js +94 -0
- package/dist/mcp-loader-CvxRDtPC.js +94 -0
- package/dist/memory-OL77OMOr.js +270 -0
- package/dist/memory-auto-CpQHZlEJ.js +306 -0
- package/dist/memory-auto-D-L2q21G.js +306 -0
- package/dist/memory-auto-DTcy5VBy.js +5 -0
- package/dist/memory-auto-Z6LCf-iK.js +5 -0
- package/dist/memory-gUi4VaIf.js +4 -0
- package/dist/memory-integration-B8RSN4pr.js +91 -0
- package/dist/memory-integration-g2vxwgoE.js +91 -0
- package/dist/moltbook-B-40gQOL.js +81 -0
- package/dist/moltbook-Cl8cQfxJ.js +81 -0
- package/dist/node-TWxRm84k.js +222 -0
- package/dist/nodes-registry-C9dCFwjh.js +52 -0
- package/dist/nodes-registry-DKRtsbNg.js +52 -0
- package/dist/oauth-flow-CeaaGAz0.js +150 -0
- package/dist/oauth-flow-JCfporKq.js +150 -0
- package/dist/oauth-provider-4R0EJlsT.js +110 -0
- package/dist/oauth-provider-B4dzn56l.js +110 -0
- package/dist/observability-CDZmeHfa.js +89 -0
- package/dist/observability-nZ3CBIxG.js +89 -0
- package/dist/onboard-BBBWcfhp.js +10 -0
- package/dist/onboard-BVOtKQdh.js +3641 -0
- package/dist/onboard-Bw28IRQ3.js +4070 -0
- package/dist/onboard-CGNIw27w.js +11 -0
- package/dist/orchestrator-BovkM63z.js +6 -0
- package/dist/orchestrator-CcKx1Ovk.js +189 -0
- package/dist/orchestrator-DSbpkP1X.js +189 -0
- package/dist/orchestrator-DcFfDLTX.js +6 -0
- package/dist/osint-B4_m3VHQ.js +277 -0
- package/dist/osint-B6BZKQAD.js +277 -0
- package/dist/pairing-B6RArWhD.js +196 -0
- package/dist/pairing-BsQ08DLq.js +4 -0
- package/dist/pc-access-B0KocJNe.js +819 -0
- package/dist/pc-access-DkzmugZ7.js +8 -0
- package/dist/pending-approval-BgNjjuI2.js +22 -0
- package/dist/pending-approval-C_HkX1QL.js +22 -0
- package/dist/providers-DxiamZSL.js +5 -0
- package/dist/providers-Dy15rDb7.js +657 -0
- package/dist/reminders-store-CzUY0zYx.js +58 -0
- package/dist/renderer-ANNfXsHn.js +225 -0
- package/dist/rules-BSQwwAYC.js +103 -0
- package/dist/run-main.js +142 -132
- package/dist/runner-BHRSOPEU.js +1271 -0
- package/dist/runner-CJFJUtPm.js +1271 -0
- package/dist/sdk/index.js +2 -2
- package/dist/sdk/index.mjs +2 -2
- package/dist/security--oQObeJO.js +4 -0
- package/dist/security-wBOg0TA8.js +73 -0
- package/dist/server-Brl_HQUB.js +1255 -0
- package/dist/server-CbTTpB5m.js +1255 -0
- package/dist/server-DP_bPzvI.js +4 -0
- package/dist/server-DhfipkwN.js +4 -0
- package/dist/session-store-B09r5HgB.js +5 -0
- package/dist/session-store-DCTQIVur.js +113 -0
- package/dist/sessions-tools-BdlN6Pb6.js +95 -0
- package/dist/sessions-tools-JVLDKSJ_.js +5 -0
- package/dist/skill-loader-B5oeliGu.js +7 -0
- package/dist/skill-loader-Wf3brNOj.js +160 -0
- package/dist/skill-runtime-BGlvly2s.js +102 -0
- package/dist/skill-runtime-BXWd-Ktf.js +102 -0
- package/dist/skill-runtime-DhL2T76p.js +5 -0
- package/dist/skill-runtime-jgklm02e.js +5 -0
- package/dist/src-BbPa6Q8p.js +63 -0
- package/dist/src-BeXtfkK2.js +458 -0
- package/dist/src-Bhybpk1J.js +63 -0
- package/dist/src-CGQjRI4N.js +20 -0
- package/dist/src-DMJ4-uqk.js +458 -0
- package/dist/sub-agent-tools-CmE345s_.js +39 -0
- package/dist/sub-agent-tools-DHY-4WWM.js +39 -0
- package/dist/theme-D0smfC_l.js +8 -0
- package/dist/theme-DajRRZbA.js +180 -0
- package/dist/tool-policy-DZvF8xlQ.js +189 -0
- package/dist/tool-policy-DgNqFWYn.js +189 -0
- package/dist/tts-elevenlabs-C06nUxMK.js +61 -0
- package/dist/tts-elevenlabs-JeFaGNJU.js +61 -0
- package/dist/update-check-BVEqHhFY.js +83 -0
- package/dist/update-check-w4XuxVl7.js +81 -0
- package/dist/vision-JOtOS1Br.js +121 -0
- package/dist/vision-fky3elEo.js +121 -0
- package/dist/vision-tools-C8B3776g.js +5 -0
- package/dist/vision-tools-CB28ZCO_.js +5 -0
- package/dist/vision-tools-dwn9p4el.js +51 -0
- package/dist/vision-tools-vPPwQ-0N.js +51 -0
- package/dist/voice-transcription-B6RtplmN.js +138 -0
- package/dist/voice-transcription-DBo5hXmu.js +138 -0
- package/dist/website-watch-tools-B-jRAeTe.js +139 -0
- package/dist/website-watch-tools-BC9xAL67.js +5 -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,239 @@
|
|
|
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 ws = require_chunk.__toESM(require("ws"));
|
|
7
|
+
const events = require_chunk.__toESM(require("events"));
|
|
8
|
+
|
|
9
|
+
//#region extensions/twitch/src/connector.ts
|
|
10
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "twitch-state.json");
|
|
11
|
+
const TWITCH_IRC_WS = "wss://irc-ws.chat.twitch.tv:443";
|
|
12
|
+
var TwitchConnector = class extends events.EventEmitter {
|
|
13
|
+
config;
|
|
14
|
+
ws = null;
|
|
15
|
+
running = false;
|
|
16
|
+
reconnectDelay = 1e3;
|
|
17
|
+
pingInterval = null;
|
|
18
|
+
constructor(config) {
|
|
19
|
+
super();
|
|
20
|
+
this.config = {
|
|
21
|
+
commandPrefix: "!",
|
|
22
|
+
whispers: true,
|
|
23
|
+
dmPolicy: "pairing",
|
|
24
|
+
allowFrom: [],
|
|
25
|
+
approvedPairings: [],
|
|
26
|
+
pendingPairings: {},
|
|
27
|
+
modsBypass: true,
|
|
28
|
+
...config
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async connect() {
|
|
32
|
+
await this.loadState();
|
|
33
|
+
await this.doConnect();
|
|
34
|
+
}
|
|
35
|
+
doConnect() {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const ws$1 = new ws.WebSocket(TWITCH_IRC_WS);
|
|
38
|
+
this.ws = ws$1;
|
|
39
|
+
ws$1.once("open", () => {
|
|
40
|
+
ws$1.send("CAP REQ :twitch.tv/membership twitch.tv/tags twitch.tv/commands");
|
|
41
|
+
ws$1.send(`PASS ${this.config.oauthToken}`);
|
|
42
|
+
ws$1.send(`NICK ${this.config.username}`);
|
|
43
|
+
for (const ch of this.config.channels) ws$1.send(`JOIN #${ch.replace(/^#/, "").toLowerCase()}`);
|
|
44
|
+
this.running = true;
|
|
45
|
+
this.reconnectDelay = 1e3;
|
|
46
|
+
this.pingInterval = setInterval(() => ws$1.send("PING :tmi.twitch.tv"), 6e4);
|
|
47
|
+
console.log(chalk.default.green(` 🦅 Twitch: connected as ${this.config.username}, joined ${this.config.channels.join(", ")}`));
|
|
48
|
+
this.emit("connected", { username: this.config.username });
|
|
49
|
+
resolve();
|
|
50
|
+
});
|
|
51
|
+
ws$1.on("message", async (raw) => {
|
|
52
|
+
const lines = raw.toString().split("\r\n").filter(Boolean);
|
|
53
|
+
for (const line of lines) await this.handleLine(line);
|
|
54
|
+
});
|
|
55
|
+
ws$1.on("close", () => {
|
|
56
|
+
if (this.pingInterval) {
|
|
57
|
+
clearInterval(this.pingInterval);
|
|
58
|
+
this.pingInterval = null;
|
|
59
|
+
}
|
|
60
|
+
if (this.running) {
|
|
61
|
+
console.log(chalk.default.yellow(` ⚠ Twitch: disconnected, reconnecting in ${this.reconnectDelay}ms`));
|
|
62
|
+
setTimeout(() => {
|
|
63
|
+
this.reconnectDelay = Math.min(this.reconnectDelay * 2, 3e4);
|
|
64
|
+
this.doConnect();
|
|
65
|
+
}, this.reconnectDelay);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
ws$1.on("error", (err) => {
|
|
69
|
+
console.log(chalk.default.yellow(` ⚠ Twitch: ${err.message}`));
|
|
70
|
+
if (!this.running) reject(err);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
disconnect() {
|
|
75
|
+
this.running = false;
|
|
76
|
+
if (this.pingInterval) {
|
|
77
|
+
clearInterval(this.pingInterval);
|
|
78
|
+
this.pingInterval = null;
|
|
79
|
+
}
|
|
80
|
+
this.ws?.close();
|
|
81
|
+
this.ws = null;
|
|
82
|
+
}
|
|
83
|
+
async handleLine(line) {
|
|
84
|
+
if (line === "PING :tmi.twitch.tv") {
|
|
85
|
+
this.ws?.send("PONG :tmi.twitch.tv");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const tags = this.parseTags(line);
|
|
89
|
+
const parsed = this.parseIRC(line);
|
|
90
|
+
if (!parsed) return;
|
|
91
|
+
const { command, prefix, params, trailing } = parsed;
|
|
92
|
+
const username = prefix?.split("!")[0]?.toLowerCase() || "";
|
|
93
|
+
if (username === this.config.username.toLowerCase()) return;
|
|
94
|
+
if (command === "PRIVMSG") {
|
|
95
|
+
const channel = params[0];
|
|
96
|
+
const text = trailing || "";
|
|
97
|
+
const isWhisper = false;
|
|
98
|
+
if (this.config.commandPrefix && !text.startsWith(this.config.commandPrefix) && !isWhisper) return;
|
|
99
|
+
const body = this.config.commandPrefix ? text.slice(this.config.commandPrefix.length).trim() : text;
|
|
100
|
+
if (!body) return;
|
|
101
|
+
const isMod = tags["mod"] === "1" || tags["badges"]?.includes("broadcaster");
|
|
102
|
+
const allowed = await this.checkPolicy(username, channel, body, isMod);
|
|
103
|
+
if (!allowed) return;
|
|
104
|
+
this.emit("message", {
|
|
105
|
+
id: tags["id"] || `twitch-${Date.now()}`,
|
|
106
|
+
channelId: "twitch",
|
|
107
|
+
from: username,
|
|
108
|
+
displayName: tags["display-name"] || username,
|
|
109
|
+
chatId: channel,
|
|
110
|
+
text: body,
|
|
111
|
+
timestamp: tags["tmi-sent-ts"] ? new Date(Number(tags["tmi-sent-ts"])).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
|
|
112
|
+
isDM: false,
|
|
113
|
+
isMod
|
|
114
|
+
});
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (command === "WHISPER" && this.config.whispers) {
|
|
118
|
+
const text = trailing || "";
|
|
119
|
+
if (!text) return;
|
|
120
|
+
const allowed = await this.checkPolicy(username, "whisper", text, false);
|
|
121
|
+
if (!allowed) return;
|
|
122
|
+
this.emit("message", {
|
|
123
|
+
id: `twitch-whisper-${Date.now()}`,
|
|
124
|
+
channelId: "twitch",
|
|
125
|
+
from: username,
|
|
126
|
+
displayName: tags["display-name"] || username,
|
|
127
|
+
chatId: `whisper:${username}`,
|
|
128
|
+
text,
|
|
129
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
130
|
+
isDM: true,
|
|
131
|
+
isMod: false
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async checkPolicy(username, chatId, text, isMod) {
|
|
136
|
+
if (this.config.dmPolicy === "none") return false;
|
|
137
|
+
if (this.config.dmPolicy === "open") return true;
|
|
138
|
+
if (this.config.modsBypass && isMod) return true;
|
|
139
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
140
|
+
if (this.config.allowFrom.includes(username)) return true;
|
|
141
|
+
await this.sendChat(chatId, `@${username} You are not on the allowlist.`);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (this.config.dmPolicy === "pairing") {
|
|
145
|
+
if (this.config.approvedPairings.includes(username)) return true;
|
|
146
|
+
const upper = text.trim().toUpperCase();
|
|
147
|
+
if (this.config.pendingPairings[upper]) {
|
|
148
|
+
this.config.approvedPairings.push(username);
|
|
149
|
+
delete this.config.pendingPairings[upper];
|
|
150
|
+
await this.saveState();
|
|
151
|
+
await this.sendChat(chatId, `@${username} Paired! You can now talk to the assistant.`);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
155
|
+
this.config.pendingPairings[code] = username;
|
|
156
|
+
await this.saveState();
|
|
157
|
+
await this.sendChat(chatId, `@${username} Pairing required. Code: ${code} — approve: hyperclaw pairing approve twitch ${code}`);
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
async sendChat(channel, text) {
|
|
163
|
+
if (!this.ws || this.ws.readyState !== ws.WebSocket.OPEN) return;
|
|
164
|
+
const ch = channel.startsWith("#") ? channel : `#${channel}`;
|
|
165
|
+
const chunks = text.match(/.{1,490}/g) || [text];
|
|
166
|
+
for (const chunk of chunks) {
|
|
167
|
+
this.ws.send(`PRIVMSG ${ch} :${chunk}`);
|
|
168
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async sendWhisper(username, text) {
|
|
172
|
+
if (!this.ws || this.ws.readyState !== ws.WebSocket.OPEN) return;
|
|
173
|
+
const ch = `#${this.config.channels[0]?.replace(/^#/, "") || this.config.username}`;
|
|
174
|
+
this.ws.send(`PRIVMSG ${ch} :/w ${username} ${text}`);
|
|
175
|
+
}
|
|
176
|
+
approvePairing(code) {
|
|
177
|
+
const upper = code.toUpperCase();
|
|
178
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
179
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
180
|
+
delete this.config.pendingPairings[upper];
|
|
181
|
+
this.saveState();
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
parseTags(line) {
|
|
185
|
+
if (!line.startsWith("@")) return {};
|
|
186
|
+
const tagStr = line.slice(1, line.indexOf(" "));
|
|
187
|
+
const tags = {};
|
|
188
|
+
for (const part of tagStr.split(";")) {
|
|
189
|
+
const [k, v] = part.split("=");
|
|
190
|
+
if (k) tags[k] = v || "";
|
|
191
|
+
}
|
|
192
|
+
return tags;
|
|
193
|
+
}
|
|
194
|
+
parseIRC(line) {
|
|
195
|
+
let rest = line.startsWith("@") ? line.slice(line.indexOf(" ") + 1) : line;
|
|
196
|
+
let prefix;
|
|
197
|
+
if (rest.startsWith(":")) {
|
|
198
|
+
const spaceIdx = rest.indexOf(" ");
|
|
199
|
+
prefix = rest.slice(1, spaceIdx);
|
|
200
|
+
rest = rest.slice(spaceIdx + 1);
|
|
201
|
+
}
|
|
202
|
+
const trailingIdx = rest.indexOf(" :");
|
|
203
|
+
let trailing;
|
|
204
|
+
if (trailingIdx !== -1) {
|
|
205
|
+
trailing = rest.slice(trailingIdx + 2);
|
|
206
|
+
rest = rest.slice(0, trailingIdx);
|
|
207
|
+
}
|
|
208
|
+
const parts = rest.trim().split(" ");
|
|
209
|
+
const command = parts[0];
|
|
210
|
+
const params = parts.slice(1).filter(Boolean);
|
|
211
|
+
if (!command) return null;
|
|
212
|
+
return {
|
|
213
|
+
command,
|
|
214
|
+
prefix,
|
|
215
|
+
params,
|
|
216
|
+
trailing
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async loadState() {
|
|
220
|
+
try {
|
|
221
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
222
|
+
if (s.p) this.config.pendingPairings = s.p;
|
|
223
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
224
|
+
} catch {}
|
|
225
|
+
}
|
|
226
|
+
async saveState() {
|
|
227
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
228
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
229
|
+
p: this.config.pendingPairings,
|
|
230
|
+
a: this.config.approvedPairings
|
|
231
|
+
}, { spaces: 2 });
|
|
232
|
+
}
|
|
233
|
+
isRunning() {
|
|
234
|
+
return this.running;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
exports.TwitchConnector = TwitchConnector;
|