hyperclaw 4.0.2 → 5.0.0
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 +54 -3
- package/dist/a2ui-protocol-CfBI44-Q.js +75 -0
- package/dist/agents-routing-ChHiZp36.js +327 -0
- package/dist/agents-routing-ChqZ6l2S.js +4 -0
- package/dist/api-keys-guide-BCcOl0Q7.js +149 -0
- package/dist/audit-BaIiyWFu.js +441 -0
- package/dist/bounty-tools-DWudyZie.js +211 -0
- package/dist/browser-tools-BsTeGMnX.js +5 -0
- package/dist/browser-tools-D8_rLe2p.js +179 -0
- package/dist/claw-tasks-CgTsiNE8.js +80 -0
- package/dist/connector-5N0-X_xs.js +194 -0
- package/dist/connector-B3v0qcXg.js +425 -0
- package/dist/connector-B8R3iBY1.js +280 -0
- package/dist/connector-BAM-08NN.js +189 -0
- package/dist/connector-BC8FIVu4.js +181 -0
- package/dist/connector-BDmwwaVc.js +213 -0
- package/dist/connector-BGjbBy69.js +225 -0
- package/dist/connector-BO2SRzfG.js +218 -0
- package/dist/connector-BfXky0L3.js +167 -0
- package/dist/connector-BiiSJpx3.js +192 -0
- package/dist/connector-BnDmIhIu.js +85 -0
- package/dist/connector-C1HSoUyk.js +189 -0
- package/dist/connector-CKQHZOXg.js +568 -0
- package/dist/connector-CRl-iidy.js +239 -0
- package/dist/connector-Ci9glMD-.js +340 -0
- package/dist/connector-CjtZIEDj.js +181 -0
- package/dist/connector-Ck6JtOsX.js +531 -0
- package/dist/connector-D8Kelee0.js +286 -0
- package/dist/connector-DAnRJ0oP.js +162 -0
- package/dist/connector-DXTp5PE8.js +508 -0
- package/dist/connector-Dih6dUPP.js +173 -0
- package/dist/connector-DqTH_tPX.js +182 -0
- package/dist/connector-DrnEiiyP.js +419 -0
- package/dist/connector-DtR5GGTX.js +167 -0
- package/dist/connector-Tky_qS_K.js +350 -0
- package/dist/connector-ZSc3oTTy.js +305 -0
- package/dist/connector-sW5yhU1m.js +498 -0
- package/dist/connector-u3ICd3Ic.js +552 -0
- package/dist/cost-tracker-DD9wtWsr.js +103 -0
- package/dist/credentials-store-C6ir0Dae.js +4 -0
- package/dist/credentials-store-H13LqOwJ.js +77 -0
- package/dist/cron-tasks-Bli7Kzd2.js +82 -0
- package/dist/daemon-Bg4GtCmc.js +318 -0
- package/dist/daemon-DhmwY8k4.js +5 -0
- package/dist/delivery-BmIYy9VQ.js +4 -0
- package/dist/delivery-pWUPBp1F.js +95 -0
- package/dist/destructive-gate-D6vWOdEl.js +101 -0
- package/dist/developer-keys-CPWT7Q6S.js +8 -0
- package/dist/developer-keys-DrrcUqFa.js +127 -0
- package/dist/doctor-BvCe8BBk.js +230 -0
- package/dist/doctor-CxyPLYsJ.js +6 -0
- package/dist/engine-CEDSqXfw.js +256 -0
- package/dist/engine-Da4JMNpI.js +7 -0
- package/dist/env-resolve-CiXbWYwe.js +10 -0
- package/dist/env-resolve-CmGWhWXJ.js +115 -0
- package/dist/extraction-tools-HOZstZ0y.js +91 -0
- package/dist/extraction-tools-m4lmAv7l.js +5 -0
- package/dist/form_data-Cz040rio.js +8657 -0
- package/dist/gmail-watch-setup-Du7DVV7S.js +40 -0
- package/dist/health-B-asI__D.js +6 -0
- package/dist/health-Ds2YlpTB.js +152 -0
- package/dist/heartbeat-engine-BYT5ayQH.js +83 -0
- package/dist/hub-D0XwdjM-.js +515 -0
- package/dist/hub-LiD5Iztb.js +6 -0
- package/dist/hyperclawbot-zvczQgKx.js +505 -0
- package/dist/inference-BKVkBREb.js +6 -0
- package/dist/inference-DCXH4Q3x.js +922 -0
- package/dist/knowledge-graph-iBG76fvm.js +131 -0
- package/dist/loader-CC45xGpC.js +4 -0
- package/dist/loader-CnEdOyjT.js +400 -0
- package/dist/logger-ybOp7VOC.js +83 -0
- package/dist/manager-03ipO9R0.js +105 -0
- package/dist/manager-BpDfbDjg.js +117 -0
- package/dist/manager-Bxl0sqlh.js +4 -0
- package/dist/manager-CrVDn6eN.js +6 -0
- package/dist/manager-FCgF1plu.js +218 -0
- package/dist/manager-rgCsaWT1.js +40 -0
- package/dist/mcp-CfoSU4Uz.js +139 -0
- package/dist/mcp-loader-DkRBsLpk.js +94 -0
- package/dist/memory-BlHL7JCO.js +4 -0
- package/dist/memory-DsS_eFvJ.js +270 -0
- package/dist/memory-auto-BkvtSFUw.js +5 -0
- package/dist/memory-auto-Bnz_-1wP.js +306 -0
- package/dist/memory-integration-cSYkZyEo.js +91 -0
- package/dist/moltbook-BtLDZTfM.js +81 -0
- package/dist/node-Dw2Gi-cP.js +222 -0
- package/dist/nodes-registry-B8dmrlLv.js +52 -0
- package/dist/oauth-flow-DQPvMHRH.js +150 -0
- package/dist/oauth-provider-Uo4Nib_c.js +110 -0
- package/dist/observability-BV-Yx0V9.js +89 -0
- package/dist/onboard-0WoDxbv_.js +10 -0
- package/dist/onboard-BXNXCQp4.js +4070 -0
- package/dist/orchestrator-DmnEvMaL.js +189 -0
- package/dist/orchestrator-RI3bpqqc.js +6 -0
- package/dist/pairing-6iM27aD8.js +196 -0
- package/dist/pairing-dGoiGepK.js +4 -0
- package/dist/pc-access-CgCsYrpt.js +8 -0
- package/dist/pc-access-_iH2aorG.js +819 -0
- package/dist/pending-approval-CUXjysAo.js +22 -0
- package/dist/reminders-store-Drjed_-h.js +58 -0
- package/dist/renderer-BVQrd0_g.js +225 -0
- package/dist/rules-BE4GV6cV.js +103 -0
- package/dist/run-main.js +1607 -443
- package/dist/runner-DatMMYYE.js +1271 -0
- package/dist/sdk/index.js +2 -2
- package/dist/sdk/index.mjs +2 -2
- package/dist/security-BqNyT4ID.js +4 -0
- package/dist/security-tpgqPWWH.js +73 -0
- package/dist/server-D4wVHiX9.js +4 -0
- package/dist/server-Dh3JlBFB.js +1255 -0
- package/dist/session-store-BUiPz0Vv.js +5 -0
- package/dist/session-store-is4B6qmD.js +113 -0
- package/dist/sessions-tools-CbUTFe4i.js +5 -0
- package/dist/sessions-tools-CeqD7iil.js +95 -0
- package/dist/skill-loader-BaNLVmJy.js +7 -0
- package/dist/skill-loader-HgpF6Vqs.js +159 -0
- package/dist/skill-runtime-CJN24QPW.js +102 -0
- package/dist/skill-runtime-w1ig_lcw.js +5 -0
- package/dist/src-BxPHKO5x.js +63 -0
- package/dist/src-DIc-L2IG.js +20 -0
- package/dist/src-g_rNx5rh.js +458 -0
- package/dist/sub-agent-tools-CHQoHz9c.js +39 -0
- package/dist/theme-DcxwcUgZ.js +180 -0
- package/dist/theme-cx0fkgWC.js +8 -0
- package/dist/tool-policy-CNT-mF2Z.js +189 -0
- package/dist/tts-elevenlabs-BRosZv-f.js +61 -0
- package/dist/update-check-C2Dz85wJ.js +81 -0
- package/dist/vision-BMmiIKy7.js +121 -0
- package/dist/vision-tools-DVuYc17I.js +51 -0
- package/dist/vision-tools-U3YC4L-g.js +5 -0
- package/dist/voice-transcription-B555DbWR.js +138 -0
- package/dist/website-watch-tools-DFMrJU-R.js +139 -0
- package/dist/website-watch-tools-Du3W5sN7.js +5 -0
- package/package.json +1 -1
|
@@ -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 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/viber/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "viber-state.json");
|
|
12
|
+
function viberReq(token, endpoint, body) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const payload = JSON.stringify(body);
|
|
15
|
+
const req = https.default.request({
|
|
16
|
+
hostname: "chatapi.viber.com",
|
|
17
|
+
port: 443,
|
|
18
|
+
path: `/pa${endpoint}`,
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: {
|
|
21
|
+
"X-Viber-Auth-Token": 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.status !== 0) reject(new Error(r.status_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 ViberConnector = class extends events.EventEmitter {
|
|
44
|
+
config;
|
|
45
|
+
running = false;
|
|
46
|
+
constructor(config) {
|
|
47
|
+
super();
|
|
48
|
+
this.config = {
|
|
49
|
+
dmPolicy: "allowlist",
|
|
50
|
+
allowFrom: [],
|
|
51
|
+
approvedPairings: [],
|
|
52
|
+
pendingPairings: {},
|
|
53
|
+
...config
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async connect() {
|
|
57
|
+
await viberReq(this.config.authToken, "/set_webhook", {
|
|
58
|
+
url: this.config.webhookUrl,
|
|
59
|
+
event_types: [
|
|
60
|
+
"delivered",
|
|
61
|
+
"seen",
|
|
62
|
+
"failed",
|
|
63
|
+
"subscribed",
|
|
64
|
+
"unsubscribed",
|
|
65
|
+
"conversation_started"
|
|
66
|
+
],
|
|
67
|
+
send_name: true
|
|
68
|
+
});
|
|
69
|
+
const info = await viberReq(this.config.authToken, "/get_account_info", {});
|
|
70
|
+
await this.loadState();
|
|
71
|
+
this.running = true;
|
|
72
|
+
console.log(chalk.default.green(` 🦅 Viber: ${info.name || this.config.botName} connected`));
|
|
73
|
+
this.emit("connected", { name: info.name });
|
|
74
|
+
}
|
|
75
|
+
disconnect() {
|
|
76
|
+
this.running = false;
|
|
77
|
+
}
|
|
78
|
+
verifySignature(body, signature) {
|
|
79
|
+
const expected = crypto.default.createHmac("sha256", this.config.authToken).update(body).digest("hex");
|
|
80
|
+
try {
|
|
81
|
+
return crypto.default.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async handleWebhook(body, signature) {
|
|
87
|
+
if (signature && !this.verifySignature(body, signature)) {
|
|
88
|
+
console.log(chalk.default.yellow(" ⚠ Viber: invalid signature"));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
let payload;
|
|
92
|
+
try {
|
|
93
|
+
payload = JSON.parse(body);
|
|
94
|
+
} catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (payload.event === "message") {
|
|
98
|
+
const userId = payload.sender?.id;
|
|
99
|
+
const text = payload.message?.text;
|
|
100
|
+
if (!userId || !text) return;
|
|
101
|
+
const allowed = await this.checkDMPolicy(userId, text);
|
|
102
|
+
if (!allowed) return;
|
|
103
|
+
this.emit("message", {
|
|
104
|
+
id: payload.message_token?.toString(),
|
|
105
|
+
channelId: "viber",
|
|
106
|
+
from: userId,
|
|
107
|
+
chatId: userId,
|
|
108
|
+
text,
|
|
109
|
+
senderName: payload.sender?.name,
|
|
110
|
+
timestamp: new Date(payload.timestamp).toISOString(),
|
|
111
|
+
isDM: true
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (payload.event === "conversation_started") await this.sendMessage(payload.user?.id, `🦅 Hi ${payload.user?.name || "there"}! I'm HyperClaw.`);
|
|
115
|
+
}
|
|
116
|
+
async checkDMPolicy(from, text) {
|
|
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(from)) return true;
|
|
121
|
+
await this.sendMessage(from, "🦅 HyperClaw: Not on allowlist.");
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (this.config.dmPolicy === "pairing") {
|
|
125
|
+
if (this.config.approvedPairings.includes(from)) return true;
|
|
126
|
+
const upper = text.trim().toUpperCase();
|
|
127
|
+
if (this.config.pendingPairings[upper]) {
|
|
128
|
+
this.config.approvedPairings.push(from);
|
|
129
|
+
delete this.config.pendingPairings[upper];
|
|
130
|
+
await this.saveState();
|
|
131
|
+
await this.sendMessage(from, "🦅 Paired!");
|
|
132
|
+
this.emit("pairing:approved", {
|
|
133
|
+
userId: from,
|
|
134
|
+
channelId: "viber"
|
|
135
|
+
});
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
139
|
+
this.config.pendingPairings[code] = from;
|
|
140
|
+
await this.saveState();
|
|
141
|
+
await this.sendMessage(from, `🦅 Pairing code: ${code}\nApprove: hyperclaw pairing approve viber ${code}`);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
async sendMessage(userId, text) {
|
|
147
|
+
await viberReq(this.config.authToken, "/send_message", {
|
|
148
|
+
receiver: userId,
|
|
149
|
+
type: "text",
|
|
150
|
+
sender: { name: this.config.botName },
|
|
151
|
+
text: text.slice(0, 7e3)
|
|
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.p) this.config.pendingPairings = s.p;
|
|
166
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
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
|
+
p: this.config.pendingPairings,
|
|
173
|
+
a: this.config.approvedPairings
|
|
174
|
+
}, { spaces: 2 });
|
|
175
|
+
}
|
|
176
|
+
isRunning() {
|
|
177
|
+
return this.running;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
exports.ViberConnector = ViberConnector;
|
|
@@ -0,0 +1,419 @@
|
|
|
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 https = require_chunk.__toESM(require("https"));
|
|
8
|
+
const events = require_chunk.__toESM(require("events"));
|
|
9
|
+
|
|
10
|
+
//#region extensions/discord/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "discord-state.json");
|
|
12
|
+
const OPC = {
|
|
13
|
+
DISPATCH: 0,
|
|
14
|
+
HEARTBEAT: 1,
|
|
15
|
+
IDENTIFY: 2,
|
|
16
|
+
RESUME: 6,
|
|
17
|
+
RECONNECT: 7,
|
|
18
|
+
INVALID_SESSION: 9,
|
|
19
|
+
HELLO: 10,
|
|
20
|
+
HEARTBEAT_ACK: 11
|
|
21
|
+
};
|
|
22
|
+
const INTENTS = {
|
|
23
|
+
GUILDS: 1,
|
|
24
|
+
GUILD_MESSAGES: 512,
|
|
25
|
+
GUILD_MESSAGE_CONTENT: 32768,
|
|
26
|
+
DIRECT_MESSAGES: 4096,
|
|
27
|
+
DIRECT_MESSAGE_CONTENT: 16384
|
|
28
|
+
};
|
|
29
|
+
function discordRest(token, method, endpoint, body) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const payload = body ? JSON.stringify(body) : null;
|
|
32
|
+
const req = https.default.request({
|
|
33
|
+
hostname: "discord.com",
|
|
34
|
+
port: 443,
|
|
35
|
+
path: `/api/v10${endpoint}`,
|
|
36
|
+
method,
|
|
37
|
+
headers: {
|
|
38
|
+
"Authorization": `Bot ${token}`,
|
|
39
|
+
"User-Agent": "HyperClaw/5.0.0 (https://hyperclaw.ai)",
|
|
40
|
+
...payload ? {
|
|
41
|
+
"Content-Type": "application/json",
|
|
42
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
43
|
+
} : {}
|
|
44
|
+
}
|
|
45
|
+
}, (res) => {
|
|
46
|
+
let data = "";
|
|
47
|
+
res.on("data", (c) => data += c);
|
|
48
|
+
res.on("end", () => {
|
|
49
|
+
if (res.statusCode === 204) return resolve(null);
|
|
50
|
+
try {
|
|
51
|
+
const r = JSON.parse(data);
|
|
52
|
+
if (res.statusCode && res.statusCode >= 400) reject(new Error(`Discord API ${res.statusCode}: ${r.message || data}`));
|
|
53
|
+
else resolve(r);
|
|
54
|
+
} catch {
|
|
55
|
+
reject(new Error(`Invalid JSON (${res.statusCode})`));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
req.on("error", reject);
|
|
60
|
+
if (payload) req.write(payload);
|
|
61
|
+
req.end();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
var DiscordConnector = class extends events.EventEmitter {
|
|
65
|
+
token;
|
|
66
|
+
config;
|
|
67
|
+
ws = null;
|
|
68
|
+
heartbeatInterval = null;
|
|
69
|
+
lastSequence = null;
|
|
70
|
+
sessionId = null;
|
|
71
|
+
resumeGatewayUrl = null;
|
|
72
|
+
running = false;
|
|
73
|
+
botUser = null;
|
|
74
|
+
constructor(token, config) {
|
|
75
|
+
super();
|
|
76
|
+
this.token = token;
|
|
77
|
+
this.config = {
|
|
78
|
+
token,
|
|
79
|
+
dmPolicy: "allowlist",
|
|
80
|
+
allowFrom: [],
|
|
81
|
+
approvedPairings: [],
|
|
82
|
+
pendingPairings: {},
|
|
83
|
+
listenGuildIds: [],
|
|
84
|
+
commandPrefix: "!",
|
|
85
|
+
...config
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
async connect() {
|
|
89
|
+
const gateway = await discordRest(this.token, "GET", "/gateway/bot");
|
|
90
|
+
const wsUrl = (gateway.url || "wss://gateway.discord.gg") + "/?v=10&encoding=json";
|
|
91
|
+
this.botUser = await discordRest(this.token, "GET", "/users/@me");
|
|
92
|
+
console.log(chalk.default.green(` 🦅 Discord: @${this.botUser?.username} connected`));
|
|
93
|
+
await this.loadState();
|
|
94
|
+
this.running = true;
|
|
95
|
+
await this.openWebSocket(wsUrl);
|
|
96
|
+
}
|
|
97
|
+
async disconnect() {
|
|
98
|
+
this.running = false;
|
|
99
|
+
if (this.heartbeatInterval) clearInterval(this.heartbeatInterval);
|
|
100
|
+
this.ws?.close(1e3);
|
|
101
|
+
await this.saveState();
|
|
102
|
+
}
|
|
103
|
+
async openWebSocket(url) {
|
|
104
|
+
this.ws = new ws.WebSocket(url, { headers: { "User-Agent": "HyperClaw/5.0.0" } });
|
|
105
|
+
this.ws.on("message", async (data) => {
|
|
106
|
+
const payload = JSON.parse(data.toString());
|
|
107
|
+
await this.handlePayload(payload);
|
|
108
|
+
});
|
|
109
|
+
this.ws.on("close", (code) => {
|
|
110
|
+
if (this.heartbeatInterval) clearInterval(this.heartbeatInterval);
|
|
111
|
+
if (!this.running) return;
|
|
112
|
+
const resumable = ![
|
|
113
|
+
1e3,
|
|
114
|
+
4004,
|
|
115
|
+
4010,
|
|
116
|
+
4011,
|
|
117
|
+
4012,
|
|
118
|
+
4013,
|
|
119
|
+
4014
|
|
120
|
+
].includes(code);
|
|
121
|
+
console.log(chalk.default.yellow(` ⚠ Discord WS closed (${code}) — ${resumable ? "resuming" : "reconnecting"} in 5s`));
|
|
122
|
+
setTimeout(() => {
|
|
123
|
+
if (this.running) {
|
|
124
|
+
const url$1 = resumable && this.resumeGatewayUrl ? this.resumeGatewayUrl + "/?v=10&encoding=json" : "wss://gateway.discord.gg/?v=10&encoding=json";
|
|
125
|
+
this.openWebSocket(url$1);
|
|
126
|
+
}
|
|
127
|
+
}, 5e3);
|
|
128
|
+
});
|
|
129
|
+
this.ws.on("error", (e) => console.log(chalk.default.yellow(` ⚠ Discord WS error: ${e.message}`)));
|
|
130
|
+
}
|
|
131
|
+
async handlePayload(payload) {
|
|
132
|
+
const { op, d, s, t } = payload;
|
|
133
|
+
if (s !== null && s !== void 0) this.lastSequence = s;
|
|
134
|
+
switch (op) {
|
|
135
|
+
case OPC.HELLO:
|
|
136
|
+
this.startHeartbeat(d.heartbeat_interval);
|
|
137
|
+
if (this.sessionId && this.lastSequence) this.resume();
|
|
138
|
+
else this.identify();
|
|
139
|
+
break;
|
|
140
|
+
case OPC.HEARTBEAT_ACK: break;
|
|
141
|
+
case OPC.HEARTBEAT:
|
|
142
|
+
this.sendWs({
|
|
143
|
+
op: OPC.HEARTBEAT,
|
|
144
|
+
d: this.lastSequence
|
|
145
|
+
});
|
|
146
|
+
break;
|
|
147
|
+
case OPC.RECONNECT:
|
|
148
|
+
this.ws?.close(4e3);
|
|
149
|
+
break;
|
|
150
|
+
case OPC.INVALID_SESSION:
|
|
151
|
+
if (d) setTimeout(() => this.resume(), 2e3);
|
|
152
|
+
else {
|
|
153
|
+
this.sessionId = null;
|
|
154
|
+
this.lastSequence = null;
|
|
155
|
+
setTimeout(() => this.identify(), 2e3);
|
|
156
|
+
}
|
|
157
|
+
break;
|
|
158
|
+
case OPC.DISPATCH:
|
|
159
|
+
await this.handleEvent(t, d);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
identify() {
|
|
164
|
+
const intents = INTENTS.GUILDS | INTENTS.GUILD_MESSAGES | INTENTS.GUILD_MESSAGE_CONTENT | INTENTS.DIRECT_MESSAGES;
|
|
165
|
+
this.sendWs({
|
|
166
|
+
op: OPC.IDENTIFY,
|
|
167
|
+
d: {
|
|
168
|
+
token: this.token,
|
|
169
|
+
intents,
|
|
170
|
+
properties: {
|
|
171
|
+
os: process.platform,
|
|
172
|
+
browser: "HyperClaw",
|
|
173
|
+
device: "HyperClaw"
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
resume() {
|
|
179
|
+
this.sendWs({
|
|
180
|
+
op: OPC.RESUME,
|
|
181
|
+
d: {
|
|
182
|
+
token: this.token,
|
|
183
|
+
session_id: this.sessionId,
|
|
184
|
+
seq: this.lastSequence
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
startHeartbeat(interval) {
|
|
189
|
+
if (this.heartbeatInterval) clearInterval(this.heartbeatInterval);
|
|
190
|
+
this.heartbeatInterval = setInterval(() => {
|
|
191
|
+
this.sendWs({
|
|
192
|
+
op: OPC.HEARTBEAT,
|
|
193
|
+
d: this.lastSequence
|
|
194
|
+
});
|
|
195
|
+
}, interval);
|
|
196
|
+
}
|
|
197
|
+
sendWs(payload) {
|
|
198
|
+
if (this.ws?.readyState === ws.WebSocket.OPEN) this.ws.send(JSON.stringify(payload));
|
|
199
|
+
}
|
|
200
|
+
async handleEvent(type, data) {
|
|
201
|
+
switch (type) {
|
|
202
|
+
case "READY":
|
|
203
|
+
this.sessionId = data.session_id;
|
|
204
|
+
this.resumeGatewayUrl = data.resume_gateway_url;
|
|
205
|
+
this.botUser = data.user;
|
|
206
|
+
await this.saveState();
|
|
207
|
+
if (this.config.slashCommands !== false) this.registerSlashCommands().catch((e) => console.log(chalk.default.yellow(` Discord slash commands: ${e.message}`)));
|
|
208
|
+
this.emit("ready", data.user);
|
|
209
|
+
break;
|
|
210
|
+
case "RESUMED":
|
|
211
|
+
console.log(chalk.default.gray(" Discord session resumed"));
|
|
212
|
+
break;
|
|
213
|
+
case "INTERACTION_CREATE":
|
|
214
|
+
await this.handleInteraction(data);
|
|
215
|
+
break;
|
|
216
|
+
case "MESSAGE_CREATE":
|
|
217
|
+
await this.handleMessage(data);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async handleMessage(msg) {
|
|
222
|
+
if (msg.author.id === this.botUser?.id) return;
|
|
223
|
+
if (msg.author.bot) return;
|
|
224
|
+
const isDM = !msg.channel_id.startsWith("0") && await this.isDirectMessage(msg.channel_id);
|
|
225
|
+
const userId = msg.author.id;
|
|
226
|
+
if (isDM) {
|
|
227
|
+
const allowed = await this.checkDMPolicy(userId, msg.channel_id, msg.content);
|
|
228
|
+
if (!allowed) return;
|
|
229
|
+
} else {
|
|
230
|
+
const guildAllowed = await this.checkGuildPolicy(msg);
|
|
231
|
+
if (!guildAllowed) return;
|
|
232
|
+
}
|
|
233
|
+
this.emit("message", {
|
|
234
|
+
id: msg.id,
|
|
235
|
+
channelId: "discord",
|
|
236
|
+
from: userId,
|
|
237
|
+
fromUsername: msg.author.global_name || msg.author.username,
|
|
238
|
+
chatId: msg.channel_id,
|
|
239
|
+
text: msg.content,
|
|
240
|
+
timestamp: msg.timestamp,
|
|
241
|
+
isDM
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
dmChannelCache = /* @__PURE__ */ new Set();
|
|
245
|
+
async isDirectMessage(channelId) {
|
|
246
|
+
if (this.dmChannelCache.has(channelId)) return true;
|
|
247
|
+
try {
|
|
248
|
+
const channel = await discordRest(this.token, "GET", `/channels/${channelId}`);
|
|
249
|
+
if (channel.type === 1) {
|
|
250
|
+
this.dmChannelCache.add(channelId);
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
} catch {}
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
async checkGuildPolicy(msg) {
|
|
257
|
+
const channelId = msg.channel_id;
|
|
258
|
+
let guildId;
|
|
259
|
+
try {
|
|
260
|
+
const ch = await discordRest(this.token, "GET", `/channels/${channelId}`);
|
|
261
|
+
guildId = ch.guild_id;
|
|
262
|
+
} catch {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
if (!guildId) return false;
|
|
266
|
+
if (this.config.listenGuildIds.length > 0 && !this.config.listenGuildIds.includes(guildId)) return false;
|
|
267
|
+
if (this.config.requireMentionInGuild !== false) {
|
|
268
|
+
const content = msg.content || "";
|
|
269
|
+
const mentioned = msg.mentions?.some((m) => m.id === this.botUser?.id) ?? false;
|
|
270
|
+
const botMention = this.botUser ? `<@${this.botUser.id}>` : "";
|
|
271
|
+
if (!mentioned && !content.includes(botMention) && !msg.referenced_message?.author?.bot) return false;
|
|
272
|
+
}
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
async checkDMPolicy(userId, channelId, text) {
|
|
276
|
+
if (this.config.dmPolicy === "none") return false;
|
|
277
|
+
if (this.config.dmPolicy === "open") return true;
|
|
278
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
279
|
+
if (this.config.allowFrom.includes(userId)) return true;
|
|
280
|
+
await this.sendMessage(channelId, "🦅 **HyperClaw**\n\nYou are not on the allowlist.");
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
if (this.config.dmPolicy === "pairing") {
|
|
284
|
+
const bridge = this.config.pairingBridge;
|
|
285
|
+
if (bridge) {
|
|
286
|
+
if (await bridge.isApproved(userId)) return true;
|
|
287
|
+
const upper$1 = text.trim().toUpperCase().replace(/\s/g, "");
|
|
288
|
+
if (upper$1.length >= 6 && upper$1.length <= 10) {
|
|
289
|
+
const verified = await bridge.verify(upper$1, userId);
|
|
290
|
+
if (verified) {
|
|
291
|
+
await this.sendMessage(channelId, "🦅 **Paired!** You can now send messages.");
|
|
292
|
+
this.emit("pairing:approved", {
|
|
293
|
+
userId,
|
|
294
|
+
channelId: "discord"
|
|
295
|
+
});
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const code$1 = await bridge.createRequest(userId);
|
|
300
|
+
if (code$1) await this.sendMessage(channelId, `🦅 **HyperClaw Pairing**\n\nSend the owner this code:\n\`${code$1}\`\n\nApprove with:\n\`hyperclaw pairing approve discord ${code$1}\``);
|
|
301
|
+
else await this.sendMessage(channelId, "🦅 **HyperClaw Pairing**\n\nYou already have a pending request. Check with the owner to approve.");
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
if (this.config.approvedPairings.includes(userId)) return true;
|
|
305
|
+
const upper = text.trim().toUpperCase();
|
|
306
|
+
if (this.config.pendingPairings[upper]) {
|
|
307
|
+
this.config.approvedPairings.push(userId);
|
|
308
|
+
delete this.config.pendingPairings[upper];
|
|
309
|
+
await this.saveState();
|
|
310
|
+
await this.sendMessage(channelId, "🦅 **Paired!** You can now send messages.");
|
|
311
|
+
this.emit("pairing:approved", {
|
|
312
|
+
userId,
|
|
313
|
+
channelId: "discord"
|
|
314
|
+
});
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
const code = this.generateCode();
|
|
318
|
+
this.config.pendingPairings[code] = userId;
|
|
319
|
+
await this.saveState();
|
|
320
|
+
await this.sendMessage(channelId, `🦅 **HyperClaw Pairing**\n\nSend the owner this code:\n\`${code}\`\n\nApprove with:\n\`hyperclaw pairing approve discord ${code}\``);
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
async registerSlashCommands() {
|
|
326
|
+
const app = await discordRest(this.token, "GET", "/oauth2/applications/@me");
|
|
327
|
+
const appId = app.id;
|
|
328
|
+
if (!appId) return;
|
|
329
|
+
const commands = [{
|
|
330
|
+
name: "help",
|
|
331
|
+
description: "Show HyperClaw help and commands"
|
|
332
|
+
}, {
|
|
333
|
+
name: "status",
|
|
334
|
+
description: "Show gateway status"
|
|
335
|
+
}];
|
|
336
|
+
await discordRest(this.token, "PUT", `/applications/${appId}/commands`, commands);
|
|
337
|
+
}
|
|
338
|
+
async handleInteraction(interaction) {
|
|
339
|
+
const { id, token, type, data } = interaction;
|
|
340
|
+
if (type !== 2 || !data?.name) return;
|
|
341
|
+
const name = data.name;
|
|
342
|
+
let content = "";
|
|
343
|
+
if (name === "help") content = "🦅 **HyperClaw** — AI agent on Discord.\n\n**Commands:** `/help`, `/status`\n**Chat:** Just send a message (DM or @mention in servers).\n**Pairing:** DM the bot to get a pairing code, then run `hyperclaw pairing approve discord <CODE>` on the host.";
|
|
344
|
+
else if (name === "status") content = "🦅 **HyperClaw** — gateway connected. Send a message to chat with the agent.";
|
|
345
|
+
else return;
|
|
346
|
+
const payload = {
|
|
347
|
+
type: 4,
|
|
348
|
+
data: {
|
|
349
|
+
content,
|
|
350
|
+
flags: 64
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
await discordRest(this.token, "POST", `/interactions/${id}/${token}/callback`, payload);
|
|
354
|
+
}
|
|
355
|
+
async sendMessage(channelId, content) {
|
|
356
|
+
const chunks = content.match(/.{1,2000}/gs) || [content];
|
|
357
|
+
let last = null;
|
|
358
|
+
for (const chunk of chunks) last = await discordRest(this.token, "POST", `/channels/${channelId}/messages`, { content: chunk });
|
|
359
|
+
return last;
|
|
360
|
+
}
|
|
361
|
+
async sendTyping(channelId) {
|
|
362
|
+
await discordRest(this.token, "POST", `/channels/${channelId}/typing`).catch(() => {});
|
|
363
|
+
}
|
|
364
|
+
async createDMChannel(userId) {
|
|
365
|
+
const ch = await discordRest(this.token, "POST", "/users/@me/channels", { recipient_id: userId });
|
|
366
|
+
return ch.id;
|
|
367
|
+
}
|
|
368
|
+
async sendDM(userId, content) {
|
|
369
|
+
const channelId = await this.createDMChannel(userId);
|
|
370
|
+
await this.sendMessage(channelId, content);
|
|
371
|
+
}
|
|
372
|
+
generateCode() {
|
|
373
|
+
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
374
|
+
return Array.from({ length: 6 }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
|
|
375
|
+
}
|
|
376
|
+
approvePairing(code) {
|
|
377
|
+
const upper = code.toUpperCase();
|
|
378
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
379
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
380
|
+
delete this.config.pendingPairings[upper];
|
|
381
|
+
this.saveState();
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
addToAllowlist(userId) {
|
|
385
|
+
if (!this.config.allowFrom.includes(userId)) {
|
|
386
|
+
this.config.allowFrom.push(userId);
|
|
387
|
+
this.saveState();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async loadState() {
|
|
391
|
+
try {
|
|
392
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
393
|
+
this.sessionId = s.sessionId || null;
|
|
394
|
+
this.lastSequence = s.lastSequence || null;
|
|
395
|
+
this.resumeGatewayUrl = s.resumeGatewayUrl || null;
|
|
396
|
+
if (s.pendingPairings) this.config.pendingPairings = s.pendingPairings;
|
|
397
|
+
if (s.approvedPairings) this.config.approvedPairings = s.approvedPairings;
|
|
398
|
+
} catch {}
|
|
399
|
+
}
|
|
400
|
+
async saveState() {
|
|
401
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
402
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
403
|
+
sessionId: this.sessionId,
|
|
404
|
+
lastSequence: this.lastSequence,
|
|
405
|
+
resumeGatewayUrl: this.resumeGatewayUrl,
|
|
406
|
+
pendingPairings: this.config.pendingPairings,
|
|
407
|
+
approvedPairings: this.config.approvedPairings
|
|
408
|
+
}, { spaces: 2 });
|
|
409
|
+
}
|
|
410
|
+
isRunning() {
|
|
411
|
+
return this.running;
|
|
412
|
+
}
|
|
413
|
+
getBotUser() {
|
|
414
|
+
return this.botUser;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
//#endregion
|
|
419
|
+
exports.DiscordConnector = DiscordConnector;
|