hyperclaw 4.0.0 → 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 +53 -18
- 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-Bzig1R5W.js +149 -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 +343 -0
- 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-B-SJqXLn.js +95 -0
- package/dist/delivery-D5Z98EVq.js +95 -0
- package/dist/delivery-DCOXhXEO.js +5 -0
- package/dist/delivery-VgFeuu2J.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-DfMGowZC.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-3q20ZyHj.js +9 -0
- package/dist/onboard-Bd_wsYdi.js +4086 -0
- package/dist/onboard-CAN7x3me.js +3026 -0
- package/dist/onboard-DnegOHMh.js +3026 -0
- 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 +366 -1109
- package/dist/runner-Bu--_RXw.js +810 -0
- 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 +1047 -0
- package/dist/server-DU9POoWc.js +4 -0
- package/dist/server-RBqwE_GN.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-CbQBToY0.js +138 -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,286 @@
|
|
|
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 net = require_chunk.__toESM(require("net"));
|
|
7
|
+
const events = require_chunk.__toESM(require("events"));
|
|
8
|
+
const tls = require_chunk.__toESM(require("tls"));
|
|
9
|
+
|
|
10
|
+
//#region extensions/email/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "email-state.json");
|
|
12
|
+
var IMAPClient = class {
|
|
13
|
+
sock = null;
|
|
14
|
+
buffer = "";
|
|
15
|
+
tag = 0;
|
|
16
|
+
pendingCallbacks = /* @__PURE__ */ new Map();
|
|
17
|
+
async connect(host, port, username, password) {
|
|
18
|
+
await new Promise((resolve, reject) => {
|
|
19
|
+
this.sock = tls.default.connect({
|
|
20
|
+
host,
|
|
21
|
+
port,
|
|
22
|
+
rejectUnauthorized: true
|
|
23
|
+
}, () => resolve());
|
|
24
|
+
this.sock.on("error", reject);
|
|
25
|
+
this.sock.on("data", (chunk) => {
|
|
26
|
+
this.buffer += chunk.toString();
|
|
27
|
+
this.processBuffer();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
await this.waitForUntagged("OK");
|
|
31
|
+
await this.command(`LOGIN "${this.escapeQuoted(username)}" "${this.escapeQuoted(password)}"`);
|
|
32
|
+
}
|
|
33
|
+
disconnect() {
|
|
34
|
+
this.sock?.end();
|
|
35
|
+
this.sock = null;
|
|
36
|
+
}
|
|
37
|
+
async selectFolder(folder) {
|
|
38
|
+
const resp = await this.command(`SELECT "${folder}"`);
|
|
39
|
+
const existsMatch = resp.match(/\* (\d+) EXISTS/);
|
|
40
|
+
const unseenMatch = resp.match(/UNSEEN (\d+)/);
|
|
41
|
+
return {
|
|
42
|
+
exists: existsMatch ? parseInt(existsMatch[1]) : 0,
|
|
43
|
+
unseen: unseenMatch ? parseInt(unseenMatch[1]) : 0
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async searchUnseen() {
|
|
47
|
+
const resp = await this.command("SEARCH UNSEEN");
|
|
48
|
+
const match = resp.match(/\* SEARCH ([\d ]+)/);
|
|
49
|
+
if (!match || !match[1].trim()) return [];
|
|
50
|
+
return match[1].trim().split(" ").map(Number).filter(Boolean);
|
|
51
|
+
}
|
|
52
|
+
async fetchEmail(uid) {
|
|
53
|
+
const resp = await this.command(`FETCH ${uid} (RFC822.HEADER BODY[TEXT])`);
|
|
54
|
+
const fromMatch = resp.match(/^From:\s*(.+)$/im);
|
|
55
|
+
const subjectMatch = resp.match(/^Subject:\s*(.+)$/im);
|
|
56
|
+
const msgIdMatch = resp.match(/^Message-ID:\s*(.+)$/im);
|
|
57
|
+
const bodyMatch = resp.match(/BODY\[TEXT\] \{(\d+)\}([\s\S]+?)(?:\* \d+ FETCH|\w+ OK)/);
|
|
58
|
+
if (!fromMatch) return null;
|
|
59
|
+
const body = bodyMatch ? decodeQP(bodyMatch[2].trim()) : "";
|
|
60
|
+
return {
|
|
61
|
+
from: fromMatch[1].trim(),
|
|
62
|
+
subject: decodeHeader(subjectMatch ? subjectMatch[1].trim() : "(no subject)"),
|
|
63
|
+
body: body.slice(0, 4e3),
|
|
64
|
+
messageId: msgIdMatch ? msgIdMatch[1].trim() : ""
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async markSeen(uid) {
|
|
68
|
+
await this.command(`STORE ${uid} +FLAGS (\\Seen)`);
|
|
69
|
+
}
|
|
70
|
+
async command(cmd) {
|
|
71
|
+
const tag = `HC${++this.tag}`;
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
this.pendingCallbacks.set(tag, resolve);
|
|
74
|
+
this.sock?.write(`${tag} ${cmd}\r\n`);
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
this.pendingCallbacks.delete(tag);
|
|
77
|
+
reject(new Error(`IMAP timeout: ${tag} ${cmd.slice(0, 30)}`));
|
|
78
|
+
}, 15e3);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async waitForUntagged(keyword) {
|
|
82
|
+
await new Promise((resolve) => {
|
|
83
|
+
const check = () => {
|
|
84
|
+
if (this.buffer.includes(`* ${keyword}`) || this.buffer.includes(`OK ${keyword}`)) resolve();
|
|
85
|
+
else setTimeout(check, 100);
|
|
86
|
+
};
|
|
87
|
+
setTimeout(check, 100);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
processBuffer() {
|
|
91
|
+
const lines = this.buffer.split("\r\n");
|
|
92
|
+
this.buffer = lines.pop() || "";
|
|
93
|
+
for (const line of lines) {
|
|
94
|
+
const tagMatch = line.match(/^(HC\d+) (OK|NO|BAD)/);
|
|
95
|
+
if (tagMatch) {
|
|
96
|
+
const cb = this.pendingCallbacks.get(tagMatch[1]);
|
|
97
|
+
if (cb) {
|
|
98
|
+
this.pendingCallbacks.delete(tagMatch[1]);
|
|
99
|
+
cb(this.buffer + line);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
escapeQuoted(s) {
|
|
105
|
+
return s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var SMTPClient = class {
|
|
109
|
+
async send(config, to, subject, body) {
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
const sock = net.default.createConnection(config.smtpPort, config.smtpHost);
|
|
112
|
+
let buf = "";
|
|
113
|
+
let tlsSock = null;
|
|
114
|
+
let upgraded = false;
|
|
115
|
+
let authenticated = false;
|
|
116
|
+
const write = (s) => {
|
|
117
|
+
const target = tlsSock || sock;
|
|
118
|
+
target.write(s + "\r\n");
|
|
119
|
+
};
|
|
120
|
+
const onData = (chunk) => {
|
|
121
|
+
buf += chunk.toString();
|
|
122
|
+
const lines = buf.split("\r\n");
|
|
123
|
+
buf = lines.pop() || "";
|
|
124
|
+
for (const line of lines) {
|
|
125
|
+
const code = parseInt(line.slice(0, 3));
|
|
126
|
+
if (line.startsWith("220") && !upgraded) write(`EHLO hyperclaw.ai`);
|
|
127
|
+
else if (line.includes("250") && line.includes("STARTTLS") && !upgraded) write("STARTTLS");
|
|
128
|
+
else if (line.startsWith("220") && !upgraded) {
|
|
129
|
+
upgraded = true;
|
|
130
|
+
tlsSock = tls.default.connect({
|
|
131
|
+
socket: sock,
|
|
132
|
+
servername: config.smtpHost
|
|
133
|
+
});
|
|
134
|
+
tlsSock.on("data", onData);
|
|
135
|
+
tlsSock.on("error", reject);
|
|
136
|
+
write(`EHLO hyperclaw.ai`);
|
|
137
|
+
} else if (line.startsWith("250") && upgraded && !authenticated) write(`AUTH LOGIN`);
|
|
138
|
+
else if (line.startsWith("334") && !authenticated) {
|
|
139
|
+
const b64user = Buffer.from(config.username).toString("base64");
|
|
140
|
+
const b64pass = Buffer.from(config.password).toString("base64");
|
|
141
|
+
if (line.includes("VXNlcm5hbWU")) write(b64user);
|
|
142
|
+
else {
|
|
143
|
+
write(b64pass);
|
|
144
|
+
authenticated = true;
|
|
145
|
+
}
|
|
146
|
+
} else if (line.startsWith("235")) write(`MAIL FROM:<${config.username}>`);
|
|
147
|
+
else if (line.startsWith("250") && authenticated) if (buf.includes("RCPT") || !buf) write(`RCPT TO:<${to}>`);
|
|
148
|
+
else write(`DATA`);
|
|
149
|
+
else if (line.startsWith("354")) {
|
|
150
|
+
const date = (/* @__PURE__ */ new Date()).toUTCString();
|
|
151
|
+
const msg = `From: ${config.username}\r\nTo: ${to}\r\nSubject: ${subject}\r\nDate: ${date}\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n` + body + "\r\n.";
|
|
152
|
+
write(msg);
|
|
153
|
+
} else if (line.startsWith("250") && buf.includes(".")) write("QUIT");
|
|
154
|
+
else if (line.startsWith("221")) {
|
|
155
|
+
sock.destroy();
|
|
156
|
+
resolve();
|
|
157
|
+
} else if (code >= 400) {
|
|
158
|
+
sock.destroy();
|
|
159
|
+
reject(new Error(`SMTP error: ${line}`));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
sock.on("data", onData);
|
|
164
|
+
sock.on("error", reject);
|
|
165
|
+
sock.setTimeout(15e3, () => {
|
|
166
|
+
sock.destroy();
|
|
167
|
+
reject(new Error("SMTP timeout"));
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function decodeQP(text) {
|
|
173
|
+
return text.replace(/=\r\n/g, "").replace(/=([0-9A-F]{2})/gi, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
174
|
+
}
|
|
175
|
+
function decodeHeader(text) {
|
|
176
|
+
return text.replace(/=\?UTF-8\?[BQ]\?([^?]+)\?=/gi, (_, enc) => {
|
|
177
|
+
try {
|
|
178
|
+
return Buffer.from(enc, "base64").toString("utf8");
|
|
179
|
+
} catch {
|
|
180
|
+
return enc;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function extractEmail(from) {
|
|
185
|
+
const match = from.match(/<([^>]+)>/) || from.match(/[\w.+-]+@[\w-]+\.[a-z]{2,}/i);
|
|
186
|
+
return match ? match[1] || match[0] : from;
|
|
187
|
+
}
|
|
188
|
+
var EmailConnector = class extends events.EventEmitter {
|
|
189
|
+
config;
|
|
190
|
+
pollTimer = null;
|
|
191
|
+
running = false;
|
|
192
|
+
lastSeenUid = 0;
|
|
193
|
+
smtp = new SMTPClient();
|
|
194
|
+
constructor(config) {
|
|
195
|
+
super();
|
|
196
|
+
this.config = {
|
|
197
|
+
imapPort: 993,
|
|
198
|
+
smtpPort: 587,
|
|
199
|
+
pollIntervalMs: 3e4,
|
|
200
|
+
inboxFolder: "INBOX",
|
|
201
|
+
markAsRead: true,
|
|
202
|
+
subjectPrefix: "",
|
|
203
|
+
fromAllowlist: [],
|
|
204
|
+
...config
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
async connect() {
|
|
208
|
+
await this.loadState();
|
|
209
|
+
const imap = new IMAPClient();
|
|
210
|
+
await imap.connect(this.config.imapHost, this.config.imapPort, this.config.username, this.config.password);
|
|
211
|
+
const { exists } = await imap.selectFolder(this.config.inboxFolder);
|
|
212
|
+
imap.disconnect();
|
|
213
|
+
console.log(chalk.default.green(` 🦅 Email: ${this.config.username} — ${exists} messages in inbox`));
|
|
214
|
+
this.running = true;
|
|
215
|
+
this.emit("connected", { username: this.config.username });
|
|
216
|
+
this.startPolling();
|
|
217
|
+
}
|
|
218
|
+
disconnect() {
|
|
219
|
+
this.running = false;
|
|
220
|
+
if (this.pollTimer) clearInterval(this.pollTimer);
|
|
221
|
+
}
|
|
222
|
+
startPolling() {
|
|
223
|
+
this.poll();
|
|
224
|
+
this.pollTimer = setInterval(() => this.poll(), this.config.pollIntervalMs);
|
|
225
|
+
}
|
|
226
|
+
/** Trigger immediate poll (e.g. from Gmail Pub/Sub webhook) */
|
|
227
|
+
triggerPoll() {
|
|
228
|
+
if (this.running) this.poll();
|
|
229
|
+
}
|
|
230
|
+
async poll() {
|
|
231
|
+
if (!this.running) return;
|
|
232
|
+
const imap = new IMAPClient();
|
|
233
|
+
try {
|
|
234
|
+
await imap.connect(this.config.imapHost, this.config.imapPort, this.config.username, this.config.password);
|
|
235
|
+
await imap.selectFolder(this.config.inboxFolder);
|
|
236
|
+
const unseenUids = await imap.searchUnseen();
|
|
237
|
+
const newUids = unseenUids.filter((uid) => uid > this.lastSeenUid);
|
|
238
|
+
for (const uid of newUids) {
|
|
239
|
+
const email = await imap.fetchEmail(uid);
|
|
240
|
+
if (!email) continue;
|
|
241
|
+
const fromAddr = extractEmail(email.from);
|
|
242
|
+
if (this.config.subjectPrefix && !email.subject.startsWith(this.config.subjectPrefix)) continue;
|
|
243
|
+
if (this.config.fromAllowlist.length > 0 && !this.config.fromAllowlist.includes(fromAddr)) continue;
|
|
244
|
+
if (this.config.markAsRead) await imap.markSeen(uid);
|
|
245
|
+
this.lastSeenUid = Math.max(this.lastSeenUid, uid);
|
|
246
|
+
await this.saveState();
|
|
247
|
+
this.emit("message", {
|
|
248
|
+
id: email.messageId || String(uid),
|
|
249
|
+
channelId: "email",
|
|
250
|
+
from: fromAddr,
|
|
251
|
+
chatId: fromAddr,
|
|
252
|
+
text: `Subject: ${email.subject}\n\n${email.body}`,
|
|
253
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
254
|
+
isDM: true
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
} catch (err) {
|
|
258
|
+
if (this.running) console.log(chalk.default.yellow(` ⚠ Email poll error: ${err.message}`));
|
|
259
|
+
} finally {
|
|
260
|
+
imap.disconnect();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async sendMessage(to, text, subject) {
|
|
264
|
+
await this.smtp.send(this.config, to, subject || `🦅 HyperClaw`, text);
|
|
265
|
+
}
|
|
266
|
+
async reply(to, originalSubject, text) {
|
|
267
|
+
const replySubject = originalSubject.startsWith("Re: ") ? originalSubject : `Re: ${originalSubject}`;
|
|
268
|
+
await this.sendMessage(to, text, replySubject);
|
|
269
|
+
}
|
|
270
|
+
async loadState() {
|
|
271
|
+
try {
|
|
272
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
273
|
+
this.lastSeenUid = s.lastSeenUid || 0;
|
|
274
|
+
} catch {}
|
|
275
|
+
}
|
|
276
|
+
async saveState() {
|
|
277
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
278
|
+
await fs_extra.default.writeJson(STATE_FILE, { lastSeenUid: this.lastSeenUid }, { spaces: 2 });
|
|
279
|
+
}
|
|
280
|
+
isRunning() {
|
|
281
|
+
return this.running;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
//#endregion
|
|
286
|
+
exports.EmailConnector = EmailConnector;
|
|
@@ -0,0 +1,163 @@
|
|
|
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/zalo/src/connector.ts
|
|
11
|
+
const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "zalo-state.json");
|
|
12
|
+
function zaloReq(token, method, endpoint, body) {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const payload = body ? JSON.stringify(body) : null;
|
|
15
|
+
const req = https.default.request({
|
|
16
|
+
hostname: "openapi.zalo.me",
|
|
17
|
+
port: 443,
|
|
18
|
+
path: `/v2.0/oa${endpoint}`,
|
|
19
|
+
method,
|
|
20
|
+
headers: {
|
|
21
|
+
"access_token": token,
|
|
22
|
+
...payload ? {
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
"Content-Length": Buffer.byteLength(payload)
|
|
25
|
+
} : {}
|
|
26
|
+
}
|
|
27
|
+
}, (res) => {
|
|
28
|
+
let data = "";
|
|
29
|
+
res.on("data", (c) => data += c);
|
|
30
|
+
res.on("end", () => {
|
|
31
|
+
try {
|
|
32
|
+
const r = JSON.parse(data);
|
|
33
|
+
if (r.error) reject(new Error(r.message));
|
|
34
|
+
else resolve(r);
|
|
35
|
+
} catch {
|
|
36
|
+
reject(new Error("Invalid JSON"));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
req.on("error", reject);
|
|
41
|
+
if (payload) req.write(payload);
|
|
42
|
+
req.end();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
var ZaloConnector = class extends events.EventEmitter {
|
|
46
|
+
config;
|
|
47
|
+
running = false;
|
|
48
|
+
constructor(config) {
|
|
49
|
+
super();
|
|
50
|
+
this.config = {
|
|
51
|
+
dmPolicy: "allowlist",
|
|
52
|
+
allowFrom: [],
|
|
53
|
+
approvedPairings: [],
|
|
54
|
+
pendingPairings: {},
|
|
55
|
+
...config
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async connect() {
|
|
59
|
+
const info = await zaloReq(this.config.oaAccessToken, "GET", "/getoa");
|
|
60
|
+
await this.loadState();
|
|
61
|
+
this.running = true;
|
|
62
|
+
console.log(chalk.default.green(` 🦅 Zalo OA: ${info.data?.name || this.config.appId} connected`));
|
|
63
|
+
this.emit("connected", { name: info.data?.name });
|
|
64
|
+
}
|
|
65
|
+
disconnect() {
|
|
66
|
+
this.running = false;
|
|
67
|
+
}
|
|
68
|
+
verifyWebhook(data, mac) {
|
|
69
|
+
const expected = crypto.default.createHmac("sha256", this.config.appSecret).update(data).digest("hex");
|
|
70
|
+
try {
|
|
71
|
+
return crypto.default.timingSafeEqual(Buffer.from(mac), Buffer.from(expected));
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async handleWebhook(body) {
|
|
77
|
+
let payload;
|
|
78
|
+
try {
|
|
79
|
+
payload = JSON.parse(body);
|
|
80
|
+
} catch {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (payload.event_name !== "user_send_text") return;
|
|
84
|
+
const from = payload.sender?.id;
|
|
85
|
+
const text = payload.message?.text;
|
|
86
|
+
if (!from || !text) return;
|
|
87
|
+
const allowed = await this.checkDMPolicy(from, text);
|
|
88
|
+
if (!allowed) return;
|
|
89
|
+
this.emit("message", {
|
|
90
|
+
id: payload.message?.msg_id,
|
|
91
|
+
channelId: "zalo",
|
|
92
|
+
from,
|
|
93
|
+
chatId: from,
|
|
94
|
+
text,
|
|
95
|
+
timestamp: new Date(payload.timestamp).toISOString(),
|
|
96
|
+
isDM: true
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async checkDMPolicy(from, text) {
|
|
100
|
+
if (this.config.dmPolicy === "none") return false;
|
|
101
|
+
if (this.config.dmPolicy === "open") return true;
|
|
102
|
+
if (this.config.dmPolicy === "allowlist") {
|
|
103
|
+
if (this.config.allowFrom.includes(from)) return true;
|
|
104
|
+
await this.sendMessage(from, "🦅 HyperClaw: Not on allowlist.");
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (this.config.dmPolicy === "pairing") {
|
|
108
|
+
if (this.config.approvedPairings.includes(from)) return true;
|
|
109
|
+
const upper = text.trim().toUpperCase();
|
|
110
|
+
if (this.config.pendingPairings[upper]) {
|
|
111
|
+
this.config.approvedPairings.push(from);
|
|
112
|
+
delete this.config.pendingPairings[upper];
|
|
113
|
+
await this.saveState();
|
|
114
|
+
await this.sendMessage(from, "🦅 Paired!");
|
|
115
|
+
this.emit("pairing:approved", {
|
|
116
|
+
userId: from,
|
|
117
|
+
channelId: "zalo"
|
|
118
|
+
});
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
|
|
122
|
+
this.config.pendingPairings[code] = from;
|
|
123
|
+
await this.saveState();
|
|
124
|
+
await this.sendMessage(from, `🦅 Mã ghép đôi: ${code}\nXác nhận: hyperclaw pairing approve zalo ${code}`);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
async sendMessage(userId, text) {
|
|
130
|
+
await zaloReq(this.config.oaAccessToken, "POST", "/message/cs", {
|
|
131
|
+
recipient: { user_id: userId },
|
|
132
|
+
message: { text: text.slice(0, 2e3) }
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
approvePairing(code) {
|
|
136
|
+
const upper = code.toUpperCase();
|
|
137
|
+
if (!this.config.pendingPairings[upper]) return false;
|
|
138
|
+
this.config.approvedPairings.push(this.config.pendingPairings[upper]);
|
|
139
|
+
delete this.config.pendingPairings[upper];
|
|
140
|
+
this.saveState();
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
async loadState() {
|
|
144
|
+
try {
|
|
145
|
+
const s = await fs_extra.default.readJson(STATE_FILE);
|
|
146
|
+
if (s.p) this.config.pendingPairings = s.p;
|
|
147
|
+
if (s.a) this.config.approvedPairings = s.a;
|
|
148
|
+
} catch {}
|
|
149
|
+
}
|
|
150
|
+
async saveState() {
|
|
151
|
+
await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
|
|
152
|
+
await fs_extra.default.writeJson(STATE_FILE, {
|
|
153
|
+
p: this.config.pendingPairings,
|
|
154
|
+
a: this.config.approvedPairings
|
|
155
|
+
}, { spaces: 2 });
|
|
156
|
+
}
|
|
157
|
+
isRunning() {
|
|
158
|
+
return this.running;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
//#endregion
|
|
163
|
+
exports.ZaloConnector = ZaloConnector;
|