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.
Files changed (199) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +449 -99
  3. package/dist/a2ui-protocol-Gzm29Gaw.js +75 -0
  4. package/dist/agents-routing-Biy5ew4a.js +4 -0
  5. package/dist/agents-routing-CL3HQNoM.js +327 -0
  6. package/dist/api-keys-guide-CGn5BSF7.js +149 -0
  7. package/dist/api-keys-guide-ChbThbPj.js +149 -0
  8. package/dist/audit-BJohI_vC.js +441 -0
  9. package/dist/audit-NPIMmOSq.js +441 -0
  10. package/dist/bounty-tools-BUqUKjt0.js +211 -0
  11. package/dist/bounty-tools-CY_i91DU.js +211 -0
  12. package/dist/browser-tools-CxJY6pAn.js +5 -0
  13. package/dist/browser-tools-JZ9ji6AW.js +179 -0
  14. package/dist/chat-qVuqhlPu.js +258 -0
  15. package/dist/claw-tasks-B-8RRMdq.js +80 -0
  16. package/dist/claw-tasks-Cyzdbhz_.js +80 -0
  17. package/dist/connector-1x1rCBHz.js +162 -0
  18. package/dist/connector-B4jeCULG.js +305 -0
  19. package/dist/connector-B7qngfkT.js +286 -0
  20. package/dist/connector-B8BK0GBo.js +531 -0
  21. package/dist/connector-BE9eJs8-.js +182 -0
  22. package/dist/connector-BEe-DTGQ.js +189 -0
  23. package/dist/connector-BU7p5ZgB.js +167 -0
  24. package/dist/connector-BUzzq7Ij.js +568 -0
  25. package/dist/connector-BpDqLgnW.js +419 -0
  26. package/dist/connector-BpW88ut2.js +189 -0
  27. package/dist/connector-Bxv-gy8U.js +167 -0
  28. package/dist/connector-Bz14zcJv.js +213 -0
  29. package/dist/connector-C1zP5-5q.js +85 -0
  30. package/dist/connector-CAcpcovF.js +498 -0
  31. package/dist/connector-CJgVjS58.js +181 -0
  32. package/dist/connector-Cf53D6qV.js +425 -0
  33. package/dist/connector-CyHmlbNz.js +508 -0
  34. package/dist/connector-D22mJGVu.js +340 -0
  35. package/dist/connector-D6RtMmlL.js +225 -0
  36. package/dist/connector-D9EnT8A4.js +280 -0
  37. package/dist/connector-DNDwIh37.js +239 -0
  38. package/dist/connector-Di27MeO4.js +350 -0
  39. package/dist/connector-Do0BPiHt.js +194 -0
  40. package/dist/connector-DvLwOfJy.js +192 -0
  41. package/dist/connector-DvU83NSq.js +181 -0
  42. package/dist/connector-DxskpDc_.js +173 -0
  43. package/dist/connector-byy3eISx.js +552 -0
  44. package/dist/connector-vV89hsyd.js +218 -0
  45. package/dist/cost-tracker-Ca1UPZ33.js +103 -0
  46. package/dist/cost-tracker-fnaj_6M9.js +103 -0
  47. package/dist/credentials-store-BxijEirw.js +77 -0
  48. package/dist/credentials-store-CA8UtK0T.js +77 -0
  49. package/dist/credentials-store-CPkVO6-z.js +4 -0
  50. package/dist/credentials-store-Cm7DH-kh.js +4 -0
  51. package/dist/cron-tasks-L0mz1yyU.js +82 -0
  52. package/dist/cron-tasks-_pqQCmxc.js +82 -0
  53. package/dist/daemon-7ViroziB.js +5 -0
  54. package/dist/daemon-BfyKmZhr.js +318 -0
  55. package/dist/daemon-CNyunwkR.js +5 -0
  56. package/dist/daemon-CindY8OK.js +318 -0
  57. package/dist/delivery-DVHmv1IR.js +4 -0
  58. package/dist/delivery-DgiZcJBp.js +4 -0
  59. package/dist/delivery-DpMX0Yyc.js +95 -0
  60. package/dist/delivery-otAU4alM.js +95 -0
  61. package/dist/destructive-gate-CA0DtA5K.js +101 -0
  62. package/dist/destructive-gate-DZt71UZR.js +101 -0
  63. package/dist/developer-keys-Cnd1kswV.js +127 -0
  64. package/dist/developer-keys-DENo3ZA6.js +8 -0
  65. package/dist/doctor-Dgjoc3DG.js +230 -0
  66. package/dist/doctor-RwsOhtAl.js +6 -0
  67. package/dist/engine-B0kLfRL0.js +256 -0
  68. package/dist/engine-BJUpRUOv.js +7 -0
  69. package/dist/engine-D_VeoZHw.js +305 -0
  70. package/dist/engine-JjRnhlsE.js +7 -0
  71. package/dist/env-resolve-17ekEU6p.js +10 -0
  72. package/dist/env-resolve-BFJXWl94.js +115 -0
  73. package/dist/env-resolve-Z2XF6leB.js +115 -0
  74. package/dist/env-resolve-bDYssfih.js +10 -0
  75. package/dist/extraction-tools-DbxnxIco.js +5 -0
  76. package/dist/extraction-tools-Dg7AHS35.js +91 -0
  77. package/dist/form_data-CGAy4HE0.js +8657 -0
  78. package/dist/gmail-watch-setup-C3uSWznp.js +40 -0
  79. package/dist/health-DUjluWHQ.js +6 -0
  80. package/dist/health-DVfkpUQW.js +152 -0
  81. package/dist/heartbeat-engine-CrgL4mrP.js +83 -0
  82. package/dist/heartbeat-engine-Ut6pXBD6.js +83 -0
  83. package/dist/hub-9LaKnLjY.js +6 -0
  84. package/dist/hub-BO6bj8Yj.js +515 -0
  85. package/dist/hub-Bu52YZqW.js +6 -0
  86. package/dist/hub-CfwUz9YW.js +515 -0
  87. package/dist/hyperclawbot-BrcoYLOp.js +505 -0
  88. package/dist/hyperclawbot-CBiDSKsa.js +505 -0
  89. package/dist/inference-0mlFQqIm.js +922 -0
  90. package/dist/inference-DHR82Gh7.js +6 -0
  91. package/dist/inference-DhA8jpfH.js +2692 -0
  92. package/dist/inference-SzqFe_nk.js +6 -0
  93. package/dist/knowledge-graph-BrYpSgxW.js +131 -0
  94. package/dist/knowledge-graph-DE5lSF02.js +131 -0
  95. package/dist/loader-9JqY6Nlq.js +4 -0
  96. package/dist/loader-BkDi8MD9.js +400 -0
  97. package/dist/loader-Cjdd1kw4.js +400 -0
  98. package/dist/loader-DI2qDRPC.js +4 -0
  99. package/dist/logger-Cp8wC7F8.js +83 -0
  100. package/dist/logger-DCT2l9GV.js +83 -0
  101. package/dist/manager-3cq3DydI.js +4 -0
  102. package/dist/manager-B2Gls5RG.js +218 -0
  103. package/dist/manager-BUrFrPuq.js +117 -0
  104. package/dist/manager-Bi9UYyVR.js +105 -0
  105. package/dist/manager-Biz9ixWJ.js +40 -0
  106. package/dist/manager-CBUHJiY7.js +6 -0
  107. package/dist/manager-CVLLaKmq.js +218 -0
  108. package/dist/manager-CWNSML5D.js +117 -0
  109. package/dist/manager-SJe9gt-q.js +4 -0
  110. package/dist/mcp-CUoTCMw-.js +139 -0
  111. package/dist/mcp-loader-BIz-450x.js +94 -0
  112. package/dist/mcp-loader-CvxRDtPC.js +94 -0
  113. package/dist/memory-OL77OMOr.js +270 -0
  114. package/dist/memory-auto-CpQHZlEJ.js +306 -0
  115. package/dist/memory-auto-D-L2q21G.js +306 -0
  116. package/dist/memory-auto-DTcy5VBy.js +5 -0
  117. package/dist/memory-auto-Z6LCf-iK.js +5 -0
  118. package/dist/memory-gUi4VaIf.js +4 -0
  119. package/dist/memory-integration-B8RSN4pr.js +91 -0
  120. package/dist/memory-integration-g2vxwgoE.js +91 -0
  121. package/dist/moltbook-B-40gQOL.js +81 -0
  122. package/dist/moltbook-Cl8cQfxJ.js +81 -0
  123. package/dist/node-TWxRm84k.js +222 -0
  124. package/dist/nodes-registry-C9dCFwjh.js +52 -0
  125. package/dist/nodes-registry-DKRtsbNg.js +52 -0
  126. package/dist/oauth-flow-CeaaGAz0.js +150 -0
  127. package/dist/oauth-flow-JCfporKq.js +150 -0
  128. package/dist/oauth-provider-4R0EJlsT.js +110 -0
  129. package/dist/oauth-provider-B4dzn56l.js +110 -0
  130. package/dist/observability-CDZmeHfa.js +89 -0
  131. package/dist/observability-nZ3CBIxG.js +89 -0
  132. package/dist/onboard-BBBWcfhp.js +10 -0
  133. package/dist/onboard-BVOtKQdh.js +3641 -0
  134. package/dist/onboard-Bw28IRQ3.js +4070 -0
  135. package/dist/onboard-CGNIw27w.js +11 -0
  136. package/dist/orchestrator-BovkM63z.js +6 -0
  137. package/dist/orchestrator-CcKx1Ovk.js +189 -0
  138. package/dist/orchestrator-DSbpkP1X.js +189 -0
  139. package/dist/orchestrator-DcFfDLTX.js +6 -0
  140. package/dist/osint-B4_m3VHQ.js +277 -0
  141. package/dist/osint-B6BZKQAD.js +277 -0
  142. package/dist/pairing-B6RArWhD.js +196 -0
  143. package/dist/pairing-BsQ08DLq.js +4 -0
  144. package/dist/pc-access-B0KocJNe.js +819 -0
  145. package/dist/pc-access-DkzmugZ7.js +8 -0
  146. package/dist/pending-approval-BgNjjuI2.js +22 -0
  147. package/dist/pending-approval-C_HkX1QL.js +22 -0
  148. package/dist/providers-DxiamZSL.js +5 -0
  149. package/dist/providers-Dy15rDb7.js +657 -0
  150. package/dist/reminders-store-CzUY0zYx.js +58 -0
  151. package/dist/renderer-ANNfXsHn.js +225 -0
  152. package/dist/rules-BSQwwAYC.js +103 -0
  153. package/dist/run-main.js +142 -132
  154. package/dist/runner-BHRSOPEU.js +1271 -0
  155. package/dist/runner-CJFJUtPm.js +1271 -0
  156. package/dist/sdk/index.js +2 -2
  157. package/dist/sdk/index.mjs +2 -2
  158. package/dist/security--oQObeJO.js +4 -0
  159. package/dist/security-wBOg0TA8.js +73 -0
  160. package/dist/server-Brl_HQUB.js +1255 -0
  161. package/dist/server-CbTTpB5m.js +1255 -0
  162. package/dist/server-DP_bPzvI.js +4 -0
  163. package/dist/server-DhfipkwN.js +4 -0
  164. package/dist/session-store-B09r5HgB.js +5 -0
  165. package/dist/session-store-DCTQIVur.js +113 -0
  166. package/dist/sessions-tools-BdlN6Pb6.js +95 -0
  167. package/dist/sessions-tools-JVLDKSJ_.js +5 -0
  168. package/dist/skill-loader-B5oeliGu.js +7 -0
  169. package/dist/skill-loader-Wf3brNOj.js +160 -0
  170. package/dist/skill-runtime-BGlvly2s.js +102 -0
  171. package/dist/skill-runtime-BXWd-Ktf.js +102 -0
  172. package/dist/skill-runtime-DhL2T76p.js +5 -0
  173. package/dist/skill-runtime-jgklm02e.js +5 -0
  174. package/dist/src-BbPa6Q8p.js +63 -0
  175. package/dist/src-BeXtfkK2.js +458 -0
  176. package/dist/src-Bhybpk1J.js +63 -0
  177. package/dist/src-CGQjRI4N.js +20 -0
  178. package/dist/src-DMJ4-uqk.js +458 -0
  179. package/dist/sub-agent-tools-CmE345s_.js +39 -0
  180. package/dist/sub-agent-tools-DHY-4WWM.js +39 -0
  181. package/dist/theme-D0smfC_l.js +8 -0
  182. package/dist/theme-DajRRZbA.js +180 -0
  183. package/dist/tool-policy-DZvF8xlQ.js +189 -0
  184. package/dist/tool-policy-DgNqFWYn.js +189 -0
  185. package/dist/tts-elevenlabs-C06nUxMK.js +61 -0
  186. package/dist/tts-elevenlabs-JeFaGNJU.js +61 -0
  187. package/dist/update-check-BVEqHhFY.js +83 -0
  188. package/dist/update-check-w4XuxVl7.js +81 -0
  189. package/dist/vision-JOtOS1Br.js +121 -0
  190. package/dist/vision-fky3elEo.js +121 -0
  191. package/dist/vision-tools-C8B3776g.js +5 -0
  192. package/dist/vision-tools-CB28ZCO_.js +5 -0
  193. package/dist/vision-tools-dwn9p4el.js +51 -0
  194. package/dist/vision-tools-vPPwQ-0N.js +51 -0
  195. package/dist/voice-transcription-B6RtplmN.js +138 -0
  196. package/dist/voice-transcription-DBo5hXmu.js +138 -0
  197. package/dist/website-watch-tools-B-jRAeTe.js +139 -0
  198. package/dist/website-watch-tools-BC9xAL67.js +5 -0
  199. package/package.json +1 -1
@@ -0,0 +1,189 @@
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/sms/src/connector.ts
11
+ const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "sms-state.json");
12
+ function twilioRequest(accountSid, authToken, method, endpoint, body) {
13
+ return new Promise((resolve, reject) => {
14
+ const auth = Buffer.from(`${accountSid}:${authToken}`).toString("base64");
15
+ const payload = body ? Object.entries(body).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&") : null;
16
+ const req = https.default.request({
17
+ hostname: "api.twilio.com",
18
+ port: 443,
19
+ path: `/2010-04-01/Accounts/${accountSid}${endpoint}.json`,
20
+ method,
21
+ headers: {
22
+ "Authorization": `Basic ${auth}`,
23
+ ...payload ? {
24
+ "Content-Type": "application/x-www-form-urlencoded",
25
+ "Content-Length": Buffer.byteLength(payload)
26
+ } : {}
27
+ }
28
+ }, (res) => {
29
+ let data = "";
30
+ res.on("data", (c) => data += c);
31
+ res.on("end", () => {
32
+ try {
33
+ const r = JSON.parse(data);
34
+ if (res.statusCode && res.statusCode >= 400) reject(new Error(r.message || `Twilio ${res.statusCode}`));
35
+ else resolve(r);
36
+ } catch {
37
+ reject(new Error("Invalid JSON"));
38
+ }
39
+ });
40
+ });
41
+ req.on("error", reject);
42
+ if (payload) req.write(payload);
43
+ req.end();
44
+ });
45
+ }
46
+ var SMSConnector = class extends events.EventEmitter {
47
+ config;
48
+ running = false;
49
+ constructor(config) {
50
+ super();
51
+ this.config = {
52
+ dmPolicy: "allowlist",
53
+ allowFrom: [],
54
+ approvedPairings: [],
55
+ pendingPairings: {},
56
+ ...config
57
+ };
58
+ }
59
+ async connect() {
60
+ await twilioRequest(this.config.accountSid, this.config.authToken, "GET", "");
61
+ await this.loadState();
62
+ this.running = true;
63
+ console.log(chalk.default.green(` 🦅 SMS: ${this.config.fromNumber} connected via Twilio`));
64
+ this.emit("connected", { number: this.config.fromNumber });
65
+ }
66
+ disconnect() {
67
+ this.running = false;
68
+ }
69
+ async handleWebhook(body, twilioSignature, webhookUrl) {
70
+ const params = Object.fromEntries(body.split("&").map((p) => {
71
+ const [k, v] = p.split("=");
72
+ return [decodeURIComponent(k), decodeURIComponent(v || "").replace(/\+/g, " ")];
73
+ }));
74
+ const from = params.From;
75
+ const text = params.Body;
76
+ const messageSid = params.MessageSid;
77
+ if (!from || !text) return;
78
+ if (twilioSignature && !this.verifySignature(body, twilioSignature, webhookUrl)) {
79
+ console.log(chalk.default.yellow(" ⚠ SMS: invalid Twilio signature"));
80
+ return;
81
+ }
82
+ const allowed = await this.checkDMPolicy(from, text);
83
+ if (!allowed) return;
84
+ this.emit("message", {
85
+ id: messageSid,
86
+ channelId: "sms",
87
+ from,
88
+ chatId: from,
89
+ text,
90
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
91
+ isDM: true
92
+ });
93
+ }
94
+ verifySignature(body, signature, url) {
95
+ const params = Object.fromEntries(body.split("&").map((p) => {
96
+ const [k, v] = p.split("=");
97
+ return [decodeURIComponent(k), decodeURIComponent(v || "").replace(/\+/g, " ")];
98
+ }));
99
+ const sortedParams = Object.keys(params).sort().map((k) => `${k}${params[k]}`).join("");
100
+ const expected = crypto.default.createHmac("sha1", this.config.authToken).update(url + sortedParams).digest("base64");
101
+ try {
102
+ return crypto.default.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
103
+ } catch {
104
+ return false;
105
+ }
106
+ }
107
+ async checkDMPolicy(from, text) {
108
+ if (this.config.dmPolicy === "none") return false;
109
+ if (this.config.dmPolicy === "open") return true;
110
+ if (this.config.dmPolicy === "allowlist") {
111
+ if (this.config.allowFrom.includes(from)) return true;
112
+ await this.sendMessage(from, "🦅 HyperClaw: You are not on the allowlist.");
113
+ return false;
114
+ }
115
+ if (this.config.dmPolicy === "pairing") {
116
+ if (this.config.approvedPairings.includes(from)) return true;
117
+ const upper = text.trim().toUpperCase();
118
+ if (this.config.pendingPairings[upper]) {
119
+ this.config.approvedPairings.push(from);
120
+ delete this.config.pendingPairings[upper];
121
+ await this.saveState();
122
+ await this.sendMessage(from, "🦅 HyperClaw: Paired! You can now send messages.");
123
+ this.emit("pairing:approved", {
124
+ userId: from,
125
+ channelId: "sms"
126
+ });
127
+ return true;
128
+ }
129
+ const code = this.generateCode();
130
+ this.config.pendingPairings[code] = from;
131
+ await this.saveState();
132
+ await this.sendMessage(from, `🦅 HyperClaw Pairing: send this code to the owner: ${code}\nThey can approve with: hyperclaw pairing approve sms ${code}`);
133
+ return false;
134
+ }
135
+ return false;
136
+ }
137
+ async sendMessage(to, text) {
138
+ await twilioRequest(this.config.accountSid, this.config.authToken, "POST", "/Messages", {
139
+ From: this.config.fromNumber,
140
+ To: to,
141
+ Body: text.slice(0, 1600)
142
+ });
143
+ }
144
+ async getStatus(messageSid) {
145
+ const r = await twilioRequest(this.config.accountSid, this.config.authToken, "GET", `/Messages/${messageSid}`);
146
+ return r.status;
147
+ }
148
+ async broadcast(numbers, text) {
149
+ for (const num of numbers) await this.sendMessage(num, text).catch((e) => console.log(chalk.default.yellow(` ⚠ SMS broadcast failed to ${num}: ${e.message}`)));
150
+ }
151
+ generateCode() {
152
+ const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
153
+ return Array.from({ length: 6 }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
154
+ }
155
+ approvePairing(code) {
156
+ const upper = code.toUpperCase();
157
+ if (!this.config.pendingPairings[upper]) return false;
158
+ this.config.approvedPairings.push(this.config.pendingPairings[upper]);
159
+ delete this.config.pendingPairings[upper];
160
+ this.saveState();
161
+ return true;
162
+ }
163
+ addToAllowlist(number) {
164
+ if (!this.config.allowFrom.includes(number)) {
165
+ this.config.allowFrom.push(number);
166
+ this.saveState();
167
+ }
168
+ }
169
+ async loadState() {
170
+ try {
171
+ const s = await fs_extra.default.readJson(STATE_FILE);
172
+ if (s.pendingPairings) this.config.pendingPairings = s.pendingPairings;
173
+ if (s.approvedPairings) this.config.approvedPairings = s.approvedPairings;
174
+ } catch {}
175
+ }
176
+ async saveState() {
177
+ await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
178
+ await fs_extra.default.writeJson(STATE_FILE, {
179
+ pendingPairings: this.config.pendingPairings,
180
+ approvedPairings: this.config.approvedPairings
181
+ }, { spaces: 2 });
182
+ }
183
+ isRunning() {
184
+ return this.running;
185
+ }
186
+ };
187
+
188
+ //#endregion
189
+ exports.SMSConnector = SMSConnector;
@@ -0,0 +1,167 @@
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/zalo-personal/src/connector.ts
10
+ const STATE_FILE = path.default.join(os.default.homedir(), ".hyperclaw", "zalo-personal-state.json");
11
+ function zaloWebReq(cookie, method, endpoint, body) {
12
+ return new Promise((resolve, reject) => {
13
+ const payload = body ? JSON.stringify(body) : null;
14
+ const req = https.default.request({
15
+ hostname: "chat.zalo.me",
16
+ port: 443,
17
+ path: `/api${endpoint}`,
18
+ method,
19
+ headers: {
20
+ "Cookie": cookie,
21
+ "Referer": "https://chat.zalo.me/",
22
+ "User-Agent": "Mozilla/5.0 (compatible; HyperClaw/4.0)",
23
+ ...payload ? {
24
+ "Content-Type": "application/json",
25
+ "Content-Length": Buffer.byteLength(payload)
26
+ } : {}
27
+ }
28
+ }, (res) => {
29
+ let data = "";
30
+ res.on("data", (c) => data += c);
31
+ res.on("end", () => {
32
+ try {
33
+ resolve(JSON.parse(data));
34
+ } catch {
35
+ resolve({ raw: data });
36
+ }
37
+ });
38
+ });
39
+ req.on("error", reject);
40
+ if (payload) req.write(payload);
41
+ req.end();
42
+ });
43
+ }
44
+ var ZaloPersonalConnector = class extends events.EventEmitter {
45
+ config;
46
+ running = false;
47
+ ws = null;
48
+ userId = "";
49
+ constructor(config) {
50
+ super();
51
+ this.config = {
52
+ imei: require("crypto").randomUUID(),
53
+ dmPolicy: "allowlist",
54
+ allowFrom: [],
55
+ approvedPairings: [],
56
+ pendingPairings: {},
57
+ ...config
58
+ };
59
+ }
60
+ async connect() {
61
+ const profile = await zaloWebReq(this.config.cookie, "GET", "/profile");
62
+ this.userId = profile?.data?.userId || profile?.userId || "";
63
+ await this.loadState();
64
+ this.running = true;
65
+ console.log(chalk.default.green(` 🦅 Zalo Personal: ${profile?.data?.displayName || this.userId} connected`));
66
+ this.emit("connected", { userId: this.userId });
67
+ this.startPolling();
68
+ }
69
+ async startPolling() {
70
+ while (this.running) try {
71
+ const msgs = await zaloWebReq(this.config.cookie, "GET", `/message/fetchlatestmsg?last_msg_id=0&limit=20`);
72
+ for (const msg of msgs?.data?.msgs || []) {
73
+ if (msg.fromUid === this.userId) continue;
74
+ if (!msg.msgId || !msg.content) continue;
75
+ const from = msg.fromUid;
76
+ const text = msg.content;
77
+ const allowed = await this.checkDMPolicy(from, text);
78
+ if (!allowed) continue;
79
+ this.emit("message", {
80
+ id: msg.msgId,
81
+ channelId: "zalo-personal",
82
+ from,
83
+ chatId: msg.toUid || from,
84
+ text,
85
+ timestamp: new Date(parseInt(msg.ts || "0")).toISOString(),
86
+ isDM: true
87
+ });
88
+ }
89
+ await new Promise((r) => setTimeout(r, 3e3));
90
+ } catch (e) {
91
+ if (this.running) {
92
+ console.log(chalk.default.yellow(` ⚠ Zalo Personal: ${e.message}`));
93
+ await new Promise((r) => setTimeout(r, 5e3));
94
+ }
95
+ }
96
+ }
97
+ async checkDMPolicy(from, text) {
98
+ if (this.config.dmPolicy === "none") return false;
99
+ if (this.config.dmPolicy === "open") return true;
100
+ if (this.config.dmPolicy === "allowlist") {
101
+ if (this.config.allowFrom.includes(from)) return true;
102
+ await this.sendMessage(from, "🦅 HyperClaw: Not on allowlist.");
103
+ return false;
104
+ }
105
+ if (this.config.dmPolicy === "pairing") {
106
+ if (this.config.approvedPairings.includes(from)) return true;
107
+ const upper = text.trim().toUpperCase();
108
+ if (this.config.pendingPairings[upper]) {
109
+ this.config.approvedPairings.push(from);
110
+ delete this.config.pendingPairings[upper];
111
+ await this.saveState();
112
+ await this.sendMessage(from, "🦅 Paired!");
113
+ this.emit("pairing:approved", {
114
+ userId: from,
115
+ channelId: "zalo-personal"
116
+ });
117
+ return true;
118
+ }
119
+ const code = Array.from({ length: 6 }, () => "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"[Math.floor(Math.random() * 32)]).join("");
120
+ this.config.pendingPairings[code] = from;
121
+ await this.saveState();
122
+ await this.sendMessage(from, `🦅 Mã ghép đôi: ${code}\nXác nhận: hyperclaw pairing approve zalo-personal ${code}`);
123
+ return false;
124
+ }
125
+ return false;
126
+ }
127
+ async sendMessage(toUid, text) {
128
+ await zaloWebReq(this.config.cookie, "POST", "/message/sendmsg", {
129
+ toid: toUid,
130
+ msg: text.slice(0, 2e3),
131
+ imei: this.config.imei,
132
+ type: 1
133
+ });
134
+ }
135
+ disconnect() {
136
+ this.running = false;
137
+ this.ws?.close();
138
+ }
139
+ approvePairing(code) {
140
+ const upper = code.toUpperCase();
141
+ if (!this.config.pendingPairings[upper]) return false;
142
+ this.config.approvedPairings.push(this.config.pendingPairings[upper]);
143
+ delete this.config.pendingPairings[upper];
144
+ this.saveState();
145
+ return true;
146
+ }
147
+ async loadState() {
148
+ try {
149
+ const s = await fs_extra.default.readJson(STATE_FILE);
150
+ if (s.p) this.config.pendingPairings = s.p;
151
+ if (s.a) this.config.approvedPairings = s.a;
152
+ } catch {}
153
+ }
154
+ async saveState() {
155
+ await fs_extra.default.ensureDir(path.default.dirname(STATE_FILE));
156
+ await fs_extra.default.writeJson(STATE_FILE, {
157
+ p: this.config.pendingPairings,
158
+ a: this.config.approvedPairings
159
+ }, { spaces: 2 });
160
+ }
161
+ isRunning() {
162
+ return this.running;
163
+ }
164
+ };
165
+
166
+ //#endregion
167
+ exports.ZaloPersonalConnector = ZaloPersonalConnector;